From patchwork Wed Oct 2 23:04:38 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Thompson X-Patchwork-Id: 15479 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 619E744A22E for ; Thu, 3 Oct 2019 02:11:12 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 4A3376881B8; Thu, 3 Oct 2019 02:11:12 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wr1-f68.google.com (mail-wr1-f68.google.com [209.85.221.68]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 0A2A06881A7 for ; Thu, 3 Oct 2019 02:11:10 +0300 (EEST) Received: by mail-wr1-f68.google.com with SMTP id r3so795315wrj.6 for ; Wed, 02 Oct 2019 16:11:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=jkqxz-net.20150623.gappssmtp.com; s=20150623; h=from:to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=WBK9Z9EphNELwg/6EPbne0oYy89Gasg8zpn0n/anPUI=; b=AcDm4N/sscddPutzs4gZdpzlHaUPd1VY8DydPRs3c4K2cMG6myIqst1UjXe05trBZt O8JjCUBSEVx1DyLin9GtUiVQINyfFAr0ArHGX1msYD+uWm142gtvKHak0SH8N6uxgadb hpEZ2vserwWH2iKsRDdMkVkz/h3NrRql0XnrAiLEGUhQpizqVaHIJ3VBhJ55tgEgHYMr 4k+K1+NODi1dvBzcS8W6zaT391ZtMUrPwZ433EizVnDYXyN5aVXmxHb+DsEHb5VL0cr4 dacvEpNqK9FSsR3dZJCX82XPluXAGUT+PeHFGsVet8okQn9lI8IMbIObRP/RPSXpOu1n 8Qxw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=WBK9Z9EphNELwg/6EPbne0oYy89Gasg8zpn0n/anPUI=; b=LKqdYZaMkMca2KU2o/CHeBoDGihyMFmj4+iBNYrwHv7qHb49VfdLuu3jWNShMOpAR6 ILOGM5GetL/hF745pSFajJH+0nV91cFEtred7FbDMKlRONEJmiJFfffK410m/UWPTPfx mJcAHnBdSSh5/55rMlAtd8xLeeh5HWVoYgwaWF0KzaD5+D5bn7Ggj1sPJLDUnUchSgLR KPOejahxlsov6I9I5WcLUDCeRVv4uBUBiu1Re6VWcudOO9Ev3YNP1sMYM7aI8l87gq5G fdfIDmW3Cc3pM/KbvC8r1JGZ2bK8m5I1yPBl9/z8V3LyweEFL1GMc/F1qCDXgi93yFyx PIdw== X-Gm-Message-State: APjAAAVJAJQ2MtWNk/vC8lkw1Y5x60MONEvRn2VcsHaTeiEwTT+PycZL VmKMMZyO3YQw0FH9tcQFPBTjFBBXxv8= X-Google-Smtp-Source: APXvYqxPk9ftBfbE/iZZCNo0fvMWU8HTn6GhlhErXgZeFr1UoErRX42juEmE5y0zeN4sBCnGuXFkwg== X-Received: by 2002:adf:ce91:: with SMTP id r17mr4270336wrn.97.1570057499256; Wed, 02 Oct 2019 16:04:59 -0700 (PDT) Received: from rywe.jkqxz.net (cpc91242-cmbg18-2-0-cust650.5-4.cable.virginm.net. [82.8.130.139]) by smtp.gmail.com with ESMTPSA id o9sm1489808wrh.46.2019.10.02.16.04.58 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Oct 2019 16:04:58 -0700 (PDT) From: Mark Thompson To: ffmpeg-devel@ffmpeg.org Date: Thu, 3 Oct 2019 00:04:38 +0100 Message-Id: <20191002230453.6462-3-sw@jkqxz.net> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20191002230453.6462-1-sw@jkqxz.net> References: <20191002230453.6462-1-sw@jkqxz.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v3 03/18] cbs: Describe allocate/free methods in tabular form 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Unit types are split into three categories, depending on how their content is managed: * POD structure - these require no special treatment. * Structure containing references to refcounted buffers - these can use a common free function when the offsets of all the internal references are known. * More complex structures - these still require ad-hoc treatment. For each codec we can then maintain a table of descriptors for each set of equivalent unit types, defining the mechanism needed to allocate/free that unit content. This is not required to be used immediately - a new alloc function supports this, but does not replace the old one which works without referring to these tables. --- I switched to a fixed-size array of unit types in each descriptor to handle the case where multiple types need the same handling. I wonder whether that might be better as a pointer, using a compound literal array in each entry - any opinion on that? libavcodec/cbs.c | 63 +++++++++++++++++++++++++++++++++++++++ libavcodec/cbs.h | 9 ++++++ libavcodec/cbs_internal.h | 42 ++++++++++++++++++++++++++ 3 files changed, 114 insertions(+) diff --git a/libavcodec/cbs.c b/libavcodec/cbs.c index 1a43cd2694..57f0c2257a 100644 --- a/libavcodec/cbs.c +++ b/libavcodec/cbs.c @@ -753,3 +753,66 @@ void ff_cbs_delete_unit(CodedBitstreamContext *ctx, frag->units + position + 1, (frag->nb_units - position) * sizeof(*frag->units)); } + +static void cbs_default_free_unit_content(void *opaque, uint8_t *data) +{ + const CodedBitstreamUnitTypeDescriptor *desc = opaque; + if (desc->content_type == CBS_CONTENT_TYPE_INTERNAL_REFS) { + int i; + for (i = 0; i < desc->nb_ref_offsets; i++) { + void **ptr = (void**)(data + desc->ref_offsets[i]); + av_buffer_unref((AVBufferRef**)(ptr + 1)); + } + } + av_free(data); +} + +static const CodedBitstreamUnitTypeDescriptor + *cbs_find_unit_type_desc(CodedBitstreamContext *ctx, + CodedBitstreamUnit *unit) +{ + const CodedBitstreamUnitTypeDescriptor *desc; + int i, j; + + if (!ctx->codec->unit_types) + return NULL; + + for (i = 0;; i++) { + desc = &ctx->codec->unit_types[i]; + if (desc->nb_unit_types == 0) + break; + for (j = 0; j < desc->nb_unit_types; j++) { + if (desc->unit_types[j] == unit->type) + return desc; + } + } + return NULL; +} + +int ff_cbs_alloc_unit_content2(CodedBitstreamContext *ctx, + CodedBitstreamUnit *unit) +{ + const CodedBitstreamUnitTypeDescriptor *desc; + + av_assert0(!unit->content && !unit->content_ref); + + desc = cbs_find_unit_type_desc(ctx, unit); + if (!desc) + return AVERROR(ENOSYS); + + unit->content = av_mallocz(desc->content_size); + if (!unit->content) + return AVERROR(ENOMEM); + + unit->content_ref = + av_buffer_create(unit->content, desc->content_size, + desc->content_free ? desc->content_free + : cbs_default_free_unit_content, + (void*)desc, 0); + if (!unit->content_ref) { + av_freep(&unit->content); + return AVERROR(ENOMEM); + } + + return 0; +} diff --git a/libavcodec/cbs.h b/libavcodec/cbs.h index be986df4e4..42896042c1 100644 --- a/libavcodec/cbs.h +++ b/libavcodec/cbs.h @@ -345,6 +345,15 @@ int ff_cbs_alloc_unit_content(CodedBitstreamContext *ctx, size_t size, void (*free)(void *opaque, uint8_t *content)); +/** + * Allocate a new internal content buffer matching the type of the unit. + * + * The content will be zeroed. + */ +int ff_cbs_alloc_unit_content2(CodedBitstreamContext *ctx, + CodedBitstreamUnit *unit); + + /** * Allocate a new internal data buffer of the given size in the unit. * diff --git a/libavcodec/cbs_internal.h b/libavcodec/cbs_internal.h index dd4babf092..7ed8ea0049 100644 --- a/libavcodec/cbs_internal.h +++ b/libavcodec/cbs_internal.h @@ -25,11 +25,53 @@ #include "put_bits.h" +enum { + // Unit content is a simple structure. + CBS_CONTENT_TYPE_POD, + // Unit content contains some references to other structures, but all + // managed via buffer reference counting. The descriptor defines the + // structure offsets of every buffer reference. + CBS_CONTENT_TYPE_INTERNAL_REFS, + // Unit content is something more complex. The descriptor defines + // special functions to manage the content. + CBS_CONTENT_TYPE_COMPLEX, +}; + +enum { + // Maximum number of unit types described by the same unit type + // descriptor. + CBS_MAX_UNIT_TYPES = 16, + // Maximum number of reference buffer offsets in any one unit. + CBS_MAX_REF_OFFSETS = 1, +}; + +typedef struct CodedBitstreamUnitTypeDescriptor { + int nb_unit_types; + const CodedBitstreamUnitType unit_types[CBS_MAX_UNIT_TYPES]; + + int content_type; + size_t content_size; + + int nb_ref_offsets; + // The structure must contain two adjacent elements: + // type *field; + // AVBufferRef *field_ref; + // where field points to something in the buffer referred to by + // field_ref. This offset is then set to offsetof(struct, field). + size_t ref_offsets[CBS_MAX_REF_OFFSETS]; + + void (*content_free)(void *opaque, uint8_t *data); +} CodedBitstreamUnitTypeDescriptor; + typedef struct CodedBitstreamType { enum AVCodecID codec_id; size_t priv_data_size; + // List of unit type descriptors for this codec. + // Terminated by a descriptor with nb_unit_types equal to zero. + const CodedBitstreamUnitTypeDescriptor *unit_types; + // Split frag->data into coded bitstream units, creating the // frag->units array. Fill data but not content on each unit. // The header argument should be set if the fragment came from