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