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