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