From patchwork Sat Apr 18 19:18:42 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marton Balint X-Patchwork-Id: 19061 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 85D5544B412 for ; Sat, 18 Apr 2020 22:19:01 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 5E85968B714; Sat, 18 Apr 2020 22:19:01 +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 D0A7568B6AA for ; Sat, 18 Apr 2020 22:18:54 +0300 (EEST) Received: from localhost (localhost [127.0.0.1]) by iq.passwd.hu (Postfix) with ESMTP id B7F10E3E0B; Sat, 18 Apr 2020 21:18:54 +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 g7au9woZWzFq; Sat, 18 Apr 2020 21:18:52 +0200 (CEST) Received: from bluegene.passwd.hu (localhost [127.0.0.1]) by iq.passwd.hu (Postfix) with ESMTP id B1E21E3DD6; Sat, 18 Apr 2020 21:18:51 +0200 (CEST) From: Marton Balint To: ffmpeg-devel@ffmpeg.org Date: Sat, 18 Apr 2020 21:18:42 +0200 Message-Id: <20200418191847.25815-1-cus@passwd.hu> X-Mailer: git-send-email 2.16.4 In-Reply-To: References: Subject: [FFmpeg-devel] [PATCH v2 1/6] avformat: only allow a single bitstream filter when muxing 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" Current muxers only use a single bitstream filter, so there is no need to maintain code which operates on a list of bitstream filters. When multiple bitstream filters are needed muxers can simply use a list bitstream filter. If there is a use case in the future when different bitstream filters should be added at subsequent packets then a new API possibly involving reconfiguring the list bistream filter can be added knowing the exact requirements. Signed-off-by: Marton Balint --- libavformat/dashenc.c | 6 ++---- libavformat/internal.h | 5 ++--- libavformat/mux.c | 6 +++--- libavformat/segment.c | 6 ++---- libavformat/utils.c | 27 +++++++++------------------ 5 files changed, 18 insertions(+), 32 deletions(-) diff --git a/libavformat/dashenc.c b/libavformat/dashenc.c index 5a8cff4034..b977761a00 100644 --- a/libavformat/dashenc.c +++ b/libavformat/dashenc.c @@ -2307,10 +2307,8 @@ static int dash_check_bitstream(struct AVFormatContext *s, const AVPacket *avpkt if (ret == 1) { AVStream *st = s->streams[avpkt->stream_index]; AVStream *ost = oc->streams[0]; - st->internal->bsfcs = ost->internal->bsfcs; - st->internal->nb_bsfcs = ost->internal->nb_bsfcs; - ost->internal->bsfcs = NULL; - ost->internal->nb_bsfcs = 0; + st->internal->bsfc = ost->internal->bsfc; + ost->internal->bsfc = NULL; } return ret; } diff --git a/libavformat/internal.h b/libavformat/internal.h index 7e4284b217..cafb4a9686 100644 --- a/libavformat/internal.h +++ b/libavformat/internal.h @@ -152,12 +152,11 @@ struct AVStreamInternal { int reorder; /** - * bitstream filters to run on stream + * bitstream filter to run on stream * - encoding: Set by muxer using ff_stream_add_bitstream_filter * - decoding: unused */ - AVBSFContext **bsfcs; - int nb_bsfcs; + AVBSFContext *bsfc; /** * Whether or not check_bitstream should still be run on each packet diff --git a/libavformat/mux.c b/libavformat/mux.c index 3d63d59faf..5209c84f40 100644 --- a/libavformat/mux.c +++ b/libavformat/mux.c @@ -824,7 +824,7 @@ static int prepare_input_packet(AVFormatContext *s, AVPacket *pkt) static int do_packet_auto_bsf(AVFormatContext *s, AVPacket *pkt) { AVStream *st = s->streams[pkt->stream_index]; - int i, ret; + int ret; if (!(s->flags & AVFMT_FLAG_AUTO_BSF)) return 1; @@ -838,8 +838,8 @@ static int do_packet_auto_bsf(AVFormatContext *s, AVPacket *pkt) { } } - for (i = 0; i < st->internal->nb_bsfcs; i++) { - AVBSFContext *ctx = st->internal->bsfcs[i]; + 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) { diff --git a/libavformat/segment.c b/libavformat/segment.c index 60b72b7d15..32c09827eb 100644 --- a/libavformat/segment.c +++ b/libavformat/segment.c @@ -1034,10 +1034,8 @@ static int seg_check_bitstream(struct AVFormatContext *s, const AVPacket *pkt) if (ret == 1) { AVStream *st = s->streams[pkt->stream_index]; AVStream *ost = oc->streams[pkt->stream_index]; - st->internal->bsfcs = ost->internal->bsfcs; - st->internal->nb_bsfcs = ost->internal->nb_bsfcs; - ost->internal->bsfcs = NULL; - ost->internal->nb_bsfcs = 0; + st->internal->bsfc = ost->internal->bsfc; + ost->internal->bsfc = NULL; } return ret; } diff --git a/libavformat/utils.c b/libavformat/utils.c index a58e47fabc..eff73252ec 100644 --- a/libavformat/utils.c +++ b/libavformat/utils.c @@ -4410,10 +4410,7 @@ static void free_stream(AVStream **pst) if (st->internal) { avcodec_free_context(&st->internal->avctx); - for (i = 0; i < st->internal->nb_bsfcs; i++) { - av_bsf_free(&st->internal->bsfcs[i]); - av_freep(&st->internal->bsfcs); - } + av_bsf_free(&st->internal->bsfc); av_freep(&st->internal->priv_pts); av_bsf_free(&st->internal->extract_extradata.bsf); av_packet_free(&st->internal->extract_extradata.pkt); @@ -5574,7 +5571,11 @@ int ff_stream_add_bitstream_filter(AVStream *st, const char *name, const char *a int ret; const AVBitStreamFilter *bsf; AVBSFContext *bsfc; - AVCodecParameters *in_par; + + if (st->internal->bsfc) { + av_log(NULL, AV_LOG_ERROR, "A bitstream filter is already specified for stream %d\n", st->index); + return AVERROR(EINVAL); + } if (!(bsf = av_bsf_get_by_name(name))) { av_log(NULL, AV_LOG_ERROR, "Unknown bitstream filter '%s'\n", name); @@ -5584,15 +5585,8 @@ int ff_stream_add_bitstream_filter(AVStream *st, const char *name, const char *a if ((ret = av_bsf_alloc(bsf, &bsfc)) < 0) return ret; - if (st->internal->nb_bsfcs) { - in_par = st->internal->bsfcs[st->internal->nb_bsfcs - 1]->par_out; - bsfc->time_base_in = st->internal->bsfcs[st->internal->nb_bsfcs - 1]->time_base_out; - } else { - in_par = st->codecpar; - bsfc->time_base_in = st->time_base; - } - - if ((ret = avcodec_parameters_copy(bsfc->par_in, in_par)) < 0) { + bsfc->time_base_in = st->time_base; + if ((ret = avcodec_parameters_copy(bsfc->par_in, st->codecpar)) < 0) { av_bsf_free(&bsfc); return ret; } @@ -5615,10 +5609,7 @@ int ff_stream_add_bitstream_filter(AVStream *st, const char *name, const char *a return ret; } - if ((ret = av_dynarray_add_nofree(&st->internal->bsfcs, &st->internal->nb_bsfcs, bsfc))) { - av_bsf_free(&bsfc); - return ret; - } + st->internal->bsfc = bsfc; av_log(NULL, AV_LOG_VERBOSE, "Automatically inserted bitstream filter '%s'; args='%s'\n", From patchwork Sat Apr 18 19:18:43 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marton Balint X-Patchwork-Id: 19062 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 8C6EA44B412 for ; Sat, 18 Apr 2020 22:19:02 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 79B0C68B738; Sat, 18 Apr 2020 22:19:02 +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 6FB6168B6AA for ; Sat, 18 Apr 2020 22:18:55 +0300 (EEST) Received: from localhost (localhost [127.0.0.1]) by iq.passwd.hu (Postfix) with ESMTP id 590C0E3DD6; Sat, 18 Apr 2020 21:18:55 +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 3eKG6Re8xiOf; Sat, 18 Apr 2020 21:18:54 +0200 (CEST) Received: from bluegene.passwd.hu (localhost [127.0.0.1]) by iq.passwd.hu (Postfix) with ESMTP id E9C68E3DDF; Sat, 18 Apr 2020 21:18:53 +0200 (CEST) From: Marton Balint To: ffmpeg-devel@ffmpeg.org Date: Sat, 18 Apr 2020 21:18:43 +0200 Message-Id: <20200418191847.25815-2-cus@passwd.hu> X-Mailer: git-send-email 2.16.4 In-Reply-To: <20200418191847.25815-1-cus@passwd.hu> References: <20200418191847.25815-1-cus@passwd.hu> Subject: [FFmpeg-devel] [PATCH v2 2/6] 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 5209c84f40..90faf51768 100644 --- a/libavformat/mux.c +++ b/libavformat/mux.c @@ -1195,6 +1195,25 @@ static int interleave_packet(AVFormatContext *s, AVPacket *out, AVPacket *in, in return ff_interleave_packet_per_dts(s, out, in, flush); } +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_interleaved_write_frame(AVFormatContext *s, AVPacket *pkt) { int ret, flush = 0; @@ -1229,22 +1248,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; @@ -1254,23 +1259,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 Sat Apr 18 19:18:44 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marton Balint X-Patchwork-Id: 19063 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 745BA44B412 for ; Sat, 18 Apr 2020 22:19:05 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 5A24B68B755; Sat, 18 Apr 2020 22:19:05 +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 80A5C68B700 for ; Sat, 18 Apr 2020 22:18:58 +0300 (EEST) Received: from localhost (localhost [127.0.0.1]) by iq.passwd.hu (Postfix) with ESMTP id 2FCD9E3F99; Sat, 18 Apr 2020 21:18:58 +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 ek13FC-pNeKg; Sat, 18 Apr 2020 21:18:56 +0200 (CEST) Received: from bluegene.passwd.hu (localhost [127.0.0.1]) by iq.passwd.hu (Postfix) with ESMTP id EE3D7E3DDF; Sat, 18 Apr 2020 21:18:55 +0200 (CEST) From: Marton Balint To: ffmpeg-devel@ffmpeg.org Date: Sat, 18 Apr 2020 21:18:44 +0200 Message-Id: <20200418191847.25815-3-cus@passwd.hu> X-Mailer: git-send-email 2.16.4 In-Reply-To: <20200418191847.25815-1-cus@passwd.hu> References: <20200418191847.25815-1-cus@passwd.hu> Subject: [FFmpeg-devel] [PATCH v2 3/6] 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 | 89 +++++++++++++++++++++++++++---------------------------- 1 file changed, 43 insertions(+), 46 deletions(-) diff --git a/libavformat/mux.c b/libavformat/mux.c index 90faf51768..4118d221e0 100644 --- a/libavformat/mux.c +++ b/libavformat/mux.c @@ -865,6 +865,43 @@ static int do_packet_auto_bsf(AVFormatContext *s, AVPacket *pkt) { return 1; } +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; @@ -903,22 +940,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 @@ -1216,43 +1238,18 @@ static int interleaved_write_packet(AVFormatContext *s, AVPacket *pkt, int flush 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 Sat Apr 18 19:18:45 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marton Balint X-Patchwork-Id: 19064 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 4F81144B412 for ; Sat, 18 Apr 2020 22:19:08 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 3724E68B7A7; Sat, 18 Apr 2020 22:19:08 +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 01CEA68B6D7 for ; Sat, 18 Apr 2020 22:19:00 +0300 (EEST) Received: from localhost (localhost [127.0.0.1]) by iq.passwd.hu (Postfix) with ESMTP id AECBEE3FD3; Sat, 18 Apr 2020 21:18:59 +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 jWZLRj331NNd; Sat, 18 Apr 2020 21:18:58 +0200 (CEST) Received: from bluegene.passwd.hu (localhost [127.0.0.1]) by iq.passwd.hu (Postfix) with ESMTP id 01E13E3DE5; Sat, 18 Apr 2020 21:18:57 +0200 (CEST) From: Marton Balint To: ffmpeg-devel@ffmpeg.org Date: Sat, 18 Apr 2020 21:18:45 +0200 Message-Id: <20200418191847.25815-4-cus@passwd.hu> X-Mailer: git-send-email 2.16.4 In-Reply-To: <20200418191847.25815-1-cus@passwd.hu> References: <20200418191847.25815-1-cus@passwd.hu> Subject: [FFmpeg-devel] [PATCH v2 4/6] 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. Signed-off-by: Marton Balint --- libavformat/mux.c | 91 ++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 57 insertions(+), 34 deletions(-) diff --git a/libavformat/mux.c b/libavformat/mux.c index 4118d221e0..c2b6d4461e 100644 --- a/libavformat/mux.c +++ b/libavformat/mux.c @@ -822,14 +822,13 @@ 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]; +static int need_auto_bsf(AVFormatContext *s, AVStream *st, AVPacket *pkt) { int ret; if (!(s->flags & AVFMT_FLAG_AUTO_BSF)) - return 1; + return 0; - if (s->oformat->check_bitstream) { + if (pkt && s->oformat->check_bitstream) { if (!st->internal->bitstream_checked) { if ((ret = s->oformat->check_bitstream(s, pkt)) < 0) return ret; @@ -838,31 +837,7 @@ 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; + return !!st->internal->bsfc; } static int interleaved_write_packet(AVFormatContext *s, AVPacket *pkt, int flush); @@ -889,17 +864,56 @@ 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; + AVPacket opkt = {0}; + 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, &opkt); + if (ret < 0) { + if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) + ret = 0; + if (ret < 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(&opkt, bsfc->time_base_out, st->time_base); + ret = write_packet_common(s, st, &opkt, interleaved); + if (!interleaved) // write_packet_common already unrefed opkt for interleaved + av_packet_unref(&opkt); + } 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 = need_auto_bsf(s, st, pkt); + if (ret < 0) return ret; - return write_packet_common(s, st, pkt, interleaved); + if (ret) { + 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) @@ -1254,9 +1268,18 @@ int av_interleaved_write_frame(AVFormatContext *s, AVPacket *pkt) int av_write_trailer(AVFormatContext *s) { - int ret, i; + int i, ret1, ret = 0; - ret = interleaved_write_packet(s, NULL, 1); + for (i = 0; i < s->nb_streams; i++) { + if (need_auto_bsf(s, s->streams[i], NULL)) { + ret1 = write_packets_from_bsfs(s, s->streams[i], NULL, 1/*interleaved*/); + 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 Sat Apr 18 19:18:46 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marton Balint X-Patchwork-Id: 19066 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 D759644B412 for ; Sat, 18 Apr 2020 22:19:11 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id C0F5668B7DF; Sat, 18 Apr 2020 22:19:11 +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 3BECB68B719 for ; Sat, 18 Apr 2020 22:19:04 +0300 (EEST) Received: from localhost (localhost [127.0.0.1]) by iq.passwd.hu (Postfix) with ESMTP id 28B0AE3AD9; Sat, 18 Apr 2020 21:19:04 +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 1e_kxwYHzmjA; Sat, 18 Apr 2020 21:19:02 +0200 (CEST) Received: from bluegene.passwd.hu (localhost [127.0.0.1]) by iq.passwd.hu (Postfix) with ESMTP id A942DE3FAD; Sat, 18 Apr 2020 21:18:59 +0200 (CEST) From: Marton Balint To: ffmpeg-devel@ffmpeg.org Date: Sat, 18 Apr 2020 21:18:46 +0200 Message-Id: <20200418191847.25815-5-cus@passwd.hu> X-Mailer: git-send-email 2.16.4 In-Reply-To: <20200418191847.25815-1-cus@passwd.hu> References: <20200418191847.25815-1-cus@passwd.hu> Subject: [FFmpeg-devel] [PATCH v2 5/6] 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 | 204 +++++++++++++++++++++++++++++++++++++++++ libavcodec/version.h | 2 +- 6 files changed, 238 insertions(+), 1 deletion(-) create mode 100644 libavcodec/pcm_rechunk_bsf.c diff --git a/Changelog b/Changelog index d9fcd8bb0a..6b0c911279 100644 --- a/Changelog +++ b/Changelog @@ -59,6 +59,7 @@ version : - mv30 decoder - Expanded styling support for 3GPP Timed Text Subtitles (movtext) - WebP parser +- pcm_rechunk bitstream filter version 4.2: diff --git a/doc/bitstream_filters.texi b/doc/bitstream_filters.texi index 8fe5b3ad75..70c276feed 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 as 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 88944d9a3a..35968bdaf7 100644 --- a/libavcodec/Makefile +++ b/libavcodec/Makefile @@ -1115,6 +1115,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..ca5c72c304 --- /dev/null +++ b/libavcodec/pcm_rechunk_bsf.c @@ -0,0 +1,204 @@ +/* + * 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/mem.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; + int64_t dts; +} 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; + + 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) + 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; + s->dts = 0; +} + +static int send_packet(PCMContext *s, int nb_samples, AVPacket *pkt) +{ + pkt->dts = pkt->pts = s->dts; + pkt->duration = nb_samples; + s->dts += nb_samples; + s->n++; + return 0; +} + +static int rechunk_filter(AVBSFContext *ctx, AVPacket *pkt) +{ + PCMContext *s = ctx->priv_data; + AVRational sr = av_make_q(ctx->par_in->sample_rate, 1); + int nb_samples = s->frame_rate.num ? (av_rescale_q(s->n + 1, sr, s->frame_rate) - s->dts) : s->nb_out_samples; + 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; + s->in_pkt->size -= drain; + s->in_pkt->data += drain; + if (s->out_pkt->size == data_size) { + av_packet_move_ref(pkt, s->out_pkt); + if (!s->in_pkt->size) + av_packet_unref(s->in_pkt); + return send_packet(s, nb_samples, pkt); + } + av_packet_unref(s->in_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; + s->in_pkt->size -= data_size; + s->in_pkt->data += data_size; + 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); + } + } 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 8cff2e855b..ad85fb15e5 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 80 +#define LIBAVCODEC_VERSION_MINOR 81 #define LIBAVCODEC_VERSION_MICRO 100 #define LIBAVCODEC_VERSION_INT AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \ From patchwork Sat Apr 18 19:18:47 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marton Balint X-Patchwork-Id: 19065 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 0B19E44B412 for ; Sat, 18 Apr 2020 22:19:11 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id EAC9768B7BE; Sat, 18 Apr 2020 22:19:10 +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 0D30F68B718 for ; Sat, 18 Apr 2020 22:19:08 +0300 (EEST) Received: from localhost (localhost [127.0.0.1]) by iq.passwd.hu (Postfix) with ESMTP id D1129E3A83; Sat, 18 Apr 2020 21:19:07 +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 Cp5oAiCpHIRf; Sat, 18 Apr 2020 21:19:05 +0200 (CEST) Received: from bluegene.passwd.hu (localhost [127.0.0.1]) by iq.passwd.hu (Postfix) with ESMTP id A8463E3A76; Sat, 18 Apr 2020 21:19:02 +0200 (CEST) From: Marton Balint To: ffmpeg-devel@ffmpeg.org Date: Sat, 18 Apr 2020 21:18:47 +0200 Message-Id: <20200418191847.25815-6-cus@passwd.hu> X-Mailer: git-send-email 2.16.4 In-Reply-To: <20200418191847.25815-1-cus@passwd.hu> References: <20200418191847.25815-1-cus@passwd.hu> Subject: [FFmpeg-devel] [PATCH v2 6/6] 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/gxfenc.c | 29 ++++++++---- libavformat/mux.c | 1 - libavformat/mxfenc.c | 32 ++++++++++---- libavformat/retimeinterleave.c | 51 ++++++++++++++++++++++ .../{audiointerleave.h => retimeinterleave.h} | 31 ++++++------- libavformat/utils.c | 1 - 8 files changed, 111 insertions(+), 40 deletions(-) create mode 100644 libavformat/retimeinterleave.c rename libavformat/{audiointerleave.h => retimeinterleave.h} (57%) diff --git a/configure b/configure index 4f285f0074..f5a84c31bd 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/gxfenc.c b/libavformat/gxfenc.c index e7536a6a7e..e95ae99cba 100644 --- a/libavformat/gxfenc.c +++ b/libavformat/gxfenc.c @@ -27,7 +27,7 @@ #include "avformat.h" #include "internal.h" #include "gxf.h" -#include "audiointerleave.h" +#include "retimeinterleave.h" #define GXF_AUDIO_PACKET_SIZE 65536 @@ -44,7 +44,7 @@ typedef struct GXFTimecode{ } GXFTimecode; typedef struct GXFStreamContext { - AudioInterleaveContext aic; + RetimeInterleaveContext aic; uint32_t track_type; uint32_t sample_size; uint32_t sample_rate; @@ -813,14 +813,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 +875,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 +1012,22 @@ 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); +} + +static int gxf_check_bitstream(struct AVFormatContext *s, const AVPacket *pkt) +{ + int ret = 1; + AVStream *st = s->streams[pkt->stream_index]; + + if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) { + char arg[32]; + snprintf(arg, sizeof(arg), "n=%d", GXF_samples_per_frame); + ret = ff_stream_add_bitstream_filter(st, "pcm_rechunk", arg); + } + + return ret; } AVOutputFormat ff_gxf_muxer = { @@ -1031,5 +1041,6 @@ AVOutputFormat ff_gxf_muxer = { .write_packet = gxf_write_packet, .write_trailer = gxf_write_trailer, .deinit = gxf_deinit, + .check_bitstream = gxf_check_bitstream, .interleave_packet = gxf_interleave_packet, }; diff --git a/libavformat/mux.c b/libavformat/mux.c index c2b6d4461e..b189450dd7 100644 --- a/libavformat/mux.c +++ b/libavformat/mux.c @@ -37,7 +37,6 @@ #include "libavutil/parseutils.h" #include "libavutil/time.h" #include "riff.h" -#include "audiointerleave.h" #include "url.h" #include #if CONFIG_NETWORK diff --git a/libavformat/mxfenc.c b/libavformat/mxfenc.c index bfce531f54..d381821116 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; @@ -2593,6 +2593,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 +2647,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 +3008,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) { @@ -3087,8 +3083,23 @@ 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); +} + +static int mxf_check_bitstream(struct AVFormatContext *s, const AVPacket *pkt) +{ + int ret = 1; + AVStream *st = s->streams[pkt->stream_index]; + MXFStreamContext *sc = st->priv_data; + + if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) { + char arg[32]; + snprintf(arg, sizeof(arg), "r=%d/%d", sc->aic.time_base.den, sc->aic.time_base.num); + ret = ff_stream_add_bitstream_filter(st, "pcm_rechunk", arg); + } + + return ret; } #define MXF_COMMON_OPTIONS \ @@ -3171,6 +3182,7 @@ AVOutputFormat ff_mxf_muxer = { .deinit = mxf_deinit, .flags = AVFMT_NOTIMESTAMPS, .interleave_packet = mxf_interleave, + .check_bitstream = mxf_check_bitstream, .priv_class = &mxf_muxer_class, }; @@ -3187,6 +3199,7 @@ AVOutputFormat ff_mxf_d10_muxer = { .deinit = mxf_deinit, .flags = AVFMT_NOTIMESTAMPS, .interleave_packet = mxf_interleave, + .check_bitstream = mxf_check_bitstream, .priv_class = &mxf_d10_muxer_class, }; @@ -3204,5 +3217,6 @@ AVOutputFormat ff_mxf_opatom_muxer = { .deinit = mxf_deinit, .flags = AVFMT_NOTIMESTAMPS, .interleave_packet = mxf_interleave, + .check_bitstream = mxf_check_bitstream, .priv_class = &mxf_opatom_muxer_class, }; 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 *)); diff --git a/libavformat/utils.c b/libavformat/utils.c index eff73252ec..25ac5f9446 100644 --- a/libavformat/utils.c +++ b/libavformat/utils.c @@ -41,7 +41,6 @@ #include "libavcodec/internal.h" #include "libavcodec/raw.h" -#include "audiointerleave.h" #include "avformat.h" #include "avio_internal.h" #include "id3v2.h"