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