summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnton Khirnov <anton@khirnov.net>2020-12-14 14:36:05 +0100
committerAnton Khirnov <anton@khirnov.net>2021-01-01 14:24:20 +0100
commitfba0ecbe20db2d63c0227887ce21a137da68b30d (patch)
tree58d3123b108cfe66ac30229745d7eed5348df938
parentc72d5264944c11ecf4e5724b56876721becd5aa3 (diff)
lavfi: add common code to handle QP tables
It will be used for converting the *pp filters to the new AVVideoEncParams API.
-rw-r--r--libavfilter/qp_table.c72
-rw-r--r--libavfilter/qp_table.h33
2 files changed, 105 insertions, 0 deletions
diff --git a/libavfilter/qp_table.c b/libavfilter/qp_table.c
new file mode 100644
index 0000000000..33812b708d
--- /dev/null
+++ b/libavfilter/qp_table.c
@@ -0,0 +1,72 @@
+/*
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU 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 <stdint.h>
+
+// for FF_QSCALE_TYPE_*
+#include "libavcodec/internal.h"
+
+#include "libavutil/frame.h"
+#include "libavutil/mem.h"
+#include "libavutil/video_enc_params.h"
+
+#include "qp_table.h"
+
+int ff_qp_table_extract(AVFrame *frame, int8_t **table, int *table_w, int *table_h,
+ int *qscale_type)
+{
+ AVFrameSideData *sd;
+ AVVideoEncParams *par;
+ unsigned int mb_h = (frame->height + 15) / 16;
+ unsigned int mb_w = (frame->width + 15) / 16;
+ unsigned int nb_mb = mb_h * mb_w;
+ unsigned int block_idx;
+
+ *table = NULL;
+
+ sd = av_frame_get_side_data(frame, AV_FRAME_DATA_VIDEO_ENC_PARAMS);
+ if (!sd)
+ return 0;
+ par = (AVVideoEncParams*)sd->data;
+ if (par->type != AV_VIDEO_ENC_PARAMS_MPEG2 ||
+ (par->nb_blocks != 0 && par->nb_blocks != nb_mb))
+ return AVERROR(ENOSYS);
+
+ *table = av_malloc(nb_mb);
+ if (!*table)
+ return AVERROR(ENOMEM);
+ if (table_w)
+ *table_w = mb_w;
+ if (table_h)
+ *table_h = mb_h;
+ if (qscale_type)
+ *qscale_type = FF_QSCALE_TYPE_MPEG2;
+
+ if (par->nb_blocks == 0) {
+ memset(*table, par->qp, nb_mb);
+ return 0;
+ }
+
+ for (block_idx = 0; block_idx < nb_mb; block_idx++) {
+ AVVideoBlockParams *b = av_video_enc_params_block(par, block_idx);
+ (*table)[block_idx] = par->qp + b->delta_qp;
+ }
+
+ return 0;
+}
+
diff --git a/libavfilter/qp_table.h b/libavfilter/qp_table.h
new file mode 100644
index 0000000000..a552fe2e64
--- /dev/null
+++ b/libavfilter/qp_table.h
@@ -0,0 +1,33 @@
+/*
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU 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 AVFILTER_QP_TABLE_H
+#define AVFILTER_QP_TABLE_H
+
+#include <stdint.h>
+
+#include "libavutil/frame.h"
+
+/**
+ * Extract a libpostproc-compatible QP table - an 8-bit QP value per 16x16
+ * macroblock, stored in raster order - from AVVideoEncParams side data.
+ */
+int ff_qp_table_extract(AVFrame *frame, int8_t **table, int *table_w, int *table_h,
+ int *qscale_type);
+
+#endif // AVFILTER_QP_TABLE_H