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