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