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.
Unified · Split
| @@ -7,6 +7,8 @@ version <next>: · 6 unmodified lines | |||
| 7 | - EVC encoding using external library libxeve | 7 | - EVC encoding using external library libxeve |
| 8 | - QOA decoder and demuxer | 8 | - QOA decoder and demuxer |
| 9 | - aap filter | 9 | - aap filter |
| 10 | - demuxing, decoding, filtering, encoding, and muxing in the | ||
| 11 | ffmpeg CLI now all run in parallel | ||
| 10 | 12 | ||
| 11 | version 6.1: | 13 | version 6.1: |
| 12 | - libaribcaption decoder | 14 | - libaribcaption decoder |
| @@ -117,7 +117,7 @@ typedef struct BenchmarkTimeStamps { · 116 unmodified lines | |||
| 117 | static BenchmarkTimeStamps get_benchmark_time_stamps(void); | 117 | static BenchmarkTimeStamps get_benchmark_time_stamps(void); |
| 118 | static int64_t getmaxrss(void); | 118 | static int64_t getmaxrss(void); |
| 119 | 119 | ||
| 120 | unsigned nb_output_dumped = 0; | 120 | atomic_uint nb_output_dumped = 0; |
| 121 | 121 | ||
| 122 | static BenchmarkTimeStamps current_time; | 122 | static BenchmarkTimeStamps current_time; |
| 123 | AVIOContext *progress_avio = NULL; | 123 | AVIOContext *progress_avio = NULL; |
| @@ -138,30 +138,6 @@ static struct termios oldtty; · 14 unmodified lines | |||
| 138 | static int restore_tty; | 138 | static int restore_tty; |
| 139 | #endif | 139 | #endif |
| 140 | 140 | ||
| 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 | |||
| 165 | static void term_exit_sigsafe(void) | 141 | static void term_exit_sigsafe(void) |
| 166 | { | 142 | { |
| 167 | #if HAVE_TERMIOS_H | 143 | #if HAVE_TERMIOS_H |
| @@ -499,23 +475,13 @@ void update_benchmark(const char *fmt, ...) · 331 unmodified lines | |||
| 499 | } | 475 | } |
| 500 | } | 476 | } |
| 501 | 477 | ||
| 502 | void close_output_stream(OutputStream *ost) | 478 | static void print_report(int is_last_report, int64_t timer_start, int64_t cur_time, int64_t pts) |
| 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) | ||
| 512 | { | 479 | { |
| 513 | AVBPrint buf, buf_script; | 480 | AVBPrint buf, buf_script; |
| 514 | int64_t total_size = of_filesize(output_files[0]); | 481 | int64_t total_size = of_filesize(output_files[0]); |
| 515 | int vid; | 482 | int vid; |
| 516 | double bitrate; | 483 | double bitrate; |
| 517 | double speed; | 484 | double speed; |
| 518 | int64_t pts = AV_NOPTS_VALUE; | ||
| 519 | static int64_t last_time = -1; | 485 | static int64_t last_time = -1; |
| 520 | static int first_report = 1; | 486 | static int first_report = 1; |
| 521 | uint64_t nb_frames_dup = 0, nb_frames_drop = 0; | 487 | 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 | |||
| 533 | last_time = cur_time; | 499 | last_time = cur_time; |
| 534 | } | 500 | } |
| 535 | if (((cur_time - last_time) < stats_period && !first_report) || | 501 | 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)) |
| 537 | return; | 503 | return; |
| 538 | last_time = cur_time; | 504 | last_time = cur_time; |
| 539 | } | 505 | } |
| @@ -544,7 +510,7 @@ static void print_report(int is_last_report, int64_t timer_start, int64_t cur_ti · 4 unmodified lines | |||
| 544 | av_bprint_init(&buf, 0, AV_BPRINT_SIZE_AUTOMATIC); | 510 | av_bprint_init(&buf, 0, AV_BPRINT_SIZE_AUTOMATIC); |
| 545 | av_bprint_init(&buf_script, 0, AV_BPRINT_SIZE_AUTOMATIC); | 511 | av_bprint_init(&buf_script, 0, AV_BPRINT_SIZE_AUTOMATIC); |
| 546 | for (OutputStream *ost = ost_iter(NULL); ost; ost = ost_iter(ost)) { | 512 | 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; |
| 548 | 514 | ||
| 549 | if (vid && ost->type == AVMEDIA_TYPE_VIDEO) { | 515 | if (vid && ost->type == AVMEDIA_TYPE_VIDEO) { |
| 550 | av_bprintf(&buf, "q=%2.1f ", q); | 516 | 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 | |||
| 565 | if (is_last_report) | 531 | if (is_last_report) |
| 566 | av_bprintf(&buf, "L"); | 532 | av_bprintf(&buf, "L"); |
| 567 | 533 | ||
| 568 | nb_frames_dup = ost->filter->nb_frames_dup; | 534 | nb_frames_dup = atomic_load(&ost->filter->nb_frames_dup); |
| 569 | nb_frames_drop = ost->filter->nb_frames_drop; | 535 | nb_frames_drop = atomic_load(&ost->filter->nb_frames_drop); |
| 570 | 536 | ||
| 571 | vid = 1; | 537 | vid = 1; |
| 572 | } | 538 | } |
| 573 | /* compute min output value */ | 539 | } |
| 574 | if (ost->last_mux_dts != AV_NOPTS_VALUE) { | 540 | |
| 575 | if (pts == AV_NOPTS_VALUE || ost->last_mux_dts > pts) | 541 | if (copy_ts) { |
| 576 | pts = ost->last_mux_dts; | 542 | if (copy_ts_first_pts == AV_NOPTS_VALUE && pts > 1) |
| 577 | if (copy_ts) { | 543 | copy_ts_first_pts = pts; |
| 578 | if (copy_ts_first_pts == AV_NOPTS_VALUE && pts > 1) | 544 | if (copy_ts_first_pts != AV_NOPTS_VALUE) |
| 579 | copy_ts_first_pts = pts; | 545 | pts -= copy_ts_first_pts; |
| 580 | if (copy_ts_first_pts != AV_NOPTS_VALUE) | ||
| 581 | pts -= copy_ts_first_pts; | ||
| 582 | } | ||
| 583 | } | ||
| 584 | } | 546 | } |
| 585 | 547 | ||
| 586 | us = FFABS64U(pts) % AV_TIME_BASE; | 548 | us = FFABS64U(pts) % AV_TIME_BASE; |
| @@ -783,81 +745,6 @@ int subtitle_wrap_frame(AVFrame *frame, AVSubtitle *subtitle, int copy) · 196 unmodified lines | |||
| 783 | return 0; | 745 | return 0; |
| 784 | } | 746 | } |
| 785 | 747 | ||
| 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 | |||
| 861 | static void print_stream_maps(void) | 748 | static void print_stream_maps(void) |
| 862 | { | 749 | { |
| 863 | av_log(NULL, AV_LOG_INFO, "Stream mapping:\n"); | 750 | av_log(NULL, AV_LOG_INFO, "Stream mapping:\n"); |
| @@ -934,43 +821,6 @@ static void print_stream_maps(void) · 70 unmodified lines | |||
| 934 | } | 821 | } |
| 935 | } | 822 | } |
| 936 | 823 | ||
| 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 | |||
| 974 | static void set_tty_echo(int on) | 824 | static void set_tty_echo(int on) |
| 975 | { | 825 | { |
| 976 | #if HAVE_TERMIOS_H | 826 | #if HAVE_TERMIOS_H |
| @@ -1042,149 +892,21 @@ static int check_keyboard_interaction(int64_t cur_time) · 65 unmodified lines | |||
| 1042 | return 0; | 892 | return 0; |
| 1043 | } | 893 | } |
| 1044 | 894 | ||
| 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 | |||
| 1168 | /* | 895 | /* |
| 1169 | * The following code is the main loop of the file converter | 896 | * The following code is the main loop of the file converter |
| 1170 | */ | 897 | */ |
| 1171 | static int transcode(Scheduler *sch, int *err_rate_exceeded) | 898 | static int transcode(Scheduler *sch) |
| 1172 | { | 899 | { |
| 1173 | int ret = 0, i; | 900 | int ret = 0, i; |
| 1174 | InputStream *ist; | 901 | int64_t timer_start, transcode_ts = 0; |
| 1175 | int64_t timer_start; | ||
| 1176 | AVPacket *demux_pkt = NULL; | ||
| 1177 | 902 | ||
| 1178 | print_stream_maps(); | 903 | print_stream_maps(); |
| 1179 | 904 | ||
| 1180 | *err_rate_exceeded = 0; | ||
| 1181 | atomic_store(&transcode_init_done, 1); | 905 | atomic_store(&transcode_init_done, 1); |
| 1182 | 906 | ||
| 1183 | demux_pkt = av_packet_alloc(); | 907 | ret = sch_start(sch); |
| 1184 | if (!demux_pkt) { | 908 | if (ret < 0) |
| 1185 | ret = AVERROR(ENOMEM); | 909 | return ret; |
| 1186 | goto fail; | ||
| 1187 | } | ||
| 1188 | 910 | ||
| 1189 | if (stdin_interaction) { | 911 | if (stdin_interaction) { |
| 1190 | av_log(NULL, AV_LOG_INFO, "Press [q] to stop, [?] for help\n"); | 912 | 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 | |||
| 1192 | 914 | ||
| 1193 | timer_start = av_gettime_relative(); | 915 | timer_start = av_gettime_relative(); |
| 1194 | 916 | ||
| 1195 | while (!received_sigterm) { | 917 | while (!sch_wait(sch, stats_period, &transcode_ts)) { |
| 1196 | OutputStream *ost; | ||
| 1197 | int64_t cur_time= av_gettime_relative(); | 918 | int64_t cur_time= av_gettime_relative(); |
| 1198 | 919 | ||
| 1199 | /* if 'q' pressed, exits */ | 920 | /* if 'q' pressed, exits */ |
| @@ -1201,49 +922,11 @@ static int transcode(Scheduler *sch, int *err_rate_exceeded) · 1 unmodified line | |||
| 1201 | if (check_keyboard_interaction(cur_time) < 0) | 922 | if (check_keyboard_interaction(cur_time) < 0) |
| 1202 | break; | 923 | break; |
| 1203 | 924 | ||
| 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 | |||
| 1222 | /* dump report by using the output first video and audio streams */ | 925 | /* dump report by using the output first video and audio streams */ |
| 1223 | print_report(0, timer_start, cur_time); | 926 | print_report(0, timer_start, cur_time, transcode_ts); |
| 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); | ||
| 1243 | } | 927 | } |
| 1244 | ret = err_merge(ret, enc_flush()); | ||
| 1245 | 928 | ||
| 1246 | term_exit(); | 929 | ret = sch_stop(sch); |
| 1247 | 930 | ||
| 1248 | /* write the trailer if needed */ | 931 | /* write the trailer if needed */ |
| 1249 | for (i = 0; i < nb_output_files; i++) { | 932 | for (i = 0; i < nb_output_files; i++) { |
| @@ -1251,11 +934,10 @@ static int transcode(Scheduler *sch, int *err_rate_exceeded) · 1 unmodified line | |||
| 1251 | ret = err_merge(ret, err); | 934 | ret = err_merge(ret, err); |
| 1252 | } | 935 | } |
| 1253 | 936 | ||
| 1254 | /* dump report by using the first video and audio streams */ | 937 | term_exit(); |
| 1255 | print_report(1, timer_start, av_gettime_relative()); | ||
| 1256 | 938 | ||
| 1257 | fail: | 939 | /* dump report by using the first video and audio streams */ |
| 1258 | av_packet_free(&demux_pkt); | 940 | print_report(1, timer_start, av_gettime_relative(), transcode_ts); |
| 1259 | 941 | ||
| 1260 | return ret; | 942 | return ret; |
| 1261 | } | 943 | } |
| @@ -1308,7 +990,7 @@ int main(int argc, char **argv) · 46 unmodified lines | |||
| 1308 | { | 990 | { |
| 1309 | Scheduler *sch = NULL; | 991 | Scheduler *sch = NULL; |
| 1310 | 992 | ||
| 1311 | int ret, err_rate_exceeded; | 993 | int ret; |
| 1312 | BenchmarkTimeStamps ti; | 994 | BenchmarkTimeStamps ti; |
| 1313 | 995 | ||
| 1314 | init_dynload(); | 996 | init_dynload(); |
| @@ -1350,7 +1032,7 @@ int main(int argc, char **argv) · 35 unmodified lines | |||
| 1350 | } | 1032 | } |
| 1351 | 1033 | ||
| 1352 | current_time = ti = get_benchmark_time_stamps(); | 1034 | current_time = ti = get_benchmark_time_stamps(); |
| 1353 | ret = transcode(sch, &err_rate_exceeded); | 1035 | ret = transcode(sch); |
| 1354 | if (ret >= 0 && do_benchmark) { | 1036 | if (ret >= 0 && do_benchmark) { |
| 1355 | int64_t utime, stime, rtime; | 1037 | int64_t utime, stime, rtime; |
| 1356 | current_time = get_benchmark_time_stamps(); | 1038 | current_time = get_benchmark_time_stamps(); |
| @@ -1362,8 +1044,8 @@ int main(int argc, char **argv) · 5 unmodified lines | |||
| 1362 | utime / 1000000.0, stime / 1000000.0, rtime / 1000000.0); | 1044 | utime / 1000000.0, stime / 1000000.0, rtime / 1000000.0); |
| 1363 | } | 1045 | } |
| 1364 | 1046 | ||
| 1365 | ret = received_nb_signals ? 255 : | 1047 | ret = received_nb_signals ? 255 : |
| 1366 | err_rate_exceeded ? 69 : ret; | 1048 | (ret == FFMPEG_ERROR_RATE_EXCEEDED) ? 69 : ret; |
| 1367 | 1049 | ||
| 1368 | finish: | 1050 | finish: |
| 1369 | if (ret == AVERROR_EXIT) | 1051 | if (ret == AVERROR_EXIT) |
| @@ -61,6 +61,8 @@ · 60 unmodified lines | |||
| 61 | #define FFMPEG_OPT_TOP 1 | 61 | #define FFMPEG_OPT_TOP 1 |
| 62 | #define FFMPEG_OPT_FORCE_KF_SOURCE_NO_DROP 1 | 62 | #define FFMPEG_OPT_FORCE_KF_SOURCE_NO_DROP 1 |
| 63 | 63 | ||
| 64 | #define FFMPEG_ERROR_RATE_EXCEEDED FFERRTAG('E', 'R', 'E', 'D') | ||
| 65 | |||
| 64 | enum VideoSyncMethod { | 66 | enum VideoSyncMethod { |
| 65 | VSYNC_AUTO = -1, | 67 | VSYNC_AUTO = -1, |
| 66 | VSYNC_PASSTHROUGH, | 68 | VSYNC_PASSTHROUGH, |
| @@ -82,13 +84,16 @@ enum HWAccelID { · 15 unmodified lines | |||
| 82 | }; | 84 | }; |
| 83 | 85 | ||
| 84 | enum FrameOpaque { | 86 | enum FrameOpaque { |
| 85 | FRAME_OPAQUE_REAP_FILTERS = 1, | 87 | FRAME_OPAQUE_SUB_HEARTBEAT = 1, |
| 86 | FRAME_OPAQUE_CHOOSE_INPUT, | ||
| 87 | FRAME_OPAQUE_SUB_HEARTBEAT, | ||
| 88 | FRAME_OPAQUE_EOF, | 88 | FRAME_OPAQUE_EOF, |
| 89 | FRAME_OPAQUE_SEND_COMMAND, | 89 | FRAME_OPAQUE_SEND_COMMAND, |
| 90 | }; | 90 | }; |
| 91 | 91 | ||
| 92 | enum PacketOpaque { | ||
| 93 | PKT_OPAQUE_SUB_HEARTBEAT = 1, | ||
| 94 | PKT_OPAQUE_FIX_SUB_DURATION, | ||
| 95 | }; | ||
| 96 | |||
| 92 | typedef struct HWDevice { | 97 | typedef struct HWDevice { |
| 93 | const char *name; | 98 | const char *name; |
| 94 | enum AVHWDeviceType type; | 99 | enum AVHWDeviceType type; |
| @@ -309,11 +314,8 @@ typedef struct OutputFilter { · 214 unmodified lines | |||
| 309 | 314 | ||
| 310 | enum AVMediaType type; | 315 | enum AVMediaType type; |
| 311 | 316 | ||
| 312 | /* pts of the last frame received from this filter, in AV_TIME_BASE_Q */ | 317 | atomic_uint_least64_t nb_frames_dup; |
| 313 | int64_t last_pts; | 318 | atomic_uint_least64_t nb_frames_drop; |
| 314 | |||
| 315 | uint64_t nb_frames_dup; | ||
| 316 | uint64_t nb_frames_drop; | ||
| 317 | } OutputFilter; | 319 | } OutputFilter; |
| 318 | 320 | ||
| 319 | typedef struct FilterGraph { | 321 | typedef struct FilterGraph { |
| @@ -426,11 +428,6 @@ typedef struct InputFile { · 106 unmodified lines | |||
| 426 | 428 | ||
| 427 | float readrate; | 429 | float readrate; |
| 428 | int accurate_seek; | 430 | 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; | ||
| 434 | } InputFile; | 431 | } InputFile; |
| 435 | 432 | ||
| 436 | enum forced_keyframes_const { | 433 | enum forced_keyframes_const { |
| @@ -532,8 +529,6 @@ typedef struct OutputStream { · 95 unmodified lines | |||
| 532 | InputStream *ist; | 529 | InputStream *ist; |
| 533 | 530 | ||
| 534 | AVStream *st; /* stream in the output file */ | 531 | 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; | ||
| 537 | 532 | ||
| 538 | AVRational enc_timebase; | 533 | AVRational enc_timebase; |
| 539 | 534 | ||
| @@ -578,13 +573,6 @@ typedef struct OutputStream { · 38 unmodified lines | |||
| 578 | AVDictionary *sws_dict; | 573 | AVDictionary *sws_dict; |
| 579 | AVDictionary *swr_opts; | 574 | AVDictionary *swr_opts; |
| 580 | char *apad; | 575 | 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; | ||
| 588 | 576 | ||
| 589 | const char *attachment_filename; | 577 | const char *attachment_filename; |
| 590 | 578 | ||
| @@ -598,9 +586,8 @@ typedef struct OutputStream { · 7 unmodified lines | |||
| 598 | uint64_t samples_encoded; | 586 | uint64_t samples_encoded; |
| 599 | 587 | ||
| 600 | /* packet quality factor */ | 588 | /* packet quality factor */ |
| 601 | int quality; | 589 | atomic_int quality; |
| 602 | 590 | ||
| 603 | int sq_idx_encode; | ||
| 604 | int sq_idx_mux; | 591 | int sq_idx_mux; |
| 605 | 592 | ||
| 606 | EncStats enc_stats_pre; | 593 | EncStats enc_stats_pre; |
| @@ -658,7 +645,6 @@ extern FilterGraph **filtergraphs; · 51 unmodified lines | |||
| 658 | extern int nb_filtergraphs; | 645 | extern int nb_filtergraphs; |
| 659 | 646 | ||
| 660 | extern char *vstats_filename; | 647 | extern char *vstats_filename; |
| 661 | extern char *sdp_filename; | ||
| 662 | 648 | ||
| 663 | extern float dts_delta_threshold; | 649 | extern float dts_delta_threshold; |
| 664 | extern float dts_error_threshold; | 650 | extern float dts_error_threshold; |
| @@ -691,7 +677,7 @@ extern const AVIOInterruptCB int_cb; · 26 unmodified lines | |||
| 691 | extern const OptionDef options[]; | 677 | extern const OptionDef options[]; |
| 692 | extern HWDevice *filter_hw_device; | 678 | extern HWDevice *filter_hw_device; |
| 693 | 679 | ||
| 694 | extern unsigned nb_output_dumped; | 680 | extern atomic_uint nb_output_dumped; |
| 695 | 681 | ||
| 696 | extern int ignore_unknown_streams; | 682 | extern int ignore_unknown_streams; |
| 697 | extern int copy_unknown_streams; | 683 | extern int copy_unknown_streams; |
| @@ -737,10 +723,6 @@ FrameData *frame_data(AVFrame *frame); · 39 unmodified lines | |||
| 737 | 723 | ||
| 738 | const FrameData *frame_data_c(AVFrame *frame); | 724 | const FrameData *frame_data_c(AVFrame *frame); |
| 739 | 725 | ||
| 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 | |||
| 744 | /** | 726 | /** |
| 745 | * Set up fallback filtering parameters from a decoder context. They will only | 727 | * Set up fallback filtering parameters from a decoder context. They will only |
| 746 | * be used if no frames are ever sent on this input, otherwise the actual | 728 | * 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 | |||
| 761 | 743 | ||
| 762 | void fg_free(FilterGraph **pfg); | 744 | void fg_free(FilterGraph **pfg); |
| 763 | 745 | ||
| 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 | |||
| 773 | void fg_send_command(FilterGraph *fg, double time, const char *target, | 746 | void fg_send_command(FilterGraph *fg, double time, const char *target, |
| 774 | const char *command, const char *arg, int all_filters); | 747 | const char *command, const char *arg, int all_filters); |
| 775 | 748 | ||
| 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 | |||
| 784 | int ffmpeg_parse_options(int argc, char **argv, Scheduler *sch); | 749 | int ffmpeg_parse_options(int argc, char **argv, Scheduler *sch); |
| 785 | 750 | ||
| 786 | void enc_stats_write(OutputStream *ost, EncStats *es, | 751 | void enc_stats_write(OutputStream *ost, EncStats *es, |
| @@ -807,25 +772,11 @@ int hwaccel_retrieve_data(AVCodecContext *avctx, AVFrame *input); · 20 unmodified lines | |||
| 807 | int dec_open(InputStream *ist, Scheduler *sch, unsigned sch_idx); | 772 | int dec_open(InputStream *ist, Scheduler *sch, unsigned sch_idx); |
| 808 | void dec_free(Decoder **pdec); | 773 | void dec_free(Decoder **pdec); |
| 809 | 774 | ||
| 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 | |||
| 821 | int enc_alloc(Encoder **penc, const AVCodec *codec, | 775 | int enc_alloc(Encoder **penc, const AVCodec *codec, |
| 822 | Scheduler *sch, unsigned sch_idx); | 776 | Scheduler *sch, unsigned sch_idx); |
| 823 | void enc_free(Encoder **penc); | 777 | void enc_free(Encoder **penc); |
| 824 | 778 | ||
| 825 | int enc_open(OutputStream *ost, const AVFrame *frame); | 779 | int enc_open(void *opaque, 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); | ||
| 829 | 780 | ||
| 830 | /* | 781 | /* |
| 831 | * Initialize muxing state for the given stream, should be called | 782 | * Initialize muxing state for the given stream, should be called |
| @@ -840,30 +791,11 @@ void of_free(OutputFile **pof); · 8 unmodified lines | |||
| 840 | 791 | ||
| 841 | void of_enc_stats_close(void); | 792 | void of_enc_stats_close(void); |
| 842 | 793 | ||
| 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 | |||
| 850 | int64_t of_filesize(OutputFile *of); | 794 | int64_t of_filesize(OutputFile *of); |
| 851 | 795 | ||
| 852 | int ifile_open(const OptionsContext *o, const char *filename, Scheduler *sch); | 796 | int ifile_open(const OptionsContext *o, const char *filename, Scheduler *sch); |
| 853 | void ifile_close(InputFile **f); | 797 | void ifile_close(InputFile **f); |
| 854 | 798 | ||
| 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 | |||
| 867 | int ist_output_add(InputStream *ist, OutputStream *ost); | 799 | int ist_output_add(InputStream *ist, OutputStream *ost); |
| 868 | int ist_filter_add(InputStream *ist, InputFilter *ifilter, int is_simple); | 800 | int ist_filter_add(InputStream *ist, InputFilter *ifilter, int is_simple); |
| 869 | 801 | ||
| @@ -880,9 +812,6 @@ InputStream *ist_iter(InputStream *prev); · 10 unmodified lines | |||
| 880 | * pass NULL to start iteration */ | 812 | * pass NULL to start iteration */ |
| 881 | OutputStream *ost_iter(OutputStream *prev); | 813 | OutputStream *ost_iter(OutputStream *prev); |
| 882 | 814 | ||
| 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); | ||
| 886 | void update_benchmark(const char *fmt, ...); | 815 | void update_benchmark(const char *fmt, ...); |
| 887 | 816 | ||
| 888 | #define SPECIFIER_OPT_FMT_str "%s" | 817 | #define SPECIFIER_OPT_FMT_str "%s" |
| @@ -54,24 +54,6 @@ struct Decoder { · 53 unmodified lines | |||
| 54 | 54 | ||
| 55 | Scheduler *sch; | 55 | Scheduler *sch; |
| 56 | unsigned sch_idx; | 56 | 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; | ||
| 75 | }; | 57 | }; |
| 76 | 58 | ||
| 77 | // data that is local to the decoder thread and not visible outside of it | 59 | // data that is local to the decoder thread and not visible outside of it |
| @@ -80,24 +62,6 @@ typedef struct DecThreadContext { · 2 unmodified lines | |||
| 80 | AVPacket *pkt; | 62 | AVPacket *pkt; |
| 81 | } DecThreadContext; | 63 | } DecThreadContext; |
| 82 | 64 | ||
| 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 | |||
| 101 | void dec_free(Decoder **pdec) | 65 | void dec_free(Decoder **pdec) |
| 102 | { | 66 | { |
| 103 | Decoder *dec = *pdec; | 67 | Decoder *dec = *pdec; |
| @@ -105,8 +69,6 @@ void dec_free(Decoder **pdec) · 1 unmodified line | |||
| 105 | if (!dec) | 69 | if (!dec) |
| 106 | return; | 70 | return; |
| 107 | 71 | ||
| 108 | dec_thread_stop(dec); | ||
| 109 | |||
| 110 | av_frame_free(&dec->frame); | 72 | av_frame_free(&dec->frame); |
| 111 | av_packet_free(&dec->pkt); | 73 | av_packet_free(&dec->pkt); |
| 112 | 74 | ||
| @@ -148,25 +110,6 @@ fail: · 35 unmodified lines | |||
| 148 | return AVERROR(ENOMEM); | 110 | return AVERROR(ENOMEM); |
| 149 | } | 111 | } |
| 150 | 112 | ||
| 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 | |||
| 170 | static AVRational audio_samplerate_update(void *logctx, Decoder *d, | 113 | static AVRational audio_samplerate_update(void *logctx, Decoder *d, |
| 171 | const AVFrame *frame) | 114 | const AVFrame *frame) |
| 172 | { | 115 | { |
| @@ -421,28 +364,14 @@ static int process_subtitle(InputStream *ist, AVFrame *frame) · 248 unmodified lines | |||
| 421 | if (!subtitle) | 364 | if (!subtitle) |
| 422 | return 0; | 365 | return 0; |
| 423 | 366 | ||
| 424 | ret = send_frame_to_filters(ist, frame); | 367 | ret = sch_dec_send(d->sch, d->sch_idx, frame); |
| 425 | if (ret < 0) | 368 | if (ret < 0) |
| 426 | return ret; | 369 | av_frame_unref(frame); |
| 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 | } | ||
| 441 | 370 | ||
| 442 | return 0; | 371 | return ret == AVERROR_EOF ? AVERROR_EXIT : ret; |
| 443 | } | 372 | } |
| 444 | 373 | ||
| 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) |
| 446 | { | 375 | { |
| 447 | Decoder *d = ist->decoder; | 376 | Decoder *d = ist->decoder; |
| 448 | int ret = AVERROR_BUG; | 377 | int ret = AVERROR_BUG; |
| @@ -468,12 +397,24 @@ int fix_sub_duration_heartbeat(InputStream *ist, int64_t signal_pts) · 19 unmodified lines | |||
| 468 | static int transcode_subtitles(InputStream *ist, const AVPacket *pkt, | 397 | static int transcode_subtitles(InputStream *ist, const AVPacket *pkt, |
| 469 | AVFrame *frame) | 398 | AVFrame *frame) |
| 470 | { | 399 | { |
| 471 | Decoder *d = ist->decoder; | 400 | Decoder *d = ist->decoder; |
| 472 | AVPacket *flush_pkt = NULL; | 401 | AVPacket *flush_pkt = NULL; |
| 473 | AVSubtitle subtitle; | 402 | AVSubtitle subtitle; |
| 474 | int got_output; | 403 | int got_output; |
| 475 | int ret; | 404 | int ret; |
| 476 | 405 | ||
| 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 | |||
| 477 | if (!pkt) { | 418 | if (!pkt) { |
| 478 | flush_pkt = av_packet_alloc(); | 419 | flush_pkt = av_packet_alloc(); |
| 479 | if (!flush_pkt) | 420 | if (!flush_pkt) |
| @@ -496,7 +437,7 @@ static int transcode_subtitles(InputStream *ist, const AVPacket *pkt, · 16 unmodified lines | |||
| 496 | 437 | ||
| 497 | ist->frames_decoded++; | 438 | ist->frames_decoded++; |
| 498 | 439 | ||
| 499 | // XXX the queue for transferring data back to the main thread runs | 440 | // XXX the queue for transferring data to consumers runs |
| 500 | // on AVFrames, so we wrap AVSubtitle in an AVBufferRef and put that | 441 | // on AVFrames, so we wrap AVSubtitle in an AVBufferRef and put that |
| 501 | // inside the frame | 442 | // inside the frame |
| 502 | // eventually, subtitles should be switched to use AVFrames natively | 443 | // 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 | |||
| 509 | frame->width = ist->dec_ctx->width; | 450 | frame->width = ist->dec_ctx->width; |
| 510 | frame->height = ist->dec_ctx->height; | 451 | frame->height = ist->dec_ctx->height; |
| 511 | 452 | ||
| 512 | ret = tq_send(d->queue_out, 0, frame); | 453 | return process_subtitle(ist, 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; | ||
| 532 | } | 454 | } |
| 533 | 455 | ||
| 534 | static int packet_decode(InputStream *ist, AVPacket *pkt, AVFrame *frame) | 456 | 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 | |||
| 635 | 557 | ||
| 636 | ist->frames_decoded++; | 558 | ist->frames_decoded++; |
| 637 | 559 | ||
| 638 | ret = tq_send(d->queue_out, 0, frame); | 560 | ret = sch_dec_send(d->sch, d->sch_idx, frame); |
| 639 | if (ret < 0) | 561 | if (ret < 0) { |
| 640 | return ret; | 562 | av_frame_unref(frame); |
| 563 | return ret == AVERROR_EOF ? AVERROR_EXIT : ret; | ||
| 564 | } | ||
| 641 | } | 565 | } |
| 642 | } | 566 | } |
| 643 | 567 | ||
| @@ -679,7 +603,6 @@ fail: · 35 unmodified lines | |||
| 679 | void *decoder_thread(void *arg) | 603 | void *decoder_thread(void *arg) |
| 680 | { | 604 | { |
| 681 | InputStream *ist = arg; | 605 | InputStream *ist = arg; |
| 682 | InputFile *ifile = input_files[ist->file_index]; | ||
| 683 | Decoder *d = ist->decoder; | 606 | Decoder *d = ist->decoder; |
| 684 | DecThreadContext dt; | 607 | DecThreadContext dt; |
| 685 | int ret = 0, input_status = 0; | 608 | int ret = 0, input_status = 0; |
| @@ -691,19 +614,31 @@ void *decoder_thread(void *arg) · 5 unmodified lines | |||
| 691 | dec_thread_set_name(ist); | 614 | dec_thread_set_name(ist); |
| 692 | 615 | ||
| 693 | while (!input_status) { | 616 | while (!input_status) { |
| 694 | int dummy, flush_buffers; | 617 | int flush_buffers, have_data; |
| 695 | 618 | ||
| 696 | input_status = tq_receive(d->queue_in, &dummy, dt.pkt); | 619 | input_status = sch_dec_receive(d->sch, d->sch_idx, dt.pkt); |
| 697 | flush_buffers = input_status >= 0 && !dt.pkt->buf; | 620 | have_data = input_status >= 0 && |
| 698 | if (!dt.pkt->buf) | 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) | ||
| 699 | av_log(ist, AV_LOG_VERBOSE, "Decoder thread received %s packet\n", | 626 | av_log(ist, AV_LOG_VERBOSE, "Decoder thread received %s packet\n", |
| 700 | flush_buffers ? "flush" : "EOF"); | 627 | flush_buffers ? "flush" : "EOF"); |
| 701 | 628 | ||
| 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); |
| 703 | 630 | ||
| 704 | av_packet_unref(dt.pkt); | 631 | av_packet_unref(dt.pkt); |
| 705 | av_frame_unref(dt.frame); | 632 | av_frame_unref(dt.frame); |
| 706 | 633 | ||
| 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 | |||
| 707 | if (ret == AVERROR_EOF) { | 642 | if (ret == AVERROR_EOF) { |
| 708 | av_log(ist, AV_LOG_VERBOSE, "Decoder returned EOF, %s\n", | 643 | av_log(ist, AV_LOG_VERBOSE, "Decoder returned EOF, %s\n", |
| 709 | flush_buffers ? "resetting" : "finishing"); | 644 | flush_buffers ? "resetting" : "finishing"); |
| @@ -711,11 +646,10 @@ void *decoder_thread(void *arg) · 1 unmodified line | |||
| 711 | if (!flush_buffers) | 646 | if (!flush_buffers) |
| 712 | break; | 647 | break; |
| 713 | 648 | ||
| 714 | /* report last frame duration to the demuxer thread */ | 649 | /* report last frame duration to the scheduler */ |
| 715 | if (ist->dec->type == AVMEDIA_TYPE_AUDIO) { | 650 | if (ist->dec->type == AVMEDIA_TYPE_AUDIO) { |
| 716 | Timestamp ts = { .ts = d->last_frame_pts + d->last_frame_duration_est, | 651 | dt.pkt->pts = d->last_frame_pts + d->last_frame_duration_est; |
| 717 | .tb = d->last_frame_tb }; | 652 | dt.pkt->time_base = d->last_frame_tb; |
| 718 | av_thread_message_queue_send(ifile->audio_ts_queue, &ts, 0); | ||
| 719 | } | 653 | } |
| 720 | 654 | ||
| 721 | avcodec_flush_buffers(ist->dec_ctx); | 655 | avcodec_flush_buffers(ist->dec_ctx); |
| @@ -724,147 +658,45 @@ void *decoder_thread(void *arg) · 2 unmodified lines | |||
| 724 | av_err2str(ret)); | 658 | av_err2str(ret)); |
| 725 | break; | 659 | break; |
| 726 | } | 660 | } |
| 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 | } | ||
| 735 | } | 661 | } |
| 736 | 662 | ||
| 737 | // EOF is normal thread termination | 663 | // EOF is normal thread termination |
| 738 | if (ret == AVERROR_EOF) | 664 | if (ret == AVERROR_EOF) |
| 739 | ret = 0; | 665 | ret = 0; |
| 740 | 666 | ||
| 741 | finish: | 667 | // on success send EOF timestamp to our downstreams |
| 742 | tq_receive_finish(d->queue_in, 0); | 668 | if (ret >= 0) { |
| 743 | tq_send_finish (d->queue_out, 0); | 669 | float err_rate; |
| 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; | ||
| 765 | 670 | ||
| 766 | // send the packet/flush request/EOF to the decoder thread | 671 | av_frame_unref(dt.frame); |
| 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); | ||
| 781 | 672 | ||
| 782 | // retrieve all decoded data for the packet | 673 | dt.frame->opaque = (void*)(intptr_t)FRAME_OPAQUE_EOF; |
| 783 | while (1) { | 674 | dt.frame->pts = d->last_frame_pts == AV_NOPTS_VALUE ? AV_NOPTS_VALUE : |
| 784 | int dummy; | 675 | d->last_frame_pts + d->last_frame_duration_est; |
| 676 | dt.frame->time_base = d->last_frame_tb; | ||
| 785 | 677 | ||
| 786 | ret = tq_receive(d->queue_out, &dummy, d->frame); | 678 | ret = sch_dec_send(d->sch, d->sch_idx, dt.frame); |
| 787 | if (ret < 0) | 679 | if (ret < 0 && ret != AVERROR_EOF) { |
| 680 | av_log(NULL, AV_LOG_FATAL, | ||
| 681 | "Error signalling EOF timestamp: %s\n", av_err2str(ret)); | ||
| 788 | goto finish; | 682 | 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); | ||
| 799 | } | 683 | } |
| 800 | av_frame_unref(d->frame); | 684 | ret = 0; |
| 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 | } | ||
| 851 | 685 | ||
| 852 | ret = pthread_create(&d->thread, NULL, decoder_thread, ist); | 686 | err_rate = (ist->frames_decoded || ist->decode_errors) ? |
| 853 | if (ret) { | 687 | ist->decode_errors / (ist->frames_decoded + ist->decode_errors) : 0.f; |
| 854 | ret = AVERROR(ret); | 688 | if (err_rate > max_error_rate) { |
| 855 | av_log(ist, AV_LOG_ERROR, "pthread_create() failed: %s\n", | 689 | av_log(ist, AV_LOG_FATAL, "Decode error rate %g exceeds maximum %g\n", |
| 856 | av_err2str(ret)); | 690 | err_rate, max_error_rate); |
| 857 | goto fail; | 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); | ||
| 858 | } | 694 | } |
| 859 | 695 | ||
| 860 | return 0; | 696 | finish: |
| 861 | fail: | 697 | dec_thread_uninit(&dt); |
| 862 | if (ret >= 0) | ||
| 863 | ret = AVERROR(ENOMEM); | ||
| 864 | 698 | ||
| 865 | tq_free(&d->queue_in); | 699 | return (void*)(intptr_t)ret; |
| 866 | tq_free(&d->queue_out); | ||
| 867 | return ret; | ||
| 868 | } | 700 | } |
| 869 | 701 | ||
| 870 | static enum AVPixelFormat get_format(AVCodecContext *s, const enum AVPixelFormat *pix_fmts) | 702 | 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 | |||
| 1118 | if (ret < 0) | 950 | if (ret < 0) |
| 1119 | return ret; | 951 | return ret; |
| 1120 | 952 | ||
| 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 | |||
| 1128 | return 0; | 953 | return 0; |
| 1129 | } | 954 | } |
| @@ -22,8 +22,6 @@ · 21 unmodified lines | |||
| 22 | #include "ffmpeg.h" | 22 | #include "ffmpeg.h" |
| 23 | #include "ffmpeg_sched.h" | 23 | #include "ffmpeg_sched.h" |
| 24 | #include "ffmpeg_utils.h" | 24 | #include "ffmpeg_utils.h" |
| 25 | #include "objpool.h" | ||
| 26 | #include "thread_queue.h" | ||
| 27 | 25 | ||
| 28 | #include "libavutil/avassert.h" | 26 | #include "libavutil/avassert.h" |
| 29 | #include "libavutil/avstring.h" | 27 | #include "libavutil/avstring.h" |
| @@ -35,7 +33,6 @@ · 5 unmodified lines | |||
| 35 | #include "libavutil/pixdesc.h" | 33 | #include "libavutil/pixdesc.h" |
| 36 | #include "libavutil/time.h" | 34 | #include "libavutil/time.h" |
| 37 | #include "libavutil/timestamp.h" | 35 | #include "libavutil/timestamp.h" |
| 38 | #include "libavutil/thread.h" | ||
| 39 | 36 | ||
| 40 | #include "libavcodec/packet.h" | 37 | #include "libavcodec/packet.h" |
| 41 | 38 | ||
| @@ -66,7 +63,11 @@ typedef struct DemuxStream { · 24 unmodified lines | |||
| 66 | 63 | ||
| 67 | double ts_scale; | 64 | double ts_scale; |
| 68 | 65 | ||
| 66 | // scheduler returned EOF for this stream | ||
| 67 | int finished; | ||
| 68 | |||
| 69 | int streamcopy_needed; | 69 | int streamcopy_needed; |
| 70 | int have_sub2video; | ||
| 70 | 71 | ||
| 71 | int wrap_correction_done; | 72 | int wrap_correction_done; |
| 72 | int saw_first_ts; | 73 | int saw_first_ts; |
| @@ -101,6 +102,7 @@ typedef struct Demuxer { · 28 unmodified lines | |||
| 101 | 102 | ||
| 102 | /* number of times input stream should be looped */ | 103 | /* number of times input stream should be looped */ |
| 103 | int loop; | 104 | int loop; |
| 105 | int have_audio_dec; | ||
| 104 | /* duration of the looped segment of the input file */ | 106 | /* duration of the looped segment of the input file */ |
| 105 | Timestamp duration; | 107 | Timestamp duration; |
| 106 | /* pts with the smallest/largest values ever seen */ | 108 | /* pts with the smallest/largest values ever seen */ |
| @@ -113,11 +115,12 @@ typedef struct Demuxer { · 6 unmodified lines | |||
| 113 | double readrate_initial_burst; | 115 | double readrate_initial_burst; |
| 114 | 116 | ||
| 115 | Scheduler *sch; | 117 | Scheduler *sch; |
| 116 | ThreadQueue *thread_queue; | 118 | |
| 117 | int thread_queue_size; | 119 | AVPacket *pkt_heartbeat; |
| 118 | pthread_t thread; | ||
| 119 | 120 | ||
| 120 | int read_started; | 121 | int read_started; |
| 122 | int nb_streams_used; | ||
| 123 | int nb_streams_finished; | ||
| 121 | } Demuxer; | 124 | } Demuxer; |
| 122 | 125 | ||
| 123 | static DemuxStream *ds_from_ist(InputStream *ist) | 126 | static DemuxStream *ds_from_ist(InputStream *ist) |
| @@ -153,7 +156,7 @@ static void report_new_stream(Demuxer *d, const AVPacket *pkt) · 29 unmodified lines | |||
| 153 | d->nb_streams_warn = pkt->stream_index + 1; | 156 | d->nb_streams_warn = pkt->stream_index + 1; |
| 154 | } | 157 | } |
| 155 | 158 | ||
| 156 | static int seek_to_start(Demuxer *d) | 159 | static int seek_to_start(Demuxer *d, Timestamp end_pts) |
| 157 | { | 160 | { |
| 158 | InputFile *ifile = &d->f; | 161 | InputFile *ifile = &d->f; |
| 159 | AVFormatContext *is = ifile->ctx; | 162 | AVFormatContext *is = ifile->ctx; |
| @@ -163,21 +166,10 @@ static int seek_to_start(Demuxer *d) · 3 unmodified lines | |||
| 163 | if (ret < 0) | 166 | if (ret < 0) |
| 164 | return ret; | 167 | return ret; |
| 165 | 168 | ||
| 166 | if (ifile->audio_ts_queue_size) { | 169 | if (end_pts.ts != AV_NOPTS_VALUE && |
| 167 | int got_ts = 0; | 170 | (d->max_pts.ts == AV_NOPTS_VALUE || |
| 168 | 171 | av_compare_ts(d->max_pts.ts, d->max_pts.tb, end_pts.ts, end_pts.tb) < 0)) | |
| 169 | while (got_ts < ifile->audio_ts_queue_size) { | 172 | d->max_pts = end_pts; |
| 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 | } | ||
| 181 | 173 | ||
| 182 | if (d->max_pts.ts != AV_NOPTS_VALUE) { | 174 | if (d->max_pts.ts != AV_NOPTS_VALUE) { |
| 183 | int64_t min_pts = d->min_pts.ts == AV_NOPTS_VALUE ? 0 : d->min_pts.ts; | 175 | 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 | |||
| 404 | duration = av_rescale_q(d->duration.ts, d->duration.tb, pkt->time_base); | 396 | duration = av_rescale_q(d->duration.ts, d->duration.tb, pkt->time_base); |
| 405 | if (pkt->pts != AV_NOPTS_VALUE) { | 397 | if (pkt->pts != AV_NOPTS_VALUE) { |
| 406 | // audio decoders take precedence for estimating total file duration | 398 | // 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; |
| 408 | 400 | ||
| 409 | pkt->pts += duration; | 401 | pkt->pts += duration; |
| 410 | 402 | ||
| @@ -440,7 +432,7 @@ static int ts_fixup(Demuxer *d, AVPacket *pkt) · 29 unmodified lines | |||
| 440 | return 0; | 432 | return 0; |
| 441 | } | 433 | } |
| 442 | 434 | ||
| 443 | static int input_packet_process(Demuxer *d, AVPacket *pkt) | 435 | static int input_packet_process(Demuxer *d, AVPacket *pkt, unsigned *send_flags) |
| 444 | { | 436 | { |
| 445 | InputFile *f = &d->f; | 437 | InputFile *f = &d->f; |
| 446 | InputStream *ist = f->streams[pkt->stream_index]; | 438 | InputStream *ist = f->streams[pkt->stream_index]; |
| @@ -451,6 +443,16 @@ static int input_packet_process(Demuxer *d, AVPacket *pkt) · 4 unmodified lines | |||
| 451 | if (ret < 0) | 443 | if (ret < 0) |
| 452 | return ret; | 444 | return ret; |
| 453 | 445 | ||
| 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 | |||
| 454 | ds->data_size += pkt->size; | 456 | ds->data_size += pkt->size; |
| 455 | ds->nb_packets++; | 457 | ds->nb_packets++; |
| 456 | 458 | ||
| @@ -465,6 +467,8 @@ static int input_packet_process(Demuxer *d, AVPacket *pkt) · 8 unmodified lines | |||
| 465 | av_ts2timestr(input_files[ist->file_index]->ts_offset, &AV_TIME_BASE_Q)); | 467 | av_ts2timestr(input_files[ist->file_index]->ts_offset, &AV_TIME_BASE_Q)); |
| 466 | } | 468 | } |
| 467 | 469 | ||
| 470 | pkt->stream_index = ds->sch_idx_stream; | ||
| 471 | |||
| 468 | return 0; | 472 | return 0; |
| 469 | } | 473 | } |
| 470 | 474 | ||
| @@ -488,6 +492,65 @@ static void readrate_sleep(Demuxer *d) · 17 unmodified lines | |||
| 488 | } | 492 | } |
| 489 | } | 493 | } |
| 490 | 494 | ||
| 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 | |||
| 491 | static void discard_unused_programs(InputFile *ifile) | 554 | static void discard_unused_programs(InputFile *ifile) |
| 492 | { | 555 | { |
| 493 | for (int j = 0; j < ifile->ctx->nb_programs; j++) { | 556 | for (int j = 0; j < ifile->ctx->nb_programs; j++) { |
| @@ -527,9 +590,13 @@ static void *input_thread(void *arg) · 33 unmodified lines | |||
| 527 | 590 | ||
| 528 | discard_unused_programs(f); | 591 | discard_unused_programs(f); |
| 529 | 592 | ||
| 593 | d->read_started = 1; | ||
| 530 | d->wallclock_start = av_gettime_relative(); | 594 | d->wallclock_start = av_gettime_relative(); |
| 531 | 595 | ||
| 532 | while (1) { | 596 | while (1) { |
| 597 | DemuxStream *ds; | ||
| 598 | unsigned send_flags = 0; | ||
| 599 | |||
| 533 | ret = av_read_frame(f->ctx, pkt); | 600 | ret = av_read_frame(f->ctx, pkt); |
| 534 | 601 | ||
| 535 | if (ret == AVERROR(EAGAIN)) { | 602 | if (ret == AVERROR(EAGAIN)) { |
| @@ -538,11 +605,13 @@ static void *input_thread(void *arg) · 2 unmodified lines | |||
| 538 | } | 605 | } |
| 539 | if (ret < 0) { | 606 | if (ret < 0) { |
| 540 | if (d->loop) { | 607 | if (d->loop) { |
| 541 | /* signal looping to the consumer thread */ | 608 | /* signal looping to our consumers */ |
| 542 | pkt->stream_index = -1; | 609 | 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); | ||
| 544 | if (ret >= 0) | 612 | if (ret >= 0) |
| 545 | ret = seek_to_start(d); | 613 | ret = seek_to_start(d, (Timestamp){ .ts = pkt->pts, |
| 614 | .tb = pkt->time_base }); | ||
| 546 | if (ret >= 0) | 615 | if (ret >= 0) |
| 547 | continue; | 616 | continue; |
| 548 | 617 | ||
| @@ -551,9 +620,11 @@ static void *input_thread(void *arg) · 2 unmodified lines | |||
| 551 | 620 | ||
| 552 | if (ret == AVERROR_EOF) | 621 | if (ret == AVERROR_EOF) |
| 553 | av_log(d, AV_LOG_VERBOSE, "EOF while reading input\n"); | 622 | av_log(d, AV_LOG_VERBOSE, "EOF while reading input\n"); |
| 554 | else | 623 | else { |
| 555 | av_log(d, AV_LOG_ERROR, "Error during demuxing: %s\n", | 624 | av_log(d, AV_LOG_ERROR, "Error during demuxing: %s\n", |
| 556 | av_err2str(ret)); | 625 | av_err2str(ret)); |
| 626 | ret = exit_on_error ? ret : 0; | ||
| 627 | } | ||
| 557 | 628 | ||
| 558 | break; | 629 | break; |
| 559 | } | 630 | } |
| @@ -565,8 +636,9 @@ static void *input_thread(void *arg) · 5 unmodified lines | |||
| 565 | 636 | ||
| 566 | /* the following test is needed in case new streams appear | 637 | /* the following test is needed in case new streams appear |
| 567 | dynamically in stream : we ignore them */ | 638 | dynamically in stream : we ignore them */ |
| 568 | if (pkt->stream_index >= f->nb_streams || | 639 | ds = pkt->stream_index < f->nb_streams ? |
| 569 | f->streams[pkt->stream_index]->discard) { | 640 | ds_from_ist(f->streams[pkt->stream_index]) : NULL; |
| 641 | if (!ds || ds->ist.discard || ds->finished) { | ||
| 570 | report_new_stream(d, pkt); | 642 | report_new_stream(d, pkt); |
| 571 | av_packet_unref(pkt); | 643 | av_packet_unref(pkt); |
| 572 | continue; | 644 | continue; |
| @@ -583,122 +655,26 @@ static void *input_thread(void *arg) · 10 unmodified lines | |||
| 583 | } | 655 | } |
| 584 | } | 656 | } |
| 585 | 657 | ||
| 586 | ret = input_packet_process(d, pkt); | 658 | ret = input_packet_process(d, pkt, &send_flags); |
| 587 | if (ret < 0) | 659 | if (ret < 0) |
| 588 | break; | 660 | break; |
| 589 | 661 | ||
| 590 | if (f->readrate) | 662 | if (f->readrate) |
| 591 | readrate_sleep(d); | 663 | readrate_sleep(d); |
| 592 | 664 | ||
| 593 | ret = tq_send(d->thread_queue, 0, pkt); | 665 | ret = demux_send(d, ds, pkt, send_flags); |
| 594 | if (ret < 0) { | 666 | 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)); | ||
| 599 | break; | 667 | break; |
| 600 | } | ||
| 601 | } | 668 | } |
| 602 | 669 | ||
| 603 | finish: | 670 | // EOF/EXIT is normal termination |
| 604 | av_assert0(ret < 0); | 671 | if (ret == AVERROR_EOF || ret == AVERROR_EXIT) |
| 605 | tq_send_finish(d->thread_queue, 0); | 672 | ret = 0; |
| 606 | 673 | ||
| 674 | finish: | ||
| 607 | av_packet_free(&pkt); | 675 | av_packet_free(&pkt); |
| 608 | 676 | ||
| 609 | av_log(d, AV_LOG_VERBOSE, "Terminating demuxer thread\n"); | 677 | return (void*)(intptr_t)ret; |
| 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; | ||
| 702 | } | 678 | } |
| 703 | 679 | ||
| 704 | static void demux_final_stats(Demuxer *d) | 680 | static void demux_final_stats(Demuxer *d) |
| @@ -769,8 +745,6 @@ void ifile_close(InputFile **pf) · 64 unmodified lines | |||
| 769 | if (!f) | 745 | if (!f) |
| 770 | return; | 746 | return; |
| 771 | 747 | ||
| 772 | thread_stop(d); | ||
| 773 | |||
| 774 | if (d->read_started) | 748 | if (d->read_started) |
| 775 | demux_final_stats(d); | 749 | demux_final_stats(d); |
| 776 | 750 | ||
| @@ -780,6 +754,8 @@ void ifile_close(InputFile **pf) · 3 unmodified lines | |||
| 780 | 754 | ||
| 781 | avformat_close_input(&f->ctx); | 755 | avformat_close_input(&f->ctx); |
| 782 | 756 | ||
| 757 | av_packet_free(&d->pkt_heartbeat); | ||
| 758 | |||
| 783 | av_freep(pf); | 759 | av_freep(pf); |
| 784 | } | 760 | } |
| 785 | 761 | ||
| @@ -802,7 +778,11 @@ static int ist_use(InputStream *ist, int decoding_needed) · 16 unmodified lines | |||
| 802 | ds->sch_idx_stream = ret; | 778 | ds->sch_idx_stream = ret; |
| 803 | } | 779 | } |
| 804 | 780 | ||
| 805 | ist->discard = 0; | 781 | if (ist->discard) { |
| 782 | ist->discard = 0; | ||
| 783 | d->nb_streams_used++; | ||
| 784 | } | ||
| 785 | |||
| 806 | ist->st->discard = ist->user_set_discard; | 786 | ist->st->discard = ist->user_set_discard; |
| 807 | ist->decoding_needed |= decoding_needed; | 787 | ist->decoding_needed |= decoding_needed; |
| 808 | ds->streamcopy_needed |= !decoding_needed; | 788 | ds->streamcopy_needed |= !decoding_needed; |
| @@ -823,6 +803,8 @@ static int ist_use(InputStream *ist, int decoding_needed) · 14 unmodified lines | |||
| 823 | ret = dec_open(ist, d->sch, ds->sch_idx_dec); | 803 | ret = dec_open(ist, d->sch, ds->sch_idx_dec); |
| 824 | if (ret < 0) | 804 | if (ret < 0) |
| 825 | return ret; | 805 | return ret; |
| 806 | |||
| 807 | d->have_audio_dec |= is_audio; | ||
| 826 | } | 808 | } |
| 827 | 809 | ||
| 828 | return 0; | 810 | return 0; |
| @@ -848,6 +830,7 @@ int ist_output_add(InputStream *ist, OutputStream *ost) · 19 unmodified lines | |||
| 848 | 830 | ||
| 849 | int ist_filter_add(InputStream *ist, InputFilter *ifilter, int is_simple) | 831 | int ist_filter_add(InputStream *ist, InputFilter *ifilter, int is_simple) |
| 850 | { | 832 | { |
| 833 | Demuxer *d = demuxer_from_ifile(input_files[ist->file_index]); | ||
| 851 | DemuxStream *ds = ds_from_ist(ist); | 834 | DemuxStream *ds = ds_from_ist(ist); |
| 852 | int ret; | 835 | int ret; |
| 853 | 836 | ||
| @@ -866,6 +849,15 @@ int ist_filter_add(InputStream *ist, InputFilter *ifilter, int is_simple) · 12 unmodified lines | |||
| 866 | if (ret < 0) | 849 | if (ret < 0) |
| 867 | return ret; | 850 | return ret; |
| 868 | 851 | ||
| 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 | |||
| 869 | return ds->sch_idx_dec; | 861 | return ds->sch_idx_dec; |
| 870 | } | 862 | } |
| 871 | 863 | ||
| @@ -1607,8 +1599,6 @@ int ifile_open(const OptionsContext *o, const char *filename, Scheduler *sch) · 735 unmodified lines | |||
| 1607 | "since neither -readrate nor -re were given\n"); | 1599 | "since neither -readrate nor -re were given\n"); |
| 1608 | } | 1600 | } |
| 1609 | 1601 | ||
| 1610 | d->thread_queue_size = o->thread_queue_size; | ||
| 1611 | |||
| 1612 | /* Add all the streams from the given input file to the demuxer */ | 1602 | /* Add all the streams from the given input file to the demuxer */ |
| 1613 | for (int i = 0; i < ic->nb_streams; i++) { | 1603 | for (int i = 0; i < ic->nb_streams; i++) { |
| 1614 | ret = ist_add(o, d, ic->streams[i]); | 1604 | ret = ist_add(o, d, ic->streams[i]); |
| @@ -41,12 +41,6 @@ · 40 unmodified lines | |||
| 41 | #include "libavformat/avformat.h" | 41 | #include "libavformat/avformat.h" |
| 42 | 42 | ||
| 43 | struct Encoder { | 43 | struct Encoder { |
| 44 | AVFrame *sq_frame; | ||
| 45 | |||
| 46 | // packet for receiving encoded output | ||
| 47 | AVPacket *pkt; | ||
| 48 | AVFrame *sub_frame; | ||
| 49 | |||
| 50 | // combined size of all the packets received from the encoder | 44 | // combined size of all the packets received from the encoder |
| 51 | uint64_t data_size; | 45 | uint64_t data_size; |
| 52 | 46 | ||
| @@ -54,25 +48,9 @@ struct Encoder { · 1 unmodified line | |||
| 54 | uint64_t packets_encoded; | 48 | uint64_t packets_encoded; |
| 55 | 49 | ||
| 56 | int opened; | 50 | int opened; |
| 57 | int finished; | ||
| 58 | 51 | ||
| 59 | Scheduler *sch; | 52 | Scheduler *sch; |
| 60 | unsigned sch_idx; | 53 | 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; | ||
| 76 | }; | 54 | }; |
| 77 | 55 | ||
| 78 | // data that is local to the decoder thread and not visible outside of it | 56 | // data that is local to the decoder thread and not visible outside of it |
| @@ -81,24 +59,6 @@ typedef struct EncoderThread { · 2 unmodified lines | |||
| 81 | AVPacket *pkt; | 59 | AVPacket *pkt; |
| 82 | } EncoderThread; | 60 | } EncoderThread; |
| 83 | 61 | ||
| 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 | |||
| 102 | void enc_free(Encoder **penc) | 62 | void enc_free(Encoder **penc) |
| 103 | { | 63 | { |
| 104 | Encoder *enc = *penc; | 64 | Encoder *enc = *penc; |
| @@ -106,13 +66,6 @@ void enc_free(Encoder **penc) · 1 unmodified line | |||
| 106 | if (!enc) | 66 | if (!enc) |
| 107 | return; | 67 | return; |
| 108 | 68 | ||
| 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 | |||
| 116 | av_freep(penc); | 69 | av_freep(penc); |
| 117 | } | 70 | } |
| 118 | 71 | ||
| @@ -127,25 +80,12 @@ int enc_alloc(Encoder **penc, const AVCodec *codec, · 8 unmodified lines | |||
| 127 | if (!enc) | 80 | if (!enc) |
| 128 | return AVERROR(ENOMEM); | 81 | return AVERROR(ENOMEM); |
| 129 | 82 | ||
| 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 | |||
| 140 | enc->sch = sch; | 83 | enc->sch = sch; |
| 141 | enc->sch_idx = sch_idx; | 84 | enc->sch_idx = sch_idx; |
| 142 | 85 | ||
| 143 | *penc = enc; | 86 | *penc = enc; |
| 144 | 87 | ||
| 145 | return 0; | 88 | return 0; |
| 146 | fail: | ||
| 147 | enc_free(&enc); | ||
| 148 | return AVERROR(ENOMEM); | ||
| 149 | } | 89 | } |
| 150 | 90 | ||
| 151 | static int hw_device_setup_for_encode(OutputStream *ost, AVBufferRef *frames_ref) | 91 | 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 | |||
| 224 | return 0; | 164 | return 0; |
| 225 | } | 165 | } |
| 226 | 166 | ||
| 227 | static int enc_thread_start(OutputStream *ost) | 167 | int enc_open(void *opaque, const AVFrame *frame) |
| 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) | ||
| 272 | { | 168 | { |
| 169 | OutputStream *ost = opaque; | ||
| 273 | InputStream *ist = ost->ist; | 170 | InputStream *ist = ost->ist; |
| 274 | Encoder *e = ost->enc; | 171 | Encoder *e = ost->enc; |
| 275 | AVCodecContext *enc_ctx = ost->enc_ctx; | 172 | AVCodecContext *enc_ctx = ost->enc_ctx; |
| @@ -277,6 +174,7 @@ int enc_open(OutputStream *ost, const AVFrame *frame) · 1 unmodified line | |||
| 277 | const AVCodec *enc = enc_ctx->codec; | 174 | const AVCodec *enc = enc_ctx->codec; |
| 278 | OutputFile *of = output_files[ost->file_index]; | 175 | OutputFile *of = output_files[ost->file_index]; |
| 279 | FrameData *fd; | 176 | FrameData *fd; |
| 177 | int frame_samples = 0; | ||
| 280 | int ret; | 178 | int ret; |
| 281 | 179 | ||
| 282 | if (e->opened) | 180 | if (e->opened) |
| @@ -420,17 +318,8 @@ int enc_open(OutputStream *ost, const AVFrame *frame) · 137 unmodified lines | |||
| 420 | 318 | ||
| 421 | e->opened = 1; | 319 | e->opened = 1; |
| 422 | 320 | ||
| 423 | if (ost->sq_idx_encode >= 0) { | 321 | if (ost->enc_ctx->frame_size) |
| 424 | e->sq_frame = av_frame_alloc(); | 322 | frame_samples = ost->enc_ctx->frame_size; |
| 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 | } | ||
| 434 | 323 | ||
| 435 | ret = check_avoptions(ost->encoder_opts); | 324 | ret = check_avoptions(ost->encoder_opts); |
| 436 | if (ret < 0) | 325 | if (ret < 0) |
| @@ -476,18 +365,11 @@ int enc_open(OutputStream *ost, const AVFrame *frame) · 39 unmodified lines | |||
| 476 | if (ost->st->time_base.num <= 0 || ost->st->time_base.den <= 0) | 365 | if (ost->st->time_base.num <= 0 || ost->st->time_base.den <= 0) |
| 477 | ost->st->time_base = av_add_q(ost->enc_ctx->time_base, (AVRational){0, 1}); | 366 | ost->st->time_base = av_add_q(ost->enc_ctx->time_base, (AVRational){0, 1}); |
| 478 | 367 | ||
| 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 | |||
| 486 | ret = of_stream_init(of, ost); | 368 | ret = of_stream_init(of, ost); |
| 487 | if (ret < 0) | 369 | if (ret < 0) |
| 488 | return ret; | 370 | return ret; |
| 489 | 371 | ||
| 490 | return 0; | 372 | return frame_samples; |
| 491 | } | 373 | } |
| 492 | 374 | ||
| 493 | static int check_recording_time(OutputStream *ost, int64_t ts, AVRational tb) | 375 | 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 | |||
| 514 | av_log(ost, AV_LOG_ERROR, "Subtitle packets must have a pts\n"); | 396 | av_log(ost, AV_LOG_ERROR, "Subtitle packets must have a pts\n"); |
| 515 | return exit_on_error ? AVERROR(EINVAL) : 0; | 397 | return exit_on_error ? AVERROR(EINVAL) : 0; |
| 516 | } | 398 | } |
| 517 | if (ost->finished || | 399 | if ((of->start_time != AV_NOPTS_VALUE && sub->pts < of->start_time)) |
| 518 | (of->start_time != AV_NOPTS_VALUE && sub->pts < of->start_time)) | ||
| 519 | return 0; | 400 | return 0; |
| 520 | 401 | ||
| 521 | enc = ost->enc_ctx; | 402 | enc = ost->enc_ctx; |
| @@ -579,7 +460,7 @@ static int do_subtitle_out(OutputFile *of, OutputStream *ost, const AVSubtitle * · 57 unmodified lines | |||
| 579 | } | 460 | } |
| 580 | pkt->dts = pkt->pts; | 461 | pkt->dts = pkt->pts; |
| 581 | 462 | ||
| 582 | ret = tq_send(e->queue_out, 0, pkt); | 463 | ret = sch_enc_send(e->sch, e->sch_idx, pkt); |
| 583 | if (ret < 0) { | 464 | if (ret < 0) { |
| 584 | av_packet_unref(pkt); | 465 | av_packet_unref(pkt); |
| 585 | return ret; | 466 | return ret; |
| @@ -671,10 +552,13 @@ static int update_video_stats(OutputStream *ost, const AVPacket *pkt, int write_ · 85 unmodified lines | |||
| 671 | int64_t frame_number; | 552 | int64_t frame_number; |
| 672 | double ti1, bitrate, avg_bitrate; | 553 | double ti1, bitrate, avg_bitrate; |
| 673 | double psnr_val = -1; | 554 | double psnr_val = -1; |
| 555 | int quality; | ||
| 674 | 556 | ||
| 675 | ost->quality = sd ? AV_RL32(sd) : -1; | 557 | quality = sd ? AV_RL32(sd) : -1; |
| 676 | pict_type = sd ? sd[4] : AV_PICTURE_TYPE_NONE; | 558 | pict_type = sd ? sd[4] : AV_PICTURE_TYPE_NONE; |
| 677 | 559 | ||
| 560 | atomic_store(&ost->quality, quality); | ||
| 561 | |||
| 678 | if ((enc->flags & AV_CODEC_FLAG_PSNR) && sd && sd[5]) { | 562 | if ((enc->flags & AV_CODEC_FLAG_PSNR) && sd && sd[5]) { |
| 679 | // FIXME the scaling assumes 8bit | 563 | // FIXME the scaling assumes 8bit |
| 680 | double error = AV_RL64(sd + 8) / (enc->width * enc->height * 255.0 * 255.0); | 564 | 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 | |||
| 697 | frame_number = e->packets_encoded; | 581 | frame_number = e->packets_encoded; |
| 698 | if (vstats_version <= 1) { | 582 | if (vstats_version <= 1) { |
| 699 | fprintf(vstats_file, "frame= %5"PRId64" q= %2.1f ", frame_number, | 583 | fprintf(vstats_file, "frame= %5"PRId64" q= %2.1f ", frame_number, |
| 700 | ost->quality / (float)FF_QP2LAMBDA); | 584 | quality / (float)FF_QP2LAMBDA); |
| 701 | } else { | 585 | } else { |
| 702 | fprintf(vstats_file, "out= %2d st= %2d frame= %5"PRId64" q= %2.1f ", ost->file_index, ost->index, frame_number, | 586 | 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); |
| 704 | } | 588 | } |
| 705 | 589 | ||
| 706 | if (psnr_val >= 0) | 590 | if (psnr_val >= 0) |
| @@ -801,18 +685,11 @@ static int encode_frame(OutputFile *of, OutputStream *ost, AVFrame *frame, · 94 unmodified lines | |||
| 801 | av_ts2str(pkt->duration), av_ts2timestr(pkt->duration, &enc->time_base)); | 685 | av_ts2str(pkt->duration), av_ts2timestr(pkt->duration, &enc->time_base)); |
| 802 | } | 686 | } |
| 803 | 687 | ||
| 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 | |||
| 811 | e->data_size += pkt->size; | 688 | e->data_size += pkt->size; |
| 812 | 689 | ||
| 813 | e->packets_encoded++; | 690 | e->packets_encoded++; |
| 814 | 691 | ||
| 815 | ret = tq_send(e->queue_out, 0, pkt); | 692 | ret = sch_enc_send(e->sch, e->sch_idx, pkt); |
| 816 | if (ret < 0) { | 693 | if (ret < 0) { |
| 817 | av_packet_unref(pkt); | 694 | av_packet_unref(pkt); |
| 818 | return ret; | 695 | return ret; |
| @@ -822,50 +699,6 @@ static int encode_frame(OutputFile *of, OutputStream *ost, AVFrame *frame, · 3 unmodified lines | |||
| 822 | av_assert0(0); | 699 | av_assert0(0); |
| 823 | } | 700 | } |
| 824 | 701 | ||
| 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 | |||
| 869 | static int do_audio_out(OutputFile *of, OutputStream *ost, | 702 | static int do_audio_out(OutputFile *of, OutputStream *ost, |
| 870 | AVFrame *frame, AVPacket *pkt) | 703 | AVFrame *frame, AVPacket *pkt) |
| 871 | { | 704 | { |
| @@ -881,7 +714,7 @@ static int do_audio_out(OutputFile *of, OutputStream *ost, · 9 unmodified lines | |||
| 881 | if (!check_recording_time(ost, frame->pts, frame->time_base)) | 714 | if (!check_recording_time(ost, frame->pts, frame->time_base)) |
| 882 | return AVERROR_EOF; | 715 | return AVERROR_EOF; |
| 883 | 716 | ||
| 884 | return submit_encode_frame(of, ost, frame, pkt); | 717 | return encode_frame(of, ost, frame, pkt); |
| 885 | } | 718 | } |
| 886 | 719 | ||
| 887 | static enum AVPictureType forced_kf_apply(void *logctx, KeyframeForceCtx *kf, | 720 | 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 | |||
| 949 | } | 782 | } |
| 950 | #endif | 783 | #endif |
| 951 | 784 | ||
| 952 | return submit_encode_frame(of, ost, in_picture, pkt); | 785 | return encode_frame(of, ost, in_picture, pkt); |
| 953 | } | 786 | } |
| 954 | 787 | ||
| 955 | static int frame_encode(OutputStream *ost, AVFrame *frame, AVPacket *pkt) | 788 | 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 | |||
| 958 | enum AVMediaType type = ost->type; | 791 | enum AVMediaType type = ost->type; |
| 959 | 792 | ||
| 960 | if (type == AVMEDIA_TYPE_SUBTITLE) { | 793 | if (type == AVMEDIA_TYPE_SUBTITLE) { |
| 794 | const AVSubtitle *subtitle = frame && frame->buf[0] ? | ||
| 795 | (AVSubtitle*)frame->buf[0]->data : NULL; | ||
| 796 | |||
| 961 | // no flushing for subtitles | 797 | // no flushing for subtitles |
| 962 | return frame ? | 798 | return subtitle && subtitle->num_rects ? |
| 963 | do_subtitle_out(of, ost, (AVSubtitle*)frame->buf[0]->data, pkt) : 0; | 799 | do_subtitle_out(of, ost, subtitle, pkt) : 0; |
| 964 | } | 800 | } |
| 965 | 801 | ||
| 966 | if (frame) { | 802 | if (frame) { |
| @@ -968,7 +804,7 @@ static int frame_encode(OutputStream *ost, AVFrame *frame, AVPacket *pkt) · 1 unmodified line | |||
| 968 | do_audio_out(of, ost, frame, pkt); | 804 | do_audio_out(of, ost, frame, pkt); |
| 969 | } | 805 | } |
| 970 | 806 | ||
| 971 | return submit_encode_frame(of, ost, NULL, pkt); | 807 | return encode_frame(of, ost, NULL, pkt); |
| 972 | } | 808 | } |
| 973 | 809 | ||
| 974 | static void enc_thread_set_name(const OutputStream *ost) | 810 | static void enc_thread_set_name(const OutputStream *ost) |
| @@ -1009,25 +845,52 @@ fail: · 34 unmodified lines | |||
| 1009 | void *encoder_thread(void *arg) | 845 | void *encoder_thread(void *arg) |
| 1010 | { | 846 | { |
| 1011 | OutputStream *ost = arg; | 847 | OutputStream *ost = arg; |
| 1012 | OutputFile *of = output_files[ost->file_index]; | ||
| 1013 | Encoder *e = ost->enc; | 848 | Encoder *e = ost->enc; |
| 1014 | EncoderThread et; | 849 | EncoderThread et; |
| 1015 | int ret = 0, input_status = 0; | 850 | int ret = 0, input_status = 0; |
| 851 | int name_set = 0; | ||
| 1016 | 852 | ||
| 1017 | ret = enc_thread_init(&et); | 853 | ret = enc_thread_init(&et); |
| 1018 | if (ret < 0) | 854 | if (ret < 0) |
| 1019 | goto finish; | 855 | goto finish; |
| 1020 | 856 | ||
| 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 | } | ||
| 1022 | 869 | ||
| 1023 | while (!input_status) { | 870 | 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 | } | ||
| 1025 | 887 | ||
| 1026 | input_status = tq_receive(e->queue_in, &dummy, et.frame); | 888 | if (!name_set) { |
| 1027 | if (input_status < 0) | 889 | enc_thread_set_name(ost); |
| 1028 | av_log(ost, AV_LOG_VERBOSE, "Encoder thread received EOF\n"); | 890 | name_set = 1; |
| 891 | } | ||
| 1029 | 892 | ||
| 1030 | ret = frame_encode(ost, input_status >= 0 ? et.frame : NULL, et.pkt); | 893 | ret = frame_encode(ost, et.frame, et.pkt); |
| 1031 | 894 | ||
| 1032 | av_packet_unref(et.pkt); | 895 | av_packet_unref(et.pkt); |
| 1033 | av_frame_unref(et.frame); | 896 | av_frame_unref(et.frame); |
| @@ -1040,15 +903,14 @@ void *encoder_thread(void *arg) · 6 unmodified lines | |||
| 1040 | av_err2str(ret)); | 903 | av_err2str(ret)); |
| 1041 | break; | 904 | break; |
| 1042 | } | 905 | } |
| 906 | } | ||
| 1043 | 907 | ||
| 1044 | // signal to the consumer thread that the frame was encoded | 908 | // flush the encoder |
| 1045 | ret = tq_send(e->queue_out, 0, et.pkt); | 909 | if (ret == 0 || ret == AVERROR_EOF) { |
| 1046 | if (ret < 0) { | 910 | ret = frame_encode(ost, NULL, et.pkt); |
| 1047 | if (ret != AVERROR_EOF) | 911 | if (ret < 0 && ret != AVERROR_EOF) |
| 1048 | av_log(ost, AV_LOG_ERROR, | 912 | av_log(ost, AV_LOG_ERROR, "Error flushing encoder: %s\n", |
| 1049 | "Error communicating with the main thread\n"); | 913 | av_err2str(ret)); |
| 1050 | break; | ||
| 1051 | } | ||
| 1052 | } | 914 | } |
| 1053 | 915 | ||
| 1054 | // EOF is normal thread termination | 916 | // EOF is normal thread termination |
| @@ -1056,118 +918,7 @@ void *encoder_thread(void *arg) · 1 unmodified line | |||
| 1056 | ret = 0; | 918 | ret = 0; |
| 1057 | 919 | ||
| 1058 | finish: | 920 | 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 | |||
| 1065 | enc_thread_uninit(&et); | 921 | enc_thread_uninit(&et); |
| 1066 | 922 | ||
| 1067 | av_log(ost, AV_LOG_VERBOSE, "Terminating encoder thread\n"); | ||
| 1068 | |||
| 1069 | return (void*)(intptr_t)ret; | 923 | return (void*)(intptr_t)ret; |
| 1070 | } | 924 | } |
| 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 | } | ||
| @@ -21,8 +21,6 @@ · 20 unmodified lines | |||
| 21 | #include <stdint.h> | 21 | #include <stdint.h> |
| 22 | 22 | ||
| 23 | #include "ffmpeg.h" | 23 | #include "ffmpeg.h" |
| 24 | #include "ffmpeg_utils.h" | ||
| 25 | #include "thread_queue.h" | ||
| 26 | 24 | ||
| 27 | #include "libavfilter/avfilter.h" | 25 | #include "libavfilter/avfilter.h" |
| 28 | #include "libavfilter/buffersink.h" | 26 | #include "libavfilter/buffersink.h" |
| @@ -53,10 +51,11 @@ typedef struct FilterGraphPriv { · 24 unmodified lines | |||
| 53 | // true when the filtergraph contains only meta filters | 51 | // true when the filtergraph contains only meta filters |
| 54 | // that do not modify the frame data | 52 | // that do not modify the frame data |
| 55 | int is_meta; | 53 | int is_meta; |
| 54 | // source filters are present in the graph | ||
| 55 | int have_sources; | ||
| 56 | int disable_conversions; | 56 | int disable_conversions; |
| 57 | 57 | ||
| 58 | int nb_inputs_bound; | 58 | unsigned nb_outputs_done; |
| 59 | int nb_outputs_bound; | ||
| 60 | 59 | ||
| 61 | const char *graph_desc; | 60 | const char *graph_desc; |
| 62 | 61 | ||
| @@ -67,41 +66,6 @@ typedef struct FilterGraphPriv { · 4 unmodified lines | |||
| 67 | 66 | ||
| 68 | Scheduler *sch; | 67 | Scheduler *sch; |
| 69 | unsigned sch_idx; | 68 | 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; | ||
| 105 | } FilterGraphPriv; | 69 | } FilterGraphPriv; |
| 106 | 70 | ||
| 107 | static FilterGraphPriv *fgp_from_fg(FilterGraph *fg) | 71 | static FilterGraphPriv *fgp_from_fg(FilterGraph *fg) |
| @@ -123,6 +87,9 @@ typedef struct FilterGraphThread { · 15 unmodified lines | |||
| 123 | // The output index is stored in frame opaque. | 87 | // The output index is stored in frame opaque. |
| 124 | AVFifo *frame_queue_out; | 88 | AVFifo *frame_queue_out; |
| 125 | 89 | ||
| 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 | ||
| 126 | int got_frame; | 93 | int got_frame; |
| 127 | 94 | ||
| 128 | // EOF status of each input/output, as received by the thread | 95 | // EOF status of each input/output, as received by the thread |
| @@ -253,9 +220,6 @@ typedef struct OutputFilterPriv { · 124 unmodified lines | |||
| 253 | int64_t ts_offset; | 220 | int64_t ts_offset; |
| 254 | int64_t next_pts; | 221 | int64_t next_pts; |
| 255 | FPSConvContext fps; | 222 | FPSConvContext fps; |
| 256 | |||
| 257 | // set to 1 after at least one frame passed through this output | ||
| 258 | int got_frame; | ||
| 259 | } OutputFilterPriv; | 223 | } OutputFilterPriv; |
| 260 | 224 | ||
| 261 | static OutputFilterPriv *ofp_from_ofilter(OutputFilter *ofilter) | 225 | static OutputFilterPriv *ofp_from_ofilter(OutputFilter *ofilter) |
| @@ -653,57 +617,6 @@ static int ifilter_has_all_input_formats(FilterGraph *fg) · 391 unmodified lines | |||
| 653 | 617 | ||
| 654 | static void *filter_thread(void *arg); | 618 | static void *filter_thread(void *arg); |
| 655 | 619 | ||
| 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 | |||
| 707 | static char *describe_filter_link(FilterGraph *fg, AVFilterInOut *inout, int in) | 620 | static char *describe_filter_link(FilterGraph *fg, AVFilterInOut *inout, int in) |
| 708 | { | 621 | { |
| 709 | AVFilterContext *ctx = inout->filter_ctx; | 622 | AVFilterContext *ctx = inout->filter_ctx; |
| @@ -729,7 +642,6 @@ static OutputFilter *ofilter_alloc(FilterGraph *fg) · 19 unmodified lines | |||
| 729 | ofilter->graph = fg; | 642 | ofilter->graph = fg; |
| 730 | ofp->format = -1; | 643 | ofp->format = -1; |
| 731 | ofp->index = fg->nb_outputs - 1; | 644 | ofp->index = fg->nb_outputs - 1; |
| 732 | ofilter->last_pts = AV_NOPTS_VALUE; | ||
| 733 | 645 | ||
| 734 | return ofilter; | 646 | return ofilter; |
| 735 | } | 647 | } |
| @@ -760,10 +672,7 @@ static int ifilter_bind_ist(InputFilter *ifilter, InputStream *ist) · 24 unmodified lines | |||
| 760 | return AVERROR(ENOMEM); | 672 | return AVERROR(ENOMEM); |
| 761 | } | 673 | } |
| 762 | 674 | ||
| 763 | fgp->nb_inputs_bound++; | 675 | return 0; |
| 764 | av_assert0(fgp->nb_inputs_bound <= ifilter->graph->nb_inputs); | ||
| 765 | |||
| 766 | return fg_thread_try_start(fgp); | ||
| 767 | } | 676 | } |
| 768 | 677 | ||
| 769 | static int set_channel_layout(OutputFilterPriv *f, OutputStream *ost) | 678 | static int set_channel_layout(OutputFilterPriv *f, OutputStream *ost) |
| @@ -902,10 +811,7 @@ int ofilter_bind_ost(OutputFilter *ofilter, OutputStream *ost, · 132 unmodified lines | |||
| 902 | if (ret < 0) | 811 | if (ret < 0) |
| 903 | return ret; | 812 | return ret; |
| 904 | 813 | ||
| 905 | fgp->nb_outputs_bound++; | 814 | return 0; |
| 906 | av_assert0(fgp->nb_outputs_bound <= fg->nb_outputs); | ||
| 907 | |||
| 908 | return fg_thread_try_start(fgp); | ||
| 909 | } | 815 | } |
| 910 | 816 | ||
| 911 | static InputFilter *ifilter_alloc(FilterGraph *fg) | 817 | static InputFilter *ifilter_alloc(FilterGraph *fg) |
| @@ -935,34 +841,6 @@ static InputFilter *ifilter_alloc(FilterGraph *fg) · 23 unmodified lines | |||
| 935 | return ifilter; | 841 | return ifilter; |
| 936 | } | 842 | } |
| 937 | 843 | ||
| 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 | |||
| 966 | void fg_free(FilterGraph **pfg) | 844 | void fg_free(FilterGraph **pfg) |
| 967 | { | 845 | { |
| 968 | FilterGraph *fg = *pfg; | 846 | FilterGraph *fg = *pfg; |
| @@ -972,8 +850,6 @@ void fg_free(FilterGraph **pfg) · 3 unmodified lines | |||
| 972 | return; | 850 | return; |
| 973 | fgp = fgp_from_fg(fg); | 851 | fgp = fgp_from_fg(fg); |
| 974 | 852 | ||
| 975 | fg_thread_stop(fgp); | ||
| 976 | |||
| 977 | avfilter_graph_free(&fg->graph); | 853 | avfilter_graph_free(&fg->graph); |
| 978 | for (int j = 0; j < fg->nb_inputs; j++) { | 854 | for (int j = 0; j < fg->nb_inputs; j++) { |
| 979 | InputFilter *ifilter = fg->inputs[j]; | 855 | InputFilter *ifilter = fg->inputs[j]; |
| @@ -1072,6 +948,15 @@ int fg_create(FilterGraph **pfg, char *graph_desc, Scheduler *sch) · 92 unmodified lines | |||
| 1072 | if (ret < 0) | 948 | if (ret < 0) |
| 1073 | goto fail; | 949 | goto fail; |
| 1074 | 950 | ||
| 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 | |||
| 1075 | for (AVFilterInOut *cur = inputs; cur; cur = cur->next) { | 960 | for (AVFilterInOut *cur = inputs; cur; cur = cur->next) { |
| 1076 | InputFilter *const ifilter = ifilter_alloc(fg); | 961 | InputFilter *const ifilter = ifilter_alloc(fg); |
| 1077 | InputFilterPriv *ifp; | 962 | InputFilterPriv *ifp; |
| @@ -1800,6 +1685,7 @@ static int configure_filtergraph(FilterGraph *fg, const FilterGraphThread *fgt) · 722 unmodified lines | |||
| 1800 | AVBufferRef *hw_device; | 1685 | AVBufferRef *hw_device; |
| 1801 | AVFilterInOut *inputs, *outputs, *cur; | 1686 | AVFilterInOut *inputs, *outputs, *cur; |
| 1802 | int ret, i, simple = filtergraph_is_simple(fg); | 1687 | int ret, i, simple = filtergraph_is_simple(fg); |
| 1688 | int have_input_eof = 0; | ||
| 1803 | const char *graph_desc = fgp->graph_desc; | 1689 | const char *graph_desc = fgp->graph_desc; |
| 1804 | 1690 | ||
| 1805 | cleanup_filtergraph(fg); | 1691 | cleanup_filtergraph(fg); |
| @@ -1922,11 +1808,18 @@ static int configure_filtergraph(FilterGraph *fg, const FilterGraphThread *fgt) · 116 unmodified lines | |||
| 1922 | ret = av_buffersrc_add_frame(ifp->filter, NULL); | 1808 | ret = av_buffersrc_add_frame(ifp->filter, NULL); |
| 1923 | if (ret < 0) | 1809 | if (ret < 0) |
| 1924 | goto fail; | 1810 | goto fail; |
| 1811 | have_input_eof = 1; | ||
| 1925 | } | 1812 | } |
| 1926 | } | 1813 | } |
| 1927 | 1814 | ||
| 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 | } | ||
| 1929 | 1821 | ||
| 1822 | return 0; | ||
| 1930 | fail: | 1823 | fail: |
| 1931 | cleanup_filtergraph(fg); | 1824 | cleanup_filtergraph(fg); |
| 1932 | return ret; | 1825 | return ret; |
| @@ -2182,7 +2075,7 @@ static void video_sync_process(OutputFilterPriv *ofp, AVFrame *frame, · 249 unmodified lines | |||
| 2182 | fps->frames_prev_hist[2]); | 2075 | fps->frames_prev_hist[2]); |
| 2183 | 2076 | ||
| 2184 | if (!*nb_frames && fps->last_dropped) { | 2077 | if (!*nb_frames && fps->last_dropped) { |
| 2185 | ofilter->nb_frames_drop++; | 2078 | atomic_fetch_add(&ofilter->nb_frames_drop, 1); |
| 2186 | fps->last_dropped++; | 2079 | fps->last_dropped++; |
| 2187 | } | 2080 | } |
| 2188 | 2081 | ||
| @@ -2260,21 +2153,23 @@ finish: · 71 unmodified lines | |||
| 2260 | fps->frames_prev_hist[0] = *nb_frames_prev; | 2153 | fps->frames_prev_hist[0] = *nb_frames_prev; |
| 2261 | 2154 | ||
| 2262 | if (*nb_frames_prev == 0 && fps->last_dropped) { | 2155 | if (*nb_frames_prev == 0 && fps->last_dropped) { |
| 2263 | ofilter->nb_frames_drop++; | 2156 | atomic_fetch_add(&ofilter->nb_frames_drop, 1); |
| 2264 | av_log(ost, AV_LOG_VERBOSE, | 2157 | av_log(ost, AV_LOG_VERBOSE, |
| 2265 | "*** dropping frame %"PRId64" at ts %"PRId64"\n", | 2158 | "*** dropping frame %"PRId64" at ts %"PRId64"\n", |
| 2266 | fps->frame_number, fps->last_frame->pts); | 2159 | fps->frame_number, fps->last_frame->pts); |
| 2267 | } | 2160 | } |
| 2268 | if (*nb_frames > (*nb_frames_prev && fps->last_dropped) + (*nb_frames > *nb_frames_prev)) { | 2161 | if (*nb_frames > (*nb_frames_prev && fps->last_dropped) + (*nb_frames > *nb_frames_prev)) { |
| 2162 | uint64_t nb_frames_dup; | ||
| 2269 | if (*nb_frames > dts_error_threshold * 30) { | 2163 | if (*nb_frames > dts_error_threshold * 30) { |
| 2270 | av_log(ost, AV_LOG_ERROR, "%"PRId64" frame duplication too large, skipping\n", *nb_frames - 1); | 2164 | 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); |
| 2272 | *nb_frames = 0; | 2166 | *nb_frames = 0; |
| 2273 | return; | 2167 | return; |
| 2274 | } | 2168 | } |
| 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)); | ||
| 2276 | av_log(ost, AV_LOG_VERBOSE, "*** %"PRId64" dup!\n", *nb_frames - 1); | 2171 | 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) { |
| 2278 | av_log(ost, AV_LOG_WARNING, "More than %"PRIu64" frames duplicated\n", fps->dup_warning); | 2173 | av_log(ost, AV_LOG_WARNING, "More than %"PRIu64" frames duplicated\n", fps->dup_warning); |
| 2279 | fps->dup_warning *= 10; | 2174 | fps->dup_warning *= 10; |
| 2280 | } | 2175 | } |
| @@ -2284,8 +2179,57 @@ finish: · 3 unmodified lines | |||
| 2284 | fps->dropped_keyframe |= fps->last_dropped && (frame->flags & AV_FRAME_FLAG_KEY); | 2179 | fps->dropped_keyframe |= fps->last_dropped && (frame->flags & AV_FRAME_FLAG_KEY); |
| 2285 | } | 2180 | } |
| 2286 | 2181 | ||
| 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 | |||
| 2287 | static int fg_output_frame(OutputFilterPriv *ofp, FilterGraphThread *fgt, | 2231 | static int fg_output_frame(OutputFilterPriv *ofp, FilterGraphThread *fgt, |
| 2288 | AVFrame *frame, int buffer) | 2232 | AVFrame *frame) |
| 2289 | { | 2233 | { |
| 2290 | FilterGraphPriv *fgp = fgp_from_fg(ofp->ofilter.graph); | 2234 | FilterGraphPriv *fgp = fgp_from_fg(ofp->ofilter.graph); |
| 2291 | AVFrame *frame_prev = ofp->fps.last_frame; | 2235 | AVFrame *frame_prev = ofp->fps.last_frame; |
| @@ -2332,28 +2276,17 @@ static int fg_output_frame(OutputFilterPriv *ofp, FilterGraphThread *fgt, · 40 unmodified lines | |||
| 2332 | frame_out = frame; | 2276 | frame_out = frame; |
| 2333 | } | 2277 | } |
| 2334 | 2278 | ||
| 2335 | if (buffer) { | 2279 | { |
| 2336 | AVFrame *f = av_frame_alloc(); | 2280 | // send the frame to consumers |
| 2337 | 2281 | ret = sch_filter_send(fgp->sch, fgp->sch_idx, ofp->index, frame_out); | |
| 2338 | if (!f) { | 2282 | if (ret < 0) { |
| 2339 | av_frame_unref(frame_out); | 2283 | av_frame_unref(frame_out); |
| 2340 | return AVERROR(ENOMEM); | ||
| 2341 | } | ||
| 2342 | 2284 | ||
| 2343 | av_frame_move_ref(f, frame_out); | 2285 | if (!fgt->eof_out[ofp->index]) { |
| 2344 | f->opaque = (void*)(intptr_t)ofp->index; | 2286 | fgt->eof_out[ofp->index] = 1; |
| 2287 | fgp->nb_outputs_done++; | ||
| 2288 | } | ||
| 2345 | 2289 | ||
| 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; | ||
| 2357 | return ret == AVERROR_EOF ? 0 : ret; | 2290 | return ret == AVERROR_EOF ? 0 : ret; |
| 2358 | } | 2291 | } |
| 2359 | } | 2292 | } |
| @@ -2374,16 +2307,14 @@ static int fg_output_frame(OutputFilterPriv *ofp, FilterGraphThread *fgt, · 14 unmodified lines | |||
| 2374 | av_frame_move_ref(frame_prev, frame); | 2307 | av_frame_move_ref(frame_prev, frame); |
| 2375 | } | 2308 | } |
| 2376 | 2309 | ||
| 2377 | if (!frame) { | 2310 | if (!frame) |
| 2378 | tq_send_finish(fgp->queue_out, ofp->index); | 2311 | return close_output(ofp, fgt); |
| 2379 | fgt->eof_out[ofp->index] = 1; | ||
| 2380 | } | ||
| 2381 | 2312 | ||
| 2382 | return 0; | 2313 | return 0; |
| 2383 | } | 2314 | } |
| 2384 | 2315 | ||
| 2385 | static int fg_output_step(OutputFilterPriv *ofp, FilterGraphThread *fgt, | 2316 | static int fg_output_step(OutputFilterPriv *ofp, FilterGraphThread *fgt, |
| 2386 | AVFrame *frame, int buffer) | 2317 | AVFrame *frame) |
| 2387 | { | 2318 | { |
| 2388 | FilterGraphPriv *fgp = fgp_from_fg(ofp->ofilter.graph); | 2319 | FilterGraphPriv *fgp = fgp_from_fg(ofp->ofilter.graph); |
| 2389 | OutputStream *ost = ofp->ofilter.ost; | 2320 | OutputStream *ost = ofp->ofilter.ost; |
| @@ -2393,8 +2324,8 @@ static int fg_output_step(OutputFilterPriv *ofp, FilterGraphThread *fgt, · 3 unmodified lines | |||
| 2393 | 2324 | ||
| 2394 | ret = av_buffersink_get_frame_flags(filter, frame, | 2325 | ret = av_buffersink_get_frame_flags(filter, frame, |
| 2395 | AV_BUFFERSINK_FLAG_NO_REQUEST); | 2326 | AV_BUFFERSINK_FLAG_NO_REQUEST); |
| 2396 | if (ret == AVERROR_EOF && !buffer && !fgt->eof_out[ofp->index]) { | 2327 | if (ret == AVERROR_EOF && !fgt->eof_out[ofp->index]) { |
| 2397 | ret = fg_output_frame(ofp, fgt, NULL, buffer); | 2328 | ret = fg_output_frame(ofp, fgt, NULL); |
| 2398 | return (ret < 0) ? ret : 1; | 2329 | return (ret < 0) ? ret : 1; |
| 2399 | } else if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) { | 2330 | } else if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) { |
| 2400 | return 1; | 2331 | return 1; |
| @@ -2448,7 +2379,7 @@ static int fg_output_step(OutputFilterPriv *ofp, FilterGraphThread *fgt, · 47 unmodified lines | |||
| 2448 | fd->frame_rate_filter = ofp->fps.framerate; | 2379 | fd->frame_rate_filter = ofp->fps.framerate; |
| 2449 | } | 2380 | } |
| 2450 | 2381 | ||
| 2451 | ret = fg_output_frame(ofp, fgt, frame, buffer); | 2382 | ret = fg_output_frame(ofp, fgt, frame); |
| 2452 | av_frame_unref(frame); | 2383 | av_frame_unref(frame); |
| 2453 | if (ret < 0) | 2384 | if (ret < 0) |
| 2454 | return ret; | 2385 | return ret; |
| @@ -2456,44 +2387,68 @@ static int fg_output_step(OutputFilterPriv *ofp, FilterGraphThread *fgt, · 1 unmodified line | |||
| 2456 | return 0; | 2387 | return 0; |
| 2457 | } | 2388 | } |
| 2458 | 2389 | ||
| 2459 | /* retrieve all frames available at filtergraph outputs and either send them to | 2390 | /* retrieve all frames available at filtergraph outputs |
| 2460 | * the main thread (buffer=0) or buffer them for later (buffer=1) */ | 2391 | * and send them to consumers */ |
| 2461 | static int read_frames(FilterGraph *fg, FilterGraphThread *fgt, | 2392 | static int read_frames(FilterGraph *fg, FilterGraphThread *fgt, |
| 2462 | AVFrame *frame, int buffer) | 2393 | AVFrame *frame) |
| 2463 | { | 2394 | { |
| 2464 | FilterGraphPriv *fgp = fgp_from_fg(fg); | 2395 | FilterGraphPriv *fgp = fgp_from_fg(fg); |
| 2465 | int ret = 0; | 2396 | int did_step = 0; |
| 2466 | 2397 | ||
| 2467 | if (!fg->graph) | 2398 | // graph not configured, just select the input to request |
| 2468 | return 0; | 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 | } | ||
| 2469 | 2407 | ||
| 2470 | // process buffered frames | 2408 | // This state - graph is not configured, but all inputs are either |
| 2471 | if (!buffer) { | 2409 | // initialized or EOF - should be unreachable because sending EOF to a |
| 2472 | AVFrame *f; | 2410 | // filter without even a fallback format should fail |
| 2411 | av_assert0(0); | ||
| 2412 | return AVERROR_BUG; | ||
| 2413 | } | ||
| 2473 | 2414 | ||
| 2474 | while (av_fifo_read(fgt->frame_queue_out, &f, 1) >= 0) { | 2415 | while (fgp->nb_outputs_done < fg->nb_outputs) { |
| 2475 | int out_idx = (intptr_t)f->opaque; | 2416 | int ret; |
| 2476 | f->opaque = NULL; | 2417 | |
| 2477 | ret = tq_send(fgp->queue_out, out_idx, f); | 2418 | ret = avfilter_graph_request_oldest(fg->graph); |
| 2478 | av_frame_free(&f); | 2419 | if (ret == AVERROR(EAGAIN)) { |
| 2479 | if (ret < 0 && ret != AVERROR_EOF) | 2420 | fgt->next_in = choose_input(fg, fgt); |
| 2480 | return ret; | 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; | ||
| 2481 | } | 2430 | } |
| 2482 | } | 2431 | fgt->next_in = fg->nb_inputs; |
| 2483 | 2432 | ||
| 2484 | /* Reap all buffers present in the buffer sinks */ | 2433 | // return after one iteration, so that scheduler can rate-control us |
| 2485 | for (int i = 0; i < fg->nb_outputs; i++) { | 2434 | if (did_step && fgp->have_sources) |
| 2486 | OutputFilterPriv *ofp = ofp_from_ofilter(fg->outputs[i]); | 2435 | return 0; |
| 2487 | int ret = 0; | ||
| 2488 | 2436 | ||
| 2489 | while (!ret) { | 2437 | /* Reap all buffers present in the buffer sinks */ |
| 2490 | ret = fg_output_step(ofp, fgt, frame, buffer); | 2438 | for (int i = 0; i < fg->nb_outputs; i++) { |
| 2491 | if (ret < 0) | 2439 | OutputFilterPriv *ofp = ofp_from_ofilter(fg->outputs[i]); |
| 2492 | return ret; | 2440 | |
| 2441 | ret = 0; | ||
| 2442 | while (!ret) { | ||
| 2443 | ret = fg_output_step(ofp, fgt, frame); | ||
| 2444 | if (ret < 0) | ||
| 2445 | return ret; | ||
| 2446 | } | ||
| 2493 | } | 2447 | } |
| 2494 | } | 2448 | did_step = 1; |
| 2449 | }; | ||
| 2495 | 2450 | ||
| 2496 | return 0; | 2451 | return (fgp->nb_outputs_done == fg->nb_outputs) ? AVERROR_EOF : 0; |
| 2497 | } | 2452 | } |
| 2498 | 2453 | ||
| 2499 | static void sub2video_heartbeat(InputFilter *ifilter, int64_t pts, AVRational tb) | 2454 | 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 | |||
| 2571 | InputFilterPriv *ifp = ifp_from_ifilter(ifilter); | 2526 | InputFilterPriv *ifp = ifp_from_ifilter(ifilter); |
| 2572 | int ret; | 2527 | int ret; |
| 2573 | 2528 | ||
| 2529 | if (fgt->eof_in[ifp->index]) | ||
| 2530 | return 0; | ||
| 2531 | |||
| 2574 | fgt->eof_in[ifp->index] = 1; | 2532 | fgt->eof_in[ifp->index] = 1; |
| 2575 | 2533 | ||
| 2576 | if (ifp->filter) { | 2534 | if (ifp->filter) { |
| @@ -2672,7 +2630,7 @@ static int send_frame(FilterGraph *fg, FilterGraphThread *fgt, · 95 unmodified lines | |||
| 2672 | return ret; | 2630 | return ret; |
| 2673 | } | 2631 | } |
| 2674 | 2632 | ||
| 2675 | ret = fg->graph ? read_frames(fg, fgt, tmp, 1) : 0; | 2633 | ret = fg->graph ? read_frames(fg, fgt, tmp) : 0; |
| 2676 | av_frame_free(&tmp); | 2634 | av_frame_free(&tmp); |
| 2677 | if (ret < 0) | 2635 | if (ret < 0) |
| 2678 | return ret; | 2636 | return ret; |
| @@ -2705,82 +2663,6 @@ static int send_frame(FilterGraph *fg, FilterGraphThread *fgt, · 26 unmodified lines | |||
| 2705 | return 0; | 2663 | return 0; |
| 2706 | } | 2664 | } |
| 2707 | 2665 | ||
| 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 | |||
| 2784 | static void fg_thread_set_name(const FilterGraph *fg) | 2666 | static void fg_thread_set_name(const FilterGraph *fg) |
| 2785 | { | 2667 | { |
| 2786 | char name[16]; | 2668 | char name[16]; |
| @@ -2867,282 +2749,86 @@ static void *filter_thread(void *arg) · 80 unmodified lines | |||
| 2867 | InputFilter *ifilter; | 2749 | InputFilter *ifilter; |
| 2868 | InputFilterPriv *ifp; | 2750 | InputFilterPriv *ifp; |
| 2869 | enum FrameOpaque o; | 2751 | enum FrameOpaque o; |
| 2870 | int input_idx, eof_frame; | 2752 | unsigned input_idx = fgt.next_in; |
| 2871 | 2753 | ||
| 2872 | input_status = tq_receive(fgp->queue_in, &input_idx, fgt.frame); | 2754 | input_status = sch_filter_receive(fgp->sch, fgp->sch_idx, |
| 2873 | if (input_idx < 0 || | 2755 | &input_idx, fgt.frame); |
| 2874 | (input_idx == fg->nb_inputs && input_status < 0)) { | 2756 | if (input_status == AVERROR_EOF) { |
| 2875 | av_log(fg, AV_LOG_VERBOSE, "Filtering thread received EOF\n"); | 2757 | av_log(fg, AV_LOG_VERBOSE, "Filtering thread received EOF\n"); |
| 2876 | break; | 2758 | 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; | ||
| 2877 | } | 2763 | } |
| 2764 | av_assert0(input_status >= 0); | ||
| 2765 | |||
| 2766 | o = (intptr_t)fgt.frame->opaque; | ||
| 2878 | 2767 | ||
| 2879 | o = (intptr_t)fgt.frame->opaque; | 2768 | o = (intptr_t)fgt.frame->opaque; |
| 2880 | 2769 | ||
| 2881 | // message on the control stream | 2770 | // message on the control stream |
| 2882 | if (input_idx == fg->nb_inputs) { | 2771 | if (input_idx == fg->nb_inputs) { |
| 2883 | ret = msg_process(fgp, &fgt, fgt.frame); | 2772 | FilterCommand *fc; |
| 2884 | if (ret < 0) | 2773 | |
| 2885 | goto finish; | 2774 | av_assert0(o == FRAME_OPAQUE_SEND_COMMAND && fgt.frame->buf[0]); |
| 2886 | 2775 | ||
| 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); | ||
| 2887 | continue; | 2780 | continue; |
| 2888 | } | 2781 | } |
| 2889 | 2782 | ||
| 2890 | // we received an input frame or EOF | 2783 | // we received an input frame or EOF |
| 2891 | ifilter = fg->inputs[input_idx]; | 2784 | ifilter = fg->inputs[input_idx]; |
| 2892 | ifp = ifp_from_ifilter(ifilter); | 2785 | ifp = ifp_from_ifilter(ifilter); |
| 2893 | eof_frame = input_status >= 0 && o == FRAME_OPAQUE_EOF; | 2786 | |
| 2894 | if (ifp->type_src == AVMEDIA_TYPE_SUBTITLE) { | 2787 | if (ifp->type_src == AVMEDIA_TYPE_SUBTITLE) { |
| 2895 | int hb_frame = input_status >= 0 && o == FRAME_OPAQUE_SUB_HEARTBEAT; | 2788 | int hb_frame = input_status >= 0 && o == FRAME_OPAQUE_SUB_HEARTBEAT; |
| 2896 | ret = sub2video_frame(ifilter, (fgt.frame->buf[0] || hb_frame) ? fgt.frame : NULL); | 2789 | 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]) { |
| 2898 | ret = send_frame(fg, &fgt, ifilter, fgt.frame); | 2791 | ret = send_frame(fg, &fgt, ifilter, fgt.frame); |
| 2899 | } else { | 2792 | } else { |
| 2900 | int64_t pts = input_status >= 0 ? fgt.frame->pts : AV_NOPTS_VALUE; | 2793 | av_assert1(o == FRAME_OPAQUE_EOF); |
| 2901 | AVRational tb = input_status >= 0 ? fgt.frame->time_base : (AVRational){ 1, 1 }; | 2794 | ret = send_eof(&fgt, ifilter, fgt.frame->pts, fgt.frame->time_base); |
| 2902 | ret = send_eof(&fgt, ifilter, pts, tb); | ||
| 2903 | } | 2795 | } |
| 2904 | av_frame_unref(fgt.frame); | 2796 | av_frame_unref(fgt.frame); |
| 2905 | if (ret < 0) | 2797 | 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"); | ||
| 2922 | goto finish; | 2798 | 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; | ||
| 2977 | 2799 | ||
| 2978 | if (keep_reference) { | 2800 | read_frames: |
| 2979 | ret = av_frame_ref(fgp->frame, frame); | 2801 | // retrieve all newly avalable frames |
| 2980 | if (ret < 0) | 2802 | ret = read_frames(fg, &fgt, fgt.frame); |
| 2981 | return ret; | 2803 | if (ret == AVERROR_EOF) { |
| 2982 | } else | 2804 | av_log(fg, AV_LOG_VERBOSE, "All consumers returned EOF\n"); |
| 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 | } | ||
| 3104 | break; | 2805 | 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; | ||
| 3105 | } | 2810 | } |
| 2811 | } | ||
| 3106 | 2812 | ||
| 3107 | // got a frame from the filtering thread, send it for encoding | 2813 | for (unsigned i = 0; i < fg->nb_outputs; i++) { |
| 3108 | ofilter = graph->outputs[output_idx]; | 2814 | OutputFilterPriv *ofp = ofp_from_ofilter(fg->outputs[i]); |
| 3109 | ost = ofilter->ost; | ||
| 3110 | ofp = ofp_from_ofilter(ofilter); | ||
| 3111 | 2815 | ||
| 3112 | if (ost->finished) { | 2816 | if (fgt.eof_out[i]) |
| 3113 | av_frame_unref(fgp->frame); | ||
| 3114 | tq_receive_finish(fgp->queue_out, output_idx); | ||
| 3115 | continue; | 2817 | 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 | } | ||
| 3123 | 2818 | ||
| 3124 | ret = enc_frame(ost, fgp->frame); | 2819 | ret = fg_output_frame(ofp, &fgt, NULL); |
| 3125 | av_frame_unref(fgp->frame); | ||
| 3126 | if (ret < 0) | 2820 | if (ret < 0) |
| 3127 | goto finish; | 2821 | goto finish; |
| 3128 | |||
| 3129 | ofp->got_frame = 1; | ||
| 3130 | got_frames = 1; | ||
| 3131 | } | 2822 | } |
| 3132 | 2823 | ||
| 3133 | finish: | 2824 | finish: |
| 3134 | if (ret < 0) { | 2825 | // EOF is normal termination |
| 3135 | fgp->eof_in = 1; | 2826 | if (ret == AVERROR_EOF) |
| 3136 | for (int i = 0; i < graph->nb_outputs; i++) | 2827 | ret = 0; |
| 3137 | close_output_stream(graph->outputs[i]->ost); | ||
| 3138 | } | ||
| 3139 | 2828 | ||
| 3140 | return ret; | 2829 | fg_thread_uninit(&fgt); |
| 3141 | } | ||
| 3142 | 2830 | ||
| 3143 | int reap_filters(FilterGraph *fg, int flush) | 2831 | return (void*)(intptr_t)ret; |
| 3144 | { | ||
| 3145 | return fg_transcode_step(fg, NULL); | ||
| 3146 | } | 2832 | } |
| 3147 | 2833 | ||
| 3148 | void fg_send_command(FilterGraph *fg, double time, const char *target, | 2834 | 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 | |||
| 3151 | FilterGraphPriv *fgp = fgp_from_fg(fg); | 2837 | FilterGraphPriv *fgp = fgp_from_fg(fg); |
| 3152 | AVBufferRef *buf; | 2838 | AVBufferRef *buf; |
| 3153 | FilterCommand *fc; | 2839 | FilterCommand *fc; |
| 3154 | int output_idx, ret; | ||
| 3155 | |||
| 3156 | if (!fgp->queue_in) | ||
| 3157 | return; | ||
| 3158 | 2840 | ||
| 3159 | fc = av_mallocz(sizeof(*fc)); | 2841 | fc = av_mallocz(sizeof(*fc)); |
| 3160 | if (!fc) | 2842 | if (!fc) |
| @@ -3180,13 +2862,5 @@ void fg_send_command(FilterGraph *fg, double time, const char *target, · 19 unmodified lines | |||
| 3180 | fgp->frame->buf[0] = buf; | 2862 | fgp->frame->buf[0] = buf; |
| 3181 | fgp->frame->opaque = (void*)(intptr_t)FRAME_OPAQUE_SEND_COMMAND; | 2863 | fgp->frame->opaque = (void*)(intptr_t)FRAME_OPAQUE_SEND_COMMAND; |
| 3182 | 2864 | ||
| 3183 | ret = tq_send(fgp->queue_in, fg->nb_inputs, fgp->frame); | 2865 | sch_filter_command(fgp->sch, fgp->sch_idx, 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); | ||
| 3192 | } | 2866 | } |
| @@ -23,16 +23,13 @@ · 22 unmodified lines | |||
| 23 | #include "ffmpeg.h" | 23 | #include "ffmpeg.h" |
| 24 | #include "ffmpeg_mux.h" | 24 | #include "ffmpeg_mux.h" |
| 25 | #include "ffmpeg_utils.h" | 25 | #include "ffmpeg_utils.h" |
| 26 | #include "objpool.h" | ||
| 27 | #include "sync_queue.h" | 26 | #include "sync_queue.h" |
| 28 | #include "thread_queue.h" | ||
| 29 | 27 | ||
| 30 | #include "libavutil/fifo.h" | 28 | #include "libavutil/fifo.h" |
| 31 | #include "libavutil/intreadwrite.h" | 29 | #include "libavutil/intreadwrite.h" |
| 32 | #include "libavutil/log.h" | 30 | #include "libavutil/log.h" |
| 33 | #include "libavutil/mem.h" | 31 | #include "libavutil/mem.h" |
| 34 | #include "libavutil/timestamp.h" | 32 | #include "libavutil/timestamp.h" |
| 35 | #include "libavutil/thread.h" | ||
| 36 | 33 | ||
| 37 | #include "libavcodec/packet.h" | 34 | #include "libavcodec/packet.h" |
| 38 | 35 | ||
| @@ -41,10 +38,9 @@ · 2 unmodified lines | |||
| 41 | 38 | ||
| 42 | typedef struct MuxThreadContext { | 39 | typedef struct MuxThreadContext { |
| 43 | AVPacket *pkt; | 40 | AVPacket *pkt; |
| 41 | AVPacket *fix_sub_duration_pkt; | ||
| 44 | } MuxThreadContext; | 42 | } MuxThreadContext; |
| 45 | 43 | ||
| 46 | int want_sdp = 1; | ||
| 47 | |||
| 48 | static Muxer *mux_from_of(OutputFile *of) | 44 | static Muxer *mux_from_of(OutputFile *of) |
| 49 | { | 45 | { |
| 50 | return (Muxer*)of; | 46 | return (Muxer*)of; |
| @@ -207,14 +203,41 @@ static int sync_queue_process(Muxer *mux, OutputStream *ost, AVPacket *pkt, int · 156 unmodified lines | |||
| 207 | return 0; | 203 | return 0; |
| 208 | } | 204 | } |
| 209 | 205 | ||
| 206 | static int of_streamcopy(OutputStream *ost, AVPacket *pkt); | ||
| 207 | |||
| 210 | /* apply the output bitstream filters */ | 208 | /* apply the output bitstream filters */ |
| 211 | static int mux_packet_filter(Muxer *mux, OutputStream *ost, | 209 | static int mux_packet_filter(Muxer *mux, MuxThreadContext *mt, |
| 212 | AVPacket *pkt, int *stream_eof) | 210 | OutputStream *ost, AVPacket *pkt, int *stream_eof) |
| 213 | { | 211 | { |
| 214 | MuxStream *ms = ms_from_ost(ost); | 212 | MuxStream *ms = ms_from_ost(ost); |
| 215 | const char *err_msg; | 213 | const char *err_msg; |
| 216 | int ret = 0; | 214 | int ret = 0; |
| 217 | 215 | ||
| 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 | |||
| 218 | if (ms->bsf_ctx) { | 241 | if (ms->bsf_ctx) { |
| 219 | int bsf_eof = 0; | 242 | int bsf_eof = 0; |
| 220 | 243 | ||
| @@ -278,6 +301,7 @@ static void thread_set_name(OutputFile *of) · 57 unmodified lines | |||
| 278 | static void mux_thread_uninit(MuxThreadContext *mt) | 301 | static void mux_thread_uninit(MuxThreadContext *mt) |
| 279 | { | 302 | { |
| 280 | av_packet_free(&mt->pkt); | 303 | av_packet_free(&mt->pkt); |
| 304 | av_packet_free(&mt->fix_sub_duration_pkt); | ||
| 281 | 305 | ||
| 282 | memset(mt, 0, sizeof(*mt)); | 306 | memset(mt, 0, sizeof(*mt)); |
| 283 | } | 307 | } |
| @@ -290,6 +314,10 @@ static int mux_thread_init(MuxThreadContext *mt) · 6 unmodified lines | |||
| 290 | if (!mt->pkt) | 314 | if (!mt->pkt) |
| 291 | goto fail; | 315 | goto fail; |
| 292 | 316 | ||
| 317 | mt->fix_sub_duration_pkt = av_packet_alloc(); | ||
| 318 | if (!mt->fix_sub_duration_pkt) | ||
| 319 | goto fail; | ||
| 320 | |||
| 293 | return 0; | 321 | return 0; |
| 294 | 322 | ||
| 295 | fail: | 323 | fail: |
| @@ -316,19 +344,22 @@ void *muxer_thread(void *arg) · 20 unmodified lines | |||
| 316 | OutputStream *ost; | 344 | OutputStream *ost; |
| 317 | int stream_idx, stream_eof = 0; | 345 | int stream_idx, stream_eof = 0; |
| 318 | 346 | ||
| 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; | ||
| 320 | if (stream_idx < 0) { | 349 | if (stream_idx < 0) { |
| 321 | av_log(mux, AV_LOG_VERBOSE, "All streams finished\n"); | 350 | av_log(mux, AV_LOG_VERBOSE, "All streams finished\n"); |
| 322 | ret = 0; | 351 | ret = 0; |
| 323 | break; | 352 | break; |
| 324 | } | 353 | } |
| 325 | 354 | ||
| 326 | ost = of->streams[stream_idx]; | 355 | ost = of->streams[mux->sch_stream_idx[stream_idx]]; |
| 327 | ret = mux_packet_filter(mux, ost, ret < 0 ? NULL : mt.pkt, &stream_eof); | 356 | mt.pkt->stream_index = ost->index; |
| 357 | |||
| 358 | ret = mux_packet_filter(mux, &mt, ost, ret < 0 ? NULL : mt.pkt, &stream_eof); | ||
| 328 | av_packet_unref(mt.pkt); | 359 | av_packet_unref(mt.pkt); |
| 329 | if (ret == AVERROR_EOF) { | 360 | if (ret == AVERROR_EOF) { |
| 330 | if (stream_eof) { | 361 | if (stream_eof) { |
| 331 | tq_receive_finish(mux->tq, stream_idx); | 362 | sch_mux_receive_finish(mux->sch, of->index, stream_idx); |
| 332 | } else { | 363 | } else { |
| 333 | av_log(mux, AV_LOG_VERBOSE, "Muxer returned EOF\n"); | 364 | av_log(mux, AV_LOG_VERBOSE, "Muxer returned EOF\n"); |
| 334 | ret = 0; | 365 | ret = 0; |
| @@ -343,243 +374,55 @@ void *muxer_thread(void *arg) · 8 unmodified lines | |||
| 343 | finish: | 374 | finish: |
| 344 | mux_thread_uninit(&mt); | 375 | mux_thread_uninit(&mt); |
| 345 | 376 | ||
| 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 | |||
| 351 | return (void*)(intptr_t)ret; | 377 | return (void*)(intptr_t)ret; |
| 352 | } | 378 | } |
| 353 | 379 | ||
| 354 | static int thread_submit_packet(Muxer *mux, OutputStream *ost, AVPacket *pkt) | 380 | static int of_streamcopy(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) | ||
| 456 | { | 381 | { |
| 457 | OutputFile *of = output_files[ost->file_index]; | 382 | OutputFile *of = output_files[ost->file_index]; |
| 458 | MuxStream *ms = ms_from_ost(ost); | 383 | 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; | ||
| 459 | int64_t start_time = (of->start_time == AV_NOPTS_VALUE) ? 0 : of->start_time; | 386 | int64_t start_time = (of->start_time == AV_NOPTS_VALUE) ? 0 : of->start_time; |
| 460 | int64_t ts_offset; | 387 | int64_t ts_offset; |
| 461 | AVPacket *opkt = ms->pkt; | ||
| 462 | int ret; | ||
| 463 | |||
| 464 | av_packet_unref(opkt); | ||
| 465 | 388 | ||
| 466 | if (of->recording_time != INT64_MAX && | 389 | if (of->recording_time != INT64_MAX && |
| 467 | dts >= of->recording_time + start_time) | 390 | dts >= of->recording_time + start_time) |
| 468 | pkt = NULL; | 391 | return AVERROR_EOF; |
| 469 | |||
| 470 | // EOF: flush output bitstream filters. | ||
| 471 | if (!pkt) | ||
| 472 | return of_output_packet(of, ost, NULL); | ||
| 473 | 392 | ||
| 474 | if (!ms->streamcopy_started && !(pkt->flags & AV_PKT_FLAG_KEY) && | 393 | if (!ms->streamcopy_started && !(pkt->flags & AV_PKT_FLAG_KEY) && |
| 475 | !ms->copy_initial_nonkeyframes) | 394 | !ms->copy_initial_nonkeyframes) |
| 476 | return 0; | 395 | return AVERROR(EAGAIN); |
| 477 | 396 | ||
| 478 | if (!ms->streamcopy_started) { | 397 | if (!ms->streamcopy_started) { |
| 479 | if (!ms->copy_prior_start && | 398 | if (!ms->copy_prior_start && |
| 480 | (pkt->pts == AV_NOPTS_VALUE ? | 399 | (pkt->pts == AV_NOPTS_VALUE ? |
| 481 | dts < ms->ts_copy_start : | 400 | dts < ms->ts_copy_start : |
| 482 | pkt->pts < av_rescale_q(ms->ts_copy_start, AV_TIME_BASE_Q, pkt->time_base))) | 401 | pkt->pts < av_rescale_q(ms->ts_copy_start, AV_TIME_BASE_Q, pkt->time_base))) |
| 483 | return 0; | 402 | return AVERROR(EAGAIN); |
| 484 | 403 | ||
| 485 | if (of->start_time != AV_NOPTS_VALUE && dts < of->start_time) | 404 | if (of->start_time != AV_NOPTS_VALUE && dts < of->start_time) |
| 486 | return 0; | 405 | return AVERROR(EAGAIN); |
| 487 | } | 406 | } |
| 488 | 407 | ||
| 489 | ret = av_packet_ref(opkt, pkt); | 408 | ts_offset = av_rescale_q(start_time, AV_TIME_BASE_Q, pkt->time_base); |
| 490 | if (ret < 0) | ||
| 491 | return ret; | ||
| 492 | |||
| 493 | ts_offset = av_rescale_q(start_time, AV_TIME_BASE_Q, opkt->time_base); | ||
| 494 | 409 | ||
| 495 | if (pkt->pts != AV_NOPTS_VALUE) | 410 | if (pkt->pts != AV_NOPTS_VALUE) |
| 496 | opkt->pts -= ts_offset; | 411 | pkt->pts -= ts_offset; |
| 497 | 412 | ||
| 498 | if (pkt->dts == AV_NOPTS_VALUE) { | 413 | 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); |
| 500 | } else if (ost->st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) { | 415 | } else if (ost->st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) { |
| 501 | opkt->pts = opkt->dts - ts_offset; | 416 | pkt->pts = pkt->dts - ts_offset; |
| 502 | } | 417 | } |
| 503 | opkt->dts -= ts_offset; | ||
| 504 | 418 | ||
| 505 | { | 419 | pkt->dts -= ts_offset; |
| 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; | ||
| 518 | 420 | ||
| 519 | ms->streamcopy_started = 1; | 421 | ms->streamcopy_started = 1; |
| 520 | 422 | ||
| 521 | return 0; | 423 | return 0; |
| 522 | } | 424 | } |
| 523 | 425 | ||
| 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 | |||
| 583 | int print_sdp(const char *filename); | 426 | int print_sdp(const char *filename); |
| 584 | 427 | ||
| 585 | int print_sdp(const char *filename) | 428 | int print_sdp(const char *filename) |
| @@ -590,11 +433,6 @@ int print_sdp(const char *filename) · 4 unmodified lines | |||
| 590 | AVIOContext *sdp_pb; | 433 | AVIOContext *sdp_pb; |
| 591 | AVFormatContext **avc; | 434 | AVFormatContext **avc; |
| 592 | 435 | ||
| 593 | for (i = 0; i < nb_output_files; i++) { | ||
| 594 | if (!mux_from_of(output_files[i])->header_written) | ||
| 595 | return 0; | ||
| 596 | } | ||
| 597 | |||
| 598 | avc = av_malloc_array(nb_output_files, sizeof(*avc)); | 436 | avc = av_malloc_array(nb_output_files, sizeof(*avc)); |
| 599 | if (!avc) | 437 | if (!avc) |
| 600 | return AVERROR(ENOMEM); | 438 | return AVERROR(ENOMEM); |
| @@ -629,25 +467,17 @@ int print_sdp(const char *filename) · 28 unmodified lines | |||
| 629 | avio_closep(&sdp_pb); | 467 | avio_closep(&sdp_pb); |
| 630 | } | 468 | } |
| 631 | 469 | ||
| 632 | // SDP successfully written, allow muxer threads to start | ||
| 633 | ret = 1; | ||
| 634 | |||
| 635 | fail: | 470 | fail: |
| 636 | av_freep(&avc); | 471 | av_freep(&avc); |
| 637 | return ret; | 472 | return ret; |
| 638 | } | 473 | } |
| 639 | 474 | ||
| 640 | int mux_check_init(Muxer *mux) | 475 | int mux_check_init(void *arg) |
| 641 | { | 476 | { |
| 477 | Muxer *mux = arg; | ||
| 642 | OutputFile *of = &mux->of; | 478 | OutputFile *of = &mux->of; |
| 643 | AVFormatContext *fc = mux->fc; | 479 | AVFormatContext *fc = mux->fc; |
| 644 | int ret, i; | 480 | int ret; |
| 645 | |||
| 646 | for (i = 0; i < fc->nb_streams; i++) { | ||
| 647 | OutputStream *ost = of->streams[i]; | ||
| 648 | if (!ost->initialized) | ||
| 649 | return 0; | ||
| 650 | } | ||
| 651 | 481 | ||
| 652 | ret = avformat_write_header(fc, &mux->opts); | 482 | ret = avformat_write_header(fc, &mux->opts); |
| 653 | if (ret < 0) { | 483 | if (ret < 0) { |
| @@ -659,27 +489,7 @@ int mux_check_init(Muxer *mux) · 5 unmodified lines | |||
| 659 | mux->header_written = 1; | 489 | mux->header_written = 1; |
| 660 | 490 | ||
| 661 | av_dump_format(fc, of->index, fc->url, 1); | 491 | av_dump_format(fc, of->index, fc->url, 1); |
| 662 | nb_output_dumped++; | 492 | atomic_fetch_add(&nb_output_dumped, 1); |
| 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 | } | ||
| 683 | 493 | ||
| 684 | return 0; | 494 | return 0; |
| 685 | } | 495 | } |
| @@ -736,9 +546,10 @@ int of_stream_init(OutputFile *of, OutputStream *ost) · 50 unmodified lines | |||
| 736 | ost->st->time_base); | 546 | ost->st->time_base); |
| 737 | } | 547 | } |
| 738 | 548 | ||
| 739 | ost->initialized = 1; | 549 | if (ms->sch_idx >= 0) |
| 550 | return sch_mux_stream_ready(mux->sch, of->index, ms->sch_idx); | ||
| 740 | 551 | ||
| 741 | return mux_check_init(mux); | 552 | return 0; |
| 742 | } | 553 | } |
| 743 | 554 | ||
| 744 | static int check_written(OutputFile *of) | 555 | static int check_written(OutputFile *of) |
| @@ -852,15 +663,13 @@ int of_write_trailer(OutputFile *of) · 107 unmodified lines | |||
| 852 | AVFormatContext *fc = mux->fc; | 663 | AVFormatContext *fc = mux->fc; |
| 853 | int ret, mux_result = 0; | 664 | int ret, mux_result = 0; |
| 854 | 665 | ||
| 855 | if (!mux->tq) { | 666 | if (!mux->header_written) { |
| 856 | av_log(mux, AV_LOG_ERROR, | 667 | av_log(mux, AV_LOG_ERROR, |
| 857 | "Nothing was written into output file, because " | 668 | "Nothing was written into output file, because " |
| 858 | "at least one of its streams received no packets.\n"); | 669 | "at least one of its streams received no packets.\n"); |
| 859 | return AVERROR(EINVAL); | 670 | return AVERROR(EINVAL); |
| 860 | } | 671 | } |
| 861 | 672 | ||
| 862 | mux_result = thread_stop(mux); | ||
| 863 | |||
| 864 | ret = av_write_trailer(fc); | 673 | ret = av_write_trailer(fc); |
| 865 | if (ret < 0) { | 674 | if (ret < 0) { |
| 866 | av_log(mux, AV_LOG_ERROR, "Error writing trailer: %s\n", av_err2str(ret)); | 675 | 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 | |||
| 905 | ost->logfile = NULL; | 714 | ost->logfile = NULL; |
| 906 | } | 715 | } |
| 907 | 716 | ||
| 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 | |||
| 915 | avcodec_parameters_free(&ost->par_in); | 717 | avcodec_parameters_free(&ost->par_in); |
| 916 | 718 | ||
| 917 | av_bsf_free(&ms->bsf_ctx); | 719 | av_bsf_free(&ms->bsf_ctx); |
| @@ -976,8 +778,6 @@ void of_free(OutputFile **pof) · 58 unmodified lines | |||
| 976 | return; | 778 | return; |
| 977 | mux = mux_from_of(of); | 779 | mux = mux_from_of(of); |
| 978 | 780 | ||
| 979 | thread_stop(mux); | ||
| 980 | |||
| 981 | sq_free(&of->sq_encode); | 781 | sq_free(&of->sq_encode); |
| 982 | sq_free(&mux->sq_mux); | 782 | sq_free(&mux->sq_mux); |
| 983 | 783 | ||
| @@ -25,7 +25,6 @@ · 24 unmodified lines | |||
| 25 | #include <stdint.h> | 25 | #include <stdint.h> |
| 26 | 26 | ||
| 27 | #include "ffmpeg_sched.h" | 27 | #include "ffmpeg_sched.h" |
| 28 | #include "thread_queue.h" | ||
| 29 | 28 | ||
| 30 | #include "libavformat/avformat.h" | 29 | #include "libavformat/avformat.h" |
| 31 | 30 | ||
| @@ -33,7 +32,6 @@ · 1 unmodified line | |||
| 33 | 32 | ||
| 34 | #include "libavutil/dict.h" | 33 | #include "libavutil/dict.h" |
| 35 | #include "libavutil/fifo.h" | 34 | #include "libavutil/fifo.h" |
| 36 | #include "libavutil/thread.h" | ||
| 37 | 35 | ||
| 38 | typedef struct MuxStream { | 36 | typedef struct MuxStream { |
| 39 | OutputStream ost; | 37 | OutputStream ost; |
| @@ -41,9 +39,6 @@ typedef struct MuxStream { · 1 unmodified line | |||
| 41 | // name used for logging | 39 | // name used for logging |
| 42 | char log_name[32]; | 40 | char log_name[32]; |
| 43 | 41 | ||
| 44 | /* the packets are buffered here until the muxer is ready to be initialized */ | ||
| 45 | AVFifo *muxing_queue; | ||
| 46 | |||
| 47 | AVBSFContext *bsf_ctx; | 42 | AVBSFContext *bsf_ctx; |
| 48 | AVPacket *bsf_pkt; | 43 | AVPacket *bsf_pkt; |
| 49 | 44 | ||
| @@ -57,17 +52,6 @@ typedef struct MuxStream { · 7 unmodified lines | |||
| 57 | 52 | ||
| 58 | int64_t max_frames; | 53 | int64_t max_frames; |
| 59 | 54 | ||
| 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 | |||
| 71 | // timestamp from which the streamcopied streams should start, | 55 | // timestamp from which the streamcopied streams should start, |
| 72 | // in AV_TIME_BASE_Q; | 56 | // in AV_TIME_BASE_Q; |
| 73 | // everything before it should be discarded | 57 | // everything before it should be discarded |
| @@ -106,9 +90,6 @@ typedef struct Muxer { · 32 unmodified lines | |||
| 106 | int *sch_stream_idx; | 90 | int *sch_stream_idx; |
| 107 | int nb_sch_stream_idx; | 91 | int nb_sch_stream_idx; |
| 108 | 92 | ||
| 109 | pthread_t thread; | ||
| 110 | ThreadQueue *tq; | ||
| 111 | |||
| 112 | AVDictionary *opts; | 93 | AVDictionary *opts; |
| 113 | 94 | ||
| 114 | int thread_queue_size; | 95 | int thread_queue_size; |
| @@ -122,10 +103,7 @@ typedef struct Muxer { · 7 unmodified lines | |||
| 122 | AVPacket *sq_pkt; | 103 | AVPacket *sq_pkt; |
| 123 | } Muxer; | 104 | } Muxer; |
| 124 | 105 | ||
| 125 | /* whether we want to print an SDP, set in of_open() */ | 106 | int mux_check_init(void *arg); |
| 126 | extern int want_sdp; | ||
| 127 | |||
| 128 | int mux_check_init(Muxer *mux); | ||
| 129 | 107 | ||
| 130 | static MuxStream *ms_from_ost(OutputStream *ost) | 108 | static MuxStream *ms_from_ost(OutputStream *ost) |
| 131 | { | 109 | { |
| @@ -924,13 +924,6 @@ static int new_stream_audio(Muxer *mux, const OptionsContext *o, · 923 unmodified lines | |||
| 924 | return 0; | 924 | return 0; |
| 925 | } | 925 | } |
| 926 | 926 | ||
| 927 | static int new_stream_attachment(Muxer *mux, const OptionsContext *o, | ||
| 928 | OutputStream *ost) | ||
| 929 | { | ||
| 930 | ost->finished = 1; | ||
| 931 | return 0; | ||
| 932 | } | ||
| 933 | |||
| 934 | static int new_stream_subtitle(Muxer *mux, const OptionsContext *o, | 927 | static int new_stream_subtitle(Muxer *mux, const OptionsContext *o, |
| 935 | OutputStream *ost) | 928 | OutputStream *ost) |
| 936 | { | 929 | { |
| @@ -1168,9 +1161,6 @@ static int ost_add(Muxer *mux, const OptionsContext *o, enum AVMediaType type, · 231 unmodified lines | |||
| 1168 | if (!ost->par_in) | 1161 | if (!ost->par_in) |
| 1169 | return AVERROR(ENOMEM); | 1162 | return AVERROR(ENOMEM); |
| 1170 | 1163 | ||
| 1171 | ms->muxing_queue = av_fifo_alloc2(8, sizeof(AVPacket*), 0); | ||
| 1172 | if (!ms->muxing_queue) | ||
| 1173 | return AVERROR(ENOMEM); | ||
| 1174 | ms->last_mux_dts = AV_NOPTS_VALUE; | 1164 | ms->last_mux_dts = AV_NOPTS_VALUE; |
| 1175 | 1165 | ||
| 1176 | ost->st = st; | 1166 | ost->st = st; |
| @@ -1190,7 +1180,8 @@ static int ost_add(Muxer *mux, const OptionsContext *o, enum AVMediaType type, · 13 unmodified lines | |||
| 1190 | if (!ost->enc_ctx) | 1180 | if (!ost->enc_ctx) |
| 1191 | return AVERROR(ENOMEM); | 1181 | return AVERROR(ENOMEM); |
| 1192 | 1182 | ||
| 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); | ||
| 1194 | if (ret < 0) | 1185 | if (ret < 0) |
| 1195 | return ret; | 1186 | return ret; |
| 1196 | ms->sch_idx_enc = ret; | 1187 | ms->sch_idx_enc = ret; |
| @@ -1414,9 +1405,6 @@ static int ost_add(Muxer *mux, const OptionsContext *o, enum AVMediaType type, · 217 unmodified lines | |||
| 1414 | 1405 | ||
| 1415 | sch_mux_stream_buffering(mux->sch, mux->sch_idx, ms->sch_idx, | 1406 | sch_mux_stream_buffering(mux->sch, mux->sch_idx, ms->sch_idx, |
| 1416 | max_muxing_queue_size, muxing_queue_data_threshold); | 1407 | 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; | ||
| 1420 | } | 1408 | } |
| 1421 | 1409 | ||
| 1422 | MATCH_PER_STREAM_OPT(bits_per_raw_sample, i, ost->bits_per_raw_sample, | 1410 | 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 | |||
| 1434 | if (ost->enc_ctx && av_get_exact_bits_per_sample(ost->enc_ctx->codec_id) == 24) | 1422 | if (ost->enc_ctx && av_get_exact_bits_per_sample(ost->enc_ctx->codec_id) == 24) |
| 1435 | av_dict_set(&ost->swr_opts, "output_sample_bits", "24", 0); | 1423 | av_dict_set(&ost->swr_opts, "output_sample_bits", "24", 0); |
| 1436 | 1424 | ||
| 1437 | ost->last_mux_dts = AV_NOPTS_VALUE; | ||
| 1438 | |||
| 1439 | MATCH_PER_STREAM_OPT(copy_initial_nonkeyframes, i, | 1425 | MATCH_PER_STREAM_OPT(copy_initial_nonkeyframes, i, |
| 1440 | ms->copy_initial_nonkeyframes, oc, st); | 1426 | ms->copy_initial_nonkeyframes, oc, st); |
| 1441 | 1427 | ||
| @@ -1443,7 +1429,6 @@ static int ost_add(Muxer *mux, const OptionsContext *o, enum AVMediaType type, · 1 unmodified line | |||
| 1443 | case AVMEDIA_TYPE_VIDEO: ret = new_stream_video (mux, o, ost); break; | 1429 | case AVMEDIA_TYPE_VIDEO: ret = new_stream_video (mux, o, ost); break; |
| 1444 | case AVMEDIA_TYPE_AUDIO: ret = new_stream_audio (mux, o, ost); break; | 1430 | case AVMEDIA_TYPE_AUDIO: ret = new_stream_audio (mux, o, ost); break; |
| 1445 | case AVMEDIA_TYPE_SUBTITLE: ret = new_stream_subtitle (mux, o, ost); break; | 1431 | 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; | ||
| 1447 | } | 1432 | } |
| 1448 | if (ret < 0) | 1433 | if (ret < 0) |
| 1449 | return ret; | 1434 | return ret; |
| @@ -1938,7 +1923,6 @@ static int setup_sync_queues(Muxer *mux, AVFormatContext *oc, int64_t buf_size_u · 488 unmodified lines | |||
| 1938 | MuxStream *ms = ms_from_ost(ost); | 1923 | MuxStream *ms = ms_from_ost(ost); |
| 1939 | enum AVMediaType type = ost->type; | 1924 | enum AVMediaType type = ost->type; |
| 1940 | 1925 | ||
| 1941 | ost->sq_idx_encode = -1; | ||
| 1942 | ost->sq_idx_mux = -1; | 1926 | ost->sq_idx_mux = -1; |
| 1943 | 1927 | ||
| 1944 | nb_interleaved += IS_INTERLEAVED(type); | 1928 | 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 | |||
| 1961 | * - at least one encoded audio/video stream is frame-limited, since | 1945 | * - at least one encoded audio/video stream is frame-limited, since |
| 1962 | * that has similar semantics to 'shortest' | 1946 | * that has similar semantics to 'shortest' |
| 1963 | * - at least one audio encoder requires constant frame sizes | 1947 | * - 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 | ||
| 1964 | */ | 1952 | */ |
| 1965 | if ((of->shortest && nb_av_enc > 1) || limit_frames_av_enc || nb_audio_fs) { | 1953 | 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); | 1954 | int sq_idx, ret; |
| 1967 | if (!of->sq_encode) | 1955 | |
| 1968 | return AVERROR(ENOMEM); | 1956 | sq_idx = sch_add_sq_enc(mux->sch, buf_size_us, mux); |
| 1957 | if (sq_idx < 0) | ||
| 1958 | return sq_idx; | ||
| 1969 | 1959 | ||
| 1970 | for (int i = 0; i < oc->nb_streams; i++) { | 1960 | for (int i = 0; i < oc->nb_streams; i++) { |
| 1971 | OutputStream *ost = of->streams[i]; | 1961 | 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 | |||
| 1975 | if (!IS_AV_ENC(ost, type)) | 1965 | if (!IS_AV_ENC(ost, type)) |
| 1976 | continue; | 1966 | continue; |
| 1977 | 1967 | ||
| 1978 | ost->sq_idx_encode = sq_add_stream(of->sq_encode, | 1968 | ret = sch_sq_add_enc(mux->sch, sq_idx, ms->sch_idx_enc, |
| 1979 | of->shortest || ms->max_frames < INT64_MAX); | 1969 | of->shortest || ms->max_frames < INT64_MAX, |
| 1980 | if (ost->sq_idx_encode < 0) | 1970 | ms->max_frames); |
| 1981 | return ost->sq_idx_encode; | 1971 | if (ret < 0) |
| 1982 | 1972 | return ret; | |
| 1983 | if (ms->max_frames != INT64_MAX) | ||
| 1984 | sq_limit_frames(of->sq_encode, ost->sq_idx_encode, ms->max_frames); | ||
| 1985 | } | 1973 | } |
| 1986 | } | 1974 | } |
| 1987 | 1975 | ||
| @@ -2652,23 +2640,6 @@ static int validate_enc_avopt(Muxer *mux, const AVDictionary *codec_avopt) · 664 unmodified lines | |||
| 2652 | return 0; | 2640 | return 0; |
| 2653 | } | 2641 | } |
| 2654 | 2642 | ||
| 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 | |||
| 2672 | static const char *output_file_item_name(void *obj) | 2643 | static const char *output_file_item_name(void *obj) |
| 2673 | { | 2644 | { |
| 2674 | const Muxer *mux = obj; | 2645 | const Muxer *mux = obj; |
| @@ -2751,8 +2722,6 @@ int of_open(const OptionsContext *o, const char *filename, Scheduler *sch) · 76 unmodified lines | |||
| 2751 | av_strlcat(mux->log_name, "/", sizeof(mux->log_name)); | 2722 | av_strlcat(mux->log_name, "/", sizeof(mux->log_name)); |
| 2752 | av_strlcat(mux->log_name, oc->oformat->name, sizeof(mux->log_name)); | 2723 | av_strlcat(mux->log_name, oc->oformat->name, sizeof(mux->log_name)); |
| 2753 | 2724 | ||
| 2754 | if (strcmp(oc->oformat->name, "rtp")) | ||
| 2755 | want_sdp = 0; | ||
| 2756 | 2725 | ||
| 2757 | of->format = oc->oformat; | 2726 | of->format = oc->oformat; |
| 2758 | if (recording_time != INT64_MAX) | 2727 | if (recording_time != INT64_MAX) |
| @@ -2768,7 +2737,7 @@ int of_open(const OptionsContext *o, const char *filename, Scheduler *sch) · 9 unmodified lines | |||
| 2768 | AVFMT_FLAG_BITEXACT); | 2737 | AVFMT_FLAG_BITEXACT); |
| 2769 | } | 2738 | } |
| 2770 | 2739 | ||
| 2771 | err = sch_add_mux(sch, muxer_thread, NULL, mux, | 2740 | err = sch_add_mux(sch, muxer_thread, mux_check_init, mux, |
| 2772 | !strcmp(oc->oformat->name, "rtp")); | 2741 | !strcmp(oc->oformat->name, "rtp")); |
| 2773 | if (err < 0) | 2742 | if (err < 0) |
| 2774 | return err; | 2743 | return err; |
| @@ -2854,26 +2823,15 @@ int of_open(const OptionsContext *o, const char *filename, Scheduler *sch) · 79 unmodified lines | |||
| 2854 | 2823 | ||
| 2855 | of->url = filename; | 2824 | of->url = filename; |
| 2856 | 2825 | ||
| 2857 | /* initialize stream copy and subtitle/data streams. | 2826 | /* initialize streamcopy streams. */ |
| 2858 | * Encoded AVFrame based streams will get initialized when the first AVFrame | ||
| 2859 | * is received in do_video_out | ||
| 2860 | */ | ||
| 2861 | for (int i = 0; i < of->nb_streams; i++) { | 2827 | for (int i = 0; i < of->nb_streams; i++) { |
| 2862 | OutputStream *ost = of->streams[i]; | 2828 | OutputStream *ost = of->streams[i]; |
| 2863 | 2829 | ||
| 2864 | if (ost->filter) | 2830 | if (!ost->enc) { |
| 2865 | continue; | 2831 | err = of_stream_init(of, ost); |
| 2866 | 2832 | if (err < 0) | |
| 2867 | err = init_output_stream_nofilter(ost); | 2833 | return err; |
| 2868 | if (err < 0) | 2834 | } |
| 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; | ||
| 2877 | } | 2835 | } |
| 2878 | 2836 | ||
| 2879 | return 0; | 2837 | return 0; |
| @@ -64,7 +64,6 @@ const char *const opt_name_top_field_first[] = {"top", NULL}; · 63 unmodified lines | |||
| 64 | HWDevice *filter_hw_device; | 64 | HWDevice *filter_hw_device; |
| 65 | 65 | ||
| 66 | char *vstats_filename; | 66 | char *vstats_filename; |
| 67 | char *sdp_filename; | ||
| 68 | 67 | ||
| 69 | float audio_drift_threshold = 0.1; | 68 | float audio_drift_threshold = 0.1; |
| 70 | float dts_delta_threshold = 10; | 69 | float dts_delta_threshold = 10; |
| @@ -580,9 +579,8 @@ fail: · 509 unmodified lines | |||
| 580 | 579 | ||
| 581 | static int opt_sdp_file(void *optctx, const char *opt, const char *arg) | 580 | static int opt_sdp_file(void *optctx, const char *opt, const char *arg) |
| 582 | { | 581 | { |
| 583 | av_free(sdp_filename); | 582 | Scheduler *sch = optctx; |
| 584 | sdp_filename = av_strdup(arg); | 583 | return sch_sdp_filename(sch, arg); |
| 585 | return 0; | ||
| 586 | } | 584 | } |
| 587 | 585 | ||
| 588 | #if CONFIG_VAAPI | 586 | #if CONFIG_VAAPI |
| @@ -1,48 +1,40 @@ | |||
| 1 | 1 | 1 | 1 |
| 2 | 00:00:00,968 --> 00:00:01,001 | 2 | 00:00:00,968 --> 00:00:01,168 |
| 3 | <font face="Monospace">{\an7}(</font> | 3 | <font face="Monospace">{\an7}(</font> |
| 4 | 4 | ||
| 5 | 2 | 5 | 2 |
| 6 | 00:00:01,001 --> 00:00:01,168 | ||
| 7 | <font face="Monospace">{\an7}(</font> | ||
| 8 | |||
| 9 | 3 | ||
| 10 | 00:00:01,168 --> 00:00:01,368 | 6 | 00:00:01,168 --> 00:00:01,368 |
| 11 | <font face="Monospace">{\an7}(<i> inaudibl</i></font> | 7 | <font face="Monospace">{\an7}(<i> inaudibl</i></font> |
| 12 | 8 | ||
| 13 | 4 | 9 | 3 |
| 14 | 00:00:01,368 --> 00:00:01,568 | 10 | 00:00:01,368 --> 00:00:01,568 |
| 15 | <font face="Monospace">{\an7}(<i> inaudible radio chat</i></font> | 11 | <font face="Monospace">{\an7}(<i> inaudible radio chat</i></font> |
| 16 | 12 | ||
| 17 | 5 | 13 | 4 |
| 18 | 00:00:01,568 --> 00:00:02,002 | 14 | 00:00:01,568 --> 00:00:02,002 |
| 19 | <font face="Monospace">{\an7}(<i> inaudible radio chatter</i> )</font> | 15 | <font face="Monospace">{\an7}(<i> inaudible radio chatter</i> )</font> |
| 20 | 16 | ||
| 21 | 6 | 17 | 5 |
| 22 | 00:00:02,002 --> 00:00:03,003 | 18 | 00:00:02,002 --> 00:00:03,103 |
| 23 | <font face="Monospace">{\an7}(<i> inaudible radio chatter</i> )</font> | ||
| 24 | |||
| 25 | 7 | ||
| 26 | 00:00:03,003 --> 00:00:03,103 | ||
| 27 | <font face="Monospace">{\an7}(<i> inaudible radio chatter</i> )</font> | 19 | <font face="Monospace">{\an7}(<i> inaudible radio chatter</i> )</font> |
| 28 | 20 | ||
| 29 | 8 | 21 | 6 |
| 30 | 00:00:03,103 --> 00:00:03,303 | 22 | 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> ) |
| 32 | >></font> | 24 | >></font> |
| 33 | 25 | ||
| 34 | 9 | 26 | 7 |
| 35 | 00:00:03,303 --> 00:00:03,503 | 27 | 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> ) |
| 37 | >> Safety rema</font> | 29 | >> Safety rema</font> |
| 38 | 30 | ||
| 39 | 10 | 31 | 8 |
| 40 | 00:00:03,504 --> 00:00:03,704 | 32 | 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> ) |
| 42 | >> Safety remains our numb</font> | 34 | >> Safety remains our numb</font> |
| 43 | 35 | ||
| 44 | 11 | 36 | 9 |
| 45 | 00:00:03,704 --> 00:00:04,004 | 37 | 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> ) |
| 47 | >> Safety remains our number one</font> | 39 | >> Safety remains our number one</font> |
| 48 | 40 | ||