History

fftools/ffmpeg: convert to a threaded architecture

Change the main loop and every component (demuxers, decoders, filters, encoders, muxers) to use the previously added transcode scheduler. Every instance of every such component was already running in a separate thread, but now they can actually run in parallel. Changes the results of ffmpeg-fix_sub_duration_heartbeat - tested by JEEB to be more correct and deterministic.

Anton Khirnov committed 3 years ago

Commit d119ae21 parent 9b8cc3612 files · +600 −2021Browse files

Unified · Split

12 files
  1. Changelog +2 −0
  2. fftools/ffmpeg.c +28 −346
  3. fftools/ffmpeg.h +13 −84
  4. fftools/ffmpeg_dec.c +69 −244
  5. fftools/ffmpeg_demux.c +129 −139
  6. fftools/ffmpeg_enc.c +64 −313
  7. fftools/ffmpeg_filter.c +194 −520
  8. fftools/ffmpeg_mux.c +62 −262
  9. fftools/ffmpeg_mux.h +1 −23
  10. fftools/ffmpeg_mux_init.c +23 −65
  11. fftools/ffmpeg_opt.c +2 −4
  12. tests/ref/fate/ffmpeg-fix_sub_duration_heartbeat +13 −21
Changelog+2−0
@@ -7,6 +7,8 @@ version <next>: · 6 unmodified lines
77 - EVC encoding using external library libxeve
88 - QOA decoder and demuxer
99 - aap filter
10+- demuxing, decoding, filtering, encoding, and muxing in the
11+ ffmpeg CLI now all run in parallel
1012
1113 version 6.1:
1214 - libaribcaption decoder
fftools/ffmpeg.c+28−346
@@ -117,7 +117,7 @@ typedef struct BenchmarkTimeStamps { · 116 unmodified lines
117117 static BenchmarkTimeStamps get_benchmark_time_stamps(void);
118118 static int64_t getmaxrss(void);
119119
120−unsigned nb_output_dumped = 0;
120+atomic_uint nb_output_dumped = 0;
121121
122122 static BenchmarkTimeStamps current_time;
123123 AVIOContext *progress_avio = NULL;
@@ -138,30 +138,6 @@ static struct termios oldtty; · 14 unmodified lines
138138 static int restore_tty;
139139 #endif
140140
141−/* sub2video hack:
142− Convert subtitles to video with alpha to insert them in filter graphs.
143− This is a temporary solution until libavfilter gets real subtitles support.
144− */
145−
146−static void sub2video_heartbeat(InputFile *infile, int64_t pts, AVRational tb)
147−{
148− /* When a frame is read from a file, examine all sub2video streams in
149− the same file and send the sub2video frame again. Otherwise, decoded
150− video frames could be accumulating in the filter graph while a filter
151− (possibly overlay) is desperately waiting for a subtitle frame. */
152− for (int i = 0; i < infile->nb_streams; i++) {
153− InputStream *ist = infile->streams[i];
154−
155− if (ist->dec_ctx->codec_type != AVMEDIA_TYPE_SUBTITLE)
156− continue;
157−
158− for (int j = 0; j < ist->nb_filters; j++)
159− ifilter_sub2video_heartbeat(ist->filters[j], pts, tb);
160− }
161−}
162−
163−/* end of sub2video hack */
164−
165141 static void term_exit_sigsafe(void)
166142 {
167143 #if HAVE_TERMIOS_H
@@ -499,23 +475,13 @@ void update_benchmark(const char *fmt, ...) · 331 unmodified lines
499475 }
500476 }
501477
502−void close_output_stream(OutputStream *ost)
503−{
504− OutputFile *of = output_files[ost->file_index];
505− ost->finished |= ENCODER_FINISHED;
506−
507− if (ost->sq_idx_encode >= 0)
508− sq_send(of->sq_encode, ost->sq_idx_encode, SQFRAME(NULL));
509−}
510−
511−static void print_report(int is_last_report, int64_t timer_start, int64_t cur_time)
478+static void print_report(int is_last_report, int64_t timer_start, int64_t cur_time, int64_t pts)
512479 {
513480 AVBPrint buf, buf_script;
514481 int64_t total_size = of_filesize(output_files[0]);
515482 int vid;
516483 double bitrate;
517484 double speed;
518− int64_t pts = AV_NOPTS_VALUE;
519485 static int64_t last_time = -1;
520486 static int first_report = 1;
521487 uint64_t nb_frames_dup = 0, nb_frames_drop = 0;
@@ -533,7 +499,7 @@ static void print_report(int is_last_report, int64_t timer_start, int64_t cur_ti · 11 unmodified lines
533499 last_time = cur_time;
534500 }
535501 if (((cur_time - last_time) < stats_period && !first_report) ||
536− (first_report && nb_output_dumped < nb_output_files))
502+ (first_report && atomic_load(&nb_output_dumped) < nb_output_files))
537503 return;
538504 last_time = cur_time;
539505 }
@@ -544,7 +510,7 @@ static void print_report(int is_last_report, int64_t timer_start, int64_t cur_ti · 4 unmodified lines
544510 av_bprint_init(&buf, 0, AV_BPRINT_SIZE_AUTOMATIC);
545511 av_bprint_init(&buf_script, 0, AV_BPRINT_SIZE_AUTOMATIC);
546512 for (OutputStream *ost = ost_iter(NULL); ost; ost = ost_iter(ost)) {
547− const float q = ost->enc ? ost->quality / (float) FF_QP2LAMBDA : -1;
513+ const float q = ost->enc ? atomic_load(&ost->quality) / (float) FF_QP2LAMBDA : -1;
548514
549515 if (vid && ost->type == AVMEDIA_TYPE_VIDEO) {
550516 av_bprintf(&buf, "q=%2.1f ", q);
@@ -565,22 +531,18 @@ static void print_report(int is_last_report, int64_t timer_start, int64_t cur_ti · 14 unmodified lines
565531 if (is_last_report)
566532 av_bprintf(&buf, "L");
567533
568− nb_frames_dup = ost->filter->nb_frames_dup;
569− nb_frames_drop = ost->filter->nb_frames_drop;
534+ nb_frames_dup = atomic_load(&ost->filter->nb_frames_dup);
535+ nb_frames_drop = atomic_load(&ost->filter->nb_frames_drop);
570536
571537 vid = 1;
572538 }
573− /* compute min output value */
574− if (ost->last_mux_dts != AV_NOPTS_VALUE) {
575− if (pts == AV_NOPTS_VALUE || ost->last_mux_dts > pts)
576− pts = ost->last_mux_dts;
577− if (copy_ts) {
578− if (copy_ts_first_pts == AV_NOPTS_VALUE && pts > 1)
579− copy_ts_first_pts = pts;
580− if (copy_ts_first_pts != AV_NOPTS_VALUE)
581− pts -= copy_ts_first_pts;
582− }
583− }
539+ }
540+
541+ if (copy_ts) {
542+ if (copy_ts_first_pts == AV_NOPTS_VALUE && pts > 1)
543+ copy_ts_first_pts = pts;
544+ if (copy_ts_first_pts != AV_NOPTS_VALUE)
545+ pts -= copy_ts_first_pts;
584546 }
585547
586548 us = FFABS64U(pts) % AV_TIME_BASE;
@@ -783,81 +745,6 @@ int subtitle_wrap_frame(AVFrame *frame, AVSubtitle *subtitle, int copy) · 196 unmodified lines
783745 return 0;
784746 }
785747
786−int trigger_fix_sub_duration_heartbeat(OutputStream *ost, const AVPacket *pkt)
787−{
788− OutputFile *of = output_files[ost->file_index];
789− int64_t signal_pts = av_rescale_q(pkt->pts, pkt->time_base,
790− AV_TIME_BASE_Q);
791−
792− if (!ost->fix_sub_duration_heartbeat || !(pkt->flags & AV_PKT_FLAG_KEY))
793− // we are only interested in heartbeats on streams configured, and
794− // only on random access points.
795− return 0;
796−
797− for (int i = 0; i < of->nb_streams; i++) {
798− OutputStream *iter_ost = of->streams[i];
799− InputStream *ist = iter_ost->ist;
800− int ret = AVERROR_BUG;
801−
802− if (iter_ost == ost || !ist || !ist->decoding_needed ||
803− ist->dec_ctx->codec_type != AVMEDIA_TYPE_SUBTITLE)
804− // We wish to skip the stream that causes the heartbeat,
805− // output streams without an input stream, streams not decoded
806− // (as fix_sub_duration is only done for decoded subtitles) as
807− // well as non-subtitle streams.
808− continue;
809−
810− if ((ret = fix_sub_duration_heartbeat(ist, signal_pts)) < 0)
811− return ret;
812− }
813−
814− return 0;
815−}
816−
817−/* pkt = NULL means EOF (needed to flush decoder buffers) */
818−static int process_input_packet(InputStream *ist, const AVPacket *pkt, int no_eof)
819−{
820− InputFile *f = input_files[ist->file_index];
821− int64_t dts_est = AV_NOPTS_VALUE;
822− int ret = 0;
823− int eof_reached = 0;
824−
825− if (ist->decoding_needed) {
826− ret = dec_packet(ist, pkt, no_eof);
827− if (ret < 0 && ret != AVERROR_EOF)
828− return ret;
829− }
830− if (ret == AVERROR_EOF || (!pkt && !ist->decoding_needed))
831− eof_reached = 1;
832−
833− if (pkt && pkt->opaque_ref) {
834− DemuxPktData *pd = (DemuxPktData*)pkt->opaque_ref->data;
835− dts_est = pd->dts_est;
836− }
837−
838− if (f->recording_time != INT64_MAX) {
839− int64_t start_time = 0;
840− if (copy_ts) {
841− start_time += f->start_time != AV_NOPTS_VALUE ? f->start_time : 0;
842− start_time += start_at_zero ? 0 : f->start_time_effective;
843− }
844− if (dts_est >= f->recording_time + start_time)
845− pkt = NULL;
846− }
847−
848− for (int oidx = 0; oidx < ist->nb_outputs; oidx++) {
849− OutputStream *ost = ist->outputs[oidx];
850− if (ost->enc || (!pkt && no_eof))
851− continue;
852−
853− ret = of_streamcopy(ost, pkt, dts_est);
854− if (ret < 0)
855− return ret;
856− }
857−
858− return !eof_reached;
859−}
860−
861748 static void print_stream_maps(void)
862749 {
863750 av_log(NULL, AV_LOG_INFO, "Stream mapping:\n");
@@ -934,43 +821,6 @@ static void print_stream_maps(void) · 70 unmodified lines
934821 }
935822 }
936823
937−/**
938− * Select the output stream to process.
939− *
940− * @retval 0 an output stream was selected
941− * @retval AVERROR(EAGAIN) need to wait until more input is available
942− * @retval AVERROR_EOF no more streams need output
943− */
944−static int choose_output(OutputStream **post)
945−{
946− int64_t opts_min = INT64_MAX;
947− OutputStream *ost_min = NULL;
948−
949− for (OutputStream *ost = ost_iter(NULL); ost; ost = ost_iter(ost)) {
950− int64_t opts;
951−
952− if (ost->filter && ost->filter->last_pts != AV_NOPTS_VALUE) {
953− opts = ost->filter->last_pts;
954− } else {
955− opts = ost->last_mux_dts == AV_NOPTS_VALUE ?
956− INT64_MIN : ost->last_mux_dts;
957− }
958−
959− if (!ost->initialized && !ost->finished) {
960− ost_min = ost;
961− break;
962− }
963− if (!ost->finished && opts < opts_min) {
964− opts_min = opts;
965− ost_min = ost;
966− }
967− }
968− if (!ost_min)
969− return AVERROR_EOF;
970− *post = ost_min;
971− return ost_min->unavailable ? AVERROR(EAGAIN) : 0;
972−}
973−
974824 static void set_tty_echo(int on)
975825 {
976826 #if HAVE_TERMIOS_H
@@ -1042,149 +892,21 @@ static int check_keyboard_interaction(int64_t cur_time) · 65 unmodified lines
1042892 return 0;
1043893 }
1044894
1045−static void reset_eagain(void)
1046−{
1047− for (OutputStream *ost = ost_iter(NULL); ost; ost = ost_iter(ost))
1048− ost->unavailable = 0;
1049−}
1050−
1051−static void decode_flush(InputFile *ifile)
1052−{
1053− for (int i = 0; i < ifile->nb_streams; i++) {
1054− InputStream *ist = ifile->streams[i];
1055−
1056− if (ist->discard || !ist->decoding_needed)
1057− continue;
1058−
1059− dec_packet(ist, NULL, 1);
1060− }
1061−}
1062−
1063−/*
1064− * Return
1065− * - 0 -- one packet was read and processed
1066− * - AVERROR(EAGAIN) -- no packets were available for selected file,
1067− * this function should be called again
1068− * - AVERROR_EOF -- this function should not be called again
1069− */
1070−static int process_input(int file_index, AVPacket *pkt)
1071−{
1072− InputFile *ifile = input_files[file_index];
1073− InputStream *ist;
1074− int ret, i;
1075−
1076− ret = ifile_get_packet(ifile, pkt);
1077−
1078− if (ret == 1) {
1079− /* the input file is looped: flush the decoders */
1080− decode_flush(ifile);
1081− return AVERROR(EAGAIN);
1082− }
1083− if (ret < 0) {
1084− if (ret != AVERROR_EOF) {
1085− av_log(ifile, AV_LOG_ERROR,
1086− "Error retrieving a packet from demuxer: %s\n", av_err2str(ret));
1087− if (exit_on_error)
1088− return ret;
1089− }
1090−
1091− for (i = 0; i < ifile->nb_streams; i++) {
1092− ist = ifile->streams[i];
1093− if (!ist->discard) {
1094− ret = process_input_packet(ist, NULL, 0);
1095− if (ret>0)
1096− return 0;
1097− else if (ret < 0)
1098− return ret;
1099− }
1100−
1101− /* mark all outputs that don't go through lavfi as finished */
1102− for (int oidx = 0; oidx < ist->nb_outputs; oidx++) {
1103− OutputStream *ost = ist->outputs[oidx];
1104− OutputFile *of = output_files[ost->file_index];
1105−
1106− ret = of_output_packet(of, ost, NULL);
1107− if (ret < 0)
1108− return ret;
1109− }
1110− }
1111−
1112− ifile->eof_reached = 1;
1113− return AVERROR(EAGAIN);
1114− }
1115−
1116− reset_eagain();
1117−
1118− ist = ifile->streams[pkt->stream_index];
1119−
1120− sub2video_heartbeat(ifile, pkt->pts, pkt->time_base);
1121−
1122− ret = process_input_packet(ist, pkt, 0);
1123−
1124− av_packet_unref(pkt);
1125−
1126− return ret < 0 ? ret : 0;
1127−}
1128−
1129−/**
1130− * Run a single step of transcoding.
1131− *
1132− * @return 0 for success, <0 for error
1133− */
1134−static int transcode_step(OutputStream *ost, AVPacket *demux_pkt)
1135−{
1136− InputStream *ist = NULL;
1137− int ret;
1138−
1139− if (ost->filter) {
1140− if ((ret = fg_transcode_step(ost->filter->graph, &ist)) < 0)
1141− return ret;
1142− if (!ist)
1143− return 0;
1144− } else {
1145− ist = ost->ist;
1146− av_assert0(ist);
1147− }
1148−
1149− ret = process_input(ist->file_index, demux_pkt);
1150− if (ret == AVERROR(EAGAIN)) {
1151− return 0;
1152− }
1153−
1154− if (ret < 0)
1155− return ret == AVERROR_EOF ? 0 : ret;
1156−
1157− // process_input() above might have caused output to become available
1158− // in multiple filtergraphs, so we process all of them
1159− for (int i = 0; i < nb_filtergraphs; i++) {
1160− ret = reap_filters(filtergraphs[i], 0);
1161− if (ret < 0)
1162− return ret;
1163− }
1164−
1165− return 0;
1166−}
1167−
1168895 /*
1169896 * The following code is the main loop of the file converter
1170897 */
1171−static int transcode(Scheduler *sch, int *err_rate_exceeded)
898+static int transcode(Scheduler *sch)
1172899 {
1173900 int ret = 0, i;
1174− InputStream *ist;
1175− int64_t timer_start;
1176− AVPacket *demux_pkt = NULL;
901+ int64_t timer_start, transcode_ts = 0;
1177902
1178903 print_stream_maps();
1179904
1180− *err_rate_exceeded = 0;
1181905 atomic_store(&transcode_init_done, 1);
1182906
1183− demux_pkt = av_packet_alloc();
1184− if (!demux_pkt) {
1185− ret = AVERROR(ENOMEM);
1186− goto fail;
1187− }
907+ ret = sch_start(sch);
908+ if (ret < 0)
909+ return ret;
1188910
1189911 if (stdin_interaction) {
1190912 av_log(NULL, AV_LOG_INFO, "Press [q] to stop, [?] for help\n");
@@ -1192,8 +914,7 @@ static int transcode(Scheduler *sch, int *err_rate_exceeded) · 1 unmodified line
1192914
1193915 timer_start = av_gettime_relative();
1194916
1195− while (!received_sigterm) {
1196− OutputStream *ost;
917+ while (!sch_wait(sch, stats_period, &transcode_ts)) {
1197918 int64_t cur_time= av_gettime_relative();
1198919
1199920 /* if 'q' pressed, exits */
@@ -1201,49 +922,11 @@ static int transcode(Scheduler *sch, int *err_rate_exceeded) · 1 unmodified line
1201922 if (check_keyboard_interaction(cur_time) < 0)
1202923 break;
1203924
1204− ret = choose_output(&ost);
1205− if (ret == AVERROR(EAGAIN)) {
1206− reset_eagain();
1207− av_usleep(10000);
1208− ret = 0;
1209− continue;
1210− } else if (ret < 0) {
1211− av_log(NULL, AV_LOG_VERBOSE, "No more output streams to write to, finishing.\n");
1212− ret = 0;
1213− break;
1214− }
1215−
1216− ret = transcode_step(ost, demux_pkt);
1217− if (ret < 0 && ret != AVERROR_EOF) {
1218− av_log(NULL, AV_LOG_ERROR, "Error while filtering: %s\n", av_err2str(ret));
1219− break;
1220− }
1221−
1222925 /* dump report by using the output first video and audio streams */
1223− print_report(0, timer_start, cur_time);
1224− }
1225−
1226− /* at the end of stream, we must flush the decoder buffers */
1227− for (ist = ist_iter(NULL); ist; ist = ist_iter(ist)) {
1228− float err_rate;
1229−
1230− if (!input_files[ist->file_index]->eof_reached) {
1231− int err = process_input_packet(ist, NULL, 0);
1232− ret = err_merge(ret, err);
1233− }
1234−
1235− err_rate = (ist->frames_decoded || ist->decode_errors) ?
1236− ist->decode_errors / (ist->frames_decoded + ist->decode_errors) : 0.f;
1237− if (err_rate > max_error_rate) {
1238− av_log(ist, AV_LOG_FATAL, "Decode error rate %g exceeds maximum %g\n",
1239− err_rate, max_error_rate);
1240− *err_rate_exceeded = 1;
1241− } else if (err_rate)
1242− av_log(ist, AV_LOG_VERBOSE, "Decode error rate %g\n", err_rate);
926+ print_report(0, timer_start, cur_time, transcode_ts);
1243927 }
1244− ret = err_merge(ret, enc_flush());
1245928
1246− term_exit();
929+ ret = sch_stop(sch);
1247930
1248931 /* write the trailer if needed */
1249932 for (i = 0; i < nb_output_files; i++) {
@@ -1251,11 +934,10 @@ static int transcode(Scheduler *sch, int *err_rate_exceeded) · 1 unmodified line
1251934 ret = err_merge(ret, err);
1252935 }
1253936
1254− /* dump report by using the first video and audio streams */
1255− print_report(1, timer_start, av_gettime_relative());
937+ term_exit();
1256938
1257−fail:
1258− av_packet_free(&demux_pkt);
939+ /* dump report by using the first video and audio streams */
940+ print_report(1, timer_start, av_gettime_relative(), transcode_ts);
1259941
1260942 return ret;
1261943 }
@@ -1308,7 +990,7 @@ int main(int argc, char **argv) · 46 unmodified lines
1308990 {
1309991 Scheduler *sch = NULL;
1310992
1311− int ret, err_rate_exceeded;
993+ int ret;
1312994 BenchmarkTimeStamps ti;
1313995
1314996 init_dynload();
@@ -1350,7 +1032,7 @@ int main(int argc, char **argv) · 35 unmodified lines
13501032 }
13511033
13521034 current_time = ti = get_benchmark_time_stamps();
1353− ret = transcode(sch, &err_rate_exceeded);
1035+ ret = transcode(sch);
13541036 if (ret >= 0 && do_benchmark) {
13551037 int64_t utime, stime, rtime;
13561038 current_time = get_benchmark_time_stamps();
@@ -1362,8 +1044,8 @@ int main(int argc, char **argv) · 5 unmodified lines
13621044 utime / 1000000.0, stime / 1000000.0, rtime / 1000000.0);
13631045 }
13641046
1365− ret = received_nb_signals ? 255 :
1366− err_rate_exceeded ? 69 : ret;
1047+ ret = received_nb_signals ? 255 :
1048+ (ret == FFMPEG_ERROR_RATE_EXCEEDED) ? 69 : ret;
13671049
13681050 finish:
13691051 if (ret == AVERROR_EXIT)
fftools/ffmpeg.h+13−84
@@ -61,6 +61,8 @@ · 60 unmodified lines
6161 #define FFMPEG_OPT_TOP 1
6262 #define FFMPEG_OPT_FORCE_KF_SOURCE_NO_DROP 1
6363
64+#define FFMPEG_ERROR_RATE_EXCEEDED FFERRTAG('E', 'R', 'E', 'D')
65+
6466 enum VideoSyncMethod {
6567 VSYNC_AUTO = -1,
6668 VSYNC_PASSTHROUGH,
@@ -82,13 +84,16 @@ enum HWAccelID { · 15 unmodified lines
8284 };
8385
8486 enum FrameOpaque {
85− FRAME_OPAQUE_REAP_FILTERS = 1,
86− FRAME_OPAQUE_CHOOSE_INPUT,
87− FRAME_OPAQUE_SUB_HEARTBEAT,
87+ FRAME_OPAQUE_SUB_HEARTBEAT = 1,
8888 FRAME_OPAQUE_EOF,
8989 FRAME_OPAQUE_SEND_COMMAND,
9090 };
9191
92+enum PacketOpaque {
93+ PKT_OPAQUE_SUB_HEARTBEAT = 1,
94+ PKT_OPAQUE_FIX_SUB_DURATION,
95+};
96+
9297 typedef struct HWDevice {
9398 const char *name;
9499 enum AVHWDeviceType type;
@@ -309,11 +314,8 @@ typedef struct OutputFilter { · 214 unmodified lines
309314
310315 enum AVMediaType type;
311316
312− /* pts of the last frame received from this filter, in AV_TIME_BASE_Q */
313− int64_t last_pts;
314−
315− uint64_t nb_frames_dup;
316− uint64_t nb_frames_drop;
317+ atomic_uint_least64_t nb_frames_dup;
318+ atomic_uint_least64_t nb_frames_drop;
317319 } OutputFilter;
318320
319321 typedef struct FilterGraph {
@@ -426,11 +428,6 @@ typedef struct InputFile { · 106 unmodified lines
426428
427429 float readrate;
428430 int accurate_seek;
429−
430− /* when looping the input file, this queue is used by decoders to report
431− * the last frame timestamp back to the demuxer thread */
432− AVThreadMessageQueue *audio_ts_queue;
433− int audio_ts_queue_size;
434431 } InputFile;
435432
436433 enum forced_keyframes_const {
@@ -532,8 +529,6 @@ typedef struct OutputStream { · 95 unmodified lines
532529 InputStream *ist;
533530
534531 AVStream *st; /* stream in the output file */
535− /* dts of the last packet sent to the muxing queue, in AV_TIME_BASE_Q */
536− int64_t last_mux_dts;
537532
538533 AVRational enc_timebase;
539534
@@ -578,13 +573,6 @@ typedef struct OutputStream { · 38 unmodified lines
578573 AVDictionary *sws_dict;
579574 AVDictionary *swr_opts;
580575 char *apad;
581− OSTFinished finished; /* no more packets should be written for this stream */
582− int unavailable; /* true if the steram is unavailable (possibly temporarily) */
583−
584− // init_output_stream() has been called for this stream
585− // The encoder and the bitstream filters have been initialized and the stream
586− // parameters are set in the AVStream.
587− int initialized;
588576
589577 const char *attachment_filename;
590578
@@ -598,9 +586,8 @@ typedef struct OutputStream { · 7 unmodified lines
598586 uint64_t samples_encoded;
599587
600588 /* packet quality factor */
601− int quality;
589+ atomic_int quality;
602590
603− int sq_idx_encode;
604591 int sq_idx_mux;
605592
606593 EncStats enc_stats_pre;
@@ -658,7 +645,6 @@ extern FilterGraph **filtergraphs; · 51 unmodified lines
658645 extern int nb_filtergraphs;
659646
660647 extern char *vstats_filename;
661−extern char *sdp_filename;
662648
663649 extern float dts_delta_threshold;
664650 extern float dts_error_threshold;
@@ -691,7 +677,7 @@ extern const AVIOInterruptCB int_cb; · 26 unmodified lines
691677 extern const OptionDef options[];
692678 extern HWDevice *filter_hw_device;
693679
694−extern unsigned nb_output_dumped;
680+extern atomic_uint nb_output_dumped;
695681
696682 extern int ignore_unknown_streams;
697683 extern int copy_unknown_streams;
@@ -737,10 +723,6 @@ FrameData *frame_data(AVFrame *frame); · 39 unmodified lines
737723
738724 const FrameData *frame_data_c(AVFrame *frame);
739725
740−int ifilter_send_frame(InputFilter *ifilter, AVFrame *frame, int keep_reference);
741−int ifilter_send_eof(InputFilter *ifilter, int64_t pts, AVRational tb);
742−void ifilter_sub2video_heartbeat(InputFilter *ifilter, int64_t pts, AVRational tb);
743−
744726 /**
745727 * Set up fallback filtering parameters from a decoder context. They will only
746728 * be used if no frames are ever sent on this input, otherwise the actual
@@ -761,26 +743,9 @@ int fg_create(FilterGraph **pfg, char *graph_desc, Scheduler *sch); · 14 unmodified lines
761743
762744 void fg_free(FilterGraph **pfg);
763745
764−/**
765− * Perform a step of transcoding for the specified filter graph.
766− *
767− * @param[in] graph filter graph to consider
768− * @param[out] best_ist input stream where a frame would allow to continue
769− * @return 0 for success, <0 for error
770− */
771−int fg_transcode_step(FilterGraph *graph, InputStream **best_ist);
772−
773746 void fg_send_command(FilterGraph *fg, double time, const char *target,
774747 const char *command, const char *arg, int all_filters);
775748
776−/**
777− * Get and encode new output from specified filtergraph, without causing
778− * activity.
779− *
780− * @return 0 for success, <0 for severe errors
781− */
782−int reap_filters(FilterGraph *fg, int flush);
783−
784749 int ffmpeg_parse_options(int argc, char **argv, Scheduler *sch);
785750
786751 void enc_stats_write(OutputStream *ost, EncStats *es,
@@ -807,25 +772,11 @@ int hwaccel_retrieve_data(AVCodecContext *avctx, AVFrame *input); · 20 unmodified lines
807772 int dec_open(InputStream *ist, Scheduler *sch, unsigned sch_idx);
808773 void dec_free(Decoder **pdec);
809774
810−/**
811− * Submit a packet for decoding
812− *
813− * When pkt==NULL and no_eof=0, there will be no more input. Flush decoders and
814− * mark all downstreams as finished.
815− *
816− * When pkt==NULL and no_eof=1, the stream was reset (e.g. after a seek). Flush
817− * decoders and await further input.
818− */
819−int dec_packet(InputStream *ist, const AVPacket *pkt, int no_eof);
820−
821775 int enc_alloc(Encoder **penc, const AVCodec *codec,
822776 Scheduler *sch, unsigned sch_idx);
823777 void enc_free(Encoder **penc);
824778
825−int enc_open(OutputStream *ost, const AVFrame *frame);
826−int enc_subtitle(OutputFile *of, OutputStream *ost, const AVSubtitle *sub);
827−int enc_frame(OutputStream *ost, AVFrame *frame);
828−int enc_flush(void);
779+int enc_open(void *opaque, const AVFrame *frame);
829780
830781 /*
831782 * Initialize muxing state for the given stream, should be called
@@ -840,30 +791,11 @@ void of_free(OutputFile **pof); · 8 unmodified lines
840791
841792 void of_enc_stats_close(void);
842793
843−int of_output_packet(OutputFile *of, OutputStream *ost, AVPacket *pkt);
844−
845−/**
846− * @param dts predicted packet dts in AV_TIME_BASE_Q
847− */
848−int of_streamcopy(OutputStream *ost, const AVPacket *pkt, int64_t dts);
849−
850794 int64_t of_filesize(OutputFile *of);
851795
852796 int ifile_open(const OptionsContext *o, const char *filename, Scheduler *sch);
853797 void ifile_close(InputFile **f);
854798
855−/**
856− * Get next input packet from the demuxer.
857− *
858− * @param pkt the packet is written here when this function returns 0
859− * @return
860− * - 0 when a packet has been read successfully
861− * - 1 when stream end was reached, but the stream is looped;
862− * caller should flush decoders and read from this demuxer again
863− * - a negative error code on failure
864− */
865−int ifile_get_packet(InputFile *f, AVPacket *pkt);
866−
867799 int ist_output_add(InputStream *ist, OutputStream *ost);
868800 int ist_filter_add(InputStream *ist, InputFilter *ifilter, int is_simple);
869801
@@ -880,9 +812,6 @@ InputStream *ist_iter(InputStream *prev); · 10 unmodified lines
880812 * pass NULL to start iteration */
881813 OutputStream *ost_iter(OutputStream *prev);
882814
883−void close_output_stream(OutputStream *ost);
884−int trigger_fix_sub_duration_heartbeat(OutputStream *ost, const AVPacket *pkt);
885−int fix_sub_duration_heartbeat(InputStream *ist, int64_t signal_pts);
886815 void update_benchmark(const char *fmt, ...);
887816
888817 #define SPECIFIER_OPT_FMT_str "%s"
fftools/ffmpeg_dec.c+69−244
@@ -54,24 +54,6 @@ struct Decoder { · 53 unmodified lines
5454
5555 Scheduler *sch;
5656 unsigned sch_idx;
57−
58− pthread_t thread;
59− /**
60− * Queue for sending coded packets from the main thread to
61− * the decoder thread.
62− *
63− * An empty packet is sent to flush the decoder without terminating
64− * decoding.
65− */
66− ThreadQueue *queue_in;
67− /**
68− * Queue for sending decoded frames from the decoder thread
69− * to the main thread.
70− *
71− * An empty frame is sent to signal that a single packet has been fully
72− * processed.
73− */
74− ThreadQueue *queue_out;
7557 };
7658
7759 // data that is local to the decoder thread and not visible outside of it
@@ -80,24 +62,6 @@ typedef struct DecThreadContext { · 2 unmodified lines
8062 AVPacket *pkt;
8163 } DecThreadContext;
8264
83−static int dec_thread_stop(Decoder *d)
84−{
85− void *ret;
86−
87− if (!d->queue_in)
88− return 0;
89−
90− tq_send_finish(d->queue_in, 0);
91− tq_receive_finish(d->queue_out, 0);
92−
93− pthread_join(d->thread, &ret);
94−
95− tq_free(&d->queue_in);
96− tq_free(&d->queue_out);
97−
98− return (intptr_t)ret;
99−}
100−
10165 void dec_free(Decoder **pdec)
10266 {
10367 Decoder *dec = *pdec;
@@ -105,8 +69,6 @@ void dec_free(Decoder **pdec) · 1 unmodified line
10569 if (!dec)
10670 return;
10771
108− dec_thread_stop(dec);
109−
11072 av_frame_free(&dec->frame);
11173 av_packet_free(&dec->pkt);
11274
@@ -148,25 +110,6 @@ fail: · 35 unmodified lines
148110 return AVERROR(ENOMEM);
149111 }
150112
151−static int send_frame_to_filters(InputStream *ist, AVFrame *decoded_frame)
152−{
153− int i, ret = 0;
154−
155− for (i = 0; i < ist->nb_filters; i++) {
156− ret = ifilter_send_frame(ist->filters[i], decoded_frame,
157− i < ist->nb_filters - 1 ||
158− ist->dec->type == AVMEDIA_TYPE_SUBTITLE);
159− if (ret == AVERROR_EOF)
160− ret = 0; /* ignore */
161− if (ret < 0) {
162− av_log(NULL, AV_LOG_ERROR,
163− "Failed to inject frame into filter network: %s\n", av_err2str(ret));
164− break;
165− }
166− }
167− return ret;
168−}
169−
170113 static AVRational audio_samplerate_update(void *logctx, Decoder *d,
171114 const AVFrame *frame)
172115 {
@@ -421,28 +364,14 @@ static int process_subtitle(InputStream *ist, AVFrame *frame) · 248 unmodified lines
421364 if (!subtitle)
422365 return 0;
423366
424− ret = send_frame_to_filters(ist, frame);
367+ ret = sch_dec_send(d->sch, d->sch_idx, frame);
425368 if (ret < 0)
426− return ret;
427−
428− subtitle = (AVSubtitle*)frame->buf[0]->data;
429− if (!subtitle->num_rects)
430− return 0;
431−
432− for (int oidx = 0; oidx < ist->nb_outputs; oidx++) {
433− OutputStream *ost = ist->outputs[oidx];
434− if (!ost->enc || ost->type != AVMEDIA_TYPE_SUBTITLE)
435− continue;
436−
437− ret = enc_subtitle(output_files[ost->file_index], ost, subtitle);
438− if (ret < 0)
439− return ret;
440− }
369+ av_frame_unref(frame);
441370
442− return 0;
371+ return ret == AVERROR_EOF ? AVERROR_EXIT : ret;
443372 }
444373
445−int fix_sub_duration_heartbeat(InputStream *ist, int64_t signal_pts)
374+static int fix_sub_duration_heartbeat(InputStream *ist, int64_t signal_pts)
446375 {
447376 Decoder *d = ist->decoder;
448377 int ret = AVERROR_BUG;
@@ -468,12 +397,24 @@ int fix_sub_duration_heartbeat(InputStream *ist, int64_t signal_pts) · 19 unmodified lines
468397 static int transcode_subtitles(InputStream *ist, const AVPacket *pkt,
469398 AVFrame *frame)
470399 {
471− Decoder *d = ist->decoder;
400+ Decoder *d = ist->decoder;
472401 AVPacket *flush_pkt = NULL;
473402 AVSubtitle subtitle;
474403 int got_output;
475404 int ret;
476405
406+ if (pkt && (intptr_t)pkt->opaque == PKT_OPAQUE_SUB_HEARTBEAT) {
407+ frame->pts = pkt->pts;
408+ frame->time_base = pkt->time_base;
409+ frame->opaque = (void*)(intptr_t)FRAME_OPAQUE_SUB_HEARTBEAT;
410+
411+ ret = sch_dec_send(d->sch, d->sch_idx, frame);
412+ return ret == AVERROR_EOF ? AVERROR_EXIT : ret;
413+ } else if (pkt && (intptr_t)pkt->opaque == PKT_OPAQUE_FIX_SUB_DURATION) {
414+ return fix_sub_duration_heartbeat(ist, av_rescale_q(pkt->pts, pkt->time_base,
415+ AV_TIME_BASE_Q));
416+ }
417+
477418 if (!pkt) {
478419 flush_pkt = av_packet_alloc();
479420 if (!flush_pkt)
@@ -496,7 +437,7 @@ static int transcode_subtitles(InputStream *ist, const AVPacket *pkt, · 16 unmodified lines
496437
497438 ist->frames_decoded++;
498439
499− // XXX the queue for transferring data back to the main thread runs
440+ // XXX the queue for transferring data to consumers runs
500441 // on AVFrames, so we wrap AVSubtitle in an AVBufferRef and put that
501442 // inside the frame
502443 // eventually, subtitles should be switched to use AVFrames natively
@@ -509,26 +450,7 @@ static int transcode_subtitles(InputStream *ist, const AVPacket *pkt, · 6 unmodified lines
509450 frame->width = ist->dec_ctx->width;
510451 frame->height = ist->dec_ctx->height;
511452
512− ret = tq_send(d->queue_out, 0, frame);
513− if (ret < 0)
514− av_frame_unref(frame);
515−
516− return ret;
517−}
518−
519−static int send_filter_eof(InputStream *ist)
520−{
521− Decoder *d = ist->decoder;
522− int i, ret;
523−
524− for (i = 0; i < ist->nb_filters; i++) {
525− int64_t end_pts = d->last_frame_pts == AV_NOPTS_VALUE ? AV_NOPTS_VALUE :
526− d->last_frame_pts + d->last_frame_duration_est;
527− ret = ifilter_send_eof(ist->filters[i], end_pts, d->last_frame_tb);
528− if (ret < 0)
529− return ret;
530− }
531− return 0;
453+ return process_subtitle(ist, frame);
532454 }
533455
534456 static int packet_decode(InputStream *ist, AVPacket *pkt, AVFrame *frame)
@@ -635,9 +557,11 @@ static int packet_decode(InputStream *ist, AVPacket *pkt, AVFrame *frame) · 100 unmodified lines
635557
636558 ist->frames_decoded++;
637559
638− ret = tq_send(d->queue_out, 0, frame);
639− if (ret < 0)
640− return ret;
560+ ret = sch_dec_send(d->sch, d->sch_idx, frame);
561+ if (ret < 0) {
562+ av_frame_unref(frame);
563+ return ret == AVERROR_EOF ? AVERROR_EXIT : ret;
564+ }
641565 }
642566 }
643567
@@ -679,7 +603,6 @@ fail: · 35 unmodified lines
679603 void *decoder_thread(void *arg)
680604 {
681605 InputStream *ist = arg;
682− InputFile *ifile = input_files[ist->file_index];
683606 Decoder *d = ist->decoder;
684607 DecThreadContext dt;
685608 int ret = 0, input_status = 0;
@@ -691,19 +614,31 @@ void *decoder_thread(void *arg) · 5 unmodified lines
691614 dec_thread_set_name(ist);
692615
693616 while (!input_status) {
694− int dummy, flush_buffers;
695−
696− input_status = tq_receive(d->queue_in, &dummy, dt.pkt);
697− flush_buffers = input_status >= 0 && !dt.pkt->buf;
698− if (!dt.pkt->buf)
617+ int flush_buffers, have_data;
618+
619+ input_status = sch_dec_receive(d->sch, d->sch_idx, dt.pkt);
620+ have_data = input_status >= 0 &&
621+ (dt.pkt->buf || dt.pkt->side_data_elems ||
622+ (intptr_t)dt.pkt->opaque == PKT_OPAQUE_SUB_HEARTBEAT ||
623+ (intptr_t)dt.pkt->opaque == PKT_OPAQUE_FIX_SUB_DURATION);
624+ flush_buffers = input_status >= 0 && !have_data;
625+ if (!have_data)
699626 av_log(ist, AV_LOG_VERBOSE, "Decoder thread received %s packet\n",
700627 flush_buffers ? "flush" : "EOF");
701628
702− ret = packet_decode(ist, dt.pkt->buf ? dt.pkt : NULL, dt.frame);
629+ ret = packet_decode(ist, have_data ? dt.pkt : NULL, dt.frame);
703630
704631 av_packet_unref(dt.pkt);
705632 av_frame_unref(dt.frame);
706633
634+ // AVERROR_EOF - EOF from the decoder
635+ // AVERROR_EXIT - EOF from the scheduler
636+ // we treat them differently when flushing
637+ if (ret == AVERROR_EXIT) {
638+ ret = AVERROR_EOF;
639+ flush_buffers = 0;
640+ }
641+
707642 if (ret == AVERROR_EOF) {
708643 av_log(ist, AV_LOG_VERBOSE, "Decoder returned EOF, %s\n",
709644 flush_buffers ? "resetting" : "finishing");
@@ -711,11 +646,10 @@ void *decoder_thread(void *arg) · 1 unmodified line
711646 if (!flush_buffers)
712647 break;
713648
714− /* report last frame duration to the demuxer thread */
649+ /* report last frame duration to the scheduler */
715650 if (ist->dec->type == AVMEDIA_TYPE_AUDIO) {
716− Timestamp ts = { .ts = d->last_frame_pts + d->last_frame_duration_est,
717− .tb = d->last_frame_tb };
718− av_thread_message_queue_send(ifile->audio_ts_queue, &ts, 0);
651+ dt.pkt->pts = d->last_frame_pts + d->last_frame_duration_est;
652+ dt.pkt->time_base = d->last_frame_tb;
719653 }
720654
721655 avcodec_flush_buffers(ist->dec_ctx);
@@ -724,147 +658,45 @@ void *decoder_thread(void *arg) · 2 unmodified lines
724658 av_err2str(ret));
725659 break;
726660 }
727−
728− // signal to the consumer thread that the entire packet was processed
729− ret = tq_send(d->queue_out, 0, dt.frame);
730− if (ret < 0) {
731− if (ret != AVERROR_EOF)
732− av_log(ist, AV_LOG_ERROR, "Error communicating with the main thread\n");
733− break;
734− }
735661 }
736662
737663 // EOF is normal thread termination
738664 if (ret == AVERROR_EOF)
739665 ret = 0;
740666
741−finish:
742− tq_receive_finish(d->queue_in, 0);
743− tq_send_finish (d->queue_out, 0);
744−
745− // make sure the demuxer does not get stuck waiting for audio durations
746− // that will never arrive
747− if (ifile->audio_ts_queue && ist->dec->type == AVMEDIA_TYPE_AUDIO)
748− av_thread_message_queue_set_err_recv(ifile->audio_ts_queue, AVERROR_EOF);
749−
750− dec_thread_uninit(&dt);
751−
752− av_log(ist, AV_LOG_VERBOSE, "Terminating decoder thread\n");
753−
754− return (void*)(intptr_t)ret;
755−}
756−
757−int dec_packet(InputStream *ist, const AVPacket *pkt, int no_eof)
758−{
759− Decoder *d = ist->decoder;
760− int ret = 0, thread_ret;
761−
762− // thread already joined
763− if (!d->queue_in)
764− return AVERROR_EOF;
667+ // on success send EOF timestamp to our downstreams
668+ if (ret >= 0) {
669+ float err_rate;
765670
766− // send the packet/flush request/EOF to the decoder thread
767− if (pkt || no_eof) {
768− av_packet_unref(d->pkt);
769−
770− if (pkt) {
771− ret = av_packet_ref(d->pkt, pkt);
772− if (ret < 0)
773− goto finish;
774− }
775−
776− ret = tq_send(d->queue_in, 0, d->pkt);
777− if (ret < 0)
778− goto finish;
779− } else
780− tq_send_finish(d->queue_in, 0);
671+ av_frame_unref(dt.frame);
781672
782− // retrieve all decoded data for the packet
783− while (1) {
784− int dummy;
673+ dt.frame->opaque = (void*)(intptr_t)FRAME_OPAQUE_EOF;
674+ dt.frame->pts = d->last_frame_pts == AV_NOPTS_VALUE ? AV_NOPTS_VALUE :
675+ d->last_frame_pts + d->last_frame_duration_est;
676+ dt.frame->time_base = d->last_frame_tb;
785677
786− ret = tq_receive(d->queue_out, &dummy, d->frame);
787− if (ret < 0)
678+ ret = sch_dec_send(d->sch, d->sch_idx, dt.frame);
679+ if (ret < 0 && ret != AVERROR_EOF) {
680+ av_log(NULL, AV_LOG_FATAL,
681+ "Error signalling EOF timestamp: %s\n", av_err2str(ret));
788682 goto finish;
789−
790− // packet fully processed
791− if (!d->frame->buf[0])
792− return 0;
793−
794− // process the decoded frame
795− if (ist->dec->type == AVMEDIA_TYPE_SUBTITLE) {
796− ret = process_subtitle(ist, d->frame);
797− } else {
798− ret = send_frame_to_filters(ist, d->frame);
799683 }
800− av_frame_unref(d->frame);
801− if (ret < 0)
802− goto finish;
803− }
804−
805−finish:
806− thread_ret = dec_thread_stop(d);
807− if (thread_ret < 0) {
808− av_log(ist, AV_LOG_ERROR, "Decoder thread returned error: %s\n",
809− av_err2str(thread_ret));
810− ret = err_merge(ret, thread_ret);
811− }
812− // non-EOF errors here are all fatal
813− if (ret < 0 && ret != AVERROR_EOF)
814− return ret;
815−
816− // signal EOF to our downstreams
817− ret = send_filter_eof(ist);
818− if (ret < 0) {
819− av_log(NULL, AV_LOG_FATAL, "Error marking filters as finished\n");
820− return ret;
821− }
822−
823− return AVERROR_EOF;
824−}
825−
826−static int dec_thread_start(InputStream *ist)
827−{
828− Decoder *d = ist->decoder;
829− ObjPool *op;
830− int ret = 0;
831−
832− op = objpool_alloc_packets();
833− if (!op)
834− return AVERROR(ENOMEM);
835−
836− d->queue_in = tq_alloc(1, 1, op, pkt_move);
837− if (!d->queue_in) {
838− objpool_free(&op);
839− return AVERROR(ENOMEM);
840− }
841−
842− op = objpool_alloc_frames();
843− if (!op)
844− goto fail;
845−
846− d->queue_out = tq_alloc(1, 4, op, frame_move);
847− if (!d->queue_out) {
848− objpool_free(&op);
849− goto fail;
850− }
684+ ret = 0;
851685
852− ret = pthread_create(&d->thread, NULL, decoder_thread, ist);
853− if (ret) {
854− ret = AVERROR(ret);
855− av_log(ist, AV_LOG_ERROR, "pthread_create() failed: %s\n",
856− av_err2str(ret));
857− goto fail;
686+ err_rate = (ist->frames_decoded || ist->decode_errors) ?
687+ ist->decode_errors / (ist->frames_decoded + ist->decode_errors) : 0.f;
688+ if (err_rate > max_error_rate) {
689+ av_log(ist, AV_LOG_FATAL, "Decode error rate %g exceeds maximum %g\n",
690+ err_rate, max_error_rate);
691+ ret = FFMPEG_ERROR_RATE_EXCEEDED;
692+ } else if (err_rate)
693+ av_log(ist, AV_LOG_VERBOSE, "Decode error rate %g\n", err_rate);
858694 }
859695
860− return 0;
861−fail:
862− if (ret >= 0)
863− ret = AVERROR(ENOMEM);
696+finish:
697+ dec_thread_uninit(&dt);
864698
865− tq_free(&d->queue_in);
866− tq_free(&d->queue_out);
867− return ret;
699+ return (void*)(intptr_t)ret;
868700 }
869701
870702 static enum AVPixelFormat get_format(AVCodecContext *s, const enum AVPixelFormat *pix_fmts)
@@ -1118,12 +950,5 @@ int dec_open(InputStream *ist, Scheduler *sch, unsigned sch_idx) · 247 unmodified lines
1118950 if (ret < 0)
1119951 return ret;
1120952
1121− ret = dec_thread_start(ist);
1122− if (ret < 0) {
1123− av_log(ist, AV_LOG_ERROR, "Error starting decoder thread: %s\n",
1124− av_err2str(ret));
1125− return ret;
1126− }
1127−
1128953 return 0;
1129954 }
fftools/ffmpeg_demux.c+129−139
@@ -22,8 +22,6 @@ · 21 unmodified lines
2222 #include "ffmpeg.h"
2323 #include "ffmpeg_sched.h"
2424 #include "ffmpeg_utils.h"
25−#include "objpool.h"
26−#include "thread_queue.h"
2725
2826 #include "libavutil/avassert.h"
2927 #include "libavutil/avstring.h"
@@ -35,7 +33,6 @@ · 5 unmodified lines
3533 #include "libavutil/pixdesc.h"
3634 #include "libavutil/time.h"
3735 #include "libavutil/timestamp.h"
38−#include "libavutil/thread.h"
3936
4037 #include "libavcodec/packet.h"
4138
@@ -66,7 +63,11 @@ typedef struct DemuxStream { · 24 unmodified lines
6663
6764 double ts_scale;
6865
66+ // scheduler returned EOF for this stream
67+ int finished;
68+
6969 int streamcopy_needed;
70+ int have_sub2video;
7071
7172 int wrap_correction_done;
7273 int saw_first_ts;
@@ -101,6 +102,7 @@ typedef struct Demuxer { · 28 unmodified lines
101102
102103 /* number of times input stream should be looped */
103104 int loop;
105+ int have_audio_dec;
104106 /* duration of the looped segment of the input file */
105107 Timestamp duration;
106108 /* pts with the smallest/largest values ever seen */
@@ -113,11 +115,12 @@ typedef struct Demuxer { · 6 unmodified lines
113115 double readrate_initial_burst;
114116
115117 Scheduler *sch;
116− ThreadQueue *thread_queue;
117− int thread_queue_size;
118− pthread_t thread;
118+
119+ AVPacket *pkt_heartbeat;
119120
120121 int read_started;
122+ int nb_streams_used;
123+ int nb_streams_finished;
121124 } Demuxer;
122125
123126 static DemuxStream *ds_from_ist(InputStream *ist)
@@ -153,7 +156,7 @@ static void report_new_stream(Demuxer *d, const AVPacket *pkt) · 29 unmodified lines
153156 d->nb_streams_warn = pkt->stream_index + 1;
154157 }
155158
156−static int seek_to_start(Demuxer *d)
159+static int seek_to_start(Demuxer *d, Timestamp end_pts)
157160 {
158161 InputFile *ifile = &d->f;
159162 AVFormatContext *is = ifile->ctx;
@@ -163,21 +166,10 @@ static int seek_to_start(Demuxer *d) · 3 unmodified lines
163166 if (ret < 0)
164167 return ret;
165168
166− if (ifile->audio_ts_queue_size) {
167− int got_ts = 0;
168−
169− while (got_ts < ifile->audio_ts_queue_size) {
170− Timestamp ts;
171− ret = av_thread_message_queue_recv(ifile->audio_ts_queue, &ts, 0);
172− if (ret < 0)
173− return ret;
174− got_ts++;
175−
176− if (d->max_pts.ts == AV_NOPTS_VALUE ||
177− av_compare_ts(d->max_pts.ts, d->max_pts.tb, ts.ts, ts.tb) < 0)
178− d->max_pts = ts;
179− }
180− }
169+ if (end_pts.ts != AV_NOPTS_VALUE &&
170+ (d->max_pts.ts == AV_NOPTS_VALUE ||
171+ av_compare_ts(d->max_pts.ts, d->max_pts.tb, end_pts.ts, end_pts.tb) < 0))
172+ d->max_pts = end_pts;
181173
182174 if (d->max_pts.ts != AV_NOPTS_VALUE) {
183175 int64_t min_pts = d->min_pts.ts == AV_NOPTS_VALUE ? 0 : d->min_pts.ts;
@@ -404,7 +396,7 @@ static int ts_fixup(Demuxer *d, AVPacket *pkt) · 220 unmodified lines
404396 duration = av_rescale_q(d->duration.ts, d->duration.tb, pkt->time_base);
405397 if (pkt->pts != AV_NOPTS_VALUE) {
406398 // audio decoders take precedence for estimating total file duration
407− int64_t pkt_duration = ifile->audio_ts_queue_size ? 0 : pkt->duration;
399+ int64_t pkt_duration = d->have_audio_dec ? 0 : pkt->duration;
408400
409401 pkt->pts += duration;
410402
@@ -440,7 +432,7 @@ static int ts_fixup(Demuxer *d, AVPacket *pkt) · 29 unmodified lines
440432 return 0;
441433 }
442434
443−static int input_packet_process(Demuxer *d, AVPacket *pkt)
435+static int input_packet_process(Demuxer *d, AVPacket *pkt, unsigned *send_flags)
444436 {
445437 InputFile *f = &d->f;
446438 InputStream *ist = f->streams[pkt->stream_index];
@@ -451,6 +443,16 @@ static int input_packet_process(Demuxer *d, AVPacket *pkt) · 4 unmodified lines
451443 if (ret < 0)
452444 return ret;
453445
446+ if (f->recording_time != INT64_MAX) {
447+ int64_t start_time = 0;
448+ if (copy_ts) {
449+ start_time += f->start_time != AV_NOPTS_VALUE ? f->start_time : 0;
450+ start_time += start_at_zero ? 0 : f->start_time_effective;
451+ }
452+ if (ds->dts >= f->recording_time + start_time)
453+ *send_flags |= DEMUX_SEND_STREAMCOPY_EOF;
454+ }
455+
454456 ds->data_size += pkt->size;
455457 ds->nb_packets++;
456458
@@ -465,6 +467,8 @@ static int input_packet_process(Demuxer *d, AVPacket *pkt) · 8 unmodified lines
465467 av_ts2timestr(input_files[ist->file_index]->ts_offset, &AV_TIME_BASE_Q));
466468 }
467469
470+ pkt->stream_index = ds->sch_idx_stream;
471+
468472 return 0;
469473 }
470474
@@ -488,6 +492,65 @@ static void readrate_sleep(Demuxer *d) · 17 unmodified lines
488492 }
489493 }
490494
495+static int do_send(Demuxer *d, DemuxStream *ds, AVPacket *pkt, unsigned flags,
496+ const char *pkt_desc)
497+{
498+ int ret;
499+
500+ ret = sch_demux_send(d->sch, d->f.index, pkt, flags);
501+ if (ret == AVERROR_EOF) {
502+ av_packet_unref(pkt);
503+
504+ av_log(ds, AV_LOG_VERBOSE, "All consumers of this stream are done\n");
505+ ds->finished = 1;
506+
507+ if (++d->nb_streams_finished == d->nb_streams_used) {
508+ av_log(d, AV_LOG_VERBOSE, "All consumers are done\n");
509+ return AVERROR_EOF;
510+ }
511+ } else if (ret < 0) {
512+ if (ret != AVERROR_EXIT)
513+ av_log(d, AV_LOG_ERROR,
514+ "Unable to send %s packet to consumers: %s\n",
515+ pkt_desc, av_err2str(ret));
516+ return ret;
517+ }
518+
519+ return 0;
520+}
521+
522+static int demux_send(Demuxer *d, DemuxStream *ds, AVPacket *pkt, unsigned flags)
523+{
524+ InputFile *f = &d->f;
525+ int ret;
526+
527+ // send heartbeat for sub2video streams
528+ if (d->pkt_heartbeat && pkt->pts != AV_NOPTS_VALUE) {
529+ for (int i = 0; i < f->nb_streams; i++) {
530+ DemuxStream *ds1 = ds_from_ist(f->streams[i]);
531+
532+ if (ds1->finished || !ds1->have_sub2video)
533+ continue;
534+
535+ d->pkt_heartbeat->pts = pkt->pts;
536+ d->pkt_heartbeat->time_base = pkt->time_base;
537+ d->pkt_heartbeat->stream_index = ds1->sch_idx_stream;
538+ d->pkt_heartbeat->opaque = (void*)(intptr_t)PKT_OPAQUE_SUB_HEARTBEAT;
539+
540+ ret = do_send(d, ds1, d->pkt_heartbeat, 0, "heartbeat");
541+ if (ret < 0)
542+ return ret;
543+ }
544+ }
545+
546+ ret = do_send(d, ds, pkt, flags, "demuxed");
547+ if (ret < 0)
548+ return ret;
549+
550+
551+ return 0;
552+}
553+
491554 static void discard_unused_programs(InputFile *ifile)
492555 {
493556 for (int j = 0; j < ifile->ctx->nb_programs; j++) {
@@ -527,9 +590,13 @@ static void *input_thread(void *arg) · 33 unmodified lines
527590
528591 discard_unused_programs(f);
529592
593+ d->read_started = 1;
530594 d->wallclock_start = av_gettime_relative();
531595
532596 while (1) {
597+ DemuxStream *ds;
598+ unsigned send_flags = 0;
599+
533600 ret = av_read_frame(f->ctx, pkt);
534601
535602 if (ret == AVERROR(EAGAIN)) {
@@ -538,11 +605,13 @@ static void *input_thread(void *arg) · 2 unmodified lines
538605 }
539606 if (ret < 0) {
540607 if (d->loop) {
541− /* signal looping to the consumer thread */
608+ /* signal looping to our consumers */
542609 pkt->stream_index = -1;
543− ret = tq_send(d->thread_queue, 0, pkt);
610+
611+ ret = sch_demux_send(d->sch, f->index, pkt, 0);
544612 if (ret >= 0)
545− ret = seek_to_start(d);
613+ ret = seek_to_start(d, (Timestamp){ .ts = pkt->pts,
614+ .tb = pkt->time_base });
546615 if (ret >= 0)
547616 continue;
548617
@@ -551,9 +620,11 @@ static void *input_thread(void *arg) · 2 unmodified lines
551620
552621 if (ret == AVERROR_EOF)
553622 av_log(d, AV_LOG_VERBOSE, "EOF while reading input\n");
554− else
623+ else {
555624 av_log(d, AV_LOG_ERROR, "Error during demuxing: %s\n",
556625 av_err2str(ret));
626+ ret = exit_on_error ? ret : 0;
627+ }
557628
558629 break;
559630 }
@@ -565,8 +636,9 @@ static void *input_thread(void *arg) · 5 unmodified lines
565636
566637 /* the following test is needed in case new streams appear
567638 dynamically in stream : we ignore them */
568− if (pkt->stream_index >= f->nb_streams ||
569− f->streams[pkt->stream_index]->discard) {
639+ ds = pkt->stream_index < f->nb_streams ?
640+ ds_from_ist(f->streams[pkt->stream_index]) : NULL;
641+ if (!ds || ds->ist.discard || ds->finished) {
570642 report_new_stream(d, pkt);
571643 av_packet_unref(pkt);
572644 continue;
@@ -583,122 +655,26 @@ static void *input_thread(void *arg) · 10 unmodified lines
583655 }
584656 }
585657
586− ret = input_packet_process(d, pkt);
658+ ret = input_packet_process(d, pkt, &send_flags);
587659 if (ret < 0)
588660 break;
589661
590662 if (f->readrate)
591663 readrate_sleep(d);
592664
593− ret = tq_send(d->thread_queue, 0, pkt);
594− if (ret < 0) {
595− if (ret != AVERROR_EOF)
596− av_log(f, AV_LOG_ERROR,
597− "Unable to send packet to main thread: %s\n",
598− av_err2str(ret));
665+ ret = demux_send(d, ds, pkt, send_flags);
666+ if (ret < 0)
599667 break;
600− }
601668 }
602669
603−finish:
604− av_assert0(ret < 0);
605− tq_send_finish(d->thread_queue, 0);
670+ // EOF/EXIT is normal termination
671+ if (ret == AVERROR_EOF || ret == AVERROR_EXIT)
672+ ret = 0;
606673
674+finish:
607675 av_packet_free(&pkt);
608676
609− av_log(d, AV_LOG_VERBOSE, "Terminating demuxer thread\n");
610−
611− return NULL;
612−}
613−
614−static void thread_stop(Demuxer *d)
615−{
616− InputFile *f = &d->f;
617−
618− if (!d->thread_queue)
619− return;
620−
621− tq_receive_finish(d->thread_queue, 0);
622−
623− pthread_join(d->thread, NULL);
624−
625− tq_free(&d->thread_queue);
626−
627− av_thread_message_queue_free(&f->audio_ts_queue);
628−}
629−
630−static int thread_start(Demuxer *d)
631−{
632− int ret;
633− InputFile *f = &d->f;
634− ObjPool *op;
635−
636− if (d->thread_queue_size <= 0)
637− d->thread_queue_size = (nb_input_files > 1 ? 8 : 1);
638−
639− op = objpool_alloc_packets();
640− if (!op)
641− return AVERROR(ENOMEM);
642−
643− d->thread_queue = tq_alloc(1, d->thread_queue_size, op, pkt_move);
644− if (!d->thread_queue) {
645− objpool_free(&op);
646− return AVERROR(ENOMEM);
647− }
648−
649− if (d->loop) {
650− int nb_audio_dec = 0;
651−
652− for (int i = 0; i < f->nb_streams; i++) {
653− InputStream *ist = f->streams[i];
654− nb_audio_dec += !!(ist->decoding_needed &&
655− ist->st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO);
656− }
657−
658− if (nb_audio_dec) {
659− ret = av_thread_message_queue_alloc(&f->audio_ts_queue,
660− nb_audio_dec, sizeof(Timestamp));
661− if (ret < 0)
662− goto fail;
663− f->audio_ts_queue_size = nb_audio_dec;
664− }
665− }
666−
667− if ((ret = pthread_create(&d->thread, NULL, input_thread, d))) {
668− av_log(d, AV_LOG_ERROR, "pthread_create failed: %s. Try to increase `ulimit -v` or decrease `ulimit -s`.\n", strerror(ret));
669− ret = AVERROR(ret);
670− goto fail;
671− }
672−
673− d->read_started = 1;
674−
675− return 0;
676−fail:
677− tq_free(&d->thread_queue);
678− return ret;
679−}
680−
681−int ifile_get_packet(InputFile *f, AVPacket *pkt)
682−{
683− Demuxer *d = demuxer_from_ifile(f);
684− int ret, dummy;
685−
686− if (!d->thread_queue) {
687− ret = thread_start(d);
688− if (ret < 0)
689− return ret;
690− }
691−
692− ret = tq_receive(d->thread_queue, &dummy, pkt);
693− if (ret < 0)
694− return ret;
695−
696− if (pkt->stream_index == -1) {
697− av_assert0(!pkt->data && !pkt->side_data_elems);
698− return 1;
699− }
700−
701− return 0;
677+ return (void*)(intptr_t)ret;
702678 }
703679
704680 static void demux_final_stats(Demuxer *d)
@@ -769,8 +745,6 @@ void ifile_close(InputFile **pf) · 64 unmodified lines
769745 if (!f)
770746 return;
771747
772− thread_stop(d);
773−
774748 if (d->read_started)
775749 demux_final_stats(d);
776750
@@ -780,6 +754,8 @@ void ifile_close(InputFile **pf) · 3 unmodified lines
780754
781755 avformat_close_input(&f->ctx);
782756
757+ av_packet_free(&d->pkt_heartbeat);
758+
783759 av_freep(pf);
784760 }
785761
@@ -802,7 +778,11 @@ static int ist_use(InputStream *ist, int decoding_needed) · 16 unmodified lines
802778 ds->sch_idx_stream = ret;
803779 }
804780
805− ist->discard = 0;
781+ if (ist->discard) {
782+ ist->discard = 0;
783+ d->nb_streams_used++;
784+ }
785+
806786 ist->st->discard = ist->user_set_discard;
807787 ist->decoding_needed |= decoding_needed;
808788 ds->streamcopy_needed |= !decoding_needed;
@@ -823,6 +803,8 @@ static int ist_use(InputStream *ist, int decoding_needed) · 14 unmodified lines
823803 ret = dec_open(ist, d->sch, ds->sch_idx_dec);
824804 if (ret < 0)
825805 return ret;
806+
807+ d->have_audio_dec |= is_audio;
826808 }
827809
828810 return 0;
@@ -848,6 +830,7 @@ int ist_output_add(InputStream *ist, OutputStream *ost) · 19 unmodified lines
848830
849831 int ist_filter_add(InputStream *ist, InputFilter *ifilter, int is_simple)
850832 {
833+ Demuxer *d = demuxer_from_ifile(input_files[ist->file_index]);
851834 DemuxStream *ds = ds_from_ist(ist);
852835 int ret;
853836
@@ -866,6 +849,15 @@ int ist_filter_add(InputStream *ist, InputFilter *ifilter, int is_simple) · 12 unmodified lines
866849 if (ret < 0)
867850 return ret;
868851
852+ if (ist->dec_ctx->codec_type == AVMEDIA_TYPE_SUBTITLE) {
853+ if (!d->pkt_heartbeat) {
854+ d->pkt_heartbeat = av_packet_alloc();
855+ if (!d->pkt_heartbeat)
856+ return AVERROR(ENOMEM);
857+ }
858+ ds->have_sub2video = 1;
859+ }
860+
869861 return ds->sch_idx_dec;
870862 }
871863
@@ -1607,8 +1599,6 @@ int ifile_open(const OptionsContext *o, const char *filename, Scheduler *sch) · 735 unmodified lines
16071599 "since neither -readrate nor -re were given\n");
16081600 }
16091601
1610− d->thread_queue_size = o->thread_queue_size;
1611−
16121602 /* Add all the streams from the given input file to the demuxer */
16131603 for (int i = 0; i < ic->nb_streams; i++) {
16141604 ret = ist_add(o, d, ic->streams[i]);
fftools/ffmpeg_enc.c+64−313
@@ -41,12 +41,6 @@ · 40 unmodified lines
4141 #include "libavformat/avformat.h"
4242
4343 struct Encoder {
44− AVFrame *sq_frame;
45−
46− // packet for receiving encoded output
47− AVPacket *pkt;
48− AVFrame *sub_frame;
49−
5044 // combined size of all the packets received from the encoder
5145 uint64_t data_size;
5246
@@ -54,25 +48,9 @@ struct Encoder { · 1 unmodified line
5448 uint64_t packets_encoded;
5549
5650 int opened;
57− int finished;
5851
5952 Scheduler *sch;
6053 unsigned sch_idx;
61−
62− pthread_t thread;
63− /**
64− * Queue for sending frames from the main thread to
65− * the encoder thread.
66− */
67− ThreadQueue *queue_in;
68− /**
69− * Queue for sending encoded packets from the encoder thread
70− * to the main thread.
71− *
72− * An empty packet is sent to signal that a previously sent
73− * frame has been fully processed.
74− */
75− ThreadQueue *queue_out;
7654 };
7755
7856 // data that is local to the decoder thread and not visible outside of it
@@ -81,24 +59,6 @@ typedef struct EncoderThread { · 2 unmodified lines
8159 AVPacket *pkt;
8260 } EncoderThread;
8361
84−static int enc_thread_stop(Encoder *e)
85−{
86− void *ret;
87−
88− if (!e->queue_in)
89− return 0;
90−
91− tq_send_finish(e->queue_in, 0);
92− tq_receive_finish(e->queue_out, 0);
93−
94− pthread_join(e->thread, &ret);
95−
96− tq_free(&e->queue_in);
97− tq_free(&e->queue_out);
98−
99− return (int)(intptr_t)ret;
100−}
101−
10262 void enc_free(Encoder **penc)
10363 {
10464 Encoder *enc = *penc;
@@ -106,13 +66,6 @@ void enc_free(Encoder **penc) · 1 unmodified line
10666 if (!enc)
10767 return;
10868
109− enc_thread_stop(enc);
110−
111− av_frame_free(&enc->sq_frame);
112− av_frame_free(&enc->sub_frame);
113−
114− av_packet_free(&enc->pkt);
115−
11669 av_freep(penc);
11770 }
11871
@@ -127,25 +80,12 @@ int enc_alloc(Encoder **penc, const AVCodec *codec, · 8 unmodified lines
12780 if (!enc)
12881 return AVERROR(ENOMEM);
12982
130− if (codec->type == AVMEDIA_TYPE_SUBTITLE) {
131− enc->sub_frame = av_frame_alloc();
132− if (!enc->sub_frame)
133− goto fail;
134− }
135−
136− enc->pkt = av_packet_alloc();
137− if (!enc->pkt)
138− goto fail;
139−
14083 enc->sch = sch;
14184 enc->sch_idx = sch_idx;
14285
14386 *penc = enc;
14487
14588 return 0;
146−fail:
147− enc_free(&enc);
148− return AVERROR(ENOMEM);
14989 }
15090
15191 static int hw_device_setup_for_encode(OutputStream *ost, AVBufferRef *frames_ref)
@@ -224,52 +164,9 @@ static int set_encoder_id(OutputFile *of, OutputStream *ost) · 72 unmodified lines
224164 return 0;
225165 }
226166
227−static int enc_thread_start(OutputStream *ost)
228−{
229− Encoder *e = ost->enc;
230− ObjPool *op;
231− int ret = 0;
232−
233− op = objpool_alloc_frames();
234− if (!op)
235− return AVERROR(ENOMEM);
236−
237− e->queue_in = tq_alloc(1, 1, op, frame_move);
238− if (!e->queue_in) {
239− objpool_free(&op);
240− return AVERROR(ENOMEM);
241− }
242−
243− op = objpool_alloc_packets();
244− if (!op)
245− goto fail;
246−
247− e->queue_out = tq_alloc(1, 4, op, pkt_move);
248− if (!e->queue_out) {
249− objpool_free(&op);
250− goto fail;
251− }
252−
253− ret = pthread_create(&e->thread, NULL, encoder_thread, ost);
254− if (ret) {
255− ret = AVERROR(ret);
256− av_log(ost, AV_LOG_ERROR, "pthread_create() failed: %s\n",
257− av_err2str(ret));
258− goto fail;
259− }
260−
261− return 0;
262−fail:
263− if (ret >= 0)
264− ret = AVERROR(ENOMEM);
265−
266− tq_free(&e->queue_in);
267− tq_free(&e->queue_out);
268− return ret;
269−}
270−
271−int enc_open(OutputStream *ost, const AVFrame *frame)
167+int enc_open(void *opaque, const AVFrame *frame)
272168 {
169+ OutputStream *ost = opaque;
273170 InputStream *ist = ost->ist;
274171 Encoder *e = ost->enc;
275172 AVCodecContext *enc_ctx = ost->enc_ctx;
@@ -277,6 +174,7 @@ int enc_open(OutputStream *ost, const AVFrame *frame) · 1 unmodified line
277174 const AVCodec *enc = enc_ctx->codec;
278175 OutputFile *of = output_files[ost->file_index];
279176 FrameData *fd;
177+ int frame_samples = 0;
280178 int ret;
281179
282180 if (e->opened)
@@ -420,17 +318,8 @@ int enc_open(OutputStream *ost, const AVFrame *frame) · 137 unmodified lines
420318
421319 e->opened = 1;
422320
423− if (ost->sq_idx_encode >= 0) {
424− e->sq_frame = av_frame_alloc();
425− if (!e->sq_frame)
426− return AVERROR(ENOMEM);
427− }
428−
429− if (ost->enc_ctx->frame_size) {
430− av_assert0(ost->sq_idx_encode >= 0);
431− sq_frame_samples(output_files[ost->file_index]->sq_encode,
432− ost->sq_idx_encode, ost->enc_ctx->frame_size);
433− }
321+ if (ost->enc_ctx->frame_size)
322+ frame_samples = ost->enc_ctx->frame_size;
434323
435324 ret = check_avoptions(ost->encoder_opts);
436325 if (ret < 0)
@@ -476,18 +365,11 @@ int enc_open(OutputStream *ost, const AVFrame *frame) · 39 unmodified lines
476365 if (ost->st->time_base.num <= 0 || ost->st->time_base.den <= 0)
477366 ost->st->time_base = av_add_q(ost->enc_ctx->time_base, (AVRational){0, 1});
478367
479− ret = enc_thread_start(ost);
480− if (ret < 0) {
481− av_log(ost, AV_LOG_ERROR, "Error starting encoder thread: %s\n",
482− av_err2str(ret));
483− return ret;
484− }
485−
486368 ret = of_stream_init(of, ost);
487369 if (ret < 0)
488370 return ret;
489371
490− return 0;
372+ return frame_samples;
491373 }
492374
493375 static int check_recording_time(OutputStream *ost, int64_t ts, AVRational tb)
@@ -514,8 +396,7 @@ static int do_subtitle_out(OutputFile *of, OutputStream *ost, const AVSubtitle * · 20 unmodified lines
514396 av_log(ost, AV_LOG_ERROR, "Subtitle packets must have a pts\n");
515397 return exit_on_error ? AVERROR(EINVAL) : 0;
516398 }
517− if (ost->finished ||
518− (of->start_time != AV_NOPTS_VALUE && sub->pts < of->start_time))
399+ if ((of->start_time != AV_NOPTS_VALUE && sub->pts < of->start_time))
519400 return 0;
520401
521402 enc = ost->enc_ctx;
@@ -579,7 +460,7 @@ static int do_subtitle_out(OutputFile *of, OutputStream *ost, const AVSubtitle * · 57 unmodified lines
579460 }
580461 pkt->dts = pkt->pts;
581462
582− ret = tq_send(e->queue_out, 0, pkt);
463+ ret = sch_enc_send(e->sch, e->sch_idx, pkt);
583464 if (ret < 0) {
584465 av_packet_unref(pkt);
585466 return ret;
@@ -671,10 +552,13 @@ static int update_video_stats(OutputStream *ost, const AVPacket *pkt, int write_ · 85 unmodified lines
671552 int64_t frame_number;
672553 double ti1, bitrate, avg_bitrate;
673554 double psnr_val = -1;
555+ int quality;
674556
675− ost->quality = sd ? AV_RL32(sd) : -1;
557+ quality = sd ? AV_RL32(sd) : -1;
676558 pict_type = sd ? sd[4] : AV_PICTURE_TYPE_NONE;
677559
560+ atomic_store(&ost->quality, quality);
561+
678562 if ((enc->flags & AV_CODEC_FLAG_PSNR) && sd && sd[5]) {
679563 // FIXME the scaling assumes 8bit
680564 double error = AV_RL64(sd + 8) / (enc->width * enc->height * 255.0 * 255.0);
@@ -697,10 +581,10 @@ static int update_video_stats(OutputStream *ost, const AVPacket *pkt, int write_ · 16 unmodified lines
697581 frame_number = e->packets_encoded;
698582 if (vstats_version <= 1) {
699583 fprintf(vstats_file, "frame= %5"PRId64" q= %2.1f ", frame_number,
700− ost->quality / (float)FF_QP2LAMBDA);
584+ quality / (float)FF_QP2LAMBDA);
701585 } else {
702586 fprintf(vstats_file, "out= %2d st= %2d frame= %5"PRId64" q= %2.1f ", ost->file_index, ost->index, frame_number,
703− ost->quality / (float)FF_QP2LAMBDA);
587+ quality / (float)FF_QP2LAMBDA);
704588 }
705589
706590 if (psnr_val >= 0)
@@ -801,18 +685,11 @@ static int encode_frame(OutputFile *of, OutputStream *ost, AVFrame *frame, · 94 unmodified lines
801685 av_ts2str(pkt->duration), av_ts2timestr(pkt->duration, &enc->time_base));
802686 }
803687
804− if ((ret = trigger_fix_sub_duration_heartbeat(ost, pkt)) < 0) {
805− av_log(NULL, AV_LOG_ERROR,
806− "Subtitle heartbeat logic failed in %s! (%s)\n",
807− __func__, av_err2str(ret));
808− return ret;
809− }
810−
811688 e->data_size += pkt->size;
812689
813690 e->packets_encoded++;
814691
815− ret = tq_send(e->queue_out, 0, pkt);
692+ ret = sch_enc_send(e->sch, e->sch_idx, pkt);
816693 if (ret < 0) {
817694 av_packet_unref(pkt);
818695 return ret;
@@ -822,50 +699,6 @@ static int encode_frame(OutputFile *of, OutputStream *ost, AVFrame *frame, · 3 unmodified lines
822699 av_assert0(0);
823700 }
824701
825−static int submit_encode_frame(OutputFile *of, OutputStream *ost,
826− AVFrame *frame, AVPacket *pkt)
827−{
828− Encoder *e = ost->enc;
829− int ret;
830−
831− if (ost->sq_idx_encode < 0)
832− return encode_frame(of, ost, frame, pkt);
833−
834− if (frame) {
835− ret = av_frame_ref(e->sq_frame, frame);
836− if (ret < 0)
837− return ret;
838− frame = e->sq_frame;
839− }
840−
841− ret = sq_send(of->sq_encode, ost->sq_idx_encode,
842− SQFRAME(frame));
843− if (ret < 0) {
844− if (frame)
845− av_frame_unref(frame);
846− if (ret != AVERROR_EOF)
847− return ret;
848− }
849−
850− while (1) {
851− AVFrame *enc_frame = e->sq_frame;
852−
853− ret = sq_receive(of->sq_encode, ost->sq_idx_encode,
854− SQFRAME(enc_frame));
855− if (ret == AVERROR_EOF) {
856− enc_frame = NULL;
857− } else if (ret < 0) {
858− return (ret == AVERROR(EAGAIN)) ? 0 : ret;
859− }
860−
861− ret = encode_frame(of, ost, enc_frame, pkt);
862− if (enc_frame)
863− av_frame_unref(enc_frame);
864− if (ret < 0)
865− return ret;
866− }
867−}
868−
869702 static int do_audio_out(OutputFile *of, OutputStream *ost,
870703 AVFrame *frame, AVPacket *pkt)
871704 {
@@ -881,7 +714,7 @@ static int do_audio_out(OutputFile *of, OutputStream *ost, · 9 unmodified lines
881714 if (!check_recording_time(ost, frame->pts, frame->time_base))
882715 return AVERROR_EOF;
883716
884− return submit_encode_frame(of, ost, frame, pkt);
717+ return encode_frame(of, ost, frame, pkt);
885718 }
886719
887720 static enum AVPictureType forced_kf_apply(void *logctx, KeyframeForceCtx *kf,
@@ -949,7 +782,7 @@ static int do_video_out(OutputFile *of, OutputStream *ost, · 61 unmodified lines
949782 }
950783 #endif
951784
952− return submit_encode_frame(of, ost, in_picture, pkt);
785+ return encode_frame(of, ost, in_picture, pkt);
953786 }
954787
955788 static int frame_encode(OutputStream *ost, AVFrame *frame, AVPacket *pkt)
@@ -958,9 +791,12 @@ static int frame_encode(OutputStream *ost, AVFrame *frame, AVPacket *pkt) · 2 unmodified lines
958791 enum AVMediaType type = ost->type;
959792
960793 if (type == AVMEDIA_TYPE_SUBTITLE) {
794+ const AVSubtitle *subtitle = frame && frame->buf[0] ?
795+ (AVSubtitle*)frame->buf[0]->data : NULL;
796+
961797 // no flushing for subtitles
962− return frame ?
963− do_subtitle_out(of, ost, (AVSubtitle*)frame->buf[0]->data, pkt) : 0;
798+ return subtitle && subtitle->num_rects ?
799+ do_subtitle_out(of, ost, subtitle, pkt) : 0;
964800 }
965801
966802 if (frame) {
@@ -968,7 +804,7 @@ static int frame_encode(OutputStream *ost, AVFrame *frame, AVPacket *pkt) · 1 unmodified line
968804 do_audio_out(of, ost, frame, pkt);
969805 }
970806
971− return submit_encode_frame(of, ost, NULL, pkt);
807+ return encode_frame(of, ost, NULL, pkt);
972808 }
973809
974810 static void enc_thread_set_name(const OutputStream *ost)
@@ -1009,25 +845,52 @@ fail: · 34 unmodified lines
1009845 void *encoder_thread(void *arg)
1010846 {
1011847 OutputStream *ost = arg;
1012− OutputFile *of = output_files[ost->file_index];
1013848 Encoder *e = ost->enc;
1014849 EncoderThread et;
1015850 int ret = 0, input_status = 0;
851+ int name_set = 0;
1016852
1017853 ret = enc_thread_init(&et);
1018854 if (ret < 0)
1019855 goto finish;
1020856
1021− enc_thread_set_name(ost);
857+ /* Open the subtitle encoders immediately. AVFrame-based encoders
858+ * are opened through a callback from the scheduler once they get
859+ * their first frame
860+ *
861+ * N.B.: because the callback is called from a different thread,
862+ * enc_ctx MUST NOT be accessed before sch_enc_receive() returns
863+ * for the first time for audio/video. */
864+ if (ost->type != AVMEDIA_TYPE_VIDEO && ost->type != AVMEDIA_TYPE_AUDIO) {
865+ ret = enc_open(ost, NULL);
866+ if (ret < 0)
867+ goto finish;
868+ }
1022869
1023870 while (!input_status) {
1024− int dummy;
871+ input_status = sch_enc_receive(e->sch, e->sch_idx, et.frame);
872+ if (input_status < 0) {
873+ if (input_status == AVERROR_EOF) {
874+ av_log(ost, AV_LOG_VERBOSE, "Encoder thread received EOF\n");
875+ if (e->opened)
876+ break;
877+
878+ av_log(ost, AV_LOG_ERROR, "Could not open encoder before EOF\n");
879+ ret = AVERROR(EINVAL);
880+ } else {
881+ av_log(ost, AV_LOG_ERROR, "Error receiving a frame for encoding: %s\n",
882+ av_err2str(ret));
883+ ret = input_status;
884+ }
885+ goto finish;
886+ }
1025887
1026− input_status = tq_receive(e->queue_in, &dummy, et.frame);
1027− if (input_status < 0)
1028− av_log(ost, AV_LOG_VERBOSE, "Encoder thread received EOF\n");
888+ if (!name_set) {
889+ enc_thread_set_name(ost);
890+ name_set = 1;
891+ }
1029892
1030− ret = frame_encode(ost, input_status >= 0 ? et.frame : NULL, et.pkt);
893+ ret = frame_encode(ost, et.frame, et.pkt);
1031894
1032895 av_packet_unref(et.pkt);
1033896 av_frame_unref(et.frame);
@@ -1040,15 +903,14 @@ void *encoder_thread(void *arg) · 6 unmodified lines
1040903 av_err2str(ret));
1041904 break;
1042905 }
906+ }
1043907
1044− // signal to the consumer thread that the frame was encoded
1045− ret = tq_send(e->queue_out, 0, et.pkt);
1046− if (ret < 0) {
1047− if (ret != AVERROR_EOF)
1048− av_log(ost, AV_LOG_ERROR,
1049− "Error communicating with the main thread\n");
1050− break;
1051− }
908+ // flush the encoder
909+ if (ret == 0 || ret == AVERROR_EOF) {
910+ ret = frame_encode(ost, NULL, et.pkt);
911+ if (ret < 0 && ret != AVERROR_EOF)
912+ av_log(ost, AV_LOG_ERROR, "Error flushing encoder: %s\n",
913+ av_err2str(ret));
1052914 }
1053915
1054916 // EOF is normal thread termination
@@ -1056,118 +918,7 @@ void *encoder_thread(void *arg) · 1 unmodified line
1056918 ret = 0;
1057919
1058920 finish:
1059− if (ost->sq_idx_encode >= 0)
1060− sq_send(of->sq_encode, ost->sq_idx_encode, SQFRAME(NULL));
1061−
1062− tq_receive_finish(e->queue_in, 0);
1063− tq_send_finish (e->queue_out, 0);
1064−
1065921 enc_thread_uninit(&et);
1066922
1067− av_log(ost, AV_LOG_VERBOSE, "Terminating encoder thread\n");
1068−
1069923 return (void*)(intptr_t)ret;
1070924 }
1071−
1072−int enc_frame(OutputStream *ost, AVFrame *frame)
1073−{
1074− OutputFile *of = output_files[ost->file_index];
1075− Encoder *e = ost->enc;
1076− int ret, thread_ret;
1077−
1078− ret = enc_open(ost, frame);
1079− if (ret < 0)
1080− return ret;
1081−
1082− if (!e->queue_in)
1083− return AVERROR_EOF;
1084−
1085− // send the frame/EOF to the encoder thread
1086− if (frame) {
1087− ret = tq_send(e->queue_in, 0, frame);
1088− if (ret < 0)
1089− goto finish;
1090− } else
1091− tq_send_finish(e->queue_in, 0);
1092−
1093− // retrieve all encoded data for the frame
1094− while (1) {
1095− int dummy;
1096−
1097− ret = tq_receive(e->queue_out, &dummy, e->pkt);
1098− if (ret < 0)
1099− break;
1100−
1101− // frame fully encoded
1102− if (!e->pkt->data && !e->pkt->side_data_elems)
1103− return 0;
1104−
1105− // process the encoded packet
1106− ret = of_output_packet(of, ost, e->pkt);
1107− if (ret < 0)
1108− goto finish;
1109− }
1110−
1111−finish:
1112− thread_ret = enc_thread_stop(e);
1113− if (thread_ret < 0) {
1114− av_log(ost, AV_LOG_ERROR, "Encoder thread returned error: %s\n",
1115− av_err2str(thread_ret));
1116− ret = err_merge(ret, thread_ret);
1117− }
1118−
1119− if (ret < 0 && ret != AVERROR_EOF)
1120− return ret;
1121−
1122− // signal EOF to the muxer
1123− return of_output_packet(of, ost, NULL);
1124−}
1125−
1126−int enc_subtitle(OutputFile *of, OutputStream *ost, const AVSubtitle *sub)
1127−{
1128− Encoder *e = ost->enc;
1129− AVFrame *f = e->sub_frame;
1130− int ret;
1131−
1132− // XXX the queue for transferring data to the encoder thread runs
1133− // on AVFrames, so we wrap AVSubtitle in an AVBufferRef and put
1134− // that inside the frame
1135− // eventually, subtitles should be switched to use AVFrames natively
1136− ret = subtitle_wrap_frame(f, sub, 1);
1137− if (ret < 0)
1138− return ret;
1139−
1140− ret = enc_frame(ost, f);
1141− av_frame_unref(f);
1142−
1143− return ret;
1144−}
1145−
1146−int enc_flush(void)
1147−{
1148− int ret = 0;
1149−
1150− for (OutputStream *ost = ost_iter(NULL); ost; ost = ost_iter(ost)) {
1151− OutputFile *of = output_files[ost->file_index];
1152− if (ost->sq_idx_encode >= 0)
1153− sq_send(of->sq_encode, ost->sq_idx_encode, SQFRAME(NULL));
1154− }
1155−
1156− for (OutputStream *ost = ost_iter(NULL); ost; ost = ost_iter(ost)) {
1157− Encoder *e = ost->enc;
1158− AVCodecContext *enc = ost->enc_ctx;
1159− int err;
1160−
1161− if (!enc || !e->opened ||
1162− (enc->codec_type != AVMEDIA_TYPE_VIDEO && enc->codec_type != AVMEDIA_TYPE_AUDIO))
1163− continue;
1164−
1165− err = enc_frame(ost, NULL);
1166− if (err != AVERROR_EOF && ret < 0)
1167− ret = err_merge(ret, err);
1168−
1169− av_assert0(!e->queue_in);
1170− }
1171−
1172− return ret;
1173−}
fftools/ffmpeg_filter.c+194−520
@@ -21,8 +21,6 @@ · 20 unmodified lines
2121 #include <stdint.h>
2222
2323 #include "ffmpeg.h"
24−#include "ffmpeg_utils.h"
25−#include "thread_queue.h"
2624
2725 #include "libavfilter/avfilter.h"
2826 #include "libavfilter/buffersink.h"
@@ -53,10 +51,11 @@ typedef struct FilterGraphPriv { · 24 unmodified lines
5351 // true when the filtergraph contains only meta filters
5452 // that do not modify the frame data
5553 int is_meta;
54+ // source filters are present in the graph
55+ int have_sources;
5656 int disable_conversions;
5757
58− int nb_inputs_bound;
59− int nb_outputs_bound;
58+ unsigned nb_outputs_done;
6059
6160 const char *graph_desc;
6261
@@ -67,41 +66,6 @@ typedef struct FilterGraphPriv { · 4 unmodified lines
6766
6867 Scheduler *sch;
6968 unsigned sch_idx;
70−
71− pthread_t thread;
72− /**
73− * Queue for sending frames from the main thread to the filtergraph. Has
74− * nb_inputs+1 streams - the first nb_inputs stream correspond to
75− * filtergraph inputs. Frames on those streams may have their opaque set to
76− * - FRAME_OPAQUE_EOF: frame contains no data, but pts+timebase of the
77− * EOF event for the correspondint stream. Will be immediately followed by
78− * this stream being send-closed.
79− * - FRAME_OPAQUE_SUB_HEARTBEAT: frame contains no data, but pts+timebase of
80− * a subtitle heartbeat event. Will only be sent for sub2video streams.
81− *
82− * The last stream is "control" - the main thread sends empty AVFrames with
83− * opaque set to
84− * - FRAME_OPAQUE_REAP_FILTERS: a request to retrieve all frame available
85− * from filtergraph outputs. These frames are sent to corresponding
86− * streams in queue_out. Finally an empty frame is sent to the control
87− * stream in queue_out.
88− * - FRAME_OPAQUE_CHOOSE_INPUT: same as above, but in case no frames are
89− * available the terminating empty frame's opaque will contain the index+1
90− * of the filtergraph input to which more input frames should be supplied.
91− */
92− ThreadQueue *queue_in;
93− /**
94− * Queue for sending frames from the filtergraph back to the main thread.
95− * Has nb_outputs+1 streams - the first nb_outputs stream correspond to
96− * filtergraph outputs.
97− *
98− * The last stream is "control" - see documentation for queue_in for more
99− * details.
100− */
101− ThreadQueue *queue_out;
102− // submitting frames to filter thread returned EOF
103− // this only happens on thread exit, so is not per-input
104− int eof_in;
10569 } FilterGraphPriv;
10670
10771 static FilterGraphPriv *fgp_from_fg(FilterGraph *fg)
@@ -123,6 +87,9 @@ typedef struct FilterGraphThread { · 15 unmodified lines
12387 // The output index is stored in frame opaque.
12488 AVFifo *frame_queue_out;
12589
90+ // index of the next input to request from the scheduler
91+ unsigned next_in;
92+ // set to 1 after at least one frame passed through this output
12693 int got_frame;
12794
12895 // EOF status of each input/output, as received by the thread
@@ -253,9 +220,6 @@ typedef struct OutputFilterPriv { · 124 unmodified lines
253220 int64_t ts_offset;
254221 int64_t next_pts;
255222 FPSConvContext fps;
256−
257− // set to 1 after at least one frame passed through this output
258− int got_frame;
259223 } OutputFilterPriv;
260224
261225 static OutputFilterPriv *ofp_from_ofilter(OutputFilter *ofilter)
@@ -653,57 +617,6 @@ static int ifilter_has_all_input_formats(FilterGraph *fg) · 391 unmodified lines
653617
654618 static void *filter_thread(void *arg);
655619
656−// start the filtering thread once all inputs and outputs are bound
657−static int fg_thread_try_start(FilterGraphPriv *fgp)
658−{
659− FilterGraph *fg = &fgp->fg;
660− ObjPool *op;
661− int ret = 0;
662−
663− if (fgp->nb_inputs_bound < fg->nb_inputs ||
664− fgp->nb_outputs_bound < fg->nb_outputs)
665− return 0;
666−
667− op = objpool_alloc_frames();
668− if (!op)
669− return AVERROR(ENOMEM);
670−
671− fgp->queue_in = tq_alloc(fg->nb_inputs + 1, 1, op, frame_move);
672− if (!fgp->queue_in) {
673− objpool_free(&op);
674− return AVERROR(ENOMEM);
675− }
676−
677− // at least one output is mandatory
678− op = objpool_alloc_frames();
679− if (!op)
680− goto fail;
681−
682− fgp->queue_out = tq_alloc(fg->nb_outputs + 1, 1, op, frame_move);
683− if (!fgp->queue_out) {
684− objpool_free(&op);
685− goto fail;
686− }
687−
688− ret = pthread_create(&fgp->thread, NULL, filter_thread, fgp);
689− if (ret) {
690− ret = AVERROR(ret);
691− av_log(NULL, AV_LOG_ERROR, "pthread_create() for filtergraph %d failed: %s\n",
692− fg->index, av_err2str(ret));
693− goto fail;
694− }
695−
696− return 0;
697−fail:
698− if (ret >= 0)
699− ret = AVERROR(ENOMEM);
700−
701− tq_free(&fgp->queue_in);
702− tq_free(&fgp->queue_out);
703−
704− return ret;
705−}
706−
707620 static char *describe_filter_link(FilterGraph *fg, AVFilterInOut *inout, int in)
708621 {
709622 AVFilterContext *ctx = inout->filter_ctx;
@@ -729,7 +642,6 @@ static OutputFilter *ofilter_alloc(FilterGraph *fg) · 19 unmodified lines
729642 ofilter->graph = fg;
730643 ofp->format = -1;
731644 ofp->index = fg->nb_outputs - 1;
732− ofilter->last_pts = AV_NOPTS_VALUE;
733645
734646 return ofilter;
735647 }
@@ -760,10 +672,7 @@ static int ifilter_bind_ist(InputFilter *ifilter, InputStream *ist) · 24 unmodified lines
760672 return AVERROR(ENOMEM);
761673 }
762674
763− fgp->nb_inputs_bound++;
764− av_assert0(fgp->nb_inputs_bound <= ifilter->graph->nb_inputs);
765−
766− return fg_thread_try_start(fgp);
675+ return 0;
767676 }
768677
769678 static int set_channel_layout(OutputFilterPriv *f, OutputStream *ost)
@@ -902,10 +811,7 @@ int ofilter_bind_ost(OutputFilter *ofilter, OutputStream *ost, · 132 unmodified lines
902811 if (ret < 0)
903812 return ret;
904813
905− fgp->nb_outputs_bound++;
906− av_assert0(fgp->nb_outputs_bound <= fg->nb_outputs);
907−
908− return fg_thread_try_start(fgp);
814+ return 0;
909815 }
910816
911817 static InputFilter *ifilter_alloc(FilterGraph *fg)
@@ -935,34 +841,6 @@ static InputFilter *ifilter_alloc(FilterGraph *fg) · 23 unmodified lines
935841 return ifilter;
936842 }
937843
938−static int fg_thread_stop(FilterGraphPriv *fgp)
939−{
940− void *ret;
941−
942− if (!fgp->queue_in)
943− return 0;
944−
945− for (int i = 0; i <= fgp->fg.nb_inputs; i++) {
946− InputFilterPriv *ifp = i < fgp->fg.nb_inputs ?
947− ifp_from_ifilter(fgp->fg.inputs[i]) : NULL;
948−
949− if (ifp)
950− ifp->eof = 1;
951−
952− tq_send_finish(fgp->queue_in, i);
953− }
954−
955− for (int i = 0; i <= fgp->fg.nb_outputs; i++)
956− tq_receive_finish(fgp->queue_out, i);
957−
958− pthread_join(fgp->thread, &ret);
959−
960− tq_free(&fgp->queue_in);
961− tq_free(&fgp->queue_out);
962−
963− return (int)(intptr_t)ret;
964−}
965−
966844 void fg_free(FilterGraph **pfg)
967845 {
968846 FilterGraph *fg = *pfg;
@@ -972,8 +850,6 @@ void fg_free(FilterGraph **pfg) · 3 unmodified lines
972850 return;
973851 fgp = fgp_from_fg(fg);
974852
975− fg_thread_stop(fgp);
976−
977853 avfilter_graph_free(&fg->graph);
978854 for (int j = 0; j < fg->nb_inputs; j++) {
979855 InputFilter *ifilter = fg->inputs[j];
@@ -1072,6 +948,15 @@ int fg_create(FilterGraph **pfg, char *graph_desc, Scheduler *sch) · 92 unmodified lines
1072948 if (ret < 0)
1073949 goto fail;
1074950
951+ for (unsigned i = 0; i < graph->nb_filters; i++) {
952+ const AVFilter *f = graph->filters[i]->filter;
953+ if (!avfilter_filter_pad_count(f, 0) &&
954+ !(f->flags & AVFILTER_FLAG_DYNAMIC_INPUTS)) {
955+ fgp->have_sources = 1;
956+ break;
957+ }
958+ }
959+
1075960 for (AVFilterInOut *cur = inputs; cur; cur = cur->next) {
1076961 InputFilter *const ifilter = ifilter_alloc(fg);
1077962 InputFilterPriv *ifp;
@@ -1800,6 +1685,7 @@ static int configure_filtergraph(FilterGraph *fg, const FilterGraphThread *fgt) · 722 unmodified lines
18001685 AVBufferRef *hw_device;
18011686 AVFilterInOut *inputs, *outputs, *cur;
18021687 int ret, i, simple = filtergraph_is_simple(fg);
1688+ int have_input_eof = 0;
18031689 const char *graph_desc = fgp->graph_desc;
18041690
18051691 cleanup_filtergraph(fg);
@@ -1922,11 +1808,18 @@ static int configure_filtergraph(FilterGraph *fg, const FilterGraphThread *fgt) · 116 unmodified lines
19221808 ret = av_buffersrc_add_frame(ifp->filter, NULL);
19231809 if (ret < 0)
19241810 goto fail;
1811+ have_input_eof = 1;
19251812 }
19261813 }
19271814
1928− return 0;
1815+ if (have_input_eof) {
1816+ // make sure the EOF propagates to the end of the graph
1817+ ret = avfilter_graph_request_oldest(fg->graph);
1818+ if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
1819+ goto fail;
1820+ }
19291821
1822+ return 0;
19301823 fail:
19311824 cleanup_filtergraph(fg);
19321825 return ret;
@@ -2182,7 +2075,7 @@ static void video_sync_process(OutputFilterPriv *ofp, AVFrame *frame, · 249 unmodified lines
21822075 fps->frames_prev_hist[2]);
21832076
21842077 if (!*nb_frames && fps->last_dropped) {
2185− ofilter->nb_frames_drop++;
2078+ atomic_fetch_add(&ofilter->nb_frames_drop, 1);
21862079 fps->last_dropped++;
21872080 }
21882081
@@ -2260,21 +2153,23 @@ finish: · 71 unmodified lines
22602153 fps->frames_prev_hist[0] = *nb_frames_prev;
22612154
22622155 if (*nb_frames_prev == 0 && fps->last_dropped) {
2263− ofilter->nb_frames_drop++;
2156+ atomic_fetch_add(&ofilter->nb_frames_drop, 1);
22642157 av_log(ost, AV_LOG_VERBOSE,
22652158 "*** dropping frame %"PRId64" at ts %"PRId64"\n",
22662159 fps->frame_number, fps->last_frame->pts);
22672160 }
22682161 if (*nb_frames > (*nb_frames_prev && fps->last_dropped) + (*nb_frames > *nb_frames_prev)) {
2162+ uint64_t nb_frames_dup;
22692163 if (*nb_frames > dts_error_threshold * 30) {
22702164 av_log(ost, AV_LOG_ERROR, "%"PRId64" frame duplication too large, skipping\n", *nb_frames - 1);
2271− ofilter->nb_frames_drop++;
2165+ atomic_fetch_add(&ofilter->nb_frames_drop, 1);
22722166 *nb_frames = 0;
22732167 return;
22742168 }
2275− ofilter->nb_frames_dup += *nb_frames - (*nb_frames_prev && fps->last_dropped) - (*nb_frames > *nb_frames_prev);
2169+ nb_frames_dup = atomic_fetch_add(&ofilter->nb_frames_dup,
2170+ *nb_frames - (*nb_frames_prev && fps->last_dropped) - (*nb_frames > *nb_frames_prev));
22762171 av_log(ost, AV_LOG_VERBOSE, "*** %"PRId64" dup!\n", *nb_frames - 1);
2277− if (ofilter->nb_frames_dup > fps->dup_warning) {
2172+ if (nb_frames_dup > fps->dup_warning) {
22782173 av_log(ost, AV_LOG_WARNING, "More than %"PRIu64" frames duplicated\n", fps->dup_warning);
22792174 fps->dup_warning *= 10;
22802175 }
@@ -2284,8 +2179,57 @@ finish: · 3 unmodified lines
22842179 fps->dropped_keyframe |= fps->last_dropped && (frame->flags & AV_FRAME_FLAG_KEY);
22852180 }
22862181
2182+static int close_output(OutputFilterPriv *ofp, FilterGraphThread *fgt)
2183+{
2184+ FilterGraphPriv *fgp = fgp_from_fg(ofp->ofilter.graph);
2185+ int ret;
2186+
2187+ // we are finished and no frames were ever seen at this output,
2188+ // at least initialize the encoder with a dummy frame
2189+ if (!fgt->got_frame) {
2190+ AVFrame *frame = fgt->frame;
2191+ FrameData *fd;
2192+
2193+ frame->time_base = ofp->tb_out;
2194+ frame->format = ofp->format;
2195+
2196+ frame->width = ofp->width;
2197+ frame->height = ofp->height;
2198+ frame->sample_aspect_ratio = ofp->sample_aspect_ratio;
2199+
2200+ frame->sample_rate = ofp->sample_rate;
2201+ if (ofp->ch_layout.nb_channels) {
2202+ ret = av_channel_layout_copy(&frame->ch_layout, &ofp->ch_layout);
2203+ if (ret < 0)
2204+ return ret;
2205+ }
2206+
2207+ fd = frame_data(frame);
2208+ if (!fd)
2209+ return AVERROR(ENOMEM);
2210+
2211+ fd->frame_rate_filter = ofp->fps.framerate;
2212+
2213+ av_assert0(!frame->buf[0]);
2214+
2215+ av_log(ofp->ofilter.ost, AV_LOG_WARNING,
2216+ "No filtered frames for output stream, trying to "
2217+ "initialize anyway.\n");
2218+
2219+ ret = sch_filter_send(fgp->sch, fgp->sch_idx, ofp->index, frame);
2220+ if (ret < 0) {
2221+ av_frame_unref(frame);
2222+ return ret;
2223+ }
2224+ }
2225+
2226+ fgt->eof_out[ofp->index] = 1;
2227+
2228+ return sch_filter_send(fgp->sch, fgp->sch_idx, ofp->index, NULL);
2229+}
2230+
22872231 static int fg_output_frame(OutputFilterPriv *ofp, FilterGraphThread *fgt,
2288− AVFrame *frame, int buffer)
2232+ AVFrame *frame)
22892233 {
22902234 FilterGraphPriv *fgp = fgp_from_fg(ofp->ofilter.graph);
22912235 AVFrame *frame_prev = ofp->fps.last_frame;
@@ -2332,28 +2276,17 @@ static int fg_output_frame(OutputFilterPriv *ofp, FilterGraphThread *fgt, · 40 unmodified lines
23322276 frame_out = frame;
23332277 }
23342278
2335− if (buffer) {
2336− AVFrame *f = av_frame_alloc();
2337−
2338− if (!f) {
2279+ {
2280+ // send the frame to consumers
2281+ ret = sch_filter_send(fgp->sch, fgp->sch_idx, ofp->index, frame_out);
2282+ if (ret < 0) {
23392283 av_frame_unref(frame_out);
2340− return AVERROR(ENOMEM);
2341− }
23422284
2343− av_frame_move_ref(f, frame_out);
2344− f->opaque = (void*)(intptr_t)ofp->index;
2285+ if (!fgt->eof_out[ofp->index]) {
2286+ fgt->eof_out[ofp->index] = 1;
2287+ fgp->nb_outputs_done++;
2288+ }
23452289
2346− ret = av_fifo_write(fgt->frame_queue_out, &f, 1);
2347− if (ret < 0) {
2348− av_frame_free(&f);
2349− return AVERROR(ENOMEM);
2350− }
2351− } else {
2352− // return the frame to the main thread
2353− ret = tq_send(fgp->queue_out, ofp->index, frame_out);
2354− if (ret < 0) {
2355− av_frame_unref(frame_out);
2356− fgt->eof_out[ofp->index] = 1;
23572290 return ret == AVERROR_EOF ? 0 : ret;
23582291 }
23592292 }
@@ -2374,16 +2307,14 @@ static int fg_output_frame(OutputFilterPriv *ofp, FilterGraphThread *fgt, · 14 unmodified lines
23742307 av_frame_move_ref(frame_prev, frame);
23752308 }
23762309
2377− if (!frame) {
2378− tq_send_finish(fgp->queue_out, ofp->index);
2379− fgt->eof_out[ofp->index] = 1;
2380− }
2310+ if (!frame)
2311+ return close_output(ofp, fgt);
23812312
23822313 return 0;
23832314 }
23842315
23852316 static int fg_output_step(OutputFilterPriv *ofp, FilterGraphThread *fgt,
2386− AVFrame *frame, int buffer)
2317+ AVFrame *frame)
23872318 {
23882319 FilterGraphPriv *fgp = fgp_from_fg(ofp->ofilter.graph);
23892320 OutputStream *ost = ofp->ofilter.ost;
@@ -2393,8 +2324,8 @@ static int fg_output_step(OutputFilterPriv *ofp, FilterGraphThread *fgt, · 3 unmodified lines
23932324
23942325 ret = av_buffersink_get_frame_flags(filter, frame,
23952326 AV_BUFFERSINK_FLAG_NO_REQUEST);
2396− if (ret == AVERROR_EOF && !buffer && !fgt->eof_out[ofp->index]) {
2397− ret = fg_output_frame(ofp, fgt, NULL, buffer);
2327+ if (ret == AVERROR_EOF && !fgt->eof_out[ofp->index]) {
2328+ ret = fg_output_frame(ofp, fgt, NULL);
23982329 return (ret < 0) ? ret : 1;
23992330 } else if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
24002331 return 1;
@@ -2448,7 +2379,7 @@ static int fg_output_step(OutputFilterPriv *ofp, FilterGraphThread *fgt, · 47 unmodified lines
24482379 fd->frame_rate_filter = ofp->fps.framerate;
24492380 }
24502381
2451− ret = fg_output_frame(ofp, fgt, frame, buffer);
2382+ ret = fg_output_frame(ofp, fgt, frame);
24522383 av_frame_unref(frame);
24532384 if (ret < 0)
24542385 return ret;
@@ -2456,44 +2387,68 @@ static int fg_output_step(OutputFilterPriv *ofp, FilterGraphThread *fgt, · 1 unmodified line
24562387 return 0;
24572388 }
24582389
2459−/* retrieve all frames available at filtergraph outputs and either send them to
2460− * the main thread (buffer=0) or buffer them for later (buffer=1) */
2390+/* retrieve all frames available at filtergraph outputs
2391+ * and send them to consumers */
24612392 static int read_frames(FilterGraph *fg, FilterGraphThread *fgt,
2462− AVFrame *frame, int buffer)
2393+ AVFrame *frame)
24632394 {
24642395 FilterGraphPriv *fgp = fgp_from_fg(fg);
2465− int ret = 0;
2396+ int did_step = 0;
24662397
2467− if (!fg->graph)
2468− return 0;
2398+ // graph not configured, just select the input to request
2399+ if (!fg->graph) {
2400+ for (int i = 0; i < fg->nb_inputs; i++) {
2401+ InputFilterPriv *ifp = ifp_from_ifilter(fg->inputs[i]);
2402+ if (ifp->format < 0 && !fgt->eof_in[i]) {
2403+ fgt->next_in = i;
2404+ return 0;
2405+ }
2406+ }
24692407
2470− // process buffered frames
2471− if (!buffer) {
2472− AVFrame *f;
2408+ // This state - graph is not configured, but all inputs are either
2409+ // initialized or EOF - should be unreachable because sending EOF to a
2410+ // filter without even a fallback format should fail
2411+ av_assert0(0);
2412+ return AVERROR_BUG;
2413+ }
24732414
2474− while (av_fifo_read(fgt->frame_queue_out, &f, 1) >= 0) {
2475− int out_idx = (intptr_t)f->opaque;
2476− f->opaque = NULL;
2477− ret = tq_send(fgp->queue_out, out_idx, f);
2478− av_frame_free(&f);
2479− if (ret < 0 && ret != AVERROR_EOF)
2480− return ret;
2415+ while (fgp->nb_outputs_done < fg->nb_outputs) {
2416+ int ret;
2417+
2418+ ret = avfilter_graph_request_oldest(fg->graph);
2419+ if (ret == AVERROR(EAGAIN)) {
2420+ fgt->next_in = choose_input(fg, fgt);
2421+ break;
2422+ } else if (ret < 0) {
2423+ if (ret == AVERROR_EOF)
2424+ av_log(fg, AV_LOG_VERBOSE, "Filtergraph returned EOF, finishing\n");
2425+ else
2426+ av_log(fg, AV_LOG_ERROR,
2427+ "Error requesting a frame from the filtergraph: %s\n",
2428+ av_err2str(ret));
2429+ return ret;
24812430 }
2482− }
2431+ fgt->next_in = fg->nb_inputs;
24832432
2484− /* Reap all buffers present in the buffer sinks */
2485− for (int i = 0; i < fg->nb_outputs; i++) {
2486− OutputFilterPriv *ofp = ofp_from_ofilter(fg->outputs[i]);
2487− int ret = 0;
2433+ // return after one iteration, so that scheduler can rate-control us
2434+ if (did_step && fgp->have_sources)
2435+ return 0;
24882436
2489− while (!ret) {
2490− ret = fg_output_step(ofp, fgt, frame, buffer);
2491− if (ret < 0)
2492− return ret;
2437+ /* Reap all buffers present in the buffer sinks */
2438+ for (int i = 0; i < fg->nb_outputs; i++) {
2439+ OutputFilterPriv *ofp = ofp_from_ofilter(fg->outputs[i]);
2440+
2441+ ret = 0;
2442+ while (!ret) {
2443+ ret = fg_output_step(ofp, fgt, frame);
2444+ if (ret < 0)
2445+ return ret;
2446+ }
24932447 }
2494− }
2448+ did_step = 1;
2449+ };
24952450
2496− return 0;
2451+ return (fgp->nb_outputs_done == fg->nb_outputs) ? AVERROR_EOF : 0;
24972452 }
24982453
24992454 static void sub2video_heartbeat(InputFilter *ifilter, int64_t pts, AVRational tb)
@@ -2571,6 +2526,9 @@ static int send_eof(FilterGraphThread *fgt, InputFilter *ifilter, · 71 unmodified lines
25712526 InputFilterPriv *ifp = ifp_from_ifilter(ifilter);
25722527 int ret;
25732528
2529+ if (fgt->eof_in[ifp->index])
2530+ return 0;
2531+
25742532 fgt->eof_in[ifp->index] = 1;
25752533
25762534 if (ifp->filter) {
@@ -2672,7 +2630,7 @@ static int send_frame(FilterGraph *fg, FilterGraphThread *fgt, · 95 unmodified lines
26722630 return ret;
26732631 }
26742632
2675− ret = fg->graph ? read_frames(fg, fgt, tmp, 1) : 0;
2633+ ret = fg->graph ? read_frames(fg, fgt, tmp) : 0;
26762634 av_frame_free(&tmp);
26772635 if (ret < 0)
26782636 return ret;
@@ -2705,82 +2663,6 @@ static int send_frame(FilterGraph *fg, FilterGraphThread *fgt, · 26 unmodified lines
27052663 return 0;
27062664 }
27072665
2708−static int msg_process(FilterGraphPriv *fgp, FilterGraphThread *fgt,
2709− AVFrame *frame)
2710−{
2711− const enum FrameOpaque msg = (intptr_t)frame->opaque;
2712− FilterGraph *fg = &fgp->fg;
2713− int graph_eof = 0;
2714− int ret;
2715−
2716− frame->opaque = NULL;
2717− av_assert0(msg > 0);
2718− av_assert0(msg == FRAME_OPAQUE_SEND_COMMAND || !frame->buf[0]);
2719−
2720− if (!fg->graph) {
2721− // graph not configured yet, ignore all messages other than choosing
2722− // the input to read from
2723− if (msg != FRAME_OPAQUE_CHOOSE_INPUT) {
2724− av_frame_unref(frame);
2725− goto done;
2726− }
2727−
2728− for (int i = 0; i < fg->nb_inputs; i++) {
2729− InputFilter *ifilter = fg->inputs[i];
2730− InputFilterPriv *ifp = ifp_from_ifilter(ifilter);
2731− if (ifp->format < 0 && !fgt->eof_in[i]) {
2732− frame->opaque = (void*)(intptr_t)(i + 1);
2733− goto done;
2734− }
2735− }
2736−
2737− // This state - graph is not configured, but all inputs are either
2738− // initialized or EOF - should be unreachable because sending EOF to a
2739− // filter without even a fallback format should fail
2740− av_assert0(0);
2741− return AVERROR_BUG;
2742− }
2743−
2744− if (msg == FRAME_OPAQUE_SEND_COMMAND) {
2745− FilterCommand *fc = (FilterCommand*)frame->buf[0]->data;
2746− send_command(fg, fc->time, fc->target, fc->command, fc->arg, fc->all_filters);
2747− av_frame_unref(frame);
2748− goto done;
2749− }
2750−
2751− if (msg == FRAME_OPAQUE_CHOOSE_INPUT) {
2752− ret = avfilter_graph_request_oldest(fg->graph);
2753−
2754− graph_eof = ret == AVERROR_EOF;
2755−
2756− if (ret == AVERROR(EAGAIN)) {
2757− frame->opaque = (void*)(intptr_t)(choose_input(fg, fgt) + 1);
2758− goto done;
2759− } else if (ret < 0 && !graph_eof)
2760− return ret;
2761− }
2762−
2763− ret = read_frames(fg, fgt, frame, 0);
2764− if (ret < 0) {
2765− av_log(fg, AV_LOG_ERROR, "Error sending filtered frames for encoding\n");
2766− return ret;
2767− }
2768−
2769− if (graph_eof)
2770− return AVERROR_EOF;
2771−
2772− // signal to the main thread that we are done processing the message
2773−done:
2774− ret = tq_send(fgp->queue_out, fg->nb_outputs, frame);
2775− if (ret < 0) {
2776− if (ret != AVERROR_EOF)
2777− av_log(fg, AV_LOG_ERROR, "Error communicating with the main thread\n");
2778− return ret;
2779− }
2780−
2781− return 0;
2782−}
2783−
27842666 static void fg_thread_set_name(const FilterGraph *fg)
27852667 {
27862668 char name[16];
@@ -2867,282 +2749,86 @@ static void *filter_thread(void *arg) · 80 unmodified lines
28672749 InputFilter *ifilter;
28682750 InputFilterPriv *ifp;
28692751 enum FrameOpaque o;
2870− int input_idx, eof_frame;
2752+ unsigned input_idx = fgt.next_in;
28712753
2872− input_status = tq_receive(fgp->queue_in, &input_idx, fgt.frame);
2873− if (input_idx < 0 ||
2874− (input_idx == fg->nb_inputs && input_status < 0)) {
2754+ input_status = sch_filter_receive(fgp->sch, fgp->sch_idx,
2755+ &input_idx, fgt.frame);
2756+ if (input_status == AVERROR_EOF) {
28752757 av_log(fg, AV_LOG_VERBOSE, "Filtering thread received EOF\n");
28762758 break;
2759+ } else if (input_status == AVERROR(EAGAIN)) {
2760+ // should only happen when we didn't request any input
2761+ av_assert0(input_idx == fg->nb_inputs);
2762+ goto read_frames;
28772763 }
2764+ av_assert0(input_status >= 0);
2765+
2766+ o = (intptr_t)fgt.frame->opaque;
28782767
28792768 o = (intptr_t)fgt.frame->opaque;
28802769
28812770 // message on the control stream
28822771 if (input_idx == fg->nb_inputs) {
2883− ret = msg_process(fgp, &fgt, fgt.frame);
2884− if (ret < 0)
2885− goto finish;
2772+ FilterCommand *fc;
2773+
2774+ av_assert0(o == FRAME_OPAQUE_SEND_COMMAND && fgt.frame->buf[0]);
28862775
2776+ fc = (FilterCommand*)fgt.frame->buf[0]->data;
2777+ send_command(fg, fc->time, fc->target, fc->command, fc->arg,
2778+ fc->all_filters);
2779+ av_frame_unref(fgt.frame);
28872780 continue;
28882781 }
28892782
28902783 // we received an input frame or EOF
28912784 ifilter = fg->inputs[input_idx];
28922785 ifp = ifp_from_ifilter(ifilter);
2893− eof_frame = input_status >= 0 && o == FRAME_OPAQUE_EOF;
2786+
28942787 if (ifp->type_src == AVMEDIA_TYPE_SUBTITLE) {
28952788 int hb_frame = input_status >= 0 && o == FRAME_OPAQUE_SUB_HEARTBEAT;
28962789 ret = sub2video_frame(ifilter, (fgt.frame->buf[0] || hb_frame) ? fgt.frame : NULL);
2897− } else if (input_status >= 0 && fgt.frame->buf[0]) {
2790+ } else if (fgt.frame->buf[0]) {
28982791 ret = send_frame(fg, &fgt, ifilter, fgt.frame);
28992792 } else {
2900− int64_t pts = input_status >= 0 ? fgt.frame->pts : AV_NOPTS_VALUE;
2901− AVRational tb = input_status >= 0 ? fgt.frame->time_base : (AVRational){ 1, 1 };
2902− ret = send_eof(&fgt, ifilter, pts, tb);
2793+ av_assert1(o == FRAME_OPAQUE_EOF);
2794+ ret = send_eof(&fgt, ifilter, fgt.frame->pts, fgt.frame->time_base);
29032795 }
29042796 av_frame_unref(fgt.frame);
29052797 if (ret < 0)
2906− break;
2907−
2908− if (eof_frame) {
2909− // an EOF frame is immediately followed by sender closing
2910− // the corresponding stream, so retrieve that event
2911− input_status = tq_receive(fgp->queue_in, &input_idx, fgt.frame);
2912− av_assert0(input_status == AVERROR_EOF && input_idx == ifp->index);
2913− }
2914−
2915− // signal to the main thread that we are done
2916− ret = tq_send(fgp->queue_out, fg->nb_outputs, fgt.frame);
2917− if (ret < 0) {
2918− if (ret == AVERROR_EOF)
2919− break;
2920−
2921− av_log(fg, AV_LOG_ERROR, "Error communicating with the main thread\n");
29222798 goto finish;
2923− }
2924− }
2925−
2926−finish:
2927− // EOF is normal termination
2928− if (ret == AVERROR_EOF)
2929− ret = 0;
2930−
2931− for (int i = 0; i <= fg->nb_inputs; i++)
2932− tq_receive_finish(fgp->queue_in, i);
2933− for (int i = 0; i <= fg->nb_outputs; i++)
2934− tq_send_finish(fgp->queue_out, i);
2935−
2936− fg_thread_uninit(&fgt);
2937−
2938− av_log(fg, AV_LOG_VERBOSE, "Terminating filtering thread\n");
2939−
2940− return (void*)(intptr_t)ret;
2941−}
2942−
2943−static int thread_send_frame(FilterGraphPriv *fgp, InputFilter *ifilter,
2944− AVFrame *frame, enum FrameOpaque type)
2945−{
2946− InputFilterPriv *ifp = ifp_from_ifilter(ifilter);
2947− int output_idx, ret;
2948−
2949− if (ifp->eof) {
2950− av_frame_unref(frame);
2951− return AVERROR_EOF;
2952− }
2953−
2954− frame->opaque = (void*)(intptr_t)type;
2955−
2956− ret = tq_send(fgp->queue_in, ifp->index, frame);
2957− if (ret < 0) {
2958− ifp->eof = 1;
2959− av_frame_unref(frame);
2960− return ret;
2961− }
2962−
2963− if (type == FRAME_OPAQUE_EOF)
2964− tq_send_finish(fgp->queue_in, ifp->index);
2965−
2966− // wait for the frame to be processed
2967− ret = tq_receive(fgp->queue_out, &output_idx, frame);
2968− av_assert0(output_idx == fgp->fg.nb_outputs || ret == AVERROR_EOF);
2969−
2970− return ret;
2971−}
2972−
2973−int ifilter_send_frame(InputFilter *ifilter, AVFrame *frame, int keep_reference)
2974−{
2975− FilterGraphPriv *fgp = fgp_from_fg(ifilter->graph);
2976− int ret;
29772799
2978− if (keep_reference) {
2979− ret = av_frame_ref(fgp->frame, frame);
2980− if (ret < 0)
2981− return ret;
2982− } else
2983− av_frame_move_ref(fgp->frame, frame);
2984−
2985− return thread_send_frame(fgp, ifilter, fgp->frame, 0);
2986−}
2987−
2988−int ifilter_send_eof(InputFilter *ifilter, int64_t pts, AVRational tb)
2989−{
2990− FilterGraphPriv *fgp = fgp_from_fg(ifilter->graph);
2991− int ret;
2992−
2993− fgp->frame->pts = pts;
2994− fgp->frame->time_base = tb;
2995−
2996− ret = thread_send_frame(fgp, ifilter, fgp->frame, FRAME_OPAQUE_EOF);
2997−
2998− return ret == AVERROR_EOF ? 0 : ret;
2999−}
3000−
3001−void ifilter_sub2video_heartbeat(InputFilter *ifilter, int64_t pts, AVRational tb)
3002−{
3003− FilterGraphPriv *fgp = fgp_from_fg(ifilter->graph);
3004−
3005− fgp->frame->pts = pts;
3006− fgp->frame->time_base = tb;
3007−
3008− thread_send_frame(fgp, ifilter, fgp->frame, FRAME_OPAQUE_SUB_HEARTBEAT);
3009−}
3010−
3011−int fg_transcode_step(FilterGraph *graph, InputStream **best_ist)
3012−{
3013− FilterGraphPriv *fgp = fgp_from_fg(graph);
3014− int ret, got_frames = 0;
3015−
3016− if (fgp->eof_in)
3017− return AVERROR_EOF;
3018−
3019− // signal to the filtering thread to return all frames it can
3020− av_assert0(!fgp->frame->buf[0]);
3021− fgp->frame->opaque = (void*)(intptr_t)(best_ist ?
3022− FRAME_OPAQUE_CHOOSE_INPUT :
3023− FRAME_OPAQUE_REAP_FILTERS);
3024−
3025− ret = tq_send(fgp->queue_in, graph->nb_inputs, fgp->frame);
3026− if (ret < 0) {
3027− fgp->eof_in = 1;
3028− goto finish;
3029− }
3030−
3031− while (1) {
3032− OutputFilter *ofilter;
3033− OutputFilterPriv *ofp;
3034− OutputStream *ost;
3035− int output_idx;
3036−
3037− ret = tq_receive(fgp->queue_out, &output_idx, fgp->frame);
3038−
3039− // EOF on the whole queue or the control stream
3040− if (output_idx < 0 ||
3041− (ret < 0 && output_idx == graph->nb_outputs))
3042− goto finish;
3043−
3044− // EOF for a specific stream
3045− if (ret < 0) {
3046− ofilter = graph->outputs[output_idx];
3047− ofp = ofp_from_ofilter(ofilter);
3048−
3049− // we are finished and no frames were ever seen at this output,
3050− // at least initialize the encoder with a dummy frame
3051− if (!ofp->got_frame) {
3052− AVFrame *frame = fgp->frame;
3053− FrameData *fd;
3054−
3055− frame->time_base = ofp->tb_out;
3056− frame->format = ofp->format;
3057−
3058− frame->width = ofp->width;
3059− frame->height = ofp->height;
3060− frame->sample_aspect_ratio = ofp->sample_aspect_ratio;
3061−
3062− frame->sample_rate = ofp->sample_rate;
3063− if (ofp->ch_layout.nb_channels) {
3064− ret = av_channel_layout_copy(&frame->ch_layout, &ofp->ch_layout);
3065− if (ret < 0)
3066− return ret;
3067− }
3068−
3069− fd = frame_data(frame);
3070− if (!fd)
3071− return AVERROR(ENOMEM);
3072−
3073− fd->frame_rate_filter = ofp->fps.framerate;
3074−
3075− av_assert0(!frame->buf[0]);
3076−
3077− av_log(ofilter->ost, AV_LOG_WARNING,
3078− "No filtered frames for output stream, trying to "
3079− "initialize anyway.\n");
3080−
3081− enc_open(ofilter->ost, frame);
3082− av_frame_unref(frame);
3083− }
3084−
3085− close_output_stream(graph->outputs[output_idx]->ost);
3086− continue;
3087− }
3088−
3089− // request was fully processed by the filtering thread,
3090− // return the input stream to read from, if needed
3091− if (output_idx == graph->nb_outputs) {
3092− int input_idx = (intptr_t)fgp->frame->opaque - 1;
3093− av_assert0(input_idx <= graph->nb_inputs);
3094−
3095− if (best_ist) {
3096− *best_ist = (input_idx >= 0 && input_idx < graph->nb_inputs) ?
3097− ifp_from_ifilter(graph->inputs[input_idx])->ist : NULL;
3098−
3099− if (input_idx < 0 && !got_frames) {
3100− for (int i = 0; i < graph->nb_outputs; i++)
3101− graph->outputs[i]->ost->unavailable = 1;
3102− }
3103− }
2800+read_frames:
2801+ // retrieve all newly avalable frames
2802+ ret = read_frames(fg, &fgt, fgt.frame);
2803+ if (ret == AVERROR_EOF) {
2804+ av_log(fg, AV_LOG_VERBOSE, "All consumers returned EOF\n");
31042805 break;
2806+ } else if (ret < 0) {
2807+ av_log(fg, AV_LOG_ERROR, "Error sending frames to consumers: %s\n",
2808+ av_err2str(ret));
2809+ goto finish;
31052810 }
2811+ }
31062812
3107− // got a frame from the filtering thread, send it for encoding
3108− ofilter = graph->outputs[output_idx];
3109− ost = ofilter->ost;
3110− ofp = ofp_from_ofilter(ofilter);
2813+ for (unsigned i = 0; i < fg->nb_outputs; i++) {
2814+ OutputFilterPriv *ofp = ofp_from_ofilter(fg->outputs[i]);
31112815
3112− if (ost->finished) {
3113− av_frame_unref(fgp->frame);
3114− tq_receive_finish(fgp->queue_out, output_idx);
2816+ if (fgt.eof_out[i])
31152817 continue;
3116− }
3117−
3118− if (fgp->frame->pts != AV_NOPTS_VALUE) {
3119− ofilter->last_pts = av_rescale_q(fgp->frame->pts,
3120− fgp->frame->time_base,
3121− AV_TIME_BASE_Q);
3122− }
31232818
3124− ret = enc_frame(ost, fgp->frame);
3125− av_frame_unref(fgp->frame);
2819+ ret = fg_output_frame(ofp, &fgt, NULL);
31262820 if (ret < 0)
31272821 goto finish;
3128−
3129− ofp->got_frame = 1;
3130− got_frames = 1;
31312822 }
31322823
31332824 finish:
3134− if (ret < 0) {
3135− fgp->eof_in = 1;
3136− for (int i = 0; i < graph->nb_outputs; i++)
3137− close_output_stream(graph->outputs[i]->ost);
3138− }
2825+ // EOF is normal termination
2826+ if (ret == AVERROR_EOF)
2827+ ret = 0;
31392828
3140− return ret;
3141−}
2829+ fg_thread_uninit(&fgt);
31422830
3143−int reap_filters(FilterGraph *fg, int flush)
3144−{
3145− return fg_transcode_step(fg, NULL);
2831+ return (void*)(intptr_t)ret;
31462832 }
31472833
31482834 void fg_send_command(FilterGraph *fg, double time, const char *target,
@@ -3151,10 +2837,6 @@ void fg_send_command(FilterGraph *fg, double time, const char *target, · 2 unmodified lines
31512837 FilterGraphPriv *fgp = fgp_from_fg(fg);
31522838 AVBufferRef *buf;
31532839 FilterCommand *fc;
3154− int output_idx, ret;
3155−
3156− if (!fgp->queue_in)
3157− return;
31582840
31592841 fc = av_mallocz(sizeof(*fc));
31602842 if (!fc)
@@ -3180,13 +2862,5 @@ void fg_send_command(FilterGraph *fg, double time, const char *target, · 19 unmodified lines
31802862 fgp->frame->buf[0] = buf;
31812863 fgp->frame->opaque = (void*)(intptr_t)FRAME_OPAQUE_SEND_COMMAND;
31822864
3183− ret = tq_send(fgp->queue_in, fg->nb_inputs, fgp->frame);
3184− if (ret < 0) {
3185− av_frame_unref(fgp->frame);
3186− return;
3187− }
3188−
3189− // wait for the frame to be processed
3190− ret = tq_receive(fgp->queue_out, &output_idx, fgp->frame);
3191− av_assert0(output_idx == fgp->fg.nb_outputs || ret == AVERROR_EOF);
2865+ sch_filter_command(fgp->sch, fgp->sch_idx, fgp->frame);
31922866 }
fftools/ffmpeg_mux.c+62−262
@@ -23,16 +23,13 @@ · 22 unmodified lines
2323 #include "ffmpeg.h"
2424 #include "ffmpeg_mux.h"
2525 #include "ffmpeg_utils.h"
26−#include "objpool.h"
2726 #include "sync_queue.h"
28−#include "thread_queue.h"
2927
3028 #include "libavutil/fifo.h"
3129 #include "libavutil/intreadwrite.h"
3230 #include "libavutil/log.h"
3331 #include "libavutil/mem.h"
3432 #include "libavutil/timestamp.h"
35−#include "libavutil/thread.h"
3633
3734 #include "libavcodec/packet.h"
3835
@@ -41,10 +38,9 @@ · 2 unmodified lines
4138
4239 typedef struct MuxThreadContext {
4340 AVPacket *pkt;
41+ AVPacket *fix_sub_duration_pkt;
4442 } MuxThreadContext;
4543
46−int want_sdp = 1;
47−
4844 static Muxer *mux_from_of(OutputFile *of)
4945 {
5046 return (Muxer*)of;
@@ -207,14 +203,41 @@ static int sync_queue_process(Muxer *mux, OutputStream *ost, AVPacket *pkt, int · 156 unmodified lines
207203 return 0;
208204 }
209205
206+static int of_streamcopy(OutputStream *ost, AVPacket *pkt);
207+
210208 /* apply the output bitstream filters */
211−static int mux_packet_filter(Muxer *mux, OutputStream *ost,
212− AVPacket *pkt, int *stream_eof)
209+static int mux_packet_filter(Muxer *mux, MuxThreadContext *mt,
210+ OutputStream *ost, AVPacket *pkt, int *stream_eof)
213211 {
214212 MuxStream *ms = ms_from_ost(ost);
215213 const char *err_msg;
216214 int ret = 0;
217215
216+ if (pkt && !ost->enc) {
217+ ret = of_streamcopy(ost, pkt);
218+ if (ret == AVERROR(EAGAIN))
219+ return 0;
220+ else if (ret == AVERROR_EOF) {
221+ av_packet_unref(pkt);
222+ pkt = NULL;
223+ ret = 0;
224+ } else if (ret < 0)
225+ goto fail;
226+ }
227+
228+ // emit heartbeat for -fix_sub_duration;
229+ // we are only interested in heartbeats on on random access points.
230+ if (pkt && (pkt->flags & AV_PKT_FLAG_KEY)) {
231+ mt->fix_sub_duration_pkt->opaque = (void*)(intptr_t)PKT_OPAQUE_FIX_SUB_DURATION;
232+ mt->fix_sub_duration_pkt->pts = pkt->pts;
233+ mt->fix_sub_duration_pkt->time_base = pkt->time_base;
234+
235+ ret = sch_mux_sub_heartbeat(mux->sch, mux->sch_idx, ms->sch_idx,
236+ mt->fix_sub_duration_pkt);
237+ if (ret < 0)
238+ goto fail;
239+ }
240+
218241 if (ms->bsf_ctx) {
219242 int bsf_eof = 0;
220243
@@ -278,6 +301,7 @@ static void thread_set_name(OutputFile *of) · 57 unmodified lines
278301 static void mux_thread_uninit(MuxThreadContext *mt)
279302 {
280303 av_packet_free(&mt->pkt);
304+ av_packet_free(&mt->fix_sub_duration_pkt);
281305
282306 memset(mt, 0, sizeof(*mt));
283307 }
@@ -290,6 +314,10 @@ static int mux_thread_init(MuxThreadContext *mt) · 6 unmodified lines
290314 if (!mt->pkt)
291315 goto fail;
292316
317+ mt->fix_sub_duration_pkt = av_packet_alloc();
318+ if (!mt->fix_sub_duration_pkt)
319+ goto fail;
320+
293321 return 0;
294322
295323 fail:
@@ -316,19 +344,22 @@ void *muxer_thread(void *arg) · 20 unmodified lines
316344 OutputStream *ost;
317345 int stream_idx, stream_eof = 0;
318346
319− ret = tq_receive(mux->tq, &stream_idx, mt.pkt);
347+ ret = sch_mux_receive(mux->sch, of->index, mt.pkt);
348+ stream_idx = mt.pkt->stream_index;
320349 if (stream_idx < 0) {
321350 av_log(mux, AV_LOG_VERBOSE, "All streams finished\n");
322351 ret = 0;
323352 break;
324353 }
325354
326− ost = of->streams[stream_idx];
327− ret = mux_packet_filter(mux, ost, ret < 0 ? NULL : mt.pkt, &stream_eof);
355+ ost = of->streams[mux->sch_stream_idx[stream_idx]];
356+ mt.pkt->stream_index = ost->index;
357+
358+ ret = mux_packet_filter(mux, &mt, ost, ret < 0 ? NULL : mt.pkt, &stream_eof);
328359 av_packet_unref(mt.pkt);
329360 if (ret == AVERROR_EOF) {
330361 if (stream_eof) {
331− tq_receive_finish(mux->tq, stream_idx);
362+ sch_mux_receive_finish(mux->sch, of->index, stream_idx);
332363 } else {
333364 av_log(mux, AV_LOG_VERBOSE, "Muxer returned EOF\n");
334365 ret = 0;
@@ -343,243 +374,55 @@ void *muxer_thread(void *arg) · 8 unmodified lines
343374 finish:
344375 mux_thread_uninit(&mt);
345376
346− for (unsigned int i = 0; i < mux->fc->nb_streams; i++)
347− tq_receive_finish(mux->tq, i);
348−
349− av_log(mux, AV_LOG_VERBOSE, "Terminating muxer thread\n");
350−
351377 return (void*)(intptr_t)ret;
352378 }
353379
354−static int thread_submit_packet(Muxer *mux, OutputStream *ost, AVPacket *pkt)
355−{
356− int ret = 0;
357−
358− if (!pkt || ost->finished & MUXER_FINISHED)
359− goto finish;
360−
361− ret = tq_send(mux->tq, ost->index, pkt);
362− if (ret < 0)
363− goto finish;
364−
365− return 0;
366−
367−finish:
368− if (pkt)
369− av_packet_unref(pkt);
370−
371− ost->finished |= MUXER_FINISHED;
372− tq_send_finish(mux->tq, ost->index);
373− return ret == AVERROR_EOF ? 0 : ret;
374−}
375−
376−static int queue_packet(OutputStream *ost, AVPacket *pkt)
377−{
378− MuxStream *ms = ms_from_ost(ost);
379− AVPacket *tmp_pkt = NULL;
380− int ret;
381−
382− if (!av_fifo_can_write(ms->muxing_queue)) {
383− size_t cur_size = av_fifo_can_read(ms->muxing_queue);
384− size_t pkt_size = pkt ? pkt->size : 0;
385− unsigned int are_we_over_size =
386− (ms->muxing_queue_data_size + pkt_size) > ms->muxing_queue_data_threshold;
387− size_t limit = are_we_over_size ? ms->max_muxing_queue_size : SIZE_MAX;
388− size_t new_size = FFMIN(2 * cur_size, limit);
389−
390− if (new_size <= cur_size) {
391− av_log(ost, AV_LOG_ERROR,
392− "Too many packets buffered for output stream %d:%d.\n",
393− ost->file_index, ost->st->index);
394− return AVERROR(ENOSPC);
395− }
396− ret = av_fifo_grow2(ms->muxing_queue, new_size - cur_size);
397− if (ret < 0)
398− return ret;
399− }
400−
401− if (pkt) {
402− ret = av_packet_make_refcounted(pkt);
403− if (ret < 0)
404− return ret;
405−
406− tmp_pkt = av_packet_alloc();
407− if (!tmp_pkt)
408− return AVERROR(ENOMEM);
409−
410− av_packet_move_ref(tmp_pkt, pkt);
411− ms->muxing_queue_data_size += tmp_pkt->size;
412− }
413− av_fifo_write(ms->muxing_queue, &tmp_pkt, 1);
414−
415− return 0;
416−}
417−
418−static int submit_packet(Muxer *mux, AVPacket *pkt, OutputStream *ost)
419−{
420− int ret;
421−
422− if (mux->tq) {
423− return thread_submit_packet(mux, ost, pkt);
424− } else {
425− /* the muxer is not initialized yet, buffer the packet */
426− ret = queue_packet(ost, pkt);
427− if (ret < 0) {
428− if (pkt)
429− av_packet_unref(pkt);
430− return ret;
431− }
432− }
433−
434− return 0;
435−}
436−
437−int of_output_packet(OutputFile *of, OutputStream *ost, AVPacket *pkt)
438−{
439− Muxer *mux = mux_from_of(of);
440− int ret = 0;
441−
442− if (pkt && pkt->dts != AV_NOPTS_VALUE)
443− ost->last_mux_dts = av_rescale_q(pkt->dts, pkt->time_base, AV_TIME_BASE_Q);
444−
445− ret = submit_packet(mux, pkt, ost);
446− if (ret < 0) {
447− av_log(ost, AV_LOG_ERROR, "Error submitting a packet to the muxer: %s",
448− av_err2str(ret));
449− return ret;
450− }
451−
452− return 0;
453−}
454−
455−int of_streamcopy(OutputStream *ost, const AVPacket *pkt, int64_t dts)
380+static int of_streamcopy(OutputStream *ost, AVPacket *pkt)
456381 {
457382 OutputFile *of = output_files[ost->file_index];
458383 MuxStream *ms = ms_from_ost(ost);
384+ DemuxPktData *pd = pkt->opaque_ref ? (DemuxPktData*)pkt->opaque_ref->data : NULL;
385+ int64_t dts = pd ? pd->dts_est : AV_NOPTS_VALUE;
459386 int64_t start_time = (of->start_time == AV_NOPTS_VALUE) ? 0 : of->start_time;
460387 int64_t ts_offset;
461− AVPacket *opkt = ms->pkt;
462− int ret;
463−
464− av_packet_unref(opkt);
465388
466389 if (of->recording_time != INT64_MAX &&
467390 dts >= of->recording_time + start_time)
468− pkt = NULL;
469−
470− // EOF: flush output bitstream filters.
471− if (!pkt)
472− return of_output_packet(of, ost, NULL);
391+ return AVERROR_EOF;
473392
474393 if (!ms->streamcopy_started && !(pkt->flags & AV_PKT_FLAG_KEY) &&
475394 !ms->copy_initial_nonkeyframes)
476− return 0;
395+ return AVERROR(EAGAIN);
477396
478397 if (!ms->streamcopy_started) {
479398 if (!ms->copy_prior_start &&
480399 (pkt->pts == AV_NOPTS_VALUE ?
481400 dts < ms->ts_copy_start :
482401 pkt->pts < av_rescale_q(ms->ts_copy_start, AV_TIME_BASE_Q, pkt->time_base)))
483− return 0;
402+ return AVERROR(EAGAIN);
484403
485404 if (of->start_time != AV_NOPTS_VALUE && dts < of->start_time)
486− return 0;
405+ return AVERROR(EAGAIN);
487406 }
488407
489− ret = av_packet_ref(opkt, pkt);
490− if (ret < 0)
491− return ret;
492−
493− ts_offset = av_rescale_q(start_time, AV_TIME_BASE_Q, opkt->time_base);
408+ ts_offset = av_rescale_q(start_time, AV_TIME_BASE_Q, pkt->time_base);
494409
495410 if (pkt->pts != AV_NOPTS_VALUE)
496− opkt->pts -= ts_offset;
411+ pkt->pts -= ts_offset;
497412
498413 if (pkt->dts == AV_NOPTS_VALUE) {
499− opkt->dts = av_rescale_q(dts, AV_TIME_BASE_Q, opkt->time_base);
414+ pkt->dts = av_rescale_q(dts, AV_TIME_BASE_Q, pkt->time_base);
500415 } else if (ost->st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
501− opkt->pts = opkt->dts - ts_offset;
416+ pkt->pts = pkt->dts - ts_offset;
502417 }
503− opkt->dts -= ts_offset;
504418
505− {
506− int ret = trigger_fix_sub_duration_heartbeat(ost, pkt);
507− if (ret < 0) {
508− av_log(NULL, AV_LOG_ERROR,
509− "Subtitle heartbeat logic failed in %s! (%s)\n",
510− __func__, av_err2str(ret));
511− return ret;
512− }
513− }
514−
515− ret = of_output_packet(of, ost, opkt);
516− if (ret < 0)
517− return ret;
419+ pkt->dts -= ts_offset;
518420
519421 ms->streamcopy_started = 1;
520422
521423 return 0;
522424 }
523425
524−static int thread_stop(Muxer *mux)
525−{
526− void *ret;
527−
528− if (!mux || !mux->tq)
529− return 0;
530−
531− for (unsigned int i = 0; i < mux->fc->nb_streams; i++)
532− tq_send_finish(mux->tq, i);
533−
534− pthread_join(mux->thread, &ret);
535−
536− tq_free(&mux->tq);
537−
538− return (int)(intptr_t)ret;
539−}
540−
541−static int thread_start(Muxer *mux)
542−{
543− AVFormatContext *fc = mux->fc;
544− ObjPool *op;
545− int ret;
546−
547− op = objpool_alloc_packets();
548− if (!op)
549− return AVERROR(ENOMEM);
550−
551− mux->tq = tq_alloc(fc->nb_streams, mux->thread_queue_size, op, pkt_move);
552− if (!mux->tq) {
553− objpool_free(&op);
554− return AVERROR(ENOMEM);
555− }
556−
557− ret = pthread_create(&mux->thread, NULL, muxer_thread, (void*)mux);
558− if (ret) {
559− tq_free(&mux->tq);
560− return AVERROR(ret);
561− }
562−
563− /* flush the muxing queues */
564− for (int i = 0; i < fc->nb_streams; i++) {
565− OutputStream *ost = mux->of.streams[i];
566− MuxStream *ms = ms_from_ost(ost);
567− AVPacket *pkt;
568−
569− while (av_fifo_read(ms->muxing_queue, &pkt, 1) >= 0) {
570− ret = thread_submit_packet(mux, ost, pkt);
571− if (pkt) {
572− ms->muxing_queue_data_size -= pkt->size;
573− av_packet_free(&pkt);
574− }
575− if (ret < 0)
576− return ret;
577− }
578− }
579−
580− return 0;
581−}
582−
583426 int print_sdp(const char *filename);
584427
585428 int print_sdp(const char *filename)
@@ -590,11 +433,6 @@ int print_sdp(const char *filename) · 4 unmodified lines
590433 AVIOContext *sdp_pb;
591434 AVFormatContext **avc;
592435
593− for (i = 0; i < nb_output_files; i++) {
594− if (!mux_from_of(output_files[i])->header_written)
595− return 0;
596− }
597−
598436 avc = av_malloc_array(nb_output_files, sizeof(*avc));
599437 if (!avc)
600438 return AVERROR(ENOMEM);
@@ -629,25 +467,17 @@ int print_sdp(const char *filename) · 28 unmodified lines
629467 avio_closep(&sdp_pb);
630468 }
631469
632− // SDP successfully written, allow muxer threads to start
633− ret = 1;
634−
635470 fail:
636471 av_freep(&avc);
637472 return ret;
638473 }
639474
640−int mux_check_init(Muxer *mux)
475+int mux_check_init(void *arg)
641476 {
477+ Muxer *mux = arg;
642478 OutputFile *of = &mux->of;
643479 AVFormatContext *fc = mux->fc;
644− int ret, i;
645−
646− for (i = 0; i < fc->nb_streams; i++) {
647− OutputStream *ost = of->streams[i];
648− if (!ost->initialized)
649− return 0;
650− }
480+ int ret;
651481
652482 ret = avformat_write_header(fc, &mux->opts);
653483 if (ret < 0) {
@@ -659,27 +489,7 @@ int mux_check_init(Muxer *mux) · 5 unmodified lines
659489 mux->header_written = 1;
660490
661491 av_dump_format(fc, of->index, fc->url, 1);
662− nb_output_dumped++;
663−
664− if (sdp_filename || want_sdp) {
665− ret = print_sdp(sdp_filename);
666− if (ret < 0) {
667− av_log(NULL, AV_LOG_ERROR, "Error writing the SDP.\n");
668− return ret;
669− } else if (ret == 1) {
670− /* SDP is written only after all the muxers are ready, so now we
671− * start ALL the threads */
672− for (i = 0; i < nb_output_files; i++) {
673− ret = thread_start(mux_from_of(output_files[i]));
674− if (ret < 0)
675− return ret;
676− }
677− }
678− } else {
679− ret = thread_start(mux_from_of(of));
680− if (ret < 0)
681− return ret;
682− }
492+ atomic_fetch_add(&nb_output_dumped, 1);
683493
684494 return 0;
685495 }
@@ -736,9 +546,10 @@ int of_stream_init(OutputFile *of, OutputStream *ost) · 50 unmodified lines
736546 ost->st->time_base);
737547 }
738548
739− ost->initialized = 1;
549+ if (ms->sch_idx >= 0)
550+ return sch_mux_stream_ready(mux->sch, of->index, ms->sch_idx);
740551
741− return mux_check_init(mux);
552+ return 0;
742553 }
743554
744555 static int check_written(OutputFile *of)
@@ -852,15 +663,13 @@ int of_write_trailer(OutputFile *of) · 107 unmodified lines
852663 AVFormatContext *fc = mux->fc;
853664 int ret, mux_result = 0;
854665
855− if (!mux->tq) {
666+ if (!mux->header_written) {
856667 av_log(mux, AV_LOG_ERROR,
857668 "Nothing was written into output file, because "
858669 "at least one of its streams received no packets.\n");
859670 return AVERROR(EINVAL);
860671 }
861672
862− mux_result = thread_stop(mux);
863−
864673 ret = av_write_trailer(fc);
865674 if (ret < 0) {
866675 av_log(mux, AV_LOG_ERROR, "Error writing trailer: %s\n", av_err2str(ret));
@@ -905,13 +714,6 @@ static void ost_free(OutputStream **post) · 38 unmodified lines
905714 ost->logfile = NULL;
906715 }
907716
908− if (ms->muxing_queue) {
909− AVPacket *pkt;
910− while (av_fifo_read(ms->muxing_queue, &pkt, 1) >= 0)
911− av_packet_free(&pkt);
912− av_fifo_freep2(&ms->muxing_queue);
913− }
914−
915717 avcodec_parameters_free(&ost->par_in);
916718
917719 av_bsf_free(&ms->bsf_ctx);
@@ -976,8 +778,6 @@ void of_free(OutputFile **pof) · 58 unmodified lines
976778 return;
977779 mux = mux_from_of(of);
978780
979− thread_stop(mux);
980−
981781 sq_free(&of->sq_encode);
982782 sq_free(&mux->sq_mux);
983783
fftools/ffmpeg_mux.h+1−23
@@ -25,7 +25,6 @@ · 24 unmodified lines
2525 #include <stdint.h>
2626
2727 #include "ffmpeg_sched.h"
28−#include "thread_queue.h"
2928
3029 #include "libavformat/avformat.h"
3130
@@ -33,7 +32,6 @@ · 1 unmodified line
3332
3433 #include "libavutil/dict.h"
3534 #include "libavutil/fifo.h"
36−#include "libavutil/thread.h"
3735
3836 typedef struct MuxStream {
3937 OutputStream ost;
@@ -41,9 +39,6 @@ typedef struct MuxStream { · 1 unmodified line
4139 // name used for logging
4240 char log_name[32];
4341
44− /* the packets are buffered here until the muxer is ready to be initialized */
45− AVFifo *muxing_queue;
46−
4742 AVBSFContext *bsf_ctx;
4843 AVPacket *bsf_pkt;
4944
@@ -57,17 +52,6 @@ typedef struct MuxStream { · 7 unmodified lines
5752
5853 int64_t max_frames;
5954
60− /*
61− * The size of the AVPackets' buffers in queue.
62− * Updated when a packet is either pushed or pulled from the queue.
63− */
64− size_t muxing_queue_data_size;
65−
66− int max_muxing_queue_size;
67−
68− /* Threshold after which max_muxing_queue_size will be in effect */
69− size_t muxing_queue_data_threshold;
70−
7155 // timestamp from which the streamcopied streams should start,
7256 // in AV_TIME_BASE_Q;
7357 // everything before it should be discarded
@@ -106,9 +90,6 @@ typedef struct Muxer { · 32 unmodified lines
10690 int *sch_stream_idx;
10791 int nb_sch_stream_idx;
10892
109− pthread_t thread;
110− ThreadQueue *tq;
111−
11293 AVDictionary *opts;
11394
11495 int thread_queue_size;
@@ -122,10 +103,7 @@ typedef struct Muxer { · 7 unmodified lines
122103 AVPacket *sq_pkt;
123104 } Muxer;
124105
125−/* whether we want to print an SDP, set in of_open() */
126−extern int want_sdp;
127−
128−int mux_check_init(Muxer *mux);
106+int mux_check_init(void *arg);
129107
130108 static MuxStream *ms_from_ost(OutputStream *ost)
131109 {
fftools/ffmpeg_mux_init.c+23−65
@@ -924,13 +924,6 @@ static int new_stream_audio(Muxer *mux, const OptionsContext *o, · 923 unmodified lines
924924 return 0;
925925 }
926926
927−static int new_stream_attachment(Muxer *mux, const OptionsContext *o,
928− OutputStream *ost)
929−{
930− ost->finished = 1;
931− return 0;
932−}
933−
934927 static int new_stream_subtitle(Muxer *mux, const OptionsContext *o,
935928 OutputStream *ost)
936929 {
@@ -1168,9 +1161,6 @@ static int ost_add(Muxer *mux, const OptionsContext *o, enum AVMediaType type, · 231 unmodified lines
11681161 if (!ost->par_in)
11691162 return AVERROR(ENOMEM);
11701163
1171− ms->muxing_queue = av_fifo_alloc2(8, sizeof(AVPacket*), 0);
1172− if (!ms->muxing_queue)
1173− return AVERROR(ENOMEM);
11741164 ms->last_mux_dts = AV_NOPTS_VALUE;
11751165
11761166 ost->st = st;
@@ -1190,7 +1180,8 @@ static int ost_add(Muxer *mux, const OptionsContext *o, enum AVMediaType type, · 13 unmodified lines
11901180 if (!ost->enc_ctx)
11911181 return AVERROR(ENOMEM);
11921182
1193− ret = sch_add_enc(mux->sch, encoder_thread, ost, NULL);
1183+ ret = sch_add_enc(mux->sch, encoder_thread, ost,
1184+ ost->type == AVMEDIA_TYPE_SUBTITLE ? NULL : enc_open);
11941185 if (ret < 0)
11951186 return ret;
11961187 ms->sch_idx_enc = ret;
@@ -1414,9 +1405,6 @@ static int ost_add(Muxer *mux, const OptionsContext *o, enum AVMediaType type, · 217 unmodified lines
14141405
14151406 sch_mux_stream_buffering(mux->sch, mux->sch_idx, ms->sch_idx,
14161407 max_muxing_queue_size, muxing_queue_data_threshold);
1417−
1418− ms->max_muxing_queue_size = max_muxing_queue_size;
1419− ms->muxing_queue_data_threshold = muxing_queue_data_threshold;
14201408 }
14211409
14221410 MATCH_PER_STREAM_OPT(bits_per_raw_sample, i, ost->bits_per_raw_sample,
@@ -1434,8 +1422,6 @@ static int ost_add(Muxer *mux, const OptionsContext *o, enum AVMediaType type, · 11 unmodified lines
14341422 if (ost->enc_ctx && av_get_exact_bits_per_sample(ost->enc_ctx->codec_id) == 24)
14351423 av_dict_set(&ost->swr_opts, "output_sample_bits", "24", 0);
14361424
1437− ost->last_mux_dts = AV_NOPTS_VALUE;
1438−
14391425 MATCH_PER_STREAM_OPT(copy_initial_nonkeyframes, i,
14401426 ms->copy_initial_nonkeyframes, oc, st);
14411427
@@ -1443,7 +1429,6 @@ static int ost_add(Muxer *mux, const OptionsContext *o, enum AVMediaType type, · 1 unmodified line
14431429 case AVMEDIA_TYPE_VIDEO: ret = new_stream_video (mux, o, ost); break;
14441430 case AVMEDIA_TYPE_AUDIO: ret = new_stream_audio (mux, o, ost); break;
14451431 case AVMEDIA_TYPE_SUBTITLE: ret = new_stream_subtitle (mux, o, ost); break;
1446− case AVMEDIA_TYPE_ATTACHMENT: ret = new_stream_attachment(mux, o, ost); break;
14471432 }
14481433 if (ret < 0)
14491434 return ret;
@@ -1938,7 +1923,6 @@ static int setup_sync_queues(Muxer *mux, AVFormatContext *oc, int64_t buf_size_u · 488 unmodified lines
19381923 MuxStream *ms = ms_from_ost(ost);
19391924 enum AVMediaType type = ost->type;
19401925
1941− ost->sq_idx_encode = -1;
19421926 ost->sq_idx_mux = -1;
19431927
19441928 nb_interleaved += IS_INTERLEAVED(type);
@@ -1961,11 +1945,17 @@ static int setup_sync_queues(Muxer *mux, AVFormatContext *oc, int64_t buf_size_u · 16 unmodified lines
19611945 * - at least one encoded audio/video stream is frame-limited, since
19621946 * that has similar semantics to 'shortest'
19631947 * - at least one audio encoder requires constant frame sizes
1948+ *
1949+ * Note that encoding sync queues are handled in the scheduler, because
1950+ * different encoders run in different threads and need external
1951+ * synchronization, while muxer sync queues can be handled inside the muxer
19641952 */
19651953 if ((of->shortest && nb_av_enc > 1) || limit_frames_av_enc || nb_audio_fs) {
1966− of->sq_encode = sq_alloc(SYNC_QUEUE_FRAMES, buf_size_us, mux);
1967− if (!of->sq_encode)
1968− return AVERROR(ENOMEM);
1954+ int sq_idx, ret;
1955+
1956+ sq_idx = sch_add_sq_enc(mux->sch, buf_size_us, mux);
1957+ if (sq_idx < 0)
1958+ return sq_idx;
19691959
19701960 for (int i = 0; i < oc->nb_streams; i++) {
19711961 OutputStream *ost = of->streams[i];
@@ -1975,13 +1965,11 @@ static int setup_sync_queues(Muxer *mux, AVFormatContext *oc, int64_t buf_size_u · 3 unmodified lines
19751965 if (!IS_AV_ENC(ost, type))
19761966 continue;
19771967
1978− ost->sq_idx_encode = sq_add_stream(of->sq_encode,
1979− of->shortest || ms->max_frames < INT64_MAX);
1980− if (ost->sq_idx_encode < 0)
1981− return ost->sq_idx_encode;
1982−
1983− if (ms->max_frames != INT64_MAX)
1984− sq_limit_frames(of->sq_encode, ost->sq_idx_encode, ms->max_frames);
1968+ ret = sch_sq_add_enc(mux->sch, sq_idx, ms->sch_idx_enc,
1969+ of->shortest || ms->max_frames < INT64_MAX,
1970+ ms->max_frames);
1971+ if (ret < 0)
1972+ return ret;
19851973 }
19861974 }
19871975
@@ -2652,23 +2640,6 @@ static int validate_enc_avopt(Muxer *mux, const AVDictionary *codec_avopt) · 664 unmodified lines
26522640 return 0;
26532641 }
26542642
2655−static int init_output_stream_nofilter(OutputStream *ost)
2656−{
2657− int ret = 0;
2658−
2659− if (ost->enc_ctx) {
2660− ret = enc_open(ost, NULL);
2661− if (ret < 0)
2662− return ret;
2663− } else {
2664− ret = of_stream_init(output_files[ost->file_index], ost);
2665− if (ret < 0)
2666− return ret;
2667− }
2668−
2669− return ret;
2670−}
2671−
26722643 static const char *output_file_item_name(void *obj)
26732644 {
26742645 const Muxer *mux = obj;
@@ -2751,8 +2722,6 @@ int of_open(const OptionsContext *o, const char *filename, Scheduler *sch) · 76 unmodified lines
27512722 av_strlcat(mux->log_name, "/", sizeof(mux->log_name));
27522723 av_strlcat(mux->log_name, oc->oformat->name, sizeof(mux->log_name));
27532724
2754− if (strcmp(oc->oformat->name, "rtp"))
2755− want_sdp = 0;
27562725
27572726 of->format = oc->oformat;
27582727 if (recording_time != INT64_MAX)
@@ -2768,7 +2737,7 @@ int of_open(const OptionsContext *o, const char *filename, Scheduler *sch) · 9 unmodified lines
27682737 AVFMT_FLAG_BITEXACT);
27692738 }
27702739
2771− err = sch_add_mux(sch, muxer_thread, NULL, mux,
2740+ err = sch_add_mux(sch, muxer_thread, mux_check_init, mux,
27722741 !strcmp(oc->oformat->name, "rtp"));
27732742 if (err < 0)
27742743 return err;
@@ -2854,26 +2823,15 @@ int of_open(const OptionsContext *o, const char *filename, Scheduler *sch) · 79 unmodified lines
28542823
28552824 of->url = filename;
28562825
2857− /* initialize stream copy and subtitle/data streams.
2858− * Encoded AVFrame based streams will get initialized when the first AVFrame
2859− * is received in do_video_out
2860− */
2826+ /* initialize streamcopy streams. */
28612827 for (int i = 0; i < of->nb_streams; i++) {
28622828 OutputStream *ost = of->streams[i];
28632829
2864− if (ost->filter)
2865− continue;
2866−
2867− err = init_output_stream_nofilter(ost);
2868− if (err < 0)
2869− return err;
2870− }
2871−
2872− /* write the header for files with no streams */
2873− if (of->format->flags & AVFMT_NOSTREAMS && oc->nb_streams == 0) {
2874− int ret = mux_check_init(mux);
2875− if (ret < 0)
2876− return ret;
2830+ if (!ost->enc) {
2831+ err = of_stream_init(of, ost);
2832+ if (err < 0)
2833+ return err;
2834+ }
28772835 }
28782836
28792837 return 0;
fftools/ffmpeg_opt.c+2−4
@@ -64,7 +64,6 @@ const char *const opt_name_top_field_first[] = {"top", NULL}; · 63 unmodified lines
6464 HWDevice *filter_hw_device;
6565
6666 char *vstats_filename;
67−char *sdp_filename;
6867
6968 float audio_drift_threshold = 0.1;
7069 float dts_delta_threshold = 10;
@@ -580,9 +579,8 @@ fail: · 509 unmodified lines
580579
581580 static int opt_sdp_file(void *optctx, const char *opt, const char *arg)
582581 {
583− av_free(sdp_filename);
584− sdp_filename = av_strdup(arg);
585− return 0;
582+ Scheduler *sch = optctx;
583+ return sch_sdp_filename(sch, arg);
586584 }
587585
588586 #if CONFIG_VAAPI
tests/ref/fate/ffmpeg-fix_sub_duration_heartbeat+13−21
@@ -1,48 +1,40 @@
11 1
2−00:00:00,968 --> 00:00:01,001
2+00:00:00,968 --> 00:00:01,168
33 <font face="Monospace">{\an7}(</font>
44
55 2
6−00:00:01,001 --> 00:00:01,168
7−<font face="Monospace">{\an7}(</font>
8−
9−3
106 00:00:01,168 --> 00:00:01,368
117 <font face="Monospace">{\an7}(<i> inaudibl</i></font>
128
13−4
9+3
1410 00:00:01,368 --> 00:00:01,568
1511 <font face="Monospace">{\an7}(<i> inaudible radio chat</i></font>
1612
17−5
13+4
1814 00:00:01,568 --> 00:00:02,002
1915 <font face="Monospace">{\an7}(<i> inaudible radio chatter</i> )</font>
2016
21−6
22−00:00:02,002 --> 00:00:03,003
23−<font face="Monospace">{\an7}(<i> inaudible radio chatter</i> )</font>
24−
25−7
26−00:00:03,003 --> 00:00:03,103
17+5
18+00:00:02,002 --> 00:00:03,103
2719 <font face="Monospace">{\an7}(<i> inaudible radio chatter</i> )</font>
2820
29−8
21+6
3022 00:00:03,103 --> 00:00:03,303
31−<font face="Monospace">{\an7}(<i> inaudible radio chatter</i> )
23+<font face="Monospace">{\an7}(<i> inaudible radio chatter</i> )
3224 >></font>
3325
34−9
26+7
3527 00:00:03,303 --> 00:00:03,503
36−<font face="Monospace">{\an7}(<i> inaudible radio chatter</i> )
28+<font face="Monospace">{\an7}(<i> inaudible radio chatter</i> )
3729 >> Safety rema</font>
3830
39−10
31+8
4032 00:00:03,504 --> 00:00:03,704
41−<font face="Monospace">{\an7}(<i> inaudible radio chatter</i> )
33+<font face="Monospace">{\an7}(<i> inaudible radio chatter</i> )
4234 >> Safety remains our numb</font>
4335
44−11
36+9
4537 00:00:03,704 --> 00:00:04,004
46−<font face="Monospace">{\an7}(<i> inaudible radio chatter</i> )
38+<font face="Monospace">{\an7}(<i> inaudible radio chatter</i> )
4739 >> Safety remains our number one</font>
4840

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t