From patchwork Tue Nov 19 16:12:32 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 16341 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 F3AE4446A29 for ; Tue, 19 Nov 2019 18:45:54 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id C8B5168A3BA; Tue, 19 Nov 2019 18:45:54 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-ed1-f68.google.com (mail-ed1-f68.google.com [209.85.208.68]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id AEEB6689B0C for ; Tue, 19 Nov 2019 18:45:48 +0200 (EET) Received: by mail-ed1-f68.google.com with SMTP id a67so17580529edf.11 for ; Tue, 19 Nov 2019 08:45:48 -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=CqrgeJYoSWBcWHj6CcxpDi060kZZ+YsIxdbKIm+Xaqs=; b=OZgGTFPEnym0vEQvB9s5UvmUAVG8+ZzDTVNvVbhuWrokbnJBJcBxUtuqeF9vCcFN6K +EevUjqTRkVeBCZ7YwjeGLKJ6WRZrELh3tk1bxpGyFrW28GCm4Rgb4Po+No6Q8BWjuMi 6FaKJfq5xxixRuRqZ7S7ad+fbbKKAei7ztzP9rNVNEyykzfuBfQSF3pzslLADx/paTzr 79H2skWm++ten/kiED5MXyPxJlQi3O3XR8hh2MyMi6ZA1u3UjH9GG7qULbUtA7ah22xH hjQU9/Mwi1uDi+yKLU8t7/D0Rt8ly6kay61nIpfNsn8dThtUdEjPxYyl9y7CR1DlKQ9l 8jAg== 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=CqrgeJYoSWBcWHj6CcxpDi060kZZ+YsIxdbKIm+Xaqs=; b=fW/NB4af7GrAirOez2EJmfrePbtgXl34e2LdOXdtRpnc82jCuyTzJmoyLF2RykAJoY wp2tGbO1R8D6/YK1h1DhbVh6WM1nayTJNeXgWcfOvErUxj+y+ynUi/8Ks8zOujnSKNEB 0A134yLsypOpY9GrD2fZr8IIRMWmEEGbm7+gFA8xWo529rfmVH5n+j2gaQSdWEHwPmew 8+bm6nsyiTNWilY/Ew/k74OYCvBJbVJtDKBXysE8N60y1M6eXpiWqzx/o05vKADejSKd NhfQayfU6089gB05ErZn/AAQEltiZwdxzfdtMEds05+GRBmci377mKmvPs3lDyIJ4KkK ARIA== X-Gm-Message-State: APjAAAUCGsU0F7t0vOS7rqWNgSmvmSrEDBXSuKRfBefTwFpbbfT7sjD5 oI4xIO6YpP6Mz4hCU7ZfeM7V6FxG X-Google-Smtp-Source: APXvYqy4rnPvVOXUqX8IZ3S0OcBrKdoNprc9BOtuxgNAbHGrIt6wgRzuaWsqaesd/H7gfD+fDEwy5Q== X-Received: by 2002:adf:d1a3:: with SMTP id w3mr41177581wrc.9.1574180289015; Tue, 19 Nov 2019 08:18:09 -0800 (PST) Received: from sblaptop.fritz.box (ipbcc08e23.dynamic.kabel-deutschland.de. [188.192.142.35]) by smtp.gmail.com with ESMTPSA id f188sm3611784wmf.3.2019.11.19.08.18.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Nov 2019 08:18:07 -0800 (PST) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Tue, 19 Nov 2019 17:12:32 +0100 Message-Id: <20191119161234.3766-5-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20191119161234.3766-1-andreas.rheinhardt@gmail.com> References: <20191119161234.3766-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v2 5/6] avcodec/cbs_jpeg: Write scan data directly 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" This commit makes use of the fact that the final unit buffer is now allocated within write_unit to avoid a memcpy of scan data: It is now no longer copied into an intermediate buffer before being written into the final buffer, but directly written into the final buffer. Writing the scan element in cbs_jpeg used an intermediate buffer to write the scan header followed by the scan data that was copied into said buffer. Afterwards, the final buffer for the element was allocated and everything copied into this buffer. But it is trivial to compute the needed size of the final buffer after having written the header, so one can allocate the final buffer immediately and copy the scan data directly into it, avoiding a memcpy. Signed-off-by: Andreas Rheinhardt --- libavcodec/cbs_jpeg.c | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/libavcodec/cbs_jpeg.c b/libavcodec/cbs_jpeg.c index da22bdf720..dacdfa985b 100644 --- a/libavcodec/cbs_jpeg.c +++ b/libavcodec/cbs_jpeg.c @@ -330,24 +330,37 @@ static int cbs_jpeg_write_scan(CodedBitstreamContext *ctx, PutBitContext *pbc) { JPEGRawScan *scan = unit->content; + size_t header_size, size; int err; err = cbs_jpeg_write_scan_header(ctx, pbc, &scan->header); if (err < 0) return err; - if (scan->data) { - if (scan->data_size * 8 > put_bits_left(pbc)) - return AVERROR(ENOSPC); + av_assert0(put_bits_count(pbc) % 8 == 0); - av_assert0(put_bits_count(pbc) % 8 == 0); + flush_put_bits(pbc); + header_size = size = put_bits_count(pbc) / 8; - flush_put_bits(pbc); + if (scan->data) { + if (scan->data_size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE + - header_size) + return AVERROR(ENOMEM); - memcpy(put_bits_ptr(pbc), scan->data, scan->data_size); - skip_put_bytes(pbc, scan->data_size); + size += scan->data_size; } + err = ff_cbs_alloc_unit_data(ctx, unit, size); + if (err < 0) + return err; + + unit->data_bit_padding = 0; + + memcpy(unit->data, pbc->buf, header_size); + + if (scan->data) + memcpy(unit->data + header_size, scan->data, scan->data_size); + return 0; } @@ -387,9 +400,9 @@ static int cbs_jpeg_write_unit(CodedBitstreamContext *ctx, int err; if (unit->type == JPEG_MARKER_SOS) - err = cbs_jpeg_write_scan (ctx, unit, pbc); - else - err = cbs_jpeg_write_segment(ctx, unit, pbc); + return cbs_jpeg_write_scan(ctx, unit, pbc); + + err = cbs_jpeg_write_segment(ctx, unit, pbc); if (err < 0) return err;