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 *****
44 #undef AVIIF_KEYFRAME // redefined in AVI_avi.h
45 #undef AVIIF_LIST // redefined in AVI_avi.h
47 #define FIXCC(fcc) if (fcc == 0) fcc = mmioFOURCC('N', 'o', 'n', 'e'); \
48 if (fcc == BI_RLE8) fcc = mmioFOURCC('R', 'l', 'e', '8');
51 #include <sys/types.h>
61 #include "BLI_blenlib.h" /* BLI_remlink BLI_filesize BLI_addtail
62 BLI_countlist BLI_stringdec */
63 #include "MEM_guardedalloc.h"
65 #include "DNA_userdef_types.h"
66 #include "BKE_utildefines.h"
67 #include "BKE_global.h"
68 #include "BKE_depsgraph.h"
75 #if defined(_WIN32) || defined(__APPLE__)
76 #include "quicktime_import.h"
77 #endif /* _WIN32 || __APPLE__ */
78 #endif /* WITH_QUICKTIME */
80 #include "IMB_imbuf_types.h"
81 #include "IMB_imbuf.h"
83 #include "IMB_allocimbuf.h"
87 #include <libavformat/avformat.h>
88 #include <libavcodec/avcodec.h>
89 #include <libavutil/rational.h>
90 #include <libswscale/swscale.h>
92 #if LIBAVFORMAT_VERSION_INT < (49 << 16)
93 #define FFMPEG_OLD_FRAME_RATE 1
95 #define FFMPEG_CODEC_IS_POINTER 1
98 #if (LIBAVCODEC_VERSION_MAJOR >= 52) && (LIBAVCODEC_VERSION_MINOR >= 29) && \
99 (LIBSWSCALE_VERSION_MAJOR >= 0) && (LIBSWSCALE_VERSION_MINOR >= 10)
100 #define FFMPEG_SWSCALE_COLOR_SPACE_SUPPORT
106 #ifdef _WIN32 /* on windows we use the ones in extern instead */
107 #include "libredcode/format.h"
108 #include "libredcode/codec.h"
110 #include "libredcode/format.h"
111 #include "libredcode/codec.h"
119 #include <dmedia/moviefile.h>
121 static void movie_printerror(char * str) {
122 const char * errstr = mvGetErrorStr(mvGetErrno());
125 if (errstr) printf("%s: %s\n", str, errstr);
126 else printf("%s: returned error\n", str);
127 } else printf("%s\n", errstr);
130 static int startmovie(struct anim * anim) {
131 if (anim == 0) return(-1);
133 if ( mvOpenFile (anim->name, O_BINARY|O_RDONLY, &anim->movie ) != DM_SUCCESS ) {
134 printf("Can't open movie: %s\n", anim->name);
137 if ( mvFindTrackByMedium (anim->movie, DM_IMAGE, &anim->track) != DM_SUCCESS ) {
138 printf("No image track in movie: %s\n", anim->name);
139 mvClose(anim->movie);
143 anim->duration = mvGetTrackLength (anim->track);
144 anim->params = mvGetParams( anim->track );
146 anim->x = dmParamsGetInt( anim->params, DM_IMAGE_WIDTH);
147 anim->y = dmParamsGetInt( anim->params, DM_IMAGE_HEIGHT);
148 anim->interlacing = dmParamsGetEnum (anim->params, DM_IMAGE_INTERLACING);
149 anim->orientation = dmParamsGetEnum (anim->params, DM_IMAGE_ORIENTATION);
150 anim->framesize = dmImageFrameSize(anim->params);
152 anim->curposition = 0;
155 /*printf("x:%d y:%d size:%d interl:%d dur:%d\n", anim->x, anim->y, anim->framesize, anim->interlacing, anim->duration);*/
159 static ImBuf * movie_fetchibuf(struct anim * anim, int position) {
161 /* extern rectcpy(); */
163 unsigned int *rect1, *rect2;
165 if (anim == 0) return (0);
167 ibuf = IMB_allocImBuf(anim->x, anim->y, 24, IB_rect);
169 if ( mvReadFrames(anim->track, position, 1, ibuf->x * ibuf->y *
170 sizeof(int), ibuf->rect ) != DM_SUCCESS ) {
171 movie_printerror("mvReadFrames");
177 if (anim->interlacing == DM_IMAGE_INTERLACED_EVEN) {
178 rect1 = ibuf->rect + (ibuf->x * ibuf->y) - 1;
179 rect2 = rect1 - ibuf->x;
181 for (size = ibuf->x * (ibuf->y - 1); size > 0; size--){
187 if (anim->interlacing == DM_IMAGE_INTERLACED_EVEN)
190 rect2 = rect1 + ibuf->x;
192 for (size = ibuf->x * (ibuf->y - 1); size > 0; size--){
196 /*if (anim->orientation == DM_TOP_TO_BOTTOM) IMB_flipy(ibuf);*/
202 static void free_anim_movie(struct anim * anim) {
203 if (anim == NULL) return;
206 mvClose(anim->movie);
212 int ismovie(char *name) {
213 return (mvIsMovieFile(name) == DM_TRUE);
218 int ismovie(char *UNUSED(name)) {
222 /* never called, just keep the linker happy */
223 static int startmovie(struct anim *UNUSED(anim)) { return 1; }
224 static ImBuf * movie_fetchibuf(struct anim *UNUSED(anim), int UNUSED(position)) { return NULL; }
225 static void free_anim_movie(struct anim *UNUSED(anim)) { ; }
230 # define PATHSEPERATOR '\\'
232 # define PATHSEPERATOR '/'
235 static int an_stringdec(const char *string, char* head, char *tail, unsigned short *numlen) {
236 unsigned short len,nume,nums=0;
242 for(i=len-1;i>=0;i--){
243 if (string[i]==PATHSEPERATOR) break;
244 if (isdigit(string[i])) {
257 strcpy(tail ,&string[nume+1]);
258 strcpy(head, string);
261 return ((int)atoi(&(string[nums])));
264 strcpy(head, string);
270 static void an_stringenc(char *string, const char *head, const char *tail, unsigned short numlen, int pic) {
271 BLI_stringenc(string, head, tail, numlen, pic);
274 static void free_anim_avi (struct anim *anim) {
275 #if defined(_WIN32) && !defined(FREE_WINDOWS)
279 if (anim == NULL) return;
280 if (anim->avi == NULL) return;
282 AVI_close (anim->avi);
283 MEM_freeN (anim->avi);
286 #if defined(_WIN32) && !defined(FREE_WINDOWS)
289 AVIStreamGetFrameClose(anim->pgf);
293 for (i = 0; i < anim->avistreams; i++){
294 AVIStreamRelease(anim->pavi[i]);
296 anim->avistreams = 0;
298 if (anim->pfileopen) {
299 AVIFileRelease(anim->pfile);
308 void IMB_free_anim_ibuf(struct anim * anim) {
309 if (anim == NULL) return;
311 if (anim->ibuf1) IMB_freeImBuf(anim->ibuf1);
312 if (anim->ibuf2) IMB_freeImBuf(anim->ibuf2);
314 anim->ibuf1 = anim->ibuf2 = NULL;
318 static void free_anim_ffmpeg(struct anim * anim);
321 static void free_anim_redcode(struct anim * anim);
324 void IMB_free_anim(struct anim * anim) {
326 printf("free anim, anim == NULL\n");
330 IMB_free_anim_ibuf(anim);
331 free_anim_movie(anim);
334 #ifdef WITH_QUICKTIME
335 free_anim_quicktime(anim);
338 free_anim_ffmpeg(anim);
341 free_anim_redcode(anim);
347 void IMB_close_anim(struct anim * anim) {
348 if (anim == 0) return;
354 struct anim * IMB_open_anim( const char * name, int ib_flags) {
357 anim = (struct anim*)MEM_callocN(sizeof(struct anim), "anim struct");
359 strcpy(anim->name, name); /* fixme: possible buffer overflow here? */
360 anim->ib_flags = ib_flags;
366 static int startavi (struct anim *anim) {
369 #if defined(_WIN32) && !defined(FREE_WINDOWS)
371 int i, firstvideo = -1;
374 LPBITMAPINFOHEADER lpbi;
378 anim->avi = MEM_callocN (sizeof(AviMovie),"animavi");
380 if (anim->avi == NULL) {
381 printf("Can't open avi: %s\n", anim->name);
385 avierror = AVI_open_movie (anim->name, anim->avi);
387 #if defined(_WIN32) && !defined(FREE_WINDOWS)
388 if (avierror == AVI_ERROR_COMPRESSION) {
390 hr = AVIFileOpen(&anim->pfile, anim->name, OF_READ, 0L);
393 for (i = 0; i < MAXNUMSTREAMS; i++) {
394 if (AVIFileGetStream(anim->pfile, &anim->pavi[i], 0L, i) != AVIERR_OK) {
398 AVIStreamInfo(anim->pavi[i], &avis, sizeof(avis));
399 if ((avis.fccType == streamtypeVIDEO) && (firstvideo == -1)) {
400 anim->pgf = AVIStreamGetFrameOpen(anim->pavi[i], NULL);
405 anim->avi->header->TotalFrames = AVIStreamLength(anim->pavi[i]);
407 // get information about images inside the stream
408 l = sizeof(abFormat);
409 AVIStreamReadFormat(anim->pavi[i], 0, &abFormat, &l);
410 lpbi = (LPBITMAPINFOHEADER)abFormat;
411 anim->avi->header->Height = lpbi->biHeight;
412 anim->avi->header->Width = lpbi->biWidth;
414 FIXCC(avis.fccHandler);
416 printf("Can't find AVI decoder for type : %4.4hs/%4.4hs\n",
417 (LPSTR)&avis.fccType,
418 (LPSTR)&avis.fccHandler);
423 // register number of opened avistreams
424 anim->avistreams = i;
427 // Couldn't get any video streams out of this file
429 if ((anim->avistreams == 0) || (firstvideo == -1)) {
430 avierror = AVI_ERROR_FORMAT;
432 avierror = AVI_ERROR_NONE;
433 anim->firstvideo = firstvideo;
441 if (avierror != AVI_ERROR_NONE) {
442 AVI_print_error(avierror);
443 printf ("Error loading avi: %s\n", anim->name);
448 anim->duration = anim->avi->header->TotalFrames;
451 anim->x = anim->avi->header->Width;
452 anim->y = anim->avi->header->Height;
453 anim->interlacing = 0;
454 anim->orientation = 0;
455 anim->framesize = anim->x * anim->y * 4;
457 anim->curposition = 0;
460 /* printf("x:%d y:%d size:%d interl:%d dur:%d\n", anim->x, anim->y, anim->framesize, anim->interlacing, anim->duration);*/
465 static ImBuf * avi_fetchibuf (struct anim *anim, int position) {
470 if (anim == NULL) return (NULL);
472 #if defined(_WIN32) && !defined(FREE_WINDOWS)
473 if (anim->avistreams) {
474 LPBITMAPINFOHEADER lpbi;
477 lpbi = AVIStreamGetFrame(anim->pgf, position + AVIStreamStart(anim->pavi[anim->firstvideo]));
479 ibuf = IMB_ibImageFromMemory((unsigned char *) lpbi, 100, IB_rect);
487 ibuf = IMB_allocImBuf (anim->x, anim->y, 24, IB_rect);
489 tmp = AVI_read_frame (anim->avi, AVI_FORMAT_RGB32, position,
490 AVI_get_stream(anim->avi, AVIST_VIDEO, 0));
493 printf ("Error reading frame from AVI");
494 IMB_freeImBuf (ibuf);
498 for (y=0; y < anim->y; y++) {
499 memcpy (&(ibuf->rect)[((anim->y-y)-1)*anim->x], &tmp[y*anim->x],
506 ibuf->profile = IB_PROFILE_SRGB;
513 extern void do_init_ffmpeg();
515 #ifdef FFMPEG_CODEC_IS_POINTER
516 static AVCodecContext* get_codec_from_stream(AVStream* stream)
518 return stream->codec;
521 static AVCodecContext* get_codec_from_stream(AVStream* stream)
523 return &stream->codec;
527 static int startffmpeg(struct anim * anim) {
531 AVFormatContext *pFormatCtx;
532 AVCodecContext *pCodecCtx;
534 #ifdef FFMPEG_SWSCALE_COLOR_SPACE_SUPPORT
535 /* The following for color space determination */
536 int srcRange, dstRange, brightness, contrast, saturation;
538 const int *inv_table;
541 if (anim == 0) return(-1);
545 if(av_open_input_file(&pFormatCtx, anim->name, NULL, 0, NULL)!=0) {
549 if(av_find_stream_info(pFormatCtx)<0) {
550 av_close_input_file(pFormatCtx);
554 dump_format(pFormatCtx, 0, anim->name, 0);
557 /* Find the first video stream */
559 for(i=0; i<pFormatCtx->nb_streams; i++)
560 if(get_codec_from_stream(pFormatCtx->streams[i])->codec_type
561 == CODEC_TYPE_VIDEO) {
566 if(videoStream==-1) {
567 av_close_input_file(pFormatCtx);
571 pCodecCtx = get_codec_from_stream(pFormatCtx->streams[videoStream]);
573 /* Find the decoder for the video stream */
574 pCodec=avcodec_find_decoder(pCodecCtx->codec_id);
576 av_close_input_file(pFormatCtx);
580 pCodecCtx->workaround_bugs = 1;
582 if(avcodec_open(pCodecCtx, pCodec)<0) {
583 av_close_input_file(pFormatCtx);
587 #ifdef FFMPEG_OLD_FRAME_RATE
588 if(pCodecCtx->frame_rate>1000 && pCodecCtx->frame_rate_base==1)
589 pCodecCtx->frame_rate_base=1000;
592 anim->duration = pFormatCtx->duration * pCodecCtx->frame_rate
593 / pCodecCtx->frame_rate_base / AV_TIME_BASE;
595 anim->duration = ceil(pFormatCtx->duration
596 * av_q2d(pFormatCtx->streams[videoStream]->r_frame_rate)
602 anim->x = pCodecCtx->width;
603 anim->y = pCodecCtx->height;
604 anim->interlacing = 0;
605 anim->orientation = 0;
606 anim->framesize = anim->x * anim->y * 4;
608 anim->curposition = -1;
610 anim->pFormatCtx = pFormatCtx;
611 anim->pCodecCtx = pCodecCtx;
612 anim->pCodec = pCodec;
613 anim->videoStream = videoStream;
615 anim->pFrame = avcodec_alloc_frame();
616 anim->pFrameDeinterlaced = avcodec_alloc_frame();
617 anim->pFrameRGB = avcodec_alloc_frame();
619 if (avpicture_get_size(PIX_FMT_RGBA, anim->x, anim->y)
620 != anim->x * anim->y * 4) {
622 "ffmpeg has changed alloc scheme ... ARGHHH!\n");
623 avcodec_close(anim->pCodecCtx);
624 av_close_input_file(anim->pFormatCtx);
625 av_free(anim->pFrameRGB);
626 av_free(anim->pFrameDeinterlaced);
627 av_free(anim->pFrame);
628 anim->pCodecCtx = NULL;
632 if (anim->ib_flags & IB_animdeinterlace) {
633 avpicture_fill((AVPicture*) anim->pFrameDeinterlaced,
634 MEM_callocN(avpicture_get_size(
635 anim->pCodecCtx->pix_fmt,
637 "ffmpeg deinterlace"),
638 anim->pCodecCtx->pix_fmt, anim->x, anim->y);
641 if (pCodecCtx->has_b_frames) {
642 anim->preseek = 25; /* FIXME: detect gopsize ... */
647 anim->img_convert_ctx = sws_getContext(
648 anim->pCodecCtx->width,
649 anim->pCodecCtx->height,
650 anim->pCodecCtx->pix_fmt,
651 anim->pCodecCtx->width,
652 anim->pCodecCtx->height,
654 SWS_FAST_BILINEAR | SWS_PRINT_INFO,
657 if (!anim->img_convert_ctx) {
659 "Can't transform color space??? Bailing out...\n");
660 avcodec_close(anim->pCodecCtx);
661 av_close_input_file(anim->pFormatCtx);
662 av_free(anim->pFrameRGB);
663 av_free(anim->pFrameDeinterlaced);
664 av_free(anim->pFrame);
665 anim->pCodecCtx = NULL;
669 #ifdef FFMPEG_SWSCALE_COLOR_SPACE_SUPPORT
670 /* Try do detect if input has 0-255 YCbCR range (JFIF Jpeg MotionJpeg) */
671 if (!sws_getColorspaceDetails(anim->img_convert_ctx, (int**)&inv_table, &srcRange,
672 &table, &dstRange, &brightness, &contrast, &saturation)) {
674 srcRange = srcRange || anim->pCodecCtx->color_range == AVCOL_RANGE_JPEG;
675 inv_table = sws_getCoefficients(anim->pCodecCtx->colorspace);
677 if(sws_setColorspaceDetails(anim->img_convert_ctx, (int *)inv_table, srcRange,
678 table, dstRange, brightness, contrast, saturation)) {
680 printf("Warning: Could not set libswscale colorspace details.\n");
684 printf("Warning: Could not set libswscale colorspace details.\n");
691 static void ffmpeg_postprocess(struct anim * anim, ImBuf * ibuf,
694 AVFrame * input = anim->pFrame;
696 /* This means the data wasnt read properly,
697 this check stops crashing */
698 if (input->data[0]==0 && input->data[1]==0
699 && input->data[2]==0 && input->data[3]==0){
700 fprintf(stderr, "ffmpeg_fetchibuf: "
701 "data not read properly...\n");
705 if (anim->ib_flags & IB_animdeinterlace) {
706 if (avpicture_deinterlace(
708 anim->pFrameDeinterlaced,
711 anim->pCodecCtx->pix_fmt,
712 anim->pCodecCtx->width,
713 anim->pCodecCtx->height)
717 input = anim->pFrameDeinterlaced;
721 if (ENDIAN_ORDER == B_ENDIAN) {
722 int * dstStride = anim->pFrameRGB->linesize;
723 uint8_t** dst = anim->pFrameRGB->data;
724 int dstStride2[4] = { dstStride[0], 0, 0, 0 };
725 uint8_t* dst2[4] = { dst[0], 0, 0, 0 };
727 unsigned char* bottom;
730 sws_scale(anim->img_convert_ctx,
731 (const uint8_t * const *)input->data,
734 anim->pCodecCtx->height,
738 /* workaround: sws_scale bug
739 sets alpha = 0 and compensate
740 for altivec-bugs and flipy... */
742 bottom = (unsigned char*) ibuf->rect;
743 top = bottom + ibuf->x * (ibuf->y-1) * 4;
745 h = (ibuf->y + 1) / 2;
748 for (y = 0; y < h; y++) {
749 unsigned char tmp[4];
750 unsigned int * tmp_l =
754 for (x = 0; x < w; x++) {
764 *(unsigned int*) top = *tmp_l;
772 int * dstStride = anim->pFrameRGB->linesize;
773 uint8_t** dst = anim->pFrameRGB->data;
774 int dstStride2[4] = { -dstStride[0], 0, 0, 0 };
775 uint8_t* dst2[4] = { dst[0] + (anim->y - 1)*dstStride[0],
780 sws_scale(anim->img_convert_ctx,
781 (const uint8_t * const *)input->data,
784 anim->pCodecCtx->height,
788 r = (unsigned char*) ibuf->rect;
790 /* workaround sws_scale bug: older version of
791 sws_scale set alpha = 0... */
793 for (i = 0; i < ibuf->x * ibuf->y; i++) {
801 static ImBuf * ffmpeg_fetchibuf(struct anim * anim, int position) {
805 int64_t pts_to_search = 0;
808 int seek_by_bytes= 0;
809 int preseek_count = 0;
811 if (anim == 0) return (0);
813 ibuf = IMB_allocImBuf(anim->x, anim->y, 32, IB_rect);
815 avpicture_fill((AVPicture*) anim->pFrameRGB,
816 (unsigned char*) ibuf->rect,
817 PIX_FMT_RGBA, anim->x, anim->y);
819 if (position != anim->curposition + 1) {
820 if (position > anim->curposition + 1
822 && position - (anim->curposition + 1) < anim->preseek) {
823 while(av_read_frame(anim->pFormatCtx, &packet)>=0) {
824 if (packet.stream_index == anim->videoStream) {
825 avcodec_decode_video(
827 anim->pFrame, &frameFinished,
828 packet.data, packet.size);
834 av_free_packet(&packet);
835 if (position == anim->curposition+1) {
842 /* disable seek_by_bytes for now, since bitrates are guessed wrong!
843 also: MPEG2TS-seeking was fixed in later versions of ffmpeg, so problem
844 is somewhat fixed by now (until we add correct timecode management code...)
847 seek_by_bytes = !!(anim->pFormatCtx->iformat->flags & AVFMT_TS_DISCONT);
849 seek_by_bytes = FALSE;
852 if (position != anim->curposition + 1) {
853 #ifdef FFMPEG_OLD_FRAME_RATE
855 (double) anim->pCodecCtx->frame_rate
856 / (double) anim->pCodecCtx->frame_rate_base;
859 av_q2d(anim->pFormatCtx->streams[anim->videoStream]
862 double pts_time_base = av_q2d(anim->pFormatCtx->streams[anim->videoStream]->time_base);
864 long long st_time = anim->pFormatCtx->start_time;
868 pos = position - anim->preseek;
872 preseek_count = position - pos;
874 pos *= anim->pFormatCtx->bit_rate / frame_rate;
877 pos = (long long) (position - anim->preseek)
878 * AV_TIME_BASE / frame_rate;
883 if (st_time != AV_NOPTS_VALUE) {
888 ret = av_seek_frame(anim->pFormatCtx, -1,
890 AVSEEK_FLAG_BACKWARD | (
893 | AVSEEK_FLAG_BYTE : 0));
895 fprintf(stderr, "error while seeking: %d\n", ret);
898 pts_to_search = (long long)
899 (((double) position) / pts_time_base / frame_rate);
900 if (st_time != AV_NOPTS_VALUE) {
901 pts_to_search += st_time / pts_time_base/ AV_TIME_BASE;
905 avcodec_flush_buffers(anim->pCodecCtx);
908 while(av_read_frame(anim->pFormatCtx, &packet)>=0) {
909 if(packet.stream_index == anim->videoStream) {
910 avcodec_decode_video(anim->pCodecCtx,
911 anim->pFrame, &frameFinished,
912 packet.data, packet.size);
914 if (seek_by_bytes && preseek_count > 0) {
918 if (frameFinished && !pos_found) {
920 if (!preseek_count) {
922 anim->curposition = position;
925 if (packet.dts >= pts_to_search) {
927 anim->curposition = position;
932 if(frameFinished && pos_found == 1) {
933 ffmpeg_postprocess(anim, ibuf, &filter_y);
934 av_free_packet(&packet);
939 av_free_packet(&packet);
942 if (filter_y && ibuf) {
946 ibuf->profile = IB_PROFILE_SRGB;
951 static void free_anim_ffmpeg(struct anim * anim) {
952 if (anim == NULL) return;
954 if (anim->pCodecCtx) {
955 avcodec_close(anim->pCodecCtx);
956 av_close_input_file(anim->pFormatCtx);
957 av_free(anim->pFrameRGB);
958 av_free(anim->pFrame);
960 if (anim->ib_flags & IB_animdeinterlace) {
961 MEM_freeN(anim->pFrameDeinterlaced->data[0]);
963 av_free(anim->pFrameDeinterlaced);
964 sws_freeContext(anim->img_convert_ctx);
973 static int startredcode(struct anim * anim) {
974 anim->redcodeCtx = redcode_open(anim->name);
975 if (!anim->redcodeCtx) {
978 anim->duration = redcode_get_length(anim->redcodeCtx);
983 static ImBuf * redcode_fetchibuf(struct anim * anim, int position) {
985 struct redcode_frame * frame;
986 struct redcode_frame_raw * raw_frame;
988 if (!anim->redcodeCtx) {
992 frame = redcode_read_video_frame(anim->redcodeCtx, position);
998 raw_frame = redcode_decode_video_raw(frame, 1);
1000 redcode_free_frame(frame);
1006 ibuf = IMB_allocImBuf(raw_frame->width * 2,
1007 raw_frame->height * 2, 32, IB_rectfloat);
1009 redcode_decode_video_float(raw_frame, ibuf->rect_float, 1);
1014 static void free_anim_redcode(struct anim * anim) {
1015 if (anim->redcodeCtx) {
1016 redcode_close(anim->redcodeCtx);
1017 anim->redcodeCtx = 0;
1024 /* probeer volgende plaatje te lezen */
1025 /* Geen plaatje, probeer dan volgende animatie te openen */
1026 /* gelukt, haal dan eerste plaatje van animatie */
1028 static struct ImBuf * anim_getnew(struct anim * anim) {
1029 struct ImBuf *ibuf = 0;
1031 if (anim == NULL) return(0);
1033 free_anim_movie(anim);
1034 free_anim_avi(anim);
1035 #ifdef WITH_QUICKTIME
1036 free_anim_quicktime(anim);
1039 free_anim_ffmpeg(anim);
1042 free_anim_redcode(anim);
1046 if (anim->curtype != 0) return (0);
1047 anim->curtype = imb_get_anim_type(anim->name);
1049 switch (anim->curtype) {
1051 ibuf = IMB_loadiffname(anim->name, anim->ib_flags);
1053 strcpy(anim->first, anim->name);
1058 if (startmovie(anim)) return (0);
1059 ibuf = IMB_allocImBuf (anim->x, anim->y, 24, 0); /* fake */
1062 if (startavi(anim)) {
1063 printf("couldnt start avi\n");
1066 ibuf = IMB_allocImBuf (anim->x, anim->y, 24, 0);
1068 #ifdef WITH_QUICKTIME
1070 if (startquicktime(anim)) return (0);
1071 ibuf = IMB_allocImBuf (anim->x, anim->y, 24, 0);
1076 if (startffmpeg(anim)) return (0);
1077 ibuf = IMB_allocImBuf (anim->x, anim->y, 24, 0);
1082 if (startredcode(anim)) return (0);
1083 ibuf = IMB_allocImBuf (8, 8, 32, 0);
1090 struct ImBuf * IMB_anim_previewframe(struct anim * anim) {
1091 struct ImBuf * ibuf = 0;
1094 ibuf = IMB_anim_absolute(anim, 0);
1096 IMB_freeImBuf(ibuf);
1097 position = anim->duration / 2;
1098 ibuf = IMB_anim_absolute(anim, position);
1103 struct ImBuf * IMB_anim_absolute(struct anim * anim, int position) {
1104 struct ImBuf * ibuf = 0;
1105 char head[256], tail[256];
1106 unsigned short digits;
1109 if (anim == NULL) return(0);
1111 filter_y = (anim->ib_flags & IB_animdeinterlace);
1113 if (anim->curtype == 0) {
1114 ibuf = anim_getnew(anim);
1119 IMB_freeImBuf(ibuf); /* ???? */
1123 if (position < 0) return(0);
1124 if (position >= anim->duration) return(0);
1126 switch(anim->curtype) {
1128 pic = an_stringdec(anim->first, head, tail, &digits);
1130 an_stringenc(anim->name, head, tail, digits, pic);
1131 ibuf = IMB_loadiffname(anim->name, IB_rect);
1133 anim->curposition = position;
1137 ibuf = movie_fetchibuf(anim, position);
1139 anim->curposition = position;
1140 IMB_convert_rgba_to_abgr(ibuf);
1141 ibuf->profile = IB_PROFILE_SRGB;
1145 ibuf = avi_fetchibuf(anim, position);
1147 anim->curposition = position;
1149 #ifdef WITH_QUICKTIME
1151 ibuf = qtime_fetchibuf(anim, position);
1153 anim->curposition = position;
1158 ibuf = ffmpeg_fetchibuf(anim, position);
1160 anim->curposition = position;
1161 filter_y = 0; /* done internally */
1166 ibuf = redcode_fetchibuf(anim, position);
1167 if (ibuf) anim->curposition = position;
1173 if (filter_y) IMB_filtery(ibuf);
1174 sprintf(ibuf->name, "%s.%04d", anim->name, anim->curposition + 1);
1182 int IMB_anim_get_duration(struct anim *anim) {
1183 return anim->duration;
1186 void IMB_anim_set_preseek(struct anim * anim, int preseek)
1188 anim->preseek = preseek;
1191 int IMB_anim_get_preseek(struct anim * anim)
1193 return anim->preseek;