87d67f5263b898ada39e8a511bb86faad7c63c9b
[blender-staging.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->pFrameRGB = avcodec_alloc_frame();
604
605         if (avpicture_get_size(PIX_FMT_BGR32, anim->x, anim->y)
606             != anim->x * anim->y * 4) {
607                 fprintf (stderr,
608                          "ffmpeg has changed alloc scheme ... ARGHHH!\n");
609                 avcodec_close(anim->pCodecCtx);
610                 av_close_input_file(anim->pFormatCtx);
611                 av_free(anim->pFrameRGB);
612                 av_free(anim->pFrame);
613                 return -1;
614         }
615
616         if (pCodecCtx->has_b_frames) {
617                 anim->preseek = 25; /* FIXME: detect gopsize ... */
618         } else {
619                 anim->preseek = 0;
620         }
621         
622         anim->img_convert_ctx = sws_getContext(
623                 anim->pCodecCtx->width,
624                 anim->pCodecCtx->height,
625                 anim->pCodecCtx->pix_fmt,
626                 anim->pCodecCtx->width,
627                 anim->pCodecCtx->height,
628                 PIX_FMT_BGR32,
629                 SWS_FAST_BILINEAR | SWS_PRINT_INFO,
630                 NULL, NULL, NULL);
631                                 
632         return (0);
633 }
634
635 static ImBuf * ffmpeg_fetchibuf(struct anim * anim, int position) {
636         ImBuf * ibuf;
637         int frameFinished;
638         AVPacket packet;
639         int64_t pts_to_search = 0;
640         int pos_found = 1;
641
642         if (anim == 0) return (0);
643
644         ibuf = IMB_allocImBuf(anim->x, anim->y, 24, IB_rect, 0);
645
646         avpicture_fill((AVPicture *)anim->pFrameRGB, 
647                        (unsigned char*) ibuf->rect, 
648                        PIX_FMT_BGR32, anim->x, anim->y);
649
650         if (position != anim->curposition + 1) { 
651                 if (position > anim->curposition + 1 
652                     && anim->preseek 
653                     && position - (anim->curposition + 1) < anim->preseek) {
654                         while(av_read_frame(anim->pFormatCtx, &packet)>=0) {
655                                 if (packet.stream_index == anim->videoStream) {
656                                         avcodec_decode_video(
657                                                 anim->pCodecCtx, 
658                                                 anim->pFrame, &frameFinished, 
659                                                 packet.data, packet.size);
660
661                                         if (frameFinished) {
662                                                 anim->curposition++;
663                                         }
664                                 }
665                                 av_free_packet(&packet);
666                                 if (position == anim->curposition+1) {
667                                         break;
668                                 }
669                         }
670                 }
671         }
672
673         if (position != anim->curposition + 1) { 
674 #ifdef FFMPEG_OLD_FRAME_RATE
675                 double frame_rate = 
676                         (double) anim->pCodecCtx->frame_rate
677                         / (double) anim->pCodecCtx->frame_rate_base;
678 #else
679                 double frame_rate = 
680                         av_q2d(anim->pFormatCtx->streams[anim->videoStream]
681                                ->r_frame_rate);
682 #endif
683                 double time_base = 
684                         av_q2d(anim->pFormatCtx->streams[anim->videoStream]
685                                ->time_base);
686                 long long pos = (long long) (position - anim->preseek) 
687                         * AV_TIME_BASE / frame_rate;
688                 long long st_time = anim->pFormatCtx
689                         ->streams[anim->videoStream]->start_time;
690
691                 if (pos < 0) {
692                         pos = 0;
693                 }
694
695                 if (st_time != AV_NOPTS_VALUE) {
696                         pos += st_time * AV_TIME_BASE * time_base;
697                 }
698
699                 av_seek_frame(anim->pFormatCtx, -1, 
700                               pos, AVSEEK_FLAG_BACKWARD);
701
702                 pts_to_search = (long long) 
703                         (((double) position) / time_base / frame_rate);
704                 if (st_time != AV_NOPTS_VALUE) {
705                         pts_to_search += st_time;
706                 }
707
708                 pos_found = 0;
709                 avcodec_flush_buffers(anim->pCodecCtx);
710         }
711
712         while(av_read_frame(anim->pFormatCtx, &packet)>=0) {
713                 if(packet.stream_index == anim->videoStream) {
714                         avcodec_decode_video(anim->pCodecCtx, 
715                                              anim->pFrame, &frameFinished, 
716                                              packet.data, packet.size);
717
718                         if (frameFinished && !pos_found) {
719                                 if (packet.dts >= pts_to_search) {
720                                         pos_found = 1;
721                                 }
722                         } 
723
724                         if(frameFinished && pos_found == 1) {
725                                 if (G.order == B_ENDIAN) {
726                                         int * dstStride 
727                                                 = anim->pFrameRGB->linesize;
728                                         uint8_t** dst = anim->pFrameRGB->data;
729                                         int dstStride2[4]
730                                                 = { dstStride[0], 0, 0, 0 };
731                                         uint8_t* dst2[4]= {
732                                                 dst[0], 0, 0, 0 };
733                                         int x,y,h,w;
734                                         unsigned char* bottom;
735                                         unsigned char* top;
736
737                                         sws_scale(anim->img_convert_ctx,
738                                                   anim->pFrame->data,
739                                                   anim->pFrame->linesize,
740                                                   0,
741                                                   anim->pCodecCtx->height,
742                                                   dst2,
743                                                   dstStride2);
744                                 
745                                         /* workaround: sws_scale 
746                                            sets alpha = 0 and compensate
747                                            for altivec-bugs and flipy... */
748                                 
749                                         bottom = (unsigned char*) ibuf->rect;
750                                         top = bottom 
751                                                 + ibuf->x * (ibuf->y-1) * 4;
752
753                                         h = (ibuf->y + 1) / 2;
754                                         w = ibuf->x;
755
756                                         for (y = 0; y < h; y++) {
757                                                 unsigned char tmp[4];
758                                                 unsigned long * tmp_l =
759                                                         (unsigned long*) tmp;
760                                                 tmp[3] = 0xff;
761
762                                                 for (x = 0; x < w; x++) {
763                                                         tmp[0] = bottom[3];
764                                                         tmp[1] = bottom[2];
765                                                         tmp[2] = bottom[1];
766
767                                                         bottom[0] = top[3];
768                                                         bottom[1] = top[2];
769                                                         bottom[2] = top[1];
770                                                         bottom[3] = 0xff;
771                                                                 
772                                                         *(unsigned long*) top
773                                                                 = *tmp_l;
774
775                                                         bottom +=4;
776                                                         top += 4;
777                                                 }
778                                                 top -= 8 * w;
779                                         }
780
781                                         av_free_packet(&packet);
782                                         break;
783                                 } else {
784                                         int * dstStride 
785                                                 = anim->pFrameRGB->linesize;
786                                         uint8_t** dst = anim->pFrameRGB->data;
787                                         int dstStride2[4]
788                                                 = { -dstStride[0], 0, 0, 0 };
789                                         uint8_t* dst2[4]= {
790                                                 dst[0] 
791                                                 + (anim->y - 1)*dstStride[0],
792                                                 0, 0, 0 };
793                                         int i;
794                                         unsigned char* r;
795                                         
796                                         /* This means the data wasnt read properly, this check stops crashing */
797                                         if (anim->pFrame->data[0]!=0 || anim->pFrame->data[1]!=0 || anim->pFrame->data[2]!=0 || anim->pFrame->data[3]!=0) {
798                                                 
799                                                 sws_scale(anim->img_convert_ctx,
800                                                           anim->pFrame->data,
801                                                           anim->pFrame->linesize,
802                                                           0,
803                                                           anim->pCodecCtx->height,
804                                                           dst2,
805                                                           dstStride2);
806                                         
807                                                 /* workaround: sws_scale 
808                                                    sets alpha = 0... */
809                                         
810                                                 r = (unsigned char*) ibuf->rect;
811         
812                                                 for (i = 0; i < ibuf->x * ibuf->y;i++){
813                                                         r[3] = 0xff;
814                                                         r+=4;
815                                                 }
816                                         }
817                                         av_free_packet(&packet);
818                                         break;
819                                 }
820                         }
821                 }
822
823                 av_free_packet(&packet);
824         }
825
826         return(ibuf);
827 }
828
829 static void free_anim_ffmpeg(struct anim * anim) {
830         if (anim == NULL) return;
831
832         if (anim->pCodecCtx) {
833                 avcodec_close(anim->pCodecCtx);
834                 av_close_input_file(anim->pFormatCtx);
835                 av_free(anim->pFrameRGB);
836                 av_free(anim->pFrame);
837                 sws_freeContext(anim->img_convert_ctx);
838         }
839         anim->duration = 0;
840 }
841
842 #endif
843
844 #ifdef WITH_REDCODE
845
846 static int startredcode(struct anim * anim) {
847         anim->redcodeCtx = redcode_open(anim->name);
848         if (!anim->redcodeCtx) {
849                 return -1;
850         }
851         anim->duration = redcode_get_length(anim->redcodeCtx);
852         
853         return 0;
854 }
855
856 static ImBuf * redcode_fetchibuf(struct anim * anim, int position) {
857         struct ImBuf * ibuf;
858         struct redcode_frame * frame;
859         struct redcode_frame_raw * raw_frame;
860
861         if (!anim->redcodeCtx) {
862                 return NULL;
863         }
864
865         frame = redcode_read_video_frame(anim->redcodeCtx, position);
866         
867         if (!frame) {
868                 return NULL;
869         }
870
871         raw_frame = redcode_decode_video_raw(frame, 1);
872
873         redcode_free_frame(frame);
874
875         if (!raw_frame) {
876                 return NULL;
877         }
878         
879         ibuf = IMB_allocImBuf(raw_frame->width * 2, 
880                               raw_frame->height * 2, 32, IB_rectfloat, 0);
881
882         redcode_decode_video_float(raw_frame, ibuf->rect_float, 1);
883
884         return ibuf;
885 }
886
887 static void free_anim_redcode(struct anim * anim) {
888         if (anim->redcodeCtx) {
889                 redcode_close(anim->redcodeCtx);
890                 anim->redcodeCtx = 0;
891         }
892         anim->duration = 0;
893 }
894
895 #endif
896
897 /* probeer volgende plaatje te lezen */
898 /* Geen plaatje, probeer dan volgende animatie te openen */
899 /* gelukt, haal dan eerste plaatje van animatie */
900
901 static struct ImBuf * anim_getnew(struct anim * anim) {
902         struct ImBuf *ibuf = 0;
903
904         if (anim == NULL) return(0);
905
906         free_anim_anim5(anim);
907         free_anim_movie(anim);
908         free_anim_avi(anim);
909 #ifdef WITH_QUICKTIME
910         free_anim_quicktime(anim);
911 #endif
912 #ifdef WITH_FFMPEG
913         free_anim_ffmpeg(anim);
914 #endif
915 #ifdef WITH_REDCODE
916         free_anim_redcode(anim);
917 #endif
918
919
920         if (anim->curtype != 0) return (0);
921         anim->curtype = imb_get_anim_type(anim->name);  
922
923         switch (anim->curtype) {
924         case ANIM_ANIM5:
925                 if (startanim5(anim)) return (0);
926                 ibuf = anim5_fetchibuf(anim);
927                 break;
928         case ANIM_SEQUENCE:
929                 ibuf = IMB_loadiffname(anim->name, anim->ib_flags);
930                 if (ibuf) {
931                         strcpy(anim->first, anim->name);
932                         anim->duration = 1;
933                 }
934                 break;
935         case ANIM_MOVIE:
936                 if (startmovie(anim)) return (0);
937                 ibuf = IMB_allocImBuf (anim->x, anim->y, 24, 0, 0); /* fake */
938                 break;
939         case ANIM_AVI:
940                 if (startavi(anim)) {
941                         printf("couldnt start avi\n"); 
942                         return (0);
943                 }
944                 ibuf = IMB_allocImBuf (anim->x, anim->y, 24, 0, 0);
945                 break;
946 #ifdef WITH_QUICKTIME
947         case ANIM_QTIME:
948                 if (startquicktime(anim)) return (0);
949                 ibuf = IMB_allocImBuf (anim->x, anim->y, 24, 0, 0);
950                 break;
951 #endif
952 #ifdef WITH_FFMPEG
953         case ANIM_FFMPEG:
954                 if (startffmpeg(anim)) return (0);
955                 ibuf = IMB_allocImBuf (anim->x, anim->y, 24, 0, 0);
956                 break;
957 #endif
958 #ifdef WITH_REDCODE
959         case ANIM_REDCODE:
960                 if (startredcode(anim)) return (0);
961                 ibuf = IMB_allocImBuf (8, 8, 32, 0, 0);
962                 break;
963 #endif
964         }
965         return(ibuf);
966 }
967
968 struct ImBuf * IMB_anim_previewframe(struct anim * anim) {
969         struct ImBuf * ibuf = 0;
970         int position = 0;
971         
972         ibuf = IMB_anim_absolute(anim, 0);
973         if (ibuf) {
974                 IMB_freeImBuf(ibuf);
975                 position = anim->duration / 2;
976                 ibuf = IMB_anim_absolute(anim, position);
977         }
978         return ibuf;
979 }
980
981 struct ImBuf * IMB_anim_absolute(struct anim * anim, int position) {
982         struct ImBuf * ibuf = 0;
983         char head[256], tail[256];
984         unsigned short digits;
985         int pic;
986
987         if (anim == NULL) return(0);
988
989         if (anim->curtype == 0) {
990                 ibuf = anim_getnew(anim);
991                 if (ibuf == NULL) {
992                         return (0);
993                 }
994                 IMB_freeImBuf(ibuf); /* ???? */
995         }
996
997         if (position < 0) return(0);
998         if (position >= anim->duration) return(0);
999
1000         switch(anim->curtype) {
1001         case ANIM_ANIM5:
1002                 if (anim->curposition > position) rewindanim5(anim);
1003                 while (anim->curposition < position) {
1004                         if (nextanim5(anim)) return (0);
1005                 }
1006                 ibuf = anim5_fetchibuf(anim);
1007                 break;
1008         case ANIM_SEQUENCE:
1009                 pic = an_stringdec(anim->first, head, tail, &digits);
1010                 pic += position;
1011                 an_stringenc(anim->name, head, tail, digits, pic);
1012                 ibuf = IMB_loadiffname(anim->name, LI_rect);
1013                 if (ibuf) {
1014                         anim->curposition = position;
1015                         /* patch... by freeing the cmap you prevent a double apply cmap... */
1016                         /* probably the IB_CMAP option isn't working proper
1017                          * after the abgr->rgba reconstruction
1018                          */
1019                         IMB_freecmapImBuf(ibuf);
1020                 }
1021                 break;
1022         case ANIM_MOVIE:
1023                 ibuf = movie_fetchibuf(anim, position);
1024                 if (ibuf) {
1025                         anim->curposition = position;
1026                         IMB_convert_rgba_to_abgr(ibuf);
1027                 }
1028                 break;
1029         case ANIM_AVI:
1030                 ibuf = avi_fetchibuf(anim, position);
1031                 if (ibuf) anim->curposition = position;
1032                 break;
1033 #ifdef WITH_QUICKTIME
1034         case ANIM_QTIME:
1035                 ibuf = qtime_fetchibuf(anim, position);
1036                 if (ibuf) anim->curposition = position;
1037                 break;
1038 #endif
1039 #ifdef WITH_FFMPEG
1040         case ANIM_FFMPEG:
1041                 ibuf = ffmpeg_fetchibuf(anim, position);
1042                 if (ibuf) anim->curposition = position;
1043                 break;
1044 #endif
1045 #ifdef WITH_REDCODE
1046         case ANIM_REDCODE:
1047                 ibuf = redcode_fetchibuf(anim, position);
1048                 if (ibuf) anim->curposition = position;
1049                 break;
1050 #endif
1051         }
1052
1053         if (ibuf) {
1054                 if (anim->ib_flags & IB_ttob) IMB_flipy(ibuf);
1055                 sprintf(ibuf->name, "%s.%04d", anim->name, anim->curposition + 1);
1056                 
1057         }
1058         return(ibuf);
1059 }
1060
1061 struct ImBuf * IMB_anim_nextpic(struct anim * anim) {
1062         struct ImBuf * ibuf = 0;
1063
1064         if (anim == 0) return(0);
1065
1066         ibuf = IMB_anim_absolute(anim, anim->curposition + 1);
1067
1068         return(ibuf);
1069 }
1070
1071 /***/
1072
1073 int IMB_anim_get_duration(struct anim *anim) {
1074         return anim->duration;
1075 }
1076
1077 void IMB_anim_set_preseek(struct anim * anim, int preseek)
1078 {
1079         anim->preseek = preseek;
1080 }
1081
1082 int IMB_anim_get_preseek(struct anim * anim)
1083 {
1084         return anim->preseek;
1085 }