/src/aom/av1/decoder/obu.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (c) 2017, Alliance for Open Media. All rights reserved. |
3 | | * |
4 | | * This source code is subject to the terms of the BSD 2 Clause License and |
5 | | * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License |
6 | | * was not distributed with this source code in the LICENSE file, you can |
7 | | * obtain it at www.aomedia.org/license/software. If the Alliance for Open |
8 | | * Media Patent License 1.0 was not distributed with this source code in the |
9 | | * PATENTS file, you can obtain it at www.aomedia.org/license/patent. |
10 | | */ |
11 | | |
12 | | #include <assert.h> |
13 | | #include <stdbool.h> |
14 | | |
15 | | #include "config/aom_config.h" |
16 | | #include "config/aom_scale_rtcd.h" |
17 | | |
18 | | #include "aom/aom_codec.h" |
19 | | #include "aom_dsp/bitreader_buffer.h" |
20 | | #include "aom_ports/mem_ops.h" |
21 | | |
22 | | #include "av1/common/common.h" |
23 | | #include "av1/common/obu_util.h" |
24 | | #include "av1/common/timing.h" |
25 | | #include "av1/decoder/decoder.h" |
26 | | #include "av1/decoder/decodeframe.h" |
27 | | #include "av1/decoder/obu.h" |
28 | | |
29 | | aom_codec_err_t aom_get_num_layers_from_operating_point_idc( |
30 | | int operating_point_idc, unsigned int *number_spatial_layers, |
31 | 200k | unsigned int *number_temporal_layers) { |
32 | | // derive number of spatial/temporal layers from operating_point_idc |
33 | | |
34 | 200k | if (!number_spatial_layers || !number_temporal_layers) |
35 | 0 | return AOM_CODEC_INVALID_PARAM; |
36 | | |
37 | 200k | if (operating_point_idc == 0) { |
38 | 144k | *number_temporal_layers = 1; |
39 | 144k | *number_spatial_layers = 1; |
40 | 144k | } else { |
41 | 56.8k | *number_spatial_layers = 0; |
42 | 56.8k | *number_temporal_layers = 0; |
43 | 284k | for (int j = 0; j < MAX_NUM_SPATIAL_LAYERS; j++) { |
44 | 227k | *number_spatial_layers += |
45 | 227k | (operating_point_idc >> (j + MAX_NUM_TEMPORAL_LAYERS)) & 0x1; |
46 | 227k | } |
47 | 511k | for (int j = 0; j < MAX_NUM_TEMPORAL_LAYERS; j++) { |
48 | 454k | *number_temporal_layers += (operating_point_idc >> j) & 0x1; |
49 | 454k | } |
50 | 56.8k | } |
51 | | |
52 | 200k | return AOM_CODEC_OK; |
53 | 200k | } |
54 | | |
55 | | static int is_obu_in_current_operating_point(AV1Decoder *pbi, |
56 | 271k | const ObuHeader *obu_header) { |
57 | 271k | if (!pbi->current_operating_point || !obu_header->has_extension) { |
58 | 270k | return 1; |
59 | 270k | } |
60 | | |
61 | 1.29k | if ((pbi->current_operating_point >> obu_header->temporal_layer_id) & 0x1 && |
62 | 1.29k | (pbi->current_operating_point >> (obu_header->spatial_layer_id + 8)) & |
63 | 676 | 0x1) { |
64 | 203 | return 1; |
65 | 203 | } |
66 | 1.09k | return 0; |
67 | 1.29k | } |
68 | | |
69 | | static int byte_alignment(AV1_COMMON *const cm, |
70 | 315k | struct aom_read_bit_buffer *const rb) { |
71 | 804k | while (rb->bit_offset & 7) { |
72 | 495k | if (aom_rb_read_bit(rb)) { |
73 | 6.82k | cm->error->error_code = AOM_CODEC_CORRUPT_FRAME; |
74 | 6.82k | return -1; |
75 | 6.82k | } |
76 | 495k | } |
77 | 308k | return 0; |
78 | 315k | } |
79 | | |
80 | 140k | static uint32_t read_temporal_delimiter_obu(void) { return 0; } |
81 | | |
82 | | // Returns a boolean that indicates success. |
83 | | static int read_bitstream_level(AV1_LEVEL *seq_level_idx, |
84 | 94.5k | struct aom_read_bit_buffer *rb) { |
85 | 94.5k | *seq_level_idx = aom_rb_read_literal(rb, LEVEL_BITS); |
86 | 94.5k | if (!is_valid_seq_level_idx(*seq_level_idx)) return 0; |
87 | 90.0k | return 1; |
88 | 94.5k | } |
89 | | |
90 | | // Returns whether two sequence headers are consistent with each other. |
91 | | // Note that the 'op_params' field is not compared per Section 7.5 in the spec: |
92 | | // Within a particular coded video sequence, the contents of |
93 | | // sequence_header_obu must be bit-identical each time the sequence header |
94 | | // appears except for the contents of operating_parameters_info. |
95 | | static int are_seq_headers_consistent(const SequenceHeader *seq_params_old, |
96 | 61.0k | const SequenceHeader *seq_params_new) { |
97 | 61.0k | return !memcmp(seq_params_old, seq_params_new, |
98 | 61.0k | offsetof(SequenceHeader, op_params)); |
99 | 61.0k | } |
100 | | |
101 | | // On success, sets pbi->sequence_header_ready to 1 and returns the number of |
102 | | // bytes read from 'rb'. |
103 | | // On failure, sets pbi->common.error.error_code and returns 0. |
104 | | static uint32_t read_sequence_header_obu(AV1Decoder *pbi, |
105 | 87.4k | struct aom_read_bit_buffer *rb) { |
106 | 87.4k | AV1_COMMON *const cm = &pbi->common; |
107 | 87.4k | const uint32_t saved_bit_offset = rb->bit_offset; |
108 | | |
109 | | // Verify rb has been configured to report errors. |
110 | 87.4k | assert(rb->error_handler); |
111 | | |
112 | | // Use a local variable to store the information as we decode. At the end, |
113 | | // if no errors have occurred, cm->seq_params is updated. |
114 | 87.4k | SequenceHeader sh = *cm->seq_params; |
115 | 87.4k | SequenceHeader *const seq_params = &sh; |
116 | | |
117 | 87.4k | seq_params->profile = av1_read_profile(rb); |
118 | 87.4k | if (seq_params->profile > CONFIG_MAX_DECODE_PROFILE) { |
119 | 258 | pbi->error.error_code = AOM_CODEC_UNSUP_BITSTREAM; |
120 | 258 | return 0; |
121 | 258 | } |
122 | | |
123 | | // Still picture or not |
124 | 87.1k | seq_params->still_picture = aom_rb_read_bit(rb); |
125 | 87.1k | seq_params->reduced_still_picture_hdr = aom_rb_read_bit(rb); |
126 | | // Video must have reduced_still_picture_hdr = 0 |
127 | 87.1k | if (!seq_params->still_picture && seq_params->reduced_still_picture_hdr) { |
128 | 30 | pbi->error.error_code = AOM_CODEC_UNSUP_BITSTREAM; |
129 | 30 | return 0; |
130 | 30 | } |
131 | | |
132 | 87.1k | if (seq_params->reduced_still_picture_hdr) { |
133 | 18.1k | seq_params->timing_info_present = 0; |
134 | 18.1k | seq_params->decoder_model_info_present_flag = 0; |
135 | 18.1k | seq_params->display_model_info_present_flag = 0; |
136 | 18.1k | seq_params->operating_points_cnt_minus_1 = 0; |
137 | 18.1k | seq_params->operating_point_idc[0] = 0; |
138 | 18.1k | seq_params->has_nonzero_operating_point_idc = false; |
139 | 18.1k | if (!read_bitstream_level(&seq_params->seq_level_idx[0], rb)) { |
140 | 515 | pbi->error.error_code = AOM_CODEC_UNSUP_BITSTREAM; |
141 | 515 | return 0; |
142 | 515 | } |
143 | 17.6k | seq_params->tier[0] = 0; |
144 | 17.6k | seq_params->op_params[0].decoder_model_param_present_flag = 0; |
145 | 17.6k | seq_params->op_params[0].display_model_param_present_flag = 0; |
146 | 68.9k | } else { |
147 | 68.9k | seq_params->timing_info_present = aom_rb_read_bit(rb); |
148 | 68.9k | if (seq_params->timing_info_present) { |
149 | 3.66k | av1_read_timing_info_header(&seq_params->timing_info, &pbi->error, rb); |
150 | | |
151 | 3.66k | seq_params->decoder_model_info_present_flag = aom_rb_read_bit(rb); |
152 | 3.66k | if (seq_params->decoder_model_info_present_flag) |
153 | 1.09k | av1_read_decoder_model_info(&seq_params->decoder_model_info, rb); |
154 | 65.3k | } else { |
155 | 65.3k | seq_params->decoder_model_info_present_flag = 0; |
156 | 65.3k | } |
157 | 68.9k | seq_params->display_model_info_present_flag = aom_rb_read_bit(rb); |
158 | 68.9k | seq_params->operating_points_cnt_minus_1 = |
159 | 68.9k | aom_rb_read_literal(rb, OP_POINTS_CNT_MINUS_1_BITS); |
160 | 68.9k | seq_params->has_nonzero_operating_point_idc = false; |
161 | 141k | for (int i = 0; i < seq_params->operating_points_cnt_minus_1 + 1; i++) { |
162 | 76.5k | seq_params->operating_point_idc[i] = |
163 | 76.5k | aom_rb_read_literal(rb, OP_POINTS_IDC_BITS); |
164 | 76.5k | if (seq_params->operating_point_idc[i] != 0) |
165 | 24.1k | seq_params->has_nonzero_operating_point_idc = true; |
166 | 76.5k | if (!read_bitstream_level(&seq_params->seq_level_idx[i], rb)) { |
167 | 4.04k | pbi->error.error_code = AOM_CODEC_UNSUP_BITSTREAM; |
168 | 4.04k | return 0; |
169 | 4.04k | } |
170 | | // This is the seq_level_idx[i] > 7 check in the spec. seq_level_idx 7 |
171 | | // is equivalent to level 3.3. |
172 | 72.4k | if (seq_params->seq_level_idx[i] >= SEQ_LEVEL_4_0) |
173 | 7.44k | seq_params->tier[i] = aom_rb_read_bit(rb); |
174 | 65.0k | else |
175 | 65.0k | seq_params->tier[i] = 0; |
176 | 72.4k | if (seq_params->decoder_model_info_present_flag) { |
177 | 3.31k | seq_params->op_params[i].decoder_model_param_present_flag = |
178 | 3.31k | aom_rb_read_bit(rb); |
179 | 3.31k | if (seq_params->op_params[i].decoder_model_param_present_flag) |
180 | 2.06k | av1_read_op_parameters_info(&seq_params->op_params[i], |
181 | 2.06k | seq_params->decoder_model_info |
182 | 2.06k | .encoder_decoder_buffer_delay_length, |
183 | 2.06k | rb); |
184 | 69.1k | } else { |
185 | 69.1k | seq_params->op_params[i].decoder_model_param_present_flag = 0; |
186 | 69.1k | } |
187 | 72.4k | if (seq_params->timing_info_present && |
188 | 72.4k | (seq_params->timing_info.equal_picture_interval || |
189 | 6.17k | seq_params->op_params[i].decoder_model_param_present_flag)) { |
190 | 4.33k | seq_params->op_params[i].bitrate = av1_max_level_bitrate( |
191 | 4.33k | seq_params->profile, seq_params->seq_level_idx[i], |
192 | 4.33k | seq_params->tier[i]); |
193 | | // Level with seq_level_idx = 31 returns a high "dummy" bitrate to pass |
194 | | // the check |
195 | 4.33k | if (seq_params->op_params[i].bitrate == 0) |
196 | 0 | aom_internal_error(&pbi->error, AOM_CODEC_UNSUP_BITSTREAM, |
197 | 0 | "AV1 does not support this combination of " |
198 | 0 | "profile, level, and tier."); |
199 | | // Buffer size in bits/s is bitrate in bits/s * 1 s |
200 | 4.33k | seq_params->op_params[i].buffer_size = seq_params->op_params[i].bitrate; |
201 | 4.33k | } |
202 | 72.4k | if (seq_params->timing_info_present && |
203 | 72.4k | seq_params->timing_info.equal_picture_interval && |
204 | 72.4k | !seq_params->op_params[i].decoder_model_param_present_flag) { |
205 | | // When the decoder_model_parameters are not sent for this op, set |
206 | | // the default ones that can be used with the resource availability mode |
207 | 2.32k | seq_params->op_params[i].decoder_buffer_delay = 70000; |
208 | 2.32k | seq_params->op_params[i].encoder_buffer_delay = 20000; |
209 | 2.32k | seq_params->op_params[i].low_delay_mode_flag = 0; |
210 | 2.32k | } |
211 | | |
212 | 72.4k | if (seq_params->display_model_info_present_flag) { |
213 | 4.39k | seq_params->op_params[i].display_model_param_present_flag = |
214 | 4.39k | aom_rb_read_bit(rb); |
215 | 4.39k | if (seq_params->op_params[i].display_model_param_present_flag) { |
216 | 1.66k | seq_params->op_params[i].initial_display_delay = |
217 | 1.66k | aom_rb_read_literal(rb, 4) + 1; |
218 | 1.66k | if (seq_params->op_params[i].initial_display_delay > 10) |
219 | 436 | aom_internal_error( |
220 | 436 | &pbi->error, AOM_CODEC_UNSUP_BITSTREAM, |
221 | 436 | "AV1 does not support more than 10 decoded frames delay"); |
222 | 2.73k | } else { |
223 | 2.73k | seq_params->op_params[i].initial_display_delay = 10; |
224 | 2.73k | } |
225 | 68.0k | } else { |
226 | 68.0k | seq_params->op_params[i].display_model_param_present_flag = 0; |
227 | 68.0k | seq_params->op_params[i].initial_display_delay = 10; |
228 | 68.0k | } |
229 | 72.4k | } |
230 | 68.9k | } |
231 | | // This decoder supports all levels. Choose operating point provided by |
232 | | // external means |
233 | 82.5k | int operating_point = pbi->operating_point; |
234 | 82.5k | if (operating_point < 0 || |
235 | 82.5k | operating_point > seq_params->operating_points_cnt_minus_1) |
236 | 27.5k | operating_point = 0; |
237 | 82.5k | pbi->current_operating_point = |
238 | 82.5k | seq_params->operating_point_idc[operating_point]; |
239 | 82.5k | if (aom_get_num_layers_from_operating_point_idc( |
240 | 82.5k | pbi->current_operating_point, &pbi->number_spatial_layers, |
241 | 82.5k | &pbi->number_temporal_layers) != AOM_CODEC_OK) { |
242 | 0 | pbi->error.error_code = AOM_CODEC_ERROR; |
243 | 0 | return 0; |
244 | 0 | } |
245 | | |
246 | 82.5k | av1_read_sequence_header(cm, rb, seq_params); |
247 | | |
248 | 82.5k | av1_read_color_config(rb, pbi->allow_lowbitdepth, seq_params, &pbi->error); |
249 | 82.5k | if (!(seq_params->subsampling_x == 0 && seq_params->subsampling_y == 0) && |
250 | 82.5k | !(seq_params->subsampling_x == 1 && seq_params->subsampling_y == 1) && |
251 | 82.5k | !(seq_params->subsampling_x == 1 && seq_params->subsampling_y == 0)) { |
252 | 0 | aom_internal_error(&pbi->error, AOM_CODEC_UNSUP_BITSTREAM, |
253 | 0 | "Only 4:4:4, 4:2:2 and 4:2:0 are currently supported, " |
254 | 0 | "%d %d subsampling is not supported.\n", |
255 | 0 | seq_params->subsampling_x, seq_params->subsampling_y); |
256 | 0 | } |
257 | | |
258 | 82.5k | seq_params->film_grain_params_present = aom_rb_read_bit(rb); |
259 | | |
260 | 82.5k | if (av1_check_trailing_bits(pbi, rb) != 0) { |
261 | | // pbi->error.error_code is already set. |
262 | 3.21k | return 0; |
263 | 3.21k | } |
264 | | |
265 | | // If a sequence header has been decoded before, we check if the new |
266 | | // one is consistent with the old one. |
267 | 79.3k | if (pbi->sequence_header_ready) { |
268 | 61.0k | if (!are_seq_headers_consistent(cm->seq_params, seq_params)) |
269 | 29.2k | pbi->sequence_header_changed = 1; |
270 | 61.0k | } |
271 | | |
272 | 79.3k | *cm->seq_params = *seq_params; |
273 | 79.3k | pbi->sequence_header_ready = 1; |
274 | | |
275 | 79.3k | return ((rb->bit_offset - saved_bit_offset + 7) >> 3); |
276 | 82.5k | } |
277 | | |
278 | | // On success, returns the frame header size. On failure, calls |
279 | | // aom_internal_error and does not return. If show existing frame, |
280 | | // also marks the data processing to end after the frame header. |
281 | | static uint32_t read_frame_header_obu(AV1Decoder *pbi, |
282 | | struct aom_read_bit_buffer *rb, |
283 | | const uint8_t *data, |
284 | | const uint8_t **p_data_end, |
285 | 251k | int trailing_bits_present) { |
286 | 251k | const uint32_t hdr_size = |
287 | 251k | av1_decode_frame_headers_and_setup(pbi, rb, trailing_bits_present); |
288 | 251k | const AV1_COMMON *cm = &pbi->common; |
289 | 251k | if (cm->show_existing_frame) { |
290 | 807 | *p_data_end = data + hdr_size; |
291 | 807 | } |
292 | 251k | return hdr_size; |
293 | 251k | } |
294 | | |
295 | | // On success, returns the tile group header size. On failure, calls |
296 | | // aom_internal_error() and returns -1. |
297 | | static int32_t read_tile_group_header(AV1Decoder *pbi, |
298 | | struct aom_read_bit_buffer *rb, |
299 | | int *start_tile, int *end_tile, |
300 | 156k | int tile_start_implicit) { |
301 | 156k | AV1_COMMON *const cm = &pbi->common; |
302 | 156k | CommonTileParams *const tiles = &cm->tiles; |
303 | 156k | uint32_t saved_bit_offset = rb->bit_offset; |
304 | 156k | int tile_start_and_end_present_flag = 0; |
305 | 156k | const int num_tiles = tiles->rows * tiles->cols; |
306 | | |
307 | 156k | if (!tiles->large_scale && num_tiles > 1) { |
308 | 8.62k | tile_start_and_end_present_flag = aom_rb_read_bit(rb); |
309 | 8.62k | if (tile_start_implicit && tile_start_and_end_present_flag) { |
310 | 1.18k | aom_internal_error( |
311 | 1.18k | &pbi->error, AOM_CODEC_UNSUP_BITSTREAM, |
312 | 1.18k | "For OBU_FRAME type obu tile_start_and_end_present_flag must be 0"); |
313 | 1.18k | return -1; |
314 | 1.18k | } |
315 | 8.62k | } |
316 | 155k | if (tiles->large_scale || num_tiles == 1 || |
317 | 155k | !tile_start_and_end_present_flag) { |
318 | 154k | *start_tile = 0; |
319 | 154k | *end_tile = num_tiles - 1; |
320 | 154k | } else { |
321 | 69 | int tile_bits = tiles->log2_rows + tiles->log2_cols; |
322 | 69 | *start_tile = aom_rb_read_literal(rb, tile_bits); |
323 | 69 | *end_tile = aom_rb_read_literal(rb, tile_bits); |
324 | 69 | } |
325 | 155k | if (*start_tile != pbi->next_start_tile) { |
326 | 2 | aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME, |
327 | 2 | "tg_start (%d) must be equal to %d", *start_tile, |
328 | 2 | pbi->next_start_tile); |
329 | 2 | return -1; |
330 | 2 | } |
331 | 155k | if (*start_tile > *end_tile) { |
332 | 0 | aom_internal_error( |
333 | 0 | &pbi->error, AOM_CODEC_CORRUPT_FRAME, |
334 | 0 | "tg_end (%d) must be greater than or equal to tg_start (%d)", *end_tile, |
335 | 0 | *start_tile); |
336 | 0 | return -1; |
337 | 0 | } |
338 | 155k | if (*end_tile >= num_tiles) { |
339 | 1 | aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME, |
340 | 1 | "tg_end (%d) must be less than NumTiles (%d)", *end_tile, |
341 | 1 | num_tiles); |
342 | 1 | return -1; |
343 | 1 | } |
344 | 155k | pbi->next_start_tile = (*end_tile == num_tiles - 1) ? 0 : *end_tile + 1; |
345 | | |
346 | 155k | return ((rb->bit_offset - saved_bit_offset + 7) >> 3); |
347 | 155k | } |
348 | | |
349 | | // On success, returns the tile group OBU size. On failure, sets |
350 | | // pbi->common.error.error_code and returns 0. |
351 | | static uint32_t read_one_tile_group_obu( |
352 | | AV1Decoder *pbi, struct aom_read_bit_buffer *rb, int is_first_tg, |
353 | | const uint8_t *data, const uint8_t *data_end, const uint8_t **p_data_end, |
354 | 156k | int *is_last_tg, int tile_start_implicit) { |
355 | 156k | AV1_COMMON *const cm = &pbi->common; |
356 | 156k | int start_tile, end_tile; |
357 | 156k | int32_t header_size, tg_payload_size; |
358 | | |
359 | 156k | assert((rb->bit_offset & 7) == 0); |
360 | 156k | assert(rb->bit_buffer + aom_rb_bytes_read(rb) == data); |
361 | | |
362 | 156k | header_size = read_tile_group_header(pbi, rb, &start_tile, &end_tile, |
363 | 156k | tile_start_implicit); |
364 | 156k | if (header_size == -1 || byte_alignment(cm, rb)) return 0; |
365 | 154k | data += header_size; |
366 | 154k | av1_decode_tg_tiles_and_wrapup(pbi, data, data_end, p_data_end, start_tile, |
367 | 154k | end_tile, is_first_tg); |
368 | | |
369 | 154k | tg_payload_size = (uint32_t)(*p_data_end - data); |
370 | | |
371 | 154k | *is_last_tg = end_tile == cm->tiles.rows * cm->tiles.cols - 1; |
372 | 154k | return header_size + tg_payload_size; |
373 | 156k | } |
374 | | |
375 | | static void alloc_tile_list_buffer(AV1Decoder *pbi, int tile_width_in_pixels, |
376 | 610 | int tile_height_in_pixels) { |
377 | | // The resolution of the output frame is read out from the bitstream. The data |
378 | | // are stored in the order of Y plane, U plane and V plane. As an example, for |
379 | | // image format 4:2:0, the output frame of U plane and V plane is 1/4 of the |
380 | | // output frame. |
381 | 610 | AV1_COMMON *const cm = &pbi->common; |
382 | 610 | const int output_frame_width = |
383 | 610 | (pbi->output_frame_width_in_tiles_minus_1 + 1) * tile_width_in_pixels; |
384 | 610 | const int output_frame_height = |
385 | 610 | (pbi->output_frame_height_in_tiles_minus_1 + 1) * tile_height_in_pixels; |
386 | | // The output frame is used to store the decoded tile list. The decoded tile |
387 | | // list has to fit into 1 output frame. |
388 | 610 | assert((pbi->tile_count_minus_1 + 1) <= |
389 | 610 | (pbi->output_frame_width_in_tiles_minus_1 + 1) * |
390 | 610 | (pbi->output_frame_height_in_tiles_minus_1 + 1)); |
391 | | |
392 | | // Allocate the tile list output buffer. |
393 | | // Note: if cm->seq_params->use_highbitdepth is 1 and |
394 | | // cm->seq_params->bit_depth is 8, we could allocate less memory, namely, 8 |
395 | | // bits/pixel. |
396 | 610 | if (aom_alloc_frame_buffer(&pbi->tile_list_outbuf, output_frame_width, |
397 | 610 | output_frame_height, cm->seq_params->subsampling_x, |
398 | 610 | cm->seq_params->subsampling_y, |
399 | 610 | (cm->seq_params->use_highbitdepth && |
400 | 610 | (cm->seq_params->bit_depth > AOM_BITS_8)), |
401 | 610 | 0, cm->features.byte_alignment, false, 0)) |
402 | 173 | aom_internal_error(&pbi->error, AOM_CODEC_MEM_ERROR, |
403 | 173 | "Failed to allocate the tile list output buffer"); |
404 | 610 | } |
405 | | |
406 | | static void yv12_tile_copy(const YV12_BUFFER_CONFIG *src, int hstart1, |
407 | | int hend1, int vstart1, int vend1, |
408 | | YV12_BUFFER_CONFIG *dst, int hstart2, int vstart2, |
409 | 0 | int plane) { |
410 | 0 | const int src_stride = (plane > 0) ? src->strides[1] : src->strides[0]; |
411 | 0 | const int dst_stride = (plane > 0) ? dst->strides[1] : dst->strides[0]; |
412 | 0 | int row, col; |
413 | |
|
414 | 0 | assert(src->flags & YV12_FLAG_HIGHBITDEPTH); |
415 | 0 | assert(!(dst->flags & YV12_FLAG_HIGHBITDEPTH)); |
416 | | |
417 | 0 | const uint16_t *src16 = |
418 | 0 | CONVERT_TO_SHORTPTR(src->buffers[plane] + vstart1 * src_stride + hstart1); |
419 | 0 | uint8_t *dst8 = dst->buffers[plane] + vstart2 * dst_stride + hstart2; |
420 | |
|
421 | 0 | for (row = vstart1; row < vend1; ++row) { |
422 | 0 | for (col = 0; col < (hend1 - hstart1); ++col) *dst8++ = (uint8_t)(*src16++); |
423 | 0 | src16 += src_stride - (hend1 - hstart1); |
424 | 0 | dst8 += dst_stride - (hend1 - hstart1); |
425 | 0 | } |
426 | 0 | return; |
427 | 0 | } |
428 | | |
429 | | static void copy_decoded_tile_to_tile_list_buffer(AV1Decoder *pbi, int tile_idx, |
430 | | int tile_width_in_pixels, |
431 | 0 | int tile_height_in_pixels) { |
432 | 0 | AV1_COMMON *const cm = &pbi->common; |
433 | 0 | const int ssy = cm->seq_params->subsampling_y; |
434 | 0 | const int ssx = cm->seq_params->subsampling_x; |
435 | 0 | const int num_planes = av1_num_planes(cm); |
436 | |
|
437 | 0 | YV12_BUFFER_CONFIG *cur_frame = &cm->cur_frame->buf; |
438 | 0 | const int tr = tile_idx / (pbi->output_frame_width_in_tiles_minus_1 + 1); |
439 | 0 | const int tc = tile_idx % (pbi->output_frame_width_in_tiles_minus_1 + 1); |
440 | 0 | int plane; |
441 | | |
442 | | // Copy decoded tile to the tile list output buffer. |
443 | 0 | for (plane = 0; plane < num_planes; ++plane) { |
444 | 0 | const int shift_x = plane > 0 ? ssx : 0; |
445 | 0 | const int shift_y = plane > 0 ? ssy : 0; |
446 | 0 | const int h = tile_height_in_pixels >> shift_y; |
447 | 0 | const int w = tile_width_in_pixels >> shift_x; |
448 | | |
449 | | // src offset |
450 | 0 | int vstart1 = pbi->dec_tile_row * h; |
451 | 0 | int vend1 = vstart1 + h; |
452 | 0 | int hstart1 = pbi->dec_tile_col * w; |
453 | 0 | int hend1 = hstart1 + w; |
454 | | // dst offset |
455 | 0 | int vstart2 = tr * h; |
456 | 0 | int hstart2 = tc * w; |
457 | |
|
458 | 0 | if (cm->seq_params->use_highbitdepth && |
459 | 0 | cm->seq_params->bit_depth == AOM_BITS_8) { |
460 | 0 | yv12_tile_copy(cur_frame, hstart1, hend1, vstart1, vend1, |
461 | 0 | &pbi->tile_list_outbuf, hstart2, vstart2, plane); |
462 | 0 | } else { |
463 | 0 | switch (plane) { |
464 | 0 | case 0: |
465 | 0 | aom_yv12_partial_copy_y(cur_frame, hstart1, hend1, vstart1, vend1, |
466 | 0 | &pbi->tile_list_outbuf, hstart2, vstart2); |
467 | 0 | break; |
468 | 0 | case 1: |
469 | 0 | aom_yv12_partial_copy_u(cur_frame, hstart1, hend1, vstart1, vend1, |
470 | 0 | &pbi->tile_list_outbuf, hstart2, vstart2); |
471 | 0 | break; |
472 | 0 | case 2: |
473 | 0 | aom_yv12_partial_copy_v(cur_frame, hstart1, hend1, vstart1, vend1, |
474 | 0 | &pbi->tile_list_outbuf, hstart2, vstart2); |
475 | 0 | break; |
476 | 0 | default: assert(0); |
477 | 0 | } |
478 | 0 | } |
479 | 0 | } |
480 | 0 | } |
481 | | |
482 | | // Only called while large_scale_tile = 1. |
483 | | // |
484 | | // On success, returns the tile list OBU size. On failure, sets |
485 | | // pbi->common.error.error_code and returns 0. |
486 | | static uint32_t read_and_decode_one_tile_list(AV1Decoder *pbi, |
487 | | struct aom_read_bit_buffer *rb, |
488 | | const uint8_t *data, |
489 | | const uint8_t *data_end, |
490 | | const uint8_t **p_data_end, |
491 | 801 | int *frame_decoding_finished) { |
492 | 801 | AV1_COMMON *const cm = &pbi->common; |
493 | 801 | uint32_t tile_list_payload_size = 0; |
494 | 801 | const int num_tiles = cm->tiles.cols * cm->tiles.rows; |
495 | 801 | const int start_tile = 0; |
496 | 801 | const int end_tile = num_tiles - 1; |
497 | 801 | int i = 0; |
498 | | |
499 | | // Process the tile list info. |
500 | 801 | pbi->output_frame_width_in_tiles_minus_1 = aom_rb_read_literal(rb, 8); |
501 | 801 | pbi->output_frame_height_in_tiles_minus_1 = aom_rb_read_literal(rb, 8); |
502 | 801 | pbi->tile_count_minus_1 = aom_rb_read_literal(rb, 16); |
503 | | |
504 | | // The output frame is used to store the decoded tile list. The decoded tile |
505 | | // list has to fit into 1 output frame. |
506 | 801 | if ((pbi->tile_count_minus_1 + 1) > |
507 | 801 | (pbi->output_frame_width_in_tiles_minus_1 + 1) * |
508 | 801 | (pbi->output_frame_height_in_tiles_minus_1 + 1)) { |
509 | 91 | pbi->error.error_code = AOM_CODEC_CORRUPT_FRAME; |
510 | 91 | return 0; |
511 | 91 | } |
512 | | |
513 | 710 | if (pbi->tile_count_minus_1 > MAX_TILES - 1) { |
514 | 12 | pbi->error.error_code = AOM_CODEC_CORRUPT_FRAME; |
515 | 12 | return 0; |
516 | 12 | } |
517 | | |
518 | 698 | int tile_width, tile_height; |
519 | 698 | if (!av1_get_uniform_tile_size(cm, &tile_width, &tile_height)) { |
520 | 88 | pbi->error.error_code = AOM_CODEC_CORRUPT_FRAME; |
521 | 88 | return 0; |
522 | 88 | } |
523 | 610 | const int tile_width_in_pixels = tile_width * MI_SIZE; |
524 | 610 | const int tile_height_in_pixels = tile_height * MI_SIZE; |
525 | | |
526 | | // Allocate output frame buffer for the tile list. |
527 | 610 | alloc_tile_list_buffer(pbi, tile_width_in_pixels, tile_height_in_pixels); |
528 | | |
529 | 610 | uint32_t tile_list_info_bytes = 4; |
530 | 610 | tile_list_payload_size += tile_list_info_bytes; |
531 | 610 | data += tile_list_info_bytes; |
532 | | |
533 | 610 | int tile_idx = 0; |
534 | 610 | for (i = 0; i <= pbi->tile_count_minus_1; i++) { |
535 | | // Process 1 tile. |
536 | | // Reset the bit reader. |
537 | 437 | rb->bit_offset = 0; |
538 | 437 | rb->bit_buffer = data; |
539 | | |
540 | | // Read out the tile info. |
541 | 437 | uint32_t tile_info_bytes = 5; |
542 | | // Set reference for each tile. |
543 | 437 | int ref_idx = aom_rb_read_literal(rb, 8); |
544 | 437 | if (ref_idx >= MAX_EXTERNAL_REFERENCES) { |
545 | 80 | pbi->error.error_code = AOM_CODEC_CORRUPT_FRAME; |
546 | 80 | return 0; |
547 | 80 | } |
548 | 357 | av1_set_reference_dec(cm, cm->remapped_ref_idx[0], 1, |
549 | 357 | &pbi->ext_refs.refs[ref_idx]); |
550 | | |
551 | 357 | pbi->dec_tile_row = aom_rb_read_literal(rb, 8); |
552 | 357 | pbi->dec_tile_col = aom_rb_read_literal(rb, 8); |
553 | 357 | if (pbi->dec_tile_row < 0 || pbi->dec_tile_col < 0 || |
554 | 357 | pbi->dec_tile_row >= cm->tiles.rows || |
555 | 357 | pbi->dec_tile_col >= cm->tiles.cols) { |
556 | 0 | pbi->error.error_code = AOM_CODEC_CORRUPT_FRAME; |
557 | 0 | return 0; |
558 | 0 | } |
559 | | |
560 | 357 | pbi->coded_tile_data_size = aom_rb_read_literal(rb, 16) + 1; |
561 | 357 | data += tile_info_bytes; |
562 | 357 | if ((size_t)(data_end - data) < pbi->coded_tile_data_size) { |
563 | 0 | pbi->error.error_code = AOM_CODEC_CORRUPT_FRAME; |
564 | 0 | return 0; |
565 | 0 | } |
566 | | |
567 | 357 | av1_decode_tg_tiles_and_wrapup(pbi, data, data + pbi->coded_tile_data_size, |
568 | 357 | p_data_end, start_tile, end_tile, 0); |
569 | 357 | uint32_t tile_payload_size = (uint32_t)(*p_data_end - data); |
570 | | |
571 | 357 | tile_list_payload_size += tile_info_bytes + tile_payload_size; |
572 | | |
573 | | // Update data ptr for next tile decoding. |
574 | 357 | data = *p_data_end; |
575 | 357 | assert(data <= data_end); |
576 | | |
577 | | // Copy the decoded tile to the tile list output buffer. |
578 | 0 | copy_decoded_tile_to_tile_list_buffer(pbi, tile_idx, tile_width_in_pixels, |
579 | 0 | tile_height_in_pixels); |
580 | 0 | tile_idx++; |
581 | 0 | } |
582 | | |
583 | 173 | *frame_decoding_finished = 1; |
584 | 173 | return tile_list_payload_size; |
585 | 610 | } |
586 | | |
587 | | // Returns the last nonzero byte index in 'data'. If there is no nonzero byte in |
588 | | // 'data', returns -1. |
589 | 421 | static int get_last_nonzero_byte_index(const uint8_t *data, size_t sz) { |
590 | | // Scan backward and return on the first nonzero byte. |
591 | 421 | int i = (int)sz - 1; |
592 | 1.09k | while (i >= 0 && data[i] == 0) { |
593 | 678 | --i; |
594 | 678 | } |
595 | 421 | return i; |
596 | 421 | } |
597 | | |
598 | | // Allocates metadata that was read and adds it to the decoders metadata array. |
599 | | static void alloc_read_metadata(AV1Decoder *const pbi, |
600 | | OBU_METADATA_TYPE metadata_type, |
601 | | const uint8_t *data, size_t sz, |
602 | 1.07k | aom_metadata_insert_flags_t insert_flag) { |
603 | 1.07k | if (!pbi->metadata) { |
604 | 118 | pbi->metadata = aom_img_metadata_array_alloc(0); |
605 | 118 | if (!pbi->metadata) { |
606 | 0 | aom_internal_error(&pbi->error, AOM_CODEC_MEM_ERROR, |
607 | 0 | "Failed to allocate metadata array"); |
608 | 0 | } |
609 | 118 | } |
610 | 1.07k | aom_metadata_t *metadata = |
611 | 1.07k | aom_img_metadata_alloc(metadata_type, data, sz, insert_flag); |
612 | 1.07k | if (!metadata) { |
613 | 0 | aom_internal_error(&pbi->error, AOM_CODEC_MEM_ERROR, |
614 | 0 | "Error allocating metadata"); |
615 | 0 | } |
616 | 1.07k | aom_metadata_t **metadata_array = |
617 | 1.07k | (aom_metadata_t **)realloc(pbi->metadata->metadata_array, |
618 | 1.07k | (pbi->metadata->sz + 1) * sizeof(metadata)); |
619 | 1.07k | if (!metadata_array) { |
620 | 0 | aom_img_metadata_free(metadata); |
621 | 0 | aom_internal_error(&pbi->error, AOM_CODEC_MEM_ERROR, |
622 | 0 | "Error growing metadata array"); |
623 | 0 | } |
624 | 1.07k | pbi->metadata->metadata_array = metadata_array; |
625 | 1.07k | pbi->metadata->metadata_array[pbi->metadata->sz] = metadata; |
626 | 1.07k | pbi->metadata->sz++; |
627 | 1.07k | } |
628 | | |
629 | | // On failure, calls aom_internal_error() and does not return. |
630 | | static void read_metadata_itut_t35(AV1Decoder *const pbi, const uint8_t *data, |
631 | 587 | size_t sz, bool has_obu_extension_header) { |
632 | 587 | if (sz == 0) { |
633 | 156 | aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME, |
634 | 156 | "itu_t_t35_country_code is missing"); |
635 | 156 | } |
636 | 587 | int country_code_size = 1; |
637 | 587 | if (*data == 0xFF) { |
638 | 229 | if (sz == 1) { |
639 | 10 | aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME, |
640 | 10 | "itu_t_t35_country_code_extension_byte is missing"); |
641 | 10 | } |
642 | 229 | ++country_code_size; |
643 | 229 | } |
644 | 587 | int end_index = get_last_nonzero_byte_index(data, sz); |
645 | 587 | if (end_index < country_code_size) { |
646 | 27 | aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME, |
647 | 27 | "No trailing bits found in ITU-T T.35 metadata OBU"); |
648 | 27 | } |
649 | | // itu_t_t35_payload_bytes is byte aligned. Section 6.7.2 of the spec says: |
650 | | // itu_t_t35_payload_bytes shall be bytes containing data registered as |
651 | | // specified in Recommendation ITU-T T.35. |
652 | | // Therefore the first trailing byte should be 0x80. |
653 | 587 | if (data[end_index] != 0x80) { |
654 | 282 | aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME, |
655 | 282 | "The last nonzero byte of the ITU-T T.35 metadata OBU " |
656 | 282 | "is 0x%02x, should be 0x80.", |
657 | 282 | data[end_index]); |
658 | 282 | } |
659 | 587 | alloc_read_metadata(pbi, OBU_METADATA_TYPE_ITUT_T35, data, end_index, |
660 | 587 | has_obu_extension_header |
661 | 587 | ? AOM_MIF_ANY_FRAME_LAYER_SPECIFIC |
662 | 587 | : AOM_MIF_ANY_FRAME); |
663 | 587 | } |
664 | | |
665 | | // On success, returns the number of bytes read from 'data'. On failure, calls |
666 | | // aom_internal_error() and does not return. |
667 | | static size_t read_metadata_hdr_cll(AV1Decoder *const pbi, const uint8_t *data, |
668 | 627 | size_t sz) { |
669 | 627 | const size_t kHdrCllPayloadSize = 4; |
670 | 627 | if (sz < kHdrCllPayloadSize) { |
671 | 41 | aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME, |
672 | 41 | "Incorrect HDR CLL metadata payload size"); |
673 | 41 | } |
674 | 627 | alloc_read_metadata(pbi, OBU_METADATA_TYPE_HDR_CLL, data, kHdrCllPayloadSize, |
675 | 627 | AOM_MIF_ANY_FRAME); |
676 | 627 | return kHdrCllPayloadSize; |
677 | 627 | } |
678 | | |
679 | | // On success, returns the number of bytes read from 'data'. On failure, calls |
680 | | // aom_internal_error() and does not return. |
681 | | static size_t read_metadata_hdr_mdcv(AV1Decoder *const pbi, const uint8_t *data, |
682 | 674 | size_t sz) { |
683 | 674 | const size_t kMdcvPayloadSize = 24; |
684 | 674 | if (sz < kMdcvPayloadSize) { |
685 | 293 | aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME, |
686 | 293 | "Incorrect HDR MDCV metadata payload size"); |
687 | 293 | } |
688 | 674 | alloc_read_metadata(pbi, OBU_METADATA_TYPE_HDR_MDCV, data, kMdcvPayloadSize, |
689 | 674 | AOM_MIF_ANY_FRAME); |
690 | 674 | return kMdcvPayloadSize; |
691 | 674 | } |
692 | | |
693 | 4.39k | static void scalability_structure(struct aom_read_bit_buffer *rb) { |
694 | 4.39k | const int spatial_layers_cnt_minus_1 = aom_rb_read_literal(rb, 2); |
695 | 4.39k | const int spatial_layer_dimensions_present_flag = aom_rb_read_bit(rb); |
696 | 4.39k | const int spatial_layer_description_present_flag = aom_rb_read_bit(rb); |
697 | 4.39k | const int temporal_group_description_present_flag = aom_rb_read_bit(rb); |
698 | | // scalability_structure_reserved_3bits must be set to zero and be ignored by |
699 | | // decoders. |
700 | 4.39k | aom_rb_read_literal(rb, 3); |
701 | | |
702 | 4.39k | if (spatial_layer_dimensions_present_flag) { |
703 | 5.02k | for (int i = 0; i <= spatial_layers_cnt_minus_1; i++) { |
704 | 2.90k | aom_rb_read_literal(rb, 16); |
705 | 2.90k | aom_rb_read_literal(rb, 16); |
706 | 2.90k | } |
707 | 2.11k | } |
708 | 4.39k | if (spatial_layer_description_present_flag) { |
709 | 1.09k | for (int i = 0; i <= spatial_layers_cnt_minus_1; i++) { |
710 | 736 | aom_rb_read_literal(rb, 8); |
711 | 736 | } |
712 | 359 | } |
713 | 4.39k | if (temporal_group_description_present_flag) { |
714 | 2.29k | const int temporal_group_size = aom_rb_read_literal(rb, 8); |
715 | 5.25k | for (int i = 0; i < temporal_group_size; i++) { |
716 | 2.96k | aom_rb_read_literal(rb, 3); |
717 | 2.96k | aom_rb_read_bit(rb); |
718 | 2.96k | aom_rb_read_bit(rb); |
719 | 2.96k | const int temporal_group_ref_cnt = aom_rb_read_literal(rb, 3); |
720 | 4.44k | for (int j = 0; j < temporal_group_ref_cnt; j++) { |
721 | 1.47k | aom_rb_read_literal(rb, 8); |
722 | 1.47k | } |
723 | 2.96k | } |
724 | 2.29k | } |
725 | 4.39k | } |
726 | | |
727 | 4.47k | static void read_metadata_scalability(struct aom_read_bit_buffer *rb) { |
728 | 4.47k | const int scalability_mode_idc = aom_rb_read_literal(rb, 8); |
729 | 4.47k | if (scalability_mode_idc == SCALABILITY_SS) { |
730 | 4.39k | scalability_structure(rb); |
731 | 4.39k | } |
732 | 4.47k | } |
733 | | |
734 | 707 | static void read_metadata_timecode(struct aom_read_bit_buffer *rb) { |
735 | 707 | aom_rb_read_literal(rb, 5); // counting_type f(5) |
736 | 707 | const int full_timestamp_flag = |
737 | 707 | aom_rb_read_bit(rb); // full_timestamp_flag f(1) |
738 | 707 | aom_rb_read_bit(rb); // discontinuity_flag (f1) |
739 | 707 | aom_rb_read_bit(rb); // cnt_dropped_flag f(1) |
740 | 707 | aom_rb_read_literal(rb, 9); // n_frames f(9) |
741 | 707 | if (full_timestamp_flag) { |
742 | 280 | aom_rb_read_literal(rb, 6); // seconds_value f(6) |
743 | 280 | aom_rb_read_literal(rb, 6); // minutes_value f(6) |
744 | 280 | aom_rb_read_literal(rb, 5); // hours_value f(5) |
745 | 427 | } else { |
746 | 427 | const int seconds_flag = aom_rb_read_bit(rb); // seconds_flag f(1) |
747 | 427 | if (seconds_flag) { |
748 | 233 | aom_rb_read_literal(rb, 6); // seconds_value f(6) |
749 | 233 | const int minutes_flag = aom_rb_read_bit(rb); // minutes_flag f(1) |
750 | 233 | if (minutes_flag) { |
751 | 181 | aom_rb_read_literal(rb, 6); // minutes_value f(6) |
752 | 181 | const int hours_flag = aom_rb_read_bit(rb); // hours_flag f(1) |
753 | 181 | if (hours_flag) { |
754 | 162 | aom_rb_read_literal(rb, 5); // hours_value f(5) |
755 | 162 | } |
756 | 181 | } |
757 | 233 | } |
758 | 427 | } |
759 | | // time_offset_length f(5) |
760 | 707 | const int time_offset_length = aom_rb_read_literal(rb, 5); |
761 | 707 | if (time_offset_length) { |
762 | | // time_offset_value f(time_offset_length) |
763 | 523 | aom_rb_read_literal(rb, time_offset_length); |
764 | 523 | } |
765 | 707 | } |
766 | | |
767 | | // Returns the last nonzero byte in 'data'. If there is no nonzero byte in |
768 | | // 'data', returns 0. |
769 | | // |
770 | | // Call this function to check the following requirement in the spec: |
771 | | // This implies that when any payload data is present for this OBU type, at |
772 | | // least one byte of the payload data (including the trailing bit) shall not |
773 | | // be equal to 0. |
774 | 6.43k | static uint8_t get_last_nonzero_byte(const uint8_t *data, size_t sz) { |
775 | | // Scan backward and return on the first nonzero byte. |
776 | 6.43k | size_t i = sz; |
777 | 26.7k | while (i != 0) { |
778 | 25.9k | --i; |
779 | 25.9k | if (data[i] != 0) return data[i]; |
780 | 25.9k | } |
781 | 772 | return 0; |
782 | 6.43k | } |
783 | | |
784 | | // Checks the metadata for correct syntax but ignores the parsed metadata. |
785 | | // |
786 | | // On success, returns the number of bytes read from 'data'. On failure, sets |
787 | | // pbi->common.error.error_code and returns 0, or calls aom_internal_error() |
788 | | // and does not return. |
789 | | static size_t read_metadata(AV1Decoder *pbi, const uint8_t *data, size_t sz, |
790 | 10.9k | bool has_obu_extension_header) { |
791 | 10.9k | size_t type_length; |
792 | 10.9k | uint64_t type_value; |
793 | 10.9k | if (aom_uleb_decode(data, sz, &type_value, &type_length) < 0) { |
794 | 130 | pbi->error.error_code = AOM_CODEC_CORRUPT_FRAME; |
795 | 130 | return 0; |
796 | 130 | } |
797 | 10.8k | const OBU_METADATA_TYPE metadata_type = (OBU_METADATA_TYPE)type_value; |
798 | 10.8k | if (metadata_type == 0 || metadata_type >= 6) { |
799 | | // If metadata_type is reserved for future use or a user private value, |
800 | | // ignore the entire OBU and just check trailing bits. |
801 | 3.77k | if (get_last_nonzero_byte(data + type_length, sz - type_length) == 0) { |
802 | 356 | pbi->error.error_code = AOM_CODEC_CORRUPT_FRAME; |
803 | 356 | return 0; |
804 | 356 | } |
805 | 3.42k | return sz; |
806 | 3.77k | } |
807 | 7.07k | if (metadata_type == OBU_METADATA_TYPE_ITUT_T35) { |
808 | | // read_metadata_itut_t35() checks trailing bits. |
809 | 587 | read_metadata_itut_t35(pbi, data + type_length, sz - type_length, |
810 | 587 | has_obu_extension_header); |
811 | 587 | return sz; |
812 | 6.48k | } else if (metadata_type == OBU_METADATA_TYPE_HDR_CLL) { |
813 | 627 | size_t bytes_read = |
814 | 627 | type_length + |
815 | 627 | read_metadata_hdr_cll(pbi, data + type_length, sz - type_length); |
816 | 627 | if (get_last_nonzero_byte(data + bytes_read, sz - bytes_read) != 0x80) { |
817 | 506 | pbi->error.error_code = AOM_CODEC_CORRUPT_FRAME; |
818 | 506 | return 0; |
819 | 506 | } |
820 | 121 | return sz; |
821 | 5.86k | } else if (metadata_type == OBU_METADATA_TYPE_HDR_MDCV) { |
822 | 674 | size_t bytes_read = |
823 | 674 | type_length + |
824 | 674 | read_metadata_hdr_mdcv(pbi, data + type_length, sz - type_length); |
825 | 674 | if (get_last_nonzero_byte(data + bytes_read, sz - bytes_read) != 0x80) { |
826 | 366 | pbi->error.error_code = AOM_CODEC_CORRUPT_FRAME; |
827 | 366 | return 0; |
828 | 366 | } |
829 | 308 | return sz; |
830 | 674 | } |
831 | | |
832 | 5.18k | struct aom_read_bit_buffer rb; |
833 | 5.18k | av1_init_read_bit_buffer(pbi, &rb, data + type_length, data + sz); |
834 | 5.18k | if (metadata_type == OBU_METADATA_TYPE_SCALABILITY) { |
835 | 4.47k | read_metadata_scalability(&rb); |
836 | 4.47k | } else { |
837 | 707 | assert(metadata_type == OBU_METADATA_TYPE_TIMECODE); |
838 | 707 | read_metadata_timecode(&rb); |
839 | 707 | } |
840 | 5.18k | if (av1_check_trailing_bits(pbi, &rb) != 0) { |
841 | | // pbi->error.error_code is already set. |
842 | 3.28k | return 0; |
843 | 3.28k | } |
844 | 1.89k | assert((rb.bit_offset & 7) == 0); |
845 | 158 | return type_length + (rb.bit_offset >> 3); |
846 | 1.89k | } |
847 | | |
848 | | // On success, returns 'sz'. On failure, sets pbi->common.error.error_code and |
849 | | // returns 0. |
850 | | static size_t read_padding(AV1_COMMON *const cm, const uint8_t *data, |
851 | 3.39k | size_t sz) { |
852 | | // The spec allows a padding OBU to be header-only (i.e., obu_size = 0). So |
853 | | // check trailing bits only if sz > 0. |
854 | 3.39k | if (sz > 0) { |
855 | | // The payload of a padding OBU is byte aligned. Therefore the first |
856 | | // trailing byte should be 0x80. See https://crbug.com/aomedia/2393. |
857 | 68 | const uint8_t last_nonzero_byte = get_last_nonzero_byte(data, sz); |
858 | 68 | if (last_nonzero_byte != 0x80) { |
859 | 44 | cm->error->error_code = AOM_CODEC_CORRUPT_FRAME; |
860 | 44 | return 0; |
861 | 44 | } |
862 | 68 | } |
863 | 3.35k | return sz; |
864 | 3.39k | } |
865 | | |
866 | | // On success, returns a boolean that indicates whether the decoding of the |
867 | | // current frame is finished. On failure, sets pbi->error.error_code and |
868 | | // returns -1. |
869 | | int aom_decode_frame_from_obus(struct AV1Decoder *pbi, const uint8_t *data, |
870 | | const uint8_t *data_end, |
871 | 311k | const uint8_t **p_data_end) { |
872 | 311k | AV1_COMMON *const cm = &pbi->common; |
873 | 311k | int frame_decoding_finished = 0; |
874 | 311k | int is_first_tg_obu_received = 1; |
875 | | // Whenever pbi->seen_frame_header is set to 1, frame_header is set to the |
876 | | // beginning of the frame_header_obu and frame_header_size is set to its |
877 | | // size. This allows us to check if a redundant frame_header_obu is a copy |
878 | | // of the previous frame_header_obu. |
879 | | // |
880 | | // Initialize frame_header to a dummy nonnull pointer, otherwise the Clang |
881 | | // Static Analyzer in clang 7.0.1 will falsely warn that a null pointer is |
882 | | // passed as an argument to a 'nonnull' parameter of memcmp(). The initial |
883 | | // value will not be used. |
884 | 311k | const uint8_t *frame_header = data; |
885 | 311k | uint32_t frame_header_size = 0; |
886 | 311k | ObuHeader obu_header; |
887 | 311k | memset(&obu_header, 0, sizeof(obu_header)); |
888 | 311k | pbi->seen_frame_header = 0; |
889 | 311k | pbi->next_start_tile = 0; |
890 | 311k | pbi->num_tile_groups = 0; |
891 | | |
892 | 311k | if (data_end < data) { |
893 | 0 | pbi->error.error_code = AOM_CODEC_CORRUPT_FRAME; |
894 | 0 | return -1; |
895 | 0 | } |
896 | | |
897 | | // Reset pbi->camera_frame_header_ready to 0 if cm->tiles.large_scale = 0. |
898 | 311k | if (!cm->tiles.large_scale) pbi->camera_frame_header_ready = 0; |
899 | | |
900 | | // decode frame as a series of OBUs |
901 | 642k | while (!frame_decoding_finished && pbi->error.error_code == AOM_CODEC_OK) { |
902 | 537k | struct aom_read_bit_buffer rb; |
903 | 537k | size_t payload_size = 0; |
904 | 537k | size_t decoded_payload_size = 0; |
905 | 537k | size_t obu_payload_offset = 0; |
906 | 537k | size_t bytes_read = 0; |
907 | 537k | const size_t bytes_available = data_end - data; |
908 | | |
909 | 537k | if (bytes_available == 0 && !pbi->seen_frame_header) { |
910 | 697 | *p_data_end = data; |
911 | 697 | pbi->error.error_code = AOM_CODEC_OK; |
912 | 697 | break; |
913 | 697 | } |
914 | | |
915 | 536k | aom_codec_err_t status = |
916 | 536k | aom_read_obu_header_and_size(data, bytes_available, pbi->is_annexb, |
917 | 536k | &obu_header, &payload_size, &bytes_read); |
918 | | |
919 | 536k | if (status != AOM_CODEC_OK) { |
920 | 28.9k | pbi->error.error_code = status; |
921 | 28.9k | return -1; |
922 | 28.9k | } |
923 | | |
924 | | // Record obu size header information. |
925 | 507k | pbi->obu_size_hdr.data = data + obu_header.size; |
926 | 507k | pbi->obu_size_hdr.size = bytes_read - obu_header.size; |
927 | | |
928 | | // Note: aom_read_obu_header_and_size() takes care of checking that this |
929 | | // doesn't cause 'data' to advance past 'data_end'. |
930 | 507k | data += bytes_read; |
931 | | |
932 | 507k | if ((size_t)(data_end - data) < payload_size) { |
933 | 8.17k | pbi->error.error_code = AOM_CODEC_CORRUPT_FRAME; |
934 | 8.17k | return -1; |
935 | 8.17k | } |
936 | | |
937 | 499k | cm->temporal_layer_id = obu_header.temporal_layer_id; |
938 | 499k | cm->spatial_layer_id = obu_header.spatial_layer_id; |
939 | | |
940 | 499k | if (obu_header.type != OBU_TEMPORAL_DELIMITER && |
941 | 499k | obu_header.type != OBU_SEQUENCE_HEADER) { |
942 | | // don't decode obu if it's not in current operating mode |
943 | 271k | if (!is_obu_in_current_operating_point(pbi, &obu_header)) { |
944 | 1.09k | data += payload_size; |
945 | 1.09k | continue; |
946 | 1.09k | } |
947 | 271k | } |
948 | | |
949 | 498k | av1_init_read_bit_buffer(pbi, &rb, data, data + payload_size); |
950 | | |
951 | 498k | switch (obu_header.type) { |
952 | 140k | case OBU_TEMPORAL_DELIMITER: |
953 | 140k | decoded_payload_size = read_temporal_delimiter_obu(); |
954 | 140k | if (pbi->seen_frame_header) { |
955 | | // A new temporal unit has started, but the frame in the previous |
956 | | // temporal unit is incomplete. |
957 | 16 | pbi->error.error_code = AOM_CODEC_CORRUPT_FRAME; |
958 | 16 | return -1; |
959 | 16 | } |
960 | 140k | break; |
961 | 140k | case OBU_SEQUENCE_HEADER: |
962 | 87.4k | decoded_payload_size = read_sequence_header_obu(pbi, &rb); |
963 | 87.4k | if (pbi->error.error_code != AOM_CODEC_OK) return -1; |
964 | | // The sequence header should not change in the middle of a frame. |
965 | 79.3k | if (pbi->sequence_header_changed && pbi->seen_frame_header) { |
966 | 2 | pbi->error.error_code = AOM_CODEC_CORRUPT_FRAME; |
967 | 2 | return -1; |
968 | 2 | } |
969 | 79.3k | break; |
970 | 79.3k | case OBU_FRAME_HEADER: |
971 | 1.75k | case OBU_REDUNDANT_FRAME_HEADER: |
972 | 252k | case OBU_FRAME: |
973 | 252k | if (obu_header.type == OBU_REDUNDANT_FRAME_HEADER) { |
974 | 703 | if (!pbi->seen_frame_header) { |
975 | 55 | pbi->error.error_code = AOM_CODEC_CORRUPT_FRAME; |
976 | 55 | return -1; |
977 | 55 | } |
978 | 252k | } else { |
979 | | // OBU_FRAME_HEADER or OBU_FRAME. |
980 | 252k | if (pbi->seen_frame_header) { |
981 | 21 | pbi->error.error_code = AOM_CODEC_CORRUPT_FRAME; |
982 | 21 | return -1; |
983 | 21 | } |
984 | 252k | } |
985 | | // Only decode first frame header received |
986 | 252k | if (!pbi->seen_frame_header || |
987 | 252k | (cm->tiles.large_scale && !pbi->camera_frame_header_ready)) { |
988 | 251k | frame_header_size = read_frame_header_obu( |
989 | 251k | pbi, &rb, data, p_data_end, obu_header.type != OBU_FRAME); |
990 | 251k | frame_header = data; |
991 | 251k | pbi->seen_frame_header = 1; |
992 | 251k | if (!pbi->ext_tile_debug && cm->tiles.large_scale) |
993 | 14.0k | pbi->camera_frame_header_ready = 1; |
994 | 251k | } else { |
995 | | // Verify that the frame_header_obu is identical to the original |
996 | | // frame_header_obu. |
997 | 648 | if (frame_header_size > payload_size || |
998 | 648 | memcmp(data, frame_header, frame_header_size) != 0) { |
999 | 365 | pbi->error.error_code = AOM_CODEC_CORRUPT_FRAME; |
1000 | 365 | return -1; |
1001 | 365 | } |
1002 | 283 | assert(rb.bit_offset == 0); |
1003 | 283 | rb.bit_offset = 8 * frame_header_size; |
1004 | 283 | } |
1005 | | |
1006 | 252k | decoded_payload_size = frame_header_size; |
1007 | 252k | pbi->frame_header_size = frame_header_size; |
1008 | 252k | cm->cur_frame->temporal_id = obu_header.temporal_layer_id; |
1009 | 252k | cm->cur_frame->spatial_id = obu_header.spatial_layer_id; |
1010 | | |
1011 | 252k | if (cm->show_existing_frame) { |
1012 | 807 | if (obu_header.type == OBU_FRAME) { |
1013 | 663 | pbi->error.error_code = AOM_CODEC_UNSUP_BITSTREAM; |
1014 | 663 | return -1; |
1015 | 663 | } |
1016 | 144 | frame_decoding_finished = 1; |
1017 | 144 | pbi->seen_frame_header = 0; |
1018 | | |
1019 | 144 | if (cm->show_frame && |
1020 | 144 | !cm->seq_params->order_hint_info.enable_order_hint) { |
1021 | 0 | ++cm->current_frame.frame_number; |
1022 | 0 | } |
1023 | 144 | break; |
1024 | 807 | } |
1025 | | |
1026 | | // In large scale tile coding, decode the common camera frame header |
1027 | | // before any tile list OBU. |
1028 | 251k | if (!pbi->ext_tile_debug && pbi->camera_frame_header_ready) { |
1029 | 14.0k | frame_decoding_finished = 1; |
1030 | | // Skip the rest of the frame data. |
1031 | 14.0k | decoded_payload_size = payload_size; |
1032 | | // Update data_end. |
1033 | 14.0k | *p_data_end = data_end; |
1034 | 14.0k | break; |
1035 | 14.0k | } |
1036 | | |
1037 | 237k | if (obu_header.type != OBU_FRAME) break; |
1038 | 236k | obu_payload_offset = frame_header_size; |
1039 | | // Byte align the reader before reading the tile group. |
1040 | | // byte_alignment() has set pbi->error.error_code if it returns -1. |
1041 | 236k | if (byte_alignment(cm, &rb)) return -1; |
1042 | 231k | AOM_FALLTHROUGH_INTENDED; // fall through to read tile group. |
1043 | 231k | case OBU_TILE_GROUP: |
1044 | 231k | if (!pbi->seen_frame_header) { |
1045 | 94 | pbi->error.error_code = AOM_CODEC_CORRUPT_FRAME; |
1046 | 94 | return -1; |
1047 | 94 | } |
1048 | 231k | if (obu_payload_offset > payload_size) { |
1049 | 0 | pbi->error.error_code = AOM_CODEC_CORRUPT_FRAME; |
1050 | 0 | return -1; |
1051 | 0 | } |
1052 | 231k | decoded_payload_size += read_one_tile_group_obu( |
1053 | 231k | pbi, &rb, is_first_tg_obu_received, data + obu_payload_offset, |
1054 | 231k | data + payload_size, p_data_end, &frame_decoding_finished, |
1055 | 231k | obu_header.type == OBU_FRAME); |
1056 | 231k | if (pbi->error.error_code != AOM_CODEC_OK) return -1; |
1057 | 229k | is_first_tg_obu_received = 0; |
1058 | 229k | if (frame_decoding_finished) { |
1059 | 91.1k | pbi->seen_frame_header = 0; |
1060 | 91.1k | pbi->next_start_tile = 0; |
1061 | 91.1k | } |
1062 | 229k | pbi->num_tile_groups++; |
1063 | 229k | break; |
1064 | 10.9k | case OBU_METADATA: { |
1065 | 10.9k | decoded_payload_size = |
1066 | 10.9k | read_metadata(pbi, data, payload_size, obu_header.has_extension); |
1067 | 10.9k | if (pbi->error.error_code != AOM_CODEC_OK) return -1; |
1068 | 6.33k | break; |
1069 | 10.9k | } |
1070 | 6.33k | case OBU_TILE_LIST: |
1071 | 1.05k | if (CONFIG_NORMAL_TILE_MODE) { |
1072 | 0 | pbi->error.error_code = AOM_CODEC_UNSUP_BITSTREAM; |
1073 | 0 | return -1; |
1074 | 0 | } |
1075 | | |
1076 | | // This OBU type is purely for the large scale tile coding mode. |
1077 | | // The common camera frame header has to be already decoded. |
1078 | 1.05k | if (!pbi->camera_frame_header_ready) { |
1079 | 257 | pbi->error.error_code = AOM_CODEC_CORRUPT_FRAME; |
1080 | 257 | return -1; |
1081 | 257 | } |
1082 | | |
1083 | 801 | cm->tiles.large_scale = 1; |
1084 | 801 | av1_set_single_tile_decoding_mode(cm); |
1085 | 801 | decoded_payload_size = |
1086 | 801 | read_and_decode_one_tile_list(pbi, &rb, data, data + payload_size, |
1087 | 801 | p_data_end, &frame_decoding_finished); |
1088 | 801 | if (pbi->error.error_code != AOM_CODEC_OK) return -1; |
1089 | 530 | break; |
1090 | 3.39k | case OBU_PADDING: |
1091 | 3.39k | decoded_payload_size = read_padding(cm, data, payload_size); |
1092 | 3.39k | if (pbi->error.error_code != AOM_CODEC_OK) return -1; |
1093 | 3.35k | break; |
1094 | 3.35k | default: |
1095 | | // Skip unrecognized OBUs |
1096 | 2.17k | if (payload_size > 0 && |
1097 | 2.17k | get_last_nonzero_byte(data, payload_size) == 0) { |
1098 | 20 | pbi->error.error_code = AOM_CODEC_CORRUPT_FRAME; |
1099 | 20 | return -1; |
1100 | 20 | } |
1101 | 2.15k | decoded_payload_size = payload_size; |
1102 | 2.15k | break; |
1103 | 498k | } |
1104 | | |
1105 | | // Check that the signalled OBU size matches the actual amount of data read |
1106 | 332k | if (decoded_payload_size > payload_size) { |
1107 | 0 | pbi->error.error_code = AOM_CODEC_CORRUPT_FRAME; |
1108 | 0 | return -1; |
1109 | 0 | } |
1110 | | |
1111 | | // If there are extra padding bytes, they should all be zero |
1112 | 354k | while (decoded_payload_size < payload_size) { |
1113 | 23.9k | uint8_t padding_byte = data[decoded_payload_size++]; |
1114 | 23.9k | if (padding_byte != 0) { |
1115 | 2.10k | pbi->error.error_code = AOM_CODEC_CORRUPT_FRAME; |
1116 | 2.10k | return -1; |
1117 | 2.10k | } |
1118 | 23.9k | } |
1119 | | |
1120 | 330k | data += payload_size; |
1121 | 330k | } |
1122 | | |
1123 | 106k | if (pbi->error.error_code != AOM_CODEC_OK) return -1; |
1124 | 105k | return frame_decoding_finished; |
1125 | 106k | } |