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