style cleanup: braces/indentation
[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 #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 #endif
434         {
435                 ibuf = IMB_allocImBuf(anim->x, anim->y, 24, IB_rect);
436
437                 tmp = AVI_read_frame(anim->avi, AVI_FORMAT_RGB32, position,
438                                      AVI_get_stream(anim->avi, AVIST_VIDEO, 0));
439                 
440                 if (tmp == NULL) {
441                         printf("Error reading frame from AVI: '%s'\n", anim->name);
442                         IMB_freeImBuf(ibuf);
443                         return NULL;
444                 }
445
446                 for (y = 0; y < anim->y; y++) {
447                         memcpy(&(ibuf->rect)[((anim->y - y) - 1) * anim->x],  &tmp[y * anim->x],
448                                anim->x * 4);
449                 }
450                 
451                 MEM_freeN(tmp);
452         }
453         
454         ibuf->rect_colorspace = colormanage_colorspace_get_named(anim->colorspace);
455
456         return ibuf;
457 }
458 #endif  /* WITH_AVI */
459
460 #ifdef WITH_FFMPEG
461
462 static int startffmpeg(struct anim *anim)
463 {
464         int i, videoStream;
465
466         AVCodec *pCodec;
467         AVFormatContext *pFormatCtx = NULL;
468         AVCodecContext *pCodecCtx;
469         int frs_num;
470         double frs_den;
471         int streamcount;
472
473 #ifdef FFMPEG_SWSCALE_COLOR_SPACE_SUPPORT
474         /* The following for color space determination */
475         int srcRange, dstRange, brightness, contrast, saturation;
476         int *table;
477         const int *inv_table;
478 #endif
479
480         if (anim == 0) return(-1);
481
482         streamcount = anim->streamindex;
483
484         if (avformat_open_input(&pFormatCtx, anim->name, NULL, NULL) != 0) {
485                 return -1;
486         }
487
488         if (avformat_find_stream_info(pFormatCtx, NULL) < 0) {
489                 av_close_input_file(pFormatCtx);
490                 return -1;
491         }
492
493         av_dump_format(pFormatCtx, 0, anim->name, 0);
494
495
496         /* Find the video stream */
497         videoStream = -1;
498
499         for (i = 0; i < pFormatCtx->nb_streams; i++)
500                 if (pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
501                         if (streamcount > 0) {
502                                 streamcount--;
503                                 continue;
504                         }
505                         videoStream = i;
506                         break;
507                 }
508
509         if (videoStream == -1) {
510                 av_close_input_file(pFormatCtx);
511                 return -1;
512         }
513
514         pCodecCtx = pFormatCtx->streams[videoStream]->codec;
515
516         /* Find the decoder for the video stream */
517         pCodec = avcodec_find_decoder(pCodecCtx->codec_id);
518         if (pCodec == NULL) {
519                 av_close_input_file(pFormatCtx);
520                 return -1;
521         }
522
523         pCodecCtx->workaround_bugs = 1;
524
525         if (avcodec_open2(pCodecCtx, pCodec, NULL) < 0) {
526                 av_close_input_file(pFormatCtx);
527                 return -1;
528         }
529
530         anim->duration = ceil(pFormatCtx->duration *
531                               av_q2d(pFormatCtx->streams[videoStream]->r_frame_rate) /
532                               AV_TIME_BASE);
533
534         frs_num = pFormatCtx->streams[videoStream]->r_frame_rate.num;
535         frs_den = pFormatCtx->streams[videoStream]->r_frame_rate.den;
536
537         frs_den *= AV_TIME_BASE;
538
539         while (frs_num % 10 == 0 && frs_den >= 2.0 && frs_num > 10) {
540                 frs_num /= 10;
541                 frs_den /= 10;
542         }
543
544         anim->frs_sec = frs_num;
545         anim->frs_sec_base = frs_den;
546
547         anim->params = 0;
548
549         anim->x = pCodecCtx->width;
550         anim->y = av_get_cropped_height_from_codec(pCodecCtx);
551
552         anim->pFormatCtx = pFormatCtx;
553         anim->pCodecCtx = pCodecCtx;
554         anim->pCodec = pCodec;
555         anim->videoStream = videoStream;
556
557         anim->interlacing = 0;
558         anim->orientation = 0;
559         anim->framesize = anim->x * anim->y * 4;
560
561         anim->curposition = -1;
562         anim->last_frame = 0;
563         anim->last_pts = -1;
564         anim->next_pts = -1;
565         anim->next_packet.stream_index = -1;
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->pCodecCtx->width,
591                                                anim->pCodecCtx->height),
592                                            "ffmpeg deinterlace"),
593                                anim->pCodecCtx->pix_fmt, 
594                                anim->pCodecCtx->width,
595                                anim->pCodecCtx->height);
596         }
597
598         if (pCodecCtx->has_b_frames) {
599                 anim->preseek = 25; /* FIXME: detect gopsize ... */
600         }
601         else {
602                 anim->preseek = 0;
603         }
604         
605         anim->img_convert_ctx = sws_getContext(
606                 anim->x,
607                 anim->y,
608                 anim->pCodecCtx->pix_fmt,
609                 anim->x,
610                 anim->y,
611                 PIX_FMT_RGBA,
612                 SWS_FAST_BILINEAR | SWS_PRINT_INFO | SWS_FULL_CHR_H_INT,
613                 NULL, NULL, NULL);
614                 
615         if (!anim->img_convert_ctx) {
616                 fprintf(stderr,
617                         "Can't transform color space??? Bailing out...\n");
618                 avcodec_close(anim->pCodecCtx);
619                 av_close_input_file(anim->pFormatCtx);
620                 av_free(anim->pFrameRGB);
621                 av_free(anim->pFrameDeinterlaced);
622                 av_free(anim->pFrame);
623                 anim->pCodecCtx = NULL;
624                 return -1;
625         }
626
627 #ifdef FFMPEG_SWSCALE_COLOR_SPACE_SUPPORT
628         /* Try do detect if input has 0-255 YCbCR range (JFIF Jpeg MotionJpeg) */
629         if (!sws_getColorspaceDetails(anim->img_convert_ctx, (int **)&inv_table, &srcRange,
630                                       &table, &dstRange, &brightness, &contrast, &saturation))
631         {
632                 srcRange = srcRange || anim->pCodecCtx->color_range == AVCOL_RANGE_JPEG;
633                 inv_table = sws_getCoefficients(anim->pCodecCtx->colorspace);
634
635                 if (sws_setColorspaceDetails(anim->img_convert_ctx, (int *)inv_table, srcRange,
636                                              table, dstRange, brightness, contrast, saturation))
637                 {
638                         fprintf(stderr, "Warning: Could not set libswscale colorspace details.\n");
639                 }
640         }
641         else {
642                 fprintf(stderr, "Warning: Could not set libswscale colorspace details.\n");
643         }
644 #endif
645                 
646         return (0);
647 }
648
649 /* postprocess the image in anim->pFrame and do color conversion
650  * and deinterlacing stuff.
651  *
652  * Output is anim->last_frame
653  */
654
655 static void ffmpeg_postprocess(struct anim *anim)
656 {
657         AVFrame *input = anim->pFrame;
658         ImBuf *ibuf = anim->last_frame;
659         int filter_y = 0;
660
661         if (!anim->pFrameComplete) {
662                 return;
663         }
664
665         /* This means the data wasnt read properly, 
666          * this check stops crashing */
667         if (input->data[0] == 0 && input->data[1] == 0 &&
668             input->data[2] == 0 && input->data[3] == 0)
669         {
670                 fprintf(stderr, "ffmpeg_fetchibuf: "
671                         "data not read properly...\n");
672                 return;
673         }
674
675         av_log(anim->pFormatCtx, AV_LOG_DEBUG, 
676                "  POSTPROC: anim->pFrame planes: %p %p %p %p\n",
677                input->data[0], input->data[1], input->data[2],
678                input->data[3]);
679
680
681         if (anim->ib_flags & IB_animdeinterlace) {
682                 if (avpicture_deinterlace(
683                         (AVPicture *)
684                         anim->pFrameDeinterlaced,
685                         (const AVPicture *)
686                         anim->pFrame,
687                         anim->pCodecCtx->pix_fmt,
688                         anim->pCodecCtx->width,
689                         anim->pCodecCtx->height) < 0)
690                 {
691                         filter_y = TRUE;
692                 }
693                 else {
694                         input = anim->pFrameDeinterlaced;
695                 }
696         }
697         
698         avpicture_fill((AVPicture *) anim->pFrameRGB,
699                        (unsigned char *) ibuf->rect,
700                        PIX_FMT_RGBA, anim->x, anim->y);
701
702         if (ENDIAN_ORDER == B_ENDIAN) {
703                 int *dstStride   = anim->pFrameRGB->linesize;
704                 uint8_t **dst     = anim->pFrameRGB->data;
705                 int dstStride2[4] = { dstStride[0], 0, 0, 0 };
706                 uint8_t *dst2[4]  = { dst[0], 0, 0, 0 };
707                 int x, y, h, w;
708                 unsigned char *bottom;
709                 unsigned char *top;
710                 
711                 sws_scale(anim->img_convert_ctx,
712                           (const uint8_t *const *)input->data,
713                           input->linesize,
714                           0,
715                           anim->y,
716                           dst2,
717                           dstStride2);
718                 
719                 bottom = (unsigned char *) ibuf->rect;
720                 top = bottom + ibuf->x * (ibuf->y - 1) * 4;
721                 
722                 h = (ibuf->y + 1) / 2;
723                 w = ibuf->x;
724                 
725                 for (y = 0; y < h; y++) {
726                         unsigned char tmp[4];
727                         unsigned int *tmp_l =
728                             (unsigned int *) tmp;
729                         
730                         for (x = 0; x < w; x++) {
731                                 tmp[0] = bottom[0];
732                                 tmp[1] = bottom[1];
733                                 tmp[2] = bottom[2];
734                                 tmp[3] = bottom[3];
735                                 
736                                 bottom[0] = top[0];
737                                 bottom[1] = top[1];
738                                 bottom[2] = top[2];
739                                 bottom[3] = top[3];
740                                 
741                                 *(unsigned int *) top = *tmp_l;
742                                 
743                                 bottom += 4;
744                                 top += 4;
745                         }
746                         top -= 8 * w;
747                 }
748         }
749         else {
750                 int *dstStride   = anim->pFrameRGB->linesize;
751                 uint8_t **dst     = anim->pFrameRGB->data;
752                 int dstStride2[4] = { -dstStride[0], 0, 0, 0 };
753                 uint8_t *dst2[4]  = { dst[0] + (anim->y - 1) * dstStride[0],
754                                           0, 0, 0 };
755                 
756                 sws_scale(anim->img_convert_ctx,
757                           (const uint8_t *const *)input->data,
758                           input->linesize,
759                           0,
760                           anim->y,
761                           dst2,
762                           dstStride2);
763         }
764
765         if (filter_y) {
766                 IMB_filtery(ibuf);
767         }
768 }
769
770 /* decode one video frame also considering the packet read into next_packet */
771
772 static int ffmpeg_decode_video_frame(struct anim *anim)
773 {
774         int rval = 0;
775
776         av_log(anim->pFormatCtx, AV_LOG_DEBUG, "  DECODE VIDEO FRAME\n");
777
778         if (anim->next_packet.stream_index == anim->videoStream) {
779                 av_free_packet(&anim->next_packet);
780                 anim->next_packet.stream_index = -1;
781         }
782         
783         while ((rval = av_read_frame(anim->pFormatCtx, &anim->next_packet)) >= 0) {
784                 av_log(anim->pFormatCtx, 
785                        AV_LOG_DEBUG, 
786                        "%sREAD: strID=%d (VID: %d) dts=%lld pts=%lld "
787                        "%s\n",
788                        (anim->next_packet.stream_index == anim->videoStream)
789                        ? "->" : "  ",
790                        anim->next_packet.stream_index, 
791                        anim->videoStream,
792                        (anim->next_packet.dts == AV_NOPTS_VALUE) ? -1 :
793                        (long long int)anim->next_packet.dts,
794                        (anim->next_packet.pts == AV_NOPTS_VALUE) ? -1 :
795                        (long long int)anim->next_packet.pts,
796                        (anim->next_packet.flags & AV_PKT_FLAG_KEY) ? 
797                        " KEY" : "");
798                 if (anim->next_packet.stream_index == anim->videoStream) {
799                         anim->pFrameComplete = 0;
800
801                         avcodec_decode_video2(
802                             anim->pCodecCtx,
803                             anim->pFrame, &anim->pFrameComplete,
804                             &anim->next_packet);
805
806                         if (anim->pFrameComplete) {
807                                 anim->next_pts = av_get_pts_from_frame(
808                                         anim->pFormatCtx, anim->pFrame);
809
810                                 av_log(anim->pFormatCtx,
811                                        AV_LOG_DEBUG,
812                                        "  FRAME DONE: next_pts=%lld "
813                                        "pkt_pts=%lld, guessed_pts=%lld\n",
814                                        (anim->pFrame->pts == AV_NOPTS_VALUE) ?
815                                        -1 : (long long int)anim->pFrame->pts,
816                                        (anim->pFrame->pkt_pts == AV_NOPTS_VALUE) ?
817                                        -1 : (long long int)anim->pFrame->pkt_pts,
818                                        (long long int)anim->next_pts);
819                                 break;
820                         }
821                 }
822                 av_free_packet(&anim->next_packet);
823                 anim->next_packet.stream_index = -1;
824         }
825         
826         if (rval == AVERROR_EOF) {
827                 /* this sets size and data fields to zero,
828                  * which is necessary to decode the remaining data
829                  * in the decoder engine after EOF. It also prevents a memory
830                  * leak, since av_read_frame spills out a full size packet even
831                  * on EOF... (and: it's save to call on NULL packets) */
832
833                 av_free_packet(&anim->next_packet);
834
835                 anim->next_packet.size = 0;
836                 anim->next_packet.data = 0;
837
838                 anim->pFrameComplete = 0;
839
840                 avcodec_decode_video2(
841                         anim->pCodecCtx,
842                         anim->pFrame, &anim->pFrameComplete,
843                         &anim->next_packet);
844
845                 if (anim->pFrameComplete) {
846                         anim->next_pts = av_get_pts_from_frame(
847                                 anim->pFormatCtx, anim->pFrame);
848
849                         av_log(anim->pFormatCtx,
850                                AV_LOG_DEBUG,
851                                "  FRAME DONE (after EOF): next_pts=%lld "
852                                "pkt_pts=%lld, guessed_pts=%lld\n",
853                                (anim->pFrame->pts == AV_NOPTS_VALUE) ?
854                                -1 : (long long int)anim->pFrame->pts,
855                                (anim->pFrame->pkt_pts == AV_NOPTS_VALUE) ?
856                                -1 : (long long int)anim->pFrame->pkt_pts,
857                                (long long int)anim->next_pts);
858                         rval = 0;
859                 }
860         }
861
862         if (rval < 0) {
863                 anim->next_packet.stream_index = -1;
864
865                 av_log(anim->pFormatCtx,
866                        AV_LOG_ERROR, "  DECODE READ FAILED: av_read_frame() "
867                        "returned error: %d\n",  rval);
868         }
869
870         return (rval >= 0);
871 }
872
873 static void ffmpeg_decode_video_frame_scan(
874         struct anim *anim, int64_t pts_to_search)
875 {
876         /* there seem to exist *very* silly GOP lengths out in the wild... */
877         int count = 1000;
878
879         av_log(anim->pFormatCtx,
880                AV_LOG_DEBUG, 
881                "SCAN start: considering pts=%lld in search of %lld\n", 
882                (long long int)anim->next_pts, (long long int)pts_to_search);
883
884         while (count > 0 && anim->next_pts < pts_to_search) {
885                 av_log(anim->pFormatCtx,
886                        AV_LOG_DEBUG, 
887                        "  WHILE: pts=%lld in search of %lld\n", 
888                        (long long int)anim->next_pts, (long long int)pts_to_search);
889                 if (!ffmpeg_decode_video_frame(anim)) {
890                         break;
891                 }
892                 count--;
893         }
894         if (count == 0) {
895                 av_log(anim->pFormatCtx,
896                        AV_LOG_ERROR, 
897                        "SCAN failed: completely lost in stream, "
898                        "bailing out at PTS=%lld, searching for PTS=%lld\n", 
899                        (long long int)anim->next_pts, (long long int)pts_to_search);
900         }
901         if (anim->next_pts == pts_to_search) {
902                 av_log(anim->pFormatCtx,
903                        AV_LOG_DEBUG, "SCAN HAPPY: we found our PTS!\n");
904         }
905         else {
906                 av_log(anim->pFormatCtx,
907                        AV_LOG_ERROR, "SCAN UNHAPPY: PTS not matched!\n");
908         }
909 }
910
911 static int match_format(const char *name, AVFormatContext *pFormatCtx)
912 {
913         const char *p;
914         int len, namelen;
915
916         const char *names = pFormatCtx->iformat->name;
917
918         if (!name || !names)
919                 return 0;
920
921         namelen = strlen(name);
922         while ((p = strchr(names, ','))) {
923                 len = MAX2(p - names, namelen);
924                 if (!BLI_strncasecmp(name, names, len))
925                         return 1;
926                 names = p + 1;
927         }
928         return !BLI_strcasecmp(name, names);
929 }
930
931 static int ffmpeg_seek_by_byte(AVFormatContext *pFormatCtx)
932 {
933         static const char *byte_seek_list[] = { "mpegts", 0 };
934         const char **p;
935
936         if (pFormatCtx->iformat->flags & AVFMT_TS_DISCONT) {
937                 return TRUE;
938         }
939
940         p = byte_seek_list;
941
942         while (*p) {
943                 if (match_format(*p++, pFormatCtx)) {
944                         return TRUE;
945                 }
946         }
947
948         return FALSE;
949 }
950
951 static ImBuf *ffmpeg_fetchibuf(struct anim *anim, int position,
952                                IMB_Timecode_Type tc)
953 {
954         int64_t pts_to_search = 0;
955         double frame_rate;
956         double pts_time_base;
957         long long st_time; 
958         struct anim_index *tc_index = 0;
959         AVStream *v_st;
960         int new_frame_index = 0; /* To quiet gcc barking... */
961         int old_frame_index = 0; /* To quiet gcc barking... */
962
963         if (anim == 0) return (0);
964
965         av_log(anim->pFormatCtx, AV_LOG_DEBUG, "FETCH: pos=%d\n", position);
966
967         if (tc != IMB_TC_NONE) {
968                 tc_index = IMB_anim_open_index(anim, tc);
969         }
970
971         v_st = anim->pFormatCtx->streams[anim->videoStream];
972
973         frame_rate = av_q2d(v_st->r_frame_rate);
974
975         st_time = anim->pFormatCtx->start_time;
976         pts_time_base = av_q2d(v_st->time_base);
977
978         if (tc_index) {
979                 new_frame_index = IMB_indexer_get_frame_index(
980                         tc_index, position);
981                 old_frame_index = IMB_indexer_get_frame_index(
982                         tc_index, anim->curposition);
983                 pts_to_search = IMB_indexer_get_pts(
984                         tc_index, new_frame_index);
985         }
986         else {
987                 pts_to_search = (long long) 
988                                 floor(((double) position) /
989                                       pts_time_base / frame_rate + 0.5);
990
991                 if (st_time != AV_NOPTS_VALUE) {
992                         pts_to_search += st_time / pts_time_base / AV_TIME_BASE;
993                 }
994         }
995
996         av_log(anim->pFormatCtx, AV_LOG_DEBUG, 
997                "FETCH: looking for PTS=%lld "
998                "(pts_timebase=%g, frame_rate=%g, st_time=%lld)\n", 
999                (long long int)pts_to_search, pts_time_base, frame_rate, st_time);
1000
1001         if (anim->last_frame && 
1002             anim->last_pts <= pts_to_search && anim->next_pts > pts_to_search)
1003         {
1004                 av_log(anim->pFormatCtx, AV_LOG_DEBUG, 
1005                        "FETCH: frame repeat: last: %lld next: %lld\n",
1006                        (long long int)anim->last_pts, 
1007                        (long long int)anim->next_pts);
1008                 IMB_refImBuf(anim->last_frame);
1009                 anim->curposition = position;
1010                 return anim->last_frame;
1011         }
1012          
1013         if (position > anim->curposition + 1 &&
1014             anim->preseek &&
1015             !tc_index &&
1016             position - (anim->curposition + 1) < anim->preseek)
1017         {
1018                 av_log(anim->pFormatCtx, AV_LOG_DEBUG, 
1019                        "FETCH: within preseek interval (no index)\n");
1020
1021                 ffmpeg_decode_video_frame_scan(anim, pts_to_search);
1022         }
1023         else if (tc_index &&
1024                  IMB_indexer_can_scan(tc_index, old_frame_index,
1025                                       new_frame_index))
1026         {
1027                 av_log(anim->pFormatCtx, AV_LOG_DEBUG, 
1028                        "FETCH: within preseek interval "
1029                        "(index tells us)\n");
1030
1031                 ffmpeg_decode_video_frame_scan(anim, pts_to_search);
1032         }
1033         else if (position != anim->curposition + 1) {
1034                 long long pos;
1035                 int ret;
1036
1037                 if (tc_index) {
1038                         unsigned long long dts;
1039
1040                         pos = IMB_indexer_get_seek_pos(
1041                             tc_index, new_frame_index);
1042                         dts = IMB_indexer_get_seek_pos_dts(
1043                             tc_index, new_frame_index);
1044
1045                         av_log(anim->pFormatCtx, AV_LOG_DEBUG, 
1046                                "TC INDEX seek pos = %lld\n", pos);
1047                         av_log(anim->pFormatCtx, AV_LOG_DEBUG, 
1048                                "TC INDEX seek dts = %lld\n", dts);
1049
1050                         if (ffmpeg_seek_by_byte(anim->pFormatCtx)) {
1051                                 av_log(anim->pFormatCtx, AV_LOG_DEBUG, 
1052                                        "... using BYTE pos\n");
1053
1054                                 ret = av_seek_frame(anim->pFormatCtx, 
1055                                                     -1,
1056                                                     pos, AVSEEK_FLAG_BYTE);
1057                                 av_update_cur_dts(anim->pFormatCtx, v_st, dts);
1058                         }
1059                         else {
1060                                 av_log(anim->pFormatCtx, AV_LOG_DEBUG, 
1061                                        "... using DTS pos\n");
1062                                 ret = av_seek_frame(anim->pFormatCtx, 
1063                                                     anim->videoStream,
1064                                                     dts, AVSEEK_FLAG_BACKWARD);
1065                         }
1066                 }
1067                 else {
1068                         pos = (long long) (position - anim->preseek) *
1069                               AV_TIME_BASE / frame_rate;
1070
1071                         av_log(anim->pFormatCtx, AV_LOG_DEBUG, 
1072                                "NO INDEX seek pos = %lld, st_time = %lld\n", 
1073                                pos, (st_time != AV_NOPTS_VALUE) ? st_time : 0);
1074
1075                         if (pos < 0) {
1076                                 pos = 0;
1077                         }
1078                 
1079                         if (st_time != AV_NOPTS_VALUE) {
1080                                 pos += st_time;
1081                         }
1082
1083                         av_log(anim->pFormatCtx, AV_LOG_DEBUG, 
1084                                "NO INDEX final seek pos = %lld\n", pos);
1085
1086                         ret = av_seek_frame(anim->pFormatCtx, -1, 
1087                                             pos, AVSEEK_FLAG_BACKWARD);
1088                 }
1089
1090                 if (ret < 0) {
1091                         av_log(anim->pFormatCtx, AV_LOG_ERROR,
1092                                "FETCH: "
1093                                "error while seeking to DTS = %lld "
1094                                "(frameno = %d, PTS = %lld): errcode = %d\n",
1095                                pos, position, (long long int)pts_to_search, ret);
1096                 }
1097
1098                 avcodec_flush_buffers(anim->pCodecCtx);
1099
1100                 anim->next_pts = -1;
1101
1102                 if (anim->next_packet.stream_index == anim->videoStream) {
1103                         av_free_packet(&anim->next_packet);
1104                         anim->next_packet.stream_index = -1;
1105                 }
1106
1107                 /* memset(anim->pFrame, ...) ?? */
1108
1109                 if (ret >= 0) {
1110                         ffmpeg_decode_video_frame_scan(anim, pts_to_search);
1111                 }
1112         }
1113         else if (position == 0 && anim->curposition == -1) {
1114                 /* first frame without seeking special case... */
1115                 ffmpeg_decode_video_frame(anim);
1116         }
1117         else {
1118                 av_log(anim->pFormatCtx, AV_LOG_DEBUG, 
1119                        "FETCH: no seek necessary, just continue...\n");
1120         }
1121
1122         IMB_freeImBuf(anim->last_frame);
1123         anim->last_frame = IMB_allocImBuf(anim->x, anim->y, 32, IB_rect);
1124         anim->last_frame->rect_colorspace = colormanage_colorspace_get_named(anim->colorspace);
1125
1126         ffmpeg_postprocess(anim);
1127
1128         anim->last_pts = anim->next_pts;
1129         
1130         ffmpeg_decode_video_frame(anim);
1131         
1132         anim->curposition = position;
1133         
1134         IMB_refImBuf(anim->last_frame);
1135
1136         return anim->last_frame;
1137 }
1138
1139 static void free_anim_ffmpeg(struct anim *anim)
1140 {
1141         if (anim == NULL) return;
1142
1143         if (anim->pCodecCtx) {
1144                 avcodec_close(anim->pCodecCtx);
1145                 av_close_input_file(anim->pFormatCtx);
1146                 av_free(anim->pFrameRGB);
1147                 av_free(anim->pFrame);
1148
1149                 if (anim->ib_flags & IB_animdeinterlace) {
1150                         MEM_freeN(anim->pFrameDeinterlaced->data[0]);
1151                 }
1152                 av_free(anim->pFrameDeinterlaced);
1153                 sws_freeContext(anim->img_convert_ctx);
1154                 IMB_freeImBuf(anim->last_frame);
1155                 if (anim->next_packet.stream_index != -1) {
1156                         av_free_packet(&anim->next_packet);
1157                 }
1158         }
1159         anim->duration = 0;
1160 }
1161
1162 #endif
1163
1164 #ifdef WITH_REDCODE
1165
1166 static int startredcode(struct anim *anim)
1167 {
1168         anim->redcodeCtx = redcode_open(anim->name);
1169         if (!anim->redcodeCtx) {
1170                 return -1;
1171         }
1172         anim->duration = redcode_get_length(anim->redcodeCtx);
1173         
1174         return 0;
1175 }
1176
1177 static ImBuf *redcode_fetchibuf(struct anim *anim, int position)
1178 {
1179         struct ImBuf *ibuf;
1180         struct redcode_frame *frame;
1181         struct redcode_frame_raw *raw_frame;
1182
1183         if (!anim->redcodeCtx) {
1184                 return NULL;
1185         }
1186
1187         frame = redcode_read_video_frame(anim->redcodeCtx, position);
1188         
1189         if (!frame) {
1190                 return NULL;
1191         }
1192
1193         raw_frame = redcode_decode_video_raw(frame, 1);
1194
1195         redcode_free_frame(frame);
1196
1197         if (!raw_frame) {
1198                 return NULL;
1199         }
1200         
1201         ibuf = IMB_allocImBuf(raw_frame->width * 2,
1202                               raw_frame->height * 2, 32, IB_rectfloat);
1203
1204         redcode_decode_video_float(raw_frame, ibuf->rect_float, 1);
1205
1206         return ibuf;
1207 }
1208
1209 static void free_anim_redcode(struct anim *anim)
1210 {
1211         if (anim->redcodeCtx) {
1212                 redcode_close(anim->redcodeCtx);
1213                 anim->redcodeCtx = 0;
1214         }
1215         anim->duration = 0;
1216 }
1217
1218 #endif
1219
1220 /* Try next picture to read */
1221 /* No picture, try to open next animation */
1222 /* Succeed, remove first image from animation */
1223
1224 static ImBuf *anim_getnew(struct anim *anim)
1225 {
1226         struct ImBuf *ibuf = NULL;
1227
1228         if (anim == NULL) return(NULL);
1229
1230         free_anim_movie(anim);
1231
1232 #ifdef WITH_AVI
1233         free_anim_avi(anim);
1234 #endif
1235
1236 #ifdef WITH_QUICKTIME
1237         free_anim_quicktime(anim);
1238 #endif
1239 #ifdef WITH_FFMPEG
1240         free_anim_ffmpeg(anim);
1241 #endif
1242 #ifdef WITH_REDCODE
1243         free_anim_redcode(anim);
1244 #endif
1245
1246
1247         if (anim->curtype != 0) return (NULL);
1248         anim->curtype = imb_get_anim_type(anim->name);
1249
1250         switch (anim->curtype) {
1251                 case ANIM_SEQUENCE:
1252                         ibuf = IMB_loadiffname(anim->name, anim->ib_flags, anim->colorspace);
1253                         if (ibuf) {
1254                                 BLI_strncpy(anim->first, anim->name, sizeof(anim->first));
1255                                 anim->duration = 1;
1256                         }
1257                         break;
1258                 case ANIM_MOVIE:
1259                         if (startmovie(anim)) return (NULL);
1260                         ibuf = IMB_allocImBuf(anim->x, anim->y, 24, 0); /* fake */
1261                         break;
1262 #ifdef WITH_AVI
1263                 case ANIM_AVI:
1264                         if (startavi(anim)) {
1265                                 printf("couldnt start avi\n");
1266                                 return (NULL);
1267                         }
1268                         ibuf = IMB_allocImBuf(anim->x, anim->y, 24, 0);
1269                         break;
1270 #endif
1271 #ifdef WITH_QUICKTIME
1272                 case ANIM_QTIME:
1273                         if (startquicktime(anim)) return (0);
1274                         ibuf = IMB_allocImBuf(anim->x, anim->y, 24, 0);
1275                         break;
1276 #endif
1277 #ifdef WITH_FFMPEG
1278                 case ANIM_FFMPEG:
1279                         if (startffmpeg(anim)) return (0);
1280                         ibuf = IMB_allocImBuf(anim->x, anim->y, 24, 0);
1281                         break;
1282 #endif
1283 #ifdef WITH_REDCODE
1284                 case ANIM_REDCODE:
1285                         if (startredcode(anim)) return (0);
1286                         ibuf = IMB_allocImBuf(8, 8, 32, 0);
1287                         break;
1288 #endif
1289         }
1290         return(ibuf);
1291 }
1292
1293 struct ImBuf *IMB_anim_previewframe(struct anim *anim)
1294 {
1295         struct ImBuf *ibuf = NULL;
1296         int position = 0;
1297         
1298         ibuf = IMB_anim_absolute(anim, 0, IMB_TC_NONE, IMB_PROXY_NONE);
1299         if (ibuf) {
1300                 IMB_freeImBuf(ibuf);
1301                 position = anim->duration / 2;
1302                 ibuf = IMB_anim_absolute(anim, position, IMB_TC_NONE,
1303                                          IMB_PROXY_NONE);
1304         }
1305         return ibuf;
1306 }
1307
1308 struct ImBuf *IMB_anim_absolute(struct anim *anim, int position,
1309                                 IMB_Timecode_Type tc,
1310                                 IMB_Proxy_Size preview_size)
1311 {
1312         struct ImBuf *ibuf = NULL;
1313         char head[256], tail[256];
1314         unsigned short digits;
1315         int pic;
1316         int filter_y;
1317         if (anim == NULL) return(NULL);
1318
1319         filter_y = (anim->ib_flags & IB_animdeinterlace);
1320
1321         if (anim->curtype == 0) {
1322                 ibuf = anim_getnew(anim);
1323                 if (ibuf == NULL) {
1324                         return(NULL);
1325                 }
1326
1327                 IMB_freeImBuf(ibuf); /* ???? */
1328                 ibuf = NULL;
1329         }
1330
1331         if (position < 0) return(NULL);
1332         if (position >= anim->duration) return(NULL);
1333
1334         if (preview_size != IMB_PROXY_NONE) {
1335                 struct anim *proxy = IMB_anim_open_proxy(anim, preview_size);
1336
1337                 if (proxy) {
1338                         position = IMB_anim_index_get_frame_index(
1339                             anim, tc, position);
1340                         return IMB_anim_absolute(
1341                                    proxy, position,
1342                                    IMB_TC_NONE, IMB_PROXY_NONE);
1343                 }
1344         }
1345
1346         switch (anim->curtype) {
1347                 case ANIM_SEQUENCE:
1348                         pic = an_stringdec(anim->first, head, tail, &digits);
1349                         pic += position;
1350                         an_stringenc(anim->name, head, tail, digits, pic);
1351                         ibuf = IMB_loadiffname(anim->name, IB_rect, anim->colorspace);
1352                         if (ibuf) {
1353                                 anim->curposition = position;
1354                         }
1355                         break;
1356                 case ANIM_MOVIE:
1357                         ibuf = movie_fetchibuf(anim, position);
1358                         if (ibuf) {
1359                                 anim->curposition = position;
1360                                 IMB_convert_rgba_to_abgr(ibuf);
1361                         }
1362                         break;
1363 #ifdef WITH_AVI
1364                 case ANIM_AVI:
1365                         ibuf = avi_fetchibuf(anim, position);
1366                         if (ibuf)
1367                                 anim->curposition = position;
1368                         break;
1369 #endif
1370 #ifdef WITH_QUICKTIME
1371                 case ANIM_QTIME:
1372                         ibuf = qtime_fetchibuf(anim, position);
1373                         if (ibuf) {
1374                                 if (ibuf->rect) {
1375                                         /* OCIO_TODO: should happen in quicktime module, but it currently doesn't have access
1376                                          *            to color management's internals
1377                                          */
1378                                         ibuf->rect_colorspace = colormanage_colorspace_get_named(anim->colorspace);
1379                                 }
1380
1381                                 anim->curposition = position;
1382                         }
1383                         break;
1384 #endif
1385 #ifdef WITH_FFMPEG
1386                 case ANIM_FFMPEG:
1387                         ibuf = ffmpeg_fetchibuf(anim, position, tc);
1388                         if (ibuf)
1389                                 anim->curposition = position;
1390                         filter_y = 0; /* done internally */
1391                         break;
1392 #endif
1393 #ifdef WITH_REDCODE
1394                 case ANIM_REDCODE:
1395                         ibuf = redcode_fetchibuf(anim, position);
1396                         if (ibuf) anim->curposition = position;
1397                         break;
1398 #endif
1399         }
1400
1401         if (ibuf) {
1402                 if (filter_y) IMB_filtery(ibuf);
1403                 BLI_snprintf(ibuf->name, sizeof(ibuf->name), "%s.%04d", anim->name, anim->curposition + 1);
1404                 
1405         }
1406         return(ibuf);
1407 }
1408
1409 /***/
1410
1411 int IMB_anim_get_duration(struct anim *anim, IMB_Timecode_Type tc)
1412 {
1413         struct anim_index *idx;
1414         if (tc == IMB_TC_NONE) {
1415                 return anim->duration;
1416         }
1417         
1418         idx = IMB_anim_open_index(anim, tc);
1419         if (!idx) {
1420                 return anim->duration;
1421         }
1422
1423         return IMB_indexer_get_duration(idx);
1424 }
1425
1426 int IMB_anim_get_fps(struct anim *anim,
1427                      short *frs_sec, float *frs_sec_base)
1428 {
1429         if (anim->frs_sec) {
1430                 *frs_sec = anim->frs_sec;
1431                 *frs_sec_base = anim->frs_sec_base;
1432                 return TRUE;
1433         }
1434         return FALSE;
1435 }
1436
1437 void IMB_anim_set_preseek(struct anim *anim, int preseek)
1438 {
1439         anim->preseek = preseek;
1440 }
1441
1442 int IMB_anim_get_preseek(struct anim *anim)
1443 {
1444         return anim->preseek;
1445 }