[FFmpeg-devel] [PATCH] Extract QP from h264 encoded videos
Michael Niedermayer
michael at niedermayer.cc
Fri Jul 26 00:11:33 EEST 2019
On Wed, Jul 24, 2019 at 12:18:59PM -0700, Juan De León wrote:
> ---
> 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 ++++++++++++++++++++++++++++++++
the changes to libavutil and libavcodec should be in separate patches
> 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;
missing failure checks for av_frame_new_side_data and av_malloc_array
> +
> + // 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;
this should be the matching enum
> +} 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);
functions need either a prefix or need to be static
also the name is inconsistent with existing function names
thx
[...]
--
Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB
If you fake or manipulate statistics in a paper in physics you will never
get a job again.
If you fake or manipulate statistics in a paper in medicin you will get
a job for life at the pharma industry.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 181 bytes
Desc: not available
URL: <http://ffmpeg.org/pipermail/ffmpeg-devel/attachments/20190725/90ccbc19/attachment.sig>
More information about the ffmpeg-devel
mailing list