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