From patchwork Fri Aug 16 03:05:25 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 14548 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 083B8448270 for ; Fri, 16 Aug 2019 06:20:52 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id E3B8E68AD5C; Fri, 16 Aug 2019 06:20:51 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wr1-f67.google.com (mail-wr1-f67.google.com [209.85.221.67]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id DD37168AD30 for ; Fri, 16 Aug 2019 06:20:45 +0300 (EEST) Received: by mail-wr1-f67.google.com with SMTP id j16so163530wrr.8 for ; Thu, 15 Aug 2019 20:20:45 -0700 (PDT) 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=Y3s00uj8K3rrh2v0Snan4QBMcLKmyYt5OBaX1hCGh3k=; b=BPL58/3dVWk0idn15lJBLKlNT4nKPdIYTDvfynOmeH5ZE6eiRRqeA3ZU6+6NEINvg0 g7QZf5SCrfuYMzOQP6FYtwmMfjAsdnOr0ok1VjqBV8cUQ8K8Yn/0sX2mppsKolJomOiY Rp+AAaxrgRnnzPAjWX5HWcRsgGofmjvUAOJyrSxvlW+PH/SYE45AVPiIYqkyJdTzDzgp VwaK7E91JtVPfiU4lExjNl9FQa1WXGXaxwFZHwNXRM6BHSew3QfOhtwydHi2oLyjOjDH EABbliO0qjf0A9xWpKHFSBgn1ZmuT4dcQpqtaraqgqI2t/P6sZMW0fRdvs9G+BMVw0ZL N1xQ== 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=Y3s00uj8K3rrh2v0Snan4QBMcLKmyYt5OBaX1hCGh3k=; b=AIaHXMxFmM6wUNr08lVH4ssxbQ50NiAjjbjEeF5btGfKgh2GOWEfvlGk8mK1SP0y3d l2dBsDoWHZjq42bb1nM+khoq9NEsYYaBZY+Hmjvk43FQm59MK7MTPlko5vlE8+tlRTHY ROod/hTTx062B9YEYod88cJ/UxJ0XTHLkHrZbQUXSLgjQlbEHQ3Ih+aHxhY3i+9uheJP thet0ouaDdx7AaZqM4pAGlkDcnruxay7WuI5WGc3CyfF/KhGi1+J+SO70QCC3+Ev6dRX 8PrL+7xHJ9vJUecSQV+d8Lr+nSaWVTj4Gy6Abrg8BUmcy6mRNjJc2I88EqB0iyy7UrWC M3Yg== X-Gm-Message-State: APjAAAWhYorqwA6u/563mxlqpjEbczvQEjNNJY70SKCmaGW7qv5aDPMr MX+2KShH3Vo8h6dKrzPHJLF5P2Ad X-Google-Smtp-Source: APXvYqz7jLJClMfk0LLbYk4oCOcB5kTf+S6ey8d6yMmdg5pqCrH4SG7nXtL60PCGewLNNzIKdjbScg== X-Received: by 2002:adf:fd8b:: with SMTP id d11mr7522719wrr.300.1565925228701; Thu, 15 Aug 2019 20:13:48 -0700 (PDT) Received: from localhost.localdomain (ipbcc08b69.dynamic.kabel-deutschland.de. [188.192.139.105]) by smtp.gmail.com with ESMTPSA id x20sm8501603wrg.10.2019.08.15.20.13.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 15 Aug 2019 20:13:48 -0700 (PDT) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Fri, 16 Aug 2019 05:05:25 +0200 Message-Id: <20190816030531.4775-5-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190816030531.4775-1-andreas.rheinhardt@gmail.com> References: <20190816030531.4775-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 05/11] avcodec/h264_parser: Reuse the RBSP buffer 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" Up until now, the H.264 parser has allocated a new buffer for every frame in order to store the unescaped RBSP in case the part of the RBSP that will be unescaped contains 0x03 escape bytes. This is expensive and this commit changes this: The buffer is now kept and reused. For an AVC file with an average framesize of 15304 B (without in-band parameter sets etc.) and 132044 frames (131072 of which ended up in the results) this reduced the average time used by the H.264 parser from 87708 decicycles (excluding about 1100-1200 skips in each iteration) to 16963 decicycles (excluding about 10-15 skips in each iteration). The test has been iterated 10 times. Signed-off-by: Andreas Rheinhardt --- libavcodec/h264_parser.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/libavcodec/h264_parser.c b/libavcodec/h264_parser.c index 5f9a9c46ef..c200a2ab8e 100644 --- a/libavcodec/h264_parser.c +++ b/libavcodec/h264_parser.c @@ -53,6 +53,7 @@ typedef struct H264ParseContext { H264DSPContext h264dsp; H264POCContext poc; H264SEIContext sei; + H2645RBSP rbsp; int is_avc; int nal_length_size; int got_first; @@ -246,7 +247,7 @@ static inline int parse_nal_units(AVCodecParserContext *s, const uint8_t * const buf, int buf_size) { H264ParseContext *p = s->priv_data; - H2645RBSP rbsp = { NULL }; + H2645RBSP *rbsp = &p->rbsp; H2645NAL nal = { NULL }; int buf_index, next_avc; unsigned int pps_id; @@ -267,9 +268,10 @@ static inline int parse_nal_units(AVCodecParserContext *s, if (!buf_size) return 0; - av_fast_padded_malloc(&rbsp.rbsp_buffer, &rbsp.rbsp_buffer_alloc_size, buf_size); - if (!rbsp.rbsp_buffer) + av_fast_padded_malloc(&rbsp->rbsp_buffer, &rbsp->rbsp_buffer_alloc_size, buf_size); + if (!rbsp->rbsp_buffer) return AVERROR(ENOMEM); + rbsp->rbsp_buffer_size = 0; buf_index = 0; next_avc = p->is_avc ? 0 : buf_size; @@ -308,7 +310,7 @@ static inline int parse_nal_units(AVCodecParserContext *s, } break; } - consumed = ff_h2645_extract_rbsp(buf + buf_index, src_length, &rbsp, &nal, 1); + consumed = ff_h2645_extract_rbsp(buf + buf_index, src_length, rbsp, &nal, 1); if (consumed < 0) break; @@ -554,18 +556,15 @@ static inline int parse_nal_units(AVCodecParserContext *s, p->last_frame_num = p->poc.frame_num; } - av_freep(&rbsp.rbsp_buffer); return 0; /* no need to evaluate the rest */ } } if (q264) { - av_freep(&rbsp.rbsp_buffer); return 0; } /* didn't find a picture! */ av_log(avctx, AV_LOG_ERROR, "missing picture in access unit with size %d\n", buf_size); fail: - av_freep(&rbsp.rbsp_buffer); return -1; } @@ -690,6 +689,8 @@ static void h264_close(AVCodecParserContext *s) ParseContext *pc = &p->pc; av_freep(&pc->buffer); + av_freep(&p->rbsp.rbsp_buffer); + p->rbsp.rbsp_buffer_alloc_size = 0; ff_h264_sei_uninit(&p->sei); ff_h264_ps_uninit(&p->ps);