From patchwork Mon Mar 30 11:45:20 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 18510 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 DBA56449A5D for ; Mon, 30 Mar 2020 14:46:00 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id CC1AF68AEC0; Mon, 30 Mar 2020 14:46:00 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail.red.khirnov.net (red.khirnov.net [176.97.15.12]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 0316268A659 for ; Mon, 30 Mar 2020 14:45:52 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail.red.khirnov.net (Postfix) with ESMTP id 684D12864DD for ; Mon, 30 Mar 2020 13:45:51 +0200 (CEST) Received: from mail.red.khirnov.net ([IPv6:::1]) by localhost (mail.red.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id Z1B-oe7iR6Et for ; Mon, 30 Mar 2020 13:45:50 +0200 (CEST) Received: from quelana.khirnov.net (unknown [IPv6:2002:b061:f0a:201:5e:e696:5100:0]) (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 "quelana.khirnov.net", Issuer "smtp.khirnov.net SMTP CA" (verified OK)) by mail.red.khirnov.net (Postfix) with ESMTPS id 3D5E9284F05 for ; Mon, 30 Mar 2020 13:45:50 +0200 (CEST) Received: from localhost (quelana.khirnov.net [IPv6:::1]) by quelana.khirnov.net (Postfix) with ESMTP id 8497C21258 for ; Mon, 30 Mar 2020 13:45:49 +0200 (CEST) Received: from quelana.khirnov.net ([IPv6:::1]) by localhost (quelana.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id vLWwhkomlbKo for ; Mon, 30 Mar 2020 13:45:37 +0200 (CEST) Received: from libav.daenerys.khirnov.net (libav.daenerys.khirnov.net [IPv6:2a00:c500:561:201::7]) by quelana.khirnov.net (Postfix) with ESMTP id 9DEEB21249 for ; Mon, 30 Mar 2020 13:45:37 +0200 (CEST) Received: by libav.daenerys.khirnov.net (Postfix, from userid 1000) id 0E46A20E0042; Mon, 30 Mar 2020 13:45:35 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Mon, 30 Mar 2020 13:45:20 +0200 Message-Id: <20200330114526.11086-1-anton@khirnov.net> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 1/7] libavutil: add API for exporting video frame quantizers 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" From: Juan De León This is intended to replace the deprecated the AV_FRAME_DATA_QP_TABLE* API and extend it to a wider range of codecs. In the future, it may also be extended to support other encoding parameters such as motion vectors. Additional changes by Anton Khirnov with suggestions by Lynne . Signed-off-by: Juan De León Signed-off-by: Michael Niedermayer Signed-off-by: Anton Khirnov --- libavutil/Makefile | 4 +- libavutil/frame.c | 1 + libavutil/frame.h | 5 ++ libavutil/video_enc_params.c | 78 +++++++++++++++++++++ libavutil/video_enc_params.h | 128 +++++++++++++++++++++++++++++++++++ 5 files changed, 215 insertions(+), 1 deletion(-) create mode 100644 libavutil/video_enc_params.c create mode 100644 libavutil/video_enc_params.h diff --git a/libavutil/Makefile b/libavutil/Makefile index 8feb029a3a..8c76052c97 100644 --- a/libavutil/Makefile +++ b/libavutil/Makefile @@ -79,6 +79,7 @@ HEADERS = adler32.h \ tree.h \ twofish.h \ version.h \ + video_enc_params.h \ xtea.h \ tea.h \ tx.h \ @@ -165,7 +166,8 @@ OBJS = adler32.o \ tx.o \ tx_float.o \ tx_double.o \ - tx_int32.o + tx_int32.o \ + video_enc_params.o OBJS-$(CONFIG_CUDA) += hwcontext_cuda.o OBJS-$(CONFIG_D3D11VA) += hwcontext_d3d11va.o diff --git a/libavutil/frame.c b/libavutil/frame.c index e4038096c2..2f9ba5b6c0 100644 --- a/libavutil/frame.c +++ b/libavutil/frame.c @@ -842,6 +842,7 @@ const char *av_frame_side_data_name(enum AVFrameSideDataType type) #endif case AV_FRAME_DATA_DYNAMIC_HDR_PLUS: return "HDR Dynamic Metadata SMPTE2094-40 (HDR10+)"; case AV_FRAME_DATA_REGIONS_OF_INTEREST: return "Regions Of Interest"; + case AV_FRAME_DATA_VIDEO_ENC_PARAMS: return "Video encoding parameters"; } return NULL; } diff --git a/libavutil/frame.h b/libavutil/frame.h index b5afb58634..fc67db0f6c 100644 --- a/libavutil/frame.h +++ b/libavutil/frame.h @@ -179,6 +179,11 @@ enum AVFrameSideDataType { * array element is implied by AVFrameSideData.size / AVRegionOfInterest.self_size. */ AV_FRAME_DATA_REGIONS_OF_INTEREST, + + /** + * Encoding parameters for a video frame, as described by AVVideoEncParams. + */ + AV_FRAME_DATA_VIDEO_ENC_PARAMS, }; enum AVActiveFormatDescription { diff --git a/libavutil/video_enc_params.c b/libavutil/video_enc_params.c new file mode 100644 index 0000000000..43668dcd12 --- /dev/null +++ b/libavutil/video_enc_params.c @@ -0,0 +1,78 @@ +/* + * 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 +#include +#include + +#include "buffer.h" +#include "common.h" +#include "frame.h" +#include "mem.h" +#include "video_enc_params.h" + +AVVideoEncParams *av_video_enc_params_alloc(unsigned int nb_blocks, size_t *out_size) +{ + AVVideoEncParams *par; + size_t size; + int i; + + size = sizeof(*par); + if (nb_blocks > SIZE_MAX / sizeof(AVVideoBlockParams) || + nb_blocks * sizeof(AVVideoBlockParams) > SIZE_MAX - size) + return NULL; + size += sizeof(AVVideoBlockParams) * nb_blocks; + + par = av_mallocz(size); + if (!par) + return NULL; + + par->nb_blocks = nb_blocks; + par->block_size = sizeof(AVVideoBlockParams); + par->blocks_offset = sizeof(*par); + + par->type = AV_VIDEO_ENC_PARAMS_NONE; + + if (out_size) + *out_size = size; + + return par; +} + +AVVideoEncParams *av_video_enc_params_create_side_data(AVFrame *frame, unsigned nb_blocks) +{ + AVBufferRef *buf; + AVVideoEncParams *par; + size_t size; + + par = av_video_enc_params_alloc(nb_blocks, &size); + if (!par) + return NULL; + buf = av_buffer_create((uint8_t *)par, size, NULL, NULL, 0); + if (!buf) { + av_freep(&par); + return NULL; + } + + if (!av_frame_new_side_data_from_buf(frame, AV_FRAME_DATA_VIDEO_ENC_PARAMS, buf)) { + av_buffer_unref(&buf); + return NULL; + } + + return par; +} diff --git a/libavutil/video_enc_params.h b/libavutil/video_enc_params.h new file mode 100644 index 0000000000..8f97bc7127 --- /dev/null +++ b/libavutil/video_enc_params.h @@ -0,0 +1,128 @@ +/* + * 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 + */ + +#ifndef AVUTIL_VIDEO_ENC_PARAMS_H +#define AVUTIL_VIDEO_ENC_PARAMS_H + +#include +#include + +#include "libavutil/avassert.h" +#include "libavutil/frame.h" + +enum AVVideoEncParamsType { + AV_VIDEO_ENC_PARAMS_NONE = -1, +}; + +/** + * Data structure for extracting block data, stored as an array in AVEncodeInfoFrame. + */ +typedef struct AVVideoBlockParams { + /** + * Distance in luma pixels from the top-left corner of the visible frame + * to the top-left corner of the block. + * Can be negative if top/right padding is present on the coded frame. + */ + int src_x, src_y; + /** + * Width and height of the block in luma pixels. + */ + int w, h; + + /** + * Difference between this block's final quantization parameter and the + * corresponding per-frame value. + */ + int delta_qp; +} AVVideoBlockParams; + +/** + * Video encoding parameters for a given frame. This struct is allocated along + * with an array of per-block AVVideoBlockParams descriptors. + * Must be allocated with av_video_enc_params_alloc(). + */ +typedef struct AVVideoEncParams { + /** + * Number of blocks in the array. + * + * May be 0, in which case no per-block information is present. In this case + * the values of blocks_offset / block_size are unspecified and should not + * be accessed. + */ + unsigned int nb_blocks; + /** + * Offset in bytes from the beginning of this structure at which the array + * of blocks starts. + */ + size_t blocks_offset; + /* + * Size of each block in bytes. May not match sizeof(AVEncodeInfoBlock). + */ + size_t block_size; + + /** + * Type of the parameters (the codec they are used with). + */ + enum AVVideoEncParamsType type; + + /** + * Base quantisation parameter for the frame. The final quantiser for a + * given block is a sum of this value with the per-block value (if it is + * present). + */ + int qp; + + /** + * Quantisation parameter offset from the base (per-frame) qp for a given + * plane (first index) and AC/DC coefficients (second index). + * I.e. the final qp for AC coefficients of the first chroma component of a + * given block is + * qp + delta_qp[1][1] + block->delta_qp + */ + int delta_qp[4][2]; +} AVVideoEncParams; + +/* + * Get the block at the specified {@code idx}. Must be between 0 and nb_blocks. + */ +static av_always_inline AVVideoBlockParams* +av_video_enc_params_block(AVVideoEncParams *par, unsigned int idx) +{ + av_assert0(idx < par->nb_blocks); + return (AVVideoBlockParams *)((uint8_t *)par + par->blocks_offset + + idx * par->block_size); +} + +/** + * Allocates memory for AVVideoEncParams plus an array of + * {@code nb_blocks} AVVideoBlockParams and initializes the variables. + * Can be freed with a normal av_free() call. + * + * @param out_size if non-NULL, the size of the resulting data array is written here. + */ +AVVideoEncParams *av_video_enc_params_alloc(unsigned int nb_blocks, size_t *out_size); + +/** + * Allocates memory for AVEncodeInfoFrame plus an array of + * {@code nb_blocks} AVEncodeInfoBlock in the given AVFrame {@code frame} + * as AVFrameSideData of type AV_FRAME_DATA_ENCODE_INFO + * and initializes the variables. + */ +AVVideoEncParams *av_video_enc_params_create_side_data(AVFrame *frame, unsigned int nb_blocks); + +#endif /* AVUTIL_VIDEO_ENC_PARAMS_H */ From patchwork Mon Mar 30 11:45:21 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 18509 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 EEFE8449A5D for ; Mon, 30 Mar 2020 14:45:59 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id CB56A68AE82; Mon, 30 Mar 2020 14:45:59 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail.red.khirnov.net (red.khirnov.net [176.97.15.12]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 00D8068A533 for ; Mon, 30 Mar 2020 14:45:52 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail.red.khirnov.net (Postfix) with ESMTP id 5DF5028658F for ; Mon, 30 Mar 2020 13:45:51 +0200 (CEST) Received: from mail.red.khirnov.net ([IPv6:::1]) by localhost (mail.red.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id 9AlMa-Qv8suU for ; Mon, 30 Mar 2020 13:45:51 +0200 (CEST) Received: from quelana.khirnov.net (unknown [IPv6:2a00:c500:61:23b::]) (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 "quelana.khirnov.net", Issuer "smtp.khirnov.net SMTP CA" (verified OK)) by mail.red.khirnov.net (Postfix) with ESMTPS id 0AF26286541 for ; Mon, 30 Mar 2020 13:45:51 +0200 (CEST) Received: from localhost (quelana.khirnov.net [IPv6:::1]) by quelana.khirnov.net (Postfix) with ESMTP id 7790121249 for ; Mon, 30 Mar 2020 13:45:50 +0200 (CEST) Received: from quelana.khirnov.net ([IPv6:::1]) by localhost (quelana.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id HFwid5kUpR_T for ; Mon, 30 Mar 2020 13:45:49 +0200 (CEST) Received: from libav.daenerys.khirnov.net (libav.daenerys.khirnov.net [IPv6:2a00:c500:561:201::7]) by quelana.khirnov.net (Postfix) with ESMTP id A3A2721253 for ; Mon, 30 Mar 2020 13:45:37 +0200 (CEST) Received: by libav.daenerys.khirnov.net (Postfix, from userid 1000) id 69CAB20E0044; Mon, 30 Mar 2020 13:45:36 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Mon, 30 Mar 2020 13:45:21 +0200 Message-Id: <20200330114526.11086-2-anton@khirnov.net> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200330114526.11086-1-anton@khirnov.net> References: <20200330114526.11086-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 2/7] vf_showinfo: support AV_FRAME_DATA_VIDEO_ENC_PARAMS 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" --- libavfilter/vf_showinfo.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/libavfilter/vf_showinfo.c b/libavfilter/vf_showinfo.c index 71534d1fa8..0025ee63fc 100644 --- a/libavfilter/vf_showinfo.c +++ b/libavfilter/vf_showinfo.c @@ -36,6 +36,7 @@ #include "libavutil/timestamp.h" #include "libavutil/timecode.h" #include "libavutil/mastering_display_metadata.h" +#include "libavutil/video_enc_params.h" #include "avfilter.h" #include "internal.h" @@ -170,6 +171,25 @@ static void dump_content_light_metadata(AVFilterContext *ctx, AVFrameSideData *s metadata->MaxCLL, metadata->MaxFALL); } +static void dump_video_enc_params(AVFilterContext *ctx, AVFrameSideData *sd) +{ + AVVideoEncParams *par = (AVVideoEncParams*)sd->data; + int plane, acdc; + + av_log(ctx, AV_LOG_INFO, "video encoding parameters: type %d; ", par->type); + if (par->qp) + av_log(ctx, AV_LOG_INFO, "qp=%d; ", par->qp); + for (plane = 0; plane < FF_ARRAY_ELEMS(par->delta_qp); plane++) + for (acdc = 0; acdc < FF_ARRAY_ELEMS(par->delta_qp[plane]); acdc++) { + int delta_qp = par->delta_qp[plane][acdc]; + if (delta_qp) + av_log(ctx, AV_LOG_INFO, "delta_qp[%d][%d]=%d; ", + plane, acdc, delta_qp); + } + if (par->nb_blocks) + av_log(ctx, AV_LOG_INFO, "%u blocks; ", par->nb_blocks); +} + static void dump_color_property(AVFilterContext *ctx, AVFrame *frame) { const char *color_range_str = av_color_range_name(frame->color_range); @@ -351,6 +371,9 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *frame) av_log(ctx, AV_LOG_INFO, "GOP timecode - %s", tcbuf); break; } + case AV_FRAME_DATA_VIDEO_ENC_PARAMS: + dump_video_enc_params(ctx, sd); + break; default: av_log(ctx, AV_LOG_WARNING, "unknown side data type %d (%d bytes)", sd->type, sd->size); From patchwork Mon Mar 30 11:45:22 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 18515 Delivered-To: andriy.gelman@gmail.com Received: by 2002:a0c:ab15:0:0:0:0:0 with SMTP id h21csp2134660qvb; Mon, 30 Mar 2020 04:46:23 -0700 (PDT) X-Google-Smtp-Source: ADFU+vu1m9NRWYw+Cajnm7r5x1Vqjtd06L6FWfasepxT+tyq/YJ0fsCVcJBsGKQ1LUACwNum76Xo X-Received: by 2002:a17:906:4006:: with SMTP id v6mr9894762ejj.249.1585568783304; Mon, 30 Mar 2020 04:46:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1585568783; cv=none; d=google.com; s=arc-20160816; b=MSlneqc9p+MULDZ1IIdNaZMGZS+9BPQN3abVhCk9X2hpPkxshWTa2ydLoB4tAkPVtV g7abbNiLjp4il+DEyLuSm27vFsyI6eRjeh1YTJIwf+XvhV8nTkzFJ9H74lcunx28vIvH fMpc29CP1HXluHNVGg085JI1hpnW8bWWZTkWmQOUtm0ebhIixtkd2X5+lCIKnnoa7Kgi IJj4QDue7D5f/2UtPRJGWjoArbcMn9dCBsmjy1iBJ6XEM9/O1ppKMqiMYRmgFW5A+P2D yEmrCLerS2B0D/GCyJslvtpQgrZe6e7E1cwFh1G5bBBAY0zMH82+MFnmi4l7aLtmz7ED hvWQ== 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=twOE6rT/nOwLIoUt/e+1TPQG0WEEC/sAjYWXs9AgCCQ=; b=Ic8AXbmLz/5qtMJcclOLzgM3Q7KOhF3T1C0q77xer0ilDxxaJkxxiL43RxPEieVvFy PGxInBjXwmMQaVH+oWv9BnfnKOvgo8Z+VSdfylnzp/qBGKl4gp9zEHwlpbe75Ku2Lckc Db4S1q4DWIOq1UIGKY2t3S2rkt4Zc7Cc5LV54n4toadN0HJs9R4DPlr2xsOaFR6HHDij M8WK4GzQFDuZztn91jr/YOdvJCraHmXJuuHicI0sD7wv7DaBKJKfc9mQxaJ2aGjDNyWO qCsedvwHXWovaGzmhC7OKktAtMk/QUC5TX5Dbv6Q/k1lMl7ychORW9paDhIdyE3d4GkV N80Q== 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 m6si8369026eje.289.2020.03.30.04.46.22; Mon, 30 Mar 2020 04:46:23 -0700 (PDT) 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 A54A968AF32; Mon, 30 Mar 2020 14:46:01 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail.red.khirnov.net (red.khirnov.net [176.97.15.12]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 050D268AA3D for ; Mon, 30 Mar 2020 14:45:52 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail.red.khirnov.net (Postfix) with ESMTP id 12FE3286541 for ; Mon, 30 Mar 2020 13:45:52 +0200 (CEST) Received: from mail.red.khirnov.net ([IPv6:::1]) by localhost (mail.red.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id R124enLnEhqO for ; Mon, 30 Mar 2020 13:45:51 +0200 (CEST) Received: from quelana.khirnov.net (unknown [IPv6:2a00:c500:61:23b::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) client-signature RSA-PSS (2048 bits)) (Client CN "quelana.khirnov.net", Issuer "smtp.khirnov.net SMTP CA" (verified OK)) by mail.red.khirnov.net (Postfix) with ESMTPS id A10B0284F05 for ; Mon, 30 Mar 2020 13:45:51 +0200 (CEST) Received: from localhost (quelana.khirnov.net [IPv6:::1]) by quelana.khirnov.net (Postfix) with ESMTP id 5CD4F21249 for ; Mon, 30 Mar 2020 13:45:51 +0200 (CEST) Received: from quelana.khirnov.net ([IPv6:::1]) by localhost (quelana.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id T2jCkIEget7z for ; Mon, 30 Mar 2020 13:45:50 +0200 (CEST) Received: from libav.daenerys.khirnov.net (libav.daenerys.khirnov.net [IPv6:2a00:c500:561:201::7]) by quelana.khirnov.net (Postfix) with ESMTP id A8CCF21254 for ; Mon, 30 Mar 2020 13:45:37 +0200 (CEST) Received: by libav.daenerys.khirnov.net (Postfix, from userid 1000) id 87C3820E0041; Mon, 30 Mar 2020 13:45:37 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Mon, 30 Mar 2020 13:45:22 +0200 Message-Id: <20200330114526.11086-3-anton@khirnov.net> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200330114526.11086-1-anton@khirnov.net> References: <20200330114526.11086-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 3/7] lavc: add a flag for exporting AVVideoEncParams from decoders 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" X-TUID: n6ZlVmfG0uD1 Content-Length: 2870 --- libavcodec/avcodec.h | 5 +++++ libavcodec/options_table.h | 1 + 2 files changed, 6 insertions(+) diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h index 78c483c25c..436a21f99c 100644 --- a/libavcodec/avcodec.h +++ b/libavcodec/avcodec.h @@ -1120,6 +1120,11 @@ typedef struct RcOverride{ * Export encoder Producer Reference Time through packet side data */ #define AV_CODEC_EXPORT_DATA_PRFT (1 << 1) +/** + * Decoding only. + * Export the AVVideoEncParams structure through frame side data. + */ +#define AV_CODEC_EXPORT_DATA_VIDEO_ENC_PARAMS (1 << 2) /** * Pan Scan area. diff --git a/libavcodec/options_table.h b/libavcodec/options_table.h index 54366747ca..695fa5c211 100644 --- a/libavcodec/options_table.h +++ b/libavcodec/options_table.h @@ -81,6 +81,7 @@ static const AVOption avcodec_options[] = { {"export_side_data", "Export metadata as side data", OFFSET(export_side_data), AV_OPT_TYPE_FLAGS, {.i64 = DEFAULT}, 0, UINT_MAX, A|V|S|D|E, "export_side_data"}, {"mvs", "export motion vectors through frame side data", 0, AV_OPT_TYPE_CONST, {.i64 = AV_CODEC_EXPORT_DATA_MVS}, INT_MIN, INT_MAX, V|D, "export_side_data"}, {"prft", "export Producer Reference Time through packet side data", 0, AV_OPT_TYPE_CONST, {.i64 = AV_CODEC_EXPORT_DATA_PRFT}, INT_MIN, INT_MAX, A|V|S|E, "export_side_data"}, +{"venc_params", "export video encoding parameters through frame side data", 0, AV_OPT_TYPE_CONST, {.i64 = AV_CODEC_EXPORT_DATA_VIDEO_ENC_PARAMS}, INT_MIN, INT_MAX, A|V|S|E, "export_side_data"}, {"time_base", NULL, OFFSET(time_base), AV_OPT_TYPE_RATIONAL, {.dbl = 0}, 0, INT_MAX}, {"g", "set the group of picture (GOP) size", OFFSET(gop_size), AV_OPT_TYPE_INT, {.i64 = 12 }, INT_MIN, INT_MAX, V|E}, {"ar", "set audio sampling rate (in Hz)", OFFSET(sample_rate), AV_OPT_TYPE_INT, {.i64 = DEFAULT }, 0, INT_MAX, A|D|E}, From patchwork Mon Mar 30 11:45:23 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 18512 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 72489449D38 for ; Mon, 30 Mar 2020 14:46:09 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 57F3168B096; Mon, 30 Mar 2020 14:46:09 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail.red.khirnov.net (red.khirnov.net [176.97.15.12]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 968A968A533 for ; Mon, 30 Mar 2020 14:46:06 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail.red.khirnov.net (Postfix) with ESMTP id 5107B2864DD for ; Mon, 30 Mar 2020 13:46:06 +0200 (CEST) Received: from mail.red.khirnov.net ([IPv6:::1]) by localhost (mail.red.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id DsNGQnSp__px for ; Mon, 30 Mar 2020 13:46:05 +0200 (CEST) Received: from quelana.khirnov.net (unknown [IPv6:2a00:c500:61:23b::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) client-signature RSA-PSS (2048 bits)) (Client CN "quelana.khirnov.net", Issuer "smtp.khirnov.net SMTP CA" (verified OK)) by mail.red.khirnov.net (Postfix) with ESMTPS id 95FEB284F05 for ; Mon, 30 Mar 2020 13:46:05 +0200 (CEST) Received: from localhost (quelana.khirnov.net [IPv6:::1]) by quelana.khirnov.net (Postfix) with ESMTP id 2DE3121249 for ; Mon, 30 Mar 2020 13:46:05 +0200 (CEST) Received: from quelana.khirnov.net ([IPv6:::1]) by localhost (quelana.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id s0BTggTLwtgP for ; Mon, 30 Mar 2020 13:45:37 +0200 (CEST) Received: from libav.daenerys.khirnov.net (libav.daenerys.khirnov.net [IPv6:2a00:c500:561:201::7]) by quelana.khirnov.net (Postfix) with ESMTP id A049121252 for ; Mon, 30 Mar 2020 13:45:37 +0200 (CEST) Received: by libav.daenerys.khirnov.net (Postfix, from userid 1000) id 41C1620E003B; Mon, 30 Mar 2020 13:45:37 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Mon, 30 Mar 2020 13:45:23 +0200 Message-Id: <20200330114526.11086-4-anton@khirnov.net> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200330114526.11086-1-anton@khirnov.net> References: <20200330114526.11086-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 4/7] mpegvideo: use the AVVideoEncParams API for exporting QP tables 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" Do it only when requested with the AV_CODEC_EXPORT_DATA_VIDEO_ENC_PARAMS flag. Drop previous code using the long-deprecated AV_FRAME_DATA_QP_TABLE* API. --- libavcodec/h263dec.c | 6 ++++-- libavcodec/mpeg12dec.c | 5 +++-- libavcodec/mpegpicture.c | 1 + libavcodec/mpegpicture.h | 1 + libavcodec/mpegvideo.c | 39 ++++++++++++++++++++++++++++-------- libavcodec/mpegvideo.h | 8 +++++++- libavcodec/rv10.c | 5 +++-- libavcodec/rv34.c | 5 +++-- libavutil/video_enc_params.h | 2 ++ 9 files changed, 55 insertions(+), 17 deletions(-) diff --git a/libavcodec/h263dec.c b/libavcodec/h263dec.c index 8ee844e298..a5793ea320 100644 --- a/libavcodec/h263dec.c +++ b/libavcodec/h263dec.c @@ -28,6 +28,8 @@ #define UNCHECKED_BITSTREAM_READER 1 #include "libavutil/cpu.h" +#include "libavutil/video_enc_params.h" + #include "avcodec.h" #include "error_resilience.h" #include "flv.h" @@ -693,12 +695,12 @@ frame_end: if ((ret = av_frame_ref(pict, s->current_picture_ptr->f)) < 0) return ret; ff_print_debug_info(s, s->current_picture_ptr, pict); - ff_mpv_export_qp_table(s, pict, s->current_picture_ptr, FF_QSCALE_TYPE_MPEG1); + ff_mpv_export_qp_table(s, pict, s->current_picture_ptr, AV_VIDEO_ENC_PARAMS_H263); } else if (s->last_picture_ptr) { if ((ret = av_frame_ref(pict, s->last_picture_ptr->f)) < 0) return ret; ff_print_debug_info(s, s->last_picture_ptr, pict); - ff_mpv_export_qp_table(s, pict, s->last_picture_ptr, FF_QSCALE_TYPE_MPEG1); + ff_mpv_export_qp_table(s, pict, s->last_picture_ptr, AV_VIDEO_ENC_PARAMS_H263); } if (s->last_picture_ptr || s->low_delay) { diff --git a/libavcodec/mpeg12dec.c b/libavcodec/mpeg12dec.c index 67ce59d451..f35da4ce80 100644 --- a/libavcodec/mpeg12dec.c +++ b/libavcodec/mpeg12dec.c @@ -32,6 +32,7 @@ #include "libavutil/imgutils.h" #include "libavutil/internal.h" #include "libavutil/stereo3d.h" +#include "libavutil/video_enc_params.h" #include "avcodec.h" #include "bytestream.h" @@ -2070,7 +2071,7 @@ static int slice_end(AVCodecContext *avctx, AVFrame *pict) if (ret < 0) return ret; ff_print_debug_info(s, s->current_picture_ptr, pict); - ff_mpv_export_qp_table(s, pict, s->current_picture_ptr, FF_QSCALE_TYPE_MPEG2); + ff_mpv_export_qp_table(s, pict, s->current_picture_ptr, AV_VIDEO_ENC_PARAMS_MPEG2); } else { if (avctx->active_thread_type & FF_THREAD_FRAME) s->picture_number++; @@ -2081,7 +2082,7 @@ static int slice_end(AVCodecContext *avctx, AVFrame *pict) if (ret < 0) return ret; ff_print_debug_info(s, s->last_picture_ptr, pict); - ff_mpv_export_qp_table(s, pict, s->last_picture_ptr, FF_QSCALE_TYPE_MPEG2); + ff_mpv_export_qp_table(s, pict, s->last_picture_ptr, AV_VIDEO_ENC_PARAMS_MPEG2); } } diff --git a/libavcodec/mpegpicture.c b/libavcodec/mpegpicture.c index 5fce25ec6e..6a079e795c 100644 --- a/libavcodec/mpegpicture.c +++ b/libavcodec/mpegpicture.c @@ -225,6 +225,7 @@ static int alloc_picture_tables(AVCodecContext *avctx, Picture *pic, int encodin pic->alloc_mb_width = mb_width; pic->alloc_mb_height = mb_height; + pic->alloc_mb_stride = mb_stride; return 0; } diff --git a/libavcodec/mpegpicture.h b/libavcodec/mpegpicture.h index 2db3d6733a..4bcd666797 100644 --- a/libavcodec/mpegpicture.h +++ b/libavcodec/mpegpicture.h @@ -69,6 +69,7 @@ typedef struct Picture { int alloc_mb_width; ///< mb_width used to allocate tables int alloc_mb_height; ///< mb_height used to allocate tables + int alloc_mb_stride; ///< mb_stride used to allocate tables AVBufferRef *mb_mean_buf; uint8_t *mb_mean; ///< Table for MB luminance diff --git a/libavcodec/mpegvideo.c b/libavcodec/mpegvideo.c index 22cab2854b..7b43c643f6 100644 --- a/libavcodec/mpegvideo.c +++ b/libavcodec/mpegvideo.c @@ -32,6 +32,8 @@ #include "libavutil/imgutils.h" #include "libavutil/internal.h" #include "libavutil/motion_vector.h" +#include "libavutil/video_enc_params.h" + #include "avcodec.h" #include "blockdsp.h" #include "h264chroma.h" @@ -1439,16 +1441,37 @@ void ff_print_debug_info(MpegEncContext *s, Picture *p, AVFrame *pict) s->mb_width, s->mb_height, s->mb_stride, s->quarter_sample); } -int ff_mpv_export_qp_table(MpegEncContext *s, AVFrame *f, Picture *p, int qp_type) +int ff_mpv_export_qp_table(MpegEncContext *s, AVFrame *f, Picture *p, + enum AVVideoEncParamsType qp_type) { - AVBufferRef *ref = av_buffer_ref(p->qscale_table_buf); - int offset = 2*s->mb_stride + 1; - if(!ref) + AVVideoEncParams *par; + unsigned int nb_mb = p->alloc_mb_height * p->alloc_mb_width; + unsigned int x, y; + + if (!(s->avctx->export_side_data & AV_CODEC_EXPORT_DATA_VIDEO_ENC_PARAMS)) + return 0; + + par = av_video_enc_params_create_side_data(f, nb_mb); + if (!par) return AVERROR(ENOMEM); - av_assert0(ref->size >= offset + s->mb_stride * ((f->height+15)/16)); - ref->size -= offset; - ref->data += offset; - return av_frame_set_qp_table(f, ref, s->mb_stride, qp_type); + + par->type = qp_type; + + for (y = 0; y < p->alloc_mb_height; y++) + for (x = 0; x < p->alloc_mb_width; x++) { + const unsigned int block_idx = y * p->alloc_mb_width + x; + const unsigned int mb_xy = y * p->alloc_mb_stride + x; + AVVideoBlockParams *b = av_video_enc_params_block(par, block_idx); + + b->src_x = x * 16; + b->src_y = y * 16; + b->w = 16; + b->h = 16; + + b->delta_qp = p->qscale_table[mb_xy]; + } + + return 0; } static inline int hpel_motion_lowres(MpegEncContext *s, diff --git a/libavcodec/mpegvideo.h b/libavcodec/mpegvideo.h index 29e692f245..410d015df2 100644 --- a/libavcodec/mpegvideo.h +++ b/libavcodec/mpegvideo.h @@ -58,6 +58,7 @@ #include "libavutil/opt.h" #include "libavutil/timecode.h" +#include "libavutil/video_enc_params.h" #define MAX_THREADS 32 @@ -713,7 +714,12 @@ void ff_mpeg_flush(AVCodecContext *avctx); void ff_print_debug_info(MpegEncContext *s, Picture *p, AVFrame *pict); -int ff_mpv_export_qp_table(MpegEncContext *s, AVFrame *f, Picture *p, int qp_type); +/** + * Export the qscale table if requested through + * AV_CODEC_EXPORT_DATA_VIDEO_ENC_PARAMS. + */ +int ff_mpv_export_qp_table(MpegEncContext *s, AVFrame *f, Picture *p, + enum AVVideoEncParamsType qp_type); void ff_write_quant_matrix(PutBitContext *pb, uint16_t *matrix); diff --git a/libavcodec/rv10.c b/libavcodec/rv10.c index 3b41d30b92..fd88dede2d 100644 --- a/libavcodec/rv10.c +++ b/libavcodec/rv10.c @@ -28,6 +28,7 @@ #include #include "libavutil/imgutils.h" +#include "libavutil/video_enc_params.h" #include "avcodec.h" #include "error_resilience.h" @@ -772,12 +773,12 @@ static int rv10_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, if ((ret = av_frame_ref(pict, s->current_picture_ptr->f)) < 0) return ret; ff_print_debug_info(s, s->current_picture_ptr, pict); - ff_mpv_export_qp_table(s, pict, s->current_picture_ptr, FF_QSCALE_TYPE_MPEG1); + ff_mpv_export_qp_table(s, pict, s->current_picture_ptr, AV_VIDEO_ENC_PARAMS_H263); } else if (s->last_picture_ptr) { if ((ret = av_frame_ref(pict, s->last_picture_ptr->f)) < 0) return ret; ff_print_debug_info(s, s->last_picture_ptr, pict); - ff_mpv_export_qp_table(s, pict,s->last_picture_ptr, FF_QSCALE_TYPE_MPEG1); + ff_mpv_export_qp_table(s, pict,s->last_picture_ptr, AV_VIDEO_ENC_PARAMS_H263); } if (s->last_picture_ptr || s->low_delay) { diff --git a/libavcodec/rv34.c b/libavcodec/rv34.c index d171e6e1bd..04339d57fc 100644 --- a/libavcodec/rv34.c +++ b/libavcodec/rv34.c @@ -26,6 +26,7 @@ #include "libavutil/imgutils.h" #include "libavutil/internal.h" +#include "libavutil/video_enc_params.h" #include "avcodec.h" #include "error_resilience.h" @@ -1617,13 +1618,13 @@ static int finish_frame(AVCodecContext *avctx, AVFrame *pict) if ((ret = av_frame_ref(pict, s->current_picture_ptr->f)) < 0) return ret; ff_print_debug_info(s, s->current_picture_ptr, pict); - ff_mpv_export_qp_table(s, pict, s->current_picture_ptr, FF_QSCALE_TYPE_MPEG1); + ff_mpv_export_qp_table(s, pict, s->current_picture_ptr, AV_VIDEO_ENC_PARAMS_H263); got_picture = 1; } else if (s->last_picture_ptr) { if ((ret = av_frame_ref(pict, s->last_picture_ptr->f)) < 0) return ret; ff_print_debug_info(s, s->last_picture_ptr, pict); - ff_mpv_export_qp_table(s, pict, s->last_picture_ptr, FF_QSCALE_TYPE_MPEG1); + ff_mpv_export_qp_table(s, pict, s->last_picture_ptr, AV_VIDEO_ENC_PARAMS_H263); got_picture = 1; } diff --git a/libavutil/video_enc_params.h b/libavutil/video_enc_params.h index 8f97bc7127..c1afcb3a63 100644 --- a/libavutil/video_enc_params.h +++ b/libavutil/video_enc_params.h @@ -27,6 +27,8 @@ enum AVVideoEncParamsType { AV_VIDEO_ENC_PARAMS_NONE = -1, + AV_VIDEO_ENC_PARAMS_MPEG2, + AV_VIDEO_ENC_PARAMS_H263, }; /** From patchwork Mon Mar 30 11:45:24 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 18516 Delivered-To: andriy.gelman@gmail.com Received: by 2002:a0c:ab15:0:0:0:0:0 with SMTP id h21csp2134864qvb; Mon, 30 Mar 2020 04:46:37 -0700 (PDT) X-Google-Smtp-Source: ADFU+vvbK2C43ngbmSMutQ5V7k6B/FPtKG7wQSLFKS3Wie+abeeACJy/2mcpsnCfg8ftETMPQqQq X-Received: by 2002:a05:6402:504:: with SMTP id m4mr11219736edv.367.1585568797107; Mon, 30 Mar 2020 04:46:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1585568797; cv=none; d=google.com; s=arc-20160816; b=T1cvOU0fGtzv8wKPlWK7WDruCpMFcu+MSNAIDqfrI/fffjuS1NjA6vvTcTdSxBUjk7 EbaDTC3pKHeobfYOWBSKISDNM/5y71vLekx+ZY8Fd0Rk3sdwQS7S6ri220jzd/l7JTIB OLn1a+lj01hHrAiI7wM/6tXhj8IYufkiud1rmvdloXMLMVQX7X4FFvFv+sJVAGcEoq23 0h/P+T2xMHnDES0Cmpbp3Do6ytQAuZbbquhySGwgytL27r/7hufrCAXYKs6i56enXRIT WF2hEOrz5dZvsd4EwHX4etRpMlhd73r+lZwsxp97vgDjtco14mVWzbnCUeWlY6TUEaSL ZIEA== 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=V9geHt87nOtrkVZXfitiIdPUrG9u8vWIzPLshgMNYs8=; b=mJ25xKhwcQ0p+Z1rlODBSTpkGzmc4XqAJWSsukBHURjakPFSV4w3APGJTRl4Lmz3j/ /CRdBj0KzfFeA21UrHDFBB1xvZm1MecH1w6At9qUHat/I4mAyDG5J1qVPSjOZtBvLKtn TYGfRzTlitmvOIgiMpesaG4Q2ulLCvRrXDCKtInmWlyJ6zmmuqmjMKyT905rZFDowc0G R+zeps/TutTI1b0tClCEkBEnR+WSpw2OIhQLdzSTCztTDK0kLE5fMzwH4KVXqNSJOdQk nqUPJ1I5wLMo7WGDfhUkQ4MByoR2k+/s2QAgaenVEofEvtxT8utl1v2wYTbHAvXUg1JU W6Aw== 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 w28si8539327edl.601.2020.03.30.04.46.36; Mon, 30 Mar 2020 04:46:37 -0700 (PDT) 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 0809068A659; Mon, 30 Mar 2020 14:46:03 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail.red.khirnov.net (red.khirnov.net [176.97.15.12]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id BC06868AA3D for ; Mon, 30 Mar 2020 14:45:53 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail.red.khirnov.net (Postfix) with ESMTP id 75323286595 for ; Mon, 30 Mar 2020 13:45:53 +0200 (CEST) Received: from mail.red.khirnov.net ([IPv6:::1]) by localhost (mail.red.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id 7lFTVqD-OFOC for ; Mon, 30 Mar 2020 13:45:53 +0200 (CEST) Received: from quelana.khirnov.net (unknown [IPv6:2002:b061:f0a:201:5e:e696:5100:0]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) client-signature RSA-PSS (2048 bits)) (Client CN "quelana.khirnov.net", Issuer "smtp.khirnov.net SMTP CA" (verified OK)) by mail.red.khirnov.net (Postfix) with ESMTPS id 06B35284F05 for ; Mon, 30 Mar 2020 13:45:53 +0200 (CEST) Received: from localhost (quelana.khirnov.net [IPv6:::1]) by quelana.khirnov.net (Postfix) with ESMTP id B830221249 for ; Mon, 30 Mar 2020 13:45:52 +0200 (CEST) Received: from quelana.khirnov.net ([IPv6:::1]) by localhost (quelana.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id EMoRbpu0RzYH for ; Mon, 30 Mar 2020 13:45:51 +0200 (CEST) Received: from libav.daenerys.khirnov.net (libav.daenerys.khirnov.net [IPv6:2a00:c500:561:201::7]) by quelana.khirnov.net (Postfix) with ESMTP id BC15421255 for ; Mon, 30 Mar 2020 13:45:37 +0200 (CEST) Received: by libav.daenerys.khirnov.net (Postfix, from userid 1000) id A786220E003B; Mon, 30 Mar 2020 13:45:37 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Mon, 30 Mar 2020 13:45:24 +0200 Message-Id: <20200330114526.11086-5-anton@khirnov.net> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200330114526.11086-1-anton@khirnov.net> References: <20200330114526.11086-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 5/7] h264dec: support exporting QP tables through the AVVideoEncParams API 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" X-TUID: B/F9MiTZEb/E Content-Length: 7830 --- libavcodec/h264_picture.c | 8 +++++- libavcodec/h264_slice.c | 9 +++++++ libavcodec/h264dec.c | 48 ++++++++++++++++++++++++++++++++++++ libavcodec/h264dec.h | 6 +++++ libavutil/video_enc_params.h | 1 + 5 files changed, 71 insertions(+), 1 deletion(-) diff --git a/libavcodec/h264_picture.c b/libavcodec/h264_picture.c index 2113947d1d..eec5e9fb9a 100644 --- a/libavcodec/h264_picture.c +++ b/libavcodec/h264_picture.c @@ -54,6 +54,7 @@ void ff_h264_unref_picture(H264Context *h, H264Picture *pic) av_buffer_unref(&pic->qscale_table_buf); av_buffer_unref(&pic->mb_type_buf); + av_buffer_unref(&pic->pps_buf); for (i = 0; i < 2; i++) { av_buffer_unref(&pic->motion_val_buf[i]); av_buffer_unref(&pic->ref_index_buf[i]); @@ -77,12 +78,14 @@ int ff_h264_ref_picture(H264Context *h, H264Picture *dst, H264Picture *src) dst->qscale_table_buf = av_buffer_ref(src->qscale_table_buf); dst->mb_type_buf = av_buffer_ref(src->mb_type_buf); - if (!dst->qscale_table_buf || !dst->mb_type_buf) { + dst->pps_buf = av_buffer_ref(src->pps_buf); + if (!dst->qscale_table_buf || !dst->mb_type_buf || !dst->pps_buf) { ret = AVERROR(ENOMEM); goto fail; } dst->qscale_table = src->qscale_table; dst->mb_type = src->mb_type; + dst->pps = src->pps; for (i = 0; i < 2; i++) { dst->motion_val_buf[i] = av_buffer_ref(src->motion_val_buf[i]); @@ -120,6 +123,9 @@ int ff_h264_ref_picture(H264Context *h, H264Picture *dst, H264Picture *src) dst->recovered = src->recovered; dst->invalid_gap = src->invalid_gap; dst->sei_recovery_frame_cnt = src->sei_recovery_frame_cnt; + dst->mb_width = src->mb_width; + dst->mb_height = src->mb_height; + dst->mb_stride = src->mb_stride; return 0; fail: diff --git a/libavcodec/h264_slice.c b/libavcodec/h264_slice.c index af30f6267b..775bf94f2e 100644 --- a/libavcodec/h264_slice.c +++ b/libavcodec/h264_slice.c @@ -243,6 +243,15 @@ static int alloc_picture(H264Context *h, H264Picture *pic) pic->ref_index[i] = pic->ref_index_buf[i]->data; } + pic->pps_buf = av_buffer_ref(h->ps.pps_ref); + if (!pic->pps_buf) + goto fail; + pic->pps = (const PPS*)pic->pps_buf->data; + + pic->mb_width = h->mb_width; + pic->mb_height = h->mb_height; + pic->mb_stride = h->mb_stride; + return 0; fail: ff_h264_unref_picture(h, pic); diff --git a/libavcodec/h264dec.c b/libavcodec/h264dec.c index fec1adfeed..242eeb9677 100644 --- a/libavcodec/h264dec.c +++ b/libavcodec/h264dec.c @@ -32,6 +32,8 @@ #include "libavutil/imgutils.h" #include "libavutil/opt.h" #include "libavutil/stereo3d.h" +#include "libavutil/video_enc_params.h" + #include "internal.h" #include "bytestream.h" #include "cabac.h" @@ -831,6 +833,43 @@ static int get_consumed_bytes(int pos, int buf_size) return pos; } +static int h264_export_enc_params(AVFrame *f, H264Picture *p) +{ + AVVideoEncParams *par; + unsigned int nb_mb = p->mb_height * p->mb_width; + unsigned int x, y; + + par = av_video_enc_params_create_side_data(f, nb_mb); + if (!par) + return AVERROR(ENOMEM); + + par->type = AV_VIDEO_ENC_PARAMS_H264; + + par->qp = p->pps->init_qp; + + par->delta_qp[1][0] = p->pps->chroma_qp_index_offset[0]; + par->delta_qp[1][1] = p->pps->chroma_qp_index_offset[0]; + par->delta_qp[2][0] = p->pps->chroma_qp_index_offset[1]; + par->delta_qp[2][1] = p->pps->chroma_qp_index_offset[1]; + + for (y = 0; y < p->mb_height; y++) + for (x = 0; x < p->mb_width; x++) { + const unsigned int block_idx = y * p->mb_width + x; + const unsigned int mb_xy = y * p->mb_stride + x; + AVVideoBlockParams *b = av_video_enc_params_block(par, block_idx); + + b->src_x = x * 16; + b->src_y = y * 16; + b->w = 16; + b->h = 16; + + b->delta_qp = p->qscale_table[mb_xy] - par->qp; + } + + + return 0; +} + static int output_frame(H264Context *h, AVFrame *dst, H264Picture *srcp) { AVFrame *src = srcp->f; @@ -845,7 +884,16 @@ static int output_frame(H264Context *h, AVFrame *dst, H264Picture *srcp) if (srcp->sei_recovery_frame_cnt == 0) dst->key_frame = 1; + if (h->avctx->export_side_data & AV_CODEC_EXPORT_DATA_VIDEO_ENC_PARAMS) { + ret = h264_export_enc_params(dst, srcp); + if (ret < 0) + goto fail; + } + return 0; +fail: + av_frame_unref(dst); + return ret; } static int is_extra(const uint8_t *buf, int buf_size) diff --git a/libavcodec/h264dec.h b/libavcodec/h264dec.h index 530e2d4071..29c4d4e42c 100644 --- a/libavcodec/h264dec.h +++ b/libavcodec/h264dec.h @@ -161,6 +161,12 @@ typedef struct H264Picture { int recovered; ///< picture at IDR or recovery point + recovery count int invalid_gap; int sei_recovery_frame_cnt; + + AVBufferRef *pps_buf; + const PPS *pps; + + int mb_width, mb_height; + int mb_stride; } H264Picture; typedef struct H264Ref { diff --git a/libavutil/video_enc_params.h b/libavutil/video_enc_params.h index c1afcb3a63..b19c7a5925 100644 --- a/libavutil/video_enc_params.h +++ b/libavutil/video_enc_params.h @@ -29,6 +29,7 @@ enum AVVideoEncParamsType { AV_VIDEO_ENC_PARAMS_NONE = -1, AV_VIDEO_ENC_PARAMS_MPEG2, AV_VIDEO_ENC_PARAMS_H263, + AV_VIDEO_ENC_PARAMS_H264, }; /** From patchwork Mon Mar 30 11:45:25 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 18511 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 29AFE449D38 for ; Mon, 30 Mar 2020 14:46:07 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 15FEE68AE4C; Mon, 30 Mar 2020 14:46:07 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail.red.khirnov.net (red.khirnov.net [176.97.15.12]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 3E7F168AEB7 for ; Mon, 30 Mar 2020 14:45:58 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail.red.khirnov.net (Postfix) with ESMTP id 796B2286670 for ; Mon, 30 Mar 2020 13:45:57 +0200 (CEST) Received: from mail.red.khirnov.net ([IPv6:::1]) by localhost (mail.red.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id Y4-ZBj7DPQ_Q for ; Mon, 30 Mar 2020 13:45:55 +0200 (CEST) Received: from quelana.khirnov.net (unknown [IPv6:2a00:c500:61:23b::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) client-signature RSA-PSS (2048 bits)) (Client CN "quelana.khirnov.net", Issuer "smtp.khirnov.net SMTP CA" (verified OK)) by mail.red.khirnov.net (Postfix) with ESMTPS id 9F678284F05 for ; Mon, 30 Mar 2020 13:45:55 +0200 (CEST) Received: from localhost (quelana.khirnov.net [IPv6:::1]) by quelana.khirnov.net (Postfix) with ESMTP id 4700921249 for ; Mon, 30 Mar 2020 13:45:55 +0200 (CEST) Received: from quelana.khirnov.net ([IPv6:::1]) by localhost (quelana.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id LAR_nFalMpkW for ; Mon, 30 Mar 2020 13:45:54 +0200 (CEST) Received: from libav.daenerys.khirnov.net (libav.daenerys.khirnov.net [IPv6:2a00:c500:561:201::7]) by quelana.khirnov.net (Postfix) with ESMTP id E5AE921257 for ; Mon, 30 Mar 2020 13:45:37 +0200 (CEST) Received: by libav.daenerys.khirnov.net (Postfix, from userid 1000) id CA87A20E003B; Mon, 30 Mar 2020 13:45:37 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Mon, 30 Mar 2020 13:45:25 +0200 Message-Id: <20200330114526.11086-6-anton@khirnov.net> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200330114526.11086-1-anton@khirnov.net> References: <20200330114526.11086-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 6/7] vp9dec: factorise freeing per-tile allocated data 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" --- libavcodec/vp9.c | 36 ++++++++++++++++-------------------- 1 file changed, 16 insertions(+), 20 deletions(-) diff --git a/libavcodec/vp9.c b/libavcodec/vp9.c index 7ee375d4d0..e03891887b 100644 --- a/libavcodec/vp9.c +++ b/libavcodec/vp9.c @@ -93,6 +93,12 @@ static void vp9_free_entries(AVCodecContext *avctx) {} static int vp9_alloc_entries(AVCodecContext *avctx, int n) { return 0; } #endif +static void vp9_tile_data_free(VP9TileData *td) +{ + av_freep(&td->b_base); + av_freep(&td->block_base); +} + static void vp9_frame_unref(AVCodecContext *avctx, VP9Frame *f) { ff_thread_release_buffer(avctx, &f->tf); @@ -281,10 +287,8 @@ static int update_size(AVCodecContext *avctx, int w, int h) #undef assign if (s->td) { - for (i = 0; i < s->active_tile_cols; i++) { - av_freep(&s->td[i].b_base); - av_freep(&s->td[i].block_base); - } + for (i = 0; i < s->active_tile_cols; i++) + vp9_tile_data_free(&s->td[i]); } if (s->s.h.bpp != s->last_bpp) { @@ -306,8 +310,7 @@ static int update_block_buffers(AVCodecContext *avctx) if (td->b_base && td->block_base && s->block_alloc_using_2pass == s->s.frames[CUR_FRAME].uses_2pass) return 0; - av_free(td->b_base); - av_free(td->block_base); + vp9_tile_data_free(td); chroma_blocks = 64 * 64 >> (s->ss_h + s->ss_v); chroma_eobs = 16 * 16 >> (s->ss_h + s->ss_v); if (s->s.frames[CUR_FRAME].uses_2pass) { @@ -324,12 +327,9 @@ static int update_block_buffers(AVCodecContext *avctx) td->uveob_base[0] = td->eob_base + 16 * 16 * sbs; td->uveob_base[1] = td->uveob_base[0] + chroma_eobs * sbs; } else { - for (i = 1; i < s->active_tile_cols; i++) { - if (s->td[i].b_base && s->td[i].block_base) { - av_free(s->td[i].b_base); - av_free(s->td[i].block_base); - } - } + for (i = 1; i < s->active_tile_cols; i++) + vp9_tile_data_free(&s->td[i]); + for (i = 0; i < s->active_tile_cols; i++) { s->td[i].b_base = av_malloc(sizeof(VP9Block)); s->td[i].block_base = av_mallocz((64 * 64 + 2 * chroma_blocks) * bytesperpixel * sizeof(int16_t) + @@ -773,10 +773,8 @@ static int decode_frame_header(AVCodecContext *avctx, VP56RangeCoder *rc; if (s->td) { - for (i = 0; i < s->active_tile_cols; i++) { - av_free(s->td[i].b_base); - av_free(s->td[i].block_base); - } + for (i = 0; i < s->active_tile_cols; i++) + vp9_tile_data_free(&s->td[i]); av_free(s->td); } @@ -1204,10 +1202,8 @@ static void free_buffers(VP9Context *s) int i; av_freep(&s->intra_pred_data[0]); - for (i = 0; i < s->active_tile_cols; i++) { - av_freep(&s->td[i].b_base); - av_freep(&s->td[i].block_base); - } + for (i = 0; i < s->active_tile_cols; i++) + vp9_tile_data_free(&s->td[i]); } static av_cold int vp9_decode_free(AVCodecContext *avctx) From patchwork Mon Mar 30 11:45:26 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 18517 Delivered-To: andriy.gelman@gmail.com Received: by 2002:a0c:ab15:0:0:0:0:0 with SMTP id h21csp2134925qvb; Mon, 30 Mar 2020 04:46:43 -0700 (PDT) X-Google-Smtp-Source: ADFU+vug4ufxHrsNzTHTHazvkGB4LxYCcJRxH9uF1QJO1Q4lo/WIhdk3PVMfgqLPscb9ZrSU1MDM X-Received: by 2002:a17:906:3008:: with SMTP id 8mr4905642ejz.162.1585568802939; Mon, 30 Mar 2020 04:46:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1585568802; cv=none; d=google.com; s=arc-20160816; b=E6rXILkTn738kHwkURNB1jrLiq2fQFR/JZgYIV53igOayJ1NvllVV8smwtLRu9BWAo quSrTOUBv4v04QtCE/urMC/CD4QV7KcWypyxhk09uCpMFmdPht7JiLk3kukfhFoLCs8V q27IGt4aEu+309nxZo4TL8kxGN05k1C7w6x0fCOj22srooFxs5SwO3vPX5ERTFWbvDFy Hsvd2kT65CDhB9Numc8PZEQzB9hN+lwLc6W1gonYSRQSBRobXSuTVtiKu8KUTt7lsioj PY6rbrXvnGkXgd7Gp+hOAXm9YMb0xFpycPnZnAz4UKw0t3YJtm+BFf6MTX67q8ut+E8B ByHg== 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=pKdVmXXRqH3pg+YHS5x7tD5MA7dcjYKokMEStauxNfk=; b=xS/r9nD8VxMA+JsPZ32OiC6SfxgO+kccumYnj53o9RwAZy6AQ13oeoRgUAmATH4f6F 09C7c4oR6kZ8Vu1q7yxu9H880h9bSv4Zy8ut1vznrfQYfwYvGTZEJtAP7lNgtfCoyMLZ LrejcQER02fhlJYRjyrRE3fjBHhsgaoLKeDpx68hWcztiMfvnrqThuDvrHlDMy6XyuoR jZcZc8wUrbsjtCOHYoShRsqUDu2q9DHzIieM5wBlXOIbXr9okKxV2XdP+SrO2OMdQHRM thtQN0zK4I7yKYyOVNof7MMhPxBzlg9yLs1tCcjGcGvXOpI5UVgHslboHOyeKHdp53og KqOw== 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 i3si8382747ejy.255.2020.03.30.04.46.42; Mon, 30 Mar 2020 04:46:42 -0700 (PDT) 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 6742668B051; Mon, 30 Mar 2020 14:46:03 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail.red.khirnov.net (red.khirnov.net [176.97.15.12]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 9201968AEB0 for ; Mon, 30 Mar 2020 14:45:55 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail.red.khirnov.net (Postfix) with ESMTP id 4782D28661B for ; Mon, 30 Mar 2020 13:45:55 +0200 (CEST) Received: from mail.red.khirnov.net ([IPv6:::1]) by localhost (mail.red.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id EQ5Eoia1jmoy for ; Mon, 30 Mar 2020 13:45:54 +0200 (CEST) Received: from quelana.khirnov.net (unknown [IPv6:2a00:c500:61:23b::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) client-signature RSA-PSS (2048 bits)) (Client CN "quelana.khirnov.net", Issuer "smtp.khirnov.net SMTP CA" (verified OK)) by mail.red.khirnov.net (Postfix) with ESMTPS id B9C9C284F05 for ; Mon, 30 Mar 2020 13:45:54 +0200 (CEST) Received: from localhost (quelana.khirnov.net [IPv6:::1]) by quelana.khirnov.net (Postfix) with ESMTP id 4321A21249 for ; Mon, 30 Mar 2020 13:45:54 +0200 (CEST) Received: from quelana.khirnov.net ([IPv6:::1]) by localhost (quelana.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id 6-2CpocCTGTf for ; Mon, 30 Mar 2020 13:45:52 +0200 (CEST) Received: from libav.daenerys.khirnov.net (libav.daenerys.khirnov.net [IPv6:2a00:c500:561:201::7]) by quelana.khirnov.net (Postfix) with ESMTP id CCE0521256 for ; Mon, 30 Mar 2020 13:45:37 +0200 (CEST) Received: by libav.daenerys.khirnov.net (Postfix, from userid 1000) id BADA820E0041; Mon, 30 Mar 2020 13:45:37 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Mon, 30 Mar 2020 13:45:26 +0200 Message-Id: <20200330114526.11086-7-anton@khirnov.net> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200330114526.11086-1-anton@khirnov.net> References: <20200330114526.11086-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 7/7] vp9dec: support exporting QP tables through the AVVideoEncParams API 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" X-TUID: 9tn3LsXPzJfU Content-Length: 9654 --- libavcodec/vp9.c | 74 ++++++++++++++++++++++++++++++++++++ libavcodec/vp9block.c | 8 ++++ libavcodec/vp9dec.h | 8 ++++ libavutil/video_enc_params.h | 1 + 4 files changed, 91 insertions(+) diff --git a/libavcodec/vp9.c b/libavcodec/vp9.c index e03891887b..8baf337f1b 100644 --- a/libavcodec/vp9.c +++ b/libavcodec/vp9.c @@ -34,6 +34,7 @@ #include "vp9dec.h" #include "libavutil/avassert.h" #include "libavutil/pixdesc.h" +#include "libavutil/video_enc_params.h" #define VP9_SYNCCODE 0x498342 @@ -97,6 +98,7 @@ static void vp9_tile_data_free(VP9TileData *td) { av_freep(&td->b_base); av_freep(&td->block_base); + av_freep(&td->block_structure); } static void vp9_frame_unref(AVCodecContext *avctx, VP9Frame *f) @@ -326,6 +328,12 @@ static int update_block_buffers(AVCodecContext *avctx) td->eob_base = (uint8_t *) (td->uvblock_base[1] + sbs * chroma_blocks * bytesperpixel); td->uveob_base[0] = td->eob_base + 16 * 16 * sbs; td->uveob_base[1] = td->uveob_base[0] + chroma_eobs * sbs; + + if (avctx->export_side_data & AV_CODEC_EXPORT_DATA_VIDEO_ENC_PARAMS) { + td->block_structure = av_malloc_array(s->cols * s->rows, sizeof(*td->block_structure)); + if (!td->block_structure) + return AVERROR(ENOMEM); + } } else { for (i = 1; i < s->active_tile_cols; i++) vp9_tile_data_free(&s->td[i]); @@ -341,6 +349,12 @@ static int update_block_buffers(AVCodecContext *avctx) s->td[i].eob_base = (uint8_t *) (s->td[i].uvblock_base[1] + chroma_blocks * bytesperpixel); s->td[i].uveob_base[0] = s->td[i].eob_base + 16 * 16; s->td[i].uveob_base[1] = s->td[i].uveob_base[0] + chroma_eobs; + + if (avctx->export_side_data & AV_CODEC_EXPORT_DATA_VIDEO_ENC_PARAMS) { + s->td[i].block_structure = av_malloc_array(s->cols * s->rows, sizeof(*td->block_structure)); + if (!s->td[i].block_structure) + return AVERROR(ENOMEM); + } } } s->block_alloc_using_2pass = s->s.frames[CUR_FRAME].uses_2pass; @@ -1475,6 +1489,59 @@ int loopfilter_proc(AVCodecContext *avctx) } #endif +static int vp9_export_enc_params(VP9Context *s, VP9Frame *frame) +{ + AVVideoEncParams *par; + unsigned int tile, nb_blocks = 0; + + if (s->s.h.segmentation.enabled) { + for (tile = 0; tile < s->active_tile_cols; tile++) + nb_blocks += s->td[tile].nb_block_structure; + } + + par = av_video_enc_params_create_side_data(frame->tf.f, nb_blocks); + if (!par) + return AVERROR(ENOMEM); + + par->type = AV_VIDEO_ENC_PARAMS_VP9; + + par->qp = s->s.h.yac_qi; + par->delta_qp[0][0] = s->s.h.ydc_qdelta; + par->delta_qp[1][0] = s->s.h.uvdc_qdelta; + par->delta_qp[2][0] = s->s.h.uvdc_qdelta; + par->delta_qp[1][1] = s->s.h.uvac_qdelta; + par->delta_qp[2][1] = s->s.h.uvac_qdelta; + + if (nb_blocks) { + unsigned int block = 0; + unsigned int tile, block_tile; + + for (tile = 0; tile < s->active_tile_cols; tile++) { + VP9TileData *td = &s->td[tile]; + + for (block_tile = 0; block_tile < td->nb_block_structure; block_tile++) { + AVVideoBlockParams *b = av_video_enc_params_block(par, block++); + unsigned int row = td->block_structure[block_tile].row; + unsigned int col = td->block_structure[block_tile].col; + uint8_t seg_id = frame->segmentation_map[row * 8 * s->sb_cols + col]; + + b->src_x = col * 8; + b->src_y = row * 8; + b->w = 1 << (3 + td->block_structure[block_tile].block_size_idx_x); + b->h = 1 << (3 + td->block_structure[block_tile].block_size_idx_y); + + if (s->s.h.segmentation.feat[seg_id].q_enabled) { + b->delta_qp = s->s.h.segmentation.feat[seg_id].q_val; + if (s->s.h.segmentation.absolute_vals) + b->delta_qp -= par->qp; + } + } + } + } + + return 0; +} + static int vp9_decode_frame(AVCodecContext *avctx, void *frame, int *got_frame, AVPacket *pkt) { @@ -1621,6 +1688,7 @@ FF_ENABLE_DEPRECATION_WARNINGS s->td[i].eob = s->td[i].eob_base; s->td[i].uveob[0] = s->td[i].uveob_base[0]; s->td[i].uveob[1] = s->td[i].uveob_base[1]; + s->td[i].nb_block_structure = 0; } #if HAVE_THREADS @@ -1677,6 +1745,12 @@ FF_ENABLE_DEPRECATION_WARNINGS } while (s->pass++ == 1); ff_thread_report_progress(&s->s.frames[CUR_FRAME].tf, INT_MAX, 0); + if (avctx->export_side_data & AV_CODEC_EXPORT_DATA_VIDEO_ENC_PARAMS) { + ret = vp9_export_enc_params(s, &s->s.frames[CUR_FRAME]); + if (ret < 0) + return ret; + } + finish: // ref frame setup for (i = 0; i < 8; i++) { diff --git a/libavcodec/vp9block.c b/libavcodec/vp9block.c index 1c3f7a7225..ec16e26c69 100644 --- a/libavcodec/vp9block.c +++ b/libavcodec/vp9block.c @@ -1290,6 +1290,14 @@ void ff_vp9_decode_block(VP9TileData *td, int row, int col, b->uvtx = b->tx - ((s->ss_h && w4 * 2 == (1 << b->tx)) || (s->ss_v && h4 * 2 == (1 << b->tx))); + if (td->block_structure) { + td->block_structure[td->nb_block_structure].row = row; + td->block_structure[td->nb_block_structure].col = col; + td->block_structure[td->nb_block_structure].block_size_idx_x = av_log2(w4); + td->block_structure[td->nb_block_structure].block_size_idx_y = av_log2(h4); + td->nb_block_structure++; + } + if (!b->skip) { int has_coeffs; diff --git a/libavcodec/vp9dec.h b/libavcodec/vp9dec.h index de02b146f0..65a8d2fc51 100644 --- a/libavcodec/vp9dec.h +++ b/libavcodec/vp9dec.h @@ -221,6 +221,14 @@ struct VP9TileData { struct { int x, y; } min_mv, max_mv; int16_t *block_base, *block, *uvblock_base[2], *uvblock[2]; uint8_t *eob_base, *uveob_base[2], *eob, *uveob[2]; + + struct { + unsigned int row:14; + unsigned int col:14; + unsigned int block_size_idx_x:2; + unsigned int block_size_idx_y:2; + } *block_structure; + unsigned int nb_block_structure; }; void ff_vp9_fill_mv(VP9TileData *td, VP56mv *mv, int mode, int sb); diff --git a/libavutil/video_enc_params.h b/libavutil/video_enc_params.h index b19c7a5925..e38f1312fb 100644 --- a/libavutil/video_enc_params.h +++ b/libavutil/video_enc_params.h @@ -30,6 +30,7 @@ enum AVVideoEncParamsType { AV_VIDEO_ENC_PARAMS_MPEG2, AV_VIDEO_ENC_PARAMS_H263, AV_VIDEO_ENC_PARAMS_H264, + AV_VIDEO_ENC_PARAMS_VP9, }; /**