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