Message ID | 20190724191859.166958-1-juandl@google.com |
---|---|
State | Superseded |
Headers | show |
Previous thread: http://ffmpeg.org/pipermail/ffmpeg-devel/2019-July/246951.html I added the modifications to the decoder, I ran some tests for performance and run times are only affected if my flag is enabled. Decoded 3 different encoded videos 20 times each with and without my debug flag, here are the results: *ExtractQP disabled:* ===> multitime results 1: ffmpeg -hide_banner -loglevel panic -debug 0 -i tveryfast.mp4 -f null - Mean Std.Dev. Min Median Max real 0.747 0.007 0.735 0.749 0.756 user 5.582 0.025 5.547 5.574 5.627 sys 0.166 0.028 0.120 0.167 0.224 ===> multitime results 1: ffmpeg -hide_banner -loglevel panic -debug 0 -i tmedium.mp4 -f null - Mean Std.Dev. Min Median Max real 0.865 0.009 0.845 0.864 0.887 user 6.296 0.036 6.198 6.299 6.365 sys 0.195 0.026 0.142 0.199 0.247 ===> multitime results 1: ffmpeg -hide_banner -loglevel panic -debug 0 -i tveryslow.mp4 -f null - Mean Std.Dev. Min Median Max real 0.919 0.011 0.892 0.920 0.943 user 6.398 0.042 6.311 6.381 6.476 sys 0.229 0.032 0.169 0.238 0.287 *ExtractQP enabled: * ===> multitime results 1: ffmpeg -hide_banner -loglevel panic -debug extractqp -i tveryfast.mp4 -f null - Mean Std.Dev. Min Median Max real 1.126 0.032 1.076 1.132 1.216 user 6.433 0.054 6.347 6.430 6.561 sys 1.069 0.047 0.989 1.063 1.161 ===> multitime results 1: ffmpeg -hide_banner -loglevel panic -debug extractqp -i tmedium.mp4 -f null - Mean Std.Dev. Min Median Max real 1.178 0.020 1.143 1.176 1.217 user 7.091 0.055 7.020 7.081 7.196 sys 1.031 0.057 0.898 1.043 1.131 ===> multitime results 1: ffmpeg -hide_banner -loglevel panic -debug extractqp -i tveryslow.mp4 -f null - Mean Std.Dev. Min Median Max real 1.234 0.028 1.196 1.230 1.322 user 7.212 0.077 6.996 7.230 7.345 sys 1.067 0.076 0.938 1.062 1.283
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 [...]
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 */