X-Git-Url: http://git.cinelerra-gg.org/git/?p=goodguy%2Fhistory.git;a=blobdiff_plain;f=cinelerra-5.1%2Fcinelerra%2Fffmpeg.C;h=2845788dc626178e0663e6dc7ddeb36625af0c9e;hp=4239bc5d203329bb8a852fb24445871d78b85449;hb=d60a59baa6cfe24c0fb153ed9e150a834ba29feb;hpb=6914ec9bd690023a5f014b54d064bbba58981982 diff --git a/cinelerra-5.1/cinelerra/ffmpeg.C b/cinelerra-5.1/cinelerra/ffmpeg.C index 4239bc5d..2845788d 100644 --- a/cinelerra-5.1/cinelerra/ffmpeg.C +++ b/cinelerra-5.1/cinelerra/ffmpeg.C @@ -7,6 +7,8 @@ #include #include #include +#include + // work arounds (centos) #include #ifndef INT64_MAX @@ -17,8 +19,11 @@ #include "asset.h" #include "bccmodels.h" #include "bchash.h" -#include "fileffmpeg.h" +#include "edl.h" +#include "edlsession.h" #include "file.h" +#include "fileffmpeg.h" +#include "filesystem.h" #include "ffmpeg.h" #include "indexfile.h" #include "interlacemodes.h" @@ -28,11 +33,18 @@ #include "mwindow.h" #include "vframe.h" +#ifdef FFMPEG3 +#define url filename +#else +#define av_register_all(s) +#define avfilter_register_all(s) +#endif #define VIDEO_INBUF_SIZE 0x10000 #define AUDIO_INBUF_SIZE 0x10000 #define VIDEO_REFILL_THRESH 0 #define AUDIO_REFILL_THRESH 0x1000 +#define AUDIO_MIN_FRAME_SZ 128 Mutex FFMPEG::fflock("FFMPEG::fflock"); @@ -148,10 +160,13 @@ void FFAudioStream::reset_history() { inp = outp = bfr; hpos = 0; + memset(bfr, 0, lmt-bfr); } void FFAudioStream::iseek(int64_t ofs) { + if( ofs > hpos ) ofs = hpos; + if( ofs > sz ) ofs = sz; outp = inp - ofs*nch; if( outp < bfr ) outp += sz*nch; } @@ -234,6 +249,7 @@ FFStream::FFStream(FFMPEG *ffmpeg, AVStream *st, int fidx) this->fidx = fidx; frm_lock = new Mutex("FFStream::frm_lock"); fmt_ctx = 0; + avctx = 0; filter_graph = 0; buffersrc_ctx = 0; buffersink_ctx = 0; @@ -245,18 +261,27 @@ FFStream::FFStream(FFMPEG *ffmpeg, AVStream *st, int fidx) flushed = 0; need_packet = 1; frame = fframe = 0; + bsfc = 0; + stats_fp = 0; + stats_filename = 0; + stats_in = 0; + pass = 0; } FFStream::~FFStream() { - if( reading > 0 || writing > 0 ) avcodec_close(st->codec); + if( reading > 0 || writing > 0 ) avcodec_close(avctx); + if( avctx ) avcodec_free_context(&avctx); if( fmt_ctx ) avformat_close_input(&fmt_ctx); + if( bsfc ) av_bsf_free(&bsfc); while( frms.first ) frms.remove(frms.first); if( filter_graph ) avfilter_graph_free(&filter_graph); if( frame ) av_frame_free(&frame); if( fframe ) av_frame_free(&fframe); - bsfilter.remove_all_objects(); delete frm_lock; + if( stats_fp ) fclose(stats_fp); + if( stats_in ) av_freep(&stats_in); + delete [] stats_filename; } void FFStream::ff_lock(const char *cp) @@ -302,18 +327,30 @@ int FFStream::decode_activate() av_dict_copy(&copts, ffmpeg->opts, 0); int ret = 0; // this should be avformat_copy_context(), but no copy avail - ret = avformat_open_input(&fmt_ctx, ffmpeg->fmt_ctx->filename, NULL, &copts); + ret = avformat_open_input(&fmt_ctx, + ffmpeg->fmt_ctx->url, ffmpeg->fmt_ctx->iformat, &copts); if( ret >= 0 ) { ret = avformat_find_stream_info(fmt_ctx, 0); st = fmt_ctx->streams[fidx]; load_markers(); } - if( ret >= 0 ) { - AVCodecID codec_id = st->codec->codec_id; + if( ret >= 0 && st != 0 ) { + AVCodecID codec_id = st->codecpar->codec_id; AVCodec *decoder = avcodec_find_decoder(codec_id); - ret = avcodec_open2(st->codec, decoder, &copts); - if( ret >= 0 ) + avctx = avcodec_alloc_context3(decoder); + if( !avctx ) { + eprintf(_("cant allocate codec context\n")); + ret = AVERROR(ENOMEM); + } + if( ret >= 0 ) { + avcodec_parameters_to_context(avctx, st->codecpar); + if( !av_dict_get(copts, "threads", NULL, 0) ) + avctx->thread_count = ffmpeg->ff_cpus(); + ret = avcodec_open2(avctx, decoder, &copts); + } + if( ret >= 0 ) { reading = 1; + } else eprintf(_("open decoder failed\n")); } @@ -331,10 +368,7 @@ int FFStream::read_packet() int ret = av_read_frame(fmt_ctx, ipkt); if( ret < 0 ) { st_eof(1); - if( ret == AVERROR_EOF ) { - ipkt->stream_index = st->index; - return 0; - } + if( ret == AVERROR_EOF ) return 0; ff_err(ret, "FFStream::read_packet: av_read_frame failed\n"); flushed = 1; return -1; @@ -346,47 +380,43 @@ int FFStream::decode(AVFrame *frame) { int ret = 0; int retries = MAX_RETRY; - int got_frame = 0; - while( ret >= 0 && !flushed && --retries >= 0 && !got_frame ) { + while( ret >= 0 && !flushed && --retries >= 0 ) { if( need_packet ) { - need_packet = 0; if( (ret=read_packet()) < 0 ) break; - } - if( ipkt->stream_index == st->index ) { - while( (ipkt->size > 0 || !ipkt->data) && !got_frame ) { - ret = decode_frame(ipkt, frame, got_frame); - if( ret < 0 ) need_packet = 1; - if( ret <= 0 || !ipkt->data ) break; - ipkt->data += ret; - ipkt->size -= ret; + AVPacket *pkt = ret > 0 ? (AVPacket*)ipkt : 0; + if( pkt ) { + if( pkt->stream_index != st->index ) continue; + if( !pkt->data | !pkt->size ) continue; } + if( (ret=avcodec_send_packet(avctx, pkt)) < 0 ) { + ff_err(ret, "FFStream::decode: avcodec_send_packet failed\n"); + break; + } + need_packet = 0; retries = MAX_RETRY; } - if( !got_frame ) { + if( (ret=decode_frame(frame)) > 0 ) break; + if( !ret ) { need_packet = 1; flushed = st_eof(); } } - if( retries < 0 ) + if( retries < 0 ) { fprintf(stderr, "FFStream::decode: Retry limit\n"); - if( ret >= 0 ) - ret = got_frame; - else + ret = 0; + } + if( ret < 0 ) fprintf(stderr, "FFStream::decode: failed\n"); - return ret; } int FFStream::load_filter(AVFrame *frame) { - int ret = av_buffersrc_add_frame_flags(buffersrc_ctx, - frame, AV_BUFFERSRC_FLAG_KEEP_REF); - if( ret < 0 ) { - av_frame_unref(frame); + int ret = av_buffersrc_add_frame_flags(buffersrc_ctx, frame, 0); + if( ret < 0 ) eprintf(_("av_buffersrc_add_frame_flags failed\n")); - } return ret; } @@ -404,6 +434,7 @@ int FFStream::read_filter(AVFrame *frame) int FFStream::read_frame(AVFrame *frame) { + av_frame_unref(frame); if( !filter_graph || !buffersrc_ctx || !buffersink_ctx ) return decode(frame); if( !fframe && !(fframe=av_frame_alloc()) ) { @@ -420,35 +451,134 @@ int FFStream::read_frame(AVFrame *frame) int FFStream::write_packet(FFPacket &pkt) { - bs_filter(pkt); - av_packet_rescale_ts(pkt, st->codec->time_base, st->time_base); - pkt->stream_index = st->index; - return av_interleaved_write_frame(ffmpeg->fmt_ctx, pkt); + int ret = 0; + if( !bsfc ) { + av_packet_rescale_ts(pkt, avctx->time_base, st->time_base); + pkt->stream_index = st->index; + ret = av_interleaved_write_frame(ffmpeg->fmt_ctx, pkt); + } + else { + ret = av_bsf_send_packet(bsfc, pkt); + while( ret >= 0 ) { + FFPacket bs; + if( (ret=av_bsf_receive_packet(bsfc, bs)) < 0 ) { + if( ret == AVERROR(EAGAIN) ) return 0; + if( ret == AVERROR_EOF ) return -1; + break; + } + av_packet_rescale_ts(bs, avctx->time_base, st->time_base); + bs->stream_index = st->index; + ret = av_interleaved_write_frame(ffmpeg->fmt_ctx, bs); + } + } + if( ret < 0 ) + ff_err(ret, "FFStream::write_packet: write packet failed\n"); + return ret; +} + +int FFStream::encode_frame(AVFrame *frame) +{ + int pkts = 0, ret = 0; + for( int retry=100; --retry>=0; ) { + if( frame || !pkts ) + ret = avcodec_send_frame(avctx, frame); + if( !ret && frame ) return pkts; + if( ret < 0 && ret != AVERROR(EAGAIN) ) break; + FFPacket opkt; + ret = avcodec_receive_packet(avctx, opkt); + if( !frame && ret == AVERROR_EOF ) return pkts; + if( ret < 0 ) break; + ret = write_packet(opkt); + if( ret < 0 ) break; + ++pkts; + if( frame && stats_fp ) { + ret = write_stats_file(); + if( ret < 0 ) break; + } + } + ff_err(ret, "FFStream::encode_frame: encode failed\n"); + return -1; } int FFStream::flush() { if( writing < 0 ) return -1; - int ret = 0; - while( ret >= 0 ) { - FFPacket pkt; - int got_packet = 0; - ret = encode_frame(pkt, 0, got_packet); - if( ret < 0 || !got_packet ) break; - ret = write_packet(pkt); + int ret = encode_frame(0); + if( ret >= 0 && stats_fp ) { + ret = write_stats_file(); + close_stats_file(); } if( ret < 0 ) ff_err(ret, "FFStream::flush"); return ret >= 0 ? 0 : 1; } + +int FFStream::open_stats_file() +{ + stats_fp = fopen(stats_filename,"w"); + return stats_fp ? 0 : AVERROR(errno); +} + +int FFStream::close_stats_file() +{ + if( stats_fp ) { + fclose(stats_fp); stats_fp = 0; + } + return 0; +} + +int FFStream::read_stats_file() +{ + int64_t len = 0; struct stat stats_st; + int fd = open(stats_filename, O_RDONLY); + int ret = fd >= 0 ? 0: ENOENT; + if( !ret && fstat(fd, &stats_st) ) + ret = EINVAL; + if( !ret ) { + len = stats_st.st_size; + stats_in = (char *)av_malloc(len+1); + if( !stats_in ) + ret = ENOMEM; + } + if( !ret && read(fd, stats_in, len+1) != len ) + ret = EIO; + if( !ret ) { + stats_in[len] = 0; + avctx->stats_in = stats_in; + } + if( fd >= 0 ) + close(fd); + return !ret ? 0 : AVERROR(ret); +} + +int FFStream::write_stats_file() +{ + int ret = 0; + if( avctx->stats_out && (ret=strlen(avctx->stats_out)) > 0 ) { + int len = fwrite(avctx->stats_out, 1, ret, stats_fp); + if( ret != len ) + ff_err(ret = AVERROR(errno), "FFStream::write_stats_file"); + } + return ret; +} + +int FFStream::init_stats_file() +{ + int ret = 0; + if( (pass & 2) && (ret = read_stats_file()) < 0 ) + ff_err(ret, "stat file read: %s", stats_filename); + if( (pass & 1) && (ret=open_stats_file()) < 0 ) + ff_err(ret, "stat file open: %s", stats_filename); + return ret >= 0 ? 0 : ret; +} + int FFStream::seek(int64_t no, double rate) { - int64_t tstmp = -INT64_MAX+1; // default ffmpeg native seek int npkts = 1; - int64_t pos = no, plmt = -1; + int64_t pos = no, pkt_pos = -1; IndexMarks *index_markers = get_markers(); if( index_markers && index_markers->size() > 1 ) { IndexMarks &marks = *index_markers; @@ -458,37 +588,77 @@ int FFStream::seek(int64_t no, double rate) if( no-n < 30*rate ) { if( n < 0 ) n = 0; pos = n; - if( i < marks.size() ) plmt = marks[i].pos; + if( i < marks.size() ) pkt_pos = marks[i].pos; npkts = MAX_RETRY; } } - if( pos > 0 ) { - double secs = pos / rate; - tstmp = secs * st->time_base.den / st->time_base.num; - if( nudge != AV_NOPTS_VALUE ) tstmp += nudge; + if( pos == curr_pos ) return 0; + 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( !tstmp ) { + if( st->nb_index_entries > 0 ) tstmp = st->index_entries[0].timestamp; + else if( st->start_time != AV_NOPTS_VALUE ) tstmp = st->start_time; + else if( st->first_dts != AV_NOPTS_VALUE ) tstmp = st->first_dts; + else tstmp = INT64_MIN+1; + } + else if( nudge != AV_NOPTS_VALUE ) tstmp += nudge; + int idx = st->index; +#if 0 +// seek all streams using the default timebase. +// this is how ffmpeg and ffplay work. stream seeks are less tested. + tstmp = av_rescale_q(tstmp, time_base, AV_TIME_BASE_Q); + idx = -1; +#endif + + avcodec_flush_buffers(avctx); + avformat_flush(fmt_ctx); +#if 0 + int64_t seek = tstmp; + int flags = AVSEEK_FLAG_ANY; + if( !(fmt_ctx->iformat->flags & AVFMT_NO_BYTE_SEEK) && pkt_pos >= 0 ) { + seek = pkt_pos; + flags = AVSEEK_FLAG_BYTE; } - int ret = avformat_seek_file(fmt_ctx, st->index, - -INT64_MAX, tstmp, INT64_MAX, AVSEEK_FLAG_ANY); - if( ret >= 0 ) { - avcodec_flush_buffers(st->codec); - ipkt.finit(); ipkt.init(); + int ret = avformat_seek_file(fmt_ctx, st->index, -INT64_MAX, seek, INT64_MAX, flags); +#else +// finds the first index frame below the target time + int flags = AVSEEK_FLAG_BACKWARD | AVSEEK_FLAG_ANY; + int ret = av_seek_frame(fmt_ctx, idx, tstmp, flags); +#endif + int retry = MAX_RETRY; + while( ret >= 0 ) { need_packet = 0; flushed = 0; seeked = 1; st_eof(0); -// read up to retry packets, limited to npkts in stream, and not past pkt.pos plmt - for(;;) { +// read up to retry packets, limited to npkts in stream, and not pkt.pos past pkt_pos + while( --retry >= 0 ) { if( read_packet() <= 0 ) { ret = -1; break; } - if( plmt >= 0 && ipkt->pos >= plmt ) break; if( ipkt->stream_index != st->index ) continue; + if( !ipkt->data || !ipkt->size ) continue; + if( pkt_pos >= 0 && ipkt->pos >= pkt_pos ) break; if( --npkts <= 0 ) break; int64_t pkt_ts = ipkt->dts != AV_NOPTS_VALUE ? ipkt->dts : ipkt->pts; if( pkt_ts == AV_NOPTS_VALUE ) continue; if( pkt_ts >= tstmp ) break; } + if( retry < 0 ) { + fprintf(stderr,"FFStream::seek: retry limit, pos=%jd tstmp=%jd\n",pos,tstmp); + ret = -1; + } + if( ret < 0 ) break; + ret = avcodec_send_packet(avctx, ipkt); + if( !ret ) break; +//some codecs need more than one pkt to resync + if( ret == AVERROR_INVALIDDATA ) ret = 0; + if( ret < 0 ) { + ff_err(ret, "FFStream::avcodec_send_packet failed\n"); + break; + } } if( ret < 0 ) { -//printf("** seek fail %ld, %ld\n", pos, tstmp); +printf("** seek fail %jd, %jd\n", pos, tstmp); seeked = need_packet = 0; - st_eof(flushed=1); + st_eof(flushed=1); return -1; } //printf("seeked pos = %ld, %ld\n", pos, tstmp); @@ -503,8 +673,10 @@ FFAudioStream::FFAudioStream(FFMPEG *ffmpeg, AVStream *strm, int idx, int fidx) channel0 = channels = 0; sample_rate = 0; mbsz = 0; + frame_sz = AUDIO_MIN_FRAME_SZ; length = 0; resample_context = 0; + swr_ichs = swr_ifmt = swr_irate = 0; aud_bfr_sz = 0; aud_bfr = 0; @@ -525,6 +697,28 @@ FFAudioStream::~FFAudioStream() delete [] bfr; } +void FFAudioStream::init_swr(int ichs, int ifmt, int irate) +{ + if( resample_context ) { + if( swr_ichs == ichs && swr_ifmt == ifmt && swr_irate == irate ) + return; + swr_free(&resample_context); + } + swr_ichs = ichs; swr_ifmt = ifmt; swr_irate = irate; + if( ichs == channels && ifmt == AV_SAMPLE_FMT_FLT && irate == sample_rate ) + return; + uint64_t ilayout = av_get_default_channel_layout(ichs); + if( !ilayout ) ilayout = ((uint64_t)1<codec, frame, &got_frame, pkt); + int ret = avcodec_receive_frame(avctx, frame); if( ret < 0 ) { - if( first_frame ) return 0; + if( first_frame || ret == AVERROR(EAGAIN) ) return 0; + if( ret == AVERROR_EOF ) { st_eof(1); return 0; } ff_err(ret, "FFAudioStream::decode_frame: Could not read audio frame\n"); return -1; } - if( got_frame ) { - int64_t pkt_ts = av_frame_get_best_effort_timestamp(frame); - if( pkt_ts != AV_NOPTS_VALUE ) - curr_pos = ffmpeg->to_secs(pkt_ts - nudge, st->time_base) * sample_rate + 0.5; - } - return ret; + int64_t pkt_ts = frame->best_effort_timestamp; + if( pkt_ts != AV_NOPTS_VALUE ) + curr_pos = ffmpeg->to_secs(pkt_ts - nudge, st->time_base) * sample_rate + 0.5; + return 1; } int FFAudioStream::encode_activate() { if( writing >= 0 ) return writing; - AVCodecContext *ctx = st->codec; - frame_sz = ctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE ? - 10000 : ctx->frame_size; + if( !avctx->codec ) return writing = 0; + frame_sz = avctx->codec->capabilities & AV_CODEC_CAP_VARIABLE_FRAME_SIZE ? + 10000 : avctx->frame_size; return FFStream::encode_activate(); } -int FFAudioStream::nb_samples() -{ - AVCodecContext *ctx = st->codec; - return ctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE ? - 10000 : ctx->frame_size; -} - int64_t FFAudioStream::load_buffer(double ** const sp, int len) { - reserve(len+1, st->codec->channels); + reserve(len+1, st->codecpar->channels); for( int ch=0; chcodec; frame->nb_samples = frame_sz; - frame->format = ctx->sample_fmt; - frame->channel_layout = ctx->channel_layout; - frame->sample_rate = ctx->sample_rate; + frame->format = avctx->sample_fmt; + frame->channel_layout = avctx->channel_layout; + frame->sample_rate = avctx->sample_rate; int ret = av_frame_get_buffer(frame, 0); if (ret < 0) ff_err(ret, "FFAudioStream::init_frame: av_frame_get_buffer failed\n"); @@ -634,10 +819,11 @@ int FFAudioStream::load(int64_t pos, int len) } if( mbsz < len ) mbsz = len; int64_t end_pos = pos + len; - int ret = 0; - for( int i=0; ret>=0 && !flushed && curr_pos=0 && !flushed && curr_pos=0 ) { ret = read_frame(frame); - if( ret > 0 ) { + if( ret > 0 && frame->nb_samples > 0 ) { + init_swr(frame->channels, frame->format, frame->sample_rate); load_history(&frame->extended_data[0], frame->nb_samples); curr_pos += frame->nb_samples; } @@ -653,8 +839,8 @@ int FFAudioStream::load(int64_t pos, int len) int FFAudioStream::audio_seek(int64_t pos) { - if( decode_activate() < 0 ) return -1; - if( !st->codec || !st->codec->codec ) return -1; + if( decode_activate() <= 0 ) return -1; + if( !st->codecpar ) return -1; if( in_history(pos) ) return 0; if( pos == curr_pos ) return 0; reset_history(); mbsz = 0; @@ -668,39 +854,47 @@ int FFAudioStream::encode(double **samples, int len) if( encode_activate() <= 0 ) return -1; ffmpeg->flow_ctl(); int ret = 0; - int64_t count = load_buffer(samples, len); + int64_t count = samples ? load_buffer(samples, len) : used(); + int frame_sz1 = samples ? frame_sz-1 : 0; FFrame *frm = 0; - while( ret >= 0 && count >= frame_sz ) { + while( ret >= 0 && count > frame_sz1 ) { frm = new FFrame(this); if( (ret=frm->initted()) < 0 ) break; AVFrame *frame = *frm; - float *bfrp = get_outp(frame_sz); + len = count >= frame_sz ? frame_sz : count; + float *bfrp = get_outp(len); ret = swr_convert(resample_context, - (uint8_t **)frame->extended_data, frame_sz, - (const uint8_t **)&bfrp, frame_sz); + (uint8_t **)frame->extended_data, len, + (const uint8_t **)&bfrp, len); if( ret < 0 ) { ff_err(ret, "FFAudioStream::encode: swr_convert failed\n"); break; } + frame->nb_samples = len; frm->queue(curr_pos); frm = 0; - curr_pos += frame_sz; - count -= frame_sz; + curr_pos += len; + count -= len; } delete frm; return ret >= 0 ? 0 : 1; } -int FFAudioStream::encode_frame(AVPacket *pkt, AVFrame *frame, int &got_packet) +int FFAudioStream::drain() { - int ret = avcodec_encode_audio2(st->codec, pkt, frame, &got_packet); - if( ret < 0 ) { - ff_err(ret, "FFAudioStream::encode_frame: encode audio failed\n"); - return -1; - } - return ret; + return encode(0,0); +} + +int FFAudioStream::encode_frame(AVFrame *frame) +{ + return FFStream::encode_frame(frame); +} + +int FFAudioStream::write_packet(FFPacket &pkt) +{ + return FFStream::write_packet(pkt); } void FFAudioStream::load_markers() @@ -734,24 +928,21 @@ FFVideoStream::~FFVideoStream() { } -int FFVideoStream::decode_frame(AVPacket *pkt, AVFrame *frame, int &got_frame) +int FFVideoStream::decode_frame(AVFrame *frame) { int first_frame = seeked; seeked = 0; - int ret = avcodec_decode_video2(st->codec, frame, &got_frame, pkt); + int ret = avcodec_receive_frame(avctx, frame); if( ret < 0 ) { - if( first_frame ) return 0; + if( first_frame || ret == AVERROR(EAGAIN) ) return 0; + if( ret == AVERROR(EAGAIN) ) return 0; + if( ret == AVERROR_EOF ) { st_eof(1); return 0; } ff_err(ret, "FFVideoStream::decode_frame: Could not read video frame\n"); return -1; } - else // this is right out of ffplay, looks questionable ??? - ret = pkt->size; - - if( got_frame ) { - int64_t pkt_ts = av_frame_get_best_effort_timestamp(frame); - if( pkt_ts != AV_NOPTS_VALUE ) - curr_pos = ffmpeg->to_secs(pkt_ts - nudge, st->time_base) * frame_rate + 0.5; - } - return ret; + int64_t pkt_ts = frame->best_effort_timestamp; + if( pkt_ts != AV_NOPTS_VALUE ) + curr_pos = ffmpeg->to_secs(pkt_ts - nudge, st->time_base) * frame_rate + 0.5; + return 1; } int FFVideoStream::load(VFrame *vframe, int64_t pos) @@ -762,7 +953,8 @@ int FFVideoStream::load(VFrame *vframe, int64_t pos) fprintf(stderr, "FFVideoStream::load: av_frame_alloc failed\n"); return -1; } - for( int i=0; ret>=0 && !flushed && curr_pos<=pos && i=0 && !flushed && curr_pos<=pos && --i>=0 ) { ret = read_frame(frame); if( ret > 0 ) ++curr_pos; } @@ -777,11 +969,11 @@ int FFVideoStream::load(VFrame *vframe, int64_t pos) int FFVideoStream::video_seek(int64_t pos) { - if( decode_activate() < 0 ) return -1; - if( !st->codec || !st->codec->codec ) return -1; + if( decode_activate() <= 0 ) return -1; + if( !st->codecpar ) return -1; if( pos == curr_pos-1 && !seeked ) return 0; // if close enough, just read up to current - int gop = st->codec->gop_size; + int gop = avctx->gop_size; if( gop < 4 ) gop = 4; if( gop > 64 ) gop = 64; int read_limit = curr_pos + 3*gop; @@ -793,10 +985,9 @@ int FFVideoStream::video_seek(int64_t pos) int FFVideoStream::init_frame(AVFrame *picture) { - AVCodecContext *ctx = st->codec; - picture->format = ctx->pix_fmt; - picture->width = ctx->width; - picture->height = ctx->height; + picture->format = avctx->pix_fmt; + picture->width = avctx->width; + picture->height = avctx->height; int ret = av_frame_get_buffer(picture, 32); return ret; } @@ -823,18 +1014,25 @@ int FFVideoStream::encode(VFrame *vframe) return ret >= 0 ? 0 : 1; } -int FFVideoStream::encode_frame(AVPacket *pkt, AVFrame *frame, int &got_packet) +int FFVideoStream::drain() +{ + return 0; +} + +int FFVideoStream::encode_frame(AVFrame *frame) { if( frame ) { frame->interlaced_frame = interlaced; frame->top_field_first = top_field_first; } - int ret = avcodec_encode_video2(st->codec, pkt, frame, &got_packet); - if( ret < 0 ) { - ff_err(ret, "FFVideoStream::encode_frame: encode video failed\n"); - return -1; - } - return ret; + return FFStream::encode_frame(frame); +} + +int FFVideoStream::write_packet(FFPacket &pkt) +{ + if( !(ffmpeg->fmt_ctx->oformat->flags & AVFMT_VARIABLE_FPS) ) + pkt->duration = 1; + return FFStream::write_packet(pkt); } AVPixelFormat FFVideoConvert::color_model_to_pix_fmt(int color_model) @@ -856,6 +1054,7 @@ AVPixelFormat FFVideoConvert::color_model_to_pix_fmt(int color_model) case BC_RGB161616: return AV_PIX_FMT_RGB48LE; case BC_RGBA16161616: return AV_PIX_FMT_RGBA64LE; case BC_AYUV16161616: return AV_PIX_FMT_AYUV64LE; + case BC_GBRP: return AV_PIX_FMT_GBRP; default: break; } @@ -881,6 +1080,7 @@ int FFVideoConvert::pix_fmt_to_color_model(AVPixelFormat pix_fmt) case AV_PIX_FMT_RGB48LE: return BC_RGB161616; case AV_PIX_FMT_RGBA64LE: return BC_RGBA16161616; case AV_PIX_FMT_AYUV64LE: return BC_AYUV16161616; + case AV_PIX_FMT_GBRP: return BC_GBRP; default: break; } @@ -977,7 +1177,7 @@ int FFVideoConvert::transfer_cmodel(VFrame *frame, AVFrame *ifp) { int ret = convert_cmodel(frame, ifp); if( ret > 0 ) { - const AVDictionary *src = av_frame_get_metadata(ifp); + const AVDictionary *src = ifp->metadata; AVDictionaryEntry *t = NULL; BC_Hash *hp = frame->get_params(); //hp->clear(); @@ -1074,7 +1274,7 @@ int FFVideoConvert::transfer_pixfmt(VFrame *frame, AVFrame *ofp) int ret = convert_pixfmt(frame, ofp); if( ret > 0 ) { BC_Hash *hp = frame->get_params(); - AVDictionary **dict = avpriv_frame_get_metadatap(ofp); + AVDictionary **dict = &ofp->metadata; //av_dict_free(dict); for( int i=0; isize(); ++i ) { char *key = hp->get_key(i), *val = hp->get_value(i); @@ -1175,7 +1375,7 @@ AVRational FFMPEG::to_sample_aspect_ratio(Asset *asset) { #if 1 double display_aspect = asset->width / (double)asset->height; - double sample_aspect = asset->aspect_ratio / display_aspect; + double sample_aspect = display_aspect / asset->aspect_ratio; int width = 1000000, height = width * sample_aspect + 0.5; float w, h; MWindow::create_aspect_ratio(w, h, width, height); @@ -1191,6 +1391,37 @@ AVRational FFMPEG::to_time_base(int sample_rate) return (AVRational){1, sample_rate}; } +int FFMPEG::get_fmt_score(AVSampleFormat dst_fmt, AVSampleFormat src_fmt) +{ + int score = 0; + int dst_planar = av_sample_fmt_is_planar(dst_fmt); + int src_planar = av_sample_fmt_is_planar(src_fmt); + if( dst_planar != src_planar ) ++score; + int dst_bytes = av_get_bytes_per_sample(dst_fmt); + int src_bytes = av_get_bytes_per_sample(src_fmt); + score += (src_bytes > dst_bytes ? 100 : -10) * (src_bytes - dst_bytes); + int src_packed = av_get_packed_sample_fmt(src_fmt); + int dst_packed = av_get_packed_sample_fmt(dst_fmt); + if( dst_packed == AV_SAMPLE_FMT_S32 && src_packed == AV_SAMPLE_FMT_FLT ) score += 20; + if( dst_packed == AV_SAMPLE_FMT_FLT && src_packed == AV_SAMPLE_FMT_S32 ) score += 2; + return score; +} + +AVSampleFormat FFMPEG::find_best_sample_fmt_of_list( + const AVSampleFormat *sample_fmts, AVSampleFormat src_fmt) +{ + AVSampleFormat best = AV_SAMPLE_FMT_NONE; + int best_score = get_fmt_score(best, src_fmt); + for( int i=0; sample_fmts[i] >= 0; ++i ) { + AVSampleFormat sample_fmt = sample_fmts[i]; + int score = get_fmt_score(sample_fmt, src_fmt); + if( score >= best_score ) continue; + best = sample_fmt; best_score = score; + } + return best; +} + + void FFMPEG::set_option_path(char *path, const char *fmt, ...) { char *ep = path + BCTEXTLEN-1; @@ -1287,10 +1518,10 @@ int FFMPEG::get_file_format() return ret; } -int FFMPEG::scan_option_line(char *cp, char *tag, char *val) +int FFMPEG::scan_option_line(const char *cp, char *tag, char *val) { while( *cp == ' ' || *cp == '\t' ) ++cp; - char *bp = cp; + const char *bp = cp; while( *cp && *cp != ' ' && *cp != '\t' && *cp != '=' && *cp != '\n' ) ++cp; int len = cp - bp; if( !len || len > BCSTRLEN-1 ) return 1; @@ -1308,6 +1539,123 @@ int FFMPEG::scan_option_line(char *cp, char *tag, char *val) return 0; } +int FFMPEG::can_render(const char *fformat, const char *type) +{ + FileSystem fs; + char option_path[BCTEXTLEN]; + FFMPEG::set_option_path(option_path, type); + fs.update(option_path); + int total_files = fs.total_files(); + for( int i=0; iget_name(); + const char *ext = strrchr(name,'.'); + if( !ext ) continue; + if( !strcmp(fformat, ++ext) ) return 1; + } + return 0; +} + +int FFMPEG::get_ff_option(const char *nm, const char *options, char *value) +{ + for( const char *cp=options; *cp!=0; ) { + char line[BCTEXTLEN], *bp = line, *ep = bp+sizeof(line)-1; + while( bp < ep && *cp && *cp!='\n' ) *bp++ = *cp++; + if( *cp ) ++cp; + *bp = 0; + if( !line[0] || line[0] == '#' || line[0] == ';' ) continue; + char key[BCSTRLEN], val[BCTEXTLEN]; + if( FFMPEG::scan_option_line(line, key, val) ) continue; + if( !strcmp(key, nm) ) { + strncpy(value, val, BCSTRLEN); + return 0; + } + } + return 1; +} + +void FFMPEG::scan_audio_options(Asset *asset, EDL *edl) +{ + char cin_sample_fmt[BCSTRLEN]; + int cin_fmt = AV_SAMPLE_FMT_NONE; + const char *options = asset->ff_audio_options; + if( !get_ff_option("cin_sample_fmt", options, cin_sample_fmt) ) + cin_fmt = (int)av_get_sample_fmt(cin_sample_fmt); + if( cin_fmt < 0 ) { + char audio_codec[BCSTRLEN]; audio_codec[0] = 0; + AVCodec *av_codec = !FFMPEG::get_codec(audio_codec, "audio", asset->acodec) ? + 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); + } + if( cin_fmt < 0 ) cin_fmt = AV_SAMPLE_FMT_S16; + const char *name = av_get_sample_fmt_name((AVSampleFormat)cin_fmt); + if( !name ) name = _("None"); + strcpy(asset->ff_sample_format, name); + + char value[BCSTRLEN]; + if( !get_ff_option("cin_bitrate", options, value) ) + asset->ff_audio_bitrate = atoi(value); + if( !get_ff_option("cin_quality", options, value) ) + asset->ff_audio_quality = atoi(value); +} + +void FFMPEG::load_audio_options(Asset *asset, EDL *edl) +{ + char options_path[BCTEXTLEN]; + set_option_path(options_path, "audio/%s", asset->acodec); + if( !load_options(options_path, + asset->ff_audio_options, + sizeof(asset->ff_audio_options)) ) + scan_audio_options(asset, edl); +} + +void FFMPEG::scan_video_options(Asset *asset, EDL *edl) +{ + char cin_pix_fmt[BCSTRLEN]; + int cin_fmt = AV_PIX_FMT_NONE; + const char *options = asset->ff_video_options; + if( !get_ff_option("cin_pix_fmt", options, cin_pix_fmt) ) + cin_fmt = (int)av_get_pix_fmt(cin_pix_fmt); + if( cin_fmt < 0 ) { + char video_codec[BCSTRLEN]; video_codec[0] = 0; + AVCodec *av_codec = !get_codec(video_codec, "video", asset->vcodec) ? + avcodec_find_encoder_by_name(video_codec) : 0; + if( av_codec && av_codec->pix_fmts ) { + if( 0 && edl ) { // frequently picks a bad answer + int color_model = edl->session->color_model; + int max_bits = BC_CModels::calculate_pixelsize(color_model) * 8; + max_bits /= BC_CModels::components(color_model); + cin_fmt = avcodec_find_best_pix_fmt_of_list(av_codec->pix_fmts, + (BC_CModels::is_yuv(color_model) ? + (max_bits > 8 ? AV_PIX_FMT_AYUV64LE : AV_PIX_FMT_YUV444P) : + (max_bits > 8 ? AV_PIX_FMT_RGB48LE : AV_PIX_FMT_RGB24)), 0, 0); + } + else + cin_fmt = av_codec->pix_fmts[0]; + } + } + if( cin_fmt < 0 ) cin_fmt = AV_PIX_FMT_YUV420P; + const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get((AVPixelFormat)cin_fmt); + const char *name = desc ? desc->name : _("None"); + strcpy(asset->ff_pixel_format, name); + + char value[BCSTRLEN]; + if( !get_ff_option("cin_bitrate", options, value) ) + asset->ff_video_bitrate = atoi(value); + if( !get_ff_option("cin_quality", options, value) ) + asset->ff_video_quality = atoi(value); +} + +void FFMPEG::load_video_options(Asset *asset, EDL *edl) +{ + char options_path[BCTEXTLEN]; + set_option_path(options_path, "video/%s", asset->vcodec); + if( !load_options(options_path, + asset->ff_video_options, + sizeof(asset->ff_video_options)) ) + scan_video_options(asset, edl); +} + int FFMPEG::load_defaults(const char *path, const char *type, char *codec, char *codec_options, int len) { @@ -1328,48 +1676,57 @@ int FFMPEG::load_defaults(const char *path, const char *type, return load_options(default_file, codec_options, len); } -void FFMPEG::set_asset_format(Asset *asset, const char *text) +void FFMPEG::set_asset_format(Asset *asset, EDL *edl, const char *text) { if( asset->format != FILE_FFMPEG ) return; if( text != asset->fformat ) strcpy(asset->fformat, text); - if( !asset->ff_audio_options[0] ) { - asset->audio_data = !load_defaults("audio", text, asset->acodec, - asset->ff_audio_options, sizeof(asset->ff_audio_options)); + if( asset->audio_data && !asset->ff_audio_options[0] ) { + if( !load_defaults("audio", text, asset->acodec, + asset->ff_audio_options, sizeof(asset->ff_audio_options)) ) + scan_audio_options(asset, edl); + else + asset->audio_data = 0; } - if( !asset->ff_video_options[0] ) { - asset->video_data = !load_defaults("video", text, asset->vcodec, - asset->ff_video_options, sizeof(asset->ff_video_options)); + if( asset->video_data && !asset->ff_video_options[0] ) { + if( !load_defaults("video", text, asset->vcodec, + asset->ff_video_options, sizeof(asset->ff_video_options)) ) + scan_video_options(asset, edl); + else + asset->video_data = 0; } } int FFMPEG::get_encoder(const char *options, - char *format, char *codec, char *bsfilter, char *bsargs) + char *format, char *codec, char *bsfilter) { FILE *fp = fopen(options,"r"); if( !fp ) { eprintf(_("options open failed %s\n"),options); return 1; } - if( get_encoder(fp, format, codec, bsfilter, bsargs) ) + char line[BCTEXTLEN]; + if( !fgets(line, sizeof(line), fp) || + scan_encoder(line, format, codec, bsfilter) ) eprintf(_("format/codec not found %s\n"), options); fclose(fp); return 0; } -int FFMPEG::get_encoder(FILE *fp, - char *format, char *codec, char *bsfilter, char *bsargs) +int FFMPEG::scan_encoder(const char *line, + char *format, char *codec, char *bsfilter) { - format[0] = codec[0] = bsfilter[0] = bsargs[0] = 0; - char line[BCTEXTLEN]; - if( !fgets(line, sizeof(line), fp) ) return 1; - line[sizeof(line)-1] = 0; + format[0] = codec[0] = bsfilter[0] = 0; if( scan_option_line(line, format, codec) ) return 1; char *cp = codec; while( *cp && *cp != '|' ) ++cp; if( !*cp ) return 0; - if( scan_option_line(cp+1, bsfilter, bsargs) ) return 1; - do { *cp-- = 0; } while( cp>=codec && (*cp==' ' || *cp == '\t' ) ); + char *bp = cp; + do { *bp-- = 0; } while( bp>=codec && (*bp==' ' || *bp == '\t' ) ); + while( *++cp && (*cp==' ' || *cp == '\t') ); + bp = bsfilter; + for( int i=BCTEXTLEN; --i>0 && *cp; ) *bp++ = *cp++; + *bp = 0; return 0; } @@ -1487,17 +1844,19 @@ int FFMPEG::info(char *text, int len) decode_activate(); #define report(s...) do { int n = snprintf(cp,len,s); cp += n; len -= n; } while(0) char *cp = text; + report("format: %s\n",fmt_ctx->iformat->name); if( ffvideo.size() > 0 ) report("\n%d video stream%s\n",ffvideo.size(), ffvideo.size()!=1 ? "s" : ""); for( int vidx=0; vidxst; - AVCodecContext *avctx = st->codec; - report(_("vid%d (%d), id 0x%06x:\n"), vid->idx, vid->fidx, avctx->codec_id); - const AVCodecDescriptor *desc = avcodec_descriptor_get(avctx->codec_id); + AVCodecID codec_id = st->codecpar->codec_id; + report(_("vid%d (%d), id 0x%06x:\n"), vid->idx, vid->fidx, codec_id); + const AVCodecDescriptor *desc = avcodec_descriptor_get(codec_id); report(" video%d %s", vidx+1, desc ? desc->name : " (unkn)"); report(" %dx%d %5.2f", vid->width, vid->height, vid->frame_rate); - const char *pfn = av_get_pix_fmt_name(avctx->pix_fmt); + AVPixelFormat pix_fmt = (AVPixelFormat)st->codecpar->format; + const char *pfn = av_get_pix_fmt_name(pix_fmt); report(" pix %s\n", pfn ? pfn : "(unkn)"); double secs = to_secs(st->duration, st->time_base); int64_t length = secs * vid->frame_rate + 0.5; @@ -1514,14 +1873,15 @@ int FFMPEG::info(char *text, int len) for( int aidx=0; aidxst; - AVCodecContext *avctx = st->codec; - report(_("aud%d (%d), id 0x%06x:\n"), aud->idx, aud->fidx, avctx->codec_id); - const AVCodecDescriptor *desc = avcodec_descriptor_get(avctx->codec_id); + AVCodecID codec_id = st->codecpar->codec_id; + report(_("aud%d (%d), id 0x%06x:\n"), aud->idx, aud->fidx, codec_id); + const AVCodecDescriptor *desc = avcodec_descriptor_get(codec_id); int nch = aud->channels, ch0 = aud->channel0+1; report(" audio%d-%d %s", ch0, ch0+nch-1, desc ? desc->name : " (unkn)"); - const char *fmt = av_get_sample_fmt_name(avctx->sample_fmt); + AVSampleFormat sample_fmt = (AVSampleFormat)st->codecpar->format; + const char *fmt = av_get_sample_fmt_name(sample_fmt); report(" %s %d", fmt, aud->sample_rate); - int sample_bits = av_get_bits_per_sample(avctx->codec_id); + int sample_bits = av_get_bits_per_sample(codec_id); report(" %dbits\n", sample_bits); double secs = to_secs(st->duration, st->time_base); int64_t length = secs * aud->sample_rate + 0.5; @@ -1575,7 +1935,9 @@ int FFMPEG::init_decoder(const char *filename) char file_opts[BCTEXTLEN]; char *bp = strrchr(strcpy(file_opts, filename), '/'); char *sp = strrchr(!bp ? file_opts : bp, '.'); + if( !sp ) sp = bp + strlen(bp); FILE *fp = 0; + AVInputFormat *ifmt = 0; if( sp ) { strcpy(sp, ".opts"); fp = fopen(file_opts, "r"); @@ -1583,12 +1945,16 @@ int FFMPEG::init_decoder(const char *filename) if( fp ) { read_options(fp, file_opts, opts); fclose(fp); + AVDictionaryEntry *tag; + if( (tag=av_dict_get(opts, "format", NULL, 0)) != 0 ) { + ifmt = av_find_input_format(tag->value); + } } else load_options("decode.opts", opts); AVDictionary *fopts = 0; av_dict_copy(&fopts, opts, 0); - int ret = avformat_open_input(&fmt_ctx, filename, NULL, &fopts); + int ret = avformat_open_input(&fmt_ctx, filename, ifmt, &fopts); av_dict_free(&fopts); if( ret >= 0 ) ret = avformat_find_stream_info(fmt_ctx, NULL); @@ -1602,8 +1968,8 @@ int FFMPEG::init_decoder(const char *filename) int FFMPEG::open_decoder() { struct stat st; - if( stat(fmt_ctx->filename, &st) < 0 ) { - eprintf(_("can't stat file: %s\n"), fmt_ctx->filename); + if( stat(fmt_ctx->url, &st) < 0 ) { + eprintf(_("can't stat file: %s\n"), fmt_ctx->url); return 1; } @@ -1622,20 +1988,24 @@ int FFMPEG::open_decoder() estimated = 1; } } - if( estimated ) + static int notified = 0; + if( !notified && estimated ) { + notified = 1; printf("FFMPEG::open_decoder: some stream times estimated\n"); + } ff_lock("FFMPEG::open_decoder"); int ret = 0, bad_time = 0; for( int i=0; !ret && i<(int)fmt_ctx->nb_streams; ++i ) { AVStream *st = fmt_ctx->streams[i]; if( st->duration == AV_NOPTS_VALUE ) bad_time = 1; - AVCodecContext *avctx = st->codec; - const AVCodecDescriptor *codec_desc = avcodec_descriptor_get(avctx->codec_id); + AVCodecParameters *avpar = st->codecpar; + const AVCodecDescriptor *codec_desc = avcodec_descriptor_get(avpar->codec_id); if( !codec_desc ) continue; - if( avctx->codec_type == AVMEDIA_TYPE_VIDEO ) { - if( avctx->width < 1 ) continue; - if( avctx->height < 1 ) continue; + switch( avpar->codec_type ) { + case AVMEDIA_TYPE_VIDEO: { + if( avpar->width < 1 ) continue; + if( avpar->height < 1 ) continue; AVRational framerate = av_guess_frame_rate(fmt_ctx, st, 0); if( framerate.num < 1 ) continue; has_video = 1; @@ -1643,8 +2013,8 @@ int FFMPEG::open_decoder() FFVideoStream *vid = new FFVideoStream(this, st, vidx, i); vstrm_index.append(ffidx(vidx, 0)); ffvideo.append(vid); - vid->width = avctx->width; - vid->height = avctx->height; + vid->width = avpar->width; + vid->height = avpar->height; vid->frame_rate = !framerate.den ? 0 : (double)framerate.num / framerate.den; double secs = to_secs(st->duration, st->time_base); vid->length = secs * vid->frame_rate; @@ -1652,35 +2022,29 @@ int FFMPEG::open_decoder() vid->nudge = st->start_time; vid->reading = -1; if( opt_video_filter ) - ret = vid->create_filter(opt_video_filter, avctx,avctx); - } - else if( avctx->codec_type == AVMEDIA_TYPE_AUDIO ) { - if( avctx->channels < 1 ) continue; - if( avctx->sample_rate < 1 ) continue; + ret = vid->create_filter(opt_video_filter, avpar); + break; } + case AVMEDIA_TYPE_AUDIO: { + if( avpar->channels < 1 ) continue; + if( avpar->sample_rate < 1 ) continue; has_audio = 1; int aidx = ffaudio.size(); FFAudioStream *aud = new FFAudioStream(this, st, aidx, i); ffaudio.append(aud); aud->channel0 = astrm_index.size(); - aud->channels = avctx->channels; + aud->channels = avpar->channels; for( int ch=0; chchannels; ++ch ) astrm_index.append(ffidx(aidx, ch)); - aud->sample_rate = avctx->sample_rate; + aud->sample_rate = avpar->sample_rate; double secs = to_secs(st->duration, st->time_base); aud->length = secs * aud->sample_rate; - if( avctx->sample_fmt != AV_SAMPLE_FMT_FLT ) { - uint64_t layout = av_get_default_channel_layout(avctx->channels); - if( !layout ) layout = ((uint64_t)1<channels) - 1; - aud->resample_context = swr_alloc_set_opts(NULL, - layout, AV_SAMPLE_FMT_FLT, avctx->sample_rate, - layout, avctx->sample_fmt, avctx->sample_rate, - 0, NULL); - swr_init(aud->resample_context); - } + aud->init_swr(aud->channels, avpar->format, aud->sample_rate); aud->nudge = st->start_time; aud->reading = -1; if( opt_audio_filter ) - ret = aud->create_filter(opt_audio_filter, avctx,avctx); + ret = aud->create_filter(opt_audio_filter, avpar); + break; } + default: break; } } if( bad_time ) @@ -1692,14 +2056,18 @@ int FFMPEG::open_decoder() int FFMPEG::init_encoder(const char *filename) { - int fd = ::open(filename,O_WRONLY); - if( fd < 0 ) fd = open(filename,O_WRONLY+O_CREAT,0666); - if( fd < 0 ) { +// try access first for named pipes + int ret = access(filename, W_OK); + if( ret ) { + int fd = ::open(filename,O_WRONLY); + if( fd < 0 ) fd = open(filename,O_WRONLY+O_CREAT,0666); + if( fd >= 0 ) { close(fd); ret = 0; } + } + if( ret ) { eprintf(_("bad file path: %s\n"), filename); return 1; } - ::close(fd); - int ret = get_file_format(); + ret = get_file_format(); if( ret > 0 ) { eprintf(_("bad file format: %s\n"), filename); return 1; @@ -1737,14 +2105,15 @@ int FFMPEG::open_encoder(const char *type, const char *spec) set_option_path(option_path, "%s/%s.opts", type, type); read_options(option_path, sopts); get_option_path(option_path, type, spec); - char format_name[BCSTRLEN], codec_name[BCTEXTLEN]; - char bsfilter[BCSTRLEN], bsargs[BCTEXTLEN]; - if( get_encoder(option_path, format_name, codec_name, bsfilter, bsargs) ) { + char format_name[BCSTRLEN], codec_name[BCTEXTLEN], bsfilter[BCTEXTLEN]; + if( get_encoder(option_path, format_name, codec_name, bsfilter) ) { eprintf(_("get_encoder failed %s:%s\n"), option_path, filename); return 1; } +#ifdef HAVE_DV if( !strcmp(codec_name, CODEC_TAG_DVSD) ) strcpy(codec_name, "dv"); +#endif else if( !strcmp(codec_name, CODEC_TAG_MJPEG) ) strcpy(codec_name, "mjpeg"); else if( !strcmp(codec_name, CODEC_TAG_JPEG) ) strcpy(codec_name, "jpeg"); @@ -1752,6 +2121,7 @@ int FFMPEG::open_encoder(const char *type, const char *spec) ff_lock("FFMPEG::open_encoder"); FFStream *fst = 0; AVStream *st = 0; + AVCodecContext *ctx = 0; const AVCodecDescriptor *codec_desc = 0; AVCodec *codec = avcodec_find_encoder_by_name(codec_name); @@ -1774,7 +2144,6 @@ int FFMPEG::open_encoder(const char *type, const char *spec) } } if( !ret ) { - AVCodecContext *ctx = st->codec; switch( codec_desc->type ) { case AVMEDIA_TYPE_AUDIO: { if( has_audio ) { @@ -1782,22 +2151,36 @@ int FFMPEG::open_encoder(const char *type, const char *spec) ret = 1; break; } - has_audio = 1; if( scan_options(asset->ff_audio_options, sopts, st) ) { eprintf(_("bad audio options %s:%s\n"), codec_name, filename); ret = 1; break; } + has_audio = 1; + ctx = avcodec_alloc_context3(codec); if( asset->ff_audio_bitrate > 0 ) { ctx->bit_rate = asset->ff_audio_bitrate; char arg[BCSTRLEN]; sprintf(arg, "%d", asset->ff_audio_bitrate); av_dict_set(&sopts, "b", arg, 0); } + else if( asset->ff_audio_quality >= 0 ) { + ctx->global_quality = asset->ff_audio_quality * FF_QP2LAMBDA; + ctx->qmin = ctx->qmax = asset->ff_audio_quality; + ctx->mb_lmin = ctx->qmin * FF_QP2LAMBDA; + ctx->mb_lmax = ctx->qmax * FF_QP2LAMBDA; + ctx->flags |= AV_CODEC_FLAG_QSCALE; + char arg[BCSTRLEN]; + av_dict_set(&sopts, "flags", "+qscale", 0); + sprintf(arg, "%d", asset->ff_audio_quality); + av_dict_set(&sopts, "qscale", arg, 0); + sprintf(arg, "%d", ctx->global_quality); + av_dict_set(&sopts, "global_quality", arg, 0); + } int aidx = ffaudio.size(); int fidx = aidx + ffvideo.size(); FFAudioStream *aud = new FFAudioStream(this, st, aidx, fidx); - ffaudio.append(aud); fst = aud; + aud->avctx = ctx; ffaudio.append(aud); fst = aud; aud->sample_rate = asset->sample_rate; ctx->channels = aud->channels = asset->channels; for( int ch=0; chchannels; ++ch ) @@ -1810,7 +2193,10 @@ int FFMPEG::open_encoder(const char *type, const char *spec) break; } ctx->time_base = st->time_base = (AVRational){1, aud->sample_rate}; - ctx->sample_fmt = codec->sample_fmts[0]; + AVSampleFormat sample_fmt = av_get_sample_fmt(asset->ff_sample_format); + if( sample_fmt == AV_SAMPLE_FMT_NONE ) + sample_fmt = codec->sample_fmts ? codec->sample_fmts[0] : AV_SAMPLE_FMT_S16; + ctx->sample_fmt = sample_fmt; uint64_t layout = av_get_default_channel_layout(ctx->channels); aud->resample_context = swr_alloc_set_opts(NULL, layout, ctx->sample_fmt, aud->sample_rate, @@ -1825,12 +2211,13 @@ int FFMPEG::open_encoder(const char *type, const char *spec) ret = 1; break; } - has_video = 1; if( scan_options(asset->ff_video_options, sopts, st) ) { eprintf(_("bad video options %s:%s\n"), codec_name, filename); ret = 1; break; } + has_video = 1; + ctx = avcodec_alloc_context3(codec); if( asset->ff_video_bitrate > 0 ) { ctx->bit_rate = asset->ff_video_bitrate; char arg[BCSTRLEN]; @@ -1842,7 +2229,7 @@ int FFMPEG::open_encoder(const char *type, const char *spec) ctx->qmin = ctx->qmax = asset->ff_video_quality; ctx->mb_lmin = ctx->qmin * FF_QP2LAMBDA; ctx->mb_lmax = ctx->qmax * FF_QP2LAMBDA; - ctx->flags |= CODEC_FLAG_QSCALE; + ctx->flags |= AV_CODEC_FLAG_QSCALE; char arg[BCSTRLEN]; av_dict_set(&sopts, "flags", "+qscale", 0); sprintf(arg, "%d", asset->ff_video_quality); @@ -1854,21 +2241,32 @@ int FFMPEG::open_encoder(const char *type, const char *spec) int fidx = vidx + ffaudio.size(); FFVideoStream *vid = new FFVideoStream(this, st, vidx, fidx); vstrm_index.append(ffidx(vidx, 0)); - ffvideo.append(vid); fst = vid; + vid->avctx = ctx; ffvideo.append(vid); fst = vid; vid->width = asset->width; - ctx->width = (vid->width+3) & ~3; vid->height = asset->height; - ctx->height = (vid->height+3) & ~3; vid->frame_rate = asset->frame_rate; + + AVPixelFormat pix_fmt = av_get_pix_fmt(asset->ff_pixel_format); + if( pix_fmt == AV_PIX_FMT_NONE ) + pix_fmt = codec->pix_fmts ? codec->pix_fmts[0] : AV_PIX_FMT_YUV420P; + ctx->pix_fmt = pix_fmt; + const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt); + int mask_w = (1<log2_chroma_w)-1; + ctx->width = (vid->width+mask_w) & ~mask_w; + 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); - ctx->pix_fmt = codec->pix_fmts ? codec->pix_fmts[0] : AV_PIX_FMT_YUV420P; AVRational frame_rate = check_frame_rate(codec, vid->frame_rate); if( !frame_rate.num || !frame_rate.den ) { eprintf(_("check_frame_rate failed %s\n"), filename); ret = 1; break; } + av_reduce(&frame_rate.num, &frame_rate.den, + frame_rate.num, frame_rate.den, INT_MAX); + ctx->framerate = (AVRational) { frame_rate.num, frame_rate.den }; ctx->time_base = (AVRational) { frame_rate.den, frame_rate.num }; + st->avg_frame_rate = frame_rate; st->time_base = ctx->time_base; vid->writing = -1; vid->interlaced = asset->interlace_mode == ILACE_MODE_TOP_FIRST || @@ -1879,15 +2277,62 @@ int FFMPEG::open_encoder(const char *type, const char *spec) eprintf(_("not audio/video, %s:%s\n"), codec_name, filename); ret = 1; } + + if( ctx ) { + AVDictionaryEntry *tag; + if( (tag=av_dict_get(sopts, "cin_stats_filename", NULL, 0)) != 0 ) { + char suffix[BCSTRLEN]; sprintf(suffix,"-%d.log",fst->fidx); + fst->stats_filename = cstrcat(2, tag->value, suffix); + } + if( (tag=av_dict_get(sopts, "flags", NULL, 0)) != 0 ) { + int pass = fst->pass; + char *cp = tag->value; + while( *cp ) { + int ch = *cp++, pfx = ch=='-' ? -1 : ch=='+' ? 1 : 0; + if( !isalnum(!pfx ? ch : (ch=*cp++)) ) continue; + char id[BCSTRLEN], *bp = id, *ep = bp+sizeof(id)-1; + for( *bp++=ch; isalnum(ch=*cp); ++cp ) + if( bp < ep ) *bp++ = ch; + *bp = 0; + if( !strcmp(id, "pass1") ) { + pass = pfx<0 ? (pass&~1) : pfx>0 ? (pass|1) : 1; + } + else if( !strcmp(id, "pass2") ) { + pass = pfx<0 ? (pass&~2) : pfx>0 ? (pass|2) : 2; + } + } + if( (fst->pass=pass) ) { + if( pass & 1 ) ctx->flags |= AV_CODEC_FLAG_PASS1; + if( pass & 2 ) ctx->flags |= AV_CODEC_FLAG_PASS2; + } + } + } } if( !ret ) { if( fmt_ctx->oformat->flags & AVFMT_GLOBALHEADER ) - st->codec->flags |= CODEC_FLAG_GLOBAL_HEADER; - + ctx->flags |= AV_CODEC_FLAG_GLOBAL_HEADER; + if( fst->stats_filename && (ret=fst->init_stats_file()) ) + eprintf(_("error: stats file = %s\n"), fst->stats_filename); + } + if( !ret ) { av_dict_set(&sopts, "cin_bitrate", 0, 0); av_dict_set(&sopts, "cin_quality", 0, 0); - ret = avcodec_open2(st->codec, codec, &sopts); + if( !av_dict_get(sopts, "threads", NULL, 0) ) + ctx->thread_count = ff_cpus(); + ret = avcodec_open2(ctx, codec, &sopts); + if( ret >= 0 ) { + ret = avcodec_parameters_from_context(st->codecpar, ctx); + if( ret < 0 ) + fprintf(stderr, "Could not copy the stream parameters\n"); + } + if( ret >= 0 ) { +_Pragma("GCC diagnostic ignored \"-Wdeprecated-declarations\"") + ret = avcodec_copy_context(st->codec, ctx); +_Pragma("GCC diagnostic warning \"-Wdeprecated-declarations\"") + if( ret < 0 ) + fprintf(stderr, "Could not copy the stream context\n"); + } if( ret < 0 ) { ff_err(ret,"FFMPEG::open_encoder"); eprintf(_("open failed %s:%s\n"), codec_name, filename); @@ -1896,9 +2341,15 @@ int FFMPEG::open_encoder(const char *type, const char *spec) else ret = 0; } - if( !ret ) { - if( fst && bsfilter[0] ) - fst->add_bsfilter(bsfilter, !bsargs[0] ? 0 : bsargs); + if( !ret && fst && bsfilter[0] ) { + ret = av_bsf_list_parse_str(bsfilter, &fst->bsfc); + if( ret < 0 ) { + ff_err(ret,"FFMPEG::open_encoder"); + eprintf(_("bitstream filter failed %s:\n%s\n"), filename, bsfilter); + ret = 1; + } + else + ret = 0; } if( !ret ) @@ -1930,42 +2381,45 @@ int FFMPEG::decode_activate() for( int aidx=0; aidxnudge = AV_NOPTS_VALUE; // set nudges for each program stream set + const int64_t min_nudge = INT64_MIN+1; int npgrms = fmt_ctx->nb_programs; for( int i=0; iprograms[i]; // first start time video stream - int64_t vstart_time = -1, astart_time = -1; + int64_t vstart_time = min_nudge, astart_time = min_nudge; for( int j=0; j<(int)pgrm->nb_stream_indexes; ++j ) { int fidx = pgrm->stream_index[j]; AVStream *st = fmt_ctx->streams[fidx]; - AVCodecContext *avctx = st->codec; - if( avctx->codec_type == AVMEDIA_TYPE_VIDEO ) { + AVCodecParameters *avpar = st->codecpar; + if( avpar->codec_type == AVMEDIA_TYPE_VIDEO ) { if( st->start_time == AV_NOPTS_VALUE ) continue; - if( vstart_time > st->start_time ) continue; - vstart_time = st->start_time; + if( vstart_time < st->start_time ) + vstart_time = st->start_time; continue; } - if( avctx->codec_type == AVMEDIA_TYPE_VIDEO ) { + if( avpar->codec_type == AVMEDIA_TYPE_AUDIO ) { if( st->start_time == AV_NOPTS_VALUE ) continue; - if( astart_time > st->start_time ) continue; - astart_time = st->start_time; + if( astart_time < st->start_time ) + astart_time = st->start_time; continue; } } - // match program streams to max start_time - int64_t nudge = vstart_time > astart_time ? vstart_time : astart_time; + //since frame rate is much more grainy than sample rate, it is better to + // align using video, so that total absolute error is minimized. + int64_t nudge = vstart_time > min_nudge ? vstart_time : + astart_time > min_nudge ? astart_time : AV_NOPTS_VALUE; for( int j=0; j<(int)pgrm->nb_stream_indexes; ++j ) { int fidx = pgrm->stream_index[j]; AVStream *st = fmt_ctx->streams[fidx]; - AVCodecContext *avctx = st->codec; - if( avctx->codec_type == AVMEDIA_TYPE_VIDEO ) { + AVCodecParameters *avpar = st->codecpar; + if( avpar->codec_type == AVMEDIA_TYPE_VIDEO ) { for( int k=0; kfidx != fidx ) continue; ffvideo[k]->nudge = nudge; } continue; } - if( avctx->codec_type == AVMEDIA_TYPE_AUDIO ) { + if( avpar->codec_type == AVMEDIA_TYPE_AUDIO ) { for( int k=0; kfidx != fidx ) continue; ffaudio[k]->nudge = nudge; @@ -1975,39 +2429,44 @@ int FFMPEG::decode_activate() } } // set nudges for any streams not yet set - int64_t vstart_time = 0, astart_time = 0; + int64_t vstart_time = min_nudge, astart_time = min_nudge; int nstreams = fmt_ctx->nb_streams; for( int i=0; istreams[i]; - AVCodecContext *avctx = st->codec; - switch( avctx->codec_type ) { + AVCodecParameters *avpar = st->codecpar; + switch( avpar->codec_type ) { case AVMEDIA_TYPE_VIDEO: { if( st->start_time == AV_NOPTS_VALUE ) continue; int vidx = ffvideo.size(); while( --vidx >= 0 && ffvideo[vidx]->fidx != i ); - if( vidx >= 0 && ffvideo[vidx]->nudge != AV_NOPTS_VALUE ) continue; - if( vstart_time >= st->start_time ) continue; - vstart_time = st->start_time; + if( vidx < 0 ) continue; + if( ffvideo[vidx]->nudge != AV_NOPTS_VALUE ) continue; + if( vstart_time < st->start_time ) + vstart_time = st->start_time; break; } case AVMEDIA_TYPE_AUDIO: { if( st->start_time == AV_NOPTS_VALUE ) continue; int aidx = ffaudio.size(); while( --aidx >= 0 && ffaudio[aidx]->fidx != i ); - if( aidx >= 0 && ffaudio[aidx]->nudge != AV_NOPTS_VALUE ) continue; - if( astart_time >= st->start_time ) continue; - astart_time = st->start_time; + if( aidx < 0 ) continue; + if( ffaudio[aidx]->frame_sz < avpar->frame_size ) + ffaudio[aidx]->frame_sz = avpar->frame_size; + if( ffaudio[aidx]->nudge != AV_NOPTS_VALUE ) continue; + if( astart_time < st->start_time ) + astart_time = st->start_time; break; } default: break; } } - int64_t nudge = vstart_time > astart_time ? vstart_time : astart_time; + int64_t nudge = vstart_time > min_nudge ? vstart_time : + astart_time > min_nudge ? astart_time : 0; for( int vidx=0; vidxnudge != AV_NOPTS_VALUE ) continue; - ffvideo[vidx]->nudge = nudge; + if( ffvideo[vidx]->nudge == AV_NOPTS_VALUE ) + ffvideo[vidx]->nudge = nudge; } for( int aidx=0; aidxnudge != AV_NOPTS_VALUE ) continue; - ffaudio[aidx]->nudge = nudge; + if( ffaudio[aidx]->nudge == AV_NOPTS_VALUE ) + ffaudio[aidx]->nudge = nudge; } decoding = 1; } @@ -2020,9 +2479,9 @@ int FFMPEG::encode_activate() if( encoding < 0 ) { encoding = 0; if( !(fmt_ctx->flags & AVFMT_NOFILE) && - (ret=avio_open(&fmt_ctx->pb, fmt_ctx->filename, AVIO_FLAG_WRITE)) < 0 ) { + (ret=avio_open(&fmt_ctx->pb, fmt_ctx->url, AVIO_FLAG_WRITE)) < 0 ) { ff_err(ret, "FFMPEG::encode_activate: err opening : %s\n", - fmt_ctx->filename); + fmt_ctx->url); return -1; } @@ -2036,7 +2495,7 @@ int FFMPEG::encode_activate() while( --pi >= 0 && fmt_ctx->programs[pi]->id != prog_id ); AVDictionary **meta = &prog->metadata; av_dict_set(meta, "service_provider", "cin5", 0); - const char *path = fmt_ctx->filename, *bp = strrchr(path,'/'); + const char *path = fmt_ctx->url, *bp = strrchr(path,'/'); if( bp ) path = bp + 1; av_dict_set(meta, "title", path, 0); @@ -2068,7 +2527,7 @@ int FFMPEG::encode_activate() ret = avformat_write_header(fmt_ctx, &fopts); if( ret < 0 ) { ff_err(ret, "FFMPEG::encode_activate: write header failed %s\n", - fmt_ctx->filename); + fmt_ctx->url); return -1; } av_dict_free(&fopts); @@ -2169,16 +2628,12 @@ void FFMPEG::flow_ctl() int FFMPEG::mux_audio(FFrame *frm) { - FFPacket pkt; FFStream *fst = frm->fst; - AVCodecContext *ctx = fst->st->codec; + AVCodecContext *ctx = fst->avctx; AVFrame *frame = *frm; AVRational tick_rate = {1, ctx->sample_rate}; frame->pts = av_rescale_q(frm->position, tick_rate, ctx->time_base); - int got_packet = 0; - int ret = fst->encode_frame(pkt, frame, got_packet); - if( ret >= 0 && got_packet ) - ret = fst->write_packet(pkt); + int ret = fst->encode_frame(frame); if( ret < 0 ) ff_err(ret, "FFMPEG::mux_audio"); return ret >= 0 ? 0 : 1; @@ -2186,14 +2641,10 @@ int FFMPEG::mux_audio(FFrame *frm) int FFMPEG::mux_video(FFrame *frm) { - FFPacket pkt; FFStream *fst = frm->fst; AVFrame *frame = *frm; frame->pts = frm->position; - int got_packet = 0; - int ret = fst->encode_frame(pkt, frame, got_packet); - if( ret >= 0 && got_packet ) - ret = fst->write_packet(pkt); + int ret = fst->encode_frame(frame); if( ret < 0 ) ff_err(ret, "FFMPEG::mux_video"); return ret >= 0 ? 0 : 1; @@ -2210,7 +2661,7 @@ void FFMPEG::mux() if( fst->frm_count < 3 ) { demand = 1; flow_on(); } FFrame *frm = fst->frms.first; if( !frm ) { if( !done ) return; continue; } - double tm = to_secs(frm->position, fst->st->codec->time_base); + double tm = to_secs(frm->position, fst->avctx->time_base); if( atm < 0 || tm < atm ) { atm = tm; afrm = frm; } } for( int i=0; ifrm_count < 2 ) { demand = 1; flow_on(); } FFrame *frm = fst->frms.first; if( !frm ) { if( !done ) return; continue; } - double tm = to_secs(frm->position, fst->st->codec->time_base); + double tm = to_secs(frm->position, fst->avctx->time_base); if( vtm < 0 || tm < vtm ) { vtm = tm; vfrm = frm; } } if( !demand ) flow_off(); if( !afrm && !vfrm ) break; int v = !afrm ? -1 : !vfrm ? 1 : av_compare_ts( - vfrm->position, vfrm->fst->st->codec->time_base, - afrm->position, afrm->fst->st->codec->time_base); + vfrm->position, vfrm->fst->avctx->time_base, + afrm->position, afrm->fst->avctx->time_base); FFrame *frm = v <= 0 ? vfrm : afrm; if( frm == afrm ) mux_audio(frm); if( frm == vfrm ) mux_video(frm); @@ -2240,6 +2691,10 @@ void FFMPEG::run() mux_lock->lock("FFMPEG::run"); if( !done ) mux(); } + for( int i=0; idrain(); + for( int i=0; idrain(); mux(); for( int i=0; iflush(); @@ -2271,7 +2726,7 @@ int FFMPEG::ff_sample_rate(int stream) const char* FFMPEG::ff_audio_format(int stream) { AVStream *st = ffaudio[stream]->st; - AVCodecID id = st->codec->codec_id; + AVCodecID id = st->codecpar->codec_id; const AVCodecDescriptor *desc = avcodec_descriptor_get(id); return desc ? desc->name : _("Unknown"); } @@ -2299,7 +2754,7 @@ int FFMPEG::ff_audio_for_video(int vstream, int astream, int64_t &channel_mask) for( int j=0; pidx<0 && j<(int)pgrm->nb_stream_indexes; ++j ) { int st_idx = pgrm->stream_index[j]; AVStream *st = fmt_ctx->streams[st_idx]; - if( st->codec->codec_type != AVMEDIA_TYPE_VIDEO ) continue; + if( st->codecpar->codec_type != AVMEDIA_TYPE_VIDEO ) continue; if( st_idx == vidx ) pidx = i; } } @@ -2310,7 +2765,7 @@ int FFMPEG::ff_audio_for_video(int vstream, int astream, int64_t &channel_mask) for( int j=0; j<(int)pgrm->nb_stream_indexes; ++j ) { int aidx = pgrm->stream_index[j]; AVStream *st = fmt_ctx->streams[aidx]; - if( st->codec->codec_type != AVMEDIA_TYPE_AUDIO ) continue; + if( st->codecpar->codec_type != AVMEDIA_TYPE_AUDIO ) continue; if( astream > 0 ) { --astream; continue; } int astrm = -1; for( int i=0; astrm<0 && ist; - return st->codec->coded_width; + return ffvideo[stream]->avctx->coded_width; } int FFMPEG::ff_coded_height(int stream) { - AVStream *st = ffvideo[stream]->st; - return st->codec->coded_height; + return ffvideo[stream]->avctx->coded_height; } float FFMPEG::ff_aspect_ratio(int stream) @@ -2381,7 +2834,7 @@ float FFMPEG::ff_aspect_ratio(int stream) const char* FFMPEG::ff_video_format(int stream) { AVStream *st = ffvideo[stream]->st; - AVCodecID id = st->codec->codec_id; + AVCodecID id = st->codecpar->codec_id; const AVCodecDescriptor *desc = avcodec_descriptor_get(id); return desc ? desc->name : _("Unknown"); } @@ -2401,14 +2854,17 @@ int FFMPEG::ff_video_pid(int stream) return ffvideo[stream]->st->id; } +int FFMPEG::ff_video_mpeg_color_range(int stream) +{ + return ffvideo[stream]->st->codecpar->color_range == AVCOL_RANGE_MPEG ? 1 : 0; +} int FFMPEG::ff_cpus() { return file_base->file->cpus; } -int FFVideoStream::create_filter(const char *filter_spec, - AVCodecContext *src_ctx, AVCodecContext *sink_ctx) +int FFVideoStream::create_filter(const char *filter_spec, AVCodecParameters *avpar) { avfilter_register_all(); const char *sp = filter_spec; @@ -2416,21 +2872,22 @@ int FFVideoStream::create_filter(const char *filter_spec, int i = sizeof(filter_name); while( --i>=0 && *sp!=0 && !strchr(" \t:=,",*sp) ) *np++ = *sp++; *np = 0; - AVFilter *filter = !filter_name[0] ? 0 : avfilter_get_by_name(filter_name); + const AVFilter *filter = !filter_name[0] ? 0 : avfilter_get_by_name(filter_name); if( !filter || avfilter_pad_get_type(filter->inputs,0) != AVMEDIA_TYPE_VIDEO ) { ff_err(AVERROR(EINVAL), "FFVideoStream::create_filter: %s\n", filter_spec); return -1; } filter_graph = avfilter_graph_alloc(); - AVFilter *buffersrc = avfilter_get_by_name("buffer"); - AVFilter *buffersink = avfilter_get_by_name("buffersink"); + const AVFilter *buffersrc = avfilter_get_by_name("buffer"); + const AVFilter *buffersink = avfilter_get_by_name("buffersink"); int ret = 0; char args[BCTEXTLEN]; + AVPixelFormat pix_fmt = (AVPixelFormat)avpar->format; snprintf(args, sizeof(args), "video_size=%dx%d:pix_fmt=%d:time_base=%d/%d:pixel_aspect=%d/%d", - src_ctx->width, src_ctx->height, src_ctx->pix_fmt, - src_ctx->time_base.num, src_ctx->time_base.den, - src_ctx->sample_aspect_ratio.num, src_ctx->sample_aspect_ratio.den); + avpar->width, avpar->height, (int)pix_fmt, + st->time_base.num, st->time_base.den, + avpar->sample_aspect_ratio.num, avpar->sample_aspect_ratio.den); if( ret >= 0 ) ret = avfilter_graph_create_filter(&buffersrc_ctx, buffersrc, "in", args, NULL, filter_graph); @@ -2439,7 +2896,7 @@ int FFVideoStream::create_filter(const char *filter_spec, NULL, NULL, filter_graph); if( ret >= 0 ) ret = av_opt_set_bin(buffersink_ctx, "pix_fmts", - (uint8_t*)&sink_ctx->pix_fmt, sizeof(sink_ctx->pix_fmt), + (uint8_t*)&pix_fmt, sizeof(pix_fmt), AV_OPT_SEARCH_CHILDREN); if( ret < 0 ) ff_err(ret, "FFVideoStream::create_filter"); @@ -2448,8 +2905,7 @@ int FFVideoStream::create_filter(const char *filter_spec, return ret >= 0 ? 0 : -1; } -int FFAudioStream::create_filter(const char *filter_spec, - AVCodecContext *src_ctx, AVCodecContext *sink_ctx) +int FFAudioStream::create_filter(const char *filter_spec, AVCodecParameters *avpar) { avfilter_register_all(); const char *sp = filter_spec; @@ -2457,19 +2913,20 @@ int FFAudioStream::create_filter(const char *filter_spec, int i = sizeof(filter_name); while( --i>=0 && *sp!=0 && !strchr(" \t:=,",*sp) ) *np++ = *sp++; *np = 0; - AVFilter *filter = !filter_name[0] ? 0 : avfilter_get_by_name(filter_name); + const AVFilter *filter = !filter_name[0] ? 0 : avfilter_get_by_name(filter_name); if( !filter || avfilter_pad_get_type(filter->inputs,0) != AVMEDIA_TYPE_AUDIO ) { ff_err(AVERROR(EINVAL), "FFAudioStream::create_filter: %s\n", filter_spec); return -1; } filter_graph = avfilter_graph_alloc(); - AVFilter *buffersrc = avfilter_get_by_name("abuffer"); - AVFilter *buffersink = avfilter_get_by_name("abuffersink"); + const AVFilter *buffersrc = avfilter_get_by_name("abuffer"); + const AVFilter *buffersink = avfilter_get_by_name("abuffersink"); int ret = 0; char args[BCTEXTLEN]; + AVSampleFormat sample_fmt = (AVSampleFormat)avpar->format; snprintf(args, sizeof(args), "time_base=%d/%d:sample_rate=%d:sample_fmt=%s:channel_layout=0x%jx", - src_ctx->time_base.num, src_ctx->time_base.den, src_ctx->sample_rate, - av_get_sample_fmt_name(src_ctx->sample_fmt), src_ctx->channel_layout); + st->time_base.num, st->time_base.den, avpar->sample_rate, + av_get_sample_fmt_name(sample_fmt), avpar->channel_layout); if( ret >= 0 ) ret = avfilter_graph_create_filter(&buffersrc_ctx, buffersrc, "in", args, NULL, filter_graph); @@ -2478,15 +2935,15 @@ int FFAudioStream::create_filter(const char *filter_spec, NULL, NULL, filter_graph); if( ret >= 0 ) ret = av_opt_set_bin(buffersink_ctx, "sample_fmts", - (uint8_t*)&sink_ctx->sample_fmt, sizeof(sink_ctx->sample_fmt), + (uint8_t*)&sample_fmt, sizeof(sample_fmt), AV_OPT_SEARCH_CHILDREN); if( ret >= 0 ) ret = av_opt_set_bin(buffersink_ctx, "channel_layouts", - (uint8_t*)&sink_ctx->channel_layout, - sizeof(sink_ctx->channel_layout), AV_OPT_SEARCH_CHILDREN); + (uint8_t*)&avpar->channel_layout, + sizeof(avpar->channel_layout), AV_OPT_SEARCH_CHILDREN); if( ret >= 0 ) ret = av_opt_set_bin(buffersink_ctx, "sample_rates", - (uint8_t*)&sink_ctx->sample_rate, sizeof(sink_ctx->sample_rate), + (uint8_t*)&sample_rate, sizeof(sample_rate), AV_OPT_SEARCH_CHILDREN); if( ret < 0 ) ff_err(ret, "FFAudioStream::create_filter"); @@ -2527,46 +2984,6 @@ int FFStream::create_filter(const char *filter_spec) return ret; } -void FFStream::add_bsfilter(const char *bsf, const char *ap) -{ - bsfilter.append(new BSFilter(bsf,ap)); -} - -int FFStream::bs_filter(AVPacket *pkt) -{ - if( !bsfilter.size() ) return 0; - av_packet_split_side_data(pkt); - - int ret = 0; - for( int i=0; ibsfc, - st->codec, bsfilter[i]->args, &bspkt.data, &bspkt.size, - pkt->data, pkt->size, pkt->flags & AV_PKT_FLAG_KEY); - if( ret < 0 ) break; - int size = bspkt.size; - uint8_t *data = bspkt.data; - if( !ret && bspkt.data != pkt->data ) { - size = bspkt.size; - data = (uint8_t *)av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE); - if( !data ) { ret = AVERROR(ENOMEM); break; } - memcpy(data, bspkt.data, size); - memset(data+size, 0, FF_INPUT_BUFFER_PADDING_SIZE); - ret = 1; - } - if( ret > 0 ) { - pkt->side_data = 0; pkt->side_data_elems = 0; - av_packet_unref(pkt); - ret = av_packet_from_data(&bspkt, data, size); - if( ret < 0 ) break; - } - *pkt = bspkt; - } - if( ret < 0 ) - ff_err(ret,"FFStream::bs_filter"); - return ret; -} - int FFMPEG::scan(IndexState *index_state, int64_t *scan_position, int *canceled) { AVPacket pkt; @@ -2575,6 +2992,7 @@ int FFMPEG::scan(IndexState *index_state, int64_t *scan_position, int *canceled) if( !frame ) { fprintf(stderr,"FFMPEG::scan: "); fprintf(stderr,_("av_frame_alloc failed\n")); + fprintf(stderr,"FFMPEG::scan:file=%s\n", file_base->asset->path); return -1; } @@ -2582,17 +3000,64 @@ int FFMPEG::scan(IndexState *index_state, int64_t *scan_position, int *canceled) index_state->add_audio_markers(ffaudio.size()); for( int i=0; i<(int)fmt_ctx->nb_streams; ++i ) { + int ret = 0; AVDictionary *copts = 0; av_dict_copy(&copts, opts, 0); AVStream *st = fmt_ctx->streams[i]; - AVCodecID codec_id = st->codec->codec_id; + AVCodecID codec_id = st->codecpar->codec_id; AVCodec *decoder = avcodec_find_decoder(codec_id); - if( avcodec_open2(st->codec, decoder, &copts) < 0 ) { - fprintf(stderr,"FFMPEG::scan: "); - fprintf(stderr,_("codec open failed\n")); + AVCodecContext *avctx = avcodec_alloc_context3(decoder); + if( !avctx ) { + eprintf(_("cant allocate codec context\n")); + ret = AVERROR(ENOMEM); + } + if( ret >= 0 ) { + avcodec_parameters_to_context(avctx, st->codecpar); + if( !av_dict_get(copts, "threads", NULL, 0) ) + avctx->thread_count = ff_cpus(); + ret = avcodec_open2(avctx, decoder, &copts); } av_dict_free(&copts); + if( ret >= 0 ) { + AVCodecParameters *avpar = st->codecpar; + switch( avpar->codec_type ) { + case AVMEDIA_TYPE_VIDEO: { + int vidx = ffvideo.size(); + while( --vidx>=0 && ffvideo[vidx]->fidx != i ); + if( vidx < 0 ) break; + ffvideo[vidx]->avctx = avctx; + continue; } + case AVMEDIA_TYPE_AUDIO: { + int aidx = ffaudio.size(); + while( --aidx>=0 && ffaudio[aidx]->fidx != i ); + if( aidx < 0 ) break; + ffaudio[aidx]->avctx = avctx; + continue; } + default: break; + } + } + fprintf(stderr,"FFMPEG::scan: "); + fprintf(stderr,_("codec open failed\n")); + fprintf(stderr,"FFMPEG::scan:file=%s\n", file_base->asset->path); + avcodec_free_context(&avctx); + } + + decode_activate(); + for( int i=0; i<(int)fmt_ctx->nb_streams; ++i ) { + AVStream *st = fmt_ctx->streams[i]; + AVCodecParameters *avpar = st->codecpar; + if( avpar->codec_type != AVMEDIA_TYPE_AUDIO ) continue; + int64_t tstmp = st->start_time; + if( tstmp == AV_NOPTS_VALUE ) continue; + int aidx = ffaudio.size(); + while( --aidx>=0 && ffaudio[aidx]->fidx != i ); + if( aidx < 0 ) continue; + FFAudioStream *aud = ffaudio[aidx]; + tstmp -= aud->nudge; + double secs = to_secs(tstmp, st->time_base); + aud->curr_pos = secs * aud->sample_rate + 0.5; } + int errs = 0; for( int64_t count=0; !*canceled; ++count ) { av_packet_unref(&pkt); @@ -2611,15 +3076,16 @@ int FFMPEG::scan(IndexState *index_state, int64_t *scan_position, int *canceled) int i = pkt.stream_index; if( i < 0 || i >= (int)fmt_ctx->nb_streams ) continue; AVStream *st = fmt_ctx->streams[i]; - AVCodecContext *avctx = st->codec; if( pkt.pos > *scan_position ) *scan_position = pkt.pos; - switch( avctx->codec_type ) { + AVCodecParameters *avpar = st->codecpar; + switch( avpar->codec_type ) { case AVMEDIA_TYPE_VIDEO: { int vidx = ffvideo.size(); while( --vidx>=0 && ffvideo[vidx]->fidx != i ); if( vidx < 0 ) break; FFVideoStream *vid = ffvideo[vidx]; + if( !vid->avctx ) break; int64_t tstmp = pkt.dts; if( tstmp == AV_NOPTS_VALUE ) tstmp = pkt.pts; if( tstmp != AV_NOPTS_VALUE && (pkt.flags & AV_PKT_FLAG_KEY) && pkt.pos > 0 ) { @@ -2630,15 +3096,9 @@ int FFMPEG::scan(IndexState *index_state, int64_t *scan_position, int *canceled) index_state->put_video_mark(vidx, frm, pkt.pos); } #if 0 - while( pkt.size > 0 ) { - av_frame_unref(frame); - int got_frame = 0; - int ret = vid->decode_frame(&pkt, frame, got_frame); - if( ret <= 0 ) break; -// if( got_frame ) {} - pkt.data += ret; - pkt.size -= ret; - } + ret = avcodec_send_packet(vid->avctx, pkt); + if( ret < 0 ) break; + while( (ret=vid->decode_frame(frame)) > 0 ) {} #endif break; } case AVMEDIA_TYPE_AUDIO: { @@ -2646,37 +3106,40 @@ int FFMPEG::scan(IndexState *index_state, int64_t *scan_position, int *canceled) while( --aidx>=0 && ffaudio[aidx]->fidx != i ); if( aidx < 0 ) break; FFAudioStream *aud = ffaudio[aidx]; + if( !aud->avctx ) break; int64_t tstmp = pkt.pts; if( tstmp == AV_NOPTS_VALUE ) tstmp = pkt.dts; if( tstmp != AV_NOPTS_VALUE && (pkt.flags & AV_PKT_FLAG_KEY) && pkt.pos > 0 ) { if( aud->nudge != AV_NOPTS_VALUE ) tstmp -= aud->nudge; double secs = to_secs(tstmp, st->time_base); int64_t sample = secs * aud->sample_rate + 0.5; - if( sample < 0 ) sample = 0; - index_state->put_audio_mark(aidx, sample, pkt.pos); + if( sample >= 0 ) + index_state->put_audio_mark(aidx, sample, pkt.pos); } - while( pkt.size > 0 ) { - int ch = aud->channel0, nch = aud->channels; - int64_t pos = index_state->pos(ch); - if( pos != aud->curr_pos ) { + ret = avcodec_send_packet(aud->avctx, &pkt); + if( ret < 0 ) break; + int ch = aud->channel0, nch = aud->channels; + int64_t pos = index_state->pos(ch); + if( pos != aud->curr_pos ) { if( abs(pos-aud->curr_pos) > 1 ) printf("audio%d pad %jd %jd (%jd)\n", aud->idx, pos, aud->curr_pos, pos-aud->curr_pos); - index_state->pad_data(ch, nch, aud->curr_pos); - } - av_frame_unref(frame); - int got_frame = 0; - int ret = aud->decode_frame(&pkt, frame, got_frame); - if( ret <= 0 ) break; - if( got_frame && frame->channels == nch ) { - float *samples; - int len = aud->get_samples(samples, - &frame->extended_data[0], frame->nb_samples); + index_state->pad_data(ch, nch, aud->curr_pos); + } + while( (ret=aud->decode_frame(frame)) > 0 ) { + //if( frame->channels != nch ) break; + aud->init_swr(frame->channels, frame->format, frame->sample_rate); + float *samples; + int len = aud->get_samples(samples, + &frame->extended_data[0], frame->nb_samples); + pos = aud->curr_pos; + if( (aud->curr_pos += len) >= 0 ) { + if( pos < 0 ) { + samples += -pos * nch; + len = aud->curr_pos; + } for( int i=0; iput_data(ch+i,nch,samples+i,len); - aud->curr_pos += len; } - pkt.data += ret; - pkt.size -= ret; } break; } default: break;