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