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"
200 extern int opt_map(
void *optctx,
const char *opt,
const char *arg);
201 extern int opt_map_channel(
void *optctx,
const char *opt,
const char *arg);
203 extern int opt_data_frames(
void *optctx,
const char *opt,
const char *arg);
204 extern int opt_progress(
void *optctx,
const char *opt,
const char *arg);
205 extern int opt_target(
void *optctx,
const char *opt,
const char *arg);
206 extern int opt_vsync(
void *optctx,
const char *opt,
const char *arg);
207 extern int opt_abort_on(
void *optctx,
const char *opt,
const char *arg);
208 extern int opt_stats_period(
void *optctx,
const char *opt,
const char *arg);
209 extern int opt_qscale(
void *optctx,
const char *opt,
const char *arg);
210 extern int opt_profile(
void *optctx,
const char *opt,
const char *arg);
213 extern int opt_attach(
void *optctx,
const char *opt,
const char *arg);
214 extern int opt_video_frames(
void *optctx,
const char *opt,
const char *arg);
216 extern int opt_video_codec(
void *optctx,
const char *opt,
const char *arg);
217 extern int opt_sameq(
void *optctx,
const char *opt,
const char *arg);
218 extern int opt_timecode(
void *optctx,
const char *opt,
const char *arg);
220 extern int opt_vstats_file(
void *optctx,
const char *opt,
const char *arg);
221 extern int opt_vstats(
void *optctx,
const char *opt,
const char *arg);
222 extern int opt_video_frames(
void *optctx,
const char *opt,
const char *arg);
223 extern int opt_old2new(
void *optctx,
const char *opt,
const char *arg);
224 extern int opt_streamid(
void *optctx,
const char *opt,
const char *arg);
225 extern int opt_bitrate(
void *optctx,
const char *opt,
const char *arg);
226 extern int show_hwaccels(
void *optctx,
const char *opt,
const char *arg);
228 extern int opt_audio_frames(
void *optctx,
const char *opt,
const char *arg);
229 extern int opt_audio_qscale(
void *optctx,
const char *opt,
const char *arg);
230 extern int opt_audio_codec(
void *optctx,
const char *opt,
const char *arg);
232 extern int opt_preset(
void *optctx,
const char *opt,
const char *arg);
237 extern int opt_sdp_file(
void *optctx,
const char *opt,
const char *arg);
238 extern int opt_data_codec(
void *optctx,
const char *opt,
const char *arg);
246 __thread
struct termios oldtty;
247 __thread
int restore_tty;
251 static void free_input_threads(
void);
274 av_frame_unref(frame);
278 if ((ret = av_frame_get_buffer(frame, 0)) < 0)
280 memset(frame->data[0], 0, frame->height * frame->linesize[0]);
287 uint32_t *pal, *dst2;
291 if (r->type != SUBTITLE_BITMAP) {
292 av_log(NULL, AV_LOG_WARNING,
"sub2video: non-bitmap subtitle\n");
295 if (r->x < 0 || r->x + r->w > w || r->y < 0 || r->y + r->h > h) {
296 av_log(NULL, AV_LOG_WARNING,
"sub2video: rectangle (%d %d %d %d) overflowing %d %d\n",
297 r->x, r->y, r->w, r->h, w, h
302 dst += r->y * dst_linesize + r->x * 4;
304 pal = (uint32_t *)r->data[1];
305 for (y = 0; y < r->h; y++) {
306 dst2 = (uint32_t *)dst;
308 for (x = 0; x < r->w; x++)
309 *(dst2++) = pal[*(src2++)];
311 src += r->linesize[0];
321 av_assert1(frame->data[0]);
324 ret = av_buffersrc_add_frame_flags(ist->
filters[i]->
filter, frame,
325 AV_BUFFERSRC_FLAG_KEEP_REF |
326 AV_BUFFERSRC_FLAG_PUSH);
327 if (ret != AVERROR_EOF && ret < 0)
328 av_log(NULL, AV_LOG_WARNING,
"Error while add the frame to buffer source(%s).\n",
339 int64_t pts, end_pts;
344 pts = av_rescale_q(sub->pts + sub->start_display_time * 1000LL,
345 AV_TIME_BASE_Q, ist->
st->time_base);
346 end_pts = av_rescale_q(sub->pts + sub->end_display_time * 1000LL,
347 AV_TIME_BASE_Q, ist->
st->time_base);
348 num_rects = sub->num_rects;
360 av_log(ist->
dec_ctx, AV_LOG_ERROR,
361 "Impossible to get a blank canvas.\n");
364 dst = frame->data [0];
365 dst_linesize = frame->linesize[0];
366 for (i = 0; i < num_rects; i++)
389 pts2 = av_rescale_q(pts, ist->
st->time_base, ist2->
st->time_base) - 1;
391 if (pts2 <= ist2->sub2video.last_pts)
398 for (j = 0, nb_reqs = 0; j < ist2->
nb_filters; j++)
399 nb_reqs += av_buffersrc_get_nb_failed_requests(ist2->
filters[j]->
filter);
414 if (ret != AVERROR_EOF && ret < 0)
415 av_log(NULL, AV_LOG_WARNING,
"Flush the frame error.\n");
425 tcsetattr (0, TCSANOW, &oldtty);
431 av_log(NULL, AV_LOG_QUIET,
"%s",
"");
451 ret = write(2,
"Received > 3 system signals, hard exiting\n",
452 strlen(
"Received > 3 system signals, hard exiting\n"));
458 #if HAVE_SETCONSOLECTRLHANDLER
459 static BOOL WINAPI CtrlHandler(DWORD fdwCtrlType)
461 av_log(NULL, AV_LOG_DEBUG,
"\nReceived windows signal %ld\n", fdwCtrlType);
466 case CTRL_BREAK_EVENT:
470 case CTRL_CLOSE_EVENT:
471 case CTRL_LOGOFF_EVENT:
472 case CTRL_SHUTDOWN_EVENT:
484 av_log(NULL, AV_LOG_ERROR,
"Received unknown windows signal %ld\n", fdwCtrlType);
491 #define SIGNAL(sig, func) \
493 action.sa_handler = func; \
494 sigaction(sig, &action, NULL); \
497 #define SIGNAL(sig, func) \
503 #if defined __linux__
504 struct sigaction action = {0};
508 sigfillset(&action.sa_mask);
511 action.sa_flags = SA_RESTART;
517 if (tcgetattr (0, &tty) == 0) {
521 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
522 |INLCR|IGNCR|ICRNL|IXON);
523 tty.c_oflag |= OPOST;
524 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
525 tty.c_cflag &= ~(CSIZE|PARENB);
530 tcsetattr (0, TCSANOW, &tty);
551 signal(SIGPIPE, SIG_IGN);
554 #if HAVE_SETCONSOLECTRLHANDLER
555 SetConsoleCtrlHandler((PHANDLER_ROUTINE) CtrlHandler, TRUE);
572 n = select(1, &rfds, NULL, NULL, &tv);
581 # if HAVE_PEEKNAMEDPIPE
583 static HANDLE input_handle;
586 input_handle = GetStdHandle(STD_INPUT_HANDLE);
587 is_pipe = !GetConsoleMode(input_handle, &dw);
592 if (!PeekNamedPipe(input_handle, NULL, 0, NULL, &nchars, NULL)) {
626 av_log(NULL, AV_LOG_INFO,
"bench: maxrss=%ikB\n", maxrss);
631 avfilter_graph_free(&fg->
graph);
639 sizeof(frame), NULL);
640 av_frame_free(&frame);
647 &sub,
sizeof(sub), NULL);
648 avsubtitle_free(&sub);
653 av_freep(&ifilter->
name);
660 avfilter_inout_free(&ofilter->
out_tmp);
661 av_freep(&ofilter->
name);
683 if (s && s->oformat && !(s->oformat->flags & AVFMT_NOFILE))
685 avformat_free_context(s);
686 av_dict_free(&of->
opts);
713 avcodec_free_context(&ost->
enc_ctx);
714 avcodec_parameters_free(&ost->
ref_par);
719 av_fifo_generic_read(ost->
muxing_queue, &pkt,
sizeof(pkt), NULL);
720 av_packet_unref(&pkt);
728 free_input_threads();
746 avcodec_free_context(&ist->
dec_ctx);
753 av_log(NULL, AV_LOG_ERROR,
754 "Error closing vstats file, loss of information possible: %s\n",
755 av_err2str(AVERROR(errno)));
766 avformat_network_deinit();
769 av_log(NULL, AV_LOG_INFO,
"Exiting normally, received signal %d.\n",
772 av_log(NULL, AV_LOG_INFO,
"Exiting normally, received cancel request.\n");
774 av_log(NULL, AV_LOG_INFO,
"Conversion failed!\n");
782 AVDictionaryEntry *t = NULL;
784 while ((t = av_dict_get(b,
"", t, AV_DICT_IGNORE_SUFFIX))) {
785 av_dict_set(a, t->key, NULL, AV_DICT_MATCH_CASE);
791 AVDictionaryEntry *t;
792 if ((t = av_dict_get(m,
"", NULL, AV_DICT_IGNORE_SUFFIX))) {
793 av_log(NULL, AV_LOG_FATAL,
"Option %s not found.\n", t->key);
812 vsnprintf(buf,
sizeof(buf), fmt, va);
814 av_log(NULL, AV_LOG_INFO,
815 "bench: %8" PRIu64
" user %8" PRIu64
" sys %8" PRIu64
" real %s \n",
829 ost2->
finished |= ost == ost2 ? this_stream : others;
835 AVFormatContext *s = of->
ctx;
836 AVStream *
st = ost->
st;
847 if (!(
st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && ost->
encoding_needed) && !unqueue) {
849 av_packet_unref(pkt);
856 AVPacket tmp_pkt = {0};
859 unsigned int are_we_over_size =
861 int new_size = are_we_over_size ?
867 av_log(NULL, AV_LOG_ERROR,
868 "Too many packets buffered for output stream %d:%d.\n",
876 ret = av_packet_make_refcounted(pkt);
879 av_packet_move_ref(&tmp_pkt, pkt);
881 av_fifo_generic_write(ost->
muxing_queue, &tmp_pkt,
sizeof(tmp_pkt), NULL);
887 pkt->pts = pkt->dts = AV_NOPTS_VALUE;
889 if (
st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
891 uint8_t *sd = av_packet_get_side_data(pkt, AV_PKT_DATA_QUALITY_STATS,
893 ost->
quality = sd ? AV_RL32(sd) : -1;
894 ost->
pict_type = sd ? sd[4] : AV_PICTURE_TYPE_NONE;
896 for (i = 0; i<FF_ARRAY_ELEMS(ost->
error); i++) {
898 ost->
error[i] = AV_RL64(sd + 8 + 8*i);
904 if (pkt->duration > 0)
905 av_log(NULL, AV_LOG_WARNING,
"Overriding packet duration by frame rate, this should not happen\n");
906 pkt->duration = av_rescale_q(1, av_inv_q(ost->
frame_rate),
913 if (!(s->oformat->flags & AVFMT_NOTIMESTAMPS)) {
914 if (pkt->dts != AV_NOPTS_VALUE &&
915 pkt->pts != AV_NOPTS_VALUE &&
916 pkt->dts > pkt->pts) {
917 av_log(s, AV_LOG_WARNING,
"Invalid DTS: %"PRId64
" PTS: %"PRId64
" in output stream %d:%d, replacing by guess\n",
925 if ((
st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO ||
st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO ||
st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE) &&
926 pkt->dts != AV_NOPTS_VALUE &&
927 !(
st->codecpar->codec_id == AV_CODEC_ID_VP9 && ost->
stream_copy) &&
929 int64_t max = ost->
last_mux_dts + !(s->oformat->flags & AVFMT_TS_NONSTRICT);
930 if (pkt->dts < max) {
931 int loglevel = max - pkt->dts > 2 ||
st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO ? AV_LOG_WARNING : AV_LOG_DEBUG;
933 loglevel = AV_LOG_ERROR;
934 av_log(s, loglevel,
"Non-monotonous DTS in output stream "
935 "%d:%d; previous: %"PRId64
", current: %"PRId64
"; ",
938 av_log(NULL, AV_LOG_FATAL,
"aborting.\n");
941 av_log(s, loglevel,
"changing to %"PRId64
". This may result "
942 "in incorrect timestamps in the output file.\n",
944 if (pkt->pts >= pkt->dts)
945 pkt->pts = FFMAX(pkt->pts, max);
955 pkt->stream_index = ost->
index;
958 av_log(NULL, AV_LOG_INFO,
"muxer <- type:%s "
959 "pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s size:%d\n",
960 av_get_media_type_string(ost->
enc_ctx->codec_type),
961 av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &ost->
st->time_base),
962 av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, &ost->
st->time_base),
967 ret = av_interleaved_write_frame(s, pkt);
973 av_packet_unref(pkt);
1005 ret = av_bsf_send_packet(ost->
bsf_ctx, eof ? NULL : pkt);
1008 while ((
ret = av_bsf_receive_packet(ost->
bsf_ctx, pkt)) >= 0)
1010 if (
ret == AVERROR(EAGAIN))
1016 if (
ret < 0 &&
ret != AVERROR_EOF) {
1017 av_log(NULL, AV_LOG_ERROR,
"Error applying bitstream filters to an output "
1030 AV_TIME_BASE_Q) >= 0) {
1039 double float_pts = AV_NOPTS_VALUE;
1040 AVCodecContext *enc = ost->
enc_ctx;
1041 if (!frame || frame->pts == AV_NOPTS_VALUE ||
1049 AVRational filter_tb = av_buffersink_get_time_base(filter);
1050 AVRational tb = enc->time_base;
1051 int extra_bits = av_clip(29 - av_log2(tb.den), 0, 16);
1053 tb.den <<= extra_bits;
1055 av_rescale_q(frame->pts, filter_tb, tb) -
1056 av_rescale_q(start_time, AV_TIME_BASE_Q, tb);
1057 float_pts /= 1 << extra_bits;
1059 float_pts += FFSIGN(float_pts) * 1.0 / (1<<17);
1062 av_rescale_q(frame->pts, filter_tb, enc->time_base) -
1063 av_rescale_q(start_time, AV_TIME_BASE_Q, enc->time_base);
1069 av_log(NULL, AV_LOG_INFO,
"filter -> pts:%s pts_time:%s exact:%f time_base:%d/%d\n",
1070 frame ? av_ts2str(frame->pts) :
"NULL",
1071 frame ? av_ts2timestr(frame->pts, &enc->time_base) :
"NULL",
1073 enc ? enc->time_base.num : -1,
1074 enc ? enc->time_base.den : -1);
1084 int ret = AVERROR_BUG;
1085 char error[1024] = {0};
1092 av_log(NULL, AV_LOG_ERROR,
"Error initializing output stream %d:%d -- %s\n",
1105 AVCodecContext *enc = ost->
enc_ctx;
1109 av_init_packet(&pkt);
1120 ost->
sync_opts = frame->pts + frame->nb_samples;
1124 av_assert0(pkt.size || !pkt.data);
1127 av_log(NULL, AV_LOG_INFO,
"encoder <- type:audio "
1128 "frame_pts:%s frame_pts_time:%s time_base:%d/%d\n",
1129 av_ts2str(frame->pts), av_ts2timestr(frame->pts, &enc->time_base),
1130 enc->time_base.num, enc->time_base.den);
1133 ret = avcodec_send_frame(enc, frame);
1138 ret = avcodec_receive_packet(enc, &pkt);
1139 if (
ret == AVERROR(EAGAIN))
1146 av_packet_rescale_ts(&pkt, enc->time_base, ost->
mux_timebase);
1149 av_log(NULL, AV_LOG_INFO,
"encoder -> type:audio "
1150 "pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s\n",
1151 av_ts2str(pkt.pts), av_ts2timestr(pkt.pts, &enc->time_base),
1152 av_ts2str(pkt.dts), av_ts2timestr(pkt.dts, &enc->time_base));
1160 av_log(NULL, AV_LOG_FATAL,
"Audio encoding failed\n");
1168 int subtitle_out_max_size = 1024 * 1024;
1169 int subtitle_out_size, nb, i;
1170 AVCodecContext *enc;
1174 if (sub->pts == AV_NOPTS_VALUE) {
1175 av_log(NULL, AV_LOG_ERROR,
"Subtitle packets must have a pts\n");
1186 av_log(NULL, AV_LOG_FATAL,
"Failed to allocate subtitle_out\n");
1194 if (enc->codec_id == AV_CODEC_ID_DVB_SUBTITLE)
1203 for (i = 0; i < nb; i++) {
1204 unsigned save_num_rects = sub->num_rects;
1206 ost->
sync_opts = av_rescale_q(
pts, AV_TIME_BASE_Q, enc->time_base);
1212 sub->pts += av_rescale_q(sub->start_display_time, (AVRational){ 1, 1000 }, AV_TIME_BASE_Q);
1213 sub->end_display_time -= sub->start_display_time;
1214 sub->start_display_time = 0;
1220 subtitle_out_size = avcodec_encode_subtitle(enc,
subtitle_out,
1221 subtitle_out_max_size, sub);
1223 sub->num_rects = save_num_rects;
1224 if (subtitle_out_size < 0) {
1225 av_log(NULL, AV_LOG_FATAL,
"Subtitle encoding failed\n");
1229 av_init_packet(&pkt);
1231 pkt.size = subtitle_out_size;
1232 pkt.pts = av_rescale_q(sub->pts, AV_TIME_BASE_Q, ost->
mux_timebase);
1233 pkt.duration = av_rescale_q(sub->end_display_time, (AVRational){ 1, 1000 }, ost->
mux_timebase);
1234 if (enc->codec_id == AV_CODEC_ID_DVB_SUBTITLE) {
1238 pkt.pts += av_rescale_q(sub->start_display_time, (AVRational){ 1, 1000 }, ost->
mux_timebase);
1240 pkt.pts += av_rescale_q(sub->end_display_time, (AVRational){ 1, 1000 }, ost->
mux_timebase);
1249 AVFrame *next_picture)
1251 int ret, format_video_sync;
1253 AVCodecContext *enc = ost->
enc_ctx;
1254 AVRational frame_rate;
1255 int nb_frames, nb0_frames, i;
1256 double delta, delta0;
1257 double duration = 0;
1258 double sync_ipts = AV_NOPTS_VALUE;
1269 frame_rate = av_buffersink_get_frame_rate(filter);
1270 if (frame_rate.num > 0 && frame_rate.den > 0)
1271 duration = 1/(av_q2d(frame_rate) * av_q2d(enc->time_base));
1273 if(ist && ist->
st->start_time != AV_NOPTS_VALUE && ist->
st->first_dts != AV_NOPTS_VALUE && ost->
frame_rate.num)
1274 duration = FFMIN(duration, 1/(av_q2d(ost->
frame_rate) * av_q2d(enc->time_base)));
1281 lrintf(next_picture->pkt_duration * av_q2d(ist->
st->time_base) / av_q2d(enc->time_base)) > 0) {
1282 duration = lrintf(next_picture->pkt_duration * av_q2d(ist->
st->time_base) / av_q2d(enc->time_base));
1285 if (!next_picture) {
1292 delta = delta0 + duration;
1300 if(!strcmp(of->
ctx->oformat->name,
"avi")) {
1320 if (delta0 < -0.6) {
1321 av_log(NULL, AV_LOG_VERBOSE,
"Past duration %f too large\n", -delta0);
1323 av_log(NULL, AV_LOG_DEBUG,
"Clipping frame in rate conversion by %f\n", -delta0);
1329 switch (format_video_sync) {
1332 av_log(NULL, AV_LOG_DEBUG,
"Not duplicating %d initial frames\n", (
int)lrintf(delta0));
1341 }
else if (delta < -1.1)
1343 else if (delta > 1.1) {
1344 nb_frames = lrintf(delta);
1346 nb0_frames = llrintf(delta0 - 0.6);
1352 else if (delta > 0.6)
1365 nb0_frames = FFMIN(nb0_frames, nb_frames);
1374 av_log(NULL, AV_LOG_VERBOSE,
1375 "*** dropping frame %d from stream %d at ts %"PRId64
"\n",
1378 if (nb_frames > (nb0_frames && ost->
last_dropped) + (nb_frames > nb0_frames)) {
1380 av_log(NULL, AV_LOG_ERROR,
"%d frame duplication too large, skipping\n", nb_frames - 1);
1385 av_log(NULL, AV_LOG_VERBOSE,
"*** %d dup!\n", nb_frames - 1);
1387 av_log(NULL, AV_LOG_WARNING,
"More than %d frames duplicated\n",
dup_warning);
1391 ost->
last_dropped = nb_frames == nb0_frames && next_picture;
1394 for (i = 0; i < nb_frames; i++) {
1395 AVFrame *in_picture;
1396 int forced_keyframe = 0;
1398 av_init_packet(&pkt);
1402 if (i < nb0_frames && ost->last_frame) {
1405 in_picture = next_picture;
1415 in_picture->quality = enc->global_quality;
1416 in_picture->pict_type = 0;
1419 in_picture->pts != AV_NOPTS_VALUE)
1422 pts_time = in_picture->pts != AV_NOPTS_VALUE ?
1427 forced_keyframe = 1;
1433 ff_dlog(NULL,
"force_key_frame: n:%f n_forced:%f prev_forced_n:%f t:%f prev_forced_t:%f -> res:%f\n",
1441 forced_keyframe = 1;
1452 && in_picture->key_frame==1
1454 forced_keyframe = 1;
1457 if (forced_keyframe) {
1458 in_picture->pict_type = AV_PICTURE_TYPE_I;
1459 av_log(NULL, AV_LOG_DEBUG,
"Forced keyframe at time %f\n", pts_time);
1464 av_log(NULL, AV_LOG_INFO,
"encoder <- type:video "
1465 "frame_pts:%s frame_pts_time:%s time_base:%d/%d\n",
1466 av_ts2str(in_picture->pts), av_ts2timestr(in_picture->pts, &enc->time_base),
1467 enc->time_base.num, enc->time_base.den);
1472 ret = avcodec_send_frame(enc, in_picture);
1476 av_frame_remove_side_data(in_picture, AV_FRAME_DATA_A53_CC);
1479 ret = avcodec_receive_packet(enc, &pkt);
1481 if (
ret == AVERROR(EAGAIN))
1487 av_log(NULL, AV_LOG_INFO,
"encoder -> type:video "
1488 "pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s\n",
1489 av_ts2str(pkt.pts), av_ts2timestr(pkt.pts, &enc->time_base),
1490 av_ts2str(pkt.dts), av_ts2timestr(pkt.dts, &enc->time_base));
1493 if (pkt.pts == AV_NOPTS_VALUE && !(enc->codec->capabilities & AV_CODEC_CAP_DELAY))
1496 av_packet_rescale_ts(&pkt, enc->time_base, ost->
mux_timebase);
1499 av_log(NULL, AV_LOG_INFO,
"encoder -> type:video "
1500 "pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s\n",
1501 av_ts2str(pkt.pts), av_ts2timestr(pkt.pts, &ost->
mux_timebase),
1502 av_ts2str(pkt.dts), av_ts2timestr(pkt.dts, &ost->
mux_timebase));
1505 frame_size = pkt.size;
1509 if (ost->
logfile && enc->stats_out) {
1510 fprintf(ost->
logfile,
"%s", enc->stats_out);
1535 av_log(NULL, AV_LOG_FATAL,
"Video encoding failed\n");
1541 return -10.0 * log10(d);
1546 AVCodecContext *enc;
1548 double ti1, bitrate, avg_bitrate;
1560 if (enc->codec_type == AVMEDIA_TYPE_VIDEO) {
1561 frame_number = ost->
st->nb_frames;
1563 fprintf(
vstats_file,
"frame= %5d q= %2.1f ", frame_number,
1564 ost->
quality / (
float)FF_QP2LAMBDA);
1567 ost->
quality / (
float)FF_QP2LAMBDA);
1570 if (ost->
error[0]>=0 && (enc->flags & AV_CODEC_FLAG_PSNR))
1571 fprintf(
vstats_file,
"PSNR= %6.2f ",
psnr(ost->
error[0] / (enc->width * enc->height * 255.0 * 255.0)));
1575 ti1 = av_stream_get_end_pts(ost->
st) * av_q2d(ost->
st->time_base);
1579 bitrate = (frame_size * 8) / av_q2d(enc->time_base) / 1000.0;
1580 avg_bitrate = (double)(ost->
data_size * 8) / ti1 / 1000.0;
1581 fprintf(
vstats_file,
"s_size= %8.0fkB time= %0.3f br= %7.1fkbits/s avg_br= %7.1fkbits/s ",
1582 (
double)ost->
data_size / 1024, ti1, bitrate, avg_bitrate);
1595 for (i = 0; i < of->
ctx->nb_streams; i++)
1608 AVFrame *filtered_frame = NULL;
1615 AVFilterContext *filter;
1616 AVCodecContext *enc = ost->
enc_ctx;
1632 if (av_buffersink_get_type(filter) == AVMEDIA_TYPE_AUDIO)
1636 return AVERROR(ENOMEM);
1641 ret = av_buffersink_get_frame_flags(filter, filtered_frame,
1642 AV_BUFFERSINK_FLAG_NO_REQUEST);
1644 if (
ret != AVERROR(EAGAIN) &&
ret != AVERROR_EOF) {
1645 av_log(NULL, AV_LOG_WARNING,
1646 "Error in av_buffersink_get_frame_flags(): %s\n", av_err2str(
ret));
1647 }
else if (flush &&
ret == AVERROR_EOF) {
1648 if (av_buffersink_get_type(filter) == AVMEDIA_TYPE_VIDEO)
1654 av_frame_unref(filtered_frame);
1658 switch (av_buffersink_get_type(filter)) {
1659 case AVMEDIA_TYPE_VIDEO:
1661 enc->sample_aspect_ratio = filtered_frame->sample_aspect_ratio;
1665 case AVMEDIA_TYPE_AUDIO:
1666 if (!(enc->codec->capabilities & AV_CODEC_CAP_PARAM_CHANGE) &&
1667 enc->channels != filtered_frame->channels) {
1668 av_log(NULL, AV_LOG_ERROR,
1669 "Audio filter graph output is not normalized and encoder does not support parameter changes\n");
1679 av_frame_unref(filtered_frame);
1688 uint64_t video_size = 0, audio_size = 0, extra_size = 0, other_size = 0;
1689 uint64_t subtitle_size = 0;
1691 float percent = -1.0;
1697 switch (ost->
enc_ctx->codec_type) {
1698 case AVMEDIA_TYPE_VIDEO: video_size += ost->
data_size;
break;
1699 case AVMEDIA_TYPE_AUDIO: audio_size += ost->
data_size;
break;
1700 case AVMEDIA_TYPE_SUBTITLE: subtitle_size += ost->
data_size;
break;
1701 default: other_size += ost->
data_size;
break;
1703 extra_size += ost->
enc_ctx->extradata_size;
1705 if ( (ost->
enc_ctx->flags & (AV_CODEC_FLAG_PASS1 | AV_CODEC_FLAG_PASS2))
1706 != AV_CODEC_FLAG_PASS1)
1713 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: ",
1714 video_size / 1024.0,
1715 audio_size / 1024.0,
1716 subtitle_size / 1024.0,
1717 other_size / 1024.0,
1718 extra_size / 1024.0);
1720 av_log(NULL, AV_LOG_INFO,
"%f%%", percent);
1722 av_log(NULL, AV_LOG_INFO,
"unknown");
1723 av_log(NULL, AV_LOG_INFO,
"\n");
1728 uint64_t total_packets = 0, total_size = 0;
1730 av_log(NULL, AV_LOG_VERBOSE,
"Input file #%d (%s):\n",
1735 enum AVMediaType type = ist->
dec_ctx->codec_type;
1740 av_log(NULL, AV_LOG_VERBOSE,
" Input stream #%d:%d (%s): ",
1742 av_log(NULL, AV_LOG_VERBOSE,
"%"PRIu64
" packets read (%"PRIu64
" bytes); ",
1746 av_log(NULL, AV_LOG_VERBOSE,
"%"PRIu64
" frames decoded",
1748 if (type == AVMEDIA_TYPE_AUDIO)
1749 av_log(NULL, AV_LOG_VERBOSE,
" (%"PRIu64
" samples)", ist->
samples_decoded);
1750 av_log(NULL, AV_LOG_VERBOSE,
"; ");
1753 av_log(NULL, AV_LOG_VERBOSE,
"\n");
1756 av_log(NULL, AV_LOG_VERBOSE,
" Total: %"PRIu64
" packets (%"PRIu64
" bytes) demuxed\n",
1757 total_packets, total_size);
1762 uint64_t total_packets = 0, total_size = 0;
1764 av_log(NULL, AV_LOG_VERBOSE,
"Output file #%d (%s):\n",
1767 for (j = 0; j < of->
ctx->nb_streams; j++) {
1769 enum AVMediaType type = ost->
enc_ctx->codec_type;
1774 av_log(NULL, AV_LOG_VERBOSE,
" Output stream #%d:%d (%s): ",
1777 av_log(NULL, AV_LOG_VERBOSE,
"%"PRIu64
" frames encoded",
1779 if (type == AVMEDIA_TYPE_AUDIO)
1780 av_log(NULL, AV_LOG_VERBOSE,
" (%"PRIu64
" samples)", ost->
samples_encoded);
1781 av_log(NULL, AV_LOG_VERBOSE,
"; ");
1784 av_log(NULL, AV_LOG_VERBOSE,
"%"PRIu64
" packets muxed (%"PRIu64
" bytes); ",
1787 av_log(NULL, AV_LOG_VERBOSE,
"\n");
1790 av_log(NULL, AV_LOG_VERBOSE,
" Total: %"PRIu64
" packets (%"PRIu64
" bytes) muxed\n",
1791 total_packets, total_size);
1793 if(video_size +
data_size + audio_size + subtitle_size + extra_size == 0){
1794 av_log(NULL, AV_LOG_WARNING,
"Output file is empty, nothing was encoded ");
1796 av_log(NULL, AV_LOG_WARNING,
"\n");
1798 av_log(NULL, AV_LOG_WARNING,
"(check -ss / -t / -frames parameters if used)\n");
1803 static void forward_report(
int is_last_report, int64_t timer_start, int64_t cur_time)
1805 AVFormatContext *oc = NULL;
1806 AVCodecContext *enc = NULL;
1808 int64_t
pts = INT64_MIN + 1;
1811 int frame_number = 0;
1814 int64_t total_size = 0;
1816 double bitrate = 0.0;
1819 float t = (cur_time-timer_start) / 1000000.0;
1824 total_size = avio_size(oc->pb);
1825 if (total_size <= 0) {
1826 total_size = avio_tell(oc->pb);
1837 quality = ost->
quality / (float) FF_QP2LAMBDA;
1840 if (!vid && enc->codec_type == AVMEDIA_TYPE_VIDEO) {
1846 fps = t > 1 ? frame_number / t : 0;
1850 if (av_stream_get_end_pts(ost->
st) != AV_NOPTS_VALUE)
1851 pts = FFMAX(
pts, av_rescale_q(av_stream_get_end_pts(ost->
st),
1852 ost->
st->time_base, AV_TIME_BASE_Q));
1858 seconds = FFABS(
pts) / 1000;
1861 bitrate =
pts && total_size >= 0 ? total_size * 8 / (
pts / 1000.0) : -1;
1864 speed = t != 0.0 ? (double)
pts / AV_TIME_BASE / t : -1;
1868 report_callback(frame_number, fps, quality, total_size, seconds, bitrate, speed);
1872 static void print_report(
int is_last_report, int64_t timer_start, int64_t cur_time)
1874 AVBPrint buf, buf_script;
1876 AVFormatContext *oc;
1878 AVCodecContext *enc;
1879 int frame_number, vid, i;
1882 int64_t
pts = INT64_MIN + 1;
1883 int hours, mins, secs, us;
1884 const char *hours_sign;
1888 if (!is_last_report) {
1903 t = (cur_time-timer_start) / 1000000.0;
1908 total_size = avio_size(oc->pb);
1909 if (total_size <= 0)
1910 total_size = avio_tell(oc->pb);
1913 av_bprint_init(&buf, 0, AV_BPRINT_SIZE_AUTOMATIC);
1914 av_bprint_init(&buf_script, 0, AV_BPRINT_SIZE_AUTOMATIC);
1920 q = ost->
quality / (float) FF_QP2LAMBDA;
1922 if (vid && enc->codec_type == AVMEDIA_TYPE_VIDEO) {
1923 av_bprintf(&buf,
"q=%2.1f ", q);
1924 av_bprintf(&buf_script,
"stream_%d_%d_q=%.1f\n",
1927 if (!vid && enc->codec_type == AVMEDIA_TYPE_VIDEO) {
1931 fps = t > 1 ? frame_number / t : 0;
1932 av_bprintf(&buf,
"frame=%5d fps=%3.*f q=%3.1f ",
1933 frame_number, fps < 9.95, fps, q);
1934 av_bprintf(&buf_script,
"frame=%d\n", frame_number);
1935 av_bprintf(&buf_script,
"fps=%.2f\n", fps);
1936 av_bprintf(&buf_script,
"stream_%d_%d_q=%.1f\n",
1939 av_bprintf(&buf,
"L");
1945 for (j = 0; j < 32; j++)
1949 if ((enc->flags & AV_CODEC_FLAG_PSNR) && (ost->
pict_type != AV_PICTURE_TYPE_NONE || is_last_report)) {
1951 double error, error_sum = 0;
1952 double scale, scale_sum = 0;
1954 char type[3] = {
'Y',
'U',
'V' };
1955 av_bprintf(&buf,
"PSNR=");
1956 for (j = 0; j < 3; j++) {
1957 if (is_last_report) {
1958 error = enc->error[j];
1959 scale = enc->width * enc->height * 255.0 * 255.0 * frame_number;
1961 error = ost->
error[j];
1962 scale = enc->width * enc->height * 255.0 * 255.0;
1968 p =
psnr(error / scale);
1969 av_bprintf(&buf,
"%c:%2.2f ", type[j], p);
1970 av_bprintf(&buf_script,
"stream_%d_%d_psnr_%c=%2.2f\n",
1973 p =
psnr(error_sum / scale_sum);
1974 av_bprintf(&buf,
"*:%2.2f ",
psnr(error_sum / scale_sum));
1975 av_bprintf(&buf_script,
"stream_%d_%d_psnr_all=%2.2f\n",
1981 if (av_stream_get_end_pts(ost->
st) != AV_NOPTS_VALUE) {
1982 pts = FFMAX(
pts, av_rescale_q(av_stream_get_end_pts(ost->
st),
1983 ost->
st->time_base, AV_TIME_BASE_Q));
1996 secs = FFABS(
pts) / AV_TIME_BASE;
1997 us = FFABS(
pts) % AV_TIME_BASE;
2002 hours_sign = (
pts < 0) ?
"-" :
"";
2004 bitrate =
pts && total_size >= 0 ? total_size * 8 / (
pts / 1000.0) : -1;
2005 speed = t != 0.0 ? (double)
pts / AV_TIME_BASE / t : -1;
2007 if (total_size < 0) av_bprintf(&buf,
"size=N/A time=");
2008 else av_bprintf(&buf,
"size=%8.0fkB time=", total_size / 1024.0);
2009 if (
pts == AV_NOPTS_VALUE) {
2010 av_bprintf(&buf,
"N/A ");
2012 av_bprintf(&buf,
"%s%02d:%02d:%02d.%02d ",
2013 hours_sign, hours, mins, secs, (100 * us) / AV_TIME_BASE);
2017 av_bprintf(&buf,
"bitrate=N/A");
2018 av_bprintf(&buf_script,
"bitrate=N/A\n");
2020 av_bprintf(&buf,
"bitrate=%6.1fkbits/s", bitrate);
2021 av_bprintf(&buf_script,
"bitrate=%6.1fkbits/s\n", bitrate);
2024 if (total_size < 0) av_bprintf(&buf_script,
"total_size=N/A\n");
2025 else av_bprintf(&buf_script,
"total_size=%"PRId64
"\n", total_size);
2026 if (
pts == AV_NOPTS_VALUE) {
2027 av_bprintf(&buf_script,
"out_time_us=N/A\n");
2028 av_bprintf(&buf_script,
"out_time_ms=N/A\n");
2029 av_bprintf(&buf_script,
"out_time=N/A\n");
2031 av_bprintf(&buf_script,
"out_time_us=%"PRId64
"\n",
pts);
2032 av_bprintf(&buf_script,
"out_time_ms=%"PRId64
"\n",
pts);
2033 av_bprintf(&buf_script,
"out_time=%s%02d:%02d:%02d.%06d\n",
2034 hours_sign, hours, mins, secs, us);
2043 av_bprintf(&buf,
" speed=N/A");
2044 av_bprintf(&buf_script,
"speed=N/A\n");
2046 av_bprintf(&buf,
" speed=%4.3gx", speed);
2047 av_bprintf(&buf_script,
"speed=%4.3gx\n", speed);
2051 const char end = is_last_report ?
'\n' :
'\r';
2052 if (
print_stats==1 && AV_LOG_INFO > av_log_get_level()) {
2053 fprintf(stderr,
"%s %c", buf.str, end);
2055 av_log(NULL, AV_LOG_INFO,
"%s %c", buf.str, end);
2059 av_bprint_finalize(&buf, NULL);
2062 av_bprintf(&buf_script,
"progress=%s\n",
2063 is_last_report ?
"end" :
"continue");
2065 FFMIN(buf_script.len, buf_script.size - 1));
2067 av_bprint_finalize(&buf_script, NULL);
2068 if (is_last_report) {
2070 av_log(NULL, AV_LOG_ERROR,
2071 "Error closing progress log, loss of information possible: %s\n", av_err2str(
ret));
2085 ifilter->
format = par->format;
2089 ifilter->
width = par->width;
2090 ifilter->
height = par->height;
2100 AVCodecContext *enc = ost->
enc_ctx;
2111 av_log(NULL, AV_LOG_WARNING,
2112 "Finishing stream %d:%d without any data written to it.\n",
2128 av_log(NULL, AV_LOG_ERROR,
"Error configuring filter graph\n");
2138 if (enc->codec_type != AVMEDIA_TYPE_VIDEO && enc->codec_type != AVMEDIA_TYPE_AUDIO)
2142 const char *desc = NULL;
2146 switch (enc->codec_type) {
2147 case AVMEDIA_TYPE_AUDIO:
2150 case AVMEDIA_TYPE_VIDEO:
2157 av_init_packet(&pkt);
2163 while ((
ret = avcodec_receive_packet(enc, &pkt)) == AVERROR(EAGAIN)) {
2164 ret = avcodec_send_frame(enc, NULL);
2166 av_log(NULL, AV_LOG_FATAL,
"%s encoding failed: %s\n",
2174 if (
ret < 0 &&
ret != AVERROR_EOF) {
2175 av_log(NULL, AV_LOG_FATAL,
"%s encoding failed: %s\n",
2180 if (ost->
logfile && enc->stats_out) {
2181 fprintf(ost->
logfile,
"%s", enc->stats_out);
2183 if (
ret == AVERROR_EOF) {
2188 av_packet_unref(&pkt);
2191 av_packet_rescale_ts(&pkt, enc->time_base, ost->
mux_timebase);
2192 pkt_size = pkt.size;
2226 int64_t ost_tb_start_time = av_rescale_q(start_time, AV_TIME_BASE_Q, ost->
mux_timebase);
2231 av_init_packet(&opkt);
2238 if ((!ost->
frame_number && !(pkt->flags & AV_PKT_FLAG_KEY)) &&
2243 int64_t comp_start = start_time;
2246 if (pkt->pts == AV_NOPTS_VALUE ?
2247 ist->
pts < comp_start :
2248 pkt->pts < av_rescale_q(comp_start, AV_TIME_BASE_Q, ist->
st->time_base))
2259 start_time = f->
ctx->start_time;
2269 if (ost->
enc_ctx->codec_type == AVMEDIA_TYPE_VIDEO)
2272 if (av_packet_ref(&opkt, pkt) < 0)
2275 if (pkt->pts != AV_NOPTS_VALUE)
2276 opkt.pts = av_rescale_q(pkt->pts, ist->
st->time_base, ost->
mux_timebase) - ost_tb_start_time;
2278 if (pkt->dts == AV_NOPTS_VALUE) {
2280 }
else if (ost->
st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
2281 int duration = av_get_audio_frame_duration(ist->
dec_ctx, pkt->size);
2283 duration = ist->
dec_ctx->frame_size;
2284 opkt.dts = av_rescale_delta(ist->
st->time_base, pkt->dts,
2285 (AVRational){1, ist->dec_ctx->sample_rate}, duration,
2288 opkt.pts = opkt.dts - ost_tb_start_time;
2290 opkt.dts = av_rescale_q(pkt->dts, ist->
st->time_base, ost->
mux_timebase);
2291 opkt.dts -= ost_tb_start_time;
2293 opkt.duration = av_rescale_q(pkt->duration, ist->
st->time_base, ost->
mux_timebase);
2302 if (!
dec->channel_layout) {
2303 char layout_name[256];
2307 dec->channel_layout = av_get_default_channel_layout(
dec->channels);
2308 if (!
dec->channel_layout)
2310 av_get_channel_layout_string(layout_name,
sizeof(layout_name),
2311 dec->channels,
dec->channel_layout);
2312 av_log(NULL, AV_LOG_WARNING,
"Guessed Channel Layout for Input Stream "
2313 "#%d.%d : %s\n", ist->
file_index, ist->
st->index, layout_name);
2351 int need_reinit,
ret, i;
2354 need_reinit = ifilter->
format != frame->format;
2356 switch (ifilter->
ist->
st->codecpar->codec_type) {
2357 case AVMEDIA_TYPE_AUDIO:
2358 need_reinit |= ifilter->
sample_rate != frame->sample_rate ||
2359 ifilter->
channels != frame->channels ||
2362 case AVMEDIA_TYPE_VIDEO:
2363 need_reinit |= ifilter->
width != frame->width ||
2364 ifilter->
height != frame->height;
2382 if (need_reinit || !fg->
graph) {
2385 AVFrame *tmp = av_frame_clone(frame);
2387 return AVERROR(ENOMEM);
2388 av_frame_unref(frame);
2393 av_frame_free(&tmp);
2397 av_fifo_generic_write(ifilter->
frame_queue, &tmp,
sizeof(tmp), NULL);
2403 if (
ret < 0 &&
ret != AVERROR_EOF) {
2404 av_log(NULL, AV_LOG_ERROR,
"Error while filtering: %s\n", av_err2str(
ret));
2410 av_log(NULL, AV_LOG_ERROR,
"Error reinitializing filters!\n");
2415 ret = av_buffersrc_add_frame_flags(ifilter->
filter, frame, AV_BUFFERSRC_FLAG_PUSH);
2417 if (
ret != AVERROR_EOF)
2418 av_log(NULL, AV_LOG_ERROR,
"Error while filtering: %s\n", av_err2str(
ret));
2435 ret = av_buffersrc_close(ifilter->
filter,
pts, AV_BUFFERSRC_FLAG_PUSH);
2443 if (ifilter->
format < 0 && (ifilter->
type == AVMEDIA_TYPE_AUDIO || ifilter->
type == AVMEDIA_TYPE_VIDEO)) {
2444 av_log(NULL, AV_LOG_ERROR,
"Cannot determine format of input stream %d:%d after EOF\n", ifilter->
ist->
file_index, ifilter->
ist->
st->index);
2445 return AVERROR_INVALIDDATA;
2456 static int decode(AVCodecContext *avctx, AVFrame *frame,
int *got_frame, AVPacket *pkt)
2463 ret = avcodec_send_packet(avctx, pkt);
2466 if (
ret < 0 &&
ret != AVERROR_EOF)
2470 ret = avcodec_receive_frame(avctx, frame);
2471 if (
ret < 0 &&
ret != AVERROR(EAGAIN))
2494 if (
ret == AVERROR_EOF)
2497 av_log(NULL, AV_LOG_ERROR,
2498 "Failed to inject frame into filter network: %s\n", av_err2str(
ret));
2509 AVCodecContext *avctx = ist->
dec_ctx;
2511 AVRational decoded_frame_tb;
2514 return AVERROR(ENOMEM);
2516 return AVERROR(ENOMEM);
2525 if (
ret >= 0 && avctx->sample_rate <= 0) {
2526 av_log(avctx, AV_LOG_ERROR,
"Sample rate %d invalid\n", avctx->sample_rate);
2527 ret = AVERROR_INVALIDDATA;
2530 if (
ret != AVERROR_EOF)
2547 decoded_frame_tb = ist->
st->time_base;
2548 }
else if (pkt && pkt->pts != AV_NOPTS_VALUE) {
2550 decoded_frame_tb = ist->
st->time_base;
2553 decoded_frame_tb = AV_TIME_BASE_Q;
2558 (AVRational){1, avctx->sample_rate});
2564 return err < 0 ? err :
ret;
2571 int i,
ret = 0, err = 0;
2572 int64_t best_effort_timestamp;
2573 int64_t
dts = AV_NOPTS_VALUE;
2579 if (!eof && pkt && pkt->size == 0)
2583 return AVERROR(ENOMEM);
2585 return AVERROR(ENOMEM);
2587 if (ist->
dts != AV_NOPTS_VALUE)
2588 dts = av_rescale_q(ist->
dts, AV_TIME_BASE_Q, ist->
st->time_base);
2599 return AVERROR(ENOMEM);
2612 if (ist->
st->codecpar->video_delay < ist->
dec_ctx->has_b_frames) {
2613 if (ist->
dec_ctx->codec_id == AV_CODEC_ID_H264) {
2614 ist->
st->codecpar->video_delay = ist->
dec_ctx->has_b_frames;
2616 av_log(ist->
dec_ctx, AV_LOG_WARNING,
2617 "video_delay is larger in decoder than demuxer %d > %d.\n"
2618 "If you want to help, upload a sample "
2619 "of this file to https://streams.videolan.org/upload/ "
2620 "and contact the ffmpeg-devel mailing list. (ffmpeg-devel@ffmpeg.org)\n",
2622 ist->
st->codecpar->video_delay);
2625 if (
ret != AVERROR_EOF)
2632 av_log(NULL, AV_LOG_DEBUG,
"Frame parameters mismatch context %d,%d,%d != %d,%d,%d\n",
2657 best_effort_timestamp=
decoded_frame->best_effort_timestamp;
2663 if (eof && best_effort_timestamp == AV_NOPTS_VALUE && ist->
nb_dts_buffer > 0) {
2671 if(best_effort_timestamp != AV_NOPTS_VALUE) {
2672 int64_t ts = av_rescale_q(
decoded_frame->pts = best_effort_timestamp, ist->
st->time_base, AV_TIME_BASE_Q);
2674 if (ts != AV_NOPTS_VALUE)
2679 av_log(NULL, AV_LOG_INFO,
"decoder -> ist_index:%d type:video "
2680 "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",
2683 best_effort_timestamp,
2684 av_ts2timestr(best_effort_timestamp, &ist->
st->time_base),
2686 ist->
st->time_base.num, ist->
st->time_base.den);
2689 if (ist->
st->sample_aspect_ratio.num)
2697 return err < 0 ? err :
ret;
2705 int i,
ret = avcodec_decode_subtitle2(ist->
dec_ctx,
2721 1000, AV_TIME_BASE);
2722 if (end < ist->
prev_sub.subtitle.end_display_time) {
2723 av_log(ist->
dec_ctx, AV_LOG_DEBUG,
2724 "Subtitle duration reduced from %"PRId32
" to %d%s\n",
2726 end <= 0 ?
", dropping it" :
"");
2765 || ost->
enc->type != AVMEDIA_TYPE_SUBTITLE)
2781 int64_t
pts = av_rescale_q_rnd(ist->
pts, AV_TIME_BASE_Q, ist->
st->time_base,
2782 AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX);
2797 int eof_reached = 0;
2801 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;
2804 ist->
dts += av_rescale_q(pkt->pts, ist->
st->time_base, AV_TIME_BASE_Q);
2810 if (ist->
next_dts == AV_NOPTS_VALUE)
2812 if (ist->
next_pts == AV_NOPTS_VALUE)
2817 av_init_packet(&avpkt);
2824 if (pkt && pkt->dts != AV_NOPTS_VALUE) {
2825 ist->
next_dts = ist->
dts = av_rescale_q(pkt->dts, ist->
st->time_base, AV_TIME_BASE_Q);
2832 int64_t duration_dts = 0;
2833 int64_t duration_pts = 0;
2835 int decode_failed = 0;
2840 switch (ist->
dec_ctx->codec_type) {
2841 case AVMEDIA_TYPE_AUDIO:
2845 case AVMEDIA_TYPE_VIDEO:
2849 if (pkt && pkt->duration) {
2850 duration_dts = av_rescale_q(pkt->duration, ist->
st->time_base, AV_TIME_BASE_Q);
2851 }
else if(ist->
dec_ctx->framerate.num != 0 && ist->
dec_ctx->framerate.den != 0) {
2852 int ticks= av_stream_get_parser(ist->
st) ? av_stream_get_parser(ist->
st)->repeat_pict+1 : ist->
dec_ctx->ticks_per_frame;
2853 duration_dts = ((int64_t)AV_TIME_BASE *
2854 ist->
dec_ctx->framerate.den * ticks) /
2858 if(ist->
dts != AV_NOPTS_VALUE && duration_dts) {
2865 if (duration_pts > 0) {
2866 ist->
next_pts += av_rescale_q(duration_pts, ist->
st->time_base, AV_TIME_BASE_Q);
2872 case AVMEDIA_TYPE_SUBTITLE:
2876 if (!pkt &&
ret >= 0)
2883 if (
ret == AVERROR_EOF) {
2889 if (decode_failed) {
2890 av_log(NULL, AV_LOG_ERROR,
"Error while decoding stream #%d:%d: %s\n",
2893 av_log(NULL, AV_LOG_FATAL,
"Error while processing the decoded "
2894 "data for stream #%d:%d\n", ist->
file_index, ist->
st->index);
2926 av_log(NULL, AV_LOG_FATAL,
"Error marking filters as finished\n");
2934 switch (ist->
dec_ctx->codec_type) {
2935 case AVMEDIA_TYPE_AUDIO:
2936 av_assert1(pkt->duration >= 0);
2937 if (ist->
dec_ctx->sample_rate) {
2941 ist->
next_dts += av_rescale_q(pkt->duration, ist->
st->time_base, AV_TIME_BASE_Q);
2944 case AVMEDIA_TYPE_VIDEO:
2947 AVRational time_base_q = AV_TIME_BASE_Q;
2950 }
else if (pkt->duration) {
2951 ist->
next_dts += av_rescale_q(pkt->duration, ist->
st->time_base, AV_TIME_BASE_Q);
2952 }
else if(ist->
dec_ctx->framerate.num != 0) {
2953 int ticks= av_stream_get_parser(ist->
st) ? av_stream_get_parser(ist->
st)->repeat_pict + 1 : ist->
dec_ctx->ticks_per_frame;
2954 ist->
next_dts += ((int64_t)AV_TIME_BASE *
2955 ist->
dec_ctx->framerate.den * ticks) /
2972 return !eof_reached;
2980 AVIOContext *sdp_pb;
2981 AVFormatContext **avc;
2992 if (!strcmp(
output_files[i]->ctx->oformat->name,
"rtp")) {
3001 av_sdp_create(avc, j, sdp,
sizeof(sdp));
3008 av_log(NULL, AV_LOG_ERROR,
"Failed to open sdp file '%s'\n",
sdp_filename);
3010 avio_print(sdp_pb, sdp);
3011 avio_closep(&sdp_pb);
3020 static enum AVPixelFormat
get_format(AVCodecContext *s,
const enum AVPixelFormat *pix_fmts)
3023 const enum AVPixelFormat *p;
3026 for (p = pix_fmts; *p != AV_PIX_FMT_NONE; p++) {
3027 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(*p);
3028 const AVCodecHWConfig *config = NULL;
3031 if (!(desc->flags & AV_PIX_FMT_FLAG_HWACCEL))
3037 config = avcodec_get_hw_config(s->codec, i);
3040 if (!(config->methods &
3041 AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX))
3043 if (config->pix_fmt == *p)
3056 av_log(NULL, AV_LOG_FATAL,
3057 "%s hwaccel requested for input stream #%d:%d, "
3058 "but cannot be initialized.\n",
3059 av_hwdevice_get_type_name(config->device_type),
3061 return AV_PIX_FMT_NONE;
3066 const HWAccel *hwaccel = NULL;
3085 av_log(NULL, AV_LOG_FATAL,
3086 "%s hwaccel requested for input stream #%d:%d, "
3087 "but cannot be initialized.\n", hwaccel->
name,
3089 return AV_PIX_FMT_NONE;
3095 if (!s->hw_frames_ctx)
3096 return AV_PIX_FMT_NONE;
3106 static int get_buffer(AVCodecContext *s, AVFrame *frame,
int flags)
3113 return avcodec_default_get_buffer2(s, frame, flags);
3122 AVCodec *codec = ist->
dec;
3124 snprintf(error, error_len,
"Decoder (codec %s) not found for input stream #%d:%d",
3126 return AVERROR(EINVAL);
3132 #if LIBAVCODEC_VERSION_MAJOR < 60
3133 ist->
dec_ctx->thread_safe_callbacks = 1;
3136 av_opt_set_int(ist->
dec_ctx,
"refcounted_frames", 1, 0);
3137 if (ist->
dec_ctx->codec_id == AV_CODEC_ID_DVB_SUBTITLE &&
3139 av_dict_set(&ist->
decoder_opts,
"compute_edt",
"1", AV_DICT_DONT_OVERWRITE);
3141 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");
3144 av_dict_set(&ist->
decoder_opts,
"sub_text_format",
"ass", AV_DICT_DONT_OVERWRITE);
3148 ist->
dec_ctx->pkt_timebase = ist->
st->time_base;
3150 if (!av_dict_get(ist->
decoder_opts,
"threads", NULL, 0))
3153 if (ist->
st->disposition & AV_DISPOSITION_ATTACHED_PIC)
3158 snprintf(error, error_len,
"Device setup failed for "
3159 "decoder on input stream #%d:%d : %s",
3165 if (
ret == AVERROR_EXPERIMENTAL)
3168 snprintf(error, error_len,
3169 "Error while opening decoder for input stream "
3192 return FFDIFFSIGN(*(
const int64_t *)a, *(
const int64_t *)b);
3200 for (i = 0; i < of->
ctx->nb_streams; i++) {
3208 ret = avformat_write_header(of->
ctx, &of->
opts);
3210 av_log(NULL, AV_LOG_ERROR,
3211 "Could not write header for output file #%d "
3212 "(incorrect codec parameters ?): %s\n",
3226 for (i = 0; i < of->
ctx->nb_streams; i++) {
3235 av_fifo_generic_read(ost->
muxing_queue, &pkt,
sizeof(pkt), NULL);
3246 AVBSFContext *ctx = ost->
bsf_ctx;
3252 ret = avcodec_parameters_copy(ctx->par_in, ost->
st->codecpar);
3256 ctx->time_base_in = ost->
st->time_base;
3258 ret = av_bsf_init(ctx);
3260 av_log(NULL, AV_LOG_ERROR,
"Error initializing bitstream filter: %s\n",
3265 ret = avcodec_parameters_copy(ost->
st->codecpar, ctx->par_out);
3269 ost->
st->time_base = ctx->time_base_out;
3278 AVCodecParameters *par_dst = ost->
st->codecpar;
3279 AVCodecParameters *par_src = ost->
ref_par;
3282 uint32_t codec_tag = par_dst->codec_tag;
3284 av_assert0(ist && !ost->
filter);
3286 ret = avcodec_parameters_to_context(ost->
enc_ctx, ist->
st->codecpar);
3290 av_log(NULL, AV_LOG_FATAL,
3291 "Error setting up codec context options.\n");
3295 ret = avcodec_parameters_from_context(par_src, ost->
enc_ctx);
3297 av_log(NULL, AV_LOG_FATAL,
3298 "Error getting reference codec parameters.\n");
3303 unsigned int codec_tag_tmp;
3304 if (!of->
ctx->oformat->codec_tag ||
3305 av_codec_get_id (of->
ctx->oformat->codec_tag, par_src->codec_tag) == par_src->codec_id ||
3306 !av_codec_get_tag2(of->
ctx->oformat->codec_tag, par_src->codec_id, &codec_tag_tmp))
3307 codec_tag = par_src->codec_tag;
3310 ret = avcodec_parameters_copy(par_dst, par_src);
3314 par_dst->codec_tag = codec_tag;
3320 ret = avformat_transfer_internal_stream_timing_info(of->
ctx->oformat, ost->
st, ist->
st,
copy_tb);
3325 if (ost->
st->time_base.num <= 0 || ost->
st->time_base.den <= 0)
3326 ost->
st->time_base = av_add_q(av_stream_get_codec_timebase(ost->
st), (AVRational){0, 1});
3329 if (ost->
st->duration <= 0 && ist->
st->duration > 0)
3330 ost->
st->duration = av_rescale_q(ist->
st->duration, ist->
st->time_base, ost->
st->time_base);
3333 ost->
st->disposition = ist->
st->disposition;
3335 if (ist->
st->nb_side_data) {
3336 for (i = 0; i < ist->
st->nb_side_data; i++) {
3337 const AVPacketSideData *sd_src = &ist->
st->side_data[i];
3340 dst_data = av_stream_new_side_data(ost->
st, sd_src->type, sd_src->size);
3342 return AVERROR(ENOMEM);
3343 memcpy(dst_data, sd_src->data, sd_src->size);
3348 uint8_t *sd = av_stream_new_side_data(ost->
st, AV_PKT_DATA_DISPLAYMATRIX,
3349 sizeof(int32_t) * 9);
3354 switch (par_dst->codec_type) {
3355 case AVMEDIA_TYPE_AUDIO:
3357 av_log(NULL, AV_LOG_FATAL,
"-acodec copy and -vol are incompatible (frames are not decoded)\n");
3360 if((par_dst->block_align == 1 || par_dst->block_align == 1152 || par_dst->block_align == 576) && par_dst->codec_id == AV_CODEC_ID_MP3)
3361 par_dst->block_align= 0;
3362 if(par_dst->codec_id == AV_CODEC_ID_AC3)
3363 par_dst->block_align= 0;
3365 case AVMEDIA_TYPE_VIDEO:
3369 (AVRational){ par_dst->height, par_dst->width });
3370 av_log(NULL, AV_LOG_WARNING,
"Overriding aspect ratio "
3371 "with stream copy may produce invalid files\n");
3373 else if (ist->
st->sample_aspect_ratio.num)
3374 sar = ist->
st->sample_aspect_ratio;
3376 sar = par_src->sample_aspect_ratio;
3377 ost->
st->sample_aspect_ratio = par_dst->sample_aspect_ratio = sar;
3378 ost->
st->avg_frame_rate = ist->
st->avg_frame_rate;
3379 ost->
st->r_frame_rate = ist->
st->r_frame_rate;
3390 AVDictionaryEntry *e;
3392 uint8_t *encoder_string;
3393 int encoder_string_len;
3394 int format_flags = 0;
3395 int codec_flags = ost->
enc_ctx->flags;
3397 if (av_dict_get(ost->
st->metadata,
"encoder", NULL, 0))
3400 e = av_dict_get(of->
opts,
"fflags", NULL, 0);
3402 const AVOption *o = av_opt_find(of->
ctx,
"fflags", NULL, 0, 0);
3405 av_opt_eval_flags(of->
ctx, o, e->value, &format_flags);
3409 const AVOption *o = av_opt_find(ost->
enc_ctx,
"flags", NULL, 0, 0);
3412 av_opt_eval_flags(ost->
enc_ctx, o, e->value, &codec_flags);
3415 encoder_string_len =
sizeof(LIBAVCODEC_IDENT) + strlen(ost->
enc->name) + 2;
3416 encoder_string = av_mallocz(encoder_string_len);
3417 if (!encoder_string)
3420 if (!(format_flags & AVFMT_FLAG_BITEXACT) && !(codec_flags & AV_CODEC_FLAG_BITEXACT))
3421 av_strlcpy(encoder_string, LIBAVCODEC_IDENT
" ", encoder_string_len);
3423 av_strlcpy(encoder_string,
"Lavc ", encoder_string_len);
3424 av_strlcat(encoder_string, ost->
enc->name, encoder_string_len);
3425 av_dict_set(&ost->
st->metadata,
"encoder", encoder_string,
3426 AV_DICT_DONT_STRDUP_VAL | AV_DICT_DONT_OVERWRITE);
3430 AVCodecContext *avctx)
3433 int n = 1, i, size, index = 0;
3436 for (p = kf; *p; p++)
3440 pts = av_malloc_array(size,
sizeof(*
pts));
3442 av_log(NULL, AV_LOG_FATAL,
"Could not allocate forced key frames array.\n");
3447 for (i = 0; i < n; i++) {
3448 char *next = strchr(p,
',');
3453 if (!memcmp(p,
"chapters", 8)) {
3458 if (avf->nb_chapters > INT_MAX - size ||
3459 !(
pts = av_realloc_f(
pts, size += avf->nb_chapters - 1,
3461 av_log(NULL, AV_LOG_FATAL,
3462 "Could not allocate forced key frames array.\n");
3466 t = av_rescale_q(t, AV_TIME_BASE_Q, avctx->time_base);
3468 for (j = 0; j < avf->nb_chapters; j++) {
3469 AVChapter *c = avf->chapters[j];
3470 av_assert1(index < size);
3471 pts[index++] = av_rescale_q(c->start, c->time_base,
3472 avctx->time_base) + t;
3478 av_assert1(index < size);
3479 pts[index++] = av_rescale_q(t, AV_TIME_BASE_Q, avctx->time_base);
3486 av_assert0(index == size);
3495 AVCodecContext *enc_ctx = ost->
enc_ctx;
3496 AVFormatContext *oc;
3505 enc_ctx->time_base = ist->
st->time_base;
3510 av_log(oc, AV_LOG_WARNING,
"Input stream data not available, using default time base\n");
3513 enc_ctx->time_base = default_time_base;
3519 AVCodecContext *enc_ctx = ost->
enc_ctx;
3520 AVCodecContext *
dec_ctx = NULL;
3529 av_dict_set(&ost->
st->metadata,
"rotate", NULL, 0);
3532 ost->
st->disposition = ist->
st->disposition;
3536 enc_ctx->chroma_sample_location =
dec_ctx->chroma_sample_location;
3538 for (j = 0; j < oc->nb_streams; j++) {
3539 AVStream *
st = oc->streams[j];
3540 if (
st != ost->
st &&
st->codecpar->codec_type == ost->
st->codecpar->codec_type)
3543 if (j == oc->nb_streams)
3544 if (ost->
st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO ||
3545 ost->
st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
3546 ost->
st->disposition = AV_DISPOSITION_DEFAULT;
3549 if (enc_ctx->codec_type == AVMEDIA_TYPE_VIDEO) {
3558 av_log(NULL, AV_LOG_WARNING,
3560 "about the input framerate is available. Falling "
3561 "back to a default value of 25fps for output stream #%d:%d. Use the -r option "
3562 "if you want a different framerate.\n",
3571 if (ost->
enc->supported_framerates && !ost->
force_fps) {
3572 int idx = av_find_nearest_q_idx(ost->
frame_rate, ost->
enc->supported_framerates);
3576 if (enc_ctx->codec_id == AV_CODEC_ID_MPEG4) {
3582 switch (enc_ctx->codec_type) {
3583 case AVMEDIA_TYPE_AUDIO:
3584 enc_ctx->sample_fmt = av_buffersink_get_format(ost->
filter->
filter);
3586 enc_ctx->bits_per_raw_sample = FFMIN(
dec_ctx->bits_per_raw_sample,
3587 av_get_bytes_per_sample(enc_ctx->sample_fmt) << 3);
3588 enc_ctx->sample_rate = av_buffersink_get_sample_rate(ost->
filter->
filter);
3589 enc_ctx->channel_layout = av_buffersink_get_channel_layout(ost->
filter->
filter);
3590 enc_ctx->channels = av_buffersink_get_channels(ost->
filter->
filter);
3595 case AVMEDIA_TYPE_VIDEO:
3598 if (!(enc_ctx->time_base.num && enc_ctx->time_base.den))
3599 enc_ctx->time_base = av_buffersink_get_time_base(ost->
filter->
filter);
3602 av_log(oc, AV_LOG_WARNING,
"Frame rate very high for a muxer not efficiently supporting it.\n"
3603 "Please consider specifying a lower framerate, a different muxer or -vsync 2\n");
3606 enc_ctx->width = av_buffersink_get_w(ost->
filter->
filter);
3607 enc_ctx->height = av_buffersink_get_h(ost->
filter->
filter);
3608 enc_ctx->sample_aspect_ratio = ost->
st->sample_aspect_ratio =
3611 av_buffersink_get_sample_aspect_ratio(ost->
filter->
filter);
3613 enc_ctx->pix_fmt = av_buffersink_get_format(ost->
filter->
filter);
3615 enc_ctx->bits_per_raw_sample = FFMIN(
dec_ctx->bits_per_raw_sample,
3616 av_pix_fmt_desc_get(enc_ctx->pix_fmt)->comp[0].depth);
3619 enc_ctx->color_range = frame->color_range;
3620 enc_ctx->color_primaries = frame->color_primaries;
3621 enc_ctx->color_trc = frame->color_trc;
3622 enc_ctx->colorspace = frame->colorspace;
3623 enc_ctx->chroma_sample_location = frame->chroma_location;
3631 enc_ctx->width !=
dec_ctx->width ||
3632 enc_ctx->height !=
dec_ctx->height ||
3633 enc_ctx->pix_fmt !=
dec_ctx->pix_fmt) {
3638 enc_ctx->field_order = AV_FIELD_BB;
3640 enc_ctx->field_order = AV_FIELD_TT;
3644 if (enc_ctx->flags & (AV_CODEC_FLAG_INTERLACED_DCT | AV_CODEC_FLAG_INTERLACED_ME) &&
3648 if (frame->interlaced_frame) {
3649 if (enc_ctx->codec->id == AV_CODEC_ID_MJPEG)
3650 enc_ctx->field_order = frame->top_field_first ? AV_FIELD_TT:AV_FIELD_BB;
3652 enc_ctx->field_order = frame->top_field_first ? AV_FIELD_TB:AV_FIELD_BT;
3654 enc_ctx->field_order = AV_FIELD_PROGRESSIVE;
3662 av_log(NULL, AV_LOG_ERROR,
3678 case AVMEDIA_TYPE_SUBTITLE:
3679 enc_ctx->time_base = AV_TIME_BASE_Q;
3680 if (!enc_ctx->width) {
3685 case AVMEDIA_TYPE_DATA:
3702 AVCodec *codec = ost->
enc;
3703 AVCodecContext *
dec = NULL;
3712 if (
dec &&
dec->subtitle_header) {
3714 ost->
enc_ctx->subtitle_header = av_mallocz(
dec->subtitle_header_size + 1);
3715 if (!ost->
enc_ctx->subtitle_header)
3716 return AVERROR(ENOMEM);
3717 memcpy(ost->
enc_ctx->subtitle_header,
dec->subtitle_header,
dec->subtitle_header_size);
3718 ost->
enc_ctx->subtitle_header_size =
dec->subtitle_header_size;
3720 if (!av_dict_get(ost->
encoder_opts,
"threads", NULL, 0))
3722 if (ost->
enc->type == AVMEDIA_TYPE_AUDIO &&
3730 snprintf(error, error_len,
"Device setup failed for "
3731 "encoder on output stream #%d:%d : %s",
3736 if (ist && ist->
dec->type == AVMEDIA_TYPE_SUBTITLE && ost->
enc->type == AVMEDIA_TYPE_SUBTITLE) {
3737 int input_props = 0, output_props = 0;
3738 AVCodecDescriptor
const *input_descriptor =
3739 avcodec_descriptor_get(
dec->codec_id);
3740 AVCodecDescriptor
const *output_descriptor =
3741 avcodec_descriptor_get(ost->
enc_ctx->codec_id);
3742 if (input_descriptor)
3743 input_props = input_descriptor->props & (AV_CODEC_PROP_TEXT_SUB | AV_CODEC_PROP_BITMAP_SUB);
3744 if (output_descriptor)
3745 output_props = output_descriptor->props & (AV_CODEC_PROP_TEXT_SUB | AV_CODEC_PROP_BITMAP_SUB);
3746 if (input_props && output_props && input_props != output_props) {
3747 snprintf(error, error_len,
3748 "Subtitle encoding currently only possible from text to text "
3749 "or bitmap to bitmap");
3750 return AVERROR_INVALIDDATA;
3755 if (
ret == AVERROR_EXPERIMENTAL)
3757 snprintf(error, error_len,
3758 "Error while opening encoder for output stream #%d:%d - "
3759 "maybe incorrect parameters such as bit_rate, rate, width or height",
3763 if (ost->
enc->type == AVMEDIA_TYPE_AUDIO &&
3764 !(ost->
enc->capabilities & AV_CODEC_CAP_VARIABLE_FRAME_SIZE))
3769 ost->
enc_ctx->codec_id != AV_CODEC_ID_CODEC2 )
3770 av_log(NULL, AV_LOG_WARNING,
"The bitrate parameter is set too low."
3771 " It takes bits/s as argument, not kbits/s\n");
3773 ret = avcodec_parameters_from_context(ost->
st->codecpar, ost->
enc_ctx);
3775 av_log(NULL, AV_LOG_FATAL,
3776 "Error initializing the output stream codec context.\n");
3780 if (ost->
enc_ctx->nb_coded_side_data) {
3783 for (i = 0; i < ost->
enc_ctx->nb_coded_side_data; i++) {
3784 const AVPacketSideData *sd_src = &ost->
enc_ctx->coded_side_data[i];
3787 dst_data = av_stream_new_side_data(ost->
st, sd_src->type, sd_src->size);
3789 return AVERROR(ENOMEM);
3790 memcpy(dst_data, sd_src->data, sd_src->size);
3803 for (i = 0; i < ist->
st->nb_side_data; i++) {
3804 AVPacketSideData *sd = &ist->
st->side_data[i];
3805 if (sd->type != AV_PKT_DATA_CPB_PROPERTIES) {
3806 uint8_t *dst = av_stream_new_side_data(ost->
st, sd->type, sd->size);
3808 return AVERROR(ENOMEM);
3809 memcpy(dst, sd->data, sd->size);
3810 if (ist->
autorotate && sd->type == AV_PKT_DATA_DISPLAYMATRIX)
3811 av_display_rotation_set((uint32_t *)dst, 0);
3817 if (ost->
st->time_base.num <= 0 || ost->
st->time_base.den <= 0)
3818 ost->
st->time_base = av_add_q(ost->
enc_ctx->time_base, (AVRational){0, 1});
3821 if (ost->
st->duration <= 0 && ist && ist->
st->duration > 0)
3822 ost->
st->duration = av_rescale_q(ist->
st->duration, ist->
st->time_base, ost->
st->time_base);
3831 static const AVOption opts[] = {
3832 {
"disposition" , NULL, 0, AV_OPT_TYPE_FLAGS, { .i64 = 0 }, INT64_MIN, (double)INT64_MAX, .unit =
"flags" },
3833 {
"default" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_DEFAULT }, .unit =
"flags" },
3834 {
"dub" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_DUB }, .unit =
"flags" },
3835 {
"original" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_ORIGINAL }, .unit =
"flags" },
3836 {
"comment" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_COMMENT }, .unit =
"flags" },
3837 {
"lyrics" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_LYRICS }, .unit =
"flags" },
3838 {
"karaoke" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_KARAOKE }, .unit =
"flags" },
3839 {
"forced" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_FORCED }, .unit =
"flags" },
3840 {
"hearing_impaired" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_HEARING_IMPAIRED }, .unit =
"flags" },
3841 {
"visual_impaired" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_VISUAL_IMPAIRED }, .unit =
"flags" },
3842 {
"clean_effects" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_CLEAN_EFFECTS }, .unit =
"flags" },
3843 {
"attached_pic" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_ATTACHED_PIC }, .unit =
"flags" },
3844 {
"captions" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_CAPTIONS }, .unit =
"flags" },
3845 {
"descriptions" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_DESCRIPTIONS }, .unit =
"flags" },
3846 {
"dependent" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_DEPENDENT }, .unit =
"flags" },
3847 {
"metadata" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_METADATA }, .unit =
"flags" },
3850 static const AVClass
class = {
3852 .item_name = av_default_item_name,
3854 .version = LIBAVUTIL_VERSION_INT,
3856 const AVClass *pclass = &
class;
3858 ret = av_opt_eval_flags(&pclass, &opts[0], ost->
disposition, &ost->
st->disposition);
3882 AVStream *st = file->
ctx->streams[pkt->stream_index];
3886 av_log(file->
ctx, AV_LOG_WARNING,
3887 "New %s stream %d:%d at pos:%"PRId64
" and DTS:%ss\n",
3888 av_get_media_type_string(st->codecpar->codec_type),
3889 input_index, pkt->stream_index,
3890 pkt->pos, av_ts2timestr(pkt->dts, &st->time_base));
3896 int ret = 0, i, j, k;
3897 AVFormatContext *oc;
3900 char error[1024] = {0};
3958 for (j = 0; j < ifile->
ctx->nb_programs; j++) {
3959 AVProgram *p = ifile->
ctx->programs[j];
3960 int discard = AVDISCARD_ALL;
3962 for (k = 0; k < p->nb_stream_indexes; k++)
3964 discard = AVDISCARD_DEFAULT;
3967 p->discard = discard;
3974 if (oc->oformat->flags & AVFMT_NOSTREAMS && oc->nb_streams == 0) {
3983 av_log(NULL, AV_LOG_INFO,
"Stream mapping:\n");
3989 av_log(NULL, AV_LOG_INFO,
" Stream #%d:%d (%s) -> %s",
3994 av_log(NULL, AV_LOG_INFO,
"\n");
4004 av_log(NULL, AV_LOG_INFO,
" File %s -> Stream #%d:%d\n",
4011 av_log(NULL, AV_LOG_INFO,
" %s", ost->
filter->
name);
4015 av_log(NULL, AV_LOG_INFO,
" -> Stream #%d:%d (%s)\n", ost->
file_index,
4020 av_log(NULL, AV_LOG_INFO,
" Stream #%d:%d -> #%d:%d",
4026 av_log(NULL, AV_LOG_INFO,
" [sync #%d:%d]",
4030 av_log(NULL, AV_LOG_INFO,
" (copy)");
4033 const AVCodec *out_codec = ost->
enc;
4034 const char *decoder_name =
"?";
4035 const char *in_codec_name =
"?";
4036 const char *encoder_name =
"?";
4037 const char *out_codec_name =
"?";
4038 const AVCodecDescriptor *desc;
4041 decoder_name = in_codec->name;
4042 desc = avcodec_descriptor_get(in_codec->id);
4044 in_codec_name = desc->name;
4045 if (!strcmp(decoder_name, in_codec_name))
4046 decoder_name =
"native";
4050 encoder_name = out_codec->name;
4051 desc = avcodec_descriptor_get(out_codec->id);
4053 out_codec_name = desc->name;
4054 if (!strcmp(encoder_name, out_codec_name))
4055 encoder_name =
"native";
4058 av_log(NULL, AV_LOG_INFO,
" (%s (%s) -> %s (%s))",
4059 in_codec_name, decoder_name,
4060 out_codec_name, encoder_name);
4062 av_log(NULL, AV_LOG_INFO,
"\n");
4066 av_log(NULL, AV_LOG_ERROR,
"%s\n", error);
4090 for (j = 0; j < of->
ctx->nb_streams; j++)
4109 int64_t opts_min = INT64_MAX;
4114 int64_t opts = ost->
st->cur_dts == AV_NOPTS_VALUE ? INT64_MIN :
4115 av_rescale_q(ost->
st->cur_dts, ost->
st->time_base,
4117 if (ost->
st->cur_dts == AV_NOPTS_VALUE)
4118 av_log(NULL, AV_LOG_DEBUG,
4119 "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",
4125 if (!ost->
finished && opts < opts_min) {
4137 if (tcgetattr(0, &tty) == 0) {
4138 if (on) tty.c_lflag |= ECHO;
4139 else tty.c_lflag &= ~ECHO;
4140 tcsetattr(0, TCSANOW, &tty);
4149 return AVERROR_EXIT;
4157 return AVERROR_EXIT;
4158 if (key ==
'+') av_log_set_level(av_log_get_level()+10);
4159 if (key ==
'-') av_log_set_level(av_log_get_level()-10);
4168 av_log_set_level(AV_LOG_DEBUG);
4170 if (key ==
'c' || key ==
'C'){
4171 char buf[4096], target[64], command[256], arg[256] = {0};
4174 fprintf(stderr,
"\nEnter command: <target>|all <time>|-1 <command>[ <argument>]\n");
4177 while ((k =
read_key()) !=
'\n' && k !=
'\r' && i <
sizeof(buf)-1)
4182 fprintf(stderr,
"\n");
4184 (n = sscanf(buf,
"%63[^ ] %lf %255[^ ] %255[^\n]", target, &time, command, arg)) >= 3) {
4185 av_log(NULL, AV_LOG_DEBUG,
"Processing command target:%s time:%f command:%s arg:%s",
4186 target, time, command, arg);
4191 ret = avfilter_graph_send_command(fg->
graph, target, command, arg, buf,
sizeof(buf),
4192 key ==
'c' ? AVFILTER_CMD_FLAG_ONE : 0);
4193 fprintf(stderr,
"Command reply for stream %d: ret:%d res:\n%s", i, ret, buf);
4194 }
else if (key ==
'c') {
4195 fprintf(stderr,
"Queuing commands only on filters supporting the specific command is unsupported\n");
4196 ret = AVERROR_PATCHWELCOME;
4198 ret = avfilter_graph_queue_command(fg->
graph, target, command, arg, 0, time);
4200 fprintf(stderr,
"Queuing command failed with error %s\n", av_err2str(ret));
4205 av_log(NULL, AV_LOG_ERROR,
4206 "Parse error, at least 3 arguments were expected, "
4207 "only %d given in string '%s'\n", n, buf);
4210 if (key ==
'd' || key ==
'D'){
4214 if(!debug) debug = 1;
4215 while (debug & FF_DEBUG_DCT_COEFF)
4222 while ((k =
read_key()) !=
'\n' && k !=
'\r' && i <
sizeof(buf)-1)
4227 fprintf(stderr,
"\n");
4228 if (k <= 0 || sscanf(buf,
"%d", &debug)!=1)
4229 fprintf(stderr,
"error parsing debug value\n");
4238 if(debug) av_log_set_level(AV_LOG_DEBUG);
4239 fprintf(stderr,
"debug=%d\n", debug);
4242 fprintf(stderr,
"key function\n"
4243 "? show this help\n"
4244 "+ increase verbosity\n"
4245 "- decrease verbosity\n"
4246 "c Send command to first matching filter supporting it\n"
4247 "C Send/Queue command to all matching filters\n"
4248 "D cycle through available debug modes\n"
4249 "h dump packets/hex press to cycle through the 3 states\n"
4251 "s Show QP histogram\n"
4258 static void *input_thread(
void *arg)
4261 unsigned flags = f->non_blocking ? AV_THREAD_MESSAGE_NONBLOCK : 0;
4266 ret = av_read_frame(f->
ctx, &pkt);
4268 if (ret == AVERROR(EAGAIN)) {
4273 av_thread_message_queue_set_err_recv(f->in_thread_queue, ret);
4276 ret = av_thread_message_queue_send(f->in_thread_queue, &pkt, flags);
4277 if (flags && ret == AVERROR(EAGAIN)) {
4279 ret = av_thread_message_queue_send(f->in_thread_queue, &pkt, flags);
4280 av_log(f->
ctx, AV_LOG_WARNING,
4281 "Thread message queue blocking; consider raising the "
4282 "thread_queue_size option (current value: %d)\n",
4283 f->thread_queue_size);
4286 if (ret != AVERROR_EOF)
4287 av_log(f->
ctx, AV_LOG_ERROR,
4288 "Unable to send packet to main thread: %s\n",
4290 av_packet_unref(&pkt);
4291 av_thread_message_queue_set_err_recv(f->in_thread_queue, ret);
4299 static void free_input_thread(
int i)
4304 if (!f || !f->in_thread_queue)
4306 av_thread_message_queue_set_err_send(f->in_thread_queue, AVERROR_EOF);
4307 while (av_thread_message_queue_recv(f->in_thread_queue, &pkt, 0) >= 0)
4308 av_packet_unref(&pkt);
4310 pthread_join(f->thread, NULL);
4312 av_thread_message_queue_free(&f->in_thread_queue);
4315 static void free_input_threads(
void)
4320 free_input_thread(i);
4323 static int init_input_thread(
int i)
4328 if (f->thread_queue_size < 0)
4330 if (!f->thread_queue_size)
4333 if (f->
ctx->pb ? !f->
ctx->pb->seekable :
4334 strcmp(f->
ctx->iformat->name,
"lavfi"))
4335 f->non_blocking = 1;
4336 ret = av_thread_message_queue_alloc(&f->in_thread_queue,
4337 f->thread_queue_size,
sizeof(AVPacket));
4341 if ((ret = pthread_create(&f->thread, NULL, input_thread, f))) {
4342 av_log(NULL, AV_LOG_ERROR,
"pthread_create failed: %s. Try to increase `ulimit -v` or decrease `ulimit -s`.\n", strerror(ret));
4343 av_thread_message_queue_free(&f->in_thread_queue);
4344 return AVERROR(ret);
4350 static int init_input_threads(
void)
4355 ret = init_input_thread(i);
4362 static int get_input_packet_mt(
InputFile *f, AVPacket *pkt)
4364 return av_thread_message_queue_recv(f->in_thread_queue, pkt,
4366 AV_THREAD_MESSAGE_NONBLOCK : 0);
4376 int64_t pts = av_rescale(ist->
dts, 1000000, AV_TIME_BASE);
4377 int64_t now = av_gettime_relative() - ist->
start;
4379 return AVERROR(EAGAIN);
4384 if (f->thread_queue_size)
4385 return get_input_packet_mt(f, pkt);
4387 return av_read_frame(f->
ctx, pkt);
4409 static AVRational
duration_max(int64_t tmp, int64_t *duration, AVRational tmp_time_base,
4410 AVRational time_base)
4416 return tmp_time_base;
4419 ret = av_compare_ts(*duration, time_base, tmp, tmp_time_base);
4422 return tmp_time_base;
4431 AVCodecContext *avctx;
4432 int i, ret, has_audio = 0;
4433 int64_t duration = 0;
4435 ret = avformat_seek_file(is, -1, INT64_MIN, is->start_time, is->start_time, 0);
4446 if (avctx->codec_type == AVMEDIA_TYPE_AUDIO && ist->
nb_samples)
4455 if (avctx->codec_type == AVMEDIA_TYPE_AUDIO && ist->
nb_samples) {
4464 duration = av_rescale_q(1, av_inv_q(ist->
framerate), ist->
st->time_base);
4465 }
else if (ist->
st->avg_frame_rate.num) {
4466 duration = av_rescale_q(1, av_inv_q(ist->
st->avg_frame_rate), ist->
st->time_base);
4481 if (ifile->
loop > 0)
4497 AVFormatContext *is;
4500 int ret, thread_ret, i, j;
4503 int disable_discontinuity_correction =
copy_ts;
4508 if (ret == AVERROR(EAGAIN)) {
4512 if (ret < 0 && ifile->loop) {
4513 AVCodecContext *avctx;
4521 avcodec_flush_buffers(avctx);
4525 free_input_thread(file_index);
4529 thread_ret = init_input_thread(file_index);
4534 av_log(NULL, AV_LOG_WARNING,
"Seek to start failed.\n");
4537 if (ret == AVERROR(EAGAIN)) {
4543 if (ret != AVERROR_EOF) {
4568 return AVERROR(EAGAIN);
4574 av_pkt_dump_log2(NULL, AV_LOG_INFO, &pkt,
do_hex_dump,
4575 is->streams[pkt.stream_index]);
4581 goto discard_packet;
4590 goto discard_packet;
4592 if (pkt.flags & AV_PKT_FLAG_CORRUPT) {
4594 "%s: corrupt input packet in stream %d\n", is->url, pkt.stream_index);
4600 av_log(NULL, AV_LOG_INFO,
"demuxer -> ist_index:%d type:%s "
4601 "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",
4602 ifile->
ist_index + pkt.stream_index, av_get_media_type_string(ist->
dec_ctx->codec_type),
4605 av_ts2str(pkt.pts), av_ts2timestr(pkt.pts, &ist->
st->time_base),
4606 av_ts2str(pkt.dts), av_ts2timestr(pkt.dts, &ist->
st->time_base),
4612 int64_t stime, stime2;
4616 if ( ist->
next_dts == AV_NOPTS_VALUE
4618 && (is->iformat->flags & AVFMT_TS_DISCONT)) {
4619 int64_t new_start_time = INT64_MAX;
4620 for (i=0; i<is->nb_streams; i++) {
4621 AVStream *st = is->streams[i];
4622 if(st->discard == AVDISCARD_ALL || st->start_time == AV_NOPTS_VALUE)
4624 new_start_time = FFMIN(new_start_time, av_rescale_q(st->start_time, st->time_base, AV_TIME_BASE_Q));
4626 if (new_start_time > is->start_time) {
4627 av_log(is, AV_LOG_VERBOSE,
"Correcting start time by %"PRId64
"\n", new_start_time - is->start_time);
4632 stime = av_rescale_q(is->start_time, AV_TIME_BASE_Q, ist->
st->time_base);
4633 stime2= stime + (1ULL<<ist->
st->pts_wrap_bits);
4636 if(stime2 > stime && pkt.dts != AV_NOPTS_VALUE && pkt.dts > stime + (1LL<<(ist->
st->pts_wrap_bits-1))) {
4637 pkt.dts -= 1ULL<<ist->
st->pts_wrap_bits;
4640 if(stime2 > stime && pkt.pts != AV_NOPTS_VALUE && pkt.pts > stime + (1LL<<(ist->
st->pts_wrap_bits-1))) {
4641 pkt.pts -= 1ULL<<ist->
st->pts_wrap_bits;
4648 for (i = 0; i < ist->
st->nb_side_data; i++) {
4649 AVPacketSideData *src_sd = &ist->
st->side_data[i];
4652 if (src_sd->type == AV_PKT_DATA_DISPLAYMATRIX)
4655 if (av_packet_get_side_data(&pkt, src_sd->type, NULL))
4658 dst_data = av_packet_new_side_data(&pkt, src_sd->type, src_sd->size);
4662 memcpy(dst_data, src_sd->data, src_sd->size);
4666 if (pkt.dts != AV_NOPTS_VALUE)
4667 pkt.dts += av_rescale_q(ifile->
ts_offset, AV_TIME_BASE_Q, ist->
st->time_base);
4668 if (pkt.pts != AV_NOPTS_VALUE)
4669 pkt.pts += av_rescale_q(ifile->
ts_offset, AV_TIME_BASE_Q, ist->
st->time_base);
4671 if (pkt.pts != AV_NOPTS_VALUE)
4673 if (pkt.dts != AV_NOPTS_VALUE)
4676 pkt_dts = av_rescale_q_rnd(pkt.dts, ist->
st->time_base, AV_TIME_BASE_Q, AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX);
4677 if ((ist->
dec_ctx->codec_type == AVMEDIA_TYPE_VIDEO ||
4678 ist->
dec_ctx->codec_type == AVMEDIA_TYPE_AUDIO) &&
4679 pkt_dts != AV_NOPTS_VALUE && ist->
next_dts == AV_NOPTS_VALUE && !
copy_ts
4680 && (is->iformat->flags & AVFMT_TS_DISCONT) && ifile->
last_ts != AV_NOPTS_VALUE) {
4681 int64_t delta = pkt_dts - ifile->
last_ts;
4685 av_log(NULL, AV_LOG_DEBUG,
4686 "Inter stream timestamp discontinuity %"PRId64
", new offset= %"PRId64
"\n",
4688 pkt.dts -= av_rescale_q(delta, AV_TIME_BASE_Q, ist->
st->time_base);
4689 if (pkt.pts != AV_NOPTS_VALUE)
4690 pkt.pts -= av_rescale_q(delta, AV_TIME_BASE_Q, ist->
st->time_base);
4695 if (pkt.pts != AV_NOPTS_VALUE) {
4696 pkt.pts += duration;
4701 if (pkt.dts != AV_NOPTS_VALUE)
4702 pkt.dts += duration;
4704 pkt_dts = av_rescale_q_rnd(pkt.dts, ist->
st->time_base, AV_TIME_BASE_Q, AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX);
4706 if (
copy_ts && pkt_dts != AV_NOPTS_VALUE && ist->
next_dts != AV_NOPTS_VALUE &&
4707 (is->iformat->flags & AVFMT_TS_DISCONT) && ist->
st->pts_wrap_bits < 60) {
4708 int64_t wrap_dts = av_rescale_q_rnd(pkt.dts + (1LL<<ist->
st->pts_wrap_bits),
4709 ist->
st->time_base, AV_TIME_BASE_Q,
4710 AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX);
4712 disable_discontinuity_correction = 0;
4715 if ((ist->
dec_ctx->codec_type == AVMEDIA_TYPE_VIDEO ||
4716 ist->
dec_ctx->codec_type == AVMEDIA_TYPE_AUDIO) &&
4717 pkt_dts != AV_NOPTS_VALUE && ist->
next_dts != AV_NOPTS_VALUE &&
4718 !disable_discontinuity_correction) {
4719 int64_t delta = pkt_dts - ist->
next_dts;
4720 if (is->iformat->flags & AVFMT_TS_DISCONT) {
4723 pkt_dts + AV_TIME_BASE/10 < FFMAX(ist->
pts, ist->
dts)) {
4725 av_log(NULL, AV_LOG_DEBUG,
4726 "timestamp discontinuity for stream #%d:%d "
4727 "(id=%d, type=%s): %"PRId64
", new offset= %"PRId64
"\n",
4729 av_get_media_type_string(ist->
dec_ctx->codec_type),
4731 pkt.dts -= av_rescale_q(delta, AV_TIME_BASE_Q, ist->
st->time_base);
4732 if (pkt.pts != AV_NOPTS_VALUE)
4733 pkt.pts -= av_rescale_q(delta, AV_TIME_BASE_Q, ist->
st->time_base);
4738 av_log(NULL, AV_LOG_WARNING,
"DTS %"PRId64
", next:%"PRId64
" st:%d invalid dropping\n", pkt.dts, ist->
next_dts, pkt.stream_index);
4739 pkt.dts = AV_NOPTS_VALUE;
4741 if (pkt.pts != AV_NOPTS_VALUE){
4742 int64_t pkt_pts = av_rescale_q(pkt.pts, ist->
st->time_base, AV_TIME_BASE_Q);
4746 av_log(NULL, AV_LOG_WARNING,
"PTS %"PRId64
", next:%"PRId64
" invalid dropping st:%d\n", pkt.pts, ist->
next_dts, pkt.stream_index);
4747 pkt.pts = AV_NOPTS_VALUE;
4753 if (pkt.dts != AV_NOPTS_VALUE)
4754 ifile->
last_ts = av_rescale_q(pkt.dts, ist->
st->time_base, AV_TIME_BASE_Q);
4757 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",
4758 ifile->
ist_index + pkt.stream_index, av_get_media_type_string(ist->
dec_ctx->codec_type),
4759 av_ts2str(pkt.pts), av_ts2timestr(pkt.pts, &ist->
st->time_base),
4760 av_ts2str(pkt.dts), av_ts2timestr(pkt.dts, &ist->
st->time_base),
4770 av_packet_unref(&pkt);
4785 int nb_requests, nb_requests_max = 0;
4790 ret = avfilter_graph_request_oldest(graph->
graph);
4794 if (ret == AVERROR_EOF) {
4800 if (ret != AVERROR(EAGAIN))
4803 for (i = 0; i < graph->
nb_inputs; i++) {
4804 ifilter = graph->
inputs[i];
4809 nb_requests = av_buffersrc_get_nb_failed_requests(ifilter->
filter);
4810 if (nb_requests > nb_requests_max) {
4811 nb_requests_max = nb_requests;
4841 av_log(NULL, AV_LOG_VERBOSE,
"No more inputs to read from, finishing.\n");
4849 av_log(NULL, AV_LOG_ERROR,
"Error reinitializing filters!\n");
4877 if (av_buffersink_get_type(ost->
filter->
filter) == AVMEDIA_TYPE_AUDIO)
4884 }
else if (ost->
filter) {
4903 if (ret == AVERROR(EAGAIN)) {
4910 return ret == AVERROR_EOF ? 0 : ret;
4921 AVFormatContext *os;
4924 int64_t timer_start;
4925 int64_t total_packets_written = 0;
4932 av_log(NULL, AV_LOG_INFO,
"Press [q] to stop, [?] for help\n");
4935 timer_start = av_gettime_relative();
4938 if ((ret = init_input_threads()) < 0)
4943 int64_t cur_time= av_gettime_relative();
4952 av_log(NULL, AV_LOG_VERBOSE,
"No more output streams to write to, finishing.\n");
4957 if (ret < 0 && ret != AVERROR_EOF) {
4958 av_log(NULL, AV_LOG_ERROR,
"Error while filtering: %s\n", av_err2str(ret));
4966 free_input_threads();
4984 av_log(NULL, AV_LOG_ERROR,
4985 "Nothing was written into output file %d (%s), because "
4986 "at least one of its streams received no packets.\n",
4990 if ((ret = av_write_trailer(os)) < 0) {
4991 av_log(NULL, AV_LOG_ERROR,
"Error writing trailer of %s: %s\n", os->url, av_err2str(ret));
5004 av_freep(&ost->
enc_ctx->stats_in);
5008 av_log(NULL, AV_LOG_FATAL,
"Empty output on stream %d.\n", i);
5014 av_log(NULL, AV_LOG_FATAL,
"Empty output\n");
5035 free_input_threads();
5044 av_log(NULL, AV_LOG_ERROR,
5045 "Error closing logfile, loss of information possible: %s\n",
5046 av_err2str(AVERROR(errno)));
5050 av_freep(&ost->
apad);
5066 struct rusage rusage;
5068 getrusage(RUSAGE_SELF, &rusage);
5070 (rusage.ru_utime.tv_sec * 1000000LL) + rusage.ru_utime.tv_usec;
5072 (rusage.ru_stime.tv_sec * 1000000LL) + rusage.ru_stime.tv_usec;
5073 #elif HAVE_GETPROCESSTIMES
5075 FILETIME c, e, k, u;
5076 proc = GetCurrentProcess();
5077 GetProcessTimes(proc, &c, &e, &k, &u);
5079 ((int64_t)u.dwHighDateTime << 32 | u.dwLowDateTime) / 10;
5081 ((int64_t)k.dwHighDateTime << 32 | k.dwLowDateTime) / 10;
5090 #if HAVE_GETRUSAGE && HAVE_STRUCT_RUSAGE_RU_MAXRSS
5091 struct rusage rusage;
5092 getrusage(RUSAGE_SELF, &rusage);
5093 return (int64_t)rusage.ru_maxrss * 1024;
5094 #elif HAVE_GETPROCESSMEMORYINFO
5096 PROCESS_MEMORY_COUNTERS memcounters;
5097 proc = GetCurrentProcess();
5098 memcounters.cb =
sizeof(memcounters);
5099 GetProcessMemoryInfo(proc, &memcounters,
sizeof(memcounters));
5100 return memcounters.PeakPagefileUsage;
5164 char _program_name[] =
"ffmpeg";
5168 #define OFFSET(x) offsetof(OptionsContext, x)
5186 {
"bsfs",
OPT_EXIT, { .func_arg =
show_bsfs },
"show available bit stream filters" },
5195 {
"report", 0, { .func_arg =
opt_report },
"generate a report" },
5196 {
"max_alloc",
HAS_ARG, { .func_arg =
opt_max_alloc },
"set maximum size of a single allocated block",
"bytes" },
5202 "list sources of the input device",
"device" },
5204 "list sinks of the output device",
"device" },
5209 "force format",
"fmt" },
5211 "overwrite output files" },
5213 "never overwrite output files" },
5215 "Ignore unknown stream types" },
5217 "Copy unknown stream types" },
5220 "codec name",
"codec" },
5223 "codec name",
"codec" },
5226 "preset name",
"preset" },
5229 "set input stream mapping",
5230 "[-]input_file_id[:stream_specifier][,sync_file_id[:stream_specifier]]" },
5232 "map an audio channel from one stream to another",
"file.stream.channel[:syncfile.syncstream]" },
5235 "set metadata information of outfile from infile",
5236 "outfile[,metadata]:infile[,metadata]" },
5239 "set chapters mapping",
"input_file_index" },
5242 "record or transcode \"duration\" seconds of audio/video",
5245 "record or transcode stop time",
"time_stop" },
5247 "set the limit file size in bytes",
"limit_size" },
5250 "set the start time offset",
"time_off" },
5253 "set the start time offset relative to EOF",
"time_off" },
5256 "enable/disable seeking by timestamp with -ss" },
5259 "enable/disable accurate seeking with -ss" },
5262 "set the input ts offset",
"time_off" },
5265 "set the input ts scale",
"scale" },
5267 "set the recording timestamp ('now' to set the current time)",
"time" },
5269 "add metadata",
"string=string" },
5271 "add program with specified streams",
"title=string:st=number..." },
5274 "set the number of data frames to output",
"number" },
5276 "add timings for benchmarking" },
5278 "add timings for each task" },
5280 "write program-readable progress information",
"url" },
5282 "enable or disable interaction on standard input" },
5284 "set max runtime in seconds in CPU user time",
"limit" },
5286 "dump each input packet" },
5288 "when dumping packets, also dump the payload" },
5291 "read input at native frame rate",
"" },
5293 "specify target file type (\"vcd\", \"svcd\", \"dvd\", \"dv\" or \"dv50\" "
5294 "with optional prefixes \"pal-\", \"ntsc-\" or \"film-\")",
"type" },
5296 "video sync method",
"" },
5298 "frame drop threshold",
"" },
5300 "audio sync method",
"" },
5302 "audio drift threshold",
"threshold" },
5304 "copy timestamps" },
5306 "shift input timestamps to start at 0 when using copyts" },
5308 "copy input stream time base when stream copying",
"mode" },
5310 "shift input timestamps to start at 0 when using copyts" },
5312 "copy input stream time base when stream copying",
"mode" },
5315 "finish encoding within shortest input" },
5323 "timestamp discontinuity delta threshold",
"threshold" },
5325 "timestamp error delta threshold",
"threshold" },
5327 "exit on error",
"error" },
5329 "abort on the specified condition flags",
"flags" },
5332 "copy initial non-keyframes" },
5334 "copy or discard frames before start time" },
5336 "set the number of frames to output",
"number" },
5339 "force codec tag/fourcc",
"fourcc/tag" },
5342 "use fixed quality scale (VBR)",
"q" },
5345 "use fixed quality scale (VBR)",
"q" },
5347 "set profile",
"profile" },
5349 "set stream filtergraph",
"filter_graph" },
5351 "number of non-complex filter threads" },
5353 "read stream filtergraph description from a file",
"filename" },
5355 "reinit filtergraph on input parameter changes",
"" },
5357 "create a complex filtergraph",
"graph_description" },
5359 "number of threads for -filter_complex" },
5361 "create a complex filtergraph",
"graph_description" },
5363 "read complex filtergraph description from a file",
"filename" },
5365 "enable automatic conversion filters globally" },
5367 "print progress report during encoding", },
5369 "set the period at which ffmpeg updates stats and -progress output",
"time" },
5372 "add an attachment to the output file",
"filename" },
5375 "extract an attachment into a file",
"filename" },
5377 OPT_OFFSET, { .off =
OFFSET(loop) },
"set number of times input stream shall be looped",
"loop count" },
5379 "print timestamp debugging info" },
5381 "ratio of errors (0.0: no errors, 1.0: 100% errors) above which ffmpeg returns an error instead of success.",
"maximum error rate" },
5387 "disposition",
"" },
5389 { .off =
OFFSET(thread_queue_size) },
5390 "set the maximum number of queued packets from the demuxer" },
5392 "read and decode the streams to fill missing information with heuristics" },
5396 "set the number of video frames to output",
"number" },
5399 "set frame rate (Hz value, fraction or abbreviation)",
"rate" },
5402 "set frame size (WxH or abbreviation)",
"size" },
5405 "set aspect ratio (4:3, 16:9 or 1.3333, 1.7777)",
"aspect" },
5408 "set pixel format",
"format" },
5410 "set the number of bits per raw sample",
"number" },
5412 "deprecated use -g 1" },
5417 "rate control override for specific intervals",
"override" },
5420 "force video codec ('copy' to copy stream)",
"codec" },
5426 "set initial TimeCode value.",
"hh:mm:ss[:;.]ff" },
5428 "select the pass number (1 to 3)",
"n" },
5431 "select two pass log file name prefix",
"prefix" },
5433 "this option is deprecated, use the yadif filter instead" },
5435 "calculate PSNR of compressed frames" },
5437 "dump video coding statistics to file" },
5439 "dump video coding statistics to file",
"file" },
5441 "Version of the vstats format to use."},
5443 "set video filters",
"filter_graph" },
5446 "specify intra matrix coeffs",
"matrix" },
5449 "specify inter matrix coeffs",
"matrix" },
5452 "specify intra matrix coeffs",
"matrix" },
5455 "top=1/bottom=0/auto=-1 field first",
"" },
5458 "force video tag/fourcc",
"fourcc/tag" },
5460 "show QP histogram" },
5463 "force the selected framerate, disable the best supported framerate selection" },
5466 "set the value of an outfile streamid",
"streamIndex:value" },
5469 "force key frames at specified timestamps",
"timestamps" },
5471 "audio bitrate (please use -b:a)",
"bitrate" },
5473 "video bitrate (please use -b:v)",
"bitrate" },
5476 "use HW accelerated decoding",
"hwaccel name" },
5479 "select a device for HW acceleration",
"devicename" },
5482 "select output format used with HW accelerated decoding",
"format" },
5483 #if CONFIG_VIDEOTOOLBOX
5487 "show available HW acceleration methods" },
5490 "automatically insert correct rotate filters" },
5493 "automatically insert a scale filter at the end of the filter graph" },
5497 "set the number of audio frames to output",
"number" },
5499 "set audio quality (codec-specific)",
"quality", },
5502 "set audio sampling rate (in Hz)",
"rate" },
5505 "set number of audio channels",
"channels" },
5510 "force audio codec ('copy' to copy stream)",
"codec" },
5513 "force audio tag/fourcc",
"fourcc/tag" },
5515 "change audio volume (256=normal)" ,
"volume" },
5518 "set sample format",
"format" },
5521 "set channel layout",
"layout" },
5523 "set audio filters",
"filter_graph" },
5525 "set the maximum number of channels to try to guess the channel layout" },
5529 "disable subtitle" },
5531 "force subtitle codec ('copy' to copy stream)",
"codec" },
5533 ,
"force subtitle tag/fourcc",
"fourcc/tag" },
5535 "fix subtitles duration" },
5537 "set canvas size (WxH or abbreviation)",
"size" },
5541 "deprecated, use -channel",
"channel" },
5543 "deprecated, use -standard",
"standard" },
5548 "set the maximum demux-decode delay",
"seconds" },
5550 "set the initial demux-decode delay",
"seconds" },
5552 "specify a file in which to print sdp information",
"file" },
5555 "set the desired time base hint for output stream (1:24, 1:48000 or 0.04166, 2.0833e-5)",
"ratio" },
5557 "set the desired time base for the encoder (1:24, 1:48000 or 0.04166, 2.0833e-5). "
5558 "two special values are defined - "
5559 "0 = use frame rate (video) or sample rate (audio),"
5560 "-1 = match source time base",
"ratio" },
5563 "A comma-separated list of bitstream filters",
"bitstream_filters" },
5565 "deprecated",
"audio bitstream_filters" },
5567 "deprecated",
"video bitstream_filters" },
5570 "set the audio options to the indicated preset",
"preset" },
5572 "set the video options to the indicated preset",
"preset" },
5574 "set the subtitle options to the indicated preset",
"preset" },
5576 "set options from indicated preset file",
"filename" },
5579 "maximum number of packets that can be buffered while waiting for all streams to initialize",
"packets" },
5581 "set the threshold after which max_muxing_queue_size is taken into account",
"bytes" },
5585 "force data codec ('copy' to copy stream)",
"codec" },
5591 "set VAAPI hardware device (DRM path or X11 display name)",
"device" },
5596 "set QSV hardware device (DirectX adapter index, DRM path or X11 display name)",
"device"},
5600 "initialise hardware device",
"args" },
5602 "set hardware device used when filtering",
"device" },
5613 if (savedCode == 0) {
5621 setvbuf(stderr,NULL,_IONBF,0);
5623 av_log_set_flags(AV_LOG_SKIP_REPEATED);
5626 if(argc>1 && !strcmp(argv[1],
"-d")){
5634 avdevice_register_all();
5636 avformat_network_init();
5647 av_log(NULL, AV_LOG_WARNING,
"Use -h to get full help or, even better, run 'man %s'\n",
program_name);
5653 av_log(NULL, AV_LOG_FATAL,
"At least one output file must be specified\n");
5658 if (strcmp(
output_files[i]->ctx->oformat->name,
"rtp"))
5666 int64_t utime, stime, rtime;
5671 av_log(NULL, AV_LOG_INFO,
5672 "bench: utime=%0.3fs stime=%0.3fs rtime=%0.3fs\n",
5673 utime / 1000000.0, stime / 1000000.0, rtime / 1000000.0);
5675 av_log(NULL, AV_LOG_DEBUG,
"%"PRIu64
" frames successfully decoded, %"PRIu64
" decoding errors\n",
__thread jmp_buf ex_buf__
int(* init)(AVCodecContext *s)
int64_t start_time
start time in microseconds == AV_TIME_BASE units
int64_t recording_time
desired length of the resulting file in microseconds == AV_TIME_BASE units
struct OutputStream * ost
uint64_t * channel_layouts
struct FilterGraph * graph
int max_muxing_queue_size
int copy_initial_nonkeyframes
double forced_keyframes_expr_const_values[FKF_NB]
AVRational frame_aspect_ratio
double rotate_override_value
int audio_channels_mapped
size_t muxing_queue_data_threshold
AVDictionary * resample_opts
AVRational max_frame_rate
AVFifoBuffer * muxing_queue
AVCodecParameters * ref_par
const char * attachment_filename
struct InputStream * sync_ist
AVDictionary * encoder_opts
char * filters
filtergraph associated to the -filter option
int64_t forced_kf_ref_pts
char * filters_script
filtergraph script associated to the -filter_script option
AVExpr * forced_keyframes_pexpr
size_t muxing_queue_data_size