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
111 #if (LIBAVFORMAT_VERSION_MAJOR > 52) || ((LIBAVFORMAT_VERSION_MAJOR >= 52) && (LIBAVFORMAT_VERSION_MINOR >= 101))
112 #define FFMPEG_HAVE_AV_DUMP_FORMAT 1
115 #ifndef FFMPEG_HAVE_AV_DUMP_FORMAT
116 #define av_dump_format dump_format
122 #ifdef _WIN32 /* on windows we use the ones in extern instead */
123 #include "libredcode/format.h"
124 #include "libredcode/codec.h"
126 #include "libredcode/format.h"
127 #include "libredcode/codec.h"
135 #include <dmedia/moviefile.h>
137 static void movie_printerror(char * str) {
138 const char * errstr = mvGetErrorStr(mvGetErrno());
141 if (errstr) printf("%s: %s\n", str, errstr);
142 else printf("%s: returned error\n", str);
143 } else printf("%s\n", errstr);
146 static int startmovie(struct anim * anim) {
147 if (anim == 0) return(-1);
149 if ( mvOpenFile (anim->name, O_BINARY|O_RDONLY, &anim->movie ) != DM_SUCCESS ) {
150 printf("Can't open movie: %s\n", anim->name);
153 if ( mvFindTrackByMedium (anim->movie, DM_IMAGE, &anim->track) != DM_SUCCESS ) {
154 printf("No image track in movie: %s\n", anim->name);
155 mvClose(anim->movie);
159 anim->duration = mvGetTrackLength (anim->track);
160 anim->params = mvGetParams( anim->track );
162 anim->x = dmParamsGetInt( anim->params, DM_IMAGE_WIDTH);
163 anim->y = dmParamsGetInt( anim->params, DM_IMAGE_HEIGHT);
164 anim->interlacing = dmParamsGetEnum (anim->params, DM_IMAGE_INTERLACING);
165 anim->orientation = dmParamsGetEnum (anim->params, DM_IMAGE_ORIENTATION);
166 anim->framesize = dmImageFrameSize(anim->params);
168 anim->curposition = 0;
171 /*printf("x:%d y:%d size:%d interl:%d dur:%d\n", anim->x, anim->y, anim->framesize, anim->interlacing, anim->duration);*/
175 static ImBuf * movie_fetchibuf(struct anim * anim, int position) {
177 /* extern rectcpy(); */
179 unsigned int *rect1, *rect2;
181 if (anim == 0) return (0);
183 ibuf = IMB_allocImBuf(anim->x, anim->y, 24, IB_rect);
185 if ( mvReadFrames(anim->track, position, 1, ibuf->x * ibuf->y *
186 sizeof(int), ibuf->rect ) != DM_SUCCESS ) {
187 movie_printerror("mvReadFrames");
193 if (anim->interlacing == DM_IMAGE_INTERLACED_EVEN) {
194 rect1 = ibuf->rect + (ibuf->x * ibuf->y) - 1;
195 rect2 = rect1 - ibuf->x;
197 for (size = ibuf->x * (ibuf->y - 1); size > 0; size--){
203 if (anim->interlacing == DM_IMAGE_INTERLACED_EVEN)
206 rect2 = rect1 + ibuf->x;
208 for (size = ibuf->x * (ibuf->y - 1); size > 0; size--){
212 /*if (anim->orientation == DM_TOP_TO_BOTTOM) IMB_flipy(ibuf);*/
218 static void free_anim_movie(struct anim * anim) {
219 if (anim == NULL) return;
222 mvClose(anim->movie);
228 int ismovie(char *name) {
229 return (mvIsMovieFile(name) == DM_TRUE);
234 int ismovie(const char *UNUSED(name)) {
238 /* never called, just keep the linker happy */
239 static int startmovie(struct anim *UNUSED(anim)) { return 1; }
240 static ImBuf * movie_fetchibuf(struct anim *UNUSED(anim), int UNUSED(position)) { return NULL; }
241 static void free_anim_movie(struct anim *UNUSED(anim)) { ; }
246 # define PATHSEPERATOR '\\'
248 # define PATHSEPERATOR '/'
251 static int an_stringdec(const char *string, char* head, char *tail, unsigned short *numlen) {
252 unsigned short len,nume,nums=0;
258 for(i=len-1;i>=0;i--){
259 if (string[i]==PATHSEPERATOR) break;
260 if (isdigit(string[i])) {
273 strcpy(tail ,&string[nume+1]);
274 strcpy(head, string);
277 return ((int)atoi(&(string[nums])));
280 strcpy(head, string);
286 static void an_stringenc(char *string, const char *head, const char *tail, unsigned short numlen, int pic) {
287 BLI_stringenc(string, head, tail, numlen, pic);
290 static void free_anim_avi (struct anim *anim) {
291 #if defined(_WIN32) && !defined(FREE_WINDOWS)
295 if (anim == NULL) return;
296 if (anim->avi == NULL) return;
298 AVI_close (anim->avi);
299 MEM_freeN (anim->avi);
302 #if defined(_WIN32) && !defined(FREE_WINDOWS)
305 AVIStreamGetFrameClose(anim->pgf);
309 for (i = 0; i < anim->avistreams; i++){
310 AVIStreamRelease(anim->pavi[i]);
312 anim->avistreams = 0;
314 if (anim->pfileopen) {
315 AVIFileRelease(anim->pfile);
324 void IMB_free_anim_ibuf(struct anim * anim) {
325 if (anim == NULL) return;
327 if (anim->ibuf1) IMB_freeImBuf(anim->ibuf1);
328 if (anim->ibuf2) IMB_freeImBuf(anim->ibuf2);
330 anim->ibuf1 = anim->ibuf2 = NULL;
334 static void free_anim_ffmpeg(struct anim * anim);
337 static void free_anim_redcode(struct anim * anim);
340 void IMB_free_anim(struct anim * anim) {
342 printf("free anim, anim == NULL\n");
346 IMB_free_anim_ibuf(anim);
347 free_anim_movie(anim);
350 #ifdef WITH_QUICKTIME
351 free_anim_quicktime(anim);
354 free_anim_ffmpeg(anim);
357 free_anim_redcode(anim);
363 void IMB_close_anim(struct anim * anim) {
364 if (anim == NULL) return;
370 struct anim * IMB_open_anim( const char * name, int ib_flags) {
373 anim = (struct anim*)MEM_callocN(sizeof(struct anim), "anim struct");
375 strcpy(anim->name, name); /* fixme: possible buffer overflow here? */
376 anim->ib_flags = ib_flags;
382 static int startavi (struct anim *anim) {
385 #if defined(_WIN32) && !defined(FREE_WINDOWS)
387 int i, firstvideo = -1;
390 LPBITMAPINFOHEADER lpbi;
394 anim->avi = MEM_callocN (sizeof(AviMovie),"animavi");
396 if (anim->avi == NULL) {
397 printf("Can't open avi: %s\n", anim->name);
401 avierror = AVI_open_movie (anim->name, anim->avi);
403 #if defined(_WIN32) && !defined(FREE_WINDOWS)
404 if (avierror == AVI_ERROR_COMPRESSION) {
406 hr = AVIFileOpen(&anim->pfile, anim->name, OF_READ, 0L);
409 for (i = 0; i < MAXNUMSTREAMS; i++) {
410 if (AVIFileGetStream(anim->pfile, &anim->pavi[i], 0L, i) != AVIERR_OK) {
414 AVIStreamInfo(anim->pavi[i], &avis, sizeof(avis));
415 if ((avis.fccType == streamtypeVIDEO) && (firstvideo == -1)) {
416 anim->pgf = AVIStreamGetFrameOpen(anim->pavi[i], NULL);
421 anim->avi->header->TotalFrames = AVIStreamLength(anim->pavi[i]);
423 // get information about images inside the stream
424 l = sizeof(abFormat);
425 AVIStreamReadFormat(anim->pavi[i], 0, &abFormat, &l);
426 lpbi = (LPBITMAPINFOHEADER)abFormat;
427 anim->avi->header->Height = lpbi->biHeight;
428 anim->avi->header->Width = lpbi->biWidth;
430 FIXCC(avis.fccHandler);
432 printf("Can't find AVI decoder for type : %4.4hs/%4.4hs\n",
433 (LPSTR)&avis.fccType,
434 (LPSTR)&avis.fccHandler);
439 // register number of opened avistreams
440 anim->avistreams = i;
443 // Couldn't get any video streams out of this file
445 if ((anim->avistreams == 0) || (firstvideo == -1)) {
446 avierror = AVI_ERROR_FORMAT;
448 avierror = AVI_ERROR_NONE;
449 anim->firstvideo = firstvideo;
457 if (avierror != AVI_ERROR_NONE) {
458 AVI_print_error(avierror);
459 printf ("Error loading avi: %s\n", anim->name);
464 anim->duration = anim->avi->header->TotalFrames;
467 anim->x = anim->avi->header->Width;
468 anim->y = anim->avi->header->Height;
469 anim->interlacing = 0;
470 anim->orientation = 0;
471 anim->framesize = anim->x * anim->y * 4;
473 anim->curposition = 0;
476 /* printf("x:%d y:%d size:%d interl:%d dur:%d\n", anim->x, anim->y, anim->framesize, anim->interlacing, anim->duration);*/
481 static ImBuf * avi_fetchibuf (struct anim *anim, int position) {
486 if (anim == NULL) return (NULL);
488 #if defined(_WIN32) && !defined(FREE_WINDOWS)
489 if (anim->avistreams) {
490 LPBITMAPINFOHEADER lpbi;
493 lpbi = AVIStreamGetFrame(anim->pgf, position + AVIStreamStart(anim->pavi[anim->firstvideo]));
495 ibuf = IMB_ibImageFromMemory((unsigned char *) lpbi, 100, IB_rect);
503 ibuf = IMB_allocImBuf (anim->x, anim->y, 24, IB_rect);
505 tmp = AVI_read_frame (anim->avi, AVI_FORMAT_RGB32, position,
506 AVI_get_stream(anim->avi, AVIST_VIDEO, 0));
509 printf ("Error reading frame from AVI");
510 IMB_freeImBuf (ibuf);
514 for (y=0; y < anim->y; y++) {
515 memcpy (&(ibuf->rect)[((anim->y-y)-1)*anim->x], &tmp[y*anim->x],
522 ibuf->profile = IB_PROFILE_SRGB;
529 extern void do_init_ffmpeg(void);
531 #ifdef FFMPEG_CODEC_IS_POINTER
532 static AVCodecContext* get_codec_from_stream(AVStream* stream)
534 return stream->codec;
537 static AVCodecContext* get_codec_from_stream(AVStream* stream)
539 return &stream->codec;
543 static int startffmpeg(struct anim * anim) {
547 AVFormatContext *pFormatCtx;
548 AVCodecContext *pCodecCtx;
550 #ifdef FFMPEG_SWSCALE_COLOR_SPACE_SUPPORT
551 /* The following for color space determination */
552 int srcRange, dstRange, brightness, contrast, saturation;
554 const int *inv_table;
557 if (anim == 0) return(-1);
561 if(av_open_input_file(&pFormatCtx, anim->name, NULL, 0, NULL)!=0) {
565 if(av_find_stream_info(pFormatCtx)<0) {
566 av_close_input_file(pFormatCtx);
570 av_dump_format(pFormatCtx, 0, anim->name, 0);
573 /* Find the first video stream */
575 for(i=0; i<pFormatCtx->nb_streams; i++)
576 if(get_codec_from_stream(pFormatCtx->streams[i])->codec_type
577 == AVMEDIA_TYPE_VIDEO) {
582 if(videoStream==-1) {
583 av_close_input_file(pFormatCtx);
587 pCodecCtx = get_codec_from_stream(pFormatCtx->streams[videoStream]);
589 /* Find the decoder for the video stream */
590 pCodec=avcodec_find_decoder(pCodecCtx->codec_id);
592 av_close_input_file(pFormatCtx);
596 pCodecCtx->workaround_bugs = 1;
598 if(avcodec_open(pCodecCtx, pCodec)<0) {
599 av_close_input_file(pFormatCtx);
603 #ifdef FFMPEG_OLD_FRAME_RATE
604 if(pCodecCtx->frame_rate>1000 && pCodecCtx->frame_rate_base==1)
605 pCodecCtx->frame_rate_base=1000;
608 anim->duration = pFormatCtx->duration * pCodecCtx->frame_rate
609 / pCodecCtx->frame_rate_base / AV_TIME_BASE;
611 anim->duration = ceil(pFormatCtx->duration
612 * av_q2d(pFormatCtx->streams[videoStream]->r_frame_rate)
618 anim->x = pCodecCtx->width;
619 anim->y = pCodecCtx->height;
620 anim->interlacing = 0;
621 anim->orientation = 0;
622 anim->framesize = anim->x * anim->y * 4;
624 anim->curposition = -1;
626 anim->pFormatCtx = pFormatCtx;
627 anim->pCodecCtx = pCodecCtx;
628 anim->pCodec = pCodec;
629 anim->videoStream = videoStream;
631 anim->pFrame = avcodec_alloc_frame();
632 anim->pFrameDeinterlaced = avcodec_alloc_frame();
633 anim->pFrameRGB = avcodec_alloc_frame();
635 if (avpicture_get_size(PIX_FMT_RGBA, anim->x, anim->y)
636 != anim->x * anim->y * 4) {
638 "ffmpeg has changed alloc scheme ... ARGHHH!\n");
639 avcodec_close(anim->pCodecCtx);
640 av_close_input_file(anim->pFormatCtx);
641 av_free(anim->pFrameRGB);
642 av_free(anim->pFrameDeinterlaced);
643 av_free(anim->pFrame);
644 anim->pCodecCtx = NULL;
648 if (anim->ib_flags & IB_animdeinterlace) {
649 avpicture_fill((AVPicture*) anim->pFrameDeinterlaced,
650 MEM_callocN(avpicture_get_size(
651 anim->pCodecCtx->pix_fmt,
653 "ffmpeg deinterlace"),
654 anim->pCodecCtx->pix_fmt, anim->x, anim->y);
657 if (pCodecCtx->has_b_frames) {
658 anim->preseek = 25; /* FIXME: detect gopsize ... */
663 anim->img_convert_ctx = sws_getContext(
664 anim->pCodecCtx->width,
665 anim->pCodecCtx->height,
666 anim->pCodecCtx->pix_fmt,
667 anim->pCodecCtx->width,
668 anim->pCodecCtx->height,
670 SWS_FAST_BILINEAR | SWS_PRINT_INFO,
673 if (!anim->img_convert_ctx) {
675 "Can't transform color space??? Bailing out...\n");
676 avcodec_close(anim->pCodecCtx);
677 av_close_input_file(anim->pFormatCtx);
678 av_free(anim->pFrameRGB);
679 av_free(anim->pFrameDeinterlaced);
680 av_free(anim->pFrame);
681 anim->pCodecCtx = NULL;
685 #ifdef FFMPEG_SWSCALE_COLOR_SPACE_SUPPORT
686 /* Try do detect if input has 0-255 YCbCR range (JFIF Jpeg MotionJpeg) */
687 if (!sws_getColorspaceDetails(anim->img_convert_ctx, (int**)&inv_table, &srcRange,
688 &table, &dstRange, &brightness, &contrast, &saturation)) {
690 srcRange = srcRange || anim->pCodecCtx->color_range == AVCOL_RANGE_JPEG;
691 inv_table = sws_getCoefficients(anim->pCodecCtx->colorspace);
693 if(sws_setColorspaceDetails(anim->img_convert_ctx, (int *)inv_table, srcRange,
694 table, dstRange, brightness, contrast, saturation)) {
696 printf("Warning: Could not set libswscale colorspace details.\n");
700 printf("Warning: Could not set libswscale colorspace details.\n");
707 static void ffmpeg_postprocess(struct anim * anim, ImBuf * ibuf,
710 AVFrame * input = anim->pFrame;
712 /* This means the data wasnt read properly,
713 this check stops crashing */
714 if (input->data[0]==0 && input->data[1]==0
715 && input->data[2]==0 && input->data[3]==0){
716 fprintf(stderr, "ffmpeg_fetchibuf: "
717 "data not read properly...\n");
721 if (anim->ib_flags & IB_animdeinterlace) {
722 if (avpicture_deinterlace(
724 anim->pFrameDeinterlaced,
727 anim->pCodecCtx->pix_fmt,
728 anim->pCodecCtx->width,
729 anim->pCodecCtx->height)
733 input = anim->pFrameDeinterlaced;
737 if (ENDIAN_ORDER == B_ENDIAN) {
738 int * dstStride = anim->pFrameRGB->linesize;
739 uint8_t** dst = anim->pFrameRGB->data;
740 int dstStride2[4] = { dstStride[0], 0, 0, 0 };
741 uint8_t* dst2[4] = { dst[0], 0, 0, 0 };
743 unsigned char* bottom;
746 sws_scale(anim->img_convert_ctx,
747 (const uint8_t * const *)input->data,
750 anim->pCodecCtx->height,
754 /* workaround: sws_scale bug
755 sets alpha = 0 and compensate
756 for altivec-bugs and flipy... */
758 bottom = (unsigned char*) ibuf->rect;
759 top = bottom + ibuf->x * (ibuf->y-1) * 4;
761 h = (ibuf->y + 1) / 2;
764 for (y = 0; y < h; y++) {
765 unsigned char tmp[4];
766 unsigned int * tmp_l =
770 for (x = 0; x < w; x++) {
780 *(unsigned int*) top = *tmp_l;
788 int * dstStride = anim->pFrameRGB->linesize;
789 uint8_t** dst = anim->pFrameRGB->data;
790 int dstStride2[4] = { -dstStride[0], 0, 0, 0 };
791 uint8_t* dst2[4] = { dst[0] + (anim->y - 1)*dstStride[0],
796 sws_scale(anim->img_convert_ctx,
797 (const uint8_t * const *)input->data,
800 anim->pCodecCtx->height,
804 r = (unsigned char*) ibuf->rect;
806 /* workaround sws_scale bug: older version of
807 sws_scale set alpha = 0... */
809 for (i = 0; i < ibuf->x * ibuf->y; i++) {
817 static ImBuf * ffmpeg_fetchibuf(struct anim * anim, int position) {
821 int64_t pts_to_search = 0;
824 int seek_by_bytes= 0;
825 int preseek_count = 0;
827 if (anim == 0) return (0);
829 ibuf = IMB_allocImBuf(anim->x, anim->y, 32, IB_rect);
831 avpicture_fill((AVPicture*) anim->pFrameRGB,
832 (unsigned char*) ibuf->rect,
833 PIX_FMT_RGBA, anim->x, anim->y);
835 if (position != anim->curposition + 1) {
836 if (position > anim->curposition + 1
838 && position - (anim->curposition + 1) < anim->preseek) {
839 while(av_read_frame(anim->pFormatCtx, &packet)>=0) {
840 if (packet.stream_index == anim->videoStream) {
841 avcodec_decode_video2(
843 anim->pFrame, &frameFinished,
850 av_free_packet(&packet);
851 if (position == anim->curposition+1) {
858 /* disable seek_by_bytes for now, since bitrates are guessed wrong!
859 also: MPEG2TS-seeking was fixed in later versions of ffmpeg, so problem
860 is somewhat fixed by now (until we add correct timecode management code...)
863 seek_by_bytes = !!(anim->pFormatCtx->iformat->flags & AVFMT_TS_DISCONT);
865 seek_by_bytes = FALSE;
868 if (position != anim->curposition + 1) {
869 #ifdef FFMPEG_OLD_FRAME_RATE
871 (double) anim->pCodecCtx->frame_rate
872 / (double) anim->pCodecCtx->frame_rate_base;
875 av_q2d(anim->pFormatCtx->streams[anim->videoStream]
878 double pts_time_base = av_q2d(anim->pFormatCtx->streams[anim->videoStream]->time_base);
880 long long st_time = anim->pFormatCtx->start_time;
884 pos = position - anim->preseek;
888 preseek_count = position - pos;
890 pos *= anim->pFormatCtx->bit_rate / frame_rate;
893 pos = (long long) (position - anim->preseek)
894 * AV_TIME_BASE / frame_rate;
899 if (st_time != AV_NOPTS_VALUE) {
904 ret = av_seek_frame(anim->pFormatCtx, -1,
906 AVSEEK_FLAG_BACKWARD | (
909 | AVSEEK_FLAG_BYTE : 0));
911 fprintf(stderr, "error while seeking: %d\n", ret);
914 pts_to_search = (long long)
915 (((double) position) / pts_time_base / frame_rate);
916 if (st_time != AV_NOPTS_VALUE) {
917 pts_to_search += st_time / pts_time_base/ AV_TIME_BASE;
921 avcodec_flush_buffers(anim->pCodecCtx);
924 while(av_read_frame(anim->pFormatCtx, &packet)>=0) {
925 if(packet.stream_index == anim->videoStream) {
926 avcodec_decode_video2(anim->pCodecCtx,
927 anim->pFrame, &frameFinished,
930 if (seek_by_bytes && preseek_count > 0) {
934 if (frameFinished && !pos_found) {
936 if (!preseek_count) {
938 anim->curposition = position;
941 if (packet.dts >= pts_to_search) {
943 anim->curposition = position;
948 if(frameFinished && pos_found == 1) {
949 ffmpeg_postprocess(anim, ibuf, &filter_y);
950 av_free_packet(&packet);
955 av_free_packet(&packet);
958 if (filter_y && ibuf) {
962 ibuf->profile = IB_PROFILE_SRGB;
967 static void free_anim_ffmpeg(struct anim * anim) {
968 if (anim == NULL) return;
970 if (anim->pCodecCtx) {
971 avcodec_close(anim->pCodecCtx);
972 av_close_input_file(anim->pFormatCtx);
973 av_free(anim->pFrameRGB);
974 av_free(anim->pFrame);
976 if (anim->ib_flags & IB_animdeinterlace) {
977 MEM_freeN(anim->pFrameDeinterlaced->data[0]);
979 av_free(anim->pFrameDeinterlaced);
980 sws_freeContext(anim->img_convert_ctx);
989 static int startredcode(struct anim * anim) {
990 anim->redcodeCtx = redcode_open(anim->name);
991 if (!anim->redcodeCtx) {
994 anim->duration = redcode_get_length(anim->redcodeCtx);
999 static ImBuf * redcode_fetchibuf(struct anim * anim, int position) {
1000 struct ImBuf * ibuf;
1001 struct redcode_frame * frame;
1002 struct redcode_frame_raw * raw_frame;
1004 if (!anim->redcodeCtx) {
1008 frame = redcode_read_video_frame(anim->redcodeCtx, position);
1014 raw_frame = redcode_decode_video_raw(frame, 1);
1016 redcode_free_frame(frame);
1022 ibuf = IMB_allocImBuf(raw_frame->width * 2,
1023 raw_frame->height * 2, 32, IB_rectfloat);
1025 redcode_decode_video_float(raw_frame, ibuf->rect_float, 1);
1030 static void free_anim_redcode(struct anim * anim) {
1031 if (anim->redcodeCtx) {
1032 redcode_close(anim->redcodeCtx);
1033 anim->redcodeCtx = 0;
1040 /* probeer volgende plaatje te lezen */
1041 /* Geen plaatje, probeer dan volgende animatie te openen */
1042 /* gelukt, haal dan eerste plaatje van animatie */
1044 static struct ImBuf * anim_getnew(struct anim * anim) {
1045 struct ImBuf *ibuf = NULL;
1047 if (anim == NULL) return(NULL);
1049 free_anim_movie(anim);
1050 free_anim_avi(anim);
1051 #ifdef WITH_QUICKTIME
1052 free_anim_quicktime(anim);
1055 free_anim_ffmpeg(anim);
1058 free_anim_redcode(anim);
1062 if (anim->curtype != 0) return (NULL);
1063 anim->curtype = imb_get_anim_type(anim->name);
1065 switch (anim->curtype) {
1067 ibuf = IMB_loadiffname(anim->name, anim->ib_flags);
1069 strcpy(anim->first, anim->name);
1074 if (startmovie(anim)) return (NULL);
1075 ibuf = IMB_allocImBuf (anim->x, anim->y, 24, 0); /* fake */
1078 if (startavi(anim)) {
1079 printf("couldnt start avi\n");
1082 ibuf = IMB_allocImBuf (anim->x, anim->y, 24, 0);
1084 #ifdef WITH_QUICKTIME
1086 if (startquicktime(anim)) return (0);
1087 ibuf = IMB_allocImBuf (anim->x, anim->y, 24, 0);
1092 if (startffmpeg(anim)) return (0);
1093 ibuf = IMB_allocImBuf (anim->x, anim->y, 24, 0);
1098 if (startredcode(anim)) return (0);
1099 ibuf = IMB_allocImBuf (8, 8, 32, 0);
1106 struct ImBuf * IMB_anim_previewframe(struct anim * anim) {
1107 struct ImBuf * ibuf = NULL;
1110 ibuf = IMB_anim_absolute(anim, 0);
1112 IMB_freeImBuf(ibuf);
1113 position = anim->duration / 2;
1114 ibuf = IMB_anim_absolute(anim, position);
1119 struct ImBuf * IMB_anim_absolute(struct anim * anim, int position) {
1120 struct ImBuf * ibuf = NULL;
1121 char head[256], tail[256];
1122 unsigned short digits;
1125 if (anim == NULL) return(NULL);
1127 filter_y = (anim->ib_flags & IB_animdeinterlace);
1129 if (anim->curtype == 0) {
1130 ibuf = anim_getnew(anim);
1135 IMB_freeImBuf(ibuf); /* ???? */
1139 if (position < 0) return(NULL);
1140 if (position >= anim->duration) return(NULL);
1142 switch(anim->curtype) {
1144 pic = an_stringdec(anim->first, head, tail, &digits);
1146 an_stringenc(anim->name, head, tail, digits, pic);
1147 ibuf = IMB_loadiffname(anim->name, IB_rect);
1149 anim->curposition = position;
1153 ibuf = movie_fetchibuf(anim, position);
1155 anim->curposition = position;
1156 IMB_convert_rgba_to_abgr(ibuf);
1157 ibuf->profile = IB_PROFILE_SRGB;
1161 ibuf = avi_fetchibuf(anim, position);
1163 anim->curposition = position;
1165 #ifdef WITH_QUICKTIME
1167 ibuf = qtime_fetchibuf(anim, position);
1169 anim->curposition = position;
1174 ibuf = ffmpeg_fetchibuf(anim, position);
1176 anim->curposition = position;
1177 filter_y = 0; /* done internally */
1182 ibuf = redcode_fetchibuf(anim, position);
1183 if (ibuf) anim->curposition = position;
1189 if (filter_y) IMB_filtery(ibuf);
1190 sprintf(ibuf->name, "%s.%04d", anim->name, anim->curposition + 1);
1198 int IMB_anim_get_duration(struct anim *anim) {
1199 return anim->duration;
1202 void IMB_anim_set_preseek(struct anim * anim, int preseek)
1204 anim->preseek = preseek;
1207 int IMB_anim_get_preseek(struct anim * anim)
1209 return anim->preseek;