From: Dmitry Ilyin <[email protected]>
When AV_CODEC_EXPORT_DATA_VIDEO_ENC_PARAMS is requested, attach
AV_FRAME_DATA_VIDEO_ENC_PARAMS of type AV_VIDEO_ENC_PARAMS_HEVC to decoded
frames, with one block per minimum coding block carrying the final luma QP
(QP_Y) of the CU that covers it, as the H.264 decoder already does per MB.
The side data is created in hevc_frame_start(), before the frame can be
queued for output, and the per-block QPs are filled in hevc_frame_end() from
qp_y_tab, once they are known. Frames handed out early through
ff_hevc_output_frames() share the same side data buffer, so they carry the
QPs as well, the same way they carry pixel data written after queueing.
Nothing is exported with hwaccel or when pixel decoding is skipped, as
qp_y_tab is not filled in those cases.
---
libavcodec/hevc/hevcdec.c | 58 +++++++++++++++++++++++++++++++++++++++
1 file changed, 58 insertions(+)
diff --git a/libavcodec/hevc/hevcdec.c b/libavcodec/hevc/hevcdec.c
index faee53c..bc9b9b5 100644
--- a/libavcodec/hevc/hevcdec.c
+++ b/libavcodec/hevc/hevcdec.c
@@ -38,6 +38,7 @@
#include "libavutil/stereo3d.h"
#include "libavutil/tdrdi.h"
#include "libavutil/timecode.h"
+#include "libavutil/video_enc_params.h"
#include "libavutil/refstruct.h"
#include "libavcodec/aom_film_grain.h"
@@ -3853,6 +3854,54 @@ static int find_finish_setup_nal(const HEVCContext *s)
return nal_idx;
}
+/**
+ * Attach AV_FRAME_DATA_VIDEO_ENC_PARAMS to the frame being decoded, with one
+ * block per minimum coding block. The per-block QPs are only known once the
+ * picture is decoded and are filled in by hevc_fill_enc_params(); the side
+ * data is created here, before the frame can be queued for output, so that
+ * every reference to the frame shares the same buffer.
+ */
+static int hevc_alloc_enc_params(const HEVCPPS *pps, AVFrame *f)
+{
+ const HEVCSPS *sps = pps->sps;
+ const int size = 1 << sps->log2_min_cb_size;
+ AVVideoEncParams *par;
+
+ par = av_video_enc_params_create_side_data(f, AV_VIDEO_ENC_PARAMS_HEVC,
+ sps->min_cb_width *
sps->min_cb_height);
+ if (!par)
+ return AVERROR(ENOMEM);
+
+ par->qp = 26 + pps->pic_init_qp_minus26;
+ par->delta_qp[1][0] = par->delta_qp[1][1] = pps->cb_qp_offset;
+ par->delta_qp[2][0] = par->delta_qp[2][1] = pps->cr_qp_offset;
+
+ for (int y = 0; y < sps->min_cb_height; y++)
+ for (int x = 0; x < sps->min_cb_width; x++) {
+ AVVideoBlockParams *b = av_video_enc_params_block(par, y *
sps->min_cb_width + x);
+
+ b->src_x = x * size;
+ b->src_y = y * size;
+ b->w = FFMIN(size, sps->width - b->src_x);
+ b->h = FFMIN(size, sps->height - b->src_y);
+ }
+
+ return 0;
+}
+
+static void hevc_fill_enc_params(const HEVCLayerContext *l, const HEVCFrame
*frame)
+{
+ const AVFrameSideData *sd = av_frame_get_side_data(frame->f,
AV_FRAME_DATA_VIDEO_ENC_PARAMS);
+ AVVideoEncParams *par;
+
+ if (!sd)
+ return;
+
+ par = (AVVideoEncParams *)sd->data;
+ for (unsigned i = 0; i < par->nb_blocks; i++)
+ av_video_enc_params_block(par, i)->delta_qp = l->qp_y_tab[i] - par->qp;
+}
+
static int hevc_frame_start(HEVCContext *s, HEVCLayerContext *l,
unsigned nal_idx)
{
@@ -4012,6 +4061,13 @@ static int hevc_frame_start(HEVCContext *s,
HEVCLayerContext *l,
if (ret < 0)
goto fail;
+ if ((s->avctx->export_side_data & AV_CODEC_EXPORT_DATA_VIDEO_ENC_PARAMS) &&
+ !s->avctx->hwaccel && !ff_decode_skip_all_pixels(s->avctx)) {
+ ret = hevc_alloc_enc_params(pps, s->cur_frame->f);
+ if (ret < 0)
+ goto fail;
+ }
+
if (s->cur_frame->needs_fg &&
(s->sei.common.film_grain_characteristics &&
s->sei.common.film_grain_characteristics->present &&
!ff_h274_film_grain_params_supported(s->sei.common.film_grain_characteristics->model_id,
@@ -4173,6 +4229,8 @@ static int hevc_frame_end(HEVCContext *s,
HEVCLayerContext *l)
return ret;
}
} else {
+ hevc_fill_enc_params(l, out);
+
if (s->avctx->err_recognition & AV_EF_CRCCHECK &&
s->sei.picture_hash.is_md5) {
ret = verify_md5(s, out->f);
--
2.54.0
_______________________________________________
ffmpeg-devel mailing list -- [email protected]
To unsubscribe send an email to [email protected]