From patchwork Tue Apr 28 17:37:18 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marton Balint X-Patchwork-Id: 19338 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id 9D86244B549 for ; Tue, 28 Apr 2020 20:37:41 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 6BC5A68C596; Tue, 28 Apr 2020 20:37:41 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from iq.passwd.hu (iq.passwd.hu [217.27.212.140]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id CDB3B68BEBE for ; Tue, 28 Apr 2020 20:37:34 +0300 (EEST) Received: from localhost (localhost [127.0.0.1]) by iq.passwd.hu (Postfix) with ESMTP id A2057E3E28; Tue, 28 Apr 2020 19:37:34 +0200 (CEST) X-Virus-Scanned: amavisd-new at passwd.hu Received: from iq.passwd.hu ([127.0.0.1]) by localhost (iq.passwd.hu [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 4S9NDK998c1T; Tue, 28 Apr 2020 19:37:32 +0200 (CEST) Received: from bluegene.passwd.hu (localhost [127.0.0.1]) by iq.passwd.hu (Postfix) with ESMTP id 575E0E3E19; Tue, 28 Apr 2020 19:37:32 +0200 (CEST) From: Marton Balint To: ffmpeg-devel@ffmpeg.org Date: Tue, 28 Apr 2020 19:37:18 +0200 Message-Id: <20200428173725.12482-1-cus@passwd.hu> X-Mailer: git-send-email 2.16.4 Subject: [FFmpeg-devel] [PATCH v4 1/8] avformat/mux: move interleaved packet functions upwards X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: Marton Balint MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Will be needed later to avoid a forward declaration. Signed-off-by: Marton Balint --- libavformat/mux.c | 208 +++++++++++++++++++++++++++--------------------------- 1 file changed, 104 insertions(+), 104 deletions(-) diff --git a/libavformat/mux.c b/libavformat/mux.c index 3d1f71ab1a..df0d9e993a 100644 --- a/libavformat/mux.c +++ b/libavformat/mux.c @@ -809,110 +809,6 @@ static int prepare_input_packet(AVFormatContext *s, AVPacket *pkt) return 0; } -static int do_packet_auto_bsf(AVFormatContext *s, AVPacket *pkt) { - AVStream *st = s->streams[pkt->stream_index]; - int ret; - - if (!(s->flags & AVFMT_FLAG_AUTO_BSF)) - return 1; - - if (s->oformat->check_bitstream) { - if (!st->internal->bitstream_checked) { - if ((ret = s->oformat->check_bitstream(s, pkt)) < 0) - return ret; - else if (ret == 1) - st->internal->bitstream_checked = 1; - } - } - - if (st->internal->bsfc) { - AVBSFContext *ctx = st->internal->bsfc; - // TODO: when any bitstream filter requires flushing at EOF, we'll need to - // flush each stream's BSF chain on write_trailer. - if ((ret = av_bsf_send_packet(ctx, pkt)) < 0) { - av_log(ctx, AV_LOG_ERROR, - "Failed to send packet to filter %s for stream %d\n", - ctx->filter->name, pkt->stream_index); - return ret; - } - // TODO: when any automatically-added bitstream filter is generating multiple - // output packets for a single input one, we'll need to call this in a loop - // and write each output packet. - if ((ret = av_bsf_receive_packet(ctx, pkt)) < 0) { - if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) - return 0; - av_log(ctx, AV_LOG_ERROR, - "Failed to receive packet from filter %s for stream %d\n", - ctx->filter->name, pkt->stream_index); - if (s->error_recognition & AV_EF_EXPLODE) - return ret; - return 0; - } - } - return 1; -} - -int av_write_frame(AVFormatContext *s, AVPacket *in) -{ - AVPacket local_pkt, *pkt = &local_pkt; - int ret; - - if (!in) { - if (s->oformat->flags & AVFMT_ALLOW_FLUSH) { - ret = s->oformat->write_packet(s, NULL); - flush_if_needed(s); - if (ret >= 0 && s->pb && s->pb->error < 0) - ret = s->pb->error; - return ret; - } - return 1; - } - - if (in->flags & AV_PKT_FLAG_UNCODED_FRAME) { - pkt = in; - } else { - /* We don't own in, so we have to make sure not to modify it. - * The following avoids copying in's data unnecessarily. - * Copying side data is unavoidable as a bitstream filter - * may change it, e.g. free it on errors. */ - pkt->buf = NULL; - pkt->data = in->data; - pkt->size = in->size; - ret = av_packet_copy_props(pkt, in); - if (ret < 0) - return ret; - if (in->buf) { - pkt->buf = av_buffer_ref(in->buf); - if (!pkt->buf) { - ret = AVERROR(ENOMEM); - goto fail; - } - } - } - - ret = prepare_input_packet(s, pkt); - if (ret < 0) - goto fail; - - ret = do_packet_auto_bsf(s, pkt); - if (ret <= 0) - goto fail; - -#if FF_API_COMPUTE_PKT_FIELDS2 && FF_API_LAVF_AVCTX - ret = compute_muxer_pkt_fields(s, s->streams[pkt->stream_index], pkt); - - if (ret < 0 && !(s->oformat->flags & AVFMT_NOTIMESTAMPS)) - goto fail; -#endif - - ret = write_packet(s, pkt); - -fail: - // Uncoded frames using the noninterleaved codepath are also freed here - av_packet_unref(pkt); - return ret; -} - #define CHUNK_START 0x1000 int ff_interleave_add_packet(AVFormatContext *s, AVPacket *pkt, @@ -1181,6 +1077,110 @@ static int interleave_packet(AVFormatContext *s, AVPacket *out, AVPacket *in, in return ff_interleave_packet_per_dts(s, out, in, flush); } +static int do_packet_auto_bsf(AVFormatContext *s, AVPacket *pkt) { + AVStream *st = s->streams[pkt->stream_index]; + int ret; + + if (!(s->flags & AVFMT_FLAG_AUTO_BSF)) + return 1; + + if (s->oformat->check_bitstream) { + if (!st->internal->bitstream_checked) { + if ((ret = s->oformat->check_bitstream(s, pkt)) < 0) + return ret; + else if (ret == 1) + st->internal->bitstream_checked = 1; + } + } + + if (st->internal->bsfc) { + AVBSFContext *ctx = st->internal->bsfc; + // TODO: when any bitstream filter requires flushing at EOF, we'll need to + // flush each stream's BSF chain on write_trailer. + if ((ret = av_bsf_send_packet(ctx, pkt)) < 0) { + av_log(ctx, AV_LOG_ERROR, + "Failed to send packet to filter %s for stream %d\n", + ctx->filter->name, pkt->stream_index); + return ret; + } + // TODO: when any automatically-added bitstream filter is generating multiple + // output packets for a single input one, we'll need to call this in a loop + // and write each output packet. + if ((ret = av_bsf_receive_packet(ctx, pkt)) < 0) { + if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) + return 0; + av_log(ctx, AV_LOG_ERROR, + "Failed to receive packet from filter %s for stream %d\n", + ctx->filter->name, pkt->stream_index); + if (s->error_recognition & AV_EF_EXPLODE) + return ret; + return 0; + } + } + return 1; +} + +int av_write_frame(AVFormatContext *s, AVPacket *in) +{ + AVPacket local_pkt, *pkt = &local_pkt; + int ret; + + if (!in) { + if (s->oformat->flags & AVFMT_ALLOW_FLUSH) { + ret = s->oformat->write_packet(s, NULL); + flush_if_needed(s); + if (ret >= 0 && s->pb && s->pb->error < 0) + ret = s->pb->error; + return ret; + } + return 1; + } + + if (in->flags & AV_PKT_FLAG_UNCODED_FRAME) { + pkt = in; + } else { + /* We don't own in, so we have to make sure not to modify it. + * The following avoids copying in's data unnecessarily. + * Copying side data is unavoidable as a bitstream filter + * may change it, e.g. free it on errors. */ + pkt->buf = NULL; + pkt->data = in->data; + pkt->size = in->size; + ret = av_packet_copy_props(pkt, in); + if (ret < 0) + return ret; + if (in->buf) { + pkt->buf = av_buffer_ref(in->buf); + if (!pkt->buf) { + ret = AVERROR(ENOMEM); + goto fail; + } + } + } + + ret = prepare_input_packet(s, pkt); + if (ret < 0) + goto fail; + + ret = do_packet_auto_bsf(s, pkt); + if (ret <= 0) + goto fail; + +#if FF_API_COMPUTE_PKT_FIELDS2 && FF_API_LAVF_AVCTX + ret = compute_muxer_pkt_fields(s, s->streams[pkt->stream_index], pkt); + + if (ret < 0 && !(s->oformat->flags & AVFMT_NOTIMESTAMPS)) + goto fail; +#endif + + ret = write_packet(s, pkt); + +fail: + // Uncoded frames using the noninterleaved codepath are also freed here + av_packet_unref(pkt); + return ret; +} + int av_interleaved_write_frame(AVFormatContext *s, AVPacket *pkt) { int ret, flush = 0; From patchwork Tue Apr 28 17:37:19 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marton Balint X-Patchwork-Id: 19339 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id 6332444B549 for ; Tue, 28 Apr 2020 20:37:48 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 4E83568C59D; Tue, 28 Apr 2020 20:37:48 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from iq.passwd.hu (iq.passwd.hu [217.27.212.140]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 0AB4568C5B0 for ; Tue, 28 Apr 2020 20:37:42 +0300 (EEST) Received: from localhost (localhost [127.0.0.1]) by iq.passwd.hu (Postfix) with ESMTP id EB3D8E40A7; Tue, 28 Apr 2020 19:37:41 +0200 (CEST) X-Virus-Scanned: amavisd-new at passwd.hu Received: from iq.passwd.hu ([127.0.0.1]) by localhost (iq.passwd.hu [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id CiUTd2vbwJ-Q; Tue, 28 Apr 2020 19:37:39 +0200 (CEST) Received: from bluegene.passwd.hu (localhost [127.0.0.1]) by iq.passwd.hu (Postfix) with ESMTP id 739EAE3E19; Tue, 28 Apr 2020 19:37:39 +0200 (CEST) From: Marton Balint To: ffmpeg-devel@ffmpeg.org Date: Tue, 28 Apr 2020 19:37:19 +0200 Message-Id: <20200428173725.12482-2-cus@passwd.hu> X-Mailer: git-send-email 2.16.4 In-Reply-To: <20200428173725.12482-1-cus@passwd.hu> References: <20200428173725.12482-1-cus@passwd.hu> Subject: [FFmpeg-devel] [PATCH v4 2/8] avformat/mux: factorize interleaved write_packet X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: Marton Balint MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Signed-off-by: Marton Balint --- libavformat/mux.c | 52 +++++++++++++++++++++------------------------------- 1 file changed, 21 insertions(+), 31 deletions(-) diff --git a/libavformat/mux.c b/libavformat/mux.c index df0d9e993a..c91df91be8 100644 --- a/libavformat/mux.c +++ b/libavformat/mux.c @@ -1120,6 +1120,25 @@ static int do_packet_auto_bsf(AVFormatContext *s, AVPacket *pkt) { return 1; } +static int interleaved_write_packet(AVFormatContext *s, AVPacket *pkt, int flush) +{ + for (;; ) { + AVPacket opkt; + int ret = interleave_packet(s, &opkt, pkt, flush); + if (ret <= 0) + return ret; + + pkt = NULL; + + ret = write_packet(s, &opkt); + + av_packet_unref(&opkt); + + if (ret < 0) + return ret; + } +} + int av_write_frame(AVFormatContext *s, AVPacket *in) { AVPacket local_pkt, *pkt = &local_pkt; @@ -1215,22 +1234,8 @@ int av_interleaved_write_frame(AVFormatContext *s, AVPacket *pkt) av_log(s, AV_LOG_TRACE, "av_interleaved_write_frame FLUSH\n"); flush = 1; } + return interleaved_write_packet(s, pkt, flush); - for (;; ) { - AVPacket opkt; - int ret = interleave_packet(s, &opkt, pkt, flush); - if (ret <= 0) - return ret; - - pkt = NULL; - - ret = write_packet(s, &opkt); - - av_packet_unref(&opkt); - - if (ret < 0) - return ret; - } fail: av_packet_unref(pkt); return ret; @@ -1240,23 +1245,8 @@ int av_write_trailer(AVFormatContext *s) { int ret, i; - for (;; ) { - AVPacket pkt; - ret = interleave_packet(s, &pkt, NULL, 1); - if (ret < 0) - goto fail; - if (!ret) - break; - - ret = write_packet(s, &pkt); - - av_packet_unref(&pkt); + ret = interleaved_write_packet(s, NULL, 1); - if (ret < 0) - goto fail; - } - -fail: if (s->oformat->write_trailer) { if (!(s->oformat->flags & AVFMT_NOFILE) && s->pb) avio_write_marker(s->pb, AV_NOPTS_VALUE, AVIO_DATA_MARKER_TRAILER); From patchwork Tue Apr 28 17:37:20 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marton Balint X-Patchwork-Id: 19340 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id 3B14744B549 for ; Tue, 28 Apr 2020 20:37:50 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 210A368C5C8; Tue, 28 Apr 2020 20:37:50 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from iq.passwd.hu (iq.passwd.hu [217.27.212.140]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 6ADF268C5C1 for ; Tue, 28 Apr 2020 20:37:43 +0300 (EEST) Received: from localhost (localhost [127.0.0.1]) by iq.passwd.hu (Postfix) with ESMTP id 4D4D3E423B; Tue, 28 Apr 2020 19:37:43 +0200 (CEST) X-Virus-Scanned: amavisd-new at passwd.hu Received: from iq.passwd.hu ([127.0.0.1]) by localhost (iq.passwd.hu [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id v6oHpjquss_j; Tue, 28 Apr 2020 19:37:41 +0200 (CEST) Received: from bluegene.passwd.hu (localhost [127.0.0.1]) by iq.passwd.hu (Postfix) with ESMTP id 950F0E3E28; Tue, 28 Apr 2020 19:37:40 +0200 (CEST) From: Marton Balint To: ffmpeg-devel@ffmpeg.org Date: Tue, 28 Apr 2020 19:37:20 +0200 Message-Id: <20200428173725.12482-3-cus@passwd.hu> X-Mailer: git-send-email 2.16.4 In-Reply-To: <20200428173725.12482-1-cus@passwd.hu> References: <20200428173725.12482-1-cus@passwd.hu> Subject: [FFmpeg-devel] [PATCH v4 3/8] avformat/mux: factorize writing a packet X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: Marton Balint MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" In preparation for N:M bsf support. Signed-off-by: Marton Balint --- libavformat/mux.c | 87 ++++++++++++++++++++++++++----------------------------- 1 file changed, 41 insertions(+), 46 deletions(-) diff --git a/libavformat/mux.c b/libavformat/mux.c index c91df91be8..33aed9da90 100644 --- a/libavformat/mux.c +++ b/libavformat/mux.c @@ -1139,6 +1139,41 @@ static int interleaved_write_packet(AVFormatContext *s, AVPacket *pkt, int flush } } +static int write_packet_common(AVFormatContext *s, AVStream *st, AVPacket *pkt, int interleaved) +{ + int ret; + + if (s->debug & FF_FDEBUG_TS) + av_log(s, AV_LOG_DEBUG, "%s size:%d dts:%s pts:%s\n", __FUNCTION__, + pkt->size, av_ts2str(pkt->dts), av_ts2str(pkt->pts)); + +#if FF_API_COMPUTE_PKT_FIELDS2 && FF_API_LAVF_AVCTX + if ((ret = compute_muxer_pkt_fields(s, st, pkt)) < 0 && !(s->oformat->flags & AVFMT_NOTIMESTAMPS)) + return ret; +#endif + + if (interleaved) { + if (pkt->dts == AV_NOPTS_VALUE && !(s->oformat->flags & AVFMT_NOTIMESTAMPS)) + return AVERROR(EINVAL); + return interleaved_write_packet(s, pkt, 0); + } else { + return write_packet(s, pkt); + } +} + +static int write_packets_common(AVFormatContext *s, AVStream *st, AVPacket *pkt, int interleaved) +{ + int ret = prepare_input_packet(s, pkt); + if (ret < 0) + return ret; + + ret = do_packet_auto_bsf(s, pkt); + if (ret <= 0) + return ret; + + return write_packet_common(s, st, pkt, interleaved); +} + int av_write_frame(AVFormatContext *s, AVPacket *in) { AVPacket local_pkt, *pkt = &local_pkt; @@ -1177,22 +1212,7 @@ int av_write_frame(AVFormatContext *s, AVPacket *in) } } - ret = prepare_input_packet(s, pkt); - if (ret < 0) - goto fail; - - ret = do_packet_auto_bsf(s, pkt); - if (ret <= 0) - goto fail; - -#if FF_API_COMPUTE_PKT_FIELDS2 && FF_API_LAVF_AVCTX - ret = compute_muxer_pkt_fields(s, s->streams[pkt->stream_index], pkt); - - if (ret < 0 && !(s->oformat->flags & AVFMT_NOTIMESTAMPS)) - goto fail; -#endif - - ret = write_packet(s, pkt); + ret = write_packets_common(s, s->streams[pkt->stream_index], pkt, 0/*non-interleaved*/); fail: // Uncoded frames using the noninterleaved codepath are also freed here @@ -1202,43 +1222,18 @@ fail: int av_interleaved_write_frame(AVFormatContext *s, AVPacket *pkt) { - int ret, flush = 0; + int ret; if (pkt) { AVStream *st = s->streams[pkt->stream_index]; - - ret = prepare_input_packet(s, pkt); + ret = write_packets_common(s, st, pkt, 1/*interleaved*/); if (ret < 0) - goto fail; - - ret = do_packet_auto_bsf(s, pkt); - if (ret == 0) - return 0; - else if (ret < 0) - goto fail; - - if (s->debug & FF_FDEBUG_TS) - av_log(s, AV_LOG_DEBUG, "av_interleaved_write_frame size:%d dts:%s pts:%s\n", - pkt->size, av_ts2str(pkt->dts), av_ts2str(pkt->pts)); - -#if FF_API_COMPUTE_PKT_FIELDS2 && FF_API_LAVF_AVCTX - if ((ret = compute_muxer_pkt_fields(s, st, pkt)) < 0 && !(s->oformat->flags & AVFMT_NOTIMESTAMPS)) - goto fail; -#endif - - if (pkt->dts == AV_NOPTS_VALUE && !(s->oformat->flags & AVFMT_NOTIMESTAMPS)) { - ret = AVERROR(EINVAL); - goto fail; - } + av_packet_unref(pkt); + return ret; } else { av_log(s, AV_LOG_TRACE, "av_interleaved_write_frame FLUSH\n"); - flush = 1; + return interleaved_write_packet(s, NULL, 1/*flush*/); } - return interleaved_write_packet(s, pkt, flush); - -fail: - av_packet_unref(pkt); - return ret; } int av_write_trailer(AVFormatContext *s) From patchwork Tue Apr 28 17:37:21 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marton Balint X-Patchwork-Id: 19341 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id 2464344B549 for ; Tue, 28 Apr 2020 20:37:53 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 0C2DB68C5F7; Tue, 28 Apr 2020 20:37:53 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from iq.passwd.hu (iq.passwd.hu [217.27.212.140]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 16FBF68C5AE for ; Tue, 28 Apr 2020 20:37:45 +0300 (EEST) Received: from localhost (localhost [127.0.0.1]) by iq.passwd.hu (Postfix) with ESMTP id F0F33E3547; Tue, 28 Apr 2020 19:37:44 +0200 (CEST) X-Virus-Scanned: amavisd-new at passwd.hu Received: from iq.passwd.hu ([127.0.0.1]) by localhost (iq.passwd.hu [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id N-w3xFuqXMCN; Tue, 28 Apr 2020 19:37:43 +0200 (CEST) Received: from bluegene.passwd.hu (localhost [127.0.0.1]) by iq.passwd.hu (Postfix) with ESMTP id C59D2E40AC; Tue, 28 Apr 2020 19:37:42 +0200 (CEST) From: Marton Balint To: ffmpeg-devel@ffmpeg.org Date: Tue, 28 Apr 2020 19:37:21 +0200 Message-Id: <20200428173725.12482-4-cus@passwd.hu> X-Mailer: git-send-email 2.16.4 In-Reply-To: <20200428173725.12482-1-cus@passwd.hu> References: <20200428173725.12482-1-cus@passwd.hu> Subject: [FFmpeg-devel] [PATCH v4 4/8] avformat/mux: add proper support for full N:M bitstream filtering X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: Marton Balint MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Previously only 1:1 bitstream filters were supported, the end of the stream was not signalled to the bitstream filters and time base changes were ignored. This change also allows muxers to set up bitstream filters regardless of the autobsf flag during write_header instead of during check_bitstream and those bitstream filters will always be executed. Signed-off-by: Marton Balint --- libavformat/mux.c | 87 +++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 56 insertions(+), 31 deletions(-) diff --git a/libavformat/mux.c b/libavformat/mux.c index 33aed9da90..d3a98499ce 100644 --- a/libavformat/mux.c +++ b/libavformat/mux.c @@ -1077,8 +1077,8 @@ static int interleave_packet(AVFormatContext *s, AVPacket *out, AVPacket *in, in return ff_interleave_packet_per_dts(s, out, in, flush); } -static int do_packet_auto_bsf(AVFormatContext *s, AVPacket *pkt) { - AVStream *st = s->streams[pkt->stream_index]; +static int check_bitstream(AVFormatContext *s, AVStream *st, AVPacket *pkt) +{ int ret; if (!(s->flags & AVFMT_FLAG_AUTO_BSF)) @@ -1093,30 +1093,6 @@ static int do_packet_auto_bsf(AVFormatContext *s, AVPacket *pkt) { } } - if (st->internal->bsfc) { - AVBSFContext *ctx = st->internal->bsfc; - // TODO: when any bitstream filter requires flushing at EOF, we'll need to - // flush each stream's BSF chain on write_trailer. - if ((ret = av_bsf_send_packet(ctx, pkt)) < 0) { - av_log(ctx, AV_LOG_ERROR, - "Failed to send packet to filter %s for stream %d\n", - ctx->filter->name, pkt->stream_index); - return ret; - } - // TODO: when any automatically-added bitstream filter is generating multiple - // output packets for a single input one, we'll need to call this in a loop - // and write each output packet. - if ((ret = av_bsf_receive_packet(ctx, pkt)) < 0) { - if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) - return 0; - av_log(ctx, AV_LOG_ERROR, - "Failed to receive packet from filter %s for stream %d\n", - ctx->filter->name, pkt->stream_index); - if (s->error_recognition & AV_EF_EXPLODE) - return ret; - return 0; - } - } return 1; } @@ -1161,17 +1137,53 @@ static int write_packet_common(AVFormatContext *s, AVStream *st, AVPacket *pkt, } } +static int write_packets_from_bsfs(AVFormatContext *s, AVStream *st, AVPacket *pkt, int interleaved) +{ + AVBSFContext *bsfc = st->internal->bsfc; + int ret; + + if ((ret = av_bsf_send_packet(bsfc, pkt)) < 0) { + av_log(s, AV_LOG_ERROR, + "Failed to send packet to filter %s for stream %d\n", + bsfc->filter->name, st->index); + return ret; + } + + do { + ret = av_bsf_receive_packet(bsfc, pkt); + if (ret < 0) { + if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) + return 0; + av_log(s, AV_LOG_ERROR, "Error applying bitstream filters to an output " + "packet for stream #%d: %s\n", st->index, av_err2str(ret)); + if (!(s->error_recognition & AV_EF_EXPLODE) && ret != AVERROR(ENOMEM)) + continue; + return ret; + } + av_packet_rescale_ts(pkt, bsfc->time_base_out, st->time_base); + ret = write_packet_common(s, st, pkt, interleaved); + if (ret >= 0 && !interleaved) // a successful write_packet_common already unrefed pkt for interleaved + av_packet_unref(pkt); + } while (ret >= 0); + + return ret; +} + static int write_packets_common(AVFormatContext *s, AVStream *st, AVPacket *pkt, int interleaved) { int ret = prepare_input_packet(s, pkt); if (ret < 0) return ret; - ret = do_packet_auto_bsf(s, pkt); - if (ret <= 0) + ret = check_bitstream(s, st, pkt); + if (ret < 0) return ret; - return write_packet_common(s, st, pkt, interleaved); + if (st->internal->bsfc) { + return write_packets_from_bsfs(s, st, pkt, interleaved); + } else { + return write_packet_common(s, st, pkt, interleaved); + } } int av_write_frame(AVFormatContext *s, AVPacket *in) @@ -1238,9 +1250,22 @@ int av_interleaved_write_frame(AVFormatContext *s, AVPacket *pkt) int av_write_trailer(AVFormatContext *s) { - int ret, i; + int i, ret1, ret = 0; + AVPacket pkt = {0}; + av_init_packet(&pkt); - ret = interleaved_write_packet(s, NULL, 1); + for (i = 0; i < s->nb_streams; i++) { + if (s->streams[i]->internal->bsfc) { + ret1 = write_packets_from_bsfs(s, s->streams[i], &pkt, 1/*interleaved*/); + if (ret1 < 0) + av_packet_unref(&pkt); + if (ret >= 0) + ret = ret1; + } + } + ret1 = interleaved_write_packet(s, NULL, 1); + if (ret >= 0) + ret = ret1; if (s->oformat->write_trailer) { if (!(s->oformat->flags & AVFMT_NOFILE) && s->pb) From patchwork Tue Apr 28 17:37:22 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marton Balint X-Patchwork-Id: 19342 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id 18E5044B549 for ; Tue, 28 Apr 2020 20:37:54 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 05B5468C60C; Tue, 28 Apr 2020 20:37:54 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from iq.passwd.hu (iq.passwd.hu [217.27.212.140]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id AC38B68C5B0 for ; Tue, 28 Apr 2020 20:37:46 +0300 (EEST) Received: from localhost (localhost [127.0.0.1]) by iq.passwd.hu (Postfix) with ESMTP id 8EEF8E423D; Tue, 28 Apr 2020 19:37:46 +0200 (CEST) X-Virus-Scanned: amavisd-new at passwd.hu Received: from iq.passwd.hu ([127.0.0.1]) by localhost (iq.passwd.hu [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id SMohsys_Ht1W; Tue, 28 Apr 2020 19:37:45 +0200 (CEST) Received: from bluegene.passwd.hu (localhost [127.0.0.1]) by iq.passwd.hu (Postfix) with ESMTP id EC083E3E28; Tue, 28 Apr 2020 19:37:43 +0200 (CEST) From: Marton Balint To: ffmpeg-devel@ffmpeg.org Date: Tue, 28 Apr 2020 19:37:22 +0200 Message-Id: <20200428173725.12482-5-cus@passwd.hu> X-Mailer: git-send-email 2.16.4 In-Reply-To: <20200428173725.12482-1-cus@passwd.hu> References: <20200428173725.12482-1-cus@passwd.hu> Subject: [FFmpeg-devel] [PATCH v4 5/8] avcodec/pcm_rechunk_bsf: add bitstream filter to rechunk pcm audio X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: Marton Balint MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Signed-off-by: Marton Balint --- Changelog | 1 + doc/bitstream_filters.texi | 30 ++++++ libavcodec/Makefile | 1 + libavcodec/bitstream_filters.c | 1 + libavcodec/pcm_rechunk_bsf.c | 220 +++++++++++++++++++++++++++++++++++++++++ libavcodec/version.h | 2 +- 6 files changed, 254 insertions(+), 1 deletion(-) create mode 100644 libavcodec/pcm_rechunk_bsf.c diff --git a/Changelog b/Changelog index 83b8a4a46e..883e0bff99 100644 --- a/Changelog +++ b/Changelog @@ -63,6 +63,7 @@ version : - maskedthreshold filter - Support for muxing pcm and pgs in m2ts - Cunning Developments ADPCM decoder +- pcm_rechunk bitstream filter version 4.2: diff --git a/doc/bitstream_filters.texi b/doc/bitstream_filters.texi index 8fe5b3ad75..287d320cc0 100644 --- a/doc/bitstream_filters.texi +++ b/doc/bitstream_filters.texi @@ -548,6 +548,36 @@ ffmpeg -i INPUT -c copy -bsf noise[=1] output.mkv @section null This bitstream filter passes the packets through unchanged. +@section pcm_rechunk + +Repacketize PCM audio to a fixed number of samples per packet or a fixed packet +rate per second. This is similar to the @ref{asetnsamples,,asetnsamples audio +filter,ffmpeg-filters} but works on audio packets instead of audio frames. + +@table @option +@item nb_out_samples, n +Set the number of samples per each output audio packet. The number is intended +as the number of samples @emph{per each channel}. Default value is 1024. + +@item pad, p +If set to 1, the filter will pad the last audio packet with silence, so that it +will contain the same number of samples (or roughly the same number of samples, +see @option{frame_rate}) as the previous ones. Default value is 1. + +@item frame_rate, r +This option makes the filter output a fixed numer of packets per second instead +of a fixed number of samples per packet. If the audio sample rate is not +divisible by the frame rate then the number of samples will not be constant but +will vary slightly so that each packet will start as close to the frame +boundary as possible. Using this option has precedence over @option{nb_out_samples}. +@end table + +You can generate the well known 1602-1601-1602-1601-1602 pattern of 48kHz audio +for NTSC frame rate using the @option{frame_rate} option. +@example +ffmpeg -f lavfi -i sine=r=48000:d=1 -c pcm_s16le -bsf pcm_rechunk=r=30000/1001 -f framecrc - +@end example + @section prores_metadata Modify color property metadata embedded in prores stream. diff --git a/libavcodec/Makefile b/libavcodec/Makefile index 28076c2c83..f5dcbb44ee 100644 --- a/libavcodec/Makefile +++ b/libavcodec/Makefile @@ -1116,6 +1116,7 @@ OBJS-$(CONFIG_MP3_HEADER_DECOMPRESS_BSF) += mp3_header_decompress_bsf.o \ OBJS-$(CONFIG_MPEG2_METADATA_BSF) += mpeg2_metadata_bsf.o OBJS-$(CONFIG_NOISE_BSF) += noise_bsf.o OBJS-$(CONFIG_NULL_BSF) += null_bsf.o +OBJS-$(CONFIG_PCM_RECHUNK_BSF) += pcm_rechunk_bsf.o OBJS-$(CONFIG_PRORES_METADATA_BSF) += prores_metadata_bsf.o OBJS-$(CONFIG_REMOVE_EXTRADATA_BSF) += remove_extradata_bsf.o OBJS-$(CONFIG_TEXT2MOVSUB_BSF) += movsub_bsf.o diff --git a/libavcodec/bitstream_filters.c b/libavcodec/bitstream_filters.c index 6b5ffe4d70..9e701191f8 100644 --- a/libavcodec/bitstream_filters.c +++ b/libavcodec/bitstream_filters.c @@ -49,6 +49,7 @@ extern const AVBitStreamFilter ff_mpeg4_unpack_bframes_bsf; extern const AVBitStreamFilter ff_mov2textsub_bsf; extern const AVBitStreamFilter ff_noise_bsf; extern const AVBitStreamFilter ff_null_bsf; +extern const AVBitStreamFilter ff_pcm_rechunk_bsf; extern const AVBitStreamFilter ff_prores_metadata_bsf; extern const AVBitStreamFilter ff_remove_extradata_bsf; extern const AVBitStreamFilter ff_text2movsub_bsf; diff --git a/libavcodec/pcm_rechunk_bsf.c b/libavcodec/pcm_rechunk_bsf.c new file mode 100644 index 0000000000..b528ed0c71 --- /dev/null +++ b/libavcodec/pcm_rechunk_bsf.c @@ -0,0 +1,220 @@ +/* + * Copyright (c) 2020 Marton Balint + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "avcodec.h" +#include "bsf.h" +#include "libavutil/avassert.h" +#include "libavutil/opt.h" + +typedef struct PCMContext { + const AVClass *class; + + int nb_out_samples; + int pad; + AVRational frame_rate; + + AVPacket *in_pkt; + AVPacket *out_pkt; + int sample_size; + int64_t n; +} PCMContext; + +static int init(AVBSFContext *ctx) +{ + PCMContext *s = ctx->priv_data; + AVRational sr = av_make_q(ctx->par_in->sample_rate, 1); + int64_t min_samples; + + if (ctx->par_in->channels <= 0 || ctx->par_in->sample_rate <= 0) + return AVERROR(EINVAL); + + ctx->time_base_out = av_inv_q(sr); + s->sample_size = ctx->par_in->channels * av_get_bits_per_sample(ctx->par_in->codec_id) / 8; + + if (s->frame_rate.num) { + min_samples = av_rescale_q_rnd(1, sr, s->frame_rate, AV_ROUND_DOWN); + } else { + min_samples = s->nb_out_samples; + } + if (min_samples <= 0 || min_samples > INT_MAX / s->sample_size - 1) + return AVERROR(EINVAL); + + s->in_pkt = av_packet_alloc(); + s->out_pkt = av_packet_alloc(); + if (!s->in_pkt || !s->out_pkt) + return AVERROR(ENOMEM); + + return 0; +} + +static void uninit(AVBSFContext *ctx) +{ + PCMContext *s = ctx->priv_data; + av_packet_free(&s->in_pkt); + av_packet_free(&s->out_pkt); +} + +static void flush(AVBSFContext *ctx) +{ + PCMContext *s = ctx->priv_data; + av_packet_unref(s->in_pkt); + av_packet_unref(s->out_pkt); + s->n = 0; +} + +static int send_packet(PCMContext *s, int nb_samples, AVPacket *pkt) +{ + pkt->duration = nb_samples; + s->n++; + return 0; +} + +static void drain_packet(AVPacket *pkt, int drain_data, int drain_samples) +{ + pkt->size -= drain_data; + pkt->data += drain_data; + if (pkt->dts != AV_NOPTS_VALUE) + pkt->dts += drain_samples; + if (pkt->pts != AV_NOPTS_VALUE) + pkt->pts += drain_samples; +} + +static int get_next_nb_samples(AVBSFContext *ctx) +{ + PCMContext *s = ctx->priv_data; + if (s->frame_rate.num) { + AVRational sr = av_make_q(ctx->par_in->sample_rate, 1); + return av_rescale_q(s->n + 1, sr, s->frame_rate) - av_rescale_q(s->n, sr, s->frame_rate); + } else { + return s->nb_out_samples; + } +} + +static int rechunk_filter(AVBSFContext *ctx, AVPacket *pkt) +{ + PCMContext *s = ctx->priv_data; + int nb_samples = get_next_nb_samples(ctx); + int data_size = nb_samples * s->sample_size; + int ret; + + do { + if (s->in_pkt->size) { + if (s->out_pkt->size || s->in_pkt->size < data_size) { + int drain = FFMIN(s->in_pkt->size, data_size - s->out_pkt->size); + if (!s->out_pkt->size) { + ret = av_new_packet(s->out_pkt, data_size); + if (ret < 0) + return ret; + ret = av_packet_copy_props(s->out_pkt, s->in_pkt); + if (ret < 0) { + av_packet_unref(s->out_pkt); + return ret; + } + s->out_pkt->size = 0; + } + memcpy(s->out_pkt->data + s->out_pkt->size, s->in_pkt->data, drain); + s->out_pkt->size += drain; + drain_packet(s->in_pkt, drain, drain / s->sample_size); + if (!s->in_pkt->size) + av_packet_unref(s->in_pkt); + if (s->out_pkt->size == data_size) { + av_packet_move_ref(pkt, s->out_pkt); + return send_packet(s, nb_samples, pkt); + } + } else if (s->in_pkt->size > data_size) { + ret = av_packet_ref(pkt, s->in_pkt); + if (ret < 0) + return ret; + pkt->size = data_size; + drain_packet(s->in_pkt, data_size, nb_samples); + return send_packet(s, nb_samples, pkt); + } else { + av_assert0(s->in_pkt->size == data_size); + av_packet_move_ref(pkt, s->in_pkt); + return send_packet(s, nb_samples, pkt); + } + } + + ret = ff_bsf_get_packet_ref(ctx, s->in_pkt); + if (ret == AVERROR_EOF && s->out_pkt->size) { + if (s->pad) { + memset(s->out_pkt->data + s->out_pkt->size, 0, data_size - s->out_pkt->size); + s->out_pkt->size = data_size; + } else { + nb_samples = s->out_pkt->size / s->sample_size; + } + av_packet_move_ref(pkt, s->out_pkt); + return send_packet(s, nb_samples, pkt); + } + if (ret >= 0) + av_packet_rescale_ts(s->in_pkt, ctx->time_base_in, ctx->time_base_out); + } while (ret >= 0); + + return ret; +} + +#define OFFSET(x) offsetof(PCMContext, x) +#define FLAGS (AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_BSF_PARAM) +static const AVOption options[] = { + { "nb_out_samples", "set the number of per-packet output samples", OFFSET(nb_out_samples), AV_OPT_TYPE_INT, {.i64=1024}, 1, INT_MAX, FLAGS }, + { "n", "set the number of per-packet output samples", OFFSET(nb_out_samples), AV_OPT_TYPE_INT, {.i64=1024}, 1, INT_MAX, FLAGS }, + { "pad", "pad last packet with zeros", OFFSET(pad), AV_OPT_TYPE_BOOL, {.i64=1} , 0, 1, FLAGS }, + { "p", "pad last packet with zeros", OFFSET(pad), AV_OPT_TYPE_BOOL, {.i64=1} , 0, 1, FLAGS }, + { "frame_rate", "set number of packets per second", OFFSET(frame_rate), AV_OPT_TYPE_RATIONAL, {.dbl=0}, 0, INT_MAX, FLAGS }, + { "r", "set number of packets per second", OFFSET(frame_rate), AV_OPT_TYPE_RATIONAL, {.dbl=0}, 0, INT_MAX, FLAGS }, + { NULL }, +}; + +static const AVClass pcm_rechunk_class = { + .class_name = "pcm_rechunk_bsf", + .item_name = av_default_item_name, + .option = options, + .version = LIBAVUTIL_VERSION_INT, +}; + +static const enum AVCodecID codec_ids[] = { + AV_CODEC_ID_PCM_S16LE, + AV_CODEC_ID_PCM_S16BE, + AV_CODEC_ID_PCM_S8, + AV_CODEC_ID_PCM_S32LE, + AV_CODEC_ID_PCM_S32BE, + AV_CODEC_ID_PCM_S24LE, + AV_CODEC_ID_PCM_S24BE, + AV_CODEC_ID_PCM_F32BE, + AV_CODEC_ID_PCM_F32LE, + AV_CODEC_ID_PCM_F64BE, + AV_CODEC_ID_PCM_F64LE, + AV_CODEC_ID_PCM_S64LE, + AV_CODEC_ID_PCM_S64BE, + AV_CODEC_ID_PCM_F16LE, + AV_CODEC_ID_PCM_F24LE, + AV_CODEC_ID_NONE, +}; + +const AVBitStreamFilter ff_pcm_rechunk_bsf = { + .name = "pcm_rechunk", + .priv_data_size = sizeof(PCMContext), + .priv_class = &pcm_rechunk_class, + .filter = rechunk_filter, + .init = init, + .flush = flush, + .close = uninit, + .codec_ids = codec_ids, +}; diff --git a/libavcodec/version.h b/libavcodec/version.h index 3de16c884c..a8cc09d0f6 100644 --- a/libavcodec/version.h +++ b/libavcodec/version.h @@ -28,7 +28,7 @@ #include "libavutil/version.h" #define LIBAVCODEC_VERSION_MAJOR 58 -#define LIBAVCODEC_VERSION_MINOR 82 +#define LIBAVCODEC_VERSION_MINOR 83 #define LIBAVCODEC_VERSION_MICRO 100 #define LIBAVCODEC_VERSION_INT AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \ From patchwork Tue Apr 28 17:37:23 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marton Balint X-Patchwork-Id: 19344 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id B764F44B549 for ; Tue, 28 Apr 2020 20:37:57 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id A975168C61F; Tue, 28 Apr 2020 20:37:57 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from iq.passwd.hu (iq.passwd.hu [217.27.212.140]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 6007F68C602 for ; Tue, 28 Apr 2020 20:37:48 +0300 (EEST) Received: from localhost (localhost [127.0.0.1]) by iq.passwd.hu (Postfix) with ESMTP id 4C2DBE4239; Tue, 28 Apr 2020 19:37:48 +0200 (CEST) X-Virus-Scanned: amavisd-new at passwd.hu Received: from iq.passwd.hu ([127.0.0.1]) by localhost (iq.passwd.hu [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id ziIsitLmHKro; Tue, 28 Apr 2020 19:37:46 +0200 (CEST) Received: from bluegene.passwd.hu (localhost [127.0.0.1]) by iq.passwd.hu (Postfix) with ESMTP id 666A4E40AC; Tue, 28 Apr 2020 19:37:45 +0200 (CEST) From: Marton Balint To: ffmpeg-devel@ffmpeg.org Date: Tue, 28 Apr 2020 19:37:23 +0200 Message-Id: <20200428173725.12482-6-cus@passwd.hu> X-Mailer: git-send-email 2.16.4 In-Reply-To: <20200428173725.12482-1-cus@passwd.hu> References: <20200428173725.12482-1-cus@passwd.hu> Subject: [FFmpeg-devel] [PATCH v4 6/8] avformat/audiointerleave: only keep the retime functionality of the audio interleaver X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: Marton Balint MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" And rename it to retimeinterleave, use the pcm_rechunk bitstream filter for rechunking. By seperating the two functions we hopefully get cleaner code. Signed-off-by: Marton Balint --- configure | 2 + libavformat/Makefile | 4 +- libavformat/audiointerleave.c | 148 --------------------- libavformat/gxfenc.c | 20 ++- libavformat/mxfenc.c | 19 +-- libavformat/retimeinterleave.c | 51 +++++++ .../{audiointerleave.h => retimeinterleave.h} | 31 ++--- 7 files changed, 87 insertions(+), 188 deletions(-) delete mode 100644 libavformat/audiointerleave.c create mode 100644 libavformat/retimeinterleave.c rename libavformat/{audiointerleave.h => retimeinterleave.h} (57%) diff --git a/configure b/configure index 080d93a129..e7162dbc56 100755 --- a/configure +++ b/configure @@ -2722,6 +2722,7 @@ fraps_decoder_select="bswapdsp huffman" g2m_decoder_deps="zlib" g2m_decoder_select="blockdsp idctdsp jpegtables" g729_decoder_select="audiodsp" +gxf_encoder_select="pcm_rechunk_bsf" h261_decoder_select="mpegvideo" h261_encoder_select="mpegvideoenc" h263_decoder_select="h263_parser h263dsp mpegvideo qpeldsp" @@ -2794,6 +2795,7 @@ mv30_decoder_select="aandcttables blockdsp" mvha_decoder_deps="zlib" mvha_decoder_select="llviddsp" mwsc_decoder_deps="zlib" +mxf_encoder_select="pcm_rechunk_bsf" mxpeg_decoder_select="mjpeg_decoder" nellymoser_decoder_select="mdct sinewin" nellymoser_encoder_select="audio_frame_queue mdct sinewin" diff --git a/libavformat/Makefile b/libavformat/Makefile index d4bed3c113..56ca55fbd5 100644 --- a/libavformat/Makefile +++ b/libavformat/Makefile @@ -205,7 +205,7 @@ OBJS-$(CONFIG_GIF_DEMUXER) += gifdec.o OBJS-$(CONFIG_GSM_DEMUXER) += gsmdec.o OBJS-$(CONFIG_GSM_MUXER) += rawenc.o OBJS-$(CONFIG_GXF_DEMUXER) += gxf.o -OBJS-$(CONFIG_GXF_MUXER) += gxfenc.o audiointerleave.o +OBJS-$(CONFIG_GXF_MUXER) += gxfenc.o retimeinterleave.o OBJS-$(CONFIG_G722_DEMUXER) += g722.o rawdec.o OBJS-$(CONFIG_G722_MUXER) += rawenc.o OBJS-$(CONFIG_G723_1_DEMUXER) += g723_1.o @@ -347,7 +347,7 @@ OBJS-$(CONFIG_MUSX_DEMUXER) += musx.o OBJS-$(CONFIG_MV_DEMUXER) += mvdec.o OBJS-$(CONFIG_MVI_DEMUXER) += mvi.o OBJS-$(CONFIG_MXF_DEMUXER) += mxfdec.o mxf.o -OBJS-$(CONFIG_MXF_MUXER) += mxfenc.o mxf.o audiointerleave.o avc.o +OBJS-$(CONFIG_MXF_MUXER) += mxfenc.o mxf.o retimeinterleave.o avc.o OBJS-$(CONFIG_MXG_DEMUXER) += mxg.o OBJS-$(CONFIG_NC_DEMUXER) += ncdec.o OBJS-$(CONFIG_NISTSPHERE_DEMUXER) += nistspheredec.o pcm.o diff --git a/libavformat/audiointerleave.c b/libavformat/audiointerleave.c deleted file mode 100644 index 36a3288242..0000000000 --- a/libavformat/audiointerleave.c +++ /dev/null @@ -1,148 +0,0 @@ -/* - * Audio Interleaving functions - * - * Copyright (c) 2009 Baptiste Coudurier - * - * This file is part of FFmpeg. - * - * FFmpeg is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * FFmpeg is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with FFmpeg; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA - */ - -#include "libavutil/fifo.h" -#include "libavutil/mathematics.h" -#include "avformat.h" -#include "audiointerleave.h" -#include "internal.h" - -void ff_audio_interleave_close(AVFormatContext *s) -{ - int i; - for (i = 0; i < s->nb_streams; i++) { - AVStream *st = s->streams[i]; - AudioInterleaveContext *aic = st->priv_data; - - if (aic && st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) - av_fifo_freep(&aic->fifo); - } -} - -int ff_audio_interleave_init(AVFormatContext *s, - const int samples_per_frame, - AVRational time_base) -{ - int i; - - if (!time_base.num) { - av_log(s, AV_LOG_ERROR, "timebase not set for audio interleave\n"); - return AVERROR(EINVAL); - } - for (i = 0; i < s->nb_streams; i++) { - AVStream *st = s->streams[i]; - AudioInterleaveContext *aic = st->priv_data; - - if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) { - int max_samples = samples_per_frame ? samples_per_frame : - av_rescale_rnd(st->codecpar->sample_rate, time_base.num, time_base.den, AV_ROUND_UP); - aic->sample_size = (st->codecpar->channels * - av_get_bits_per_sample(st->codecpar->codec_id)) / 8; - if (!aic->sample_size) { - av_log(s, AV_LOG_ERROR, "could not compute sample size\n"); - return AVERROR(EINVAL); - } - aic->samples_per_frame = samples_per_frame; - aic->time_base = time_base; - - if (!(aic->fifo = av_fifo_alloc_array(100, max_samples))) - return AVERROR(ENOMEM); - aic->fifo_size = 100 * max_samples; - } - } - - return 0; -} - -static int interleave_new_audio_packet(AVFormatContext *s, AVPacket *pkt, - int stream_index, int flush) -{ - AVStream *st = s->streams[stream_index]; - AudioInterleaveContext *aic = st->priv_data; - int ret; - int nb_samples = aic->samples_per_frame ? aic->samples_per_frame : - (av_rescale_q(aic->n + 1, av_make_q(st->codecpar->sample_rate, 1), av_inv_q(aic->time_base)) - aic->nb_samples); - int frame_size = nb_samples * aic->sample_size; - int size = FFMIN(av_fifo_size(aic->fifo), frame_size); - if (!size || (!flush && size == av_fifo_size(aic->fifo))) - return 0; - - ret = av_new_packet(pkt, frame_size); - if (ret < 0) - return ret; - av_fifo_generic_read(aic->fifo, pkt->data, size, NULL); - - if (size < pkt->size) - memset(pkt->data + size, 0, pkt->size - size); - - pkt->dts = pkt->pts = aic->dts; - pkt->duration = av_rescale_q(nb_samples, st->time_base, aic->time_base); - pkt->stream_index = stream_index; - aic->dts += pkt->duration; - aic->nb_samples += nb_samples; - aic->n++; - - return pkt->size; -} - -int ff_audio_rechunk_interleave(AVFormatContext *s, AVPacket *out, AVPacket *pkt, int flush, - int (*get_packet)(AVFormatContext *, AVPacket *, AVPacket *, int), - int (*compare_ts)(AVFormatContext *, const AVPacket *, const AVPacket *)) -{ - int i, ret; - - if (pkt) { - AVStream *st = s->streams[pkt->stream_index]; - AudioInterleaveContext *aic = st->priv_data; - if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) { - unsigned new_size = av_fifo_size(aic->fifo) + pkt->size; - if (new_size > aic->fifo_size) { - if (av_fifo_realloc2(aic->fifo, new_size) < 0) - return AVERROR(ENOMEM); - aic->fifo_size = new_size; - } - av_fifo_generic_write(aic->fifo, pkt->data, pkt->size, NULL); - } else { - // rewrite pts and dts to be decoded time line position - pkt->pts = pkt->dts = aic->dts; - aic->dts += pkt->duration; - if ((ret = ff_interleave_add_packet(s, pkt, compare_ts)) < 0) - return ret; - } - pkt = NULL; - } - - for (i = 0; i < s->nb_streams; i++) { - AVStream *st = s->streams[i]; - if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) { - AVPacket new_pkt; - while ((ret = interleave_new_audio_packet(s, &new_pkt, i, flush)) > 0) { - if ((ret = ff_interleave_add_packet(s, &new_pkt, compare_ts)) < 0) - return ret; - } - if (ret < 0) - return ret; - } - } - - return get_packet(s, out, NULL, flush); -} diff --git a/libavformat/gxfenc.c b/libavformat/gxfenc.c index e7536a6a7e..60468c36ce 100644 --- a/libavformat/gxfenc.c +++ b/libavformat/gxfenc.c @@ -27,8 +27,9 @@ #include "avformat.h" #include "internal.h" #include "gxf.h" -#include "audiointerleave.h" +#include "retimeinterleave.h" +#define GXF_SAMPLES_PER_FRAME 32768 #define GXF_AUDIO_PACKET_SIZE 65536 #define GXF_TIMECODE(c, d, h, m, s, f) \ @@ -44,7 +45,7 @@ typedef struct GXFTimecode{ } GXFTimecode; typedef struct GXFStreamContext { - AudioInterleaveContext aic; + RetimeInterleaveContext aic; uint32_t track_type; uint32_t sample_size; uint32_t sample_rate; @@ -663,8 +664,6 @@ static int gxf_write_umf_packet(AVFormatContext *s) return updatePacketSize(pb, pos); } -static const int GXF_samples_per_frame = 32768; - static void gxf_init_timecode_track(GXFStreamContext *sc, GXFStreamContext *vsc) { if (!vsc) @@ -736,6 +735,9 @@ static int gxf_write_header(AVFormatContext *s) av_log(s, AV_LOG_ERROR, "only mono tracks are allowed\n"); return -1; } + ret = ff_stream_add_bitstream_filter(st, "pcm_rechunk", "n="AV_STRINGIFY(GXF_SAMPLES_PER_FRAME)); + if (ret < 0) + return ret; sc->track_type = 2; sc->sample_rate = st->codecpar->sample_rate; avpriv_set_pts_info(st, 64, 1, sc->sample_rate); @@ -813,14 +815,12 @@ static int gxf_write_header(AVFormatContext *s) return -1; } } + ff_retime_interleave_init(&sc->aic, st->time_base); /* FIXME first 10 audio tracks are 0 to 9 next 22 are A to V */ sc->media_info = media_info<<8 | ('0'+tracks[media_info]++); sc->order = s->nb_streams - st->index; } - if (ff_audio_interleave_init(s, GXF_samples_per_frame, (AVRational){ 1, 48000 }) < 0) - return -1; - if (tcr && vsc) gxf_init_timecode(s, &gxf->tc, tcr->value, vsc->fields); @@ -877,8 +877,6 @@ static void gxf_deinit(AVFormatContext *s) { GXFContext *gxf = s->priv_data; - ff_audio_interleave_close(s); - av_freep(&gxf->flt_entries); av_freep(&gxf->map_offsets); } @@ -1016,8 +1014,8 @@ static int gxf_interleave_packet(AVFormatContext *s, AVPacket *out, AVPacket *pk { if (pkt && s->streams[pkt->stream_index]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) pkt->duration = 2; // enforce 2 fields - return ff_audio_rechunk_interleave(s, out, pkt, flush, - ff_interleave_packet_per_dts, gxf_compare_field_nb); + return ff_retime_interleave(s, out, pkt, flush, + ff_interleave_packet_per_dts, gxf_compare_field_nb); } AVOutputFormat ff_gxf_muxer = { diff --git a/libavformat/mxfenc.c b/libavformat/mxfenc.c index 23147e9b84..63a2799b08 100644 --- a/libavformat/mxfenc.c +++ b/libavformat/mxfenc.c @@ -52,7 +52,7 @@ #include "libavcodec/h264_ps.h" #include "libavcodec/golomb.h" #include "libavcodec/internal.h" -#include "audiointerleave.h" +#include "retimeinterleave.h" #include "avformat.h" #include "avio_internal.h" #include "internal.h" @@ -79,7 +79,7 @@ typedef struct MXFIndexEntry { } MXFIndexEntry; typedef struct MXFStreamContext { - AudioInterleaveContext aic; + RetimeInterleaveContext aic; UID track_essence_element_key; int index; ///< index in mxf_essence_container_uls table const UID *codec_ul; @@ -2538,6 +2538,7 @@ static int mxf_write_header(AVFormatContext *s) if (mxf->signal_standard >= 0) sc->signal_standard = mxf->signal_standard; } else if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) { + char bsf_arg[32]; if (st->codecpar->sample_rate != 48000) { av_log(s, AV_LOG_ERROR, "only 48khz is implemented\n"); return -1; @@ -2580,6 +2581,10 @@ static int mxf_write_header(AVFormatContext *s) av_rescale_rnd(st->codecpar->sample_rate, mxf->time_base.num, mxf->time_base.den, AV_ROUND_UP) * av_get_bits_per_sample(st->codecpar->codec_id) / 8; } + snprintf(bsf_arg, sizeof(bsf_arg), "r=%d/%d", mxf->tc.rate.num, mxf->tc.rate.den); + ret = ff_stream_add_bitstream_filter(st, "pcm_rechunk", bsf_arg); + if (ret < 0) + return ret; } else if (st->codecpar->codec_type == AVMEDIA_TYPE_DATA) { AVDictionaryEntry *e = av_dict_get(st->metadata, "data_type", NULL, 0); if (e && !strcmp(e->value, "vbi_vanc_smpte_436M")) { @@ -2593,6 +2598,7 @@ static int mxf_write_header(AVFormatContext *s) return -1; } } + ff_retime_interleave_init(&sc->aic, av_inv_q(mxf->tc.rate)); if (sc->index == -1) { sc->index = mxf_get_essence_container_ul_index(st->codecpar->codec_id); @@ -2646,9 +2652,6 @@ static int mxf_write_header(AVFormatContext *s) return AVERROR(ENOMEM); mxf->timecode_track->index = -1; - if (ff_audio_interleave_init(s, 0, av_inv_q(mxf->tc.rate)) < 0) - return -1; - return 0; } @@ -3010,8 +3013,6 @@ static void mxf_deinit(AVFormatContext *s) { MXFContext *mxf = s->priv_data; - ff_audio_interleave_close(s); - av_freep(&mxf->index_entries); av_freep(&mxf->body_partition_offset); if (mxf->timecode_track) { @@ -3086,8 +3087,8 @@ static int mxf_compare_timestamps(AVFormatContext *s, const AVPacket *next, static int mxf_interleave(AVFormatContext *s, AVPacket *out, AVPacket *pkt, int flush) { - return ff_audio_rechunk_interleave(s, out, pkt, flush, - mxf_interleave_get_packet, mxf_compare_timestamps); + return ff_retime_interleave(s, out, pkt, flush, + mxf_interleave_get_packet, mxf_compare_timestamps); } #define MXF_COMMON_OPTIONS \ diff --git a/libavformat/retimeinterleave.c b/libavformat/retimeinterleave.c new file mode 100644 index 0000000000..9f874e3626 --- /dev/null +++ b/libavformat/retimeinterleave.c @@ -0,0 +1,51 @@ +/* + * Retime Interleaving functions + * + * Copyright (c) 2009 Baptiste Coudurier + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "libavutil/mathematics.h" +#include "avformat.h" +#include "retimeinterleave.h" +#include "internal.h" + +void ff_retime_interleave_init(RetimeInterleaveContext *aic, AVRational time_base) +{ + aic->time_base = time_base; +} + +int ff_retime_interleave(AVFormatContext *s, AVPacket *out, AVPacket *pkt, int flush, + int (*get_packet)(AVFormatContext *, AVPacket *, AVPacket *, int), + int (*compare_ts)(AVFormatContext *, const AVPacket *, const AVPacket *)) +{ + int ret; + + if (pkt) { + AVStream *st = s->streams[pkt->stream_index]; + RetimeInterleaveContext *aic = st->priv_data; + pkt->duration = av_rescale_q(pkt->duration, st->time_base, aic->time_base); + // rewrite pts and dts to be decoded time line position + pkt->pts = pkt->dts = aic->dts; + aic->dts += pkt->duration; + if ((ret = ff_interleave_add_packet(s, pkt, compare_ts)) < 0) + return ret; + } + + return get_packet(s, out, NULL, flush); +} diff --git a/libavformat/audiointerleave.h b/libavformat/retimeinterleave.h similarity index 57% rename from libavformat/audiointerleave.h rename to libavformat/retimeinterleave.h index 0933310f4c..de0a7442b0 100644 --- a/libavformat/audiointerleave.h +++ b/libavformat/retimeinterleave.h @@ -20,36 +20,31 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ -#ifndef AVFORMAT_AUDIOINTERLEAVE_H -#define AVFORMAT_AUDIOINTERLEAVE_H +#ifndef AVFORMAT_RETIMEINTERLEAVE_H +#define AVFORMAT_RETIMEINTERLEAVE_H -#include "libavutil/fifo.h" #include "avformat.h" -typedef struct AudioInterleaveContext { - AVFifoBuffer *fifo; - unsigned fifo_size; ///< size of currently allocated FIFO - int64_t n; ///< number of generated packets - int64_t nb_samples; ///< number of generated samples +typedef struct RetimeInterleaveContext { uint64_t dts; ///< current dts - int sample_size; ///< size of one sample all channels included - int samples_per_frame; ///< samples per frame if fixed, 0 otherwise - AVRational time_base; ///< time base of output audio packets -} AudioInterleaveContext; + AVRational time_base; ///< time base of output packets +} RetimeInterleaveContext; -int ff_audio_interleave_init(AVFormatContext *s, const int samples_per_frame, AVRational time_base); -void ff_audio_interleave_close(AVFormatContext *s); +/** + * Init the retime interleave context + */ +void ff_retime_interleave_init(RetimeInterleaveContext *aic, AVRational time_base); /** - * Rechunk audio PCM packets per AudioInterleaveContext->samples_per_frame - * and interleave them correctly. - * The first element of AVStream->priv_data must be AudioInterleaveContext + * Retime packets per RetimeInterleaveContext->time_base and interleave them + * correctly. + * The first element of AVStream->priv_data must be RetimeInterleaveContext * when using this function. * * @param get_packet function will output a packet when streams are correctly interleaved. * @param compare_ts function will compare AVPackets and decide interleaving order. */ -int ff_audio_rechunk_interleave(AVFormatContext *s, AVPacket *out, AVPacket *pkt, int flush, +int ff_retime_interleave(AVFormatContext *s, AVPacket *out, AVPacket *pkt, int flush, int (*get_packet)(AVFormatContext *, AVPacket *, AVPacket *, int), int (*compare_ts)(AVFormatContext *, const AVPacket *, const AVPacket *)); From patchwork Tue Apr 28 17:37:24 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marton Balint X-Patchwork-Id: 19345 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id AA28744B549 for ; Tue, 28 Apr 2020 20:37:58 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 95F2A68C640; Tue, 28 Apr 2020 20:37:58 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from iq.passwd.hu (iq.passwd.hu [217.27.212.140]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 98AF968BE09 for ; Tue, 28 Apr 2020 20:37:50 +0300 (EEST) Received: from localhost (localhost [127.0.0.1]) by iq.passwd.hu (Postfix) with ESMTP id 3CD51E3547; Tue, 28 Apr 2020 19:37:50 +0200 (CEST) X-Virus-Scanned: amavisd-new at passwd.hu Received: from iq.passwd.hu ([127.0.0.1]) by localhost (iq.passwd.hu [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id Ari3LWyr1M60; Tue, 28 Apr 2020 19:37:48 +0200 (CEST) Received: from bluegene.passwd.hu (localhost [127.0.0.1]) by iq.passwd.hu (Postfix) with ESMTP id 9A01DE40A7; Tue, 28 Apr 2020 19:37:47 +0200 (CEST) From: Marton Balint To: ffmpeg-devel@ffmpeg.org Date: Tue, 28 Apr 2020 19:37:24 +0200 Message-Id: <20200428173725.12482-7-cus@passwd.hu> X-Mailer: git-send-email 2.16.4 In-Reply-To: <20200428173725.12482-1-cus@passwd.hu> References: <20200428173725.12482-1-cus@passwd.hu> Subject: [FFmpeg-devel] [PATCH v4 7/8] avformat: implement retiming directly in mxfenc and gxfenc X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: Marton Balint MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Generic retime functionality is replaced by a few lines of code directly in the muxers which used it, which seems a lot easier to understand and this way the retiming is not dependant of the input durations. Signed-off-by: Marton Balint --- libavformat/Makefile | 4 ++-- libavformat/gxfenc.c | 21 ++++++++++++++------- libavformat/mxfenc.c | 14 +++++++++----- 3 files changed, 25 insertions(+), 14 deletions(-) diff --git a/libavformat/Makefile b/libavformat/Makefile index 56ca55fbd5..0a2edffc86 100644 --- a/libavformat/Makefile +++ b/libavformat/Makefile @@ -205,7 +205,7 @@ OBJS-$(CONFIG_GIF_DEMUXER) += gifdec.o OBJS-$(CONFIG_GSM_DEMUXER) += gsmdec.o OBJS-$(CONFIG_GSM_MUXER) += rawenc.o OBJS-$(CONFIG_GXF_DEMUXER) += gxf.o -OBJS-$(CONFIG_GXF_MUXER) += gxfenc.o retimeinterleave.o +OBJS-$(CONFIG_GXF_MUXER) += gxfenc.o OBJS-$(CONFIG_G722_DEMUXER) += g722.o rawdec.o OBJS-$(CONFIG_G722_MUXER) += rawenc.o OBJS-$(CONFIG_G723_1_DEMUXER) += g723_1.o @@ -347,7 +347,7 @@ OBJS-$(CONFIG_MUSX_DEMUXER) += musx.o OBJS-$(CONFIG_MV_DEMUXER) += mvdec.o OBJS-$(CONFIG_MVI_DEMUXER) += mvi.o OBJS-$(CONFIG_MXF_DEMUXER) += mxfdec.o mxf.o -OBJS-$(CONFIG_MXF_MUXER) += mxfenc.o mxf.o retimeinterleave.o avc.o +OBJS-$(CONFIG_MXF_MUXER) += mxfenc.o mxf.o avc.o OBJS-$(CONFIG_MXG_DEMUXER) += mxg.o OBJS-$(CONFIG_NC_DEMUXER) += ncdec.o OBJS-$(CONFIG_NISTSPHERE_DEMUXER) += nistspheredec.o pcm.o diff --git a/libavformat/gxfenc.c b/libavformat/gxfenc.c index 60468c36ce..6d4df894f6 100644 --- a/libavformat/gxfenc.c +++ b/libavformat/gxfenc.c @@ -27,7 +27,6 @@ #include "avformat.h" #include "internal.h" #include "gxf.h" -#include "retimeinterleave.h" #define GXF_SAMPLES_PER_FRAME 32768 #define GXF_AUDIO_PACKET_SIZE 65536 @@ -45,7 +44,7 @@ typedef struct GXFTimecode{ } GXFTimecode; typedef struct GXFStreamContext { - RetimeInterleaveContext aic; + int64_t pkt_cnt; uint32_t track_type; uint32_t sample_size; uint32_t sample_rate; @@ -815,7 +814,6 @@ static int gxf_write_header(AVFormatContext *s) return -1; } } - ff_retime_interleave_init(&sc->aic, st->time_base); /* FIXME first 10 audio tracks are 0 to 9 next 22 are A to V */ sc->media_info = media_info<<8 | ('0'+tracks[media_info]++); sc->order = s->nb_streams - st->index; @@ -1012,10 +1010,19 @@ static int gxf_compare_field_nb(AVFormatContext *s, const AVPacket *next, static int gxf_interleave_packet(AVFormatContext *s, AVPacket *out, AVPacket *pkt, int flush) { - if (pkt && s->streams[pkt->stream_index]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) - pkt->duration = 2; // enforce 2 fields - return ff_retime_interleave(s, out, pkt, flush, - ff_interleave_packet_per_dts, gxf_compare_field_nb); + int ret; + if (pkt) { + AVStream *st = s->streams[pkt->stream_index]; + GXFStreamContext *sc = st->priv_data; + if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) + pkt->pts = pkt->dts = sc->pkt_cnt * 2; // enforce 2 fields + else + pkt->pts = pkt->dts = sc->pkt_cnt * GXF_SAMPLES_PER_FRAME; + sc->pkt_cnt++; + if ((ret = ff_interleave_add_packet(s, pkt, gxf_compare_field_nb)) < 0) + return ret; + } + return ff_interleave_packet_per_dts(s, out, NULL, flush); } AVOutputFormat ff_gxf_muxer = { diff --git a/libavformat/mxfenc.c b/libavformat/mxfenc.c index 63a2799b08..c3b6809e98 100644 --- a/libavformat/mxfenc.c +++ b/libavformat/mxfenc.c @@ -52,7 +52,6 @@ #include "libavcodec/h264_ps.h" #include "libavcodec/golomb.h" #include "libavcodec/internal.h" -#include "retimeinterleave.h" #include "avformat.h" #include "avio_internal.h" #include "internal.h" @@ -79,7 +78,7 @@ typedef struct MXFIndexEntry { } MXFIndexEntry; typedef struct MXFStreamContext { - RetimeInterleaveContext aic; + int64_t pkt_cnt; ///< pkt counter for muxed packets UID track_essence_element_key; int index; ///< index in mxf_essence_container_uls table const UID *codec_ul; @@ -2598,7 +2597,6 @@ static int mxf_write_header(AVFormatContext *s) return -1; } } - ff_retime_interleave_init(&sc->aic, av_inv_q(mxf->tc.rate)); if (sc->index == -1) { sc->index = mxf_get_essence_container_ul_index(st->codecpar->codec_id); @@ -3087,8 +3085,14 @@ static int mxf_compare_timestamps(AVFormatContext *s, const AVPacket *next, static int mxf_interleave(AVFormatContext *s, AVPacket *out, AVPacket *pkt, int flush) { - return ff_retime_interleave(s, out, pkt, flush, - mxf_interleave_get_packet, mxf_compare_timestamps); + int ret; + if (pkt) { + MXFStreamContext *sc = s->streams[pkt->stream_index]->priv_data; + pkt->pts = pkt->dts = sc->pkt_cnt++; + if ((ret = ff_interleave_add_packet(s, pkt, mxf_compare_timestamps)) < 0) + return ret; + } + return mxf_interleave_get_packet(s, out, NULL, flush); } #define MXF_COMMON_OPTIONS \ From patchwork Tue Apr 28 17:37:25 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marton Balint X-Patchwork-Id: 19343 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id E2A5444B549 for ; Tue, 28 Apr 2020 20:37:56 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id CDAF068C61A; Tue, 28 Apr 2020 20:37:56 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from iq.passwd.hu (iq.passwd.hu [217.27.212.140]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 59DD768C5CF for ; Tue, 28 Apr 2020 20:37:52 +0300 (EEST) Received: from localhost (localhost [127.0.0.1]) by iq.passwd.hu (Postfix) with ESMTP id 3D2FAE3E28; Tue, 28 Apr 2020 19:37:52 +0200 (CEST) X-Virus-Scanned: amavisd-new at passwd.hu Received: from iq.passwd.hu ([127.0.0.1]) by localhost (iq.passwd.hu [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id ZKU579KPz703; Tue, 28 Apr 2020 19:37:50 +0200 (CEST) Received: from bluegene.passwd.hu (localhost [127.0.0.1]) by iq.passwd.hu (Postfix) with ESMTP id C245FE40AC; Tue, 28 Apr 2020 19:37:48 +0200 (CEST) From: Marton Balint To: ffmpeg-devel@ffmpeg.org Date: Tue, 28 Apr 2020 19:37:25 +0200 Message-Id: <20200428173725.12482-8-cus@passwd.hu> X-Mailer: git-send-email 2.16.4 In-Reply-To: <20200428173725.12482-1-cus@passwd.hu> References: <20200428173725.12482-1-cus@passwd.hu> Subject: [FFmpeg-devel] [PATCH v4 8/8] avformat: remove retimeinterleave X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: Marton Balint MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" It is not used by anything anymore. Signed-off-by: Marton Balint --- libavformat/retimeinterleave.c | 51 ------------------------------------------ libavformat/retimeinterleave.h | 51 ------------------------------------------ 2 files changed, 102 deletions(-) delete mode 100644 libavformat/retimeinterleave.c delete mode 100644 libavformat/retimeinterleave.h diff --git a/libavformat/retimeinterleave.c b/libavformat/retimeinterleave.c deleted file mode 100644 index 9f874e3626..0000000000 --- a/libavformat/retimeinterleave.c +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Retime Interleaving functions - * - * Copyright (c) 2009 Baptiste Coudurier - * - * This file is part of FFmpeg. - * - * FFmpeg is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * FFmpeg is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with FFmpeg; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA - */ - -#include "libavutil/mathematics.h" -#include "avformat.h" -#include "retimeinterleave.h" -#include "internal.h" - -void ff_retime_interleave_init(RetimeInterleaveContext *aic, AVRational time_base) -{ - aic->time_base = time_base; -} - -int ff_retime_interleave(AVFormatContext *s, AVPacket *out, AVPacket *pkt, int flush, - int (*get_packet)(AVFormatContext *, AVPacket *, AVPacket *, int), - int (*compare_ts)(AVFormatContext *, const AVPacket *, const AVPacket *)) -{ - int ret; - - if (pkt) { - AVStream *st = s->streams[pkt->stream_index]; - RetimeInterleaveContext *aic = st->priv_data; - pkt->duration = av_rescale_q(pkt->duration, st->time_base, aic->time_base); - // rewrite pts and dts to be decoded time line position - pkt->pts = pkt->dts = aic->dts; - aic->dts += pkt->duration; - if ((ret = ff_interleave_add_packet(s, pkt, compare_ts)) < 0) - return ret; - } - - return get_packet(s, out, NULL, flush); -} diff --git a/libavformat/retimeinterleave.h b/libavformat/retimeinterleave.h deleted file mode 100644 index de0a7442b0..0000000000 --- a/libavformat/retimeinterleave.h +++ /dev/null @@ -1,51 +0,0 @@ -/* - * audio interleaving prototypes and declarations - * - * Copyright (c) 2009 Baptiste Coudurier - * - * This file is part of FFmpeg. - * - * FFmpeg is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * FFmpeg is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with FFmpeg; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA - */ - -#ifndef AVFORMAT_RETIMEINTERLEAVE_H -#define AVFORMAT_RETIMEINTERLEAVE_H - -#include "avformat.h" - -typedef struct RetimeInterleaveContext { - uint64_t dts; ///< current dts - AVRational time_base; ///< time base of output packets -} RetimeInterleaveContext; - -/** - * Init the retime interleave context - */ -void ff_retime_interleave_init(RetimeInterleaveContext *aic, AVRational time_base); - -/** - * Retime packets per RetimeInterleaveContext->time_base and interleave them - * correctly. - * The first element of AVStream->priv_data must be RetimeInterleaveContext - * when using this function. - * - * @param get_packet function will output a packet when streams are correctly interleaved. - * @param compare_ts function will compare AVPackets and decide interleaving order. - */ -int ff_retime_interleave(AVFormatContext *s, AVPacket *out, AVPacket *pkt, int flush, - int (*get_packet)(AVFormatContext *, AVPacket *, AVPacket *, int), - int (*compare_ts)(AVFormatContext *, const AVPacket *, const AVPacket *)); - -#endif /* AVFORMAT_AUDIOINTERLEAVE_H */