FFmpeg
hw_base_encode.h
Go to the documentation of this file.
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 {
43 };
44 
45 enum {
46  // Codec supports controlling the subdivision of pictures into slices.
48  // Codec only supports constant quality (no rate control).
50  // Codec is intra-only.
52  // Codec supports B-pictures.
54  // Codec supports referencing B-pictures.
56  // Codec supports non-IDR key pictures (that is, key pictures do
57  // not necessarily empty the DPB).
59 };
60 
61 typedef struct FFHWBaseEncodePicture {
62  // API-specific private data
63  void *priv;
64  // Codec-specific private data
65  void *codec_priv;
66 
68 
73  int force_idr;
74 
75  void *opaque;
77 
78  int type;
79  int b_depth;
82 
85 
86  // Whether this picture is a reference picture.
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.
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.
99  // The previous reference picture in encode order. Must be in at least
100  // one of the reference list and DPB list.
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];
108 
110  // Size of API-specific internal picture data
111  size_t priv_size;
112  // Initialize API-specific internals
114  // Issue the picture structure, which will send the frame surface to HW Encode API.
116  // Get the output AVPacket.
118  // Free the picture structure.
121 
122 typedef struct FFHWBaseEncodeContext {
123  const AVClass *class;
124  void *log_ctx;
125 
126  // Hardware-specific hooks.
128 
129  // Global options.
130 
131  // Number of I frames between IDR frames.
133 
134  // Desired B frame reference 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.
142 
143  // The block size for slice calculations.
146 
147  // The hardware device context.
150 
151  // The hardware frame context containing the input frames.
154 
155  // The hardware frame context containing the reconstructed frames.
158 
159  // Current encoding window, in display (input) order.
161  // The next picture to use as the previous reference picture in
162  // encoding order. Order from small to large in encoding order.
165 
166  // Next input order index (display order).
168  // Number of frames that output is behind input.
170  // Next encode order index.
172  // Number of frames decode output will need to be delayed.
174  // Next output order index (in encode order).
176 
177  // Timestamp handling.
182 
183  // Frame type decision.
184  int gop_size;
187  int p_per_i;
189  int b_per_p;
194  int p_to_gpb;
195 
196  // Whether the driver supports ROI at all.
198 
199  // The encoder does not support cropping information, so warn about
200  // it the first time we encounter any nonzero crop fields.
202  // If the driver does not support ROI then warn the first time we
203  // encounter a frame with ROI side data.
205 
206  // The frame to be filled with data.
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.
213 
214  // Store buffered pic.
216  // Max number of frame buffered in encoder.
218 
219  /** Tail data of a pic, now only used for av1 repeat frame header. */
222 
224  FFHWBaseEncodePicture *pic, AVPacket *pkt, int flag_no_delay);
225 
227 
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 
236 
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 */
FFHWBaseEncodeContext::idr_counter
int idr_counter
Definition: hw_base_encode.h:191
FFHWBaseEncodeContext::output_delay
int64_t output_delay
Definition: hw_base_encode.h:169
FFHWBaseEncodePicture::b_depth
int b_depth
Definition: hw_base_encode.h:79
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:71
FFHWBaseEncodeContext::recon_frames_ref
AVBufferRef * recon_frames_ref
Definition: hw_base_encode.h:156
FFHWBaseEncodePicture::next
struct FFHWBaseEncodePicture * next
Definition: hw_base_encode.h:67
FF_HW_PICTURE_TYPE_P
@ FF_HW_PICTURE_TYPE_P
Definition: hw_base_encode.h:41
FFHWBaseEncodePicture::priv
void * priv
Definition: hw_base_encode.h:63
FFHWBaseEncodePicture::codec_priv
void * codec_priv
Definition: hw_base_encode.h:65
int64_t
long long int64_t
Definition: coverity.c:34
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:389
FFHWBaseEncodeContext::next_prev
FFHWBaseEncodePicture * next_prev[MAX_PICTURE_REFERENCES]
Definition: hw_base_encode.h:163
FFHWBaseEncodeContext::gop_counter
int gop_counter
Definition: hw_base_encode.h:192
FFHWBaseEncodePicture::recon_image
AVFrame * recon_image
Definition: hw_base_encode.h:84
FF_HW_FLAG_B_PICTURE_REFERENCES
@ FF_HW_FLAG_B_PICTURE_REFERENCES
Definition: hw_base_encode.h:55
FFHWBaseEncodeContext::slice_block_width
int slice_block_width
Definition: hw_base_encode.h:144
FFHWBaseEncodeContext::roi_warned
int roi_warned
Definition: hw_base_encode.h:204
FFHWBaseEncodeContext::output_order
int64_t output_order
Definition: hw_base_encode.h:175
FFHWBaseEncodeContext::slice_block_height
int slice_block_height
Definition: hw_base_encode.h:145
FFHWBaseEncodeContext
Definition: hw_base_encode.h:122
FFHWBaseEncodeContext::ts_ring
int64_t ts_ring[MAX_REORDER_DELAY *3+MAX_ASYNC_DEPTH]
Definition: hw_base_encode.h:181
fifo.h
FFHWBaseEncodePicture::type
int type
Definition: hw_base_encode.h:78
FFHWBaseEncodePicture::is_reference
int is_reference
Definition: hw_base_encode.h:87
FFHWBaseEncodePicture::ref_removed
int ref_removed[2]
Definition: hw_base_encode.h:106
FFHWBaseEncodeContext::input_frames_ref
AVBufferRef * input_frames_ref
Definition: hw_base_encode.h:152
FF_HW_FLAG_SLICE_CONTROL
@ FF_HW_FLAG_SLICE_CONTROL
Definition: hw_base_encode.h:47
FFHWBaseEncodePicture::input_image
AVFrame * input_image
Definition: hw_base_encode.h:83
FF_HW_FLAG_B_PICTURES
@ FF_HW_FLAG_B_PICTURES
Definition: hw_base_encode.h:53
type
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf type
Definition: writing_filters.txt:86
FFHWBaseEncodeContext::device
AVHWDeviceContext * device
Definition: hw_base_encode.h:149
FFHWBaseEncodePicture::prev
struct FFHWBaseEncodePicture * prev
Definition: hw_base_encode.h:101
AVHWDeviceContext
This struct aggregates all the (hardware/vendor-specific) "high-level" state, i.e.
Definition: hwcontext.h:60
pkt
AVPacket * pkt
Definition: movenc.c:60
FFHWBaseEncodePicture::opaque
void * opaque
Definition: hw_base_encode.h:75
FFHWBaseEncodeContext::tail_pkt
AVPacket * tail_pkt
Tail data of a pic, now only used for av1 repeat frame header.
Definition: hw_base_encode.h:220
ff_hw_base_get_recon_format
int ff_hw_base_get_recon_format(FFHWBaseEncodeContext *ctx, const void *hwconfig, enum AVPixelFormat *fmt)
Definition: hw_base_encode.c:714
FFHWBaseEncodeContext::max_b_depth
int max_b_depth
Definition: hw_base_encode.h:188
FFHWBaseEncodeContext::async_encode
int async_encode
Definition: hw_base_encode.h:212
ff_hw_base_encode_close
int ff_hw_base_encode_close(FFHWBaseEncodeContext *ctx)
Definition: hw_base_encode.c:805
FFHWBaseEncodeContext::pic_end
FFHWBaseEncodePicture * pic_end
Definition: hw_base_encode.h:160
ctx
AVFormatContext * ctx
Definition: movenc.c:49
FFHWBaseEncodeContext::p_per_i
int p_per_i
Definition: hw_base_encode.h:187
FF_HW_PICTURE_TYPE_IDR
@ FF_HW_PICTURE_TYPE_IDR
Definition: hw_base_encode.h:39
ff_hw_base_encode_set_output_property
int ff_hw_base_encode_set_output_property(FFHWBaseEncodeContext *ctx, AVCodecContext *avctx, FFHWBaseEncodePicture *pic, AVPacket *pkt, int flag_no_delay)
Definition: hw_base_encode.c:518
FFHWBaseEncodeContext::pic_start
FFHWBaseEncodePicture * pic_start
Definition: hw_base_encode.h:160
FFHWBaseEncodeContext::b_per_p
int b_per_p
Definition: hw_base_encode.h:189
FFHWBaseEncodePicture::dpb
struct FFHWBaseEncodePicture * dpb[MAX_DPB_SIZE]
Definition: hw_base_encode.h:93
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:75
FFHWBaseEncodeContext::log_ctx
void * log_ctx
Definition: hw_base_encode.h:124
FFHWEncodePictureOperation::free
int(* free)(AVCodecContext *avctx, FFHWBaseEncodePicture *pic)
Definition: hw_base_encode.h:119
FFHWEncodePictureOperation
Definition: hw_base_encode.h:109
FFHWBaseEncodeContext::input_order
int64_t input_order
Definition: hw_base_encode.h:167
FFHWBaseEncodePicture::force_idr
int force_idr
Definition: hw_base_encode.h:73
FFHWEncodePictureOperation::priv_size
size_t priv_size
Definition: hw_base_encode.h:111
FFHWBaseEncodeContext::frame
AVFrame * frame
Definition: hw_base_encode.h:207
AVFifo
Definition: fifo.c:35
FFHWBaseEncodePicture::nb_refs
int nb_refs[MAX_REFERENCE_LIST_NUM]
Definition: hw_base_encode.h:97
FFHWBaseEncodeContext::nb_next_prev
int nb_next_prev
Definition: hw_base_encode.h:164
MAX_DPB_SIZE
#define MAX_DPB_SIZE
Definition: hw_base_encode.h:26
FF_HW_FLAG_NON_IDR_KEY_PICTURES
@ FF_HW_FLAG_NON_IDR_KEY_PICTURES
Definition: hw_base_encode.h:58
FFHWBaseEncodeContext::crop_warned
int crop_warned
Definition: hw_base_encode.h:201
FFHWBaseEncodeContext::decode_delay
int64_t decode_delay
Definition: hw_base_encode.h:173
FFHWBaseEncodeContext::p_to_gpb
int p_to_gpb
Definition: hw_base_encode.h:194
FFHWBaseEncodePicture::encode_order
int64_t encode_order
Definition: hw_base_encode.h:70
FFHWBaseEncodeContext::closed_gop
int closed_gop
Definition: hw_base_encode.h:185
FFHWBaseEncodeContext::encode_order
int64_t encode_order
Definition: hw_base_encode.h:171
ff_hw_base_encode_receive_packet
int ff_hw_base_encode_receive_packet(FFHWBaseEncodeContext *ctx, AVCodecContext *avctx, AVPacket *pkt)
Definition: hw_base_encode.c:557
FFHWBaseEncodeContext::roi_allowed
int roi_allowed
Definition: hw_base_encode.h:197
FFHWBaseEncodePicture::opaque_ref
AVBufferRef * opaque_ref
Definition: hw_base_encode.h:76
MAX_ASYNC_DEPTH
#define MAX_ASYNC_DEPTH
Definition: hw_base_encode.h:29
FFHWBaseEncodeContext::op
const struct FFHWEncodePictureOperation * op
Definition: hw_base_encode.h:127
MAX_REFERENCE_LIST_NUM
#define MAX_REFERENCE_LIST_NUM
Definition: hw_base_encode.h:30
FFHWEncodePictureOperation::init
int(* init)(AVCodecContext *avctx, FFHWBaseEncodePicture *pic)
Definition: hw_base_encode.h:113
FFHWBaseEncodePicture::refs
struct FFHWBaseEncodePicture * refs[MAX_REFERENCE_LIST_NUM][MAX_PICTURE_REFERENCES]
Definition: hw_base_encode.h:98
FFHWBaseEncodeContext::gop_per_idr
int gop_per_idr
Definition: hw_base_encode.h:186
FFHWBaseEncodeContext::end_of_stream
int end_of_stream
Definition: hw_base_encode.h:193
avcodec.h
AVHWFramesContext
This struct describes a set or pool of "hardware" frames (i.e.
Definition: hwcontext.h:115
FF_HW_FLAG_INTRA_ONLY
@ FF_HW_FLAG_INTRA_ONLY
Definition: hw_base_encode.h:51
FFHWBaseEncodeContext::gop_size
int gop_size
Definition: hw_base_encode.h:184
FFHWBaseEncodePicture
Definition: hw_base_encode.h:61
FFHWBaseEncodeContext::first_pts
int64_t first_pts
Definition: hw_base_encode.h:178
FFHWBaseEncodeContext::idr_interval
int idr_interval
Definition: hw_base_encode.h:132
FFHWBaseEncodeContext::device_ref
AVBufferRef * device_ref
Definition: hw_base_encode.h:148
FFHWBaseEncodeContext::encode_fifo
AVFifo * encode_fifo
Definition: hw_base_encode.h:215
FFHWBaseEncodeContext::force_idr
int force_idr
Definition: hw_base_encode.h:190
ff_hw_base_encode_get_pictype_name
static const char * ff_hw_base_encode_get_pictype_name(const int type)
Definition: hw_base_encode.h:32
FFHWBaseEncodeContext::surface_height
int surface_height
Definition: hw_base_encode.h:141
FFHWBaseEncodeContext::async_depth
int async_depth
Definition: hw_base_encode.h:217
AVCodecContext
main external API structure.
Definition: avcodec.h:451
FF_HW_FLAG_CONSTANT_QUALITY_ONLY
@ FF_HW_FLAG_CONSTANT_QUALITY_ONLY
Definition: hw_base_encode.h:49
FFHWBaseEncodeContext::input_frames
AVHWFramesContext * input_frames
Definition: hw_base_encode.h:153
FFHWBaseEncodeContext::desired_b_depth
int desired_b_depth
Definition: hw_base_encode.h:135
FFHWBaseEncodeContext::surface_width
int surface_width
Definition: hw_base_encode.h:140
FFHWBaseEncodeContext::dts_pts_diff
int64_t dts_pts_diff
Definition: hw_base_encode.h:179
FFHWBaseEncodePicture::encode_complete
int encode_complete
Definition: hw_base_encode.h:81
AVBufferRef
A reference to a data buffer.
Definition: buffer.h:82
ff_hw_base_init_gop_structure
int ff_hw_base_init_gop_structure(FFHWBaseEncodeContext *ctx, AVCodecContext *avctx, uint32_t ref_l0, uint32_t ref_l1, int flags, int prediction_pre_only)
Definition: hw_base_encode.c:661
FFHWBaseEncodePicture::pts
int64_t pts
Definition: hw_base_encode.h:71
AVPacket
This structure stores compressed data.
Definition: packet.h:516
FF_HW_PICTURE_TYPE_B
@ FF_HW_PICTURE_TYPE_B
Definition: hw_base_encode.h:42
FFHWBaseEncodeContext::recon_frames
AVHWFramesContext * recon_frames
Definition: hw_base_encode.h:157
FFHWBaseEncodePicture::duration
int64_t duration
Definition: hw_base_encode.h:72
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:482
hwcontext.h
FFHWEncodePictureOperation::issue
int(* issue)(AVCodecContext *avctx, FFHWBaseEncodePicture *pic)
Definition: hw_base_encode.h:115
FFHWBaseEncodePicture::encode_issued
int encode_issued
Definition: hw_base_encode.h:80
FFHWBaseEncodePicture::display_order
int64_t display_order
Definition: hw_base_encode.h:69
FFHWEncodePictureOperation::output
int(* output)(AVCodecContext *avctx, FFHWBaseEncodePicture *pic, AVPacket *pkt)
Definition: hw_base_encode.h:117
FF_HW_PICTURE_TYPE_I
@ FF_HW_PICTURE_TYPE_I
Definition: hw_base_encode.h:40
FFHWBaseEncodePicture::ref_count
int ref_count[2]
Definition: hw_base_encode.h:105
FFHWBaseEncodePicture::nb_dpb_pics
int nb_dpb_pics
Definition: hw_base_encode.h:92
MAX_REORDER_DELAY
#define MAX_REORDER_DELAY
Definition: hw_base_encode.h:28
MAX_PICTURE_REFERENCES
#define MAX_PICTURE_REFERENCES
Definition: hw_base_encode.h:27
ff_hw_base_encode_init
int ff_hw_base_encode_init(AVCodecContext *avctx, FFHWBaseEncodeContext *ctx)
Definition: hw_base_encode.c:772