151
147
for (i = 0; i < nb_filtergraphs; i++) {
152
avfilter_graph_free(&filtergraphs[i]->graph);
153
for (j = 0; j < filtergraphs[i]->nb_inputs; j++) {
154
av_freep(&filtergraphs[i]->inputs[j]->name);
155
av_freep(&filtergraphs[i]->inputs[j]);
157
av_freep(&filtergraphs[i]->inputs);
158
for (j = 0; j < filtergraphs[i]->nb_outputs; j++) {
159
av_freep(&filtergraphs[i]->outputs[j]->name);
160
av_freep(&filtergraphs[i]->outputs[j]);
162
av_freep(&filtergraphs[i]->outputs);
163
av_freep(&filtergraphs[i]->graph_desc);
148
FilterGraph *fg = filtergraphs[i];
149
avfilter_graph_free(&fg->graph);
150
for (j = 0; j < fg->nb_inputs; j++) {
151
av_freep(&fg->inputs[j]->name);
152
av_freep(&fg->inputs[j]);
154
av_freep(&fg->inputs);
155
for (j = 0; j < fg->nb_outputs; j++) {
156
av_freep(&fg->outputs[j]->name);
157
av_freep(&fg->outputs[j]);
159
av_freep(&fg->outputs);
160
av_freep(&fg->graph_desc);
164
162
av_freep(&filtergraphs[i]);
166
164
av_freep(&filtergraphs);
168
166
/* close files */
169
167
for (i = 0; i < nb_output_files; i++) {
170
AVFormatContext *s = output_files[i]->ctx;
168
OutputFile *of = output_files[i];
169
AVFormatContext *s = of->ctx;
171
170
if (s && s->oformat && !(s->oformat->flags & AVFMT_NOFILE) && s->pb)
172
171
avio_close(s->pb);
173
172
avformat_free_context(s);
174
av_dict_free(&output_files[i]->opts);
173
av_dict_free(&of->opts);
175
175
av_freep(&output_files[i]);
177
177
for (i = 0; i < nb_output_streams; i++) {
178
AVBitStreamFilterContext *bsfc = output_streams[i]->bitstream_filters;
178
OutputStream *ost = output_streams[i];
179
AVBitStreamFilterContext *bsfc = ost->bitstream_filters;
180
181
AVBitStreamFilterContext *next = bsfc->next;
181
182
av_bitstream_filter_close(bsfc);
184
output_streams[i]->bitstream_filters = NULL;
185
av_frame_free(&output_streams[i]->filtered_frame);
187
av_parser_close(output_streams[i]->parser);
189
av_freep(&output_streams[i]->forced_keyframes);
190
av_freep(&output_streams[i]->avfilter);
191
av_freep(&output_streams[i]->logfile_prefix);
185
ost->bitstream_filters = NULL;
186
av_frame_free(&ost->filtered_frame);
188
av_parser_close(ost->parser);
190
av_freep(&ost->forced_keyframes);
191
av_freep(&ost->avfilter);
192
av_freep(&ost->logfile_prefix);
194
avcodec_free_context(&ost->enc_ctx);
192
196
av_freep(&output_streams[i]);
194
198
for (i = 0; i < nb_input_files; i++) {
407
419
frame->pts = ost->sync_opts;
408
420
ost->sync_opts = frame->pts + frame->nb_samples;
422
ost->samples_encoded += frame->nb_samples;
423
ost->frames_encoded++;
410
425
if (avcodec_encode_audio2(enc, &pkt, frame, &got_packet) < 0) {
411
426
av_log(NULL, AV_LOG_FATAL, "Audio encoding failed\n");
415
430
if (got_packet) {
416
if (pkt.pts != AV_NOPTS_VALUE)
417
pkt.pts = av_rescale_q(pkt.pts, enc->time_base, ost->st->time_base);
418
if (pkt.dts != AV_NOPTS_VALUE)
419
pkt.dts = av_rescale_q(pkt.dts, enc->time_base, ost->st->time_base);
420
if (pkt.duration > 0)
421
pkt.duration = av_rescale_q(pkt.duration, enc->time_base, ost->st->time_base);
431
av_packet_rescale_ts(&pkt, enc->time_base, ost->st->time_base);
423
432
write_frame(s, &pkt, ost);
425
audio_size += pkt.size;
659
669
int64_t start_time = (of->start_time == AV_NOPTS_VALUE) ? 0 : of->start_time;
660
670
filtered_frame->pts = av_rescale_q(filtered_frame->pts,
661
671
ost->filter->filter->inputs[0]->time_base,
662
ost->st->codec->time_base) -
672
ost->enc_ctx->time_base) -
663
673
av_rescale_q(start_time,
665
ost->st->codec->time_base);
675
ost->enc_ctx->time_base);
668
678
switch (ost->filter->filter->inputs[0]->type) {
669
679
case AVMEDIA_TYPE_VIDEO:
670
680
if (!ost->frame_aspect_ratio)
671
ost->st->codec->sample_aspect_ratio = filtered_frame->sample_aspect_ratio;
681
ost->enc_ctx->sample_aspect_ratio = filtered_frame->sample_aspect_ratio;
673
683
do_video_out(of->ctx, ost, filtered_frame, &frame_size);
674
684
if (vstats_filename && frame_size)
759
static void print_final_stats(int64_t total_size)
761
uint64_t video_size = 0, audio_size = 0, extra_size = 0, other_size = 0;
762
uint64_t data_size = 0;
763
float percent = -1.0;
766
for (i = 0; i < nb_output_streams; i++) {
767
OutputStream *ost = output_streams[i];
768
switch (ost->enc_ctx->codec_type) {
769
case AVMEDIA_TYPE_VIDEO: video_size += ost->data_size; break;
770
case AVMEDIA_TYPE_AUDIO: audio_size += ost->data_size; break;
771
default: other_size += ost->data_size; break;
773
extra_size += ost->enc_ctx->extradata_size;
774
data_size += ost->data_size;
777
if (data_size && total_size >= data_size)
778
percent = 100.0 * (total_size - data_size) / data_size;
780
av_log(NULL, AV_LOG_INFO, "\n");
781
av_log(NULL, AV_LOG_INFO, "video:%1.0fkB audio:%1.0fkB other streams:%1.0fkB global headers:%1.0fkB muxing overhead: ",
785
extra_size / 1024.0);
787
av_log(NULL, AV_LOG_INFO, "%f%%", percent);
789
av_log(NULL, AV_LOG_INFO, "unknown");
790
av_log(NULL, AV_LOG_INFO, "\n");
792
/* print verbose per-stream stats */
793
for (i = 0; i < nb_input_files; i++) {
794
InputFile *f = input_files[i];
795
uint64_t total_packets = 0, total_size = 0;
797
av_log(NULL, AV_LOG_VERBOSE, "Input file #%d (%s):\n",
798
i, f->ctx->filename);
800
for (j = 0; j < f->nb_streams; j++) {
801
InputStream *ist = input_streams[f->ist_index + j];
802
enum AVMediaType type = ist->dec_ctx->codec_type;
804
total_size += ist->data_size;
805
total_packets += ist->nb_packets;
807
av_log(NULL, AV_LOG_VERBOSE, " Input stream #%d:%d (%s): ",
808
i, j, media_type_string(type));
809
av_log(NULL, AV_LOG_VERBOSE, "%"PRIu64" packets read (%"PRIu64" bytes); ",
810
ist->nb_packets, ist->data_size);
812
if (ist->decoding_needed) {
813
av_log(NULL, AV_LOG_VERBOSE, "%"PRIu64" frames decoded",
814
ist->frames_decoded);
815
if (type == AVMEDIA_TYPE_AUDIO)
816
av_log(NULL, AV_LOG_VERBOSE, " (%"PRIu64" samples)", ist->samples_decoded);
817
av_log(NULL, AV_LOG_VERBOSE, "; ");
820
av_log(NULL, AV_LOG_VERBOSE, "\n");
823
av_log(NULL, AV_LOG_VERBOSE, " Total: %"PRIu64" packets (%"PRIu64" bytes) demuxed\n",
824
total_packets, total_size);
827
for (i = 0; i < nb_output_files; i++) {
828
OutputFile *of = output_files[i];
829
uint64_t total_packets = 0, total_size = 0;
831
av_log(NULL, AV_LOG_VERBOSE, "Output file #%d (%s):\n",
832
i, of->ctx->filename);
834
for (j = 0; j < of->ctx->nb_streams; j++) {
835
OutputStream *ost = output_streams[of->ost_index + j];
836
enum AVMediaType type = ost->enc_ctx->codec_type;
838
total_size += ost->data_size;
839
total_packets += ost->packets_written;
841
av_log(NULL, AV_LOG_VERBOSE, " Output stream #%d:%d (%s): ",
842
i, j, media_type_string(type));
843
if (ost->encoding_needed) {
844
av_log(NULL, AV_LOG_VERBOSE, "%"PRIu64" frames encoded",
845
ost->frames_encoded);
846
if (type == AVMEDIA_TYPE_AUDIO)
847
av_log(NULL, AV_LOG_VERBOSE, " (%"PRIu64" samples)", ost->samples_encoded);
848
av_log(NULL, AV_LOG_VERBOSE, "; ");
851
av_log(NULL, AV_LOG_VERBOSE, "%"PRIu64" packets muxed (%"PRIu64" bytes); ",
852
ost->packets_written, ost->data_size);
854
av_log(NULL, AV_LOG_VERBOSE, "\n");
857
av_log(NULL, AV_LOG_VERBOSE, " Total: %"PRIu64" packets (%"PRIu64" bytes) muxed\n",
858
total_packets, total_size);
749
862
static void print_report(int is_last_report, int64_t timer_start)
854
967
"size=%8.0fkB time=%0.2f bitrate=%6.1fkbits/s",
855
968
(double)total_size / 1024, ti1, bitrate);
857
if (nb_frames_dup || nb_frames_drop)
858
snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), " dup=%d drop=%d",
859
nb_frames_dup, nb_frames_drop);
971
snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), " drop=%d",
861
974
av_log(NULL, AV_LOG_INFO, "%s \r", buf);
865
if (is_last_report) {
866
int64_t raw = audio_size + video_size + extra_size;
870
percent = 100.0 * (total_size - raw) / raw;
872
av_log(NULL, AV_LOG_INFO, "\n");
873
av_log(NULL, AV_LOG_INFO, "video:%1.0fkB audio:%1.0fkB global headers:%1.0fkB muxing overhead %f%%\n",
979
print_final_stats(total_size);
881
983
static void flush_encoders(void)
885
987
for (i = 0; i < nb_output_streams; i++) {
886
988
OutputStream *ost = output_streams[i];
887
AVCodecContext *enc = ost->st->codec;
989
AVCodecContext *enc = ost->enc_ctx;
888
990
AVFormatContext *os = output_files[ost->file_index]->ctx;
889
991
int stop_encoding = 0;
891
993
if (!ost->encoding_needed)
894
if (ost->st->codec->codec_type == AVMEDIA_TYPE_AUDIO && enc->frame_size <= 1)
996
if (enc->codec_type == AVMEDIA_TYPE_AUDIO && enc->frame_size <= 1)
896
if (ost->st->codec->codec_type == AVMEDIA_TYPE_VIDEO && (os->oformat->flags & AVFMT_RAWPICTURE) && enc->codec->id == AV_CODEC_ID_RAWVIDEO)
998
if (enc->codec_type == AVMEDIA_TYPE_VIDEO && (os->oformat->flags & AVFMT_RAWPICTURE) && enc->codec->id == AV_CODEC_ID_RAWVIDEO)
900
1002
int (*encode)(AVCodecContext*, AVPacket*, const AVFrame*, int*) = NULL;
901
1003
const char *desc;
904
switch (ost->st->codec->codec_type) {
1005
switch (enc->codec_type) {
905
1006
case AVMEDIA_TYPE_AUDIO:
906
1007
encode = avcodec_encode_audio2;
910
1010
case AVMEDIA_TYPE_VIDEO:
911
1011
encode = avcodec_encode_video2;
916
1015
stop_encoding = 1;
1334
1429
/* handle stream copy */
1335
1430
if (!ist->decoding_needed) {
1336
1431
ist->last_dts = ist->next_dts;
1337
switch (ist->st->codec->codec_type) {
1432
switch (ist->dec_ctx->codec_type) {
1338
1433
case AVMEDIA_TYPE_AUDIO:
1339
ist->next_dts += ((int64_t)AV_TIME_BASE * ist->st->codec->frame_size) /
1340
ist->st->codec->sample_rate;
1434
ist->next_dts += ((int64_t)AV_TIME_BASE * ist->dec_ctx->frame_size) /
1435
ist->dec_ctx->sample_rate;
1342
1437
case AVMEDIA_TYPE_VIDEO:
1343
if (ist->st->codec->time_base.num != 0) {
1344
int ticks = ist->st->parser ? ist->st->parser->repeat_pict + 1 : ist->st->codec->ticks_per_frame;
1438
if (ist->dec_ctx->time_base.num != 0) {
1439
int ticks = ist->st->parser ? ist->st->parser->repeat_pict + 1 : ist->dec_ctx->ticks_per_frame;
1345
1440
ist->next_dts += ((int64_t)AV_TIME_BASE *
1346
ist->st->codec->time_base.num * ticks) /
1347
ist->st->codec->time_base.den;
1441
ist->dec_ctx->time_base.num * ticks) /
1442
ist->dec_ctx->time_base.den;
1452
1547
for (i = 0; i < nb_output_streams; i++) {
1453
1548
OutputStream *ost = output_streams[i];
1454
1549
if (ost->source_index == ist_index) {
1455
update_sample_fmt(ist->st->codec, codec, ost->st->codec);
1550
update_sample_fmt(ist->dec_ctx, codec, ost->enc_ctx);
1460
ist->st->codec->opaque = ist;
1461
ist->st->codec->get_format = get_format;
1462
ist->st->codec->get_buffer2 = get_buffer;
1463
ist->st->codec->thread_safe_callbacks = 1;
1465
av_opt_set_int(ist->st->codec, "refcounted_frames", 1, 0);
1467
if (!av_dict_get(ist->opts, "threads", NULL, 0))
1468
av_dict_set(&ist->opts, "threads", "auto", 0);
1469
if ((ret = avcodec_open2(ist->st->codec, codec, &ist->opts)) < 0) {
1555
ist->dec_ctx->opaque = ist;
1556
ist->dec_ctx->get_format = get_format;
1557
ist->dec_ctx->get_buffer2 = get_buffer;
1558
ist->dec_ctx->thread_safe_callbacks = 1;
1560
av_opt_set_int(ist->dec_ctx, "refcounted_frames", 1, 0);
1562
if (!av_dict_get(ist->decoder_opts, "threads", NULL, 0))
1563
av_dict_set(&ist->decoder_opts, "threads", "auto", 0);
1564
if ((ret = avcodec_open2(ist->dec_ctx, codec, &ist->decoder_opts)) < 0) {
1470
1565
char errbuf[128];
1471
1566
if (ret == AVERROR_EXPERIMENTAL)
1472
1567
abort_codec_experimental(codec, 0);
1635
static void set_encoder_id(OutputFile *of, OutputStream *ost)
1637
AVDictionaryEntry *e;
1639
uint8_t *encoder_string;
1640
int encoder_string_len;
1641
int format_flags = 0;
1643
e = av_dict_get(of->opts, "fflags", NULL, 0);
1645
const AVOption *o = av_opt_find(of->ctx, "fflags", NULL, 0, 0);
1648
av_opt_eval_flags(of->ctx, o, e->value, &format_flags);
1651
encoder_string_len = sizeof(LIBAVCODEC_IDENT) + strlen(ost->enc->name) + 2;
1652
encoder_string = av_mallocz(encoder_string_len);
1653
if (!encoder_string)
1656
if (!(format_flags & AVFMT_FLAG_BITEXACT))
1657
av_strlcpy(encoder_string, LIBAVCODEC_IDENT " ", encoder_string_len);
1658
av_strlcat(encoder_string, ost->enc->name, encoder_string_len);
1659
av_dict_set(&ost->st->metadata, "encoder", encoder_string,
1660
AV_DICT_DONT_STRDUP_VAL | AV_DICT_DONT_OVERWRITE);
1541
1663
static int transcode_init(void)
1543
1665
int ret = 0, i, j, k;
1544
1666
AVFormatContext *oc;
1545
AVCodecContext *codec;
1546
1667
OutputStream *ost;
1547
1668
InputStream *ist;
1548
1669
char error[1024];
1598
1720
av_assert0(ist && !ost->filter);
1600
extra_size = (uint64_t)icodec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE;
1722
extra_size = (uint64_t)dec_ctx->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE;
1602
1724
if (extra_size > INT_MAX) {
1603
1725
return AVERROR(EINVAL);
1606
1728
/* if stream_copy is selected, no need to decode or encode */
1607
codec->codec_id = icodec->codec_id;
1608
codec->codec_type = icodec->codec_type;
1729
enc_ctx->codec_id = dec_ctx->codec_id;
1730
enc_ctx->codec_type = dec_ctx->codec_type;
1610
if (!codec->codec_tag) {
1732
if (!enc_ctx->codec_tag) {
1611
1733
if (!oc->oformat->codec_tag ||
1612
av_codec_get_id (oc->oformat->codec_tag, icodec->codec_tag) == codec->codec_id ||
1613
av_codec_get_tag(oc->oformat->codec_tag, icodec->codec_id) <= 0)
1614
codec->codec_tag = icodec->codec_tag;
1734
av_codec_get_id (oc->oformat->codec_tag, dec_ctx->codec_tag) == enc_ctx->codec_id ||
1735
av_codec_get_tag(oc->oformat->codec_tag, dec_ctx->codec_id) <= 0)
1736
enc_ctx->codec_tag = dec_ctx->codec_tag;
1617
codec->bit_rate = icodec->bit_rate;
1618
codec->rc_max_rate = icodec->rc_max_rate;
1619
codec->rc_buffer_size = icodec->rc_buffer_size;
1620
codec->field_order = icodec->field_order;
1621
codec->extradata = av_mallocz(extra_size);
1622
if (!codec->extradata) {
1739
enc_ctx->bit_rate = dec_ctx->bit_rate;
1740
enc_ctx->rc_max_rate = dec_ctx->rc_max_rate;
1741
enc_ctx->rc_buffer_size = dec_ctx->rc_buffer_size;
1742
enc_ctx->field_order = dec_ctx->field_order;
1743
enc_ctx->extradata = av_mallocz(extra_size);
1744
if (!enc_ctx->extradata) {
1623
1745
return AVERROR(ENOMEM);
1625
memcpy(codec->extradata, icodec->extradata, icodec->extradata_size);
1626
codec->extradata_size = icodec->extradata_size;
1747
memcpy(enc_ctx->extradata, dec_ctx->extradata, dec_ctx->extradata_size);
1748
enc_ctx->extradata_size = dec_ctx->extradata_size;
1627
1749
if (!copy_tb) {
1628
codec->time_base = icodec->time_base;
1629
codec->time_base.num *= icodec->ticks_per_frame;
1630
av_reduce(&codec->time_base.num, &codec->time_base.den,
1631
codec->time_base.num, codec->time_base.den, INT_MAX);
1750
enc_ctx->time_base = dec_ctx->time_base;
1751
enc_ctx->time_base.num *= dec_ctx->ticks_per_frame;
1752
av_reduce(&enc_ctx->time_base.num, &enc_ctx->time_base.den,
1753
enc_ctx->time_base.num, enc_ctx->time_base.den, INT_MAX);
1633
codec->time_base = ist->st->time_base;
1635
ost->parser = av_parser_init(codec->codec_id);
1637
switch (codec->codec_type) {
1755
enc_ctx->time_base = ist->st->time_base;
1757
ost->parser = av_parser_init(enc_ctx->codec_id);
1759
switch (enc_ctx->codec_type) {
1638
1760
case AVMEDIA_TYPE_AUDIO:
1639
1761
if (audio_volume != 256) {
1640
1762
av_log(NULL, AV_LOG_FATAL, "-acodec copy and -vol are incompatible (frames are not decoded)\n");
1641
1763
exit_program(1);
1643
codec->channel_layout = icodec->channel_layout;
1644
codec->sample_rate = icodec->sample_rate;
1645
codec->channels = icodec->channels;
1646
codec->frame_size = icodec->frame_size;
1647
codec->audio_service_type = icodec->audio_service_type;
1648
codec->block_align = icodec->block_align;
1765
enc_ctx->channel_layout = dec_ctx->channel_layout;
1766
enc_ctx->sample_rate = dec_ctx->sample_rate;
1767
enc_ctx->channels = dec_ctx->channels;
1768
enc_ctx->frame_size = dec_ctx->frame_size;
1769
enc_ctx->audio_service_type = dec_ctx->audio_service_type;
1770
enc_ctx->block_align = dec_ctx->block_align;
1650
1772
case AVMEDIA_TYPE_VIDEO:
1651
codec->pix_fmt = icodec->pix_fmt;
1652
codec->width = icodec->width;
1653
codec->height = icodec->height;
1654
codec->has_b_frames = icodec->has_b_frames;
1773
enc_ctx->pix_fmt = dec_ctx->pix_fmt;
1774
enc_ctx->width = dec_ctx->width;
1775
enc_ctx->height = dec_ctx->height;
1776
enc_ctx->has_b_frames = dec_ctx->has_b_frames;
1655
1777
if (ost->frame_aspect_ratio)
1656
sar = av_d2q(ost->frame_aspect_ratio * codec->height / codec->width, 255);
1778
sar = av_d2q(ost->frame_aspect_ratio * enc_ctx->height / enc_ctx->width, 255);
1657
1779
else if (ist->st->sample_aspect_ratio.num)
1658
1780
sar = ist->st->sample_aspect_ratio;
1660
sar = icodec->sample_aspect_ratio;
1661
ost->st->sample_aspect_ratio = codec->sample_aspect_ratio = sar;
1782
sar = dec_ctx->sample_aspect_ratio;
1783
ost->st->sample_aspect_ratio = enc_ctx->sample_aspect_ratio = sar;
1663
1785
case AVMEDIA_TYPE_SUBTITLE:
1664
codec->width = icodec->width;
1665
codec->height = icodec->height;
1786
enc_ctx->width = dec_ctx->width;
1787
enc_ctx->height = dec_ctx->height;
1667
1789
case AVMEDIA_TYPE_DATA:
1668
1790
case AVMEDIA_TYPE_ATTACHMENT:
1734
switch (codec->codec_type) {
1858
switch (enc_ctx->codec_type) {
1735
1859
case AVMEDIA_TYPE_AUDIO:
1736
codec->sample_fmt = ost->filter->filter->inputs[0]->format;
1737
codec->sample_rate = ost->filter->filter->inputs[0]->sample_rate;
1738
codec->channel_layout = ost->filter->filter->inputs[0]->channel_layout;
1739
codec->channels = av_get_channel_layout_nb_channels(codec->channel_layout);
1740
codec->time_base = (AVRational){ 1, codec->sample_rate };
1860
enc_ctx->sample_fmt = ost->filter->filter->inputs[0]->format;
1861
enc_ctx->sample_rate = ost->filter->filter->inputs[0]->sample_rate;
1862
enc_ctx->channel_layout = ost->filter->filter->inputs[0]->channel_layout;
1863
enc_ctx->channels = av_get_channel_layout_nb_channels(enc_ctx->channel_layout);
1864
enc_ctx->time_base = (AVRational){ 1, enc_ctx->sample_rate };
1742
1866
case AVMEDIA_TYPE_VIDEO:
1743
codec->time_base = ost->filter->filter->inputs[0]->time_base;
1867
enc_ctx->time_base = ost->filter->filter->inputs[0]->time_base;
1745
codec->width = ost->filter->filter->inputs[0]->w;
1746
codec->height = ost->filter->filter->inputs[0]->h;
1747
codec->sample_aspect_ratio = ost->st->sample_aspect_ratio =
1869
enc_ctx->width = ost->filter->filter->inputs[0]->w;
1870
enc_ctx->height = ost->filter->filter->inputs[0]->h;
1871
enc_ctx->sample_aspect_ratio = ost->st->sample_aspect_ratio =
1748
1872
ost->frame_aspect_ratio ? // overridden by the -aspect cli option
1749
av_d2q(ost->frame_aspect_ratio * codec->height/codec->width, 255) :
1873
av_d2q(ost->frame_aspect_ratio * enc_ctx->height/enc_ctx->width, 255) :
1750
1874
ost->filter->filter->inputs[0]->sample_aspect_ratio;
1751
codec->pix_fmt = ost->filter->filter->inputs[0]->format;
1754
(codec->width != icodec->width ||
1755
codec->height != icodec->height ||
1756
codec->pix_fmt != icodec->pix_fmt)) {
1757
codec->bits_per_raw_sample = 0;
1875
enc_ctx->pix_fmt = ost->filter->filter->inputs[0]->format;
1877
ost->st->avg_frame_rate = ost->frame_rate;
1880
(enc_ctx->width != dec_ctx->width ||
1881
enc_ctx->height != dec_ctx->height ||
1882
enc_ctx->pix_fmt != dec_ctx->pix_fmt)) {
1883
enc_ctx->bits_per_raw_sample = 0;
1760
1886
if (ost->forced_keyframes)
1761
1887
parse_forced_key_frames(ost->forced_keyframes, ost,
1764
1890
case AVMEDIA_TYPE_SUBTITLE:
1765
codec->time_base = (AVRational){1, 1000};
1891
enc_ctx->time_base = (AVRational){1, 1000};
1771
1897
/* two pass mode */
1772
if ((codec->flags & (CODEC_FLAG_PASS1 | CODEC_FLAG_PASS2))) {
1898
if ((enc_ctx->flags & (CODEC_FLAG_PASS1 | CODEC_FLAG_PASS2))) {
1773
1899
char logfilename[1024];
1811
1937
AVCodecContext *dec = NULL;
1813
1939
if ((ist = get_input_stream(ost)))
1814
dec = ist->st->codec;
1815
1941
if (dec && dec->subtitle_header) {
1816
ost->st->codec->subtitle_header = av_malloc(dec->subtitle_header_size);
1817
if (!ost->st->codec->subtitle_header) {
1942
ost->enc_ctx->subtitle_header = av_malloc(dec->subtitle_header_size);
1943
if (!ost->enc_ctx->subtitle_header) {
1818
1944
ret = AVERROR(ENOMEM);
1819
1945
goto dump_format;
1821
memcpy(ost->st->codec->subtitle_header, dec->subtitle_header, dec->subtitle_header_size);
1822
ost->st->codec->subtitle_header_size = dec->subtitle_header_size;
1947
memcpy(ost->enc_ctx->subtitle_header, dec->subtitle_header, dec->subtitle_header_size);
1948
ost->enc_ctx->subtitle_header_size = dec->subtitle_header_size;
1824
if (!av_dict_get(ost->opts, "threads", NULL, 0))
1825
av_dict_set(&ost->opts, "threads", "auto", 0);
1826
if ((ret = avcodec_open2(ost->st->codec, codec, &ost->opts)) < 0) {
1950
if (!av_dict_get(ost->encoder_opts, "threads", NULL, 0))
1951
av_dict_set(&ost->encoder_opts, "threads", "auto", 0);
1952
av_dict_set(&ost->encoder_opts, "side_data_only_packets", "1", 0);
1954
if ((ret = avcodec_open2(ost->enc_ctx, codec, &ost->encoder_opts)) < 0) {
1827
1955
if (ret == AVERROR_EXPERIMENTAL)
1828
1956
abort_codec_experimental(codec, 1);
1829
1957
snprintf(error, sizeof(error), "Error while opening encoder for output stream #%d:%d - maybe incorrect parameters such as bit_rate, rate, width or height",
1830
1958
ost->file_index, ost->index);
1831
1959
goto dump_format;
1833
assert_avoptions(ost->opts);
1834
if (ost->st->codec->bit_rate && ost->st->codec->bit_rate < 1000)
1961
assert_avoptions(ost->encoder_opts);
1962
if (ost->enc_ctx->bit_rate && ost->enc_ctx->bit_rate < 1000)
1835
1963
av_log(NULL, AV_LOG_WARNING, "The bitrate parameter is set too low."
1836
1964
"It takes bits/s as argument, not kbits/s\n");
1837
extra_size += ost->st->codec->extradata_size;
1839
av_opt_set_dict(ost->st->codec, &ost->opts);
1966
av_opt_set_dict(ost->enc_ctx, &ost->encoder_opts);
1969
ret = avcodec_copy_context(ost->st->codec, ost->enc_ctx);
1971
av_log(NULL, AV_LOG_FATAL,
1972
"Error initializing the output stream codec context.\n");
1976
ost->st->time_base = ost->enc_ctx->time_base;
1843
1979
/* init input streams */
1937
2073
ost->sync_ist->st->index);
1938
2074
if (ost->stream_copy)
1939
2075
av_log(NULL, AV_LOG_INFO, " (copy)");
1941
av_log(NULL, AV_LOG_INFO, " (%s -> %s)", input_streams[ost->source_index]->dec ?
1942
input_streams[ost->source_index]->dec->name : "?",
1943
ost->enc ? ost->enc->name : "?");
2077
const AVCodec *in_codec = input_streams[ost->source_index]->dec;
2078
const AVCodec *out_codec = ost->enc;
2079
const char *decoder_name = "?";
2080
const char *in_codec_name = "?";
2081
const char *encoder_name = "?";
2082
const char *out_codec_name = "?";
2085
decoder_name = in_codec->name;
2086
in_codec_name = avcodec_descriptor_get(in_codec->id)->name;
2087
if (!strcmp(decoder_name, in_codec_name))
2088
decoder_name = "native";
2092
encoder_name = out_codec->name;
2093
out_codec_name = avcodec_descriptor_get(out_codec->id)->name;
2094
if (!strcmp(encoder_name, out_codec_name))
2095
encoder_name = "native";
2098
av_log(NULL, AV_LOG_INFO, " (%s (%s) -> %s (%s))",
2099
in_codec_name, decoder_name,
2100
out_codec_name, encoder_name);
1944
2102
av_log(NULL, AV_LOG_INFO, "\n");
2229
2387
goto discard_packet;
2231
2389
ist = input_streams[ifile->ist_index + pkt.stream_index];
2391
ist->data_size += pkt.size;
2232
2394
if (ist->discard)
2233
2395
goto discard_packet;
2397
/* add the stream-global side data to the first packet */
2398
if (ist->nb_packets == 1)
2399
for (i = 0; i < ist->st->nb_side_data; i++) {
2400
AVPacketSideData *src_sd = &ist->st->side_data[i];
2403
if (av_packet_get_side_data(&pkt, src_sd->type, NULL))
2406
dst_data = av_packet_new_side_data(&pkt, src_sd->type, src_sd->size);
2410
memcpy(dst_data, src_sd->data, src_sd->size);
2235
2413
if (pkt.dts != AV_NOPTS_VALUE)
2236
2414
pkt.dts += av_rescale_q(ifile->ts_offset, AV_TIME_BASE_Q, ist->st->time_base);
2237
2415
if (pkt.pts != AV_NOPTS_VALUE)