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