FFmpeg coverage


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