== Sequencer ==
[blender.git] / source / blender / imbuf / intern / anim.c
1 /**
2  * anim.c
3  *
4  * $Id$
5  *
6  * ***** BEGIN GPL/BL DUAL 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. The Blender
12  * Foundation also sells licenses for use in proprietary software under
13  * the Blender License.  See http://www.blender.org/BL/ for information
14  * about this.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software Foundation,
23  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
24  *
25  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
26  * All rights reserved.
27  *
28  * The Original Code is: all of this file.
29  *
30  * Contributor(s): none yet.
31  *
32  * ***** END GPL/BL DUAL LICENSE BLOCK *****
33  */
34
35 #ifdef _WIN32
36 #define INC_OLE2
37 #include <windows.h>
38 #include <windowsx.h>
39 #include <mmsystem.h>
40 #include <memory.h>
41 #include <commdlg.h>
42
43 #ifndef FREE_WINDOWS
44 #include <vfw.h>
45 #endif
46
47 #undef AVIIF_KEYFRAME // redefined in AVI_avi.h
48 #undef AVIIF_LIST // redefined in AVI_avi.h
49
50 #define FIXCC(fcc)  if (fcc == 0)       fcc = mmioFOURCC('N', 'o', 'n', 'e'); \
51                 if (fcc == BI_RLE8) fcc = mmioFOURCC('R', 'l', 'e', '8');
52 #endif
53
54 #include <sys/types.h>
55 #include <ctype.h>
56 #include <stdlib.h>
57 #include <stdio.h>
58 #ifndef _WIN32
59 #include <dirent.h>
60 #else
61 #include <io.h>
62 #endif
63
64 #include "BLI_blenlib.h" /* BLI_remlink BLI_filesize BLI_addtail
65                             BLI_countlist BLI_stringdec */
66 #include "DNA_userdef_types.h"
67 #include "BKE_global.h"
68
69 #include "imbuf.h"
70 #include "imbuf_patch.h"
71
72 #include "AVI_avi.h"
73
74 #ifdef WITH_QUICKTIME
75 #if defined(_WIN32) || defined(__APPLE__)
76 #include "quicktime_import.h"
77 #endif /* _WIN32 || __APPLE__ */
78 #endif /* WITH_QUICKTIME */
79
80 #include "IMB_imbuf_types.h"
81 #include "IMB_imbuf.h"
82
83 #include "IMB_allocimbuf.h"
84 #include "IMB_bitplanes.h"
85 #include "IMB_anim.h"
86 #include "IMB_anim5.h"
87
88 #ifdef WITH_FFMPEG
89 #include <ffmpeg/avformat.h>
90 #include <ffmpeg/avcodec.h>
91 #include <ffmpeg/rational.h>
92 #include <ffmpeg/swscale.h>
93
94 #if LIBAVFORMAT_VERSION_INT < (49 << 16)
95 #define FFMPEG_OLD_FRAME_RATE 1
96 #else
97 #define FFMPEG_CODEC_IS_POINTER 1
98 #endif
99
100 #endif
101
102 /****/
103
104 #ifdef __sgi
105
106 #include <dmedia/moviefile.h>
107
108 static void movie_printerror(char * str) {
109         const char * errstr = mvGetErrorStr(mvGetErrno());
110
111         if (str) {
112                 if (errstr) printf("%s: %s\n", str, errstr);
113                 else printf("%s: returned error\n", str);
114         } else printf("%s\n", errstr);
115 }
116
117 static int startmovie(struct anim * anim) {
118         if (anim == 0) return(-1);
119
120         if ( mvOpenFile (anim->name, O_BINARY|O_RDONLY, &anim->movie ) != DM_SUCCESS ) {
121                 printf("Can't open movie: %s\n", anim->name);
122                 return(-1);
123         }
124         if ( mvFindTrackByMedium (anim->movie, DM_IMAGE, &anim->track) != DM_SUCCESS ) {
125                 printf("No image track in movie: %s\n", anim->name);
126                 mvClose(anim->movie);
127                 return(-1);
128         }
129
130         anim->duration = mvGetTrackLength (anim->track);
131         anim->params = mvGetParams( anim->track );
132
133         anim->x = dmParamsGetInt( anim->params, DM_IMAGE_WIDTH);
134         anim->y = dmParamsGetInt( anim->params, DM_IMAGE_HEIGHT);
135         anim->interlacing = dmParamsGetEnum (anim->params, DM_IMAGE_INTERLACING);
136         anim->orientation = dmParamsGetEnum (anim->params, DM_IMAGE_ORIENTATION);
137         anim->framesize = dmImageFrameSize(anim->params);
138
139         anim->curposition = 0;
140         anim->preseek = 0;
141
142         /*printf("x:%d y:%d size:%d interl:%d dur:%d\n", anim->x, anim->y, anim->framesize, anim->interlacing, anim->duration);*/
143         return (0);
144 }
145
146 static ImBuf * movie_fetchibuf(struct anim * anim, int position) {
147         ImBuf * ibuf;
148 /*      extern rectcpy(); */
149         int size;
150         unsigned int *rect1, *rect2;
151
152         if (anim == 0) return (0);
153
154         ibuf = IMB_allocImBuf(anim->x, anim->y, 24, IB_rect, 0);
155
156         if ( mvReadFrames(anim->track, position, 1, ibuf->x * ibuf->y * 
157                 sizeof(int), ibuf->rect ) != DM_SUCCESS ) {
158                 movie_printerror("mvReadFrames");
159                 IMB_freeImBuf(ibuf);
160                 return(0);
161         }
162
163 /*
164         if (anim->interlacing == DM_IMAGE_INTERLACED_EVEN) {
165                 rect1 = ibuf->rect + (ibuf->x * ibuf->y) - 1;
166                 rect2 = rect1 - ibuf->x;
167     
168                 for (size = ibuf->x * (ibuf->y - 1); size > 0; size--){
169                         *rect1-- = *rect2--;
170                 }
171         }
172 */
173
174         if (anim->interlacing == DM_IMAGE_INTERLACED_EVEN)
175         {
176                 rect1 = ibuf->rect;
177                 rect2 = rect1 + ibuf->x;
178
179                 for (size = ibuf->x * (ibuf->y - 1); size > 0; size--){
180                         *rect1++ = *rect2++;
181                 }
182         }
183         /*if (anim->orientation == DM_TOP_TO_BOTTOM) IMB_flipy(ibuf);*/
184
185
186         return(ibuf);
187 }
188
189 static void free_anim_movie(struct anim * anim) {
190         if (anim == NULL) return;
191
192         if (anim->movie) {
193                 mvClose(anim->movie);
194                 anim->movie = NULL;
195         }
196         anim->duration = 0;
197 }
198
199 int ismovie(char *name) {
200         return (mvIsMovieFile(name) == DM_TRUE);
201 }
202
203 #else
204
205 int ismovie(char *name) {
206         return 0;
207 }
208
209         /* never called, just keep the linker happy */
210 static int startmovie(struct anim * anim) { return 1; }
211 static ImBuf * movie_fetchibuf(struct anim * anim, int position) { return NULL; }
212 static void free_anim_movie(struct anim * anim) { ; }
213
214 #endif
215
216 static int an_stringdec(char *string, char* kop, char *staart,unsigned short *numlen) {
217         unsigned short len,nume,nums=0;
218         short i,found=FALSE;
219
220         len=strlen(string);
221         nume = len;
222
223         for(i=len-1;i>=0;i--){
224                 if (string[i]=='/') break;
225                 if (isdigit(string[i])) {
226                         if (found){
227                                 nums=i;
228                         } else{
229                                 nume=i;
230                                 nums=i;
231                                 found=TRUE;
232                         }
233                 } else{
234                         if (found) break;
235                 }
236         }
237         if (found){
238                 strcpy(staart,&string[nume+1]);
239                 strcpy(kop,string);
240                 kop[nums]=0;
241                 *numlen=nume-nums+1;
242                 return ((int)atoi(&(string[nums])));
243         }
244         staart[0]=0;
245         strcpy(kop,string);
246         *numlen=0;
247         return (1);
248 }
249
250
251 static void an_stringenc(char *string, char *kop, char *staart, 
252 unsigned short numlen, int pic) {
253         char numstr[10];
254         unsigned short len,i;
255
256         len=sprintf(numstr,"%d",pic);
257
258         strcpy(string,kop);
259         for(i=len;i<numlen;i++){
260                 strcat(string,"0");
261         }
262         strcat(string,numstr);
263         strcat(string,staart);
264 }
265
266
267 static void free_anim_avi (struct anim *anim) {
268 #if defined(_WIN32) && !defined(FREE_WINDOWS)
269         int i;
270 #endif
271
272         if (anim == NULL) return;
273         if (anim->avi == NULL) return;
274
275         AVI_close (anim->avi);
276         MEM_freeN (anim->avi);
277         anim->avi = NULL;
278
279 #if defined(_WIN32) && !defined(FREE_WINDOWS)
280
281         if (anim->pgf) {
282                 AVIStreamGetFrameClose(anim->pgf);
283                 anim->pgf = NULL;
284         }
285
286         for (i = 0; i < anim->avistreams; i++){
287                 AVIStreamRelease(anim->pavi[i]);
288         }
289         anim->avistreams = 0;
290
291         if (anim->pfileopen) {
292                 AVIFileRelease(anim->pfile);
293                 anim->pfileopen = 0;
294                 AVIFileExit();
295         }
296 #endif
297
298         anim->duration = 0;
299 }
300
301 void IMB_free_anim_ibuf(struct anim * anim) {
302         if (anim == NULL) return;
303
304         if (anim->ibuf1) IMB_freeImBuf(anim->ibuf1);
305         if (anim->ibuf2) IMB_freeImBuf(anim->ibuf2);
306
307         anim->ibuf1 = anim->ibuf2 = NULL;
308 }
309
310 #ifdef WITH_FFMPEG
311 static void free_anim_ffmpeg(struct anim * anim);
312 #endif
313
314 void IMB_free_anim(struct anim * anim) {
315         if (anim == NULL) {
316                 printf("free anim, anim == NULL\n");
317                 return;
318         }
319
320         IMB_free_anim_ibuf(anim);
321         free_anim_anim5(anim);
322         free_anim_movie(anim);
323         free_anim_avi(anim);
324
325 #ifdef WITH_QUICKTIME
326         free_anim_quicktime(anim);
327 #endif
328 #ifdef WITH_FFMPEG
329         free_anim_ffmpeg(anim);
330 #endif
331
332         free(anim);
333 }
334
335 void IMB_close_anim(struct anim * anim) {
336         if (anim == 0) return;
337
338         IMB_free_anim(anim);
339 }
340
341
342 struct anim * IMB_open_anim( const char * name, int ib_flags) {
343         struct anim * anim;
344
345         anim = (struct anim*)MEM_callocN(sizeof(struct anim), "anim struct");
346         if (anim != NULL) {
347                 strcpy(anim->name, name);  /* fixme: possible buffer overflow here? */
348                 anim->ib_flags = ib_flags;
349         }
350         return(anim);
351 }
352
353
354 static int startavi (struct anim *anim) {
355
356         AviError avierror;
357 #if defined(_WIN32) && !defined(FREE_WINDOWS)
358         HRESULT hr;
359         int i, firstvideo = -1;
360         BYTE abFormat[1024];
361         LONG l;
362         LPBITMAPINFOHEADER lpbi;
363         AVISTREAMINFO avis;
364 #endif
365
366         anim->avi = MEM_callocN (sizeof(AviMovie),"animavi");
367
368         if (anim->avi == NULL) {
369                 printf("Can't open avi: %s\n", anim->name);
370                 return -1;
371         }
372
373         avierror = AVI_open_movie (anim->name, anim->avi);
374
375 #if defined(_WIN32) && !defined(FREE_WINDOWS)
376         if (avierror == AVI_ERROR_COMPRESSION) {
377                 AVIFileInit();
378                 hr = AVIFileOpen(&anim->pfile, anim->name, OF_READ, 0L);
379                 if (hr == 0) {
380                         anim->pfileopen = 1;
381                         for (i = 0; i < MAXNUMSTREAMS; i++) {
382                                 if (AVIFileGetStream(anim->pfile, &anim->pavi[i], 0L, i) != AVIERR_OK) {
383                                         break;
384                                 }
385                                 
386                                 AVIStreamInfo(anim->pavi[i], &avis, sizeof(avis));
387                                 if ((avis.fccType == streamtypeVIDEO) && (firstvideo == -1)) {
388                                         anim->pgf = AVIStreamGetFrameOpen(anim->pavi[i], NULL);
389                                         if (anim->pgf) {
390                                                 firstvideo = i;
391
392                                                 // get stream length
393                                                 anim->avi->header->TotalFrames = AVIStreamLength(anim->pavi[i]);
394                                                 
395                                                 // get information about images inside the stream
396                                                 l = sizeof(abFormat);
397                                                 AVIStreamReadFormat(anim->pavi[i], 0, &abFormat, &l);
398                                                 lpbi = (LPBITMAPINFOHEADER)abFormat;
399                                                 anim->avi->header->Height = lpbi->biHeight;
400                                                 anim->avi->header->Width = lpbi->biWidth;
401                                         } else {
402                                                 FIXCC(avis.fccHandler);
403                                                 FIXCC(avis.fccType);
404                                                 printf("Can't find AVI decoder for type : %4.4hs/%4.4hs\n",
405                                                         (LPSTR)&avis.fccType,
406                                                         (LPSTR)&avis.fccHandler);
407                                         }
408                                 }
409                         }
410
411                         // register number of opened avistreams
412                         anim->avistreams = i;
413
414                         //
415                         // Couldn't get any video streams out of this file
416                         //
417                         if ((anim->avistreams == 0) || (firstvideo == -1)) {
418                                 avierror = AVI_ERROR_FORMAT;
419                         } else {
420                                 avierror = AVI_ERROR_NONE;
421                                 anim->firstvideo = firstvideo;
422                         }
423                 } else {
424                         AVIFileExit();
425                 }
426         }
427 #endif
428
429         if (avierror != AVI_ERROR_NONE) {
430                 AVI_print_error(avierror);
431                 printf ("Error loading avi: %s\n", anim->name);         
432                 free_anim_avi(anim);
433                 return -1;
434         }
435         
436         anim->duration = anim->avi->header->TotalFrames;
437         anim->params = 0;
438
439         anim->x = anim->avi->header->Width;
440         anim->y = anim->avi->header->Height;
441         anim->interlacing = 0;
442         anim->orientation = 0;
443         anim->framesize = anim->x * anim->y * 4;
444
445         anim->curposition = 0;
446         anim->preseek = 0;
447
448         /*  printf("x:%d y:%d size:%d interl:%d dur:%d\n", anim->x, anim->y, anim->framesize, anim->interlacing, anim->duration);*/
449
450         return 0;
451 }
452
453 static ImBuf * avi_fetchibuf (struct anim *anim, int position) {
454         ImBuf *ibuf = NULL;
455         int *tmp;
456         int y;
457         
458         if (anim == NULL) return (NULL);
459
460 #if defined(_WIN32) && !defined(FREE_WINDOWS)
461         if (anim->avistreams) {
462                 LPBITMAPINFOHEADER lpbi;
463
464                 if (anim->pgf) {
465                         lpbi = AVIStreamGetFrame(anim->pgf, position + AVIStreamStart(anim->pavi[anim->firstvideo]));
466                         if (lpbi) {
467                                 ibuf = IMB_ibImageFromMemory((int *) lpbi, 100, IB_rect);
468 //Oh brother...
469                         }
470                 }
471         } else {
472 #else
473         if (1) {
474 #endif
475                 ibuf = IMB_allocImBuf (anim->x, anim->y, 24, IB_rect, 0);
476
477                 tmp = AVI_read_frame (anim->avi, AVI_FORMAT_RGB32, position,
478                         AVI_get_stream(anim->avi, AVIST_VIDEO, 0));
479                 
480                 if (tmp == NULL) {
481                         printf ("Error reading frame from AVI");
482                         IMB_freeImBuf (ibuf);
483                         return NULL;
484                 }
485
486                 for (y=0; y < anim->y; y++) {
487                         memcpy (&(ibuf->rect)[((anim->y-y)-1)*anim->x],  &tmp[y*anim->x],  
488                                         anim->x * 4);
489                 }
490                 
491                 MEM_freeN (tmp);
492         }
493
494         return ibuf;
495 }
496
497 #ifdef WITH_FFMPEG
498
499 extern void do_init_ffmpeg();
500
501 #ifdef FFMPEG_CODEC_IS_POINTER
502 static AVCodecContext* get_codec_from_stream(AVStream* stream)
503 {
504         return stream->codec;
505 }
506 #else
507 static AVCodecContext* get_codec_from_stream(AVStream* stream)
508 {
509         return &stream->codec;
510 }
511 #endif
512
513 static int startffmpeg(struct anim * anim) {
514         int            i, videoStream;
515
516         AVCodec *pCodec;
517         AVFormatContext *pFormatCtx;
518         AVCodecContext *pCodecCtx;
519
520         if (anim == 0) return(-1);
521
522         do_init_ffmpeg();
523
524         if(av_open_input_file(&pFormatCtx, anim->name, NULL, 0, NULL)!=0) {
525                 return -1;
526         }
527
528         if(av_find_stream_info(pFormatCtx)<0) {
529                 av_close_input_file(pFormatCtx);
530                 return -1;
531         }
532
533         dump_format(pFormatCtx, 0, anim->name, 0);
534
535
536         /* Find the first video stream */
537         videoStream=-1;
538         for(i=0; i<pFormatCtx->nb_streams; i++)
539                 if(get_codec_from_stream(pFormatCtx->streams[i])->codec_type
540                    == CODEC_TYPE_VIDEO) {
541                         videoStream=i;
542                         break;
543                 }
544
545         if(videoStream==-1) {
546                 av_close_input_file(pFormatCtx);
547                 return -1;
548         }
549
550         pCodecCtx = get_codec_from_stream(pFormatCtx->streams[videoStream]);
551
552         /* Find the decoder for the video stream */
553         pCodec=avcodec_find_decoder(pCodecCtx->codec_id);
554         if(pCodec==NULL) {
555                 av_close_input_file(pFormatCtx);
556                 return -1;
557         }
558
559         pCodecCtx->workaround_bugs = 1;
560
561         if(avcodec_open(pCodecCtx, pCodec)<0) {
562                 av_close_input_file(pFormatCtx);
563                 return -1;
564         }
565
566 #ifdef FFMPEG_OLD_FRAME_RATE
567         if(pCodecCtx->frame_rate>1000 && pCodecCtx->frame_rate_base==1)
568                 pCodecCtx->frame_rate_base=1000;
569
570
571         anim->duration = pFormatCtx->duration * pCodecCtx->frame_rate 
572                 / pCodecCtx->frame_rate_base / AV_TIME_BASE;
573 #else
574         anim->duration = pFormatCtx->duration 
575                 * av_q2d(pFormatCtx->streams[videoStream]->r_frame_rate) 
576                 / AV_TIME_BASE;
577
578 #endif
579         anim->params = 0;
580
581         anim->x = pCodecCtx->width;
582         anim->y = pCodecCtx->height;
583         anim->interlacing = 0;
584         anim->orientation = 0;
585         anim->framesize = anim->x * anim->y * 4;
586
587         anim->curposition = -1;
588
589         anim->pFormatCtx = pFormatCtx;
590         anim->pCodecCtx = pCodecCtx;
591         anim->pCodec = pCodec;
592         anim->videoStream = videoStream;
593
594         anim->pFrame = avcodec_alloc_frame();
595         anim->pFrameRGB = avcodec_alloc_frame();
596
597         if (avpicture_get_size(PIX_FMT_RGBA32, anim->x, anim->y)
598             != anim->x * anim->y * 4) {
599                 fprintf (stderr,
600                          "ffmpeg has changed alloc scheme ... ARGHHH!\n");
601                 avcodec_close(anim->pCodecCtx);
602                 av_close_input_file(anim->pFormatCtx);
603                 av_free(anim->pFrameRGB);
604                 av_free(anim->pFrame);
605                 return -1;
606         }
607
608         if (pCodecCtx->has_b_frames) {
609                 anim->preseek = 25; /* FIXME: detect gopsize ... */
610         } else {
611                 anim->preseek = 0;
612         }
613         
614         anim->img_convert_ctx = sws_getContext(
615                 anim->pCodecCtx->width,
616                 anim->pCodecCtx->height,
617                 anim->pCodecCtx->pix_fmt,
618                 anim->pCodecCtx->width,
619                 anim->pCodecCtx->height,
620                 PIX_FMT_RGBA,
621                 SWS_FAST_BILINEAR | SWS_PRINT_INFO,
622                 NULL, NULL, NULL);
623                                 
624         return (0);
625 }
626
627 static ImBuf * ffmpeg_fetchibuf(struct anim * anim, int position) {
628         ImBuf * ibuf;
629         int frameFinished;
630         AVPacket packet;
631         int64_t pts_to_search = 0;
632         int pos_found = 1;
633
634         if (anim == 0) return (0);
635
636         ibuf = IMB_allocImBuf(anim->x, anim->y, 24, IB_rect, 0);
637
638         avpicture_fill((AVPicture *)anim->pFrameRGB, 
639                        (unsigned char*) ibuf->rect, 
640                        PIX_FMT_RGBA32, anim->x, anim->y);
641
642         if (position != anim->curposition + 1) { 
643                 if (position > anim->curposition + 1 
644                     && anim->preseek 
645                     && position - (anim->curposition + 1) < anim->preseek) {
646                         while(av_read_frame(anim->pFormatCtx, &packet)>=0) {
647                                 if (packet.stream_index == anim->videoStream) {
648                                         avcodec_decode_video(
649                                                 anim->pCodecCtx, 
650                                                 anim->pFrame, &frameFinished, 
651                                                 packet.data, packet.size);
652
653                                         if (frameFinished) {
654                                                 anim->curposition++;
655                                         }
656                                 }
657                                 av_free_packet(&packet);
658                                 if (position == anim->curposition+1) {
659                                         break;
660                                 }
661                         }
662                 }
663         }
664
665         if (position != anim->curposition + 1) { 
666 #ifdef FFMPEG_OLD_FRAME_RATE
667                 double frame_rate = 
668                         (double) anim->pCodecCtx->frame_rate
669                         / (double) anim->pCodecCtx->frame_rate_base;
670 #else
671                 double frame_rate = 
672                         av_q2d(anim->pFormatCtx->streams[anim->videoStream]
673                                ->r_frame_rate);
674 #endif
675                 double time_base = 
676                         av_q2d(anim->pFormatCtx->streams[anim->videoStream]
677                                ->time_base);
678                 long long pos = (long long) (position - anim->preseek) 
679                         * AV_TIME_BASE / frame_rate;
680                 long long st_time = anim->pFormatCtx
681                         ->streams[anim->videoStream]->start_time;
682
683                 if (pos < 0) {
684                         pos = 0;
685                 }
686
687                 if (st_time != AV_NOPTS_VALUE) {
688                         pos += st_time * AV_TIME_BASE * time_base;
689                 }
690
691                 av_seek_frame(anim->pFormatCtx, -1, 
692                               pos, AVSEEK_FLAG_BACKWARD);
693
694                 pts_to_search = (long long) 
695                         (((double) position) / time_base / frame_rate);
696                 if (st_time != AV_NOPTS_VALUE) {
697                         pts_to_search += st_time;
698                 }
699
700                 pos_found = 0;
701                 avcodec_flush_buffers(anim->pCodecCtx);
702         }
703
704         while(av_read_frame(anim->pFormatCtx, &packet)>=0) {
705                 if(packet.stream_index == anim->videoStream) {
706                         avcodec_decode_video(anim->pCodecCtx, 
707                                              anim->pFrame, &frameFinished, 
708                                              packet.data, packet.size);
709
710                         if (frameFinished && !pos_found) {
711                                 if (packet.dts >= pts_to_search) {
712                                         pos_found = 1;
713                                 }
714                         } 
715
716                         if(frameFinished && pos_found == 1) {
717                                 int * dstStride = anim->pFrameRGB->linesize;
718                                 uint8_t** dst = anim->pFrameRGB->data;
719                                 int dstStride2[4]= { -dstStride[0], 0, 0, 0 };
720                                 uint8_t* dst2[4]= {
721                                         dst[0] + (anim->y - 1)*dstStride[0],
722                                         0, 0, 0 };
723                                 int i;
724                                 unsigned char* r;
725
726                                 sws_scale(anim->img_convert_ctx,
727                                           anim->pFrame->data,
728                                           anim->pFrame->linesize,
729                                           0,
730                                           anim->pCodecCtx->height,
731                                           dst2,
732                                           dstStride2);
733                                 
734                                 /* workaround: sws_scale sets alpha = 0... */
735                                 
736                                 r = (unsigned char*) ibuf->rect;
737
738                                 for (i = 0; i < ibuf->x * ibuf->y; i++) {
739                                         r[3] = 0xff;
740                                         r+=4;
741                                 }
742
743                                 av_free_packet(&packet);
744                                 break;
745                         }
746                 }
747
748                 av_free_packet(&packet);
749         }
750
751         return(ibuf);
752 }
753
754 static void free_anim_ffmpeg(struct anim * anim) {
755         if (anim == NULL) return;
756
757         if (anim->pCodecCtx) {
758                 avcodec_close(anim->pCodecCtx);
759                 av_close_input_file(anim->pFormatCtx);
760                 av_free(anim->pFrameRGB);
761                 av_free(anim->pFrame);
762                 sws_freeContext(anim->img_convert_ctx);
763         }
764         anim->duration = 0;
765 }
766
767 #endif
768
769
770 /* probeer volgende plaatje te lezen */
771 /* Geen plaatje, probeer dan volgende animatie te openen */
772 /* gelukt, haal dan eerste plaatje van animatie */
773
774 static struct ImBuf * anim_getnew(struct anim * anim) {
775         struct ImBuf *ibuf = 0;
776
777         if (anim == NULL) return(0);
778
779         free_anim_anim5(anim);
780         free_anim_movie(anim);
781         free_anim_avi(anim);
782 #ifdef WITH_QUICKTIME
783         free_anim_quicktime(anim);
784 #endif
785 #ifdef WITH_FFMPEG
786         free_anim_ffmpeg(anim);
787 #endif
788
789         if (anim->curtype != 0) return (0);
790         anim->curtype = imb_get_anim_type(anim->name);  
791
792         switch (anim->curtype) {
793         case ANIM_ANIM5:
794                 if (startanim5(anim)) return (0);
795                 ibuf = anim5_fetchibuf(anim);
796                 break;
797         case ANIM_SEQUENCE:
798                 ibuf = IMB_loadiffname(anim->name, anim->ib_flags);
799                 if (ibuf) {
800                         strcpy(anim->first, anim->name);
801                         anim->duration = 1;
802                 }
803                 break;
804         case ANIM_MOVIE:
805                 if (startmovie(anim)) return (0);
806                 ibuf = IMB_allocImBuf (anim->x, anim->y, 24, 0, 0); /* fake */
807                 break;
808         case ANIM_AVI:
809                 if (startavi(anim)) {
810                         printf("couldnt start avi\n"); 
811                         return (0);
812                 }
813                 ibuf = IMB_allocImBuf (anim->x, anim->y, 24, 0, 0);
814                 break;
815 #ifdef WITH_QUICKTIME
816         case ANIM_QTIME:
817                 if (startquicktime(anim)) return (0);
818                 ibuf = IMB_allocImBuf (anim->x, anim->y, 24, 0, 0);
819                 break;
820 #endif
821 #ifdef WITH_FFMPEG
822         case ANIM_FFMPEG:
823                 if (startffmpeg(anim)) return (0);
824                 ibuf = IMB_allocImBuf (anim->x, anim->y, 24, 0, 0);
825                 break;
826 #endif
827         }
828
829         return(ibuf);
830 }
831
832 struct ImBuf * IMB_anim_previewframe(struct anim * anim) {
833         struct ImBuf * ibuf = 0;
834         int position = 0;
835         
836         ibuf = IMB_anim_absolute(anim, 0);
837         if (ibuf) {
838                 IMB_freeImBuf(ibuf);
839                 position = anim->duration / 2;
840                 ibuf = IMB_anim_absolute(anim, position);
841         }
842         return ibuf;
843 }
844
845 struct ImBuf * IMB_anim_absolute(struct anim * anim, int position) {
846         struct ImBuf * ibuf = 0;
847         char head[256], tail[256];
848         unsigned short digits;
849         int pic;
850
851         if (anim == NULL) return(0);
852
853         if (anim->curtype == 0) {
854                 ibuf = anim_getnew(anim);
855                 if (ibuf == NULL) {
856                         return (0);
857                 }
858                 IMB_freeImBuf(ibuf); /* ???? */
859         }
860
861         if (position < 0) return(0);
862         if (position >= anim->duration) return(0);
863
864         switch(anim->curtype) {
865         case ANIM_ANIM5:
866                 if (anim->curposition > position) rewindanim5(anim);
867                 while (anim->curposition < position) {
868                         if (nextanim5(anim)) return (0);
869                 }
870                 ibuf = anim5_fetchibuf(anim);
871                 break;
872         case ANIM_SEQUENCE:
873                 pic = an_stringdec(anim->first, head, tail, &digits);
874                 pic += position;
875                 an_stringenc(anim->name, head, tail, digits, pic);
876                 ibuf = IMB_loadiffname(anim->name, LI_rect);
877                 if (ibuf) {
878                         anim->curposition = position;
879                         /* patch... by freeing the cmap you prevent a double apply cmap... */
880                         /* probably the IB_CMAP option isn't working proper
881                          * after the abgr->rgba reconstruction
882                          */
883                         IMB_freecmapImBuf(ibuf);
884                 }
885                 break;
886         case ANIM_MOVIE:
887                 ibuf = movie_fetchibuf(anim, position);
888                 if (ibuf) {
889                         anim->curposition = position;
890                         IMB_convert_rgba_to_abgr(ibuf);
891                 }
892                 break;
893         case ANIM_AVI:
894                 ibuf = avi_fetchibuf(anim, position);
895                 if (ibuf) anim->curposition = position;
896                 break;
897 #ifdef WITH_QUICKTIME
898         case ANIM_QTIME:
899                 ibuf = qtime_fetchibuf(anim, position);
900                 if (ibuf) anim->curposition = position;
901                 break;
902 #endif
903 #ifdef WITH_FFMPEG
904         case ANIM_FFMPEG:
905                 ibuf = ffmpeg_fetchibuf(anim, position);
906                 if (ibuf) anim->curposition = position;
907                 break;
908 #endif
909         }
910
911         if (ibuf) {
912                 if (anim->ib_flags & IB_ttob) IMB_flipy(ibuf);
913                 sprintf(ibuf->name, "%s.%04d", anim->name, anim->curposition + 1);
914                 
915         }
916         return(ibuf);
917 }
918
919 struct ImBuf * IMB_anim_nextpic(struct anim * anim) {
920         struct ImBuf * ibuf = 0;
921
922         if (anim == 0) return(0);
923
924         ibuf = IMB_anim_absolute(anim, anim->curposition + 1);
925
926         return(ibuf);
927 }
928
929 /***/
930
931 int IMB_anim_get_duration(struct anim *anim) {
932         return anim->duration;
933 }
934
935 void IMB_anim_set_preseek(struct anim * anim, int preseek)
936 {
937         anim->preseek = preseek;
938 }
939
940 int IMB_anim_get_preseek(struct anim * anim)
941 {
942         return anim->preseek;
943 }