#include "file.h"
#include "ffmpeg.h"
#include "indexfile.h"
+#include "interlacemodes.h"
#include "libdv.h"
#include "libmjpeg.h"
#include "mainerror.h"
#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");
{
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;
}
this->fidx = fidx;
frm_lock = new Mutex("FFStream::frm_lock");
fmt_ctx = 0;
+ avctx = 0;
filter_graph = 0;
buffersrc_ctx = 0;
buffersink_ctx = 0;
flushed = 0;
need_packet = 1;
frame = fframe = 0;
+ bsfc = 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;
}
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 ) {
+ av_codec_set_pkt_timebase(avctx, st->time_base);
+ if( decoder->capabilities & AV_CODEC_CAP_DR1 )
+ avctx->flags |= CODEC_FLAG_EMU_EDGE;
+ avcodec_parameters_to_context(avctx, st->codecpar);
+ ret = avcodec_open2(avctx, decoder, &copts);
+ }
+ if( ret >= 0 ) {
reading = 1;
+ }
else
eprintf(_("open decoder failed\n"));
}
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;
{
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;
}
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()) ) {
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;
+ }
+ 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 )
ff_err(ret, "FFStream::flush");
return ret >= 0 ? 0 : 1;
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;
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);
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;
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<<ichs) - 1;
+ uint64_t olayout = av_get_default_channel_layout(channels);
+ if( !olayout ) olayout = ((uint64_t)1<<channels) - 1;
+ resample_context = swr_alloc_set_opts(NULL,
+ olayout, AV_SAMPLE_FMT_FLT, sample_rate,
+ ilayout, (AVSampleFormat)ifmt, irate,
+ 0, NULL);
+ if( resample_context )
+ swr_init(resample_context);
+}
+
int FFAudioStream::get_samples(float *&samples, uint8_t **data, int len)
{
samples = *(float **)data;
return len;
}
-int FFAudioStream::decode_frame(AVPacket *pkt, AVFrame *frame, int &got_frame)
+int FFAudioStream::decode_frame(AVFrame *frame)
{
int first_frame = seeked; seeked = 0;
- int ret = avcodec_decode_audio4(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_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 = 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 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;
+ frame_sz = avctx->codec->capabilities & 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; ch<nch; ++ch )
write(sp[ch], len, ch);
return put_inp(len);
int FFAudioStream::init_frame(AVFrame *frame)
{
- AVCodecContext *ctx = st->codec;
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");
}
if( mbsz < len ) mbsz = len;
int64_t end_pos = pos + len;
- int ret = 0;
- for( int i=0; ret>=0 && !flushed && curr_pos<end_pos && i<MAX_RETRY; ++i ) {
+ int ret = 0, i = len / frame_sz + MAX_RETRY;
+ while( ret>=0 && !flushed && curr_pos<end_pos && --i>=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;
}
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;
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()
frame_rate = 0;
aspect_ratio = 0;
length = 0;
+ interlaced = 0;
+ top_field_first = 0;
}
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 = 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 1;
}
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<MAX_RETRY; ++i ) {
+ int i = MAX_RETRY + pos - curr_pos;
+ while( ret>=0 && !flushed && curr_pos<=pos && --i>=0 ) {
ret = read_frame(frame);
if( ret > 0 ) ++curr_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;
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;
}
return ret >= 0 ? 0 : 1;
}
-int FFVideoStream::encode_frame(AVPacket *pkt, AVFrame *frame, int &got_packet)
+int FFVideoStream::drain()
{
- 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 0;
+}
+
+int FFVideoStream::encode_frame(AVFrame *frame)
+{
+ if( frame ) {
+ frame->interlaced_frame = interlaced;
+ frame->top_field_first = top_field_first;
}
- 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)
if( bits > max_bits ) max_bits = bits;
}
int imodel = pix_fmt_to_color_model(ifmt);
- if( imodel < 0 ) {
- int cmodel = frame->get_color_model();
- BC_CModels::is_yuv(cmodel) ?
+ int imodel_is_yuv = BC_CModels::is_yuv(imodel);
+ int cmodel = frame->get_color_model();
+ int cmodel_is_yuv = BC_CModels::is_yuv(cmodel);
+ if( imodel < 0 || imodel_is_yuv != cmodel_is_yuv ) {
+ imodel = cmodel_is_yuv ?
(BC_CModels::has_alpha(cmodel) ?
BC_AYUV16161616 :
(max_bits > 8 ? BC_AYUV16161616 : BC_YUV444P)) :
if( !convert_vframe_picture(frame, op) ) return 1;
// use indirect transfer
int cmodel = frame->get_color_model();
- int bits = BC_CModels::calculate_pixelsize(cmodel) * 8;
- bits /= BC_CModels::components(cmodel);
+ int max_bits = BC_CModels::calculate_pixelsize(cmodel) * 8;
+ max_bits /= BC_CModels::components(cmodel);
AVPixelFormat ofmt = (AVPixelFormat)op->format;
int imodel = pix_fmt_to_color_model(ofmt);
- if( imodel < 0 ) {
- imodel =
- BC_CModels::is_yuv(cmodel) ?
- (BC_CModels::has_alpha(cmodel) ?
+ int imodel_is_yuv = BC_CModels::is_yuv(imodel);
+ int cmodel_is_yuv = BC_CModels::is_yuv(cmodel);
+ if( imodel < 0 || imodel_is_yuv != cmodel_is_yuv ) {
+ imodel = cmodel_is_yuv ?
+ (BC_CModels::has_alpha(cmodel) ?
BC_AYUV16161616 :
- (bits > 8 ? BC_AYUV16161616 : BC_YUV444P)) :
+ (max_bits > 8 ? BC_AYUV16161616 : BC_YUV444P)) :
(BC_CModels::has_alpha(cmodel) ?
- (bits > 8 ? BC_RGBA16161616 : BC_RGBA8888) :
- (bits > 8 ? BC_RGB161616 : BC_RGB888)) ;
+ (max_bits > 8 ? BC_RGBA16161616 : BC_RGBA8888) :
+ (max_bits > 8 ? BC_RGB161616 : BC_RGB888)) ;
}
VFrame vframe(frame->get_w(), frame->get_h(), imodel);
vframe.transfer_from(frame);
{
#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);
- return (AVRational){(int)h, (int)w};
+ return (AVRational){(int)w, (int)h};
#else
// square pixels
return (AVRational){1, 1};
int FFMPEG::get_file_format()
{
- int ret = 0;
+ char audio_muxer[BCSTRLEN], video_muxer[BCSTRLEN];
char audio_format[BCSTRLEN], video_format[BCSTRLEN];
- file_format[0] = audio_format[0] = video_format[0] = 0;
+ audio_muxer[0] = audio_format[0] = 0;
+ video_muxer[0] = video_format[0] = 0;
Asset *asset = file_base->asset;
- if( !ret && asset->audio_data )
- ret = get_format(audio_format, "audio", asset->acodec);
- if( !ret && asset->video_data )
- ret = get_format(video_format, "video", asset->vcodec);
- if( !ret && !audio_format[0] && !video_format[0] )
+ int ret = asset ? 0 : 1;
+ if( !ret && asset->audio_data ) {
+ if( !(ret=get_format(audio_format, "audio", asset->acodec)) ) {
+ if( get_format(audio_muxer, "format", audio_format) ) {
+ strcpy(audio_muxer, audio_format);
+ audio_format[0] = 0;
+ }
+ }
+ }
+ if( !ret && asset->video_data ) {
+ if( !(ret=get_format(video_format, "video", asset->vcodec)) ) {
+ if( get_format(video_muxer, "format", video_format) ) {
+ strcpy(video_muxer, video_format);
+ video_format[0] = 0;
+ }
+ }
+ }
+ if( !ret && !audio_muxer[0] && !video_muxer[0] )
ret = 1;
+ if( !ret && audio_muxer[0] && video_muxer[0] &&
+ strcmp(audio_muxer, video_muxer) ) ret = -1;
if( !ret && audio_format[0] && video_format[0] &&
strcmp(audio_format, video_format) ) ret = -1;
if( !ret )
- strcpy(file_format, audio_format[0] ? audio_format : video_format);
+ strcpy(file_format, !audio_format[0] && !video_format[0] ?
+ (audio_muxer[0] ? audio_muxer : video_muxer) :
+ (audio_format[0] ? audio_format : video_format));
return ret;
}
{
while( *cp == ' ' || *cp == '\t' ) ++cp;
char *bp = cp;
- while( *cp && *cp != ' ' && *cp != '\t' && *cp != '=' ) ++cp;
+ while( *cp && *cp != ' ' && *cp != '\t' && *cp != '=' && *cp != '\n' ) ++cp;
int len = cp - bp;
if( !len || len > BCSTRLEN-1 ) return 1;
while( bp < cp ) *tag++ = *bp++;
char *codec, char *codec_options, int len)
{
char default_file[BCTEXTLEN];
- FFMPEG::set_option_path(default_file, "%s/%s.dfl", path, type);
+ set_option_path(default_file, "%s/%s.dfl", path, type);
FILE *fp = fopen(default_file,"r");
if( !fp ) return 1;
fgets(codec, BCSTRLEN, fp);
codec_options += n; len -= n;
}
fclose(fp);
- FFMPEG::set_option_path(default_file, "%s/%s", path, codec);
- return FFMPEG::load_options(default_file, codec_options, len);
+ set_option_path(default_file, "%s/%s", path, codec);
+ return load_options(default_file, codec_options, len);
}
void FFMPEG::set_asset_format(Asset *asset, const char *text)
{
if( asset->format != FILE_FFMPEG ) return;
- strcpy(asset->fformat, text);
+ 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));
}
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) )
+ if( get_encoder(fp, 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)
+ char *format, char *codec, char *bsfilter)
{
- format[0] = codec[0] = bsfilter[0] = bsargs[0] = 0;
+ format[0] = codec[0] = bsfilter[0] = 0;
char line[BCTEXTLEN];
if( !fgets(line, sizeof(line), fp) ) return 1;
line[sizeof(line)-1] = 0;
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;
}
-int FFMPEG::read_options(const char *options, AVDictionary *&opts)
+int FFMPEG::read_options(const char *options, AVDictionary *&opts, int skip)
{
FILE *fp = fopen(options,"r");
if( !fp ) return 1;
- int ret = read_options(fp, options, opts);
+ int ret = 0;
+ while( !ret && --skip >= 0 ) {
+ int ch = getc(fp);
+ while( ch >= 0 && ch != '\n' ) ch = getc(fp);
+ if( ch < 0 ) ret = 1;
+ }
+ if( !ret )
+ ret = read_options(fp, options, opts);
fclose(fp);
return ret;
}
char line[BCTEXTLEN];
while( !ret && fgets(line, sizeof(line), fp) ) {
line[sizeof(line)-1] = 0;
- ++no;
if( line[0] == '#' ) continue;
if( line[0] == '\n' ) continue;
char key[BCSTRLEN], val[BCTEXTLEN];
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; vidx<ffvideo.size(); ++vidx ) {
FFVideoStream *vid = ffvideo[vidx];
AVStream *st = vid->st;
- 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;
for( int aidx=0; aidx<ffaudio.size(); ++aidx ) {
FFAudioStream *aud = ffaudio[aidx];
AVStream *st = aud->st;
- 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;
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;
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;
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; ch<aud->channels; ++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<<aud->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 )
}
ff_lock("FFMPEG::init_encoder");
av_register_all();
- avformat_alloc_output_context2(&fmt_ctx, 0, file_format, filename);
+ char format[BCSTRLEN];
+ if( get_format(format, "format", file_format) )
+ strcpy(format, file_format);
+ avformat_alloc_output_context2(&fmt_ctx, 0, format, filename);
if( !fmt_ctx ) {
eprintf(_("failed: %s\n"), filename);
ret = 1;
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;
}
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);
}
}
if( !ret ) {
- AVCodecContext *ctx = st->codec;
switch( codec_desc->type ) {
case AVMEDIA_TYPE_AUDIO: {
if( has_audio ) {
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];
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; ch<aud->channels; ++ch )
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];
sprintf(arg, "%d", asset->ff_video_bitrate);
av_dict_set(&sopts, "b", arg, 0);
}
- else if( asset->ff_video_quality > 0 ) {
+ else if( asset->ff_video_quality >= 0 ) {
ctx->global_quality = asset->ff_video_quality * FF_QP2LAMBDA;
ctx->qmin = ctx->qmax = asset->ff_video_quality;
ctx->mb_lmin = ctx->qmin * FF_QP2LAMBDA;
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->time_base = (AVRational) { frame_rate.den, frame_rate.num };
st->time_base = ctx->time_base;
vid->writing = -1;
+ 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;
break; }
default:
eprintf(_("not audio/video, %s:%s\n"), codec_name, filename);
}
if( !ret ) {
if( fmt_ctx->oformat->flags & AVFMT_GLOBALHEADER )
- st->codec->flags |= CODEC_FLAG_GLOBAL_HEADER;
+ ctx->flags |= CODEC_FLAG_GLOBAL_HEADER;
+
+ av_dict_set(&sopts, "cin_bitrate", 0, 0);
+ av_dict_set(&sopts, "cin_quality", 0, 0);
- ret = avcodec_open2(st->codec, codec, &sopts);
+ 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 ) {
ff_err(ret,"FFMPEG::open_encoder");
eprintf(_("open failed %s:%s\n"), codec_name, filename);
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 )
for( int aidx=0; aidx<ffaudio.size(); ++aidx )
ffaudio[aidx]->nudge = 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; i<npgrms; ++i ) {
AVProgram *pgrm = fmt_ctx->programs[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; k<ffvideo.size(); ++k ) {
if( ffvideo[k]->fidx != 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; k<ffaudio.size(); ++k ) {
if( ffaudio[k]->fidx != fidx ) continue;
ffaudio[k]->nudge = nudge;
}
}
// 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; i<nstreams; ++i ) {
AVStream *st = fmt_ctx->streams[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; vidx<ffvideo.size(); ++vidx ) {
- if( ffvideo[vidx]->nudge != AV_NOPTS_VALUE ) continue;
- ffvideo[vidx]->nudge = nudge;
+ if( ffvideo[vidx]->nudge == AV_NOPTS_VALUE )
+ ffvideo[vidx]->nudge = nudge;
}
for( int aidx=0; aidx<ffaudio.size(); ++aidx ) {
- if( ffaudio[aidx]->nudge != AV_NOPTS_VALUE ) continue;
- ffaudio[aidx]->nudge = nudge;
+ if( ffaudio[aidx]->nudge == AV_NOPTS_VALUE )
+ ffaudio[aidx]->nudge = nudge;
}
decoding = 1;
}
return -1;
}
+ int prog_id = 1;
+ AVProgram *prog = av_new_program(fmt_ctx, prog_id);
+ for( int i=0; i< ffvideo.size(); ++i )
+ av_program_add_stream_index(fmt_ctx, prog_id, ffvideo[i]->fidx);
+ for( int i=0; i< ffaudio.size(); ++i )
+ av_program_add_stream_index(fmt_ctx, prog_id, ffaudio[i]->fidx);
+ int pi = fmt_ctx->nb_programs;
+ 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,'/');
+ if( bp ) path = bp + 1;
+ av_dict_set(meta, "title", path, 0);
+
+ if( ffaudio.size() ) {
+ const char *ep = getenv("CIN_AUDIO_LANG"), *lp = 0;
+ if( !ep && (lp=getenv("LANG")) ) { // some are guesses
+ static struct { const char lc[3], lng[4]; } lcode[] = {
+ { "en", "eng" }, { "de", "ger" }, { "es", "spa" },
+ { "eu", "bas" }, { "fr", "fre" }, { "el", "gre" },
+ { "hi", "hin" }, { "it", "ita" }, { "ja", "jap" },
+ { "ko", "kor" }, { "du", "dut" }, { "pl", "pol" },
+ { "pt", "por" }, { "ru", "rus" }, { "sl", "slv" },
+ { "uk", "ukr" }, { "vi", "vie" }, { "zh", "chi" },
+ };
+ for( int i=sizeof(lcode)/sizeof(lcode[0]); --i>=0 && !ep; )
+ if( !strncmp(lcode[i].lc,lp,2) ) ep = lcode[i].lng;
+ }
+ if( !ep ) ep = "und";
+ char lang[5];
+ strncpy(lang,ep,3); lang[3] = 0;
+ AVStream *st = ffaudio[0]->st;
+ av_dict_set(&st->metadata,"language",lang,0);
+ }
+
AVDictionary *fopts = 0;
char option_path[BCTEXTLEN];
set_option_path(option_path, "format/%s", file_format);
- read_options(option_path, fopts);
+ read_options(option_path, fopts, 1);
ret = avformat_write_header(fmt_ctx, &fopts);
- av_dict_free(&fopts);
if( ret < 0 ) {
ff_err(ret, "FFMPEG::encode_activate: write header failed %s\n",
fmt_ctx->filename);
return -1;
}
+ av_dict_free(&fopts);
encoding = 1;
}
return encoding;
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;
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;
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; i<ffvideo.size(); ++i ) { // earliest video
if( fst->frm_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);
mux_lock->lock("FFMPEG::run");
if( !done ) mux();
}
+ for( int i=0; i<ffaudio.size(); ++i )
+ ffaudio[i]->drain();
+ for( int i=0; i<ffvideo.size(); ++i )
+ ffvideo[i]->drain();
mux();
for( int i=0; i<ffaudio.size(); ++i )
ffaudio[i]->flush();
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");
}
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;
}
}
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 && i<ffaudio.size(); ++i )
int FFMPEG::ff_coded_width(int stream)
{
- AVStream *st = ffvideo[stream]->st;
- 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)
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");
}
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;
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);
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");
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;
AVFilter *buffersrc = avfilter_get_by_name("abuffer");
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);
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");
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; i<bsfilter.size(); ++i ) {
- AVPacket bspkt = *pkt;
- ret = av_bitstream_filter_filter(bsfilter[i]->bsfc,
- 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;
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);
+ 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"));
+ 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);
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 ) {
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: {
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; i<nch; ++i )
index_state->put_data(ch+i,nch,samples+i,len);
- aud->curr_pos += len;
}
- pkt.data += ret;
- pkt.size -= ret;
}
break; }
default: break;