static int same_quant = 0;
static int do_deinterlace = 0;
static int intra_dc_precision = 8;
-static int loop_input = 0;
-static int loop_output = AVFMT_NOOUTPUTLOOP;
static int qp_hist = 0;
static int intra_only = 0;
static const char *video_codec_name = NULL;
static int print_stats = 1;
static uint8_t *audio_buf;
-static uint8_t *audio_out;
-static unsigned int allocated_audio_out_size, allocated_audio_buf_size;
+static unsigned int allocated_audio_buf_size;
static uint8_t *input_tmp= NULL;
int frame_number;
/* input pts and corresponding output pts
for A/V sync */
- // double sync_ipts; /* dts from the AVPacket of the demuxer in second units */
struct InputStream *sync_ist; /* input stream to sync against */
int64_t sync_opts; /* output frame counter, could be changed to some true timestamp */ // FIXME look at frame_number
AVBitStreamFilterContext *bitstream_filters;
AVCodec *enc;
int64_t max_frames;
+ AVFrame *output_frame;
/* video only */
int video_resample;
/* init terminal so that we can grab keys */
static struct termios oldtty;
+static int restore_tty;
#endif
typedef struct OutputFile {
init_opts();
}
-static int alloc_buffer(InputStream *ist, FrameBuffer **pbuf)
+static int alloc_buffer(AVCodecContext *s, InputStream *ist, FrameBuffer **pbuf)
{
- AVCodecContext *s = ist->st->codec;
FrameBuffer *buf = av_mallocz(sizeof(*buf));
int ret, i;
const int pixel_size = av_pix_fmt_descriptors[s->pix_fmt].comp[0].step_minus1+1;
FrameBuffer *buf;
int ret, i;
- if (!ist->buffer_pool && (ret = alloc_buffer(ist, &ist->buffer_pool)) < 0)
+ if(av_image_check_size(s->width, s->height, 0, s))
+ return -1;
+
+ if (!ist->buffer_pool && (ret = alloc_buffer(s, ist, &ist->buffer_pool)) < 0)
return ret;
buf = ist->buffer_pool;
av_freep(&buf->base[0]);
av_free(buf);
ist->dr1 = 0;
- if ((ret = alloc_buffer(ist, &buf)) < 0)
+ if ((ret = alloc_buffer(s, ist, &buf)) < 0)
return ret;
}
buf->refcount++;
"src", args, NULL, ost->graph);
if (ret < 0)
return ret;
+
#if FF_API_OLD_VSINK_API
ret = avfilter_graph_create_filter(&ost->output_video_filter, avfilter_get_by_name("buffersink"),
"out", NULL, pix_fmts, ost->graph);
"out", NULL, buffersink_params, ost->graph);
#endif
av_freep(&buffersink_params);
+
if (ret < 0)
return ret;
last_filter = ost->input_video_filter;
{
av_log(NULL, AV_LOG_QUIET, "%s", "");
#if HAVE_TERMIOS_H
- if(!run_as_daemon)
+ if(restore_tty)
tcsetattr (0, TCSANOW, &oldtty);
#endif
}
if(!run_as_daemon){
struct termios tty;
- tcgetattr (0, &tty);
+ if (tcgetattr (0, &tty) == 0) {
oldtty = tty;
+ restore_tty = 1;
atexit(term_exit);
tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
tty.c_cc[VTIME] = 0;
tcsetattr (0, TCSANOW, &tty);
+ }
signal(SIGQUIT, sigterm_handler); /* Quit (POSIX). */
}
#endif
bsfc = next;
}
output_streams[i].bitstream_filters = NULL;
+
+ if (output_streams[i].output_frame) {
+ AVFrame *frame = output_streams[i].output_frame;
+ if (frame->extended_data != frame->data)
+ av_freep(&frame->extended_data);
+ av_freep(&frame);
+ }
}
for (i = 0; i < nb_input_files; i++) {
avformat_close_input(&input_files[i].ctx);
uninit_opts();
av_free(audio_buf);
- av_free(audio_out);
- allocated_audio_buf_size = allocated_audio_out_size = 0;
+ allocated_audio_buf_size = 0;
#if CONFIG_AVFILTER
avfilter_uninit();
AVCodecContext *avctx = ost->st->codec;
int ret;
+ /*
+ * Audio encoders may split the packets -- #frames in != #packets out.
+ * But there is no reordering, so we can limit the number of output packets
+ * by simply dropping them here.
+ * Counting encoded video frames needs to be done separately because of
+ * reordering, see do_video_out()
+ */
+ if (!(avctx->codec_type == AVMEDIA_TYPE_VIDEO && avctx->codec)) {
+ if (ost->frame_number >= ost->max_frames)
+ return;
+ ost->frame_number++;
+ }
+
while (bsfc) {
AVPacket new_pkt = *pkt;
int a = av_bitstream_filter_filter(bsfc, avctx, NULL,
av_free_packet(pkt);
new_pkt.destruct = av_destruct_packet;
} else if (a < 0) {
- av_log(NULL, AV_LOG_ERROR, "%s failed for stream %d, codec %s",
+ av_log(NULL, AV_LOG_ERROR, "Failed to open bitstream filter %s for stream %d with codec %s",
bsfc->filter->name, pkt->stream_index,
avctx->codec ? avctx->codec->name : "copy");
print_error("", a);
print_error("av_interleaved_write_frame()", ret);
exit_program(1);
}
- ost->frame_number++;
}
static void generate_silence(uint8_t* buf, enum AVSampleFormat sample_fmt, size_t size)
memset(buf, fill_char, size);
}
+static int encode_audio_frame(AVFormatContext *s, OutputStream *ost,
+ const uint8_t *buf, int buf_size)
+{
+ AVCodecContext *enc = ost->st->codec;
+ AVFrame *frame = NULL;
+ AVPacket pkt;
+ int ret, got_packet;
+
+ av_init_packet(&pkt);
+ pkt.data = NULL;
+ pkt.size = 0;
+
+ if (buf) {
+ if (!ost->output_frame) {
+ ost->output_frame = avcodec_alloc_frame();
+ if (!ost->output_frame) {
+ av_log(NULL, AV_LOG_FATAL, "out-of-memory in encode_audio_frame()\n");
+ exit_program(1);
+ }
+ }
+ frame = ost->output_frame;
+ if (frame->extended_data != frame->data)
+ av_freep(&frame->extended_data);
+ avcodec_get_frame_defaults(frame);
+
+ frame->nb_samples = buf_size /
+ (enc->channels * av_get_bytes_per_sample(enc->sample_fmt));
+ if ((ret = avcodec_fill_audio_frame(frame, enc->channels, enc->sample_fmt,
+ buf, buf_size, 1)) < 0) {
+ av_log(NULL, AV_LOG_FATAL, "Audio encoding failed\n");
+ exit_program(1);
+ }
+ }
+
+ got_packet = 0;
+ if (avcodec_encode_audio2(enc, &pkt, frame, &got_packet) < 0) {
+ av_log(NULL, AV_LOG_FATAL, "Audio encoding failed\n");
+ exit_program(1);
+ }
+
+ ret = pkt.size;
+
+ if (got_packet) {
+ pkt.stream_index = ost->index;
+ if (pkt.pts != AV_NOPTS_VALUE)
+ pkt.pts = av_rescale_q(pkt.pts, enc->time_base, ost->st->time_base);
+ if (pkt.duration > 0)
+ pkt.duration = av_rescale_q(pkt.duration, enc->time_base, ost->st->time_base);
+
+ write_frame(s, &pkt, ost);
+
+ audio_size += pkt.size;
+
+ av_free_packet(&pkt);
+ }
+
+ if (frame)
+ ost->sync_opts += frame->nb_samples;
+
+ return ret;
+}
+
static void do_audio_out(AVFormatContext *s, OutputStream *ost,
InputStream *ist, AVFrame *decoded_frame)
{
uint8_t *buftmp;
- int64_t audio_out_size, audio_buf_size, size_out;
+ int64_t audio_buf_size, size_out;
- int frame_bytes, ret, resample_changed;
+ int frame_bytes, resample_changed;
AVCodecContext *enc = ost->st->codec;
AVCodecContext *dec = ist->st->codec;
int osize = av_get_bytes_per_sample(enc->sample_fmt);
int isize = av_get_bytes_per_sample(dec->sample_fmt);
- const int coded_bps = av_get_bits_per_sample(enc->codec->id);
uint8_t *buf = decoded_frame->data[0];
int size = decoded_frame->nb_samples * dec->channels * isize;
int64_t allocated_for_size = size;
audio_buf_size = FFMAX(audio_buf_size, enc->frame_size);
audio_buf_size *= osize * enc->channels;
- audio_out_size = FFMAX(audio_buf_size, enc->frame_size * osize * enc->channels);
- if (coded_bps > 8 * osize)
- audio_out_size = audio_out_size * coded_bps / (8*osize);
- audio_out_size += FF_MIN_BUFFER_SIZE;
-
- if (audio_out_size > INT_MAX || audio_buf_size > INT_MAX) {
+ if (audio_buf_size > INT_MAX) {
av_log(NULL, AV_LOG_FATAL, "Buffer sizes too large\n");
exit_program(1);
}
av_fast_malloc(&audio_buf, &allocated_audio_buf_size, audio_buf_size);
- av_fast_malloc(&audio_out, &allocated_audio_out_size, audio_out_size);
- if (!audio_buf || !audio_out) {
+ if (!audio_buf) {
av_log(NULL, AV_LOG_FATAL, "Out of memory in do_audio_out\n");
exit_program(1);
}
av_assert0(ost->audio_resample || dec->sample_fmt==enc->sample_fmt);
/* now encode as many frames as possible */
- if (enc->frame_size > 1) {
+ if (!(enc->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)) {
/* output resampled raw samples */
if (av_fifo_realloc2(ost->fifo, av_fifo_size(ost->fifo) + size_out) < 0) {
av_log(NULL, AV_LOG_FATAL, "av_fifo_realloc2() failed\n");
frame_bytes = enc->frame_size * osize * enc->channels;
while (av_fifo_size(ost->fifo) >= frame_bytes) {
- AVPacket pkt;
- av_init_packet(&pkt);
-
av_fifo_generic_read(ost->fifo, audio_buf, frame_bytes, NULL);
-
- // FIXME pass ost->sync_opts as AVFrame.pts in avcodec_encode_audio()
-
- ret = avcodec_encode_audio(enc, audio_out, audio_out_size,
- (short *)audio_buf);
- if (ret < 0) {
- av_log(NULL, AV_LOG_FATAL, "Audio encoding failed\n");
- exit_program(1);
- }
- audio_size += ret;
- pkt.stream_index = ost->index;
- pkt.data = audio_out;
- pkt.size = ret;
- if (enc->coded_frame && enc->coded_frame->pts != AV_NOPTS_VALUE)
- pkt.pts = av_rescale_q(enc->coded_frame->pts, enc->time_base, ost->st->time_base);
- pkt.flags |= AV_PKT_FLAG_KEY;
- write_frame(s, &pkt, ost);
-
- ost->sync_opts += enc->frame_size;
+ encode_audio_frame(s, ost, audio_buf, frame_bytes);
}
} else {
- AVPacket pkt;
- av_init_packet(&pkt);
-
- ost->sync_opts += size_out / (osize * enc->channels);
-
- /* output a pcm frame */
- /* determine the size of the coded buffer */
- size_out /= osize;
- if (coded_bps)
- size_out = size_out * coded_bps / 8;
-
- if (size_out > audio_out_size) {
- av_log(NULL, AV_LOG_FATAL, "Internal error, buffer size too small\n");
- exit_program(1);
- }
-
- // FIXME pass ost->sync_opts as AVFrame.pts in avcodec_encode_audio()
- ret = avcodec_encode_audio(enc, audio_out, size_out,
- (short *)buftmp);
- if (ret < 0) {
- av_log(NULL, AV_LOG_FATAL, "Audio encoding failed\n");
- exit_program(1);
- }
- audio_size += ret;
- pkt.stream_index = ost->index;
- pkt.data = audio_out;
- pkt.size = ret;
- if (enc->coded_frame && enc->coded_frame->pts != AV_NOPTS_VALUE)
- pkt.pts = av_rescale_q(enc->coded_frame->pts, enc->time_base, ost->st->time_base);
- pkt.flags |= AV_PKT_FLAG_KEY;
- write_frame(s, &pkt, ost);
+ encode_audio_frame(s, ost, buftmp, size_out);
}
}
}
}
ost->sync_opts++;
+ /*
+ * For video, number of frames in == number of packets out.
+ * But there may be reordering, so we can't throw away frames on encoder
+ * flush, we need to limit them here, before they go into encoder.
+ */
+ ost->frame_number++;
}
}
OutputStream *ost = &ost_table[i];
AVCodecContext *enc = ost->st->codec;
AVFormatContext *os = output_files[ost->file_index].ctx;
+ int stop_encoding = 0;
if (!ost->encoding_needed)
continue;
AVPacket pkt;
int fifo_bytes;
av_init_packet(&pkt);
- pkt.stream_index = ost->index;
+ pkt.data = NULL;
+ pkt.size = 0;
switch (ost->st->codec->codec_type) {
case AVMEDIA_TYPE_AUDIO:
fifo_bytes = av_fifo_size(ost->fifo);
- ret = 0;
- /* encode any samples remaining in fifo */
if (fifo_bytes > 0) {
- int osize = av_get_bytes_per_sample(enc->sample_fmt);
- int fs_tmp = enc->frame_size;
+ /* encode any samples remaining in fifo */
+ int frame_bytes = fifo_bytes;
av_fifo_generic_read(ost->fifo, audio_buf, fifo_bytes, NULL);
- if (enc->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) {
- enc->frame_size = fifo_bytes / (osize * enc->channels);
- } else { /* pad */
- int frame_bytes = enc->frame_size*osize*enc->channels;
+
+ /* pad last frame with silence if needed */
+ if (!(enc->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME)) {
+ frame_bytes = enc->frame_size * enc->channels *
+ av_get_bytes_per_sample(enc->sample_fmt);
if (allocated_audio_buf_size < frame_bytes)
exit_program(1);
generate_silence(audio_buf+fifo_bytes, enc->sample_fmt, frame_bytes - fifo_bytes);
}
-
- ret = avcodec_encode_audio(enc, bit_buffer, bit_buffer_size, (short *)audio_buf);
- pkt.duration = av_rescale((int64_t)enc->frame_size*ost->st->time_base.den,
- ost->st->time_base.num, enc->sample_rate);
- enc->frame_size = fs_tmp;
- }
- if (ret <= 0) {
- ret = avcodec_encode_audio(enc, bit_buffer, bit_buffer_size, NULL);
- }
- if (ret < 0) {
- av_log(NULL, AV_LOG_FATAL, "Audio encoding failed\n");
- exit_program(1);
+ encode_audio_frame(os, ost, audio_buf, frame_bytes);
+ } else {
+ /* flush encoder with NULL frames until it is done
+ returning packets */
+ if (encode_audio_frame(os, ost, NULL, 0) == 0) {
+ stop_encoding = 1;
+ break;
+ }
}
- audio_size += ret;
- pkt.flags |= AV_PKT_FLAG_KEY;
break;
case AVMEDIA_TYPE_VIDEO:
ret = avcodec_encode_video(enc, bit_buffer, bit_buffer_size, NULL);
if (ost->logfile && enc->stats_out) {
fprintf(ost->logfile, "%s", enc->stats_out);
}
+ if (ret <= 0) {
+ stop_encoding = 1;
+ break;
+ }
+ pkt.stream_index = ost->index;
+ pkt.data = bit_buffer;
+ pkt.size = ret;
+ if (enc->coded_frame && enc->coded_frame->pts != AV_NOPTS_VALUE)
+ pkt.pts = av_rescale_q(enc->coded_frame->pts, enc->time_base, ost->st->time_base);
+ write_frame(os, &pkt, ost);
break;
default:
- ret = -1;
+ stop_encoding = 1;
}
-
- if (ret <= 0)
+ if (stop_encoding)
break;
- pkt.data = bit_buffer;
- pkt.size = ret;
- if (enc->coded_frame && enc->coded_frame->pts != AV_NOPTS_VALUE)
- pkt.pts = av_rescale_q(enc->coded_frame->pts, enc->time_base, ost->st->time_base);
- write_frame(os, &pkt, ost);
}
}
}
frame_sample_aspect= av_opt_ptr(avcodec_get_frame_class(), decoded_frame, "sample_aspect_ratio");
for(i=0;i<nb_output_streams;i++) {
OutputStream *ost = ost = &output_streams[i];
- if(check_output_constraints(ist, ost)){
+ if(check_output_constraints(ist, ost) && ost->encoding_needed){
if (!frame_sample_aspect->num)
*frame_sample_aspect = ist->st->sample_aspect_ratio;
decoded_frame->pts = ist->pts;
ist->st->codec->opaque = ist;
}
+ if (!av_dict_get(ist->opts, "threads", NULL, 0))
+ av_dict_set(&ist->opts, "threads", "auto", 0);
if (avcodec_open2(ist->st->codec, codec, &ist->opts) < 0) {
snprintf(error, error_len, "Error while opening decoder for input stream #%d:%d",
ist->file_index, ist->st->index);
}
}
if (codec->codec_type == AVMEDIA_TYPE_VIDEO) {
- /* maximum video buffer size is 6-bytes per pixel, plus DPX header size (1664)*/
+ /* maximum video buffer size is 8-bytes per pixel, plus DPX header size (1664)*/
int size = codec->width * codec->height;
- bit_buffer_size = FFMAX(bit_buffer_size, 7*size + 10000);
+ bit_buffer_size = FFMAX(bit_buffer_size, 9*size + 10000);
}
}
memcpy(ost->st->codec->subtitle_header, dec->subtitle_header, dec->subtitle_header_size);
ost->st->codec->subtitle_header_size = dec->subtitle_header_size;
}
+ if (!av_dict_get(ost->opts, "threads", NULL, 0))
+ av_dict_set(&ost->opts, "threads", "auto", 0);
if (avcodec_open2(ost->st->codec, codec, &ost->opts) < 0) {
snprintf(error, sizeof(error), "Error while opening encoder for output stream #%d:%d - maybe incorrect parameters such as bit_rate, rate, width or height",
ost->file_index, ost->index);
ic->flags |= AVFMT_FLAG_NONBLOCK;
ic->interrupt_callback = int_cb;
- if (loop_input) {
- av_log(NULL, AV_LOG_WARNING,
- "-loop_input is deprecated, use -loop 1\n"
- "Note, both loop options only work with -f image2\n"
- );
- ic->loop_input = loop_input;
- }
-
/* open the input file with generic avformat function */
err = avformat_open_input(&ic, filename, file_iformat, &format_opts);
if (err < 0) {
}
oc->max_delay = (int)(o->mux_max_delay * AV_TIME_BASE);
- if (loop_output >= 0) {
- av_log(NULL, AV_LOG_WARNING, "-loop_output is deprecated, use -loop\n");
- oc->loop_output = loop_output;
- }
-
/* copy metadata */
for (i = 0; i < o->nb_metadata_map; i++) {
char *p;
static int opt_old2new(OptionsContext *o, const char *opt, const char *arg)
{
- char *s= av_malloc(strlen(opt)+2);
- snprintf(s, strlen(opt)+2, "%s:%c", opt+1, *opt);
- return parse_option(o, s, arg, options);
+ char *s = av_asprintf("%s:%c", opt + 1, *opt);
+ int ret = parse_option(o, s, arg, options);
+ av_free(s);
+ return ret;
}
static int opt_bitrate(OptionsContext *o, const char *opt, const char *arg)
{
if(!strcmp(opt, "b")){
av_log(0,AV_LOG_WARNING, "Please use -b:a or -b:v, -b is ambiguous\n");
- return parse_option(o, av_strdup("b:v"), arg, options);
+ return parse_option(o, "b:v", arg, options);
}
return opt_default(opt, arg);
}
+static int opt_qscale(OptionsContext *o, const char *opt, const char *arg)
+{
+ char *s;
+ int ret;
+ if(!strcmp(opt, "qscale")){
+ av_log(0,AV_LOG_WARNING, "Please use -q:a or -q:v, -qscale is ambiguous\n");
+ return parse_option(o, "q:v", arg, options);
+ }
+ s = av_asprintf("q%s", opt + 6);
+ ret = parse_option(o, s, arg, options);
+ av_free(s);
+ return ret;
+}
+
static int opt_video_filters(OptionsContext *o, const char *opt, const char *arg)
{
return parse_option(o, "filter:v", arg, options);
{ "hex", OPT_BOOL | OPT_EXPERT, {(void*)&do_hex_dump},
"when dumping packets, also dump the payload" },
{ "re", OPT_BOOL | OPT_EXPERT | OPT_OFFSET, {.off = OFFSET(rate_emu)}, "read input at native frame rate", "" },
- { "loop_input", OPT_BOOL | OPT_EXPERT, {(void*)&loop_input}, "deprecated, use -loop" },
- { "loop_output", HAS_ARG | OPT_INT | OPT_EXPERT, {(void*)&loop_output}, "deprecated, use -loop", "" },
{ "target", HAS_ARG | OPT_FUNC2, {(void*)opt_target}, "specify target file type (\"vcd\", \"svcd\", \"dvd\", \"dv\", \"dv50\", \"pal-vcd\", \"ntsc-svcd\", ...)", "type" },
{ "vsync", HAS_ARG | OPT_EXPERT, {(void*)opt_vsync}, "video sync method", "" },
{ "async", HAS_ARG | OPT_INT | OPT_EXPERT, {(void*)&audio_sync_method}, "audio sync method", "" },
{ "frames", OPT_INT64 | HAS_ARG | OPT_SPEC, {.off = OFFSET(max_frames)}, "set the number of frames to record", "number" },
{ "tag", OPT_STRING | HAS_ARG | OPT_SPEC, {.off = OFFSET(codec_tags)}, "force codec tag/fourcc", "fourcc/tag" },
{ "q", HAS_ARG | OPT_EXPERT | OPT_DOUBLE | OPT_SPEC, {.off = OFFSET(qscale)}, "use fixed quality scale (VBR)", "q" },
- { "qscale", HAS_ARG | OPT_EXPERT | OPT_DOUBLE | OPT_SPEC, {.off = OFFSET(qscale)}, "use fixed quality scale (VBR)", "q" },
+ { "qscale", HAS_ARG | OPT_EXPERT | OPT_FUNC2, {(void*)opt_qscale}, "use fixed quality scale (VBR)", "q" },
#if CONFIG_AVFILTER
{ "filter", HAS_ARG | OPT_STRING | OPT_SPEC, {.off = OFFSET(filters)}, "set stream filterchain", "filter_list" },
#endif