2 * This file is part of FFmpeg.
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.
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.
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
19 #ifndef AVCODEC_HW_BASE_ENCODE_H
20 #define AVCODEC_HW_BASE_ENCODE_H
23 #include "libavutil/hwcontext.h"
24 #include "libavutil/fifo.h"
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
32 static inline const char *ff_hw_base_encode_get_pictype_name(const int type
)
34 const char * const picture_type_name
[] = { "IDR", "I", "P", "B" };
35 return picture_type_name
[type
];
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,
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,
61 typedef struct FFHWBaseEncodePicture
{
62 // API-specific private data
64 // Codec-specific private data
67 struct FFHWBaseEncodePicture
*next
;
69 int64_t display_order
;
76 AVBufferRef
*opaque_ref
;
86 // Whether this picture is a reference picture.
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.
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.
107 } FFHWBaseEncodePicture
;
109 typedef struct FFHWEncodePictureOperation
{
110 // Size of API-specific internal picture data
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
;
122 typedef struct FFHWBaseEncodeContext
{
123 const AVClass
*class;
126 // Hardware-specific hooks.
127 const struct FFHWEncodePictureOperation
*op
;
131 // Number of I frames between IDR frames.
134 // Desired B frame reference depth.
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.
143 // The block size for slice calculations.
144 int slice_block_width
;
145 int slice_block_height
;
147 // The hardware device context.
148 AVBufferRef
*device_ref
;
149 AVHWDeviceContext
*device
;
151 // The hardware frame context containing the input frames.
152 AVBufferRef
*input_frames_ref
;
153 AVHWFramesContext
*input_frames
;
155 // The hardware frame context containing the reconstructed frames.
156 AVBufferRef
*recon_frames_ref
;
157 AVHWFramesContext
*recon_frames
;
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
];
166 // Next input order index (display 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
;
177 // Timestamp handling.
179 int64_t dts_pts_diff
;
180 int64_t ts_ring
[MAX_REORDER_DELAY
* 3 +
183 // Frame type decision.
196 // The number of L0/L1 references supported by the driver.
200 // Whether the driver supports ROI at all.
203 // The encoder does not support cropping information, so warn about
204 // it the first time we encounter any nonzero crop fields.
206 // If the driver does not support ROI then warn the first time we
207 // encounter a frame with ROI side data.
210 // The frame to be filled with data.
213 // Whether the HW supports sync buffer function.
214 // If supported, encode_fifo/async_depth will be used together.
215 // Used for output buffer synchronization.
218 // Store buffered pic.
220 // Max number of frame buffered in encoder.
223 /** Tail data of a pic, now only used for av1 repeat frame header. */
225 } FFHWBaseEncodeContext
;
227 int ff_hw_base_encode_set_output_property(FFHWBaseEncodeContext
*ctx
, AVCodecContext
*avctx
,
228 FFHWBaseEncodePicture
*pic
, AVPacket
*pkt
, int flag_no_delay
);
230 int ff_hw_base_encode_receive_packet(FFHWBaseEncodeContext
*ctx
, AVCodecContext
*avctx
, AVPacket
*pkt
);
232 int ff_hw_base_init_gop_structure(FFHWBaseEncodeContext
*ctx
, AVCodecContext
*avctx
,
233 uint32_t ref_l0
, uint32_t ref_l1
,
234 int flags
, int prediction_pre_only
);
236 int ff_hw_base_get_recon_format(FFHWBaseEncodeContext
*ctx
, const void *hwconfig
,
237 enum AVPixelFormat
*fmt
);
239 int ff_hw_base_encode_init(AVCodecContext
*avctx
, FFHWBaseEncodeContext
*ctx
);
241 int ff_hw_base_encode_close(FFHWBaseEncodeContext
*ctx
);
243 #define HW_BASE_ENCODE_COMMON_OPTIONS \
245 "Distance (in I-frames) between key frames", \
246 OFFSET(common.base.idr_interval), AV_OPT_TYPE_INT, \
247 { .i64 = 0 }, 0, INT_MAX, FLAGS }, \
249 "Maximum B-frame reference depth", \
250 OFFSET(common.base.desired_b_depth), AV_OPT_TYPE_INT, \
251 { .i64 = 1 }, 1, INT_MAX, FLAGS }, \
252 { "async_depth", "Maximum processing parallelism. " \
253 "Increase this to improve single channel performance.", \
254 OFFSET(common.base.async_depth), AV_OPT_TYPE_INT, \
255 { .i64 = 2 }, 1, MAX_ASYNC_DEPTH, FLAGS }
257 #endif /* AVCODEC_HW_BASE_ENCODE_H */