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