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