Merging r39312 through r39329 from trunk into soc-2011-tomato
[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 /** \file blender/imbuf/intern/anim_movie.c
33  *  \ingroup imbuf
34  */
35
36
37 #ifdef _WIN32
38 #define INC_OLE2
39 #include <windows.h>
40 #include <windowsx.h>
41 #include <mmsystem.h>
42 #include <memory.h>
43 #include <commdlg.h>
44
45 #ifndef FREE_WINDOWS
46 #include <vfw.h>
47 #endif
48
49 #undef AVIIF_KEYFRAME // redefined in AVI_avi.h
50 #undef AVIIF_LIST // redefined in AVI_avi.h
51
52 #define FIXCC(fcc)  if (fcc == 0)       fcc = mmioFOURCC('N', 'o', 'n', 'e'); \
53                 if (fcc == BI_RLE8) fcc = mmioFOURCC('R', 'l', 'e', '8');
54 #endif
55
56 #include <sys/types.h>
57 #include <ctype.h>
58 #include <stdlib.h>
59 #include <stdio.h>
60 #include <math.h>
61 #ifndef _WIN32
62 #include <dirent.h>
63 #else
64 #include <io.h>
65 #endif
66
67 #include "BLI_blenlib.h" /* BLI_remlink BLI_filesize BLI_addtail
68                                                         BLI_countlist BLI_stringdec */
69 #include "BLI_utildefines.h"
70
71 #include "MEM_guardedalloc.h"
72
73 #include "DNA_userdef_types.h"
74
75
76 #include "BKE_global.h"
77 #include "BKE_depsgraph.h"
78
79 #include "BLI_math_base.h"
80
81 #include "imbuf.h"
82
83 #include "AVI_avi.h"
84
85 #ifdef WITH_QUICKTIME
86 #if defined(_WIN32) || defined(__APPLE__)
87 #include "quicktime_import.h"
88 #endif /* _WIN32 || __APPLE__ */
89 #endif /* WITH_QUICKTIME */
90
91 #include "IMB_imbuf_types.h"
92 #include "IMB_imbuf.h"
93
94 #include "IMB_allocimbuf.h"
95 #include "IMB_anim.h"
96 #include "IMB_indexer.h"
97
98 #ifdef WITH_FFMPEG
99 #include <libavformat/avformat.h>
100 #include <libavcodec/avcodec.h>
101 #include <libavutil/rational.h>
102 #include <libswscale/swscale.h>
103
104 #include "ffmpeg_compat.h"
105
106 #endif //WITH_FFMPEG
107
108 #ifdef WITH_REDCODE
109 #ifdef _WIN32 /* on windows we use the ones in extern instead */
110 #include "libredcode/format.h"
111 #include "libredcode/codec.h"
112 #else
113 #include "libredcode/format.h"
114 #include "libredcode/codec.h"
115 #endif
116 #endif
117
118 /****/
119
120 #ifdef __sgi
121
122 #include <dmedia/moviefile.h>
123
124 static void movie_printerror(char * str) {
125         const char * errstr = mvGetErrorStr(mvGetErrno());
126
127         if (str) {
128                 if (errstr) printf("%s: %s\n", str, errstr);
129                 else printf("%s: returned error\n", str);
130         } else printf("%s\n", errstr);
131 }
132
133 static int startmovie(struct anim * anim) {
134         if (anim == 0) return(-1);
135
136         if ( mvOpenFile (anim->name, O_BINARY|O_RDONLY, &anim->movie ) != DM_SUCCESS ) {
137                 printf("Can't open movie: %s\n", anim->name);
138                 return(-1);
139         }
140         if ( mvFindTrackByMedium (anim->movie, DM_IMAGE, &anim->track) != DM_SUCCESS ) {
141                 printf("No image track in movie: %s\n", anim->name);
142                 mvClose(anim->movie);
143                 return(-1);
144         }
145
146         anim->duration = mvGetTrackLength (anim->track);
147         anim->params = mvGetParams( anim->track );
148
149         anim->x = dmParamsGetInt( anim->params, DM_IMAGE_WIDTH);
150         anim->y = dmParamsGetInt( anim->params, DM_IMAGE_HEIGHT);
151         anim->interlacing = dmParamsGetEnum (anim->params, DM_IMAGE_INTERLACING);
152         anim->orientation = dmParamsGetEnum (anim->params, DM_IMAGE_ORIENTATION);
153         anim->framesize = dmImageFrameSize(anim->params);
154
155         anim->curposition = 0;
156         anim->preseek = 0;
157
158         /*printf("x:%d y:%d size:%d interl:%d dur:%d\n", anim->x, anim->y, anim->framesize, anim->interlacing, anim->duration);*/
159         return (0);
160 }
161
162 static ImBuf * movie_fetchibuf(struct anim * anim, int position) {
163         ImBuf * ibuf;
164 /*      extern rectcpy(); */
165         int size;
166         unsigned int *rect1, *rect2;
167
168         if (anim == 0) return (0);
169
170         ibuf = IMB_allocImBuf(anim->x, anim->y, 24, IB_rect);
171
172         if ( mvReadFrames(anim->track, position, 1, ibuf->x * ibuf->y * 
173                 sizeof(int), ibuf->rect ) != DM_SUCCESS ) {
174                 movie_printerror("mvReadFrames");
175                 IMB_freeImBuf(ibuf);
176                 return(0);
177         }
178
179 /*
180         if (anim->interlacing == DM_IMAGE_INTERLACED_EVEN) {
181                 rect1 = ibuf->rect + (ibuf->x * ibuf->y) - 1;
182                 rect2 = rect1 - ibuf->x;
183
184                 for (size = ibuf->x * (ibuf->y - 1); size > 0; size--){
185                         *rect1-- = *rect2--;
186                 }
187         }
188 */
189
190         if (anim->interlacing == DM_IMAGE_INTERLACED_EVEN)
191         {
192                 rect1 = ibuf->rect;
193                 rect2 = rect1 + ibuf->x;
194
195                 for (size = ibuf->x * (ibuf->y - 1); size > 0; size--){
196                         *rect1++ = *rect2++;
197                 }
198         }
199         /*if (anim->orientation == DM_TOP_TO_BOTTOM) IMB_flipy(ibuf);*/
200
201
202         return(ibuf);
203 }
204
205 static void free_anim_movie(struct anim * anim) {
206         if (anim == NULL) return;
207
208         if (anim->movie) {
209                 mvClose(anim->movie);
210                 anim->movie = NULL;
211         }
212         anim->duration = 0;
213 }
214
215 int ismovie(char *name) {
216         return (mvIsMovieFile(name) == DM_TRUE);
217 }
218
219 #else
220
221 int ismovie(const char *UNUSED(name)) {
222         return 0;
223 }
224
225         /* never called, just keep the linker happy */
226 static int startmovie(struct anim *UNUSED(anim)) { return 1; }
227 static ImBuf * movie_fetchibuf(struct anim *UNUSED(anim), int UNUSED(position)) { return NULL; }
228 static void free_anim_movie(struct anim *UNUSED(anim)) { ; }
229
230 #endif
231
232 #if defined(_WIN32)
233 # define PATHSEPERATOR '\\'
234 #else
235 # define PATHSEPERATOR '/'
236 #endif
237
238 static int an_stringdec(const char *string, char* head, char *tail, unsigned short *numlen) {
239         unsigned short len,nume,nums=0;
240         short i,found=FALSE;
241
242         len=strlen(string);
243         nume = len;
244
245         for(i=len-1;i>=0;i--){
246                 if (string[i]==PATHSEPERATOR) break;
247                 if (isdigit(string[i])) {
248                         if (found){
249                                 nums=i;
250                         } else{
251                                 nume=i;
252                                 nums=i;
253                                 found=TRUE;
254                         }
255                 } else{
256                         if (found) break;
257                 }
258         }
259         if (found){
260                 strcpy(tail ,&string[nume+1]);
261                 strcpy(head, string);
262                 head[nums]= '\0';
263                 *numlen=nume-nums+1;
264                 return ((int)atoi(&(string[nums])));
265         }
266         tail[0]= '\0';
267         strcpy(head, string);
268         *numlen=0;
269         return TRUE;
270 }
271
272
273 static void an_stringenc(char *string, const char *head, const char *tail, unsigned short numlen, int pic) {
274         BLI_stringenc(string, head, tail, numlen, pic);
275 }
276
277 static void free_anim_avi (struct anim *anim) {
278 #if defined(_WIN32) && !defined(FREE_WINDOWS)
279         int i;
280 #endif
281
282         if (anim == NULL) return;
283         if (anim->avi == NULL) return;
284
285         AVI_close (anim->avi);
286         MEM_freeN (anim->avi);
287         anim->avi = NULL;
288
289 #if defined(_WIN32) && !defined(FREE_WINDOWS)
290
291         if (anim->pgf) {
292                 AVIStreamGetFrameClose(anim->pgf);
293                 anim->pgf = NULL;
294         }
295
296         for (i = 0; i < anim->avistreams; i++){
297                 AVIStreamRelease(anim->pavi[i]);
298         }
299         anim->avistreams = 0;
300
301         if (anim->pfileopen) {
302                 AVIFileRelease(anim->pfile);
303                 anim->pfileopen = 0;
304                 AVIFileExit();
305         }
306 #endif
307
308         anim->duration = 0;
309 }
310
311 #ifdef WITH_FFMPEG
312 static void free_anim_ffmpeg(struct anim * anim);
313 #endif
314 #ifdef WITH_REDCODE
315 static void free_anim_redcode(struct anim * anim);
316 #endif
317
318 void IMB_free_anim(struct anim * anim) {
319         if (anim == NULL) {
320                 printf("free anim, anim == NULL\n");
321                 return;
322         }
323
324         free_anim_movie(anim);
325         free_anim_avi(anim);
326
327 #ifdef WITH_QUICKTIME
328         free_anim_quicktime(anim);
329 #endif
330 #ifdef WITH_FFMPEG
331         free_anim_ffmpeg(anim);
332 #endif
333 #ifdef WITH_REDCODE
334         free_anim_redcode(anim);
335 #endif
336         IMB_free_indices(anim);
337
338         MEM_freeN(anim);
339 }
340
341 void IMB_close_anim(struct anim * anim) {
342         if (anim == NULL) return;
343
344         IMB_free_anim(anim);
345 }
346
347
348 struct anim * IMB_open_anim( const char * name, int ib_flags) {
349         struct anim * anim;
350
351         anim = (struct anim*)MEM_callocN(sizeof(struct anim), "anim struct");
352         if (anim != NULL) {
353                 strcpy(anim->name, name);  /* fixme: possible buffer overflow here? */
354                 anim->ib_flags = ib_flags;
355         }
356         return(anim);
357 }
358
359
360 static int startavi (struct anim *anim) {
361
362         AviError avierror;
363 #if defined(_WIN32) && !defined(FREE_WINDOWS)
364         HRESULT hr;
365         int i, firstvideo = -1;
366         BYTE abFormat[1024];
367         LONG l;
368         LPBITMAPINFOHEADER lpbi;
369         AVISTREAMINFO avis;
370 #endif
371
372         anim->avi = MEM_callocN (sizeof(AviMovie),"animavi");
373
374         if (anim->avi == NULL) {
375                 printf("Can't open avi: %s\n", anim->name);
376                 return -1;
377         }
378
379         avierror = AVI_open_movie (anim->name, anim->avi);
380
381 #if defined(_WIN32) && !defined(FREE_WINDOWS)
382         if (avierror == AVI_ERROR_COMPRESSION) {
383                 AVIFileInit();
384                 hr = AVIFileOpen(&anim->pfile, anim->name, OF_READ, 0L);
385                 if (hr == 0) {
386                         anim->pfileopen = 1;
387                         for (i = 0; i < MAXNUMSTREAMS; i++) {
388                                 if (AVIFileGetStream(anim->pfile, &anim->pavi[i], 0L, i) != AVIERR_OK) {
389                                         break;
390                                 }
391                                 
392                                 AVIStreamInfo(anim->pavi[i], &avis, sizeof(avis));
393                                 if ((avis.fccType == streamtypeVIDEO) && (firstvideo == -1)) {
394                                         anim->pgf = AVIStreamGetFrameOpen(anim->pavi[i], NULL);
395                                         if (anim->pgf) {
396                                                 firstvideo = i;
397
398                                                 // get stream length
399                                                 anim->avi->header->TotalFrames = AVIStreamLength(anim->pavi[i]);
400                                                 
401                                                 // get information about images inside the stream
402                                                 l = sizeof(abFormat);
403                                                 AVIStreamReadFormat(anim->pavi[i], 0, &abFormat, &l);
404                                                 lpbi = (LPBITMAPINFOHEADER)abFormat;
405                                                 anim->avi->header->Height = lpbi->biHeight;
406                                                 anim->avi->header->Width = lpbi->biWidth;
407                                         } else {
408                                                 FIXCC(avis.fccHandler);
409                                                 FIXCC(avis.fccType);
410                                                 printf("Can't find AVI decoder for type : %4.4hs/%4.4hs\n",
411                                                         (LPSTR)&avis.fccType,
412                                                         (LPSTR)&avis.fccHandler);
413                                         }
414                                 }
415                         }
416
417                         // register number of opened avistreams
418                         anim->avistreams = i;
419
420                         //
421                         // Couldn't get any video streams out of this file
422                         //
423                         if ((anim->avistreams == 0) || (firstvideo == -1)) {
424                                 avierror = AVI_ERROR_FORMAT;
425                         } else {
426                                 avierror = AVI_ERROR_NONE;
427                                 anim->firstvideo = firstvideo;
428                         }
429                 } else {
430                         AVIFileExit();
431                 }
432         }
433 #endif
434
435         if (avierror != AVI_ERROR_NONE) {
436                 AVI_print_error(avierror);
437                 printf ("Error loading avi: %s\n", anim->name);         
438                 free_anim_avi(anim);
439                 return -1;
440         }
441         
442         anim->duration = anim->avi->header->TotalFrames;
443         anim->params = NULL;
444
445         anim->x = anim->avi->header->Width;
446         anim->y = anim->avi->header->Height;
447         anim->interlacing = 0;
448         anim->orientation = 0;
449         anim->framesize = anim->x * anim->y * 4;
450
451         anim->curposition = 0;
452         anim->preseek = 0;
453
454         /*  printf("x:%d y:%d size:%d interl:%d dur:%d\n", anim->x, anim->y, anim->framesize, anim->interlacing, anim->duration);*/
455
456         return 0;
457 }
458
459 static ImBuf * avi_fetchibuf (struct anim *anim, int position) {
460         ImBuf *ibuf = NULL;
461         int *tmp;
462         int y;
463         
464         if (anim == NULL) return (NULL);
465
466 #if defined(_WIN32) && !defined(FREE_WINDOWS)
467         if (anim->avistreams) {
468                 LPBITMAPINFOHEADER lpbi;
469
470                 if (anim->pgf) {
471                         lpbi = AVIStreamGetFrame(anim->pgf, position + AVIStreamStart(anim->pavi[anim->firstvideo]));
472                         if (lpbi) {
473                                 ibuf = IMB_ibImageFromMemory((unsigned char *) lpbi, 100, IB_rect);
474 //Oh brother...
475                         }
476                 }
477         } else {
478 #else
479         if (1) {
480 #endif
481                 ibuf = IMB_allocImBuf (anim->x, anim->y, 24, IB_rect);
482
483                 tmp = AVI_read_frame (anim->avi, AVI_FORMAT_RGB32, position,
484                         AVI_get_stream(anim->avi, AVIST_VIDEO, 0));
485                 
486                 if (tmp == NULL) {
487                         printf ("Error reading frame from AVI");
488                         IMB_freeImBuf (ibuf);
489                         return NULL;
490                 }
491
492                 for (y=0; y < anim->y; y++) {
493                         memcpy (&(ibuf->rect)[((anim->y-y)-1)*anim->x],  &tmp[y*anim->x],  
494                                 anim->x * 4);
495                 }
496                 
497                 MEM_freeN (tmp);
498         }
499         
500         ibuf->profile = IB_PROFILE_SRGB;
501         
502         return ibuf;
503 }
504
505 #ifdef WITH_FFMPEG
506
507 extern void do_init_ffmpeg(void);
508
509 static int startffmpeg(struct anim * anim) {
510         int            i, videoStream;
511
512         AVCodec *pCodec;
513         AVFormatContext *pFormatCtx;
514         AVCodecContext *pCodecCtx;
515         int frs_num;
516         double frs_den;
517
518 #ifdef FFMPEG_SWSCALE_COLOR_SPACE_SUPPORT
519         /* The following for color space determination */
520         int srcRange, dstRange, brightness, contrast, saturation;
521         int *table;
522         const int *inv_table;
523 #endif
524
525         if (anim == 0) return(-1);
526
527         do_init_ffmpeg();
528
529         if(av_open_input_file(&pFormatCtx, anim->name, NULL, 0, NULL)!=0) {
530                 return -1;
531         }
532
533         if(av_find_stream_info(pFormatCtx)<0) {
534                 av_close_input_file(pFormatCtx);
535                 return -1;
536         }
537
538         av_dump_format(pFormatCtx, 0, anim->name, 0);
539
540
541                 /* Find the first video stream */
542         videoStream=-1;
543         for(i=0; i<pFormatCtx->nb_streams; i++)
544                 if(pFormatCtx->streams[i]->codec->codec_type
545                    == AVMEDIA_TYPE_VIDEO) {
546                         videoStream=i;
547                         break;
548                 }
549
550         if(videoStream==-1) {
551                 av_close_input_file(pFormatCtx);
552                 return -1;
553         }
554
555         pCodecCtx = pFormatCtx->streams[videoStream]->codec;
556
557                 /* Find the decoder for the video stream */
558         pCodec=avcodec_find_decoder(pCodecCtx->codec_id);
559         if(pCodec==NULL) {
560                 av_close_input_file(pFormatCtx);
561                 return -1;
562         }
563
564         pCodecCtx->workaround_bugs = 1;
565
566         if(avcodec_open(pCodecCtx, pCodec)<0) {
567                 av_close_input_file(pFormatCtx);
568                 return -1;
569         }
570
571         anim->duration = ceil(pFormatCtx->duration
572                 * av_q2d(pFormatCtx->streams[videoStream]->r_frame_rate) 
573                 / AV_TIME_BASE);
574
575         frs_num = pFormatCtx->streams[videoStream]->r_frame_rate.num;
576         frs_den = pFormatCtx->streams[videoStream]->r_frame_rate.den;
577
578         frs_den *= AV_TIME_BASE;
579
580         while (frs_num % 10 == 0 && frs_den >= 2.0 && frs_num > 10) {
581                 frs_num /= 10;
582                 frs_den /= 10;
583         }
584
585         anim->frs_sec = frs_num;
586         anim->frs_sec_base = frs_den;
587
588         anim->params = 0;
589
590         anim->x = pCodecCtx->width;
591         anim->y = pCodecCtx->height;
592         anim->interlacing = 0;
593         anim->orientation = 0;
594         anim->framesize = anim->x * anim->y * 4;
595
596         anim->curposition = -1;
597         anim->last_frame = 0;
598         anim->last_pts = -1;
599         anim->next_pts = -1;
600         anim->next_undecoded_pts = -1;
601         anim->next_packet.stream_index = -1;
602
603         anim->pFormatCtx = pFormatCtx;
604         anim->pCodecCtx = pCodecCtx;
605         anim->pCodec = pCodec;
606         anim->videoStream = videoStream;
607
608         anim->pFrame = avcodec_alloc_frame();
609         anim->pFrameDeinterlaced = avcodec_alloc_frame();
610         anim->pFrameRGB = avcodec_alloc_frame();
611
612         if (avpicture_get_size(PIX_FMT_RGBA, anim->x, anim->y)
613                 != anim->x * anim->y * 4) {
614                 fprintf (stderr,
615                          "ffmpeg has changed alloc scheme ... ARGHHH!\n");
616                 avcodec_close(anim->pCodecCtx);
617                 av_close_input_file(anim->pFormatCtx);
618                 av_free(anim->pFrameRGB);
619                 av_free(anim->pFrameDeinterlaced);
620                 av_free(anim->pFrame);
621                 anim->pCodecCtx = NULL;
622                 return -1;
623         }
624
625         if (anim->ib_flags & IB_animdeinterlace) {
626                 avpicture_fill((AVPicture*) anim->pFrameDeinterlaced, 
627                                    MEM_callocN(avpicture_get_size(
628                                                    anim->pCodecCtx->pix_fmt,
629                                                    anim->x, anim->y), 
630                                            "ffmpeg deinterlace"), 
631                                    anim->pCodecCtx->pix_fmt, anim->x, anim->y);
632         }
633
634         if (pCodecCtx->has_b_frames) {
635                 anim->preseek = 25; /* FIXME: detect gopsize ... */
636         } else {
637                 anim->preseek = 0;
638         }
639         
640         anim->img_convert_ctx = sws_getContext(
641                 anim->pCodecCtx->width,
642                 anim->pCodecCtx->height,
643                 anim->pCodecCtx->pix_fmt,
644                 anim->pCodecCtx->width,
645                 anim->pCodecCtx->height,
646                 PIX_FMT_RGBA,
647                 SWS_FAST_BILINEAR | SWS_PRINT_INFO,
648                 NULL, NULL, NULL);
649                 
650         if (!anim->img_convert_ctx) {
651                 fprintf (stderr,
652                          "Can't transform color space??? Bailing out...\n");
653                 avcodec_close(anim->pCodecCtx);
654                 av_close_input_file(anim->pFormatCtx);
655                 av_free(anim->pFrameRGB);
656                 av_free(anim->pFrameDeinterlaced);
657                 av_free(anim->pFrame);
658                 anim->pCodecCtx = NULL;
659                 return -1;
660         }
661
662 #ifdef FFMPEG_SWSCALE_COLOR_SPACE_SUPPORT
663         /* Try do detect if input has 0-255 YCbCR range (JFIF Jpeg MotionJpeg) */
664         if (!sws_getColorspaceDetails(anim->img_convert_ctx, (int**)&inv_table, &srcRange,
665                 &table, &dstRange, &brightness, &contrast, &saturation)) {
666
667                 srcRange = srcRange || anim->pCodecCtx->color_range == AVCOL_RANGE_JPEG;
668                 inv_table = sws_getCoefficients(anim->pCodecCtx->colorspace);
669
670                 if(sws_setColorspaceDetails(anim->img_convert_ctx, (int *)inv_table, srcRange,
671                         table, dstRange, brightness, contrast, saturation)) {
672
673                         printf("Warning: Could not set libswscale colorspace details.\n");
674                         }
675         }
676         else {
677                 printf("Warning: Could not set libswscale colorspace details.\n");
678         }
679 #endif
680                 
681         return (0);
682 }
683
684 /* postprocess the image in anim->pFrame and do color conversion
685    and deinterlacing stuff.
686
687    Output is anim->last_frame
688 */
689
690 static void ffmpeg_postprocess(struct anim * anim)
691 {
692         AVFrame * input = anim->pFrame;
693         ImBuf * ibuf = anim->last_frame;
694         int filter_y = 0;
695
696         ibuf->profile = IB_PROFILE_SRGB;
697
698         /* This means the data wasnt read properly, 
699            this check stops crashing */
700         if (input->data[0]==0 && input->data[1]==0 
701             && input->data[2]==0 && input->data[3]==0){
702                 fprintf(stderr, "ffmpeg_fetchibuf: "
703                         "data not read properly...\n");
704                 return;
705         }
706
707         if (anim->ib_flags & IB_animdeinterlace) {
708                 if (avpicture_deinterlace(
709                             (AVPicture*) 
710                             anim->pFrameDeinterlaced,
711                             (const AVPicture*)
712                             anim->pFrame,
713                             anim->pCodecCtx->pix_fmt,
714                             anim->pCodecCtx->width,
715                             anim->pCodecCtx->height)
716                     < 0) {
717                         filter_y = TRUE;
718                 } else {
719                         input = anim->pFrameDeinterlaced;
720                 }
721         }
722         
723         avpicture_fill((AVPicture*) anim->pFrameRGB, 
724                        (unsigned char*) ibuf->rect, 
725                        PIX_FMT_RGBA, anim->x, anim->y);
726
727         if (ENDIAN_ORDER == B_ENDIAN) {
728                 int * dstStride   = anim->pFrameRGB->linesize;
729                 uint8_t** dst     = anim->pFrameRGB->data;
730                 int dstStride2[4] = { dstStride[0], 0, 0, 0 };
731                 uint8_t* dst2[4]  = { dst[0], 0, 0, 0 };
732                 int x,y,h,w;
733                 unsigned char* bottom;
734                 unsigned char* top;
735                 
736                 sws_scale(anim->img_convert_ctx,
737                           (const uint8_t * const *)input->data,
738                           input->linesize,
739                           0,
740                           anim->pCodecCtx->height,
741                           dst2,
742                           dstStride2);
743                 
744                 /* workaround: sws_scale bug
745                    sets alpha = 0 and compensate
746                    for altivec-bugs and flipy... */
747                 
748                 bottom = (unsigned char*) ibuf->rect;
749                 top = bottom + ibuf->x * (ibuf->y-1) * 4;
750                 
751                 h = (ibuf->y + 1) / 2;
752                 w = ibuf->x;
753                 
754                 for (y = 0; y < h; y++) {
755                         unsigned char tmp[4];
756                         unsigned int * tmp_l =
757                                 (unsigned int*) tmp;
758                         tmp[3] = 0xff;
759                         
760                         for (x = 0; x < w; x++) {
761                                 tmp[0] = bottom[0];
762                                 tmp[1] = bottom[1];
763                                 tmp[2] = bottom[2];
764                                 
765                                 bottom[0] = top[0];
766                                 bottom[1] = top[1];
767                                 bottom[2] = top[2];
768                                 bottom[3] = 0xff;
769                                 
770                                 *(unsigned int*) top = *tmp_l;
771                                 
772                                 bottom +=4;
773                                 top += 4;
774                         }
775                         top -= 8 * w;
776                 }
777         } else {
778                 int * dstStride   = anim->pFrameRGB->linesize;
779                 uint8_t** dst     = anim->pFrameRGB->data;
780                 int dstStride2[4] = { -dstStride[0], 0, 0, 0 };
781                 uint8_t* dst2[4]  = { dst[0] + (anim->y - 1)*dstStride[0],
782                                       0, 0, 0 };
783                 int i;
784                 unsigned char* r;
785                 
786                 sws_scale(anim->img_convert_ctx,
787                           (const uint8_t * const *)input->data,
788                           input->linesize,
789                           0,
790                           anim->pCodecCtx->height,
791                           dst2,
792                           dstStride2);
793                 
794                 r = (unsigned char*) ibuf->rect;
795                 
796                 /* workaround sws_scale bug: older version of 
797                    sws_scale set alpha = 0... */
798                 if (r[3] == 0) {
799                         for (i = 0; i < ibuf->x * ibuf->y; i++) {
800                                 r[3] = 0xff;
801                                 r += 4;
802                         }
803                 }
804         }
805
806         if (filter_y) {
807                 IMB_filtery(ibuf);
808         }
809 }
810
811 /* decode one video frame and load the next packet into anim->packet,
812    so that we can obtain next_pts and next undecoded pts */
813
814 static int ffmpeg_decode_video_frame(struct anim * anim)
815 {
816         int frameFinished = 0;
817         int rval = 0;
818
819         av_log(anim->pFormatCtx, AV_LOG_DEBUG, "  DECODE VIDEO FRAME\n");
820
821         anim->next_undecoded_pts = -1;
822
823         if (anim->next_packet.stream_index == anim->videoStream) {
824                 av_log(anim->pFormatCtx, AV_LOG_DEBUG, 
825                        "  DECODE: cached next packet\n");
826
827                 avcodec_decode_video2(anim->pCodecCtx, 
828                                       anim->pFrame, &frameFinished, 
829                                       &anim->next_packet);
830
831                 if (frameFinished) {
832                         av_log(anim->pFormatCtx, 
833                                AV_LOG_DEBUG, 
834                                "  FRAME DONE: "
835                                 "next_pts=%lld pkt_pts=%lld\n",
836                                 (anim->pFrame->pts == AV_NOPTS_VALUE) ? 
837                                 -1 : anim->pFrame->pts, 
838                                 (anim->pFrame->pkt_pts == AV_NOPTS_VALUE) ?
839                                 -1 : anim->pFrame->pkt_pts);
840                         anim->next_pts = 
841                                 av_get_pts_from_frame(anim->pFormatCtx,
842                                                       anim->pFrame);
843                 }
844
845                 av_free_packet(&anim->next_packet);
846                 anim->next_packet.stream_index = -1;
847         }
848         
849         while((rval = av_read_frame(anim->pFormatCtx, &anim->next_packet)) >= 0) {
850                 av_log(anim->pFormatCtx, 
851                        AV_LOG_DEBUG, 
852                        "%sREAD: strID=%d (VID: %d) dts=%lld pts=%lld "
853                        "%s\n",
854                        (anim->next_packet.stream_index == anim->videoStream)
855                        ? "->" : "  ",
856                        anim->next_packet.stream_index, 
857                        anim->videoStream,
858                        (anim->next_packet.dts == AV_NOPTS_VALUE) ? -1:
859                        anim->next_packet.dts,
860                        (anim->next_packet.pts == AV_NOPTS_VALUE) ? -1:
861                        anim->next_packet.pts,
862                        (anim->next_packet.flags & AV_PKT_FLAG_KEY) ? 
863                        " KEY" : "");
864                 if (anim->next_packet.stream_index == anim->videoStream) {
865                         if (frameFinished) {
866                                 av_log(anim->pFormatCtx,
867                                        AV_LOG_DEBUG,
868                                        "  FRAME finished, we leave\n");
869                                 anim->next_undecoded_pts 
870                                         = anim->next_packet.dts;
871                                 break;
872                         }
873
874                         avcodec_decode_video2(
875                                 anim->pCodecCtx, 
876                                 anim->pFrame, &frameFinished, 
877                                 &anim->next_packet);
878
879                         if (frameFinished) {
880                                 anim->next_pts = av_get_pts_from_frame(
881                                         anim->pFormatCtx, anim->pFrame);
882
883                                 av_log(anim->pFormatCtx,
884                                        AV_LOG_DEBUG,
885                                        "  FRAME DONE: next_pts=%lld "
886                                        "pkt_pts=%lld, guessed_pts=%lld\n",
887                                        (anim->pFrame->pts == AV_NOPTS_VALUE) ?
888                                        -1 : anim->pFrame->pts, 
889                                        (anim->pFrame->pkt_pts 
890                                         == AV_NOPTS_VALUE) ?
891                                        -1 : anim->pFrame->pkt_pts,
892                                         anim->next_pts);
893                         }
894                 }
895                 av_free_packet(&anim->next_packet);
896                 anim->next_packet.stream_index = -1;
897         }
898         
899         if (rval < 0) {
900                 av_log(anim->pFormatCtx,
901                        AV_LOG_ERROR, "  DECODE READ FAILED: av_read_frame() "
902                        "returned error: %d\n",  rval);
903         }
904         return (rval >= 0);
905 }
906
907 static void ffmpeg_decode_video_frame_scan(
908         struct anim * anim, int64_t pts_to_search)
909 {
910         /* there seem to exist *very* silly GOP lengths out in the wild... */
911         int count = 1000;
912
913         av_log(anim->pFormatCtx,
914                AV_LOG_DEBUG, 
915                "SCAN start: considering pts=%lld in search of %lld\n", 
916                anim->next_pts, pts_to_search);
917
918         while (count > 0 && anim->next_pts < pts_to_search) {
919                 av_log(anim->pFormatCtx,
920                        AV_LOG_DEBUG, 
921                        "  WHILE: pts=%lld in search of %lld\n", 
922                        anim->next_pts, pts_to_search);
923                 if (!ffmpeg_decode_video_frame(anim)) {
924                         break;
925                 }
926                 count--;
927         }
928         if (count == 0) {
929                 av_log(anim->pFormatCtx,
930                        AV_LOG_ERROR, 
931                        "SCAN failed: completely lost in stream, "
932                        "bailing out at PTS=%lld, searching for PTS=%lld\n", 
933                        anim->next_pts, pts_to_search);
934         }
935         if (anim->next_pts == pts_to_search) {
936                 av_log(anim->pFormatCtx,
937                        AV_LOG_DEBUG, "SCAN HAPPY: we found our PTS!\n");
938         } else {
939                 av_log(anim->pFormatCtx,
940                        AV_LOG_ERROR, "SCAN UNHAPPY: PTS not matched!\n");
941         }
942 }
943
944 static int match_format(const char *name, AVFormatContext * pFormatCtx)
945 {
946         const char *p;
947         int len, namelen;
948
949         const char *names = pFormatCtx->iformat->name;
950
951         if (!name || !names)
952                 return 0;
953
954         namelen = strlen(name);
955         while ((p = strchr(names, ','))) {
956                 len = MAX2(p - names, namelen);
957                 if (!BLI_strncasecmp(name, names, len))
958                         return 1;
959                 names = p+1;
960         }
961         return !BLI_strcasecmp(name, names);
962 }
963
964 static int ffmpeg_seek_by_byte(AVFormatContext *pFormatCtx)
965 {
966         static const char * byte_seek_list [] = { "dv", "mpegts", 0 };
967         const char ** p;
968
969         if (pFormatCtx->iformat->flags & AVFMT_TS_DISCONT) {
970                 return TRUE;
971         }
972
973         p = byte_seek_list;
974
975         while (*p) {
976                 if (match_format(*p++, pFormatCtx)) {
977                         return TRUE;
978                 }
979         }
980
981         return FALSE;
982 }
983
984 static ImBuf * ffmpeg_fetchibuf(struct anim * anim, int position,
985                                 IMB_Timecode_Type tc) {
986         int64_t pts_to_search = 0;
987         double frame_rate;
988         double pts_time_base;
989         long long st_time; 
990         struct anim_index * tc_index = 0;
991         AVStream * v_st;
992         int new_frame_index;
993         int old_frame_index;
994
995         if (anim == 0) return (0);
996
997         av_log(anim->pFormatCtx, AV_LOG_DEBUG, "FETCH: pos=%d\n", position);
998
999         if (tc != IMB_TC_NONE) {
1000                 tc_index = IMB_anim_open_index(anim, tc);
1001         }
1002
1003         v_st = anim->pFormatCtx->streams[anim->videoStream];
1004
1005         frame_rate = av_q2d(v_st->r_frame_rate);
1006
1007         st_time = anim->pFormatCtx->start_time;
1008         pts_time_base = av_q2d(v_st->time_base);
1009
1010         if (tc_index) {
1011                 new_frame_index = IMB_indexer_get_frame_index(
1012                         tc_index, position);
1013                 old_frame_index = IMB_indexer_get_frame_index(
1014                         tc_index, anim->curposition);
1015                 pts_to_search = IMB_indexer_get_pts(
1016                         tc_index, new_frame_index);
1017         } else {
1018                 pts_to_search = (long long) 
1019                         floor(((double) position) / pts_time_base / frame_rate + 0.5);
1020
1021                 if (st_time != AV_NOPTS_VALUE) {
1022                         pts_to_search += st_time / pts_time_base 
1023                                 / AV_TIME_BASE;
1024                 }
1025         }
1026
1027         av_log(anim->pFormatCtx, AV_LOG_DEBUG, 
1028                "FETCH: looking for PTS=%lld "
1029                "(pts_timebase=%g, frame_rate=%g, st_time=%lld)\n", 
1030                pts_to_search, pts_time_base, frame_rate, st_time);
1031
1032         if (anim->last_frame && 
1033             anim->last_pts <= pts_to_search && anim->next_pts > pts_to_search){
1034                 av_log(anim->pFormatCtx, AV_LOG_DEBUG, 
1035                        "FETCH: frame repeat: last: %lld next: %lld\n",
1036                        anim->last_pts, anim->next_pts);
1037                 IMB_refImBuf(anim->last_frame);
1038                 anim->curposition = position;
1039                 return anim->last_frame;
1040         }
1041          
1042         IMB_freeImBuf(anim->last_frame);
1043
1044         if (anim->next_pts <= pts_to_search && 
1045             anim->next_undecoded_pts > pts_to_search) {
1046                 av_log(anim->pFormatCtx, AV_LOG_DEBUG, 
1047                        "FETCH: no seek necessary: "
1048                         "next: %lld next undecoded: %lld\n",
1049                         anim->next_pts, anim->next_undecoded_pts);
1050
1051                 /* we are already done :) */
1052
1053         } else if (position > anim->curposition + 1 
1054                    && anim->preseek 
1055                    && !tc_index
1056                    && position - (anim->curposition + 1) < anim->preseek) {
1057
1058                 av_log(anim->pFormatCtx, AV_LOG_DEBUG, 
1059                        "FETCH: within preseek interval (no index)\n");
1060
1061                 ffmpeg_decode_video_frame_scan(anim, pts_to_search);
1062         } else if (tc_index && 
1063                    IMB_indexer_can_scan(tc_index, old_frame_index,
1064                                         new_frame_index)) {
1065
1066                 av_log(anim->pFormatCtx, AV_LOG_DEBUG, 
1067                        "FETCH: within preseek interval "
1068                        "(index tells us)\n");
1069
1070                 ffmpeg_decode_video_frame_scan(anim, pts_to_search);
1071         } else if (position != anim->curposition + 1) { 
1072                 long long pos;
1073                 int ret;
1074
1075                 if (tc_index) {
1076                         unsigned long long dts;
1077
1078                         pos = IMB_indexer_get_seek_pos(
1079                                 tc_index, new_frame_index);
1080                         dts = IMB_indexer_get_seek_pos_dts(
1081                                 tc_index, new_frame_index);
1082
1083                         av_log(anim->pFormatCtx, AV_LOG_DEBUG, 
1084                                "TC INDEX seek pos = %lld\n", pos);
1085                         av_log(anim->pFormatCtx, AV_LOG_DEBUG, 
1086                                "TC INDEX seek dts = %lld\n", dts);
1087
1088                         if (ffmpeg_seek_by_byte(anim->pFormatCtx)) {
1089                                 av_log(anim->pFormatCtx, AV_LOG_DEBUG, 
1090                                        "... using BYTE pos\n");
1091
1092                                 ret = av_seek_frame(anim->pFormatCtx, 
1093                                                     -1,
1094                                                     pos, AVSEEK_FLAG_BYTE);
1095                                 av_update_cur_dts(anim->pFormatCtx, v_st, dts);
1096                         } else {
1097                                 av_log(anim->pFormatCtx, AV_LOG_DEBUG, 
1098                                        "... using DTS pos\n");
1099                                 ret = av_seek_frame(anim->pFormatCtx, 
1100                                                     anim->videoStream, 
1101                                                     dts, AVSEEK_FLAG_BACKWARD);
1102                         }
1103                 } else {
1104                         pos = (long long) (position - anim->preseek) 
1105                                 * AV_TIME_BASE / frame_rate;
1106                         if (pos < 0) {
1107                                 pos = 0;
1108                         }
1109                 
1110                         if (st_time != AV_NOPTS_VALUE) {
1111                                 pos += st_time;
1112                         }
1113
1114                         ret = av_seek_frame(anim->pFormatCtx, -1, 
1115                                             pos, AVSEEK_FLAG_BACKWARD);
1116                 }
1117
1118                 if (ret < 0) {
1119                         av_log(anim->pFormatCtx, AV_LOG_ERROR, 
1120                                "FETCH: "
1121                                "error while seeking to DTS = %lld "
1122                                "(frameno = %d, PTS = %lld): errcode = %d\n", 
1123                                pos, position, pts_to_search, ret);
1124                 }
1125
1126                 avcodec_flush_buffers(anim->pCodecCtx);
1127
1128                 anim->next_pts = -1;
1129
1130                 if (anim->next_packet.stream_index == anim->videoStream) {
1131                         av_free_packet(&anim->next_packet);
1132                         anim->next_packet.stream_index = -1;
1133                 }
1134
1135                 /* memset(anim->pFrame,...) ?? */
1136
1137                 if (ret >= 0) {
1138                         ffmpeg_decode_video_frame_scan(anim, pts_to_search);
1139                 }
1140         } else if (position == 0 && anim->curposition == -1) {
1141                 /* first frame without seeking special case... */
1142                 ffmpeg_decode_video_frame(anim);
1143         }
1144
1145         anim->last_frame = IMB_allocImBuf(anim->x, anim->y, 32, IB_rect);
1146
1147         ffmpeg_postprocess(anim);
1148         
1149         anim->last_pts = anim->next_pts;
1150         
1151         ffmpeg_decode_video_frame(anim);
1152         
1153         anim->curposition = position;
1154         
1155         IMB_refImBuf(anim->last_frame);
1156         
1157         return anim->last_frame;
1158 }
1159
1160 static void free_anim_ffmpeg(struct anim * anim) {
1161         if (anim == NULL) return;
1162
1163         if (anim->pCodecCtx) {
1164                 avcodec_close(anim->pCodecCtx);
1165                 av_close_input_file(anim->pFormatCtx);
1166                 av_free(anim->pFrameRGB);
1167                 av_free(anim->pFrame);
1168
1169                 if (anim->ib_flags & IB_animdeinterlace) {
1170                         MEM_freeN(anim->pFrameDeinterlaced->data[0]);
1171                 }
1172                 av_free(anim->pFrameDeinterlaced);
1173                 sws_freeContext(anim->img_convert_ctx);
1174                 IMB_freeImBuf(anim->last_frame);
1175                 if (anim->next_packet.stream_index != -1) {
1176                         av_free_packet(&anim->next_packet);
1177                 }
1178         }
1179         anim->duration = 0;
1180 }
1181
1182 #endif
1183
1184 #ifdef WITH_REDCODE
1185
1186 static int startredcode(struct anim * anim) {
1187         anim->redcodeCtx = redcode_open(anim->name);
1188         if (!anim->redcodeCtx) {
1189                 return -1;
1190         }
1191         anim->duration = redcode_get_length(anim->redcodeCtx);
1192         
1193         return 0;
1194 }
1195
1196 static ImBuf * redcode_fetchibuf(struct anim * anim, int position) {
1197         struct ImBuf * ibuf;
1198         struct redcode_frame * frame;
1199         struct redcode_frame_raw * raw_frame;
1200
1201         if (!anim->redcodeCtx) {
1202                 return NULL;
1203         }
1204
1205         frame = redcode_read_video_frame(anim->redcodeCtx, position);
1206         
1207         if (!frame) {
1208                 return NULL;
1209         }
1210
1211         raw_frame = redcode_decode_video_raw(frame, 1);
1212
1213         redcode_free_frame(frame);
1214
1215         if (!raw_frame) {
1216                 return NULL;
1217         }
1218         
1219                 ibuf = IMB_allocImBuf(raw_frame->width * 2, 
1220                                   raw_frame->height * 2, 32, IB_rectfloat);
1221
1222         redcode_decode_video_float(raw_frame, ibuf->rect_float, 1);
1223
1224         return ibuf;
1225 }
1226
1227 static void free_anim_redcode(struct anim * anim) {
1228         if (anim->redcodeCtx) {
1229                 redcode_close(anim->redcodeCtx);
1230                 anim->redcodeCtx = 0;
1231         }
1232         anim->duration = 0;
1233 }
1234
1235 #endif
1236
1237 /* probeer volgende plaatje te lezen */
1238 /* Geen plaatje, probeer dan volgende animatie te openen */
1239 /* gelukt, haal dan eerste plaatje van animatie */
1240
1241 static struct ImBuf * anim_getnew(struct anim * anim) {
1242         struct ImBuf *ibuf = NULL;
1243
1244         if (anim == NULL) return(NULL);
1245
1246         free_anim_movie(anim);
1247         free_anim_avi(anim);
1248 #ifdef WITH_QUICKTIME
1249         free_anim_quicktime(anim);
1250 #endif
1251 #ifdef WITH_FFMPEG
1252         free_anim_ffmpeg(anim);
1253 #endif
1254 #ifdef WITH_REDCODE
1255         free_anim_redcode(anim);
1256 #endif
1257
1258
1259         if (anim->curtype != 0) return (NULL);
1260         anim->curtype = imb_get_anim_type(anim->name);  
1261
1262         switch (anim->curtype) {
1263         case ANIM_SEQUENCE:
1264                 ibuf = IMB_loadiffname(anim->name, anim->ib_flags);
1265                 if (ibuf) {
1266                         strcpy(anim->first, anim->name);
1267                         anim->duration = 1;
1268                 }
1269                 break;
1270         case ANIM_MOVIE:
1271                 if (startmovie(anim)) return (NULL);
1272                 ibuf = IMB_allocImBuf (anim->x, anim->y, 24, 0); /* fake */
1273                 break;
1274         case ANIM_AVI:
1275                 if (startavi(anim)) {
1276                         printf("couldnt start avi\n"); 
1277                         return (NULL);
1278                 }
1279                 ibuf = IMB_allocImBuf (anim->x, anim->y, 24, 0);
1280                 break;
1281 #ifdef WITH_QUICKTIME
1282         case ANIM_QTIME:
1283                 if (startquicktime(anim)) return (0);
1284                 ibuf = IMB_allocImBuf (anim->x, anim->y, 24, 0);
1285                 break;
1286 #endif
1287 #ifdef WITH_FFMPEG
1288         case ANIM_FFMPEG:
1289                 if (startffmpeg(anim)) return (0);
1290                 ibuf = IMB_allocImBuf (anim->x, anim->y, 24, 0);
1291                 break;
1292 #endif
1293 #ifdef WITH_REDCODE
1294         case ANIM_REDCODE:
1295                 if (startredcode(anim)) return (0);
1296                 ibuf = IMB_allocImBuf (8, 8, 32, 0);
1297                 break;
1298 #endif
1299         }
1300         return(ibuf);
1301 }
1302
1303 struct ImBuf * IMB_anim_previewframe(struct anim * anim) {
1304         struct ImBuf * ibuf = NULL;
1305         int position = 0;
1306         
1307         ibuf = IMB_anim_absolute(anim, 0, IMB_TC_NONE, IMB_PROXY_NONE);
1308         if (ibuf) {
1309                 IMB_freeImBuf(ibuf);
1310                 position = anim->duration / 2;
1311                 ibuf = IMB_anim_absolute(anim, position, IMB_TC_NONE,
1312                                          IMB_PROXY_NONE);
1313         }
1314         return ibuf;
1315 }
1316
1317 struct ImBuf * IMB_anim_absolute(struct anim * anim, int position,
1318                                  IMB_Timecode_Type tc,
1319                                  IMB_Proxy_Size preview_size) {
1320         struct ImBuf * ibuf = NULL;
1321         char head[256], tail[256];
1322         unsigned short digits;
1323         int pic;
1324         int filter_y;
1325         if (anim == NULL) return(NULL);
1326
1327         filter_y = (anim->ib_flags & IB_animdeinterlace);
1328
1329         if (anim->curtype == 0) {
1330                 ibuf = anim_getnew(anim);
1331                 if (ibuf == NULL) {
1332                         return(NULL);
1333                 }
1334
1335                 IMB_freeImBuf(ibuf); /* ???? */
1336                 ibuf= NULL;
1337         }
1338
1339         if (position < 0) return(NULL);
1340         if (position >= anim->duration) return(NULL);
1341
1342         if (preview_size != IMB_PROXY_NONE) {
1343                 struct anim * proxy = IMB_anim_open_proxy(anim, preview_size);
1344
1345                 if (proxy) {
1346                         position = IMB_anim_index_get_frame_index(
1347                                 anim, tc, position);
1348                         return IMB_anim_absolute(
1349                                 proxy, position,
1350                                 IMB_TC_NONE, IMB_PROXY_NONE);
1351                 }
1352         }
1353
1354         switch(anim->curtype) {
1355         case ANIM_SEQUENCE:
1356                 pic = an_stringdec(anim->first, head, tail, &digits);
1357                 pic += position;
1358                 an_stringenc(anim->name, head, tail, digits, pic);
1359                 ibuf = IMB_loadiffname(anim->name, IB_rect);
1360                 if (ibuf) {
1361                         anim->curposition = position;
1362                 }
1363                 break;
1364         case ANIM_MOVIE:
1365                 ibuf = movie_fetchibuf(anim, position);
1366                 if (ibuf) {
1367                         anim->curposition = position;
1368                         IMB_convert_rgba_to_abgr(ibuf);
1369                         ibuf->profile = IB_PROFILE_SRGB;
1370                 }
1371                 break;
1372         case ANIM_AVI:
1373                 ibuf = avi_fetchibuf(anim, position);
1374                 if (ibuf)
1375                         anim->curposition = position;
1376                 break;
1377 #ifdef WITH_QUICKTIME
1378         case ANIM_QTIME:
1379                 ibuf = qtime_fetchibuf(anim, position);
1380                 if (ibuf)
1381                         anim->curposition = position;
1382                 break;
1383 #endif
1384 #ifdef WITH_FFMPEG
1385         case ANIM_FFMPEG:
1386                 ibuf = ffmpeg_fetchibuf(anim, position, tc);
1387                 if (ibuf)
1388                         anim->curposition = position;
1389                 filter_y = 0; /* done internally */
1390                 break;
1391 #endif
1392 #ifdef WITH_REDCODE
1393         case ANIM_REDCODE:
1394                 ibuf = redcode_fetchibuf(anim, position);
1395                 if (ibuf) anim->curposition = position;
1396                 break;
1397 #endif
1398         }
1399
1400         if (ibuf) {
1401                 if (filter_y) IMB_filtery(ibuf);
1402                 sprintf(ibuf->name, "%s.%04d", anim->name, anim->curposition + 1);
1403                 
1404         }
1405         return(ibuf);
1406 }
1407
1408 /***/
1409
1410 int IMB_anim_get_duration(struct anim *anim, IMB_Timecode_Type tc) {
1411         struct anim_index * idx;
1412         if (tc == IMB_TC_NONE) {
1413                 return anim->duration;
1414         }
1415         
1416         idx = IMB_anim_open_index(anim, tc);
1417         if (!idx) {
1418                 return anim->duration;
1419         }
1420
1421         return IMB_indexer_get_duration(idx);
1422 }
1423
1424 int IMB_anim_get_fps(struct anim * anim, 
1425                      short * frs_sec, float * frs_sec_base)
1426 {
1427         if (anim->frs_sec) {
1428                 *frs_sec = anim->frs_sec;
1429                 *frs_sec_base = anim->frs_sec_base;
1430                 return TRUE;
1431         }
1432         return FALSE;
1433 }
1434
1435 void IMB_anim_set_preseek(struct anim * anim, int preseek)
1436 {
1437         anim->preseek = preseek;
1438 }
1439
1440 int IMB_anim_get_preseek(struct anim * anim)
1441 {
1442         return anim->preseek;
1443 }