Blender V2.27

anim_movie.c

00001 
00032 #ifdef _WIN32
00033 #define INC_OLE2
00034 #include <windows.h>
00035 #include <windowsx.h>
00036 #include <mmsystem.h>
00037 #include <memory.h>
00038 #include <commdlg.h>
00039 
00040 #ifndef FREE_WINDOWS
00041 #include <vfw.h>
00042 #endif
00043 
00044 #undef AVIIF_KEYFRAME // redefined in AVI_avi.h
00045 #undef AVIIF_LIST // redefined in AVI_avi.h
00046 
00047 #define FIXCC(fcc)  if (fcc == 0)       fcc = mmioFOURCC('N', 'o', 'n', 'e'); \
00048                 if (fcc == BI_RLE8) fcc = mmioFOURCC('R', 'l', 'e', '8');
00049 #endif
00050 
00051 #include <sys/types.h>
00052 #include <ctype.h>
00053 #include <stdlib.h>
00054 #include <stdio.h>
00055 #ifndef _WIN32
00056 #include <dirent.h>
00057 #else
00058 #include <io.h>
00059 #endif
00060 
00061 #include "BLI_blenlib.h" /* BLI_remlink BLI_filesize BLI_addtail
00062                                                         BLI_countlist BLI_stringdec */
00063 #include "BLI_utildefines.h"
00064 
00065 #include "MEM_guardedalloc.h"
00066 
00067 #include "DNA_userdef_types.h"
00068 
00069 
00070 #include "BKE_global.h"
00071 #include "BKE_depsgraph.h"
00072 
00073 #include "imbuf.h"
00074 
00075 #include "AVI_avi.h"
00076 
00077 #ifdef WITH_QUICKTIME
00078 #if defined(_WIN32) || defined(__APPLE__)
00079 #include "quicktime_import.h"
00080 #endif /* _WIN32 || __APPLE__ */
00081 #endif /* WITH_QUICKTIME */
00082 
00083 #include "IMB_imbuf_types.h"
00084 #include "IMB_imbuf.h"
00085 
00086 #include "IMB_allocimbuf.h"
00087 #include "IMB_anim.h"
00088 
00089 #ifdef WITH_FFMPEG
00090 #include <libavformat/avformat.h>
00091 #include <libavcodec/avcodec.h>
00092 #include <libavutil/rational.h>
00093 #include <libswscale/swscale.h>
00094 
00095 #if LIBAVFORMAT_VERSION_INT < (49 << 16)
00096 #define FFMPEG_OLD_FRAME_RATE 1
00097 #else
00098 #define FFMPEG_CODEC_IS_POINTER 1
00099 #endif
00100 
00101 #if (LIBAVCODEC_VERSION_MAJOR >= 52) && (LIBAVCODEC_VERSION_MINOR >= 29) && \
00102          (LIBSWSCALE_VERSION_MAJOR >= 0) && (LIBSWSCALE_VERSION_MINOR >= 10)
00103 #define FFMPEG_SWSCALE_COLOR_SPACE_SUPPORT
00104 #endif
00105 
00106 #endif //WITH_FFMPEG
00107 
00108 #ifdef WITH_REDCODE
00109 #ifdef _WIN32 /* on windows we use the ones in extern instead */
00110 #include "libredcode/format.h"
00111 #include "libredcode/codec.h"
00112 #else
00113 #include "libredcode/format.h"
00114 #include "libredcode/codec.h"
00115 #endif
00116 #endif
00117 
00118 /****/
00119 
00120 #ifdef __sgi
00121 
00122 #include <dmedia/moviefile.h>
00123 
00124 static void movie_printerror(char * str) {
00125         const char * errstr = mvGetErrorStr(mvGetErrno());
00126 
00127         if (str) {
00128                 if (errstr) printf("%s: %s\n", str, errstr);
00129                 else printf("%s: returned error\n", str);
00130         } else printf("%s\n", errstr);
00131 }
00132 
00133 static int startmovie(struct anim * anim) {
00134         if (anim == 0) return(-1);
00135 
00136         if ( mvOpenFile (anim->name, O_BINARY|O_RDONLY, &anim->movie ) != DM_SUCCESS ) {
00137                 printf("Can't open movie: %s\n", anim->name);
00138                 return(-1);
00139         }
00140         if ( mvFindTrackByMedium (anim->movie, DM_IMAGE, &anim->track) != DM_SUCCESS ) {
00141                 printf("No image track in movie: %s\n", anim->name);
00142                 mvClose(anim->movie);
00143                 return(-1);
00144         }
00145 
00146         anim->duration = mvGetTrackLength (anim->track);
00147         anim->params = mvGetParams( anim->track );
00148 
00149         anim->x = dmParamsGetInt( anim->params, DM_IMAGE_WIDTH);
00150         anim->y = dmParamsGetInt( anim->params, DM_IMAGE_HEIGHT);
00151         anim->interlacing = dmParamsGetEnum (anim->params, DM_IMAGE_INTERLACING);
00152         anim->orientation = dmParamsGetEnum (anim->params, DM_IMAGE_ORIENTATION);
00153         anim->framesize = dmImageFrameSize(anim->params);
00154 
00155         anim->curposition = 0;
00156         anim->preseek = 0;
00157 
00158         /*printf("x:%d y:%d size:%d interl:%d dur:%d\n", anim->x, anim->y, anim->framesize, anim->interlacing, anim->duration);*/
00159         return (0);
00160 }
00161 
00162 static ImBuf * movie_fetchibuf(struct anim * anim, int position) {
00163         ImBuf * ibuf;
00164 /*      extern rectcpy(); */
00165         int size;
00166         unsigned int *rect1, *rect2;
00167 
00168         if (anim == 0) return (0);
00169 
00170         ibuf = IMB_allocImBuf(anim->x, anim->y, 24, IB_rect);
00171 
00172         if ( mvReadFrames(anim->track, position, 1, ibuf->x * ibuf->y * 
00173                 sizeof(int), ibuf->rect ) != DM_SUCCESS ) {
00174                 movie_printerror("mvReadFrames");
00175                 IMB_freeImBuf(ibuf);
00176                 return(0);
00177         }
00178 
00179 /*
00180         if (anim->interlacing == DM_IMAGE_INTERLACED_EVEN) {
00181                 rect1 = ibuf->rect + (ibuf->x * ibuf->y) - 1;
00182                 rect2 = rect1 - ibuf->x;
00183     
00184                 for (size = ibuf->x * (ibuf->y - 1); size > 0; size--){
00185                         *rect1-- = *rect2--;
00186                 }
00187         }
00188 */
00189 
00190         if (anim->interlacing == DM_IMAGE_INTERLACED_EVEN)
00191         {
00192                 rect1 = ibuf->rect;
00193                 rect2 = rect1 + ibuf->x;
00194 
00195                 for (size = ibuf->x * (ibuf->y - 1); size > 0; size--){
00196                         *rect1++ = *rect2++;
00197                 }
00198         }
00199         /*if (anim->orientation == DM_TOP_TO_BOTTOM) IMB_flipy(ibuf);*/
00200 
00201 
00202         return(ibuf);
00203 }
00204 
00205 static void free_anim_movie(struct anim * anim) {
00206         if (anim == NULL) return;
00207 
00208         if (anim->movie) {
00209                 mvClose(anim->movie);
00210                 anim->movie = NULL;
00211         }
00212         anim->duration = 0;
00213 }
00214 
00215 int ismovie(char *name) {
00216         return (mvIsMovieFile(name) == DM_TRUE);
00217 }
00218 
00219 #else
00220 
00221 int ismovie(const char *UNUSED(name)) {
00222         return 0;
00223 }
00224 
00225         /* never called, just keep the linker happy */
00226 static int startmovie(struct anim *UNUSED(anim)) { return 1; }
00227 static ImBuf * movie_fetchibuf(struct anim *UNUSED(anim), int UNUSED(position)) { return NULL; }
00228 static void free_anim_movie(struct anim *UNUSED(anim)) { ; }
00229 
00230 #endif
00231 
00232 #if defined(_WIN32)
00233 # define PATHSEPERATOR '\\'
00234 #else
00235 # define PATHSEPERATOR '/'
00236 #endif
00237 
00238 static int an_stringdec(const char *string, char* head, char *tail, unsigned short *numlen) {
00239         unsigned short len,nume,nums=0;
00240         short i,found=FALSE;
00241 
00242         len=strlen(string);
00243         nume = len;
00244 
00245         for(i=len-1;i>=0;i--){
00246                 if (string[i]==PATHSEPERATOR) break;
00247                 if (isdigit(string[i])) {
00248                         if (found){
00249                                 nums=i;
00250                         } else{
00251                                 nume=i;
00252                                 nums=i;
00253                                 found=TRUE;
00254                         }
00255                 } else{
00256                         if (found) break;
00257                 }
00258         }
00259         if (found){
00260                 strcpy(tail ,&string[nume+1]);
00261                 strcpy(head, string);
00262                 head[nums]= '\0';
00263                 *numlen=nume-nums+1;
00264                 return ((int)atoi(&(string[nums])));
00265         }
00266         tail[0]= '\0';
00267         strcpy(head, string);
00268         *numlen=0;
00269         return TRUE;
00270 }
00271 
00272 
00273 static void an_stringenc(char *string, const char *head, const char *tail, unsigned short numlen, int pic) {
00274         BLI_stringenc(string, head, tail, numlen, pic);
00275 }
00276 
00277 static void free_anim_avi (struct anim *anim) {
00278 #if defined(_WIN32) && !defined(FREE_WINDOWS)
00279         int i;
00280 #endif
00281 
00282         if (anim == NULL) return;
00283         if (anim->avi == NULL) return;
00284 
00285         AVI_close (anim->avi);
00286         MEM_freeN (anim->avi);
00287         anim->avi = NULL;
00288 
00289 #if defined(_WIN32) && !defined(FREE_WINDOWS)
00290 
00291         if (anim->pgf) {
00292                 AVIStreamGetFrameClose(anim->pgf);
00293                 anim->pgf = NULL;
00294         }
00295 
00296         for (i = 0; i < anim->avistreams; i++){
00297                 AVIStreamRelease(anim->pavi[i]);
00298         }
00299         anim->avistreams = 0;
00300 
00301         if (anim->pfileopen) {
00302                 AVIFileRelease(anim->pfile);
00303                 anim->pfileopen = 0;
00304                 AVIFileExit();
00305         }
00306 #endif
00307 
00308         anim->duration = 0;
00309 }
00310 
00311 void IMB_free_anim_ibuf(struct anim * anim) {
00312         if (anim == NULL) return;
00313 
00314         if (anim->ibuf1) IMB_freeImBuf(anim->ibuf1);
00315         if (anim->ibuf2) IMB_freeImBuf(anim->ibuf2);
00316 
00317         anim->ibuf1 = anim->ibuf2 = NULL;
00318 }
00319 
00320 #ifdef WITH_FFMPEG
00321 static void free_anim_ffmpeg(struct anim * anim);
00322 #endif
00323 #ifdef WITH_REDCODE
00324 static void free_anim_redcode(struct anim * anim);
00325 #endif
00326 
00327 void IMB_free_anim(struct anim * anim) {
00328         if (anim == NULL) {
00329                 printf("free anim, anim == NULL\n");
00330                 return;
00331         }
00332 
00333         IMB_free_anim_ibuf(anim);
00334         free_anim_movie(anim);
00335         free_anim_avi(anim);
00336 
00337 #ifdef WITH_QUICKTIME
00338         free_anim_quicktime(anim);
00339 #endif
00340 #ifdef WITH_FFMPEG
00341         free_anim_ffmpeg(anim);
00342 #endif
00343 #ifdef WITH_REDCODE
00344         free_anim_redcode(anim);
00345 #endif
00346 
00347         MEM_freeN(anim);
00348 }
00349 
00350 void IMB_close_anim(struct anim * anim) {
00351         if (anim == 0) return;
00352 
00353         IMB_free_anim(anim);
00354 }
00355 
00356 
00357 struct anim * IMB_open_anim( const char * name, int ib_flags) {
00358         struct anim * anim;
00359 
00360         anim = (struct anim*)MEM_callocN(sizeof(struct anim), "anim struct");
00361         if (anim != NULL) {
00362                 strcpy(anim->name, name);  /* fixme: possible buffer overflow here? */
00363                 anim->ib_flags = ib_flags;
00364         }
00365         return(anim);
00366 }
00367 
00368 
00369 static int startavi (struct anim *anim) {
00370 
00371         AviError avierror;
00372 #if defined(_WIN32) && !defined(FREE_WINDOWS)
00373         HRESULT hr;
00374         int i, firstvideo = -1;
00375         BYTE abFormat[1024];
00376         LONG l;
00377         LPBITMAPINFOHEADER lpbi;
00378         AVISTREAMINFO avis;
00379 #endif
00380 
00381         anim->avi = MEM_callocN (sizeof(AviMovie),"animavi");
00382 
00383         if (anim->avi == NULL) {
00384                 printf("Can't open avi: %s\n", anim->name);
00385                 return -1;
00386         }
00387 
00388         avierror = AVI_open_movie (anim->name, anim->avi);
00389 
00390 #if defined(_WIN32) && !defined(FREE_WINDOWS)
00391         if (avierror == AVI_ERROR_COMPRESSION) {
00392                 AVIFileInit();
00393                 hr = AVIFileOpen(&anim->pfile, anim->name, OF_READ, 0L);
00394                 if (hr == 0) {
00395                         anim->pfileopen = 1;
00396                         for (i = 0; i < MAXNUMSTREAMS; i++) {
00397                                 if (AVIFileGetStream(anim->pfile, &anim->pavi[i], 0L, i) != AVIERR_OK) {
00398                                         break;
00399                                 }
00400                                 
00401                                 AVIStreamInfo(anim->pavi[i], &avis, sizeof(avis));
00402                                 if ((avis.fccType == streamtypeVIDEO) && (firstvideo == -1)) {
00403                                         anim->pgf = AVIStreamGetFrameOpen(anim->pavi[i], NULL);
00404                                         if (anim->pgf) {
00405                                                 firstvideo = i;
00406 
00407                                                 // get stream length
00408                                                 anim->avi->header->TotalFrames = AVIStreamLength(anim->pavi[i]);
00409                                                 
00410                                                 // get information about images inside the stream
00411                                                 l = sizeof(abFormat);
00412                                                 AVIStreamReadFormat(anim->pavi[i], 0, &abFormat, &l);
00413                                                 lpbi = (LPBITMAPINFOHEADER)abFormat;
00414                                                 anim->avi->header->Height = lpbi->biHeight;
00415                                                 anim->avi->header->Width = lpbi->biWidth;
00416                                         } else {
00417                                                 FIXCC(avis.fccHandler);
00418                                                 FIXCC(avis.fccType);
00419                                                 printf("Can't find AVI decoder for type : %4.4hs/%4.4hs\n",
00420                                                         (LPSTR)&avis.fccType,
00421                                                         (LPSTR)&avis.fccHandler);
00422                                         }
00423                                 }
00424                         }
00425 
00426                         // register number of opened avistreams
00427                         anim->avistreams = i;
00428 
00429                         //
00430                         // Couldn't get any video streams out of this file
00431                         //
00432                         if ((anim->avistreams == 0) || (firstvideo == -1)) {
00433                                 avierror = AVI_ERROR_FORMAT;
00434                         } else {
00435                                 avierror = AVI_ERROR_NONE;
00436                                 anim->firstvideo = firstvideo;
00437                         }
00438                 } else {
00439                         AVIFileExit();
00440                 }
00441         }
00442 #endif
00443 
00444         if (avierror != AVI_ERROR_NONE) {
00445                 AVI_print_error(avierror);
00446                 printf ("Error loading avi: %s\n", anim->name);         
00447                 free_anim_avi(anim);
00448                 return -1;
00449         }
00450         
00451         anim->duration = anim->avi->header->TotalFrames;
00452         anim->params = 0;
00453 
00454         anim->x = anim->avi->header->Width;
00455         anim->y = anim->avi->header->Height;
00456         anim->interlacing = 0;
00457         anim->orientation = 0;
00458         anim->framesize = anim->x * anim->y * 4;
00459 
00460         anim->curposition = 0;
00461         anim->preseek = 0;
00462 
00463         /*  printf("x:%d y:%d size:%d interl:%d dur:%d\n", anim->x, anim->y, anim->framesize, anim->interlacing, anim->duration);*/
00464 
00465         return 0;
00466 }
00467 
00468 static ImBuf * avi_fetchibuf (struct anim *anim, int position) {
00469         ImBuf *ibuf = NULL;
00470         int *tmp;
00471         int y;
00472         
00473         if (anim == NULL) return (NULL);
00474 
00475 #if defined(_WIN32) && !defined(FREE_WINDOWS)
00476         if (anim->avistreams) {
00477                 LPBITMAPINFOHEADER lpbi;
00478 
00479                 if (anim->pgf) {
00480                         lpbi = AVIStreamGetFrame(anim->pgf, position + AVIStreamStart(anim->pavi[anim->firstvideo]));
00481                         if (lpbi) {
00482                                 ibuf = IMB_ibImageFromMemory((unsigned char *) lpbi, 100, IB_rect);
00483 //Oh brother...
00484                         }
00485                 }
00486         } else {
00487 #else
00488         if (1) {
00489 #endif
00490                 ibuf = IMB_allocImBuf (anim->x, anim->y, 24, IB_rect);
00491 
00492                 tmp = AVI_read_frame (anim->avi, AVI_FORMAT_RGB32, position,
00493                         AVI_get_stream(anim->avi, AVIST_VIDEO, 0));
00494                 
00495                 if (tmp == NULL) {
00496                         printf ("Error reading frame from AVI");
00497                         IMB_freeImBuf (ibuf);
00498                         return NULL;
00499                 }
00500 
00501                 for (y=0; y < anim->y; y++) {
00502                         memcpy (&(ibuf->rect)[((anim->y-y)-1)*anim->x],  &tmp[y*anim->x],  
00503                                         anim->x * 4);
00504                 }
00505                 
00506                 MEM_freeN (tmp);
00507         }
00508 
00509         ibuf->profile = IB_PROFILE_SRGB;
00510                 
00511         return ibuf;
00512 }
00513 
00514 #ifdef WITH_FFMPEG
00515 
00516 extern void do_init_ffmpeg(void);
00517 
00518 #ifdef FFMPEG_CODEC_IS_POINTER
00519 static AVCodecContext* get_codec_from_stream(AVStream* stream)
00520 {
00521         return stream->codec;
00522 }
00523 #else
00524 static AVCodecContext* get_codec_from_stream(AVStream* stream)
00525 {
00526         return &stream->codec;
00527 }
00528 #endif
00529 
00530 static int startffmpeg(struct anim * anim) {
00531         int            i, videoStream;
00532 
00533         AVCodec *pCodec;
00534         AVFormatContext *pFormatCtx;
00535         AVCodecContext *pCodecCtx;
00536 
00537 #ifdef FFMPEG_SWSCALE_COLOR_SPACE_SUPPORT
00538         /* The following for color space determination */
00539         int srcRange, dstRange, brightness, contrast, saturation;
00540         int *table;
00541         const int *inv_table;
00542 #endif
00543 
00544         if (anim == 0) return(-1);
00545 
00546         do_init_ffmpeg();
00547 
00548         if(av_open_input_file(&pFormatCtx, anim->name, NULL, 0, NULL)!=0) {
00549                 return -1;
00550         }
00551 
00552         if(av_find_stream_info(pFormatCtx)<0) {
00553                 av_close_input_file(pFormatCtx);
00554                 return -1;
00555         }
00556 
00557         dump_format(pFormatCtx, 0, anim->name, 0);
00558 
00559 
00560                 /* Find the first video stream */
00561         videoStream=-1;
00562         for(i=0; i<pFormatCtx->nb_streams; i++)
00563                 if(get_codec_from_stream(pFormatCtx->streams[i])->codec_type
00564                    == CODEC_TYPE_VIDEO) {
00565                         videoStream=i;
00566                         break;
00567                 }
00568 
00569         if(videoStream==-1) {
00570                 av_close_input_file(pFormatCtx);
00571                 return -1;
00572         }
00573 
00574         pCodecCtx = get_codec_from_stream(pFormatCtx->streams[videoStream]);
00575 
00576                 /* Find the decoder for the video stream */
00577         pCodec=avcodec_find_decoder(pCodecCtx->codec_id);
00578         if(pCodec==NULL) {
00579                 av_close_input_file(pFormatCtx);
00580                 return -1;
00581         }
00582 
00583         pCodecCtx->workaround_bugs = 1;
00584 
00585         if(avcodec_open(pCodecCtx, pCodec)<0) {
00586                 av_close_input_file(pFormatCtx);
00587                 return -1;
00588         }
00589 
00590 #ifdef FFMPEG_OLD_FRAME_RATE
00591         if(pCodecCtx->frame_rate>1000 && pCodecCtx->frame_rate_base==1)
00592                 pCodecCtx->frame_rate_base=1000;
00593 
00594 
00595         anim->duration = pFormatCtx->duration * pCodecCtx->frame_rate 
00596                 / pCodecCtx->frame_rate_base / AV_TIME_BASE;
00597 #else
00598         anim->duration = ceil(pFormatCtx->duration
00599                 * av_q2d(pFormatCtx->streams[videoStream]->r_frame_rate) 
00600                 / AV_TIME_BASE);
00601 
00602 #endif
00603         anim->params = 0;
00604 
00605         anim->x = pCodecCtx->width;
00606         anim->y = pCodecCtx->height;
00607         anim->interlacing = 0;
00608         anim->orientation = 0;
00609         anim->framesize = anim->x * anim->y * 4;
00610 
00611         anim->curposition = -1;
00612 
00613         anim->pFormatCtx = pFormatCtx;
00614         anim->pCodecCtx = pCodecCtx;
00615         anim->pCodec = pCodec;
00616         anim->videoStream = videoStream;
00617 
00618         anim->pFrame = avcodec_alloc_frame();
00619         anim->pFrameDeinterlaced = avcodec_alloc_frame();
00620         anim->pFrameRGB = avcodec_alloc_frame();
00621 
00622         if (avpicture_get_size(PIX_FMT_RGBA, anim->x, anim->y)
00623                 != anim->x * anim->y * 4) {
00624                 fprintf (stderr,
00625                          "ffmpeg has changed alloc scheme ... ARGHHH!\n");
00626                 avcodec_close(anim->pCodecCtx);
00627                 av_close_input_file(anim->pFormatCtx);
00628                 av_free(anim->pFrameRGB);
00629                 av_free(anim->pFrameDeinterlaced);
00630                 av_free(anim->pFrame);
00631                 anim->pCodecCtx = NULL;
00632                 return -1;
00633         }
00634 
00635         if (anim->ib_flags & IB_animdeinterlace) {
00636                 avpicture_fill((AVPicture*) anim->pFrameDeinterlaced, 
00637                                    MEM_callocN(avpicture_get_size(
00638                                                    anim->pCodecCtx->pix_fmt,
00639                                                    anim->x, anim->y), 
00640                                            "ffmpeg deinterlace"), 
00641                                    anim->pCodecCtx->pix_fmt, anim->x, anim->y);
00642         }
00643 
00644         if (pCodecCtx->has_b_frames) {
00645                 anim->preseek = 25; /* FIXME: detect gopsize ... */
00646         } else {
00647                 anim->preseek = 0;
00648         }
00649         
00650         anim->img_convert_ctx = sws_getContext(
00651                 anim->pCodecCtx->width,
00652                 anim->pCodecCtx->height,
00653                 anim->pCodecCtx->pix_fmt,
00654                 anim->pCodecCtx->width,
00655                 anim->pCodecCtx->height,
00656                 PIX_FMT_RGBA,
00657                 SWS_FAST_BILINEAR | SWS_PRINT_INFO,
00658                 NULL, NULL, NULL);
00659                 
00660         if (!anim->img_convert_ctx) {
00661                 fprintf (stderr,
00662                          "Can't transform color space??? Bailing out...\n");
00663                 avcodec_close(anim->pCodecCtx);
00664                 av_close_input_file(anim->pFormatCtx);
00665                 av_free(anim->pFrameRGB);
00666                 av_free(anim->pFrameDeinterlaced);
00667                 av_free(anim->pFrame);
00668                 anim->pCodecCtx = NULL;
00669                 return -1;
00670         }
00671 
00672 #ifdef FFMPEG_SWSCALE_COLOR_SPACE_SUPPORT
00673         /* Try do detect if input has 0-255 YCbCR range (JFIF Jpeg MotionJpeg) */
00674         if (!sws_getColorspaceDetails(anim->img_convert_ctx, (int**)&inv_table, &srcRange,
00675                 &table, &dstRange, &brightness, &contrast, &saturation)) {
00676 
00677                 srcRange = srcRange || anim->pCodecCtx->color_range == AVCOL_RANGE_JPEG;
00678                 inv_table = sws_getCoefficients(anim->pCodecCtx->colorspace);
00679 
00680                 if(sws_setColorspaceDetails(anim->img_convert_ctx, (int *)inv_table, srcRange,
00681                         table, dstRange, brightness, contrast, saturation)) {
00682 
00683                         printf("Warning: Could not set libswscale colorspace details.\n");
00684                         }
00685         }
00686         else {
00687                 printf("Warning: Could not set libswscale colorspace details.\n");
00688         }
00689 #endif
00690                 
00691         return (0);
00692 }
00693 
00694 static void ffmpeg_postprocess(struct anim * anim, ImBuf * ibuf,
00695                                int * filter_y)
00696 {
00697         AVFrame * input = anim->pFrame;
00698 
00699         /* This means the data wasnt read properly, 
00700            this check stops crashing */
00701         if (input->data[0]==0 && input->data[1]==0 
00702             && input->data[2]==0 && input->data[3]==0){
00703                 fprintf(stderr, "ffmpeg_fetchibuf: "
00704                         "data not read properly...\n");
00705                 return;
00706         }
00707 
00708         if (anim->ib_flags & IB_animdeinterlace) {
00709                 if (avpicture_deinterlace(
00710                             (AVPicture*) 
00711                             anim->pFrameDeinterlaced,
00712                             (const AVPicture*)
00713                             anim->pFrame,
00714                             anim->pCodecCtx->pix_fmt,
00715                             anim->pCodecCtx->width,
00716                             anim->pCodecCtx->height)
00717                     < 0) {
00718                         *filter_y = 1;
00719                 } else {
00720                         input = anim->pFrameDeinterlaced;
00721                 }
00722         }
00723         
00724         if (ENDIAN_ORDER == B_ENDIAN) {
00725                 int * dstStride   = anim->pFrameRGB->linesize;
00726                 uint8_t** dst     = anim->pFrameRGB->data;
00727                 int dstStride2[4] = { dstStride[0], 0, 0, 0 };
00728                 uint8_t* dst2[4]  = { dst[0], 0, 0, 0 };
00729                 int x,y,h,w;
00730                 unsigned char* bottom;
00731                 unsigned char* top;
00732                 
00733                 sws_scale(anim->img_convert_ctx,
00734                           (const uint8_t * const *)input->data,
00735                           input->linesize,
00736                           0,
00737                           anim->pCodecCtx->height,
00738                           dst2,
00739                           dstStride2);
00740                 
00741                 /* workaround: sws_scale bug
00742                    sets alpha = 0 and compensate
00743                    for altivec-bugs and flipy... */
00744                 
00745                 bottom = (unsigned char*) ibuf->rect;
00746                 top = bottom + ibuf->x * (ibuf->y-1) * 4;
00747                 
00748                 h = (ibuf->y + 1) / 2;
00749                 w = ibuf->x;
00750                 
00751                 for (y = 0; y < h; y++) {
00752                         unsigned char tmp[4];
00753                         unsigned int * tmp_l =
00754                                 (unsigned int*) tmp;
00755                         tmp[3] = 0xff;
00756                         
00757                         for (x = 0; x < w; x++) {
00758                                 tmp[0] = bottom[0];
00759                                 tmp[1] = bottom[1];
00760                                 tmp[2] = bottom[2];
00761                                 
00762                                 bottom[0] = top[0];
00763                                 bottom[1] = top[1];
00764                                 bottom[2] = top[2];
00765                                 bottom[3] = 0xff;
00766                                 
00767                                 *(unsigned int*) top = *tmp_l;
00768                                 
00769                                 bottom +=4;
00770                                 top += 4;
00771                         }
00772                         top -= 8 * w;
00773                 }
00774         } else {
00775                 int * dstStride   = anim->pFrameRGB->linesize;
00776                 uint8_t** dst     = anim->pFrameRGB->data;
00777                 int dstStride2[4] = { -dstStride[0], 0, 0, 0 };
00778                 uint8_t* dst2[4]  = { dst[0] + (anim->y - 1)*dstStride[0],
00779                                       0, 0, 0 };
00780                 int i;
00781                 unsigned char* r;
00782                 
00783                 sws_scale(anim->img_convert_ctx,
00784                           (const uint8_t * const *)input->data,
00785                           input->linesize,
00786                           0,
00787                           anim->pCodecCtx->height,
00788                           dst2,
00789                           dstStride2);
00790                 
00791                 r = (unsigned char*) ibuf->rect;
00792                 
00793                 /* workaround sws_scale bug: older version of 
00794                    sws_scale set alpha = 0... */
00795                 if (r[3] == 0) {
00796                         for (i = 0; i < ibuf->x * ibuf->y; i++) {
00797                                 r[3] = 0xff;
00798                                 r += 4;
00799                         }
00800                 }
00801         }
00802 }
00803 
00804 static ImBuf * ffmpeg_fetchibuf(struct anim * anim, int position) {
00805         ImBuf * ibuf;
00806         int frameFinished;
00807         AVPacket packet;
00808         int64_t pts_to_search = 0;
00809         int pos_found = 1;
00810         int filter_y = 0;
00811         int seek_by_bytes= 0;
00812         int preseek_count = 0;
00813 
00814         if (anim == 0) return (0);
00815 
00816         ibuf = IMB_allocImBuf(anim->x, anim->y, 32, IB_rect);
00817 
00818         avpicture_fill((AVPicture*) anim->pFrameRGB, 
00819                            (unsigned char*) ibuf->rect, 
00820                            PIX_FMT_RGBA, anim->x, anim->y);
00821 
00822         if (position != anim->curposition + 1) { 
00823                 if (position > anim->curposition + 1 
00824                         && anim->preseek 
00825                         && position - (anim->curposition + 1) < anim->preseek) {
00826                         while(av_read_frame(anim->pFormatCtx, &packet)>=0) {
00827                                 if (packet.stream_index == anim->videoStream) {
00828                                         avcodec_decode_video(
00829                                                 anim->pCodecCtx, 
00830                                                 anim->pFrame, &frameFinished, 
00831                                                 packet.data, packet.size);
00832 
00833                                         if (frameFinished) {
00834                                                 anim->curposition++;
00835                                         }
00836                                 }
00837                                 av_free_packet(&packet);
00838                                 if (position == anim->curposition+1) {
00839                                         break;
00840                                 }
00841                         }
00842                 }
00843         }
00844 
00845 /* disable seek_by_bytes for now, since bitrates are guessed wrong!
00846    also: MPEG2TS-seeking was fixed in later versions of ffmpeg, so problem
00847    is somewhat fixed by now (until we add correct timecode management code...)
00848 */
00849 #if 0
00850         seek_by_bytes = !!(anim->pFormatCtx->iformat->flags & AVFMT_TS_DISCONT);
00851 #else
00852         seek_by_bytes = FALSE;
00853 #endif
00854 
00855         if (position != anim->curposition + 1) { 
00856 #ifdef FFMPEG_OLD_FRAME_RATE
00857                 double frame_rate = 
00858                         (double) anim->pCodecCtx->frame_rate
00859                         / (double) anim->pCodecCtx->frame_rate_base;
00860 #else
00861                 double frame_rate = 
00862                         av_q2d(anim->pFormatCtx->streams[anim->videoStream]
00863                                    ->r_frame_rate);
00864 #endif
00865                 double pts_time_base = av_q2d(anim->pFormatCtx->streams[anim->videoStream]->time_base);
00866                 long long pos;
00867                 long long st_time = anim->pFormatCtx->start_time;
00868                 int ret;
00869 
00870                 if (seek_by_bytes) {
00871                         pos = position - anim->preseek;
00872                         if (pos < 0) {
00873                                 pos = 0;
00874                         }
00875                         preseek_count = position - pos;
00876 
00877                         pos *= anim->pFormatCtx->bit_rate / frame_rate;
00878                         pos /= 8;
00879                 } else {
00880                         pos = (long long) (position - anim->preseek) 
00881                                 * AV_TIME_BASE / frame_rate;
00882                         if (pos < 0) {
00883                                 pos = 0;
00884                         }
00885 
00886                         if (st_time != AV_NOPTS_VALUE) {
00887                                 pos += st_time;
00888                         }
00889                 }
00890 
00891                 ret = av_seek_frame(anim->pFormatCtx, -1, 
00892                                     pos, 
00893                                     AVSEEK_FLAG_BACKWARD | (
00894                                             seek_by_bytes 
00895                                             ? AVSEEK_FLAG_ANY 
00896                                             | AVSEEK_FLAG_BYTE : 0));
00897                 if (ret < 0) {
00898                         fprintf(stderr, "error while seeking: %d\n", ret);
00899                 }
00900 
00901                 pts_to_search = (long long) 
00902                         (((double) position) / pts_time_base / frame_rate);
00903                 if (st_time != AV_NOPTS_VALUE) {
00904                         pts_to_search += st_time / pts_time_base/ AV_TIME_BASE;
00905                 }
00906 
00907                 pos_found = 0;
00908                 avcodec_flush_buffers(anim->pCodecCtx);
00909         }
00910 
00911         while(av_read_frame(anim->pFormatCtx, &packet)>=0) {
00912                 if(packet.stream_index == anim->videoStream) {
00913                         avcodec_decode_video(anim->pCodecCtx, 
00914                                                  anim->pFrame, &frameFinished, 
00915                                                  packet.data, packet.size);
00916 
00917                         if (seek_by_bytes && preseek_count > 0) {
00918                                 preseek_count--;
00919                         }
00920 
00921                         if (frameFinished && !pos_found) {
00922                                 if (seek_by_bytes) {
00923                                         if (!preseek_count) {
00924                                                 pos_found = 1;
00925                                                 anim->curposition = position;
00926                                         }
00927                                 } else {
00928                                         if (packet.dts >= pts_to_search) {
00929                                                 pos_found = 1;
00930                                                 anim->curposition = position;
00931                                         }
00932                                 }
00933                         } 
00934 
00935                         if(frameFinished && pos_found == 1) {
00936                                 ffmpeg_postprocess(anim, ibuf, &filter_y);
00937                                 av_free_packet(&packet);
00938                                 break;
00939                         }
00940                 }
00941 
00942                 av_free_packet(&packet);
00943         }
00944 
00945         if (filter_y && ibuf) {
00946                 IMB_filtery(ibuf);
00947         }
00948 
00949         ibuf->profile = IB_PROFILE_SRGB;
00950         
00951         return(ibuf);
00952 }
00953 
00954 static void free_anim_ffmpeg(struct anim * anim) {
00955         if (anim == NULL) return;
00956 
00957         if (anim->pCodecCtx) {
00958                 avcodec_close(anim->pCodecCtx);
00959                 av_close_input_file(anim->pFormatCtx);
00960                 av_free(anim->pFrameRGB);
00961                 av_free(anim->pFrame);
00962 
00963                 if (anim->ib_flags & IB_animdeinterlace) {
00964                         MEM_freeN(anim->pFrameDeinterlaced->data[0]);
00965                 }
00966                 av_free(anim->pFrameDeinterlaced);
00967                 sws_freeContext(anim->img_convert_ctx);
00968         }
00969         anim->duration = 0;
00970 }
00971 
00972 #endif
00973 
00974 #ifdef WITH_REDCODE
00975 
00976 static int startredcode(struct anim * anim) {
00977         anim->redcodeCtx = redcode_open(anim->name);
00978         if (!anim->redcodeCtx) {
00979                 return -1;
00980         }
00981         anim->duration = redcode_get_length(anim->redcodeCtx);
00982         
00983         return 0;
00984 }
00985 
00986 static ImBuf * redcode_fetchibuf(struct anim * anim, int position) {
00987         struct ImBuf * ibuf;
00988         struct redcode_frame * frame;
00989         struct redcode_frame_raw * raw_frame;
00990 
00991         if (!anim->redcodeCtx) {
00992                 return NULL;
00993         }
00994 
00995         frame = redcode_read_video_frame(anim->redcodeCtx, position);
00996         
00997         if (!frame) {
00998                 return NULL;
00999         }
01000 
01001         raw_frame = redcode_decode_video_raw(frame, 1);
01002 
01003         redcode_free_frame(frame);
01004 
01005         if (!raw_frame) {
01006                 return NULL;
01007         }
01008         
01009                 ibuf = IMB_allocImBuf(raw_frame->width * 2, 
01010                                   raw_frame->height * 2, 32, IB_rectfloat);
01011 
01012         redcode_decode_video_float(raw_frame, ibuf->rect_float, 1);
01013 
01014         return ibuf;
01015 }
01016 
01017 static void free_anim_redcode(struct anim * anim) {
01018         if (anim->redcodeCtx) {
01019                 redcode_close(anim->redcodeCtx);
01020                 anim->redcodeCtx = 0;
01021         }
01022         anim->duration = 0;
01023 }
01024 
01025 #endif
01026 
01027 /* probeer volgende plaatje te lezen */
01028 /* Geen plaatje, probeer dan volgende animatie te openen */
01029 /* gelukt, haal dan eerste plaatje van animatie */
01030 
01031 static struct ImBuf * anim_getnew(struct anim * anim) {
01032         struct ImBuf *ibuf = 0;
01033 
01034         if (anim == NULL) return(0);
01035 
01036         free_anim_movie(anim);
01037         free_anim_avi(anim);
01038 #ifdef WITH_QUICKTIME
01039         free_anim_quicktime(anim);
01040 #endif
01041 #ifdef WITH_FFMPEG
01042         free_anim_ffmpeg(anim);
01043 #endif
01044 #ifdef WITH_REDCODE
01045         free_anim_redcode(anim);
01046 #endif
01047 
01048 
01049         if (anim->curtype != 0) return (0);
01050         anim->curtype = imb_get_anim_type(anim->name);  
01051 
01052         switch (anim->curtype) {
01053         case ANIM_SEQUENCE:
01054                 ibuf = IMB_loadiffname(anim->name, anim->ib_flags);
01055                 if (ibuf) {
01056                         strcpy(anim->first, anim->name);
01057                         anim->duration = 1;
01058                 }
01059                 break;
01060         case ANIM_MOVIE:
01061                 if (startmovie(anim)) return (0);
01062                 ibuf = IMB_allocImBuf (anim->x, anim->y, 24, 0); /* fake */
01063                 break;
01064         case ANIM_AVI:
01065                 if (startavi(anim)) {
01066                         printf("couldnt start avi\n"); 
01067                         return (0);
01068                 }
01069                 ibuf = IMB_allocImBuf (anim->x, anim->y, 24, 0);
01070                 break;
01071 #ifdef WITH_QUICKTIME
01072         case ANIM_QTIME:
01073                 if (startquicktime(anim)) return (0);
01074                 ibuf = IMB_allocImBuf (anim->x, anim->y, 24, 0);
01075                 break;
01076 #endif
01077 #ifdef WITH_FFMPEG
01078         case ANIM_FFMPEG:
01079                 if (startffmpeg(anim)) return (0);
01080                 ibuf = IMB_allocImBuf (anim->x, anim->y, 24, 0);
01081                 break;
01082 #endif
01083 #ifdef WITH_REDCODE
01084         case ANIM_REDCODE:
01085                 if (startredcode(anim)) return (0);
01086                 ibuf = IMB_allocImBuf (8, 8, 32, 0);
01087                 break;
01088 #endif
01089         }
01090         return(ibuf);
01091 }
01092 
01093 struct ImBuf * IMB_anim_previewframe(struct anim * anim) {
01094         struct ImBuf * ibuf = 0;
01095         int position = 0;
01096         
01097         ibuf = IMB_anim_absolute(anim, 0);
01098         if (ibuf) {
01099                 IMB_freeImBuf(ibuf);
01100                 position = anim->duration / 2;
01101                 ibuf = IMB_anim_absolute(anim, position);
01102         }
01103         return ibuf;
01104 }
01105 
01106 struct ImBuf * IMB_anim_absolute(struct anim * anim, int position) {
01107         struct ImBuf * ibuf = 0;
01108         char head[256], tail[256];
01109         unsigned short digits;
01110         int pic;
01111         int filter_y;
01112         if (anim == NULL) return(0);
01113 
01114         filter_y = (anim->ib_flags & IB_animdeinterlace);
01115 
01116         if (anim->curtype == 0) {
01117                 ibuf = anim_getnew(anim);
01118                 if (ibuf == NULL) {
01119                         return (0);
01120                 }
01121 
01122                 IMB_freeImBuf(ibuf); /* ???? */
01123                 ibuf= NULL;
01124         }
01125 
01126         if (position < 0) return(0);
01127         if (position >= anim->duration) return(0);
01128 
01129         switch(anim->curtype) {
01130         case ANIM_SEQUENCE:
01131                 pic = an_stringdec(anim->first, head, tail, &digits);
01132                 pic += position;
01133                 an_stringenc(anim->name, head, tail, digits, pic);
01134                 ibuf = IMB_loadiffname(anim->name, IB_rect);
01135                 if (ibuf) {
01136                         anim->curposition = position;
01137                 }
01138                 break;
01139         case ANIM_MOVIE:
01140                 ibuf = movie_fetchibuf(anim, position);
01141                 if (ibuf) {
01142                         anim->curposition = position;
01143                         IMB_convert_rgba_to_abgr(ibuf);
01144                         ibuf->profile = IB_PROFILE_SRGB;
01145                 }
01146                 break;
01147         case ANIM_AVI:
01148                 ibuf = avi_fetchibuf(anim, position);
01149                 if (ibuf)
01150                         anim->curposition = position;
01151                 break;
01152 #ifdef WITH_QUICKTIME
01153         case ANIM_QTIME:
01154                 ibuf = qtime_fetchibuf(anim, position);
01155                 if (ibuf)
01156                         anim->curposition = position;
01157                 break;
01158 #endif
01159 #ifdef WITH_FFMPEG
01160         case ANIM_FFMPEG:
01161                 ibuf = ffmpeg_fetchibuf(anim, position);
01162                 if (ibuf)
01163                         anim->curposition = position;
01164                 filter_y = 0; /* done internally */
01165                 break;
01166 #endif
01167 #ifdef WITH_REDCODE
01168         case ANIM_REDCODE:
01169                 ibuf = redcode_fetchibuf(anim, position);
01170                 if (ibuf) anim->curposition = position;
01171                 break;
01172 #endif
01173         }
01174 
01175         if (ibuf) {
01176                 if (filter_y) IMB_filtery(ibuf);
01177                 sprintf(ibuf->name, "%s.%04d", anim->name, anim->curposition + 1);
01178                 
01179         }
01180         return(ibuf);
01181 }
01182 
01183 /***/
01184 
01185 int IMB_anim_get_duration(struct anim *anim) {
01186         return anim->duration;
01187 }
01188 
01189 void IMB_anim_set_preseek(struct anim * anim, int preseek)
01190 {
01191         anim->preseek = preseek;
01192 }
01193 
01194 int IMB_anim_get_preseek(struct anim * anim)
01195 {
01196         return anim->preseek;
01197 }