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 {
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;
72 
73  int type;
74  int b_depth;
77 
80 
81  void *priv_data;
82 
83  // Whether this picture is a reference picture.
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.
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.
96  // The previous reference picture in encode order. Must be in at least
97  // one of the reference list and DPB list.
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];
105 
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.
114  // Free the picture structure.
117 
118 typedef struct FFHWBaseEncodeContext {
119  const AVClass *class;
120  void *log_ctx;
121 
122  // Hardware-specific hooks.
124 
125  // Global options.
126 
127  // Number of I frames between IDR frames.
129 
130  // Desired B frame reference 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.
138 
139  // The block size for slice calculations.
142 
143  // The hardware device context.
146 
147  // The hardware frame context containing the input frames.
150 
151  // The hardware frame context containing the reconstructed frames.
154 
155  // Current encoding window, in display (input) order.
157  // The next picture to use as the previous reference picture in
158  // encoding order. Order from small to large in encoding order.
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 +
178 
179  // Frame type decision.
180  int gop_size;
183  int p_per_i;
185  int b_per_p;
190  int p_to_gpb;
191 
192  // Whether the driver supports ROI at all.
194 
195  // The encoder does not support cropping information, so warn about
196  // it the first time we encounter any nonzero crop fields.
198  // If the driver does not support ROI then warn the first time we
199  // encounter a frame with ROI side data.
201 
202  // The frame to be filled with data.
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.
209 
210  // Store buffered pic.
212  // Max number of frame buffered in encoder.
214 
215  /** Tail data of a pic, now only used for av1 repeat frame header. */
218 
220  FFHWBaseEncodePicture *pic, AVPacket *pkt, int flag_no_delay);
221 
223 
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 
232 
234 
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 */
FFHWBaseEncodeContext::idr_counter
int idr_counter
Definition: hw_base_encode.h:187
FFHWBaseEncodeContext::output_delay
int64_t output_delay
Definition: hw_base_encode.h:165
FFHWBaseEncodePicture::b_depth
int b_depth
Definition: hw_base_encode.h:74
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:71
FFHWBaseEncodeContext::recon_frames_ref
AVBufferRef * recon_frames_ref
Definition: hw_base_encode.h:152
FFHWBaseEncodePicture::next
struct FFHWBaseEncodePicture * next
Definition: hw_base_encode.h:62
FF_HW_PICTURE_TYPE_I
@ FF_HW_PICTURE_TYPE_I
Definition: hw_base_encode.h:40
FF_HW_FLAG_SLICE_CONTROL
@ FF_HW_FLAG_SLICE_CONTROL
Definition: hw_base_encode.h:47
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:374
FFHWBaseEncodeContext::next_prev
FFHWBaseEncodePicture * next_prev[MAX_PICTURE_REFERENCES]
Definition: hw_base_encode.h:159
FFHWBaseEncodeContext::gop_counter
int gop_counter
Definition: hw_base_encode.h:188
FFHWBaseEncodePicture::recon_image
AVFrame * recon_image
Definition: hw_base_encode.h:79
FFHWBaseEncodeContext::slice_block_width
int slice_block_width
Definition: hw_base_encode.h:140
FFHWBaseEncodeContext::roi_warned
int roi_warned
Definition: hw_base_encode.h:200
FF_HW_FLAG_B_PICTURE_REFERENCES
@ FF_HW_FLAG_B_PICTURE_REFERENCES
Definition: hw_base_encode.h:55
FFHWBaseEncodeContext::output_order
int64_t output_order
Definition: hw_base_encode.h:171
FFHWBaseEncodeContext::slice_block_height
int slice_block_height
Definition: hw_base_encode.h:141
FFHWBaseEncodeContext
Definition: hw_base_encode.h:118
FFHWBaseEncodeContext::ts_ring
int64_t ts_ring[MAX_REORDER_DELAY *3+MAX_ASYNC_DEPTH]
Definition: hw_base_encode.h:177
fifo.h
FFHWBaseEncodePicture::type
int type
Definition: hw_base_encode.h:73
FFHWBaseEncodePicture::is_reference
int is_reference
Definition: hw_base_encode.h:84
FF_HW_PICTURE_TYPE_B
@ FF_HW_PICTURE_TYPE_B
Definition: hw_base_encode.h:42
FFHWBaseEncodePicture::ref_removed
int ref_removed[2]
Definition: hw_base_encode.h:103
FFHWEncodePictureOperation::output
int(* output)(AVCodecContext *avctx, const FFHWBaseEncodePicture *base_pic, AVPacket *pkt)
Definition: hw_base_encode.h:113
FFHWBaseEncodeContext::input_frames_ref
AVBufferRef * input_frames_ref
Definition: hw_base_encode.h:148
FFHWBaseEncodePicture::input_image
AVFrame * input_image
Definition: hw_base_encode.h:78
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
FF_HW_FLAG_B_PICTURES
@ FF_HW_FLAG_B_PICTURES
Definition: hw_base_encode.h:53
FFHWBaseEncodeContext::device
AVHWDeviceContext * device
Definition: hw_base_encode.h:145
FFHWBaseEncodePicture::prev
struct FFHWBaseEncodePicture * prev
Definition: hw_base_encode.h:98
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:70
FFHWBaseEncodeContext::tail_pkt
AVPacket * tail_pkt
Tail data of a pic, now only used for av1 repeat frame header.
Definition: hw_base_encode.h:216
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:683
FFHWBaseEncodeContext::max_b_depth
int max_b_depth
Definition: hw_base_encode.h:184
FFHWBaseEncodeContext::async_encode
int async_encode
Definition: hw_base_encode.h:208
FFHWEncodePictureOperation::issue
int(* issue)(AVCodecContext *avctx, const FFHWBaseEncodePicture *base_pic)
Definition: hw_base_encode.h:111
FF_HW_FLAG_CONSTANT_QUALITY_ONLY
@ FF_HW_FLAG_CONSTANT_QUALITY_ONLY
Definition: hw_base_encode.h:49
ff_hw_base_encode_close
int ff_hw_base_encode_close(FFHWBaseEncodeContext *ctx)
Definition: hw_base_encode.c:785
FFHWBaseEncodePicture::priv_data
void * priv_data
Definition: hw_base_encode.h:81
FFHWBaseEncodeContext::pic_end
FFHWBaseEncodePicture * pic_end
Definition: hw_base_encode.h:156
ctx
AVFormatContext * ctx
Definition: movenc.c:49
FFHWBaseEncodeContext::p_per_i
int p_per_i
Definition: hw_base_encode.h:183
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:486
FFHWBaseEncodeContext::pic_start
FFHWBaseEncodePicture * pic_start
Definition: hw_base_encode.h:156
FFHWBaseEncodeContext::b_per_p
int b_per_p
Definition: hw_base_encode.h:185
FFHWBaseEncodePicture::dpb
struct FFHWBaseEncodePicture * dpb[MAX_DPB_SIZE]
Definition: hw_base_encode.h:90
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
FFHWBaseEncodeContext::log_ctx
void * log_ctx
Definition: hw_base_encode.h:120
FFHWEncodePictureOperation
Definition: hw_base_encode.h:106
FFHWBaseEncodeContext::input_order
int64_t input_order
Definition: hw_base_encode.h:163
FFHWBaseEncodePicture::force_idr
int force_idr
Definition: hw_base_encode.h:68
FFHWBaseEncodeContext::frame
AVFrame * frame
Definition: hw_base_encode.h:203
AVFifo
Definition: fifo.c:35
FFHWBaseEncodePicture::nb_refs
int nb_refs[MAX_REFERENCE_LIST_NUM]
Definition: hw_base_encode.h:94
FFHWBaseEncodeContext::nb_next_prev
int nb_next_prev
Definition: hw_base_encode.h:160
MAX_DPB_SIZE
#define MAX_DPB_SIZE
Definition: hw_base_encode.h:26
FFHWBaseEncodeContext::crop_warned
int crop_warned
Definition: hw_base_encode.h:197
FFHWBaseEncodeContext::decode_delay
int64_t decode_delay
Definition: hw_base_encode.h:169
FFHWBaseEncodeContext::p_to_gpb
int p_to_gpb
Definition: hw_base_encode.h:190
FFHWBaseEncodePicture::encode_order
int64_t encode_order
Definition: hw_base_encode.h:65
FFHWBaseEncodeContext::closed_gop
int closed_gop
Definition: hw_base_encode.h:181
FFHWBaseEncodeContext::encode_order
int64_t encode_order
Definition: hw_base_encode.h:167
ff_hw_base_encode_receive_packet
int ff_hw_base_encode_receive_packet(FFHWBaseEncodeContext *ctx, AVCodecContext *avctx, AVPacket *pkt)
Definition: hw_base_encode.c:525
FFHWBaseEncodeContext::roi_allowed
int roi_allowed
Definition: hw_base_encode.h:193
FFHWBaseEncodePicture::opaque_ref
AVBufferRef * opaque_ref
Definition: hw_base_encode.h:71
MAX_ASYNC_DEPTH
#define MAX_ASYNC_DEPTH
Definition: hw_base_encode.h:29
FFHWBaseEncodeContext::op
const struct FFHWEncodePictureOperation * op
Definition: hw_base_encode.h:123
MAX_REFERENCE_LIST_NUM
#define MAX_REFERENCE_LIST_NUM
Definition: hw_base_encode.h:30
FFHWBaseEncodePicture::refs
struct FFHWBaseEncodePicture * refs[MAX_REFERENCE_LIST_NUM][MAX_PICTURE_REFERENCES]
Definition: hw_base_encode.h:95
FFHWBaseEncodeContext::gop_per_idr
int gop_per_idr
Definition: hw_base_encode.h:182
FFHWBaseEncodeContext::end_of_stream
int end_of_stream
Definition: hw_base_encode.h:189
avcodec.h
AVHWFramesContext
This struct describes a set or pool of "hardware" frames (i.e.
Definition: hwcontext.h:115
FFHWBaseEncodeContext::gop_size
int gop_size
Definition: hw_base_encode.h:180
FFHWBaseEncodePicture
Definition: hw_base_encode.h:61
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:264
FFHWBaseEncodeContext::first_pts
int64_t first_pts
Definition: hw_base_encode.h:174
FFHWBaseEncodeContext::idr_interval
int idr_interval
Definition: hw_base_encode.h:128
FFHWBaseEncodeContext::device_ref
AVBufferRef * device_ref
Definition: hw_base_encode.h:144
FFHWBaseEncodeContext::encode_fifo
AVFifo * encode_fifo
Definition: hw_base_encode.h:211
ff_hw_base_encode_free
int ff_hw_base_encode_free(FFHWBaseEncodePicture *pic)
Definition: hw_base_encode.c:741
FF_HW_PICTURE_TYPE_P
@ FF_HW_PICTURE_TYPE_P
Definition: hw_base_encode.h:41
FFHWBaseEncodeContext::force_idr
int force_idr
Definition: hw_base_encode.h:186
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:137
FFHWBaseEncodeContext::async_depth
int async_depth
Definition: hw_base_encode.h:213
AVCodecContext
main external API structure.
Definition: avcodec.h:445
FF_HW_FLAG_INTRA_ONLY
@ FF_HW_FLAG_INTRA_ONLY
Definition: hw_base_encode.h:51
FFHWEncodePictureOperation::free
int(* free)(AVCodecContext *avctx, FFHWBaseEncodePicture *base_pic)
Definition: hw_base_encode.h:115
FFHWBaseEncodeContext::input_frames
AVHWFramesContext * input_frames
Definition: hw_base_encode.h:149
FFHWBaseEncodeContext::desired_b_depth
int desired_b_depth
Definition: hw_base_encode.h:131
FF_HW_FLAG_NON_IDR_KEY_PICTURES
@ FF_HW_FLAG_NON_IDR_KEY_PICTURES
Definition: hw_base_encode.h:58
FFHWBaseEncodeContext::surface_width
int surface_width
Definition: hw_base_encode.h:136
FFHWBaseEncodeContext::dts_pts_diff
int64_t dts_pts_diff
Definition: hw_base_encode.h:175
FFHWBaseEncodePicture::encode_complete
int encode_complete
Definition: hw_base_encode.h:76
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:630
FFHWBaseEncodePicture::pts
int64_t pts
Definition: hw_base_encode.h:66
AVPacket
This structure stores compressed data.
Definition: packet.h:497
FFHWBaseEncodeContext::recon_frames
AVHWFramesContext * recon_frames
Definition: hw_base_encode.h:153
FFHWBaseEncodePicture::duration
int64_t duration
Definition: hw_base_encode.h:67
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:474
hwcontext.h
FFHWBaseEncodePicture::encode_issued
int encode_issued
Definition: hw_base_encode.h:75
FFHWBaseEncodePicture::display_order
int64_t display_order
Definition: hw_base_encode.h:64
int
int
Definition: ffmpeg_filter.c:424
FFHWBaseEncodePicture::ref_count
int ref_count[2]
Definition: hw_base_encode.h:102
FFHWBaseEncodePicture::nb_dpb_pics
int nb_dpb_pics
Definition: hw_base_encode.h:89
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:752