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