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