]> git.sesse.net Git - ffmpeg/blob - libavutil/video_enc_params.h
libavutil: add API for exporting video frame quantizers
[ffmpeg] / libavutil / video_enc_params.h
1 /*
2  * This file is part of FFmpeg.
3  *
4  * FFmpeg is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * FFmpeg is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with FFmpeg; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18
19 #ifndef AVUTIL_VIDEO_ENC_PARAMS_H
20 #define AVUTIL_VIDEO_ENC_PARAMS_H
21
22 #include <stddef.h>
23 #include <stdint.h>
24
25 #include "libavutil/avassert.h"
26 #include "libavutil/frame.h"
27
28 enum AVVideoEncParamsType {
29     AV_VIDEO_ENC_PARAMS_NONE = -1,
30 };
31
32 /**
33  * Video encoding parameters for a given frame. This struct is allocated along
34  * with an optional array of per-block AVVideoBlockParams descriptors.
35  * Must be allocated with av_video_enc_params_alloc().
36  */
37 typedef struct AVVideoEncParams {
38     /**
39      * Number of blocks in the array.
40      *
41      * May be 0, in which case no per-block information is present. In this case
42      * the values of blocks_offset / block_size are unspecified and should not
43      * be accessed.
44      */
45     unsigned int nb_blocks;
46     /**
47      * Offset in bytes from the beginning of this structure at which the array
48      * of blocks starts.
49      */
50     size_t blocks_offset;
51     /*
52      * Size of each block in bytes. May not match sizeof(AVVideoBlockParams).
53      */
54     size_t block_size;
55
56     /**
57      * Type of the parameters (the codec they are used with).
58      */
59     enum AVVideoEncParamsType type;
60
61     /**
62      * Base quantisation parameter for the frame. The final quantiser for a
63      * given block in a given plane is obtained from this value, possibly
64      * combined with {@code delta_qp} and the per-block delta in a manner
65      * documented for each type.
66      */
67     int32_t qp;
68
69     /**
70      * Quantisation parameter offset from the base (per-frame) qp for a given
71      * plane (first index) and AC/DC coefficients (second index).
72      */
73     int32_t delta_qp[4][2];
74 } AVVideoEncParams;
75
76 /**
77  * Data structure for storing block-level encoding information.
78  * It is allocated as a part of AVVideoEncParams and should be retrieved with
79  * av_video_enc_params_block().
80  *
81  * sizeof(AVVideoBlockParams) is not a part of the ABI and new fields may be
82  * added to it.
83  */
84 typedef struct AVVideoBlockParams {
85     /**
86      * Distance in luma pixels from the top-left corner of the visible frame
87      * to the top-left corner of the block.
88      * Can be negative if top/right padding is present on the coded frame.
89      */
90     int src_x, src_y;
91     /**
92      * Width and height of the block in luma pixels.
93      */
94     int w, h;
95
96     /**
97      * Difference between this block's final quantization parameter and the
98      * corresponding per-frame value.
99      */
100     int32_t delta_qp;
101 } AVVideoBlockParams;
102
103 /*
104  * Get the block at the specified {@code idx}. Must be between 0 and nb_blocks.
105  */
106 static av_always_inline AVVideoBlockParams*
107 av_video_enc_params_block(AVVideoEncParams *par, unsigned int idx)
108 {
109     av_assert0(idx < par->nb_blocks);
110     return (AVVideoBlockParams *)((uint8_t *)par + par->blocks_offset +
111                                   idx * par->block_size);
112 }
113
114 /**
115  * Allocates memory for AVVideoEncParams of the given type, plus an array of
116  * {@code nb_blocks} AVVideoBlockParams and initializes the variables. Can be
117  * freed with a normal av_free() call.
118  *
119  * @param out_size if non-NULL, the size in bytes of the resulting data array is
120  * written here.
121  */
122 AVVideoEncParams *av_video_enc_params_alloc(enum AVVideoEncParamsType type,
123                                             unsigned int nb_blocks, size_t *out_size);
124
125 /**
126  * Allocates memory for AVEncodeInfoFrame plus an array of
127  * {@code nb_blocks} AVEncodeInfoBlock in the given AVFrame {@code frame}
128  * as AVFrameSideData of type AV_FRAME_DATA_ENCODE_INFO
129  * and initializes the variables.
130  */
131 AVVideoEncParams*
132 av_video_enc_params_create_side_data(AVFrame *frame, enum AVVideoEncParamsType type,
133                                      unsigned int nb_blocks);
134
135 #endif /* AVUTIL_VIDEO_ENC_PARAMS_H */