From patchwork Fri Dec 31 10:53:01 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 32956 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a6b:cd86:0:0:0:0:0 with SMTP id d128csp13864348iog; Fri, 31 Dec 2021 02:53:42 -0800 (PST) X-Google-Smtp-Source: ABdhPJy6gEclZRHV8JpO3YJ2ZwuJ+BaW83DqEuGFId9hr3MPL5h8IHLPBkdI3iVxVhoFUBBJOJ1U X-Received: by 2002:a17:907:2d8c:: with SMTP id gt12mr27776646ejc.430.1640948022473; Fri, 31 Dec 2021 02:53:42 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1640948022; cv=none; d=google.com; s=arc-20160816; b=ZG9vdHdKknyTYJQO7TRWgqrQHlOpZQSbdYbQK4Mxq6WD24XgEpCOAzmV22zUzqOdHB Xn4b8XN/rY91yxo1N4d2ZHOdsmp6U0GAY5iA2MJ0NKElPcPV9EBC6oGptfHWoilxzeaN 3wSZ943KjwsxXhiSpXErJOrEfWHIEaWwo1MaCnHFiyA881jCzjHPRKzf4VT3wHvrFt9+ WhNMKW4TEgTaEB6osaPyQjUvNBrGzPGb7LdR2jpw8KAh/RopFpfla6S4OHt4+P5ElVjx QMuqLX5RLpVqOEBiDvniEZvS8pK2zoIxonpncsssHEUzAh7OCcyMyCUMmBybjZg8kiit xg7g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:message-id:date:to:from :delivered-to; bh=boEOJjbM+11zegq3Y/JZBNvUhI4ULfMTkXfhzOwiBLw=; b=kJJI4dJUcNKcGRV/oygZLa+kYcz3jBM7FGGAXWix5NKONRk4Na88HYspX7jHtTIRfD ci7Wq8PH6+W3xYzTKubwjfencqgvufJ9RCSDvaKoyEyOr4QDMyHAEHjDq8Qnb/ORIsbo IY5XB8qbzfvOED+05bPJQp5+Km75oN2qXtnZL1fBaW+HJQMkgU0E9AqHzjB5MMPGz7FG VmmBpw/9Ldqwz0wsB+BeoSgpvdM26sgOkFc8bICwTSvwvMglSJuU8iHrW8P4XzbEFwZL K5jgh0G5JVaq4zSzLcqm56u8bfDeRzeGYixn4SWQfkk0CYtFkpDcPmNp2Lj/KugLzEuT RiSw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id nd17si16766836ejc.722.2021.12.31.02.53.42; Fri, 31 Dec 2021 02:53:42 -0800 (PST) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id C17B068AFDE; Fri, 31 Dec 2021 12:53:29 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail0.khirnov.net (red.khirnov.net [176.97.15.12]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 4B5B9680044 for ; Fri, 31 Dec 2021 12:53:22 +0200 (EET) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 9AC2F2404FE for ; Fri, 31 Dec 2021 11:53:21 +0100 (CET) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id b-5yfsP-nhaw for ; Fri, 31 Dec 2021 11:53:20 +0100 (CET) Received: from libav.khirnov.net (libav.khirnov.net [IPv6:2a00:c500:561:201::7]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256 client-signature RSA-PSS (2048 bits) client-digest SHA256) (Client CN "libav.khirnov.net", Issuer "smtp.khirnov.net SMTP CA" (verified OK)) by mail0.khirnov.net (Postfix) with ESMTPS id 8A16324017C for ; Fri, 31 Dec 2021 11:53:19 +0100 (CET) Received: by libav.khirnov.net (Postfix, from userid 1000) id 94BF03A0631; Fri, 31 Dec 2021 11:53:19 +0100 (CET) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Fri, 31 Dec 2021 11:53:01 +0100 Message-Id: <20211231105307.30946-1-anton@khirnov.net> X-Mailer: git-send-email 2.33.0 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 1/7] lavc/flac_parser: use a custom FIFO implementation 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: BF4DKO3ocWuJ FLAC parser currently uses AVFifoBuffer in a highly non-trivial manner, modifying its "internals" (the whole struct is currently public, but no other code touches its contents directly). E.g. it does not use any av_fifo functions for reading the FIFO contents, but implements its own. Reimplement the needed parts of the AVFifoBuffer API in the FLAC parser, making it completely self-contained. This will allow us to make AVFifoBuffer private. --- libavcodec/flac_parser.c | 191 +++++++++++++++++++++++++++++++-------- 1 file changed, 153 insertions(+), 38 deletions(-) diff --git a/libavcodec/flac_parser.c b/libavcodec/flac_parser.c index 3b27b152fc..d6af5ce836 100644 --- a/libavcodec/flac_parser.c +++ b/libavcodec/flac_parser.c @@ -34,7 +34,6 @@ #include "libavutil/attributes.h" #include "libavutil/crc.h" -#include "libavutil/fifo.h" #include "bytestream.h" #include "parser.h" #include "flac.h" @@ -57,6 +56,15 @@ #define MAX_FRAME_HEADER_SIZE 16 #define MAX_FRAME_VERIFY_SIZE (MAX_FRAME_HEADER_SIZE + 1) +typedef struct FifoBuffer { + uint8_t *buffer; + uint8_t *end; + uint8_t *rptr; + uint8_t *wptr; + size_t rndx; + size_t wndx; +} FifoBuffer; + typedef struct FLACHeaderMarker { int offset; /**< byte offset from start of FLACParseContext->buffer */ int link_penalty[FLAC_MAX_SEQUENTIAL_HEADERS]; /**< array of local scores @@ -84,7 +92,7 @@ typedef struct FLACParseContext { int nb_headers_buffered; /**< number of headers that are buffered */ int best_header_valid; /**< flag set when the parser returns junk; if set return best_header next time */ - AVFifoBuffer *fifo_buf; /**< buffer to store all data until headers + FifoBuffer fifo_buf; /**< buffer to store all data until headers can be verified */ int end_padded; /**< specifies if fifo_buf's end is padded */ uint8_t *wrap_buf; /**< general fifo read buffer when wrapped */ @@ -127,6 +135,17 @@ static int frame_header_is_valid(AVCodecContext *avctx, const uint8_t *buf, return 1; } +static size_t flac_fifo_size(FifoBuffer *f) +{ + av_assert0(f->wndx >= f->rndx); + return f->wndx - f->rndx; +} + +static size_t flac_fifo_space(FifoBuffer *f) +{ + return f->end - f->buffer - flac_fifo_size(f); +} + /** * Non-destructive fast fifo pointer fetching * Returns a pointer from the specified offset. @@ -143,7 +162,7 @@ static int frame_header_is_valid(AVCodecContext *avctx, const uint8_t *buf, static uint8_t *flac_fifo_read_wrap(FLACParseContext *fpc, int offset, int len, uint8_t **wrap_buf, int *allocated_size) { - AVFifoBuffer *f = fpc->fifo_buf; + FifoBuffer *f = &fpc->fifo_buf; uint8_t *start = f->rptr + offset; uint8_t *tmp_buf; @@ -180,9 +199,8 @@ static uint8_t *flac_fifo_read_wrap(FLACParseContext *fpc, int offset, int len, * A second call to flac_fifo_read (with new offset and len) should be called * to get the post-wrap buf if the returned len is less than the requested. **/ -static uint8_t *flac_fifo_read(FLACParseContext *fpc, int offset, int *len) +static uint8_t *flac_fifo_read(FifoBuffer *f, int offset, int *len) { - AVFifoBuffer *f = fpc->fifo_buf; uint8_t *start = f->rptr + offset; if (start >= f->end) @@ -191,6 +209,106 @@ static uint8_t *flac_fifo_read(FLACParseContext *fpc, int offset, int *len) return start; } +static int flac_fifo_grow(FifoBuffer *f, size_t inc) +{ + size_t size_old = f->end - f->buffer; + size_t offset_r = f->rptr - f->buffer; + size_t offset_w = f->wptr - f->buffer; + size_t size_new; + + uint8_t *tmp; + + if (size_old > SIZE_MAX - inc) + return AVERROR(EINVAL); + size_new = size_old + inc; + + tmp = av_realloc(f->buffer, size_new); + if (!tmp) + return AVERROR(ENOMEM); + + // move the data from the beginning of the ring buffer + // to the newly allocated space + // the second condition distinguishes full vs empty fifo + if (offset_w <= offset_r && flac_fifo_size(f)) { + const size_t copy = FFMIN(inc, offset_w); + memcpy(tmp + size_old, tmp, copy); + if (copy < offset_w) { + memmove(tmp, tmp + copy, offset_w - copy); + offset_w -= copy; + } else + offset_w = size_old + copy; + } + + f->buffer = tmp; + f->end = f->buffer + size_new; + f->rptr = f->buffer + offset_r; + f->wptr = f->buffer + offset_w; + + return 0; +} + +static int flac_fifo_write(FifoBuffer *f, const uint8_t *src, size_t size) +{ + uint32_t wndx = f->wndx; + uint8_t *wptr; + + if (flac_fifo_space(f) < size) { + int ret = flac_fifo_grow(f, FFMAX(flac_fifo_size(f), size)); + if (ret < 0) + return ret; + } + + wptr = f->wptr; + do { + size_t len = FFMIN(f->end - wptr, size); + memcpy(wptr, src, len); + src += len; + wptr += len; + if (wptr >= f->end) + wptr = f->buffer; + wndx += len; + size -= len; + } while (size > 0); + + f->wndx = wndx; + f->wptr = wptr; + + return 0; +} + +static void flac_fifo_drain(FifoBuffer *f, size_t size) +{ + av_assert0(flac_fifo_size(f) >= size); + f->rptr += size; + if (f->rptr >= f->end) + f->rptr -= f->end - f->buffer; + f->rndx += size; +} + +static int flac_fifo_alloc(FifoBuffer *f, size_t size) +{ + memset(f, 0, sizeof(*f)); + + f->buffer = av_realloc(NULL, size); + if (!f->buffer) + return AVERROR(ENOMEM); + + f->wptr = f->buffer; + f->rptr = f->buffer; + f->end = f->buffer + size; + + f->rndx = 0; + f->wndx = 0; + + return 0; +} + +static void flac_fifo_free(FifoBuffer *f) +{ + av_freep(&f->buffer); + memset(f, 0, sizeof(*f)); +} + static int find_headers_search_validate(FLACParseContext *fpc, int offset) { FLACFrameInfo fi; @@ -263,9 +381,9 @@ static int find_new_headers(FLACParseContext *fpc, int search_start) fpc->nb_headers_found = 0; /* Search for a new header of at most 16 bytes. */ - search_end = av_fifo_size(fpc->fifo_buf) - (MAX_FRAME_HEADER_SIZE - 1); + search_end = flac_fifo_size(&fpc->fifo_buf) - (MAX_FRAME_HEADER_SIZE - 1); read_len = search_end - search_start + 1; - buf = flac_fifo_read(fpc, search_start, &read_len); + buf = flac_fifo_read(&fpc->fifo_buf, search_start, &read_len); size = find_headers_search(fpc, buf, read_len, search_start); search_start += read_len - 1; @@ -277,7 +395,7 @@ static int find_new_headers(FLACParseContext *fpc, int search_start) /* search_start + 1 is the post-wrap offset in the fifo. */ read_len = search_end - (search_start + 1) + 1; - buf = flac_fifo_read(fpc, search_start + 1, &read_len); + buf = flac_fifo_read(&fpc->fifo_buf, search_start + 1, &read_len); wrap[1] = buf[0]; if ((AV_RB16(wrap) & 0xFFFE) == 0xFFF8) { @@ -406,12 +524,12 @@ static int check_header_mismatch(FLACParseContext *fpc, } read_len = end->offset - start->offset; - buf = flac_fifo_read(fpc, start->offset, &read_len); + buf = flac_fifo_read(&fpc->fifo_buf, start->offset, &read_len); crc = av_crc(av_crc_get_table(AV_CRC_16_ANSI), 0, buf, read_len); read_len = (end->offset - start->offset) - read_len; if (read_len) { - buf = flac_fifo_read(fpc, end->offset - read_len, &read_len); + buf = flac_fifo_read(&fpc->fifo_buf, end->offset - read_len, &read_len); crc = av_crc(av_crc_get_table(AV_CRC_16_ANSI), crc, buf, read_len); } } @@ -500,7 +618,7 @@ static int get_best_header(FLACParseContext *fpc, const uint8_t **poutbuf, FLACHeaderMarker *header = fpc->best_header; FLACHeaderMarker *child = header->best_child; if (!child) { - *poutbuf_size = av_fifo_size(fpc->fifo_buf) - header->offset; + *poutbuf_size = flac_fifo_size(&fpc->fifo_buf) - header->offset; } else { *poutbuf_size = child->offset - header->offset; @@ -519,7 +637,6 @@ static int get_best_header(FLACParseContext *fpc, const uint8_t **poutbuf, &fpc->wrap_buf, &fpc->wrap_buf_allocated_size); - if (fpc->pc->flags & PARSER_FLAG_USE_CODEC_TS) { if (header->fi.is_var_size) fpc->pc->pts = header->fi.frame_or_sample_num; @@ -534,7 +651,7 @@ static int get_best_header(FLACParseContext *fpc, const uint8_t **poutbuf, /* Return the negative overread index so the client can compute pos. This should be the amount overread to the beginning of the child */ if (child) - return child->offset - av_fifo_size(fpc->fifo_buf); + return child->offset - flac_fifo_size(&fpc->fifo_buf); return 0; } @@ -586,7 +703,7 @@ static int flac_parse(AVCodecParserContext *s, AVCodecContext *avctx, fpc->nb_headers_buffered--; } /* Release returned data from ring buffer. */ - av_fifo_drain(fpc->fifo_buf, best_child->offset); + flac_fifo_drain(&fpc->fifo_buf, best_child->offset); /* Fix the offset for the headers remaining to match the new buffer. */ for (curr = best_child->next; curr; curr = curr->next) @@ -620,7 +737,7 @@ static int flac_parse(AVCodecParserContext *s, AVCodecContext *avctx, while ((buf_size && read_end < buf + buf_size && fpc->nb_headers_buffered < FLAC_MIN_HEADERS) || (!buf_size && !fpc->end_padded)) { - int start_offset; + int start_offset, ret; /* Pad the end once if EOF, to check the final region for headers. */ if (!buf_size) { @@ -634,8 +751,8 @@ static int flac_parse(AVCodecParserContext *s, AVCodecContext *avctx, nb_desired * FLAC_AVG_FRAME_SIZE); } - if (!av_fifo_space(fpc->fifo_buf) && - av_fifo_size(fpc->fifo_buf) / FLAC_AVG_FRAME_SIZE > + if (!flac_fifo_space(&fpc->fifo_buf) && + flac_fifo_size(&fpc->fifo_buf) / FLAC_AVG_FRAME_SIZE > fpc->nb_headers_buffered * 20) { /* There is less than one valid flac header buffered for 20 headers * buffered. Therefore the fifo is most likely filled with invalid @@ -644,24 +761,20 @@ static int flac_parse(AVCodecParserContext *s, AVCodecContext *avctx, } /* Fill the buffer. */ - if ( av_fifo_space(fpc->fifo_buf) < read_end - read_start - && av_fifo_realloc2(fpc->fifo_buf, (read_end - read_start) + 2*av_fifo_size(fpc->fifo_buf)) < 0) { - av_log(avctx, AV_LOG_ERROR, - "couldn't reallocate buffer of size %"PTRDIFF_SPECIFIER"\n", - (read_end - read_start) + av_fifo_size(fpc->fifo_buf)); - goto handle_error; - } - if (buf_size) { - av_fifo_generic_write(fpc->fifo_buf, (void*) read_start, - read_end - read_start, NULL); + ret = flac_fifo_write(&fpc->fifo_buf, read_start, + read_end - read_start); } else { int8_t pad[MAX_FRAME_HEADER_SIZE] = { 0 }; - av_fifo_generic_write(fpc->fifo_buf, pad, sizeof(pad), NULL); + ret = flac_fifo_write(&fpc->fifo_buf, pad, sizeof(pad)); + } + if (ret < 0) { + av_log(avctx, AV_LOG_ERROR, "Error buffering data\n"); + goto handle_error; } /* Tag headers and update sequences. */ - start_offset = av_fifo_size(fpc->fifo_buf) - + start_offset = flac_fifo_size(&fpc->fifo_buf) - ((read_end - read_start) + (MAX_FRAME_HEADER_SIZE - 1)); start_offset = FFMAX(0, start_offset); nb_headers = find_new_headers(fpc, start_offset); @@ -689,13 +802,13 @@ static int flac_parse(AVCodecParserContext *s, AVCodecContext *avctx, /* restore the state pre-padding */ if (fpc->end_padded) { - int warp = fpc->fifo_buf->wptr - fpc->fifo_buf->buffer < MAX_FRAME_HEADER_SIZE; + int warp = fpc->fifo_buf.wptr - fpc->fifo_buf.buffer < MAX_FRAME_HEADER_SIZE; /* HACK: drain the tail of the fifo */ - fpc->fifo_buf->wptr -= MAX_FRAME_HEADER_SIZE; - fpc->fifo_buf->wndx -= MAX_FRAME_HEADER_SIZE; + fpc->fifo_buf.wptr -= MAX_FRAME_HEADER_SIZE; + fpc->fifo_buf.wndx -= MAX_FRAME_HEADER_SIZE; if (warp) { - fpc->fifo_buf->wptr += fpc->fifo_buf->end - - fpc->fifo_buf->buffer; + fpc->fifo_buf.wptr += fpc->fifo_buf.end - + fpc->fifo_buf.buffer; } read_start = read_end = NULL; } @@ -727,7 +840,7 @@ static int flac_parse(AVCodecParserContext *s, AVCodecContext *avctx, &fpc->wrap_buf, &fpc->wrap_buf_allocated_size); return buf_size ? (read_end - buf) : (fpc->best_header->offset - - av_fifo_size(fpc->fifo_buf)); + flac_fifo_size(&fpc->fifo_buf)); } if (!buf_size) return get_best_header(fpc, poutbuf, poutbuf_size); @@ -742,11 +855,13 @@ handle_error: static av_cold int flac_parse_init(AVCodecParserContext *c) { FLACParseContext *fpc = c->priv_data; + int ret; + fpc->pc = c; /* There will generally be FLAC_MIN_HEADERS buffered in the fifo before it drains. This is allocated early to avoid slow reallocation. */ - fpc->fifo_buf = av_fifo_alloc_array(FLAC_MIN_HEADERS + 3, FLAC_AVG_FRAME_SIZE); - if (!fpc->fifo_buf) { + ret = flac_fifo_alloc(&fpc->fifo_buf, (FLAC_MIN_HEADERS + 3) * FLAC_AVG_FRAME_SIZE); + if (ret < 0) { av_log(fpc->avctx, AV_LOG_ERROR, "couldn't allocate fifo_buf\n"); return AVERROR(ENOMEM); @@ -765,7 +880,7 @@ static void flac_parse_close(AVCodecParserContext *c) curr = temp; } fpc->headers = NULL; - av_fifo_freep(&fpc->fifo_buf); + flac_fifo_free(&fpc->fifo_buf); av_freep(&fpc->wrap_buf); } From patchwork Fri Dec 31 10:53:02 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 32959 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a6b:cd86:0:0:0:0:0 with SMTP id d128csp13864652iog; Fri, 31 Dec 2021 02:54:13 -0800 (PST) X-Google-Smtp-Source: ABdhPJyGLmqll6aARyK0QHejEe73JTdhqopSwhKb+zUAk0DCXtoPvQrSnJcFfsGSQ0Xco7e+Kxcu X-Received: by 2002:a05:6402:491:: with SMTP id k17mr34091546edv.333.1640948053628; Fri, 31 Dec 2021 02:54:13 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1640948053; cv=none; d=google.com; s=arc-20160816; b=P3W6waiXvplHTsfhsgHFnSPDVt1OAb7dXa7BksZrd981lBQwiW01YMm5mzhTtZRurE fIM9xZj4YESV9uZH4lsAMZBbQXGBu306o48exLjUn14suc15a1o+EJyFbUkdPouzk0YQ 3yD0n3IRNTQJAuWC/5wO1BBqf5mF4Ag9EX86NtsSJE6s2fTNUW3KhdqVkQjt8US7IKTH m9c5a/AedxpEFztiAr67qfg47bGKKeGWqCBdjutwiTEk6MjL0UbMtKZunVhr15SEkm6L bxJgAHYPRr141dELP8OH/wy/Kz+UNzQze2d3+cQARYVHNMZC5T4REDrAyLhaNK4MJR4n d4Mw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:references:in-reply-to:message-id :date:to:from:delivered-to; bh=7W120TG5xmoT5CuIuzCmKvdGVVxlYulZza0WppJEYUs=; b=t6/kD+H+fqp+my58QZtNJWFuIsgZ9ySoxsQXpX+PyfZt1kcYlAgAFXho0HIYr+o1iu eIrK7+epa7S4c2n+CQPwUwATN+Q4pqs/K36KbOyVG1wjrO+bSRTVQtrbJJsr2US2keV4 hxOqBv2Zd6qcB2Q1vOxH2n4fn/ik3X2iCA9OhOWCCS9oAd3ocK1mMfr11ujSaxdxr7Yz +RUnMZTSI4raI1FvvkQtrT4j3FoiA6PY4zBuxWW8I71CqA1MmokF+s47DLtnHI9EwDwY uQyEVBgOgsIJBsXCQz00NFDwvlQWkHlNRcQ9sVkwyCrNMHLcxH7UThmN9t7a96RUygF4 m6SQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id i22si15908506ejw.575.2021.12.31.02.54.13; Fri, 31 Dec 2021 02:54:13 -0800 (PST) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id B68DB68AFFF; Fri, 31 Dec 2021 12:53:33 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail0.khirnov.net (red.khirnov.net [176.97.15.12]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 4B9C368AF97 for ; Fri, 31 Dec 2021 12:53:23 +0200 (EET) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 5B55B24017E for ; Fri, 31 Dec 2021 11:53:22 +0100 (CET) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id ABfzB3UaTbso for ; Fri, 31 Dec 2021 11:53:22 +0100 (CET) Received: from libav.khirnov.net (libav.khirnov.net [IPv6:2a00:c500:561:201::7]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256 client-signature RSA-PSS (2048 bits) client-digest SHA256) (Client CN "libav.khirnov.net", Issuer "smtp.khirnov.net SMTP CA" (verified OK)) by mail0.khirnov.net (Postfix) with ESMTPS id 9AC58240506 for ; Fri, 31 Dec 2021 11:53:19 +0100 (CET) Received: by libav.khirnov.net (Postfix, from userid 1000) id 970BB3A038E; Fri, 31 Dec 2021 11:53:19 +0100 (CET) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Fri, 31 Dec 2021 11:53:02 +0100 Message-Id: <20211231105307.30946-2-anton@khirnov.net> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20211231105307.30946-1-anton@khirnov.net> References: <20211231105307.30946-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 2/7] lavf/dvenc: replace av_fifo_peek2() with av_fifo_generic_peek_at() 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: PIAlAr1v0MKT This is the only remaining caller of av_fifo_peek2(), which will be deprecated. --- libavformat/dvenc.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/libavformat/dvenc.c b/libavformat/dvenc.c index 9a853ba7ce..b76539b59f 100644 --- a/libavformat/dvenc.c +++ b/libavformat/dvenc.c @@ -201,8 +201,9 @@ static void dv_inject_audio(DVMuxContext *c, int channel, uint8_t* frame_ptr) if (of*2 >= size) continue; - frame_ptr[d] = *av_fifo_peek2(c->audio_data[channel], of*2+1); // FIXME: maybe we have to admit - frame_ptr[d+1] = *av_fifo_peek2(c->audio_data[channel], of*2); // that DV is a big-endian PCM + // FIXME: maybe we have to admit that DV is a big-endian PCM + av_fifo_generic_peek_at(c->audio_data[channel], frame_ptr + d, of * 2, 2, NULL); + FFSWAP(uint8_t, frame_ptr[d], frame_ptr[d + 1]); } frame_ptr += 16 * 80; /* 15 Video DIFs + 1 Audio DIF */ } From patchwork Fri Dec 31 10:53:03 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 32957 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a6b:cd86:0:0:0:0:0 with SMTP id d128csp13864445iog; Fri, 31 Dec 2021 02:53:52 -0800 (PST) X-Google-Smtp-Source: ABdhPJwvKjm69OSYcPUvy/roD8Kga6MU/EiBRZBgKsOhZCb164VRB/5012p7uQymRJp9LGLgfE6Q X-Received: by 2002:a17:906:1b52:: with SMTP id p18mr27631773ejg.564.1640948032057; Fri, 31 Dec 2021 02:53:52 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1640948032; cv=none; d=google.com; s=arc-20160816; b=h2ozddSS3WeV3xZO27FIE7HsXc00lCLcOvW5uvINgBeYbs7pL3zjdjHw5c5fCRuV6V Q4Xm2srYni3jVkunEB1DijfP8Bs1ZPP/ghghbPDo4jHlJPWXVTDsE1cRwqWBmMcEpqO6 iEFDBLwEzVfn1e8lJAxlo7JCMR4/8ol87x9tFoMR5ChXV0MK82orVAdHmMM39PaH4cyv no76UFaaaYM+zCLoHV21ecuRZUGiGBFnA6shm47q+2ydy9go9KYPkkx9e2IBFy2bKPqv ZhZ9eRLcWm06gmzpQDcRCAXTm2X97eOdlGbzHwQSfvgPLjkMtJ76bEcpVMvYAECSibtT Q+EQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:references:in-reply-to:message-id :date:to:from:delivered-to; bh=reUZdEr+Mdgn0JX0o5jslMHyu6ykmtRFRIA42NbQTns=; b=MOoZiKVCDg41eDFaI0/5yumGa9eiKCz8VBe6fIyR7nFKGQP65V674kPTvhxmhhJl6F Wp0LwsTP/asIhfZm+8gNgDS1v7WaNebOL9So/sjXN+IkDqus6upmUHxmhbzqG5c8i6G5 5sJKiOtWZlGgPYhYzyT7bAFQC/8RcmTUDFmutVJdYuNLE6cYwx6PhW1nm5pM/p+2fX4j nnK8oO/C/Dg9olQS9QOhI6x8tsWuHI46ut+nOpRZ05qHVma+wYtLVe5yIxwZhDA/+NNA +RjHlXfEFmuwW70G9HJLKziuTpa33r5MWglIZT/EN5SPleLlC58PBVRkAfvO8pNYUn7o z80g== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id cq24si13108991edb.173.2021.12.31.02.53.51; Fri, 31 Dec 2021 02:53:52 -0800 (PST) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id C7CC668AFEB; Fri, 31 Dec 2021 12:53:30 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail0.khirnov.net (red.khirnov.net [176.97.15.12]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 9FD71680044 for ; Fri, 31 Dec 2021 12:53:22 +0200 (EET) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id EDE7B24017C for ; Fri, 31 Dec 2021 11:53:21 +0100 (CET) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id K4y-AHUJKnwT for ; Fri, 31 Dec 2021 11:53:19 +0100 (CET) Received: from libav.khirnov.net (libav.khirnov.net [IPv6:2a00:c500:561:201::7]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256 client-signature RSA-PSS (2048 bits) client-digest SHA256) (Client CN "libav.khirnov.net", Issuer "smtp.khirnov.net SMTP CA" (verified OK)) by mail0.khirnov.net (Postfix) with ESMTPS id 81872240179 for ; Fri, 31 Dec 2021 11:53:19 +0100 (CET) Received: by libav.khirnov.net (Postfix, from userid 1000) id 9C5753A073E; Fri, 31 Dec 2021 11:53:19 +0100 (CET) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Fri, 31 Dec 2021 11:53:03 +0100 Message-Id: <20211231105307.30946-3-anton@khirnov.net> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20211231105307.30946-1-anton@khirnov.net> References: <20211231105307.30946-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 3/7] lavu/fifo: deprecate av_fifo_peek2() 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: 7/9Ck1sJoh63 It returns a pointer inside the fifo's buffer, which cannot be safely used without accessing AVFifoBuffer internals. It is easier and safer to use av_fifo_generic_peek_at(). --- libavutil/fifo.h | 4 ++++ libavutil/tests/fifo.c | 8 -------- libavutil/version.h | 1 + tests/ref/fate/fifo | 26 -------------------------- 4 files changed, 5 insertions(+), 34 deletions(-) diff --git a/libavutil/fifo.h b/libavutil/fifo.h index dc7bc6f0dd..37da9f14c2 100644 --- a/libavutil/fifo.h +++ b/libavutil/fifo.h @@ -156,6 +156,7 @@ int av_fifo_grow(AVFifoBuffer *f, unsigned int additional_space); */ void av_fifo_drain(AVFifoBuffer *f, int size); +#if FF_API_OLD_FIFO /** * Return a pointer to the data stored in a FIFO buffer at a certain offset. * The FIFO buffer is not modified. @@ -165,7 +166,9 @@ void av_fifo_drain(AVFifoBuffer *f, int size); * than the used buffer size or the returned pointer will * point outside to the buffer data. * The used buffer size can be checked with av_fifo_size(). + * @deprecated use av_fifo_generic_peek_at() */ +attribute_deprecated static inline uint8_t *av_fifo_peek2(const AVFifoBuffer *f, int offs) { uint8_t *ptr = f->rptr + offs; @@ -175,5 +178,6 @@ static inline uint8_t *av_fifo_peek2(const AVFifoBuffer *f, int offs) ptr = f->end - (f->buffer - ptr); return ptr; } +#endif #endif /* AVUTIL_FIFO_H */ diff --git a/libavutil/tests/fifo.c b/libavutil/tests/fifo.c index 8a550e088b..a17d913233 100644 --- a/libavutil/tests/fifo.c +++ b/libavutil/tests/fifo.c @@ -30,14 +30,6 @@ int main(void) for (i = 0; av_fifo_space(fifo) >= sizeof(int); i++) av_fifo_generic_write(fifo, &i, sizeof(int), NULL); - /* peek at FIFO */ - n = av_fifo_size(fifo) / sizeof(int); - for (i = -n + 1; i < n; i++) { - int *v = (int *)av_fifo_peek2(fifo, i * sizeof(int)); - printf("%d: %d\n", i, *v); - } - printf("\n"); - /* peek_at at FIFO */ n = av_fifo_size(fifo) / sizeof(int); for (i = 0; i < n; i++) { diff --git a/libavutil/version.h b/libavutil/version.h index 3cac09cb96..0ba8f2d2d0 100644 --- a/libavutil/version.h +++ b/libavutil/version.h @@ -109,6 +109,7 @@ #define FF_API_DECLARE_ALIGNED (LIBAVUTIL_VERSION_MAJOR < 58) #define FF_API_COLORSPACE_NAME (LIBAVUTIL_VERSION_MAJOR < 58) #define FF_API_AV_MALLOCZ_ARRAY (LIBAVUTIL_VERSION_MAJOR < 58) +#define FF_API_OLD_FIFO (LIBAVUTIL_VERSION_MAJOR < 58) /** * @} diff --git a/tests/ref/fate/fifo b/tests/ref/fate/fifo index 2b18ed5ffc..1b0e005412 100644 --- a/tests/ref/fate/fifo +++ b/tests/ref/fate/fifo @@ -1,29 +1,3 @@ --12: 1 --11: 2 --10: 3 --9: 4 --8: 5 --7: 6 --6: 7 --5: 8 --4: 9 --3: 10 --2: 11 --1: 12 -0: 0 -1: 1 -2: 2 -3: 3 -4: 4 -5: 5 -6: 6 -7: 7 -8: 8 -9: 9 -10: 10 -11: 11 -12: 12 - 0: 0 1: 1 2: 2 From patchwork Fri Dec 31 10:53:04 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 32955 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a6b:cd86:0:0:0:0:0 with SMTP id d128csp13864279iog; Fri, 31 Dec 2021 02:53:33 -0800 (PST) X-Google-Smtp-Source: ABdhPJxUZDKMh1If1wzY/7q3U6JdJJqWVNkplDpdpUPMJ/BQ1Nma5dTjDxOS0tWl859x3OgveR33 X-Received: by 2002:a17:907:2cc4:: with SMTP id hg4mr27398227ejc.150.1640948013718; Fri, 31 Dec 2021 02:53:33 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1640948013; cv=none; d=google.com; s=arc-20160816; b=wSfvzQ61caU5zJ7zl1jJNWceEVpH458ngr7kipu2sDp4Rzpk4J+W1AHyThtvqJHjgO Dv9nzBnJ4louGr4hAzHoq8HViCwoaN7Q3A3ne9FdFMYrBr9NJYQENJjZpg5Nd51uBM2p XC5/2tf/ovw7rDM8E34zvDApQsqeocvIqQjRV88cOJc/PkXLwMQTS8tuKprhrU1AlyZu 7kHmBkfF9U/Fx8ImojF7hgYhLOo9JzTOHNdYgX0rXYDtqxUXL9rEJCBLeN8NPbYLkbDL NZKTFkO2Fyq1eCUSL5iKgJ5asquoj3JdDMokIEgUoHAUzw2ETCNHwO3Jq/2aEcnKDjiC OrjQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:references:in-reply-to:message-id :date:to:from:delivered-to; bh=G0wwQbY4G4nzKtfm6g+oQiGbofTtPo0Ukwm6Qa16t5g=; b=yXTkDCjRycaJf9vfvK5FGsCcp1+sWpev/FhhSnh2O9rx4+4CMg8za0Rahg4WnB9oGj Mzonlx8/+bw+oXb0S8z3JIWUfHpPQfUpcm8uPzt17xEMzOY+tUmU2mn1EN04PRESo4kg TGDoGaY0AKtAnTD36zQtx6PV1R9QN1IlJUt/Hjz2cGI9BUyLTEJEV10u9srU6MznpRu6 PcNEwgjNeP5G2dhvBMu3R3luMpB0CQL1iDFzbL5n1P6DKvpn4/J/rqz3V8vVroA3YCo0 tpCt9PDE53OPw1TZgINO0HniOrVLWBPVTH0jW906Z1Wj7eTY0BNoPbkIxt58cUrxPcn0 Cy7w== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id dm13si15067611ejc.579.2021.12.31.02.53.32; Fri, 31 Dec 2021 02:53:33 -0800 (PST) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id D2EDC68AFD1; Fri, 31 Dec 2021 12:53:28 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail0.khirnov.net (red.khirnov.net [176.97.15.12]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id D0CAC680044 for ; Fri, 31 Dec 2021 12:53:21 +0200 (EET) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 897F4240511 for ; Fri, 31 Dec 2021 11:53:20 +0100 (CET) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id PLaOrf_8AtBB for ; Fri, 31 Dec 2021 11:53:19 +0100 (CET) Received: from libav.khirnov.net (libav.khirnov.net [IPv6:2a00:c500:561:201::7]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256 client-signature RSA-PSS (2048 bits) client-digest SHA256) (Client CN "libav.khirnov.net", Issuer "smtp.khirnov.net SMTP CA" (verified OK)) by mail0.khirnov.net (Postfix) with ESMTPS id 97B622404FE for ; Fri, 31 Dec 2021 11:53:19 +0100 (CET) Received: by libav.khirnov.net (Postfix, from userid 1000) id A10013A0743; Fri, 31 Dec 2021 11:53:19 +0100 (CET) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Fri, 31 Dec 2021 11:53:04 +0100 Message-Id: <20211231105307.30946-4-anton@khirnov.net> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20211231105307.30946-1-anton@khirnov.net> References: <20211231105307.30946-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 4/7] lavu/fifo: simplify av_fifo_alloc() 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: 9EedPtJC2z74 Turn it into a wrapper around av_fifo_alloc_array(). --- libavutil/fifo.c | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/libavutil/fifo.c b/libavutil/fifo.c index 1060aedf13..5eee18a8c8 100644 --- a/libavutil/fifo.c +++ b/libavutil/fifo.c @@ -24,9 +24,10 @@ #include "common.h" #include "fifo.h" -static AVFifoBuffer *fifo_alloc_common(void *buffer, size_t size) +AVFifoBuffer *av_fifo_alloc_array(size_t nmemb, size_t size) { AVFifoBuffer *f; + void *buffer = av_malloc_array(nmemb, size); if (!buffer) return NULL; f = av_mallocz(sizeof(AVFifoBuffer)); @@ -35,21 +36,14 @@ static AVFifoBuffer *fifo_alloc_common(void *buffer, size_t size) return NULL; } f->buffer = buffer; - f->end = f->buffer + size; + f->end = f->buffer + nmemb * size; av_fifo_reset(f); return f; } AVFifoBuffer *av_fifo_alloc(unsigned int size) { - void *buffer = av_malloc(size); - return fifo_alloc_common(buffer, size); -} - -AVFifoBuffer *av_fifo_alloc_array(size_t nmemb, size_t size) -{ - void *buffer = av_malloc_array(nmemb, size); - return fifo_alloc_common(buffer, nmemb * size); + return av_fifo_alloc_array(size, 1); } void av_fifo_free(AVFifoBuffer *f) From patchwork Fri Dec 31 10:53:05 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 32958 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a6b:cd86:0:0:0:0:0 with SMTP id d128csp13864536iog; Fri, 31 Dec 2021 02:54:02 -0800 (PST) X-Google-Smtp-Source: ABdhPJyGHWfvfGNwSyhYX0XpaX+AHRML6MPe2iZXgUwH6vTWDy2/P3EWxSu0Dfvk7kaVKAy7kvlM X-Received: by 2002:a05:6402:f0f:: with SMTP id i15mr3672981eda.97.1640948042038; Fri, 31 Dec 2021 02:54:02 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1640948042; cv=none; d=google.com; s=arc-20160816; b=selkQdVzyweHA+t0AJKqcgBPR+6/NymwFBKTLfOocixtiCU6/UUt8p3fmez5XZ0Hy1 LujxzouZYDzqzQCI5zhS83blOWqoQFamXbCSFsa76S6nl8BPgewfEICKZZ/dKyYMP0Ry N1QjfJpJsQwywabeYDhA82yxOTqswWeWLf5pRnxL/mvzDfEPzbAlpgRahV0NuaHS5S/E U2G6bSllSzMPlJ5YtBjVSS7rxNh7JzF5zbSCgxd5Y4Ym8ZPic4I55GuaPj0eC+bf5IuL 4VzUwHBVFSm0/tc8XM+mbc1au3FCsHJWWZdLblT7OAvu4sDGQ0e8CrObQSzKDWQt43uA Ik0g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:references:in-reply-to:message-id :date:to:from:delivered-to; bh=5HP3smqSBRlYpPBEtmurvxv+9M5jI/vIuD4IEjPNgUk=; b=Fa+m0/28nB0QcmvBAw42pvyKNOOP7PwxX/WOm/1IQHkDn+FXV9l/Wbj70tL6IJATkF K4eFElnexYokJLRP872dtUnuIh6osD9U68xXehYihZfQEnk95YzzNLNo6Yt8N13aC15t X85e/YIVl0/igIQpsn4VHqt1Q8dthbQaKdbQxAqW+qY8cZYAlX0apOJvV/YkRjvbm9BO CGJUtKQyNrTjQPqPksWIRf2PG+oGvfVWl5JvRWUiRcBPcqtQWS8IL2uihXeFb8n0hNmU 1AFIHTekAYD0U3/pLXgfl3xNiBmDvyLAIAhRNQ3TOcAQm8eqBghcnauMa2GgtQY650Jt xOQg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id ox26si11774477ejb.331.2021.12.31.02.54.01; Fri, 31 Dec 2021 02:54:02 -0800 (PST) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id A932668AFF0; Fri, 31 Dec 2021 12:53:31 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail0.khirnov.net (red.khirnov.net [176.97.15.12]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id C8C42680044 for ; Fri, 31 Dec 2021 12:53:22 +0200 (EET) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 24B34240179 for ; Fri, 31 Dec 2021 11:53:22 +0100 (CET) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id a3NcqjqwogKY for ; Fri, 31 Dec 2021 11:53:21 +0100 (CET) Received: from libav.khirnov.net (libav.khirnov.net [IPv6:2a00:c500:561:201::7]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256 client-signature RSA-PSS (2048 bits) client-digest SHA256) (Client CN "libav.khirnov.net", Issuer "smtp.khirnov.net SMTP CA" (verified OK)) by mail0.khirnov.net (Postfix) with ESMTPS id 961A124017E for ; Fri, 31 Dec 2021 11:53:19 +0100 (CET) Received: by libav.khirnov.net (Postfix, from userid 1000) id A59E33A0746; Fri, 31 Dec 2021 11:53:19 +0100 (CET) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Fri, 31 Dec 2021 11:53:05 +0100 Message-Id: <20211231105307.30946-5-anton@khirnov.net> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20211231105307.30946-1-anton@khirnov.net> References: <20211231105307.30946-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 5/7] lavu/fifo: do not copy the whole fifo when reallocating 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: DT3baWfGh7Ub av_realloc() the buffer and only move the part of the ring buffer that needs it. Also avoids allocating a temporary fifo. --- libavutil/fifo.c | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/libavutil/fifo.c b/libavutil/fifo.c index 5eee18a8c8..171c1aa9cd 100644 --- a/libavutil/fifo.c +++ b/libavutil/fifo.c @@ -27,7 +27,7 @@ AVFifoBuffer *av_fifo_alloc_array(size_t nmemb, size_t size) { AVFifoBuffer *f; - void *buffer = av_malloc_array(nmemb, size); + void *buffer = av_realloc_array(NULL, nmemb, size); if (!buffer) return NULL; f = av_mallocz(sizeof(AVFifoBuffer)); @@ -83,17 +83,31 @@ int av_fifo_realloc2(AVFifoBuffer *f, unsigned int new_size) unsigned int old_size = f->end - f->buffer; if (old_size < new_size) { - int len = av_fifo_size(f); - AVFifoBuffer *f2 = av_fifo_alloc(new_size); + size_t offset_r = f->rptr - f->buffer; + size_t offset_w = f->wptr - f->buffer; + uint8_t *tmp; - if (!f2) + tmp = av_realloc(f->buffer, new_size); + if (!tmp) return AVERROR(ENOMEM); - av_fifo_generic_read(f, f2->buffer, len, NULL); - f2->wptr += len; - f2->wndx += len; - av_free(f->buffer); - *f = *f2; - av_free(f2); + + // move the data from the beginning of the ring buffer + // to the newly allocated space + // the second condition distinguishes full vs empty fifo + if (offset_w <= offset_r && av_fifo_size(f)) { + const size_t copy = FFMIN(new_size - old_size, offset_w); + memcpy(tmp + old_size, tmp, copy); + if (copy < offset_w) { + memmove(tmp, tmp + copy , offset_w - copy); + offset_w -= copy; + } else + offset_w = old_size + copy; + } + + f->buffer = tmp; + f->end = f->buffer + new_size; + f->rptr = f->buffer + offset_r; + f->wptr = f->buffer + offset_w; } return 0; } From patchwork Fri Dec 31 10:53:06 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 32961 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a6b:cd86:0:0:0:0:0 with SMTP id d128csp13864798iog; Fri, 31 Dec 2021 02:54:33 -0800 (PST) X-Google-Smtp-Source: ABdhPJwSFuhGJ5uDnE54vVjFYYqJ98LuedVSPn2THh5FFr/cuIWvFVb6tCzTO6W/5Whb1Hsiam7t X-Received: by 2002:a17:907:6d08:: with SMTP id sa8mr27105118ejc.28.1640948073563; Fri, 31 Dec 2021 02:54:33 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1640948073; cv=none; d=google.com; s=arc-20160816; b=gBhDKC3VqjJkgYh4EJ+xYE99c6AO7WihLROqiVSIbzhfszggiIGwKcmCL/QF+8tJ+w 8YsmesJw9UgA/qstpdGytSM72RZ07dLs3jz9p8StZSrZYknHN/DnoainKiy+uUFsf+IZ 3nv7ZFC7q61BBppOt02tZjde0+YMHzalhUeFigFnfU+7Jg9rUSn6cirVdLMNJvvt29hV vY5blPXCMam+/Tk2kGHHTWmzgxuV4ekPEZAInggs8E0ESFLMgSBjQBBlFmIdqA3YEHlK H2hegUjWyewrNRw7k8woJLbXXvDdXdD2FHREJChcK0oZ6MSEJobt41bF+4+0aaERhTRR 4EOA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:references:in-reply-to:message-id :date:to:from:delivered-to; bh=+nQSqQAPRRQJrDChtFcoBtoxUgw/mXpjrczxLsB9mEU=; b=VwFRvsR5CshOy3XAhUFKy+InOFkKC+2e0KS6QTsmtQ/Rs0xZ6xq4uf20059m8UjH6y 2oCkgh1nbSNSFONjnQUy/YdP+j5DOU/ecoGWL/tMpHYhhPMSxDlJH4YKA7QsDKg3QuTt LJlFqgVISraFkbSbUbH7cLT3pTjtQcIfkileFVUMltZyS8nPudrKDe1iVRn2Zn99rDTY NuRi+8JQLRrFvxrVdE7H1WGx7TmHo58StGthZZ4FvnG1fSFNvrdnSyr29xuZEsnGT/kX j4tkDimFkGMy7oDUAKwl6GXByr6NKV1nntXOdJol7kYOe7mnvkMe9DC38RETo37CRXMT 7LDg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id z20si14545404edi.289.2021.12.31.02.54.33; Fri, 31 Dec 2021 02:54:33 -0800 (PST) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id D432168AFF8; Fri, 31 Dec 2021 12:53:35 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail0.khirnov.net (red.khirnov.net [176.97.15.12]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 2144168AF88 for ; Fri, 31 Dec 2021 12:53:27 +0200 (EET) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 7B6D824050B for ; Fri, 31 Dec 2021 11:53:23 +0100 (CET) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id aDGc4DjG5dGR for ; Fri, 31 Dec 2021 11:53:22 +0100 (CET) Received: from libav.khirnov.net (libav.khirnov.net [IPv6:2a00:c500:561:201::7]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256 client-signature RSA-PSS (2048 bits) client-digest SHA256) (Client CN "libav.khirnov.net", Issuer "smtp.khirnov.net SMTP CA" (verified OK)) by mail0.khirnov.net (Postfix) with ESMTPS id AD6B3240507 for ; Fri, 31 Dec 2021 11:53:19 +0100 (CET) Received: by libav.khirnov.net (Postfix, from userid 1000) id AA7C83A0769; Fri, 31 Dec 2021 11:53:19 +0100 (CET) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Fri, 31 Dec 2021 11:53:06 +0100 Message-Id: <20211231105307.30946-6-anton@khirnov.net> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20211231105307.30946-1-anton@khirnov.net> References: <20211231105307.30946-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 6/7] lavu/fifo: drop useless comments 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: cTK7DHFhUINl This object was never intended to be thread-safe, so these carry no useful information. --- libavutil/fifo.c | 5 ----- 1 file changed, 5 deletions(-) diff --git a/libavutil/fifo.c b/libavutil/fifo.c index 171c1aa9cd..f38e8ff089 100644 --- a/libavutil/fifo.c +++ b/libavutil/fifo.c @@ -144,7 +144,6 @@ int av_fifo_generic_write(AVFifoBuffer *f, void *src, int size, memcpy(wptr, src, len); src = (uint8_t *)src + len; } -// Write memory barrier needed for SMP here in theory wptr += len; if (wptr >= f->end) wptr = f->buffer; @@ -197,7 +196,6 @@ int av_fifo_generic_peek_at(AVFifoBuffer *f, void *dest, int offset, int buf_siz int av_fifo_generic_peek(AVFifoBuffer *f, void *dest, int buf_size, void (*func)(void *, void *, int)) { -// Read memory barrier needed for SMP here in theory uint8_t *rptr = f->rptr; do { @@ -208,7 +206,6 @@ int av_fifo_generic_peek(AVFifoBuffer *f, void *dest, int buf_size, memcpy(dest, rptr, len); dest = (uint8_t *)dest + len; } -// memory barrier needed for SMP here in theory rptr += len; if (rptr >= f->end) rptr -= f->end - f->buffer; @@ -221,7 +218,6 @@ int av_fifo_generic_peek(AVFifoBuffer *f, void *dest, int buf_size, int av_fifo_generic_read(AVFifoBuffer *f, void *dest, int buf_size, void (*func)(void *, void *, int)) { -// Read memory barrier needed for SMP here in theory do { int len = FFMIN(f->end - f->rptr, buf_size); if (func) @@ -230,7 +226,6 @@ int av_fifo_generic_read(AVFifoBuffer *f, void *dest, int buf_size, memcpy(dest, f->rptr, len); dest = (uint8_t *)dest + len; } -// memory barrier needed for SMP here in theory av_fifo_drain(f, len); buf_size -= len; } while (buf_size > 0); From patchwork Fri Dec 31 10:53:07 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 32960 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a6b:cd86:0:0:0:0:0 with SMTP id d128csp13864726iog; Fri, 31 Dec 2021 02:54:24 -0800 (PST) X-Google-Smtp-Source: ABdhPJxspguO2SHPulabOFVd939bmZBMeVuoKAjk9+cBo3Tj96DJYfUEzpB9ufOEfMrNeQY5mWGO X-Received: by 2002:a17:906:5f97:: with SMTP id a23mr8495051eju.154.1640948064474; Fri, 31 Dec 2021 02:54:24 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1640948064; cv=none; d=google.com; s=arc-20160816; b=iaTW+msMzfGgKnX3d+7//cZNrTIkoAdM3H2YbUWCIOfXpix/0VOVx5YEKeHh3u/Skf AR9+ghcDYvDVRxkHKMj3q9zRsr0hlX6DgZQaMSEZRRghrfSFiK0jU0s3wAirTKD0MoNU PgYb84CuiFH3YLSDjrV00Mn6bmDuoa7xD7mw7zagnrLAdrjaASsLpjECQr4Fa/VvmZq3 KMrMoQD2dBBPRi4ZAkRgnER/ePx6gp8uUsHZUUfdHyXl+7hMZxLNlBcoPtDSpM3lADbC N352y9c+c1LImEcPT7NqdEbcb8hqZ7aTraEpgdDkQPta2eDMWVA0c/Y97vv7+SCTgIJq wQ/g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:references:in-reply-to:message-id :date:to:from:delivered-to; bh=7oc4XZDTeyAjDTfrgawXNKaswL1VmdSTE/dHAwDs9eU=; b=UX5h2AUnJk1/5+++4aCbtI90TMdXrdBDDNNwPvBhdkk5EOVKLPI0d7y0dQwo0ZdNCK Yojc4eOIe/fNSRpDA19J43Kkz8mXDSZ5dNIRQSyaaxdCyVSuvh2S2qbejj7ZNcW1G5Il 2N8O7P0D0VSdyNUtUB4OSc8wfcid2cALcX6EidC9kEzXAF3UNC+3AoxBLc47biePIy1n qBLYCTDIRi92zuf4GBYdV46wrOWD4fP+fIoN5bPXIhUAAOS13o123IbNg6xUXSMskBGH +qEPr9e+faS1jzm/8IsW8MEhD6+NEzUTK72+X5FJQI4jUI+F3miDw9bhFpbiqc/fVw54 4xeQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id d25si11842324eje.732.2021.12.31.02.54.24; Fri, 31 Dec 2021 02:54:24 -0800 (PST) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id C3BEF68B009; Fri, 31 Dec 2021 12:53:34 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail0.khirnov.net (red.khirnov.net [176.97.15.12]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 16A1168AFF5 for ; Fri, 31 Dec 2021 12:53:27 +0200 (EET) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id C0AFA240506 for ; Fri, 31 Dec 2021 11:53:22 +0100 (CET) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id zPyIdCzSSI67 for ; Fri, 31 Dec 2021 11:53:22 +0100 (CET) Received: from libav.khirnov.net (libav.khirnov.net [IPv6:2a00:c500:561:201::7]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256 client-signature RSA-PSS (2048 bits) client-digest SHA256) (Client CN "libav.khirnov.net", Issuer "smtp.khirnov.net SMTP CA" (verified OK)) by mail0.khirnov.net (Postfix) with ESMTPS id B2E5D24050B for ; Fri, 31 Dec 2021 11:53:19 +0100 (CET) Received: by libav.khirnov.net (Postfix, from userid 1000) id AEEFA3A078F; Fri, 31 Dec 2021 11:53:19 +0100 (CET) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Fri, 31 Dec 2021 11:53:07 +0100 Message-Id: <20211231105307.30946-7-anton@khirnov.net> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20211231105307.30946-1-anton@khirnov.net> References: <20211231105307.30946-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 7/7] lavu/fifo: return errors on trying to read/write too much 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: 16AugmPoen80 Trying to write too much will currently overwrite previous data. Trying to read too much will either av_assert2() in av_fifo_drain() or return old data. Trying to peek too much will either av_assert2() in av_fifo_generic_peek_at() or return old data. Return an error code in all these cases, which is safer and more consistent. --- libavutil/fifo.c | 18 +++++++++++------- libavutil/fifo.h | 8 +++++++- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/libavutil/fifo.c b/libavutil/fifo.c index f38e8ff089..d741bdd395 100644 --- a/libavutil/fifo.c +++ b/libavutil/fifo.c @@ -134,6 +134,9 @@ int av_fifo_generic_write(AVFifoBuffer *f, void *src, int size, uint32_t wndx= f->wndx; uint8_t *wptr= f->wptr; + if (size > av_fifo_space(f)) + return AVERROR(ENOSPC); + do { int len = FFMIN(f->end - wptr, size); if (func) { @@ -159,13 +162,8 @@ int av_fifo_generic_peek_at(AVFifoBuffer *f, void *dest, int offset, int buf_siz { uint8_t *rptr = f->rptr; - av_assert2(offset >= 0); - - /* - * *ndx are indexes modulo 2^32, they are intended to overflow, - * to handle *ndx greater than 4gb. - */ - av_assert2(buf_size + (unsigned)offset <= f->wndx - f->rndx); + if (offset < 0 || buf_size > av_fifo_size(f) - offset) + return AVERROR(EINVAL); if (offset >= f->end - rptr) rptr += offset - (f->end - f->buffer); @@ -198,6 +196,9 @@ int av_fifo_generic_peek(AVFifoBuffer *f, void *dest, int buf_size, { uint8_t *rptr = f->rptr; + if (buf_size > av_fifo_size(f)) + return AVERROR(EINVAL); + do { int len = FFMIN(f->end - rptr, buf_size); if (func) @@ -218,6 +219,9 @@ int av_fifo_generic_peek(AVFifoBuffer *f, void *dest, int buf_size, int av_fifo_generic_read(AVFifoBuffer *f, void *dest, int buf_size, void (*func)(void *, void *, int)) { + if (buf_size > av_fifo_size(f)) + return AVERROR(EINVAL); + do { int len = FFMIN(f->end - f->rptr, buf_size); if (func) diff --git a/libavutil/fifo.h b/libavutil/fifo.h index 37da9f14c2..53b668aa17 100644 --- a/libavutil/fifo.h +++ b/libavutil/fifo.h @@ -91,6 +91,8 @@ int av_fifo_space(const AVFifoBuffer *f); * @param buf_size number of bytes to read * @param func generic read function * @param dest data destination + * + * @return a non-negative number on success, a negative error code on failure */ int av_fifo_generic_peek_at(AVFifoBuffer *f, void *dest, int offset, int buf_size, void (*func)(void*, void*, int)); @@ -101,6 +103,8 @@ int av_fifo_generic_peek_at(AVFifoBuffer *f, void *dest, int offset, int buf_siz * @param buf_size number of bytes to read * @param func generic read function * @param dest data destination + * + * @return a non-negative number on success, a negative error code on failure */ int av_fifo_generic_peek(AVFifoBuffer *f, void *dest, int buf_size, void (*func)(void*, void*, int)); @@ -110,6 +114,8 @@ int av_fifo_generic_peek(AVFifoBuffer *f, void *dest, int buf_size, void (*func) * @param buf_size number of bytes to read * @param func generic read function * @param dest data destination + * + * @return a non-negative number on success, a negative error code on failure */ int av_fifo_generic_read(AVFifoBuffer *f, void *dest, int buf_size, void (*func)(void*, void*, int)); @@ -124,7 +130,7 @@ int av_fifo_generic_read(AVFifoBuffer *f, void *dest, int buf_size, void (*func) * func must return the number of bytes written to dest_buf, or <= 0 to * indicate no more data available to write. * If func is NULL, src is interpreted as a simple byte array for source data. - * @return the number of bytes written to the FIFO + * @return the number of bytes written to the FIFO or a negative error code on failure */ int av_fifo_generic_write(AVFifoBuffer *f, void *src, int size, int (*func)(void*, void*, int));