From patchwork Wed Aug 14 19:10:49 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?UTF-8?q?Juan=20De=20Le=C3=B3n?= X-Patchwork-Id: 14507 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 CC5EC44A0C5 for ; Wed, 14 Aug 2019 22:17:35 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id AD7FE68A803; Wed, 14 Aug 2019 22:17:35 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-vs1-f73.google.com (mail-vs1-f73.google.com [209.85.217.73]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 664B06898A1 for ; Wed, 14 Aug 2019 22:17:29 +0300 (EEST) Received: by mail-vs1-f73.google.com with SMTP id v20so27379vsi.12 for ; Wed, 14 Aug 2019 12:17:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc:content-transfer-encoding; bh=i+xlyDtN5Oi83zv5hPWJbi154sVNCYMRScLxM16VmMA=; b=foBL4unqsY5eLVYhPbl32FZ0zJSXbfa9dbQdJEDQZ9KLYwzoP1DE+WJRFgR+KIoWYv deYgJlbJ+6CaTJ8Mf95gIJw2gNtgZH7dFxSR7qVcDha66n7LXv9EQQu57zNpPzSikg+3 R/RQmPWswHc6o5J241k8rs9WDFg/vJ6hVJFlOQIpx3NZ0MXofe1XQpue23xsvQPB98kp NOyAJHU0QU38NtXt4a7wAU8dRGlSG/Nuy6IlhuFnlNiztclsu9aXzRH/cm0SUY4AvLyb IkNkEfgBtyo1iI3HWosIQYpl+rfPMSPrNtejVXAjltm0GQCOybR7VRwp7bGGYYHzfocu knrA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc:content-transfer-encoding; bh=i+xlyDtN5Oi83zv5hPWJbi154sVNCYMRScLxM16VmMA=; b=fdnzzjETa7guBjFM946YUYt4tS1abq8juK6TdklhneqFIg5CB//iieXF5TVPFJRqI1 uLYlsLwhRo37tDNxViFt0PeKdVSSATDXjvLA6nPqtA7ox/varQEHoMi2xqhPRSVi3c4p tqPuQ57LOuqVSjRB3GUSA9Gt5S263PrZE8zwITahc1D1joJ0zShnK58FUBxrcKdGAtZD 3Fg7iRud4x0tfzOiNswKnLzv6g0yDm9z+/ct7GSs9eVF2pHHWMmJvc1yaDcec0eVCqtg mkVzjjLhU3xLD9/R2fhxUGjAsApoPTibAPKLTl2KQH3uO5oWmkpDRcw6q+tz5xQ2Drjs 6GtQ== X-Gm-Message-State: APjAAAVVtmiBFjlL56Q+Bwz0FeiAA2DI0wfbOuXkOfGDAAXm0tbi6GIQ /NeI76mL3nK6a21wu/HOne/K5E1JcsRlKA1D2zeNAOUU8Tb3usXsVR63eZf6Xey0/6SOgE36q+C g/+2hhoVxP8iOSslqIHZ2RJ0Lh6DLhF9dE3/1vea/g+V0G9MgFzwmt5UigqFR8K0= X-Google-Smtp-Source: APXvYqyal+UkU5N5pb0S5AV+B0dfvkoclzfOfF0KNM56Nz6/mOAiwrj9/tIXIplAP0VTVyqZx7OHzNgeG6E= X-Received: by 2002:a67:f043:: with SMTP id q3mr696784vsm.219.1565809875251; Wed, 14 Aug 2019 12:11:15 -0700 (PDT) Date: Wed, 14 Aug 2019 12:10:49 -0700 In-Reply-To: Message-Id: <20190814191048.57002-1-juandl@google.com> Mime-Version: 1.0 References: X-Mailer: git-send-email 2.23.0.rc1.153.gdeed80330f-goog From: "=?UTF-8?q?Juan=20De=20Le=C3=B3n?=" To: ffmpeg-devel@ffmpeg.org Subject: [FFmpeg-devel] [PATCH] libavutil: AVEncodeInfo data structures X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: =?UTF-8?q?Juan=20De=20Le=C3=B3n?= Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" AVEncodeInfoFrame data structure to store as AVFrameSideData of type AV_FRAME_DATA_ENCODE_INFO. The structure stores quantization index for each plane, DC/AC deltas for luma and chroma planes, and an array of AVEncodeInfoBlock type denoting position, size, and delta quantizer for each block in the frame. Can be extended to support extraction of other block information. Signed-off-by: Juan De León --- libavutil/Makefile | 2 + libavutil/encode_info.c | 68 ++++++++++++++++++++++++ libavutil/encode_info.h | 112 ++++++++++++++++++++++++++++++++++++++++ libavutil/frame.c | 1 + libavutil/frame.h | 7 +++ 5 files changed, 190 insertions(+) create mode 100644 libavutil/encode_info.c create mode 100644 libavutil/encode_info.h diff --git a/libavutil/Makefile b/libavutil/Makefile index 57e6e3d7e8..37cfb099e9 100644 --- a/libavutil/Makefile +++ b/libavutil/Makefile @@ -24,6 +24,7 @@ HEADERS = adler32.h \ dict.h \ display.h \ downmix_info.h \ + encode_info.h \ encryption_info.h \ error.h \ eval.h \ @@ -111,6 +112,7 @@ OBJS = adler32.o \ dict.o \ display.o \ downmix_info.o \ + encode_info.o \ encryption_info.o \ error.o \ eval.o \ diff --git a/libavutil/encode_info.c b/libavutil/encode_info.c new file mode 100644 index 0000000000..351333cf43 --- /dev/null +++ b/libavutil/encode_info.c @@ -0,0 +1,68 @@ +/* + * 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 "libavutil/encode_info.h" +#include "libavutil/mem.h" + +static int init_encode_info_data(AVEncodeInfoFrame *info, int nb_blocks) { + info->nb_blocks = nb_blocks; + info->block_size = sizeof(AVEncodeInfoBlock); + info->blocks_offset = offsetof(AVEncodeInfoFrame, blocks); + + for(int i = 0; i < AV_NUM_DATA_POINTERS; i++) + info->plane_q[i] = -1; + + return 0; +} + +AVEncodeInfoFrame *av_encode_info_alloc(int nb_blocks) +{ + AVEncodeInfoFrame *ptr; + //AVEncodeInfoFrame already allocates size for one element of AVEncodeInfoBlock + size_t size = sizeof(AVEncodeInfoFrame) + sizeof(AVEncodeInfoBlock)*FFMAX(nb_blocks - 1, 0); + + if (nb_blocks < 0 || size >= INT_MAX) + return NULL; + + ptr = av_mallocz(size); + if (!ptr) + return NULL; + + init_encode_info_data(ptr, nb_blocks); + + return ptr; +} + +AVEncodeInfoFrame *av_encode_info_create_side_data(AVFrame *frame, int nb_blocks) +{ + size_t size = sizeof(AVEncodeInfoFrame) + sizeof(AVEncodeInfoBlock)*FFMAX(nb_blocks - 1, 0); + + if (nb_blocks < 0 || size >= INT_MAX) + return NULL; + + AVFrameSideData *sd = av_frame_new_side_data(frame, + AV_FRAME_DATA_ENCODE_INFO, + size); + if (!sd) + return NULL; + + memset(sd->data, 0, size); + init_encode_info_data((AVEncodeInfoFrame*)sd->data, nb_blocks); + + return (AVEncodeInfoFrame*)sd->data; +} diff --git a/libavutil/encode_info.h b/libavutil/encode_info.h new file mode 100644 index 0000000000..ae0a6563dc --- /dev/null +++ b/libavutil/encode_info.h @@ -0,0 +1,112 @@ +/* + * 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_ENCODE_INFO_H +#define AVUTIL_ENCODE_INFO_H + +#include "libavutil/avassert.h" +#include "libavutil/frame.h" + +/** + * Data structure for extracting block data, stored as an array in AVEncodeInfoFrame. + */ +typedef struct AVEncodeInfoBlock{ + /** + * 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; + /** + * Delta quantization index for the block. + */ + int delta_q; +} AVEncodeInfoBlock; + +/** + * Frame encoding info, used as AVFrameSideData. Data in this structure concerns + * the whole frame. + * Additional entries may be added without bumping major before nb_blocks, + * so using the accessor function av_encode_info_get_block() is recommended. + */ +typedef struct AVEncodeInfoFrame { + /** + * Base plane quantizer for the frame, set to -1 when value is unsupported. + */ + int plane_q[AV_NUM_DATA_POINTERS]; + /** + * DC/AC quantizer index delta, set to -1 when value is value unsupported. + */ + int ac_q, dc_q; + /** + * DC/AC chroma quantizer index delta, set to -1 when value is value unsupported. + */ + int ac_chroma_q, dc_chroma_q; + /** + * Number of blocks in the array, may be 0. + */ + int nb_blocks; + /** + * Offset in this structure at which blocks begin in bytes. May not match + * offsetof(AVEncodeInfoFrame, blocks). + */ + size_t blocks_offset; + /* + * Size of each block in bytes. May not match sizeof(AVEncodeInfoBlock). + */ + size_t block_size; + + /* + * Array of blocks, with a total size of block_size*nb_blocks, the [1] + * is meant for compatibility with C++. + */ + AVEncodeInfoBlock blocks[1]; +} AVEncodeInfoFrame; + +/* + * Gets the block at the specified {@code idx}. Must be between 0 and nb_blocks. + * Out of range index returns NULL. + */ +static inline AVEncodeInfoBlock *av_encode_info_get_block(AVEncodeInfoFrame *info, unsigned idx) +{ + if (idx >= info->nb_blocks) + return NULL; + + return (AVEncodeInfoBlock *)((uint8_t *)info + info->blocks_offset + idx*info->block_size); +} + +/** + * Allocates memory for AVEncodeInfoFrame plus an array of + * {@code nb_blocks} AVEncodeInfoBlock and initializes the variables. + * Can be freed with a normal av_free() call. + */ +AVEncodeInfoFrame *av_encode_info_alloc(int nb_blocks); + +/** + * 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. + */ +AVEncodeInfoFrame *av_encode_info_create_side_data(AVFrame *frame, int nb_blocks); + +#endif /* AVUTIL_ENCODE_INFO_H */ diff --git a/libavutil/frame.c b/libavutil/frame.c index dcf1fc3d17..65c25e6cd7 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_ENCODE_INFO: return "AVEncodeInfo"; } return NULL; } diff --git a/libavutil/frame.h b/libavutil/frame.h index 5d3231e7bb..ec112c5d15 100644 --- a/libavutil/frame.h +++ b/libavutil/frame.h @@ -179,6 +179,13 @@ enum AVFrameSideDataType { * array element is implied by AVFrameSideData.size / AVRegionOfInterest.self_size. */ AV_FRAME_DATA_REGIONS_OF_INTEREST, + /** + * Extract frame and block encode info from supported decoders. The data + * stored is an AVEncodeInfoFrame type, which contains an array of + * AVEncodeInfoBlock. Described in libavuitls/encode_info.h + * Can be allocated in the frame directly with av_encode_info_create_side_data(). + */ + AV_FRAME_DATA_ENCODE_INFO, }; enum AVActiveFormatDescription {