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