From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by master.gitmailbox.com (Postfix) with ESMTP id 74BF04350E for ; Wed, 15 Jun 2022 23:23:02 +0000 (UTC) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 02A3D68B61E; Thu, 16 Jun 2022 02:22:59 +0300 (EEST) Received: from mail-ed1-f41.google.com (mail-ed1-f41.google.com [209.85.208.41]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 92435680374 for ; Thu, 16 Jun 2022 02:22:52 +0300 (EEST) Received: by mail-ed1-f41.google.com with SMTP id b8so18263138edj.11 for ; Wed, 15 Jun 2022 16:22:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=date:from:to:subject:message-id:mail-followup-to:references :mime-version:content-disposition:in-reply-to:user-agent; bh=2cl2p/PYxLu8KR9+/ZunungGFxyCUQfTItzZgMC0SFg=; b=WAGSA5QBZyJW3DhCRHrkTCVLQeDTpJOgY2zA7fdsSw6mZFrZIK8Vw542XOqvG/ZG66 tzxz61IniOkLVFL54WvdgZ4SOfMtC1w1ZktOvuxh46vwiPQR0Un4mFOwmi7UfJg7f7kg k7Jubf8LslOLe+53uZdXh5IcaB7TWs9vrqq0KZhnxUxnmQ/ZYGw2S124CCaKWQWAQwsE 8oUB5cOv6fCYLCl+tWCAjIKTFpY09imtHwMGcBg6HwuzRQTBSJ8u26PbbtXJV9npbhTM PHeCd/huQfgLinUJiSuVu0PqbonMvKn3U7cgwEXeQm83oF3b93ZwB/y1L0K6B7CMAB+k nRTQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:subject:message-id:mail-followup-to :references:mime-version:content-disposition:in-reply-to:user-agent; bh=2cl2p/PYxLu8KR9+/ZunungGFxyCUQfTItzZgMC0SFg=; b=W7y8USw6Mtz3hDqp2I8jiGAboGWvyTeimf3GAZ54hOxAE6vJdCrTYS42H6dkmAqo4i fehdBUIYp/+G+X1AhiKlYEURkXWsPeNr+p4sOxi3s5c45FKiPcCNbjeg4ufHsEOaO+dW i/wkQg4oYvLd5lRu1JWMto9P0z3PeyDe17G+dRtVmW6YwNSDUtTDUZIlXCODdyQv0Mw9 Wk/GSfXBFswOkbrkLJ1x4F9VzGG2E/0bF51gmMNuRS9SAwff4/sEj+eoy6yTXwngTtIg B39asYY+b7NGdD/zU6MitGAnUvftIAs0s1P3PRYI6kf8MM7S34HadkNgpZ+OHaDug9y9 DhQQ== X-Gm-Message-State: AJIora9DjC8M44ai4fBnexpb77k53xaUhK0+/otnnWptHB6n7voNT4Pr /UiYKnLl/mTwQgVWfXZ0IMA3kiFm+Qw= X-Google-Smtp-Source: AGRyM1vvcNLxas9ZFCJdih9J8uziAAqOndRDYoNhWOwgF76Pj0/fLg1EM9k7wBsQNIq/iyzRSeKiGA== X-Received: by 2002:a05:6402:430f:b0:42e:2a86:abaf with SMTP id m15-20020a056402430f00b0042e2a86abafmr2758186edc.194.1655335371567; Wed, 15 Jun 2022 16:22:51 -0700 (PDT) Received: from mariano (dynamic-adsl-78-14-101-54.clienti.tiscali.it. [78.14.101.54]) by smtp.gmail.com with ESMTPSA id q8-20020aa7da88000000b0042b5cf75d6esm401006eds.97.2022.06.15.16.22.49 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Jun 2022 16:22:50 -0700 (PDT) Received: by mariano (Postfix, from userid 1000) id AC8C6BFA36; Thu, 16 Jun 2022 01:22:48 +0200 (CEST) Date: Thu, 16 Jun 2022 01:22:48 +0200 From: Stefano Sabatini To: FFmpeg development discussions and patches Message-ID: <20220615232248.GE11679@mariano> Mail-Followup-To: FFmpeg development discussions and patches References: <20210421122706.9002-1-george@nsup.org> <20210421122706.9002-3-george@nsup.org> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20210421122706.9002-3-george@nsup.org> User-Agent: Mutt/1.13.2 (2019-12-18) Subject: Re: [FFmpeg-devel] [PATCH 2/7] lavu: new AVWriter API. X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 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 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Archived-At: List-Archive: List-Post: Preliminary quick review. On date Wednesday 2021-04-21 14:27:01 +0200, Nicolas George wrote: [...] > --- /dev/null > +++ b/libavutil/writer.c > @@ -0,0 +1,443 @@ > +/* > + * Copyright (c) 2021 Nicolas George > + * > + * 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 "avassert.h" > +#include "log.h" > +#include "writer.h" > + > +/*************************************************************************** > + * Generic API > + ***************************************************************************/ > + > +#define FIELDOK(st, f) ((char *)(&(st)->f + 1) <= (char *)(st) + (st)->self_size) > + > +#define methods_assert_abi(methods) av_assert1(FIELDOK(methods, flush)) "methods" is somehow confusing since it can contain also other things (name, size, etc.). What about using something as class instead? Or "implementation" to mean that the functionality is embedded there. > + > +static void printf_unchecked(AVWriter wr, const char *fmt, ...) > +{ > + va_list va; > + > + va_start(va, fmt); > + wr.methods->vprintf(wr, fmt, va); > + va_end(va); > +} > + > +static void write_or_discard(AVWriter wr, size_t buf_size, size_t write_size) > +{ > + av_assert1(wr.methods->advance_buffer); > + wr.methods->advance_buffer(wr, FFMIN(buf_size, write_size)); > + if (write_size > buf_size && wr.methods->notify_discard) > + wr.methods->notify_discard(wr, write_size - buf_size); > +} > + > +static void av_writer_impossible(AVWriter wr, const char *message) you can possibly discard the av_ and rename to something as notify_unsupported_operation() ("impossible" is confusing in this context). [...] > +/*************************************************************************** > + * AVBufWriter - write to pre-allocated memory > + ***************************************************************************/ > + > +#define buf_writer_assert_abi(bwr) av_assert1(FIELDOK(bwr, pos)) > + > +static size_t buf_writer_room(AVBufWriter *bwr) inline? > +{ > + return bwr->pos < bwr->size ? bwr->size - bwr->pos - 1 : 0; > +} > + > +static void buf_writer_write(AVWriter wr, const char *data, size_t size) > +{ > + AVBufWriter *bwr = wr.obj; > + > + av_assert1(av_buf_writer_check(wr)); > + buf_writer_assert_abi(bwr); maybe factorize with a macro? > + size = FFMIN(buf_writer_room(bwr), size); > + memcpy(bwr->buf + bwr->pos, data, size); > + bwr->pos += size; > + bwr->buf[bwr->pos] = 0; > +} > + > +static void buf_writer_vprintf(AVWriter wr, const char *fmt, va_list va) > +{ > + AVBufWriter *bwr = wr.obj; > + int ret; > + > + av_assert1(av_buf_writer_check(wr)); > + buf_writer_assert_abi(bwr); > + ret = vsnprintf(bwr->buf + bwr->pos, buf_writer_room(bwr) + 1, fmt, va); > + if (ret > 0) > + bwr->pos += ret; > +} > + > +static char *buf_writer_get_buffer(AVWriter wr, size_t min_size, size_t *size) > +{ > + AVBufWriter *bwr = wr.obj; > + > + av_assert1(av_buf_writer_check(wr)); > + buf_writer_assert_abi(bwr); > + *size = bwr->size - 1 - bwr->pos; > + return bwr->buf + bwr->pos; > +} > + > +static void buf_writer_advance_buffer(AVWriter wr, size_t size) > +{ > + AVBufWriter *bwr = wr.obj; > + > + av_assert1(av_buf_writer_check(wr)); > + buf_writer_assert_abi(bwr); > + bwr->pos += size; > + bwr->buf[bwr->pos] = 0; buffer overflow, is this set needed? > +} > + > +AV_WRITER_DEFINE_METHODS(/*public*/, AVBufWriter, av_buf_writer) { > + .self_size = sizeof(AVWriterMethods), > + .name = "AVBufWriter", > + .write = buf_writer_write, > + .vprintf = buf_writer_vprintf, > + .get_buffer = buf_writer_get_buffer, > + .advance_buffer = buf_writer_advance_buffer, > +}; > + > +AVBufWriter *av_buf_writer_init(AVBufWriter *bwr, char *buf, size_t size) > +{ > + buf_writer_assert_abi(bwr); > + bwr->buf = buf; > + bwr->size = size; > + bwr->pos = 0; > + buf[0] = 0; > + return bwr; > +} > + > +AVWriter av_buf_writer_wrap(AVBufWriter *bwr) > +{ > + AVWriter r = { av_buf_writer_get_methods(), bwr }; nit+++: wr for consistency [...] > +AVWriter av_dynbuf_writer_wrap(AVDynbufWriter *dwr) > +{ > + AVWriter r = { av_dynbuf_writer_get_methods(), dwr }; > + dynbuf_writer_assert_abi(dwr); > + return r; > +} this also seems a common pattern and could be factorized: #define AV_WRITER_DEFINE_WRAP(type_, name_) \ AVWriter av_##name_##_writer_wrap(type_, *wrimpl) \ { \ AVWriter wr = { av_##name_##_writer_get_methods(), wrimpl }; \ type_##_writer_assert_abi(wrimpl); \ return wr; \ } [...] _______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org https://ffmpeg.org/mailman/listinfo/ffmpeg-devel To unsubscribe, visit link above, or email ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".