FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/hw_base_encode.h
Date: 2026-08-27 23:16:01
Exec Total Coverage
Lines: 0 3 0.0%
Functions: 0 1 0.0%
Branches: 0 0 -%

Line Branch Exec Source
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 AVCODEC_HW_BASE_ENCODE_H
20 #define AVCODEC_HW_BASE_ENCODE_H
21
22 #include "avcodec.h"
23 #include "libavutil/hwcontext.h"
24 #include "libavutil/fifo.h"
25
26 #define MAX_DPB_SIZE 16
27 #define MAX_PICTURE_REFERENCES 2
28 #define MAX_REORDER_DELAY 16
29 #define MAX_ASYNC_DEPTH 64
30 #define MAX_REFERENCE_LIST_NUM 2
31
32 static inline const char *ff_hw_base_encode_get_pictype_name(const int type)
33 {
34 const char * const picture_type_name[] = { "IDR", "I", "P", "B" };
35 return picture_type_name[type];
36 }
37
38 enum {
39 FF_HW_PICTURE_TYPE_IDR = 0,
40 FF_HW_PICTURE_TYPE_I = 1,
41 FF_HW_PICTURE_TYPE_P = 2,
42 FF_HW_PICTURE_TYPE_B = 3,
43 };
44
45 enum {
46 // Codec supports controlling the subdivision of pictures into slices.
47 FF_HW_FLAG_SLICE_CONTROL = 1 << 0,
48 // Codec only supports constant quality (no rate control).
49 FF_HW_FLAG_CONSTANT_QUALITY_ONLY = 1 << 1,
50 // Codec is intra-only.
51 FF_HW_FLAG_INTRA_ONLY = 1 << 2,
52 // Codec supports B-pictures.
53 FF_HW_FLAG_B_PICTURES = 1 << 3,
54 // Codec supports referencing B-pictures.
55 FF_HW_FLAG_B_PICTURE_REFERENCES = 1 << 4,
56 // Codec supports non-IDR key pictures (that is, key pictures do
57 // not necessarily empty the DPB).
58 FF_HW_FLAG_NON_IDR_KEY_PICTURES = 1 << 5,
59 // Output is in display order; set DTS = PTS on every packet
60 // (e.g. AV1 hidden anchor frames with show_existing_frame).
61 FF_HW_FLAG_TIMESTAMP_NO_DELAY = 1 << 6,
62 };
63
64 typedef struct FFHWBaseEncodePicture {
65 // API-specific private data
66 void *priv;
67 // Codec-specific private data
68 void *codec_priv;
69
70 struct FFHWBaseEncodePicture *next;
71
72 int64_t display_order;
73 int64_t encode_order;
74 int64_t pts;
75 int64_t duration;
76 int force_idr;
77
78 void *opaque;
79 AVBufferRef *opaque_ref;
80
81 int type;
82 int b_depth;
83 int encode_issued;
84 int encode_complete;
85
86 AVFrame *input_image;
87 AVFrame *recon_image;
88
89 // Whether this picture is a reference picture.
90 int is_reference;
91
92 // The contents of the DPB after this picture has been decoded.
93 // This will contain the picture itself if it is a reference picture,
94 // but not if it isn't.
95 int nb_dpb_pics;
96 struct FFHWBaseEncodePicture *dpb[MAX_DPB_SIZE];
97 // The reference pictures used in decoding this picture. If they are
98 // used by later pictures they will also appear in the DPB. ref[0][] for
99 // previous reference frames. ref[1][] for future reference frames.
100 int nb_refs[MAX_REFERENCE_LIST_NUM];
101 struct FFHWBaseEncodePicture *refs[MAX_REFERENCE_LIST_NUM][MAX_PICTURE_REFERENCES];
102 // The previous reference picture in encode order. Must be in at least
103 // one of the reference list and DPB list.
104 struct FFHWBaseEncodePicture *prev;
105 // Reference count for other pictures referring to this one through
106 // the above pointers, directly from incomplete pictures and indirectly
107 // through completed pictures.
108 int ref_count[2];
109 int ref_removed[2];
110 } FFHWBaseEncodePicture;
111
112 typedef struct FFHWEncodePictureOperation {
113 // Size of API-specific internal picture data
114 size_t priv_size;
115 // Initialize API-specific internals
116 int (*init)(AVCodecContext *avctx, FFHWBaseEncodePicture *pic);
117 // Issue the picture structure, which will send the frame surface to HW Encode API.
118 int (*issue)(AVCodecContext *avctx, FFHWBaseEncodePicture *pic);
119 // Get the output AVPacket.
120 int (*output)(AVCodecContext *avctx, FFHWBaseEncodePicture *pic, AVPacket *pkt);
121 // Free the picture structure.
122 int (*free)(AVCodecContext *avctx, FFHWBaseEncodePicture *pic);
123 } FFHWEncodePictureOperation;
124
125 typedef struct FFHWBaseEncodeContext {
126 const AVClass *class;
127 void *log_ctx;
128
129 // Hardware-specific hooks.
130 const struct FFHWEncodePictureOperation *op;
131
132 // Global options.
133
134 // Number of I frames between IDR frames.
135 int idr_interval;
136
137 // Desired B frame reference depth.
138 int desired_b_depth;
139
140 // The required size of surfaces. This is probably the input
141 // size (AVCodecContext.width|height) aligned up to whatever
142 // block size is required by the codec.
143 int surface_width;
144 int surface_height;
145
146 // The block size for slice calculations.
147 int slice_block_width;
148 int slice_block_height;
149
150 // The hardware device context.
151 AVBufferRef *device_ref;
152 AVHWDeviceContext *device;
153
154 // The hardware frame context containing the input frames.
155 AVBufferRef *input_frames_ref;
156 AVHWFramesContext *input_frames;
157
158 // The hardware frame context containing the reconstructed frames.
159 AVBufferRef *recon_frames_ref;
160 AVHWFramesContext *recon_frames;
161
162 /* Allocates a reconstruction frame; used instead of recon_frames_ref */
163 int (*get_recon_frame)(AVCodecContext *avctx, AVFrame *frame);
164
165 // Current encoding window, in display (input) order.
166 FFHWBaseEncodePicture *pic_start, *pic_end;
167 // The next picture to use as the previous reference picture in
168 // encoding order. Order from small to large in encoding order.
169 FFHWBaseEncodePicture *next_prev[MAX_PICTURE_REFERENCES];
170 int nb_next_prev;
171
172 // Next input order index (display order).
173 int64_t input_order;
174 // Number of frames that output is behind input.
175 int64_t output_delay;
176 // Next encode order index.
177 int64_t encode_order;
178 // Number of frames decode output will need to be delayed.
179 int64_t decode_delay;
180 // Next output order index (in encode order).
181 int64_t output_order;
182
183 // Timestamp handling.
184 int64_t first_pts;
185 int64_t dts_pts_diff;
186 int64_t ts_ring[MAX_REORDER_DELAY * 3 +
187 MAX_ASYNC_DEPTH];
188
189 // Frame type decision.
190 int gop_size;
191 int closed_gop;
192 int gop_per_idr;
193 int p_per_i;
194 int max_b_depth;
195 int b_per_p;
196 int force_idr;
197 int idr_counter;
198 int gop_counter;
199 int end_of_stream;
200 int p_to_gpb;
201
202 // The number of L0/L1 references supported by the driver.
203 int ref_l0;
204 int ref_l1;
205
206 // Whether the driver supports ROI at all.
207 int roi_allowed;
208
209 // The encoder does not support cropping information, so warn about
210 // it the first time we encounter any nonzero crop fields.
211 int crop_warned;
212 // If the driver does not support ROI then warn the first time we
213 // encounter a frame with ROI side data.
214 int roi_warned;
215
216 // The frame to be filled with data.
217 AVFrame *frame;
218
219 // Whether the HW supports sync buffer function.
220 // If supported, encode_fifo/async_depth will be used together.
221 // Used for output buffer synchronization.
222 int async_encode;
223
224 // Store buffered pic.
225 AVFifo *encode_fifo;
226 // Max number of frame buffered in encoder.
227 int async_depth;
228
229 /** Tail data of a pic, now only used for av1 repeat frame header. */
230 AVPacket *tail_pkt;
231 } FFHWBaseEncodeContext;
232
233 int ff_hw_base_encode_set_output_property(FFHWBaseEncodeContext *ctx, AVCodecContext *avctx,
234 FFHWBaseEncodePicture *pic, AVPacket *pkt, int flag_no_delay);
235
236 int ff_hw_base_encode_receive_packet(FFHWBaseEncodeContext *ctx, AVCodecContext *avctx, AVPacket *pkt);
237
238 int ff_hw_base_init_gop_structure(FFHWBaseEncodeContext *ctx, AVCodecContext *avctx,
239 uint32_t ref_l0, uint32_t ref_l1,
240 int flags, int prediction_pre_only);
241
242 int ff_hw_base_get_recon_format(FFHWBaseEncodeContext *ctx, const void *hwconfig,
243 enum AVPixelFormat *fmt);
244
245 int ff_hw_base_encode_init(AVCodecContext *avctx, FFHWBaseEncodeContext *ctx);
246
247 int ff_hw_base_encode_close(FFHWBaseEncodeContext *ctx);
248
249 #define HW_BASE_ENCODE_COMMON_OPTIONS \
250 { "idr_interval", \
251 "Distance (in I-frames) between key frames", \
252 OFFSET(common.base.idr_interval), AV_OPT_TYPE_INT, \
253 { .i64 = 0 }, 0, INT_MAX, FLAGS }, \
254 { "b_depth", \
255 "Maximum B-frame reference depth", \
256 OFFSET(common.base.desired_b_depth), AV_OPT_TYPE_INT, \
257 { .i64 = 1 }, 1, INT_MAX, FLAGS }, \
258 { "async_depth", "Maximum processing parallelism. " \
259 "Increase this to improve single channel performance.", \
260 OFFSET(common.base.async_depth), AV_OPT_TYPE_INT, \
261 { .i64 = 2 }, 1, MAX_ASYNC_DEPTH, FLAGS }
262
263 #endif /* AVCODEC_HW_BASE_ENCODE_H */
264