62 #include <stdatomic.h>
74 #include "libavformat/avformat.h"
75 #include "libavdevice/avdevice.h"
76 #include "libswresample/swresample.h"
77 #include "libavutil/opt.h"
78 #include "libavutil/channel_layout.h"
79 #include "libavutil/parseutils.h"
80 #include "libavutil/samplefmt.h"
81 #include "libavutil/fifo.h"
82 #include "libavutil/hwcontext.h"
83 #include "libavutil/internal.h"
84 #include "libavutil/intreadwrite.h"
85 #include "libavutil/dict.h"
86 #include "libavutil/display.h"
87 #include "libavutil/mathematics.h"
88 #include "libavutil/pixdesc.h"
89 #include "libavutil/avstring.h"
90 #include "libavutil/libm.h"
91 #include "libavutil/imgutils.h"
92 #include "libavutil/timestamp.h"
93 #include "libavutil/bprint.h"
94 #include "libavutil/time.h"
95 #include "libavutil/thread.h"
96 #include "libavutil/threadmessage.h"
97 #include "libavcodec/mathops.h"
98 #include "libavformat/os_support.h"
100 # include "libavfilter/avfilter.h"
101 # include "libavfilter/buffersrc.h"
102 # include "libavfilter/buffersink.h"
104 #if HAVE_SYS_RESOURCE_H
105 #include <sys/time.h>
106 #include <sys/types.h>
107 #include <sys/resource.h>
108 #elif HAVE_GETPROCESSTIMES
111 #if HAVE_GETPROCESSMEMORYINFO
115 #if HAVE_SETCONSOLECTRLHANDLER
120 #if HAVE_SYS_SELECT_H
121 #include <sys/select.h>
126 #include <sys/ioctl.h>
127 #include <sys/time.h>
138 #include "libavutil/avassert.h"
194 extern int opt_map(
void *optctx,
const char *opt,
const char *arg);
195 extern int opt_map_channel(
void *optctx,
const char *opt,
const char *arg);
197 extern int opt_data_frames(
void *optctx,
const char *opt,
const char *arg);
198 extern int opt_progress(
void *optctx,
const char *opt,
const char *arg);
199 extern int opt_target(
void *optctx,
const char *opt,
const char *arg);
200 extern int opt_vsync(
void *optctx,
const char *opt,
const char *arg);
201 extern int opt_abort_on(
void *optctx,
const char *opt,
const char *arg);
202 extern int opt_qscale(
void *optctx,
const char *opt,
const char *arg);
203 extern int opt_profile(
void *optctx,
const char *opt,
const char *arg);
206 extern int opt_attach(
void *optctx,
const char *opt,
const char *arg);
207 extern int opt_video_frames(
void *optctx,
const char *opt,
const char *arg);
209 extern int opt_video_codec(
void *optctx,
const char *opt,
const char *arg);
210 extern int opt_sameq(
void *optctx,
const char *opt,
const char *arg);
211 extern int opt_timecode(
void *optctx,
const char *opt,
const char *arg);
213 extern int opt_vstats_file(
void *optctx,
const char *opt,
const char *arg);
214 extern int opt_vstats(
void *optctx,
const char *opt,
const char *arg);
215 extern int opt_video_frames(
void *optctx,
const char *opt,
const char *arg);
216 extern int opt_old2new(
void *optctx,
const char *opt,
const char *arg);
217 extern int opt_streamid(
void *optctx,
const char *opt,
const char *arg);
218 extern int opt_bitrate(
void *optctx,
const char *opt,
const char *arg);
219 extern int show_hwaccels(
void *optctx,
const char *opt,
const char *arg);
221 extern int opt_audio_frames(
void *optctx,
const char *opt,
const char *arg);
222 extern int opt_audio_qscale(
void *optctx,
const char *opt,
const char *arg);
223 extern int opt_audio_codec(
void *optctx,
const char *opt,
const char *arg);
225 extern int opt_preset(
void *optctx,
const char *opt,
const char *arg);
230 extern int opt_sdp_file(
void *optctx,
const char *opt,
const char *arg);
231 extern int opt_data_codec(
void *optctx,
const char *opt,
const char *arg);
239 __thread
struct termios oldtty;
240 __thread
int restore_tty;
244 static void free_input_threads(
void);
267 av_frame_unref(frame);
271 if ((ret = av_frame_get_buffer(frame, 32)) < 0)
273 memset(frame->data[0], 0, frame->height * frame->linesize[0]);
280 uint32_t *pal, *dst2;
284 if (r->type != SUBTITLE_BITMAP) {
288 if (r->x < 0 || r->x + r->w > w || r->y < 0 || r->y + r->h > h) {
289 av_log(NULL,
AV_LOG_WARNING,
"sub2video: rectangle (%d %d %d %d) overflowing %d %d\n",
290 r->x, r->y, r->w, r->h, w, h
295 dst += r->y * dst_linesize + r->x * 4;
297 pal = (uint32_t *)r->data[1];
298 for (y = 0; y < r->h; y++) {
299 dst2 = (uint32_t *)dst;
301 for (x = 0; x < r->w; x++)
302 *(dst2++) = pal[*(src2++)];
304 src += r->linesize[0];
314 av_assert1(frame->data[0]);
317 ret = av_buffersrc_add_frame_flags(ist->
filters[i]->
filter, frame,
318 AV_BUFFERSRC_FLAG_KEEP_REF |
319 AV_BUFFERSRC_FLAG_PUSH);
320 if (ret != AVERROR_EOF && ret < 0)
321 av_log(NULL,
AV_LOG_WARNING,
"Error while add the frame to buffer source(%s).\n",
332 int64_t pts, end_pts;
337 pts = av_rescale_q(sub->pts + sub->start_display_time * 1000LL,
338 AV_TIME_BASE_Q, ist->
st->time_base);
339 end_pts = av_rescale_q(sub->pts + sub->end_display_time * 1000LL,
340 AV_TIME_BASE_Q, ist->
st->time_base);
341 num_rects = sub->num_rects;
349 "Impossible to get a blank canvas.\n");
352 dst = frame->data [0];
353 dst_linesize = frame->linesize[0];
354 for (i = 0; i < num_rects; i++)
376 pts2 = av_rescale_q(pts, ist->
st->time_base, ist2->
st->time_base) - 1;
378 if (pts2 <= ist2->sub2video.last_pts)
383 for (j = 0, nb_reqs = 0; j < ist2->
nb_filters; j++)
384 nb_reqs += av_buffersrc_get_nb_failed_requests(ist2->
filters[j]->
filter);
399 if (ret != AVERROR_EOF && ret < 0)
410 tcsetattr (0, TCSANOW, &oldtty);
435 ret = write(2,
"Received > 3 system signals, hard exiting\n",
436 strlen(
"Received > 3 system signals, hard exiting\n"));
442 #if HAVE_SETCONSOLECTRLHANDLER
443 static BOOL WINAPI CtrlHandler(DWORD fdwCtrlType)
445 av_log(NULL,
AV_LOG_DEBUG,
"\nReceived windows signal %ld\n", fdwCtrlType);
450 case CTRL_BREAK_EVENT:
454 case CTRL_CLOSE_EVENT:
455 case CTRL_LOGOFF_EVENT:
456 case CTRL_SHUTDOWN_EVENT:
468 av_log(NULL,
AV_LOG_ERROR,
"Received unknown windows signal %ld\n", fdwCtrlType);
479 if (tcgetattr (0, &tty) == 0) {
483 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
484 |INLCR|IGNCR|ICRNL|IXON);
485 tty.c_oflag |= OPOST;
486 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
487 tty.c_cflag &= ~(CSIZE|PARENB);
492 tcsetattr (0, TCSANOW, &tty);
513 signal(SIGPIPE, SIG_IGN);
516 #if HAVE_SETCONSOLECTRLHANDLER
517 SetConsoleCtrlHandler((PHANDLER_ROUTINE) CtrlHandler, TRUE);
534 n = select(1, &rfds, NULL, NULL, &tv);
543 # if HAVE_PEEKNAMEDPIPE
545 static HANDLE input_handle;
548 input_handle = GetStdHandle(STD_INPUT_HANDLE);
549 is_pipe = !GetConsoleMode(input_handle, &dw);
554 if (!PeekNamedPipe(input_handle, NULL, 0, NULL, &nchars, NULL)) {
588 av_log(NULL,
AV_LOG_INFO,
"bench: maxrss=%ikB\n", maxrss);
593 avfilter_graph_free(&fg->
graph);
598 sizeof(frame), NULL);
599 av_frame_free(&frame);
606 &sub,
sizeof(sub), NULL);
607 avsubtitle_free(&sub);
639 if (s && s->oformat && !(s->oformat->flags & AVFMT_NOFILE))
641 avformat_free_context(s);
642 av_dict_free(&of->
opts);
671 avcodec_free_context(&ost->
enc_ctx);
672 avcodec_parameters_free(&ost->
ref_par);
677 av_fifo_generic_read(ost->
muxing_queue, &pkt,
sizeof(pkt), NULL);
678 av_packet_unref(&pkt);
686 free_input_threads();
704 avcodec_free_context(&ist->
dec_ctx);
712 "Error closing vstats file, loss of information possible: %s\n",
713 av_err2str(AVERROR(errno)));
724 avformat_network_deinit();
727 av_log(NULL,
AV_LOG_INFO,
"Exiting normally, received signal %d.\n",
730 av_log(NULL,
AV_LOG_INFO,
"Exiting normally, received cancel signal.\n");
740 AVDictionaryEntry *t = NULL;
742 while ((t = av_dict_get(b,
"", t, AV_DICT_IGNORE_SUFFIX))) {
743 av_dict_set(a, t->key, NULL, AV_DICT_MATCH_CASE);
749 AVDictionaryEntry *t;
750 if ((t = av_dict_get(m,
"", NULL, AV_DICT_IGNORE_SUFFIX))) {
751 av_log(NULL,
AV_LOG_FATAL,
"Option %s not found.\n", t->key);
770 vsnprintf(buf,
sizeof(buf), fmt, va);
773 "bench: %8" PRIu64
" user %8" PRIu64
" sys %8" PRIu64
" real %s \n",
787 ost2->
finished |= ost == ost2 ? this_stream : others;
793 AVFormatContext *s = of->
ctx;
794 AVStream *st = ost->
st;
805 if (!(st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && ost->
encoding_needed) && !unqueue) {
807 av_packet_unref(pkt);
814 AVPacket tmp_pkt = {0};
817 int new_size = FFMIN(2 * av_fifo_size(ost->
muxing_queue),
821 "Too many packets buffered for output stream %d:%d.\n",
829 ret = av_packet_make_refcounted(pkt);
832 av_packet_move_ref(&tmp_pkt, pkt);
833 av_fifo_generic_write(ost->
muxing_queue, &tmp_pkt,
sizeof(tmp_pkt), NULL);
839 pkt->pts = pkt->dts = AV_NOPTS_VALUE;
841 if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
843 uint8_t *sd = av_packet_get_side_data(pkt, AV_PKT_DATA_QUALITY_STATS,
845 ost->
quality = sd ? AV_RL32(sd) : -1;
846 ost->
pict_type = sd ? sd[4] : AV_PICTURE_TYPE_NONE;
848 for (i = 0; i<FF_ARRAY_ELEMS(ost->
error); i++) {
850 ost->
error[i] = AV_RL64(sd + 8 + 8*i);
856 if (pkt->duration > 0)
857 av_log(NULL,
AV_LOG_WARNING,
"Overriding packet duration by frame rate, this should not happen\n");
858 pkt->duration = av_rescale_q(1, av_inv_q(ost->
frame_rate),
865 if (!(s->oformat->flags & AVFMT_NOTIMESTAMPS)) {
866 if (pkt->dts != AV_NOPTS_VALUE &&
867 pkt->pts != AV_NOPTS_VALUE &&
868 pkt->dts > pkt->pts) {
869 av_log(s,
AV_LOG_WARNING,
"Invalid DTS: %"PRId64
" PTS: %"PRId64
" in output stream %d:%d, replacing by guess\n",
877 if ((st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO || st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO || st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE) &&
878 pkt->dts != AV_NOPTS_VALUE &&
879 !(st->codecpar->codec_id == AV_CODEC_ID_VP9 && ost->
stream_copy) &&
881 int64_t max = ost->
last_mux_dts + !(s->oformat->flags & AVFMT_TS_NONSTRICT);
882 if (pkt->dts < max) {
884 av_log(s, loglevel,
"Non-monotonous DTS in output stream "
885 "%d:%d; previous: %"PRId64
", current: %"PRId64
"; ",
891 av_log(s, loglevel,
"changing to %"PRId64
". This may result "
892 "in incorrect timestamps in the output file.\n",
894 if (pkt->pts >= pkt->dts)
895 pkt->pts = FFMAX(pkt->pts, max);
905 pkt->stream_index = ost->
index;
909 "pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s size:%d\n",
910 av_get_media_type_string(ost->
enc_ctx->codec_type),
911 av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &ost->
st->time_base),
912 av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, &ost->
st->time_base),
917 ret = av_interleaved_write_frame(s, pkt);
923 av_packet_unref(pkt);
957 ret = av_bsf_send_packet(ost->
bsf_ctx[0], eof ? NULL : pkt);
965 ret = av_bsf_receive_packet(ost->
bsf_ctx[idx - 1], pkt);
966 if (ret == AVERROR(EAGAIN)) {
970 }
else if (ret == AVERROR_EOF) {
976 if (idx < ost->nb_bitstream_filters) {
977 ret = av_bsf_send_packet(ost->
bsf_ctx[idx], eof ? NULL : pkt);
991 if (ret < 0 && ret != AVERROR_EOF) {
992 av_log(NULL,
AV_LOG_ERROR,
"Error applying bitstream filters to an output "
1005 AV_TIME_BASE_Q) >= 0) {
1015 AVCodecContext *enc = ost->
enc_ctx;
1019 av_init_packet(&pkt);
1028 ost->
sync_opts = frame->pts + frame->nb_samples;
1032 av_assert0(pkt.size || !pkt.data);
1035 av_log(NULL,
AV_LOG_INFO,
"encoder <- type:audio "
1036 "frame_pts:%s frame_pts_time:%s time_base:%d/%d\n",
1037 av_ts2str(frame->pts), av_ts2timestr(frame->pts, &enc->time_base),
1038 enc->time_base.num, enc->time_base.den);
1041 ret = avcodec_send_frame(enc, frame);
1046 ret = avcodec_receive_packet(enc, &pkt);
1047 if (ret == AVERROR(EAGAIN))
1054 av_packet_rescale_ts(&pkt, enc->time_base, ost->
mux_timebase);
1057 av_log(NULL,
AV_LOG_INFO,
"encoder -> type:audio "
1058 "pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s\n",
1059 av_ts2str(pkt.pts), av_ts2timestr(pkt.pts, &enc->time_base),
1060 av_ts2str(pkt.dts), av_ts2timestr(pkt.dts, &enc->time_base));
1076 int subtitle_out_max_size = 1024 * 1024;
1077 int subtitle_out_size, nb, i;
1078 AVCodecContext *enc;
1082 if (sub->pts == AV_NOPTS_VALUE) {
1083 av_log(NULL,
AV_LOG_ERROR,
"Subtitle packets must have a pts\n");
1094 av_log(NULL,
AV_LOG_FATAL,
"Failed to allocate subtitle_out\n");
1102 if (enc->codec_id == AV_CODEC_ID_DVB_SUBTITLE)
1111 for (i = 0; i < nb; i++) {
1112 unsigned save_num_rects = sub->num_rects;
1114 ost->
sync_opts = av_rescale_q(pts, AV_TIME_BASE_Q, enc->time_base);
1120 sub->pts += av_rescale_q(sub->start_display_time, (AVRational){ 1, 1000 }, AV_TIME_BASE_Q);
1121 sub->end_display_time -= sub->start_display_time;
1122 sub->start_display_time = 0;
1128 subtitle_out_size = avcodec_encode_subtitle(enc,
subtitle_out,
1129 subtitle_out_max_size, sub);
1131 sub->num_rects = save_num_rects;
1132 if (subtitle_out_size < 0) {
1133 av_log(NULL,
AV_LOG_FATAL,
"Subtitle encoding failed\n");
1137 av_init_packet(&pkt);
1139 pkt.size = subtitle_out_size;
1140 pkt.pts = av_rescale_q(sub->pts, AV_TIME_BASE_Q, ost->
mux_timebase);
1141 pkt.duration = av_rescale_q(sub->end_display_time, (AVRational){ 1, 1000 }, ost->
mux_timebase);
1142 if (enc->codec_id == AV_CODEC_ID_DVB_SUBTITLE) {
1146 pkt.pts += av_rescale_q(sub->start_display_time, (AVRational){ 1, 1000 }, ost->
mux_timebase);
1148 pkt.pts += av_rescale_q(sub->end_display_time, (AVRational){ 1, 1000 }, ost->
mux_timebase);
1157 AVFrame *next_picture,
1160 int ret, format_video_sync;
1162 AVCodecContext *enc = ost->
enc_ctx;
1163 AVCodecParameters *mux_par = ost->
st->codecpar;
1164 AVRational frame_rate;
1165 int nb_frames, nb0_frames, i;
1166 double delta, delta0;
1167 double duration = 0;
1175 frame_rate = av_buffersink_get_frame_rate(filter);
1176 if (frame_rate.num > 0 && frame_rate.den > 0)
1177 duration = 1/(av_q2d(frame_rate) * av_q2d(enc->time_base));
1179 if(ist && ist->
st->start_time != AV_NOPTS_VALUE && ist->
st->first_dts != AV_NOPTS_VALUE && ost->
frame_rate.num)
1180 duration = FFMIN(duration, 1/(av_q2d(ost->
frame_rate) * av_q2d(enc->time_base)));
1187 lrintf(next_picture->pkt_duration * av_q2d(ist->
st->time_base) / av_q2d(enc->time_base)) > 0) {
1188 duration = lrintf(next_picture->pkt_duration * av_q2d(ist->
st->time_base) / av_q2d(enc->time_base));
1191 if (!next_picture) {
1198 delta = delta0 + duration;
1206 if(!strcmp(of->
ctx->oformat->name,
"avi")) {
1226 if (delta0 < -0.6) {
1227 av_log(NULL,
AV_LOG_VERBOSE,
"Past duration %f too large\n", -delta0);
1229 av_log(NULL,
AV_LOG_DEBUG,
"Clipping frame in rate conversion by %f\n", -delta0);
1235 switch (format_video_sync) {
1238 av_log(NULL,
AV_LOG_DEBUG,
"Not duplicating %d initial frames\n", (
int)lrintf(delta0));
1247 }
else if (delta < -1.1)
1249 else if (delta > 1.1) {
1250 nb_frames = lrintf(delta);
1252 nb0_frames = lrintf(delta0 - 0.6);
1258 else if (delta > 0.6)
1271 nb0_frames = FFMIN(nb0_frames, nb_frames);
1281 "*** dropping frame %d from stream %d at ts %"PRId64
"\n",
1284 if (nb_frames > (nb0_frames && ost->
last_dropped) + (nb_frames > nb0_frames)) {
1286 av_log(NULL,
AV_LOG_ERROR,
"%d frame duplication too large, skipping\n", nb_frames - 1);
1297 ost->
last_dropped = nb_frames == nb0_frames && next_picture;
1300 for (i = 0; i < nb_frames; i++) {
1301 AVFrame *in_picture;
1302 int forced_keyframe = 0;
1304 av_init_packet(&pkt);
1308 if (i < nb0_frames && ost->last_frame) {
1311 in_picture = next_picture;
1321 if (enc->flags & (AV_CODEC_FLAG_INTERLACED_DCT | AV_CODEC_FLAG_INTERLACED_ME) &&
1325 if (in_picture->interlaced_frame) {
1326 if (enc->codec->id == AV_CODEC_ID_MJPEG)
1327 mux_par->field_order = in_picture->top_field_first ? AV_FIELD_TT:AV_FIELD_BB;
1329 mux_par->field_order = in_picture->top_field_first ? AV_FIELD_TB:AV_FIELD_BT;
1331 mux_par->field_order = AV_FIELD_PROGRESSIVE;
1333 in_picture->quality = enc->global_quality;
1334 in_picture->pict_type = 0;
1337 in_picture->pts != AV_NOPTS_VALUE)
1340 pts_time = in_picture->pts != AV_NOPTS_VALUE ?
1345 forced_keyframe = 1;
1351 ff_dlog(NULL,
"force_key_frame: n:%f n_forced:%f prev_forced_n:%f t:%f prev_forced_t:%f -> res:%f\n",
1359 forced_keyframe = 1;
1370 && in_picture->key_frame==1) {
1371 forced_keyframe = 1;
1374 if (forced_keyframe) {
1375 in_picture->pict_type = AV_PICTURE_TYPE_I;
1376 av_log(NULL,
AV_LOG_DEBUG,
"Forced keyframe at time %f\n", pts_time);
1381 av_log(NULL,
AV_LOG_INFO,
"encoder <- type:video "
1382 "frame_pts:%s frame_pts_time:%s time_base:%d/%d\n",
1383 av_ts2str(in_picture->pts), av_ts2timestr(in_picture->pts, &enc->time_base),
1384 enc->time_base.num, enc->time_base.den);
1389 ret = avcodec_send_frame(enc, in_picture);
1393 av_frame_remove_side_data(in_picture, AV_FRAME_DATA_A53_CC);
1396 ret = avcodec_receive_packet(enc, &pkt);
1398 if (ret == AVERROR(EAGAIN))
1404 av_log(NULL,
AV_LOG_INFO,
"encoder -> type:video "
1405 "pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s\n",
1406 av_ts2str(pkt.pts), av_ts2timestr(pkt.pts, &enc->time_base),
1407 av_ts2str(pkt.dts), av_ts2timestr(pkt.dts, &enc->time_base));
1410 if (pkt.pts == AV_NOPTS_VALUE && !(enc->codec->capabilities & AV_CODEC_CAP_DELAY))
1413 av_packet_rescale_ts(&pkt, enc->time_base, ost->
mux_timebase);
1416 av_log(NULL,
AV_LOG_INFO,
"encoder -> type:video "
1417 "pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s\n",
1418 av_ts2str(pkt.pts), av_ts2timestr(pkt.pts, &ost->
mux_timebase),
1419 av_ts2str(pkt.dts), av_ts2timestr(pkt.dts, &ost->
mux_timebase));
1422 frame_size = pkt.size;
1426 if (ost->
logfile && enc->stats_out) {
1427 fprintf(ost->
logfile,
"%s", enc->stats_out);
1458 return -10.0 * log10(d);
1463 AVCodecContext *enc;
1465 double ti1,
bitrate, avg_bitrate;
1477 if (enc->codec_type == AVMEDIA_TYPE_VIDEO) {
1478 frame_number = ost->
st->nb_frames;
1480 fprintf(
vstats_file,
"frame= %5d q= %2.1f ", frame_number,
1481 ost->
quality / (
float)FF_QP2LAMBDA);
1484 ost->
quality / (
float)FF_QP2LAMBDA);
1487 if (ost->
error[0]>=0 && (enc->flags & AV_CODEC_FLAG_PSNR))
1488 fprintf(
vstats_file,
"PSNR= %6.2f ",
psnr(ost->
error[0] / (enc->width * enc->height * 255.0 * 255.0)));
1492 ti1 = av_stream_get_end_pts(ost->
st) * av_q2d(ost->
st->time_base);
1496 bitrate = (frame_size * 8) / av_q2d(enc->time_base) / 1000.0;
1497 avg_bitrate = (double)(ost->
data_size * 8) / ti1 / 1000.0;
1498 fprintf(
vstats_file,
"s_size= %8.0fkB time= %0.3f br= %7.1fkbits/s avg_br= %7.1fkbits/s ",
1514 for (i = 0; i < of->
ctx->nb_streams; i++)
1527 AVFrame *filtered_frame = NULL;
1534 AVFilterContext *filter;
1535 AVCodecContext *enc = ost->
enc_ctx;
1543 char error[1024] =
"";
1546 av_log(NULL,
AV_LOG_ERROR,
"Error initializing output stream %d:%d -- %s\n",
1553 return AVERROR(ENOMEM);
1558 double float_pts = AV_NOPTS_VALUE;
1559 ret = av_buffersink_get_frame_flags(filter, filtered_frame,
1560 AV_BUFFERSINK_FLAG_NO_REQUEST);
1562 if (ret != AVERROR(EAGAIN) && ret != AVERROR_EOF) {
1564 "Error in av_buffersink_get_frame_flags(): %s\n", av_err2str(ret));
1565 }
else if (flush && ret == AVERROR_EOF) {
1566 if (av_buffersink_get_type(filter) == AVMEDIA_TYPE_VIDEO)
1572 av_frame_unref(filtered_frame);
1575 if (filtered_frame->pts != AV_NOPTS_VALUE) {
1577 AVRational filter_tb = av_buffersink_get_time_base(filter);
1578 AVRational tb = enc->time_base;
1579 int extra_bits = av_clip(29 - av_log2(tb.den), 0, 16);
1581 tb.den <<= extra_bits;
1583 av_rescale_q(filtered_frame->pts, filter_tb, tb) -
1584 av_rescale_q(start_time, AV_TIME_BASE_Q, tb);
1585 float_pts /= 1 << extra_bits;
1587 float_pts += FFSIGN(float_pts) * 1.0 / (1<<17);
1589 filtered_frame->pts =
1590 av_rescale_q(filtered_frame->pts, filter_tb, enc->time_base) -
1591 av_rescale_q(start_time, AV_TIME_BASE_Q, enc->time_base);
1594 switch (av_buffersink_get_type(filter)) {
1595 case AVMEDIA_TYPE_VIDEO:
1597 enc->sample_aspect_ratio = filtered_frame->sample_aspect_ratio;
1600 av_log(NULL,
AV_LOG_INFO,
"filter -> pts:%s pts_time:%s exact:%f time_base:%d/%d\n",
1601 av_ts2str(filtered_frame->pts), av_ts2timestr(filtered_frame->pts, &enc->time_base),
1603 enc->time_base.num, enc->time_base.den);
1608 case AVMEDIA_TYPE_AUDIO:
1609 if (!(enc->codec->capabilities & AV_CODEC_CAP_PARAM_CHANGE) &&
1610 enc->channels != filtered_frame->channels) {
1612 "Audio filter graph output is not normalized and encoder does not support parameter changes\n");
1622 av_frame_unref(filtered_frame);
1631 uint64_t video_size = 0, audio_size = 0, extra_size = 0, other_size = 0;
1632 uint64_t subtitle_size = 0;
1633 uint64_t data_size = 0;
1634 float percent = -1.0;
1640 switch (ost->
enc_ctx->codec_type) {
1641 case AVMEDIA_TYPE_VIDEO: video_size += ost->
data_size;
break;
1642 case AVMEDIA_TYPE_AUDIO: audio_size += ost->
data_size;
break;
1643 case AVMEDIA_TYPE_SUBTITLE: subtitle_size += ost->
data_size;
break;
1644 default: other_size += ost->
data_size;
break;
1646 extra_size += ost->
enc_ctx->extradata_size;
1648 if ( (ost->
enc_ctx->flags & (AV_CODEC_FLAG_PASS1 | AV_CODEC_FLAG_PASS2))
1649 != AV_CODEC_FLAG_PASS1)
1653 if (data_size && total_size>0 && total_size >= data_size)
1654 percent = 100.0 * (total_size - data_size) / data_size;
1656 av_log(NULL,
AV_LOG_INFO,
"video:%1.0fkB audio:%1.0fkB subtitle:%1.0fkB other streams:%1.0fkB global headers:%1.0fkB muxing overhead: ",
1657 video_size / 1024.0,
1658 audio_size / 1024.0,
1659 subtitle_size / 1024.0,
1660 other_size / 1024.0,
1661 extra_size / 1024.0);
1671 uint64_t total_packets = 0, total_size = 0;
1678 enum AVMediaType type = ist->
dec_ctx->codec_type;
1685 av_log(NULL,
AV_LOG_VERBOSE,
"%"PRIu64
" packets read (%"PRIu64
" bytes); ",
1691 if (type == AVMEDIA_TYPE_AUDIO)
1699 av_log(NULL,
AV_LOG_VERBOSE,
" Total: %"PRIu64
" packets (%"PRIu64
" bytes) demuxed\n",
1700 total_packets, total_size);
1705 uint64_t total_packets = 0, total_size = 0;
1710 for (j = 0; j < of->
ctx->nb_streams; j++) {
1712 enum AVMediaType type = ost->
enc_ctx->codec_type;
1722 if (type == AVMEDIA_TYPE_AUDIO)
1727 av_log(NULL,
AV_LOG_VERBOSE,
"%"PRIu64
" packets muxed (%"PRIu64
" bytes); ",
1733 av_log(NULL,
AV_LOG_VERBOSE,
" Total: %"PRIu64
" packets (%"PRIu64
" bytes) muxed\n",
1734 total_packets, total_size);
1736 if(video_size + data_size + audio_size + subtitle_size + extra_size == 0){
1737 av_log(NULL,
AV_LOG_WARNING,
"Output file is empty, nothing was encoded ");
1741 av_log(NULL,
AV_LOG_WARNING,
"(check -ss / -t / -frames parameters if used)\n");
1747 static void forward_report(
int is_last_report, int64_t timer_start, int64_t cur_time)
1749 AVFormatContext *oc = NULL;
1750 AVCodecContext *enc = NULL;
1752 static int64_t last_time = -1;
1753 int64_t pts = INT64_MIN + 1;
1756 int frame_number = 0;
1759 int64_t total_size = 0;
1765 if (!is_last_report) {
1766 if (last_time == -1) {
1767 last_time = cur_time;
1770 if ((cur_time - last_time) < 500000) {
1773 last_time = cur_time;
1775 float t = (cur_time-timer_start) / 1000000.0;
1780 total_size = avio_size(oc->pb);
1781 if (total_size <= 0) {
1782 total_size = avio_tell(oc->pb);
1793 quality = ost->
quality / (float) FF_QP2LAMBDA;
1796 if (!vid && enc->codec_type == AVMEDIA_TYPE_VIDEO) {
1802 fps = t > 1 ? frame_number / t : 0;
1806 if (av_stream_get_end_pts(ost->
st) != AV_NOPTS_VALUE)
1807 pts = FFMAX(pts, av_rescale_q(av_stream_get_end_pts(ost->
st),
1808 ost->
st->time_base, AV_TIME_BASE_Q));
1814 seconds = FFABS(pts) / 1000;
1817 bitrate = pts && total_size >= 0 ? total_size * 8 / (pts / 1000.0) : -1;
1820 speed = t != 0.0 ? (double)pts / AV_TIME_BASE / t : -1;
1828 static void print_report(
int is_last_report, int64_t timer_start, int64_t cur_time)
1830 AVBPrint buf, buf_script;
1832 AVFormatContext *oc;
1834 AVCodecContext *enc;
1835 int frame_number, vid, i;
1838 int64_t pts = INT64_MIN + 1;
1839 static int64_t last_time = -1;
1840 static int qp_histogram[52];
1841 int hours, mins, secs, us;
1842 const char *hours_sign;
1852 if (!is_last_report) {
1853 if (last_time == -1) {
1854 last_time = cur_time;
1857 if ((cur_time - last_time) < 500000)
1859 last_time = cur_time;
1862 t = (cur_time-timer_start) / 1000000.0;
1867 total_size = avio_size(oc->pb);
1868 if (total_size <= 0)
1869 total_size = avio_tell(oc->pb);
1872 av_bprint_init(&buf, 0, AV_BPRINT_SIZE_AUTOMATIC);
1873 av_bprint_init(&buf_script, 0, AV_BPRINT_SIZE_AUTOMATIC);
1879 q = ost->
quality / (float) FF_QP2LAMBDA;
1881 if (vid && enc->codec_type == AVMEDIA_TYPE_VIDEO) {
1882 av_bprintf(&buf,
"q=%2.1f ", q);
1883 av_bprintf(&buf_script,
"stream_%d_%d_q=%.1f\n",
1886 if (!vid && enc->codec_type == AVMEDIA_TYPE_VIDEO) {
1890 fps = t > 1 ? frame_number / t : 0;
1891 av_bprintf(&buf,
"frame=%5d fps=%3.*f q=%3.1f ",
1892 frame_number, fps < 9.95, fps, q);
1893 av_bprintf(&buf_script,
"frame=%d\n", frame_number);
1894 av_bprintf(&buf_script,
"fps=%.2f\n", fps);
1895 av_bprintf(&buf_script,
"stream_%d_%d_q=%.1f\n",
1898 av_bprintf(&buf,
"L");
1902 if (qp >= 0 && qp < FF_ARRAY_ELEMS(qp_histogram))
1904 for (j = 0; j < 32; j++)
1905 av_bprintf(&buf,
"%X", av_log2(qp_histogram[j] + 1));
1908 if ((enc->flags & AV_CODEC_FLAG_PSNR) && (ost->
pict_type != AV_PICTURE_TYPE_NONE || is_last_report)) {
1910 double error, error_sum = 0;
1911 double scale, scale_sum = 0;
1913 char type[3] = {
'Y',
'U',
'V' };
1914 av_bprintf(&buf,
"PSNR=");
1915 for (j = 0; j < 3; j++) {
1916 if (is_last_report) {
1917 error = enc->error[j];
1918 scale = enc->width * enc->height * 255.0 * 255.0 * frame_number;
1920 error = ost->
error[j];
1921 scale = enc->width * enc->height * 255.0 * 255.0;
1927 p =
psnr(error / scale);
1928 av_bprintf(&buf,
"%c:%2.2f ", type[j], p);
1929 av_bprintf(&buf_script,
"stream_%d_%d_psnr_%c=%2.2f\n",
1932 p =
psnr(error_sum / scale_sum);
1933 av_bprintf(&buf,
"*:%2.2f ",
psnr(error_sum / scale_sum));
1934 av_bprintf(&buf_script,
"stream_%d_%d_psnr_all=%2.2f\n",
1940 if (av_stream_get_end_pts(ost->
st) != AV_NOPTS_VALUE)
1941 pts = FFMAX(pts, av_rescale_q(av_stream_get_end_pts(ost->
st),
1942 ost->
st->time_base, AV_TIME_BASE_Q));
1947 secs = FFABS(pts) / AV_TIME_BASE;
1948 us = FFABS(pts) % AV_TIME_BASE;
1953 hours_sign = (pts < 0) ?
"-" :
"";
1955 bitrate = pts && total_size >= 0 ? total_size * 8 / (pts / 1000.0) : -1;
1956 speed = t != 0.0 ? (double)pts / AV_TIME_BASE / t : -1;
1958 if (total_size < 0) av_bprintf(&buf,
"size=N/A time=");
1959 else av_bprintf(&buf,
"size=%8.0fkB time=", total_size / 1024.0);
1960 if (pts == AV_NOPTS_VALUE) {
1961 av_bprintf(&buf,
"N/A ");
1963 av_bprintf(&buf,
"%s%02d:%02d:%02d.%02d ",
1964 hours_sign, hours, mins, secs, (100 * us) / AV_TIME_BASE);
1968 av_bprintf(&buf,
"bitrate=N/A");
1969 av_bprintf(&buf_script,
"bitrate=N/A\n");
1971 av_bprintf(&buf,
"bitrate=%6.1fkbits/s",
bitrate);
1972 av_bprintf(&buf_script,
"bitrate=%6.1fkbits/s\n",
bitrate);
1975 if (total_size < 0) av_bprintf(&buf_script,
"total_size=N/A\n");
1976 else av_bprintf(&buf_script,
"total_size=%"PRId64
"\n", total_size);
1977 if (pts == AV_NOPTS_VALUE) {
1978 av_bprintf(&buf_script,
"out_time_us=N/A\n");
1979 av_bprintf(&buf_script,
"out_time_ms=N/A\n");
1980 av_bprintf(&buf_script,
"out_time=N/A\n");
1982 av_bprintf(&buf_script,
"out_time_us=%"PRId64
"\n", pts);
1983 av_bprintf(&buf_script,
"out_time_ms=%"PRId64
"\n", pts);
1984 av_bprintf(&buf_script,
"out_time=%s%02d:%02d:%02d.%06d\n",
1985 hours_sign, hours, mins, secs, us);
1994 av_bprintf(&buf,
" speed=N/A");
1995 av_bprintf(&buf_script,
"speed=N/A\n");
1997 av_bprintf(&buf,
" speed=%4.3gx",
speed);
1998 av_bprintf(&buf_script,
"speed=%4.3gx\n",
speed);
2002 const char end = is_last_report ?
'\n' :
'\r';
2004 fprintf(stderr,
"%s %c", buf.str, end);
2010 av_bprint_finalize(&buf, NULL);
2013 av_bprintf(&buf_script,
"progress=%s\n",
2014 is_last_report ?
"end" :
"continue");
2016 FFMIN(buf_script.len, buf_script.size - 1));
2018 av_bprint_finalize(&buf_script, NULL);
2019 if (is_last_report) {
2022 "Error closing progress log, loss of information possible: %s\n", av_err2str(ret));
2034 ifilter->
format = par->format;
2038 ifilter->
width = par->width;
2039 ifilter->
height = par->height;
2049 AVCodecContext *enc = ost->
enc_ctx;
2059 char error[1024] =
"";
2062 "Finishing stream %d:%d without any data written to it.\n",
2078 av_log(NULL,
AV_LOG_ERROR,
"Error configuring filter graph\n");
2087 av_log(NULL,
AV_LOG_ERROR,
"Error initializing output stream %d:%d -- %s\n",
2093 if (enc->codec_type == AVMEDIA_TYPE_AUDIO && enc->frame_size <= 1)
2096 if (enc->codec_type != AVMEDIA_TYPE_VIDEO && enc->codec_type != AVMEDIA_TYPE_AUDIO)
2100 const char *desc = NULL;
2104 switch (enc->codec_type) {
2105 case AVMEDIA_TYPE_AUDIO:
2108 case AVMEDIA_TYPE_VIDEO:
2115 av_init_packet(&pkt);
2121 while ((ret = avcodec_receive_packet(enc, &pkt)) == AVERROR(EAGAIN)) {
2122 ret = avcodec_send_frame(enc, NULL);
2132 if (ret < 0 && ret != AVERROR_EOF) {
2138 if (ost->
logfile && enc->stats_out) {
2139 fprintf(ost->
logfile,
"%s", enc->stats_out);
2141 if (ret == AVERROR_EOF) {
2146 av_packet_unref(&pkt);
2149 av_packet_rescale_ts(&pkt, enc->time_base, ost->
mux_timebase);
2150 pkt_size = pkt.size;
2184 int64_t ost_tb_start_time = av_rescale_q(start_time, AV_TIME_BASE_Q, ost->
mux_timebase);
2189 av_init_packet(&opkt);
2196 if ((!ost->
frame_number && !(pkt->flags & AV_PKT_FLAG_KEY)) &&
2201 int64_t comp_start = start_time;
2204 if (pkt->pts == AV_NOPTS_VALUE ?
2205 ist->
pts < comp_start :
2206 pkt->pts < av_rescale_q(comp_start, AV_TIME_BASE_Q, ist->
st->time_base))
2217 start_time = f->
ctx->start_time;
2227 if (ost->
enc_ctx->codec_type == AVMEDIA_TYPE_VIDEO)
2230 if (av_packet_ref(&opkt, pkt) < 0)
2233 if (pkt->pts != AV_NOPTS_VALUE)
2234 opkt.pts = av_rescale_q(pkt->pts, ist->
st->time_base, ost->
mux_timebase) - ost_tb_start_time;
2236 if (pkt->dts == AV_NOPTS_VALUE)
2239 opkt.dts = av_rescale_q(pkt->dts, ist->
st->time_base, ost->
mux_timebase);
2240 opkt.dts -= ost_tb_start_time;
2242 if (ost->
st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO && pkt->dts != AV_NOPTS_VALUE) {
2243 int duration = av_get_audio_frame_duration(ist->
dec_ctx, pkt->size);
2245 duration = ist->
dec_ctx->frame_size;
2246 opkt.dts = opkt.pts = av_rescale_delta(ist->
st->time_base, pkt->dts,
2251 opkt.duration = av_rescale_q(pkt->duration, ist->
st->time_base, ost->
mux_timebase);
2258 AVCodecContext *dec = ist->
dec_ctx;
2260 if (!dec->channel_layout) {
2261 char layout_name[256];
2265 dec->channel_layout = av_get_default_channel_layout(dec->channels);
2266 if (!dec->channel_layout)
2268 av_get_channel_layout_string(layout_name,
sizeof(layout_name),
2269 dec->channels, dec->channel_layout);
2270 av_log(NULL,
AV_LOG_WARNING,
"Guessed Channel Layout for Input Stream "
2271 "#%d.%d : %s\n", ist->
file_index, ist->
st->index, layout_name);
2278 if (*got_output || ret<0)
2284 if (*got_output && ist) {
2309 int need_reinit, ret, i;
2312 need_reinit = ifilter->
format != frame->format;
2314 switch (ifilter->
ist->
st->codecpar->codec_type) {
2315 case AVMEDIA_TYPE_AUDIO:
2316 need_reinit |= ifilter->
sample_rate != frame->sample_rate ||
2317 ifilter->
channels != frame->channels ||
2320 case AVMEDIA_TYPE_VIDEO:
2321 need_reinit |= ifilter->
width != frame->width ||
2322 ifilter->
height != frame->height;
2340 if (need_reinit || !fg->
graph) {
2343 AVFrame *tmp = av_frame_clone(frame);
2345 return AVERROR(ENOMEM);
2346 av_frame_unref(frame);
2351 av_frame_free(&tmp);
2355 av_fifo_generic_write(ifilter->
frame_queue, &tmp,
sizeof(tmp), NULL);
2361 if (ret < 0 && ret != AVERROR_EOF) {
2362 av_log(NULL,
AV_LOG_ERROR,
"Error while filtering: %s\n", av_err2str(ret));
2368 av_log(NULL,
AV_LOG_ERROR,
"Error reinitializing filters!\n");
2373 ret = av_buffersrc_add_frame_flags(ifilter->
filter, frame, AV_BUFFERSRC_FLAG_PUSH);
2375 if (ret != AVERROR_EOF)
2376 av_log(NULL,
AV_LOG_ERROR,
"Error while filtering: %s\n", av_err2str(ret));
2393 ret = av_buffersrc_close(ifilter->
filter, pts, AV_BUFFERSRC_FLAG_PUSH);
2401 if (ifilter->
format < 0 && (ifilter->
type == AVMEDIA_TYPE_AUDIO || ifilter->
type == AVMEDIA_TYPE_VIDEO)) {
2403 return AVERROR_INVALIDDATA;
2414 static int decode(AVCodecContext *avctx, AVFrame *frame,
int *got_frame, AVPacket *pkt)
2421 ret = avcodec_send_packet(avctx, pkt);
2424 if (ret < 0 && ret != AVERROR_EOF)
2428 ret = avcodec_receive_frame(avctx, frame);
2429 if (ret < 0 && ret != AVERROR(EAGAIN))
2444 if (i < ist->nb_filters - 1) {
2446 ret = av_frame_ref(f, decoded_frame);
2452 if (ret == AVERROR_EOF)
2456 "Failed to inject frame into filter network: %s\n", av_err2str(ret));
2466 AVFrame *decoded_frame;
2467 AVCodecContext *avctx = ist->
dec_ctx;
2469 AVRational decoded_frame_tb;
2472 return AVERROR(ENOMEM);
2474 return AVERROR(ENOMEM);
2478 ret =
decode(avctx, decoded_frame, got_output, pkt);
2483 if (ret >= 0 && avctx->sample_rate <= 0) {
2484 av_log(avctx,
AV_LOG_ERROR,
"Sample rate %d invalid\n", avctx->sample_rate);
2485 ret = AVERROR_INVALIDDATA;
2488 if (ret != AVERROR_EOF)
2491 if (!*got_output || ret < 0)
2499 ist->
next_pts += ((int64_t)AV_TIME_BASE * decoded_frame->nb_samples) /
2501 ist->
next_dts += ((int64_t)AV_TIME_BASE * decoded_frame->nb_samples) /
2504 if (decoded_frame->pts != AV_NOPTS_VALUE) {
2505 decoded_frame_tb = ist->
st->time_base;
2506 }
else if (pkt && pkt->pts != AV_NOPTS_VALUE) {
2507 decoded_frame->pts = pkt->pts;
2508 decoded_frame_tb = ist->
st->time_base;
2510 decoded_frame->pts = ist->
dts;
2511 decoded_frame_tb = AV_TIME_BASE_Q;
2513 if (decoded_frame->pts != AV_NOPTS_VALUE)
2514 decoded_frame->pts = av_rescale_delta(decoded_frame_tb, decoded_frame->pts,
2516 (AVRational){1, avctx->sample_rate});
2521 av_frame_unref(decoded_frame);
2522 return err < 0 ? err : ret;
2528 AVFrame *decoded_frame;
2529 int i, ret = 0, err = 0;
2530 int64_t best_effort_timestamp;
2531 int64_t dts = AV_NOPTS_VALUE;
2537 if (!eof && pkt && pkt->size == 0)
2541 return AVERROR(ENOMEM);
2543 return AVERROR(ENOMEM);
2545 if (ist->
dts != AV_NOPTS_VALUE)
2546 dts = av_rescale_q(ist->
dts, AV_TIME_BASE_Q, ist->
st->time_base);
2557 return AVERROR(ENOMEM);
2563 ret =
decode(ist->
dec_ctx, decoded_frame, got_output, pkt ? &avpkt : NULL);
2570 if (ist->
st->codecpar->video_delay < ist->
dec_ctx->has_b_frames) {
2571 if (ist->
dec_ctx->codec_id == AV_CODEC_ID_H264) {
2572 ist->
st->codecpar->video_delay = ist->
dec_ctx->has_b_frames;
2575 "video_delay is larger in decoder than demuxer %d > %d.\n"
2576 "If you want to help, upload a sample "
2577 "of this file to ftp://upload.ffmpeg.org/incoming/ "
2578 "and contact the ffmpeg-devel mailing list. (ffmpeg-devel@ffmpeg.org)\n",
2580 ist->
st->codecpar->video_delay);
2583 if (ret != AVERROR_EOF)
2586 if (*got_output && ret >= 0) {
2587 if (ist->
dec_ctx->width != decoded_frame->width ||
2588 ist->
dec_ctx->height != decoded_frame->height ||
2589 ist->
dec_ctx->pix_fmt != decoded_frame->format) {
2590 av_log(NULL,
AV_LOG_DEBUG,
"Frame parameters mismatch context %d,%d,%d != %d,%d,%d\n",
2591 decoded_frame->width,
2592 decoded_frame->height,
2593 decoded_frame->format,
2600 if (!*got_output || ret < 0)
2615 best_effort_timestamp= decoded_frame->best_effort_timestamp;
2616 *duration_pts = decoded_frame->pkt_duration;
2621 if (eof && best_effort_timestamp == AV_NOPTS_VALUE && ist->
nb_dts_buffer > 0) {
2629 if(best_effort_timestamp != AV_NOPTS_VALUE) {
2630 int64_t ts = av_rescale_q(decoded_frame->pts = best_effort_timestamp, ist->
st->time_base, AV_TIME_BASE_Q);
2632 if (ts != AV_NOPTS_VALUE)
2637 av_log(NULL,
AV_LOG_INFO,
"decoder -> ist_index:%d type:video "
2638 "frame_pts:%s frame_pts_time:%s best_effort_ts:%"PRId64
" best_effort_ts_time:%s keyframe:%d frame_type:%d time_base:%d/%d\n",
2639 ist->
st->index, av_ts2str(decoded_frame->pts),
2640 av_ts2timestr(decoded_frame->pts, &ist->
st->time_base),
2641 best_effort_timestamp,
2642 av_ts2timestr(best_effort_timestamp, &ist->
st->time_base),
2643 decoded_frame->key_frame, decoded_frame->pict_type,
2644 ist->
st->time_base.num, ist->
st->time_base.den);
2647 if (ist->
st->sample_aspect_ratio.num)
2648 decoded_frame->sample_aspect_ratio = ist->
st->sample_aspect_ratio;
2654 av_frame_unref(decoded_frame);
2655 return err < 0 ? err : ret;
2661 AVSubtitle subtitle;
2663 int i, ret = avcodec_decode_subtitle2(ist->
dec_ctx,
2664 &subtitle, got_output, pkt);
2668 if (ret < 0 || !*got_output) {
2679 1000, AV_TIME_BASE);
2680 if (end < ist->prev_sub.subtitle.end_display_time) {
2682 "Subtitle duration reduced from %"PRId32
" to %d%s\n",
2684 end <= 0 ?
", dropping it" :
"");
2714 if (!subtitle.num_rects)
2723 || ost->
enc->type != AVMEDIA_TYPE_SUBTITLE)
2731 avsubtitle_free(&subtitle);
2739 int64_t pts = av_rescale_q_rnd(ist->
pts, AV_TIME_BASE_Q, ist->
st->time_base,
2740 AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX);
2755 int eof_reached = 0;
2759 ist->
dts = ist->
st->avg_frame_rate.num ? - ist->
dec_ctx->has_b_frames * AV_TIME_BASE / av_q2d(ist->
st->avg_frame_rate) : 0;
2762 ist->
dts += av_rescale_q(pkt->pts, ist->
st->time_base, AV_TIME_BASE_Q);
2768 if (ist->
next_dts == AV_NOPTS_VALUE)
2770 if (ist->
next_pts == AV_NOPTS_VALUE)
2775 av_init_packet(&avpkt);
2782 if (pkt && pkt->dts != AV_NOPTS_VALUE) {
2783 ist->
next_dts = ist->
dts = av_rescale_q(pkt->dts, ist->
st->time_base, AV_TIME_BASE_Q);
2790 int64_t duration_dts = 0;
2791 int64_t duration_pts = 0;
2793 int decode_failed = 0;
2798 switch (ist->
dec_ctx->codec_type) {
2799 case AVMEDIA_TYPE_AUDIO:
2800 ret =
decode_audio (ist, repeating ? NULL : &avpkt, &got_output,
2803 case AVMEDIA_TYPE_VIDEO:
2804 ret =
decode_video (ist, repeating ? NULL : &avpkt, &got_output, &duration_pts, !pkt,
2806 if (!repeating || !pkt || got_output) {
2807 if (pkt && pkt->duration) {
2808 duration_dts = av_rescale_q(pkt->duration, ist->
st->time_base, AV_TIME_BASE_Q);
2809 }
else if(ist->
dec_ctx->framerate.num != 0 && ist->
dec_ctx->framerate.den != 0) {
2810 int ticks= av_stream_get_parser(ist->
st) ? av_stream_get_parser(ist->
st)->repeat_pict+1 : ist->
dec_ctx->ticks_per_frame;
2811 duration_dts = ((int64_t)AV_TIME_BASE *
2812 ist->
dec_ctx->framerate.den * ticks) /
2816 if(ist->
dts != AV_NOPTS_VALUE && duration_dts) {
2823 if (duration_pts > 0) {
2824 ist->
next_pts += av_rescale_q(duration_pts, ist->
st->time_base, AV_TIME_BASE_Q);
2830 case AVMEDIA_TYPE_SUBTITLE:
2834 if (!pkt && ret >= 0)
2841 if (ret == AVERROR_EOF) {
2847 if (decode_failed) {
2848 av_log(NULL,
AV_LOG_ERROR,
"Error while decoding stream #%d:%d: %s\n",
2851 av_log(NULL,
AV_LOG_FATAL,
"Error while processing the decoded "
2852 "data for stream #%d:%d\n", ist->
file_index, ist->
st->index);
2884 av_log(NULL,
AV_LOG_FATAL,
"Error marking filters as finished\n");
2892 switch (ist->
dec_ctx->codec_type) {
2893 case AVMEDIA_TYPE_AUDIO:
2894 av_assert1(pkt->duration >= 0);
2895 if (ist->
dec_ctx->sample_rate) {
2899 ist->
next_dts += av_rescale_q(pkt->duration, ist->
st->time_base, AV_TIME_BASE_Q);
2902 case AVMEDIA_TYPE_VIDEO:
2905 AVRational time_base_q = AV_TIME_BASE_Q;
2906 int64_t next_dts = av_rescale_q(ist->
next_dts, time_base_q, av_inv_q(ist->
framerate));
2907 ist->
next_dts = av_rescale_q(next_dts + 1, av_inv_q(ist->
framerate), time_base_q);
2908 }
else if (pkt->duration) {
2909 ist->
next_dts += av_rescale_q(pkt->duration, ist->
st->time_base, AV_TIME_BASE_Q);
2910 }
else if(ist->
dec_ctx->framerate.num != 0) {
2911 int ticks= av_stream_get_parser(ist->
st) ? av_stream_get_parser(ist->
st)->repeat_pict + 1 : ist->
dec_ctx->ticks_per_frame;
2912 ist->
next_dts += ((int64_t)AV_TIME_BASE *
2913 ist->
dec_ctx->framerate.den * ticks) /
2930 return !eof_reached;
2938 AVIOContext *sdp_pb;
2939 AVFormatContext **avc;
2950 if (!strcmp(
output_files[i]->ctx->oformat->name,
"rtp")) {
2959 av_sdp_create(avc, j, sdp,
sizeof(sdp));
2968 avio_printf(sdp_pb,
"SDP:\n%s", sdp);
2969 avio_closep(&sdp_pb);
2978 static enum AVPixelFormat
get_format(AVCodecContext *s,
const enum AVPixelFormat *pix_fmts)
2981 const enum AVPixelFormat *p;
2984 for (p = pix_fmts; *p != AV_PIX_FMT_NONE; p++) {
2985 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(*p);
2986 const AVCodecHWConfig *config = NULL;
2989 if (!(desc->flags & AV_PIX_FMT_FLAG_HWACCEL))
2995 config = avcodec_get_hw_config(s->codec, i);
2998 if (!(config->methods &
2999 AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX))
3001 if (config->pix_fmt == *p)
3015 "%s hwaccel requested for input stream #%d:%d, "
3016 "but cannot be initialized.\n",
3017 av_hwdevice_get_type_name(config->device_type),
3019 return AV_PIX_FMT_NONE;
3024 const HWAccel *hwaccel = NULL;
3041 ret = hwaccel->
init(s);
3044 "%s hwaccel requested for input stream #%d:%d, "
3045 "but cannot be initialized.\n", hwaccel->
name,
3047 return AV_PIX_FMT_NONE;
3053 if (!s->hw_frames_ctx)
3054 return AV_PIX_FMT_NONE;
3064 static int get_buffer(AVCodecContext *s, AVFrame *frame,
int flags)
3071 return avcodec_default_get_buffer2(s, frame, flags);
3080 AVCodec *codec = ist->
dec;
3082 snprintf(error, error_len,
"Decoder (codec %s) not found for input stream #%d:%d",
3084 return AVERROR(EINVAL);
3090 ist->
dec_ctx->thread_safe_callbacks = 1;
3092 av_opt_set_int(ist->
dec_ctx,
"refcounted_frames", 1, 0);
3093 if (ist->
dec_ctx->codec_id == AV_CODEC_ID_DVB_SUBTITLE &&
3095 av_dict_set(&ist->
decoder_opts,
"compute_edt",
"1", AV_DICT_DONT_OVERWRITE);
3097 av_log(NULL,
AV_LOG_WARNING,
"Warning using DVB subtitles for filtering and output at the same time is not fully supported, also see -compute_edt [0|1]\n");
3100 av_dict_set(&ist->
decoder_opts,
"sub_text_format",
"ass", AV_DICT_DONT_OVERWRITE);
3104 ist->
dec_ctx->pkt_timebase = ist->
st->time_base;
3106 if (!av_dict_get(ist->
decoder_opts,
"threads", NULL, 0))
3109 if (ist->
st->disposition & AV_DISPOSITION_ATTACHED_PIC)
3114 snprintf(error, error_len,
"Device setup failed for "
3115 "decoder on input stream #%d:%d : %s",
3121 if (ret == AVERROR_EXPERIMENTAL)
3124 snprintf(error, error_len,
3125 "Error while opening decoder for input stream "
3148 return FFDIFFSIGN(*(
const int64_t *)a, *(
const int64_t *)b);
3156 for (i = 0; i < of->
ctx->nb_streams; i++) {
3164 ret = avformat_write_header(of->
ctx, &of->
opts);
3167 "Could not write header for output file #%d "
3168 "(incorrect codec parameters ?): %s\n",
3169 file_index, av_err2str(ret));
3175 av_dump_format(of->
ctx, file_index, of->
ctx->url, 1);
3181 for (i = 0; i < of->
ctx->nb_streams; i++) {
3190 av_fifo_generic_read(ost->
muxing_queue, &pkt,
sizeof(pkt), NULL);
3209 ret = avcodec_parameters_copy(ctx->par_in,
3210 i ? ost->
bsf_ctx[i - 1]->par_out : ost->
st->codecpar);
3214 ctx->time_base_in = i ? ost->
bsf_ctx[i - 1]->time_base_out : ost->
st->time_base;
3216 ret = av_bsf_init(ctx);
3218 av_log(NULL,
AV_LOG_ERROR,
"Error initializing bitstream filter: %s\n",
3219 ost->
bsf_ctx[i]->filter->name);
3225 ret = avcodec_parameters_copy(ost->
st->codecpar, ctx->par_out);
3229 ost->
st->time_base = ctx->time_base_out;
3238 AVCodecParameters *par_dst = ost->
st->codecpar;
3239 AVCodecParameters *par_src = ost->
ref_par;
3242 uint32_t codec_tag = par_dst->codec_tag;
3244 av_assert0(ist && !ost->
filter);
3246 ret = avcodec_parameters_to_context(ost->
enc_ctx, ist->
st->codecpar);
3251 "Error setting up codec context options.\n");
3255 ret = avcodec_parameters_from_context(par_src, ost->
enc_ctx);
3258 "Error getting reference codec parameters.\n");
3263 unsigned int codec_tag_tmp;
3264 if (!of->
ctx->oformat->codec_tag ||
3265 av_codec_get_id (of->
ctx->oformat->codec_tag, par_src->codec_tag) == par_src->codec_id ||
3266 !av_codec_get_tag2(of->
ctx->oformat->codec_tag, par_src->codec_id, &codec_tag_tmp))
3267 codec_tag = par_src->codec_tag;
3270 ret = avcodec_parameters_copy(par_dst, par_src);
3274 par_dst->codec_tag = codec_tag;
3280 ret = avformat_transfer_internal_stream_timing_info(of->
ctx->oformat, ost->
st, ist->
st,
copy_tb);
3285 if (ost->
st->time_base.num <= 0 || ost->
st->time_base.den <= 0)
3286 ost->
st->time_base = av_add_q(av_stream_get_codec_timebase(ost->
st), (AVRational){0, 1});
3289 if (ost->
st->duration <= 0 && ist->
st->duration > 0)
3290 ost->
st->duration = av_rescale_q(ist->
st->duration, ist->
st->time_base, ost->
st->time_base);
3293 ost->
st->disposition = ist->
st->disposition;
3295 if (ist->
st->nb_side_data) {
3296 for (i = 0; i < ist->
st->nb_side_data; i++) {
3297 const AVPacketSideData *sd_src = &ist->
st->side_data[i];
3300 dst_data = av_stream_new_side_data(ost->
st, sd_src->type, sd_src->size);
3302 return AVERROR(ENOMEM);
3303 memcpy(dst_data, sd_src->data, sd_src->size);
3308 uint8_t *sd = av_stream_new_side_data(ost->
st, AV_PKT_DATA_DISPLAYMATRIX,
3309 sizeof(int32_t) * 9);
3314 switch (par_dst->codec_type) {
3315 case AVMEDIA_TYPE_AUDIO:
3317 av_log(NULL,
AV_LOG_FATAL,
"-acodec copy and -vol are incompatible (frames are not decoded)\n");
3320 if((par_dst->block_align == 1 || par_dst->block_align == 1152 || par_dst->block_align == 576) && par_dst->codec_id == AV_CODEC_ID_MP3)
3321 par_dst->block_align= 0;
3322 if(par_dst->codec_id == AV_CODEC_ID_AC3)
3323 par_dst->block_align= 0;
3325 case AVMEDIA_TYPE_VIDEO:
3329 (AVRational){ par_dst->height, par_dst->width });
3331 "with stream copy may produce invalid files\n");
3333 else if (ist->
st->sample_aspect_ratio.num)
3334 sar = ist->
st->sample_aspect_ratio;
3336 sar = par_src->sample_aspect_ratio;
3337 ost->
st->sample_aspect_ratio = par_dst->sample_aspect_ratio = sar;
3338 ost->
st->avg_frame_rate = ist->
st->avg_frame_rate;
3339 ost->
st->r_frame_rate = ist->
st->r_frame_rate;
3350 AVDictionaryEntry *e;
3352 uint8_t *encoder_string;
3353 int encoder_string_len;
3354 int format_flags = 0;
3355 int codec_flags = ost->
enc_ctx->flags;
3357 if (av_dict_get(ost->
st->metadata,
"encoder", NULL, 0))
3360 e = av_dict_get(of->
opts,
"fflags", NULL, 0);
3362 const AVOption *o = av_opt_find(of->
ctx,
"fflags", NULL, 0, 0);
3365 av_opt_eval_flags(of->
ctx, o, e->value, &format_flags);
3369 const AVOption *o = av_opt_find(ost->
enc_ctx,
"flags", NULL, 0, 0);
3372 av_opt_eval_flags(ost->
enc_ctx, o, e->value, &codec_flags);
3375 encoder_string_len =
sizeof(LIBAVCODEC_IDENT) + strlen(ost->
enc->name) + 2;
3376 encoder_string = av_mallocz(encoder_string_len);
3377 if (!encoder_string)
3380 if (!(format_flags & AVFMT_FLAG_BITEXACT) && !(codec_flags & AV_CODEC_FLAG_BITEXACT))
3381 av_strlcpy(encoder_string, LIBAVCODEC_IDENT
" ", encoder_string_len);
3383 av_strlcpy(encoder_string,
"Lavc ", encoder_string_len);
3384 av_strlcat(encoder_string, ost->
enc->name, encoder_string_len);
3385 av_dict_set(&ost->
st->metadata,
"encoder", encoder_string,
3386 AV_DICT_DONT_STRDUP_VAL | AV_DICT_DONT_OVERWRITE);
3390 AVCodecContext *avctx)
3393 int n = 1, i,
size, index = 0;
3396 for (p = kf; *p; p++)
3400 pts = av_malloc_array(
size,
sizeof(*pts));
3402 av_log(NULL,
AV_LOG_FATAL,
"Could not allocate forced key frames array.\n");
3407 for (i = 0; i < n; i++) {
3408 char *next = strchr(p,
',');
3413 if (!memcmp(p,
"chapters", 8)) {
3418 if (avf->nb_chapters > INT_MAX -
size ||
3419 !(pts = av_realloc_f(pts,
size += avf->nb_chapters - 1,
3422 "Could not allocate forced key frames array.\n");
3426 t = av_rescale_q(t, AV_TIME_BASE_Q, avctx->time_base);
3428 for (j = 0; j < avf->nb_chapters; j++) {
3429 AVChapter *c = avf->chapters[j];
3430 av_assert1(index <
size);
3431 pts[index++] = av_rescale_q(c->start, c->time_base,
3432 avctx->time_base) + t;
3438 av_assert1(index <
size);
3439 pts[index++] = av_rescale_q(t, AV_TIME_BASE_Q, avctx->time_base);
3446 av_assert0(index ==
size);
3455 AVCodecContext *enc_ctx = ost->
enc_ctx;
3456 AVFormatContext *oc;
3465 enc_ctx->time_base = ist->
st->time_base;
3470 av_log(oc,
AV_LOG_WARNING,
"Input stream data not available, using default time base\n");
3473 enc_ctx->time_base = default_time_base;
3479 AVCodecContext *enc_ctx = ost->
enc_ctx;
3480 AVCodecContext *dec_ctx = NULL;
3489 av_dict_set(&ost->
st->metadata,
"rotate", NULL, 0);
3492 ost->
st->disposition = ist->
st->disposition;
3496 enc_ctx->chroma_sample_location = dec_ctx->chroma_sample_location;
3498 for (j = 0; j < oc->nb_streams; j++) {
3499 AVStream *st = oc->streams[j];
3500 if (st != ost->
st && st->codecpar->codec_type == ost->
st->codecpar->codec_type)
3503 if (j == oc->nb_streams)
3504 if (ost->
st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO ||
3505 ost->
st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
3506 ost->
st->disposition = AV_DISPOSITION_DEFAULT;
3509 if (enc_ctx->codec_type == AVMEDIA_TYPE_VIDEO) {
3520 "about the input framerate is available. Falling "
3521 "back to a default value of 25fps for output stream #%d:%d. Use the -r option "
3522 "if you want a different framerate.\n",
3526 if (ost->
enc->supported_framerates && !ost->
force_fps) {
3527 int idx = av_find_nearest_q_idx(ost->
frame_rate, ost->
enc->supported_framerates);
3531 if (enc_ctx->codec_id == AV_CODEC_ID_MPEG4) {
3537 switch (enc_ctx->codec_type) {
3538 case AVMEDIA_TYPE_AUDIO:
3539 enc_ctx->sample_fmt = av_buffersink_get_format(ost->
filter->
filter);
3541 enc_ctx->bits_per_raw_sample = FFMIN(dec_ctx->bits_per_raw_sample,
3542 av_get_bytes_per_sample(enc_ctx->sample_fmt) << 3);
3543 enc_ctx->sample_rate = av_buffersink_get_sample_rate(ost->
filter->
filter);
3544 enc_ctx->channel_layout = av_buffersink_get_channel_layout(ost->
filter->
filter);
3545 enc_ctx->channels = av_buffersink_get_channels(ost->
filter->
filter);
3550 case AVMEDIA_TYPE_VIDEO:
3553 if (!(enc_ctx->time_base.num && enc_ctx->time_base.den))
3554 enc_ctx->time_base = av_buffersink_get_time_base(ost->
filter->
filter);
3557 av_log(oc,
AV_LOG_WARNING,
"Frame rate very high for a muxer not efficiently supporting it.\n"
3558 "Please consider specifying a lower framerate, a different muxer or -vsync 2\n");
3563 enc_ctx->time_base);
3565 enc_ctx->width = av_buffersink_get_w(ost->
filter->
filter);
3566 enc_ctx->height = av_buffersink_get_h(ost->
filter->
filter);
3567 enc_ctx->sample_aspect_ratio = ost->
st->sample_aspect_ratio =
3570 av_buffersink_get_sample_aspect_ratio(ost->
filter->
filter);
3572 enc_ctx->pix_fmt = av_buffersink_get_format(ost->
filter->
filter);
3574 enc_ctx->bits_per_raw_sample = FFMIN(dec_ctx->bits_per_raw_sample,
3575 av_pix_fmt_desc_get(enc_ctx->pix_fmt)->comp[0].depth);
3582 enc_ctx->width != dec_ctx->width ||
3583 enc_ctx->height != dec_ctx->height ||
3584 enc_ctx->pix_fmt != dec_ctx->pix_fmt) {
3589 enc_ctx->field_order = AV_FIELD_BB;
3591 enc_ctx->field_order = AV_FIELD_TT;
3615 case AVMEDIA_TYPE_SUBTITLE:
3616 enc_ctx->time_base = AV_TIME_BASE_Q;
3617 if (!enc_ctx->width) {
3622 case AVMEDIA_TYPE_DATA:
3639 AVCodec *codec = ost->
enc;
3640 AVCodecContext *dec = NULL;
3649 if (dec && dec->subtitle_header) {
3651 ost->
enc_ctx->subtitle_header = av_mallocz(dec->subtitle_header_size + 1);
3652 if (!ost->
enc_ctx->subtitle_header)
3653 return AVERROR(ENOMEM);
3654 memcpy(ost->
enc_ctx->subtitle_header, dec->subtitle_header, dec->subtitle_header_size);
3655 ost->
enc_ctx->subtitle_header_size = dec->subtitle_header_size;
3657 if (!av_dict_get(ost->
encoder_opts,
"threads", NULL, 0))
3659 if (ost->
enc->type == AVMEDIA_TYPE_AUDIO &&
3666 ((AVHWFramesContext*)av_buffersink_get_hw_frames_ctx(ost->
filter->
filter)->data)->
format ==
3668 ost->
enc_ctx->hw_frames_ctx = av_buffer_ref(av_buffersink_get_hw_frames_ctx(ost->
filter->
filter));
3669 if (!ost->
enc_ctx->hw_frames_ctx)
3670 return AVERROR(ENOMEM);
3674 snprintf(error, error_len,
"Device setup failed for "
3675 "encoder on output stream #%d:%d : %s",
3680 if (ist && ist->
dec->type == AVMEDIA_TYPE_SUBTITLE && ost->
enc->type == AVMEDIA_TYPE_SUBTITLE) {
3681 int input_props = 0, output_props = 0;
3682 AVCodecDescriptor
const *input_descriptor =
3683 avcodec_descriptor_get(dec->codec_id);
3684 AVCodecDescriptor
const *output_descriptor =
3685 avcodec_descriptor_get(ost->
enc_ctx->codec_id);
3686 if (input_descriptor)
3687 input_props = input_descriptor->props & (AV_CODEC_PROP_TEXT_SUB | AV_CODEC_PROP_BITMAP_SUB);
3688 if (output_descriptor)
3689 output_props = output_descriptor->props & (AV_CODEC_PROP_TEXT_SUB | AV_CODEC_PROP_BITMAP_SUB);
3690 if (input_props && output_props && input_props != output_props) {
3691 snprintf(error, error_len,
3692 "Subtitle encoding currently only possible from text to text "
3693 "or bitmap to bitmap");
3694 return AVERROR_INVALIDDATA;
3699 if (ret == AVERROR_EXPERIMENTAL)
3701 snprintf(error, error_len,
3702 "Error while opening encoder for output stream #%d:%d - "
3703 "maybe incorrect parameters such as bit_rate, rate, width or height",
3707 if (ost->
enc->type == AVMEDIA_TYPE_AUDIO &&
3708 !(ost->
enc->capabilities & AV_CODEC_CAP_VARIABLE_FRAME_SIZE))
3713 ost->
enc_ctx->codec_id != AV_CODEC_ID_CODEC2 )
3714 av_log(NULL,
AV_LOG_WARNING,
"The bitrate parameter is set too low."
3715 " It takes bits/s as argument, not kbits/s\n");
3717 ret = avcodec_parameters_from_context(ost->
st->codecpar, ost->
enc_ctx);
3720 "Error initializing the output stream codec context.\n");
3726 ret = avcodec_copy_context(ost->
st->codec, ost->
enc_ctx);
3730 if (ost->
enc_ctx->nb_coded_side_data) {
3733 for (i = 0; i < ost->
enc_ctx->nb_coded_side_data; i++) {
3734 const AVPacketSideData *sd_src = &ost->
enc_ctx->coded_side_data[i];
3737 dst_data = av_stream_new_side_data(ost->
st, sd_src->type, sd_src->size);
3739 return AVERROR(ENOMEM);
3740 memcpy(dst_data, sd_src->data, sd_src->size);
3753 for (i = 0; i < ist->
st->nb_side_data; i++) {
3754 AVPacketSideData *sd = &ist->
st->side_data[i];
3755 uint8_t *dst = av_stream_new_side_data(ost->
st, sd->type, sd->size);
3757 return AVERROR(ENOMEM);
3758 memcpy(dst, sd->data, sd->size);
3759 if (ist->
autorotate && sd->type == AV_PKT_DATA_DISPLAYMATRIX)
3760 av_display_rotation_set((uint32_t *)dst, 0);
3765 if (ost->
st->time_base.num <= 0 || ost->
st->time_base.den <= 0)
3766 ost->
st->time_base = av_add_q(ost->
enc_ctx->time_base, (AVRational){0, 1});
3769 if (ost->
st->duration <= 0 && ist && ist->
st->duration > 0)
3770 ost->
st->duration = av_rescale_q(ist->
st->duration, ist->
st->time_base, ost->
st->time_base);
3772 ost->
st->codec->codec= ost->
enc_ctx->codec;
3781 static const AVOption opts[] = {
3782 {
"disposition" , NULL, 0, AV_OPT_TYPE_FLAGS, { .i64 = 0 }, INT64_MIN, INT64_MAX, .unit =
"flags" },
3783 {
"default" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_DEFAULT }, .unit =
"flags" },
3784 {
"dub" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_DUB }, .unit =
"flags" },
3785 {
"original" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_ORIGINAL }, .unit =
"flags" },
3786 {
"comment" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_COMMENT }, .unit =
"flags" },
3787 {
"lyrics" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_LYRICS }, .unit =
"flags" },
3788 {
"karaoke" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_KARAOKE }, .unit =
"flags" },
3789 {
"forced" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_FORCED }, .unit =
"flags" },
3790 {
"hearing_impaired" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_HEARING_IMPAIRED }, .unit =
"flags" },
3791 {
"visual_impaired" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_VISUAL_IMPAIRED }, .unit =
"flags" },
3792 {
"clean_effects" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_CLEAN_EFFECTS }, .unit =
"flags" },
3793 {
"attached_pic" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_ATTACHED_PIC }, .unit =
"flags" },
3794 {
"captions" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_CAPTIONS }, .unit =
"flags" },
3795 {
"descriptions" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_DESCRIPTIONS }, .unit =
"flags" },
3796 {
"dependent" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_DEPENDENT }, .unit =
"flags" },
3797 {
"metadata" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_METADATA }, .unit =
"flags" },
3800 static const AVClass
class = {
3802 .item_name = av_default_item_name,
3804 .version = LIBAVUTIL_VERSION_INT,
3806 const AVClass *pclass = &
class;
3808 ret = av_opt_eval_flags(&pclass, &opts[0], ost->
disposition, &ost->
st->disposition);
3832 AVStream *st = file->
ctx->streams[pkt->stream_index];
3837 "New %s stream %d:%d at pos:%"PRId64
" and DTS:%ss\n",
3838 av_get_media_type_string(st->codecpar->codec_type),
3839 input_index, pkt->stream_index,
3840 pkt->pos, av_ts2timestr(pkt->dts, &st->time_base));
3846 int ret = 0, i, j, k;
3847 AVFormatContext *oc;
3850 char error[1024] = {0};
3899 for (j = 0; j < ifile->
ctx->nb_programs; j++) {
3900 AVProgram *p = ifile->
ctx->programs[j];
3901 int discard = AVDISCARD_ALL;
3903 for (k = 0; k < p->nb_stream_indexes; k++)
3905 discard = AVDISCARD_DEFAULT;
3908 p->discard = discard;
3915 if (oc->oformat->flags & AVFMT_NOSTREAMS && oc->nb_streams == 0) {
3930 av_log(NULL,
AV_LOG_INFO,
" Stream #%d:%d (%s) -> %s",
3945 av_log(NULL,
AV_LOG_INFO,
" File %s -> Stream #%d:%d\n",
3961 av_log(NULL,
AV_LOG_INFO,
" Stream #%d:%d -> #%d:%d",
3974 const AVCodec *out_codec = ost->
enc;
3975 const char *decoder_name =
"?";
3976 const char *in_codec_name =
"?";
3977 const char *encoder_name =
"?";
3978 const char *out_codec_name =
"?";
3979 const AVCodecDescriptor *desc;
3982 decoder_name = in_codec->name;
3983 desc = avcodec_descriptor_get(in_codec->id);
3985 in_codec_name = desc->name;
3986 if (!strcmp(decoder_name, in_codec_name))
3987 decoder_name =
"native";
3991 encoder_name = out_codec->name;
3992 desc = avcodec_descriptor_get(out_codec->id);
3994 out_codec_name = desc->name;
3995 if (!strcmp(encoder_name, out_codec_name))
3996 encoder_name =
"native";
3999 av_log(NULL,
AV_LOG_INFO,
" (%s (%s) -> %s (%s))",
4000 in_codec_name, decoder_name,
4001 out_codec_name, encoder_name);
4031 for (j = 0; j < of->
ctx->nb_streams; j++)
4050 int64_t opts_min = INT64_MAX;
4055 int64_t opts = ost->
st->cur_dts == AV_NOPTS_VALUE ? INT64_MIN :
4056 av_rescale_q(ost->
st->cur_dts, ost->
st->time_base,
4058 if (ost->
st->cur_dts == AV_NOPTS_VALUE)
4060 "cur_dts is invalid st:%d (%d) [init:%d i_done:%d finish:%d] (this is harmless if it occurs once at the start per stream)\n",
4066 if (!ost->
finished && opts < opts_min) {
4078 if (tcgetattr(0, &tty) == 0) {
4079 if (on) tty.c_lflag |= ECHO;
4080 else tty.c_lflag &= ~ECHO;
4081 tcsetattr(0, TCSANOW, &tty);
4089 static int64_t last_time;
4091 return AVERROR_EXIT;
4095 last_time = cur_time;
4099 return AVERROR_EXIT;
4100 if (key ==
'+') av_log_set_level(av_log_get_level()+10);
4101 if (key ==
'-') av_log_set_level(av_log_get_level()-10);
4112 if (key ==
'c' || key ==
'C'){
4113 char buf[4096], target[64],
command[256], arg[256] = {0};
4116 fprintf(stderr,
"\nEnter command: <target>|all <time>|-1 <command>[ <argument>]\n");
4119 while ((k =
read_key()) !=
'\n' && k !=
'\r' && i <
sizeof(buf)-1)
4124 fprintf(stderr,
"\n");
4126 (n = sscanf(buf,
"%63[^ ] %lf %255[^ ] %255[^\n]", target, &
time,
command, arg)) >= 3) {
4127 av_log(NULL,
AV_LOG_DEBUG,
"Processing command target:%s time:%f command:%s arg:%s",
4133 ret = avfilter_graph_send_command(fg->
graph, target,
command, arg, buf,
sizeof(buf),
4134 key ==
'c' ? AVFILTER_CMD_FLAG_ONE : 0);
4135 fprintf(stderr,
"Command reply for stream %d: ret:%d res:\n%s", i, ret, buf);
4136 }
else if (key ==
'c') {
4137 fprintf(stderr,
"Queuing commands only on filters supporting the specific command is unsupported\n");
4138 ret = AVERROR_PATCHWELCOME;
4140 ret = avfilter_graph_queue_command(fg->
graph, target,
command, arg, 0,
time);
4142 fprintf(stderr,
"Queuing command failed with error %s\n", av_err2str(ret));
4148 "Parse error, at least 3 arguments were expected, "
4149 "only %d given in string '%s'\n", n, buf);
4152 if (key ==
'd' || key ==
'D'){
4156 if(!debug) debug = 1;
4157 while(debug & (FF_DEBUG_DCT_COEFF
4159 |FF_DEBUG_VIS_QP|FF_DEBUG_VIS_MB_TYPE
4168 while ((k =
read_key()) !=
'\n' && k !=
'\r' && i <
sizeof(buf)-1)
4173 fprintf(stderr,
"\n");
4174 if (k <= 0 || sscanf(buf,
"%d", &debug)!=1)
4175 fprintf(stderr,
"error parsing debug value\n");
4185 fprintf(stderr,
"debug=%d\n", debug);
4188 fprintf(stderr,
"key function\n"
4189 "? show this help\n"
4190 "+ increase verbosity\n"
4191 "- decrease verbosity\n"
4192 "c Send command to first matching filter supporting it\n"
4193 "C Send/Queue command to all matching filters\n"
4194 "D cycle through available debug modes\n"
4195 "h dump packets/hex press to cycle through the 3 states\n"
4197 "s Show QP histogram\n"
4204 static void *input_thread(
void *arg)
4207 unsigned flags = f->non_blocking ? AV_THREAD_MESSAGE_NONBLOCK : 0;
4212 ret = av_read_frame(f->
ctx, &pkt);
4214 if (ret == AVERROR(EAGAIN)) {
4219 av_thread_message_queue_set_err_recv(f->in_thread_queue, ret);
4222 ret = av_thread_message_queue_send(f->in_thread_queue, &pkt, flags);
4223 if (flags && ret == AVERROR(EAGAIN)) {
4225 ret = av_thread_message_queue_send(f->in_thread_queue, &pkt, flags);
4227 "Thread message queue blocking; consider raising the "
4228 "thread_queue_size option (current value: %d)\n",
4229 f->thread_queue_size);
4232 if (ret != AVERROR_EOF)
4234 "Unable to send packet to main thread: %s\n",
4236 av_packet_unref(&pkt);
4237 av_thread_message_queue_set_err_recv(f->in_thread_queue, ret);
4245 static void free_input_thread(
int i)
4250 if (!f || !f->in_thread_queue)
4252 av_thread_message_queue_set_err_send(f->in_thread_queue, AVERROR_EOF);
4253 while (av_thread_message_queue_recv(f->in_thread_queue, &pkt, 0) >= 0)
4254 av_packet_unref(&pkt);
4256 pthread_join(f->thread, NULL);
4258 av_thread_message_queue_free(&f->in_thread_queue);
4261 static void free_input_threads(
void)
4266 free_input_thread(i);
4269 static int init_input_thread(
int i)
4277 if (f->
ctx->pb ? !f->
ctx->pb->seekable :
4278 strcmp(f->
ctx->iformat->name,
"lavfi"))
4279 f->non_blocking = 1;
4280 ret = av_thread_message_queue_alloc(&f->in_thread_queue,
4281 f->thread_queue_size,
sizeof(AVPacket));
4285 if ((ret = pthread_create(&f->thread, NULL, input_thread, f))) {
4286 av_log(NULL,
AV_LOG_ERROR,
"pthread_create failed: %s. Try to increase `ulimit -v` or decrease `ulimit -s`.\n", strerror(ret));
4287 av_thread_message_queue_free(&f->in_thread_queue);
4288 return AVERROR(ret);
4294 static int init_input_threads(
void)
4299 ret = init_input_thread(i);
4306 static int get_input_packet_mt(
InputFile *f, AVPacket *pkt)
4308 return av_thread_message_queue_recv(f->in_thread_queue, pkt,
4310 AV_THREAD_MESSAGE_NONBLOCK : 0);
4320 int64_t pts = av_rescale(ist->
dts, 1000000, AV_TIME_BASE);
4321 int64_t now = av_gettime_relative() - ist->
start;
4323 return AVERROR(EAGAIN);
4329 return get_input_packet_mt(f, pkt);
4331 return av_read_frame(f->
ctx, pkt);
4353 static AVRational
duration_max(int64_t tmp, int64_t *duration, AVRational tmp_time_base,
4354 AVRational time_base)
4360 return tmp_time_base;
4363 ret = av_compare_ts(*duration, time_base, tmp, tmp_time_base);
4366 return tmp_time_base;
4375 AVCodecContext *avctx;
4376 int i, ret, has_audio = 0;
4377 int64_t duration = 0;
4379 ret = avformat_seek_file(is, -1, INT64_MIN, is->start_time, is->start_time, 0);
4390 if (avctx->codec_type == AVMEDIA_TYPE_AUDIO && ist->
nb_samples)
4399 if (avctx->codec_type == AVMEDIA_TYPE_AUDIO && ist->
nb_samples) {
4408 duration = av_rescale_q(1, av_inv_q(ist->
framerate), ist->
st->time_base);
4409 }
else if (ist->
st->avg_frame_rate.num) {
4410 duration = av_rescale_q(1, av_inv_q(ist->
st->avg_frame_rate), ist->
st->time_base);
4424 if (ifile->
loop > 0)
4440 AVFormatContext *is;
4443 int ret, thread_ret, i, j;
4450 if (ret == AVERROR(EAGAIN)) {
4454 if (ret < 0 && ifile->loop) {
4455 AVCodecContext *avctx;
4463 avcodec_flush_buffers(avctx);
4467 free_input_thread(file_index);
4471 thread_ret = init_input_thread(file_index);
4479 if (ret == AVERROR(EAGAIN)) {
4485 if (ret != AVERROR_EOF) {
4510 return AVERROR(EAGAIN);
4517 is->streams[pkt.stream_index]);
4523 goto discard_packet;
4532 goto discard_packet;
4534 if (pkt.flags & AV_PKT_FLAG_CORRUPT) {
4536 "%s: corrupt input packet in stream %d\n", is->url, pkt.stream_index);
4542 av_log(NULL,
AV_LOG_INFO,
"demuxer -> ist_index:%d type:%s "
4543 "next_dts:%s next_dts_time:%s next_pts:%s next_pts_time:%s pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s off:%s off_time:%s\n",
4544 ifile->
ist_index + pkt.stream_index, av_get_media_type_string(ist->
dec_ctx->codec_type),
4547 av_ts2str(pkt.pts), av_ts2timestr(pkt.pts, &ist->
st->time_base),
4548 av_ts2str(pkt.dts), av_ts2timestr(pkt.dts, &ist->
st->time_base),
4554 int64_t stime, stime2;
4558 if ( ist->
next_dts == AV_NOPTS_VALUE
4560 && (is->iformat->flags & AVFMT_TS_DISCONT)) {
4561 int64_t new_start_time = INT64_MAX;
4562 for (i=0; i<is->nb_streams; i++) {
4563 AVStream *st = is->streams[i];
4564 if(st->discard == AVDISCARD_ALL || st->start_time == AV_NOPTS_VALUE)
4566 new_start_time = FFMIN(new_start_time, av_rescale_q(st->start_time, st->time_base, AV_TIME_BASE_Q));
4568 if (new_start_time > is->start_time) {
4569 av_log(is,
AV_LOG_VERBOSE,
"Correcting start time by %"PRId64
"\n", new_start_time - is->start_time);
4574 stime = av_rescale_q(is->start_time, AV_TIME_BASE_Q, ist->
st->time_base);
4575 stime2= stime + (1ULL<<ist->
st->pts_wrap_bits);
4578 if(stime2 > stime && pkt.dts != AV_NOPTS_VALUE && pkt.dts > stime + (1LL<<(ist->
st->pts_wrap_bits-1))) {
4579 pkt.dts -= 1ULL<<ist->
st->pts_wrap_bits;
4582 if(stime2 > stime && pkt.pts != AV_NOPTS_VALUE && pkt.pts > stime + (1LL<<(ist->
st->pts_wrap_bits-1))) {
4583 pkt.pts -= 1ULL<<ist->
st->pts_wrap_bits;
4590 for (i = 0; i < ist->
st->nb_side_data; i++) {
4591 AVPacketSideData *src_sd = &ist->
st->side_data[i];
4594 if (src_sd->type == AV_PKT_DATA_DISPLAYMATRIX)
4597 if (av_packet_get_side_data(&pkt, src_sd->type, NULL))
4600 dst_data = av_packet_new_side_data(&pkt, src_sd->type, src_sd->size);
4604 memcpy(dst_data, src_sd->data, src_sd->size);
4608 if (pkt.dts != AV_NOPTS_VALUE)
4609 pkt.dts += av_rescale_q(ifile->
ts_offset, AV_TIME_BASE_Q, ist->
st->time_base);
4610 if (pkt.pts != AV_NOPTS_VALUE)
4611 pkt.pts += av_rescale_q(ifile->
ts_offset, AV_TIME_BASE_Q, ist->
st->time_base);
4613 if (pkt.pts != AV_NOPTS_VALUE)
4615 if (pkt.dts != AV_NOPTS_VALUE)
4618 pkt_dts = av_rescale_q_rnd(pkt.dts, ist->
st->time_base, AV_TIME_BASE_Q, AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX);
4619 if ((ist->
dec_ctx->codec_type == AVMEDIA_TYPE_VIDEO ||
4620 ist->
dec_ctx->codec_type == AVMEDIA_TYPE_AUDIO) &&
4621 pkt_dts != AV_NOPTS_VALUE && ist->
next_dts == AV_NOPTS_VALUE && !
copy_ts
4622 && (is->iformat->flags & AVFMT_TS_DISCONT) && ifile->
last_ts != AV_NOPTS_VALUE) {
4623 int64_t delta = pkt_dts - ifile->
last_ts;
4628 "Inter stream timestamp discontinuity %"PRId64
", new offset= %"PRId64
"\n",
4630 pkt.dts -= av_rescale_q(delta, AV_TIME_BASE_Q, ist->
st->time_base);
4631 if (pkt.pts != AV_NOPTS_VALUE)
4632 pkt.pts -= av_rescale_q(delta, AV_TIME_BASE_Q, ist->
st->time_base);
4637 if (pkt.pts != AV_NOPTS_VALUE) {
4638 pkt.pts += duration;
4643 if (pkt.dts != AV_NOPTS_VALUE)
4644 pkt.dts += duration;
4646 pkt_dts = av_rescale_q_rnd(pkt.dts, ist->
st->time_base, AV_TIME_BASE_Q, AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX);
4647 if ((ist->
dec_ctx->codec_type == AVMEDIA_TYPE_VIDEO ||
4648 ist->
dec_ctx->codec_type == AVMEDIA_TYPE_AUDIO) &&
4649 pkt_dts != AV_NOPTS_VALUE && ist->
next_dts != AV_NOPTS_VALUE &&
4651 int64_t delta = pkt_dts - ist->
next_dts;
4652 if (is->iformat->flags & AVFMT_TS_DISCONT) {
4655 pkt_dts + AV_TIME_BASE/10 < FFMAX(ist->
pts, ist->
dts)) {
4658 "timestamp discontinuity for stream #%d:%d "
4659 "(id=%d, type=%s): %"PRId64
", new offset= %"PRId64
"\n",
4661 av_get_media_type_string(ist->
dec_ctx->codec_type),
4663 pkt.dts -= av_rescale_q(delta, AV_TIME_BASE_Q, ist->
st->time_base);
4664 if (pkt.pts != AV_NOPTS_VALUE)
4665 pkt.pts -= av_rescale_q(delta, AV_TIME_BASE_Q, ist->
st->time_base);
4670 av_log(NULL,
AV_LOG_WARNING,
"DTS %"PRId64
", next:%"PRId64
" st:%d invalid dropping\n", pkt.dts, ist->
next_dts, pkt.stream_index);
4671 pkt.dts = AV_NOPTS_VALUE;
4673 if (pkt.pts != AV_NOPTS_VALUE){
4674 int64_t pkt_pts = av_rescale_q(pkt.pts, ist->
st->time_base, AV_TIME_BASE_Q);
4678 av_log(NULL,
AV_LOG_WARNING,
"PTS %"PRId64
", next:%"PRId64
" invalid dropping st:%d\n", pkt.pts, ist->
next_dts, pkt.stream_index);
4679 pkt.pts = AV_NOPTS_VALUE;
4685 if (pkt.dts != AV_NOPTS_VALUE)
4686 ifile->
last_ts = av_rescale_q(pkt.dts, ist->
st->time_base, AV_TIME_BASE_Q);
4689 av_log(NULL,
AV_LOG_INFO,
"demuxer+ffmpeg -> ist_index:%d type:%s pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s off:%s off_time:%s\n",
4690 ifile->
ist_index + pkt.stream_index, av_get_media_type_string(ist->
dec_ctx->codec_type),
4691 av_ts2str(pkt.pts), av_ts2timestr(pkt.pts, &ist->
st->time_base),
4692 av_ts2str(pkt.dts), av_ts2timestr(pkt.dts, &ist->
st->time_base),
4702 av_packet_unref(&pkt);
4717 int nb_requests, nb_requests_max = 0;
4722 ret = avfilter_graph_request_oldest(graph->
graph);
4726 if (ret == AVERROR_EOF) {
4732 if (ret != AVERROR(EAGAIN))
4735 for (i = 0; i < graph->
nb_inputs; i++) {
4736 ifilter = graph->
inputs[i];
4741 nb_requests = av_buffersrc_get_nb_failed_requests(ifilter->
filter);
4742 if (nb_requests > nb_requests_max) {
4743 nb_requests_max = nb_requests;
4773 av_log(NULL,
AV_LOG_VERBOSE,
"No more inputs to read from, finishing.\n");
4781 av_log(NULL,
AV_LOG_ERROR,
"Error reinitializing filters!\n");
4789 char error[1024] = {0};
4792 av_log(NULL,
AV_LOG_ERROR,
"Error initializing output stream %d:%d -- %s\n",
4801 }
else if (ost->
filter) {
4820 if (ret == AVERROR(EAGAIN)) {
4827 return ret == AVERROR_EOF ? 0 : ret;
4838 AVFormatContext *os;
4841 int64_t timer_start;
4842 int64_t total_packets_written = 0;
4849 av_log(NULL,
AV_LOG_INFO,
"Press [q] to stop, [?] for help\n");
4852 timer_start = av_gettime_relative();
4855 if ((ret = init_input_threads()) < 0)
4860 int64_t cur_time= av_gettime_relative();
4869 av_log(NULL,
AV_LOG_VERBOSE,
"No more output streams to write to, finishing.\n");
4874 if (ret < 0 && ret != AVERROR_EOF) {
4875 av_log(NULL,
AV_LOG_ERROR,
"Error while filtering: %s\n", av_err2str(ret));
4883 free_input_threads();
4902 "Nothing was written into output file %d (%s), because "
4903 "at least one of its streams received no packets.\n",
4907 if ((ret = av_write_trailer(os)) < 0) {
4908 av_log(NULL,
AV_LOG_ERROR,
"Error writing trailer of %s: %s\n", os->url, av_err2str(ret));
4921 av_freep(&ost->
enc_ctx->stats_in);
4949 free_input_threads();
4959 "Error closing logfile, loss of information possible: %s\n",
4960 av_err2str(AVERROR(errno)));
4964 av_freep(&ost->
apad);
4980 struct rusage rusage;
4982 getrusage(RUSAGE_SELF, &rusage);
4984 (rusage.ru_utime.tv_sec * 1000000LL) + rusage.ru_utime.tv_usec;
4986 (rusage.ru_stime.tv_sec * 1000000LL) + rusage.ru_stime.tv_usec;
4987 #elif HAVE_GETPROCESSTIMES
4989 FILETIME c, e, k, u;
4990 proc = GetCurrentProcess();
4991 GetProcessTimes(proc, &c, &e, &k, &u);
4993 ((int64_t)u.dwHighDateTime << 32 | u.dwLowDateTime) / 10;
4995 ((int64_t)k.dwHighDateTime << 32 | k.dwLowDateTime) / 10;
5004 #if HAVE_GETRUSAGE && HAVE_STRUCT_RUSAGE_RU_MAXRSS
5005 struct rusage rusage;
5006 getrusage(RUSAGE_SELF, &rusage);
5007 return (int64_t)rusage.ru_maxrss * 1024;
5008 #elif HAVE_GETPROCESSMEMORYINFO
5010 PROCESS_MEMORY_COUNTERS memcounters;
5011 proc = GetCurrentProcess();
5012 memcounters.cb =
sizeof(memcounters);
5013 GetProcessMemoryInfo(proc, &memcounters,
sizeof(memcounters));
5014 return memcounters.PeakPagefileUsage;
5072 char _program_name[] =
"ffmpeg";
5076 #define OFFSET(x) offsetof(OptionsContext, x)
5094 {
"bsfs",
OPT_EXIT, { .func_arg =
show_bsfs },
"show available bit stream filters" },
5103 {
"report", 0, { .func_arg =
opt_report },
"generate a report" },
5104 {
"max_alloc",
HAS_ARG, { .func_arg =
opt_max_alloc },
"set maximum size of a single allocated block",
"bytes" },
5110 "list sources of the input device",
"device" },
5112 "list sinks of the output device",
"device" },
5117 "force format",
"fmt" },
5119 "overwrite output files" },
5121 "never overwrite output files" },
5123 "Ignore unknown stream types" },
5125 "Copy unknown stream types" },
5128 "codec name",
"codec" },
5131 "codec name",
"codec" },
5134 "preset name",
"preset" },
5137 "set input stream mapping",
5138 "[-]input_file_id[:stream_specifier][,sync_file_id[:stream_specifier]]" },
5140 "map an audio channel from one stream to another",
"file.stream.channel[:syncfile.syncstream]" },
5143 "set metadata information of outfile from infile",
5144 "outfile[,metadata]:infile[,metadata]" },
5147 "set chapters mapping",
"input_file_index" },
5150 "record or transcode \"duration\" seconds of audio/video",
5153 "record or transcode stop time",
"time_stop" },
5155 "set the limit file size in bytes",
"limit_size" },
5158 "set the start time offset",
"time_off" },
5161 "set the start time offset relative to EOF",
"time_off" },
5164 "enable/disable seeking by timestamp with -ss" },
5167 "enable/disable accurate seeking with -ss" },
5170 "set the input ts offset",
"time_off" },
5173 "set the input ts scale",
"scale" },
5175 "set the recording timestamp ('now' to set the current time)",
"time" },
5177 "add metadata",
"string=string" },
5179 "add program with specified streams",
"title=string:st=number..." },
5182 "set the number of data frames to output",
"number" },
5184 "add timings for benchmarking" },
5186 "add timings for each task" },
5188 "write program-readable progress information",
"url" },
5190 "enable or disable interaction on standard input" },
5192 "set max runtime in seconds in CPU user time",
"limit" },
5194 "dump each input packet" },
5196 "when dumping packets, also dump the payload" },
5199 "read input at native frame rate",
"" },
5201 "specify target file type (\"vcd\", \"svcd\", \"dvd\", \"dv\" or \"dv50\" "
5202 "with optional prefixes \"pal-\", \"ntsc-\" or \"film-\")",
"type" },
5204 "video sync method",
"" },
5206 "frame drop threshold",
"" },
5208 "audio sync method",
"" },
5210 "audio drift threshold",
"threshold" },
5212 "copy timestamps" },
5214 "shift input timestamps to start at 0 when using copyts" },
5216 "copy input stream time base when stream copying",
"mode" },
5219 "finish encoding within shortest input" },
5227 "timestamp discontinuity delta threshold",
"threshold" },
5229 "timestamp error delta threshold",
"threshold" },
5231 "exit on error",
"error" },
5233 "abort on the specified condition flags",
"flags" },
5236 "copy initial non-keyframes" },
5238 "copy or discard frames before start time" },
5240 "set the number of frames to output",
"number" },
5243 "force codec tag/fourcc",
"fourcc/tag" },
5246 "use fixed quality scale (VBR)",
"q" },
5249 "use fixed quality scale (VBR)",
"q" },
5251 "set profile",
"profile" },
5253 "set stream filtergraph",
"filter_graph" },
5255 "number of non-complex filter threads" },
5257 "read stream filtergraph description from a file",
"filename" },
5259 "reinit filtergraph on input parameter changes",
"" },
5261 "create a complex filtergraph",
"graph_description" },
5263 "number of threads for -filter_complex" },
5265 "create a complex filtergraph",
"graph_description" },
5267 "read complex filtergraph description from a file",
"filename" },
5269 "print progress report during encoding", },
5272 "add an attachment to the output file",
"filename" },
5275 "extract an attachment into a file",
"filename" },
5277 OPT_OFFSET, { .off =
OFFSET(loop) },
"set number of times input stream shall be looped",
"loop count" },
5279 "print timestamp debugging info" },
5281 "ratio of errors (0.0: no errors, 1.0: 100% errors) above which ffmpeg returns an error instead of success.",
"maximum error rate" },
5287 "disposition",
"" },
5289 { .off =
OFFSET(thread_queue_size) },
5290 "set the maximum number of queued packets from the demuxer" },
5292 "read and decode the streams to fill missing information with heuristics" },
5296 "set the number of video frames to output",
"number" },
5299 "set frame rate (Hz value, fraction or abbreviation)",
"rate" },
5302 "set frame size (WxH or abbreviation)",
"size" },
5305 "set aspect ratio (4:3, 16:9 or 1.3333, 1.7777)",
"aspect" },
5308 "set pixel format",
"format" },
5310 "set the number of bits per raw sample",
"number" },
5312 "deprecated use -g 1" },
5317 "rate control override for specific intervals",
"override" },
5320 "force video codec ('copy' to copy stream)",
"codec" },
5326 "set initial TimeCode value.",
"hh:mm:ss[:;.]ff" },
5328 "select the pass number (1 to 3)",
"n" },
5331 "select two pass log file name prefix",
"prefix" },
5333 "this option is deprecated, use the yadif filter instead" },
5335 "calculate PSNR of compressed frames" },
5337 "dump video coding statistics to file" },
5339 "dump video coding statistics to file",
"file" },
5341 "Version of the vstats format to use."},
5343 "set video filters",
"filter_graph" },
5346 "specify intra matrix coeffs",
"matrix" },
5349 "specify inter matrix coeffs",
"matrix" },
5352 "specify intra matrix coeffs",
"matrix" },
5355 "top=1/bottom=0/auto=-1 field first",
"" },
5358 "force video tag/fourcc",
"fourcc/tag" },
5360 "show QP histogram" },
5363 "force the selected framerate, disable the best supported framerate selection" },
5366 "set the value of an outfile streamid",
"streamIndex:value" },
5369 "force key frames at specified timestamps",
"timestamps" },
5371 "audio bitrate (please use -b:a)",
"bitrate" },
5373 "video bitrate (please use -b:v)",
"bitrate" },
5376 "use HW accelerated decoding",
"hwaccel name" },
5379 "select a device for HW acceleration",
"devicename" },
5382 "select output format used with HW accelerated decoding",
"format" },
5383 #if CONFIG_VIDEOTOOLBOX
5387 "show available HW acceleration methods" },
5390 "automatically insert correct rotate filters" },
5394 "set the number of audio frames to output",
"number" },
5396 "set audio quality (codec-specific)",
"quality", },
5399 "set audio sampling rate (in Hz)",
"rate" },
5402 "set number of audio channels",
"channels" },
5407 "force audio codec ('copy' to copy stream)",
"codec" },
5410 "force audio tag/fourcc",
"fourcc/tag" },
5412 "change audio volume (256=normal)" ,
"volume" },
5415 "set sample format",
"format" },
5418 "set channel layout",
"layout" },
5420 "set audio filters",
"filter_graph" },
5422 "set the maximum number of channels to try to guess the channel layout" },
5426 "disable subtitle" },
5428 "force subtitle codec ('copy' to copy stream)",
"codec" },
5430 ,
"force subtitle tag/fourcc",
"fourcc/tag" },
5432 "fix subtitles duration" },
5434 "set canvas size (WxH or abbreviation)",
"size" },
5438 "deprecated, use -channel",
"channel" },
5440 "deprecated, use -standard",
"standard" },
5445 "set the maximum demux-decode delay",
"seconds" },
5447 "set the initial demux-decode delay",
"seconds" },
5449 "specify a file in which to print sdp information",
"file" },
5452 "set the desired time base hint for output stream (1:24, 1:48000 or 0.04166, 2.0833e-5)",
"ratio" },
5454 "set the desired time base for the encoder (1:24, 1:48000 or 0.04166, 2.0833e-5). "
5455 "two special values are defined - "
5456 "0 = use frame rate (video) or sample rate (audio),"
5457 "-1 = match source time base",
"ratio" },
5460 "A comma-separated list of bitstream filters",
"bitstream_filters" },
5462 "deprecated",
"audio bitstream_filters" },
5464 "deprecated",
"video bitstream_filters" },
5467 "set the audio options to the indicated preset",
"preset" },
5469 "set the video options to the indicated preset",
"preset" },
5471 "set the subtitle options to the indicated preset",
"preset" },
5473 "set options from indicated preset file",
"filename" },
5476 "maximum number of packets that can be buffered while waiting for all streams to initialize",
"packets" },
5480 "force data codec ('copy' to copy stream)",
"codec" },
5486 "set VAAPI hardware device (DRM path or X11 display name)",
"device" },
5491 "set QSV hardware device (DirectX adapter index, DRM path or X11 display name)",
"device"},
5495 "initialise hardware device",
"args" },
5497 "set hardware device used when filtering",
"device" },
5508 if (savedCode == 0) {
5516 setvbuf(stderr,NULL,_IONBF,0);
5518 av_log_set_flags(AV_LOG_SKIP_REPEATED);
5521 if(argc>1 && !strcmp(argv[1],
"-d")){
5529 avdevice_register_all();
5531 avformat_network_init();
5548 av_log(NULL,
AV_LOG_FATAL,
"At least one output file must be specified\n");
5553 if (strcmp(
output_files[i]->ctx->oformat->name,
"rtp"))
5561 int64_t utime, stime, rtime;
5567 "bench: utime=%0.3fs stime=%0.3fs rtime=%0.3fs\n",
5568 utime / 1000000.0, stime / 1000000.0, rtime / 1000000.0);
5570 av_log(NULL,
AV_LOG_DEBUG,
"%"PRIu64
" frames successfully decoded, %"PRIu64
" decoding errors\n",