1 /*
2 * Copyright(c) 2019 Netflix, Inc.
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 https://www.aomedia.org/license/software-license. 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 https://www.aomedia.org/license/patent-license.
10 */
11 
12 /******************************************************************************
13  * @file RefDecoder.h
14  *
15  * @brief Defines a reference decoder wrapped AOM decoder
16  *
17  * @author Cidana-Edmond
18  *
19  ******************************************************************************/
20 #ifndef _REF_DECODER_H_
21 #define _REF_DECODER_H_
22 
23 #include <memory.h>
24 #include <stdio.h>
25 #include <string>
26 #include <vector>
27 #include "VideoFrame.h"
28 
29 /** RefDecoder is a class designed for a refenece tool of conformance
30  * test. It provides decoding AV1 compressed data with OBU frames, its output is
31  * the YUV frame in display order. User should call get_frame right after
32  * decode to avoid missing any video frame
33  */
34 class RefDecoder {
35   public:
36     /** RefDecoderErr is enumerate type of errors from decoder, refered to
37      * errors in AOM */
38     typedef enum {
39         /*!\brief Operation completed without error */
40         REF_CODEC_OK,
41 
42         /*!\brief Unspecified error */
43         REF_CODEC_ERROR = 0 - AOM_CODEC_ERROR,
44 
45         /*!\brief Memory operation failed */
46         REF_CODEC_MEM_ERROR = 0 - AOM_CODEC_MEM_ERROR,
47 
48         /*!\brief ABI version mismatch */
49         REF_CODEC_ABI_MISMATCH = 0 - AOM_CODEC_ABI_MISMATCH,
50 
51         /*!\brief Algorithm does not have required capability */
52         REF_CODEC_INCAPABLE = 0 - AOM_CODEC_INCAPABLE,
53 
54         /*!\brief The given Bitstream is not supported.
55          *
56          * The Bitstream was unable to be parsed at the highest level. The
57          * decoder is unable to proceed. This error \ref SHOULD be treated as
58          * fatal to the stream. */
59         REF_CODEC_UNSUP_BITSTREAM = 0 - AOM_CODEC_UNSUP_BITSTREAM,
60 
61         /*!\brief Encoded Bitstream uses an unsupported feature
62          *
63          * The decoder does not implement a feature required by the encoder.
64          * This return code should only be used for features that prevent future
65          * pictures from being properly decoded. This error \ref MAY be treated
66          * as fatal to the stream or \ref MAY be treated as fatal to the current
67          * GOP.
68          */
69         REF_CODEC_UNSUP_FEATURE = 0 - AOM_CODEC_UNSUP_FEATURE,
70 
71         /*!\brief The coded data for this stream is corrupt or incomplete
72          *
73          * There was a problem decoding the current frame.  This return code
74          * should only be used for failures that prevent future pictures from
75          * being properly decoded. This error \ref MAY be treated as fatal to
76          * the stream or \ref MAY be treated as fatal to the current GOP. If
77          * decoding is continued for the current GOP, artifacts may be present.
78          */
79         REF_CODEC_CORRUPT_FRAME = 0 - AOM_CODEC_CORRUPT_FRAME,
80 
81         /*!\brief An application-supplied parameter is not valid.
82          *
83          */
84         REF_CODEC_INVALID_PARAM = 0 - AOM_CODEC_INVALID_PARAM,
85 
86         /*!\brief An iterator reached the end of list.
87          *
88          */
89         REF_CODEC_LIST_END = 0 - AOM_CODEC_LIST_END,
90 
91         /*!\brief Decoder need more input data to generate frame
92          *
93          */
94         REF_CODEC_NEED_MORE_INPUT = -100,
95 
96     } RefDecoderErr;
97 
98     typedef struct StreamInfo {
99         std::vector<int> frame_type_list;
100         uint32_t profile;
101         int monochrome;  // Monochorme video
102         VideoColorFormat format;
103         uint32_t bit_depth;
104         uint32_t sb_size;
105         bool still_pic;
106         /* coding options */
107         int force_integer_mv;
108         int enable_filter_intra;
109         int enable_intra_edge_filter;
110         int enable_masked_compound;
111         int enable_dual_filter;
112         int enable_jnt_comp;
113         int enable_ref_frame_mvs;
114         int enable_warped_motion;
115         int cdef_level;
116         int enable_restoration;
117         int film_grain_params_present;
118         int enable_superres;
119 
120         uint32_t tile_rows;
121         uint32_t tile_cols;
122         uint32_t min_block_size;
123         uint32_t
124             ext_block_flag; /**< if contains extended block, 0--no, 1--yes */
125         std::vector<uint32_t> qindex_list;
126         int16_t max_qindex;
127         int16_t min_qindex;
128         int32_t max_intra_period;
129         uint32_t frame_bit_rate;
StreamInfoStreamInfo130         StreamInfo() {
131             format = IMG_FMT_420;
132             tile_rows = 0;
133             tile_cols = 0;
134             min_block_size = 128;
135             ext_block_flag = 0;
136             max_qindex = 0;
137             min_qindex = 255;
138             frame_bit_rate = 0;
139             frame_type_list.clear();
140             qindex_list.clear();
141         }
142     } StreamInfo;
143 
144   public:
145     /** Constructor of RefDecoder
146      * @param ret the error code found in construction
147      * @param enable_analyzer the flag to create analyzer with decoder
148      */
149     RefDecoder(RefDecoderErr &ret, bool enable_analyzer);
150     /** Destructor of RefDecoder      */
151     virtual ~RefDecoder();
152 
153   public:
154     /** Decode raw data
155      * @param data  the memory buffer of a frame of compressed data
156      * @param size  the size of data
157      * @return
158      * REF_CODEC_OK -- no error found in processing
159      * others -- errors found in process, refer to RefDecoderErr
160      */
161     RefDecoderErr decode(const uint8_t *data, const uint32_t size);
162 
163     /** Get a video frame after data proceed
164      * @param frame  the video frame with its attributes
165      * @return
166      * REF_CODEC_OK -- no error found in processing
167      * others -- errors found in process, refer to RefDecoderErr
168      */
169     RefDecoderErr get_frame(VideoFrame &frame);
170 
171     /** Get a video frame after data proceed
172      * @param frame  the video frame with its attributes
173      * @return
174      * REF_CODEC_OK -- no error found in processing
175      * others -- errors found in process, refer to RefDecoderErr
176      */
get_stream_info()177     StreamInfo *get_stream_info() {
178         return &stream_info_;
179     }
180 
181     /** Setup resolution, for initialization for inspection frame before first
182      * frame
183      * @param width  width of source video frame
184      * @param height  height of source video frame
185      */
186     void set_resolution(const uint32_t width, const uint32_t height);
187 
188     void set_invert_tile_decoding_order();
189     void control(int ctrl_id, int arg);
190 
191   private:
192     /** Tool of translation from AOM image info to a video frame
193      * @param image  the video image from AOM decoder
194      * @param frame  the video frame to output
195      */
196     void trans_video_frame(const void *image, VideoFrame &frame);
197 
198     /** Callback fuction of inspection frame output */
199     static void inspect_cb(void *pbi, void *data);
200     /** Tool of pasring inspection frame for its paramters */
201     void parse_frame_info();
202 
203   protected:
204     void *codec_handle_;      /**< AOM codec context */
205     uint32_t dec_frame_cnt_;  /**< count of decoded frame in processing */
206     uint64_t init_timestamp_; /**< initial timestamp of stream */
207     uint32_t frame_interval_; /**< time interval of two frame in miliseconds */
208     void *insp_frame_data_;   /**< inspect frame data structure */
209     VideoFrameParam video_param_; /**< parameter of decoded video frame */
210     void *parser_;           /**< sequence parser for parameter verification */
211     StreamInfo stream_info_; /** stream info of the encoded stream  */
212     uint32_t enc_bytes_;     /**< total bytes of input, for bit-rate counting */
213     uint32_t burst_bytes_; /**< the largest size of input for burst bit-rate */
214 };
215 
216 /** Interface of reference decoder creation
217  * @param enable_parser  the flag of using inspection frame for parameter check
218  * @return
219  * RefDecoder -- decoder handle created
220  * nullptr -- creation failed
221  */
222 RefDecoder *create_reference_decoder(bool enable_analyzer = false);
223 
224 #endif  // !_REF_DECODER_H_
225