4ef1f962a0f284e4e4a6d465aae5b39d9e81c02e
[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 #include "BLI_math_base.h"
71
72 #include "MEM_guardedalloc.h"
73
74 #include "DNA_userdef_types.h"
75
76 #include "BKE_global.h"
77 #include "BKE_depsgraph.h"
78
79 #include "imbuf.h"
80
81 #ifdef WITH_AVI
82 #  include "AVI_avi.h"
83 #endif
84
85 #ifdef WITH_QUICKTIME
86 #if defined(_WIN32) || defined(__APPLE__)
87 #include "quicktime_import.h"
88 #endif /* _WIN32 || __APPLE__ */
89 #endif /* WITH_QUICKTIME */
90
91 #include "IMB_imbuf_types.h"
92 #include "IMB_imbuf.h"
93
94 #include "IMB_allocimbuf.h"
95 #include "IMB_anim.h"
96 #include "IMB_indexer.h"
97
98 #ifdef WITH_FFMPEG
99 #include <libavformat/avformat.h>
100 #include <libavcodec/avcodec.h>
101 #include <libavutil/rational.h>
102 #include <libswscale/swscale.h>
103
104 #include "ffmpeg_compat.h"
105
106 #endif //WITH_FFMPEG
107
108 #ifdef WITH_REDCODE
109 #ifdef _WIN32 /* on windows we use the ones in extern instead */
110 #include "libredcode/format.h"
111 #include "libredcode/codec.h"
112 #else
113 #include "libredcode/format.h"
114 #include "libredcode/codec.h"
115 #endif
116 #endif
117
118 #include "IMB_colormanagement.h"
119 #include "IMB_colormanagement_intern.h"
120
121 int ismovie(const char *UNUSED(filepath))
122 {
123         return 0;
124 }
125
126 /* never called, just keep the linker happy */
127 static int startmovie(struct anim *UNUSED(anim))
128 {
129         return 1;
130 }
131 static ImBuf *movie_fetchibuf(struct anim *UNUSED(anim), int UNUSED(position))
132 {
133         return NULL;
134 }
135 static void free_anim_movie(struct anim *UNUSED(anim))
136 {
137         /* pass */
138 }
139
140
141 #if defined(_WIN32)
142 # define PATHSEPERATOR '\\'
143 #else
144 # define PATHSEPERATOR '/'
145 #endif
146
147 static int an_stringdec(const char *string, char *head, char *tail, unsigned short *numlen)
148 {
149         unsigned short len, nume, nums = 0;
150         short i, found = FALSE;
151
152         len = strlen(string);
153         nume = len;
154
155         for (i = len - 1; i >= 0; i--) {
156                 if (string[i] == PATHSEPERATOR) break;
157                 if (isdigit(string[i])) {
158                         if (found) {
159                                 nums = i;
160                         }
161                         else {
162                                 nume = i;
163                                 nums = i;
164                                 found = TRUE;
165                         }
166                 }
167                 else {
168                         if (found) break;
169                 }
170         }
171         if (found) {
172                 strcpy(tail, &string[nume + 1]);
173                 strcpy(head, string);
174                 head[nums] = '\0';
175                 *numlen = nume - nums + 1;
176                 return ((int)atoi(&(string[nums])));
177         }
178         tail[0] = '\0';
179         strcpy(head, string);
180         *numlen = 0;
181         return TRUE;
182 }
183
184
185 static void an_stringenc(char *string, const char *head, const char *tail, unsigned short numlen, int pic)
186 {
187         BLI_stringenc(string, head, tail, numlen, pic);
188 }
189
190 #ifdef WITH_AVI
191 static void free_anim_avi(struct anim *anim)
192 {
193 #if defined(_WIN32) && !defined(FREE_WINDOWS)
194         int i;
195 #endif
196
197         if (anim == NULL) return;
198         if (anim->avi == NULL) return;
199
200         AVI_close(anim->avi);
201         MEM_freeN(anim->avi);
202         anim->avi = NULL;
203
204 #if defined(_WIN32) && !defined(FREE_WINDOWS)
205
206         if (anim->pgf) {
207                 AVIStreamGetFrameClose(anim->pgf);
208                 anim->pgf = NULL;
209         }
210
211         for (i = 0; i < anim->avistreams; i++) {
212                 AVIStreamRelease(anim->pavi[i]);
213         }
214         anim->avistreams = 0;
215
216         if (anim->pfileopen) {
217                 AVIFileRelease(anim->pfile);
218                 anim->pfileopen = 0;
219                 AVIFileExit();
220         }
221 #endif
222
223         anim->duration = 0;
224 }
225 #endif  /* WITH_AVI */
226
227 #ifdef WITH_FFMPEG
228 static void free_anim_ffmpeg(struct anim *anim);
229 #endif
230 #ifdef WITH_REDCODE
231 static void free_anim_redcode(struct anim *anim);
232 #endif
233
234 void IMB_free_anim(struct anim *anim)
235 {
236         if (anim == NULL) {
237                 printf("free anim, anim == NULL\n");
238                 return;
239         }
240
241         free_anim_movie(anim);
242
243 #ifdef WITH_AVI
244         free_anim_avi(anim);
245 #endif
246
247 #ifdef WITH_QUICKTIME
248         free_anim_quicktime(anim);
249 #endif
250 #ifdef WITH_FFMPEG
251         free_anim_ffmpeg(anim);
252 #endif
253 #ifdef WITH_REDCODE
254         free_anim_redcode(anim);
255 #endif
256         IMB_free_indices(anim);
257
258         MEM_freeN(anim);
259 }
260
261 void IMB_close_anim(struct anim *anim)
262 {
263         if (anim == NULL) return;
264
265         IMB_free_anim(anim);
266 }
267
268 void IMB_close_anim_proxies(struct anim *anim)
269 {
270         if (anim == NULL)
271                 return;
272
273         IMB_free_indices(anim);
274 }
275
276 struct anim *IMB_open_anim(const char *name, int ib_flags, int streamindex, char colorspace[IM_MAX_SPACE])
277 {
278         struct anim *anim;
279
280         anim = (struct anim *)MEM_callocN(sizeof(struct anim), "anim struct");
281         if (anim != NULL) {
282                 if (colorspace) {
283                         colorspace_set_default_role(colorspace, IM_MAX_SPACE, COLOR_ROLE_DEFAULT_BYTE);
284                         BLI_strncpy(anim->colorspace, colorspace, sizeof(anim->colorspace));
285                 }
286                 else {
287                         colorspace_set_default_role(anim->colorspace, sizeof(anim->colorspace), COLOR_ROLE_DEFAULT_BYTE);
288                 }
289
290                 BLI_strncpy(anim->name, name, sizeof(anim->name));
291                 anim->ib_flags = ib_flags;
292                 anim->streamindex = streamindex;
293         }
294         return(anim);
295 }
296
297 #ifdef WITH_AVI
298 static int startavi(struct anim *anim)
299 {
300
301         AviError avierror;
302 #if defined(_WIN32) && !defined(FREE_WINDOWS)
303         HRESULT hr;
304         int i, firstvideo = -1;
305         int streamcount;
306         BYTE abFormat[1024];
307         LONG l;
308         LPBITMAPINFOHEADER lpbi;
309         AVISTREAMINFO avis;
310
311         streamcount = anim->streamindex;
312 #endif
313
314         anim->avi = MEM_callocN(sizeof(AviMovie), "animavi");
315
316         if (anim->avi == NULL) {
317                 printf("Can't open avi: %s\n", anim->name);
318                 return -1;
319         }
320
321         avierror = AVI_open_movie(anim->name, anim->avi);
322
323 #if defined(_WIN32) && !defined(FREE_WINDOWS)
324         if (avierror == AVI_ERROR_COMPRESSION) {
325                 AVIFileInit();
326                 hr = AVIFileOpen(&anim->pfile, anim->name, OF_READ, 0L);
327                 if (hr == 0) {
328                         anim->pfileopen = 1;
329                         for (i = 0; i < MAXNUMSTREAMS; i++) {
330                                 if (AVIFileGetStream(anim->pfile, &anim->pavi[i], 0L, i) != AVIERR_OK) {
331                                         break;
332                                 }
333                                 
334                                 AVIStreamInfo(anim->pavi[i], &avis, sizeof(avis));
335                                 if ((avis.fccType == streamtypeVIDEO) && (firstvideo == -1)) {
336                                         if (streamcount > 0) {
337                                                 streamcount--;
338                                                 continue;
339                                         }
340                                         anim->pgf = AVIStreamGetFrameOpen(anim->pavi[i], NULL);
341                                         if (anim->pgf) {
342                                                 firstvideo = i;
343
344                                                 /* get stream length */
345                                                 anim->avi->header->TotalFrames = AVIStreamLength(anim->pavi[i]);
346
347                                                 /* get information about images inside the stream */
348                                                 l = sizeof(abFormat);
349                                                 AVIStreamReadFormat(anim->pavi[i], 0, &abFormat, &l);
350                                                 lpbi = (LPBITMAPINFOHEADER)abFormat;
351                                                 anim->avi->header->Height = lpbi->biHeight;
352                                                 anim->avi->header->Width = lpbi->biWidth;
353                                         }
354                                         else {
355                                                 FIXCC(avis.fccHandler);
356                                                 FIXCC(avis.fccType);
357                                                 printf("Can't find AVI decoder for type : %4.4hs/%4.4hs\n",
358                                                        (LPSTR)&avis.fccType,
359                                                        (LPSTR)&avis.fccHandler);
360                                         }
361                                 }
362                         }
363
364                         /* register number of opened avistreams */
365                         anim->avistreams = i;
366
367                         /*
368                          * Couldn't get any video streams out of this file
369                          */
370                         if ((anim->avistreams == 0) || (firstvideo == -1)) {
371                                 avierror = AVI_ERROR_FORMAT;
372                         }
373                         else {
374                                 avierror = AVI_ERROR_NONE;
375                                 anim->firstvideo = firstvideo;
376                         }
377                 }
378                 else {
379                         AVIFileExit();
380                 }
381         }
382 #endif
383
384         if (avierror != AVI_ERROR_NONE) {
385                 AVI_print_error(avierror);
386                 printf("Error loading avi: %s\n", anim->name);
387                 free_anim_avi(anim);
388                 return -1;
389         }
390         
391         anim->duration = anim->avi->header->TotalFrames;
392         anim->params = NULL;
393
394         anim->x = anim->avi->header->Width;
395         anim->y = anim->avi->header->Height;
396         anim->interlacing = 0;
397         anim->orientation = 0;
398         anim->framesize = anim->x * anim->y * 4;
399
400         anim->curposition = 0;
401         anim->preseek = 0;
402
403         /*  printf("x:%d y:%d size:%d interl:%d dur:%d\n", anim->x, anim->y, anim->framesize, anim->interlacing, anim->duration);*/
404
405         return 0;
406 }
407 #endif  /* WITH_AVI */
408
409 #ifdef WITH_AVI
410 static ImBuf *avi_fetchibuf(struct anim *anim, int position)
411 {
412         ImBuf *ibuf = NULL;
413         int *tmp;
414         int y;
415         
416         if (anim == NULL) {
417                 return NULL;
418         }
419
420 #if defined(_WIN32) && !defined(FREE_WINDOWS)
421         if (anim->avistreams) {
422                 LPBITMAPINFOHEADER lpbi;
423
424                 if (anim->pgf) {
425                         lpbi = AVIStreamGetFrame(anim->pgf, position + AVIStreamStart(anim->pavi[anim->firstvideo]));
426                         if (lpbi) {
427                                 ibuf = IMB_ibImageFromMemory((unsigned char *) lpbi, 100, IB_rect, anim->colorspace, "<avi_fetchibuf>");
428 //Oh brother...
429                         }
430                 }
431         }
432         else {
433 #else
434         if (1) {
435 #endif
436                 ibuf = IMB_allocImBuf(anim->x, anim->y, 24, IB_rect);
437
438                 tmp = AVI_read_frame(anim->avi, AVI_FORMAT_RGB32, position,
439                                      AVI_get_stream(anim->avi, AVIST_VIDEO, 0));
440                 
441                 if (tmp == NULL) {
442                         printf("Error reading frame from AVI: '%s'\n", anim->name);
443                         IMB_freeImBuf(ibuf);
444                         return NULL;
445                 }
446
447                 for (y = 0; y < anim->y; y++) {
448                         memcpy(&(ibuf->rect)[((anim->y - y) - 1) * anim->x],  &tmp[y * anim->x],
449                                anim->x * 4);
450                 }
451                 
452                 MEM_freeN(tmp);
453         }
454         
455         ibuf->rect_colorspace = colormanage_colorspace_get_named(anim->colorspace);
456
457         return ibuf;
458 }
459 #endif  /* WITH_AVI */
460
461 #ifdef WITH_FFMPEG
462
463 static int startffmpeg(struct anim *anim)
464 {
465         int i, videoStream;
466
467         AVCodec *pCodec;
468         AVFormatContext *pFormatCtx = NULL;
469         AVCodecContext *pCodecCtx;
470         int frs_num;
471         double frs_den;
472         int streamcount;
473
474 #ifdef FFMPEG_SWSCALE_COLOR_SPACE_SUPPORT
475         /* The following for color space determination */
476         int srcRange, dstRange, brightness, contrast, saturation;
477         int *table;
478         const int *inv_table;
479 #endif
480
481         if (anim == 0) return(-1);
482
483         streamcount = anim->streamindex;
484
485         if (avformat_open_input(&pFormatCtx, anim->name, NULL, NULL) != 0) {
486                 return -1;
487         }
488
489         if (avformat_find_stream_info(pFormatCtx, NULL) < 0) {
490                 av_close_input_file(pFormatCtx);
491                 return -1;
492         }
493
494         av_dump_format(pFormatCtx, 0, anim->name, 0);
495
496
497         /* Find the video stream */
498         videoStream = -1;
499
500         for (i = 0; i < pFormatCtx->nb_streams; i++)
501                 if (pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
502                         if (streamcount > 0) {
503                                 streamcount--;
504                                 continue;
505                         }
506                         videoStream = i;
507                         break;
508                 }
509
510         if (videoStream == -1) {
511                 av_close_input_file(pFormatCtx);
512                 return -1;
513         }
514
515         pCodecCtx = pFormatCtx->streams[videoStream]->codec;
516
517         /* Find the decoder for the video stream */
518         pCodec = avcodec_find_decoder(pCodecCtx->codec_id);
519         if (pCodec == NULL) {
520                 av_close_input_file(pFormatCtx);
521                 return -1;
522         }
523
524         pCodecCtx->workaround_bugs = 1;
525
526         if (avcodec_open2(pCodecCtx, pCodec, NULL) < 0) {
527                 av_close_input_file(pFormatCtx);
528                 return -1;
529         }
530
531         anim->duration = ceil(pFormatCtx->duration *
532                               av_q2d(pFormatCtx->streams[videoStream]->r_frame_rate) /
533                               AV_TIME_BASE);
534
535         frs_num = pFormatCtx->streams[videoStream]->r_frame_rate.num;
536         frs_den = pFormatCtx->streams[videoStream]->r_frame_rate.den;
537
538         frs_den *= AV_TIME_BASE;
539
540         while (frs_num % 10 == 0 && frs_den >= 2.0 && frs_num > 10) {
541                 frs_num /= 10;
542                 frs_den /= 10;
543         }
544
545         anim->frs_sec = frs_num;
546         anim->frs_sec_base = frs_den;
547
548         anim->params = 0;
549
550         anim->x = pCodecCtx->width;
551         anim->y = pCodecCtx->height;
552         anim->interlacing = 0;
553         anim->orientation = 0;
554         anim->framesize = anim->x * anim->y * 4;
555
556         anim->curposition = -1;
557         anim->last_frame = 0;
558         anim->last_pts = -1;
559         anim->next_pts = -1;
560         anim->next_packet.stream_index = -1;
561
562         anim->pFormatCtx = pFormatCtx;
563         anim->pCodecCtx = pCodecCtx;
564         anim->pCodec = pCodec;
565         anim->videoStream = videoStream;
566
567         anim->pFrame = avcodec_alloc_frame();
568         anim->pFrameComplete = FALSE;
569         anim->pFrameDeinterlaced = avcodec_alloc_frame();
570         anim->pFrameRGB = avcodec_alloc_frame();
571
572         if (avpicture_get_size(PIX_FMT_RGBA, anim->x, anim->y) !=
573             anim->x * anim->y * 4)
574         {
575                 fprintf(stderr,
576                         "ffmpeg has changed alloc scheme ... ARGHHH!\n");
577                 avcodec_close(anim->pCodecCtx);
578                 av_close_input_file(anim->pFormatCtx);
579                 av_free(anim->pFrameRGB);
580                 av_free(anim->pFrameDeinterlaced);
581                 av_free(anim->pFrame);
582                 anim->pCodecCtx = NULL;
583                 return -1;
584         }
585
586         if (anim->ib_flags & IB_animdeinterlace) {
587                 avpicture_fill((AVPicture *) anim->pFrameDeinterlaced,
588                                MEM_callocN(avpicture_get_size(
589                                                anim->pCodecCtx->pix_fmt,
590                                                anim->x, anim->y),
591                                            "ffmpeg deinterlace"),
592                                anim->pCodecCtx->pix_fmt, anim->x, anim->y);
593         }
594
595         if (pCodecCtx->has_b_frames) {
596                 anim->preseek = 25; /* FIXME: detect gopsize ... */
597         }
598         else {
599                 anim->preseek = 0;
600         }
601         
602         anim->img_convert_ctx = sws_getContext(
603                 anim->pCodecCtx->width,
604                 anim->pCodecCtx->height,
605                 anim->pCodecCtx->pix_fmt,
606                 anim->pCodecCtx->width,
607                 anim->pCodecCtx->height,
608                 PIX_FMT_RGBA,
609                 SWS_FAST_BILINEAR | SWS_PRINT_INFO,
610                 NULL, NULL, NULL);
611                 
612         if (!anim->img_convert_ctx) {
613                 fprintf(stderr,
614                         "Can't transform color space??? Bailing out...\n");
615                 avcodec_close(anim->pCodecCtx);
616                 av_close_input_file(anim->pFormatCtx);
617                 av_free(anim->pFrameRGB);
618                 av_free(anim->pFrameDeinterlaced);
619                 av_free(anim->pFrame);
620                 anim->pCodecCtx = NULL;
621                 return -1;
622         }
623
624 #ifdef FFMPEG_SWSCALE_COLOR_SPACE_SUPPORT
625         /* Try do detect if input has 0-255 YCbCR range (JFIF Jpeg MotionJpeg) */
626         if (!sws_getColorspaceDetails(anim->img_convert_ctx, (int **)&inv_table, &srcRange,
627                                       &table, &dstRange, &brightness, &contrast, &saturation))
628         {
629                 srcRange = srcRange || anim->pCodecCtx->color_range == AVCOL_RANGE_JPEG;
630                 inv_table = sws_getCoefficients(anim->pCodecCtx->colorspace);
631
632                 if (sws_setColorspaceDetails(anim->img_convert_ctx, (int *)inv_table, srcRange,
633                                              table, dstRange, brightness, contrast, saturation))
634                 {
635                         printf("Warning: Could not set libswscale colorspace details.\n");
636                 }
637         }
638         else {
639                 printf("Warning: Could not set libswscale colorspace details.\n");
640         }
641 #endif
642                 
643         return (0);
644 }
645
646 /* postprocess the image in anim->pFrame and do color conversion
647  * and deinterlacing stuff.
648  *
649  * Output is anim->last_frame
650  */
651
652 static void ffmpeg_postprocess(struct anim *anim)
653 {
654         AVFrame *input = anim->pFrame;
655         ImBuf *ibuf = anim->last_frame;
656         int filter_y = 0;
657
658         if (!anim->pFrameComplete) {
659                 return;
660         }
661
662         /* This means the data wasnt read properly, 
663          * this check stops crashing */
664         if (input->data[0] == 0 && input->data[1] == 0 &&
665             input->data[2] == 0 && input->data[3] == 0)
666         {
667                 fprintf(stderr, "ffmpeg_fetchibuf: "
668                         "data not read properly...\n");
669                 return;
670         }
671
672         av_log(anim->pFormatCtx, AV_LOG_DEBUG, 
673                "  POSTPROC: anim->pFrame planes: %p %p %p %p\n",
674                input->data[0], input->data[1], input->data[2],
675                input->data[3]);
676
677
678         if (anim->ib_flags & IB_animdeinterlace) {
679                 if (avpicture_deinterlace(
680                         (AVPicture *)
681                         anim->pFrameDeinterlaced,
682                         (const AVPicture *)
683                         anim->pFrame,
684                         anim->pCodecCtx->pix_fmt,
685                         anim->pCodecCtx->width,
686                         anim->pCodecCtx->height) < 0)
687                 {
688                         filter_y = TRUE;
689                 }
690                 else {
691                         input = anim->pFrameDeinterlaced;
692                 }
693         }
694         
695         avpicture_fill((AVPicture *) anim->pFrameRGB,
696                        (unsigned char *) ibuf->rect,
697                        PIX_FMT_RGBA, anim->x, anim->y);
698
699         if (ENDIAN_ORDER == B_ENDIAN) {
700                 int *dstStride   = anim->pFrameRGB->linesize;
701                 uint8_t **dst     = anim->pFrameRGB->data;
702                 int dstStride2[4] = { dstStride[0], 0, 0, 0 };
703                 uint8_t *dst2[4]  = { dst[0], 0, 0, 0 };
704                 int x, y, h, w;
705                 unsigned char *bottom;
706                 unsigned char *top;
707                 
708                 sws_scale(anim->img_convert_ctx,
709                           (const uint8_t *const *)input->data,
710                           input->linesize,
711                           0,
712                           anim->pCodecCtx->height,
713                           dst2,
714                           dstStride2);
715                 
716                 bottom = (unsigned char *) ibuf->rect;
717                 top = bottom + ibuf->x * (ibuf->y - 1) * 4;
718                 
719                 h = (ibuf->y + 1) / 2;
720                 w = ibuf->x;
721                 
722                 for (y = 0; y < h; y++) {
723                         unsigned char tmp[4];
724                         unsigned int *tmp_l =
725                             (unsigned int *) tmp;
726                         
727                         for (x = 0; x < w; x++) {
728                                 tmp[0] = bottom[0];
729                                 tmp[1] = bottom[1];
730                                 tmp[2] = bottom[2];
731                                 tmp[3] = bottom[3];
732                                 
733                                 bottom[0] = top[0];
734                                 bottom[1] = top[1];
735                                 bottom[2] = top[2];
736                                 bottom[3] = top[3];
737                                 
738                                 *(unsigned int *) top = *tmp_l;
739                                 
740                                 bottom += 4;
741                                 top += 4;
742                         }
743                         top -= 8 * w;
744                 }
745         }
746         else {
747                 int *dstStride   = anim->pFrameRGB->linesize;
748                 uint8_t **dst     = anim->pFrameRGB->data;
749                 int dstStride2[4] = { -dstStride[0], 0, 0, 0 };
750                 uint8_t *dst2[4]  = { dst[0] + (anim->y - 1) * dstStride[0],
751                                           0, 0, 0 };
752                 
753                 sws_scale(anim->img_convert_ctx,
754                           (const uint8_t *const *)input->data,
755                           input->linesize,
756                           0,
757                           anim->pCodecCtx->height,
758                           dst2,
759                           dstStride2);
760         }
761
762         if (filter_y) {
763                 IMB_filtery(ibuf);
764         }
765 }
766
767 /* decode one video frame also considering the packet read into next_packet */
768
769 static int ffmpeg_decode_video_frame(struct anim *anim)
770 {
771         int rval = 0;
772
773         av_log(anim->pFormatCtx, AV_LOG_DEBUG, "  DECODE VIDEO FRAME\n");
774
775         if (anim->next_packet.stream_index == anim->videoStream) {
776                 av_free_packet(&anim->next_packet);
777                 anim->next_packet.stream_index = -1;
778         }
779         
780         while ((rval = av_read_frame(anim->pFormatCtx, &anim->next_packet)) >= 0) {
781                 av_log(anim->pFormatCtx, 
782                        AV_LOG_DEBUG, 
783                        "%sREAD: strID=%d (VID: %d) dts=%lld pts=%lld "
784                        "%s\n",
785                        (anim->next_packet.stream_index == anim->videoStream)
786                        ? "->" : "  ",
787                        anim->next_packet.stream_index, 
788                        anim->videoStream,
789                        (anim->next_packet.dts == AV_NOPTS_VALUE) ? -1 :
790                        (long long int)anim->next_packet.dts,
791                        (anim->next_packet.pts == AV_NOPTS_VALUE) ? -1 :
792                        (long long int)anim->next_packet.pts,
793                        (anim->next_packet.flags & AV_PKT_FLAG_KEY) ? 
794                        " KEY" : "");
795                 if (anim->next_packet.stream_index == anim->videoStream) {
796                         anim->pFrameComplete = 0;
797
798                         avcodec_decode_video2(
799                             anim->pCodecCtx,
800                             anim->pFrame, &anim->pFrameComplete,
801                             &anim->next_packet);
802
803                         if (anim->pFrameComplete) {
804                                 anim->next_pts = av_get_pts_from_frame(
805                                         anim->pFormatCtx, anim->pFrame);
806
807                                 av_log(anim->pFormatCtx,
808                                        AV_LOG_DEBUG,
809                                        "  FRAME DONE: next_pts=%lld "
810                                        "pkt_pts=%lld, guessed_pts=%lld\n",
811                                        (anim->pFrame->pts == AV_NOPTS_VALUE) ?
812                                        -1 : (long long int)anim->pFrame->pts,
813                                        (anim->pFrame->pkt_pts == AV_NOPTS_VALUE) ?
814                                        -1 : (long long int)anim->pFrame->pkt_pts,
815                                        (long long int)anim->next_pts);
816                                 break;
817                         }
818                 }
819                 av_free_packet(&anim->next_packet);
820                 anim->next_packet.stream_index = -1;
821         }
822         
823         if (rval == AVERROR_EOF) {
824                 /* this sets size and data fields to zero,
825                  * which is necessary to decode the remaining data
826                  * in the decoder engine after EOF. It also prevents a memory
827                  * leak, since av_read_frame spills out a full size packet even
828                  * on EOF... (and: it's save to call on NULL packets) */
829
830                 av_free_packet(&anim->next_packet);
831
832                 anim->next_packet.size = 0;
833                 anim->next_packet.data = 0;
834
835                 anim->pFrameComplete = 0;
836
837                 avcodec_decode_video2(
838                         anim->pCodecCtx,
839                         anim->pFrame, &anim->pFrameComplete,
840                         &anim->next_packet);
841
842                 if (anim->pFrameComplete) {
843                         anim->next_pts = av_get_pts_from_frame(
844                                 anim->pFormatCtx, anim->pFrame);
845
846                         av_log(anim->pFormatCtx,
847                                AV_LOG_DEBUG,
848                                "  FRAME DONE (after EOF): next_pts=%lld "
849                                "pkt_pts=%lld, guessed_pts=%lld\n",
850                                (anim->pFrame->pts == AV_NOPTS_VALUE) ?
851                                -1 : (long long int)anim->pFrame->pts,
852                                (anim->pFrame->pkt_pts == AV_NOPTS_VALUE) ?
853                                -1 : (long long int)anim->pFrame->pkt_pts,
854                                (long long int)anim->next_pts);
855                         rval = 0;
856                 }
857         }
858
859         if (rval < 0) {
860                 anim->next_packet.stream_index = -1;
861
862                 av_log(anim->pFormatCtx,
863                        AV_LOG_ERROR, "  DECODE READ FAILED: av_read_frame() "
864                        "returned error: %d\n",  rval);
865         }
866
867         return (rval >= 0);
868 }
869
870 static void ffmpeg_decode_video_frame_scan(
871         struct anim *anim, int64_t pts_to_search)
872 {
873         /* there seem to exist *very* silly GOP lengths out in the wild... */
874         int count = 1000;
875
876         av_log(anim->pFormatCtx,
877                AV_LOG_DEBUG, 
878                "SCAN start: considering pts=%lld in search of %lld\n", 
879                (long long int)anim->next_pts, (long long int)pts_to_search);
880
881         while (count > 0 && anim->next_pts < pts_to_search) {
882                 av_log(anim->pFormatCtx,
883                        AV_LOG_DEBUG, 
884                        "  WHILE: pts=%lld in search of %lld\n", 
885                        (long long int)anim->next_pts, (long long int)pts_to_search);
886                 if (!ffmpeg_decode_video_frame(anim)) {
887                         break;
888                 }
889                 count--;
890         }
891         if (count == 0) {
892                 av_log(anim->pFormatCtx,
893                        AV_LOG_ERROR, 
894                        "SCAN failed: completely lost in stream, "
895                        "bailing out at PTS=%lld, searching for PTS=%lld\n", 
896                        (long long int)anim->next_pts, (long long int)pts_to_search);
897         }
898         if (anim->next_pts == pts_to_search) {
899                 av_log(anim->pFormatCtx,
900                        AV_LOG_DEBUG, "SCAN HAPPY: we found our PTS!\n");
901         }
902         else {
903                 av_log(anim->pFormatCtx,
904                        AV_LOG_ERROR, "SCAN UNHAPPY: PTS not matched!\n");
905         }
906 }
907
908 static int match_format(const char *name, AVFormatContext *pFormatCtx)
909 {
910         const char *p;
911         int len, namelen;
912
913         const char *names = pFormatCtx->iformat->name;
914
915         if (!name || !names)
916                 return 0;
917
918         namelen = strlen(name);
919         while ((p = strchr(names, ','))) {
920                 len = MAX2(p - names, namelen);
921                 if (!BLI_strncasecmp(name, names, len))
922                         return 1;
923                 names = p + 1;
924         }
925         return !BLI_strcasecmp(name, names);
926 }
927
928 static int ffmpeg_seek_by_byte(AVFormatContext *pFormatCtx)
929 {
930         static const char *byte_seek_list[] = { "mpegts", 0 };
931         const char **p;
932
933         if (pFormatCtx->iformat->flags & AVFMT_TS_DISCONT) {
934                 return TRUE;
935         }
936
937         p = byte_seek_list;
938
939         while (*p) {
940                 if (match_format(*p++, pFormatCtx)) {
941                         return TRUE;
942                 }
943         }
944
945         return FALSE;
946 }
947
948 static ImBuf *ffmpeg_fetchibuf(struct anim *anim, int position,
949                                IMB_Timecode_Type tc) {
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 quite gcc barking... */
957         int old_frame_index = 0; /* To quite gcc barking... */
958
959         if (anim == 0) 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(v_st->r_frame_rate);
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 = %lld\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                 av_close_input_file(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         struct ImBuf *ibuf = NULL;
1308         char head[256], tail[256];
1309         unsigned short digits;
1310         int pic;
1311         int filter_y;
1312         if (anim == NULL) return(NULL);
1313
1314         filter_y = (anim->ib_flags & IB_animdeinterlace);
1315
1316         if (anim->curtype == 0) {
1317                 ibuf = anim_getnew(anim);
1318                 if (ibuf == NULL) {
1319                         return(NULL);
1320                 }
1321
1322                 IMB_freeImBuf(ibuf); /* ???? */
1323                 ibuf = NULL;
1324         }
1325
1326         if (position < 0) return(NULL);
1327         if (position >= anim->duration) return(NULL);
1328
1329         if (preview_size != IMB_PROXY_NONE) {
1330                 struct anim *proxy = IMB_anim_open_proxy(anim, preview_size);
1331
1332                 if (proxy) {
1333                         position = IMB_anim_index_get_frame_index(
1334                             anim, tc, position);
1335                         return IMB_anim_absolute(
1336                                    proxy, position,
1337                                    IMB_TC_NONE, IMB_PROXY_NONE);
1338                 }
1339         }
1340
1341         switch (anim->curtype) {
1342                 case ANIM_SEQUENCE:
1343                         pic = an_stringdec(anim->first, head, tail, &digits);
1344                         pic += position;
1345                         an_stringenc(anim->name, head, tail, digits, pic);
1346                         ibuf = IMB_loadiffname(anim->name, IB_rect, anim->colorspace);
1347                         if (ibuf) {
1348                                 anim->curposition = position;
1349                         }
1350                         break;
1351                 case ANIM_MOVIE:
1352                         ibuf = movie_fetchibuf(anim, position);
1353                         if (ibuf) {
1354                                 anim->curposition = position;
1355                                 IMB_convert_rgba_to_abgr(ibuf);
1356                         }
1357                         break;
1358 #ifdef WITH_AVI
1359                 case ANIM_AVI:
1360                         ibuf = avi_fetchibuf(anim, position);
1361                         if (ibuf)
1362                                 anim->curposition = position;
1363                         break;
1364 #endif
1365 #ifdef WITH_QUICKTIME
1366                 case ANIM_QTIME:
1367                         ibuf = qtime_fetchibuf(anim, position);
1368                         if (ibuf) {
1369                                 if (ibuf->rect) {
1370                                         /* OCIO_TODO: should happen in quicktime module, but it currently doesn't have access
1371                                          *            to color management's internals
1372                                          */
1373                                         ibuf->rect_colorspace = colormanage_colorspace_get_named(anim->colorspace);
1374                                 }
1375
1376                                 anim->curposition = position;
1377                         }
1378                         break;
1379 #endif
1380 #ifdef WITH_FFMPEG
1381                 case ANIM_FFMPEG:
1382                         ibuf = ffmpeg_fetchibuf(anim, position, tc);
1383                         if (ibuf)
1384                                 anim->curposition = position;
1385                         filter_y = 0; /* done internally */
1386                         break;
1387 #endif
1388 #ifdef WITH_REDCODE
1389                 case ANIM_REDCODE:
1390                         ibuf = redcode_fetchibuf(anim, position);
1391                         if (ibuf) anim->curposition = position;
1392                         break;
1393 #endif
1394         }
1395
1396         if (ibuf) {
1397                 if (filter_y) IMB_filtery(ibuf);
1398                 BLI_snprintf(ibuf->name, sizeof(ibuf->name), "%s.%04d", anim->name, anim->curposition + 1);
1399                 
1400         }
1401         return(ibuf);
1402 }
1403
1404 /***/
1405
1406 int IMB_anim_get_duration(struct anim *anim, IMB_Timecode_Type tc)
1407 {
1408         struct anim_index *idx;
1409         if (tc == IMB_TC_NONE) {
1410                 return anim->duration;
1411         }
1412         
1413         idx = IMB_anim_open_index(anim, tc);
1414         if (!idx) {
1415                 return anim->duration;
1416         }
1417
1418         return IMB_indexer_get_duration(idx);
1419 }
1420
1421 int IMB_anim_get_fps(struct anim *anim,
1422                      short *frs_sec, float *frs_sec_base)
1423 {
1424         if (anim->frs_sec) {
1425                 *frs_sec = anim->frs_sec;
1426                 *frs_sec_base = anim->frs_sec_base;
1427                 return TRUE;
1428         }
1429         return FALSE;
1430 }
1431
1432 void IMB_anim_set_preseek(struct anim *anim, int preseek)
1433 {
1434         anim->preseek = preseek;
1435 }
1436
1437 int IMB_anim_get_preseek(struct anim *anim)
1438 {
1439         return anim->preseek;
1440 }