From patchwork Tue Mar 10 17:45:57 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 18113 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 C46F344974D for ; Tue, 10 Mar 2020 19:48:55 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id B02A268A8BA; Tue, 10 Mar 2020 19:48:55 +0200 (EET) 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 881AC687F30 for ; Tue, 10 Mar 2020 19:48:45 +0200 (EET) Received: from localhost (localhost [IPv6:::1]) by mail.red.khirnov.net (Postfix) with ESMTP id 49DF12956D2 for ; Tue, 10 Mar 2020 18:48:45 +0100 (CET) Received: from mail.red.khirnov.net ([IPv6:::1]) by localhost (mail.red.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id wVzYowBs2WrE for ; Tue, 10 Mar 2020 18:48:45 +0100 (CET) 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 B33182956E8 for ; Tue, 10 Mar 2020 18:48:44 +0100 (CET) Received: from localhost (quelana.khirnov.net [IPv6:::1]) by quelana.khirnov.net (Postfix) with ESMTP id 5C0952530F for ; Tue, 10 Mar 2020 18:48:44 +0100 (CET) Received: from quelana.khirnov.net ([IPv6:::1]) by localhost (quelana.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id V3lACLslRPsa for ; Tue, 10 Mar 2020 18:48:43 +0100 (CET) Received: from libav.daenerys.khirnov.net (libav.daenerys.khirnov.net [IPv6:2a00:c500:561:201::7]) by quelana.khirnov.net (Postfix) with ESMTP id 0574A25327 for ; Tue, 10 Mar 2020 18:48:40 +0100 (CET) Received: by libav.daenerys.khirnov.net (Postfix, from userid 1000) id 2BA5220E0356; Tue, 10 Mar 2020 18:48:36 +0100 (CET) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Tue, 10 Mar 2020 18:45:57 +0100 Message-Id: <20200310174602.10217-2-anton@khirnov.net> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200310174602.10217-1-anton@khirnov.net> References: <20200310174602.10217-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 1/6] mpegvideodata: drop useless comments 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/mpegvideodata.c | 5 ----- 1 file changed, 5 deletions(-) diff --git a/libavcodec/mpegvideodata.c b/libavcodec/mpegvideodata.c index 5f1d8f7c00..da0638e052 100644 --- a/libavcodec/mpegvideodata.c +++ b/libavcodec/mpegvideodata.c @@ -19,7 +19,6 @@ #include const uint8_t ff_default_chroma_qscale_table[32] = { -// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31 }; @@ -32,7 +31,6 @@ const uint8_t ff_mpeg2_non_linear_qscale[32] = { }; const uint8_t ff_mpeg1_dc_scale_table[128] = { -// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, @@ -44,7 +42,6 @@ const uint8_t ff_mpeg1_dc_scale_table[128] = { }; static const uint8_t mpeg2_dc_scale_table1[128] = { -// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, @@ -56,7 +53,6 @@ static const uint8_t mpeg2_dc_scale_table1[128] = { }; static const uint8_t mpeg2_dc_scale_table2[128] = { -// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, @@ -68,7 +64,6 @@ static const uint8_t mpeg2_dc_scale_table2[128] = { }; static const uint8_t mpeg2_dc_scale_table3[128] = { -// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, From patchwork Tue Mar 10 17:45:58 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 18111 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 042C044974D for ; Tue, 10 Mar 2020 19:48:53 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id D57646880E4; Tue, 10 Mar 2020 19:48:52 +0200 (EET) 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 2F565687F85 for ; Tue, 10 Mar 2020 19:48:45 +0200 (EET) Received: from localhost (localhost [IPv6:::1]) by mail.red.khirnov.net (Postfix) with ESMTP id 389272956E9 for ; Tue, 10 Mar 2020 18:48:44 +0100 (CET) Received: from mail.red.khirnov.net ([IPv6:::1]) by localhost (mail.red.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id 6vXrcHxN7WAK for ; Tue, 10 Mar 2020 18:48:43 +0100 (CET) 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 C50EA2956E8 for ; Tue, 10 Mar 2020 18:48:43 +0100 (CET) Received: from localhost (quelana.khirnov.net [IPv6:::1]) by quelana.khirnov.net (Postfix) with ESMTP id 62B8B251FA for ; Tue, 10 Mar 2020 18:48:43 +0100 (CET) Received: from quelana.khirnov.net ([IPv6:::1]) by localhost (quelana.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id 0fEmHklNlSHF for ; Tue, 10 Mar 2020 18:48:42 +0100 (CET) Received: from libav.daenerys.khirnov.net (libav.daenerys.khirnov.net [IPv6:2a00:c500:561:201::7]) by quelana.khirnov.net (Postfix) with ESMTP id 0395E25326 for ; Tue, 10 Mar 2020 18:48:40 +0100 (CET) Received: by libav.daenerys.khirnov.net (Postfix, from userid 1000) id 4D02020E0353; Tue, 10 Mar 2020 18:48:36 +0100 (CET) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Tue, 10 Mar 2020 18:45:58 +0100 Message-Id: <20200310174602.10217-3-anton@khirnov.net> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200310174602.10217-1-anton@khirnov.net> References: <20200310174602.10217-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 2/6] mpegvideo: drop an unnecessary function parameter 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" It is always 0. --- libavcodec/mpegvideo.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/libavcodec/mpegvideo.c b/libavcodec/mpegvideo.c index dbb6ab9b39..0be5364422 100644 --- a/libavcodec/mpegvideo.c +++ b/libavcodec/mpegvideo.c @@ -347,9 +347,9 @@ av_cold void ff_mpv_idct_init(MpegEncContext *s) ff_init_scantable(s->idsp.idct_permutation, &s->intra_v_scantable, ff_alternate_vertical_scan); } -static int alloc_picture(MpegEncContext *s, Picture *pic, int shared) +static int alloc_picture(MpegEncContext *s, Picture *pic) { - return ff_alloc_picture(s->avctx, pic, &s->me, &s->sc, shared, 0, + return ff_alloc_picture(s->avctx, pic, &s->me, &s->sc, 0, 0, s->chroma_x_shift, s->chroma_y_shift, s->out_format, s->mb_stride, s->mb_width, s->mb_height, s->b8_stride, &s->linesize, &s->uvlinesize); @@ -1263,7 +1263,7 @@ int ff_mpv_frame_start(MpegEncContext *s, AVCodecContext *avctx) pic->f->coded_picture_number = s->coded_picture_number++; - if (alloc_picture(s, pic, 0) < 0) + if (alloc_picture(s, pic) < 0) return -1; s->current_picture_ptr = pic; @@ -1324,7 +1324,7 @@ int ff_mpv_frame_start(MpegEncContext *s, AVCodecContext *avctx) s->last_picture_ptr->f->key_frame = 0; s->last_picture_ptr->f->pict_type = AV_PICTURE_TYPE_P; - if (alloc_picture(s, s->last_picture_ptr, 0) < 0) { + if (alloc_picture(s, s->last_picture_ptr) < 0) { s->last_picture_ptr = NULL; return -1; } @@ -1365,7 +1365,7 @@ int ff_mpv_frame_start(MpegEncContext *s, AVCodecContext *avctx) s->next_picture_ptr->f->key_frame = 0; s->next_picture_ptr->f->pict_type = AV_PICTURE_TYPE_P; - if (alloc_picture(s, s->next_picture_ptr, 0) < 0) { + if (alloc_picture(s, s->next_picture_ptr) < 0) { s->next_picture_ptr = NULL; return -1; } From patchwork Tue Mar 10 17:45:59 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 18110 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 B149044974D for ; Tue, 10 Mar 2020 19:48:51 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 9248768975D; Tue, 10 Mar 2020 19:48:51 +0200 (EET) 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 2CDCD687F36 for ; Tue, 10 Mar 2020 19:48:45 +0200 (EET) Received: from localhost (localhost [IPv6:::1]) by mail.red.khirnov.net (Postfix) with ESMTP id 3BA892956EA for ; Tue, 10 Mar 2020 18:48:44 +0100 (CET) Received: from mail.red.khirnov.net ([IPv6:::1]) by localhost (mail.red.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id Bz9DdmzzkbLS for ; Tue, 10 Mar 2020 18:48:43 +0100 (CET) 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 90A862956D2 for ; Tue, 10 Mar 2020 18:48:43 +0100 (CET) Received: from localhost (quelana.khirnov.net [IPv6:::1]) by quelana.khirnov.net (Postfix) with ESMTP id 4B8EA2532C for ; Tue, 10 Mar 2020 18:48:43 +0100 (CET) Received: from quelana.khirnov.net ([IPv6:::1]) by localhost (quelana.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id TU23dIaVaSWc for ; Tue, 10 Mar 2020 18:48:41 +0100 (CET) Received: from libav.daenerys.khirnov.net (libav.daenerys.khirnov.net [IPv6:2a00:c500:561:201::7]) by quelana.khirnov.net (Postfix) with ESMTP id F3F67251FA for ; Tue, 10 Mar 2020 18:48:40 +0100 (CET) Received: by libav.daenerys.khirnov.net (Postfix, from userid 1000) id 15E5520E004A; Tue, 10 Mar 2020 18:48:36 +0100 (CET) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Tue, 10 Mar 2020 18:45:59 +0100 Message-Id: <20200310174602.10217-4-anton@khirnov.net> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200310174602.10217-1-anton@khirnov.net> References: <20200310174602.10217-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 3/6] 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. 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 | 81 ++++++++++++++++++++++++ libavutil/video_enc_params.h | 118 +++++++++++++++++++++++++++++++++++ 5 files changed, 208 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 a2dae8e89a..2592f952af 100644 --- a/libavutil/Makefile +++ b/libavutil/Makefile @@ -78,6 +78,7 @@ HEADERS = adler32.h \ tree.h \ twofish.h \ version.h \ + video_enc_params.h \ xtea.h \ tea.h \ tx.h \ @@ -164,7 +165,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..4ae088939a --- /dev/null +++ b/libavutil/video_enc_params.c @@ -0,0 +1,81 @@ +/* + * 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; + + for (i = 0; i < FF_ARRAY_ELEMS(par->qp); i++) + par->qp[i] = 0; + + 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..a7cc4210f3 --- /dev/null +++ b/libavutil/video_enc_params.h @@ -0,0 +1,118 @@ +/* + * 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 (for each + * plane) and the per-frame value. + */ + int delta_qp[4]; +} 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 parametere for each plane. The final quantiser is a sum + * of this value with the per-block value (if it is present). + */ + int qp[4]; +} 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 Tue Mar 10 17:46:00 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 18112 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 0B5A244974D for ; Tue, 10 Mar 2020 19:48:54 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id E130A689EE7; Tue, 10 Mar 2020 19:48:53 +0200 (EET) 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 294E0687F30 for ; Tue, 10 Mar 2020 19:48:45 +0200 (EET) Received: from localhost (localhost [IPv6:::1]) by mail.red.khirnov.net (Postfix) with ESMTP id 7FCF22956E7 for ; Tue, 10 Mar 2020 18:48:43 +0100 (CET) Received: from mail.red.khirnov.net ([IPv6:::1]) by localhost (mail.red.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id LoEVkrIVLJaQ for ; Tue, 10 Mar 2020 18:48:43 +0100 (CET) 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 EF5002956D2 for ; Tue, 10 Mar 2020 18:48:42 +0100 (CET) Received: from localhost (quelana.khirnov.net [IPv6:::1]) by quelana.khirnov.net (Postfix) with ESMTP id 6ADEC2532D for ; Tue, 10 Mar 2020 18:48:42 +0100 (CET) Received: from quelana.khirnov.net ([IPv6:::1]) by localhost (quelana.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id H6Xcu1Tz0tHX for ; Tue, 10 Mar 2020 18:48:41 +0100 (CET) Received: from libav.daenerys.khirnov.net (libav.daenerys.khirnov.net [IPv6:2a00:c500:561:201::7]) by quelana.khirnov.net (Postfix) with ESMTP id 01CCF2530F for ; Tue, 10 Mar 2020 18:48:40 +0100 (CET) Received: by libav.daenerys.khirnov.net (Postfix, from userid 1000) id 649DB20E0355; Tue, 10 Mar 2020 18:48:37 +0100 (CET) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Tue, 10 Mar 2020 18:46:00 +0100 Message-Id: <20200310174602.10217-5-anton@khirnov.net> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200310174602.10217-1-anton@khirnov.net> References: <20200310174602.10217-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 4/6] 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" --- 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 5a0fc3405c..7b3b59dc1e 100644 --- a/libavcodec/avcodec.h +++ b/libavcodec/avcodec.h @@ -1116,6 +1116,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 3265889b8e..3722e2e87a 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 Tue Mar 10 17:46:01 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 18115 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 65F9544974D for ; Tue, 10 Mar 2020 19:48:57 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 50BE668A92E; Tue, 10 Mar 2020 19:48:57 +0200 (EET) 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 70A2668A7E1 for ; Tue, 10 Mar 2020 19:48:50 +0200 (EET) Received: from localhost (localhost [IPv6:::1]) by mail.red.khirnov.net (Postfix) with ESMTP id 6FE752956ED for ; Tue, 10 Mar 2020 18:48:47 +0100 (CET) Received: from mail.red.khirnov.net ([IPv6:::1]) by localhost (mail.red.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id cmi8veRfzpmy for ; Tue, 10 Mar 2020 18:48:46 +0100 (CET) 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 AA47B2956EC for ; Tue, 10 Mar 2020 18:48:46 +0100 (CET) Received: from localhost (quelana.khirnov.net [IPv6:::1]) by quelana.khirnov.net (Postfix) with ESMTP id 42E9B2530F for ; Tue, 10 Mar 2020 18:48:46 +0100 (CET) Received: from quelana.khirnov.net ([IPv6:::1]) by localhost (quelana.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id 6L-sgEe8PROp for ; Tue, 10 Mar 2020 18:48:44 +0100 (CET) Received: from libav.daenerys.khirnov.net (libav.daenerys.khirnov.net [IPv6:2a00:c500:561:201::7]) by quelana.khirnov.net (Postfix) with ESMTP id 0E09B25329 for ; Tue, 10 Mar 2020 18:48:41 +0100 (CET) Received: by libav.daenerys.khirnov.net (Postfix, from userid 1000) id 945C120E0359; Tue, 10 Mar 2020 18:48:37 +0100 (CET) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Tue, 10 Mar 2020 18:46:01 +0100 Message-Id: <20200310174602.10217-6-anton@khirnov.net> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200310174602.10217-1-anton@khirnov.net> References: <20200310174602.10217-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 5/6] 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 | 41 +++++++++++++++++++++++++++++------- libavcodec/mpegvideo.h | 8 ++++++- libavcodec/rv10.c | 5 +++-- libavcodec/rv34.c | 5 +++-- libavutil/video_enc_params.h | 2 ++ 9 files changed, 57 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 17f9495a1d..bfdf273c82 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" @@ -2062,7 +2063,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++; @@ -2073,7 +2074,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 0be5364422..ad5d119e94 100644 --- a/libavcodec/mpegvideo.c +++ b/libavcodec/mpegvideo.c @@ -33,6 +33,8 @@ #include "libavutil/internal.h" #include "libavutil/motion_vector.h" #include "libavutil/timer.h" +#include "libavutil/video_enc_params.h" + #include "avcodec.h" #include "blockdsp.h" #include "h264chroma.h" @@ -1443,16 +1445,39 @@ 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[0] = p->qscale_table[mb_xy]; + b->delta_qp[1] = b->delta_qp[0]; + b->delta_qp[2] = b->delta_qp[0]; + } + + 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 a7cc4210f3..166a49bd6d 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 Tue Mar 10 17:46:02 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 18114 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 91FCD44974D for ; Tue, 10 Mar 2020 19:48:56 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 7A03868A9AA; Tue, 10 Mar 2020 19:48:56 +0200 (EET) 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 7219F68A812 for ; Tue, 10 Mar 2020 19:48:50 +0200 (EET) Received: from localhost (localhost [IPv6:::1]) by mail.red.khirnov.net (Postfix) with ESMTP id 706E72956EE for ; Tue, 10 Mar 2020 18:48:47 +0100 (CET) Received: from mail.red.khirnov.net ([IPv6:::1]) by localhost (mail.red.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id vZkUT4vKxGY6 for ; Tue, 10 Mar 2020 18:48:46 +0100 (CET) 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 465A12956E8 for ; Tue, 10 Mar 2020 18:48:46 +0100 (CET) Received: from localhost (quelana.khirnov.net [IPv6:::1]) by quelana.khirnov.net (Postfix) with ESMTP id D3B89251FA for ; Tue, 10 Mar 2020 18:48:45 +0100 (CET) Received: from quelana.khirnov.net ([IPv6:::1]) by localhost (quelana.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id 74mAh8JqCSjK for ; Tue, 10 Mar 2020 18:48:44 +0100 (CET) Received: from libav.daenerys.khirnov.net (libav.daenerys.khirnov.net [IPv6:2a00:c500:561:201::7]) by quelana.khirnov.net (Postfix) with ESMTP id 0E0E52532A for ; Tue, 10 Mar 2020 18:48:41 +0100 (CET) Received: by libav.daenerys.khirnov.net (Postfix, from userid 1000) id 7BF9320E0357; Tue, 10 Mar 2020 18:48:37 +0100 (CET) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Tue, 10 Mar 2020 18:46:02 +0100 Message-Id: <20200310174602.10217-7-anton@khirnov.net> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200310174602.10217-1-anton@khirnov.net> References: <20200310174602.10217-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 6/6] 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" --- libavcodec/h264_picture.c | 8 +++++- libavcodec/h264_slice.c | 9 +++++++ libavcodec/h264dec.c | 47 ++++++++++++++++++++++++++++++++++++ libavcodec/h264dec.h | 6 +++++ libavutil/video_enc_params.h | 1 + 5 files changed, 70 insertions(+), 1 deletion(-) diff --git a/libavcodec/h264_picture.c b/libavcodec/h264_picture.c index e833835a77..0fcb45ffed 100644 --- a/libavcodec/h264_picture.c +++ b/libavcodec/h264_picture.c @@ -55,6 +55,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]); @@ -78,12 +79,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]); @@ -121,6 +124,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 e24d41ca50..d064234b64 100644 --- a/libavcodec/h264_slice.c +++ b/libavcodec/h264_slice.c @@ -244,6 +244,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 f12651fa3d..0975d1cb2e 100644 --- a/libavcodec/h264dec.c +++ b/libavcodec/h264dec.c @@ -33,6 +33,8 @@ #include "libavutil/opt.h" #include "libavutil/stereo3d.h" #include "libavutil/timer.h" +#include "libavutil/video_enc_params.h" + #include "internal.h" #include "bytestream.h" #include "cabac.h" @@ -834,6 +836,42 @@ static int get_consumed_bytes(int pos, int buf_size) return pos; } +static int export_enc_params(H264Context *h, 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[0] = p->pps->init_qp; + par->qp[1] = par->qp[0]; + par->qp[2] = par->qp[0]; + + 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[0] = p->qscale_table[mb_xy] - par->qp[0]; + b->delta_qp[1] = get_chroma_qp(p->pps, 0, b->delta_qp[0]) - par->qp[1]; + b->delta_qp[2] = get_chroma_qp(p->pps, 1, b->delta_qp[0]) - par->qp[2]; + } + + + return 0; +} + static int output_frame(H264Context *h, AVFrame *dst, H264Picture *srcp) { AVFrame *src = srcp->f; @@ -848,7 +886,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 = export_enc_params(h, 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 166a49bd6d..da3e831b54 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, }; /**