return ret;
}
+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 FFStream::flush()
+{
+ 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);
+ }
+ if( ret < 0 )
+ ff_err(ret, "FFStream::flush");
+ return ret >= 0 ? 0 : 1;
+}
+
FFAudioStream::FFAudioStream(FFMPEG *ffmpeg, AVStream *strm, int idx)
: FFStream(ffmpeg, strm, idx)
{
return ret >= 0 ? 0 : 1;
}
+int FFAudioStream::encode_frame(FFPacket &pkt, AVFrame *frame, int &got_packet)
+{
+ 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;
+}
+
FFVideoStream::FFVideoStream(FFMPEG *ffmpeg, AVStream *strm, int idx)
: FFStream(ffmpeg, strm, idx)
{
int FFVideoStream::load(VFrame *vframe, int64_t pos)
{
- if( video_seek(pos) < 0 ) return -1;
+ int ret = video_seek(pos);
+ if( ret < 0 ) return -1;
if( !frame && !(frame=av_frame_alloc()) ) {
fprintf(stderr, "FFVideoStream::load: av_frame_alloc failed\n");
return -1;
}
- int ret = 0;
for( int i=0; ret>=0 && !flushed && curr_pos<=pos && i<1000; ++i ) {
ret = read_frame(frame);
}
if( gop < 4 ) gop = 4;
if( gop > 64 ) gop = 64;
if( pos >= curr_pos && pos <= curr_pos + gop ) return 0;
+ if( pos == curr_pos-1 && curr_pos > seek_pos ) return 1;
if( !st->codec || !st->codec->codec ) return -1;
avcodec_flush_buffers(st->codec);
// back up a few frames to read up to current to help repair damages
return ret >= 0 ? 0 : 1;
}
+int FFVideoStream::encode_frame(FFPacket &pkt, AVFrame *frame, int &got_packet)
+{
+ 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;
+}
PixelFormat FFVideoStream::color_model_to_pix_fmt(int color_model)
{
return (AVRational){1, sample_rate};
}
-extern void get_exe_path(char *result); // from main.C
-
void FFMPEG::set_option_path(char *path, const char *fmt, ...)
{
get_exe_path(path);
return ret;
}
-int FFMPEG::scan_options(const char *options, AVDictionary *&opts)
+int FFMPEG::scan_options(const char *options, AVDictionary *&opts, AVStream *st)
{
FILE *fp = fmemopen((void *)options,strlen(options),"r");
if( !fp ) return 0;
int ret = read_options(fp, options, opts);
fclose(fp);
+ AVDictionaryEntry *tag = av_dict_get(opts, "id", NULL, 0);
+ if( tag ) st->id = strtol(tag->value,0,0);
return ret;
}
break;
}
has_audio = 1;
- if( scan_options(asset->ff_audio_options, sopts) ) {
+ if( scan_options(asset->ff_audio_options, sopts, st) ) {
eprintf("FFMPEG::open_encoder: bad audio options %s:%s\n",
codec_name, filename);
ret = 1;
break;
}
has_video = 1;
- if( scan_options(asset->ff_video_options, sopts) ) {
+ if( scan_options(asset->ff_video_options, sopts, st) ) {
eprintf("FFMPEG::open_encoder: bad video options %s:%s\n",
codec_name, filename);
ret = 1;
int FFMPEG::mux_audio(FFrame *frm)
{
FFPacket pkt;
- AVStream *st = frm->fst->st;
- AVCodecContext *ctx = st->codec;
+ FFStream *fst = frm->fst;
+ AVCodecContext *ctx = fst->st->codec;
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 = avcodec_encode_audio2(ctx, pkt, frame, &got_packet);
- if( ret >= 0 && got_packet ) {
- frm->fst->bs_filter(pkt);
- av_packet_rescale_ts(pkt, ctx->time_base, st->time_base);
- pkt->stream_index = st->index;
- ret = av_interleaved_write_frame(fmt_ctx, pkt);
- }
+ int ret = fst->encode_frame(pkt, frame, got_packet);
+ if( ret >= 0 && got_packet )
+ ret = fst->write_packet(pkt);
if( ret < 0 )
ff_err(ret, "FFMPEG::mux_audio");
return ret >= 0 ? 0 : 1;
int FFMPEG::mux_video(FFrame *frm)
{
FFPacket pkt;
- AVStream *st = frm->fst->st;
+ FFStream *fst = frm->fst;
AVFrame *frame = *frm;
frame->pts = frm->position;
int ret = 1, got_packet = 0;
if( fmt_ctx->oformat->flags & AVFMT_RAWPICTURE ) {
/* a hack to avoid data copy with some raw video muxers */
pkt->flags |= AV_PKT_FLAG_KEY;
- pkt->stream_index = st->index;
+ pkt->stream_index = fst->st->index;
AVPicture *picture = (AVPicture *)frame;
pkt->data = (uint8_t *)picture;
pkt->size = sizeof(AVPicture);
got_packet = 1;
}
else
- ret = avcodec_encode_video2(st->codec, pkt, frame, &got_packet);
- if( ret >= 0 && got_packet ) {
- frm->fst->bs_filter(pkt);
- av_packet_rescale_ts(pkt, st->codec->time_base, st->time_base);
- pkt->stream_index = st->index;
- ret = av_interleaved_write_frame(fmt_ctx, pkt);
- }
+ ret = fst->encode_frame(pkt, frame, got_packet);
+ if( ret >= 0 && got_packet )
+ ret = fst->write_packet(pkt);
if( ret < 0 )
ff_err(ret, "FFMPEG::mux_video");
return ret >= 0 ? 0 : 1;
if( !done ) mux();
}
mux();
+ for( int i=0; i<ffaudio.size(); ++i )
+ ffaudio[i]->flush();
+ for( int i=0; i<ffvideo.size(); ++i )
+ ffvideo[i]->flush();
}