From patchwork Fri Jan 24 22:48:33 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 17526 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 7391A449962 for ; Sat, 25 Jan 2020 00:49:00 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 5EE3668B0BE; Sat, 25 Jan 2020 00:49:00 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wr1-f66.google.com (mail-wr1-f66.google.com [209.85.221.66]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id D1F2268B06D for ; Sat, 25 Jan 2020 00:48:52 +0200 (EET) Received: by mail-wr1-f66.google.com with SMTP id q6so3893307wro.9 for ; Fri, 24 Jan 2020 14:48:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=sczkhvkESylyMVdYwbfdAK+2vxqhZV8kVx7OuXQggzs=; b=XACh7w4O77P1yDyd8B3ewsXMRmjqR7k5zFGQfcLHKtRtHlaW2QJ4wleLWeTBr0BUZb euxyyX1JImgxjemtyn9EG3bF6RhURVK+I5AnpqOLn+iVrgbivOo2pHmR1C3y6aaJ17Y+ OADAwuL7qWzpigoHJB0TcnO8ezRcbY97JEKEvQvO1CyP3l51RIg5mZS1xw9IdKY+ovvb CP/S9J2/QyHfQAU386SidoB/nQk6h6i3BYgq2F1U6pUWfLb7teuZv82MQAgdhVlJaqeW 8vyK5O0Al6MiKEmvJMopRWi5wpjlGXPJ40Srq3KZESMbUNVkmN6+dOx+UECdg1QzPiEi 4VxQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=sczkhvkESylyMVdYwbfdAK+2vxqhZV8kVx7OuXQggzs=; b=kId9J+fBnHrlRoA7CwXxV0nZkbGpuvc1CPovSMG7kYpOsW/CQ12M8+r5UBlCzhtLYG g9VIr0fq03N9bJvgRYorVHFpNPLOMp2VgeEehwwTqVcjAZ3olX7fj2jvtgDaLDHh7b/C n1sbePIZIpBpfDmkd0LWythX2mexPvjxdHMj6QaWwpkdC05uhnl261hUog6BAqi5DAR6 cAjnIRVXBgTV+k4QUDybilfHmk2p4YSPP8juZi4xwogXRXJXZsoVfZUJ9P5bDR6mR0iG MUTGn+6rz2wOenPy9pD8wTVmztS8xZpTqDkDbOlkFDK8PWIwk/8E6lqZBfz/hAhSxx9q /GRw== X-Gm-Message-State: APjAAAUguJNLTtNwi7lZ99n+xagX/Il7a0mAR63xl9fPaG2o+RRoDh2t SxZwz/1awdq4Vj/PTMlDXBdmTo/G X-Google-Smtp-Source: APXvYqxf4SV1dju213ZMxoUfoZMmESKXhWZNAJJHQn4XLw4BAtvMgFoneYTRy9hxRhzKX0Viqjdq1Q== X-Received: by 2002:adf:814c:: with SMTP id 70mr7344604wrm.157.1579906132067; Fri, 24 Jan 2020 14:48:52 -0800 (PST) Received: from sblaptop.fritz.box (ipbcc08bbf.dynamic.kabel-deutschland.de. [188.192.139.191]) by smtp.gmail.com with ESMTPSA id z25sm8977594wmf.14.2020.01.24.14.48.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 Jan 2020 14:48:51 -0800 (PST) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Fri, 24 Jan 2020 23:48:33 +0100 Message-Id: <20200124224833.17579-6-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200123160832.2020-1-andreas.rheinhardt@gmail.com> References: <20200123160832.2020-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 12/12] avformat/av1: Avoid allocation + copying when filtering OBUs 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: Andreas Rheinhardt Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Certain types of OBUs are stripped away before muxing into Matroska and ISOBMFF; there are two functions to do this: One that outputs by directly writing in an AVIOContext and one that returns a freshly allocated buffer with the units not stripped away copied into it. The latter option is bad for performance, especially when the input does already not contain any of the units intended to be stripped away (this covers typical remuxing scenarios). Therefore this commit changes this by avoiding allocating and copying when possible; it is possible if the OBUs to be retained are consecutively in the input buffer (without an OBU to be discarded between them). In this case, the caller receives the offset as well as the length of the part of the buffer that contains the units to be kept. This also avoids copying when e.g. the only unit to be discarded is a temporal delimiter at the front. For a 22.7mb/s file with average framesize 113 kB this improved the time for the calls to ff_av1_filter_obus_buf() when writing Matroska from 313319 decicycles to 2368 decicycles; for another file with 1.5mb/s (average framesize 7.3 kB) it improved from 34539 decicycles to 1922 decicyles. For these files the only units that needed to be stripped away were temporal unit delimiters at the front. Signed-off-by: Andreas Rheinhardt --- If it is desired, I can add a commit to switch ff_mov_write_packet() to not use a pointer just for reformatted_data (that is of course initialized to NULL), but to replace it by a data buffer that gets initialized to pkt->data and modified so that data + offset always points to the current data. (This is possible now that the av_freep() have been removed from the reformatting functions.) libavformat/av1.c | 50 ++++++++++++++++++++++++++++++++------- libavformat/av1.h | 13 ++++++---- libavformat/matroskaenc.c | 2 +- libavformat/movenc.c | 11 +++++---- 4 files changed, 58 insertions(+), 18 deletions(-) diff --git a/libavformat/av1.c b/libavformat/av1.c index 07b399efcc..fef8e96f8d 100644 --- a/libavformat/av1.c +++ b/libavformat/av1.c @@ -29,13 +29,20 @@ #include "avio.h" #include "avio_internal.h" -int ff_av1_filter_obus(AVIOContext *pb, const uint8_t *buf, int size) +static int av1_filter_obus(AVIOContext *pb, const uint8_t *buf, + int size, int *offset) { - const uint8_t *end = buf + size; + const uint8_t *start = buf, *end = buf + size; int64_t obu_size; - int start_pos, type, temporal_id, spatial_id; - - size = 0; + int off, start_pos, type, temporal_id, spatial_id; + enum { + START_NOT_FOUND, + START_FOUND, + END_FOUND, + OFFSET_IMPOSSIBLE, + } state = START_NOT_FOUND; + + off = size = 0; while (buf < end) { int len = parse_obu_header(buf, end - buf, &obu_size, &start_pos, &type, &temporal_id, &spatial_id); @@ -47,8 +54,16 @@ int ff_av1_filter_obus(AVIOContext *pb, const uint8_t *buf, int size) case AV1_OBU_REDUNDANT_FRAME_HEADER: case AV1_OBU_TILE_LIST: case AV1_OBU_PADDING: + if (state == START_FOUND) + state = END_FOUND; break; default: + if (state == START_NOT_FOUND) { + off = buf - start; + state = START_FOUND; + } else if (state == END_FOUND) { + state = OFFSET_IMPOSSIBLE; + } if (pb) avio_write(pb, buf, len); size += len; @@ -57,19 +72,35 @@ int ff_av1_filter_obus(AVIOContext *pb, const uint8_t *buf, int size) buf += len; } + if (offset) + *offset = state != OFFSET_IMPOSSIBLE ? off : -1; + return size; } -int ff_av1_filter_obus_buf(const uint8_t *in, uint8_t **out, int *size) +int ff_av1_filter_obus(AVIOContext *pb, const uint8_t *buf, int size) +{ + return av1_filter_obus(pb, buf, size, NULL); +} + +int ff_av1_filter_obus_buf(const uint8_t *in, uint8_t **out, + int *size, int *offset) { AVIOContext pb; uint8_t *buf; - int len, ret; + int len, off, ret; - len = ret = ff_av1_filter_obus(NULL, in, *size); + len = ret = av1_filter_obus(NULL, in, *size, &off); if (ret < 0) { return ret; } + if (off >= 0) { + *out = (uint8_t *)in; + *size = len; + *offset = off; + + return 0; + } buf = av_malloc((size_t)len + AV_INPUT_BUFFER_PADDING_SIZE); if (!buf) @@ -77,13 +108,14 @@ int ff_av1_filter_obus_buf(const uint8_t *in, uint8_t **out, int *size) ffio_init_context(&pb, buf, len, 1, NULL, NULL, NULL, NULL); - ret = ff_av1_filter_obus(&pb, in, *size); + ret = av1_filter_obus(&pb, in, *size, NULL); av_assert1(ret == len); memset(buf + len, 0, AV_INPUT_BUFFER_PADDING_SIZE); *out = buf; *size = len; + *offset = 0; return 0; } diff --git a/libavformat/av1.h b/libavformat/av1.h index 6cc3fcaad2..dd5b47dc25 100644 --- a/libavformat/av1.h +++ b/libavformat/av1.h @@ -56,19 +56,24 @@ typedef struct AV1SequenceParameters { int ff_av1_filter_obus(AVIOContext *pb, const uint8_t *buf, int size); /** - * Filter out AV1 OBUs not meant to be present in ISOBMFF sample data and write - * the resulting bitstream to a newly allocated data buffer. + * Filter out AV1 OBUs not meant to be present in ISOBMFF sample data and return + * the result in a data buffer, avoiding allocations and copies if possible. * * @param in input data buffer - * @param out pointer to pointer that will hold the allocated data buffer + * @param out pointer to pointer for the returned buffer. In case of success, + * it is independently allocated if and only if `*out` differs from in. * @param size size of the input data buffer. The size of the resulting output * data buffer will be written here + * @param offset offset of the returned data inside `*out`: It runs from + * `*out + offset` (inclusive) to `*out + offset + size` + * (exclusive); is zero if `*out` is independently allocated. * * @return 0 in case of success, a negative AVERROR code in case of failure. * On failure, *out and *size are unchanged * @note *out will be treated as unintialized on input and will not be freed. */ -int ff_av1_filter_obus_buf(const uint8_t *in, uint8_t **out, int *size); +int ff_av1_filter_obus_buf(const uint8_t *in, uint8_t **out, + int *size, int *offset); /** * Parses a Sequence Header from the the provided buffer. diff --git a/libavformat/matroskaenc.c b/libavformat/matroskaenc.c index 20bad95262..618f07c769 100644 --- a/libavformat/matroskaenc.c +++ b/libavformat/matroskaenc.c @@ -2112,7 +2112,7 @@ static int mkv_write_block(AVFormatContext *s, AVIOContext *pb, /* extradata is Annex B, assume the bitstream is too and convert it */ err = ff_hevc_annexb2mp4_buf(pkt->data, &data, &size, 0, NULL); } else if (par->codec_id == AV_CODEC_ID_AV1) { - err = ff_av1_filter_obus_buf(pkt->data, &data, &size); + err = ff_av1_filter_obus_buf(pkt->data, &data, &size, &offset); } else if (par->codec_id == AV_CODEC_ID_WAVPACK) { err = mkv_strip_wavpack(pkt->data, &data, &size); } else diff --git a/libavformat/movenc.c b/libavformat/movenc.c index b5e06de3d5..f715f911f3 100644 --- a/libavformat/movenc.c +++ b/libavformat/movenc.c @@ -5374,7 +5374,7 @@ int ff_mov_write_packet(AVFormatContext *s, AVPacket *pkt) AVCodecParameters *par = trk->par; AVProducerReferenceTime *prft; unsigned int samples_in_chunk = 0; - int size = pkt->size, ret = 0; + int size = pkt->size, ret = 0, offset = 0; int prft_size; uint8_t *reformatted_data = NULL; @@ -5491,7 +5491,8 @@ int ff_mov_write_packet(AVFormatContext *s, AVPacket *pkt) } } else if (par->codec_id == AV_CODEC_ID_AV1) { if (trk->hint_track >= 0 && trk->hint_track < mov->nb_streams) { - ret = ff_av1_filter_obus_buf(pkt->data, &reformatted_data, &size); + ret = ff_av1_filter_obus_buf(pkt->data, &reformatted_data, + &size, &offset); if (ret < 0) return ret; avio_write(pb, reformatted_data, size); @@ -5667,12 +5668,14 @@ int ff_mov_write_packet(AVFormatContext *s, AVPacket *pkt) if (trk->hint_track >= 0 && trk->hint_track < mov->nb_streams) ff_mov_add_hinted_packet(s, pkt, trk->hint_track, trk->entry, - reformatted_data, size); + reformatted_data ? reformatted_data + offset + : NULL, size); end: err: - av_free(reformatted_data); + if (pkt->data != reformatted_data) + av_free(reformatted_data); return ret; }