1 /*
2  * Copyright (c) 2016, Alliance for Open Media. All rights reserved
3  *
4  * This source code is subject to the terms of the BSD 2 Clause License and
5  * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6  * was not distributed with this source code in the LICENSE file, you can
7  * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8  * Media Patent License 1.0 was not distributed with this source code in the
9  * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10  */
11 
12 /*!\file
13  * \brief Describes the decoder algorithm interface for algorithm
14  *        implementations.
15  *
16  * This file defines the private structures and data types that are only
17  * relevant to implementing an algorithm, as opposed to using it.
18  *
19  * To create a decoder algorithm class, an interface structure is put
20  * into the global namespace:
21  *     <pre>
22  *     my_codec.c:
23  *       aom_codec_iface_t my_codec = {
24  *           "My Codec v1.0",
25  *           AOM_CODEC_ALG_ABI_VERSION,
26  *           ...
27  *       };
28  *     </pre>
29  *
30  * An application instantiates a specific decoder instance by using
31  * aom_codec_dec_init() and a pointer to the algorithm's interface structure:
32  *     <pre>
33  *     my_app.c:
34  *       extern aom_codec_iface_t my_codec;
35  *       {
36  *           aom_codec_ctx_t algo;
37  *           int threads = 4;
38  *           aom_codec_dec_cfg_t cfg = { threads, 0, 0, 1 };
39  *           res = aom_codec_dec_init(&algo, &my_codec, &cfg, 0);
40  *       }
41  *     </pre>
42  *
43  * Once initialized, the instance is managed using other functions from
44  * the aom_codec_* family.
45  */
46 #ifndef AOM_AOM_INTERNAL_AOM_CODEC_INTERNAL_H_
47 #define AOM_AOM_INTERNAL_AOM_CODEC_INTERNAL_H_
48 #include "../aom_decoder.h"
49 #include "../aom_encoder.h"
50 #include <stdarg.h>
51 
52 #ifdef __cplusplus
53 extern "C" {
54 #endif
55 
56 /*!\brief Current ABI version number
57  *
58  * \internal
59  * If this file is altered in any way that changes the ABI, this value
60  * must be bumped.  Examples include, but are not limited to, changing
61  * types, removing or reassigning enums, adding/removing/rearranging
62  * fields to structures
63  */
64 #define AOM_CODEC_INTERNAL_ABI_VERSION (7) /**<\hideinitializer*/
65 
66 typedef struct aom_codec_alg_priv aom_codec_alg_priv_t;
67 
68 /*!\brief init function pointer prototype
69  *
70  * Performs algorithm-specific initialization of the decoder context. This
71  * function is called by aom_codec_dec_init() and aom_codec_enc_init(), so
72  * plugins implementing this interface may trust the input parameters to be
73  * properly initialized.
74  *
75  * \param[in] ctx   Pointer to this instance's context
76  * \retval #AOM_CODEC_OK
77  *     The input stream was recognized and decoder initialized.
78  * \retval #AOM_CODEC_MEM_ERROR
79  *     Memory operation failed.
80  */
81 typedef aom_codec_err_t (*aom_codec_init_fn_t)(aom_codec_ctx_t *ctx);
82 
83 /*!\brief destroy function pointer prototype
84  *
85  * Performs algorithm-specific destruction of the decoder context. This
86  * function is called by the generic aom_codec_destroy() wrapper function,
87  * so plugins implementing this interface may trust the input parameters
88  * to be properly initialized.
89  *
90  * \param[in] ctx   Pointer to this instance's context
91  * \retval #AOM_CODEC_OK
92  *     The input stream was recognized and decoder initialized.
93  * \retval #AOM_CODEC_MEM_ERROR
94  *     Memory operation failed.
95  */
96 typedef aom_codec_err_t (*aom_codec_destroy_fn_t)(aom_codec_alg_priv_t *ctx);
97 
98 /*!\brief parse stream info function pointer prototype
99  *
100  * Performs high level parsing of the bitstream. This function is called by the
101  * generic aom_codec_peek_stream_info() wrapper function, so plugins
102  * implementing this interface may trust the input parameters to be properly
103  * initialized.
104  *
105  * \param[in]      data    Pointer to a block of data to parse
106  * \param[in]      data_sz Size of the data buffer
107  * \param[in,out]  si      Pointer to stream info to update. The is_annexb
108  *                         member \ref MUST be properly initialized. This
109  *                         function sets the rest of the members.
110  *
111  * \retval #AOM_CODEC_OK
112  *     Bitstream is parsable and stream information updated
113  */
114 typedef aom_codec_err_t (*aom_codec_peek_si_fn_t)(const uint8_t *data,
115                                                   size_t data_sz,
116                                                   aom_codec_stream_info_t *si);
117 
118 /*!\brief Return information about the current stream.
119  *
120  * Returns information about the stream that has been parsed during decoding.
121  *
122  * \param[in]      ctx     Pointer to this instance's context
123  * \param[in,out]  si      Pointer to stream info to update
124  *
125  * \retval #AOM_CODEC_OK
126  *     Bitstream is parsable and stream information updated
127  */
128 typedef aom_codec_err_t (*aom_codec_get_si_fn_t)(aom_codec_alg_priv_t *ctx,
129                                                  aom_codec_stream_info_t *si);
130 
131 /*!\brief control function pointer prototype
132  *
133  * This function is used to exchange algorithm specific data with the decoder
134  * instance. This can be used to implement features specific to a particular
135  * algorithm.
136  *
137  * This function is called by the generic aom_codec_control() wrapper
138  * function, so plugins implementing this interface may trust the input
139  * parameters to be properly initialized. However,  this interface does not
140  * provide type safety for the exchanged data or assign meanings to the
141  * control IDs. Those details should be specified in the algorithm's
142  * header file. In particular, the ctrl_id parameter is guaranteed to exist
143  * in the algorithm's control mapping table, and the data parameter may be NULL.
144  *
145  *
146  * \param[in]     ctx              Pointer to this instance's context
147  * \param[in]     ctrl_id          Algorithm specific control identifier
148  * \param[in,out] data             Data to exchange with algorithm instance.
149  *
150  * \retval #AOM_CODEC_OK
151  *     The internal state data was deserialized.
152  */
153 typedef aom_codec_err_t (*aom_codec_control_fn_t)(aom_codec_alg_priv_t *ctx,
154                                                   va_list ap);
155 
156 /*!\brief control function pointer mapping
157  *
158  * This structure stores the mapping between control identifiers and
159  * implementing functions. Each algorithm provides a list of these
160  * mappings. This list is searched by the aom_codec_control()
161  * function to determine which function to invoke. The special
162  * value defined by CTRL_MAP_END is used to indicate end-of-list, and must be
163  * present. It can be tested with the at_ctrl_map_end function. Note that
164  * ctrl_id values \ref MUST be non-zero.
165  */
166 typedef const struct aom_codec_ctrl_fn_map {
167   int ctrl_id;
168   aom_codec_control_fn_t fn;
169 } aom_codec_ctrl_fn_map_t;
170 
171 #define CTRL_MAP_END \
172   { 0, NULL }
173 
at_ctrl_map_end(aom_codec_ctrl_fn_map_t * e)174 static AOM_INLINE int at_ctrl_map_end(aom_codec_ctrl_fn_map_t *e) {
175   return e->ctrl_id == 0 && e->fn == NULL;
176 }
177 
178 /*!\brief decode data function pointer prototype
179  *
180  * Processes a buffer of coded data. This function is called by the generic
181  * aom_codec_decode() wrapper function, so plugins implementing this interface
182  * may trust the input parameters to be properly initialized.
183  *
184  * \param[in] ctx          Pointer to this instance's context
185  * \param[in] data         Pointer to this block of new coded data.
186  * \param[in] data_sz      Size of the coded data, in bytes.
187  *
188  * \return Returns #AOM_CODEC_OK if the coded data was processed completely
189  *         and future pictures can be decoded without error. Otherwise,
190  *         see the descriptions of the other error codes in ::aom_codec_err_t
191  *         for recoverability capabilities.
192  */
193 typedef aom_codec_err_t (*aom_codec_decode_fn_t)(aom_codec_alg_priv_t *ctx,
194                                                  const uint8_t *data,
195                                                  size_t data_sz,
196                                                  void *user_priv);
197 
198 /*!\brief Decoded frames iterator
199  *
200  * Iterates over a list of the frames available for display. The iterator
201  * storage should be initialized to NULL to start the iteration. Iteration is
202  * complete when this function returns NULL.
203  *
204  * The list of available frames becomes valid upon completion of the
205  * aom_codec_decode call, and remains valid until the next call to
206  * aom_codec_decode.
207  *
208  * \param[in]     ctx      Pointer to this instance's context
209  * \param[in out] iter     Iterator storage, initialized to NULL
210  *
211  * \return Returns a pointer to an image, if one is ready for display. Frames
212  *         produced will always be in PTS (presentation time stamp) order.
213  */
214 typedef aom_image_t *(*aom_codec_get_frame_fn_t)(aom_codec_alg_priv_t *ctx,
215                                                  aom_codec_iter_t *iter);
216 
217 /*!\brief Pass in external frame buffers for the decoder to use.
218  *
219  * Registers functions to be called when libaom needs a frame buffer
220  * to decode the current frame and a function to be called when libaom does
221  * not internally reference the frame buffer. This set function must
222  * be called before the first call to decode or libaom will assume the
223  * default behavior of allocating frame buffers internally.
224  *
225  * \param[in] ctx          Pointer to this instance's context
226  * \param[in] cb_get       Pointer to the get callback function
227  * \param[in] cb_release   Pointer to the release callback function
228  * \param[in] cb_priv      Callback's private data
229  *
230  * \retval #AOM_CODEC_OK
231  *     External frame buffers will be used by libaom.
232  * \retval #AOM_CODEC_INVALID_PARAM
233  *     One or more of the callbacks were NULL.
234  * \retval #AOM_CODEC_ERROR
235  *     Decoder context not initialized, or algorithm not capable of
236  *     using external frame buffers.
237  *
238  * \note
239  * When decoding AV1, the application may be required to pass in at least
240  * #AOM_MAXIMUM_WORK_BUFFERS external frame
241  * buffers.
242  */
243 typedef aom_codec_err_t (*aom_codec_set_fb_fn_t)(
244     aom_codec_alg_priv_t *ctx, aom_get_frame_buffer_cb_fn_t cb_get,
245     aom_release_frame_buffer_cb_fn_t cb_release, void *cb_priv);
246 
247 typedef aom_codec_err_t (*aom_codec_encode_fn_t)(aom_codec_alg_priv_t *ctx,
248                                                  const aom_image_t *img,
249                                                  aom_codec_pts_t pts,
250                                                  unsigned long duration,
251                                                  aom_enc_frame_flags_t flags);
252 typedef const aom_codec_cx_pkt_t *(*aom_codec_get_cx_data_fn_t)(
253     aom_codec_alg_priv_t *ctx, aom_codec_iter_t *iter);
254 
255 typedef aom_codec_err_t (*aom_codec_enc_config_set_fn_t)(
256     aom_codec_alg_priv_t *ctx, const aom_codec_enc_cfg_t *cfg);
257 typedef aom_fixed_buf_t *(*aom_codec_get_global_headers_fn_t)(
258     aom_codec_alg_priv_t *ctx);
259 
260 typedef aom_image_t *(*aom_codec_get_preview_frame_fn_t)(
261     aom_codec_alg_priv_t *ctx);
262 
263 /*!\brief Decoder algorithm interface interface
264  *
265  * All decoders \ref MUST expose a variable of this type.
266  */
267 struct aom_codec_iface {
268   const char *name;                   /**< Identification String  */
269   int abi_version;                    /**< Implemented ABI version */
270   aom_codec_caps_t caps;              /**< Decoder capabilities */
271   aom_codec_init_fn_t init;           /**< \copydoc ::aom_codec_init_fn_t */
272   aom_codec_destroy_fn_t destroy;     /**< \copydoc ::aom_codec_destroy_fn_t */
273   aom_codec_ctrl_fn_map_t *ctrl_maps; /**< \copydoc ::aom_codec_ctrl_fn_map_t */
274   struct aom_codec_dec_iface {
275     aom_codec_peek_si_fn_t peek_si; /**< \copydoc ::aom_codec_peek_si_fn_t */
276     aom_codec_get_si_fn_t get_si;   /**< \copydoc ::aom_codec_get_si_fn_t */
277     aom_codec_decode_fn_t decode;   /**< \copydoc ::aom_codec_decode_fn_t */
278     aom_codec_get_frame_fn_t
279         get_frame;                   /**< \copydoc ::aom_codec_get_frame_fn_t */
280     aom_codec_set_fb_fn_t set_fb_fn; /**< \copydoc ::aom_codec_set_fb_fn_t */
281   } dec;
282   struct aom_codec_enc_iface {
283     int cfg_count;
284     const aom_codec_enc_cfg_t *cfgs; /**< \copydoc ::aom_codec_enc_cfg_t */
285     aom_codec_encode_fn_t encode;    /**< \copydoc ::aom_codec_encode_fn_t */
286     aom_codec_get_cx_data_fn_t
287         get_cx_data; /**< \copydoc ::aom_codec_get_cx_data_fn_t */
288     aom_codec_enc_config_set_fn_t
289         cfg_set; /**< \copydoc ::aom_codec_enc_config_set_fn_t */
290     aom_codec_get_global_headers_fn_t
291         get_glob_hdrs; /**< \copydoc ::aom_codec_get_global_headers_fn_t */
292     aom_codec_get_preview_frame_fn_t
293         get_preview; /**< \copydoc ::aom_codec_get_preview_frame_fn_t */
294   } enc;
295 };
296 
297 /*!\brief Instance private storage
298  *
299  * This structure is allocated by the algorithm's init function. It can be
300  * extended in one of two ways. First, a second, algorithm specific structure
301  * can be allocated and the priv member pointed to it. Alternatively, this
302  * structure can be made the first member of the algorithm specific structure,
303  * and the pointer cast to the proper type.
304  */
305 struct aom_codec_priv {
306   const char *err_detail;
307   aom_codec_flags_t init_flags;
308   struct {
309     aom_fixed_buf_t cx_data_dst_buf;
310     unsigned int cx_data_pad_before;
311     unsigned int cx_data_pad_after;
312     aom_codec_cx_pkt_t cx_data_pkt;
313   } enc;
314 };
315 
316 #define CAST(id, arg) va_arg((arg), aom_codec_control_type_##id)
317 
318 /* Internal Utility Functions
319  *
320  * The following functions are intended to be used inside algorithms as
321  * utilities for manipulating aom_codec_* data structures.
322  */
323 struct aom_codec_pkt_list {
324   unsigned int cnt;
325   unsigned int max;
326   struct aom_codec_cx_pkt pkts[1];
327 };
328 
329 #define aom_codec_pkt_list_decl(n)     \
330   union {                              \
331     struct aom_codec_pkt_list head;    \
332     struct {                           \
333       struct aom_codec_pkt_list head;  \
334       struct aom_codec_cx_pkt pkts[n]; \
335     } alloc;                           \
336   }
337 
338 #define aom_codec_pkt_list_init(m) \
339   (m)->alloc.head.cnt = 0,         \
340   (m)->alloc.head.max = sizeof((m)->alloc.pkts) / sizeof((m)->alloc.pkts[0])
341 
342 int aom_codec_pkt_list_add(struct aom_codec_pkt_list *,
343                            const struct aom_codec_cx_pkt *);
344 
345 const aom_codec_cx_pkt_t *aom_codec_pkt_list_get(
346     struct aom_codec_pkt_list *list, aom_codec_iter_t *iter);
347 
348 #include <stdio.h>
349 #include <setjmp.h>
350 
351 struct aom_internal_error_info {
352   aom_codec_err_t error_code;
353   int has_detail;
354   char detail[80];
355   int setjmp;  // Boolean: whether 'jmp' is valid.
356   jmp_buf jmp;
357 };
358 
359 #define CLANG_ANALYZER_NORETURN
360 #if defined(__has_feature)
361 #if __has_feature(attribute_analyzer_noreturn)
362 #undef CLANG_ANALYZER_NORETURN
363 #define CLANG_ANALYZER_NORETURN __attribute__((analyzer_noreturn))
364 #endif
365 #endif
366 
367 void aom_internal_error(struct aom_internal_error_info *info,
368                         aom_codec_err_t error, const char *fmt,
369                         ...) CLANG_ANALYZER_NORETURN;
370 
371 void aom_merge_corrupted_flag(int *corrupted, int value);
372 #ifdef __cplusplus
373 }  // extern "C"
374 #endif
375 
376 #endif  // AOM_AOM_INTERNAL_AOM_CODEC_INTERNAL_H_
377