X-Git-Url: http://git.cinelerra-gg.org/git/?a=blobdiff_plain;f=cinelerra-5.1%2Fcinelerra%2Fffmpeg.C;h=2d441a79f6c2bcb244362d024305140cb61c6723;hb=17304a9fc042d480cca91f04ace7985f98fa28d8;hp=7dca62a696defe3282d7494aaa1c29e15dfc41d0;hpb=2441cb0cb7f655fad2f76c982f2ac93897d09dbe;p=goodguy%2Fcinelerra.git diff --git a/cinelerra-5.1/cinelerra/ffmpeg.C b/cinelerra-5.1/cinelerra/ffmpeg.C index 7dca62a6..2d441a79 100644 --- a/cinelerra-5.1/cinelerra/ffmpeg.C +++ b/cinelerra-5.1/cinelerra/ffmpeg.C @@ -29,7 +29,9 @@ #include "ffmpeg.h" #include "indexfile.h" #include "interlacemodes.h" +#ifdef HAVE_DV #include "libdv.h" +#endif #include "libmjpeg.h" #include "mainerror.h" #include "mwindow.h" @@ -378,8 +380,11 @@ AVHWDeviceType FFStream::decode_hw_activate() { return AV_HWDEVICE_TYPE_NONE; } - +#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(59,16,100) +int FFStream::decode_hw_format(const AVCodec *decoder, AVHWDeviceType type) +#else int FFStream::decode_hw_format(AVCodec *decoder, AVHWDeviceType type) +#endif { return 0; } @@ -404,7 +409,11 @@ int FFStream::decode_activate() } while( ret >= 0 && st != 0 && !reading ) { AVCodecID codec_id = st->codecpar->codec_id; +#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(59,16,100) + const AVCodec *decoder = 0; +#else AVCodec *decoder = 0; +#endif if( is_video() ) { if( ffmpeg->opt_video_decoder ) decoder = avcodec_find_decoder_by_name(ffmpeg->opt_video_decoder); @@ -603,6 +612,7 @@ int FFStream::encode_frame(AVFrame *frame) ret = avcodec_send_frame(avctx, frame); if( !ret && frame ) return pkts; if( ret < 0 && ret != AVERROR(EAGAIN) ) break; + if ( ret == AVERROR(EAGAIN) && !frame ) continue; FFPacket opkt; ret = avcodec_receive_packet(avctx, opkt); if( !frame && ret == AVERROR_EOF ) return pkts; @@ -719,10 +729,19 @@ int FFStream::seek(int64_t no, double rate) double secs = pos < 0 ? 0. : pos / rate; AVRational time_base = st->time_base; int64_t tstmp = time_base.num > 0 ? secs * time_base.den/time_base.num : 0; +#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(59,16,100) + int nb_index_entries = avformat_index_get_entries_count(st); +#endif if( !tstmp ) { +#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(59,16,100) + if( nb_index_entries > 0 ) tstmp = (avformat_index_get_entry(st, 0))->timestamp; +#else if( st->nb_index_entries > 0 ) tstmp = st->index_entries[0].timestamp; +#endif else if( st->start_time != AV_NOPTS_VALUE ) tstmp = st->start_time; +#if LIBAVCODEC_VERSION_INT <= AV_VERSION_INT(58,134,100) else if( st->first_dts != AV_NOPTS_VALUE ) tstmp = st->first_dts; +#endif else tstmp = INT64_MIN+1; } else if( nudge != AV_NOPTS_VALUE ) tstmp += nudge; @@ -1090,8 +1109,11 @@ AVHWDeviceType FFVideoStream::decode_hw_activate() } return type; } - +#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(59,16,100) +int FFVideoStream::decode_hw_format(const AVCodec *decoder, AVHWDeviceType type) +#else int FFVideoStream::decode_hw_format(AVCodec *decoder, AVHWDeviceType type) +#endif { int ret = 0; hw_pix_fmt = AV_PIX_FMT_NONE; @@ -1213,6 +1235,40 @@ int FFVideoStream::decode_frame(AVFrame *frame) return 1; } +int FFVideoStream::probe(int64_t pos) +{ + int ret = video_seek(pos); + if( ret < 0 ) return -1; + if( !frame && !(frame=av_frame_alloc()) ) { + fprintf(stderr, "FFVideoStream::probe: av_frame_alloc failed\n"); + return -1; + } + + if (ffmpeg->interlace_from_codec) return 1; + + ret = read_frame(frame); + if( ret > 0 ) { + //printf("codec interlace: %i \n",frame->interlaced_frame); + //printf("codec tff: %i \n",frame->top_field_first); + + if (!frame->interlaced_frame) + ffmpeg->interlace_from_codec = AV_FIELD_PROGRESSIVE; + if ((frame->interlaced_frame) && (frame->top_field_first)) + ffmpeg->interlace_from_codec = AV_FIELD_TT; + if ((frame->interlaced_frame) && (!frame->top_field_first)) + ffmpeg->interlace_from_codec = AV_FIELD_BB; + //printf("Interlace mode from codec: %i\n", ffmpeg->interlace_from_codec); + + } + + if( frame->format == AV_PIX_FMT_NONE || frame->width <= 0 || frame->height <= 0 ) + ret = -1; + + ret = ret > 0 ? 1 : ret < 0 ? -1 : 0; + av_frame_free(&frame); + return ret; +} + int FFVideoStream::load(VFrame *vframe, int64_t pos) { int ret = video_seek(pos); @@ -1221,7 +1277,10 @@ int FFVideoStream::load(VFrame *vframe, int64_t pos) fprintf(stderr, "FFVideoStream::load: av_frame_alloc failed\n"); return -1; } + + int i = MAX_RETRY + pos - curr_pos; + int64_t cache_start = 0; while( ret>=0 && !flushed && curr_pos<=pos && --i>=0 ) { ret = read_frame(frame); if( ret > 0 ) { @@ -1230,15 +1289,19 @@ int FFVideoStream::load(VFrame *vframe, int64_t pos) if( use_cache < 0 ) { // for reverse read, reload file frame_cache from keyframe to pos ffmpeg->purge_cache(); + int count = preferences->cache_size / + vframe->get_data_size() / 2; // try to burn only 1/2 of cache + cache_start = pos - count + 1; seeking = 1; } else seeking = 0; } - if( seeking > 0 && curr_pos < pos ) { + if( seeking > 0 && curr_pos >= cache_start && curr_pos < pos ) { int vw =vframe->get_w(), vh = vframe->get_h(); int vcolor_model = vframe->get_color_model(); - VFrame *cache_frame = new VFrame(vw, vh, vcolor_model); +// do not use shm here, puts too much pressure on 32bit systems + VFrame *cache_frame = new VFrame(vw, vh, vcolor_model, 0); ret = convert_cmodel(cache_frame, frame); if( ret > 0 ) ffmpeg->put_cache_frame(cache_frame, curr_pos); @@ -1386,6 +1449,7 @@ int FFVideoStream::encode(VFrame *vframe) int FFVideoStream::drain() { + return 0; } @@ -1548,9 +1612,11 @@ int FFVideoConvert::convert_picture_vframe(VFrame *frame, AVFrame *ip, AVFrame * } int color_space = SWS_CS_ITU601; switch( preferences->yuv_color_space ) { - case BC_COLORS_BT601: color_space = SWS_CS_ITU601; break; + case BC_COLORS_BT601_PAL: color_space = SWS_CS_ITU601; break; + case BC_COLORS_BT601_NTSC: color_space = SWS_CS_SMPTE170M; break; case BC_COLORS_BT709: color_space = SWS_CS_ITU709; break; - case BC_COLORS_BT2020: color_space = SWS_CS_BT2020; break; + case BC_COLORS_BT2020_NCL: + case BC_COLORS_BT2020_CL: color_space = SWS_CS_BT2020; break; } const int *color_table = sws_getCoefficients(color_space); @@ -1677,9 +1743,11 @@ int FFVideoConvert::convert_vframe_picture(VFrame *frame, AVFrame *op, AVFrame * } int color_space = SWS_CS_ITU601; switch( preferences->yuv_color_space ) { - case BC_COLORS_BT601: color_space = SWS_CS_ITU601; break; + case BC_COLORS_BT601_PAL: color_space = SWS_CS_ITU601; break; + case BC_COLORS_BT601_NTSC: color_space = SWS_CS_SMPTE170M; break; case BC_COLORS_BT709: color_space = SWS_CS_ITU709; break; - case BC_COLORS_BT2020: color_space = SWS_CS_BT2020; break; + case BC_COLORS_BT2020_NCL: + case BC_COLORS_BT2020_CL: color_space = SWS_CS_BT2020; break; } const int *color_table = sws_getCoefficients(color_space); @@ -1771,6 +1839,7 @@ FFMPEG::FFMPEG(FileBase *file_base) flow = 1; decoding = encoding = 0; has_audio = has_video = 0; + interlace_from_codec = 0; opts = 0; opt_duration = -1; opt_video_filter = 0; @@ -1800,7 +1869,11 @@ FFMPEG::~FFMPEG() delete [] opt_hw_dev; } +#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(59,16,100) +int FFMPEG::check_sample_rate(const AVCodec *codec, int sample_rate) +#else int FFMPEG::check_sample_rate(AVCodec *codec, int sample_rate) +#endif { const int *p = codec->supported_samplerates; if( !p ) return sample_rate; @@ -1811,12 +1884,15 @@ int FFMPEG::check_sample_rate(AVCodec *codec, int sample_rate) return 0; } +// check_frame_rate and std_frame_rate needed for 23.976 +// and 59.94 fps mpeg2 static inline AVRational std_frame_rate(int i) { static const int m1 = 1001*12, m2 = 1000*12; static const int freqs[] = { 40*m1, 48*m1, 50*m1, 60*m1, 80*m1,120*m1, 240*m1, - 24*m2, 30*m2, 60*m2, 12*m2, 15*m2, 48*m2, 0, + 24*m2, 30*m2, 60*m2, 12*m2, 15*m2, 48*m2, 90*m2, + 100*m2, 120*m2, 144*m2, 72*m2, 0, }; int freq = i<30*12 ? (i+1)*1001 : freqs[i-30*12]; return (AVRational) { freq, 1001*12 }; @@ -2047,7 +2123,11 @@ void FFMPEG::scan_audio_options(Asset *asset, EDL *edl) cin_fmt = (int)av_get_sample_fmt(cin_sample_fmt); if( cin_fmt < 0 ) { char audio_codec[BCSTRLEN]; audio_codec[0] = 0; +#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(59,18,100) + const AVCodec *av_codec = !FFMPEG::get_codec(audio_codec, "audio", asset->acodec) ? +#else AVCodec *av_codec = !FFMPEG::get_codec(audio_codec, "audio", asset->acodec) ? +#endif avcodec_find_encoder_by_name(audio_codec) : 0; if( av_codec && av_codec->sample_fmts ) cin_fmt = find_best_sample_fmt_of_list(av_codec->sample_fmts, AV_SAMPLE_FMT_FLT); @@ -2083,7 +2163,11 @@ void FFMPEG::scan_video_options(Asset *asset, EDL *edl) cin_fmt = (int)av_get_pix_fmt(cin_pix_fmt); if( cin_fmt < 0 ) { char video_codec[BCSTRLEN]; video_codec[0] = 0; +#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(59,18,100) + const AVCodec *av_codec = !get_codec(video_codec, "video", asset->vcodec) ? +#else AVCodec *av_codec = !get_codec(video_codec, "video", asset->vcodec) ? +#endif avcodec_find_encoder_by_name(video_codec) : 0; if( av_codec && av_codec->pix_fmts ) { if( 0 && edl ) { // frequently picks a bad answer @@ -2277,10 +2361,15 @@ int FFCodecRemaps::add(const char *val) return 0; } - +#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(59,16,100) +int FFCodecRemaps::update(AVCodecID &codec_id, const AVCodec *&decoder) +{ + const AVCodec *codec = avcodec_find_decoder(codec_id); +#else int FFCodecRemaps::update(AVCodecID &codec_id, AVCodec *&decoder) { AVCodec *codec = avcodec_find_decoder(codec_id); +#endif if( !codec ) return -1; const char *name = codec->name; FFCodecRemaps &map = *this; @@ -2406,6 +2495,10 @@ int FFMPEG::info(char *text, int len) AVPixelFormat pix_fmt = (AVPixelFormat)st->codecpar->format; const char *pfn = av_get_pix_fmt_name(pix_fmt); report(" pix %s\n", pfn ? pfn : unkn); + int interlace = st->codecpar->field_order; + report(" interlace (container level): %i\n", interlace ? interlace : -1); + int interlace_codec = interlace_from_codec; + report(" interlace (codec level): %i\n", interlace_codec ? interlace_codec : -1); enum AVColorSpace space = st->codecpar->color_space; const char *nm = av_color_space_name(space); report(" color space:%s", nm ? nm : unkn); @@ -2496,7 +2589,11 @@ int FFMPEG::init_decoder(const char *filename) char *sp = strrchr(bp, '.'); if( !sp ) sp = bp + strlen(bp); FILE *fp = 0; +#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(59,18,100) + const AVInputFormat *ifmt = 0; +#else AVInputFormat *ifmt = 0; +#endif if( sp ) { strcpy(sp, ".opts"); fp = fopen(file_opts, "r"); @@ -2589,18 +2686,22 @@ int FFMPEG::open_decoder() } switch( avpar->color_space ) { case AVCOL_SPC_BT470BG: + vid->color_space = BC_COLORS_BT601_PAL; + break; case AVCOL_SPC_SMPTE170M: - vid->color_space = BC_COLORS_BT601; + vid->color_space = BC_COLORS_BT601_NTSC; break; case AVCOL_SPC_BT709: vid->color_space = BC_COLORS_BT709; break; case AVCOL_SPC_BT2020_NCL: + vid->color_space = BC_COLORS_BT2020_NCL; + break; case AVCOL_SPC_BT2020_CL: - vid->color_space = BC_COLORS_BT2020; + vid->color_space = BC_COLORS_BT2020_CL; break; default: - vid->color_space = !file_base ? BC_COLORS_BT601 : + vid->color_space = !file_base ? BC_COLORS_BT601_NTSC : file_base->file->preferences->yuv_color_space; break; } @@ -2713,7 +2814,11 @@ int FFMPEG::open_encoder(const char *type, const char *spec) AVCodecContext *ctx = 0; const AVCodecDescriptor *codec_desc = 0; +#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(59,16,100) + const AVCodec *codec = avcodec_find_encoder_by_name(codec_name); +#else AVCodec *codec = avcodec_find_encoder_by_name(codec_name); +#endif if( !codec ) { eprintf(_("cant find codec %s:%s\n"), codec_name, filename); ret = 1; @@ -2843,9 +2948,11 @@ int FFMPEG::open_encoder(const char *type, const char *spec) if( (vid->color_space = asset->ff_color_space) < 0 ) vid->color_space = file_base->file->preferences->yuv_color_space; switch( vid->color_space ) { - case BC_COLORS_BT601: ctx->colorspace = AVCOL_SPC_SMPTE170M; break; + case BC_COLORS_BT601_NTSC: ctx->colorspace = AVCOL_SPC_SMPTE170M; break; + case BC_COLORS_BT601_PAL: ctx->colorspace = AVCOL_SPC_BT470BG; break; case BC_COLORS_BT709: ctx->colorspace = AVCOL_SPC_BT709; break; - case BC_COLORS_BT2020: ctx->colorspace = AVCOL_SPC_BT2020_NCL; break; + case BC_COLORS_BT2020_NCL: ctx->colorspace = AVCOL_SPC_BT2020_NCL; break; + case BC_COLORS_BT2020_CL: ctx->colorspace = AVCOL_SPC_BT2020_CL; break; } AVPixelFormat pix_fmt = av_get_pix_fmt(asset->ff_pixel_format); if( opt_hw_dev != 0 ) { @@ -2868,7 +2975,12 @@ int FFMPEG::open_encoder(const char *type, const char *spec) int mask_h = (1<log2_chroma_h)-1; ctx->height = (vid->height+mask_h) & ~mask_h; ctx->sample_aspect_ratio = to_sample_aspect_ratio(asset); - AVRational frame_rate = check_frame_rate(codec->supported_framerates, vid->frame_rate); + AVRational frame_rate; + if (ctx->codec->id == AV_CODEC_ID_MPEG1VIDEO || + ctx->codec->id == AV_CODEC_ID_MPEG2VIDEO) + frame_rate = check_frame_rate(codec->supported_framerates, vid->frame_rate); + else + frame_rate = av_d2q(vid->frame_rate, INT_MAX); if( !frame_rate.num || !frame_rate.den ) { eprintf(_("check_frame_rate failed %s\n"), filename); ret = 1; @@ -2884,6 +2996,25 @@ int FFMPEG::open_encoder(const char *type, const char *spec) vid->interlaced = asset->interlace_mode == ILACE_MODE_TOP_FIRST || asset->interlace_mode == ILACE_MODE_BOTTOM_FIRST ? 1 : 0; vid->top_field_first = asset->interlace_mode == ILACE_MODE_TOP_FIRST ? 1 : 0; + switch (asset->interlace_mode) { + case ILACE_MODE_TOP_FIRST: + if (ctx->codec->id == AV_CODEC_ID_MJPEG) + av_dict_set(&sopts, "field_order", "tt", 0); + else + av_dict_set(&sopts, "field_order", "tb", 0); + if (ctx->codec_id != AV_CODEC_ID_MJPEG) + av_dict_set(&sopts, "flags", "+ilme+ildct", 0); + break; + case ILACE_MODE_BOTTOM_FIRST: + if (ctx->codec->id == AV_CODEC_ID_MJPEG) + av_dict_set(&sopts, "field_order", "bb", 0); + else + av_dict_set(&sopts, "field_order", "bt", 0); + if (ctx->codec_id != AV_CODEC_ID_MJPEG) + av_dict_set(&sopts, "flags", "+ilme+ildct", 0); + break; + case ILACE_MODE_NOTINTERLACED: av_dict_set(&sopts, "field_order", "progressive", 0); break; + } break; } default: eprintf(_("not audio/video, %s:%s\n"), codec_name, filename); @@ -2940,7 +3071,11 @@ int FFMPEG::open_encoder(const char *type, const char *spec) } if( ret >= 0 ) { _Pragma("GCC diagnostic ignored \"-Wdeprecated-declarations\"") +#if LIBAVCODEC_VERSION_INT <= AV_VERSION_INT(58,134,100) ret = avcodec_copy_context(st->codec, ctx); +#else + ret = avcodec_parameters_to_context(ctx, st->codecpar); +#endif _Pragma("GCC diagnostic warning \"-Wdeprecated-declarations\"") if( ret < 0 ) fprintf(stderr, "Could not copy the stream context\n"); @@ -3176,6 +3311,33 @@ int FFMPEG::audio_seek(int stream, int64_t pos) return 0; } +int FFMPEG::video_probe(int64_t pos) +{ + int vidx = vstrm_index[0].st_idx; + FFVideoStream *vid = ffvideo[vidx]; + vid->probe(pos); + + int interlace1 = interlace_from_codec; + //printf("interlace from codec: %i\n", interlace1); + + switch (interlace1) + { + case AV_FIELD_TT: + case AV_FIELD_TB: + return ILACE_MODE_TOP_FIRST; + case AV_FIELD_BB: + case AV_FIELD_BT: + return ILACE_MODE_BOTTOM_FIRST; + case AV_FIELD_PROGRESSIVE: + return ILACE_MODE_NOTINTERLACED; + default: + return ILACE_MODE_UNDETECTED; + } + +} + + + int FFMPEG::video_seek(int stream, int64_t pos) { int vidx = vstrm_index[stream].st_idx; @@ -3463,7 +3625,20 @@ int FFMPEG::ff_coded_height(int stream) float FFMPEG::ff_aspect_ratio(int stream) { - return ffvideo[stream]->aspect_ratio; + //return ffvideo[stream]->aspect_ratio; + AVFormatContext *fmt_ctx = ffvideo[stream]->fmt_ctx; + AVStream *strm = ffvideo[stream]->st; + AVCodecParameters *par = ffvideo[stream]->st->codecpar; + AVRational dar; + AVRational sar = av_guess_sample_aspect_ratio(fmt_ctx, strm, NULL); + if (sar.num) { + av_reduce(&dar.num, &dar.den, + par->width * sar.num, + par->height * sar.den, + 1024*1024); + return av_q2d(dar); + } + return ffvideo[stream]->aspect_ratio; } const char* FFMPEG::ff_video_codec(int stream) @@ -3504,6 +3679,30 @@ int FFMPEG::ff_video_mpeg_color_range(int stream) return ffvideo[stream]->st->codecpar->color_range == AVCOL_RANGE_MPEG ? 1 : 0; } +int FFMPEG::ff_interlace(int stream) +{ +// https://ffmpeg.org/doxygen/trunk/structAVCodecParserContext.html +/* reads from demuxer because codec frame not ready */ + int interlace0 = ffvideo[stream]->st->codecpar->field_order; + + switch (interlace0) + { + case AV_FIELD_TT: + case AV_FIELD_TB: + return ILACE_MODE_TOP_FIRST; + case AV_FIELD_BB: + case AV_FIELD_BT: + return ILACE_MODE_BOTTOM_FIRST; + case AV_FIELD_PROGRESSIVE: + return ILACE_MODE_NOTINTERLACED; + default: + return ILACE_MODE_UNDETECTED; + } + +} + + + int FFMPEG::ff_cpus() { return !file_base ? 1 : file_base->file->cpus; @@ -3521,7 +3720,11 @@ Preferences *FFMPEG::ff_prefs() double FFVideoStream::get_rotation_angle() { +#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(59,16,100) + size_t size = 0; +#else int size = 0; +#endif int *matrix = (int*)av_stream_get_side_data(st, AV_PKT_DATA_DISPLAYMATRIX, &size); int len = size/sizeof(*matrix); if( !matrix || len < 5 ) return 0; @@ -3746,7 +3949,11 @@ AVCodecContext *FFMPEG::activate_decoder(AVStream *st) AVDictionary *copts = 0; av_dict_copy(&copts, opts, 0); AVCodecID codec_id = st->codecpar->codec_id; +#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(59,16,100) + const AVCodec *decoder = 0; +#else AVCodec *decoder = 0; +#endif switch( st->codecpar->codec_type ) { case AVMEDIA_TYPE_VIDEO: if( opt_video_decoder ) @@ -3938,10 +4145,20 @@ void FFStream::load_markers(IndexMarks &marks, double rate) int in = 0; int64_t sz = marks.size(); int max_entries = fmt_ctx->max_index_size / sizeof(AVIndexEntry) - 1; +#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(59,16,100) + int nb_ent = avformat_index_get_entries_count(st); +#endif +#if LIBAVCODEC_VERSION_INT <= AV_VERSION_INT(58,134,100) int nb_ent = st->nb_index_entries; +#endif // some formats already have an index if( nb_ent > 0 ) { +#if LIBAVCODEC_VERSION_INT <= AV_VERSION_INT(58,134,100) AVIndexEntry *ep = &st->index_entries[nb_ent-1]; +#endif +#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(59,16,100) + const AVIndexEntry *ep = avformat_index_get_entry(st, nb_ent-1); +#endif int64_t tstmp = ep->timestamp; if( nudge != AV_NOPTS_VALUE ) tstmp -= nudge; double secs = ffmpeg->to_secs(tstmp, st->time_base);