From: Anton Khirnov <anton@khirnov.net> To: ffmpeg-devel@ffmpeg.org Subject: [FFmpeg-devel] [PATCH 02/13] fftools/ffmpeg: move output_packet() to ffmpeg_mux Date: Thu, 13 Oct 2022 15:48:53 +0200 Message-ID: <20221013134904.10104-2-anton@khirnov.net> (raw) In-Reply-To: <20221013134904.10104-1-anton@khirnov.net> This function is common to both transcoding and streamcopy, so it properly belongs into the muxing code. --- fftools/ffmpeg.c | 78 ++++---------------------------------------- fftools/ffmpeg.h | 13 +++++++- fftools/ffmpeg_mux.c | 54 +++++++++++++++++++++++++++++- 3 files changed, 72 insertions(+), 73 deletions(-) diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c index 754172e568..015ff762c9 100644 --- a/fftools/ffmpeg.c +++ b/fftools/ffmpeg.c @@ -694,70 +694,6 @@ static void close_output_stream(OutputStream *ost) sq_send(of->sq_encode, ost->sq_idx_encode, SQFRAME(NULL)); } -/* - * Send a single packet to the output, applying any bitstream filters - * associated with the output stream. This may result in any number - * of packets actually being written, depending on what bitstream - * filters are applied. The supplied packet is consumed and will be - * blank (as if newly-allocated) when this function returns. - * - * If eof is set, instead indicate EOF to all bitstream filters and - * therefore flush any delayed packets to the output. A blank packet - * must be supplied in this case. - */ -static void output_packet(OutputFile *of, AVPacket *pkt, - OutputStream *ost, int eof) -{ - const char *err_msg; - int ret = 0; - - if (!eof && pkt->dts != AV_NOPTS_VALUE) - ost->last_mux_dts = av_rescale_q(pkt->dts, ost->mux_timebase, AV_TIME_BASE_Q); - - /* apply the output bitstream filters */ - if (ost->bsf_ctx) { - int bsf_eof = 0; - - ret = av_bsf_send_packet(ost->bsf_ctx, eof ? NULL : pkt); - if (ret < 0) { - err_msg = "submitting a packet for bitstream filtering"; - goto fail; - } - - while (!bsf_eof) { - ret = av_bsf_receive_packet(ost->bsf_ctx, pkt); - if (ret == AVERROR(EAGAIN)) - return; - else if (ret == AVERROR_EOF) - bsf_eof = 1; - else if (ret < 0) { - err_msg = "applying bitstream filters to a packet"; - goto fail; - } - - ret = of_submit_packet(of, bsf_eof ? NULL : pkt, ost); - if (ret < 0) - goto mux_fail; - } - } else { - ret = of_submit_packet(of, eof ? NULL : pkt, ost); - if (ret < 0) - goto mux_fail; - } - - return; - -mux_fail: - err_msg = "submitting a packet to the muxer"; - -fail: - av_log(NULL, AV_LOG_ERROR, "Error %s for output stream #%d:%d.\n", - err_msg, ost->file_index, ost->index); - if (exit_on_error) - exit_program(1); - -} - static int check_recording_time(OutputStream *ost, int64_t ts, AVRational tb) { OutputFile *of = output_files[ost->file_index]; @@ -949,7 +885,7 @@ static int encode_frame(OutputFile *of, OutputStream *ost, AVFrame *frame) av_assert0(frame); // should never happen during flushing return 0; } else if (ret == AVERROR_EOF) { - output_packet(of, pkt, ost, 1); + of_output_packet(of, pkt, ost, 1); return ret; } else if (ret < 0) { av_log(NULL, AV_LOG_ERROR, "%s encoding failed\n", type_desc); @@ -985,7 +921,7 @@ static int encode_frame(OutputFile *of, OutputStream *ost, AVFrame *frame) ost->packets_encoded++; - output_packet(of, pkt, ost, 0); + of_output_packet(of, pkt, ost, 0); } av_assert0(0); @@ -1127,7 +1063,7 @@ static void do_subtitle_out(OutputFile *of, pkt->pts += av_rescale_q(sub->end_display_time, (AVRational){ 1, 1000 }, ost->mux_timebase); } pkt->dts = pkt->pts; - output_packet(of, pkt, ost, 0); + of_output_packet(of, pkt, ost, 0); } } @@ -1832,7 +1768,7 @@ static void flush_encoders(void) exit_program(1); } - output_packet(of, ost->pkt, ost, 1); + of_output_packet(of, ost->pkt, ost, 1); } init_output_stream_wrapper(ost, NULL, 1); @@ -1878,7 +1814,7 @@ static void do_streamcopy(InputStream *ist, OutputStream *ost, const AVPacket *p av_packet_unref(opkt); // EOF: flush output bitstream filters. if (!pkt) { - output_packet(of, opkt, ost, 1); + of_output_packet(of, opkt, ost, 1); return; } @@ -1934,7 +1870,7 @@ static void do_streamcopy(InputStream *ist, OutputStream *ost, const AVPacket *p opkt->duration = av_rescale_q(pkt->duration, ist->st->time_base, ost->mux_timebase); - output_packet(of, opkt, ost, 0); + of_output_packet(of, opkt, ost, 0); ost->streamcopy_started = 1; } @@ -3833,7 +3769,7 @@ static int process_input(int file_index) if (ost->source_index == ifile->ist_index + i && (!ost->enc_ctx || ost->enc_ctx->codec_type == AVMEDIA_TYPE_SUBTITLE)) { OutputFile *of = output_files[ost->file_index]; - output_packet(of, ost->pkt, ost, 1); + of_output_packet(of, ost->pkt, ost, 1); } } } diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h index 9c908e7465..632b7c0e86 100644 --- a/fftools/ffmpeg.h +++ b/fftools/ffmpeg.h @@ -725,7 +725,18 @@ int of_check_init(OutputFile *of); int of_write_trailer(OutputFile *of); void of_close(OutputFile **pof); -int of_submit_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost); +/* + * Send a single packet to the output, applying any bitstream filters + * associated with the output stream. This may result in any number + * of packets actually being written, depending on what bitstream + * filters are applied. The supplied packet is consumed and will be + * blank (as if newly-allocated) when this function returns. + * + * If eof is set, instead indicate EOF to all bitstream filters and + * therefore flush any delayed packets to the output. A blank packet + * must be supplied in this case. + */ +void of_output_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, int eof); int64_t of_filesize(OutputFile *of); AVChapter * const * of_get_chapters(OutputFile *of, unsigned int *nb_chapters); diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c index a6877ae5ec..6e9cbb4bab 100644 --- a/fftools/ffmpeg_mux.c +++ b/fftools/ffmpeg_mux.c @@ -319,7 +319,7 @@ static int queue_packet(OutputFile *of, OutputStream *ost, AVPacket *pkt) return 0; } -int of_submit_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost) +static int of_submit_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost) { int ret; @@ -338,6 +338,58 @@ int of_submit_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost) return 0; } +void of_output_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, int eof) +{ + const char *err_msg; + int ret = 0; + + if (!eof && pkt->dts != AV_NOPTS_VALUE) + ost->last_mux_dts = av_rescale_q(pkt->dts, ost->mux_timebase, AV_TIME_BASE_Q); + + /* apply the output bitstream filters */ + if (ost->bsf_ctx) { + int bsf_eof = 0; + + ret = av_bsf_send_packet(ost->bsf_ctx, eof ? NULL : pkt); + if (ret < 0) { + err_msg = "submitting a packet for bitstream filtering"; + goto fail; + } + + while (!bsf_eof) { + ret = av_bsf_receive_packet(ost->bsf_ctx, pkt); + if (ret == AVERROR(EAGAIN)) + return; + else if (ret == AVERROR_EOF) + bsf_eof = 1; + else if (ret < 0) { + err_msg = "applying bitstream filters to a packet"; + goto fail; + } + + ret = of_submit_packet(of, bsf_eof ? NULL : pkt, ost); + if (ret < 0) + goto mux_fail; + } + } else { + ret = of_submit_packet(of, eof ? NULL : pkt, ost); + if (ret < 0) + goto mux_fail; + } + + return; + +mux_fail: + err_msg = "submitting a packet to the muxer"; + +fail: + av_log(NULL, AV_LOG_ERROR, "Error %s for output stream #%d:%d.\n", + err_msg, ost->file_index, ost->index); + if (exit_on_error) + exit_program(1); + +} + static int thread_stop(OutputFile *of) { Muxer *mux = of->mux; -- 2.35.1 _______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org https://ffmpeg.org/mailman/listinfo/ffmpeg-devel To unsubscribe, visit link above, or email ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
next prev parent reply other threads:[~2022-10-13 13:49 UTC|newest] Thread overview: 23+ messages / expand[flat|nested] mbox.gz Atom feed top 2022-10-13 13:48 [FFmpeg-devel] [PATCH 01/13] fftools/ffmpeg_mux: do not unref a NULL packet Anton Khirnov 2022-10-13 13:48 ` Anton Khirnov [this message] 2022-10-13 13:48 ` [FFmpeg-devel] [PATCH 03/13] fftools/ffmpeg_mux: rename submit_packet() to thread_submit_packet() Anton Khirnov 2022-10-13 13:48 ` [FFmpeg-devel] [PATCH 04/13] fftools/ffmpeg_mux: drop the of_ prefix from of_submit_packet() Anton Khirnov 2022-10-13 13:48 ` [FFmpeg-devel] [PATCH 05/13] fftools/ffmpeg: move some stream initialization code to ffmpeg_mux Anton Khirnov 2022-10-13 13:48 ` [FFmpeg-devel] [PATCH 06/13] fftools/ffmpeg_opt: move opening output files into a new file Anton Khirnov 2022-10-13 13:48 ` [FFmpeg-devel] [PATCH 07/13] fftools/ffmpeg_mux: move Muxer and MuxStream to a new header Anton Khirnov 2022-10-13 13:48 ` [FFmpeg-devel] [PATCH 08/13] fftools/ffmpeg_mux: embed OutputFile in a Muxer Anton Khirnov 2022-10-13 13:49 ` [FFmpeg-devel] [PATCH 09/13] fftools/ffmpeg_mux: allocate sq_pkt in setup_sync_queues() Anton Khirnov 2022-10-13 13:49 ` [FFmpeg-devel] [PATCH 10/13] fftools/ffmpeg_mux: inline of_muxer_init() into of_open() Anton Khirnov 2022-10-13 13:49 ` [FFmpeg-devel] [PATCH 11/13] fftools/ffmpeg_mux: inline mux_free() into of_close() Anton Khirnov 2022-10-13 13:49 ` [FFmpeg-devel] [PATCH 12/13] fftools/ffmpeg_mux: move sq_mux from OutputFile to Muxer Anton Khirnov 2022-10-13 13:49 ` [FFmpeg-devel] [PATCH 13/13] fftools/ffmpeg: move init_output_bsfs() to ffmpeg_mux Anton Khirnov 2022-10-14 10:15 ` [FFmpeg-devel] [PATCH 14/22] fftools/ffmpeg: move freeing an output stream into a separate function Anton Khirnov 2022-10-14 10:15 ` [FFmpeg-devel] [PATCH 15/22] fftools/ffmpeg: reindent after previous commit Anton Khirnov 2022-10-14 10:15 ` [FFmpeg-devel] [PATCH 16/22] fftools/ffmpeg_mux_init: pass Muxer to new_output_stream() Anton Khirnov 2022-10-14 10:15 ` [FFmpeg-devel] [PATCH 17/22] fftools/ffmpeg: remove the output_streams global Anton Khirnov 2022-10-14 10:15 ` [FFmpeg-devel] [PATCH 18/22] fftools/ffmpeg: remove a cleanup block at the end of transcode() Anton Khirnov 2022-10-14 10:15 ` [FFmpeg-devel] [PATCH 19/22] fftools/ffmpeg: free output streams in of_close() Anton Khirnov 2022-10-14 10:15 ` [FFmpeg-devel] [PATCH 20/22] fftools/ffmpeg_mux: embed OutputStream in a MuxStream Anton Khirnov 2022-10-14 10:15 ` [FFmpeg-devel] [PATCH 21/22] fftools/ffmpeg_mux: move bsf_ctx from OutputStream to MuxStream Anton Khirnov 2022-10-14 10:15 ` [FFmpeg-devel] [PATCH 22/22] fftools/ffmpeg_mux: move muxing queue fields " Anton Khirnov 2022-10-17 8:49 ` [FFmpeg-devel] [PATCH 01/13] fftools/ffmpeg_mux: do not unref a NULL packet Anton Khirnov
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=20221013134904.10104-2-anton@khirnov.net \ --to=anton@khirnov.net \ --cc=ffmpeg-devel@ffmpeg.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: link
Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel This inbox may be cloned and mirrored by anyone: git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git # If you have public-inbox 1.1+ installed, you may # initialize and index your mirror using the following commands: public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \ ffmpegdev@gitmailbox.com public-inbox-index ffmpegdev Example config snippet for mirrors. AGPL code for this site: git clone https://public-inbox.org/public-inbox.git