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