6 * ***** BEGIN GPL LICENSE BLOCK *****
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.
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.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
23 * All rights reserved.
25 * The Original Code is: all of this file.
27 * Contributor(s): none yet.
29 * ***** END GPL LICENSE BLOCK *****
32 /** \file blender/imbuf/intern/anim_movie.c
49 #undef AVIIF_KEYFRAME // redefined in AVI_avi.h
50 #undef AVIIF_LIST // redefined in AVI_avi.h
52 #define FIXCC(fcc) if (fcc == 0) fcc = mmioFOURCC('N', 'o', 'n', 'e'); \
53 if (fcc == BI_RLE8) fcc = mmioFOURCC('R', 'l', 'e', '8');
56 #include <sys/types.h>
66 #include "BLI_blenlib.h" /* BLI_remlink BLI_filesize BLI_addtail
67 BLI_countlist BLI_stringdec */
68 #include "BLI_utildefines.h"
70 #include "MEM_guardedalloc.h"
72 #include "DNA_userdef_types.h"
75 #include "BKE_global.h"
76 #include "BKE_depsgraph.h"
83 #if defined(_WIN32) || defined(__APPLE__)
84 #include "quicktime_import.h"
85 #endif /* _WIN32 || __APPLE__ */
86 #endif /* WITH_QUICKTIME */
88 #include "IMB_imbuf_types.h"
89 #include "IMB_imbuf.h"
91 #include "IMB_allocimbuf.h"
95 #include <libavformat/avformat.h>
96 #include <libavcodec/avcodec.h>
97 #include <libavutil/rational.h>
98 #include <libswscale/swscale.h>
100 #if LIBAVFORMAT_VERSION_INT < (49 << 16)
101 #define FFMPEG_OLD_FRAME_RATE 1
103 #define FFMPEG_CODEC_IS_POINTER 1
106 #if (LIBAVCODEC_VERSION_MAJOR >= 52) && (LIBAVCODEC_VERSION_MINOR >= 29) && \
107 (LIBSWSCALE_VERSION_MAJOR >= 0) && (LIBSWSCALE_VERSION_MINOR >= 10)
108 #define FFMPEG_SWSCALE_COLOR_SPACE_SUPPORT
114 #ifdef _WIN32 /* on windows we use the ones in extern instead */
115 #include "libredcode/format.h"
116 #include "libredcode/codec.h"
118 #include "libredcode/format.h"
119 #include "libredcode/codec.h"
127 #include <dmedia/moviefile.h>
129 static void movie_printerror(char * str) {
130 const char * errstr = mvGetErrorStr(mvGetErrno());
133 if (errstr) printf("%s: %s\n", str, errstr);
134 else printf("%s: returned error\n", str);
135 } else printf("%s\n", errstr);
138 static int startmovie(struct anim * anim) {
139 if (anim == 0) return(-1);
141 if ( mvOpenFile (anim->name, O_BINARY|O_RDONLY, &anim->movie ) != DM_SUCCESS ) {
142 printf("Can't open movie: %s\n", anim->name);
145 if ( mvFindTrackByMedium (anim->movie, DM_IMAGE, &anim->track) != DM_SUCCESS ) {
146 printf("No image track in movie: %s\n", anim->name);
147 mvClose(anim->movie);
151 anim->duration = mvGetTrackLength (anim->track);
152 anim->params = mvGetParams( anim->track );
154 anim->x = dmParamsGetInt( anim->params, DM_IMAGE_WIDTH);
155 anim->y = dmParamsGetInt( anim->params, DM_IMAGE_HEIGHT);
156 anim->interlacing = dmParamsGetEnum (anim->params, DM_IMAGE_INTERLACING);
157 anim->orientation = dmParamsGetEnum (anim->params, DM_IMAGE_ORIENTATION);
158 anim->framesize = dmImageFrameSize(anim->params);
160 anim->curposition = 0;
163 /*printf("x:%d y:%d size:%d interl:%d dur:%d\n", anim->x, anim->y, anim->framesize, anim->interlacing, anim->duration);*/
167 static ImBuf * movie_fetchibuf(struct anim * anim, int position) {
169 /* extern rectcpy(); */
171 unsigned int *rect1, *rect2;
173 if (anim == 0) return (0);
175 ibuf = IMB_allocImBuf(anim->x, anim->y, 24, IB_rect);
177 if ( mvReadFrames(anim->track, position, 1, ibuf->x * ibuf->y *
178 sizeof(int), ibuf->rect ) != DM_SUCCESS ) {
179 movie_printerror("mvReadFrames");
185 if (anim->interlacing == DM_IMAGE_INTERLACED_EVEN) {
186 rect1 = ibuf->rect + (ibuf->x * ibuf->y) - 1;
187 rect2 = rect1 - ibuf->x;
189 for (size = ibuf->x * (ibuf->y - 1); size > 0; size--){
195 if (anim->interlacing == DM_IMAGE_INTERLACED_EVEN)
198 rect2 = rect1 + ibuf->x;
200 for (size = ibuf->x * (ibuf->y - 1); size > 0; size--){
204 /*if (anim->orientation == DM_TOP_TO_BOTTOM) IMB_flipy(ibuf);*/
210 static void free_anim_movie(struct anim * anim) {
211 if (anim == NULL) return;
214 mvClose(anim->movie);
220 int ismovie(char *name) {
221 return (mvIsMovieFile(name) == DM_TRUE);
226 int ismovie(const char *UNUSED(name)) {
230 /* never called, just keep the linker happy */
231 static int startmovie(struct anim *UNUSED(anim)) { return 1; }
232 static ImBuf * movie_fetchibuf(struct anim *UNUSED(anim), int UNUSED(position)) { return NULL; }
233 static void free_anim_movie(struct anim *UNUSED(anim)) { ; }
238 # define PATHSEPERATOR '\\'
240 # define PATHSEPERATOR '/'
243 static int an_stringdec(const char *string, char* head, char *tail, unsigned short *numlen) {
244 unsigned short len,nume,nums=0;
250 for(i=len-1;i>=0;i--){
251 if (string[i]==PATHSEPERATOR) break;
252 if (isdigit(string[i])) {
265 strcpy(tail ,&string[nume+1]);
266 strcpy(head, string);
269 return ((int)atoi(&(string[nums])));
272 strcpy(head, string);
278 static void an_stringenc(char *string, const char *head, const char *tail, unsigned short numlen, int pic) {
279 BLI_stringenc(string, head, tail, numlen, pic);
282 static void free_anim_avi (struct anim *anim) {
283 #if defined(_WIN32) && !defined(FREE_WINDOWS)
287 if (anim == NULL) return;
288 if (anim->avi == NULL) return;
290 AVI_close (anim->avi);
291 MEM_freeN (anim->avi);
294 #if defined(_WIN32) && !defined(FREE_WINDOWS)
297 AVIStreamGetFrameClose(anim->pgf);
301 for (i = 0; i < anim->avistreams; i++){
302 AVIStreamRelease(anim->pavi[i]);
304 anim->avistreams = 0;
306 if (anim->pfileopen) {
307 AVIFileRelease(anim->pfile);
316 void IMB_free_anim_ibuf(struct anim * anim) {
317 if (anim == NULL) return;
319 if (anim->ibuf1) IMB_freeImBuf(anim->ibuf1);
320 if (anim->ibuf2) IMB_freeImBuf(anim->ibuf2);
322 anim->ibuf1 = anim->ibuf2 = NULL;
326 static void free_anim_ffmpeg(struct anim * anim);
329 static void free_anim_redcode(struct anim * anim);
332 void IMB_free_anim(struct anim * anim) {
334 printf("free anim, anim == NULL\n");
338 IMB_free_anim_ibuf(anim);
339 free_anim_movie(anim);
342 #ifdef WITH_QUICKTIME
343 free_anim_quicktime(anim);
346 free_anim_ffmpeg(anim);
349 free_anim_redcode(anim);
355 void IMB_close_anim(struct anim * anim) {
356 if (anim == NULL) return;
362 struct anim * IMB_open_anim( const char * name, int ib_flags) {
365 anim = (struct anim*)MEM_callocN(sizeof(struct anim), "anim struct");
367 strcpy(anim->name, name); /* fixme: possible buffer overflow here? */
368 anim->ib_flags = ib_flags;
374 static int startavi (struct anim *anim) {
377 #if defined(_WIN32) && !defined(FREE_WINDOWS)
379 int i, firstvideo = -1;
382 LPBITMAPINFOHEADER lpbi;
386 anim->avi = MEM_callocN (sizeof(AviMovie),"animavi");
388 if (anim->avi == NULL) {
389 printf("Can't open avi: %s\n", anim->name);
393 avierror = AVI_open_movie (anim->name, anim->avi);
395 #if defined(_WIN32) && !defined(FREE_WINDOWS)
396 if (avierror == AVI_ERROR_COMPRESSION) {
398 hr = AVIFileOpen(&anim->pfile, anim->name, OF_READ, 0L);
401 for (i = 0; i < MAXNUMSTREAMS; i++) {
402 if (AVIFileGetStream(anim->pfile, &anim->pavi[i], 0L, i) != AVIERR_OK) {
406 AVIStreamInfo(anim->pavi[i], &avis, sizeof(avis));
407 if ((avis.fccType == streamtypeVIDEO) && (firstvideo == -1)) {
408 anim->pgf = AVIStreamGetFrameOpen(anim->pavi[i], NULL);
413 anim->avi->header->TotalFrames = AVIStreamLength(anim->pavi[i]);
415 // get information about images inside the stream
416 l = sizeof(abFormat);
417 AVIStreamReadFormat(anim->pavi[i], 0, &abFormat, &l);
418 lpbi = (LPBITMAPINFOHEADER)abFormat;
419 anim->avi->header->Height = lpbi->biHeight;
420 anim->avi->header->Width = lpbi->biWidth;
422 FIXCC(avis.fccHandler);
424 printf("Can't find AVI decoder for type : %4.4hs/%4.4hs\n",
425 (LPSTR)&avis.fccType,
426 (LPSTR)&avis.fccHandler);
431 // register number of opened avistreams
432 anim->avistreams = i;
435 // Couldn't get any video streams out of this file
437 if ((anim->avistreams == 0) || (firstvideo == -1)) {
438 avierror = AVI_ERROR_FORMAT;
440 avierror = AVI_ERROR_NONE;
441 anim->firstvideo = firstvideo;
449 if (avierror != AVI_ERROR_NONE) {
450 AVI_print_error(avierror);
451 printf ("Error loading avi: %s\n", anim->name);
456 anim->duration = anim->avi->header->TotalFrames;
459 anim->x = anim->avi->header->Width;
460 anim->y = anim->avi->header->Height;
461 anim->interlacing = 0;
462 anim->orientation = 0;
463 anim->framesize = anim->x * anim->y * 4;
465 anim->curposition = 0;
468 /* printf("x:%d y:%d size:%d interl:%d dur:%d\n", anim->x, anim->y, anim->framesize, anim->interlacing, anim->duration);*/
473 static ImBuf * avi_fetchibuf (struct anim *anim, int position) {
478 if (anim == NULL) return (NULL);
480 #if defined(_WIN32) && !defined(FREE_WINDOWS)
481 if (anim->avistreams) {
482 LPBITMAPINFOHEADER lpbi;
485 lpbi = AVIStreamGetFrame(anim->pgf, position + AVIStreamStart(anim->pavi[anim->firstvideo]));
487 ibuf = IMB_ibImageFromMemory((unsigned char *) lpbi, 100, IB_rect);
495 ibuf = IMB_allocImBuf (anim->x, anim->y, 24, IB_rect);
497 tmp = AVI_read_frame (anim->avi, AVI_FORMAT_RGB32, position,
498 AVI_get_stream(anim->avi, AVIST_VIDEO, 0));
501 printf ("Error reading frame from AVI");
502 IMB_freeImBuf (ibuf);
506 for (y=0; y < anim->y; y++) {
507 memcpy (&(ibuf->rect)[((anim->y-y)-1)*anim->x], &tmp[y*anim->x],
514 ibuf->profile = IB_PROFILE_SRGB;
521 extern void do_init_ffmpeg(void);
523 #ifdef FFMPEG_CODEC_IS_POINTER
524 static AVCodecContext* get_codec_from_stream(AVStream* stream)
526 return stream->codec;
529 static AVCodecContext* get_codec_from_stream(AVStream* stream)
531 return &stream->codec;
535 static int startffmpeg(struct anim * anim) {
539 AVFormatContext *pFormatCtx;
540 AVCodecContext *pCodecCtx;
542 #ifdef FFMPEG_SWSCALE_COLOR_SPACE_SUPPORT
543 /* The following for color space determination */
544 int srcRange, dstRange, brightness, contrast, saturation;
546 const int *inv_table;
549 if (anim == 0) return(-1);
553 if(av_open_input_file(&pFormatCtx, anim->name, NULL, 0, NULL)!=0) {
557 if(av_find_stream_info(pFormatCtx)<0) {
558 av_close_input_file(pFormatCtx);
562 dump_format(pFormatCtx, 0, anim->name, 0);
565 /* Find the first video stream */
567 for(i=0; i<pFormatCtx->nb_streams; i++)
568 if(get_codec_from_stream(pFormatCtx->streams[i])->codec_type
569 == CODEC_TYPE_VIDEO) {
574 if(videoStream==-1) {
575 av_close_input_file(pFormatCtx);
579 pCodecCtx = get_codec_from_stream(pFormatCtx->streams[videoStream]);
581 /* Find the decoder for the video stream */
582 pCodec=avcodec_find_decoder(pCodecCtx->codec_id);
584 av_close_input_file(pFormatCtx);
588 pCodecCtx->workaround_bugs = 1;
590 if(avcodec_open(pCodecCtx, pCodec)<0) {
591 av_close_input_file(pFormatCtx);
595 #ifdef FFMPEG_OLD_FRAME_RATE
596 if(pCodecCtx->frame_rate>1000 && pCodecCtx->frame_rate_base==1)
597 pCodecCtx->frame_rate_base=1000;
600 anim->duration = pFormatCtx->duration * pCodecCtx->frame_rate
601 / pCodecCtx->frame_rate_base / AV_TIME_BASE;
603 anim->duration = ceil(pFormatCtx->duration
604 * av_q2d(pFormatCtx->streams[videoStream]->r_frame_rate)
610 anim->x = pCodecCtx->width;
611 anim->y = pCodecCtx->height;
612 anim->interlacing = 0;
613 anim->orientation = 0;
614 anim->framesize = anim->x * anim->y * 4;
616 anim->curposition = -1;
618 anim->pFormatCtx = pFormatCtx;
619 anim->pCodecCtx = pCodecCtx;
620 anim->pCodec = pCodec;
621 anim->videoStream = videoStream;
623 anim->pFrame = avcodec_alloc_frame();
624 anim->pFrameDeinterlaced = avcodec_alloc_frame();
625 anim->pFrameRGB = avcodec_alloc_frame();
627 if (avpicture_get_size(PIX_FMT_RGBA, anim->x, anim->y)
628 != anim->x * anim->y * 4) {
630 "ffmpeg has changed alloc scheme ... ARGHHH!\n");
631 avcodec_close(anim->pCodecCtx);
632 av_close_input_file(anim->pFormatCtx);
633 av_free(anim->pFrameRGB);
634 av_free(anim->pFrameDeinterlaced);
635 av_free(anim->pFrame);
636 anim->pCodecCtx = NULL;
640 if (anim->ib_flags & IB_animdeinterlace) {
641 avpicture_fill((AVPicture*) anim->pFrameDeinterlaced,
642 MEM_callocN(avpicture_get_size(
643 anim->pCodecCtx->pix_fmt,
645 "ffmpeg deinterlace"),
646 anim->pCodecCtx->pix_fmt, anim->x, anim->y);
649 if (pCodecCtx->has_b_frames) {
650 anim->preseek = 25; /* FIXME: detect gopsize ... */
655 anim->img_convert_ctx = sws_getContext(
656 anim->pCodecCtx->width,
657 anim->pCodecCtx->height,
658 anim->pCodecCtx->pix_fmt,
659 anim->pCodecCtx->width,
660 anim->pCodecCtx->height,
662 SWS_FAST_BILINEAR | SWS_PRINT_INFO,
665 if (!anim->img_convert_ctx) {
667 "Can't transform color space??? Bailing out...\n");
668 avcodec_close(anim->pCodecCtx);
669 av_close_input_file(anim->pFormatCtx);
670 av_free(anim->pFrameRGB);
671 av_free(anim->pFrameDeinterlaced);
672 av_free(anim->pFrame);
673 anim->pCodecCtx = NULL;
677 #ifdef FFMPEG_SWSCALE_COLOR_SPACE_SUPPORT
678 /* Try do detect if input has 0-255 YCbCR range (JFIF Jpeg MotionJpeg) */
679 if (!sws_getColorspaceDetails(anim->img_convert_ctx, (int**)&inv_table, &srcRange,
680 &table, &dstRange, &brightness, &contrast, &saturation)) {
682 srcRange = srcRange || anim->pCodecCtx->color_range == AVCOL_RANGE_JPEG;
683 inv_table = sws_getCoefficients(anim->pCodecCtx->colorspace);
685 if(sws_setColorspaceDetails(anim->img_convert_ctx, (int *)inv_table, srcRange,
686 table, dstRange, brightness, contrast, saturation)) {
688 printf("Warning: Could not set libswscale colorspace details.\n");
692 printf("Warning: Could not set libswscale colorspace details.\n");
699 static void ffmpeg_postprocess(struct anim * anim, ImBuf * ibuf,
702 AVFrame * input = anim->pFrame;
704 /* This means the data wasnt read properly,
705 this check stops crashing */
706 if (input->data[0]==0 && input->data[1]==0
707 && input->data[2]==0 && input->data[3]==0){
708 fprintf(stderr, "ffmpeg_fetchibuf: "
709 "data not read properly...\n");
713 if (anim->ib_flags & IB_animdeinterlace) {
714 if (avpicture_deinterlace(
716 anim->pFrameDeinterlaced,
719 anim->pCodecCtx->pix_fmt,
720 anim->pCodecCtx->width,
721 anim->pCodecCtx->height)
725 input = anim->pFrameDeinterlaced;
729 if (ENDIAN_ORDER == B_ENDIAN) {
730 int * dstStride = anim->pFrameRGB->linesize;
731 uint8_t** dst = anim->pFrameRGB->data;
732 int dstStride2[4] = { dstStride[0], 0, 0, 0 };
733 uint8_t* dst2[4] = { dst[0], 0, 0, 0 };
735 unsigned char* bottom;
738 sws_scale(anim->img_convert_ctx,
739 (const uint8_t * const *)input->data,
742 anim->pCodecCtx->height,
746 /* workaround: sws_scale bug
747 sets alpha = 0 and compensate
748 for altivec-bugs and flipy... */
750 bottom = (unsigned char*) ibuf->rect;
751 top = bottom + ibuf->x * (ibuf->y-1) * 4;
753 h = (ibuf->y + 1) / 2;
756 for (y = 0; y < h; y++) {
757 unsigned char tmp[4];
758 unsigned int * tmp_l =
762 for (x = 0; x < w; x++) {
772 *(unsigned int*) top = *tmp_l;
780 int * dstStride = anim->pFrameRGB->linesize;
781 uint8_t** dst = anim->pFrameRGB->data;
782 int dstStride2[4] = { -dstStride[0], 0, 0, 0 };
783 uint8_t* dst2[4] = { dst[0] + (anim->y - 1)*dstStride[0],
788 sws_scale(anim->img_convert_ctx,
789 (const uint8_t * const *)input->data,
792 anim->pCodecCtx->height,
796 r = (unsigned char*) ibuf->rect;
798 /* workaround sws_scale bug: older version of
799 sws_scale set alpha = 0... */
801 for (i = 0; i < ibuf->x * ibuf->y; i++) {
809 static ImBuf * ffmpeg_fetchibuf(struct anim * anim, int position) {
813 int64_t pts_to_search = 0;
816 int seek_by_bytes= 0;
817 int preseek_count = 0;
819 if (anim == 0) return (0);
821 ibuf = IMB_allocImBuf(anim->x, anim->y, 32, IB_rect);
823 avpicture_fill((AVPicture*) anim->pFrameRGB,
824 (unsigned char*) ibuf->rect,
825 PIX_FMT_RGBA, anim->x, anim->y);
827 if (position != anim->curposition + 1) {
828 if (position > anim->curposition + 1
830 && position - (anim->curposition + 1) < anim->preseek) {
831 while(av_read_frame(anim->pFormatCtx, &packet)>=0) {
832 if (packet.stream_index == anim->videoStream) {
833 avcodec_decode_video(
835 anim->pFrame, &frameFinished,
836 packet.data, packet.size);
842 av_free_packet(&packet);
843 if (position == anim->curposition+1) {
850 /* disable seek_by_bytes for now, since bitrates are guessed wrong!
851 also: MPEG2TS-seeking was fixed in later versions of ffmpeg, so problem
852 is somewhat fixed by now (until we add correct timecode management code...)
855 seek_by_bytes = !!(anim->pFormatCtx->iformat->flags & AVFMT_TS_DISCONT);
857 seek_by_bytes = FALSE;
860 if (position != anim->curposition + 1) {
861 #ifdef FFMPEG_OLD_FRAME_RATE
863 (double) anim->pCodecCtx->frame_rate
864 / (double) anim->pCodecCtx->frame_rate_base;
867 av_q2d(anim->pFormatCtx->streams[anim->videoStream]
870 double pts_time_base = av_q2d(anim->pFormatCtx->streams[anim->videoStream]->time_base);
872 long long st_time = anim->pFormatCtx->start_time;
876 pos = position - anim->preseek;
880 preseek_count = position - pos;
882 pos *= anim->pFormatCtx->bit_rate / frame_rate;
885 pos = (long long) (position - anim->preseek)
886 * AV_TIME_BASE / frame_rate;
891 if (st_time != AV_NOPTS_VALUE) {
896 ret = av_seek_frame(anim->pFormatCtx, -1,
898 AVSEEK_FLAG_BACKWARD | (
901 | AVSEEK_FLAG_BYTE : 0));
903 fprintf(stderr, "error while seeking: %d\n", ret);
906 pts_to_search = (long long)
907 (((double) position) / pts_time_base / frame_rate);
908 if (st_time != AV_NOPTS_VALUE) {
909 pts_to_search += st_time / pts_time_base/ AV_TIME_BASE;
913 avcodec_flush_buffers(anim->pCodecCtx);
916 while(av_read_frame(anim->pFormatCtx, &packet)>=0) {
917 if(packet.stream_index == anim->videoStream) {
918 avcodec_decode_video(anim->pCodecCtx,
919 anim->pFrame, &frameFinished,
920 packet.data, packet.size);
922 if (seek_by_bytes && preseek_count > 0) {
926 if (frameFinished && !pos_found) {
928 if (!preseek_count) {
930 anim->curposition = position;
933 if (packet.dts >= pts_to_search) {
935 anim->curposition = position;
940 if(frameFinished && pos_found == 1) {
941 ffmpeg_postprocess(anim, ibuf, &filter_y);
942 av_free_packet(&packet);
947 av_free_packet(&packet);
950 if (filter_y && ibuf) {
954 ibuf->profile = IB_PROFILE_SRGB;
959 static void free_anim_ffmpeg(struct anim * anim) {
960 if (anim == NULL) return;
962 if (anim->pCodecCtx) {
963 avcodec_close(anim->pCodecCtx);
964 av_close_input_file(anim->pFormatCtx);
965 av_free(anim->pFrameRGB);
966 av_free(anim->pFrame);
968 if (anim->ib_flags & IB_animdeinterlace) {
969 MEM_freeN(anim->pFrameDeinterlaced->data[0]);
971 av_free(anim->pFrameDeinterlaced);
972 sws_freeContext(anim->img_convert_ctx);
981 static int startredcode(struct anim * anim) {
982 anim->redcodeCtx = redcode_open(anim->name);
983 if (!anim->redcodeCtx) {
986 anim->duration = redcode_get_length(anim->redcodeCtx);
991 static ImBuf * redcode_fetchibuf(struct anim * anim, int position) {
993 struct redcode_frame * frame;
994 struct redcode_frame_raw * raw_frame;
996 if (!anim->redcodeCtx) {
1000 frame = redcode_read_video_frame(anim->redcodeCtx, position);
1006 raw_frame = redcode_decode_video_raw(frame, 1);
1008 redcode_free_frame(frame);
1014 ibuf = IMB_allocImBuf(raw_frame->width * 2,
1015 raw_frame->height * 2, 32, IB_rectfloat);
1017 redcode_decode_video_float(raw_frame, ibuf->rect_float, 1);
1022 static void free_anim_redcode(struct anim * anim) {
1023 if (anim->redcodeCtx) {
1024 redcode_close(anim->redcodeCtx);
1025 anim->redcodeCtx = 0;
1032 /* probeer volgende plaatje te lezen */
1033 /* Geen plaatje, probeer dan volgende animatie te openen */
1034 /* gelukt, haal dan eerste plaatje van animatie */
1036 static struct ImBuf * anim_getnew(struct anim * anim) {
1037 struct ImBuf *ibuf = NULL;
1039 if (anim == NULL) return(NULL);
1041 free_anim_movie(anim);
1042 free_anim_avi(anim);
1043 #ifdef WITH_QUICKTIME
1044 free_anim_quicktime(anim);
1047 free_anim_ffmpeg(anim);
1050 free_anim_redcode(anim);
1054 if (anim->curtype != 0) return (NULL);
1055 anim->curtype = imb_get_anim_type(anim->name);
1057 switch (anim->curtype) {
1059 ibuf = IMB_loadiffname(anim->name, anim->ib_flags);
1061 strcpy(anim->first, anim->name);
1066 if (startmovie(anim)) return (NULL);
1067 ibuf = IMB_allocImBuf (anim->x, anim->y, 24, 0); /* fake */
1070 if (startavi(anim)) {
1071 printf("couldnt start avi\n");
1074 ibuf = IMB_allocImBuf (anim->x, anim->y, 24, 0);
1076 #ifdef WITH_QUICKTIME
1078 if (startquicktime(anim)) return (0);
1079 ibuf = IMB_allocImBuf (anim->x, anim->y, 24, 0);
1084 if (startffmpeg(anim)) return (0);
1085 ibuf = IMB_allocImBuf (anim->x, anim->y, 24, 0);
1090 if (startredcode(anim)) return (0);
1091 ibuf = IMB_allocImBuf (8, 8, 32, 0);
1098 struct ImBuf * IMB_anim_previewframe(struct anim * anim) {
1099 struct ImBuf * ibuf = NULL;
1102 ibuf = IMB_anim_absolute(anim, 0);
1104 IMB_freeImBuf(ibuf);
1105 position = anim->duration / 2;
1106 ibuf = IMB_anim_absolute(anim, position);
1111 struct ImBuf * IMB_anim_absolute(struct anim * anim, int position) {
1112 struct ImBuf * ibuf = NULL;
1113 char head[256], tail[256];
1114 unsigned short digits;
1117 if (anim == NULL) return(NULL);
1119 filter_y = (anim->ib_flags & IB_animdeinterlace);
1121 if (anim->curtype == 0) {
1122 ibuf = anim_getnew(anim);
1127 IMB_freeImBuf(ibuf); /* ???? */
1131 if (position < 0) return(NULL);
1132 if (position >= anim->duration) return(NULL);
1134 switch(anim->curtype) {
1136 pic = an_stringdec(anim->first, head, tail, &digits);
1138 an_stringenc(anim->name, head, tail, digits, pic);
1139 ibuf = IMB_loadiffname(anim->name, IB_rect);
1141 anim->curposition = position;
1145 ibuf = movie_fetchibuf(anim, position);
1147 anim->curposition = position;
1148 IMB_convert_rgba_to_abgr(ibuf);
1149 ibuf->profile = IB_PROFILE_SRGB;
1153 ibuf = avi_fetchibuf(anim, position);
1155 anim->curposition = position;
1157 #ifdef WITH_QUICKTIME
1159 ibuf = qtime_fetchibuf(anim, position);
1161 anim->curposition = position;
1166 ibuf = ffmpeg_fetchibuf(anim, position);
1168 anim->curposition = position;
1169 filter_y = 0; /* done internally */
1174 ibuf = redcode_fetchibuf(anim, position);
1175 if (ibuf) anim->curposition = position;
1181 if (filter_y) IMB_filtery(ibuf);
1182 sprintf(ibuf->name, "%s.%04d", anim->name, anim->curposition + 1);
1190 int IMB_anim_get_duration(struct anim *anim) {
1191 return anim->duration;
1194 void IMB_anim_set_preseek(struct anim * anim, int preseek)
1196 anim->preseek = preseek;
1199 int IMB_anim_get_preseek(struct anim * anim)
1201 return anim->preseek;