/src/aom/av1/encoder/level.c
Line | Count | Source (jump to first uncovered line) |
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 | | #include "av1/encoder/encoder.h" |
13 | | #include "av1/encoder/level.h" |
14 | | |
15 | | #define UNDEFINED_LEVEL \ |
16 | | { \ |
17 | | .level = SEQ_LEVEL_MAX, .max_picture_size = 0, .max_h_size = 0, \ |
18 | | .max_v_size = 0, .max_display_rate = 0, .max_decode_rate = 0, \ |
19 | | .max_header_rate = 0, .main_mbps = 0, .high_mbps = 0, .main_cr = 0, \ |
20 | | .high_cr = 0, .max_tiles = 0, .max_tile_cols = 0 \ |
21 | | } |
22 | | |
23 | | static const AV1LevelSpec av1_level_defs[SEQ_LEVELS] = { |
24 | | { .level = SEQ_LEVEL_2_0, |
25 | | .max_picture_size = 147456, |
26 | | .max_h_size = 2048, |
27 | | .max_v_size = 1152, |
28 | | .max_display_rate = 4423680L, |
29 | | .max_decode_rate = 5529600L, |
30 | | .max_header_rate = 150, |
31 | | .main_mbps = 1.5, |
32 | | .high_mbps = 0, |
33 | | .main_cr = 2.0, |
34 | | .high_cr = 0, |
35 | | .max_tiles = 8, |
36 | | .max_tile_cols = 4 }, |
37 | | { .level = SEQ_LEVEL_2_1, |
38 | | .max_picture_size = 278784, |
39 | | .max_h_size = 2816, |
40 | | .max_v_size = 1584, |
41 | | .max_display_rate = 8363520L, |
42 | | .max_decode_rate = 10454400L, |
43 | | .max_header_rate = 150, |
44 | | .main_mbps = 3.0, |
45 | | .high_mbps = 0, |
46 | | .main_cr = 2.0, |
47 | | .high_cr = 0, |
48 | | .max_tiles = 8, |
49 | | .max_tile_cols = 4 }, |
50 | | UNDEFINED_LEVEL, |
51 | | UNDEFINED_LEVEL, |
52 | | { .level = SEQ_LEVEL_3_0, |
53 | | .max_picture_size = 665856, |
54 | | .max_h_size = 4352, |
55 | | .max_v_size = 2448, |
56 | | .max_display_rate = 19975680L, |
57 | | .max_decode_rate = 24969600L, |
58 | | .max_header_rate = 150, |
59 | | .main_mbps = 6.0, |
60 | | .high_mbps = 0, |
61 | | .main_cr = 2.0, |
62 | | .high_cr = 0, |
63 | | .max_tiles = 16, |
64 | | .max_tile_cols = 6 }, |
65 | | { .level = SEQ_LEVEL_3_1, |
66 | | .max_picture_size = 1065024, |
67 | | .max_h_size = 5504, |
68 | | .max_v_size = 3096, |
69 | | .max_display_rate = 31950720L, |
70 | | .max_decode_rate = 39938400L, |
71 | | .max_header_rate = 150, |
72 | | .main_mbps = 10.0, |
73 | | .high_mbps = 0, |
74 | | .main_cr = 2.0, |
75 | | .high_cr = 0, |
76 | | .max_tiles = 16, |
77 | | .max_tile_cols = 6 }, |
78 | | UNDEFINED_LEVEL, |
79 | | UNDEFINED_LEVEL, |
80 | | { .level = SEQ_LEVEL_4_0, |
81 | | .max_picture_size = 2359296, |
82 | | .max_h_size = 6144, |
83 | | .max_v_size = 3456, |
84 | | .max_display_rate = 70778880L, |
85 | | .max_decode_rate = 77856768L, |
86 | | .max_header_rate = 300, |
87 | | .main_mbps = 12.0, |
88 | | .high_mbps = 30.0, |
89 | | .main_cr = 4.0, |
90 | | .high_cr = 4.0, |
91 | | .max_tiles = 32, |
92 | | .max_tile_cols = 8 }, |
93 | | { .level = SEQ_LEVEL_4_1, |
94 | | .max_picture_size = 2359296, |
95 | | .max_h_size = 6144, |
96 | | .max_v_size = 3456, |
97 | | .max_display_rate = 141557760L, |
98 | | .max_decode_rate = 155713536L, |
99 | | .max_header_rate = 300, |
100 | | .main_mbps = 20.0, |
101 | | .high_mbps = 50.0, |
102 | | .main_cr = 4.0, |
103 | | .high_cr = 4.0, |
104 | | .max_tiles = 32, |
105 | | .max_tile_cols = 8 }, |
106 | | UNDEFINED_LEVEL, |
107 | | UNDEFINED_LEVEL, |
108 | | { .level = SEQ_LEVEL_5_0, |
109 | | .max_picture_size = 8912896, |
110 | | .max_h_size = 8192, |
111 | | .max_v_size = 4352, |
112 | | .max_display_rate = 267386880L, |
113 | | .max_decode_rate = 273715200L, |
114 | | .max_header_rate = 300, |
115 | | .main_mbps = 30.0, |
116 | | .high_mbps = 100.0, |
117 | | .main_cr = 6.0, |
118 | | .high_cr = 4.0, |
119 | | .max_tiles = 64, |
120 | | .max_tile_cols = 8 }, |
121 | | { .level = SEQ_LEVEL_5_1, |
122 | | .max_picture_size = 8912896, |
123 | | .max_h_size = 8192, |
124 | | .max_v_size = 4352, |
125 | | .max_display_rate = 534773760L, |
126 | | .max_decode_rate = 547430400L, |
127 | | .max_header_rate = 300, |
128 | | .main_mbps = 40.0, |
129 | | .high_mbps = 160.0, |
130 | | .main_cr = 8.0, |
131 | | .high_cr = 4.0, |
132 | | .max_tiles = 64, |
133 | | .max_tile_cols = 8 }, |
134 | | { .level = SEQ_LEVEL_5_2, |
135 | | .max_picture_size = 8912896, |
136 | | .max_h_size = 8192, |
137 | | .max_v_size = 4352, |
138 | | .max_display_rate = 1069547520L, |
139 | | .max_decode_rate = 1094860800L, |
140 | | .max_header_rate = 300, |
141 | | .main_mbps = 60.0, |
142 | | .high_mbps = 240.0, |
143 | | .main_cr = 8.0, |
144 | | .high_cr = 4.0, |
145 | | .max_tiles = 64, |
146 | | .max_tile_cols = 8 }, |
147 | | { .level = SEQ_LEVEL_5_3, |
148 | | .max_picture_size = 8912896, |
149 | | .max_h_size = 8192, |
150 | | .max_v_size = 4352, |
151 | | .max_display_rate = 1069547520L, |
152 | | .max_decode_rate = 1176502272L, |
153 | | .max_header_rate = 300, |
154 | | .main_mbps = 60.0, |
155 | | .high_mbps = 240.0, |
156 | | .main_cr = 8.0, |
157 | | .high_cr = 4.0, |
158 | | .max_tiles = 64, |
159 | | .max_tile_cols = 8 }, |
160 | | { .level = SEQ_LEVEL_6_0, |
161 | | .max_picture_size = 35651584, |
162 | | .max_h_size = 16384, |
163 | | .max_v_size = 8704, |
164 | | .max_display_rate = 1069547520L, |
165 | | .max_decode_rate = 1176502272L, |
166 | | .max_header_rate = 300, |
167 | | .main_mbps = 60.0, |
168 | | .high_mbps = 240.0, |
169 | | .main_cr = 8.0, |
170 | | .high_cr = 4.0, |
171 | | .max_tiles = 128, |
172 | | .max_tile_cols = 16 }, |
173 | | { .level = SEQ_LEVEL_6_1, |
174 | | .max_picture_size = 35651584, |
175 | | .max_h_size = 16384, |
176 | | .max_v_size = 8704, |
177 | | .max_display_rate = 2139095040L, |
178 | | .max_decode_rate = 2189721600L, |
179 | | .max_header_rate = 300, |
180 | | .main_mbps = 100.0, |
181 | | .high_mbps = 480.0, |
182 | | .main_cr = 8.0, |
183 | | .high_cr = 4.0, |
184 | | .max_tiles = 128, |
185 | | .max_tile_cols = 16 }, |
186 | | { .level = SEQ_LEVEL_6_2, |
187 | | .max_picture_size = 35651584, |
188 | | .max_h_size = 16384, |
189 | | .max_v_size = 8704, |
190 | | .max_display_rate = 4278190080L, |
191 | | .max_decode_rate = 4379443200L, |
192 | | .max_header_rate = 300, |
193 | | .main_mbps = 160.0, |
194 | | .high_mbps = 800.0, |
195 | | .main_cr = 8.0, |
196 | | .high_cr = 4.0, |
197 | | .max_tiles = 128, |
198 | | .max_tile_cols = 16 }, |
199 | | { .level = SEQ_LEVEL_6_3, |
200 | | .max_picture_size = 35651584, |
201 | | .max_h_size = 16384, |
202 | | .max_v_size = 8704, |
203 | | .max_display_rate = 4278190080L, |
204 | | .max_decode_rate = 4706009088L, |
205 | | .max_header_rate = 300, |
206 | | .main_mbps = 160.0, |
207 | | .high_mbps = 800.0, |
208 | | .main_cr = 8.0, |
209 | | .high_cr = 4.0, |
210 | | .max_tiles = 128, |
211 | | .max_tile_cols = 16 }, |
212 | | UNDEFINED_LEVEL, |
213 | | UNDEFINED_LEVEL, |
214 | | UNDEFINED_LEVEL, |
215 | | UNDEFINED_LEVEL, |
216 | | }; |
217 | | |
218 | | typedef enum { |
219 | | LUMA_PIC_SIZE_TOO_LARGE, |
220 | | LUMA_PIC_H_SIZE_TOO_LARGE, |
221 | | LUMA_PIC_V_SIZE_TOO_LARGE, |
222 | | LUMA_PIC_H_SIZE_TOO_SMALL, |
223 | | LUMA_PIC_V_SIZE_TOO_SMALL, |
224 | | TOO_MANY_TILE_COLUMNS, |
225 | | TOO_MANY_TILES, |
226 | | TILE_RATE_TOO_HIGH, |
227 | | TILE_TOO_LARGE, |
228 | | SUPERRES_TILE_WIDTH_TOO_LARGE, |
229 | | CROPPED_TILE_WIDTH_TOO_SMALL, |
230 | | CROPPED_TILE_HEIGHT_TOO_SMALL, |
231 | | TILE_WIDTH_INVALID, |
232 | | FRAME_HEADER_RATE_TOO_HIGH, |
233 | | DISPLAY_RATE_TOO_HIGH, |
234 | | DECODE_RATE_TOO_HIGH, |
235 | | CR_TOO_SMALL, |
236 | | TILE_SIZE_HEADER_RATE_TOO_HIGH, |
237 | | BITRATE_TOO_HIGH, |
238 | | DECODER_MODEL_FAIL, |
239 | | |
240 | | TARGET_LEVEL_FAIL_IDS, |
241 | | TARGET_LEVEL_OK, |
242 | | } TARGET_LEVEL_FAIL_ID; |
243 | | |
244 | | static const char *level_fail_messages[TARGET_LEVEL_FAIL_IDS] = { |
245 | | "The picture size is too large.", |
246 | | "The picture width is too large.", |
247 | | "The picture height is too large.", |
248 | | "The picture width is too small.", |
249 | | "The picture height is too small.", |
250 | | "Too many tile columns are used.", |
251 | | "Too many tiles are used.", |
252 | | "The tile rate is too high.", |
253 | | "The tile size is too large.", |
254 | | "The superres tile width is too large.", |
255 | | "The cropped tile width is less than 8.", |
256 | | "The cropped tile height is less than 8.", |
257 | | "The tile width is invalid.", |
258 | | "The frame header rate is too high.", |
259 | | "The display luma sample rate is too high.", |
260 | | "The decoded luma sample rate is too high.", |
261 | | "The compression ratio is too small.", |
262 | | "The product of max tile size and header rate is too high.", |
263 | | "The bitrate is too high.", |
264 | | "The decoder model fails.", |
265 | | }; |
266 | | |
267 | | static double get_max_bitrate(const AV1LevelSpec *const level_spec, int tier, |
268 | 0 | BITSTREAM_PROFILE profile) { |
269 | 0 | if (level_spec->level < SEQ_LEVEL_4_0) tier = 0; |
270 | 0 | const double bitrate_basis = |
271 | 0 | (tier ? level_spec->high_mbps : level_spec->main_mbps) * 1e6; |
272 | 0 | const double bitrate_profile_factor = |
273 | 0 | profile == PROFILE_0 ? 1.0 : (profile == PROFILE_1 ? 2.0 : 3.0); |
274 | 0 | return bitrate_basis * bitrate_profile_factor; |
275 | 0 | } |
276 | | |
277 | | double av1_get_max_bitrate_for_level(AV1_LEVEL level_index, int tier, |
278 | 0 | BITSTREAM_PROFILE profile) { |
279 | 0 | assert(is_valid_seq_level_idx(level_index)); |
280 | 0 | return get_max_bitrate(&av1_level_defs[level_index], tier, profile); |
281 | 0 | } |
282 | | |
283 | | void av1_get_max_tiles_for_level(AV1_LEVEL level_index, int *const max_tiles, |
284 | 0 | int *const max_tile_cols) { |
285 | 0 | assert(is_valid_seq_level_idx(level_index)); |
286 | 0 | const AV1LevelSpec *const level_spec = &av1_level_defs[level_index]; |
287 | 0 | *max_tiles = level_spec->max_tiles; |
288 | 0 | *max_tile_cols = level_spec->max_tile_cols; |
289 | 0 | } |
290 | | |
291 | | // We assume time t to be valid if and only if t >= 0.0. |
292 | | // So INVALID_TIME can be defined as anything less than 0. |
293 | 0 | #define INVALID_TIME (-1.0) |
294 | | |
295 | | // This corresponds to "free_buffer" in the spec. |
296 | 0 | static void release_buffer(DECODER_MODEL *const decoder_model, int idx) { |
297 | 0 | assert(idx >= 0 && idx < BUFFER_POOL_MAX_SIZE); |
298 | 0 | FRAME_BUFFER *const this_buffer = &decoder_model->frame_buffer_pool[idx]; |
299 | 0 | this_buffer->decoder_ref_count = 0; |
300 | 0 | this_buffer->player_ref_count = 0; |
301 | 0 | this_buffer->display_index = -1; |
302 | 0 | this_buffer->presentation_time = INVALID_TIME; |
303 | 0 | } |
304 | | |
305 | 0 | static void initialize_buffer_pool(DECODER_MODEL *const decoder_model) { |
306 | 0 | for (int i = 0; i < BUFFER_POOL_MAX_SIZE; ++i) { |
307 | 0 | release_buffer(decoder_model, i); |
308 | 0 | } |
309 | 0 | for (int i = 0; i < REF_FRAMES; ++i) { |
310 | 0 | decoder_model->vbi[i] = -1; |
311 | 0 | } |
312 | 0 | } |
313 | | |
314 | 0 | static int get_free_buffer(DECODER_MODEL *const decoder_model) { |
315 | 0 | for (int i = 0; i < BUFFER_POOL_MAX_SIZE; ++i) { |
316 | 0 | const FRAME_BUFFER *const this_buffer = |
317 | 0 | &decoder_model->frame_buffer_pool[i]; |
318 | 0 | if (this_buffer->decoder_ref_count == 0 && |
319 | 0 | this_buffer->player_ref_count == 0) |
320 | 0 | return i; |
321 | 0 | } |
322 | 0 | return -1; |
323 | 0 | } |
324 | | |
325 | | static void update_ref_buffers(DECODER_MODEL *const decoder_model, int idx, |
326 | 0 | int refresh_frame_flags) { |
327 | 0 | FRAME_BUFFER *const this_buffer = &decoder_model->frame_buffer_pool[idx]; |
328 | 0 | for (int i = 0; i < REF_FRAMES; ++i) { |
329 | 0 | if (refresh_frame_flags & (1 << i)) { |
330 | 0 | const int pre_idx = decoder_model->vbi[i]; |
331 | 0 | if (pre_idx != -1) { |
332 | 0 | --decoder_model->frame_buffer_pool[pre_idx].decoder_ref_count; |
333 | 0 | } |
334 | 0 | decoder_model->vbi[i] = idx; |
335 | 0 | ++this_buffer->decoder_ref_count; |
336 | 0 | } |
337 | 0 | } |
338 | 0 | } |
339 | | |
340 | | // The time (in seconds) required to decode a frame. |
341 | | static double time_to_decode_frame(const AV1_COMMON *const cm, |
342 | 0 | int64_t max_decode_rate) { |
343 | 0 | if (cm->show_existing_frame) return 0.0; |
344 | | |
345 | 0 | const FRAME_TYPE frame_type = cm->current_frame.frame_type; |
346 | 0 | int luma_samples = 0; |
347 | 0 | if (frame_type == KEY_FRAME || frame_type == INTRA_ONLY_FRAME) { |
348 | 0 | luma_samples = cm->superres_upscaled_width * cm->height; |
349 | 0 | } else { |
350 | 0 | const int spatial_layer_dimensions_present_flag = 0; |
351 | 0 | if (spatial_layer_dimensions_present_flag) { |
352 | 0 | assert(0 && "Spatial layer dimensions not supported yet."); |
353 | 0 | } else { |
354 | 0 | const SequenceHeader *const seq_params = cm->seq_params; |
355 | 0 | const int max_frame_width = seq_params->max_frame_width; |
356 | 0 | const int max_frame_height = seq_params->max_frame_height; |
357 | 0 | luma_samples = max_frame_width * max_frame_height; |
358 | 0 | } |
359 | 0 | } |
360 | |
|
361 | 0 | return luma_samples / (double)max_decode_rate; |
362 | 0 | } |
363 | | |
364 | | // Release frame buffers that are no longer needed for decode or display. |
365 | | // It corresponds to "start_decode_at_removal_time" in the spec. |
366 | | static void release_processed_frames(DECODER_MODEL *const decoder_model, |
367 | 0 | double removal_time) { |
368 | 0 | for (int i = 0; i < BUFFER_POOL_MAX_SIZE; ++i) { |
369 | 0 | FRAME_BUFFER *const this_buffer = &decoder_model->frame_buffer_pool[i]; |
370 | 0 | if (this_buffer->player_ref_count > 0) { |
371 | 0 | if (this_buffer->presentation_time >= 0.0 && |
372 | 0 | this_buffer->presentation_time <= removal_time) { |
373 | 0 | this_buffer->player_ref_count = 0; |
374 | 0 | if (this_buffer->decoder_ref_count == 0) { |
375 | 0 | release_buffer(decoder_model, i); |
376 | 0 | } |
377 | 0 | } |
378 | 0 | } |
379 | 0 | } |
380 | 0 | } |
381 | | |
382 | 0 | static int frames_in_buffer_pool(const DECODER_MODEL *const decoder_model) { |
383 | 0 | int frames_in_pool = 0; |
384 | 0 | for (int i = 0; i < BUFFER_POOL_MAX_SIZE; ++i) { |
385 | 0 | const FRAME_BUFFER *const this_buffer = |
386 | 0 | &decoder_model->frame_buffer_pool[i]; |
387 | 0 | if (this_buffer->decoder_ref_count > 0 || |
388 | 0 | this_buffer->player_ref_count > 0) { |
389 | 0 | ++frames_in_pool; |
390 | 0 | } |
391 | 0 | } |
392 | 0 | return frames_in_pool; |
393 | 0 | } |
394 | | |
395 | | static double get_presentation_time(const DECODER_MODEL *const decoder_model, |
396 | 0 | int display_index) { |
397 | 0 | if (decoder_model->mode == SCHEDULE_MODE) { |
398 | 0 | assert(0 && "SCHEDULE_MODE NOT SUPPORTED"); |
399 | 0 | return INVALID_TIME; |
400 | 0 | } else { |
401 | 0 | const double initial_presentation_delay = |
402 | 0 | decoder_model->initial_presentation_delay; |
403 | | // Can't decide presentation time until the initial presentation delay is |
404 | | // known. |
405 | 0 | if (initial_presentation_delay < 0.0) return INVALID_TIME; |
406 | | |
407 | 0 | return initial_presentation_delay + |
408 | 0 | display_index * decoder_model->num_ticks_per_picture * |
409 | 0 | decoder_model->display_clock_tick; |
410 | 0 | } |
411 | 0 | } |
412 | | |
413 | 0 | #define MAX_TIME 1e16 |
414 | | double time_next_buffer_is_free(int num_decoded_frame, int decoder_buffer_delay, |
415 | | const FRAME_BUFFER *frame_buffer_pool, |
416 | 0 | double current_time) { |
417 | 0 | if (num_decoded_frame == 0) { |
418 | 0 | return (double)decoder_buffer_delay / 90000.0; |
419 | 0 | } |
420 | | |
421 | 0 | double buf_free_time = MAX_TIME; |
422 | 0 | for (int i = 0; i < BUFFER_POOL_MAX_SIZE; ++i) { |
423 | 0 | const FRAME_BUFFER *const this_buffer = &frame_buffer_pool[i]; |
424 | 0 | if (this_buffer->decoder_ref_count == 0) { |
425 | 0 | if (this_buffer->player_ref_count == 0) { |
426 | 0 | return current_time; |
427 | 0 | } |
428 | 0 | const double presentation_time = this_buffer->presentation_time; |
429 | 0 | if (presentation_time >= 0.0 && presentation_time < buf_free_time) { |
430 | 0 | buf_free_time = presentation_time; |
431 | 0 | } |
432 | 0 | } |
433 | 0 | } |
434 | 0 | return buf_free_time < MAX_TIME ? buf_free_time : INVALID_TIME; |
435 | 0 | } |
436 | | #undef MAX_TIME |
437 | | |
438 | | static double get_removal_time(int mode, int num_decoded_frame, |
439 | | int decoder_buffer_delay, |
440 | | const FRAME_BUFFER *frame_buffer_pool, |
441 | 0 | double current_time) { |
442 | 0 | if (mode == SCHEDULE_MODE) { |
443 | 0 | assert(0 && "SCHEDULE_MODE IS NOT SUPPORTED YET"); |
444 | 0 | return INVALID_TIME; |
445 | 0 | } else { |
446 | 0 | return time_next_buffer_is_free(num_decoded_frame, decoder_buffer_delay, |
447 | 0 | frame_buffer_pool, current_time); |
448 | 0 | } |
449 | 0 | } |
450 | | |
451 | 0 | void av1_decoder_model_print_status(const DECODER_MODEL *const decoder_model) { |
452 | 0 | printf( |
453 | 0 | "\n status %d, num_frame %3d, num_decoded_frame %3d, " |
454 | 0 | "num_shown_frame %3d, current time %6.2f, frames in buffer %2d, " |
455 | 0 | "presentation delay %6.2f, total interval %6.2f\n", |
456 | 0 | decoder_model->status, decoder_model->num_frame, |
457 | 0 | decoder_model->num_decoded_frame, decoder_model->num_shown_frame, |
458 | 0 | decoder_model->current_time, frames_in_buffer_pool(decoder_model), |
459 | 0 | decoder_model->initial_presentation_delay, |
460 | 0 | decoder_model->dfg_interval_queue.total_interval); |
461 | 0 | for (int i = 0; i < 10; ++i) { |
462 | 0 | const FRAME_BUFFER *const this_buffer = |
463 | 0 | &decoder_model->frame_buffer_pool[i]; |
464 | 0 | printf("buffer %d, decode count %d, display count %d, present time %6.4f\n", |
465 | 0 | i, this_buffer->decoder_ref_count, this_buffer->player_ref_count, |
466 | 0 | this_buffer->presentation_time); |
467 | 0 | } |
468 | 0 | } |
469 | | |
470 | | // op_index is the operating point index. |
471 | | void av1_decoder_model_init(const AV1_COMP *const cpi, AV1_LEVEL level, |
472 | 0 | int op_index, DECODER_MODEL *const decoder_model) { |
473 | 0 | decoder_model->status = DECODER_MODEL_OK; |
474 | 0 | decoder_model->level = level; |
475 | |
|
476 | 0 | const AV1_COMMON *const cm = &cpi->common; |
477 | 0 | const SequenceHeader *const seq_params = cm->seq_params; |
478 | 0 | decoder_model->bit_rate = get_max_bitrate( |
479 | 0 | av1_level_defs + level, seq_params->tier[op_index], seq_params->profile); |
480 | | |
481 | | // TODO(huisu or anyone): implement SCHEDULE_MODE. |
482 | 0 | decoder_model->mode = RESOURCE_MODE; |
483 | 0 | decoder_model->encoder_buffer_delay = 20000; |
484 | 0 | decoder_model->decoder_buffer_delay = 70000; |
485 | 0 | decoder_model->is_low_delay_mode = false; |
486 | |
|
487 | 0 | decoder_model->first_bit_arrival_time = 0.0; |
488 | 0 | decoder_model->last_bit_arrival_time = 0.0; |
489 | 0 | decoder_model->coded_bits = 0; |
490 | |
|
491 | 0 | decoder_model->removal_time = INVALID_TIME; |
492 | 0 | decoder_model->presentation_time = INVALID_TIME; |
493 | 0 | decoder_model->decode_samples = 0; |
494 | 0 | decoder_model->display_samples = 0; |
495 | 0 | decoder_model->max_decode_rate = 0.0; |
496 | 0 | decoder_model->max_display_rate = 0.0; |
497 | |
|
498 | 0 | decoder_model->num_frame = -1; |
499 | 0 | decoder_model->num_decoded_frame = -1; |
500 | 0 | decoder_model->num_shown_frame = -1; |
501 | 0 | decoder_model->current_time = 0.0; |
502 | |
|
503 | 0 | initialize_buffer_pool(decoder_model); |
504 | |
|
505 | 0 | DFG_INTERVAL_QUEUE *const dfg_interval_queue = |
506 | 0 | &decoder_model->dfg_interval_queue; |
507 | 0 | dfg_interval_queue->total_interval = 0.0; |
508 | 0 | dfg_interval_queue->head = 0; |
509 | 0 | dfg_interval_queue->size = 0; |
510 | |
|
511 | 0 | if (seq_params->timing_info_present) { |
512 | 0 | decoder_model->num_ticks_per_picture = |
513 | 0 | seq_params->timing_info.num_ticks_per_picture; |
514 | 0 | decoder_model->display_clock_tick = |
515 | 0 | seq_params->timing_info.num_units_in_display_tick / |
516 | 0 | seq_params->timing_info.time_scale; |
517 | 0 | } else { |
518 | 0 | decoder_model->num_ticks_per_picture = 1; |
519 | 0 | decoder_model->display_clock_tick = 1.0 / cpi->framerate; |
520 | 0 | } |
521 | |
|
522 | 0 | decoder_model->initial_display_delay = |
523 | 0 | seq_params->op_params[op_index].initial_display_delay; |
524 | 0 | decoder_model->initial_presentation_delay = INVALID_TIME; |
525 | 0 | decoder_model->decode_rate = av1_level_defs[level].max_decode_rate; |
526 | 0 | } |
527 | | |
528 | | DECODER_MODEL_STATUS av1_decoder_model_try_smooth_buf( |
529 | | const AV1_COMP *const cpi, size_t coded_bits, |
530 | 0 | const DECODER_MODEL *const decoder_model) { |
531 | 0 | DECODER_MODEL_STATUS status = DECODER_MODEL_OK; |
532 | |
|
533 | 0 | if (!decoder_model || decoder_model->status != DECODER_MODEL_OK) { |
534 | 0 | return status; |
535 | 0 | } |
536 | | |
537 | 0 | const AV1_COMMON *const cm = &cpi->common; |
538 | 0 | const int show_existing_frame = cm->show_existing_frame; |
539 | |
|
540 | 0 | size_t cur_coded_bits = decoder_model->coded_bits + coded_bits; |
541 | 0 | int num_decoded_frame = decoder_model->num_decoded_frame; |
542 | 0 | if (!show_existing_frame) ++num_decoded_frame; |
543 | |
|
544 | 0 | if (show_existing_frame) { |
545 | 0 | return status; |
546 | 0 | } else { |
547 | 0 | const double removal_time = get_removal_time( |
548 | 0 | decoder_model->mode, num_decoded_frame, |
549 | 0 | decoder_model->decoder_buffer_delay, decoder_model->frame_buffer_pool, |
550 | 0 | decoder_model->current_time); |
551 | 0 | if (removal_time < 0.0) { |
552 | 0 | status = DECODE_FRAME_BUF_UNAVAILABLE; |
553 | 0 | return status; |
554 | 0 | } |
555 | | |
556 | | // A frame with show_existing_frame being false indicates the end of a DFG. |
557 | | // Update the bits arrival time of this DFG. |
558 | 0 | const double buffer_delay = (decoder_model->encoder_buffer_delay + |
559 | 0 | decoder_model->decoder_buffer_delay) / |
560 | 0 | 90000.0; |
561 | 0 | const double latest_arrival_time = removal_time - buffer_delay; |
562 | 0 | const double first_bit_arrival_time = |
563 | 0 | AOMMAX(decoder_model->last_bit_arrival_time, latest_arrival_time); |
564 | 0 | const double last_bit_arrival_time = |
565 | 0 | first_bit_arrival_time + |
566 | 0 | (double)cur_coded_bits / decoder_model->bit_rate; |
567 | | // Smoothing buffer underflows if the last bit arrives after the removal |
568 | | // time. |
569 | 0 | if (last_bit_arrival_time > removal_time && |
570 | 0 | !decoder_model->is_low_delay_mode) { |
571 | 0 | status = SMOOTHING_BUFFER_UNDERFLOW; |
572 | 0 | return status; |
573 | 0 | } |
574 | | |
575 | | // Check if the smoothing buffer overflows. |
576 | 0 | const DFG_INTERVAL_QUEUE *const queue = &decoder_model->dfg_interval_queue; |
577 | 0 | if (queue->size >= DFG_INTERVAL_QUEUE_SIZE) { |
578 | 0 | assert(0); |
579 | 0 | } |
580 | |
|
581 | 0 | double total_interval = queue->total_interval; |
582 | 0 | int qhead = queue->head; |
583 | 0 | int qsize = queue->size; |
584 | | // Remove the DFGs with removal time earlier than last_bit_arrival_time. |
585 | 0 | while (queue->buf[qhead].removal_time <= last_bit_arrival_time && |
586 | 0 | qsize > 0) { |
587 | 0 | if (queue->buf[qhead].removal_time - first_bit_arrival_time + |
588 | 0 | total_interval > |
589 | 0 | 1.0) { |
590 | 0 | status = SMOOTHING_BUFFER_OVERFLOW; |
591 | 0 | return status; |
592 | 0 | } |
593 | 0 | total_interval -= queue->buf[qhead].last_bit_arrival_time - |
594 | 0 | queue->buf[qhead].first_bit_arrival_time; |
595 | 0 | qhead = (qhead + 1) % DFG_INTERVAL_QUEUE_SIZE; |
596 | 0 | --qsize; |
597 | 0 | } |
598 | 0 | total_interval += last_bit_arrival_time - first_bit_arrival_time; |
599 | | // The smoothing buffer can hold at most "bit_rate" bits, which is |
600 | | // equivalent to 1 second of total interval. |
601 | 0 | if (total_interval > 1.0) { |
602 | 0 | status = SMOOTHING_BUFFER_OVERFLOW; |
603 | 0 | return status; |
604 | 0 | } |
605 | | |
606 | 0 | return status; |
607 | 0 | } |
608 | 0 | } |
609 | | |
610 | | void av1_decoder_model_process_frame(const AV1_COMP *const cpi, |
611 | | size_t coded_bits, |
612 | 0 | DECODER_MODEL *const decoder_model) { |
613 | 0 | if (!decoder_model || decoder_model->status != DECODER_MODEL_OK) return; |
614 | | |
615 | 0 | const AV1_COMMON *const cm = &cpi->common; |
616 | 0 | const int luma_pic_size = cm->superres_upscaled_width * cm->height; |
617 | 0 | const int show_existing_frame = cm->show_existing_frame; |
618 | 0 | const int show_frame = cm->show_frame || show_existing_frame; |
619 | 0 | ++decoder_model->num_frame; |
620 | 0 | if (!show_existing_frame) ++decoder_model->num_decoded_frame; |
621 | 0 | if (show_frame) ++decoder_model->num_shown_frame; |
622 | 0 | decoder_model->coded_bits += coded_bits; |
623 | |
|
624 | 0 | int display_idx = -1; |
625 | 0 | if (show_existing_frame) { |
626 | 0 | display_idx = decoder_model->vbi[cpi->existing_fb_idx_to_show]; |
627 | 0 | if (display_idx < 0) { |
628 | 0 | decoder_model->status = DECODE_EXISTING_FRAME_BUF_EMPTY; |
629 | 0 | return; |
630 | 0 | } |
631 | 0 | if (decoder_model->frame_buffer_pool[display_idx].frame_type == KEY_FRAME) { |
632 | 0 | update_ref_buffers(decoder_model, display_idx, 0xFF); |
633 | 0 | } |
634 | 0 | } else { |
635 | 0 | const double removal_time = get_removal_time( |
636 | 0 | decoder_model->mode, decoder_model->num_decoded_frame, |
637 | 0 | decoder_model->decoder_buffer_delay, decoder_model->frame_buffer_pool, |
638 | 0 | decoder_model->current_time); |
639 | 0 | if (removal_time < 0.0) { |
640 | 0 | decoder_model->status = DECODE_FRAME_BUF_UNAVAILABLE; |
641 | 0 | return; |
642 | 0 | } |
643 | | |
644 | 0 | const int previous_decode_samples = decoder_model->decode_samples; |
645 | 0 | const double previous_removal_time = decoder_model->removal_time; |
646 | 0 | assert(previous_removal_time < removal_time); |
647 | 0 | decoder_model->removal_time = removal_time; |
648 | 0 | decoder_model->decode_samples = luma_pic_size; |
649 | 0 | const double this_decode_rate = |
650 | 0 | previous_decode_samples / (removal_time - previous_removal_time); |
651 | 0 | decoder_model->max_decode_rate = |
652 | 0 | AOMMAX(decoder_model->max_decode_rate, this_decode_rate); |
653 | | |
654 | | // A frame with show_existing_frame being false indicates the end of a DFG. |
655 | | // Update the bits arrival time of this DFG. |
656 | 0 | const double buffer_delay = (decoder_model->encoder_buffer_delay + |
657 | 0 | decoder_model->decoder_buffer_delay) / |
658 | 0 | 90000.0; |
659 | 0 | const double latest_arrival_time = removal_time - buffer_delay; |
660 | 0 | decoder_model->first_bit_arrival_time = |
661 | 0 | AOMMAX(decoder_model->last_bit_arrival_time, latest_arrival_time); |
662 | 0 | decoder_model->last_bit_arrival_time = |
663 | 0 | decoder_model->first_bit_arrival_time + |
664 | 0 | (double)decoder_model->coded_bits / decoder_model->bit_rate; |
665 | | // Smoothing buffer underflows if the last bit arrives after the removal |
666 | | // time. |
667 | 0 | if (decoder_model->last_bit_arrival_time > removal_time && |
668 | 0 | !decoder_model->is_low_delay_mode) { |
669 | 0 | decoder_model->status = SMOOTHING_BUFFER_UNDERFLOW; |
670 | 0 | return; |
671 | 0 | } |
672 | | // Reset the coded bits for the next DFG. |
673 | 0 | decoder_model->coded_bits = 0; |
674 | | |
675 | | // Check if the smoothing buffer overflows. |
676 | 0 | DFG_INTERVAL_QUEUE *const queue = &decoder_model->dfg_interval_queue; |
677 | 0 | if (queue->size >= DFG_INTERVAL_QUEUE_SIZE) { |
678 | 0 | assert(0); |
679 | 0 | } |
680 | 0 | const double first_bit_arrival_time = decoder_model->first_bit_arrival_time; |
681 | 0 | const double last_bit_arrival_time = decoder_model->last_bit_arrival_time; |
682 | | // Remove the DFGs with removal time earlier than last_bit_arrival_time. |
683 | 0 | while (queue->buf[queue->head].removal_time <= last_bit_arrival_time && |
684 | 0 | queue->size > 0) { |
685 | 0 | if (queue->buf[queue->head].removal_time - first_bit_arrival_time + |
686 | 0 | queue->total_interval > |
687 | 0 | 1.0) { |
688 | 0 | decoder_model->status = SMOOTHING_BUFFER_OVERFLOW; |
689 | 0 | return; |
690 | 0 | } |
691 | 0 | queue->total_interval -= queue->buf[queue->head].last_bit_arrival_time - |
692 | 0 | queue->buf[queue->head].first_bit_arrival_time; |
693 | 0 | queue->head = (queue->head + 1) % DFG_INTERVAL_QUEUE_SIZE; |
694 | 0 | --queue->size; |
695 | 0 | } |
696 | | // Push current DFG into the queue. |
697 | 0 | const int queue_index = |
698 | 0 | (queue->head + queue->size++) % DFG_INTERVAL_QUEUE_SIZE; |
699 | 0 | queue->buf[queue_index].first_bit_arrival_time = first_bit_arrival_time; |
700 | 0 | queue->buf[queue_index].last_bit_arrival_time = last_bit_arrival_time; |
701 | 0 | queue->buf[queue_index].removal_time = removal_time; |
702 | 0 | queue->total_interval += last_bit_arrival_time - first_bit_arrival_time; |
703 | | // The smoothing buffer can hold at most "bit_rate" bits, which is |
704 | | // equivalent to 1 second of total interval. |
705 | 0 | if (queue->total_interval > 1.0) { |
706 | 0 | decoder_model->status = SMOOTHING_BUFFER_OVERFLOW; |
707 | 0 | return; |
708 | 0 | } |
709 | | |
710 | 0 | release_processed_frames(decoder_model, removal_time); |
711 | 0 | decoder_model->current_time = |
712 | 0 | removal_time + time_to_decode_frame(cm, decoder_model->decode_rate); |
713 | |
|
714 | 0 | const int cfbi = get_free_buffer(decoder_model); |
715 | 0 | if (cfbi < 0) { |
716 | 0 | decoder_model->status = DECODE_FRAME_BUF_UNAVAILABLE; |
717 | 0 | return; |
718 | 0 | } |
719 | 0 | const CurrentFrame *const current_frame = &cm->current_frame; |
720 | 0 | decoder_model->frame_buffer_pool[cfbi].frame_type = |
721 | 0 | cm->current_frame.frame_type; |
722 | 0 | display_idx = cfbi; |
723 | 0 | update_ref_buffers(decoder_model, cfbi, current_frame->refresh_frame_flags); |
724 | |
|
725 | 0 | if (decoder_model->initial_presentation_delay < 0.0) { |
726 | | // Display can begin after required number of frames have been buffered. |
727 | 0 | if (frames_in_buffer_pool(decoder_model) >= |
728 | 0 | decoder_model->initial_display_delay - 1) { |
729 | 0 | decoder_model->initial_presentation_delay = decoder_model->current_time; |
730 | | // Update presentation time for each shown frame in the frame buffer. |
731 | 0 | for (int i = 0; i < BUFFER_POOL_MAX_SIZE; ++i) { |
732 | 0 | FRAME_BUFFER *const this_buffer = |
733 | 0 | &decoder_model->frame_buffer_pool[i]; |
734 | 0 | if (this_buffer->player_ref_count == 0) continue; |
735 | 0 | assert(this_buffer->display_index >= 0); |
736 | 0 | this_buffer->presentation_time = |
737 | 0 | get_presentation_time(decoder_model, this_buffer->display_index); |
738 | 0 | } |
739 | 0 | } |
740 | 0 | } |
741 | 0 | } |
742 | | |
743 | | // Display. |
744 | 0 | if (show_frame) { |
745 | 0 | assert(display_idx >= 0 && display_idx < BUFFER_POOL_MAX_SIZE); |
746 | 0 | FRAME_BUFFER *const this_buffer = |
747 | 0 | &decoder_model->frame_buffer_pool[display_idx]; |
748 | 0 | ++this_buffer->player_ref_count; |
749 | 0 | this_buffer->display_index = decoder_model->num_shown_frame; |
750 | 0 | const double presentation_time = |
751 | 0 | get_presentation_time(decoder_model, this_buffer->display_index); |
752 | 0 | this_buffer->presentation_time = presentation_time; |
753 | 0 | if (presentation_time >= 0.0 && |
754 | 0 | decoder_model->current_time > presentation_time) { |
755 | 0 | decoder_model->status = DISPLAY_FRAME_LATE; |
756 | 0 | return; |
757 | 0 | } |
758 | | |
759 | 0 | const int previous_display_samples = decoder_model->display_samples; |
760 | 0 | const double previous_presentation_time = decoder_model->presentation_time; |
761 | 0 | decoder_model->display_samples = luma_pic_size; |
762 | 0 | decoder_model->presentation_time = presentation_time; |
763 | 0 | if (presentation_time >= 0.0 && previous_presentation_time >= 0.0) { |
764 | 0 | assert(previous_presentation_time < presentation_time); |
765 | 0 | const double this_display_rate = |
766 | 0 | previous_display_samples / |
767 | 0 | (presentation_time - previous_presentation_time); |
768 | 0 | decoder_model->max_display_rate = |
769 | 0 | AOMMAX(decoder_model->max_display_rate, this_display_rate); |
770 | 0 | } |
771 | 0 | } |
772 | 0 | } |
773 | | |
774 | 0 | void av1_init_level_info(AV1_COMP *cpi) { |
775 | 0 | for (int op_index = 0; op_index < MAX_NUM_OPERATING_POINTS; ++op_index) { |
776 | 0 | AV1LevelInfo *const this_level_info = |
777 | 0 | cpi->ppi->level_params.level_info[op_index]; |
778 | 0 | if (!this_level_info) continue; |
779 | 0 | memset(this_level_info, 0, sizeof(*this_level_info)); |
780 | 0 | AV1LevelSpec *const level_spec = &this_level_info->level_spec; |
781 | 0 | level_spec->level = SEQ_LEVEL_MAX; |
782 | 0 | AV1LevelStats *const level_stats = &this_level_info->level_stats; |
783 | 0 | level_stats->min_cropped_tile_width = INT_MAX; |
784 | 0 | level_stats->min_cropped_tile_height = INT_MAX; |
785 | 0 | level_stats->min_frame_width = INT_MAX; |
786 | 0 | level_stats->min_frame_height = INT_MAX; |
787 | 0 | level_stats->tile_width_is_valid = 1; |
788 | 0 | level_stats->min_cr = 1e8; |
789 | |
|
790 | 0 | FrameWindowBuffer *const frame_window_buffer = |
791 | 0 | &this_level_info->frame_window_buffer; |
792 | 0 | frame_window_buffer->num = 0; |
793 | 0 | frame_window_buffer->start = 0; |
794 | |
|
795 | 0 | const AV1_COMMON *const cm = &cpi->common; |
796 | 0 | const int upscaled_width = cm->superres_upscaled_width; |
797 | 0 | const int height = cm->height; |
798 | 0 | const int pic_size = upscaled_width * height; |
799 | 0 | for (AV1_LEVEL level = SEQ_LEVEL_2_0; level < SEQ_LEVELS; ++level) { |
800 | 0 | DECODER_MODEL *const this_model = &this_level_info->decoder_models[level]; |
801 | 0 | const AV1LevelSpec *const spec = &av1_level_defs[level]; |
802 | 0 | if (upscaled_width > spec->max_h_size || height > spec->max_v_size || |
803 | 0 | pic_size > spec->max_picture_size) { |
804 | | // Turn off decoder model for this level as the frame size already |
805 | | // exceeds level constraints. |
806 | 0 | this_model->status = DECODER_MODEL_DISABLED; |
807 | 0 | } else { |
808 | 0 | av1_decoder_model_init(cpi, level, op_index, this_model); |
809 | 0 | } |
810 | 0 | } |
811 | 0 | } |
812 | 0 | } |
813 | | |
814 | | static double get_min_cr(const AV1LevelSpec *const level_spec, int tier, |
815 | 0 | int is_still_picture, int64_t decoded_sample_rate) { |
816 | 0 | if (is_still_picture) return 0.8; |
817 | 0 | if (level_spec->level < SEQ_LEVEL_4_0) tier = 0; |
818 | 0 | const double min_cr_basis = tier ? level_spec->high_cr : level_spec->main_cr; |
819 | 0 | const double speed_adj = |
820 | 0 | (double)decoded_sample_rate / level_spec->max_display_rate; |
821 | 0 | return AOMMAX(min_cr_basis * speed_adj, 0.8); |
822 | 0 | } |
823 | | |
824 | | double av1_get_min_cr_for_level(AV1_LEVEL level_index, int tier, |
825 | 0 | int is_still_picture) { |
826 | 0 | assert(is_valid_seq_level_idx(level_index)); |
827 | 0 | const AV1LevelSpec *const level_spec = &av1_level_defs[level_index]; |
828 | 0 | return get_min_cr(level_spec, tier, is_still_picture, |
829 | 0 | level_spec->max_decode_rate); |
830 | 0 | } |
831 | | |
832 | | static void get_temporal_parallel_params(int scalability_mode_idc, |
833 | | int *temporal_parallel_num, |
834 | 0 | int *temporal_parallel_denom) { |
835 | 0 | if (scalability_mode_idc < 0) { |
836 | 0 | *temporal_parallel_num = 1; |
837 | 0 | *temporal_parallel_denom = 1; |
838 | 0 | return; |
839 | 0 | } |
840 | | |
841 | | // TODO(huisu@): handle scalability cases. |
842 | 0 | if (scalability_mode_idc == SCALABILITY_SS) { |
843 | 0 | (void)scalability_mode_idc; |
844 | 0 | } else { |
845 | 0 | (void)scalability_mode_idc; |
846 | 0 | } |
847 | 0 | } |
848 | | |
849 | 0 | #define MAX_TILE_SIZE (4096 * 2304) |
850 | 0 | #define MIN_CROPPED_TILE_WIDTH 8 |
851 | 0 | #define MIN_CROPPED_TILE_HEIGHT 8 |
852 | 0 | #define MIN_FRAME_WIDTH 16 |
853 | 0 | #define MIN_FRAME_HEIGHT 16 |
854 | 0 | #define MAX_TILE_SIZE_HEADER_RATE_PRODUCT 588251136 |
855 | | |
856 | | static TARGET_LEVEL_FAIL_ID check_level_constraints( |
857 | | const AV1LevelInfo *const level_info, AV1_LEVEL level, int tier, |
858 | 0 | int is_still_picture, BITSTREAM_PROFILE profile, int check_bitrate) { |
859 | 0 | const DECODER_MODEL *const decoder_model = &level_info->decoder_models[level]; |
860 | 0 | const DECODER_MODEL_STATUS decoder_model_status = decoder_model->status; |
861 | 0 | if (decoder_model_status != DECODER_MODEL_OK && |
862 | 0 | decoder_model_status != DECODER_MODEL_DISABLED) { |
863 | 0 | return DECODER_MODEL_FAIL; |
864 | 0 | } |
865 | | |
866 | 0 | const AV1LevelSpec *const level_spec = &level_info->level_spec; |
867 | 0 | const AV1LevelSpec *const target_level_spec = &av1_level_defs[level]; |
868 | 0 | const AV1LevelStats *const level_stats = &level_info->level_stats; |
869 | 0 | TARGET_LEVEL_FAIL_ID fail_id = TARGET_LEVEL_OK; |
870 | 0 | do { |
871 | 0 | if (level_spec->max_picture_size > target_level_spec->max_picture_size) { |
872 | 0 | fail_id = LUMA_PIC_SIZE_TOO_LARGE; |
873 | 0 | break; |
874 | 0 | } |
875 | | |
876 | 0 | if (level_spec->max_h_size > target_level_spec->max_h_size) { |
877 | 0 | fail_id = LUMA_PIC_H_SIZE_TOO_LARGE; |
878 | 0 | break; |
879 | 0 | } |
880 | | |
881 | 0 | if (level_spec->max_v_size > target_level_spec->max_v_size) { |
882 | 0 | fail_id = LUMA_PIC_V_SIZE_TOO_LARGE; |
883 | 0 | break; |
884 | 0 | } |
885 | | |
886 | 0 | if (level_spec->max_tile_cols > target_level_spec->max_tile_cols) { |
887 | 0 | fail_id = TOO_MANY_TILE_COLUMNS; |
888 | 0 | break; |
889 | 0 | } |
890 | | |
891 | 0 | if (level_spec->max_tiles > target_level_spec->max_tiles) { |
892 | 0 | fail_id = TOO_MANY_TILES; |
893 | 0 | break; |
894 | 0 | } |
895 | | |
896 | 0 | if (level_spec->max_header_rate > target_level_spec->max_header_rate) { |
897 | 0 | fail_id = FRAME_HEADER_RATE_TOO_HIGH; |
898 | 0 | break; |
899 | 0 | } |
900 | | |
901 | 0 | if (decoder_model->max_display_rate > |
902 | 0 | (double)target_level_spec->max_display_rate) { |
903 | 0 | fail_id = DISPLAY_RATE_TOO_HIGH; |
904 | 0 | break; |
905 | 0 | } |
906 | | |
907 | | // TODO(huisu): we are not using max decode rate calculated by the decoder |
908 | | // model because the model in resource availability mode always returns |
909 | | // MaxDecodeRate(as in the level definitions) as the max decode rate. |
910 | 0 | if (level_spec->max_decode_rate > target_level_spec->max_decode_rate) { |
911 | 0 | fail_id = DECODE_RATE_TOO_HIGH; |
912 | 0 | break; |
913 | 0 | } |
914 | | |
915 | 0 | if (level_spec->max_tile_rate > target_level_spec->max_tiles * 120) { |
916 | 0 | fail_id = TILE_RATE_TOO_HIGH; |
917 | 0 | break; |
918 | 0 | } |
919 | | |
920 | 0 | if (level_stats->max_tile_size > MAX_TILE_SIZE) { |
921 | 0 | fail_id = TILE_TOO_LARGE; |
922 | 0 | break; |
923 | 0 | } |
924 | | |
925 | 0 | if (level_stats->max_superres_tile_width > MAX_TILE_WIDTH) { |
926 | 0 | fail_id = SUPERRES_TILE_WIDTH_TOO_LARGE; |
927 | 0 | break; |
928 | 0 | } |
929 | | |
930 | 0 | if (level_stats->min_cropped_tile_width < MIN_CROPPED_TILE_WIDTH) { |
931 | 0 | fail_id = CROPPED_TILE_WIDTH_TOO_SMALL; |
932 | 0 | break; |
933 | 0 | } |
934 | | |
935 | 0 | if (level_stats->min_cropped_tile_height < MIN_CROPPED_TILE_HEIGHT) { |
936 | 0 | fail_id = CROPPED_TILE_HEIGHT_TOO_SMALL; |
937 | 0 | break; |
938 | 0 | } |
939 | | |
940 | 0 | if (level_stats->min_frame_width < MIN_FRAME_WIDTH) { |
941 | 0 | fail_id = LUMA_PIC_H_SIZE_TOO_SMALL; |
942 | 0 | break; |
943 | 0 | } |
944 | | |
945 | 0 | if (level_stats->min_frame_height < MIN_FRAME_HEIGHT) { |
946 | 0 | fail_id = LUMA_PIC_V_SIZE_TOO_SMALL; |
947 | 0 | break; |
948 | 0 | } |
949 | | |
950 | 0 | if (!level_stats->tile_width_is_valid) { |
951 | 0 | fail_id = TILE_WIDTH_INVALID; |
952 | 0 | break; |
953 | 0 | } |
954 | | |
955 | 0 | const double min_cr = get_min_cr(target_level_spec, tier, is_still_picture, |
956 | 0 | level_spec->max_decode_rate); |
957 | 0 | if (level_stats->min_cr < min_cr) { |
958 | 0 | fail_id = CR_TOO_SMALL; |
959 | 0 | break; |
960 | 0 | } |
961 | | |
962 | 0 | if (check_bitrate) { |
963 | | // Check average bitrate instead of max_bitrate. |
964 | 0 | const double bitrate_limit = |
965 | 0 | get_max_bitrate(target_level_spec, tier, profile); |
966 | 0 | const double avg_bitrate = level_stats->total_compressed_size * 8.0 / |
967 | 0 | level_stats->total_time_encoded; |
968 | 0 | if (avg_bitrate > bitrate_limit) { |
969 | 0 | fail_id = BITRATE_TOO_HIGH; |
970 | 0 | break; |
971 | 0 | } |
972 | 0 | } |
973 | | |
974 | 0 | if (target_level_spec->level > SEQ_LEVEL_5_1) { |
975 | 0 | int temporal_parallel_num; |
976 | 0 | int temporal_parallel_denom; |
977 | 0 | const int scalability_mode_idc = -1; |
978 | 0 | get_temporal_parallel_params(scalability_mode_idc, &temporal_parallel_num, |
979 | 0 | &temporal_parallel_denom); |
980 | 0 | const int val = level_stats->max_tile_size * level_spec->max_header_rate * |
981 | 0 | temporal_parallel_denom / temporal_parallel_num; |
982 | 0 | if (val > MAX_TILE_SIZE_HEADER_RATE_PRODUCT) { |
983 | 0 | fail_id = TILE_SIZE_HEADER_RATE_TOO_HIGH; |
984 | 0 | break; |
985 | 0 | } |
986 | 0 | } |
987 | 0 | } while (0); |
988 | | |
989 | 0 | return fail_id; |
990 | 0 | } |
991 | | |
992 | | static void get_tile_stats(const AV1_COMMON *const cm, |
993 | | const TileDataEnc *const tile_data, |
994 | | int *max_tile_size, int *max_superres_tile_width, |
995 | | int *min_cropped_tile_width, |
996 | | int *min_cropped_tile_height, |
997 | 0 | int *tile_width_valid) { |
998 | 0 | const int tile_cols = cm->tiles.cols; |
999 | 0 | const int tile_rows = cm->tiles.rows; |
1000 | 0 | const int superres_scale_denominator = cm->superres_scale_denominator; |
1001 | |
|
1002 | 0 | *max_tile_size = 0; |
1003 | 0 | *max_superres_tile_width = 0; |
1004 | 0 | *min_cropped_tile_width = INT_MAX; |
1005 | 0 | *min_cropped_tile_height = INT_MAX; |
1006 | 0 | *tile_width_valid = 1; |
1007 | |
|
1008 | 0 | for (int tile_row = 0; tile_row < tile_rows; ++tile_row) { |
1009 | 0 | for (int tile_col = 0; tile_col < tile_cols; ++tile_col) { |
1010 | 0 | const TileInfo *const tile_info = |
1011 | 0 | &tile_data[tile_row * cm->tiles.cols + tile_col].tile_info; |
1012 | 0 | const int tile_width = |
1013 | 0 | (tile_info->mi_col_end - tile_info->mi_col_start) * MI_SIZE; |
1014 | 0 | const int tile_height = |
1015 | 0 | (tile_info->mi_row_end - tile_info->mi_row_start) * MI_SIZE; |
1016 | 0 | const int tile_size = tile_width * tile_height; |
1017 | 0 | *max_tile_size = AOMMAX(*max_tile_size, tile_size); |
1018 | |
|
1019 | 0 | const int supperres_tile_width = |
1020 | 0 | tile_width * superres_scale_denominator / SCALE_NUMERATOR; |
1021 | 0 | *max_superres_tile_width = |
1022 | 0 | AOMMAX(*max_superres_tile_width, supperres_tile_width); |
1023 | |
|
1024 | 0 | const int cropped_tile_width = |
1025 | 0 | cm->width - tile_info->mi_col_start * MI_SIZE; |
1026 | 0 | const int cropped_tile_height = |
1027 | 0 | cm->height - tile_info->mi_row_start * MI_SIZE; |
1028 | 0 | *min_cropped_tile_width = |
1029 | 0 | AOMMIN(*min_cropped_tile_width, cropped_tile_width); |
1030 | 0 | *min_cropped_tile_height = |
1031 | 0 | AOMMIN(*min_cropped_tile_height, cropped_tile_height); |
1032 | |
|
1033 | 0 | const int is_right_most_tile = |
1034 | 0 | tile_info->mi_col_end == cm->mi_params.mi_cols; |
1035 | 0 | if (!is_right_most_tile) { |
1036 | 0 | if (av1_superres_scaled(cm)) |
1037 | 0 | *tile_width_valid &= tile_width >= 128; |
1038 | 0 | else |
1039 | 0 | *tile_width_valid &= tile_width >= 64; |
1040 | 0 | } |
1041 | 0 | } |
1042 | 0 | } |
1043 | 0 | } |
1044 | | |
1045 | | static int store_frame_record(int64_t ts_start, int64_t ts_end, |
1046 | | size_t encoded_size, int pic_size, |
1047 | | int frame_header_count, int tiles, int show_frame, |
1048 | | int show_existing_frame, |
1049 | 0 | FrameWindowBuffer *const buffer) { |
1050 | 0 | if (buffer->num < FRAME_WINDOW_SIZE) { |
1051 | 0 | ++buffer->num; |
1052 | 0 | } else { |
1053 | 0 | buffer->start = (buffer->start + 1) % FRAME_WINDOW_SIZE; |
1054 | 0 | } |
1055 | 0 | const int new_idx = (buffer->start + buffer->num - 1) % FRAME_WINDOW_SIZE; |
1056 | 0 | FrameRecord *const record = &buffer->buf[new_idx]; |
1057 | 0 | record->ts_start = ts_start; |
1058 | 0 | record->ts_end = ts_end; |
1059 | 0 | record->encoded_size_in_bytes = encoded_size; |
1060 | 0 | record->pic_size = pic_size; |
1061 | 0 | record->frame_header_count = frame_header_count; |
1062 | 0 | record->tiles = tiles; |
1063 | 0 | record->show_frame = show_frame; |
1064 | 0 | record->show_existing_frame = show_existing_frame; |
1065 | |
|
1066 | 0 | return new_idx; |
1067 | 0 | } |
1068 | | |
1069 | | // Count the number of frames encoded in the last "duration" ticks, in display |
1070 | | // time. |
1071 | | static int count_frames(const FrameWindowBuffer *const buffer, |
1072 | 0 | int64_t duration) { |
1073 | 0 | const int current_idx = (buffer->start + buffer->num - 1) % FRAME_WINDOW_SIZE; |
1074 | | // Assume current frame is shown frame. |
1075 | 0 | assert(buffer->buf[current_idx].show_frame); |
1076 | |
|
1077 | 0 | const int64_t current_time = buffer->buf[current_idx].ts_end; |
1078 | 0 | const int64_t time_limit = AOMMAX(current_time - duration, 0); |
1079 | 0 | int num_frames = 1; |
1080 | 0 | int index = current_idx - 1; |
1081 | 0 | for (int i = buffer->num - 2; i >= 0; --i, --index, ++num_frames) { |
1082 | 0 | if (index < 0) index = FRAME_WINDOW_SIZE - 1; |
1083 | 0 | const FrameRecord *const record = &buffer->buf[index]; |
1084 | 0 | if (!record->show_frame) continue; |
1085 | 0 | const int64_t ts_start = record->ts_start; |
1086 | 0 | if (ts_start < time_limit) break; |
1087 | 0 | } |
1088 | |
|
1089 | 0 | return num_frames; |
1090 | 0 | } |
1091 | | |
1092 | | // Scan previously encoded frames and update level metrics accordingly. |
1093 | | static void scan_past_frames(const FrameWindowBuffer *const buffer, |
1094 | | int num_frames_to_scan, |
1095 | | AV1LevelSpec *const level_spec, |
1096 | 0 | AV1LevelStats *const level_stats) { |
1097 | 0 | const int num_frames_in_buffer = buffer->num; |
1098 | 0 | int index = (buffer->start + num_frames_in_buffer - 1) % FRAME_WINDOW_SIZE; |
1099 | 0 | int frame_headers = 0; |
1100 | 0 | int tiles = 0; |
1101 | 0 | int64_t display_samples = 0; |
1102 | 0 | int64_t decoded_samples = 0; |
1103 | 0 | size_t encoded_size_in_bytes = 0; |
1104 | 0 | for (int i = 0; i < AOMMIN(num_frames_in_buffer, num_frames_to_scan); ++i) { |
1105 | 0 | const FrameRecord *const record = &buffer->buf[index]; |
1106 | 0 | if (!record->show_existing_frame) { |
1107 | 0 | frame_headers += record->frame_header_count; |
1108 | 0 | decoded_samples += record->pic_size; |
1109 | 0 | } |
1110 | 0 | if (record->show_frame) { |
1111 | 0 | display_samples += record->pic_size; |
1112 | 0 | } |
1113 | 0 | tiles += record->tiles; |
1114 | 0 | encoded_size_in_bytes += record->encoded_size_in_bytes; |
1115 | 0 | --index; |
1116 | 0 | if (index < 0) index = FRAME_WINDOW_SIZE - 1; |
1117 | 0 | } |
1118 | 0 | level_spec->max_header_rate = |
1119 | 0 | AOMMAX(level_spec->max_header_rate, frame_headers); |
1120 | | // TODO(huisu): we can now compute max display rate with the decoder model, so |
1121 | | // these couple of lines can be removed. Keep them here for a while for |
1122 | | // debugging purpose. |
1123 | 0 | level_spec->max_display_rate = |
1124 | 0 | AOMMAX(level_spec->max_display_rate, display_samples); |
1125 | 0 | level_spec->max_decode_rate = |
1126 | 0 | AOMMAX(level_spec->max_decode_rate, decoded_samples); |
1127 | 0 | level_spec->max_tile_rate = AOMMAX(level_spec->max_tile_rate, tiles); |
1128 | 0 | level_stats->max_bitrate = |
1129 | 0 | AOMMAX(level_stats->max_bitrate, (int)encoded_size_in_bytes * 8); |
1130 | 0 | } |
1131 | | |
1132 | | void av1_update_level_info(AV1_COMP *cpi, size_t size, int64_t ts_start, |
1133 | 0 | int64_t ts_end) { |
1134 | 0 | AV1_COMMON *const cm = &cpi->common; |
1135 | 0 | const AV1LevelParams *const level_params = &cpi->ppi->level_params; |
1136 | |
|
1137 | 0 | const int upscaled_width = cm->superres_upscaled_width; |
1138 | 0 | const int width = cm->width; |
1139 | 0 | const int height = cm->height; |
1140 | 0 | const int tile_cols = cm->tiles.cols; |
1141 | 0 | const int tile_rows = cm->tiles.rows; |
1142 | 0 | const int tiles = tile_cols * tile_rows; |
1143 | 0 | const int luma_pic_size = upscaled_width * height; |
1144 | 0 | const int frame_header_count = cpi->frame_header_count; |
1145 | 0 | const int show_frame = cm->show_frame; |
1146 | 0 | const int show_existing_frame = cm->show_existing_frame; |
1147 | |
|
1148 | 0 | int max_tile_size; |
1149 | 0 | int min_cropped_tile_width; |
1150 | 0 | int min_cropped_tile_height; |
1151 | 0 | int max_superres_tile_width; |
1152 | 0 | int tile_width_is_valid; |
1153 | 0 | get_tile_stats(cm, cpi->tile_data, &max_tile_size, &max_superres_tile_width, |
1154 | 0 | &min_cropped_tile_width, &min_cropped_tile_height, |
1155 | 0 | &tile_width_is_valid); |
1156 | |
|
1157 | 0 | const double compression_ratio = av1_get_compression_ratio(cm, size); |
1158 | |
|
1159 | 0 | const int temporal_layer_id = cm->temporal_layer_id; |
1160 | 0 | const int spatial_layer_id = cm->spatial_layer_id; |
1161 | 0 | const SequenceHeader *const seq_params = cm->seq_params; |
1162 | 0 | const BITSTREAM_PROFILE profile = seq_params->profile; |
1163 | 0 | const int is_still_picture = seq_params->still_picture; |
1164 | | // update level_stats |
1165 | | // TODO(kyslov@) fix the implementation according to buffer model |
1166 | 0 | for (int i = 0; i < seq_params->operating_points_cnt_minus_1 + 1; ++i) { |
1167 | 0 | if (!is_in_operating_point(seq_params->operating_point_idc[i], |
1168 | 0 | temporal_layer_id, spatial_layer_id) || |
1169 | 0 | !((level_params->keep_level_stats >> i) & 1)) { |
1170 | 0 | continue; |
1171 | 0 | } |
1172 | | |
1173 | 0 | AV1LevelInfo *const level_info = level_params->level_info[i]; |
1174 | 0 | assert(level_info != NULL); |
1175 | 0 | AV1LevelStats *const level_stats = &level_info->level_stats; |
1176 | |
|
1177 | 0 | level_stats->max_tile_size = |
1178 | 0 | AOMMAX(level_stats->max_tile_size, max_tile_size); |
1179 | 0 | level_stats->max_superres_tile_width = |
1180 | 0 | AOMMAX(level_stats->max_superres_tile_width, max_superres_tile_width); |
1181 | 0 | level_stats->min_cropped_tile_width = |
1182 | 0 | AOMMIN(level_stats->min_cropped_tile_width, min_cropped_tile_width); |
1183 | 0 | level_stats->min_cropped_tile_height = |
1184 | 0 | AOMMIN(level_stats->min_cropped_tile_height, min_cropped_tile_height); |
1185 | 0 | level_stats->tile_width_is_valid &= tile_width_is_valid; |
1186 | 0 | level_stats->min_frame_width = AOMMIN(level_stats->min_frame_width, width); |
1187 | 0 | level_stats->min_frame_height = |
1188 | 0 | AOMMIN(level_stats->min_frame_height, height); |
1189 | 0 | level_stats->min_cr = AOMMIN(level_stats->min_cr, compression_ratio); |
1190 | 0 | level_stats->total_compressed_size += (double)size; |
1191 | | |
1192 | | // update level_spec |
1193 | | // TODO(kyslov@) update all spec fields |
1194 | 0 | AV1LevelSpec *const level_spec = &level_info->level_spec; |
1195 | 0 | level_spec->max_picture_size = |
1196 | 0 | AOMMAX(level_spec->max_picture_size, luma_pic_size); |
1197 | 0 | level_spec->max_h_size = |
1198 | 0 | AOMMAX(level_spec->max_h_size, cm->superres_upscaled_width); |
1199 | 0 | level_spec->max_v_size = AOMMAX(level_spec->max_v_size, height); |
1200 | 0 | level_spec->max_tile_cols = AOMMAX(level_spec->max_tile_cols, tile_cols); |
1201 | 0 | level_spec->max_tiles = AOMMAX(level_spec->max_tiles, tiles); |
1202 | | |
1203 | | // Store info. of current frame into FrameWindowBuffer. |
1204 | 0 | FrameWindowBuffer *const buffer = &level_info->frame_window_buffer; |
1205 | 0 | store_frame_record(ts_start, ts_end, size, luma_pic_size, |
1206 | 0 | frame_header_count, tiles, show_frame, |
1207 | 0 | show_existing_frame, buffer); |
1208 | 0 | if (show_frame) { |
1209 | | // Count the number of frames encoded in the past 1 second. |
1210 | 0 | const int encoded_frames_in_last_second = |
1211 | 0 | show_frame ? count_frames(buffer, TICKS_PER_SEC) : 0; |
1212 | 0 | scan_past_frames(buffer, encoded_frames_in_last_second, level_spec, |
1213 | 0 | level_stats); |
1214 | 0 | level_stats->total_time_encoded += |
1215 | 0 | (cpi->time_stamps.prev_ts_end - cpi->time_stamps.prev_ts_start) / |
1216 | 0 | (double)TICKS_PER_SEC; |
1217 | 0 | } |
1218 | |
|
1219 | 0 | DECODER_MODEL *const decoder_models = level_info->decoder_models; |
1220 | 0 | for (AV1_LEVEL level = SEQ_LEVEL_2_0; level < SEQ_LEVELS; ++level) { |
1221 | 0 | av1_decoder_model_process_frame(cpi, size << 3, &decoder_models[level]); |
1222 | 0 | } |
1223 | | |
1224 | | // Check whether target level is met. |
1225 | 0 | const AV1_LEVEL target_level = level_params->target_seq_level_idx[i]; |
1226 | 0 | if (target_level < SEQ_LEVELS) { |
1227 | 0 | assert(is_valid_seq_level_idx(target_level)); |
1228 | 0 | const int tier = seq_params->tier[i]; |
1229 | 0 | const TARGET_LEVEL_FAIL_ID fail_id = check_level_constraints( |
1230 | 0 | level_info, target_level, tier, is_still_picture, profile, 0); |
1231 | 0 | if (fail_id != TARGET_LEVEL_OK) { |
1232 | 0 | const int target_level_major = 2 + (target_level >> 2); |
1233 | 0 | const int target_level_minor = target_level & 3; |
1234 | 0 | aom_internal_error(cm->error, AOM_CODEC_ERROR, |
1235 | 0 | "Failed to encode to the target level %d_%d. %s", |
1236 | 0 | target_level_major, target_level_minor, |
1237 | 0 | level_fail_messages[fail_id]); |
1238 | 0 | } |
1239 | 0 | } |
1240 | 0 | } |
1241 | 0 | } |
1242 | | |
1243 | | aom_codec_err_t av1_get_seq_level_idx(const SequenceHeader *seq_params, |
1244 | | const AV1LevelParams *level_params, |
1245 | 0 | int *seq_level_idx) { |
1246 | 0 | const int is_still_picture = seq_params->still_picture; |
1247 | 0 | const BITSTREAM_PROFILE profile = seq_params->profile; |
1248 | 0 | for (int op = 0; op < seq_params->operating_points_cnt_minus_1 + 1; ++op) { |
1249 | 0 | seq_level_idx[op] = (int)SEQ_LEVEL_MAX; |
1250 | 0 | if (!((level_params->keep_level_stats >> op) & 1)) continue; |
1251 | 0 | const int tier = seq_params->tier[op]; |
1252 | 0 | const AV1LevelInfo *const level_info = level_params->level_info[op]; |
1253 | 0 | assert(level_info != NULL); |
1254 | 0 | for (int level = 0; level < SEQ_LEVELS; ++level) { |
1255 | 0 | if (!is_valid_seq_level_idx(level)) continue; |
1256 | 0 | const TARGET_LEVEL_FAIL_ID fail_id = check_level_constraints( |
1257 | 0 | level_info, level, tier, is_still_picture, profile, 1); |
1258 | 0 | if (fail_id == TARGET_LEVEL_OK) { |
1259 | 0 | seq_level_idx[op] = level; |
1260 | 0 | break; |
1261 | 0 | } |
1262 | 0 | } |
1263 | 0 | } |
1264 | |
|
1265 | 0 | return AOM_CODEC_OK; |
1266 | 0 | } |