2 * ***** BEGIN GPL LICENSE BLOCK *****
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19 * All rights reserved.
21 * The Original Code is: all of this file.
23 * Contributor(s): none yet.
25 * ***** END GPL LICENSE BLOCK *****
28 /** \file blender/imbuf/intern/anim_movie.c
45 #undef AVIIF_KEYFRAME /* redefined in AVI_avi.h */
46 #undef AVIIF_LIST /* redefined in AVI_avi.h */
50 if (fcc == 0) { fcc = mmioFOURCC('N', 'o', 'n', 'e'); } \
51 if (fcc == BI_RLE8) { fcc = mmioFOURCC('R', 'l', 'e', '8'); } \
56 #include <sys/types.h>
67 #include "BLI_utildefines.h"
68 #include "BLI_string.h"
69 #include "BLI_path_util.h"
70 #include "BLI_math_base.h"
72 #include "MEM_guardedalloc.h"
74 #include "DNA_userdef_types.h"
76 #include "BKE_global.h"
77 #include "BKE_depsgraph.h"
86 #if defined(_WIN32) || defined(__APPLE__)
87 #include "quicktime_import.h"
88 #endif /* _WIN32 || __APPLE__ */
89 #endif /* WITH_QUICKTIME */
91 #include "IMB_imbuf_types.h"
92 #include "IMB_imbuf.h"
94 #include "IMB_allocimbuf.h"
96 #include "IMB_indexer.h"
99 #include <libavformat/avformat.h>
100 #include <libavcodec/avcodec.h>
101 #include <libavutil/rational.h>
102 #include <libswscale/swscale.h>
104 #include "ffmpeg_compat.h"
109 #ifdef _WIN32 /* on windows we use the ones in extern instead */
110 #include "libredcode/format.h"
111 #include "libredcode/codec.h"
113 #include "libredcode/format.h"
114 #include "libredcode/codec.h"
118 #include "IMB_colormanagement.h"
119 #include "IMB_colormanagement_intern.h"
121 int ismovie(const char *UNUSED(filepath))
126 /* never called, just keep the linker happy */
127 static int startmovie(struct anim *UNUSED(anim))
131 static ImBuf *movie_fetchibuf(struct anim *UNUSED(anim), int UNUSED(position))
135 static void free_anim_movie(struct anim *UNUSED(anim))
142 # define PATHSEPERATOR '\\'
144 # define PATHSEPERATOR '/'
147 static int an_stringdec(const char *string, char *head, char *tail, unsigned short *numlen)
149 unsigned short len, nume, nums = 0;
150 short i, found = FALSE;
152 len = strlen(string);
155 for (i = len - 1; i >= 0; i--) {
156 if (string[i] == PATHSEPERATOR) break;
157 if (isdigit(string[i])) {
172 strcpy(tail, &string[nume + 1]);
173 strcpy(head, string);
175 *numlen = nume - nums + 1;
176 return ((int)atoi(&(string[nums])));
179 strcpy(head, string);
185 static void an_stringenc(char *string, const char *head, const char *tail, unsigned short numlen, int pic)
187 BLI_stringenc(string, head, tail, numlen, pic);
191 static void free_anim_avi(struct anim *anim)
193 #if defined(_WIN32) && !defined(FREE_WINDOWS)
197 if (anim == NULL) return;
198 if (anim->avi == NULL) return;
200 AVI_close(anim->avi);
201 MEM_freeN(anim->avi);
204 #if defined(_WIN32) && !defined(FREE_WINDOWS)
207 AVIStreamGetFrameClose(anim->pgf);
211 for (i = 0; i < anim->avistreams; i++) {
212 AVIStreamRelease(anim->pavi[i]);
214 anim->avistreams = 0;
216 if (anim->pfileopen) {
217 AVIFileRelease(anim->pfile);
225 #endif /* WITH_AVI */
228 static void free_anim_ffmpeg(struct anim *anim);
231 static void free_anim_redcode(struct anim *anim);
234 void IMB_free_anim(struct anim *anim)
237 printf("free anim, anim == NULL\n");
241 free_anim_movie(anim);
247 #ifdef WITH_QUICKTIME
248 free_anim_quicktime(anim);
251 free_anim_ffmpeg(anim);
254 free_anim_redcode(anim);
256 IMB_free_indices(anim);
261 void IMB_close_anim(struct anim *anim)
263 if (anim == NULL) return;
268 void IMB_close_anim_proxies(struct anim *anim)
273 IMB_free_indices(anim);
276 struct anim *IMB_open_anim(const char *name, int ib_flags, int streamindex, char colorspace[IM_MAX_SPACE])
280 anim = (struct anim *)MEM_callocN(sizeof(struct anim), "anim struct");
283 colorspace_set_default_role(colorspace, IM_MAX_SPACE, COLOR_ROLE_DEFAULT_BYTE);
284 BLI_strncpy(anim->colorspace, colorspace, sizeof(anim->colorspace));
287 colorspace_set_default_role(anim->colorspace, sizeof(anim->colorspace), COLOR_ROLE_DEFAULT_BYTE);
290 BLI_strncpy(anim->name, name, sizeof(anim->name));
291 anim->ib_flags = ib_flags;
292 anim->streamindex = streamindex;
298 static int startavi(struct anim *anim)
302 #if defined(_WIN32) && !defined(FREE_WINDOWS)
304 int i, firstvideo = -1;
308 LPBITMAPINFOHEADER lpbi;
311 streamcount = anim->streamindex;
314 anim->avi = MEM_callocN(sizeof(AviMovie), "animavi");
316 if (anim->avi == NULL) {
317 printf("Can't open avi: %s\n", anim->name);
321 avierror = AVI_open_movie(anim->name, anim->avi);
323 #if defined(_WIN32) && !defined(FREE_WINDOWS)
324 if (avierror == AVI_ERROR_COMPRESSION) {
326 hr = AVIFileOpen(&anim->pfile, anim->name, OF_READ, 0L);
329 for (i = 0; i < MAXNUMSTREAMS; i++) {
330 if (AVIFileGetStream(anim->pfile, &anim->pavi[i], 0L, i) != AVIERR_OK) {
334 AVIStreamInfo(anim->pavi[i], &avis, sizeof(avis));
335 if ((avis.fccType == streamtypeVIDEO) && (firstvideo == -1)) {
336 if (streamcount > 0) {
340 anim->pgf = AVIStreamGetFrameOpen(anim->pavi[i], NULL);
344 /* get stream length */
345 anim->avi->header->TotalFrames = AVIStreamLength(anim->pavi[i]);
347 /* get information about images inside the stream */
348 l = sizeof(abFormat);
349 AVIStreamReadFormat(anim->pavi[i], 0, &abFormat, &l);
350 lpbi = (LPBITMAPINFOHEADER)abFormat;
351 anim->avi->header->Height = lpbi->biHeight;
352 anim->avi->header->Width = lpbi->biWidth;
355 FIXCC(avis.fccHandler);
357 printf("Can't find AVI decoder for type : %4.4hs/%4.4hs\n",
358 (LPSTR)&avis.fccType,
359 (LPSTR)&avis.fccHandler);
364 /* register number of opened avistreams */
365 anim->avistreams = i;
368 * Couldn't get any video streams out of this file
370 if ((anim->avistreams == 0) || (firstvideo == -1)) {
371 avierror = AVI_ERROR_FORMAT;
374 avierror = AVI_ERROR_NONE;
375 anim->firstvideo = firstvideo;
384 if (avierror != AVI_ERROR_NONE) {
385 AVI_print_error(avierror);
386 printf("Error loading avi: %s\n", anim->name);
391 anim->duration = anim->avi->header->TotalFrames;
394 anim->x = anim->avi->header->Width;
395 anim->y = anim->avi->header->Height;
396 anim->interlacing = 0;
397 anim->orientation = 0;
398 anim->framesize = anim->x * anim->y * 4;
400 anim->curposition = 0;
403 /* printf("x:%d y:%d size:%d interl:%d dur:%d\n", anim->x, anim->y, anim->framesize, anim->interlacing, anim->duration);*/
407 #endif /* WITH_AVI */
410 static ImBuf *avi_fetchibuf(struct anim *anim, int position)
420 #if defined(_WIN32) && !defined(FREE_WINDOWS)
421 if (anim->avistreams) {
422 LPBITMAPINFOHEADER lpbi;
425 lpbi = AVIStreamGetFrame(anim->pgf, position + AVIStreamStart(anim->pavi[anim->firstvideo]));
427 ibuf = IMB_ibImageFromMemory((unsigned char *) lpbi, 100, IB_rect, anim->colorspace, "<avi_fetchibuf>");
435 ibuf = IMB_allocImBuf(anim->x, anim->y, 24, IB_rect);
437 tmp = AVI_read_frame(anim->avi, AVI_FORMAT_RGB32, position,
438 AVI_get_stream(anim->avi, AVIST_VIDEO, 0));
441 printf("Error reading frame from AVI: '%s'\n", anim->name);
446 for (y = 0; y < anim->y; y++) {
447 memcpy(&(ibuf->rect)[((anim->y - y) - 1) * anim->x], &tmp[y * anim->x],
454 ibuf->rect_colorspace = colormanage_colorspace_get_named(anim->colorspace);
458 #endif /* WITH_AVI */
462 static int startffmpeg(struct anim *anim)
467 AVFormatContext *pFormatCtx = NULL;
468 AVCodecContext *pCodecCtx;
469 AVRational frame_rate;
474 #ifdef FFMPEG_SWSCALE_COLOR_SPACE_SUPPORT
475 /* The following for color space determination */
476 int srcRange, dstRange, brightness, contrast, saturation;
478 const int *inv_table;
481 if (anim == NULL) return(-1);
483 streamcount = anim->streamindex;
485 if (avformat_open_input(&pFormatCtx, anim->name, NULL, NULL) != 0) {
489 if (avformat_find_stream_info(pFormatCtx, NULL) < 0) {
490 avformat_close_input(&pFormatCtx);
494 av_dump_format(pFormatCtx, 0, anim->name, 0);
497 /* Find the video stream */
500 for (i = 0; i < pFormatCtx->nb_streams; i++)
501 if (pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
502 if (streamcount > 0) {
510 if (videoStream == -1) {
511 avformat_close_input(&pFormatCtx);
515 pCodecCtx = pFormatCtx->streams[videoStream]->codec;
517 /* Find the decoder for the video stream */
518 pCodec = avcodec_find_decoder(pCodecCtx->codec_id);
519 if (pCodec == NULL) {
520 avformat_close_input(&pFormatCtx);
524 pCodecCtx->workaround_bugs = 1;
526 if (avcodec_open2(pCodecCtx, pCodec, NULL) < 0) {
527 avformat_close_input(&pFormatCtx);
531 frame_rate = av_get_r_frame_rate_compat(pFormatCtx->streams[videoStream]);
532 anim->duration = ceil(pFormatCtx->duration *
535 printf("%d\n", anim->duration);
537 frs_num = frame_rate.num;
538 frs_den = frame_rate.den;
540 frs_den *= AV_TIME_BASE;
542 while (frs_num % 10 == 0 && frs_den >= 2.0 && frs_num > 10) {
547 anim->frs_sec = frs_num;
548 anim->frs_sec_base = frs_den;
552 anim->x = pCodecCtx->width;
553 anim->y = av_get_cropped_height_from_codec(pCodecCtx);
555 anim->pFormatCtx = pFormatCtx;
556 anim->pCodecCtx = pCodecCtx;
557 anim->pCodec = pCodec;
558 anim->videoStream = videoStream;
560 anim->interlacing = 0;
561 anim->orientation = 0;
562 anim->framesize = anim->x * anim->y * 4;
564 anim->curposition = -1;
565 anim->last_frame = 0;
568 anim->next_packet.stream_index = -1;
570 anim->pFrame = avcodec_alloc_frame();
571 anim->pFrameComplete = FALSE;
572 anim->pFrameDeinterlaced = avcodec_alloc_frame();
573 anim->pFrameRGB = avcodec_alloc_frame();
575 if (avpicture_get_size(PIX_FMT_RGBA, anim->x, anim->y) !=
576 anim->x * anim->y * 4)
579 "ffmpeg has changed alloc scheme ... ARGHHH!\n");
580 avcodec_close(anim->pCodecCtx);
581 avformat_close_input(&anim->pFormatCtx);
582 av_free(anim->pFrameRGB);
583 av_free(anim->pFrameDeinterlaced);
584 av_free(anim->pFrame);
585 anim->pCodecCtx = NULL;
589 if (anim->ib_flags & IB_animdeinterlace) {
590 avpicture_fill((AVPicture *) anim->pFrameDeinterlaced,
591 MEM_callocN(avpicture_get_size(
592 anim->pCodecCtx->pix_fmt,
593 anim->pCodecCtx->width,
594 anim->pCodecCtx->height),
595 "ffmpeg deinterlace"),
596 anim->pCodecCtx->pix_fmt,
597 anim->pCodecCtx->width,
598 anim->pCodecCtx->height);
601 if (pCodecCtx->has_b_frames) {
602 anim->preseek = 25; /* FIXME: detect gopsize ... */
608 anim->img_convert_ctx = sws_getContext(
611 anim->pCodecCtx->pix_fmt,
615 SWS_FAST_BILINEAR | SWS_PRINT_INFO | SWS_FULL_CHR_H_INT,
618 if (!anim->img_convert_ctx) {
620 "Can't transform color space??? Bailing out...\n");
621 avcodec_close(anim->pCodecCtx);
622 avformat_close_input(&anim->pFormatCtx);
623 av_free(anim->pFrameRGB);
624 av_free(anim->pFrameDeinterlaced);
625 av_free(anim->pFrame);
626 anim->pCodecCtx = NULL;
630 #ifdef FFMPEG_SWSCALE_COLOR_SPACE_SUPPORT
631 /* Try do detect if input has 0-255 YCbCR range (JFIF Jpeg MotionJpeg) */
632 if (!sws_getColorspaceDetails(anim->img_convert_ctx, (int **)&inv_table, &srcRange,
633 &table, &dstRange, &brightness, &contrast, &saturation))
635 srcRange = srcRange || anim->pCodecCtx->color_range == AVCOL_RANGE_JPEG;
636 inv_table = sws_getCoefficients(anim->pCodecCtx->colorspace);
638 if (sws_setColorspaceDetails(anim->img_convert_ctx, (int *)inv_table, srcRange,
639 table, dstRange, brightness, contrast, saturation))
641 fprintf(stderr, "Warning: Could not set libswscale colorspace details.\n");
645 fprintf(stderr, "Warning: Could not set libswscale colorspace details.\n");
652 /* postprocess the image in anim->pFrame and do color conversion
653 * and deinterlacing stuff.
655 * Output is anim->last_frame
658 static void ffmpeg_postprocess(struct anim *anim)
660 AVFrame *input = anim->pFrame;
661 ImBuf *ibuf = anim->last_frame;
664 if (!anim->pFrameComplete) {
668 /* This means the data wasnt read properly,
669 * this check stops crashing */
670 if (input->data[0] == 0 && input->data[1] == 0 &&
671 input->data[2] == 0 && input->data[3] == 0)
673 fprintf(stderr, "ffmpeg_fetchibuf: "
674 "data not read properly...\n");
678 av_log(anim->pFormatCtx, AV_LOG_DEBUG,
679 " POSTPROC: anim->pFrame planes: %p %p %p %p\n",
680 input->data[0], input->data[1], input->data[2],
684 if (anim->ib_flags & IB_animdeinterlace) {
685 if (avpicture_deinterlace(
687 anim->pFrameDeinterlaced,
690 anim->pCodecCtx->pix_fmt,
691 anim->pCodecCtx->width,
692 anim->pCodecCtx->height) < 0)
697 input = anim->pFrameDeinterlaced;
701 avpicture_fill((AVPicture *) anim->pFrameRGB,
702 (unsigned char *) ibuf->rect,
703 PIX_FMT_RGBA, anim->x, anim->y);
705 if (ENDIAN_ORDER == B_ENDIAN) {
706 int *dstStride = anim->pFrameRGB->linesize;
707 uint8_t **dst = anim->pFrameRGB->data;
708 int dstStride2[4] = { dstStride[0], 0, 0, 0 };
709 uint8_t *dst2[4] = { dst[0], 0, 0, 0 };
711 unsigned char *bottom;
714 sws_scale(anim->img_convert_ctx,
715 (const uint8_t *const *)input->data,
722 bottom = (unsigned char *) ibuf->rect;
723 top = bottom + ibuf->x * (ibuf->y - 1) * 4;
725 h = (ibuf->y + 1) / 2;
728 for (y = 0; y < h; y++) {
729 unsigned char tmp[4];
730 unsigned int *tmp_l =
731 (unsigned int *) tmp;
733 for (x = 0; x < w; x++) {
744 *(unsigned int *) top = *tmp_l;
753 int *dstStride = anim->pFrameRGB->linesize;
754 uint8_t **dst = anim->pFrameRGB->data;
755 int dstStride2[4] = { -dstStride[0], 0, 0, 0 };
756 uint8_t *dst2[4] = { dst[0] + (anim->y - 1) * dstStride[0],
759 sws_scale(anim->img_convert_ctx,
760 (const uint8_t *const *)input->data,
773 /* decode one video frame also considering the packet read into next_packet */
775 static int ffmpeg_decode_video_frame(struct anim *anim)
779 av_log(anim->pFormatCtx, AV_LOG_DEBUG, " DECODE VIDEO FRAME\n");
781 if (anim->next_packet.stream_index == anim->videoStream) {
782 av_free_packet(&anim->next_packet);
783 anim->next_packet.stream_index = -1;
786 while ((rval = av_read_frame(anim->pFormatCtx, &anim->next_packet)) >= 0) {
787 av_log(anim->pFormatCtx,
789 "%sREAD: strID=%d (VID: %d) dts=%lld pts=%lld "
791 (anim->next_packet.stream_index == anim->videoStream)
793 anim->next_packet.stream_index,
795 (anim->next_packet.dts == AV_NOPTS_VALUE) ? -1 :
796 (long long int)anim->next_packet.dts,
797 (anim->next_packet.pts == AV_NOPTS_VALUE) ? -1 :
798 (long long int)anim->next_packet.pts,
799 (anim->next_packet.flags & AV_PKT_FLAG_KEY) ?
801 if (anim->next_packet.stream_index == anim->videoStream) {
802 anim->pFrameComplete = 0;
804 avcodec_decode_video2(
806 anim->pFrame, &anim->pFrameComplete,
809 if (anim->pFrameComplete) {
810 anim->next_pts = av_get_pts_from_frame(
811 anim->pFormatCtx, anim->pFrame);
813 av_log(anim->pFormatCtx,
815 " FRAME DONE: next_pts=%lld "
816 "pkt_pts=%lld, guessed_pts=%lld\n",
817 (anim->pFrame->pts == AV_NOPTS_VALUE) ?
818 -1 : (long long int)anim->pFrame->pts,
819 (anim->pFrame->pkt_pts == AV_NOPTS_VALUE) ?
820 -1 : (long long int)anim->pFrame->pkt_pts,
821 (long long int)anim->next_pts);
825 av_free_packet(&anim->next_packet);
826 anim->next_packet.stream_index = -1;
829 if (rval == AVERROR_EOF) {
830 /* this sets size and data fields to zero,
831 * which is necessary to decode the remaining data
832 * in the decoder engine after EOF. It also prevents a memory
833 * leak, since av_read_frame spills out a full size packet even
834 * on EOF... (and: it's save to call on NULL packets) */
836 av_free_packet(&anim->next_packet);
838 anim->next_packet.size = 0;
839 anim->next_packet.data = 0;
841 anim->pFrameComplete = 0;
843 avcodec_decode_video2(
845 anim->pFrame, &anim->pFrameComplete,
848 if (anim->pFrameComplete) {
849 anim->next_pts = av_get_pts_from_frame(
850 anim->pFormatCtx, anim->pFrame);
852 av_log(anim->pFormatCtx,
854 " FRAME DONE (after EOF): next_pts=%lld "
855 "pkt_pts=%lld, guessed_pts=%lld\n",
856 (anim->pFrame->pts == AV_NOPTS_VALUE) ?
857 -1 : (long long int)anim->pFrame->pts,
858 (anim->pFrame->pkt_pts == AV_NOPTS_VALUE) ?
859 -1 : (long long int)anim->pFrame->pkt_pts,
860 (long long int)anim->next_pts);
866 anim->next_packet.stream_index = -1;
868 av_log(anim->pFormatCtx,
869 AV_LOG_ERROR, " DECODE READ FAILED: av_read_frame() "
870 "returned error: %d\n", rval);
876 static void ffmpeg_decode_video_frame_scan(
877 struct anim *anim, int64_t pts_to_search)
879 /* there seem to exist *very* silly GOP lengths out in the wild... */
882 av_log(anim->pFormatCtx,
884 "SCAN start: considering pts=%lld in search of %lld\n",
885 (long long int)anim->next_pts, (long long int)pts_to_search);
887 while (count > 0 && anim->next_pts < pts_to_search) {
888 av_log(anim->pFormatCtx,
890 " WHILE: pts=%lld in search of %lld\n",
891 (long long int)anim->next_pts, (long long int)pts_to_search);
892 if (!ffmpeg_decode_video_frame(anim)) {
898 av_log(anim->pFormatCtx,
900 "SCAN failed: completely lost in stream, "
901 "bailing out at PTS=%lld, searching for PTS=%lld\n",
902 (long long int)anim->next_pts, (long long int)pts_to_search);
904 if (anim->next_pts == pts_to_search) {
905 av_log(anim->pFormatCtx,
906 AV_LOG_DEBUG, "SCAN HAPPY: we found our PTS!\n");
909 av_log(anim->pFormatCtx,
910 AV_LOG_ERROR, "SCAN UNHAPPY: PTS not matched!\n");
914 static int match_format(const char *name, AVFormatContext *pFormatCtx)
919 const char *names = pFormatCtx->iformat->name;
924 namelen = strlen(name);
925 while ((p = strchr(names, ','))) {
926 len = MAX2(p - names, namelen);
927 if (!BLI_strncasecmp(name, names, len))
931 return !BLI_strcasecmp(name, names);
934 static int ffmpeg_seek_by_byte(AVFormatContext *pFormatCtx)
936 static const char *byte_seek_list[] = { "mpegts", 0 };
939 if (pFormatCtx->iformat->flags & AVFMT_TS_DISCONT) {
946 if (match_format(*p++, pFormatCtx)) {
954 static ImBuf *ffmpeg_fetchibuf(struct anim *anim, int position,
955 IMB_Timecode_Type tc)
957 int64_t pts_to_search = 0;
959 double pts_time_base;
961 struct anim_index *tc_index = 0;
963 int new_frame_index = 0; /* To quiet gcc barking... */
964 int old_frame_index = 0; /* To quiet gcc barking... */
966 if (anim == NULL) return (0);
968 av_log(anim->pFormatCtx, AV_LOG_DEBUG, "FETCH: pos=%d\n", position);
970 if (tc != IMB_TC_NONE) {
971 tc_index = IMB_anim_open_index(anim, tc);
974 v_st = anim->pFormatCtx->streams[anim->videoStream];
976 frame_rate = av_q2d(av_get_r_frame_rate_compat(v_st));
978 st_time = anim->pFormatCtx->start_time;
979 pts_time_base = av_q2d(v_st->time_base);
982 new_frame_index = IMB_indexer_get_frame_index(
984 old_frame_index = IMB_indexer_get_frame_index(
985 tc_index, anim->curposition);
986 pts_to_search = IMB_indexer_get_pts(
987 tc_index, new_frame_index);
990 pts_to_search = (long long)
991 floor(((double) position) /
992 pts_time_base / frame_rate + 0.5);
994 if (st_time != AV_NOPTS_VALUE) {
995 pts_to_search += st_time / pts_time_base / AV_TIME_BASE;
999 av_log(anim->pFormatCtx, AV_LOG_DEBUG,
1000 "FETCH: looking for PTS=%lld "
1001 "(pts_timebase=%g, frame_rate=%g, st_time=%lld)\n",
1002 (long long int)pts_to_search, pts_time_base, frame_rate, st_time);
1004 if (anim->last_frame &&
1005 anim->last_pts <= pts_to_search && anim->next_pts > pts_to_search)
1007 av_log(anim->pFormatCtx, AV_LOG_DEBUG,
1008 "FETCH: frame repeat: last: %lld next: %lld\n",
1009 (long long int)anim->last_pts,
1010 (long long int)anim->next_pts);
1011 IMB_refImBuf(anim->last_frame);
1012 anim->curposition = position;
1013 return anim->last_frame;
1016 if (position > anim->curposition + 1 &&
1019 position - (anim->curposition + 1) < anim->preseek)
1021 av_log(anim->pFormatCtx, AV_LOG_DEBUG,
1022 "FETCH: within preseek interval (no index)\n");
1024 ffmpeg_decode_video_frame_scan(anim, pts_to_search);
1026 else if (tc_index &&
1027 IMB_indexer_can_scan(tc_index, old_frame_index,
1030 av_log(anim->pFormatCtx, AV_LOG_DEBUG,
1031 "FETCH: within preseek interval "
1032 "(index tells us)\n");
1034 ffmpeg_decode_video_frame_scan(anim, pts_to_search);
1036 else if (position != anim->curposition + 1) {
1041 unsigned long long dts;
1043 pos = IMB_indexer_get_seek_pos(
1044 tc_index, new_frame_index);
1045 dts = IMB_indexer_get_seek_pos_dts(
1046 tc_index, new_frame_index);
1048 av_log(anim->pFormatCtx, AV_LOG_DEBUG,
1049 "TC INDEX seek pos = %lld\n", pos);
1050 av_log(anim->pFormatCtx, AV_LOG_DEBUG,
1051 "TC INDEX seek dts = %lld\n", dts);
1053 if (ffmpeg_seek_by_byte(anim->pFormatCtx)) {
1054 av_log(anim->pFormatCtx, AV_LOG_DEBUG,
1055 "... using BYTE pos\n");
1057 ret = av_seek_frame(anim->pFormatCtx,
1059 pos, AVSEEK_FLAG_BYTE);
1060 av_update_cur_dts(anim->pFormatCtx, v_st, dts);
1063 av_log(anim->pFormatCtx, AV_LOG_DEBUG,
1064 "... using DTS pos\n");
1065 ret = av_seek_frame(anim->pFormatCtx,
1067 dts, AVSEEK_FLAG_BACKWARD);
1071 pos = (long long) (position - anim->preseek) *
1072 AV_TIME_BASE / frame_rate;
1074 av_log(anim->pFormatCtx, AV_LOG_DEBUG,
1075 "NO INDEX seek pos = %lld, st_time = %lld\n",
1076 pos, (st_time != AV_NOPTS_VALUE) ? st_time : 0);
1082 if (st_time != AV_NOPTS_VALUE) {
1086 av_log(anim->pFormatCtx, AV_LOG_DEBUG,
1087 "NO INDEX final seek pos = %lld\n", pos);
1089 ret = av_seek_frame(anim->pFormatCtx, -1,
1090 pos, AVSEEK_FLAG_BACKWARD);
1094 av_log(anim->pFormatCtx, AV_LOG_ERROR,
1096 "error while seeking to DTS = %lld "
1097 "(frameno = %d, PTS = %lld): errcode = %d\n",
1098 pos, position, (long long int)pts_to_search, ret);
1101 avcodec_flush_buffers(anim->pCodecCtx);
1103 anim->next_pts = -1;
1105 if (anim->next_packet.stream_index == anim->videoStream) {
1106 av_free_packet(&anim->next_packet);
1107 anim->next_packet.stream_index = -1;
1110 /* memset(anim->pFrame, ...) ?? */
1113 ffmpeg_decode_video_frame_scan(anim, pts_to_search);
1116 else if (position == 0 && anim->curposition == -1) {
1117 /* first frame without seeking special case... */
1118 ffmpeg_decode_video_frame(anim);
1121 av_log(anim->pFormatCtx, AV_LOG_DEBUG,
1122 "FETCH: no seek necessary, just continue...\n");
1125 IMB_freeImBuf(anim->last_frame);
1126 anim->last_frame = IMB_allocImBuf(anim->x, anim->y, 32, IB_rect);
1127 anim->last_frame->rect_colorspace = colormanage_colorspace_get_named(anim->colorspace);
1129 ffmpeg_postprocess(anim);
1131 anim->last_pts = anim->next_pts;
1133 ffmpeg_decode_video_frame(anim);
1135 anim->curposition = position;
1137 IMB_refImBuf(anim->last_frame);
1139 return anim->last_frame;
1142 static void free_anim_ffmpeg(struct anim *anim)
1144 if (anim == NULL) return;
1146 if (anim->pCodecCtx) {
1147 avcodec_close(anim->pCodecCtx);
1148 avformat_close_input(&anim->pFormatCtx);
1149 av_free(anim->pFrameRGB);
1150 av_free(anim->pFrame);
1152 if (anim->ib_flags & IB_animdeinterlace) {
1153 MEM_freeN(anim->pFrameDeinterlaced->data[0]);
1155 av_free(anim->pFrameDeinterlaced);
1156 sws_freeContext(anim->img_convert_ctx);
1157 IMB_freeImBuf(anim->last_frame);
1158 if (anim->next_packet.stream_index != -1) {
1159 av_free_packet(&anim->next_packet);
1169 static int startredcode(struct anim *anim)
1171 anim->redcodeCtx = redcode_open(anim->name);
1172 if (!anim->redcodeCtx) {
1175 anim->duration = redcode_get_length(anim->redcodeCtx);
1180 static ImBuf *redcode_fetchibuf(struct anim *anim, int position)
1183 struct redcode_frame *frame;
1184 struct redcode_frame_raw *raw_frame;
1186 if (!anim->redcodeCtx) {
1190 frame = redcode_read_video_frame(anim->redcodeCtx, position);
1196 raw_frame = redcode_decode_video_raw(frame, 1);
1198 redcode_free_frame(frame);
1204 ibuf = IMB_allocImBuf(raw_frame->width * 2,
1205 raw_frame->height * 2, 32, IB_rectfloat);
1207 redcode_decode_video_float(raw_frame, ibuf->rect_float, 1);
1212 static void free_anim_redcode(struct anim *anim)
1214 if (anim->redcodeCtx) {
1215 redcode_close(anim->redcodeCtx);
1216 anim->redcodeCtx = 0;
1223 /* Try next picture to read */
1224 /* No picture, try to open next animation */
1225 /* Succeed, remove first image from animation */
1227 static ImBuf *anim_getnew(struct anim *anim)
1229 struct ImBuf *ibuf = NULL;
1231 if (anim == NULL) return(NULL);
1233 free_anim_movie(anim);
1236 free_anim_avi(anim);
1239 #ifdef WITH_QUICKTIME
1240 free_anim_quicktime(anim);
1243 free_anim_ffmpeg(anim);
1246 free_anim_redcode(anim);
1250 if (anim->curtype != 0) return (NULL);
1251 anim->curtype = imb_get_anim_type(anim->name);
1253 switch (anim->curtype) {
1255 ibuf = IMB_loadiffname(anim->name, anim->ib_flags, anim->colorspace);
1257 BLI_strncpy(anim->first, anim->name, sizeof(anim->first));
1262 if (startmovie(anim)) return (NULL);
1263 ibuf = IMB_allocImBuf(anim->x, anim->y, 24, 0); /* fake */
1267 if (startavi(anim)) {
1268 printf("couldnt start avi\n");
1271 ibuf = IMB_allocImBuf(anim->x, anim->y, 24, 0);
1274 #ifdef WITH_QUICKTIME
1276 if (startquicktime(anim)) return (0);
1277 ibuf = IMB_allocImBuf(anim->x, anim->y, 24, 0);
1282 if (startffmpeg(anim)) return (0);
1283 ibuf = IMB_allocImBuf(anim->x, anim->y, 24, 0);
1288 if (startredcode(anim)) return (0);
1289 ibuf = IMB_allocImBuf(8, 8, 32, 0);
1296 struct ImBuf *IMB_anim_previewframe(struct anim *anim)
1298 struct ImBuf *ibuf = NULL;
1301 ibuf = IMB_anim_absolute(anim, 0, IMB_TC_NONE, IMB_PROXY_NONE);
1303 IMB_freeImBuf(ibuf);
1304 position = anim->duration / 2;
1305 ibuf = IMB_anim_absolute(anim, position, IMB_TC_NONE,
1311 struct ImBuf *IMB_anim_absolute(struct anim *anim, int position,
1312 IMB_Timecode_Type tc,
1313 IMB_Proxy_Size preview_size)
1315 struct ImBuf *ibuf = NULL;
1316 char head[256], tail[256];
1317 unsigned short digits;
1320 if (anim == NULL) return(NULL);
1322 filter_y = (anim->ib_flags & IB_animdeinterlace);
1324 if (anim->curtype == 0) {
1325 ibuf = anim_getnew(anim);
1330 IMB_freeImBuf(ibuf); /* ???? */
1334 if (position < 0) return(NULL);
1335 if (position >= anim->duration) return(NULL);
1337 if (preview_size != IMB_PROXY_NONE) {
1338 struct anim *proxy = IMB_anim_open_proxy(anim, preview_size);
1341 position = IMB_anim_index_get_frame_index(
1342 anim, tc, position);
1343 return IMB_anim_absolute(
1345 IMB_TC_NONE, IMB_PROXY_NONE);
1349 switch (anim->curtype) {
1351 pic = an_stringdec(anim->first, head, tail, &digits);
1353 an_stringenc(anim->name, head, tail, digits, pic);
1354 ibuf = IMB_loadiffname(anim->name, IB_rect, anim->colorspace);
1356 anim->curposition = position;
1360 ibuf = movie_fetchibuf(anim, position);
1362 anim->curposition = position;
1363 IMB_convert_rgba_to_abgr(ibuf);
1368 ibuf = avi_fetchibuf(anim, position);
1370 anim->curposition = position;
1373 #ifdef WITH_QUICKTIME
1375 ibuf = qtime_fetchibuf(anim, position);
1378 /* OCIO_TODO: should happen in quicktime module, but it currently doesn't have access
1379 * to color management's internals
1381 ibuf->rect_colorspace = colormanage_colorspace_get_named(anim->colorspace);
1384 anim->curposition = position;
1390 ibuf = ffmpeg_fetchibuf(anim, position, tc);
1392 anim->curposition = position;
1393 filter_y = 0; /* done internally */
1398 ibuf = redcode_fetchibuf(anim, position);
1399 if (ibuf) anim->curposition = position;
1405 if (filter_y) IMB_filtery(ibuf);
1406 BLI_snprintf(ibuf->name, sizeof(ibuf->name), "%s.%04d", anim->name, anim->curposition + 1);
1414 int IMB_anim_get_duration(struct anim *anim, IMB_Timecode_Type tc)
1416 struct anim_index *idx;
1417 if (tc == IMB_TC_NONE) {
1418 return anim->duration;
1421 idx = IMB_anim_open_index(anim, tc);
1423 return anim->duration;
1426 return IMB_indexer_get_duration(idx);
1429 int IMB_anim_get_fps(struct anim *anim,
1430 short *frs_sec, float *frs_sec_base)
1432 if (anim->frs_sec) {
1433 *frs_sec = anim->frs_sec;
1434 *frs_sec_base = anim->frs_sec_base;
1440 void IMB_anim_set_preseek(struct anim *anim, int preseek)
1442 anim->preseek = preseek;
1445 int IMB_anim_get_preseek(struct anim *anim)
1447 return anim->preseek;