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