== FFMPEG ==
[blender-staging.git] / source / blender / imbuf / intern / anim_movie.c
1 /**
2  * anim.c
3  *
4  * $Id$
5  *
6  * ***** BEGIN GPL LICENSE BLOCK *****
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software Foundation,
20  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21  *
22  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
23  * All rights reserved.
24  *
25  * The Original Code is: all of this file.
26  *
27  * Contributor(s): none yet.
28  *
29  * ***** END GPL LICENSE BLOCK *****
30  */
31
32 #ifdef _WIN32
33 #define INC_OLE2
34 #include <windows.h>
35 #include <windowsx.h>
36 #include <mmsystem.h>
37 #include <memory.h>
38 #include <commdlg.h>
39
40 #ifndef FREE_WINDOWS
41 #include <vfw.h>
42 #endif
43
44 #undef AVIIF_KEYFRAME // redefined in AVI_avi.h
45 #undef AVIIF_LIST // redefined in AVI_avi.h
46
47 #define FIXCC(fcc)  if (fcc == 0)       fcc = mmioFOURCC('N', 'o', 'n', 'e'); \
48                 if (fcc == BI_RLE8) fcc = mmioFOURCC('R', 'l', 'e', '8');
49 #endif
50
51 #include <sys/types.h>
52 #include <ctype.h>
53 #include <stdlib.h>
54 #include <stdio.h>
55 #ifndef _WIN32
56 #include <dirent.h>
57 #else
58 #include <io.h>
59 #endif
60
61 #include "BLI_blenlib.h" /* BLI_remlink BLI_filesize BLI_addtail
62                                                         BLI_countlist BLI_stringdec */
63 #include "MEM_guardedalloc.h"
64
65 #include "DNA_userdef_types.h"
66 #include "BKE_utildefines.h"
67 #include "BKE_global.h"
68 #include "BKE_depsgraph.h"
69
70 #include "imbuf.h"
71
72 #include "AVI_avi.h"
73
74 #ifdef WITH_QUICKTIME
75 #if defined(_WIN32) || defined(__APPLE__)
76 #include "quicktime_import.h"
77 #endif /* _WIN32 || __APPLE__ */
78 #endif /* WITH_QUICKTIME */
79
80 #include "IMB_imbuf_types.h"
81 #include "IMB_imbuf.h"
82
83 #include "IMB_allocimbuf.h"
84 #include "IMB_anim.h"
85
86 #ifdef WITH_FFMPEG
87 #include <libavformat/avformat.h>
88 #include <libavcodec/avcodec.h>
89 #include <libavutil/rational.h>
90 #include <libswscale/swscale.h>
91
92 #if LIBAVFORMAT_VERSION_INT < (49 << 16)
93 #define FFMPEG_OLD_FRAME_RATE 1
94 #else
95 #define FFMPEG_CODEC_IS_POINTER 1
96 #endif
97
98 #if (LIBAVCODEC_VERSION_MAJOR >= 52) && (LIBAVCODEC_VERSION_MINOR >= 29) && \
99          (LIBSWSCALE_VERSION_MAJOR >= 0) && (LIBSWSCALE_VERSION_MINOR >= 10)
100 #define FFMPEG_SWSCALE_COLOR_SPACE_SUPPORT
101 #endif
102
103 #endif //WITH_FFMPEG
104
105 #ifdef WITH_REDCODE
106 #ifdef _WIN32 /* on windows we use the ones in extern instead */
107 #include "libredcode/format.h"
108 #include "libredcode/codec.h"
109 #else
110 #include "libredcode/format.h"
111 #include "libredcode/codec.h"
112 #endif
113 #endif
114
115 /****/
116
117 #ifdef __sgi
118
119 #include <dmedia/moviefile.h>
120
121 static void movie_printerror(char * str) {
122         const char * errstr = mvGetErrorStr(mvGetErrno());
123
124         if (str) {
125                 if (errstr) printf("%s: %s\n", str, errstr);
126                 else printf("%s: returned error\n", str);
127         } else printf("%s\n", errstr);
128 }
129
130 static int startmovie(struct anim * anim) {
131         if (anim == 0) return(-1);
132
133         if ( mvOpenFile (anim->name, O_BINARY|O_RDONLY, &anim->movie ) != DM_SUCCESS ) {
134                 printf("Can't open movie: %s\n", anim->name);
135                 return(-1);
136         }
137         if ( mvFindTrackByMedium (anim->movie, DM_IMAGE, &anim->track) != DM_SUCCESS ) {
138                 printf("No image track in movie: %s\n", anim->name);
139                 mvClose(anim->movie);
140                 return(-1);
141         }
142
143         anim->duration = mvGetTrackLength (anim->track);
144         anim->params = mvGetParams( anim->track );
145
146         anim->x = dmParamsGetInt( anim->params, DM_IMAGE_WIDTH);
147         anim->y = dmParamsGetInt( anim->params, DM_IMAGE_HEIGHT);
148         anim->interlacing = dmParamsGetEnum (anim->params, DM_IMAGE_INTERLACING);
149         anim->orientation = dmParamsGetEnum (anim->params, DM_IMAGE_ORIENTATION);
150         anim->framesize = dmImageFrameSize(anim->params);
151
152         anim->curposition = 0;
153         anim->preseek = 0;
154
155         /*printf("x:%d y:%d size:%d interl:%d dur:%d\n", anim->x, anim->y, anim->framesize, anim->interlacing, anim->duration);*/
156         return (0);
157 }
158
159 static ImBuf * movie_fetchibuf(struct anim * anim, int position) {
160         ImBuf * ibuf;
161 /*      extern rectcpy(); */
162         int size;
163         unsigned int *rect1, *rect2;
164
165         if (anim == 0) return (0);
166
167         ibuf = IMB_allocImBuf(anim->x, anim->y, 24, IB_rect);
168
169         if ( mvReadFrames(anim->track, position, 1, ibuf->x * ibuf->y * 
170                 sizeof(int), ibuf->rect ) != DM_SUCCESS ) {
171                 movie_printerror("mvReadFrames");
172                 IMB_freeImBuf(ibuf);
173                 return(0);
174         }
175
176 /*
177         if (anim->interlacing == DM_IMAGE_INTERLACED_EVEN) {
178                 rect1 = ibuf->rect + (ibuf->x * ibuf->y) - 1;
179                 rect2 = rect1 - ibuf->x;
180     
181                 for (size = ibuf->x * (ibuf->y - 1); size > 0; size--){
182                         *rect1-- = *rect2--;
183                 }
184         }
185 */
186
187         if (anim->interlacing == DM_IMAGE_INTERLACED_EVEN)
188         {
189                 rect1 = ibuf->rect;
190                 rect2 = rect1 + ibuf->x;
191
192                 for (size = ibuf->x * (ibuf->y - 1); size > 0; size--){
193                         *rect1++ = *rect2++;
194                 }
195         }
196         /*if (anim->orientation == DM_TOP_TO_BOTTOM) IMB_flipy(ibuf);*/
197
198
199         return(ibuf);
200 }
201
202 static void free_anim_movie(struct anim * anim) {
203         if (anim == NULL) return;
204
205         if (anim->movie) {
206                 mvClose(anim->movie);
207                 anim->movie = NULL;
208         }
209         anim->duration = 0;
210 }
211
212 int ismovie(char *name) {
213         return (mvIsMovieFile(name) == DM_TRUE);
214 }
215
216 #else
217
218 int ismovie(char *UNUSED(name)) {
219         return 0;
220 }
221
222         /* never called, just keep the linker happy */
223 static int startmovie(struct anim *UNUSED(anim)) { return 1; }
224 static ImBuf * movie_fetchibuf(struct anim *UNUSED(anim), int UNUSED(position)) { return NULL; }
225 static void free_anim_movie(struct anim *UNUSED(anim)) { ; }
226
227 #endif
228
229 #if defined(_WIN32)
230 # define PATHSEPERATOR '\\'
231 #else
232 # define PATHSEPERATOR '/'
233 #endif
234
235 static int an_stringdec(const char *string, char* head, char *tail, unsigned short *numlen) {
236         unsigned short len,nume,nums=0;
237         short i,found=FALSE;
238
239         len=strlen(string);
240         nume = len;
241
242         for(i=len-1;i>=0;i--){
243                 if (string[i]==PATHSEPERATOR) break;
244                 if (isdigit(string[i])) {
245                         if (found){
246                                 nums=i;
247                         } else{
248                                 nume=i;
249                                 nums=i;
250                                 found=TRUE;
251                         }
252                 } else{
253                         if (found) break;
254                 }
255         }
256         if (found){
257                 strcpy(tail ,&string[nume+1]);
258                 strcpy(head, string);
259                 head[nums]= '\0';
260                 *numlen=nume-nums+1;
261                 return ((int)atoi(&(string[nums])));
262         }
263         tail[0]= '\0';
264         strcpy(head, string);
265         *numlen=0;
266         return TRUE;
267 }
268
269
270 static void an_stringenc(char *string, const char *head, const char *tail, unsigned short numlen, int pic) {
271         BLI_stringenc(string, head, tail, numlen, pic);
272 }
273
274 static void free_anim_avi (struct anim *anim) {
275 #if defined(_WIN32) && !defined(FREE_WINDOWS)
276         int i;
277 #endif
278
279         if (anim == NULL) return;
280         if (anim->avi == NULL) return;
281
282         AVI_close (anim->avi);
283         MEM_freeN (anim->avi);
284         anim->avi = NULL;
285
286 #if defined(_WIN32) && !defined(FREE_WINDOWS)
287
288         if (anim->pgf) {
289                 AVIStreamGetFrameClose(anim->pgf);
290                 anim->pgf = NULL;
291         }
292
293         for (i = 0; i < anim->avistreams; i++){
294                 AVIStreamRelease(anim->pavi[i]);
295         }
296         anim->avistreams = 0;
297
298         if (anim->pfileopen) {
299                 AVIFileRelease(anim->pfile);
300                 anim->pfileopen = 0;
301                 AVIFileExit();
302         }
303 #endif
304
305         anim->duration = 0;
306 }
307
308 void IMB_free_anim_ibuf(struct anim * anim) {
309         if (anim == NULL) return;
310
311         if (anim->ibuf1) IMB_freeImBuf(anim->ibuf1);
312         if (anim->ibuf2) IMB_freeImBuf(anim->ibuf2);
313
314         anim->ibuf1 = anim->ibuf2 = NULL;
315 }
316
317 #ifdef WITH_FFMPEG
318 static void free_anim_ffmpeg(struct anim * anim);
319 #endif
320 #ifdef WITH_REDCODE
321 static void free_anim_redcode(struct anim * anim);
322 #endif
323
324 void IMB_free_anim(struct anim * anim) {
325         if (anim == NULL) {
326                 printf("free anim, anim == NULL\n");
327                 return;
328         }
329
330         IMB_free_anim_ibuf(anim);
331         free_anim_movie(anim);
332         free_anim_avi(anim);
333
334 #ifdef WITH_QUICKTIME
335         free_anim_quicktime(anim);
336 #endif
337 #ifdef WITH_FFMPEG
338         free_anim_ffmpeg(anim);
339 #endif
340 #ifdef WITH_REDCODE
341         free_anim_redcode(anim);
342 #endif
343
344         MEM_freeN(anim);
345 }
346
347 void IMB_close_anim(struct anim * anim) {
348         if (anim == 0) return;
349
350         IMB_free_anim(anim);
351 }
352
353
354 struct anim * IMB_open_anim( const char * name, int ib_flags) {
355         struct anim * anim;
356
357         anim = (struct anim*)MEM_callocN(sizeof(struct anim), "anim struct");
358         if (anim != NULL) {
359                 strcpy(anim->name, name);  /* fixme: possible buffer overflow here? */
360                 anim->ib_flags = ib_flags;
361         }
362         return(anim);
363 }
364
365
366 static int startavi (struct anim *anim) {
367
368         AviError avierror;
369 #if defined(_WIN32) && !defined(FREE_WINDOWS)
370         HRESULT hr;
371         int i, firstvideo = -1;
372         BYTE abFormat[1024];
373         LONG l;
374         LPBITMAPINFOHEADER lpbi;
375         AVISTREAMINFO avis;
376 #endif
377
378         anim->avi = MEM_callocN (sizeof(AviMovie),"animavi");
379
380         if (anim->avi == NULL) {
381                 printf("Can't open avi: %s\n", anim->name);
382                 return -1;
383         }
384
385         avierror = AVI_open_movie (anim->name, anim->avi);
386
387 #if defined(_WIN32) && !defined(FREE_WINDOWS)
388         if (avierror == AVI_ERROR_COMPRESSION) {
389                 AVIFileInit();
390                 hr = AVIFileOpen(&anim->pfile, anim->name, OF_READ, 0L);
391                 if (hr == 0) {
392                         anim->pfileopen = 1;
393                         for (i = 0; i < MAXNUMSTREAMS; i++) {
394                                 if (AVIFileGetStream(anim->pfile, &anim->pavi[i], 0L, i) != AVIERR_OK) {
395                                         break;
396                                 }
397                                 
398                                 AVIStreamInfo(anim->pavi[i], &avis, sizeof(avis));
399                                 if ((avis.fccType == streamtypeVIDEO) && (firstvideo == -1)) {
400                                         anim->pgf = AVIStreamGetFrameOpen(anim->pavi[i], NULL);
401                                         if (anim->pgf) {
402                                                 firstvideo = i;
403
404                                                 // get stream length
405                                                 anim->avi->header->TotalFrames = AVIStreamLength(anim->pavi[i]);
406                                                 
407                                                 // get information about images inside the stream
408                                                 l = sizeof(abFormat);
409                                                 AVIStreamReadFormat(anim->pavi[i], 0, &abFormat, &l);
410                                                 lpbi = (LPBITMAPINFOHEADER)abFormat;
411                                                 anim->avi->header->Height = lpbi->biHeight;
412                                                 anim->avi->header->Width = lpbi->biWidth;
413                                         } else {
414                                                 FIXCC(avis.fccHandler);
415                                                 FIXCC(avis.fccType);
416                                                 printf("Can't find AVI decoder for type : %4.4hs/%4.4hs\n",
417                                                         (LPSTR)&avis.fccType,
418                                                         (LPSTR)&avis.fccHandler);
419                                         }
420                                 }
421                         }
422
423                         // register number of opened avistreams
424                         anim->avistreams = i;
425
426                         //
427                         // Couldn't get any video streams out of this file
428                         //
429                         if ((anim->avistreams == 0) || (firstvideo == -1)) {
430                                 avierror = AVI_ERROR_FORMAT;
431                         } else {
432                                 avierror = AVI_ERROR_NONE;
433                                 anim->firstvideo = firstvideo;
434                         }
435                 } else {
436                         AVIFileExit();
437                 }
438         }
439 #endif
440
441         if (avierror != AVI_ERROR_NONE) {
442                 AVI_print_error(avierror);
443                 printf ("Error loading avi: %s\n", anim->name);         
444                 free_anim_avi(anim);
445                 return -1;
446         }
447         
448         anim->duration = anim->avi->header->TotalFrames;
449         anim->params = 0;
450
451         anim->x = anim->avi->header->Width;
452         anim->y = anim->avi->header->Height;
453         anim->interlacing = 0;
454         anim->orientation = 0;
455         anim->framesize = anim->x * anim->y * 4;
456
457         anim->curposition = 0;
458         anim->preseek = 0;
459
460         /*  printf("x:%d y:%d size:%d interl:%d dur:%d\n", anim->x, anim->y, anim->framesize, anim->interlacing, anim->duration);*/
461
462         return 0;
463 }
464
465 static ImBuf * avi_fetchibuf (struct anim *anim, int position) {
466         ImBuf *ibuf = NULL;
467         int *tmp;
468         int y;
469         
470         if (anim == NULL) return (NULL);
471
472 #if defined(_WIN32) && !defined(FREE_WINDOWS)
473         if (anim->avistreams) {
474                 LPBITMAPINFOHEADER lpbi;
475
476                 if (anim->pgf) {
477                         lpbi = AVIStreamGetFrame(anim->pgf, position + AVIStreamStart(anim->pavi[anim->firstvideo]));
478                         if (lpbi) {
479                                 ibuf = IMB_ibImageFromMemory((unsigned char *) lpbi, 100, IB_rect);
480 //Oh brother...
481                         }
482                 }
483         } else {
484 #else
485         if (1) {
486 #endif
487                 ibuf = IMB_allocImBuf (anim->x, anim->y, 24, IB_rect);
488
489                 tmp = AVI_read_frame (anim->avi, AVI_FORMAT_RGB32, position,
490                         AVI_get_stream(anim->avi, AVIST_VIDEO, 0));
491                 
492                 if (tmp == NULL) {
493                         printf ("Error reading frame from AVI");
494                         IMB_freeImBuf (ibuf);
495                         return NULL;
496                 }
497
498                 for (y=0; y < anim->y; y++) {
499                         memcpy (&(ibuf->rect)[((anim->y-y)-1)*anim->x],  &tmp[y*anim->x],  
500                                         anim->x * 4);
501                 }
502                 
503                 MEM_freeN (tmp);
504         }
505
506         ibuf->profile = IB_PROFILE_SRGB;
507                 
508         return ibuf;
509 }
510
511 #ifdef WITH_FFMPEG
512
513 extern void do_init_ffmpeg();
514
515 #ifdef FFMPEG_CODEC_IS_POINTER
516 static AVCodecContext* get_codec_from_stream(AVStream* stream)
517 {
518         return stream->codec;
519 }
520 #else
521 static AVCodecContext* get_codec_from_stream(AVStream* stream)
522 {
523         return &stream->codec;
524 }
525 #endif
526
527 static int startffmpeg(struct anim * anim) {
528         int            i, videoStream;
529
530         AVCodec *pCodec;
531         AVFormatContext *pFormatCtx;
532         AVCodecContext *pCodecCtx;
533
534 #ifdef FFMPEG_SWSCALE_COLOR_SPACE_SUPPORT
535         /* The following for color space determination */
536         int srcRange, dstRange, brightness, contrast, saturation;
537         int *table;
538         const int *inv_table;
539 #endif
540
541         if (anim == 0) return(-1);
542
543         do_init_ffmpeg();
544
545         if(av_open_input_file(&pFormatCtx, anim->name, NULL, 0, NULL)!=0) {
546                 return -1;
547         }
548
549         if(av_find_stream_info(pFormatCtx)<0) {
550                 av_close_input_file(pFormatCtx);
551                 return -1;
552         }
553
554         dump_format(pFormatCtx, 0, anim->name, 0);
555
556
557                 /* Find the first video stream */
558         videoStream=-1;
559         for(i=0; i<pFormatCtx->nb_streams; i++)
560                 if(get_codec_from_stream(pFormatCtx->streams[i])->codec_type
561                    == CODEC_TYPE_VIDEO) {
562                         videoStream=i;
563                         break;
564                 }
565
566         if(videoStream==-1) {
567                 av_close_input_file(pFormatCtx);
568                 return -1;
569         }
570
571         pCodecCtx = get_codec_from_stream(pFormatCtx->streams[videoStream]);
572
573                 /* Find the decoder for the video stream */
574         pCodec=avcodec_find_decoder(pCodecCtx->codec_id);
575         if(pCodec==NULL) {
576                 av_close_input_file(pFormatCtx);
577                 return -1;
578         }
579
580         pCodecCtx->workaround_bugs = 1;
581
582         if(avcodec_open(pCodecCtx, pCodec)<0) {
583                 av_close_input_file(pFormatCtx);
584                 return -1;
585         }
586
587 #ifdef FFMPEG_OLD_FRAME_RATE
588         if(pCodecCtx->frame_rate>1000 && pCodecCtx->frame_rate_base==1)
589                 pCodecCtx->frame_rate_base=1000;
590
591
592         anim->duration = pFormatCtx->duration * pCodecCtx->frame_rate 
593                 / pCodecCtx->frame_rate_base / AV_TIME_BASE;
594 #else
595         anim->duration = ceil(pFormatCtx->duration
596                 * av_q2d(pFormatCtx->streams[videoStream]->r_frame_rate) 
597                 / AV_TIME_BASE);
598
599 #endif
600         anim->params = 0;
601
602         anim->x = pCodecCtx->width;
603         anim->y = pCodecCtx->height;
604         anim->interlacing = 0;
605         anim->orientation = 0;
606         anim->framesize = anim->x * anim->y * 4;
607
608         anim->curposition = -1;
609
610         anim->pFormatCtx = pFormatCtx;
611         anim->pCodecCtx = pCodecCtx;
612         anim->pCodec = pCodec;
613         anim->videoStream = videoStream;
614
615         anim->pFrame = avcodec_alloc_frame();
616         anim->pFrameDeinterlaced = avcodec_alloc_frame();
617         anim->pFrameRGB = avcodec_alloc_frame();
618
619         if (avpicture_get_size(PIX_FMT_RGBA, anim->x, anim->y)
620                 != anim->x * anim->y * 4) {
621                 fprintf (stderr,
622                          "ffmpeg has changed alloc scheme ... ARGHHH!\n");
623                 avcodec_close(anim->pCodecCtx);
624                 av_close_input_file(anim->pFormatCtx);
625                 av_free(anim->pFrameRGB);
626                 av_free(anim->pFrameDeinterlaced);
627                 av_free(anim->pFrame);
628                 anim->pCodecCtx = NULL;
629                 return -1;
630         }
631
632         if (anim->ib_flags & IB_animdeinterlace) {
633                 avpicture_fill((AVPicture*) anim->pFrameDeinterlaced, 
634                                    MEM_callocN(avpicture_get_size(
635                                                    anim->pCodecCtx->pix_fmt,
636                                                    anim->x, anim->y), 
637                                            "ffmpeg deinterlace"), 
638                                    anim->pCodecCtx->pix_fmt, anim->x, anim->y);
639         }
640
641         if (pCodecCtx->has_b_frames) {
642                 anim->preseek = 25; /* FIXME: detect gopsize ... */
643         } else {
644                 anim->preseek = 0;
645         }
646         
647         anim->img_convert_ctx = sws_getContext(
648                 anim->pCodecCtx->width,
649                 anim->pCodecCtx->height,
650                 anim->pCodecCtx->pix_fmt,
651                 anim->pCodecCtx->width,
652                 anim->pCodecCtx->height,
653                 PIX_FMT_RGBA,
654                 SWS_FAST_BILINEAR | SWS_PRINT_INFO,
655                 NULL, NULL, NULL);
656                 
657         if (!anim->img_convert_ctx) {
658                 fprintf (stderr,
659                          "Can't transform color space??? Bailing out...\n");
660                 avcodec_close(anim->pCodecCtx);
661                 av_close_input_file(anim->pFormatCtx);
662                 av_free(anim->pFrameRGB);
663                 av_free(anim->pFrameDeinterlaced);
664                 av_free(anim->pFrame);
665                 anim->pCodecCtx = NULL;
666                 return -1;
667         }
668
669 #ifdef FFMPEG_SWSCALE_COLOR_SPACE_SUPPORT
670         /* Try do detect if input has 0-255 YCbCR range (JFIF Jpeg MotionJpeg) */
671         if (!sws_getColorspaceDetails(anim->img_convert_ctx, (int**)&inv_table, &srcRange,
672                 &table, &dstRange, &brightness, &contrast, &saturation)) {
673
674                 srcRange = srcRange || anim->pCodecCtx->color_range == AVCOL_RANGE_JPEG;
675                 inv_table = sws_getCoefficients(anim->pCodecCtx->colorspace);
676
677                 if(sws_setColorspaceDetails(anim->img_convert_ctx, (int *)inv_table, srcRange,
678                         table, dstRange, brightness, contrast, saturation)) {
679
680                         printf("Warning: Could not set libswscale colorspace details.\n");
681                         }
682         }
683         else {
684                 printf("Warning: Could not set libswscale colorspace details.\n");
685         }
686 #endif
687                 
688         return (0);
689 }
690
691 static void ffmpeg_postprocess(struct anim * anim, ImBuf * ibuf,
692                                int * filter_y)
693 {
694         AVFrame * input = anim->pFrame;
695
696         /* This means the data wasnt read properly, 
697            this check stops crashing */
698         if (input->data[0]==0 && input->data[1]==0 
699             && input->data[2]==0 && input->data[3]==0){
700                 fprintf(stderr, "ffmpeg_fetchibuf: "
701                         "data not read properly...\n");
702                 return;
703         }
704
705         if (anim->ib_flags & IB_animdeinterlace) {
706                 if (avpicture_deinterlace(
707                             (AVPicture*) 
708                             anim->pFrameDeinterlaced,
709                             (const AVPicture*)
710                             anim->pFrame,
711                             anim->pCodecCtx->pix_fmt,
712                             anim->pCodecCtx->width,
713                             anim->pCodecCtx->height)
714                     < 0) {
715                         *filter_y = 1;
716                 } else {
717                         input = anim->pFrameDeinterlaced;
718                 }
719         }
720         
721         if (ENDIAN_ORDER == B_ENDIAN) {
722                 int * dstStride   = anim->pFrameRGB->linesize;
723                 uint8_t** dst     = anim->pFrameRGB->data;
724                 int dstStride2[4] = { dstStride[0], 0, 0, 0 };
725                 uint8_t* dst2[4]  = { dst[0], 0, 0, 0 };
726                 int x,y,h,w;
727                 unsigned char* bottom;
728                 unsigned char* top;
729                 
730                 sws_scale(anim->img_convert_ctx,
731                           (const uint8_t * const *)input->data,
732                           input->linesize,
733                           0,
734                           anim->pCodecCtx->height,
735                           dst2,
736                           dstStride2);
737                 
738                 /* workaround: sws_scale bug
739                    sets alpha = 0 and compensate
740                    for altivec-bugs and flipy... */
741                 
742                 bottom = (unsigned char*) ibuf->rect;
743                 top = bottom + ibuf->x * (ibuf->y-1) * 4;
744                 
745                 h = (ibuf->y + 1) / 2;
746                 w = ibuf->x;
747                 
748                 for (y = 0; y < h; y++) {
749                         unsigned char tmp[4];
750                         unsigned int * tmp_l =
751                                 (unsigned int*) tmp;
752                         tmp[3] = 0xff;
753                         
754                         for (x = 0; x < w; x++) {
755                                 tmp[0] = bottom[0];
756                                 tmp[1] = bottom[1];
757                                 tmp[2] = bottom[2];
758                                 
759                                 bottom[0] = top[0];
760                                 bottom[1] = top[1];
761                                 bottom[2] = top[2];
762                                 bottom[3] = 0xff;
763                                 
764                                 *(unsigned int*) top = *tmp_l;
765                                 
766                                 bottom +=4;
767                                 top += 4;
768                         }
769                         top -= 8 * w;
770                 }
771         } else {
772                 int * dstStride   = anim->pFrameRGB->linesize;
773                 uint8_t** dst     = anim->pFrameRGB->data;
774                 int dstStride2[4] = { -dstStride[0], 0, 0, 0 };
775                 uint8_t* dst2[4]  = { dst[0] + (anim->y - 1)*dstStride[0],
776                                       0, 0, 0 };
777                 int i;
778                 unsigned char* r;
779                 
780                 sws_scale(anim->img_convert_ctx,
781                           (const uint8_t * const *)input->data,
782                           input->linesize,
783                           0,
784                           anim->pCodecCtx->height,
785                           dst2,
786                           dstStride2);
787                 
788                 r = (unsigned char*) ibuf->rect;
789                 
790                 /* workaround sws_scale bug: older version of 
791                    sws_scale set alpha = 0... */
792                 if (r[3] == 0) {
793                         for (i = 0; i < ibuf->x * ibuf->y; i++) {
794                                 r[3] = 0xff;
795                                 r += 4;
796                         }
797                 }
798         }
799 }
800
801 static ImBuf * ffmpeg_fetchibuf(struct anim * anim, int position) {
802         ImBuf * ibuf;
803         int frameFinished;
804         AVPacket packet;
805         int64_t pts_to_search = 0;
806         int pos_found = 1;
807         int filter_y = 0;
808         int seek_by_bytes= 0;
809         int preseek_count = 0;
810
811         if (anim == 0) return (0);
812
813         ibuf = IMB_allocImBuf(anim->x, anim->y, 32, IB_rect);
814
815         avpicture_fill((AVPicture*) anim->pFrameRGB, 
816                            (unsigned char*) ibuf->rect, 
817                            PIX_FMT_RGBA, anim->x, anim->y);
818
819         if (position != anim->curposition + 1) { 
820                 if (position > anim->curposition + 1 
821                         && anim->preseek 
822                         && position - (anim->curposition + 1) < anim->preseek) {
823                         while(av_read_frame(anim->pFormatCtx, &packet)>=0) {
824                                 if (packet.stream_index == anim->videoStream) {
825                                         avcodec_decode_video(
826                                                 anim->pCodecCtx, 
827                                                 anim->pFrame, &frameFinished, 
828                                                 packet.data, packet.size);
829
830                                         if (frameFinished) {
831                                                 anim->curposition++;
832                                         }
833                                 }
834                                 av_free_packet(&packet);
835                                 if (position == anim->curposition+1) {
836                                         break;
837                                 }
838                         }
839                 }
840         }
841
842 /* disable seek_by_bytes for now, since bitrates are guessed wrong!
843    also: MPEG2TS-seeking was fixed in later versions of ffmpeg, so problem
844    is somewhat fixed by now (until we add correct timecode management code...)
845 */
846 #if 0
847         seek_by_bytes = !!(anim->pFormatCtx->iformat->flags & AVFMT_TS_DISCONT);
848 #else
849         seek_by_bytes = FALSE;
850 #endif
851
852         if (position != anim->curposition + 1) { 
853 #ifdef FFMPEG_OLD_FRAME_RATE
854                 double frame_rate = 
855                         (double) anim->pCodecCtx->frame_rate
856                         / (double) anim->pCodecCtx->frame_rate_base;
857 #else
858                 double frame_rate = 
859                         av_q2d(anim->pFormatCtx->streams[anim->videoStream]
860                                    ->r_frame_rate);
861 #endif
862                 double pts_time_base = av_q2d(anim->pFormatCtx->streams[anim->videoStream]->time_base);
863                 long long pos;
864                 long long st_time = anim->pFormatCtx->start_time;
865                 int ret;
866
867                 if (seek_by_bytes) {
868                         pos = position - anim->preseek;
869                         if (pos < 0) {
870                                 pos = 0;
871                         }
872                         preseek_count = position - pos;
873
874                         pos *= anim->pFormatCtx->bit_rate / frame_rate;
875                         pos /= 8;
876                 } else {
877                         pos = (long long) (position - anim->preseek) 
878                                 * AV_TIME_BASE / frame_rate;
879                         if (pos < 0) {
880                                 pos = 0;
881                         }
882
883                         if (st_time != AV_NOPTS_VALUE) {
884                                 pos += st_time;
885                         }
886                 }
887
888                 ret = av_seek_frame(anim->pFormatCtx, -1, 
889                                     pos, 
890                                     AVSEEK_FLAG_BACKWARD | (
891                                             seek_by_bytes 
892                                             ? AVSEEK_FLAG_ANY 
893                                             | AVSEEK_FLAG_BYTE : 0));
894                 if (ret < 0) {
895                         fprintf(stderr, "error while seeking: %d\n", ret);
896                 }
897
898                 pts_to_search = (long long) 
899                         (((double) position) / pts_time_base / frame_rate);
900                 if (st_time != AV_NOPTS_VALUE) {
901                         pts_to_search += st_time / pts_time_base/ AV_TIME_BASE;
902                 }
903
904                 pos_found = 0;
905                 avcodec_flush_buffers(anim->pCodecCtx);
906         }
907
908         while(av_read_frame(anim->pFormatCtx, &packet)>=0) {
909                 if(packet.stream_index == anim->videoStream) {
910                         avcodec_decode_video(anim->pCodecCtx, 
911                                                  anim->pFrame, &frameFinished, 
912                                                  packet.data, packet.size);
913
914                         if (seek_by_bytes && preseek_count > 0) {
915                                 preseek_count--;
916                         }
917
918                         if (frameFinished && !pos_found) {
919                                 if (seek_by_bytes) {
920                                         if (!preseek_count) {
921                                                 pos_found = 1;
922                                                 anim->curposition = position;
923                                         }
924                                 } else {
925                                         if (packet.dts >= pts_to_search) {
926                                                 pos_found = 1;
927                                                 anim->curposition = position;
928                                         }
929                                 }
930                         } 
931
932                         if(frameFinished && pos_found == 1) {
933                                 ffmpeg_postprocess(anim, ibuf, &filter_y);
934                                 av_free_packet(&packet);
935                                 break;
936                         }
937                 }
938
939                 av_free_packet(&packet);
940         }
941
942         if (filter_y && ibuf) {
943                 IMB_filtery(ibuf);
944         }
945
946         ibuf->profile = IB_PROFILE_SRGB;
947         
948         return(ibuf);
949 }
950
951 static void free_anim_ffmpeg(struct anim * anim) {
952         if (anim == NULL) return;
953
954         if (anim->pCodecCtx) {
955                 avcodec_close(anim->pCodecCtx);
956                 av_close_input_file(anim->pFormatCtx);
957                 av_free(anim->pFrameRGB);
958                 av_free(anim->pFrame);
959
960                 if (anim->ib_flags & IB_animdeinterlace) {
961                         MEM_freeN(anim->pFrameDeinterlaced->data[0]);
962                 }
963                 av_free(anim->pFrameDeinterlaced);
964                 sws_freeContext(anim->img_convert_ctx);
965         }
966         anim->duration = 0;
967 }
968
969 #endif
970
971 #ifdef WITH_REDCODE
972
973 static int startredcode(struct anim * anim) {
974         anim->redcodeCtx = redcode_open(anim->name);
975         if (!anim->redcodeCtx) {
976                 return -1;
977         }
978         anim->duration = redcode_get_length(anim->redcodeCtx);
979         
980         return 0;
981 }
982
983 static ImBuf * redcode_fetchibuf(struct anim * anim, int position) {
984         struct ImBuf * ibuf;
985         struct redcode_frame * frame;
986         struct redcode_frame_raw * raw_frame;
987
988         if (!anim->redcodeCtx) {
989                 return NULL;
990         }
991
992         frame = redcode_read_video_frame(anim->redcodeCtx, position);
993         
994         if (!frame) {
995                 return NULL;
996         }
997
998         raw_frame = redcode_decode_video_raw(frame, 1);
999
1000         redcode_free_frame(frame);
1001
1002         if (!raw_frame) {
1003                 return NULL;
1004         }
1005         
1006                 ibuf = IMB_allocImBuf(raw_frame->width * 2, 
1007                                   raw_frame->height * 2, 32, IB_rectfloat);
1008
1009         redcode_decode_video_float(raw_frame, ibuf->rect_float, 1);
1010
1011         return ibuf;
1012 }
1013
1014 static void free_anim_redcode(struct anim * anim) {
1015         if (anim->redcodeCtx) {
1016                 redcode_close(anim->redcodeCtx);
1017                 anim->redcodeCtx = 0;
1018         }
1019         anim->duration = 0;
1020 }
1021
1022 #endif
1023
1024 /* probeer volgende plaatje te lezen */
1025 /* Geen plaatje, probeer dan volgende animatie te openen */
1026 /* gelukt, haal dan eerste plaatje van animatie */
1027
1028 static struct ImBuf * anim_getnew(struct anim * anim) {
1029         struct ImBuf *ibuf = 0;
1030
1031         if (anim == NULL) return(0);
1032
1033         free_anim_movie(anim);
1034         free_anim_avi(anim);
1035 #ifdef WITH_QUICKTIME
1036         free_anim_quicktime(anim);
1037 #endif
1038 #ifdef WITH_FFMPEG
1039         free_anim_ffmpeg(anim);
1040 #endif
1041 #ifdef WITH_REDCODE
1042         free_anim_redcode(anim);
1043 #endif
1044
1045
1046         if (anim->curtype != 0) return (0);
1047         anim->curtype = imb_get_anim_type(anim->name);  
1048
1049         switch (anim->curtype) {
1050         case ANIM_SEQUENCE:
1051                 ibuf = IMB_loadiffname(anim->name, anim->ib_flags);
1052                 if (ibuf) {
1053                         strcpy(anim->first, anim->name);
1054                         anim->duration = 1;
1055                 }
1056                 break;
1057         case ANIM_MOVIE:
1058                 if (startmovie(anim)) return (0);
1059                 ibuf = IMB_allocImBuf (anim->x, anim->y, 24, 0); /* fake */
1060                 break;
1061         case ANIM_AVI:
1062                 if (startavi(anim)) {
1063                         printf("couldnt start avi\n"); 
1064                         return (0);
1065                 }
1066                 ibuf = IMB_allocImBuf (anim->x, anim->y, 24, 0);
1067                 break;
1068 #ifdef WITH_QUICKTIME
1069         case ANIM_QTIME:
1070                 if (startquicktime(anim)) return (0);
1071                 ibuf = IMB_allocImBuf (anim->x, anim->y, 24, 0);
1072                 break;
1073 #endif
1074 #ifdef WITH_FFMPEG
1075         case ANIM_FFMPEG:
1076                 if (startffmpeg(anim)) return (0);
1077                 ibuf = IMB_allocImBuf (anim->x, anim->y, 24, 0);
1078                 break;
1079 #endif
1080 #ifdef WITH_REDCODE
1081         case ANIM_REDCODE:
1082                 if (startredcode(anim)) return (0);
1083                 ibuf = IMB_allocImBuf (8, 8, 32, 0);
1084                 break;
1085 #endif
1086         }
1087         return(ibuf);
1088 }
1089
1090 struct ImBuf * IMB_anim_previewframe(struct anim * anim) {
1091         struct ImBuf * ibuf = 0;
1092         int position = 0;
1093         
1094         ibuf = IMB_anim_absolute(anim, 0);
1095         if (ibuf) {
1096                 IMB_freeImBuf(ibuf);
1097                 position = anim->duration / 2;
1098                 ibuf = IMB_anim_absolute(anim, position);
1099         }
1100         return ibuf;
1101 }
1102
1103 struct ImBuf * IMB_anim_absolute(struct anim * anim, int position) {
1104         struct ImBuf * ibuf = 0;
1105         char head[256], tail[256];
1106         unsigned short digits;
1107         int pic;
1108         int filter_y;
1109         if (anim == NULL) return(0);
1110
1111         filter_y = (anim->ib_flags & IB_animdeinterlace);
1112
1113         if (anim->curtype == 0) {
1114                 ibuf = anim_getnew(anim);
1115                 if (ibuf == NULL) {
1116                         return (0);
1117                 }
1118
1119                 IMB_freeImBuf(ibuf); /* ???? */
1120                 ibuf= NULL;
1121         }
1122
1123         if (position < 0) return(0);
1124         if (position >= anim->duration) return(0);
1125
1126         switch(anim->curtype) {
1127         case ANIM_SEQUENCE:
1128                 pic = an_stringdec(anim->first, head, tail, &digits);
1129                 pic += position;
1130                 an_stringenc(anim->name, head, tail, digits, pic);
1131                 ibuf = IMB_loadiffname(anim->name, IB_rect);
1132                 if (ibuf) {
1133                         anim->curposition = position;
1134                 }
1135                 break;
1136         case ANIM_MOVIE:
1137                 ibuf = movie_fetchibuf(anim, position);
1138                 if (ibuf) {
1139                         anim->curposition = position;
1140                         IMB_convert_rgba_to_abgr(ibuf);
1141                         ibuf->profile = IB_PROFILE_SRGB;
1142                 }
1143                 break;
1144         case ANIM_AVI:
1145                 ibuf = avi_fetchibuf(anim, position);
1146                 if (ibuf)
1147                         anim->curposition = position;
1148                 break;
1149 #ifdef WITH_QUICKTIME
1150         case ANIM_QTIME:
1151                 ibuf = qtime_fetchibuf(anim, position);
1152                 if (ibuf)
1153                         anim->curposition = position;
1154                 break;
1155 #endif
1156 #ifdef WITH_FFMPEG
1157         case ANIM_FFMPEG:
1158                 ibuf = ffmpeg_fetchibuf(anim, position);
1159                 if (ibuf)
1160                         anim->curposition = position;
1161                 filter_y = 0; /* done internally */
1162                 break;
1163 #endif
1164 #ifdef WITH_REDCODE
1165         case ANIM_REDCODE:
1166                 ibuf = redcode_fetchibuf(anim, position);
1167                 if (ibuf) anim->curposition = position;
1168                 break;
1169 #endif
1170         }
1171
1172         if (ibuf) {
1173                 if (filter_y) IMB_filtery(ibuf);
1174                 sprintf(ibuf->name, "%s.%04d", anim->name, anim->curposition + 1);
1175                 
1176         }
1177         return(ibuf);
1178 }
1179
1180 /***/
1181
1182 int IMB_anim_get_duration(struct anim *anim) {
1183         return anim->duration;
1184 }
1185
1186 void IMB_anim_set_preseek(struct anim * anim, int preseek)
1187 {
1188         anim->preseek = preseek;
1189 }
1190
1191 int IMB_anim_get_preseek(struct anim * anim)
1192 {
1193         return anim->preseek;
1194 }