FFmpeg coverage


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