1 /*
2 * Copyright (c) 2019, 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 #ifndef AOM_AV1_ENCODER_LEVEL_H_
13 #define AOM_AV1_ENCODER_LEVEL_H_
14
15 #include "av1/common/enums.h"
16
17 struct AV1_COMP;
18
19 // AV1 Level Specifications
20 typedef struct {
21 AV1_LEVEL level;
22 int max_picture_size;
23 int max_h_size;
24 int max_v_size;
25 int max_header_rate;
26 int max_tile_rate;
27 int max_tiles;
28 int max_tile_cols;
29 int64_t max_display_rate;
30 int64_t max_decode_rate;
31 double main_mbps;
32 double high_mbps;
33 double main_cr;
34 double high_cr;
35 } AV1LevelSpec;
36
37 typedef struct {
38 int64_t ts_start;
39 int64_t ts_end;
40 size_t encoded_size_in_bytes;
41 int pic_size;
42 int frame_header_count;
43 int tiles;
44 int show_frame;
45 int show_existing_frame;
46 } FrameRecord;
47
48 // Record frame info. in a rolling window.
49 #define FRAME_WINDOW_SIZE 256
50 typedef struct {
51 FrameRecord buf[FRAME_WINDOW_SIZE];
52 int num; // Number of FrameRecord stored in the buffer.
53 int start; // Buffer index of the first FrameRecord.
54 } FrameWindowBuffer;
55
56 typedef struct {
57 int max_bitrate; // Max bitrate in any 1-second window, in bps.
58 int max_tile_size;
59 int max_superres_tile_width;
60 int min_cropped_tile_width;
61 int min_cropped_tile_height;
62 int tile_width_is_valid;
63 int min_frame_width;
64 int min_frame_height;
65 double total_compressed_size; // In bytes.
66 double total_time_encoded; // In seconds.
67 double min_cr;
68 } AV1LevelStats;
69
70 // The following data structures are for the decoder model.
71 typedef struct {
72 int decoder_ref_count;
73 int player_ref_count;
74 int display_index;
75 FRAME_TYPE frame_type;
76 double presentation_time;
77 } FRAME_BUFFER;
78
79 // Interval of bits transmission for a DFG(Decodable Frame Group).
80 typedef struct {
81 double first_bit_arrival_time; // Time when the first bit arrives.
82 double last_bit_arrival_time; // Time when the last bit arrives.
83 // Removal time means the time when the bits to be decoded are removed from
84 // the smoothing buffer. Removal time is essentially the time when the
85 // decoding of the frame starts.
86 double removal_time;
87 } DFG_INTERVAL;
88
89 #define DFG_INTERVAL_QUEUE_SIZE 64
90 typedef struct {
91 int head;
92 int size;
93 double total_interval;
94 DFG_INTERVAL buf[DFG_INTERVAL_QUEUE_SIZE];
95 } DFG_INTERVAL_QUEUE;
96
97 enum {
98 RESOURCE_MODE = 0, // Resource availability mode.
99 SCHEDULE_MODE // Decoding schedule mode.
100 } UENUM1BYTE(DECODER_MODEL_MODE);
101
102 enum {
103 DECODER_MODEL_OK = 0,
104 DECODE_BUFFER_AVAILABLE_LATE,
105 DECODE_FRAME_BUF_UNAVAILABLE,
106 DECODE_EXISTING_FRAME_BUF_EMPTY,
107 DISPLAY_FRAME_LATE,
108 SMOOTHING_BUFFER_UNDERFLOW,
109 SMOOTHING_BUFFER_OVERFLOW,
110 DECODER_MODEL_DISABLED
111 } UENUM1BYTE(DECODER_MODEL_STATUS);
112
113 #define BUFFER_POOL_MAX_SIZE 10
114 typedef struct {
115 DECODER_MODEL_STATUS status;
116 DECODER_MODEL_MODE mode;
117 bool is_low_delay_mode;
118 AV1_LEVEL level;
119 int encoder_buffer_delay; // In units of 1/90000 seconds.
120 int decoder_buffer_delay; // In units of 1/90000 seconds.
121 int num_ticks_per_picture;
122 int initial_display_delay; // In units of frames.
123 int64_t decode_rate;
124 double display_clock_tick; // In units of seconds.
125 double current_time; // In units of seconds.
126 double initial_presentation_delay; // In units of seconds.
127 double bit_rate; // Bits per second.
128
129 int num_frame;
130 int num_decoded_frame;
131 int num_shown_frame;
132 int vbi[REF_FRAMES]; // Virtual buffer index.
133 FRAME_BUFFER frame_buffer_pool[BUFFER_POOL_MAX_SIZE];
134 DFG_INTERVAL_QUEUE dfg_interval_queue;
135
136 // Information for the DFG(Decodable Frame Group) being processed.
137 double first_bit_arrival_time;
138 double last_bit_arrival_time;
139 size_t coded_bits;
140
141 // Information for the frame being processed.
142 double removal_time;
143 double presentation_time;
144 int decode_samples;
145 int display_samples;
146
147 double max_display_rate;
148 double max_decode_rate;
149 } DECODER_MODEL;
150
151 typedef struct {
152 AV1LevelStats level_stats;
153 AV1LevelSpec level_spec;
154 FrameWindowBuffer frame_window_buffer;
155 DECODER_MODEL decoder_models[SEQ_LEVELS];
156 } AV1LevelInfo;
157
158 typedef struct AV1LevelParams {
159 // Specifies the level that the coded video sequence conforms to for each
160 // operating point.
161 AV1_LEVEL target_seq_level_idx[MAX_NUM_OPERATING_POINTS];
162 // Bit mask to indicate whether to keep level stats for corresponding
163 // operating points.
164 uint32_t keep_level_stats;
165 // Level information for each operating point.
166 AV1LevelInfo *level_info[MAX_NUM_OPERATING_POINTS];
167 // Count the number of OBU_FRAME and OBU_FRAME_HEADER for level calculation.
168 int frame_header_count;
169 } AV1LevelParams;
170
is_in_operating_point(int operating_point,int temporal_layer_id,int spatial_layer_id)171 static INLINE int is_in_operating_point(int operating_point,
172 int temporal_layer_id,
173 int spatial_layer_id) {
174 if (!operating_point) return 1;
175
176 return ((operating_point >> temporal_layer_id) & 1) &&
177 ((operating_point >> (spatial_layer_id + 8)) & 1);
178 }
179
180 void av1_init_level_info(struct AV1_COMP *cpi);
181
182 void av1_update_level_info(struct AV1_COMP *cpi, size_t size, int64_t ts_start,
183 int64_t ts_end);
184
185 // Return sequence level indices in seq_level_idx[MAX_NUM_OPERATING_POINTS].
186 aom_codec_err_t av1_get_seq_level_idx(const SequenceHeader *seq_params,
187 const AV1LevelParams *level_params,
188 int *seq_level_idx);
189
190 // Print the status of the decoder model(for debugging).
191 void av1_decoder_model_print_status(const DECODER_MODEL *const decoder_model);
192
193 void av1_decoder_model_init(const struct AV1_COMP *const cpi, AV1_LEVEL level,
194 int op_index, DECODER_MODEL *const decoder_model);
195
196 void av1_decoder_model_process_frame(const struct AV1_COMP *const cpi,
197 size_t coded_bits,
198 DECODER_MODEL *const decoder_model);
199
200 // Return max bitrate(bps) for given level.
201 double av1_get_max_bitrate_for_level(AV1_LEVEL level_index, int tier,
202 BITSTREAM_PROFILE profile);
203
204 // Get max number of tiles and tile columns for given level.
205 void av1_get_max_tiles_for_level(AV1_LEVEL level_index, int *const max_tiles,
206 int *const max_tile_cols);
207
208 // Return minimum compression ratio for given level.
209 double av1_get_min_cr_for_level(AV1_LEVEL level_index, int tier,
210 int is_still_picture);
211 #endif // AOM_AV1_ENCODER_LEVEL_H_
212