From patchwork Wed Jul 24 19:18:59 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?UTF-8?q?Juan=20De=20Le=C3=B3n?= X-Patchwork-Id: 14062 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 A747B448768 for ; Wed, 24 Jul 2019 22:24:40 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 8908F68A6C5; Wed, 24 Jul 2019 22:24:40 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-qt1-f201.google.com (mail-qt1-f201.google.com [209.85.160.201]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id C173E689D06 for ; Wed, 24 Jul 2019 22:24:33 +0300 (EEST) Received: by mail-qt1-f201.google.com with SMTP id o16so42324847qtj.6 for ; Wed, 24 Jul 2019 12:24:33 -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; bh=FNpqgtvJJMfRy22juCahu28lhexN6ZNZ3m1pQstaK5w=; b=p6yvG2UJzfjGaHLZuPOstZJ2rx5/+Mskxbl5u1H4vQp4jn6Nwr74mtjqJ4nW8GDfte 8DRI9A0Dq94NVCOt1uGPQ7tlkJ3hySsnjsgqh9rS7W8EnZETlgsFDTAVLLt4khWxboYu 3K7A0kIsVnTdM9wiYcvR7VYL+xdB9pD5InSy9StONM+hMHfiCUhalH71UQV/gLsCdvLl ++Fcb446mgLULtveHCsq1ZROfOiN6MOafqELcxWfq2oe011tYtj4SPP2Y8QUP1vtnrkF 8pUNa98AF7c1WTftn4nQyiHB3cb/5XNMR93sUiDUvMtMGxT8+T/54myBt1AXuQyYUUUW hmTA== 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; bh=FNpqgtvJJMfRy22juCahu28lhexN6ZNZ3m1pQstaK5w=; b=OPkk3voGgV4xxxLKiItQvjvlnx9uLN9r0qnHH8wOsht291i5lIqntkgDqVZcgZCNnE ASCwvkBlsGDK3+7PHHwPb69/WzcoJLU789D8TYpeXaSh1nUK+5Pu2+Rbs2GzFG0e1jqJ V+4xCEozXIr3Jhb4BecUFL8ougoPbLfj/7KBJ5RVFJW+Nexq2iy8BS7Xc2r6350+pZVf L5SJgdNBV/n6fcXZb6LzYpYcFHkohuSGqupLfRZkIx4r9eTKQ3KUwmVHQIZQuiSwrrwr 3BKikGx2Th5vvDUMLHQEO/SCuM4Ea7weIemG6FciwooiRLN3gbT2RfUGqedSq7e62a0P TDNg== X-Gm-Message-State: APjAAAXD8xcDqpfrGeUb+GzTan1nVzppFfx3FBuVkVCAaOT/OITWIYxI JLZgpiwmLDzDc5cSaoOUBiakq+rREen6NVutYr5zJOsI24yWf6nkG+Z+l7c4DwyBIb6TUsbAN49 Y9BXebj2wJ8hqdYhepVwFjdZcxOPtIA5ZwTx5WtkFf9YkRGwb/GaXpAyzM8duYXs= X-Google-Smtp-Source: APXvYqzzcTIgwy/13cWdap8g9qhnQ1R2OzE+8TMYeqCaiByBhOj6GSveflAn+O9eoc2SFQeDNHLTtGMY8n4= X-Received: by 2002:ac8:32ec:: with SMTP id a41mr59955247qtb.375.1563995958018; Wed, 24 Jul 2019 12:19:18 -0700 (PDT) Date: Wed, 24 Jul 2019 12:18:59 -0700 In-Reply-To: Message-Id: <20190724191859.166958-1-juandl@google.com> Mime-Version: 1.0 References: X-Mailer: git-send-email 2.22.0.709.g102302147b-goog From: "=?UTF-8?q?Juan=20De=20Le=C3=B3n?=" To: ffmpeg-devel@ffmpeg.org Subject: [FFmpeg-devel] [PATCH] Extract QP from h264 encoded videos 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" --- libavcodec/avcodec.h | 1 + libavcodec/h264dec.c | 37 ++++++++++++ libavcodec/options_table.h | 1 + libavutil/Makefile | 2 + libavutil/frame.h | 6 ++ libavutil/quantization_params.c | 40 +++++++++++++ libavutil/quantization_params.h | 102 ++++++++++++++++++++++++++++++++ 7 files changed, 189 insertions(+) create mode 100644 libavutil/quantization_params.c create mode 100644 libavutil/quantization_params.h diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h index d234271c5b..9e3185720a 100644 --- a/libavcodec/avcodec.h +++ b/libavcodec/avcodec.h @@ -2671,6 +2671,7 @@ typedef struct AVCodecContext { #endif #define FF_DEBUG_BUFFERS 0x00008000 #define FF_DEBUG_THREADS 0x00010000 +#define FF_DEBUG_EXTRACTQP 0x00020000 #define FF_DEBUG_GREEN_MD 0x00800000 #define FF_DEBUG_NOMC 0x01000000 diff --git a/libavcodec/h264dec.c b/libavcodec/h264dec.c index 8d1bd16a8e..07b85f4e0a 100644 --- a/libavcodec/h264dec.c +++ b/libavcodec/h264dec.c @@ -33,6 +33,7 @@ #include "libavutil/opt.h" #include "libavutil/stereo3d.h" #include "libavutil/timer.h" +#include "libavutil/quantization_params.h" #include "internal.h" #include "bytestream.h" #include "cabac.h" @@ -922,6 +923,42 @@ static int finalize_frame(H264Context *h, AVFrame *dst, H264Picture *out, int *g } } + if (h->avctx->debug & FF_DEBUG_EXTRACTQP) { + int mb_height = h->height / 16; + int mb_width = h->width / 16; + int mb_xy = mb_width * mb_height; + + AVFrameSideData *sd; + sd = av_frame_new_side_data(dst, AV_FRAME_DATA_QUANTIZATION_PARAMS, + sizeof(AVQuantizationParamsArray)); + + AVQuantizationParamsArray *params; + params = (AVQuantizationParamsArray *)sd->data; + params->nb_blocks = mb_xy; + params->qp_arr = av_malloc_array(mb_xy, sizeof(AVQuantizationParams)); + + params->codec_id = h->avctx->codec_id; + + // loop allocate QP + int qp_index = 0; + for (int mb_y = 0; mb_y < mb_height; mb_y++) { + for (int mb_x = 0; mb_x < mb_width; mb_x++) { + int qs_index = mb_x + mb_y * h->mb_stride; + AVQuantizationParams *qp_block = &(params->qp_arr[qp_index]); + + qp_block->x = mb_x * 16; + qp_block->y = mb_y * 16; + qp_block->w = qp_block->h = 16; + + // ALLOCATE MEMORY TO THE QP ARRAY + qp_block->type = av_malloc(QP_TYPE_ARR_SIZE_H264 * sizeof(int)); + qp_block->type[QP_H264] = out->qscale_table[qs_index]; + + qp_index++; + } + } + } + return 0; } diff --git a/libavcodec/options_table.h b/libavcodec/options_table.h index 4a266eca16..e0e78a69c5 100644 --- a/libavcodec/options_table.h +++ b/libavcodec/options_table.h @@ -219,6 +219,7 @@ static const AVOption avcodec_options[] = { {"buffers", "picture buffer allocations", 0, AV_OPT_TYPE_CONST, {.i64 = FF_DEBUG_BUFFERS }, INT_MIN, INT_MAX, V|D, "debug"}, {"thread_ops", "threading operations", 0, AV_OPT_TYPE_CONST, {.i64 = FF_DEBUG_THREADS }, INT_MIN, INT_MAX, V|A|D, "debug"}, {"nomc", "skip motion compensation", 0, AV_OPT_TYPE_CONST, {.i64 = FF_DEBUG_NOMC }, INT_MIN, INT_MAX, V|A|D, "debug"}, +{"extractqp", "enable QP extraction per frame", 0, AV_OPT_TYPE_CONST, {.i64 = FF_DEBUG_EXTRACTQP }, INT_MIN, INT_MAX, V|D, "debug"}, {"dia_size", "diamond type & size for motion estimation", OFFSET(dia_size), AV_OPT_TYPE_INT, {.i64 = DEFAULT }, INT_MIN, INT_MAX, V|E}, {"last_pred", "amount of motion predictors from the previous frame", OFFSET(last_predictor_count), AV_OPT_TYPE_INT, {.i64 = DEFAULT }, INT_MIN, INT_MAX, V|E}, #if FF_API_PRIVATE_OPT diff --git a/libavutil/Makefile b/libavutil/Makefile index 8a7a44e4b5..be1a9c3a9c 100644 --- a/libavutil/Makefile +++ b/libavutil/Makefile @@ -60,6 +60,7 @@ HEADERS = adler32.h \ pixdesc.h \ pixelutils.h \ pixfmt.h \ + quantization_params.h \ random_seed.h \ rc4.h \ rational.h \ @@ -140,6 +141,7 @@ OBJS = adler32.o \ parseutils.o \ pixdesc.o \ pixelutils.o \ + quantization_params.o \ random_seed.o \ rational.o \ reverse.o \ diff --git a/libavutil/frame.h b/libavutil/frame.h index 5d3231e7bb..d48ccf342f 100644 --- a/libavutil/frame.h +++ b/libavutil/frame.h @@ -179,6 +179,12 @@ enum AVFrameSideDataType { * array element is implied by AVFrameSideData.size / AVRegionOfInterest.self_size. */ AV_FRAME_DATA_REGIONS_OF_INTEREST, + /** + * To extract quantization parameters from supported decoders. + * The data stored is AVQuantizationParamsArray type, described in + * libavuitls/quantization_params.h + */ + AV_FRAME_DATA_QUANTIZATION_PARAMS, }; enum AVActiveFormatDescription { diff --git a/libavutil/quantization_params.c b/libavutil/quantization_params.c new file mode 100644 index 0000000000..230a9869bb --- /dev/null +++ b/libavutil/quantization_params.c @@ -0,0 +1,40 @@ +/* + * 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/quantization_params.h" +/** + * Strings for names of enums, used by Filter + */ +const char* const QP_NAMES_H264[] = {"qp"}; +const char* const QP_NAMES_VP9[] = {"qydc", "qyac", "quvdc", "quvac", "qiydc", "qiyac", + "qiuvdc", "qiuvac"}; +const char* const QP_NAMES_AV1[] = {"qydc", "qyac", "qudc", "quac", "qvdc", "qvac", + "qiydc", "qiyac", "qiudc", "qiuac", "qivdc", "qivac"}; + +char* getQPTypeStr(enum AVCodecID codec_id, int index) { + switch (codec_id) { + case AV_CODEC_ID_H264: + return QP_NAMES_H264[index]; + case AV_CODEC_ID_VP9: + return QP_NAMES_VP9[index]; + case AV_CODEC_ID_AV1: + return QP_NAMES_AV1[index]; + default: + return NULL; + } +} diff --git a/libavutil/quantization_params.h b/libavutil/quantization_params.h new file mode 100644 index 0000000000..a48c472143 --- /dev/null +++ b/libavutil/quantization_params.h @@ -0,0 +1,102 @@ +/* + * 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_QUANTIZATION_PARAMS_H +#define AVUTIL_QUANTIZATION_PARAMS_H + +#include "libavcodec/avcodec.h" + +/** + * Data structure for extracting Quantization Parameters codec independent + */ +typedef struct AVQuantizationParams { + /** + * x and y coordinates of the block in pixels + */ + int x, y; + /** + * width and height of the block in pixels + */ + int w, h; + /** + * qp array, indexed by type according to + * the enum corresponding to the codec + * size depends on codec + */ + int *type; +} AVQuantizationParams; + +/** + * For storing an array of AVQuantization parameters and its size + * To be used as AVFrameSideData + */ +typedef struct AVQuantizationParamsArray { + /** + * AVQuantizationParams block array + */ + AVQuantizationParams *qp_arr; + /** + * size of the array + */ + int nb_blocks; + int codec_id; +} AVQuantizationParamsArray; + +/** + * Enums for different codecs to store qp in the type array + * Each enum must have an array of strings describing each field + */ +char* getQPTypeStr(enum AVCodecID codec_id, int index); + +extern const char* const QP_NAMES_H264[]; +enum QP_ARR_INDEXES_FOR_H264 { + QP_H264 = 0, // qp value + QP_TYPE_ARR_SIZE_H264 // used for allocating memory +}; + +extern const char* const QP_NAMES_VP9[]; +enum QP_ARR_INDEXES_FOR_VP9 { + QP_YDC_VP9 = 0, + QP_YAC_VP9, + QP_UVDC_VP9, + QP_UVAC_VP9, + QP_INDEX_YDC_VP9, + QP_INDEX_YAC_VP9, + QP_INDEX_UVDC_VP9, + QP_INDEX_UVAC_VP9, + QP_ARR_SIZE_VP9 +}; + +extern const char* const QP_NAMES_AV1[]; +enum QP_ARR_INDEXES_FOR_AV1 { + QP_YDC_AV1 = 0, + QP_YAC_AV1, + QP_UDC_AV1, + QP_UAC_AV1, + QP_VDC_AV1, + QP_VAC_AV1, + QP_INDEX_YDC_AV1, + QP_INDEX_YAC_AV1, + QP_INDEX_UDC_AV1, + QP_INDEX_UAC_AV1, + QP_INDEX_VDC_AV1, + QP_INDEX_VAC_AV1, + QP_ARR_SIZE_AV1 +}; + +#endif /* AVUTIL_QUANTIZATION_PARAMS_H */