/src/avm/av2/decoder/obu.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2021, Alliance for Open Media. All rights reserved |
3 | | * |
4 | | * This source code is subject to the terms of the BSD 3-Clause Clear License |
5 | | * and the Alliance for Open Media Patent License 1.0. If the BSD 3-Clause Clear |
6 | | * License was not distributed with this source code in the LICENSE file, you |
7 | | * can obtain it at aomedia.org/license/software-license/bsd-3-c-c/. If the |
8 | | * Alliance for Open Media Patent License 1.0 was not distributed with this |
9 | | * source code in the PATENTS file, you can obtain it at |
10 | | * aomedia.org/license/patent-license/. |
11 | | */ |
12 | | |
13 | | #include <assert.h> |
14 | | |
15 | | #include "config/avm_config.h" |
16 | | #include "av2/common/banding_metadata.h" |
17 | | #include "config/avm_scale_rtcd.h" |
18 | | |
19 | | #include "avm/avm_codec.h" |
20 | | #include "avm_dsp/bitreader_buffer.h" |
21 | | #include "avm_mem/avm_mem.h" |
22 | | #include "avm_ports/mem_ops.h" |
23 | | |
24 | | #include "av2/common/common.h" |
25 | | #include "av2/common/obu_util.h" |
26 | | #include "av2/common/level.h" |
27 | | #include "av2/common/timing.h" |
28 | | #include "av2/decoder/decoder.h" |
29 | | #include "av2/decoder/decodeframe.h" |
30 | | #include "av2/decoder/obu.h" |
31 | | #include "av2/common/enums.h" |
32 | | #include "av2/common/annexA.h" |
33 | | #include "av2/decoder/annexF.h" |
34 | | |
35 | 59 | static uint32_t read_temporal_delimiter_obu() { return 0; } |
36 | | |
37 | | // Returns a boolean that indicates success. |
38 | | static int read_bitstream_level(AV2_LEVEL *seq_level_idx, |
39 | 22.1k | struct avm_read_bit_buffer *rb) { |
40 | 22.1k | *seq_level_idx = avm_rb_read_literal(rb, LEVEL_BITS); |
41 | 22.1k | if (!is_valid_seq_level_idx(*seq_level_idx)) return 0; |
42 | 22.0k | return 1; |
43 | 22.1k | } |
44 | | |
45 | | static void av2_read_tlayer_dependency_info(SequenceHeader *const seq, |
46 | 267 | struct avm_read_bit_buffer *rb) { |
47 | 267 | const int max_mlayer_id = seq->max_mlayer_id; |
48 | 267 | const int max_tlayer_id = seq->max_tlayer_id; |
49 | 267 | const int multi_tlayer_flag = seq->multi_tlayer_dependency_map_present_flag; |
50 | 536 | for (int curr_mlayer_id = 0; curr_mlayer_id <= max_mlayer_id; |
51 | 269 | curr_mlayer_id++) { |
52 | 804 | for (int curr_tlayer_id = 1; curr_tlayer_id <= max_tlayer_id; |
53 | 535 | curr_tlayer_id++) { |
54 | 1.87k | for (int ref_tlayer_id = curr_tlayer_id; ref_tlayer_id >= 0; |
55 | 1.33k | ref_tlayer_id--) { |
56 | 1.33k | if (multi_tlayer_flag > 0 || curr_mlayer_id == 0) { |
57 | 1.33k | seq->tlayer_dependency_map[curr_mlayer_id][curr_tlayer_id] |
58 | 1.33k | [ref_tlayer_id] = avm_rb_read_bit(rb); |
59 | 1.33k | } else { |
60 | 4 | seq->tlayer_dependency_map[curr_mlayer_id][curr_tlayer_id] |
61 | 4 | [ref_tlayer_id] = |
62 | 4 | seq->tlayer_dependency_map[0][curr_tlayer_id][ref_tlayer_id]; |
63 | 4 | } |
64 | 1.33k | } |
65 | 535 | } |
66 | 269 | } |
67 | 267 | } |
68 | | |
69 | | static void av2_read_mlayer_dependency_info(SequenceHeader *const seq, |
70 | 1.40k | struct avm_read_bit_buffer *rb) { |
71 | 1.40k | const int max_mlayer_id = seq->max_mlayer_id; |
72 | 4.21k | for (int curr_mlayer_id = 1; curr_mlayer_id <= max_mlayer_id; |
73 | 2.81k | curr_mlayer_id++) { |
74 | 9.83k | for (int ref_mlayer_id = curr_mlayer_id; ref_mlayer_id >= 0; |
75 | 7.02k | ref_mlayer_id--) { |
76 | 7.02k | seq->mlayer_dependency_map[curr_mlayer_id][ref_mlayer_id] = |
77 | 7.02k | avm_rb_read_bit(rb); |
78 | 7.02k | } |
79 | 2.81k | } |
80 | 1.40k | } |
81 | | |
82 | | // This function validates the conformance window params |
83 | | static void av2_validate_seq_conformance_window( |
84 | | const struct SequenceHeader *seq_params, |
85 | 21.9k | struct avm_internal_error_info *error_info) { |
86 | 21.9k | const struct CropWindow *conf = &seq_params->conf; |
87 | 21.9k | if (!conf->conf_win_enabled_flag) return; |
88 | | |
89 | 1.56k | if (conf->conf_win_left_offset >= seq_params->max_frame_width) { |
90 | 1 | avm_internal_error( |
91 | 1 | error_info, AVM_CODEC_UNSUP_BITSTREAM, |
92 | 1 | "Conformance window left offset %d exceeds max width %d\n", |
93 | 1 | conf->conf_win_left_offset, seq_params->max_frame_width); |
94 | 1 | } |
95 | | |
96 | 1.56k | if (conf->conf_win_right_offset >= seq_params->max_frame_width) { |
97 | 10 | avm_internal_error( |
98 | 10 | error_info, AVM_CODEC_UNSUP_BITSTREAM, |
99 | 10 | "Conformance window right offset %d exceeds max width %d\n", |
100 | 10 | conf->conf_win_right_offset, seq_params->max_frame_width); |
101 | 10 | } |
102 | | |
103 | 1.56k | if (conf->conf_win_top_offset >= seq_params->max_frame_height) { |
104 | 0 | avm_internal_error( |
105 | 0 | error_info, AVM_CODEC_UNSUP_BITSTREAM, |
106 | 0 | "Conformance window top offset %d exceeds max height %d\n", |
107 | 0 | conf->conf_win_top_offset, seq_params->max_frame_height); |
108 | 0 | } |
109 | | |
110 | 1.56k | if (conf->conf_win_bottom_offset >= seq_params->max_frame_height) { |
111 | 0 | avm_internal_error( |
112 | 0 | error_info, AVM_CODEC_UNSUP_BITSTREAM, |
113 | 0 | "Conformance window bottom offset %d exceeds max height %d\n", |
114 | 0 | conf->conf_win_bottom_offset, seq_params->max_frame_height); |
115 | 0 | } |
116 | 1.56k | } |
117 | | |
118 | | // Returns whether two sequence headers are consistent with each other. |
119 | | // Note that the 'op_params' field is not compared per Section 7.5 in the spec: |
120 | | // Within a particular coded video sequence, the contents of |
121 | | // sequence_header_obu must be bit-identical each time the sequence header |
122 | | // appears except for the contents of operating_parameters_info. |
123 | | int are_seq_headers_consistent(const SequenceHeader *seq_params_old, |
124 | 14.5k | const SequenceHeader *seq_params_new) { |
125 | 14.5k | return !memcmp(seq_params_old, seq_params_new, |
126 | 14.5k | offsetof(SequenceHeader, op_params)); |
127 | 14.5k | } |
128 | | |
129 | | void av2_read_color_info(int *color_description_idc, int *color_primaries, |
130 | | int *transfer_characteristics, |
131 | | int *matrix_coefficients, int *full_range_flag, |
132 | 52 | struct avm_read_bit_buffer *rb) { |
133 | | // color_description_idc: indicates the combination of color primaries, |
134 | | // transfer characteristics and matrix coefficients as defined in Section |
135 | | // 6.10.4 (Operating point set color info semantics) in the spec. |
136 | | // The value of color_description_idc shall be in the range of 0 to 127, |
137 | | // inclusive. Values larger than 5 are reserved for future use by AOMedia and |
138 | | // should be ignored by decoders conforming to this version of this |
139 | | // specification. |
140 | 52 | *color_description_idc = avm_rb_read_rice_golomb(rb, 2); |
141 | 52 | if (*color_description_idc > 127) { |
142 | 0 | rb->error_handler(rb->error_handler_data, AVM_CODEC_UNSUP_BITSTREAM, |
143 | 0 | "color_description_idc is not in the range of 0 to 127"); |
144 | 0 | } |
145 | 52 | switch (*color_description_idc) { |
146 | 24 | case AVM_COLOR_DESC_IDC_EXPLICIT: // 0 |
147 | 24 | *color_primaries = avm_rb_read_literal(rb, 8); |
148 | 24 | *transfer_characteristics = avm_rb_read_literal(rb, 8); |
149 | 24 | *matrix_coefficients = avm_rb_read_literal(rb, 8); |
150 | 24 | break; |
151 | 1 | case AVM_COLOR_DESC_IDC_BT709SDR: // 1 |
152 | 1 | *color_primaries = AVM_CICP_CP_BT_709; // 1 |
153 | 1 | *transfer_characteristics = AVM_CICP_TC_BT_709; // 1 |
154 | 1 | *matrix_coefficients = AVM_CICP_MC_BT_709; // 1 |
155 | 1 | break; |
156 | 0 | case AVM_COLOR_DESC_IDC_BT2100PQ: // 2 |
157 | 0 | *color_primaries = AVM_CICP_CP_BT_2020; // 9 |
158 | 0 | *transfer_characteristics = AVM_CICP_TC_SMPTE_2084; // 16 |
159 | 0 | *matrix_coefficients = AVM_CICP_MC_BT_2020_NCL; // 9 |
160 | 0 | break; |
161 | 23 | case AVM_COLOR_DESC_IDC_BT2100HLG: // 3 |
162 | 23 | *color_primaries = AVM_CICP_CP_BT_2020; // 9 |
163 | 23 | *transfer_characteristics = AVM_CICP_TC_HLG; // 18 |
164 | 23 | *matrix_coefficients = AVM_CICP_MC_BT_2020_NCL; // 9 |
165 | 23 | break; |
166 | 0 | case AVM_COLOR_DESC_IDC_SRGB: // 4 |
167 | 0 | *color_primaries = AVM_CICP_CP_BT_709; // 1 |
168 | 0 | *transfer_characteristics = AVM_CICP_TC_SRGB; // 13 |
169 | 0 | *matrix_coefficients = AVM_CICP_MC_IDENTITY; // 0 |
170 | 0 | break; |
171 | 2 | case AVM_COLOR_DESC_IDC_SYCC: // 5 |
172 | 2 | *color_primaries = AVM_CICP_CP_BT_709; // 1 |
173 | 2 | *transfer_characteristics = AVM_CICP_TC_SRGB; // 13 |
174 | 2 | *matrix_coefficients = AVM_CICP_MC_BT_470_B_G; // 5 |
175 | 2 | break; |
176 | 2 | default: |
177 | | // Values larger than 5 are reserved for future use by AOMedia and should |
178 | | // be ignored by decoders. |
179 | 2 | *color_primaries = AVM_CICP_CP_UNSPECIFIED; |
180 | 2 | *transfer_characteristics = AVM_CICP_TC_UNSPECIFIED; |
181 | 2 | *matrix_coefficients = AVM_CICP_MC_UNSPECIFIED; |
182 | 2 | break; |
183 | 52 | } |
184 | 52 | *full_range_flag = avm_rb_read_bit(rb); |
185 | 52 | } |
186 | | |
187 | | // Helper function to store xlayer context |
188 | | // Helper function to map xlayer_id to stream_id array index |
189 | 22.8k | int av2_get_stream_index(const AV2_COMMON *cm, int xlayer_id) { |
190 | | // GLOBAL_XLAYER_ID doesn't use stream_info |
191 | 22.8k | if (xlayer_id == GLOBAL_XLAYER_ID) { |
192 | 1.82k | return -1; |
193 | 1.82k | } |
194 | | |
195 | | // Find which index in stream_ids matches this xlayer_id |
196 | 27.7k | for (int i = 0; i < cm->num_streams; i++) { |
197 | 14.4k | if (cm->stream_ids[i] == xlayer_id) { |
198 | 7.69k | return i; |
199 | 7.69k | } |
200 | 14.4k | } |
201 | | |
202 | | // Should never happen with valid bitstream |
203 | 13.2k | return -1; |
204 | 20.9k | } |
205 | | |
206 | 3.23k | void av2_store_xlayer_context(AV2Decoder *pbi, AV2_COMMON *cm, int xlayer_id) { |
207 | 3.23k | int stream_idx = av2_get_stream_index(cm, xlayer_id); |
208 | 3.23k | if (stream_idx < 0) return; // Invalid or GLOBAL_XLAYER_ID |
209 | | |
210 | 34.0k | for (int i = 0; i < REF_FRAMES; i++) { |
211 | 32.0k | pbi->stream_info[stream_idx].ref_frame_map_buf[i] = cm->ref_frame_map[i]; |
212 | 32.0k | pbi->stream_info[stream_idx].valid_for_referencing_buf[i] = |
213 | 32.0k | pbi->valid_for_referencing[i]; |
214 | 32.0k | } |
215 | 16.0k | for (int i = 0; i < INTER_REFS_PER_FRAME; i++) { |
216 | 14.0k | pbi->stream_info[stream_idx].remapped_ref_idx_buf[i] = |
217 | 14.0k | cm->remapped_ref_idx[i]; |
218 | 14.0k | } |
219 | 34.0k | for (int i = 0; i < MAX_MFH_NUM; i++) { |
220 | 32.0k | pbi->stream_info[stream_idx].mfh_params_buf[i] = cm->mfh_params[i]; |
221 | 32.0k | } |
222 | | |
223 | | // Global OBUs (xlayer_id=31) excluded from per-layer save/restore |
224 | 2.00k | pbi->stream_info[stream_idx].lcr_counter_buf = pbi->lcr_counter; |
225 | 2.00k | pbi->stream_info[stream_idx].active_lcr_buf = pbi->active_lcr; |
226 | 2.00k | pbi->stream_info[stream_idx].lcr_params_buf = cm->lcr_params; |
227 | 2.00k | pbi->stream_info[stream_idx].global_lcr_params_buf = cm->global_lcr_params; |
228 | 2.00k | pbi->stream_info[stream_idx].active_atlas_segment_info_buf = |
229 | 2.00k | pbi->active_atlas_segment_info; |
230 | 2.00k | pbi->stream_info[stream_idx].ops_counter_buf = pbi->ops_counter; |
231 | | |
232 | 32.0k | for (int i = 0; i < NUM_CUSTOM_QMS; i++) { |
233 | 30.0k | pbi->stream_info[stream_idx].qm_list_buf[i] = pbi->qm_list[i]; |
234 | 30.0k | pbi->stream_info[stream_idx].qm_protected_buf[i] = pbi->qm_protected[i]; |
235 | 30.0k | } |
236 | 2.00k | pbi->stream_info[stream_idx].olk_encountered_buf = pbi->olk_encountered; |
237 | 2.00k | pbi->stream_info[stream_idx].random_access_point_index_buf = |
238 | 2.00k | pbi->random_access_point_index; |
239 | 2.00k | pbi->stream_info[stream_idx].random_access_point_count_buf = |
240 | 2.00k | pbi->random_access_point_count; |
241 | 18.0k | for (int i = 0; i < MAX_FGM_NUM; i++) { |
242 | 16.0k | pbi->stream_info[stream_idx].fgm_list_buf[i] = pbi->fgm_list[i]; |
243 | 16.0k | } |
244 | 2.00k | pbi->stream_info[stream_idx].prev_frame_buf = cm->prev_frame; |
245 | 2.00k | pbi->stream_info[stream_idx].last_frame_seg_map_buf = cm->last_frame_seg_map; |
246 | 18.0k | for (int i = 0; i < MAX_NUM_MLAYERS; i++) { |
247 | 16.0k | pbi->stream_info[stream_idx].ci_params_per_layer_buf[i] = |
248 | 16.0k | cm->ci_params_per_layer[i]; |
249 | 16.0k | } |
250 | 18.0k | for (int i = 0; i < MAX_NUM_MLAYERS; i++) { |
251 | 16.0k | pbi->stream_info[stream_idx].olk_refresh_frame_flags_buf[i] = |
252 | 16.0k | cm->olk_refresh_frame_flags[i]; |
253 | 16.0k | pbi->stream_info[stream_idx].olk_co_vcl_refresh_frame_flags_buf[i] = |
254 | 16.0k | cm->olk_co_vcl_refresh_frame_flags[i]; |
255 | 16.0k | } |
256 | 2.00k | pbi->stream_info[stream_idx].seq_params_buf = cm->seq_params; |
257 | 34.0k | for (int i = 0; i < MAX_MFH_NUM; i++) { |
258 | 32.0k | pbi->stream_info[stream_idx].mfh_valid_buf[i] = cm->mfh_valid[i]; |
259 | 32.0k | } |
260 | | |
261 | 2.00k | pbi->stream_info[stream_idx].decoding_first_frame = pbi->decoding_first_frame; |
262 | 2.00k | pbi->stream_info[stream_idx].last_olk_tu_display_order_hint = |
263 | 2.00k | pbi->last_olk_tu_display_order_hint; |
264 | 2.00k | pbi->stream_info[stream_idx].seen_frame_header_buf = pbi->seen_frame_header; |
265 | 2.00k | pbi->stream_info[stream_idx].next_start_tile_buf = pbi->next_start_tile; |
266 | 2.00k | pbi->stream_info[stream_idx].seen_vcl_obu_in_this_tu_buf = |
267 | 2.00k | pbi->seen_vcl_obu_in_this_tu; |
268 | 2.00k | } |
269 | | |
270 | | // Helper function to restore xlayer context |
271 | | void av2_restore_xlayer_context(AV2Decoder *pbi, AV2_COMMON *cm, |
272 | 3.20k | int xlayer_id) { |
273 | 3.20k | int stream_idx = av2_get_stream_index(cm, xlayer_id); |
274 | 3.20k | if (stream_idx < 0) return; // Invalid or GLOBAL_XLAYER_ID |
275 | | |
276 | | // Check if stream_info is valid |
277 | 2.07k | if (pbi->stream_info == NULL) { |
278 | 0 | return; |
279 | 0 | } |
280 | | |
281 | 35.2k | for (int i = 0; i < REF_FRAMES; i++) { |
282 | 33.1k | cm->ref_frame_map[i] = pbi->stream_info[stream_idx].ref_frame_map_buf[i]; |
283 | 33.1k | pbi->valid_for_referencing[i] = |
284 | 33.1k | pbi->stream_info[stream_idx].valid_for_referencing_buf[i]; |
285 | 33.1k | } |
286 | 16.5k | for (int i = 0; i < INTER_REFS_PER_FRAME; i++) { |
287 | 14.4k | cm->remapped_ref_idx[i] = |
288 | 14.4k | pbi->stream_info[stream_idx].remapped_ref_idx_buf[i]; |
289 | 14.4k | } |
290 | 35.2k | for (int i = 0; i < MAX_MFH_NUM; i++) { |
291 | 33.1k | cm->mfh_params[i] = pbi->stream_info[stream_idx].mfh_params_buf[i]; |
292 | 33.1k | } |
293 | | |
294 | | // Global OBUs (xlayer_id=31) excluded from per-layer save/restore |
295 | 2.07k | pbi->lcr_counter = pbi->stream_info[stream_idx].lcr_counter_buf; |
296 | 2.07k | pbi->active_lcr = pbi->stream_info[stream_idx].active_lcr_buf; |
297 | 2.07k | cm->lcr_params = pbi->stream_info[stream_idx].lcr_params_buf; |
298 | 2.07k | cm->global_lcr_params = pbi->stream_info[stream_idx].global_lcr_params_buf; |
299 | 2.07k | pbi->active_atlas_segment_info = |
300 | 2.07k | pbi->stream_info[stream_idx].active_atlas_segment_info_buf; |
301 | 2.07k | pbi->ops_counter = pbi->stream_info[stream_idx].ops_counter_buf; |
302 | 33.1k | for (int i = 0; i < NUM_CUSTOM_QMS; i++) { |
303 | 31.0k | pbi->qm_list[i] = pbi->stream_info[stream_idx].qm_list_buf[i]; |
304 | 31.0k | pbi->qm_protected[i] = pbi->stream_info[stream_idx].qm_protected_buf[i]; |
305 | 31.0k | } |
306 | 2.07k | pbi->olk_encountered = pbi->stream_info[stream_idx].olk_encountered_buf; |
307 | 2.07k | pbi->random_access_point_index = |
308 | 2.07k | pbi->stream_info[stream_idx].random_access_point_index_buf; |
309 | 2.07k | pbi->random_access_point_count = |
310 | 2.07k | pbi->stream_info[stream_idx].random_access_point_count_buf; |
311 | 18.6k | for (int i = 0; i < MAX_FGM_NUM; i++) { |
312 | 16.5k | pbi->fgm_list[i] = pbi->stream_info[stream_idx].fgm_list_buf[i]; |
313 | 16.5k | } |
314 | 2.07k | cm->prev_frame = pbi->stream_info[stream_idx].prev_frame_buf; |
315 | 2.07k | cm->last_frame_seg_map = pbi->stream_info[stream_idx].last_frame_seg_map_buf; |
316 | 18.6k | for (int i = 0; i < MAX_NUM_MLAYERS; i++) { |
317 | 16.5k | cm->ci_params_per_layer[i] = |
318 | 16.5k | pbi->stream_info[stream_idx].ci_params_per_layer_buf[i]; |
319 | 16.5k | } |
320 | 18.6k | for (int i = 0; i < MAX_NUM_MLAYERS; i++) { |
321 | 16.5k | cm->olk_refresh_frame_flags[i] = |
322 | 16.5k | pbi->stream_info[stream_idx].olk_refresh_frame_flags_buf[i]; |
323 | 16.5k | cm->olk_co_vcl_refresh_frame_flags[i] = |
324 | 16.5k | pbi->stream_info[stream_idx].olk_co_vcl_refresh_frame_flags_buf[i]; |
325 | 16.5k | } |
326 | 2.07k | cm->seq_params = pbi->stream_info[stream_idx].seq_params_buf; |
327 | 35.2k | for (int i = 0; i < MAX_MFH_NUM; i++) { |
328 | 33.1k | cm->mfh_valid[i] = pbi->stream_info[stream_idx].mfh_valid_buf[i]; |
329 | 33.1k | } |
330 | | |
331 | 2.07k | pbi->decoding_first_frame = pbi->stream_info[stream_idx].decoding_first_frame; |
332 | 2.07k | pbi->last_olk_tu_display_order_hint = |
333 | 2.07k | pbi->stream_info[stream_idx].last_olk_tu_display_order_hint; |
334 | 2.07k | pbi->seen_frame_header = pbi->stream_info[stream_idx].seen_frame_header_buf; |
335 | 2.07k | pbi->next_start_tile = pbi->stream_info[stream_idx].next_start_tile_buf; |
336 | 2.07k | pbi->seen_vcl_obu_in_this_tu = |
337 | 2.07k | pbi->stream_info[stream_idx].seen_vcl_obu_in_this_tu_buf; |
338 | 2.07k | } |
339 | | |
340 | 4.34k | static void init_stream_info(StreamInfo *stream_info) { |
341 | 4.34k | stream_info->olk_encountered_buf = 0; |
342 | 4.34k | stream_info->random_access_point_index_buf = -1; |
343 | 4.34k | stream_info->random_access_point_count_buf = 0; |
344 | 34.7k | for (int i = 0; i < INTER_REFS_PER_FRAME; ++i) { |
345 | 30.4k | stream_info->remapped_ref_idx_buf[i] = INVALID_IDX; |
346 | 30.4k | } |
347 | 73.8k | for (int i = 0; i < REF_FRAMES; i++) { |
348 | 69.5k | stream_info->ref_frame_map_buf[i] = NULL; |
349 | 69.5k | } |
350 | 4.34k | stream_info->mfh_valid_buf[0] = true; |
351 | 69.5k | for (int i = 1; i < MAX_MFH_NUM; i++) { |
352 | 65.1k | stream_info->mfh_valid_buf[i] = false; |
353 | 65.1k | } |
354 | 39.1k | for (int i = 0; i < MAX_NUM_MLAYERS; i++) { |
355 | 34.7k | av2_initialize_ci_params(&stream_info->ci_params_per_layer_buf[i]); |
356 | 34.7k | } |
357 | 4.34k | stream_info->decoding_first_frame = 1; |
358 | 4.34k | stream_info->last_olk_tu_display_order_hint = -1; |
359 | 4.34k | } |
360 | | |
361 | | /*! |
362 | | * \brief Save current MSDO configuration |
363 | | */ |
364 | 2.63k | static void save_msdo_config(const AV2Decoder *pbi, MsdoConfig *config) { |
365 | 2.63k | const AV2_COMMON *const cm = &pbi->common; |
366 | 2.63k | config->multistream_profile_idc = |
367 | 2.63k | pbi->common.msdo_params.multistream_profile_idc; |
368 | 2.63k | config->multistream_tier_idx = pbi->common.msdo_params.multistream_tier_idx; |
369 | 2.63k | config->multistream_level_idx = pbi->common.msdo_params.multistream_level_idx; |
370 | 2.63k | config->num_streams = cm->num_streams; |
371 | 2.63k | memcpy(config->stream_ids, cm->stream_ids, cm->num_streams * sizeof(int)); |
372 | 2.63k | } |
373 | | |
374 | | /*! |
375 | | * \brief Compare two MSDO configurations |
376 | | * \return true if configurations are identical, false otherwise |
377 | | */ |
378 | 1.19k | static bool msdo_config_equal(const MsdoConfig *a, const MsdoConfig *b) { |
379 | 1.19k | if (a->multistream_profile_idc != b->multistream_profile_idc) return false; |
380 | 310 | if (a->multistream_tier_idx != b->multistream_tier_idx) return false; |
381 | 277 | if (a->multistream_level_idx != b->multistream_level_idx) return false; |
382 | 274 | if (a->num_streams != b->num_streams) return false; |
383 | | |
384 | 274 | return true; |
385 | 274 | } |
386 | | |
387 | | avm_codec_err_t flush_all_xlayer_frames(struct AV2Decoder *pbi, AV2_COMMON *cm, |
388 | 921 | bool release_dpb) { |
389 | 921 | BufferPool *const pool = cm->buffer_pool; |
390 | 921 | int saved_xlayer_id = cm->xlayer_id; |
391 | 921 | avm_codec_err_t err = AVM_CODEC_OK; |
392 | | |
393 | 30.3k | for (int xlayer = 0; xlayer < AVM_MAX_NUM_STREAMS; xlayer++) { |
394 | 29.4k | if (xlayer == GLOBAL_XLAYER_ID) continue; // Global context is not a stream |
395 | 28.5k | if (pbi->xlayer_id_map[xlayer] > 0) { |
396 | 932 | if (xlayer != cm->xlayer_id) { |
397 | 930 | av2_store_xlayer_context(pbi, cm, cm->xlayer_id); |
398 | 930 | cm->xlayer_id = xlayer; |
399 | 930 | av2_restore_xlayer_context(pbi, cm, xlayer); |
400 | 930 | } |
401 | 932 | size_t num_before = pbi->num_output_frames; |
402 | 932 | err = flush_remaining_frames(pbi, INT_MAX); |
403 | 932 | if (err != AVM_CODEC_OK) break; |
404 | 932 | for (size_t j = num_before; j < pbi->num_output_frames; j++) { |
405 | 0 | decrease_ref_count(pbi->output_frames[j], pool); |
406 | 0 | } |
407 | 932 | if (release_dpb) { |
408 | 15.8k | for (int r = 0; r < REF_FRAMES; r++) { |
409 | 14.9k | decrease_ref_count(cm->ref_frame_map[r], pool); |
410 | 14.9k | cm->ref_frame_map[r] = NULL; |
411 | 14.9k | } |
412 | 932 | } |
413 | 932 | } |
414 | 28.5k | } |
415 | | |
416 | 921 | if (cm->xlayer_id != saved_xlayer_id) { |
417 | 900 | av2_store_xlayer_context(pbi, cm, cm->xlayer_id); |
418 | 900 | cm->xlayer_id = saved_xlayer_id; |
419 | 900 | av2_restore_xlayer_context(pbi, cm, saved_xlayer_id); |
420 | 900 | } |
421 | 921 | return err; |
422 | 921 | } |
423 | | |
424 | | static uint32_t read_multi_stream_decoder_operation_obu( |
425 | 1.45k | AV2Decoder *pbi, struct avm_read_bit_buffer *rb) { |
426 | 1.45k | AV2_COMMON *const cm = &pbi->common; |
427 | 1.45k | const uint32_t saved_bit_offset = rb->bit_offset; |
428 | | |
429 | | // Verify rb has been configured to report errors. |
430 | 1.45k | assert(rb->error_handler); |
431 | | |
432 | | // Save previous configuration if it exists |
433 | 1.45k | MsdoConfig prev_config; |
434 | 1.45k | bool has_previous = false; |
435 | | |
436 | 1.45k | if (pbi->stream_info != NULL) { |
437 | 1.33k | save_msdo_config(pbi, &prev_config); |
438 | 1.33k | has_previous = true; |
439 | 1.33k | } |
440 | | |
441 | 1.45k | const int num_streams = |
442 | 1.45k | avm_rb_read_literal(rb, 3) + 2; // read number of streams |
443 | 1.45k | if (num_streams > AVM_MAX_NUM_STREAMS) { |
444 | 0 | avm_internal_error( |
445 | 0 | &cm->error, AVM_CODEC_UNSUP_BITSTREAM, |
446 | 0 | "The number of streams cannot exceed the max value (4)."); |
447 | 0 | } |
448 | 1.45k | cm->num_streams = num_streams; |
449 | | |
450 | 1.45k | pbi->common.msdo_params.multistream_profile_idc = |
451 | 1.45k | avm_rb_read_literal(rb, PROFILE_BITS); // read profile of multistream |
452 | | |
453 | 1.45k | pbi->common.msdo_params.multistream_level_idx = |
454 | 1.45k | avm_rb_read_literal(rb, LEVEL_BITS); // read level of multistream |
455 | | |
456 | 1.45k | pbi->common.msdo_params.multistream_tier_idx = |
457 | 1.45k | avm_rb_read_bit(rb); // read tier of multistream |
458 | | |
459 | 1.45k | const int multistream_even_allocation_flag = |
460 | 1.45k | avm_rb_read_bit(rb); // read multistream_even_allocation_flag |
461 | | |
462 | 1.45k | if (!multistream_even_allocation_flag) { |
463 | 185 | const int multistream_large_picture_idc = |
464 | 185 | avm_rb_read_literal(rb, 3); // read multistream_large_picture_idc |
465 | 185 | (void)multistream_large_picture_idc; |
466 | 185 | } |
467 | | |
468 | 7.98k | for (int i = 0; i < num_streams; i++) { |
469 | 6.53k | cm->stream_ids[i] = avm_rb_read_literal(rb, XLAYER_BITS); // read stream ID |
470 | 6.53k | const int substream_profile_idc = |
471 | 6.53k | avm_rb_read_literal(rb, PROFILE_BITS); // read profile of multistream |
472 | 6.53k | (void)substream_profile_idc; |
473 | | |
474 | 6.53k | const int substream_level_idx = |
475 | 6.53k | avm_rb_read_literal(rb, LEVEL_BITS); // read level of multistream |
476 | 6.53k | (void)substream_level_idx; |
477 | | |
478 | 6.53k | const int substream_tier_idx = |
479 | 6.53k | avm_rb_read_bit(rb); // read tier of multistream |
480 | 6.53k | (void)substream_tier_idx; |
481 | 6.53k | } |
482 | | |
483 | 1.45k | cm->msdo_params.msdo_doh_constraint_flag = avm_rb_read_bit(rb); |
484 | | |
485 | | // Check if configuration changed |
486 | 1.45k | MsdoConfig new_config; |
487 | 1.45k | save_msdo_config(pbi, &new_config); |
488 | 1.45k | bool config_changed = |
489 | 1.45k | !has_previous || !msdo_config_equal(&prev_config, &new_config); |
490 | | |
491 | | // Flush remaining frames from all active streams before switching config |
492 | 1.45k | if (pbi->stream_info != NULL && config_changed) { |
493 | 919 | flush_all_xlayer_frames(pbi, cm, true); |
494 | 919 | avm_free(pbi->stream_info); |
495 | 919 | pbi->stream_info = NULL; |
496 | 919 | pbi->glcr_stream_info_num_allocated = 0; |
497 | | |
498 | | // Reset xlayer_id_map so only the new segment's layers are active |
499 | 30.3k | for (int i = 0; i < AVM_MAX_NUM_STREAMS; i++) { |
500 | 29.4k | pbi->xlayer_id_map[i] = 0; |
501 | 29.4k | } |
502 | 919 | } |
503 | | |
504 | | // Only allocate if stream_info is NULL (first time OR after freeing due to |
505 | | // config change) |
506 | 1.45k | if (pbi->stream_info == NULL) { |
507 | 1.02k | pbi->stream_info = |
508 | 1.02k | (StreamInfo *)avm_malloc(num_streams * sizeof(StreamInfo)); |
509 | 1.02k | if (pbi->stream_info == NULL) { |
510 | 0 | avm_internal_error(&cm->error, AVM_CODEC_MEM_ERROR, |
511 | 0 | "Memory allocation failed for pbi->stream_info\n"); |
512 | 0 | } |
513 | 1.02k | memset(pbi->stream_info, 0, num_streams * sizeof(StreamInfo)); |
514 | 5.37k | for (int i = 0; i < num_streams; i++) { |
515 | 4.34k | init_stream_info(&pbi->stream_info[i]); |
516 | 4.34k | } |
517 | 1.02k | } |
518 | | |
519 | 1.45k | pbi->msdo_is_present_in_tu = 1; |
520 | | |
521 | 1.45k | if (av2_check_trailing_bits(pbi, rb) != 0) { |
522 | 1.30k | return 0; |
523 | 1.30k | } |
524 | | |
525 | 158 | return ((rb->bit_offset - saved_bit_offset + 7) >> 3); |
526 | 1.45k | } |
527 | | |
528 | | // On success, returns the number of bytes read from 'rb'. |
529 | | // On failure, sets pbi->common.error.error_code and returns 0. |
530 | | static uint32_t read_sequence_header_obu(AV2Decoder *pbi, int xlayer_id, |
531 | | struct avm_read_bit_buffer *rb, |
532 | 22.1k | uint16_t *acc_sh_id_bitmap) { |
533 | 22.1k | AV2_COMMON *const cm = &pbi->common; |
534 | 22.1k | const uint32_t saved_bit_offset = rb->bit_offset; |
535 | | |
536 | | // Verify rb has been configured to report errors. |
537 | 22.1k | assert(rb->error_handler); |
538 | | |
539 | | // Use an element in the pbi->seq_list array to store the information as we |
540 | | // decode. At the end, if no errors have occurred, cm->seq_params is updated. |
541 | 22.1k | uint32_t seq_header_id = avm_rb_read_uvlc(rb); |
542 | 22.1k | if (seq_header_id >= MAX_SEQ_NUM) { |
543 | 3 | cm->error.error_code = AVM_CODEC_UNSUP_BITSTREAM; |
544 | 3 | return 0; |
545 | 3 | } |
546 | | |
547 | 22.1k | struct SequenceHeader *seq_params = &pbi->seq_list[xlayer_id][seq_header_id]; |
548 | 22.1k | seq_params->seq_header_id = seq_header_id; |
549 | 22.1k | seq_params->sh_from_leading = 0; |
550 | 22.1k | acc_sh_id_bitmap[xlayer_id] |= (1 << seq_header_id); |
551 | | |
552 | 22.1k | seq_params->seq_profile_idc = av2_read_profile(rb); |
553 | 22.1k | if (seq_params->seq_profile_idc >= MAX_PROFILES) { |
554 | 0 | cm->error.error_code = AVM_CODEC_UNSUP_BITSTREAM; |
555 | 0 | return 0; |
556 | 0 | } |
557 | | |
558 | 22.1k | seq_params->single_picture_header_flag = avm_rb_read_bit(rb); |
559 | 22.1k | if (!read_bitstream_level(&seq_params->seq_max_level_idx, rb)) { |
560 | 152 | cm->error.error_code = AVM_CODEC_UNSUP_BITSTREAM; |
561 | 152 | return 0; |
562 | 152 | } |
563 | 22.0k | if (seq_params->seq_max_level_idx >= SEQ_LEVEL_4_0 && |
564 | 22.0k | !seq_params->single_picture_header_flag) |
565 | 2.13k | seq_params->seq_tier = avm_rb_read_bit(rb); |
566 | 19.9k | else |
567 | 19.9k | seq_params->seq_tier = 0; |
568 | 22.0k | av2_read_chroma_format_bitdepth(rb, seq_params, &cm->error); |
569 | 22.0k | if (seq_params->single_picture_header_flag) { |
570 | 19.9k | seq_params->seq_lcr_id = LCR_ID_UNSPECIFIED; |
571 | 19.9k | seq_params->still_picture = 1; |
572 | 19.9k | seq_params->max_tlayer_id = 0; |
573 | 19.9k | seq_params->max_mlayer_id = 0; |
574 | 19.9k | seq_params->seq_max_mlayer_cnt = 1; |
575 | 19.9k | seq_params->monotonic_output_order_flag = 1; |
576 | 19.9k | } else { |
577 | 2.13k | int seq_lcr_id = avm_rb_read_literal(rb, 3); |
578 | 2.13k | if (seq_lcr_id > MAX_NUM_SEQ_LCR_ID) { |
579 | 0 | avm_internal_error(&cm->error, AVM_CODEC_UNSUP_BITSTREAM, |
580 | 0 | "Unsupported LCR id in the Sequence Header.\n"); |
581 | 0 | } |
582 | 2.13k | seq_params->seq_lcr_id = seq_lcr_id; |
583 | 2.13k | seq_params->still_picture = avm_rb_read_bit(rb); |
584 | 2.13k | seq_params->max_tlayer_id = avm_rb_read_literal(rb, TLAYER_BITS); |
585 | 2.13k | seq_params->max_mlayer_id = avm_rb_read_literal(rb, MLAYER_BITS); |
586 | 2.13k | if (seq_params->max_mlayer_id > 0) { |
587 | 1.84k | int n = avm_ceil_log2(seq_params->max_mlayer_id + 1); |
588 | 1.84k | int seq_max_mlayer_cnt_minus_1 = avm_rb_read_literal(rb, n); |
589 | 1.84k | if (seq_max_mlayer_cnt_minus_1 > seq_params->max_mlayer_id) { |
590 | 37 | avm_internal_error( |
591 | 37 | &cm->error, AVM_CODEC_UNSUP_BITSTREAM, |
592 | 37 | "seq_max_mlayer_cnt_minus_1 %d is greater than max_mlayer_id %d", |
593 | 37 | seq_max_mlayer_cnt_minus_1, seq_params->max_mlayer_id); |
594 | 37 | } |
595 | 1.84k | seq_params->seq_max_mlayer_cnt = seq_max_mlayer_cnt_minus_1 + 1; |
596 | 1.84k | } else { |
597 | 290 | seq_params->seq_max_mlayer_cnt = 1; |
598 | 290 | } |
599 | 2.13k | seq_params->monotonic_output_order_flag = avm_rb_read_bit(rb); |
600 | 2.13k | } |
601 | | |
602 | 22.0k | if (!av2_check_profile_interop_conformance(seq_params, &cm->error, 1)) { |
603 | 87 | avm_internal_error(&cm->error, AVM_CODEC_UNSUP_BITSTREAM, |
604 | 87 | "Unsupported profile, bitdepth, chroma format or number " |
605 | 87 | "of embedded layers"); |
606 | 87 | } |
607 | | |
608 | 22.0k | const int num_bits_width = avm_rb_read_literal(rb, 4) + 1; |
609 | 22.0k | const int num_bits_height = avm_rb_read_literal(rb, 4) + 1; |
610 | 22.0k | const int max_frame_width = avm_rb_read_literal(rb, num_bits_width) + 1; |
611 | 22.0k | const int max_frame_height = avm_rb_read_literal(rb, num_bits_height) + 1; |
612 | | |
613 | 22.0k | seq_params->num_bits_width = num_bits_width; |
614 | 22.0k | seq_params->num_bits_height = num_bits_height; |
615 | 22.0k | seq_params->max_frame_width = max_frame_width; |
616 | 22.0k | seq_params->max_frame_height = max_frame_height; |
617 | | |
618 | 22.0k | av2_read_conformance_window(rb, seq_params); |
619 | 22.0k | av2_validate_seq_conformance_window(seq_params, &cm->error); |
620 | | |
621 | 22.0k | if (seq_params->single_picture_header_flag) { |
622 | 19.8k | seq_params->decoder_model_info_present_flag = 0; |
623 | 19.8k | seq_params->display_model_info_present_flag = 0; |
624 | 19.8k | } else { |
625 | 2.14k | seq_params->seq_max_display_model_info_present_flag = avm_rb_read_bit(rb); |
626 | 2.14k | seq_params->seq_max_initial_display_delay_minus_1 = |
627 | 2.14k | BUFFER_POOL_MAX_SIZE - 1; |
628 | 2.14k | if (seq_params->seq_max_display_model_info_present_flag) |
629 | 299 | seq_params->seq_max_initial_display_delay_minus_1 = |
630 | 299 | avm_rb_read_literal(rb, 4); |
631 | 2.14k | seq_params->decoder_model_info_present_flag = avm_rb_read_bit(rb); |
632 | 2.14k | if (seq_params->decoder_model_info_present_flag) { |
633 | 1.66k | seq_params->decoder_model_info.num_units_in_decoding_tick = |
634 | 1.66k | avm_rb_read_unsigned_literal(rb, 32); |
635 | 1.66k | seq_params->seq_max_decoder_model_present_flag = avm_rb_read_bit(rb); |
636 | 1.66k | if (seq_params->seq_max_decoder_model_present_flag) { |
637 | 1 | seq_params->seq_max_decoder_buffer_delay = avm_rb_read_uvlc(rb); |
638 | 1 | seq_params->seq_max_encoder_buffer_delay = avm_rb_read_uvlc(rb); |
639 | 1 | seq_params->seq_max_low_delay_mode_flag = avm_rb_read_bit(rb); |
640 | 1.66k | } else { |
641 | 1.66k | seq_params->seq_max_decoder_buffer_delay = 70000; |
642 | 1.66k | seq_params->seq_max_encoder_buffer_delay = 20000; |
643 | 1.66k | seq_params->seq_max_low_delay_mode_flag = 0; |
644 | 1.66k | } |
645 | 1.66k | } else { |
646 | 482 | seq_params->decoder_model_info.num_units_in_decoding_tick = 1; |
647 | 482 | seq_params->seq_max_decoder_buffer_delay = 70000; |
648 | 482 | seq_params->seq_max_encoder_buffer_delay = 20000; |
649 | 482 | seq_params->seq_max_low_delay_mode_flag = 0; |
650 | 482 | } |
651 | | // Configurable profile does not define bitrate and buffer size constraints |
652 | 2.14k | if (seq_params->seq_profile_idc != CONFIGURABLE) { |
653 | 1.98k | int64_t seq_bitrate = av2_max_level_bitrate(seq_params->seq_profile_idc, |
654 | 1.98k | seq_params->seq_max_level_idx, |
655 | 1.98k | seq_params->seq_tier); |
656 | 1.98k | if (seq_bitrate == 0) |
657 | 0 | avm_internal_error(&cm->error, AVM_CODEC_UNSUP_BITSTREAM, |
658 | 0 | "AV2 does not support this combination of " |
659 | 0 | "profile, level, and tier."); |
660 | | // Buffer size in bits/s is bitrate in bits/s * 1 s |
661 | 1.98k | int64_t buffer_size = seq_bitrate; |
662 | 1.98k | if (buffer_size == 0) |
663 | 0 | avm_internal_error(&cm->error, AVM_CODEC_UNSUP_BITSTREAM, |
664 | 0 | "AV2 does not support this combination of " |
665 | 0 | "profile, level, and tier."); |
666 | 1.98k | } |
667 | 2.14k | } |
668 | | |
669 | | // setup default embedded layer dependency |
670 | 22.0k | setup_default_embedded_layer_dependency_structure(seq_params); |
671 | | // setup default temporal layer dependency |
672 | 22.0k | setup_default_temporal_layer_dependency_structure(seq_params); |
673 | | |
674 | | // mlayer dependency description |
675 | 22.0k | seq_params->mlayer_dependency_present_flag = 0; |
676 | 22.0k | if (seq_params->max_mlayer_id > 0) { |
677 | 1.71k | seq_params->mlayer_dependency_present_flag = avm_rb_read_bit(rb); |
678 | 1.71k | if (seq_params->mlayer_dependency_present_flag) { |
679 | 1.40k | av2_read_mlayer_dependency_info(seq_params, rb); |
680 | 1.40k | } |
681 | 1.71k | } |
682 | | |
683 | | // tlayer dependency description |
684 | 22.0k | seq_params->tlayer_dependency_present_flag = 0; |
685 | 22.0k | seq_params->multi_tlayer_dependency_map_present_flag = 0; |
686 | 22.0k | if (seq_params->max_tlayer_id > 0) { |
687 | 1.98k | seq_params->tlayer_dependency_present_flag = avm_rb_read_bit(rb); |
688 | 1.98k | if (seq_params->tlayer_dependency_present_flag) { |
689 | 267 | if (seq_params->max_mlayer_id > 0) { |
690 | 1 | seq_params->multi_tlayer_dependency_map_present_flag = |
691 | 1 | avm_rb_read_bit(rb); |
692 | 1 | } |
693 | 267 | av2_read_tlayer_dependency_info(seq_params, rb); |
694 | 267 | } |
695 | 1.98k | } |
696 | | |
697 | 22.0k | av2_read_sequence_header(rb, seq_params); |
698 | | |
699 | 22.0k | seq_params->film_grain_params_present = avm_rb_read_bit(rb); |
700 | | |
701 | 22.0k | size_t bits_before_ext = rb->bit_offset - saved_bit_offset; |
702 | 22.0k | seq_params->seq_extension_present_flag = avm_rb_read_bit(rb); |
703 | 22.0k | if (seq_params->seq_extension_present_flag) { |
704 | | // Extension data bits = total - bits_read_before_extension -1 (ext flag) - |
705 | | // trailing bits |
706 | 21.6k | int extension_bits = read_obu_extension_bits( |
707 | 21.6k | rb->bit_buffer, rb->bit_buffer_end - rb->bit_buffer, bits_before_ext, |
708 | 21.6k | &cm->error); |
709 | 21.6k | if (extension_bits > 0) { |
710 | | // skip over the extension bits |
711 | 21.6k | rb->bit_offset += extension_bits; |
712 | 21.6k | } else { |
713 | | // No extension data is present |
714 | 1 | } |
715 | 21.6k | } |
716 | | |
717 | 22.0k | if (av2_check_trailing_bits(pbi, rb) != 0) { |
718 | | // cm->error.error_code is already set. |
719 | 64 | return 0; |
720 | 64 | } |
721 | 21.9k | return ((rb->bit_offset - saved_bit_offset + 7) >> 3); |
722 | 22.0k | } |
723 | | |
724 | | static uint32_t read_multi_frame_header_obu(AV2Decoder *pbi, |
725 | | uint32_t *acc_mfh_id_bitmap, |
726 | 6 | struct avm_read_bit_buffer *rb) { |
727 | 6 | AV2_COMMON *const cm = &pbi->common; |
728 | 6 | const uint32_t saved_bit_offset = rb->bit_offset; |
729 | | |
730 | 6 | const uint32_t cur_mfh_id = av2_read_multi_frame_header(cm, rb); |
731 | 6 | assert(cur_mfh_id < MAX_MFH_NUM); |
732 | 6 | if (acc_mfh_id_bitmap) { |
733 | 2 | *acc_mfh_id_bitmap |= (1 << cur_mfh_id); |
734 | 2 | } |
735 | | |
736 | 6 | size_t bits_before_ext = rb->bit_offset - saved_bit_offset; |
737 | 6 | cm->mfh_params[cur_mfh_id].mfh_extension_present_flag = avm_rb_read_bit(rb); |
738 | 6 | if (cm->mfh_params[cur_mfh_id].mfh_extension_present_flag) { |
739 | | // Extension data bits = total - bits_read_before_extension -1 (ext flag) - |
740 | | // trailing bits |
741 | 2 | int extension_bits = read_obu_extension_bits( |
742 | 2 | rb->bit_buffer, rb->bit_buffer_end - rb->bit_buffer, bits_before_ext, |
743 | 2 | &cm->error); |
744 | 2 | if (extension_bits > 0) { |
745 | | // skip over the extension bits |
746 | 2 | rb->bit_offset += extension_bits; |
747 | 2 | } else { |
748 | | // No extension data present |
749 | 0 | } |
750 | 2 | } |
751 | | |
752 | 6 | if (av2_check_trailing_bits(pbi, rb) != 0) { |
753 | | // cm->error.error_code is already set. |
754 | 0 | return 0; |
755 | 0 | } |
756 | | |
757 | 6 | return ((rb->bit_offset - saved_bit_offset + 7) >> 3); |
758 | 6 | } |
759 | | |
760 | | static uint32_t read_tilegroup_obu(AV2Decoder *pbi, |
761 | | struct avm_read_bit_buffer *rb, |
762 | | const uint8_t *data, const uint8_t *data_end, |
763 | | const uint8_t **p_data_end, |
764 | | OBU_TYPE obu_type, int obu_xlayer_id, |
765 | 18.4k | int *is_first_tg, int *is_last_tg) { |
766 | 18.4k | AV2_COMMON *const cm = &pbi->common; |
767 | 18.4k | int start_tile, end_tile; |
768 | 18.4k | int32_t header_size, tg_payload_size; |
769 | | |
770 | 18.4k | assert(rb->bit_offset == 0); |
771 | 18.4k | assert(rb->bit_buffer == data); |
772 | 18.4k | *is_first_tg = 1; // it is updated by av2_read_tilegroup_header() |
773 | 18.4k | header_size = av2_read_tilegroup_header(pbi, rb, data, p_data_end, |
774 | 18.4k | is_first_tg, &start_tile, &end_tile, |
775 | 18.4k | obu_type, obu_xlayer_id); |
776 | | |
777 | 18.4k | bool skip_payload = false; |
778 | 18.4k | skip_payload |= (obu_type == OBU_LEADING_SEF); |
779 | 18.4k | skip_payload |= (obu_type == OBU_REGULAR_SEF); |
780 | 18.4k | skip_payload |= (obu_type == OBU_LEADING_TIP); |
781 | 18.4k | skip_payload |= (obu_type == OBU_REGULAR_TIP); |
782 | 18.4k | skip_payload |= cm->bru.frame_inactive_flag; |
783 | 18.4k | skip_payload |= cm->bridge_frame_info.is_bridge_frame; |
784 | | |
785 | 18.4k | if (skip_payload) { |
786 | 0 | *is_last_tg = 1; |
787 | 0 | tg_payload_size = 0; |
788 | 0 | if (av2_check_trailing_bits(pbi, rb) != 0) { |
789 | | // cm->error.error_code is already set. |
790 | 0 | return 0; |
791 | 0 | } |
792 | 0 | header_size = (int32_t)avm_rb_bytes_read(rb); |
793 | 18.4k | } else { |
794 | 18.4k | if (av2_check_byte_alignment(cm, rb)) return 0; |
795 | 17.8k | data += header_size; |
796 | | |
797 | 17.8k | av2_decode_tg_tiles_and_wrapup(pbi, data, data_end, p_data_end, start_tile, |
798 | 17.8k | end_tile, *is_first_tg); |
799 | | |
800 | 17.8k | tg_payload_size = (uint32_t)(*p_data_end - data); |
801 | 17.8k | *is_last_tg = end_tile == cm->tiles.rows * cm->tiles.cols - 1; |
802 | 17.8k | } |
803 | 17.8k | return header_size + tg_payload_size; |
804 | 18.4k | } |
805 | | |
806 | | // Returns the last nonzero byte index in 'data'. If there is no nonzero byte in |
807 | | // 'data', returns -1. |
808 | 0 | static int get_last_nonzero_byte_index(const uint8_t *data, size_t sz) { |
809 | | // Scan backward and return on the first nonzero byte. |
810 | 0 | int i = (int)sz - 1; |
811 | 0 | while (i >= 0 && data[i] == 0) { |
812 | 0 | --i; |
813 | 0 | } |
814 | 0 | return i; |
815 | 0 | } |
816 | | |
817 | | // Allocates metadata that was read and adds it to the decoders metadata array. |
818 | | static void alloc_read_metadata(AV2Decoder *const pbi, |
819 | | OBU_METADATA_TYPE metadata_type, |
820 | | const uint8_t *data, size_t sz, |
821 | 1 | avm_metadata_insert_flags_t insert_flag) { |
822 | 1 | AV2_COMMON *const cm = &pbi->common; |
823 | 1 | if (!pbi->metadata) { |
824 | 1 | pbi->metadata = avm_img_metadata_array_alloc(0); |
825 | 1 | if (!pbi->metadata) { |
826 | 0 | avm_internal_error(&cm->error, AVM_CODEC_MEM_ERROR, |
827 | 0 | "Failed to allocate metadata array"); |
828 | 0 | } |
829 | 1 | } |
830 | 1 | avm_metadata_t *metadata = |
831 | 1 | avm_img_metadata_alloc(metadata_type, data, sz, insert_flag); |
832 | 1 | if (!metadata) { |
833 | 0 | avm_internal_error(&cm->error, AVM_CODEC_MEM_ERROR, |
834 | 0 | "Error allocating metadata"); |
835 | 0 | } |
836 | 1 | avm_metadata_t **metadata_array = |
837 | 1 | (avm_metadata_t **)realloc(pbi->metadata->metadata_array, |
838 | 1 | (pbi->metadata->sz + 1) * sizeof(metadata)); |
839 | 1 | if (!metadata_array) { |
840 | 0 | avm_img_metadata_free(metadata); |
841 | 0 | avm_internal_error(&cm->error, AVM_CODEC_MEM_ERROR, |
842 | 0 | "Error growing metadata array"); |
843 | 0 | } |
844 | 1 | pbi->metadata->metadata_array = metadata_array; |
845 | 1 | pbi->metadata->metadata_array[pbi->metadata->sz] = metadata; |
846 | 1 | pbi->metadata->sz++; |
847 | 1 | } |
848 | | |
849 | | // On failure, calls avm_internal_error() and does not return. |
850 | | static void read_metadata_itut_t35(AV2Decoder *const pbi, const uint8_t *data, |
851 | 0 | size_t sz) { |
852 | 0 | AV2_COMMON *const cm = &pbi->common; |
853 | 0 | if (sz == 0) { |
854 | 0 | avm_internal_error(&cm->error, AVM_CODEC_CORRUPT_FRAME, |
855 | 0 | "itu_t_t35_country_code is missing"); |
856 | 0 | } |
857 | 0 | int country_code_size = 1; |
858 | 0 | if (*data == 0xFF) { |
859 | 0 | if (sz == 1) { |
860 | 0 | avm_internal_error(&cm->error, AVM_CODEC_CORRUPT_FRAME, |
861 | 0 | "itu_t_t35_country_code_extension_byte is missing"); |
862 | 0 | } |
863 | 0 | ++country_code_size; |
864 | 0 | } |
865 | 0 | const int end_index = (int)sz; |
866 | 0 | if (end_index < country_code_size) { |
867 | 0 | avm_internal_error(&cm->error, AVM_CODEC_CORRUPT_FRAME, |
868 | 0 | "No trailing bits found in ITU-T T.35 metadata OBU"); |
869 | 0 | } |
870 | 0 | alloc_read_metadata(pbi, OBU_METADATA_TYPE_ITUT_T35, data, end_index, |
871 | 0 | AVM_MIF_ANY_FRAME); |
872 | 0 | } |
873 | | |
874 | | // On failure, calls avm_internal_error() and does not return. |
875 | | static void read_metadata_itut_t35_short(AV2Decoder *const pbi, |
876 | 0 | const uint8_t *data, size_t sz) { |
877 | 0 | AV2_COMMON *const cm = &pbi->common; |
878 | 0 | if (sz == 0) { |
879 | 0 | avm_internal_error(&cm->error, AVM_CODEC_CORRUPT_FRAME, |
880 | 0 | "itu_t_t35_country_code is missing"); |
881 | 0 | } |
882 | 0 | int country_code_size = 1; |
883 | 0 | if (*data == 0xFF) { |
884 | 0 | if (sz == 1) { |
885 | 0 | avm_internal_error(&cm->error, AVM_CODEC_CORRUPT_FRAME, |
886 | 0 | "itu_t_t35_country_code_extension_byte is missing"); |
887 | 0 | } |
888 | 0 | ++country_code_size; |
889 | 0 | } |
890 | 0 | int end_index = get_last_nonzero_byte_index(data, sz); |
891 | 0 | if (end_index < country_code_size) { |
892 | 0 | avm_internal_error(&cm->error, AVM_CODEC_CORRUPT_FRAME, |
893 | 0 | "No trailing bits found in ITU-T T.35 metadata OBU"); |
894 | 0 | } |
895 | | // itu_t_t35_payload_bytes is byte aligned. Section 6.7.2 of the spec says: |
896 | | // itu_t_t35_payload_bytes shall be bytes containing data registered as |
897 | | // specified in Recommendation ITU-T T.35. |
898 | | // Therefore the first trailing byte should be 0x80. |
899 | 0 | if (data[end_index] != 0x80) { |
900 | 0 | avm_internal_error(&cm->error, AVM_CODEC_CORRUPT_FRAME, |
901 | 0 | "The last nonzero byte of the ITU-T T.35 metadata OBU " |
902 | 0 | "is 0x%02x, should be 0x80.", |
903 | 0 | data[end_index]); |
904 | 0 | } |
905 | |
|
906 | 0 | alloc_read_metadata(pbi, OBU_METADATA_TYPE_ITUT_T35, data, end_index, |
907 | 0 | AVM_MIF_ANY_FRAME); |
908 | 0 | } |
909 | | // On success, returns the number of bytes read from 'data'. On failure, calls |
910 | | // avm_internal_error() and does not return. |
911 | | static size_t read_metadata_hdr_cll(AV2Decoder *const pbi, const uint8_t *data, |
912 | 0 | size_t sz) { |
913 | 0 | const size_t kHdrCllPayloadSize = 4; |
914 | 0 | AV2_COMMON *const cm = &pbi->common; |
915 | 0 | if (sz < kHdrCllPayloadSize) { |
916 | 0 | avm_internal_error(&cm->error, AVM_CODEC_CORRUPT_FRAME, |
917 | 0 | "Incorrect HDR CLL metadata payload size"); |
918 | 0 | } |
919 | 0 | alloc_read_metadata(pbi, OBU_METADATA_TYPE_HDR_CLL, data, kHdrCllPayloadSize, |
920 | 0 | AVM_MIF_ANY_FRAME); |
921 | 0 | return kHdrCllPayloadSize; |
922 | 0 | } |
923 | | |
924 | | // On success, returns the number of bytes read from 'data'. On failure, calls |
925 | | // avm_internal_error() and does not return. |
926 | | static size_t read_metadata_hdr_mdcv(AV2Decoder *const pbi, const uint8_t *data, |
927 | 0 | size_t sz) { |
928 | 0 | const size_t kMdcvPayloadSize = 24; |
929 | 0 | AV2_COMMON *const cm = &pbi->common; |
930 | 0 | if (sz < kMdcvPayloadSize) { |
931 | 0 | avm_internal_error(&cm->error, AVM_CODEC_CORRUPT_FRAME, |
932 | 0 | "Incorrect HDR MDCV metadata payload size"); |
933 | 0 | } |
934 | 0 | alloc_read_metadata(pbi, OBU_METADATA_TYPE_HDR_MDCV, data, kMdcvPayloadSize, |
935 | 0 | AVM_MIF_ANY_FRAME); |
936 | 0 | return kMdcvPayloadSize; |
937 | 0 | } |
938 | | |
939 | | // On success, returns the number of bytes read from 'data'. On failure, calls |
940 | | // avm_internal_error() and does not return. |
941 | | static size_t read_metadata_banding_hints(AV2Decoder *const pbi, |
942 | 1 | const uint8_t *data, size_t sz) { |
943 | 1 | AV2_COMMON *const cm = &pbi->common; |
944 | | |
945 | | // Validate minimum payload size (at least 3 bits for basic flags) |
946 | 1 | if (sz == 0) { |
947 | 0 | avm_internal_error(&cm->error, AVM_CODEC_CORRUPT_FRAME, |
948 | 0 | "Empty banding hints metadata payload"); |
949 | 0 | } |
950 | | |
951 | | // Decode the banding hints metadata payload |
952 | 1 | if (avm_decode_banding_hints_metadata(data, sz, &pbi->band_metadata) == 0) { |
953 | | // Successfully decoded |
954 | 1 | pbi->band_metadata_present = 1; |
955 | 1 | } else { |
956 | 0 | pbi->band_metadata_present = 0; |
957 | 0 | avm_internal_error(&cm->error, AVM_CODEC_CORRUPT_FRAME, |
958 | 0 | "Failed to decode banding hints metadata"); |
959 | 0 | } |
960 | | |
961 | 1 | alloc_read_metadata(pbi, OBU_METADATA_TYPE_BANDING_HINTS, data, sz, |
962 | 1 | AVM_MIF_ANY_FRAME); |
963 | | |
964 | 1 | return sz; |
965 | 1 | } |
966 | | |
967 | | // Helper function to read banding hints from a bit buffer (short metadata path) |
968 | | static void read_metadata_banding_hints_from_rb( |
969 | 0 | AV2Decoder *const pbi, struct avm_read_bit_buffer *rb) { |
970 | 0 | avm_banding_hints_metadata_t *md = &pbi->band_metadata; |
971 | 0 | memset(md, 0, sizeof(*md)); |
972 | |
|
973 | 0 | md->coding_banding_present_flag = avm_rb_read_bit(rb); |
974 | 0 | md->source_banding_present_flag = avm_rb_read_bit(rb); |
975 | |
|
976 | 0 | if (md->coding_banding_present_flag) { |
977 | 0 | md->banding_hints_flag = avm_rb_read_bit(rb); |
978 | |
|
979 | 0 | if (md->banding_hints_flag) { |
980 | 0 | md->three_color_components = avm_rb_read_bit(rb); |
981 | 0 | const int num_components = md->three_color_components ? 3 : 1; |
982 | |
|
983 | 0 | for (int plane = 0; plane < num_components; plane++) { |
984 | 0 | md->banding_in_component_present_flag[plane] = avm_rb_read_bit(rb); |
985 | 0 | if (md->banding_in_component_present_flag[plane]) { |
986 | 0 | md->max_band_width_minus4[plane] = avm_rb_read_literal(rb, 6); |
987 | 0 | md->max_band_step_minus1[plane] = avm_rb_read_literal(rb, 4); |
988 | 0 | } |
989 | 0 | } |
990 | |
|
991 | 0 | md->band_units_information_present_flag = avm_rb_read_bit(rb); |
992 | 0 | if (md->band_units_information_present_flag) { |
993 | 0 | md->num_band_units_rows_minus_1 = avm_rb_read_literal(rb, 5); |
994 | 0 | md->num_band_units_cols_minus_1 = avm_rb_read_literal(rb, 5); |
995 | 0 | md->varying_size_band_units_flag = avm_rb_read_bit(rb); |
996 | |
|
997 | 0 | if (md->varying_size_band_units_flag) { |
998 | 0 | md->band_block_in_luma_samples = avm_rb_read_literal(rb, 3); |
999 | |
|
1000 | 0 | for (int r = 0; r <= md->num_band_units_rows_minus_1; r++) { |
1001 | 0 | md->vert_size_in_band_blocks_minus1[r] = avm_rb_read_literal(rb, 5); |
1002 | 0 | } |
1003 | |
|
1004 | 0 | for (int c = 0; c <= md->num_band_units_cols_minus_1; c++) { |
1005 | 0 | md->horz_size_in_band_blocks_minus1[c] = avm_rb_read_literal(rb, 5); |
1006 | 0 | } |
1007 | 0 | } |
1008 | |
|
1009 | 0 | for (int r = 0; r <= md->num_band_units_rows_minus_1; r++) { |
1010 | 0 | for (int c = 0; c <= md->num_band_units_cols_minus_1; c++) { |
1011 | 0 | md->banding_in_band_unit_present_flag[r][c] = avm_rb_read_bit(rb); |
1012 | 0 | } |
1013 | 0 | } |
1014 | 0 | } |
1015 | 0 | } |
1016 | 0 | } |
1017 | |
|
1018 | 0 | pbi->band_metadata_present = 1; |
1019 | | |
1020 | | // Re-encode to raw payload and store in metadata array |
1021 | 0 | uint8_t payload_buf[256]; |
1022 | 0 | size_t payload_size = sizeof(payload_buf); |
1023 | 0 | if (avm_encode_banding_hints_metadata(md, payload_buf, &payload_size) == 0) { |
1024 | 0 | alloc_read_metadata(pbi, OBU_METADATA_TYPE_BANDING_HINTS, payload_buf, |
1025 | 0 | payload_size, AVM_MIF_ANY_FRAME); |
1026 | 0 | } |
1027 | 0 | } |
1028 | | |
1029 | | // On success, returns the number of bytes read from 'data'. On failure, calls |
1030 | | // avm_internal_error() and does not return. |
1031 | | static size_t read_metadata_icc_profile(AV2Decoder *const pbi, |
1032 | 0 | const uint8_t *data, size_t sz) { |
1033 | 0 | const size_t kMinIccProfileHeaderSize = 128; |
1034 | 0 | AV2_COMMON *const cm = &pbi->common; |
1035 | 0 | if (sz < kMinIccProfileHeaderSize) { |
1036 | 0 | avm_internal_error(&cm->error, AVM_CODEC_CORRUPT_FRAME, |
1037 | 0 | "Incorrect ICC profile metadata payload size"); |
1038 | 0 | } |
1039 | 0 | alloc_read_metadata(pbi, OBU_METADATA_TYPE_ICC_PROFILE, data, sz, |
1040 | 0 | AVM_MIF_ANY_FRAME); |
1041 | 0 | return sz; |
1042 | 0 | } |
1043 | | |
1044 | | // On failure, calls avm_internal_error() and does not return. |
1045 | | static void read_metadata_user_data_unregistered(AV2Decoder *const pbi, |
1046 | | const uint8_t *data, |
1047 | 0 | size_t sz) { |
1048 | 0 | AV2_COMMON *const cm = &pbi->common; |
1049 | | // uuid_iso_iec_11578 is 128 bits (16 bytes) |
1050 | 0 | const size_t uuid_size = 16; |
1051 | 0 | if (sz < uuid_size) { |
1052 | 0 | avm_internal_error(&cm->error, AVM_CODEC_CORRUPT_FRAME, |
1053 | 0 | "uuid_iso_iec_11578 is missing or incomplete"); |
1054 | 0 | } |
1055 | 0 | const int end_index = (int)sz; |
1056 | 0 | alloc_read_metadata(pbi, OBU_METADATA_TYPE_USER_DATA_UNREGISTERED, data, |
1057 | 0 | end_index, AVM_MIF_ANY_FRAME); |
1058 | 0 | } |
1059 | | |
1060 | | // On success, returns the number of bytes read from 'data'. On failure, calls |
1061 | | // avm_internal_error() and does not return. |
1062 | | static void read_metadata_scan_type(AV2Decoder *const pbi, |
1063 | 0 | struct avm_read_bit_buffer *rb) { |
1064 | 0 | AV2_COMMON *const cm = &pbi->common; |
1065 | 0 | cm->pic_struct_metadata_params.mps_pic_struct_type = |
1066 | 0 | avm_rb_read_literal(rb, 5); |
1067 | 0 | cm->pic_struct_metadata_params.mps_source_scan_type_idc = |
1068 | 0 | avm_rb_read_literal(rb, 2); |
1069 | 0 | cm->pic_struct_metadata_params.mps_duplicate_flag = avm_rb_read_bit(rb); |
1070 | |
|
1071 | 0 | uint8_t payload[1]; |
1072 | 0 | payload[0] = (cm->pic_struct_metadata_params.mps_pic_struct_type << 3) | |
1073 | 0 | (cm->pic_struct_metadata_params.mps_source_scan_type_idc << 1) | |
1074 | 0 | cm->pic_struct_metadata_params.mps_duplicate_flag; |
1075 | 0 | alloc_read_metadata(pbi, OBU_METADATA_TYPE_SCAN_TYPE, payload, 1, |
1076 | 0 | AVM_MIF_ANY_FRAME); |
1077 | 0 | } |
1078 | | |
1079 | | // On success, returns the number of bytes read from 'data'. On failure, calls |
1080 | | // avm_internal_error() and does not return. |
1081 | | static void read_metadata_temporal_point_info(AV2Decoder *const pbi, |
1082 | 0 | struct avm_read_bit_buffer *rb) { |
1083 | 0 | AV2_COMMON *const cm = &pbi->common; |
1084 | 0 | cm->temporal_point_info_metadata.mtpi_frame_presentation_time = |
1085 | 0 | avm_rb_read_uleb(rb); |
1086 | 0 | uint8_t payload[1]; |
1087 | 0 | payload[0] = |
1088 | 0 | (cm->temporal_point_info_metadata.mtpi_frame_presentation_time & 0XFF); |
1089 | 0 | alloc_read_metadata(pbi, OBU_METADATA_TYPE_TEMPORAL_POINT_INFO, payload, 1, |
1090 | 0 | AVM_MIF_ANY_FRAME); |
1091 | 0 | } |
1092 | | |
1093 | | static int read_metadata_frame_hash(AV2Decoder *const pbi, |
1094 | 104 | struct avm_read_bit_buffer *rb) { |
1095 | 104 | AV2_COMMON *const cm = &pbi->common; |
1096 | 104 | const unsigned hash_type = avm_rb_read_literal(rb, 4); |
1097 | 104 | const unsigned per_plane = avm_rb_read_bit(rb); |
1098 | 104 | const unsigned has_grain = avm_rb_read_bit(rb); |
1099 | 104 | const unsigned is_monochrome = avm_rb_read_bit(rb); |
1100 | 104 | avm_rb_read_literal(rb, 1); // reserved |
1101 | | |
1102 | | // If hash_type is reserved for future use, ignore the entire OBU |
1103 | 104 | if (hash_type) return -1; |
1104 | | |
1105 | 58 | FrameHash *const frame_hash = has_grain ? &cm->cur_frame->grain_frame_hash |
1106 | 58 | : &cm->cur_frame->raw_frame_hash; |
1107 | 58 | memset(frame_hash, 0, sizeof(*frame_hash)); |
1108 | | |
1109 | 58 | frame_hash->hash_type = hash_type; |
1110 | 58 | frame_hash->per_plane = per_plane; |
1111 | 58 | frame_hash->has_grain = has_grain; |
1112 | 58 | if (per_plane) { |
1113 | 0 | const int num_planes = is_monochrome ? 1 : 3; |
1114 | 0 | for (int i = 0; i < num_planes; ++i) { |
1115 | 0 | PlaneHash *plane = &frame_hash->plane[i]; |
1116 | 0 | for (size_t j = 0; j < 16; ++j) |
1117 | 0 | plane->md5[j] = avm_rb_read_literal(rb, 8); |
1118 | 0 | } |
1119 | 58 | } else { |
1120 | 58 | PlaneHash *plane = &frame_hash->plane[0]; |
1121 | 146 | for (size_t i = 0; i < 16; ++i) plane->md5[i] = avm_rb_read_literal(rb, 8); |
1122 | 58 | } |
1123 | 58 | frame_hash->is_present = 1; |
1124 | | |
1125 | 58 | return 0; |
1126 | 104 | } |
1127 | | |
1128 | 0 | static void read_metadata_timecode(struct avm_read_bit_buffer *rb) { |
1129 | 0 | avm_rb_read_literal(rb, 5); // counting_type f(5) |
1130 | 0 | const int full_timestamp_flag = |
1131 | 0 | avm_rb_read_bit(rb); // full_timestamp_flag f(1) |
1132 | 0 | avm_rb_read_bit(rb); // discontinuity_flag (f1) |
1133 | 0 | avm_rb_read_bit(rb); // cnt_dropped_flag f(1) |
1134 | 0 | avm_rb_read_literal(rb, 9); // n_frames f(9) |
1135 | 0 | if (full_timestamp_flag) { |
1136 | 0 | avm_rb_read_literal(rb, 6); // seconds_value f(6) |
1137 | 0 | avm_rb_read_literal(rb, 6); // minutes_value f(6) |
1138 | 0 | avm_rb_read_literal(rb, 5); // hours_value f(5) |
1139 | 0 | } else { |
1140 | 0 | const int seconds_flag = avm_rb_read_bit(rb); // seconds_flag f(1) |
1141 | 0 | if (seconds_flag) { |
1142 | 0 | avm_rb_read_literal(rb, 6); // seconds_value f(6) |
1143 | 0 | const int minutes_flag = avm_rb_read_bit(rb); // minutes_flag f(1) |
1144 | 0 | if (minutes_flag) { |
1145 | 0 | avm_rb_read_literal(rb, 6); // minutes_value f(6) |
1146 | 0 | const int hours_flag = avm_rb_read_bit(rb); // hours_flag f(1) |
1147 | 0 | if (hours_flag) { |
1148 | 0 | avm_rb_read_literal(rb, 5); // hours_value f(5) |
1149 | 0 | } |
1150 | 0 | } |
1151 | 0 | } |
1152 | 0 | } |
1153 | | // time_offset_length f(5) |
1154 | 0 | const int time_offset_length = avm_rb_read_literal(rb, 5); |
1155 | 0 | if (time_offset_length) { |
1156 | | // time_offset_value f(time_offset_length) |
1157 | 0 | avm_rb_read_literal(rb, time_offset_length); |
1158 | 0 | } |
1159 | 0 | } |
1160 | | |
1161 | | // Returns the last nonzero byte in 'data'. If there is no nonzero byte in |
1162 | | // 'data', returns 0. |
1163 | | // |
1164 | | // Call this function to check the following requirement in the spec: |
1165 | | // This implies that when any payload data is present for this OBU type, at |
1166 | | // least one byte of the payload data (including the trailing bit) shall not |
1167 | | // be equal to 0. |
1168 | 15.1k | static uint8_t get_last_nonzero_byte(const uint8_t *data, size_t sz) { |
1169 | | // Scan backward and return on the first nonzero byte. |
1170 | 15.1k | size_t i = sz; |
1171 | 15.6k | while (i != 0) { |
1172 | 15.6k | --i; |
1173 | 15.6k | if (data[i] != 0) return data[i]; |
1174 | 15.6k | } |
1175 | 0 | return 0; |
1176 | 15.1k | } |
1177 | | |
1178 | | // Skip metadata_unit_remaining_bits: decoders conforming to this version of |
1179 | | // the specification shall ignore metadata_unit_remaining_bits. |
1180 | | static void skip_remaining_mu_payload_bits(struct avm_read_bit_buffer *rb, |
1181 | | size_t parsed_payload_bits, |
1182 | 3 | size_t total_payload_bits) { |
1183 | 3 | if (parsed_payload_bits < total_payload_bits) { |
1184 | 2 | size_t remaining_bits = total_payload_bits - parsed_payload_bits; |
1185 | 10 | while (remaining_bits > 0) { |
1186 | 8 | const int chunk = (remaining_bits > 31) ? 31 : (int)remaining_bits; |
1187 | 8 | avm_rb_read_literal(rb, chunk); |
1188 | 8 | remaining_bits -= chunk; |
1189 | 8 | } |
1190 | 2 | } |
1191 | 3 | } |
1192 | | |
1193 | | // Checks the metadata for correct syntax but ignores the parsed metadata. |
1194 | | // |
1195 | | // On success, returns the number of bytes read from 'data'. On failure, sets |
1196 | | // pbi->common.error.error_code and returns 0, or calls avm_internal_error() |
1197 | | // and does not return. |
1198 | | static size_t read_metadata_unit_payload(AV2Decoder *pbi, const uint8_t *data, |
1199 | 4 | avm_metadata_t *metadata) { |
1200 | 4 | AV2_COMMON *const cm = &pbi->common; |
1201 | 4 | size_t type_length = 0; |
1202 | 4 | const OBU_METADATA_TYPE metadata_type = metadata->type; |
1203 | 4 | const size_t sz = metadata->sz; |
1204 | | |
1205 | 4 | int known_metadata_type = metadata_type >= OBU_METADATA_TYPE_HDR_CLL && |
1206 | 4 | metadata_type < NUM_OBU_METADATA_TYPES; |
1207 | 4 | known_metadata_type |= metadata_type == OBU_METADATA_TYPE_ICC_PROFILE; |
1208 | 4 | known_metadata_type |= metadata_type == OBU_METADATA_TYPE_SCAN_TYPE; |
1209 | 4 | known_metadata_type |= metadata_type == OBU_METADATA_TYPE_TEMPORAL_POINT_INFO; |
1210 | 4 | known_metadata_type |= |
1211 | 4 | metadata_type == OBU_METADATA_TYPE_USER_DATA_UNREGISTERED; |
1212 | 4 | if (!known_metadata_type) { |
1213 | 3 | return sz; |
1214 | 3 | } |
1215 | | // Temporal point info metadata is only valid in SHORT format, not GROUP. |
1216 | 1 | if (metadata_type == OBU_METADATA_TYPE_TEMPORAL_POINT_INFO) { |
1217 | 0 | avm_internal_error(&cm->error, AVM_CODEC_CORRUPT_FRAME, |
1218 | 0 | "Temporal point info metadata shall only appear in " |
1219 | 0 | "OBU_METADATA_SHORT, not OBU_METADATA_GROUP"); |
1220 | 0 | } |
1221 | | // Track bits consumed by the type-specific metadata reader, matching the |
1222 | | // proposal's parsedPayloadBits = currentPosition - startPosition. |
1223 | 1 | size_t parsed_payload_bits = 0; |
1224 | 1 | struct avm_read_bit_buffer rb; |
1225 | 1 | if (metadata_type == OBU_METADATA_TYPE_ITUT_T35) { |
1226 | 0 | read_metadata_itut_t35(pbi, data + type_length, sz - type_length); |
1227 | 0 | parsed_payload_bits = sz * 8; |
1228 | 1 | } else if (metadata_type == OBU_METADATA_TYPE_HDR_CLL) { |
1229 | 0 | read_metadata_hdr_cll(pbi, data + type_length, sz - type_length); |
1230 | 0 | parsed_payload_bits = sz * 8; |
1231 | 1 | } else if (metadata_type == OBU_METADATA_TYPE_HDR_MDCV) { |
1232 | 0 | read_metadata_hdr_mdcv(pbi, data + type_length, sz - type_length); |
1233 | 0 | parsed_payload_bits = sz * 8; |
1234 | 1 | } else if (metadata_type == OBU_METADATA_TYPE_BANDING_HINTS) { |
1235 | 1 | read_metadata_banding_hints(pbi, data + type_length, sz - type_length); |
1236 | 1 | parsed_payload_bits = sz * 8; |
1237 | 1 | } else if (metadata_type == OBU_METADATA_TYPE_SCAN_TYPE) { |
1238 | 0 | av2_init_read_bit_buffer(pbi, &rb, data + type_length, data + sz); |
1239 | 0 | read_metadata_scan_type(pbi, &rb); |
1240 | 0 | parsed_payload_bits = rb.bit_offset; |
1241 | 0 | } else if (metadata_type == OBU_METADATA_TYPE_TEMPORAL_POINT_INFO) { |
1242 | 0 | av2_init_read_bit_buffer(pbi, &rb, data + type_length, data + sz); |
1243 | 0 | read_metadata_temporal_point_info(pbi, &rb); |
1244 | 0 | parsed_payload_bits = rb.bit_offset; |
1245 | 0 | } else if (metadata_type == OBU_METADATA_TYPE_ICC_PROFILE) { |
1246 | 0 | read_metadata_icc_profile(pbi, data + type_length, sz - type_length); |
1247 | 0 | parsed_payload_bits = sz * 8; |
1248 | 0 | } else if (metadata_type == OBU_METADATA_TYPE_USER_DATA_UNREGISTERED) { |
1249 | 0 | read_metadata_user_data_unregistered(pbi, data + type_length, |
1250 | 0 | sz - type_length); |
1251 | 0 | parsed_payload_bits = sz * 8; |
1252 | 0 | } else { |
1253 | | // Remaining types: TIMECODE, DECODED_FRAME_HASH, SCALABILITY. |
1254 | 0 | av2_init_read_bit_buffer(pbi, &rb, data + type_length, data + sz); |
1255 | 0 | if (metadata_type == OBU_METADATA_TYPE_DECODED_FRAME_HASH) { |
1256 | 0 | if (read_metadata_frame_hash(pbi, &rb)) { |
1257 | 0 | return sz; |
1258 | 0 | } |
1259 | 0 | } else { |
1260 | 0 | assert(metadata_type == OBU_METADATA_TYPE_TIMECODE); |
1261 | 0 | read_metadata_timecode(&rb); |
1262 | 0 | } |
1263 | 0 | parsed_payload_bits = rb.bit_offset; |
1264 | 0 | } |
1265 | | |
1266 | | // Compute remaining payload bits and skip them. |
1267 | 1 | const size_t total_payload_bits = (sz - type_length) * 8; |
1268 | 1 | av2_init_read_bit_buffer(pbi, &rb, data + type_length, data + sz); |
1269 | 1 | rb.bit_offset = (uint32_t)parsed_payload_bits; |
1270 | 1 | skip_remaining_mu_payload_bits(&rb, parsed_payload_bits, total_payload_bits); |
1271 | | |
1272 | 1 | return sz; |
1273 | 1 | } |
1274 | | |
1275 | | static size_t read_metadata_obsp(AV2Decoder *pbi, const uint8_t *data, |
1276 | | size_t sz, |
1277 | | avm_metadata_array_t *metadata_array, |
1278 | | avm_metadata_t *metadata_base, |
1279 | 5 | int expected_suffix) { |
1280 | 5 | AV2_COMMON *const cm = &pbi->common; |
1281 | | |
1282 | 5 | struct avm_read_bit_buffer rb; |
1283 | 5 | av2_init_read_bit_buffer(pbi, &rb, data, data + sz); |
1284 | | |
1285 | 5 | metadata_base->is_suffix = avm_rb_read_literal(&rb, 1); |
1286 | | |
1287 | | // Validate suffix bit if requested |
1288 | 5 | if (expected_suffix >= 0 && metadata_base->is_suffix != expected_suffix) { |
1289 | 0 | cm->error.error_code = AVM_CODEC_CORRUPT_FRAME; |
1290 | 0 | return 0; |
1291 | 0 | } |
1292 | | |
1293 | 5 | metadata_base->necessity_idc = |
1294 | 5 | (avm_metadata_necessity_t)avm_rb_read_literal(&rb, 2); |
1295 | 5 | metadata_base->application_id = |
1296 | 5 | (avm_metadata_application_id_t)avm_rb_read_literal(&rb, 5); |
1297 | | |
1298 | 5 | const size_t bytes_read = avm_rb_bytes_read(&rb); |
1299 | 5 | assert(bytes_read == 1); |
1300 | | |
1301 | 5 | size_t count_length; |
1302 | 5 | uint64_t count_minus_1; |
1303 | 5 | if (avm_uleb_decode(data + bytes_read, sz - bytes_read, &count_minus_1, |
1304 | 5 | &count_length) < 0) { |
1305 | 0 | cm->error.error_code = AVM_CODEC_CORRUPT_FRAME; |
1306 | 0 | return 0; |
1307 | 0 | } |
1308 | | |
1309 | 5 | metadata_array->sz = count_minus_1 + 1; |
1310 | | |
1311 | | // Ensure metadata_unit_cnt doesn't exceed 2^14 (uleb128 <= 2 bytes) |
1312 | 5 | if (metadata_array->sz > 16384) { |
1313 | 0 | cm->error.error_code = AVM_CODEC_CORRUPT_FRAME; |
1314 | 0 | return 0; |
1315 | 0 | } |
1316 | | |
1317 | 5 | return bytes_read + count_length; |
1318 | 5 | } |
1319 | | |
1320 | | static size_t read_metadata_unit_header(AV2Decoder *pbi, const uint8_t *data, |
1321 | | size_t sz, avm_metadata_t *metadata, |
1322 | 79 | const ObuHeader *obu_header) { |
1323 | 79 | AV2_COMMON *const cm = &pbi->common; |
1324 | 79 | size_t type_length; |
1325 | 79 | uint64_t type_value; |
1326 | 79 | if (avm_uleb_decode(data, sz, &type_value, &type_length) < 0) { |
1327 | 0 | cm->error.error_code = AVM_CODEC_CORRUPT_FRAME; |
1328 | 0 | return 0; |
1329 | 0 | } |
1330 | 79 | assert(type_value <= UINT32_MAX); |
1331 | 79 | metadata->type = (uint32_t)type_value; |
1332 | 79 | size_t bytes_read = type_length; |
1333 | | |
1334 | 79 | struct avm_read_bit_buffer rb; |
1335 | 79 | av2_init_read_bit_buffer(pbi, &rb, data + bytes_read, data + sz); |
1336 | | |
1337 | 79 | const size_t muh_header_size = avm_rb_read_literal(&rb, 7); |
1338 | 79 | metadata->cancel_flag = avm_rb_read_literal(&rb, 1); |
1339 | 79 | assert(avm_rb_bytes_read(&rb) == 1); |
1340 | 79 | bytes_read += avm_rb_bytes_read(&rb); |
1341 | | |
1342 | 79 | const size_t total_size = bytes_read + muh_header_size; |
1343 | 79 | if (total_size > sz) { |
1344 | 3 | cm->error.error_code = AVM_CODEC_CORRUPT_FRAME; |
1345 | 3 | return 0; |
1346 | 3 | } |
1347 | | |
1348 | 76 | if (!metadata->cancel_flag) { |
1349 | 4 | size_t size_length; |
1350 | 4 | uint64_t size_value = 0; |
1351 | 4 | if (avm_uleb_decode(data + bytes_read, total_size - bytes_read, &size_value, |
1352 | 4 | &size_length) < 0) { |
1353 | 0 | cm->error.error_code = AVM_CODEC_CORRUPT_FRAME; |
1354 | 0 | return 0; |
1355 | 0 | } |
1356 | 4 | metadata->sz = size_value; |
1357 | 4 | bytes_read += size_length; |
1358 | | |
1359 | 4 | av2_init_read_bit_buffer(pbi, &rb, data + bytes_read, data + total_size); |
1360 | | |
1361 | 4 | metadata->layer_idc = (avm_metadata_layer_t)avm_rb_read_literal(&rb, 3); |
1362 | 4 | metadata->persistence_idc = |
1363 | 4 | (avm_metadata_persistence_t)avm_rb_read_literal(&rb, 3); |
1364 | 4 | metadata->priority = avm_rb_read_literal(&rb, 8); |
1365 | 4 | avm_rb_read_literal(&rb, 2); // reserved bits |
1366 | | |
1367 | 4 | assert(avm_rb_bytes_read(&rb) == 2); |
1368 | | |
1369 | 4 | if (metadata->layer_idc == AVM_LAYER_VALUES) { |
1370 | 0 | if (obu_header->obu_xlayer_id == 31) { |
1371 | 0 | metadata->xlayer_map = avm_rb_read_unsigned_literal(&rb, 32); |
1372 | 0 | if ((metadata->xlayer_map & (1u << 31)) != 0) { |
1373 | 0 | cm->error.error_code = AVM_CODEC_CORRUPT_FRAME; |
1374 | 0 | return 0; |
1375 | 0 | } |
1376 | 0 | for (int n = 0; n < 31; n++) { |
1377 | 0 | if (metadata->xlayer_map & (1u << n)) { |
1378 | 0 | metadata->mlayer_map[n] = avm_rb_read_unsigned_literal(&rb, 8); |
1379 | 0 | } |
1380 | 0 | } |
1381 | 0 | } else { |
1382 | 0 | metadata->mlayer_map[obu_header->obu_xlayer_id] = |
1383 | 0 | avm_rb_read_unsigned_literal(&rb, 8); |
1384 | 0 | } |
1385 | 0 | } |
1386 | | |
1387 | 4 | bytes_read += avm_rb_bytes_read(&rb); |
1388 | 4 | } |
1389 | | |
1390 | 76 | assert(bytes_read <= total_size); |
1391 | 76 | return total_size; |
1392 | 76 | } |
1393 | | |
1394 | | static size_t read_metadata_obu(AV2Decoder *pbi, const uint8_t *data, size_t sz, |
1395 | 5 | ObuHeader *obu_header, int expected_suffix) { |
1396 | 5 | AV2_COMMON *const cm = &pbi->common; |
1397 | | |
1398 | 5 | avm_metadata_array_t metadata_array = { 0 }; |
1399 | 5 | avm_metadata_t metadata_base; |
1400 | 5 | memset(&metadata_base, 0, sizeof(metadata_base)); |
1401 | 5 | size_t bytes_read = read_metadata_obsp(pbi, data, sz, &metadata_array, |
1402 | 5 | &metadata_base, expected_suffix); |
1403 | | |
1404 | 81 | for (uint32_t i = 0; i < metadata_array.sz; i++) { |
1405 | 79 | avm_metadata_t metadata = { 0 }; |
1406 | | // copy shared fields read in `read_metadata_obsp` |
1407 | 79 | memcpy(&metadata, &metadata_base, sizeof(metadata)); |
1408 | | |
1409 | 79 | const size_t muh_size = read_metadata_unit_header( |
1410 | 79 | pbi, data + bytes_read, sz - bytes_read, &metadata, obu_header); |
1411 | 79 | if (muh_size == 0) { |
1412 | 3 | cm->error.error_code = AVM_CODEC_CORRUPT_FRAME; |
1413 | 3 | return 0; |
1414 | 3 | } |
1415 | 76 | bytes_read += muh_size; |
1416 | 76 | if (!metadata.cancel_flag) { |
1417 | 4 | if (sz - bytes_read < metadata.sz) { |
1418 | 0 | cm->error.error_code = AVM_CODEC_CORRUPT_FRAME; |
1419 | 0 | return 0; |
1420 | 0 | } |
1421 | 4 | const size_t mup_size = |
1422 | 4 | read_metadata_unit_payload(pbi, data + bytes_read, &metadata); |
1423 | 4 | bytes_read += mup_size; |
1424 | 4 | } |
1425 | 76 | } |
1426 | | |
1427 | 2 | if (bytes_read >= sz || data[bytes_read] != 0x80) { |
1428 | 2 | cm->error.error_code = AVM_CODEC_CORRUPT_FRAME; |
1429 | 2 | return 0; |
1430 | 2 | } |
1431 | 0 | return bytes_read + 1; |
1432 | 2 | } |
1433 | | |
1434 | | // Checks the metadata for correct syntax but ignores the parsed metadata. |
1435 | | // |
1436 | | // On success, returns the number of bytes read from 'data'. On failure, sets |
1437 | | // pbi->common.error.error_code and returns 0, or calls avm_internal_error() |
1438 | | // and does not return. |
1439 | | // expected_suffix: 0 for prefix metadata, 1 for suffix metadata, -1 for no |
1440 | | // validation |
1441 | | static size_t read_metadata_short(AV2Decoder *pbi, const uint8_t *data, |
1442 | 807 | size_t sz, int expected_suffix) { |
1443 | 807 | AV2_COMMON *const cm = &pbi->common; |
1444 | 807 | size_t type_length; |
1445 | 807 | uint64_t type_value; |
1446 | 807 | struct avm_read_bit_buffer rb; |
1447 | 807 | av2_init_read_bit_buffer(pbi, &rb, data, data + sz); |
1448 | | |
1449 | 807 | uint8_t metadata_is_suffix = avm_rb_read_bit(&rb); |
1450 | | |
1451 | | // Validate suffix bit if requested |
1452 | 807 | if (expected_suffix >= 0 && metadata_is_suffix != expected_suffix) { |
1453 | 5 | cm->error.error_code = AVM_CODEC_CORRUPT_FRAME; |
1454 | 5 | return 0; |
1455 | 5 | } |
1456 | | |
1457 | 802 | uint8_t muh_layer_idc = avm_rb_read_literal(&rb, 3); |
1458 | 802 | uint8_t muh_cancel_flag = avm_rb_read_bit(&rb); |
1459 | 802 | uint8_t muh_persistence_idc = avm_rb_read_literal(&rb, 3); |
1460 | 802 | if (avm_uleb_decode( |
1461 | 802 | data + 1, // read type from the position data + 1 |
1462 | 802 | sz - 1, // one less bytes available due to extra parameters |
1463 | 802 | &type_value, &type_length) < 0) { |
1464 | 1 | cm->error.error_code = AVM_CODEC_CORRUPT_FRAME; |
1465 | 1 | return 0; |
1466 | 1 | } |
1467 | | |
1468 | 801 | const OBU_METADATA_TYPE metadata_type = (OBU_METADATA_TYPE)type_value; |
1469 | | |
1470 | | // Increase the type_length by 1 byte since there is one prefix byte added |
1471 | | // before the type |
1472 | 801 | ++type_length; |
1473 | 801 | if (muh_cancel_flag) return sz; |
1474 | | |
1475 | | // Update the metadata with the header fields we read |
1476 | 801 | if (pbi->metadata && pbi->metadata->sz > 0) { |
1477 | 0 | avm_metadata_t *last_metadata = |
1478 | 0 | pbi->metadata->metadata_array[pbi->metadata->sz - 1]; |
1479 | 0 | if (last_metadata && last_metadata->type == OBU_METADATA_TYPE_ITUT_T35) { |
1480 | 0 | last_metadata->is_suffix = metadata_is_suffix; |
1481 | 0 | last_metadata->layer_idc = muh_layer_idc; |
1482 | 0 | last_metadata->cancel_flag = muh_cancel_flag; |
1483 | 0 | last_metadata->persistence_idc = muh_persistence_idc; |
1484 | 0 | } |
1485 | 0 | } |
1486 | | |
1487 | 801 | const bool known_metadata_type = |
1488 | 801 | (metadata_type > OBU_METADATA_TYPE_AVM_RESERVED_0) && |
1489 | 776 | (metadata_type < NUM_OBU_METADATA_TYPES); |
1490 | 801 | if (!known_metadata_type) { |
1491 | | // If metadata_type is reserved for future use or a user private value, |
1492 | | // ignore the entire OBU and just check trailing bits. |
1493 | 697 | if (get_last_nonzero_byte(data + type_length, sz - type_length) == 0) { |
1494 | 0 | cm->error.error_code = AVM_CODEC_CORRUPT_FRAME; |
1495 | 0 | return 0; |
1496 | 0 | } |
1497 | 697 | return sz; |
1498 | 697 | } |
1499 | 104 | if (metadata_type == OBU_METADATA_TYPE_ITUT_T35) { |
1500 | | // read_metadata_itut_t35() checks trailing bits. |
1501 | 0 | read_metadata_itut_t35_short(pbi, data + type_length, sz - type_length); |
1502 | | // Update the metadata with the header fields we read |
1503 | 0 | if (pbi->metadata && pbi->metadata->sz > 0) { |
1504 | 0 | avm_metadata_t *last_metadata = |
1505 | 0 | pbi->metadata->metadata_array[pbi->metadata->sz - 1]; |
1506 | 0 | if (last_metadata && last_metadata->type == OBU_METADATA_TYPE_ITUT_T35) { |
1507 | 0 | last_metadata->is_suffix = metadata_is_suffix; |
1508 | 0 | last_metadata->layer_idc = muh_layer_idc; |
1509 | 0 | last_metadata->cancel_flag = muh_cancel_flag; |
1510 | 0 | last_metadata->persistence_idc = muh_persistence_idc; |
1511 | 0 | } |
1512 | 0 | } |
1513 | 0 | return sz; |
1514 | 104 | } else if (metadata_type == OBU_METADATA_TYPE_HDR_CLL) { |
1515 | 0 | size_t bytes_read = |
1516 | 0 | type_length + |
1517 | 0 | read_metadata_hdr_cll(pbi, data + type_length, sz - type_length); |
1518 | 0 | if (get_last_nonzero_byte(data + bytes_read, sz - bytes_read) != 0x80) { |
1519 | 0 | cm->error.error_code = AVM_CODEC_CORRUPT_FRAME; |
1520 | 0 | return 0; |
1521 | 0 | } |
1522 | | // Update the metadata with the header fields we read |
1523 | 0 | if (pbi->metadata && pbi->metadata->sz > 0) { |
1524 | 0 | avm_metadata_t *last_metadata = |
1525 | 0 | pbi->metadata->metadata_array[pbi->metadata->sz - 1]; |
1526 | 0 | if (last_metadata && last_metadata->type == OBU_METADATA_TYPE_HDR_CLL) { |
1527 | 0 | last_metadata->is_suffix = metadata_is_suffix; |
1528 | 0 | last_metadata->layer_idc = muh_layer_idc; |
1529 | 0 | last_metadata->cancel_flag = muh_cancel_flag; |
1530 | 0 | last_metadata->persistence_idc = muh_persistence_idc; |
1531 | 0 | } |
1532 | 0 | } |
1533 | 0 | return sz; |
1534 | 104 | } else if (metadata_type == OBU_METADATA_TYPE_HDR_MDCV) { |
1535 | 0 | size_t bytes_read = |
1536 | 0 | type_length + |
1537 | 0 | read_metadata_hdr_mdcv(pbi, data + type_length, sz - type_length); |
1538 | 0 | if (get_last_nonzero_byte(data + bytes_read, sz - bytes_read) != 0x80) { |
1539 | 0 | cm->error.error_code = AVM_CODEC_CORRUPT_FRAME; |
1540 | 0 | return 0; |
1541 | 0 | } |
1542 | | // Update the metadata with the header fields we read |
1543 | 0 | if (pbi->metadata && pbi->metadata->sz > 0) { |
1544 | 0 | avm_metadata_t *last_metadata = |
1545 | 0 | pbi->metadata->metadata_array[pbi->metadata->sz - 1]; |
1546 | 0 | if (last_metadata && last_metadata->type == OBU_METADATA_TYPE_HDR_MDCV) { |
1547 | 0 | last_metadata->is_suffix = metadata_is_suffix; |
1548 | 0 | last_metadata->layer_idc = muh_layer_idc; |
1549 | 0 | last_metadata->cancel_flag = muh_cancel_flag; |
1550 | 0 | last_metadata->persistence_idc = muh_persistence_idc; |
1551 | 0 | } |
1552 | 0 | } |
1553 | 0 | return sz; |
1554 | 104 | } else if (metadata_type == OBU_METADATA_TYPE_SCAN_TYPE) { |
1555 | 0 | const size_t kMinScanTypeHeaderSize = 1; |
1556 | 0 | if (sz < kMinScanTypeHeaderSize) { |
1557 | 0 | avm_internal_error(&cm->error, AVM_CODEC_CORRUPT_FRAME, |
1558 | 0 | "Incorrect scan type metadata payload size"); |
1559 | 0 | } |
1560 | 0 | av2_init_read_bit_buffer(pbi, &rb, data + type_length, data + sz); |
1561 | 0 | read_metadata_scan_type(pbi, &rb); |
1562 | 0 | return sz; |
1563 | 104 | } else if (metadata_type == OBU_METADATA_TYPE_TEMPORAL_POINT_INFO) { |
1564 | 0 | const size_t kMinTemporalPointInfoHeaderSize = 1; |
1565 | 0 | if (sz < kMinTemporalPointInfoHeaderSize) { |
1566 | 0 | avm_internal_error(&cm->error, AVM_CODEC_CORRUPT_FRAME, |
1567 | 0 | "Incorrect temporal point info metadata payload size"); |
1568 | 0 | } |
1569 | 0 | av2_init_read_bit_buffer(pbi, &rb, data + type_length, data + sz); |
1570 | 0 | read_metadata_temporal_point_info(pbi, &rb); |
1571 | 0 | return sz; |
1572 | 0 | } |
1573 | | |
1574 | 104 | av2_init_read_bit_buffer(pbi, &rb, data + type_length, data + sz); |
1575 | 104 | if (metadata_type == OBU_METADATA_TYPE_DECODED_FRAME_HASH) { |
1576 | 104 | if (read_metadata_frame_hash(pbi, &rb)) { |
1577 | | // Unsupported Decoded Frame Hash metadata. Ignoring the entire OBU and |
1578 | | // just checking trailing bits |
1579 | 46 | if (get_last_nonzero_byte(data + type_length, sz - type_length) == 0) { |
1580 | 0 | cm->error.error_code = AVM_CODEC_CORRUPT_FRAME; |
1581 | 0 | return 0; |
1582 | 0 | } |
1583 | 46 | return sz; |
1584 | 46 | } |
1585 | 104 | } else if (metadata_type == OBU_METADATA_TYPE_BANDING_HINTS) { |
1586 | | // Banding hints metadata is variable bits, not byte-aligned |
1587 | 0 | read_metadata_banding_hints_from_rb(pbi, &rb); |
1588 | | // Update the metadata with the header fields we read |
1589 | 0 | if (pbi->metadata && pbi->metadata->sz > 0) { |
1590 | 0 | avm_metadata_t *last_metadata = |
1591 | 0 | pbi->metadata->metadata_array[pbi->metadata->sz - 1]; |
1592 | 0 | if (last_metadata && |
1593 | 0 | last_metadata->type == OBU_METADATA_TYPE_BANDING_HINTS) { |
1594 | 0 | last_metadata->is_suffix = metadata_is_suffix; |
1595 | 0 | last_metadata->layer_idc = muh_layer_idc; |
1596 | 0 | last_metadata->cancel_flag = muh_cancel_flag; |
1597 | 0 | last_metadata->persistence_idc = muh_persistence_idc; |
1598 | 0 | } |
1599 | 0 | } |
1600 | 0 | } else if (metadata_type == OBU_METADATA_TYPE_ICC_PROFILE) { |
1601 | | // ICC profile is byte-aligned binary data |
1602 | | // Find the last nonzero byte (should be 0x80 trailing byte) |
1603 | 0 | const int last_nonzero_idx = |
1604 | 0 | get_last_nonzero_byte_index(data + type_length, sz - type_length); |
1605 | 0 | if (last_nonzero_idx < 0 || data[type_length + last_nonzero_idx] != 0x80) { |
1606 | 0 | cm->error.error_code = AVM_CODEC_CORRUPT_FRAME; |
1607 | 0 | return 0; |
1608 | 0 | } |
1609 | | // ICC payload size excludes the trailing 0x80 byte |
1610 | 0 | const size_t icc_payload_size = last_nonzero_idx; |
1611 | 0 | read_metadata_icc_profile(pbi, data + type_length, icc_payload_size); |
1612 | 0 | return sz; |
1613 | 0 | } else if (metadata_type == OBU_METADATA_TYPE_USER_DATA_UNREGISTERED) { |
1614 | | // User data unregistered is byte-aligned binary data |
1615 | | // Find the last nonzero byte (should be 0x80 trailing byte) |
1616 | 0 | const int last_nonzero_idx = |
1617 | 0 | get_last_nonzero_byte_index(data + type_length, sz - type_length); |
1618 | 0 | if (last_nonzero_idx < 0 || data[type_length + last_nonzero_idx] != 0x80) { |
1619 | 0 | cm->error.error_code = AVM_CODEC_CORRUPT_FRAME; |
1620 | 0 | return 0; |
1621 | 0 | } |
1622 | | // User data payload size excludes the trailing 0x80 byte |
1623 | 0 | const size_t user_data_payload_size = last_nonzero_idx; |
1624 | 0 | read_metadata_user_data_unregistered(pbi, data + type_length, |
1625 | 0 | user_data_payload_size); |
1626 | 0 | return sz; |
1627 | 0 | } else { |
1628 | 0 | assert(metadata_type == OBU_METADATA_TYPE_TIMECODE); |
1629 | 0 | read_metadata_timecode(&rb); |
1630 | 0 | } |
1631 | 58 | { |
1632 | | // Compute remaining payload bits and skip them. |
1633 | | // Subtract 1 from sz to exclude the trailing 0x80 byte, which is part of |
1634 | | // metadata_short_obu() trailing_bits() syntax, not metadata_unit() payload. |
1635 | 58 | const size_t parsed_payload_bits = rb.bit_offset; |
1636 | 58 | const size_t total_payload_bits = (sz - type_length - 1) * 8; |
1637 | 58 | skip_remaining_mu_payload_bits(&rb, parsed_payload_bits, |
1638 | 58 | total_payload_bits); |
1639 | 58 | } |
1640 | 58 | if (av2_check_trailing_bits(pbi, &rb) != 0) { |
1641 | | // cm->error.error_code is already set. |
1642 | 2 | return 0; |
1643 | 2 | } |
1644 | 58 | assert((rb.bit_offset & 7) == 0); |
1645 | 56 | return type_length + (rb.bit_offset >> 3); |
1646 | 58 | } |
1647 | | // On success, returns 'sz'. On failure, sets pbi->common.error.error_code and |
1648 | | // returns 0. |
1649 | | static size_t read_padding(AV2_COMMON *const cm, const uint8_t *data, |
1650 | 234 | size_t sz) { |
1651 | | // The spec allows a padding OBU to be header-only (i.e., obu_size = 0). So |
1652 | | // check trailing bits only if sz > 0. |
1653 | 234 | if (sz > 0) { |
1654 | | // The payload of a padding OBU is byte aligned. Therefore the first |
1655 | | // trailing byte should be 0x80. See https://crbug.com/aomedia/2393. |
1656 | 234 | const uint8_t last_nonzero_byte = get_last_nonzero_byte(data, sz); |
1657 | 234 | if (last_nonzero_byte != 0x80) { |
1658 | 234 | cm->error.error_code = AVM_CODEC_CORRUPT_FRAME; |
1659 | 234 | return 0; |
1660 | 234 | } |
1661 | 234 | } |
1662 | 0 | return sz; |
1663 | 234 | } |
1664 | | |
1665 | 112k | int av2_is_leading_vcl_obu(OBU_TYPE obu_type) { |
1666 | 112k | return (obu_type == OBU_LEADING_TILE_GROUP || obu_type == OBU_LEADING_SEF || |
1667 | 111k | obu_type == OBU_LEADING_TIP); |
1668 | 112k | } |
1669 | | |
1670 | | // Check if any obu is present between two tile groups of one frame unit. |
1671 | | static void check_tilegroup_obus_in_a_frame_unit(AV2_COMMON *const cm, |
1672 | | obu_info *current_obu, |
1673 | 0 | obu_info *prev_obu) { |
1674 | 0 | if (current_obu->obu_type != prev_obu->obu_type || |
1675 | 0 | current_obu->immediate_output_picture != |
1676 | 0 | prev_obu->immediate_output_picture || |
1677 | 0 | current_obu->showable_frame != prev_obu->showable_frame || |
1678 | 0 | current_obu->display_order_hint != prev_obu->display_order_hint || |
1679 | 0 | current_obu->mlayer_id != prev_obu->mlayer_id) { |
1680 | 0 | avm_internal_error( |
1681 | 0 | &cm->error, AVM_CODEC_UNSUP_BITSTREAM, |
1682 | 0 | "%s : no non-padding obu is allowed between tilegroup obus in a frame " |
1683 | 0 | "unit (current obu %s, current oh %d previous obu %s previous oh %d)", |
1684 | 0 | __func__, avm_obu_type_to_string(current_obu->obu_type), |
1685 | 0 | current_obu->display_order_hint, |
1686 | 0 | avm_obu_type_to_string(prev_obu->obu_type), |
1687 | 0 | prev_obu->display_order_hint); |
1688 | 0 | } |
1689 | 0 | } |
1690 | | |
1691 | | // TU validation: Check if an OBU type is metadata obu |
1692 | 1.44k | static int is_metadata_obu(OBU_TYPE obu_type) { |
1693 | 1.44k | return (obu_type == OBU_METADATA_SHORT || obu_type == OBU_METADATA_GROUP); |
1694 | 1.44k | } |
1695 | | |
1696 | | // TU validation: Check if an OBU type is global configuration information |
1697 | 40.7k | static int is_global_config_obu(OBU_TYPE obu_type, int xlayer_id) { |
1698 | 40.7k | return obu_type == OBU_MULTI_STREAM_DECODER_OPERATION || |
1699 | 39.2k | (obu_type == OBU_LAYER_CONFIGURATION_RECORD && |
1700 | 164 | xlayer_id == GLOBAL_XLAYER_ID) || |
1701 | 39.2k | (obu_type == OBU_OPERATING_POINT_SET && |
1702 | 38 | xlayer_id == GLOBAL_XLAYER_ID) || |
1703 | 39.2k | (obu_type == OBU_ATLAS_SEGMENT && xlayer_id == GLOBAL_XLAYER_ID) || |
1704 | 39.2k | (obu_type == OBU_METADATA_SHORT && xlayer_id == GLOBAL_XLAYER_ID) || |
1705 | 39.2k | (obu_type == OBU_METADATA_GROUP && xlayer_id == GLOBAL_XLAYER_ID); |
1706 | 40.7k | } |
1707 | | |
1708 | | // TU validation: Check if an OBU type is local configuration information |
1709 | 39.4k | static int is_local_config_obu(OBU_TYPE obu_type, int xlayer_id) { |
1710 | 39.4k | return (obu_type == OBU_LAYER_CONFIGURATION_RECORD && |
1711 | 164 | xlayer_id != GLOBAL_XLAYER_ID) || |
1712 | 39.2k | (obu_type == OBU_OPERATING_POINT_SET && |
1713 | 38 | xlayer_id != GLOBAL_XLAYER_ID) || |
1714 | 39.2k | (obu_type == OBU_ATLAS_SEGMENT && xlayer_id != GLOBAL_XLAYER_ID); |
1715 | 39.4k | } |
1716 | | |
1717 | | // TU validation: Check if an OBU type is not global or local configuration |
1718 | | // information, not sequence header or not padding |
1719 | 19.6k | static int is_frame_unit(OBU_TYPE obu_type, int xlayer_id) { |
1720 | | // OBU_MULTI_FRAME_HEADER, |
1721 | | // OBU_BUFFER_REMOVAL_TIMING, |
1722 | | // OBU_QUANTIZATION_MATRIX, |
1723 | | // OBU_FILM_GRAIN_MODEL, |
1724 | | // OBU_CONTENT_INTERPRETATION, |
1725 | | // OBU_METADATA_SHORT, |
1726 | | // OBU_METADATA_GROUP, |
1727 | | // OBU_CLOSED_LOOP_KEY, |
1728 | | // OBU_OPEN_LOOP_KEY, |
1729 | | // OBU_LEADING_TILE_GROUP, |
1730 | | // OBU_REGULAR_TILE_GROUP, |
1731 | | // OBU_SWITCH, |
1732 | | // OBU_LEADING_SEF, |
1733 | | // OBU_REGULAR_SEF, |
1734 | | // OBU_LEADING_TIP, |
1735 | | // OBU_REGULAR_TIP, |
1736 | | // OBU_BRIDGE_FRAME, |
1737 | | // OBU_RAS_FRAME |
1738 | 19.6k | int non_frame_unit_obu = is_global_config_obu(obu_type, xlayer_id) || |
1739 | 19.6k | is_local_config_obu(obu_type, xlayer_id) || |
1740 | 19.6k | obu_type == OBU_SEQUENCE_HEADER || |
1741 | 19.6k | obu_type == OBU_PADDING; |
1742 | | |
1743 | 19.6k | return !non_frame_unit_obu; |
1744 | 19.6k | } |
1745 | | |
1746 | 700 | static int is_coded_frame(OBU_TYPE obu_type) { |
1747 | 700 | return is_multi_tile_vcl_obu(obu_type) || is_single_tile_vcl_obu(obu_type); |
1748 | 700 | } |
1749 | | // Validates OBU order within a Temporal Unit with state machine. |
1750 | | // Returns 1 if OBU is valid for the current state, 0 if it violates rules. |
1751 | | // Note: The caller is responsible for filtering out padding OBUs and reserved |
1752 | | // OBUs before calling this function. |
1753 | | // |
1754 | | // After the global information phase, all remaining OBUs are grouped by |
1755 | | // xlayer_id in strictly increasing order. Within each xlayer group the full |
1756 | | // phase progression is enforced: |
1757 | | // local config (LCR -> OPS -> Atlas) -> SH -> frame unit data |
1758 | | // When the xlayer_id increases, the state resets to TU_STATE_LOCAL_INFO so |
1759 | | // the new xlayer group may begin with local config, SH, or frame unit data. |
1760 | | int check_temporal_unit_structure(temporal_unit_state_t *state, int obu_type, |
1761 | | int xlayer_id, int metadata_is_suffix, |
1762 | 43.7k | int prev_obu_type, int prev_xlayer_id) { |
1763 | | // Validate input parameters |
1764 | 43.7k | if (!state) return 0; |
1765 | | |
1766 | | // After the global phase, detect an xlayer_id increase and reset the |
1767 | | // per-xlayer state machine so the new group starts fresh. |
1768 | 43.7k | if (*state == TU_STATE_LOCAL_INFO || *state == TU_STATE_SEQUENCE_HEADER || |
1769 | 22.6k | *state == TU_STATE_FRAME_UINT_DATA) { |
1770 | 22.6k | if (xlayer_id != GLOBAL_XLAYER_ID && prev_xlayer_id != GLOBAL_XLAYER_ID && |
1771 | 22.6k | xlayer_id != prev_xlayer_id) { |
1772 | 49 | if (xlayer_id < prev_xlayer_id) return 0; // Must be increasing |
1773 | | // New xlayer group: reset to local info phase. |
1774 | 4 | *state = TU_STATE_LOCAL_INFO; |
1775 | 4 | } |
1776 | 22.6k | } |
1777 | | |
1778 | 43.7k | switch (*state) { |
1779 | 21.1k | case TU_STATE_START: |
1780 | 21.1k | case TU_STATE_TEMPORAL_DELIMITER: |
1781 | 21.1k | if (obu_type == OBU_TEMPORAL_DELIMITER) { |
1782 | 36 | if (*state == TU_STATE_TEMPORAL_DELIMITER) |
1783 | 0 | return 0; // Only one allowed |
1784 | 36 | *state = TU_STATE_TEMPORAL_DELIMITER; |
1785 | 36 | return 1; |
1786 | 21.1k | } else if (is_global_config_obu(obu_type, |
1787 | 21.1k | xlayer_id)) { // First OBU: global config |
1788 | 1.46k | *state = TU_STATE_GLOBAL_INFO; |
1789 | 1.46k | return 1; |
1790 | 19.6k | } else if (is_local_config_obu(obu_type, |
1791 | 19.6k | xlayer_id)) { // First OBU: local config |
1792 | 197 | *state = TU_STATE_LOCAL_INFO; |
1793 | 197 | return 1; |
1794 | 19.4k | } else if (obu_type == OBU_SEQUENCE_HEADER) { |
1795 | 18.0k | *state = TU_STATE_SEQUENCE_HEADER; |
1796 | 18.0k | return 1; |
1797 | 18.0k | } else if (is_frame_unit(obu_type, xlayer_id)) { |
1798 | 1.42k | *state = TU_STATE_FRAME_UINT_DATA; |
1799 | 1.42k | return 1; |
1800 | 1.42k | } else { // Invalid OBU type for start of temporal unit |
1801 | 0 | return 0; |
1802 | 0 | } |
1803 | | |
1804 | 0 | case TU_STATE_GLOBAL_INFO: |
1805 | 0 | if (is_global_config_obu(obu_type, xlayer_id)) { |
1806 | | // 0 or 1 OBU_MULTI_STREAM_DECODER_OPERATION, |
1807 | | // 0 or more: OBU_LAYER_CONFIGURATION_RECORD |
1808 | | // 0 or more: OBU_OPERATING_POINT_SET |
1809 | | // 0 or more: OBU_ATLAS_SEGMENT |
1810 | | // 0 or more: OBU_METADATA(obu_xlayer_id = 0x1F) |
1811 | | // MSDO -> LCR -> LCR -> OPS -> OPS -> ATS -> ATS -> METADATA -> |
1812 | | // METADATA |
1813 | 0 | if (obu_type == OBU_MULTI_STREAM_DECODER_OPERATION) |
1814 | 0 | return 0; // Only one allowed |
1815 | 0 | else if (obu_type == OBU_LAYER_CONFIGURATION_RECORD && |
1816 | 0 | (prev_obu_type == OBU_OPERATING_POINT_SET || |
1817 | 0 | prev_obu_type == OBU_ATLAS_SEGMENT || |
1818 | 0 | is_metadata_obu(prev_obu_type))) { |
1819 | 0 | return 0; |
1820 | 0 | } else if (obu_type == OBU_OPERATING_POINT_SET && |
1821 | 0 | (prev_obu_type == OBU_ATLAS_SEGMENT || |
1822 | 0 | is_metadata_obu(prev_obu_type))) { |
1823 | 0 | return 0; |
1824 | 0 | } else if (obu_type == OBU_ATLAS_SEGMENT && |
1825 | 0 | is_metadata_obu(prev_obu_type)) { |
1826 | 0 | return 0; |
1827 | 0 | } else |
1828 | 0 | return 1; |
1829 | 0 | } else if (is_local_config_obu(obu_type, xlayer_id)) { |
1830 | 0 | *state = TU_STATE_LOCAL_INFO; // Transition from global to local |
1831 | 0 | return 1; |
1832 | 0 | } else if (obu_type == OBU_SEQUENCE_HEADER) { |
1833 | 0 | *state = TU_STATE_SEQUENCE_HEADER; |
1834 | 0 | return 1; |
1835 | 0 | } else if (is_frame_unit(obu_type, xlayer_id)) { |
1836 | 0 | *state = TU_STATE_FRAME_UINT_DATA; |
1837 | 0 | return 1; |
1838 | 0 | } else { |
1839 | | // Invalid OBU type during global info phase or wrong xlayer_id |
1840 | 0 | return 0; |
1841 | 0 | } |
1842 | | |
1843 | 169 | case TU_STATE_LOCAL_INFO: |
1844 | 169 | if (is_local_config_obu(obu_type, xlayer_id)) { |
1845 | | // Within the same xlayer group: LCR -> OPS -> Atlas (no going back). |
1846 | 22 | if (xlayer_id == prev_xlayer_id) { |
1847 | 22 | if (obu_type == OBU_LAYER_CONFIGURATION_RECORD && |
1848 | 0 | prev_obu_type != OBU_LAYER_CONFIGURATION_RECORD) { |
1849 | 0 | return 0; |
1850 | 22 | } else if (obu_type == OBU_OPERATING_POINT_SET && |
1851 | 0 | prev_obu_type == OBU_ATLAS_SEGMENT) { |
1852 | 0 | return 0; |
1853 | 0 | } |
1854 | 22 | } |
1855 | | // First OBU in a new xlayer group: any local config type is valid. |
1856 | 22 | return 1; |
1857 | 147 | } else if (obu_type == OBU_SEQUENCE_HEADER) { |
1858 | 0 | *state = TU_STATE_SEQUENCE_HEADER; |
1859 | 0 | return 1; |
1860 | 147 | } else if (is_frame_unit(obu_type, xlayer_id)) { |
1861 | 147 | *state = TU_STATE_FRAME_UINT_DATA; |
1862 | 147 | return 1; |
1863 | 147 | } else { |
1864 | 0 | return 0; // Invalid OBU type(such as global obus) during local info |
1865 | | // phase |
1866 | 0 | } |
1867 | | |
1868 | 21.6k | case TU_STATE_SEQUENCE_HEADER: |
1869 | 21.6k | if (obu_type == OBU_SEQUENCE_HEADER) { |
1870 | 4.35k | return 1; |
1871 | 17.3k | } else if (is_frame_unit(obu_type, xlayer_id)) { |
1872 | 17.3k | *state = TU_STATE_FRAME_UINT_DATA; |
1873 | 17.3k | return 1; |
1874 | 17.3k | } else { |
1875 | 1 | return 0; // Invalid OBU type(such as global obus) during sequence |
1876 | | // header phase |
1877 | 1 | } |
1878 | | |
1879 | 750 | case TU_STATE_FRAME_UINT_DATA: |
1880 | 750 | if (is_frame_unit(obu_type, xlayer_id)) { |
1881 | | // Within the same xlayer group: enforce existing type ordering. |
1882 | | // CI -> CI -> MFH -> MFH -> (BRT, QM, FGM, METADATA_prefix, |
1883 | | // METADATA_SHORT_prefix) -> coded_frame -> METADATA_suffix, |
1884 | | // METADATA_SHORT_suffix) |
1885 | 742 | if (obu_type == OBU_CONTENT_INTERPRETATION && |
1886 | 16 | prev_obu_type != OBU_CONTENT_INTERPRETATION) |
1887 | 1 | return 0; |
1888 | 741 | else if (obu_type == OBU_MULTI_FRAME_HEADER && |
1889 | 0 | (prev_obu_type != OBU_CONTENT_INTERPRETATION && |
1890 | 0 | prev_obu_type != OBU_MULTI_FRAME_HEADER)) |
1891 | 0 | return 0; |
1892 | 741 | else if (((is_metadata_obu(obu_type) && |
1893 | 666 | metadata_is_suffix == 0) || // prefix |
1894 | 75 | obu_type == OBU_BUFFER_REMOVAL_TIMING || |
1895 | 41 | obu_type == OBU_QUANTIZATION_MATRIX || |
1896 | 41 | obu_type == OBU_FILM_GRAIN_MODEL) && |
1897 | 700 | (is_coded_frame(prev_obu_type) || |
1898 | 700 | (is_metadata_obu(prev_obu_type) && |
1899 | 665 | metadata_is_suffix == 1))) // suffix |
1900 | 0 | return 0; |
1901 | 741 | else // other cases may be evaluated later |
1902 | 741 | return 1; |
1903 | 742 | } else { |
1904 | 8 | return 0; |
1905 | 8 | } |
1906 | | |
1907 | 0 | default: |
1908 | | // Invalid state |
1909 | 0 | return 0; |
1910 | 43.7k | } |
1911 | 43.7k | } |
1912 | | |
1913 | | // Check if the CLK is the first frame of a mlayer. |
1914 | | static void check_clk_in_a_layer(AV2_COMMON *const cm, |
1915 | | obu_info *current_frame_unit, |
1916 | 0 | obu_info *last_frame_unit) { |
1917 | 0 | if (current_frame_unit->obu_type == OBU_CLOSED_LOOP_KEY && |
1918 | 0 | last_frame_unit->obu_type != OBU_CLOSED_LOOP_KEY && |
1919 | 0 | current_frame_unit->mlayer_id == last_frame_unit->mlayer_id && |
1920 | 0 | current_frame_unit->display_order_hint == |
1921 | 0 | last_frame_unit->display_order_hint) { |
1922 | 0 | avm_internal_error( |
1923 | 0 | &cm->error, AVM_CODEC_UNSUP_BITSTREAM, |
1924 | 0 | "%s : a CLK should be the first frame of a mlayer. " |
1925 | 0 | "current obu %s, current oh " |
1926 | 0 | "%d, current mlayer_id %d, " |
1927 | 0 | "previous obu %s previous oh %d previous mlayer_id %d ", |
1928 | 0 | __func__, avm_obu_type_to_string(current_frame_unit->obu_type), |
1929 | 0 | current_frame_unit->display_order_hint, current_frame_unit->mlayer_id, |
1930 | 0 | avm_obu_type_to_string(last_frame_unit->obu_type), |
1931 | 0 | last_frame_unit->display_order_hint, last_frame_unit->mlayer_id); |
1932 | 0 | } |
1933 | 0 | } |
1934 | | |
1935 | | // Check the mlayer ids of frame units before the current hidden frame. |
1936 | | static void check_layerid_hidden_frame_units(AV2_COMMON *const cm, |
1937 | | obu_info *current_frame_unit, |
1938 | 0 | obu_info *last_frame_unit) { |
1939 | | //[H:layer0][H:layer1] not allowed |
1940 | | //[H:layer1][H:layer1] checked later : [H:layer1][H:layer1][S:layer1] ok, |
1941 | | //[H:layer1][H:layer1][S:layer0] not allowed [H:layer2][H:layer1] not allowed |
1942 | | //[S:layer0][H:layer1] checked later: |
1943 | | // 1) [S:layer0][H:layer1][S:layer1] maybe ok, |
1944 | | // 2) [S:layer0][H:layer1][S:layer0] not allowed |
1945 | | //[S:layer1][H:layer1] checked later : |
1946 | | // 1) [S:layer1][H:layer1][S:layer1] maybe ok (e.g. CLK[0], Bridge[0], TG[1]) |
1947 | | // 2) [S:layer1][H:layer1][S:layer0] not allowed |
1948 | | // 3) [S:layer1][H:layer1][S:layer2] not allowed |
1949 | | //[S:layer2][H:layer1] allowed |
1950 | 0 | if ((last_frame_unit->showable_frame == 0 && |
1951 | 0 | current_frame_unit->mlayer_id != last_frame_unit->mlayer_id)) { |
1952 | 0 | avm_internal_error( |
1953 | 0 | &cm->error, AVM_CODEC_UNSUP_BITSTREAM, |
1954 | 0 | "%s : hidden frames should proceed displayable frames in a " |
1955 | 0 | "layer:\n\tcurrent : " |
1956 | 0 | "(%s, OH%d, L%d, S%d)\n\t" |
1957 | 0 | "previous : (%s, OH%d, L%d, S%d)", |
1958 | 0 | __func__, avm_obu_type_to_string(current_frame_unit->obu_type), |
1959 | 0 | current_frame_unit->display_order_hint, current_frame_unit->mlayer_id, |
1960 | 0 | current_frame_unit->showable_frame, |
1961 | 0 | avm_obu_type_to_string(last_frame_unit->obu_type), |
1962 | 0 | last_frame_unit->display_order_hint, last_frame_unit->mlayer_id, |
1963 | 0 | last_frame_unit->showable_frame); |
1964 | 0 | } |
1965 | 0 | } |
1966 | | |
1967 | | // Check the mlayer ids of frame units before the current showable frame. |
1968 | | static void check_layerid_showable_frame_units( |
1969 | | AV2_COMMON *const cm, obu_info *current_frame_unit, |
1970 | 0 | obu_info *last_frame_unit, obu_info *last_displayable_frame_unit) { |
1971 | | //[H:layer0][*S:layer1] not allowed |
1972 | | // 3) [S:layer1][H:layer1][*S:layer2] not allowed |
1973 | | //[H:layer1][*S:layer1] check last displayable frame unit |
1974 | | // 1) [S:layer0][H:layer1][*S:layer1] is allowed |
1975 | | // 1) [S:layer1][H:layer1][*S:layer1] maybe okay - doh comparison required |
1976 | | // betwee S and S |
1977 | | //[H:layer2][*S:layer1] check last displayable frame unit |
1978 | | // 2) [S:layer0][H:layer1][*S:layer0] not allowed |
1979 | | // 2) [S:layer1][H:layer1][*S:layer0] not allowed |
1980 | | |
1981 | | //[S:layer0][*S:layer1] allowed |
1982 | | //[S:layer1][*S:layer1] check orderhint of [S:layer1] and [S:layer1] |
1983 | | //[S:layer2][*S:layer1] check orderhint of [S:layer2] and [S:layer1] |
1984 | |
|
1985 | 0 | if (last_frame_unit->showable_frame == 0 && |
1986 | 0 | current_frame_unit->mlayer_id != last_frame_unit->mlayer_id) { |
1987 | 0 | avm_internal_error( |
1988 | 0 | &cm->error, AVM_CODEC_UNSUP_BITSTREAM, |
1989 | 0 | "%s : hidden frames should proceed displayable frames in a " |
1990 | 0 | "layer:\n\tcurrent : " |
1991 | 0 | "(%s, OH%d, L%d, S%d)\n\t" |
1992 | 0 | "previous : (%s, OH%d, L%d, S%d)", |
1993 | 0 | __func__, avm_obu_type_to_string(current_frame_unit->obu_type), |
1994 | 0 | current_frame_unit->display_order_hint, current_frame_unit->mlayer_id, |
1995 | 0 | current_frame_unit->showable_frame, |
1996 | 0 | avm_obu_type_to_string(last_frame_unit->obu_type), |
1997 | 0 | last_frame_unit->display_order_hint, last_frame_unit->mlayer_id, |
1998 | 0 | last_frame_unit->showable_frame); |
1999 | 0 | } else if (last_frame_unit->showable_frame == 0 && |
2000 | 0 | current_frame_unit->mlayer_id == last_frame_unit->mlayer_id && |
2001 | 0 | current_frame_unit->mlayer_id == |
2002 | 0 | last_displayable_frame_unit->mlayer_id) { |
2003 | 0 | if (current_frame_unit->display_order_hint == |
2004 | 0 | last_displayable_frame_unit->display_order_hint) { |
2005 | 0 | avm_internal_error( |
2006 | 0 | &cm->error, AVM_CODEC_UNSUP_BITSTREAM, |
2007 | 0 | "%s: mlayer_id should be in ascending order or order_hint should be " |
2008 | 0 | "different:\n" |
2009 | 0 | "\tcurrent : (%s, OH%d, L%d, S%d)\n" |
2010 | 0 | "\tprevious : (%s, S%d)\n" |
2011 | 0 | "\tlast_displayable : (%s, OH%d, L%d)", |
2012 | 0 | __func__, avm_obu_type_to_string(current_frame_unit->obu_type), |
2013 | 0 | current_frame_unit->display_order_hint, current_frame_unit->mlayer_id, |
2014 | 0 | current_frame_unit->showable_frame, |
2015 | 0 | avm_obu_type_to_string(last_frame_unit->obu_type), |
2016 | 0 | last_frame_unit->showable_frame, |
2017 | 0 | avm_obu_type_to_string(last_displayable_frame_unit->obu_type), |
2018 | 0 | last_displayable_frame_unit->display_order_hint, |
2019 | 0 | last_displayable_frame_unit->mlayer_id); |
2020 | 0 | } |
2021 | 0 | } else if (last_frame_unit->showable_frame == 1 && |
2022 | 0 | current_frame_unit->mlayer_id <= last_frame_unit->mlayer_id) { |
2023 | 0 | if (current_frame_unit->display_order_hint == |
2024 | 0 | last_frame_unit->display_order_hint) { |
2025 | 0 | avm_internal_error( |
2026 | 0 | &cm->error, AVM_CODEC_UNSUP_BITSTREAM, |
2027 | 0 | "%s: mlayer_id should be in ascending order or order_hint should be " |
2028 | 0 | "different:\n\tcurrent obu %s, current oh " |
2029 | 0 | "%d, current mlayer_id %d\n\t" |
2030 | 0 | "previous obu %s previous oh %d previous mlayer_id %d", |
2031 | 0 | __func__, avm_obu_type_to_string(current_frame_unit->obu_type), |
2032 | 0 | current_frame_unit->display_order_hint, current_frame_unit->mlayer_id, |
2033 | 0 | avm_obu_type_to_string(last_frame_unit->obu_type), |
2034 | 0 | last_frame_unit->display_order_hint, last_frame_unit->mlayer_id); |
2035 | 0 | } |
2036 | 0 | } |
2037 | 0 | } |
2038 | | |
2039 | | // Check xlayer_id, mlayer_id, and tlayer_id of the obu is valid for the |
2040 | | // obu_type. Reports with avm_internal_error. |
2041 | 93.4k | static void check_valid_layer_id(ObuHeader obu_header, AV2_COMMON *const cm) { |
2042 | | // Ignore reserved OBUs. |
2043 | 93.4k | if (!avm_obu_type_is_valid(obu_header.type)) return; |
2044 | | |
2045 | 43.9k | if (obu_header.obu_xlayer_id == GLOBAL_XLAYER_ID && |
2046 | 1.65k | (obu_header.obu_tlayer_id != 0 || obu_header.obu_mlayer_id != 0)) { |
2047 | 169 | avm_internal_error( |
2048 | 169 | &cm->error, AVM_CODEC_UNSUP_BITSTREAM, |
2049 | 169 | "Incorrect layer_id for %s: tlayer_id %d mlayer_id %d xlayer_id %d", |
2050 | 169 | avm_obu_type_to_string(obu_header.type), obu_header.obu_tlayer_id, |
2051 | 169 | obu_header.obu_mlayer_id, obu_header.obu_xlayer_id); |
2052 | 169 | } |
2053 | 43.9k | if (obu_header.type == OBU_MULTI_STREAM_DECODER_OPERATION && |
2054 | 1.45k | obu_header.obu_xlayer_id != GLOBAL_XLAYER_ID) { |
2055 | 1 | avm_internal_error(&cm->error, AVM_CODEC_UNSUP_BITSTREAM, |
2056 | 1 | "Incorrect obu_xlayer_id for MSDO: %d", |
2057 | 1 | obu_header.obu_xlayer_id); |
2058 | 1 | } |
2059 | 43.9k | if (obu_header.type == OBU_SEQUENCE_HEADER || |
2060 | 21.5k | obu_header.type == OBU_TEMPORAL_DELIMITER || |
2061 | 21.5k | obu_header.type == OBU_LAYER_CONFIGURATION_RECORD || |
2062 | 21.3k | obu_header.type == OBU_OPERATING_POINT_SET || |
2063 | 22.4k | obu_header.type == OBU_ATLAS_SEGMENT) { |
2064 | 22.4k | if (obu_header.obu_tlayer_id != 0 || obu_header.obu_mlayer_id != 0) |
2065 | 39 | avm_internal_error(&cm->error, AVM_CODEC_UNSUP_BITSTREAM, |
2066 | 39 | "Incorrect layer_id for %s: " |
2067 | 39 | "tlayer_id %d mlayer_id %d", |
2068 | 39 | avm_obu_type_to_string(obu_header.type), |
2069 | 39 | obu_header.obu_tlayer_id, obu_header.obu_mlayer_id); |
2070 | 22.4k | } |
2071 | | |
2072 | | // MSDO, LCR, OPS, Atlas, Short Metadata OBU, Metadata Group OBU, Padding, |
2073 | | // Temporal Delimiter, and Buffer removal timing. |
2074 | 43.9k | if (obu_header.obu_xlayer_id == GLOBAL_XLAYER_ID && |
2075 | 1.48k | !(obu_header.type == OBU_TEMPORAL_DELIMITER || |
2076 | 1.45k | obu_header.type == OBU_METADATA_SHORT || |
2077 | 1.45k | obu_header.type == OBU_METADATA_GROUP || |
2078 | 1.45k | obu_header.type == OBU_BUFFER_REMOVAL_TIMING || |
2079 | 1.45k | obu_header.type == OBU_LAYER_CONFIGURATION_RECORD || |
2080 | 1.45k | obu_header.type == OBU_ATLAS_SEGMENT || |
2081 | 1.45k | obu_header.type == OBU_OPERATING_POINT_SET || |
2082 | 1.45k | obu_header.type == OBU_MULTI_STREAM_DECODER_OPERATION || |
2083 | 0 | obu_header.type == OBU_PADDING)) { |
2084 | 0 | avm_internal_error(&cm->error, AVM_CODEC_UNSUP_BITSTREAM, |
2085 | 0 | "Incorrect layer_id for %s: xlayer_id %d", |
2086 | 0 | avm_obu_type_to_string(obu_header.type), |
2087 | 0 | obu_header.obu_xlayer_id); |
2088 | 0 | } |
2089 | | |
2090 | | // CLK/OLK are only present in temporal layer 0 |
2091 | 43.9k | if ((obu_header.type == OBU_CLOSED_LOOP_KEY || |
2092 | 26.0k | obu_header.type == OBU_OPEN_LOOP_KEY) && |
2093 | 17.7k | obu_header.obu_tlayer_id != 0) { |
2094 | 19 | avm_internal_error(&cm->error, AVM_CODEC_UNSUP_BITSTREAM, |
2095 | 19 | "Incorrect tlayer_id for %s: tlayer_id %d", |
2096 | 19 | avm_obu_type_to_string(obu_header.type), |
2097 | 19 | obu_header.obu_tlayer_id); |
2098 | 19 | } |
2099 | 43.9k | } |
2100 | | |
2101 | 1.36k | static int get_msdo_interop(struct AV2Decoder *pbi) { |
2102 | 1.36k | return av2_get_interop_from_profile( |
2103 | 1.36k | pbi->common.msdo_params.multistream_profile_idc); |
2104 | 1.36k | } |
2105 | | |
2106 | 1.36k | static int get_lcr_global_interop(struct AV2Decoder *pbi) { |
2107 | | // Use the activated global LCR (not just any valid one), and only its |
2108 | | // aggregate PTL; lcr_seq_profile_idc is not an Annex A IOP source. |
2109 | 1.36k | const AV2_COMMON *const cm = &pbi->common; |
2110 | 1.36k | const struct GlobalLayerConfigurationRecord *glcr = |
2111 | 1.36k | (cm->lcr_params.valid && cm->lcr_params.is_global) |
2112 | 1.36k | ? &cm->lcr_params.global_lcr |
2113 | 1.36k | : (cm->global_lcr_params.valid ? &cm->global_lcr_params.global_lcr |
2114 | 1.36k | : NULL); |
2115 | 1.36k | if (glcr != NULL && glcr->lcr_aggregate_info_present_flag) |
2116 | 0 | return glcr->aggregate_ptl.lcr_max_interop; |
2117 | | // lcr_max_interop is inferred to be the maximum when absent. |
2118 | 1.36k | return INTEROP_MAX; |
2119 | 1.36k | } |
2120 | | |
2121 | 1.36k | static int get_lcr_local_interop(struct AV2Decoder *pbi) { |
2122 | | // A local LCR governs a single extended layer; use its own seq_profile_idc. |
2123 | 1.36k | return av2_get_interop_from_profile(pbi->common.seq_params.seq_profile_idc); |
2124 | 1.36k | } |
2125 | | |
2126 | | // Returns true if the Annex A OBU requirements table does not constrain this |
2127 | | // interoperability point: INTEROP_NONE (the CONFIGURABLE profile) and any IOP |
2128 | | // >= INTEROP_3 (reserved and max) are unconstrained. INTEROP_INVALID (a |
2129 | | // reserved profile) is not exempted, so such a stream is rejected here. |
2130 | 681 | static bool interop_is_unconstrained(int interop) { |
2131 | 681 | return interop == INTEROP_NONE || interop >= INTEROP_3; |
2132 | 681 | } |
2133 | | |
2134 | | // Conformance check for the presence of MSDO and LCR. |
2135 | | // The OBU requirements table in Annex A only applies when IOP < 3, i.e. |
2136 | | // when the MSDO profile or LCR profile has an interoperability point of |
2137 | | // 0, 1 or 2. For the CONFIGURABLE profile and for IOPs >= 3 the table does |
2138 | | // not impose additional constraints, so we return true. |
2139 | | // |
2140 | | // The checks below are expressed in terms of interoperability points rather |
2141 | | // than individual profile labels, so that profiles sharing an IOP (e.g. |
2142 | | // MAIN_420_10_IP2 and MAIN_444C_12_IP2) are treated identically and newly |
2143 | | // added profiles need no change here. |
2144 | | bool conformance_check_msdo_lcr(struct AV2Decoder *pbi, bool global_lcr_present, |
2145 | 1.36k | bool local_lcr_present) { |
2146 | 1.36k | int msdo_present = pbi->multistream_decoder_mode; |
2147 | 1.36k | int num_extended_layers = 0; |
2148 | 1.36k | int num_embedded_layers = 0; |
2149 | | |
2150 | 43.8k | for (int i = 0; i < AVM_MAX_NUM_STREAMS - 1; i++) { |
2151 | 42.4k | if (pbi->xlayer_id_map[i] > 0) num_extended_layers++; |
2152 | 42.4k | } |
2153 | 12.3k | for (int i = 0; i < MAX_NUM_MLAYERS; i++) { |
2154 | 10.9k | bool found = false; |
2155 | 325k | for (int x = 0; x < AVM_MAX_NUM_STREAMS; x++) { |
2156 | 315k | if (pbi->mlayer_id_map[x][i] > 0) { |
2157 | 1.44k | found = true; |
2158 | 1.44k | break; |
2159 | 1.44k | } |
2160 | 315k | } |
2161 | 10.9k | if (found) num_embedded_layers++; |
2162 | 10.9k | } |
2163 | 1.36k | assert(num_extended_layers > 0 && num_embedded_layers > 0); |
2164 | | |
2165 | | // Determine the effective MSDO and LCR interoperability points. |
2166 | 1.36k | const int msdo_iop = get_msdo_interop(pbi); |
2167 | 1.36k | const int glcr_iop = get_lcr_global_interop(pbi); |
2168 | 1.36k | const int llcr_iop = get_lcr_local_interop(pbi); |
2169 | | |
2170 | | // The IOP table only applies for IOPs 0-2. |
2171 | 1.36k | if (msdo_present && interop_is_unconstrained(msdo_iop)) return true; |
2172 | 1.36k | if (global_lcr_present && interop_is_unconstrained(glcr_iop)) return true; |
2173 | 1.36k | if (!msdo_present && !global_lcr_present && local_lcr_present && |
2174 | 582 | interop_is_unconstrained(llcr_iop)) { |
2175 | 451 | return true; |
2176 | 451 | } |
2177 | 918 | if (!msdo_present && !global_lcr_present && !local_lcr_present) { |
2178 | 688 | return true; |
2179 | 688 | } |
2180 | | |
2181 | 230 | if (num_extended_layers == 1 && num_embedded_layers == 1) { |
2182 | 177 | if (!msdo_present) return true; |
2183 | 177 | } |
2184 | | |
2185 | 143 | if (num_extended_layers > 1 && num_embedded_layers == 1) { |
2186 | | // Every IOP constrained by the table (0..2) allows multiple extended layers |
2187 | | // with a single embedded layer. |
2188 | 17 | if (msdo_present && msdo_iop >= INTEROP_0) return true; |
2189 | | |
2190 | 17 | if (global_lcr_present && glcr_iop >= INTEROP_2) return true; |
2191 | 17 | } |
2192 | | |
2193 | 143 | if (num_extended_layers == 1 && num_embedded_layers > 1) { |
2194 | | // Multiple embedded layers require IOP >= 1. |
2195 | 0 | if (!msdo_present && local_lcr_present && llcr_iop >= INTEROP_1) |
2196 | 0 | return true; |
2197 | | |
2198 | | // Each interoperability point is only consulted when the corresponding LCR |
2199 | | // is actually present, so that the permissive fallback returned by |
2200 | | // get_lcr_{global,local}_interop() for an absent LCR cannot allow a |
2201 | | // configuration the present LCR's IOP forbids. |
2202 | 0 | if (!msdo_present && ((global_lcr_present && glcr_iop >= INTEROP_2) || |
2203 | 0 | (local_lcr_present && llcr_iop >= INTEROP_2))) |
2204 | 0 | return true; |
2205 | 0 | } |
2206 | | |
2207 | 143 | if (num_extended_layers > 1 && num_embedded_layers > 1) { |
2208 | | // Combinations of multiple extended and embedded layers require IOP 2. |
2209 | 36 | if (msdo_present && local_lcr_present && msdo_iop >= INTEROP_2) return true; |
2210 | | |
2211 | 36 | if (global_lcr_present && glcr_iop >= INTEROP_2) return true; |
2212 | 36 | } |
2213 | 143 | return false; |
2214 | 143 | } |
2215 | | |
2216 | | static int get_ops_mlayer_count(const struct OpsMLayerInfo *mlayer_info, |
2217 | 0 | int xlayer_id) { |
2218 | 0 | if (mlayer_info == NULL) return 0; |
2219 | 0 | if (xlayer_id < 0 || xlayer_id >= MAX_NUM_XLAYERS) return 0; |
2220 | 0 | return mlayer_info->OPMLayerCount[xlayer_id]; |
2221 | 0 | } |
2222 | | |
2223 | | static int get_ops_tlayer_count(const struct OpsMLayerInfo *mlayer_info, |
2224 | 0 | int xlayer_id, int mlayer_id) { |
2225 | 0 | if (mlayer_info == NULL) return 0; |
2226 | 0 | if (xlayer_id < 0 || xlayer_id >= MAX_NUM_XLAYERS) return 0; |
2227 | 0 | if (mlayer_id < 0 || mlayer_id >= MAX_NUM_MLAYERS) return 0; |
2228 | | |
2229 | 0 | return mlayer_info->OPTLayerCount[xlayer_id][mlayer_id]; |
2230 | 0 | } |
2231 | | |
2232 | | // Default operating point set id |
2233 | | static const int default_ops_id = 0; |
2234 | | // Default operating point index |
2235 | | static const int default_op_index = 0; |
2236 | | |
2237 | | // Select the operating point for the current xlayer. |
2238 | | // If the user has explicitly set an OPS via codec control, use that; |
2239 | | // otherwise fall back to the default (ops_id=0, op_index=0). |
2240 | | // This function determines the OPS ID to use: |
2241 | | // 1. selected_ops_id if >= 0 (user-specified via AV2D_SET_SELECTED_OPS) |
2242 | | // 2. otherwise, default_ops_id (0) |
2243 | | // |
2244 | | // When sbe_state.retention_map_ready is set, num_mlayers and num_tlayers are |
2245 | | // derived directly from the SBE retention map so that the decoder layer counts |
2246 | | // are consistent with the OBU-level filtering already applied by Annex F. |
2247 | | // When the retention map is not ready (no OPS selected, SBE disabled, or map |
2248 | | // not yet built), the decoder decodes all layers in the bitstream. |
2249 | | static void avm_set_current_operating_point(struct AV2Decoder *pbi, |
2250 | 0 | int xlayer_id) { |
2251 | 0 | struct DecOperatingPointParams *dec_op_params = &pbi->dec_op_params; |
2252 | | |
2253 | | // If the Annex F retention map is ready, derive num_mlayers and num_tlayers |
2254 | | // from it so that the decoder layer counts match the OBU filter exactly. |
2255 | | // This ensures avm_set_current_operating_point and the SBE are always |
2256 | | // consistent and share the same source of truth. |
2257 | 0 | if (pbi->sbe_state.retention_map_ready && pbi->sbe_state.extraction_enabled) { |
2258 | 0 | int xid = xlayer_id; |
2259 | 0 | if (xid < 0 || xid >= MAX_NUM_XLAYERS) { |
2260 | 0 | avm_internal_error(&pbi->common.error, AVM_CODEC_UNSUP_BITSTREAM, |
2261 | 0 | "Invalid xlayer_id %d for operating point selection", |
2262 | 0 | xid); |
2263 | 0 | } |
2264 | | |
2265 | | // Count distinct mlayers retained for this xlayer. |
2266 | 0 | unsigned int num_mlayers = 0; |
2267 | 0 | for (int j = 0; j < MAX_NUM_MLAYERS; j++) { |
2268 | 0 | for (int k = 0; k < MAX_NUM_TLAYERS; k++) { |
2269 | 0 | if (pbi->sbe_state.retention_map[xid][j][k]) { |
2270 | 0 | num_mlayers = j + 1; |
2271 | 0 | break; |
2272 | 0 | } |
2273 | 0 | } |
2274 | 0 | } |
2275 | | |
2276 | | // Count distinct tlayers retained for the base mlayer of this xlayer. |
2277 | 0 | unsigned int num_tlayers = 0; |
2278 | 0 | for (int k = 0; k < MAX_NUM_TLAYERS; k++) { |
2279 | 0 | for (int j = 0; j < MAX_NUM_MLAYERS; j++) { |
2280 | 0 | if (pbi->sbe_state.retention_map[xid][j][k]) { |
2281 | 0 | num_tlayers = k + 1; |
2282 | 0 | break; |
2283 | 0 | } |
2284 | 0 | } |
2285 | 0 | } |
2286 | |
|
2287 | 0 | dec_op_params->num_mlayers = num_mlayers > 0 ? num_mlayers : 1; |
2288 | 0 | dec_op_params->num_tlayers = num_tlayers > 0 ? num_tlayers : 1; |
2289 | 0 | dec_op_params->isValid = 1; |
2290 | | // dec_ops/dec_op/DecOpSetId etc. are still populated below from the OPS |
2291 | | // for use by other decoder subsystems that inspect those fields. |
2292 | 0 | } |
2293 | |
|
2294 | 0 | int target_ops_id = |
2295 | 0 | pbi->selected_ops_id >= 0 ? pbi->selected_ops_id : default_ops_id; |
2296 | | |
2297 | | // Only proceed to check for the operating point iff the ops id is valid |
2298 | 0 | if (target_ops_id < 0 || target_ops_id >= MAX_NUM_OPS_ID) return; |
2299 | | |
2300 | | // Determine the target op point index. |
2301 | 0 | int target_op_index = |
2302 | 0 | (pbi->selected_ops_id >= 0) ? pbi->selected_op_index : default_op_index; |
2303 | |
|
2304 | 0 | struct OperatingPointSet *ops = &pbi->ops_list[xlayer_id][target_ops_id]; |
2305 | 0 | dec_op_params->dec_ops = ops; |
2306 | 0 | dec_op_params->dec_op = dec_op_params->dec_ops->op; |
2307 | 0 | if (!ops->valid || ops->ops_id != target_ops_id) { |
2308 | 0 | dec_op_params->DecOpSetId = 0; |
2309 | 0 | dec_op_params->DecOpCount = 0; |
2310 | 0 | dec_op_params->DecOpIndex = 0; |
2311 | | // If SBE retention map is not ready or not enabled, decode all layers. |
2312 | 0 | if (!pbi->sbe_state.retention_map_ready || |
2313 | 0 | !pbi->sbe_state.extraction_enabled) { |
2314 | 0 | dec_op_params->num_mlayers = MAX_NUM_MLAYERS; |
2315 | 0 | dec_op_params->num_tlayers = MAX_NUM_TLAYERS; |
2316 | 0 | } |
2317 | 0 | dec_op_params->isValid = 0; // No matching OPS |
2318 | 0 | dec_op_params->dec_ops = NULL; |
2319 | 0 | dec_op_params->dec_op = NULL; |
2320 | | // No OPS was available, all layers to be decoded. |
2321 | 0 | return; |
2322 | 0 | } |
2323 | | |
2324 | | // NOTE: if a GLOBAL vs. LOCAL OPS is to be used, then |
2325 | | // Additional logic can be placed here to differentiate between |
2326 | | // the global and local OPS |
2327 | 0 | if (target_op_index >= 0 && target_op_index < ops->ops_cnt) { |
2328 | 0 | struct OperatingPoint *op = &ops->op[target_op_index]; |
2329 | 0 | dec_op_params->DecOpSetId = ops->ops_id; |
2330 | 0 | dec_op_params->DecOpCount = ops->ops_cnt; |
2331 | 0 | dec_op_params->DecOpIndex = target_op_index; |
2332 | 0 | dec_op_params->DecXlayerId = ops->obu_xlayer_id; |
2333 | 0 | int xlayer = dec_op_params->DecXlayerId; |
2334 | | // Only overwrite num_mlayers/num_tlayers from OPS if the SBE retention |
2335 | | // map has not already set them. The SBE map is the authoritative source |
2336 | | // when Annex F extraction is active. |
2337 | 0 | if (!pbi->sbe_state.retention_map_ready || |
2338 | 0 | !pbi->sbe_state.extraction_enabled) { |
2339 | 0 | dec_op_params->num_mlayers = |
2340 | 0 | get_ops_mlayer_count(&op->mlayer_info, xlayer); |
2341 | 0 | dec_op_params->num_tlayers = |
2342 | 0 | get_ops_tlayer_count(&op->mlayer_info, xlayer, pbi->common.mlayer_id); |
2343 | 0 | if (dec_op_params->num_mlayers == 0) dec_op_params->num_mlayers = 1; |
2344 | 0 | if (dec_op_params->num_tlayers == 0) dec_op_params->num_tlayers = 1; |
2345 | 0 | } |
2346 | 0 | dec_op_params->dec_op = op; |
2347 | 0 | dec_op_params->isValid = 1; |
2348 | 0 | return; |
2349 | 0 | } |
2350 | 0 | return; |
2351 | 0 | } |
2352 | | |
2353 | | // On success, sets *p_data_end and returns a boolean that indicates whether |
2354 | | // the decoding of the current frame is finished. On failure, sets |
2355 | | // cm->error.error_code and returns -1. |
2356 | | int avm_decode_frame_from_obus(struct AV2Decoder *pbi, const uint8_t *data, |
2357 | | const uint8_t *data_end, |
2358 | 21.5k | const uint8_t **p_data_end) { |
2359 | | #if CONFIG_COLLECT_COMPONENT_TIMING |
2360 | | start_timing(pbi, avm_decode_frame_from_obus_time); |
2361 | | #endif |
2362 | 21.5k | AV2_COMMON *const cm = &pbi->common; |
2363 | 21.5k | int frame_decoding_finished = 0; |
2364 | 21.5k | ObuHeader obu_header; |
2365 | 21.5k | memset(&obu_header, 0, sizeof(obu_header)); |
2366 | | |
2367 | | // Enable is_multistream if multiple extended layers are present. |
2368 | | // Enable multistream_decoder_mode only when an MSDO OBU is present. |
2369 | 21.5k | if (pbi->obus_in_frame_unit_data[0][0][OBU_MULTI_STREAM_DECODER_OPERATION]) { |
2370 | 1.48k | pbi->is_multistream = 1; |
2371 | 1.48k | pbi->multistream_decoder_mode = 1; |
2372 | 20.0k | } else if (pbi->glcr_is_present_in_tu) { |
2373 | 0 | pbi->is_multistream = 1; |
2374 | 0 | } |
2375 | | |
2376 | | // Check for CMVS end at a random access point TU without MSDO. |
2377 | | // Per spec, the CMVS ends when a new CVS begins without an activated |
2378 | | // global LCR. prescan_glcr_will_activate is computed during OBU scanning |
2379 | | // (check_frame_unit_data) by extracting seq_lcr_id from the frame header's |
2380 | | // sequence header, then checking local LCRs first and falling back to |
2381 | | // global LCRs (replicating find_active_lcr logic). |
2382 | 21.5k | if (pbi->is_multistream) { |
2383 | 6.93k | int tid = pbi->current_tlayer_id; |
2384 | 6.93k | int mid = pbi->current_mlayer_id; |
2385 | 6.93k | if (tid >= 0 && mid >= 0 && |
2386 | 6.93k | pbi->obus_in_frame_unit_data[0][0][OBU_TEMPORAL_DELIMITER] && |
2387 | 7 | !pbi->obus_in_frame_unit_data[0][0] |
2388 | 7 | [OBU_MULTI_STREAM_DECODER_OPERATION] && |
2389 | 2 | (pbi->obus_in_frame_unit_data[tid][mid][OBU_CLOSED_LOOP_KEY] || |
2390 | 0 | pbi->obus_in_frame_unit_data[tid][mid][OBU_OPEN_LOOP_KEY] || |
2391 | 2 | pbi->obus_in_frame_unit_data[tid][mid][OBU_RAS_FRAME])) { |
2392 | 2 | if (!pbi->prescan_glcr_will_activate) { |
2393 | | // Flush and reset like a config change |
2394 | 2 | if (pbi->stream_info != NULL) { |
2395 | 2 | flush_all_xlayer_frames(pbi, cm, true); |
2396 | 2 | avm_free(pbi->stream_info); |
2397 | 2 | pbi->stream_info = NULL; |
2398 | 2 | pbi->glcr_stream_info_num_allocated = 0; |
2399 | 2 | } |
2400 | 2 | cm->num_streams = 0; |
2401 | 66 | for (int i = 0; i < AVM_MAX_NUM_STREAMS; i++) pbi->xlayer_id_map[i] = 0; |
2402 | | |
2403 | 2 | pbi->is_multistream = 0; |
2404 | 2 | } |
2405 | 2 | pbi->multistream_decoder_mode = 0; |
2406 | 2 | } |
2407 | 6.93k | } |
2408 | | |
2409 | | // Per-TU conformance check: validate the PREVIOUS TU's accumulated |
2410 | | // xlayer/mlayer maps before resetting them for the current TU. |
2411 | | // This must run once per TU (here), not per-OBU (inside the loop). |
2412 | 21.5k | if (pbi->random_accessed) { |
2413 | 1.42k | int num_xlayers = 0; |
2414 | 1.42k | int num_mlayers = 0; |
2415 | 45.6k | for (int i = 0; i < AVM_MAX_NUM_STREAMS - 1; i++) { |
2416 | 44.2k | if (pbi->xlayer_id_map[i] > 0) num_xlayers++; |
2417 | 44.2k | } |
2418 | 12.8k | for (int i = 0; i < MAX_NUM_MLAYERS; i++) { |
2419 | 11.4k | bool found = false; |
2420 | 340k | for (int x = 0; x < AVM_MAX_NUM_STREAMS; x++) { |
2421 | 330k | if (pbi->mlayer_id_map[x][i] > 0) { |
2422 | 1.44k | found = true; |
2423 | 1.44k | break; |
2424 | 1.44k | } |
2425 | 330k | } |
2426 | 11.4k | if (found) num_mlayers++; |
2427 | 11.4k | } |
2428 | | |
2429 | 1.42k | if (num_xlayers > 0 && num_mlayers > 0) { |
2430 | 1.36k | bool global_lcr_present = false; |
2431 | 1.36k | bool local_lcr_present = false; |
2432 | 12.3k | for (int j = 0; j < MAX_NUM_LCR; j++) { |
2433 | 10.9k | if (pbi->lcr_list[GLOBAL_XLAYER_ID][j].valid) global_lcr_present = true; |
2434 | 10.9k | } |
2435 | 24.9k | for (int i = 0; i < GLOBAL_XLAYER_ID && !local_lcr_present; i++) { |
2436 | 209k | for (int j = 0; j < MAX_NUM_LCR; j++) { |
2437 | 186k | if (pbi->lcr_list[i][j].valid) { |
2438 | 629 | local_lcr_present = true; |
2439 | 629 | break; |
2440 | 629 | } |
2441 | 186k | } |
2442 | 23.5k | } |
2443 | | |
2444 | 1.36k | if (!conformance_check_msdo_lcr(pbi, global_lcr_present, |
2445 | 1.36k | local_lcr_present)) { |
2446 | 143 | avm_internal_error( |
2447 | 143 | &cm->error, AVM_CODEC_UNSUP_BITSTREAM, |
2448 | 143 | "An MSDO or LCR OBU in the current CVS violates the requirements " |
2449 | 143 | "of bitstream conformance for MSDO and LCR"); |
2450 | 143 | } |
2451 | 1.36k | } |
2452 | | |
2453 | | // Reset maps for the current TU |
2454 | 41.1k | for (int i = 0; i < AVM_MAX_NUM_STREAMS - 1; i++) pbi->xlayer_id_map[i] = 0; |
2455 | 42.4k | for (int x = 0; x < AVM_MAX_NUM_STREAMS; x++) |
2456 | 369k | for (int i = 0; i < MAX_NUM_MLAYERS; i++) pbi->mlayer_id_map[x][i] = 0; |
2457 | 1.42k | } |
2458 | | |
2459 | 21.5k | pbi->seen_frame_header = 0; |
2460 | 21.5k | pbi->next_start_tile = 0; |
2461 | 21.5k | pbi->num_tile_groups = 0; |
2462 | 21.5k | pbi->msdo_is_present_in_tu = 0; |
2463 | 21.5k | pbi->glcr_is_present_in_tu = 0; |
2464 | | |
2465 | 21.5k | if (data_end < data) { |
2466 | 0 | cm->error.error_code = AVM_CODEC_CORRUPT_FRAME; |
2467 | 0 | return -1; |
2468 | 0 | } |
2469 | | |
2470 | 21.5k | int count_obus_with_frame_unit = 0; |
2471 | 21.5k | obu_info *obu_list = pbi->obu_list; |
2472 | 21.5k | uint32_t acc_qm_id_bitmap = 0; |
2473 | 21.5k | int qm_bit_map_zero_signalled = 0; |
2474 | | // acc_fgm_id_bitmap accumulates fgm_id_bitmap in FGM OBU to check if film |
2475 | | // grain models signalled before a coded frame have the same fgm_id |
2476 | 21.5k | uint32_t acc_fgm_id_bitmap = 0; |
2477 | 21.5k | uint32_t acc_mfh_id_bitmap = 0; |
2478 | 21.5k | uint16_t acc_sh_id_bitmap[MAX_NUM_XLAYERS] = { 0 }; |
2479 | 21.5k | uint8_t acc_lcr_id_bitmap[MAX_NUM_XLAYERS] = { 0 }; |
2480 | 21.5k | int prev_obu_xlayer_id = -1; |
2481 | | // prev_obu_type, prev_xlayer_id and tu_validation_state are used to compare |
2482 | | // obus in this "data" |
2483 | 21.5k | OBU_TYPE prev_obu_type = NUM_OBU_TYPES; |
2484 | 21.5k | int prev_xlayer_id = -1; |
2485 | 21.5k | temporal_unit_state_t tu_validation_state = TU_STATE_START; |
2486 | | |
2487 | | // decode frame as a series of OBUs |
2488 | 93.7k | while (!frame_decoding_finished && cm->error.error_code == AVM_CODEC_OK) { |
2489 | 93.5k | struct avm_read_bit_buffer rb; |
2490 | 93.5k | size_t payload_size = 0; |
2491 | 93.5k | size_t decoded_payload_size = 0; |
2492 | 93.5k | size_t obu_payload_offset = 0; |
2493 | 93.5k | size_t bytes_read = 0; |
2494 | 93.5k | const size_t bytes_available = data_end - data; |
2495 | | |
2496 | 93.5k | if (bytes_available == 0 && !pbi->seen_frame_header) { |
2497 | 0 | cm->error.error_code = AVM_CODEC_OK; |
2498 | 0 | break; |
2499 | 0 | } |
2500 | | |
2501 | 93.5k | avm_codec_err_t status = avm_read_obu_header_and_size( |
2502 | 93.5k | data, bytes_available, &obu_header, &payload_size, &bytes_read); |
2503 | | |
2504 | 93.5k | if (status != AVM_CODEC_OK) { |
2505 | 0 | cm->error.error_code = status; |
2506 | 0 | return -1; |
2507 | 0 | } |
2508 | | |
2509 | | // Note: avm_read_obu_header_and_size() takes care of checking that this |
2510 | | // doesn't cause 'data' to advance past 'data_end'. |
2511 | 93.5k | data += bytes_read; |
2512 | | |
2513 | 93.5k | if ((size_t)(data_end - data) < payload_size) { |
2514 | 0 | cm->error.error_code = AVM_CODEC_CORRUPT_FRAME; |
2515 | 0 | return -1; |
2516 | 0 | } |
2517 | | |
2518 | | // Annex F: Sub-bitstream extraction. |
2519 | | // When extraction is enabled, trigger retention map construction when |
2520 | | // transitioning from structural OBUs to non-structural OBUs, then |
2521 | | // filter OBUs based on the retention map. |
2522 | 93.5k | if (pbi->sbe_state.extraction_enabled) { |
2523 | 0 | if (!pbi->sbe_state.retention_map_ready && |
2524 | 0 | !is_sbe_structural_obu(obu_header.type)) { |
2525 | 0 | av2_sbe_build_retention_map(&pbi->sbe_state, pbi, |
2526 | 0 | obu_header.obu_xlayer_id); |
2527 | 0 | } |
2528 | 0 | if (pbi->sbe_state.retention_map_ready) { |
2529 | 0 | if (!av2_sbe_should_retain_obu( |
2530 | 0 | &pbi->sbe_state, obu_header.type, obu_header.obu_xlayer_id, |
2531 | 0 | obu_header.obu_mlayer_id, obu_header.obu_tlayer_id)) { |
2532 | 0 | pbi->sbe_state.obus_removed++; |
2533 | 0 | data += payload_size; |
2534 | 0 | continue; |
2535 | 0 | } |
2536 | 0 | pbi->sbe_state.obus_retained++; |
2537 | 0 | } |
2538 | 0 | } |
2539 | | |
2540 | 93.5k | if (av2_is_leading_vcl_obu(obu_header.type)) |
2541 | 345 | cm->is_leading_picture = 1; |
2542 | 93.1k | else if (av2_is_regular_vcl_obu(obu_header.type)) |
2543 | 426 | cm->is_leading_picture = 0; |
2544 | 92.7k | else |
2545 | 92.7k | cm->is_leading_picture = -1; |
2546 | 93.5k | pbi->xlayer_id_map[obu_header.obu_xlayer_id] = 1; |
2547 | 93.5k | pbi->mlayer_id_map[obu_header.obu_xlayer_id][obu_header.obu_mlayer_id] = 1; |
2548 | | |
2549 | 93.5k | obu_info *const curr_obu_info = &obu_list[count_obus_with_frame_unit]; |
2550 | 93.5k | curr_obu_info->obu_type = obu_header.type; |
2551 | 93.5k | curr_obu_info->is_vcl = is_single_tile_vcl_obu(obu_header.type) || |
2552 | 93.1k | is_multi_tile_vcl_obu(obu_header.type); |
2553 | 93.5k | if (curr_obu_info->is_vcl) { |
2554 | 18.4k | assert(curr_obu_info->xlayer_id == obu_header.obu_xlayer_id); |
2555 | 18.4k | assert(curr_obu_info->mlayer_id == obu_header.obu_mlayer_id); |
2556 | 18.4k | assert(curr_obu_info->tlayer_id == obu_header.obu_tlayer_id); |
2557 | 18.4k | } |
2558 | 93.5k | curr_obu_info->first_tile_group = -1; |
2559 | 93.5k | curr_obu_info->immediate_output_picture = -1; |
2560 | 93.5k | curr_obu_info->showable_frame = -1; |
2561 | 93.5k | curr_obu_info->display_order_hint = -1; |
2562 | | |
2563 | | // Extract metadata_is_suffix for metadata OBUs |
2564 | | // Per spec sections 5.17.2 and 5.17.3, metadata_is_suffix is f(1) - the |
2565 | | // first bit of the payload |
2566 | 93.5k | int metadata_is_suffix = -1; // -1 means not applicable |
2567 | 93.5k | if (obu_header.type == OBU_METADATA_SHORT || |
2568 | 92.6k | obu_header.type == OBU_METADATA_GROUP) { |
2569 | 813 | if (payload_size == 0) { |
2570 | 1 | avm_internal_error(&cm->error, AVM_CODEC_CORRUPT_FRAME, |
2571 | 1 | "OBU_METADATA_x has an empty payload"); |
2572 | 1 | } |
2573 | | // data has been advanced by bytes_read, so data[0] is first payload byte |
2574 | 813 | metadata_is_suffix = (data[0] & 0x80) >> 7; |
2575 | 813 | } |
2576 | | |
2577 | | // Validate OBU ordering within temporal units. Ignore padding OBUs and |
2578 | | // reserved OBUs in this check. |
2579 | 93.5k | if (avm_obu_type_is_valid(obu_header.type) && |
2580 | 44.0k | obu_header.type != OBU_PADDING) { |
2581 | 43.7k | if (!check_temporal_unit_structure( |
2582 | 43.7k | &tu_validation_state, obu_header.type, obu_header.obu_xlayer_id, |
2583 | 43.7k | metadata_is_suffix, prev_obu_type, prev_xlayer_id)) { |
2584 | 55 | avm_internal_error( |
2585 | 55 | &cm->error, AVM_CODEC_UNSUP_BITSTREAM, |
2586 | 55 | "OBU order violation: current OBU %s with xlayer_id %d mlayer_id " |
2587 | 55 | "%d previous OBU %s(xlayer_id=%d) invalid in current state", |
2588 | 55 | avm_obu_type_to_string(obu_header.type), obu_header.obu_xlayer_id, |
2589 | 55 | obu_header.obu_mlayer_id, avm_obu_type_to_string(prev_obu_type), |
2590 | 55 | prev_xlayer_id); |
2591 | 55 | } |
2592 | 43.7k | prev_obu_type = obu_header.type; |
2593 | 43.7k | prev_xlayer_id = obu_header.obu_xlayer_id; |
2594 | 43.7k | } |
2595 | | |
2596 | 93.5k | check_valid_layer_id(obu_header, cm); |
2597 | | |
2598 | 93.5k | pbi->obu_type = obu_header.type; |
2599 | | |
2600 | 93.5k | cm->tlayer_id = obu_header.obu_tlayer_id; |
2601 | 93.5k | cm->mlayer_id = obu_header.obu_mlayer_id; |
2602 | | |
2603 | 93.5k | if (!pbi->is_multistream || (obu_header.obu_xlayer_id == GLOBAL_XLAYER_ID && |
2604 | 81.7k | cm->xlayer_id == GLOBAL_XLAYER_ID)) { |
2605 | 81.7k | cm->xlayer_id = obu_header.obu_xlayer_id; |
2606 | 81.7k | } else if (cm->xlayer_id != GLOBAL_XLAYER_ID && |
2607 | 10.1k | obu_header.obu_xlayer_id == GLOBAL_XLAYER_ID) { |
2608 | | // Store xlayer context |
2609 | 1.30k | av2_store_xlayer_context(pbi, cm, cm->xlayer_id); |
2610 | 1.30k | cm->xlayer_id = obu_header.obu_xlayer_id; |
2611 | 10.4k | } else if (cm->xlayer_id == GLOBAL_XLAYER_ID && |
2612 | 1.26k | obu_header.obu_xlayer_id != GLOBAL_XLAYER_ID) { |
2613 | | // Restore xlayer context |
2614 | 1.26k | cm->xlayer_id = obu_header.obu_xlayer_id; |
2615 | 1.26k | av2_restore_xlayer_context(pbi, cm, cm->xlayer_id); |
2616 | 9.17k | } else if (cm->xlayer_id != obu_header.obu_xlayer_id) { |
2617 | | // Store and restore xlayer context |
2618 | 75 | av2_store_xlayer_context(pbi, cm, cm->xlayer_id); |
2619 | 75 | cm->xlayer_id = obu_header.obu_xlayer_id; |
2620 | 75 | av2_restore_xlayer_context(pbi, cm, cm->xlayer_id); |
2621 | 75 | } |
2622 | | |
2623 | 93.5k | if (obu_header.type == OBU_LEADING_TILE_GROUP || |
2624 | 92.9k | obu_header.type == OBU_REGULAR_TILE_GROUP) { |
2625 | 274 | if (prev_obu_xlayer_id == -1) { |
2626 | 274 | prev_obu_xlayer_id = obu_header.obu_xlayer_id; |
2627 | 274 | } else { |
2628 | 0 | if (pbi->is_multistream && prev_obu_xlayer_id >= 0 && |
2629 | 0 | obu_header.obu_xlayer_id != prev_obu_xlayer_id) { |
2630 | 0 | avm_internal_error(&cm->error, AVM_CODEC_UNSUP_BITSTREAM, |
2631 | 0 | "tile group OBUs with the same stream_id shall " |
2632 | 0 | "be contiguous within a temporal unit"); |
2633 | 0 | } |
2634 | 0 | } |
2635 | 274 | } |
2636 | | // Set is_bridge_frame flag based on OBU type |
2637 | 93.5k | if (obu_header.type == OBU_BRIDGE_FRAME) { |
2638 | 317 | cm->bridge_frame_info.is_bridge_frame = 1; |
2639 | 93.1k | } else { |
2640 | 93.1k | cm->bridge_frame_info.is_bridge_frame = 0; |
2641 | 93.1k | } |
2642 | | |
2643 | 93.5k | if (is_single_tile_vcl_obu(obu_header.type) || |
2644 | 92.8k | is_multi_tile_vcl_obu(obu_header.type)) { |
2645 | 18.4k | if (!pbi->seen_vcl_obu_in_this_tu) { |
2646 | 3.00k | pbi->this_is_first_vcl_obu_in_tu = 1; |
2647 | 3.00k | pbi->seen_vcl_obu_in_this_tu = 1; |
2648 | 15.4k | } else { |
2649 | 15.4k | pbi->this_is_first_vcl_obu_in_tu = 0; |
2650 | 15.4k | } |
2651 | 18.4k | } |
2652 | | |
2653 | | // Flush remaining frames after xlayer context is correctly set. |
2654 | | // This must happen after xlayer switching but before processing frame OBUs. |
2655 | | // Skip flush while in GLOBAL_XLAYER_ID context -- the target layer's |
2656 | | // context has not been restored yet; flush will run once a non-global |
2657 | | // OBU restores it. |
2658 | 93.5k | if (pbi->this_is_first_vcl_obu_in_tu && cm->xlayer_id != GLOBAL_XLAYER_ID && |
2659 | 4.33k | obu_header.type == OBU_CLOSED_LOOP_KEY && |
2660 | 2.96k | pbi->obus_in_frame_unit_data[cm->tlayer_id][cm->mlayer_id] |
2661 | 2.96k | [OBU_CLOSED_LOOP_KEY]) { |
2662 | 2.96k | flush_remaining_frames(pbi, INT_MAX); |
2663 | | // Reset last_output_doh for this xlayer only, after flushing so that |
2664 | | // the flushed frames' DOH values are still checked against the old |
2665 | | // last_output_doh. Other xlayers may still be mid-sequence. |
2666 | 26.6k | for (int ml = 0; ml < MAX_NUM_MLAYERS; ml++) |
2667 | 23.6k | pbi->last_output_doh[cm->xlayer_id][ml] = -1; |
2668 | 2.96k | } |
2669 | | |
2670 | | // Flush leading frames (doh < last_olk_tu_display_order_hint) at the start |
2671 | | // of the first regular temporal unit after an OLK, before |
2672 | | // reset_buffer_other_than_OLK() clears their DPB slots. |
2673 | 93.5k | if (pbi->olk_encountered && pbi->this_is_first_vcl_obu_in_tu && |
2674 | 0 | !pbi->seen_frame_header && cm->is_leading_picture == 0) { |
2675 | 0 | flush_remaining_frames(pbi, pbi->last_olk_tu_display_order_hint); |
2676 | 0 | } |
2677 | | |
2678 | 93.5k | av2_init_read_bit_buffer(pbi, &rb, data, data + payload_size); |
2679 | 93.5k | switch (obu_header.type) { |
2680 | 30 | case OBU_TEMPORAL_DELIMITER: |
2681 | | // Reset per-TU state unconditionally for the current stream. |
2682 | 30 | decoded_payload_size = read_temporal_delimiter_obu(); |
2683 | 30 | pbi->seen_frame_header = 0; |
2684 | 30 | pbi->next_start_tile = 0; |
2685 | 30 | pbi->seen_vcl_obu_in_this_tu = 0; |
2686 | 30 | pbi->doh_tu_order_hint_bits_set = 0; |
2687 | 480 | for (int i = 0; i < NUM_CUSTOM_QMS; i++) pbi->qm_protected[i] = 0; |
2688 | | |
2689 | | // Propagate the reset to each active xlayer's saved context. |
2690 | 960 | for (int xlayer = 0; xlayer < AVM_MAX_NUM_STREAMS - 1; xlayer++) { |
2691 | 930 | if (pbi->xlayer_id_map[xlayer] > 0) { |
2692 | 29 | av2_store_xlayer_context(pbi, cm, cm->xlayer_id); |
2693 | 29 | cm->xlayer_id = xlayer; |
2694 | 29 | av2_restore_xlayer_context(pbi, cm, xlayer); |
2695 | 29 | decoded_payload_size = read_temporal_delimiter_obu(); |
2696 | 29 | pbi->seen_frame_header = 0; |
2697 | 29 | pbi->next_start_tile = 0; |
2698 | 29 | pbi->seen_vcl_obu_in_this_tu = 0; |
2699 | 29 | pbi->doh_tu_order_hint_bits_set = 0; |
2700 | 464 | for (int i = 0; i < NUM_CUSTOM_QMS; i++) pbi->qm_protected[i] = 0; |
2701 | 29 | } |
2702 | 930 | } |
2703 | 30 | break; |
2704 | 1.45k | case OBU_MULTI_STREAM_DECODER_OPERATION: |
2705 | 1.45k | decoded_payload_size = |
2706 | 1.45k | read_multi_stream_decoder_operation_obu(pbi, &rb); |
2707 | 1.45k | if (cm->error.error_code != AVM_CODEC_OK) return -1; |
2708 | 158 | if (pbi->sbe_state.extraction_enabled) { |
2709 | 0 | av2_sbe_process_msdo(&pbi->sbe_state, cm->num_streams, |
2710 | 0 | cm->stream_ids); |
2711 | 0 | } |
2712 | 158 | break; |
2713 | 22.1k | case OBU_SEQUENCE_HEADER: |
2714 | 22.1k | cm->xlayer_id = obu_header.obu_xlayer_id; |
2715 | 22.1k | decoded_payload_size = read_sequence_header_obu( |
2716 | 22.1k | pbi, obu_header.obu_xlayer_id, &rb, acc_sh_id_bitmap); |
2717 | 22.1k | if (cm->error.error_code != AVM_CODEC_OK) return -1; |
2718 | 21.9k | if (pbi->sbe_state.extraction_enabled) { |
2719 | 0 | av2_sbe_extract_seq_header_params( |
2720 | 0 | &pbi->sbe_state, obu_header.obu_xlayer_id, |
2721 | 0 | cm->seq_params.seq_profile_idc, cm->seq_params.seq_max_level_idx, |
2722 | 0 | cm->seq_params.seq_tier, cm->seq_params.seq_max_mlayer_cnt); |
2723 | 0 | } |
2724 | 21.9k | break; |
2725 | 66 | case OBU_BUFFER_REMOVAL_TIMING: |
2726 | 66 | decoded_payload_size = |
2727 | 66 | av2_read_buffer_removal_timing_obu(pbi, &rb, cm->xlayer_id); |
2728 | 66 | if (cm->error.error_code != AVM_CODEC_OK) return -1; |
2729 | 63 | break; |
2730 | 160 | case OBU_LAYER_CONFIGURATION_RECORD: |
2731 | 160 | decoded_payload_size = av2_read_layer_configuration_record_obu( |
2732 | 160 | pbi, cm->xlayer_id, &rb, acc_lcr_id_bitmap); |
2733 | 160 | if (cm->error.error_code != AVM_CODEC_OK) return -1; |
2734 | | // Allocate or reallocate stream_info if Global LCR triggers |
2735 | | // multi-stream mode and more space is needed. |
2736 | 159 | if (cm->xlayer_id == GLOBAL_XLAYER_ID && pbi->glcr_is_present_in_tu && |
2737 | 0 | !pbi->msdo_is_present_in_tu && |
2738 | 0 | pbi->glcr_num_xlayers > pbi->glcr_stream_info_num_allocated) { |
2739 | 0 | const int num_streams = pbi->glcr_num_xlayers; |
2740 | 0 | const int old_count = pbi->glcr_stream_info_num_allocated; |
2741 | 0 | StreamInfo *new_info = |
2742 | 0 | (StreamInfo *)avm_malloc(num_streams * sizeof(StreamInfo)); |
2743 | 0 | if (new_info == NULL) { |
2744 | 0 | avm_internal_error(&cm->error, AVM_CODEC_MEM_ERROR, |
2745 | 0 | "Memory allocation failed for pbi->stream_info " |
2746 | 0 | "(GLCR)\n"); |
2747 | 0 | } |
2748 | 0 | memset(new_info, 0, num_streams * sizeof(StreamInfo)); |
2749 | 0 | if (pbi->stream_info != NULL && old_count > 0) { |
2750 | 0 | memcpy(new_info, pbi->stream_info, old_count * sizeof(StreamInfo)); |
2751 | 0 | avm_free(pbi->stream_info); |
2752 | 0 | } |
2753 | 0 | for (int i = old_count; i < num_streams; i++) { |
2754 | 0 | init_stream_info(&new_info[i]); |
2755 | 0 | } |
2756 | 0 | pbi->stream_info = new_info; |
2757 | 0 | pbi->glcr_stream_info_num_allocated = num_streams; |
2758 | 0 | } |
2759 | 159 | if (pbi->sbe_state.extraction_enabled && |
2760 | 0 | cm->xlayer_id == GLOBAL_XLAYER_ID && pbi->glcr_is_present_in_tu) { |
2761 | 0 | av2_sbe_process_global_lcr(&pbi->sbe_state, cm->num_streams, |
2762 | 0 | cm->stream_ids); |
2763 | 0 | } |
2764 | 159 | break; |
2765 | 24 | case OBU_ATLAS_SEGMENT: |
2766 | 24 | decoded_payload_size = |
2767 | 24 | av2_read_atlas_segment_info_obu(pbi, cm->xlayer_id, &rb); |
2768 | 24 | if (cm->error.error_code != AVM_CODEC_OK) return -1; |
2769 | 23 | break; |
2770 | 35 | case OBU_OPERATING_POINT_SET: |
2771 | 35 | decoded_payload_size = |
2772 | 35 | av2_read_operating_point_set_obu(pbi, cm->xlayer_id, &rb); |
2773 | 35 | avm_set_current_operating_point(pbi, cm->xlayer_id); |
2774 | 35 | if (cm->error.error_code != AVM_CODEC_OK) return -1; |
2775 | 35 | if (pbi->sbe_state.extraction_enabled) { |
2776 | 0 | if (cm->xlayer_id == GLOBAL_XLAYER_ID) { |
2777 | | // Check if the user-selected global OPS is now available |
2778 | 0 | if (pbi->selected_ops_id >= 0 && |
2779 | 0 | pbi->selected_ops_id < MAX_NUM_OPS_ID) { |
2780 | 0 | const struct OperatingPointSet *sel_ops = |
2781 | 0 | &pbi->ops_list[GLOBAL_XLAYER_ID][pbi->selected_ops_id]; |
2782 | 0 | if (sel_ops->valid) { |
2783 | 0 | av2_sbe_process_global_ops( |
2784 | 0 | &pbi->sbe_state, sel_ops->ops_id, sel_ops->ops_cnt, |
2785 | 0 | pbi->selected_ops_id, pbi->selected_op_index, |
2786 | 0 | (sel_ops->ops_cnt > 0 && pbi->selected_op_index >= 0 && |
2787 | 0 | pbi->selected_op_index < sel_ops->ops_cnt) |
2788 | 0 | ? sel_ops->op[pbi->selected_op_index].ops_xlayer_map |
2789 | 0 | : 0, |
2790 | 0 | sel_ops->ops_mlayer_info_idc); |
2791 | 0 | } |
2792 | 0 | } |
2793 | 0 | } else { |
2794 | 0 | av2_sbe_process_local_ops(&pbi->sbe_state, pbi, cm->xlayer_id, |
2795 | 0 | pbi->dec_op_params.DecOpSetId, |
2796 | 0 | pbi->dec_op_params.DecOpCount); |
2797 | 0 | } |
2798 | 0 | } |
2799 | 35 | break; |
2800 | 28 | case OBU_CONTENT_INTERPRETATION: |
2801 | 28 | decoded_payload_size = av2_read_content_interpretation_obu(pbi, &rb); |
2802 | 28 | if (cm->error.error_code != AVM_CODEC_OK) return -1; |
2803 | 18 | break; |
2804 | 18 | case OBU_MULTI_FRAME_HEADER: |
2805 | 6 | decoded_payload_size = |
2806 | 6 | read_multi_frame_header_obu(pbi, &acc_mfh_id_bitmap, &rb); |
2807 | 6 | if (cm->error.error_code != AVM_CODEC_OK) return -1; |
2808 | 6 | break; |
2809 | 17.6k | case OBU_CLOSED_LOOP_KEY: |
2810 | 17.7k | case OBU_OPEN_LOOP_KEY: |
2811 | 18.0k | case OBU_LEADING_TILE_GROUP: |
2812 | 18.0k | case OBU_REGULAR_TILE_GROUP: |
2813 | 18.0k | case OBU_SWITCH: |
2814 | 18.1k | case OBU_LEADING_SEF: |
2815 | 18.1k | case OBU_REGULAR_SEF: |
2816 | 18.1k | case OBU_LEADING_TIP: |
2817 | 18.1k | case OBU_REGULAR_TIP: |
2818 | 18.1k | case OBU_RAS_FRAME: |
2819 | 18.4k | case OBU_BRIDGE_FRAME: |
2820 | | // Constraint 4: Switch frames shall be at the base temporal layer. |
2821 | 18.4k | if ((obu_header.type == OBU_SWITCH || |
2822 | 18.4k | obu_header.type == OBU_RAS_FRAME) && |
2823 | 27 | obu_header.obu_tlayer_id != 0) { |
2824 | 26 | avm_internal_error( |
2825 | 26 | &cm->error, AVM_CODEC_UNSUP_BITSTREAM, |
2826 | 26 | "Switch frame (obu_type=%d) must have obu_tlayer_id equal to " |
2827 | 26 | "0, got %d.", |
2828 | 26 | obu_header.type, obu_header.obu_tlayer_id); |
2829 | 26 | } |
2830 | 18.4k | if (obu_header.type == OBU_SWITCH || obu_header.type == OBU_RAS_FRAME) { |
2831 | 1 | const int mid = obu_header.obu_mlayer_id; |
2832 | 9 | for (int j = 0; j < MAX_NUM_MLAYERS; j++) { |
2833 | 8 | if (j != mid && cm->seq_params.mlayer_dependency_map[mid][j] != 0) { |
2834 | 0 | avm_internal_error( |
2835 | 0 | &cm->error, AVM_CODEC_UNSUP_BITSTREAM, |
2836 | 0 | "Switch frame at mlayer_id=%d depends on mlayer %d. " |
2837 | 0 | "Switch frames must be in independent embedded layers.", |
2838 | 0 | mid, j); |
2839 | 0 | } |
2840 | 8 | } |
2841 | 1 | } |
2842 | | // Drop picture unit HLS state that was derived exclusively from leading |
2843 | | // frame picture units when the first regular VCL OBU is encountered. |
2844 | 18.4k | if (av2_is_leading_vcl_obu(obu_header.type)) { |
2845 | | // Tag every MFH/QM/FGM/CI/BRT signalled in the leading temporal |
2846 | | // unit so we can identify and discard them at the transition. |
2847 | 333 | const int leading_mlayer_id = cm->mlayer_id; |
2848 | 5.66k | for (int i = 0; i < MAX_MFH_NUM; i++) |
2849 | 5.32k | if (acc_mfh_id_bitmap & (1 << i)) |
2850 | 2 | cm->mfh_params[i].mfh_from_leading = 1; |
2851 | 5.32k | for (int i = 0; i < NUM_CUSTOM_QMS; i++) |
2852 | 4.99k | if ((acc_qm_id_bitmap & (1 << i)) || qm_bit_map_zero_signalled) { |
2853 | 0 | pbi->qm_list[i].qm_from_leading = 1; |
2854 | 0 | } |
2855 | 2.99k | for (int i = 0; i < MAX_FGM_NUM; i++) |
2856 | 2.66k | if (acc_fgm_id_bitmap & (1 << i)) { |
2857 | 0 | pbi->fgm_list[i].fgm_from_leading = 1; |
2858 | 0 | } |
2859 | 333 | if (pbi->obus_in_frame_unit_data[obu_header.obu_tlayer_id] |
2860 | 333 | [leading_mlayer_id] |
2861 | 333 | [OBU_CONTENT_INTERPRETATION]) |
2862 | 0 | cm->ci_params_per_layer[leading_mlayer_id].ci_from_leading = true; |
2863 | 333 | if (pbi->obus_in_frame_unit_data[obu_header.obu_tlayer_id] |
2864 | 333 | [leading_mlayer_id] |
2865 | 333 | [OBU_BUFFER_REMOVAL_TIMING]) |
2866 | 0 | cm->brt_info.brt_from_leading = true; |
2867 | 10.9k | for (int x = 0; x < MAX_NUM_XLAYERS; x++) |
2868 | 181k | for (int s = 0; s < MAX_SEQ_NUM; s++) |
2869 | 170k | if (acc_sh_id_bitmap[x] & (1 << s)) |
2870 | 265 | pbi->seq_list[x][s].sh_from_leading = 1; |
2871 | 10.9k | for (int x = 0; x < MAX_NUM_XLAYERS; x++) |
2872 | 95.9k | for (int l = 0; l < MAX_NUM_LCR; l++) |
2873 | 85.2k | if (acc_lcr_id_bitmap[x] & (1 << l)) |
2874 | 10 | pbi->lcr_list[x][l].lcr_from_leading = true; |
2875 | 18.1k | } else if (pbi->this_is_first_vcl_obu_in_tu == 1) { |
2876 | | // SEF, TIP, SWITCH, RAS, BRIDGE, TG (not CLK) |
2877 | | // MFH, QM, FGM, BRT, CI signalled in leading temporal unit cannot |
2878 | | // be used. Drop state not re-signalled in the regular picture unit. |
2879 | 2.99k | const int regular_mlayer_id = cm->mlayer_id; |
2880 | 2.99k | const int num_planes = av2_num_planes(cm); |
2881 | | |
2882 | 47.8k | for (int i = 0; i < NUM_CUSTOM_QMS; i++) { |
2883 | 44.8k | struct quantization_matrix_set *qmset = &pbi->qm_list[i]; |
2884 | 44.8k | if (qmset->qm_from_leading == 1 && !(acc_qm_id_bitmap & (1 << i)) && |
2885 | 0 | !qm_bit_map_zero_signalled) { |
2886 | 0 | qmset->qm_id = -1; |
2887 | 0 | qmset->qm_mlayer_id = -1; |
2888 | 0 | qmset->qm_tlayer_id = -1; |
2889 | 0 | qmset->quantizer_matrix_num_planes = num_planes; |
2890 | 0 | qmset->is_user_defined_qm = false; |
2891 | 0 | pbi->qm_protected[i] = 0; |
2892 | 0 | } |
2893 | 44.8k | qmset->qm_from_leading = 0; |
2894 | 44.8k | } |
2895 | | |
2896 | 26.9k | for (int i = 0; i < MAX_FGM_NUM; i++) { |
2897 | 23.9k | if (pbi->fgm_list[i].fgm_from_leading == 1 && |
2898 | 0 | !(acc_fgm_id_bitmap & (1 << i))) { |
2899 | 0 | pbi->fgm_list[i].fgm_id = -1; |
2900 | 0 | pbi->fgm_list[i].fgm_tlayer_id = -1; |
2901 | 0 | pbi->fgm_list[i].fgm_mlayer_id = -1; |
2902 | 0 | } |
2903 | 23.9k | pbi->fgm_list[i].fgm_from_leading = 0; |
2904 | 23.9k | } |
2905 | | |
2906 | 2.99k | if (cm->ci_params_per_layer[regular_mlayer_id].ci_from_leading == 1 && |
2907 | 0 | !pbi->obus_in_frame_unit_data[obu_header.obu_tlayer_id] |
2908 | 0 | [regular_mlayer_id] |
2909 | 0 | [OBU_CONTENT_INTERPRETATION]) { |
2910 | 0 | av2_initialize_ci_params( |
2911 | 0 | &cm->ci_params_per_layer[regular_mlayer_id]); |
2912 | 0 | } |
2913 | 2.99k | cm->ci_params_per_layer[regular_mlayer_id].ci_from_leading = false; |
2914 | 50.8k | for (int i = 0; i < MAX_MFH_NUM; i++) { |
2915 | 47.8k | if (cm->mfh_params[i].mfh_from_leading == 1 && |
2916 | 0 | !(acc_mfh_id_bitmap & (1 << i))) { |
2917 | 0 | cm->mfh_valid[i] = false; |
2918 | 0 | } |
2919 | 47.8k | cm->mfh_params[i].mfh_from_leading = 0; |
2920 | 47.8k | } |
2921 | 2.99k | if (cm->brt_info.brt_from_leading == 1 && |
2922 | 0 | !pbi->obus_in_frame_unit_data[obu_header.obu_tlayer_id] |
2923 | 0 | [regular_mlayer_id] |
2924 | 0 | [OBU_BUFFER_REMOVAL_TIMING]) { |
2925 | 0 | memset(&cm->brt_info, 0, sizeof(cm->brt_info)); |
2926 | 0 | } |
2927 | 2.99k | cm->brt_info.brt_from_leading = false; |
2928 | 98.7k | for (int x = 0; x < MAX_NUM_XLAYERS; x++) { |
2929 | 1.62M | for (int s = 0; s < MAX_SEQ_NUM; s++) { |
2930 | 1.53M | if (!(pbi->seq_list[x][s].sh_from_leading == 1 && |
2931 | 0 | !(acc_sh_id_bitmap[x] & (1 << s)))) |
2932 | 1.53M | pbi->seq_list[x][s].sh_from_leading = 0; |
2933 | 1.53M | } |
2934 | 861k | for (int l = 0; l < MAX_NUM_LCR; l++) { |
2935 | 765k | if (!(pbi->lcr_list[x][l].lcr_from_leading && |
2936 | 0 | !(acc_lcr_id_bitmap[x] & (1 << l)))) |
2937 | 765k | pbi->lcr_list[x][l].lcr_from_leading = false; |
2938 | 765k | } |
2939 | 95.7k | } |
2940 | 2.99k | } |
2941 | | |
2942 | | // It is a requirement that if multiple QM OBUs are present |
2943 | | // consecutively prior to a coded frame, other than a QM OBU with |
2944 | | // qm_bit_map equal to 0, such QM OBUs will not set the same QM ID more |
2945 | | // than once. |
2946 | 18.4k | acc_qm_id_bitmap = 0; |
2947 | 18.4k | qm_bit_map_zero_signalled = 0; |
2948 | | // It is a requirement that if multiple FGM OBUs are present |
2949 | | // consecutively prior to a coded frame, such FGM OBUs will not set |
2950 | | // the same FGM ID more than once. |
2951 | 18.4k | acc_fgm_id_bitmap = 0; |
2952 | 18.4k | acc_mfh_id_bitmap = 0; |
2953 | 18.4k | memset(acc_sh_id_bitmap, 0, sizeof(acc_sh_id_bitmap)); |
2954 | 18.4k | memset(acc_lcr_id_bitmap, 0, sizeof(acc_lcr_id_bitmap)); |
2955 | 18.4k | decoded_payload_size = read_tilegroup_obu( |
2956 | 18.4k | pbi, &rb, data, data + payload_size, p_data_end, obu_header.type, |
2957 | 18.4k | obu_header.obu_xlayer_id, &curr_obu_info->first_tile_group, |
2958 | 18.4k | &frame_decoding_finished); |
2959 | 18.4k | if (cm->error.error_code != AVM_CODEC_OK) return -1; |
2960 | 17.8k | curr_obu_info->immediate_output_picture = cm->immediate_output_picture; |
2961 | 17.8k | curr_obu_info->showable_frame = |
2962 | 17.8k | cm->immediate_output_picture || cm->implicit_output_picture; |
2963 | 17.8k | curr_obu_info->display_order_hint = |
2964 | 17.8k | cm->current_frame.display_order_hint; |
2965 | 17.8k | int num_mlayers = 0; |
2966 | 18.3k | for (int i = 0; i < MAX_NUM_MLAYERS; ++i) { |
2967 | 424 | if (pbi->mlayer_id_map[obu_header.obu_xlayer_id][i] == 1) |
2968 | 53 | num_mlayers++; |
2969 | 424 | } |
2970 | 17.8k | if (num_mlayers > 1 && |
2971 | 0 | num_mlayers > cm->seq_params.seq_max_mlayer_cnt) { |
2972 | 0 | avm_internal_error( |
2973 | 0 | &cm->error, AVM_CODEC_UNSUP_BITSTREAM, |
2974 | 0 | "The number of embedded layers (%d) in the current video " |
2975 | 0 | "sequence is larger than the seq_max_mlayer_cnt (%d).", |
2976 | 0 | num_mlayers, cm->seq_params.seq_max_mlayer_cnt); |
2977 | 0 | } |
2978 | 17.8k | if (cm->bru.frame_inactive_flag || |
2979 | 53 | cm->bridge_frame_info.is_bridge_frame) { |
2980 | 0 | pbi->seen_frame_header = 0; |
2981 | 0 | frame_decoding_finished = 1; |
2982 | 0 | CommonTileParams *const tiles = &cm->tiles; |
2983 | 0 | av2_get_tile_limits( |
2984 | 0 | &cm->tiles, cm->mi_params.mi_rows, cm->mi_params.mi_cols, |
2985 | 0 | cm->mib_size_log2, cm->seq_params.mib_size_log2, |
2986 | 0 | cm->seq_params.seq_max_level_idx, cm->seq_params.seq_tier); |
2987 | 0 | tiles->uniform_spacing = 1; |
2988 | 0 | tiles->log2_cols = 0; |
2989 | 0 | av2_calculate_tile_cols(tiles); |
2990 | 0 | tiles->log2_rows = 0; |
2991 | 0 | av2_calculate_tile_rows(tiles); |
2992 | 0 | const int num_tiles = cm->tiles.cols * cm->tiles.rows; |
2993 | 0 | const int end_tile = num_tiles - 1; |
2994 | | // skip parsing and go directly to decode |
2995 | 0 | av2_decode_tg_tiles_and_wrapup(pbi, data, data + payload_size, |
2996 | 0 | p_data_end, 0, end_tile, 0); |
2997 | 0 | if (cm->bridge_frame_info.is_bridge_frame) { |
2998 | 0 | *p_data_end = data + payload_size; |
2999 | 0 | } |
3000 | 0 | break; |
3001 | 0 | } |
3002 | 17.8k | if (obu_payload_offset > payload_size) { |
3003 | 0 | cm->error.error_code = AVM_CODEC_CORRUPT_FRAME; |
3004 | 0 | return -1; |
3005 | 0 | } |
3006 | 17.8k | if (cm->error.error_code != AVM_CODEC_OK) return -1; |
3007 | 17.8k | if (frame_decoding_finished) pbi->seen_frame_header = 0; |
3008 | 17.8k | pbi->num_tile_groups++; |
3009 | 17.8k | break; |
3010 | 20 | case OBU_QUANTIZATION_MATRIX: |
3011 | 20 | decoded_payload_size = |
3012 | 20 | read_qm_obu(pbi, obu_header.obu_tlayer_id, obu_header.obu_mlayer_id, |
3013 | 20 | &acc_qm_id_bitmap, &qm_bit_map_zero_signalled, &rb); |
3014 | 20 | if (cm->error.error_code != AVM_CODEC_OK) return -1; |
3015 | 17 | break; |
3016 | 807 | case OBU_METADATA_SHORT: |
3017 | 807 | decoded_payload_size = read_metadata_short(pbi, data, payload_size, 0); |
3018 | 807 | if (cm->error.error_code != AVM_CODEC_OK) return -1; |
3019 | 799 | break; |
3020 | 799 | case OBU_METADATA_GROUP: |
3021 | 5 | decoded_payload_size = |
3022 | 5 | read_metadata_obu(pbi, data, payload_size, &obu_header, 0); |
3023 | 5 | if (cm->error.error_code != AVM_CODEC_OK) return -1; |
3024 | 0 | break; |
3025 | 219 | case OBU_FILM_GRAIN_MODEL: |
3026 | 219 | decoded_payload_size = |
3027 | 219 | read_fgm_obu(pbi, obu_header.obu_tlayer_id, |
3028 | 219 | obu_header.obu_mlayer_id, &acc_fgm_id_bitmap, &rb); |
3029 | 219 | if (cm->error.error_code != AVM_CODEC_OK) return -1; |
3030 | 219 | break; |
3031 | 234 | case OBU_PADDING: |
3032 | 234 | decoded_payload_size = read_padding(cm, data, payload_size); |
3033 | 234 | if (cm->error.error_code != AVM_CODEC_OK) return -1; |
3034 | 0 | break; |
3035 | 49.4k | default: |
3036 | | // Skip unrecognized OBUs |
3037 | 49.4k | if (payload_size > 0 && |
3038 | 14.1k | get_last_nonzero_byte(data, payload_size) == 0) { |
3039 | 0 | cm->error.error_code = AVM_CODEC_CORRUPT_FRAME; |
3040 | 0 | return -1; |
3041 | 0 | } |
3042 | 49.4k | decoded_payload_size = payload_size; |
3043 | 49.4k | break; |
3044 | 93.5k | } |
3045 | | |
3046 | | // Spec 6.2.2: |
3047 | | // "At the end of reading the OBU, it is a requirement of bitstream " |
3048 | | // "conformance that obu_mlayer_id is less than or equal to max_mlayer_id." |
3049 | 72.2k | if (obu_header.obu_mlayer_id > cm->seq_params.max_mlayer_id && |
3050 | 1.33k | (!pbi->decoding_first_frame || frame_decoding_finished)) { |
3051 | 1 | avm_internal_error(&cm->error, AVM_CODEC_UNSUP_BITSTREAM, |
3052 | 1 | "obu_mlayer_id (%d) must be less than or equal to " |
3053 | 1 | "max_mlayer_id (%d).", |
3054 | 1 | obu_header.obu_mlayer_id, |
3055 | 1 | cm->seq_params.max_mlayer_id); |
3056 | 1 | } |
3057 | | |
3058 | | // Check that the signalled OBU size matches the actual amount of data read |
3059 | 72.2k | if (decoded_payload_size > payload_size) { |
3060 | 0 | cm->error.error_code = AVM_CODEC_CORRUPT_FRAME; |
3061 | 0 | return -1; |
3062 | 0 | } |
3063 | | |
3064 | | // If there are extra padding bytes, they should all be zero |
3065 | 73.5k | while (decoded_payload_size < payload_size) { |
3066 | 1.38k | uint8_t padding_byte = data[decoded_payload_size++]; |
3067 | 1.38k | if (padding_byte != 0) { |
3068 | 3 | cm->error.error_code = AVM_CODEC_CORRUPT_FRAME; |
3069 | 3 | return -1; |
3070 | 3 | } |
3071 | 1.38k | } |
3072 | | |
3073 | 72.2k | data += payload_size; |
3074 | 72.2k | count_obus_with_frame_unit++; |
3075 | 72.2k | } |
3076 | | |
3077 | | // check whether suffix metadata OBUs are present |
3078 | 197 | while (cm->error.error_code == AVM_CODEC_OK && data < data_end) { |
3079 | 0 | size_t payload_size = 0; |
3080 | 0 | size_t decoded_payload_size = 0; |
3081 | 0 | size_t bytes_read = 0; |
3082 | 0 | const size_t bytes_available = data_end - data; |
3083 | 0 | avm_codec_err_t status = avm_read_obu_header_and_size( |
3084 | 0 | data, bytes_available, &obu_header, &payload_size, &bytes_read); |
3085 | |
|
3086 | 0 | if (status != AVM_CODEC_OK) { |
3087 | 0 | cm->error.error_code = status; |
3088 | 0 | return -1; |
3089 | 0 | } |
3090 | | |
3091 | | // Accept both OBU_METADATA_SHORT and OBU_METADATA_GROUP for suffix metadata |
3092 | 0 | if (!(is_metadata_obu(obu_header.type) || (obu_header.type == OBU_PADDING))) |
3093 | 0 | break; |
3094 | | |
3095 | | // Note: avm_read_obu_header_and_size() takes care of checking that this |
3096 | | // doesn't cause 'data' to advance past 'data_end'. |
3097 | 0 | data += bytes_read; |
3098 | |
|
3099 | 0 | if ((size_t)(data_end - data) < payload_size) { |
3100 | 0 | cm->error.error_code = AVM_CODEC_CORRUPT_FRAME; |
3101 | 0 | return -1; |
3102 | 0 | } |
3103 | | |
3104 | 0 | if (obu_header.type == OBU_PADDING) { |
3105 | 0 | decoded_payload_size = read_padding(cm, data, payload_size); |
3106 | 0 | obu_info *const curr_obu_info = &obu_list[count_obus_with_frame_unit]; |
3107 | 0 | curr_obu_info->obu_type = obu_header.type; |
3108 | 0 | curr_obu_info->first_tile_group = -1; |
3109 | 0 | curr_obu_info->immediate_output_picture = -1; |
3110 | 0 | curr_obu_info->showable_frame = -1; |
3111 | 0 | curr_obu_info->display_order_hint = -1; |
3112 | 0 | count_obus_with_frame_unit++; |
3113 | 0 | if (cm->error.error_code != AVM_CODEC_OK) return -1; |
3114 | 0 | } else if (is_metadata_obu(obu_header.type)) { |
3115 | | // check whether it is a suffix metadata OBU |
3116 | 0 | if (payload_size == 0) { |
3117 | 0 | avm_internal_error(&cm->error, AVM_CODEC_CORRUPT_FRAME, |
3118 | 0 | "OBU_METADATA_x has an empty payload"); |
3119 | 0 | } |
3120 | 0 | if (!(data[0] & 0x80)) { |
3121 | 0 | avm_internal_error(&cm->error, AVM_CODEC_UNSUP_BITSTREAM, |
3122 | 0 | "OBU order violation: OBU_METADATA_x(prefix) cannot " |
3123 | 0 | "be present after a coded frame"); |
3124 | 0 | } |
3125 | | |
3126 | | // Call the appropriate read function based on OBU type |
3127 | 0 | if (obu_header.type == OBU_METADATA_GROUP) { |
3128 | 0 | decoded_payload_size = |
3129 | 0 | read_metadata_obu(pbi, data, payload_size, &obu_header, 1); |
3130 | 0 | } else { |
3131 | 0 | decoded_payload_size = read_metadata_short(pbi, data, payload_size, 1); |
3132 | 0 | } |
3133 | 0 | obu_info *const curr_obu_info = &obu_list[count_obus_with_frame_unit]; |
3134 | 0 | curr_obu_info->obu_type = obu_header.type; |
3135 | 0 | curr_obu_info->first_tile_group = -1; |
3136 | 0 | curr_obu_info->immediate_output_picture = -1; |
3137 | 0 | curr_obu_info->showable_frame = -1; |
3138 | 0 | curr_obu_info->display_order_hint = -1; |
3139 | 0 | count_obus_with_frame_unit++; |
3140 | 0 | } |
3141 | 0 | if (cm->error.error_code != AVM_CODEC_OK) return -1; |
3142 | | |
3143 | | // Spec 6.2.2: |
3144 | | // "At the end of reading the OBU, it is a requirement of bitstream " |
3145 | | // "conformance that obu_mlayer_id is less than or equal to max_mlayer_id." |
3146 | 0 | if (obu_header.obu_mlayer_id > cm->seq_params.max_mlayer_id && |
3147 | 0 | (!pbi->decoding_first_frame || frame_decoding_finished)) { |
3148 | 0 | avm_internal_error(&cm->error, AVM_CODEC_UNSUP_BITSTREAM, |
3149 | 0 | "obu_mlayer_id (%d) must be less than or equal to " |
3150 | 0 | "max_mlayer_id (%d).", |
3151 | 0 | obu_header.obu_mlayer_id, |
3152 | 0 | cm->seq_params.max_mlayer_id); |
3153 | 0 | } |
3154 | | |
3155 | | // Check that the signalled OBU size matches the actual amount of data read |
3156 | 0 | if (decoded_payload_size > payload_size) { |
3157 | 0 | cm->error.error_code = AVM_CODEC_CORRUPT_FRAME; |
3158 | 0 | return -1; |
3159 | 0 | } |
3160 | | |
3161 | 0 | data += payload_size; |
3162 | 0 | } |
3163 | | |
3164 | 197 | if (cm->error.error_code != AVM_CODEC_OK) return -1; |
3165 | | |
3166 | 197 | *p_data_end = data; |
3167 | | |
3168 | | #if CONFIG_COLLECT_COMPONENT_TIMING |
3169 | | end_timing(pbi, avm_decode_frame_from_obus_time); |
3170 | | |
3171 | | // Print out timing information. |
3172 | | int i; |
3173 | | fprintf(stderr, "\n Frame number: %d, Frame type: %s, Show Frame: %d\n", |
3174 | | cm->current_frame.frame_number, |
3175 | | get_frame_type_enum(cm->current_frame.frame_type), |
3176 | | cm->immediate_output_picture); |
3177 | | for (i = 0; i < kTimingComponents; i++) { |
3178 | | pbi->component_time[i] += pbi->frame_component_time[i]; |
3179 | | fprintf(stderr, " %s: %" PRId64 " us (total: %" PRId64 " us)\n", |
3180 | | get_component_name(i), pbi->frame_component_time[i], |
3181 | | pbi->component_time[i]); |
3182 | | pbi->frame_component_time[i] = 0; |
3183 | | } |
3184 | | #endif |
3185 | | |
3186 | 197 | obu_info current_frame_unit; |
3187 | 197 | memset(¤t_frame_unit, -1, sizeof(current_frame_unit)); |
3188 | 359 | for (int obu_idx = 0; obu_idx < count_obus_with_frame_unit; obu_idx++) { |
3189 | 162 | obu_info *this_obu = &obu_list[obu_idx]; |
3190 | | |
3191 | 162 | if (this_obu->first_tile_group == 1) { |
3192 | 53 | current_frame_unit = *this_obu; |
3193 | 53 | pbi->num_displayable_frame_unit[this_obu->mlayer_id]++; |
3194 | 53 | } |
3195 | 162 | if (is_multi_tile_vcl_obu(this_obu->obu_type) && |
3196 | 53 | this_obu->first_tile_group == 0) { |
3197 | | // Allow padding OBUs between tile group OBUs. |
3198 | 0 | int prev_obu_idx = obu_idx - 1; |
3199 | 0 | while (prev_obu_idx >= 0 && |
3200 | 0 | obu_list[prev_obu_idx].obu_type == OBU_PADDING) { |
3201 | 0 | prev_obu_idx--; |
3202 | 0 | } |
3203 | 0 | if (prev_obu_idx < 0) { |
3204 | 0 | avm_internal_error(&cm->error, AVM_CODEC_UNSUP_BITSTREAM, |
3205 | 0 | "The first non-padding OBU in a frame unit cannot " |
3206 | 0 | "be a tile group with is_first_tile_group == 0"); |
3207 | 0 | } |
3208 | 0 | check_tilegroup_obus_in_a_frame_unit(cm, this_obu, |
3209 | 0 | &obu_list[prev_obu_idx]); |
3210 | 0 | } |
3211 | 162 | } |
3212 | | |
3213 | | // When SBE filters out all frame OBUs in a temporal unit, no frame was |
3214 | | // found: skip the consistency checks. |
3215 | 197 | int xId = current_frame_unit.xlayer_id; |
3216 | 197 | if (current_frame_unit.display_order_hint != -1 && |
3217 | 53 | pbi->last_frame_unit[xId].display_order_hint != -1 && |
3218 | 0 | (pbi->last_frame_unit[xId].xlayer_id == current_frame_unit.xlayer_id)) { |
3219 | 0 | check_clk_in_a_layer(cm, ¤t_frame_unit, &pbi->last_frame_unit[xId]); |
3220 | |
|
3221 | 0 | if (current_frame_unit.showable_frame == 0) { |
3222 | 0 | check_layerid_hidden_frame_units(cm, ¤t_frame_unit, |
3223 | 0 | &pbi->last_frame_unit[xId]); |
3224 | 0 | } else { |
3225 | 0 | check_layerid_showable_frame_units( |
3226 | 0 | cm, ¤t_frame_unit, &pbi->last_frame_unit[xId], |
3227 | 0 | &pbi->last_displayable_frame_unit[xId]); |
3228 | 0 | } |
3229 | 0 | } |
3230 | | |
3231 | 197 | return frame_decoding_finished; |
3232 | 197 | } |