/src/mozilla-central/third_party/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 | | |
16 | | #include "aom/aom_codec.h" |
17 | | #include "aom_dsp/bitreader_buffer.h" |
18 | | #include "aom_ports/mem_ops.h" |
19 | | |
20 | | #include "av1/common/common.h" |
21 | | #include "av1/common/obu_util.h" |
22 | | #include "av1/common/timing.h" |
23 | | #include "av1/decoder/decoder.h" |
24 | | #include "av1/decoder/decodeframe.h" |
25 | | #include "av1/decoder/obu.h" |
26 | | |
27 | | // Picture prediction structures (0-12 are predefined) in scalability metadata. |
28 | | typedef enum { |
29 | | SCALABILITY_L1T2 = 0, |
30 | | SCALABILITY_L1T3 = 1, |
31 | | SCALABILITY_L2T1 = 2, |
32 | | SCALABILITY_L2T2 = 3, |
33 | | SCALABILITY_L2T3 = 4, |
34 | | SCALABILITY_S2T1 = 5, |
35 | | SCALABILITY_S2T2 = 6, |
36 | | SCALABILITY_S2T3 = 7, |
37 | | SCALABILITY_L2T1h = 8, |
38 | | SCALABILITY_L2T2h = 9, |
39 | | SCALABILITY_L2T3h = 10, |
40 | | SCALABILITY_S2T1h = 11, |
41 | | SCALABILITY_S2T2h = 12, |
42 | | SCALABILITY_S2T3h = 13, |
43 | | SCALABILITY_SS = 14 |
44 | | } SCALABILITY_STRUCTURES; |
45 | | |
46 | | aom_codec_err_t aom_get_num_layers_from_operating_point_idc( |
47 | | int operating_point_idc, unsigned int *number_spatial_layers, |
48 | 0 | unsigned int *number_temporal_layers) { |
49 | 0 | // derive number of spatial/temporal layers from operating_point_idc |
50 | 0 |
|
51 | 0 | if (!number_spatial_layers || !number_temporal_layers) |
52 | 0 | return AOM_CODEC_INVALID_PARAM; |
53 | 0 | |
54 | 0 | if (operating_point_idc == 0) { |
55 | 0 | *number_temporal_layers = 1; |
56 | 0 | *number_spatial_layers = 1; |
57 | 0 | } else { |
58 | 0 | *number_spatial_layers = 0; |
59 | 0 | *number_temporal_layers = 0; |
60 | 0 | for (int j = 0; j < MAX_NUM_SPATIAL_LAYERS; j++) { |
61 | 0 | *number_spatial_layers += |
62 | 0 | (operating_point_idc >> (j + MAX_NUM_TEMPORAL_LAYERS)) & 0x1; |
63 | 0 | } |
64 | 0 | for (int j = 0; j < MAX_NUM_TEMPORAL_LAYERS; j++) { |
65 | 0 | *number_temporal_layers += (operating_point_idc >> j) & 0x1; |
66 | 0 | } |
67 | 0 | } |
68 | 0 |
|
69 | 0 | return AOM_CODEC_OK; |
70 | 0 | } |
71 | | |
72 | | static int is_obu_in_current_operating_point(AV1Decoder *pbi, |
73 | 0 | ObuHeader obu_header) { |
74 | 0 | if (!pbi->current_operating_point) { |
75 | 0 | return 1; |
76 | 0 | } |
77 | 0 | |
78 | 0 | if ((pbi->current_operating_point >> obu_header.temporal_layer_id) & 0x1 && |
79 | 0 | (pbi->current_operating_point >> (obu_header.spatial_layer_id + 8)) & |
80 | 0 | 0x1) { |
81 | 0 | return 1; |
82 | 0 | } |
83 | 0 | return 0; |
84 | 0 | } |
85 | | |
86 | | static int byte_alignment(AV1_COMMON *const cm, |
87 | 0 | struct aom_read_bit_buffer *const rb) { |
88 | 0 | while (rb->bit_offset & 7) { |
89 | 0 | if (aom_rb_read_bit(rb)) { |
90 | 0 | cm->error.error_code = AOM_CODEC_CORRUPT_FRAME; |
91 | 0 | return -1; |
92 | 0 | } |
93 | 0 | } |
94 | 0 | return 0; |
95 | 0 | } |
96 | | |
97 | 0 | static uint32_t read_temporal_delimiter_obu() { return 0; } |
98 | | |
99 | | // Returns a boolean that indicates success. |
100 | | static int read_bitstream_level(BitstreamLevel *bl, |
101 | 0 | struct aom_read_bit_buffer *rb) { |
102 | 0 | const uint8_t seq_level_idx = aom_rb_read_literal(rb, LEVEL_BITS); |
103 | 0 | if (!is_valid_seq_level_idx(seq_level_idx)) return 0; |
104 | 0 | bl->major = (seq_level_idx >> LEVEL_MINOR_BITS) + LEVEL_MAJOR_MIN; |
105 | 0 | bl->minor = seq_level_idx & ((1 << LEVEL_MINOR_BITS) - 1); |
106 | 0 | return 1; |
107 | 0 | } |
108 | | |
109 | | // Returns whether two sequence headers are consistent with each other. |
110 | | // TODO(huisu,wtc@google.com): make sure the code matches the spec exactly. |
111 | | static int are_seq_headers_consistent(const SequenceHeader *seq_params_old, |
112 | 0 | const SequenceHeader *seq_params_new) { |
113 | 0 | return !memcmp(seq_params_old, seq_params_new, sizeof(SequenceHeader)); |
114 | 0 | } |
115 | | |
116 | | // On success, sets pbi->sequence_header_ready to 1 and returns the number of |
117 | | // bytes read from 'rb'. |
118 | | // On failure, sets pbi->common.error.error_code and returns 0. |
119 | | static uint32_t read_sequence_header_obu(AV1Decoder *pbi, |
120 | 0 | struct aom_read_bit_buffer *rb) { |
121 | 0 | AV1_COMMON *const cm = &pbi->common; |
122 | 0 | const uint32_t saved_bit_offset = rb->bit_offset; |
123 | 0 |
|
124 | 0 | // Verify rb has been configured to report errors. |
125 | 0 | assert(rb->error_handler); |
126 | 0 |
|
127 | 0 | // Use a local variable to store the information as we decode. At the end, |
128 | 0 | // if no errors have occurred, cm->seq_params is updated. |
129 | 0 | SequenceHeader sh = cm->seq_params; |
130 | 0 | SequenceHeader *const seq_params = &sh; |
131 | 0 |
|
132 | 0 | seq_params->profile = av1_read_profile(rb); |
133 | 0 | if (seq_params->profile > CONFIG_MAX_DECODE_PROFILE) { |
134 | 0 | cm->error.error_code = AOM_CODEC_UNSUP_BITSTREAM; |
135 | 0 | return 0; |
136 | 0 | } |
137 | 0 | |
138 | 0 | // Still picture or not |
139 | 0 | seq_params->still_picture = aom_rb_read_bit(rb); |
140 | 0 | seq_params->reduced_still_picture_hdr = aom_rb_read_bit(rb); |
141 | 0 | // Video must have reduced_still_picture_hdr = 0 |
142 | 0 | if (!seq_params->still_picture && seq_params->reduced_still_picture_hdr) { |
143 | 0 | cm->error.error_code = AOM_CODEC_UNSUP_BITSTREAM; |
144 | 0 | return 0; |
145 | 0 | } |
146 | 0 | |
147 | 0 | if (seq_params->reduced_still_picture_hdr) { |
148 | 0 | cm->timing_info_present = 0; |
149 | 0 | seq_params->decoder_model_info_present_flag = 0; |
150 | 0 | seq_params->display_model_info_present_flag = 0; |
151 | 0 | seq_params->operating_points_cnt_minus_1 = 0; |
152 | 0 | seq_params->operating_point_idc[0] = 0; |
153 | 0 | if (!read_bitstream_level(&seq_params->level[0], rb)) { |
154 | 0 | cm->error.error_code = AOM_CODEC_UNSUP_BITSTREAM; |
155 | 0 | return 0; |
156 | 0 | } |
157 | 0 | seq_params->tier[0] = 0; |
158 | 0 | cm->op_params[0].decoder_model_param_present_flag = 0; |
159 | 0 | cm->op_params[0].display_model_param_present_flag = 0; |
160 | 0 | } else { |
161 | 0 | cm->timing_info_present = aom_rb_read_bit(rb); // timing_info_present_flag |
162 | 0 | if (cm->timing_info_present) { |
163 | 0 | av1_read_timing_info_header(cm, rb); |
164 | 0 |
|
165 | 0 | seq_params->decoder_model_info_present_flag = aom_rb_read_bit(rb); |
166 | 0 | if (seq_params->decoder_model_info_present_flag) |
167 | 0 | av1_read_decoder_model_info(cm, rb); |
168 | 0 | } else { |
169 | 0 | seq_params->decoder_model_info_present_flag = 0; |
170 | 0 | } |
171 | 0 | seq_params->display_model_info_present_flag = aom_rb_read_bit(rb); |
172 | 0 | seq_params->operating_points_cnt_minus_1 = |
173 | 0 | aom_rb_read_literal(rb, OP_POINTS_CNT_MINUS_1_BITS); |
174 | 0 | for (int i = 0; i < seq_params->operating_points_cnt_minus_1 + 1; i++) { |
175 | 0 | seq_params->operating_point_idc[i] = |
176 | 0 | aom_rb_read_literal(rb, OP_POINTS_IDC_BITS); |
177 | 0 | if (!read_bitstream_level(&seq_params->level[i], rb)) { |
178 | 0 | cm->error.error_code = AOM_CODEC_UNSUP_BITSTREAM; |
179 | 0 | return 0; |
180 | 0 | } |
181 | 0 | // This is the seq_level_idx[i] > 7 check in the spec. seq_level_idx 7 |
182 | 0 | // is equivalent to level 3.3. |
183 | 0 | if (seq_params->level[i].major > 3) |
184 | 0 | seq_params->tier[i] = aom_rb_read_bit(rb); |
185 | 0 | else |
186 | 0 | seq_params->tier[i] = 0; |
187 | 0 | if (seq_params->decoder_model_info_present_flag) { |
188 | 0 | cm->op_params[i].decoder_model_param_present_flag = aom_rb_read_bit(rb); |
189 | 0 | if (cm->op_params[i].decoder_model_param_present_flag) |
190 | 0 | av1_read_op_parameters_info(cm, rb, i); |
191 | 0 | } else { |
192 | 0 | cm->op_params[i].decoder_model_param_present_flag = 0; |
193 | 0 | } |
194 | 0 | if (cm->timing_info_present && |
195 | 0 | (cm->timing_info.equal_picture_interval || |
196 | 0 | cm->op_params[i].decoder_model_param_present_flag)) { |
197 | 0 | cm->op_params[i].bitrate = max_level_bitrate( |
198 | 0 | seq_params->profile, |
199 | 0 | major_minor_to_seq_level_idx(seq_params->level[i]), |
200 | 0 | seq_params->tier[i]); |
201 | 0 | // Level with seq_level_idx = 31 returns a high "dummy" bitrate to pass |
202 | 0 | // the check |
203 | 0 | if (cm->op_params[i].bitrate == 0) |
204 | 0 | aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM, |
205 | 0 | "AV1 does not support this combination of " |
206 | 0 | "profile, level, and tier."); |
207 | 0 | // Buffer size in bits/s is bitrate in bits/s * 1 s |
208 | 0 | cm->op_params[i].buffer_size = cm->op_params[i].bitrate; |
209 | 0 | } |
210 | 0 | if (cm->timing_info_present && cm->timing_info.equal_picture_interval && |
211 | 0 | !cm->op_params[i].decoder_model_param_present_flag) { |
212 | 0 | // When the decoder_model_parameters are not sent for this op, set |
213 | 0 | // the default ones that can be used with the resource availability mode |
214 | 0 | cm->op_params[i].decoder_buffer_delay = 70000; |
215 | 0 | cm->op_params[i].encoder_buffer_delay = 20000; |
216 | 0 | cm->op_params[i].low_delay_mode_flag = 0; |
217 | 0 | } |
218 | 0 |
|
219 | 0 | if (seq_params->display_model_info_present_flag) { |
220 | 0 | cm->op_params[i].display_model_param_present_flag = aom_rb_read_bit(rb); |
221 | 0 | if (cm->op_params[i].display_model_param_present_flag) { |
222 | 0 | cm->op_params[i].initial_display_delay = |
223 | 0 | aom_rb_read_literal(rb, 4) + 1; |
224 | 0 | if (cm->op_params[i].initial_display_delay > 10) |
225 | 0 | aom_internal_error( |
226 | 0 | &cm->error, AOM_CODEC_UNSUP_BITSTREAM, |
227 | 0 | "AV1 does not support more than 10 decoded frames delay"); |
228 | 0 | } else { |
229 | 0 | cm->op_params[i].initial_display_delay = 10; |
230 | 0 | } |
231 | 0 | } else { |
232 | 0 | cm->op_params[i].display_model_param_present_flag = 0; |
233 | 0 | cm->op_params[i].initial_display_delay = 10; |
234 | 0 | } |
235 | 0 | } |
236 | 0 | } |
237 | 0 | // This decoder supports all levels. Choose operating point provided by |
238 | 0 | // external means |
239 | 0 | int operating_point = pbi->operating_point; |
240 | 0 | if (operating_point < 0 || |
241 | 0 | operating_point > seq_params->operating_points_cnt_minus_1) |
242 | 0 | operating_point = 0; |
243 | 0 | pbi->current_operating_point = |
244 | 0 | seq_params->operating_point_idc[operating_point]; |
245 | 0 | if (aom_get_num_layers_from_operating_point_idc( |
246 | 0 | pbi->current_operating_point, &cm->number_spatial_layers, |
247 | 0 | &cm->number_temporal_layers) != AOM_CODEC_OK) { |
248 | 0 | cm->error.error_code = AOM_CODEC_ERROR; |
249 | 0 | return 0; |
250 | 0 | } |
251 | 0 | |
252 | 0 | av1_read_sequence_header(cm, rb, seq_params); |
253 | 0 |
|
254 | 0 | av1_read_color_config(rb, pbi->allow_lowbitdepth, seq_params, &cm->error); |
255 | 0 | if (!(seq_params->subsampling_x == 0 && seq_params->subsampling_y == 0) && |
256 | 0 | !(seq_params->subsampling_x == 1 && seq_params->subsampling_y == 1) && |
257 | 0 | !(seq_params->subsampling_x == 1 && seq_params->subsampling_y == 0)) { |
258 | 0 | aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM, |
259 | 0 | "Only 4:4:4, 4:2:2 and 4:2:0 are currently supported, " |
260 | 0 | "%d %d subsampling is not supported.\n", |
261 | 0 | seq_params->subsampling_x, seq_params->subsampling_y); |
262 | 0 | } |
263 | 0 |
|
264 | 0 | seq_params->film_grain_params_present = aom_rb_read_bit(rb); |
265 | 0 |
|
266 | 0 | if (av1_check_trailing_bits(pbi, rb) != 0) { |
267 | 0 | // cm->error.error_code is already set. |
268 | 0 | return 0; |
269 | 0 | } |
270 | 0 | |
271 | 0 | // If a sequence header has been decoded before, we check if the new |
272 | 0 | // one is consistent with the old one. |
273 | 0 | if (pbi->sequence_header_ready) { |
274 | 0 | if (!are_seq_headers_consistent(&cm->seq_params, seq_params)) |
275 | 0 | pbi->sequence_header_changed = 1; |
276 | 0 | } |
277 | 0 |
|
278 | 0 | cm->seq_params = *seq_params; |
279 | 0 | pbi->sequence_header_ready = 1; |
280 | 0 |
|
281 | 0 | return ((rb->bit_offset - saved_bit_offset + 7) >> 3); |
282 | 0 | } |
283 | | |
284 | | // On success, returns the frame header size. On failure, calls |
285 | | // aom_internal_error and does not return. |
286 | | static uint32_t read_frame_header_obu(AV1Decoder *pbi, |
287 | | struct aom_read_bit_buffer *rb, |
288 | | const uint8_t *data, |
289 | | const uint8_t **p_data_end, |
290 | 0 | int trailing_bits_present) { |
291 | 0 | return av1_decode_frame_headers_and_setup(pbi, rb, data, p_data_end, |
292 | 0 | trailing_bits_present); |
293 | 0 | } |
294 | | |
295 | | static int32_t read_tile_group_header(AV1Decoder *pbi, |
296 | | struct aom_read_bit_buffer *rb, |
297 | | int *start_tile, int *end_tile, |
298 | 0 | int tile_start_implicit) { |
299 | 0 | AV1_COMMON *const cm = &pbi->common; |
300 | 0 | uint32_t saved_bit_offset = rb->bit_offset; |
301 | 0 | int tile_start_and_end_present_flag = 0; |
302 | 0 | const int num_tiles = pbi->common.tile_rows * pbi->common.tile_cols; |
303 | 0 |
|
304 | 0 | if (!pbi->common.large_scale_tile && num_tiles > 1) { |
305 | 0 | tile_start_and_end_present_flag = aom_rb_read_bit(rb); |
306 | 0 | } |
307 | 0 | if (pbi->common.large_scale_tile || num_tiles == 1 || |
308 | 0 | !tile_start_and_end_present_flag) { |
309 | 0 | *start_tile = 0; |
310 | 0 | *end_tile = num_tiles - 1; |
311 | 0 | return ((rb->bit_offset - saved_bit_offset + 7) >> 3); |
312 | 0 | } |
313 | 0 | if (tile_start_implicit && tile_start_and_end_present_flag) { |
314 | 0 | aom_internal_error( |
315 | 0 | &cm->error, AOM_CODEC_UNSUP_BITSTREAM, |
316 | 0 | "For OBU_FRAME type obu tile_start_and_end_present_flag must be 0"); |
317 | 0 | return -1; |
318 | 0 | } |
319 | 0 | *start_tile = |
320 | 0 | aom_rb_read_literal(rb, cm->log2_tile_rows + cm->log2_tile_cols); |
321 | 0 | *end_tile = aom_rb_read_literal(rb, cm->log2_tile_rows + cm->log2_tile_cols); |
322 | 0 |
|
323 | 0 | return ((rb->bit_offset - saved_bit_offset + 7) >> 3); |
324 | 0 | } |
325 | | |
326 | | static uint32_t read_one_tile_group_obu( |
327 | | AV1Decoder *pbi, struct aom_read_bit_buffer *rb, int is_first_tg, |
328 | | const uint8_t *data, const uint8_t *data_end, const uint8_t **p_data_end, |
329 | 0 | int *is_last_tg, int tile_start_implicit) { |
330 | 0 | AV1_COMMON *const cm = &pbi->common; |
331 | 0 | int start_tile, end_tile; |
332 | 0 | int32_t header_size, tg_payload_size; |
333 | 0 |
|
334 | 0 | assert((rb->bit_offset & 7) == 0); |
335 | 0 | assert(rb->bit_buffer + aom_rb_bytes_read(rb) == data); |
336 | 0 |
|
337 | 0 | header_size = read_tile_group_header(pbi, rb, &start_tile, &end_tile, |
338 | 0 | tile_start_implicit); |
339 | 0 | if (header_size == -1 || byte_alignment(cm, rb)) return 0; |
340 | 0 | if (start_tile > end_tile) return header_size; |
341 | 0 | data += header_size; |
342 | 0 | av1_decode_tg_tiles_and_wrapup(pbi, data, data_end, p_data_end, start_tile, |
343 | 0 | end_tile, is_first_tg); |
344 | 0 |
|
345 | 0 | tg_payload_size = (uint32_t)(*p_data_end - data); |
346 | 0 |
|
347 | 0 | // TODO(shan): For now, assume all tile groups received in order |
348 | 0 | *is_last_tg = end_tile == cm->tile_rows * cm->tile_cols - 1; |
349 | 0 | return header_size + tg_payload_size; |
350 | 0 | } |
351 | | |
352 | 0 | static void alloc_tile_list_buffer(AV1Decoder *pbi) { |
353 | 0 | // TODO(yunqing): for now, copy each tile's decoded YUV data directly to the |
354 | 0 | // output buffer. This needs to be modified according to the application |
355 | 0 | // requirement. |
356 | 0 | AV1_COMMON *const cm = &pbi->common; |
357 | 0 | const int tile_width_in_pixels = cm->tile_width * MI_SIZE; |
358 | 0 | const int tile_height_in_pixels = cm->tile_height * MI_SIZE; |
359 | 0 | const int ssy = cm->seq_params.subsampling_y; |
360 | 0 | const int ssx = cm->seq_params.subsampling_x; |
361 | 0 | const int num_planes = av1_num_planes(cm); |
362 | 0 | const size_t yplane_tile_size = tile_height_in_pixels * tile_width_in_pixels; |
363 | 0 | const size_t uvplane_tile_size = |
364 | 0 | (num_planes > 1) |
365 | 0 | ? (tile_height_in_pixels >> ssy) * (tile_width_in_pixels >> ssx) |
366 | 0 | : 0; |
367 | 0 | const size_t tile_size = (cm->seq_params.use_highbitdepth ? 2 : 1) * |
368 | 0 | (yplane_tile_size + 2 * uvplane_tile_size); |
369 | 0 | pbi->tile_list_size = tile_size * (pbi->tile_count_minus_1 + 1); |
370 | 0 |
|
371 | 0 | if (pbi->tile_list_size > pbi->buffer_sz) { |
372 | 0 | if (pbi->tile_list_output != NULL) aom_free(pbi->tile_list_output); |
373 | 0 | pbi->tile_list_output = NULL; |
374 | 0 |
|
375 | 0 | pbi->tile_list_output = (uint8_t *)aom_memalign(32, pbi->tile_list_size); |
376 | 0 | if (pbi->tile_list_output == NULL) |
377 | 0 | aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR, |
378 | 0 | "Failed to allocate the tile list output buffer"); |
379 | 0 | pbi->buffer_sz = pbi->tile_list_size; |
380 | 0 | } |
381 | 0 | } |
382 | | |
383 | | static void copy_decoded_tile_to_tile_list_buffer(AV1Decoder *pbi, |
384 | 0 | uint8_t **output) { |
385 | 0 | AV1_COMMON *const cm = &pbi->common; |
386 | 0 | const int tile_width_in_pixels = cm->tile_width * MI_SIZE; |
387 | 0 | const int tile_height_in_pixels = cm->tile_height * MI_SIZE; |
388 | 0 | const int ssy = cm->seq_params.subsampling_y; |
389 | 0 | const int ssx = cm->seq_params.subsampling_x; |
390 | 0 | const int num_planes = av1_num_planes(cm); |
391 | 0 |
|
392 | 0 | // Copy decoded tile to the tile list output buffer. |
393 | 0 | YV12_BUFFER_CONFIG *cur_frame = get_frame_new_buffer(cm); |
394 | 0 | const int mi_row = pbi->dec_tile_row * cm->tile_height; |
395 | 0 | const int mi_col = pbi->dec_tile_col * cm->tile_width; |
396 | 0 | const int is_hbd = (cur_frame->flags & YV12_FLAG_HIGHBITDEPTH) ? 1 : 0; |
397 | 0 | uint8_t *bufs[MAX_MB_PLANE] = { NULL, NULL, NULL }; |
398 | 0 | int strides[MAX_MB_PLANE] = { 0, 0, 0 }; |
399 | 0 | int plane; |
400 | 0 |
|
401 | 0 | for (plane = 0; plane < num_planes; ++plane) { |
402 | 0 | int shift_x = plane > 0 ? ssx : 0; |
403 | 0 | int shift_y = plane > 0 ? ssy : 0; |
404 | 0 |
|
405 | 0 | bufs[plane] = cur_frame->buffers[plane]; |
406 | 0 | strides[plane] = |
407 | 0 | (plane > 0) ? cur_frame->strides[1] : cur_frame->strides[0]; |
408 | 0 |
|
409 | 0 | bufs[plane] += mi_row * (MI_SIZE >> shift_y) * strides[plane] + |
410 | 0 | mi_col * (MI_SIZE >> shift_x); |
411 | 0 |
|
412 | 0 | if (is_hbd) { |
413 | 0 | bufs[plane] = (uint8_t *)CONVERT_TO_SHORTPTR(bufs[plane]); |
414 | 0 | strides[plane] *= 2; |
415 | 0 | } |
416 | 0 |
|
417 | 0 | int w, h; |
418 | 0 | w = (plane > 0 && shift_x > 0) ? ((tile_width_in_pixels + 1) >> shift_x) |
419 | 0 | : tile_width_in_pixels; |
420 | 0 | w *= (1 + is_hbd); |
421 | 0 | h = (plane > 0 && shift_y > 0) ? ((tile_height_in_pixels + 1) >> shift_y) |
422 | 0 | : tile_height_in_pixels; |
423 | 0 | int j; |
424 | 0 |
|
425 | 0 | for (j = 0; j < h; ++j) { |
426 | 0 | memcpy(*output, bufs[plane], w); |
427 | 0 | bufs[plane] += strides[plane]; |
428 | 0 | *output += w; |
429 | 0 | } |
430 | 0 | } |
431 | 0 | } |
432 | | |
433 | | // Only called while large_scale_tile = 1. |
434 | | static uint32_t read_and_decode_one_tile_list(AV1Decoder *pbi, |
435 | | struct aom_read_bit_buffer *rb, |
436 | | const uint8_t *data, |
437 | | const uint8_t *data_end, |
438 | | const uint8_t **p_data_end, |
439 | 0 | int *frame_decoding_finished) { |
440 | 0 | AV1_COMMON *const cm = &pbi->common; |
441 | 0 | uint32_t tile_list_payload_size = 0; |
442 | 0 | const int num_tiles = cm->tile_cols * cm->tile_rows; |
443 | 0 | const int start_tile = 0; |
444 | 0 | const int end_tile = num_tiles - 1; |
445 | 0 | int i = 0; |
446 | 0 |
|
447 | 0 | // Process the tile list info. |
448 | 0 | pbi->output_frame_width_in_tiles_minus_1 = aom_rb_read_literal(rb, 8); |
449 | 0 | pbi->output_frame_height_in_tiles_minus_1 = aom_rb_read_literal(rb, 8); |
450 | 0 | pbi->tile_count_minus_1 = aom_rb_read_literal(rb, 16); |
451 | 0 | if (pbi->tile_count_minus_1 > MAX_TILES - 1) { |
452 | 0 | cm->error.error_code = AOM_CODEC_CORRUPT_FRAME; |
453 | 0 | return 0; |
454 | 0 | } |
455 | 0 | |
456 | 0 | // Allocate output frame buffer for the tile list. |
457 | 0 | alloc_tile_list_buffer(pbi); |
458 | 0 |
|
459 | 0 | uint32_t tile_list_info_bytes = 4; |
460 | 0 | tile_list_payload_size += tile_list_info_bytes; |
461 | 0 | data += tile_list_info_bytes; |
462 | 0 | uint8_t *output = pbi->tile_list_output; |
463 | 0 |
|
464 | 0 | for (i = 0; i <= pbi->tile_count_minus_1; i++) { |
465 | 0 | // Process 1 tile. |
466 | 0 | // Reset the bit reader. |
467 | 0 | rb->bit_offset = 0; |
468 | 0 | rb->bit_buffer = data; |
469 | 0 |
|
470 | 0 | // Read out the tile info. |
471 | 0 | uint32_t tile_info_bytes = 5; |
472 | 0 | // Set reference for each tile. |
473 | 0 | int ref_idx = aom_rb_read_literal(rb, 8); |
474 | 0 | if (ref_idx >= MAX_EXTERNAL_REFERENCES) { |
475 | 0 | cm->error.error_code = AOM_CODEC_CORRUPT_FRAME; |
476 | 0 | return 0; |
477 | 0 | } |
478 | 0 | av1_set_reference_dec(cm, 0, 1, &pbi->ext_refs.refs[ref_idx]); |
479 | 0 |
|
480 | 0 | pbi->dec_tile_row = aom_rb_read_literal(rb, 8); |
481 | 0 | pbi->dec_tile_col = aom_rb_read_literal(rb, 8); |
482 | 0 | if (pbi->dec_tile_row < 0 || pbi->dec_tile_col < 0 || |
483 | 0 | pbi->dec_tile_row >= cm->tile_rows || |
484 | 0 | pbi->dec_tile_col >= cm->tile_cols) { |
485 | 0 | cm->error.error_code = AOM_CODEC_CORRUPT_FRAME; |
486 | 0 | return 0; |
487 | 0 | } |
488 | 0 | |
489 | 0 | pbi->coded_tile_data_size = aom_rb_read_literal(rb, 16) + 1; |
490 | 0 | data += tile_info_bytes; |
491 | 0 | if ((size_t)(data_end - data) < pbi->coded_tile_data_size) { |
492 | 0 | cm->error.error_code = AOM_CODEC_CORRUPT_FRAME; |
493 | 0 | return 0; |
494 | 0 | } |
495 | 0 | |
496 | 0 | av1_decode_tg_tiles_and_wrapup(pbi, data, data + pbi->coded_tile_data_size, |
497 | 0 | p_data_end, start_tile, end_tile, 0); |
498 | 0 | uint32_t tile_payload_size = (uint32_t)(*p_data_end - data); |
499 | 0 |
|
500 | 0 | tile_list_payload_size += tile_info_bytes + tile_payload_size; |
501 | 0 |
|
502 | 0 | // Update data ptr for next tile decoding. |
503 | 0 | data = *p_data_end; |
504 | 0 | assert(data <= data_end); |
505 | 0 |
|
506 | 0 | // Copy the decoded tile to the tile list output buffer. |
507 | 0 | copy_decoded_tile_to_tile_list_buffer(pbi, &output); |
508 | 0 | } |
509 | 0 |
|
510 | 0 | *frame_decoding_finished = 1; |
511 | 0 | return tile_list_payload_size; |
512 | 0 | } |
513 | | |
514 | 0 | static void read_metadata_itut_t35(const uint8_t *data, size_t sz) { |
515 | 0 | struct aom_read_bit_buffer rb = { data, data + sz, 0, NULL, NULL }; |
516 | 0 | for (size_t i = 0; i < sz; i++) { |
517 | 0 | aom_rb_read_literal(&rb, 8); |
518 | 0 | } |
519 | 0 | } |
520 | | |
521 | 0 | static void read_metadata_hdr_cll(const uint8_t *data, size_t sz) { |
522 | 0 | struct aom_read_bit_buffer rb = { data, data + sz, 0, NULL, NULL }; |
523 | 0 | aom_rb_read_literal(&rb, 16); // max_cll |
524 | 0 | aom_rb_read_literal(&rb, 16); // max_fall |
525 | 0 | } |
526 | | |
527 | 0 | static void read_metadata_hdr_mdcv(const uint8_t *data, size_t sz) { |
528 | 0 | struct aom_read_bit_buffer rb = { data, data + sz, 0, NULL, NULL }; |
529 | 0 | for (int i = 0; i < 3; i++) { |
530 | 0 | aom_rb_read_literal(&rb, 16); // primary_i_chromaticity_x |
531 | 0 | aom_rb_read_literal(&rb, 16); // primary_i_chromaticity_y |
532 | 0 | } |
533 | 0 |
|
534 | 0 | aom_rb_read_literal(&rb, 16); // white_point_chromaticity_x |
535 | 0 | aom_rb_read_literal(&rb, 16); // white_point_chromaticity_y |
536 | 0 |
|
537 | 0 | aom_rb_read_unsigned_literal(&rb, 32); // luminance_max |
538 | 0 | aom_rb_read_unsigned_literal(&rb, 32); // luminance_min |
539 | 0 | } |
540 | | |
541 | 0 | static void scalability_structure(struct aom_read_bit_buffer *rb) { |
542 | 0 | int spatial_layers_cnt = aom_rb_read_literal(rb, 2); |
543 | 0 | int spatial_layer_dimensions_present_flag = aom_rb_read_bit(rb); |
544 | 0 | int spatial_layer_description_present_flag = aom_rb_read_bit(rb); |
545 | 0 | int temporal_group_description_present_flag = aom_rb_read_bit(rb); |
546 | 0 | aom_rb_read_literal(rb, 3); // reserved |
547 | 0 |
|
548 | 0 | if (spatial_layer_dimensions_present_flag) { |
549 | 0 | int i; |
550 | 0 | for (i = 0; i < spatial_layers_cnt + 1; i++) { |
551 | 0 | aom_rb_read_literal(rb, 16); |
552 | 0 | aom_rb_read_literal(rb, 16); |
553 | 0 | } |
554 | 0 | } |
555 | 0 | if (spatial_layer_description_present_flag) { |
556 | 0 | int i; |
557 | 0 | for (i = 0; i < spatial_layers_cnt + 1; i++) { |
558 | 0 | aom_rb_read_literal(rb, 8); |
559 | 0 | } |
560 | 0 | } |
561 | 0 | if (temporal_group_description_present_flag) { |
562 | 0 | int i, j, temporal_group_size; |
563 | 0 | temporal_group_size = aom_rb_read_literal(rb, 8); |
564 | 0 | for (i = 0; i < temporal_group_size; i++) { |
565 | 0 | aom_rb_read_literal(rb, 3); |
566 | 0 | aom_rb_read_bit(rb); |
567 | 0 | aom_rb_read_bit(rb); |
568 | 0 | int temporal_group_ref_cnt = aom_rb_read_literal(rb, 3); |
569 | 0 | for (j = 0; j < temporal_group_ref_cnt; j++) { |
570 | 0 | aom_rb_read_literal(rb, 8); |
571 | 0 | } |
572 | 0 | } |
573 | 0 | } |
574 | 0 | } |
575 | | |
576 | 0 | static void read_metadata_scalability(const uint8_t *data, size_t sz) { |
577 | 0 | struct aom_read_bit_buffer rb = { data, data + sz, 0, NULL, NULL }; |
578 | 0 | int scalability_mode_idc = aom_rb_read_literal(&rb, 8); |
579 | 0 | if (scalability_mode_idc == SCALABILITY_SS) { |
580 | 0 | scalability_structure(&rb); |
581 | 0 | } |
582 | 0 | } |
583 | | |
584 | 0 | static void read_metadata_timecode(const uint8_t *data, size_t sz) { |
585 | 0 | struct aom_read_bit_buffer rb = { data, data + sz, 0, NULL, NULL }; |
586 | 0 | aom_rb_read_literal(&rb, 5); // counting_type f(5) |
587 | 0 | int full_timestamp_flag = aom_rb_read_bit(&rb); // full_timestamp_flag f(1) |
588 | 0 | aom_rb_read_bit(&rb); // discontinuity_flag (f1) |
589 | 0 | aom_rb_read_bit(&rb); // cnt_dropped_flag f(1) |
590 | 0 | aom_rb_read_literal(&rb, 9); // n_frames f(9) |
591 | 0 | if (full_timestamp_flag) { |
592 | 0 | aom_rb_read_literal(&rb, 6); // seconds_value f(6) |
593 | 0 | aom_rb_read_literal(&rb, 6); // minutes_value f(6) |
594 | 0 | aom_rb_read_literal(&rb, 5); // hours_value f(5) |
595 | 0 | } else { |
596 | 0 | int seconds_flag = aom_rb_read_bit(&rb); // seconds_flag f(1) |
597 | 0 | if (seconds_flag) { |
598 | 0 | aom_rb_read_literal(&rb, 6); // seconds_value f(6) |
599 | 0 | int minutes_flag = aom_rb_read_bit(&rb); // minutes_flag f(1) |
600 | 0 | if (minutes_flag) { |
601 | 0 | aom_rb_read_literal(&rb, 6); // minutes_value f(6) |
602 | 0 | int hours_flag = aom_rb_read_bit(&rb); // hours_flag f(1) |
603 | 0 | if (hours_flag) { |
604 | 0 | aom_rb_read_literal(&rb, 5); // hours_value f(5) |
605 | 0 | } |
606 | 0 | } |
607 | 0 | } |
608 | 0 | } |
609 | 0 | // time_offset_length f(5) |
610 | 0 | int time_offset_length = aom_rb_read_literal(&rb, 5); |
611 | 0 | if (time_offset_length) { |
612 | 0 | aom_rb_read_literal(&rb, time_offset_length); // f(time_offset_length) |
613 | 0 | } |
614 | 0 | } |
615 | | |
616 | 0 | static size_t read_metadata(const uint8_t *data, size_t sz) { |
617 | 0 | size_t type_length; |
618 | 0 | uint64_t type_value; |
619 | 0 | OBU_METADATA_TYPE metadata_type; |
620 | 0 | if (aom_uleb_decode(data, sz, &type_value, &type_length) < 0) { |
621 | 0 | return sz; |
622 | 0 | } |
623 | 0 | metadata_type = (OBU_METADATA_TYPE)type_value; |
624 | 0 | if (metadata_type == OBU_METADATA_TYPE_ITUT_T35) { |
625 | 0 | read_metadata_itut_t35(data + type_length, sz - type_length); |
626 | 0 | } else if (metadata_type == OBU_METADATA_TYPE_HDR_CLL) { |
627 | 0 | read_metadata_hdr_cll(data + type_length, sz - type_length); |
628 | 0 | } else if (metadata_type == OBU_METADATA_TYPE_HDR_MDCV) { |
629 | 0 | read_metadata_hdr_mdcv(data + type_length, sz - type_length); |
630 | 0 | } else if (metadata_type == OBU_METADATA_TYPE_SCALABILITY) { |
631 | 0 | read_metadata_scalability(data + type_length, sz - type_length); |
632 | 0 | } else if (metadata_type == OBU_METADATA_TYPE_TIMECODE) { |
633 | 0 | read_metadata_timecode(data + type_length, sz - type_length); |
634 | 0 | } |
635 | 0 |
|
636 | 0 | return sz; |
637 | 0 | } |
638 | | |
639 | | // On success, returns a boolean that indicates whether the decoding of the |
640 | | // current frame is finished. On failure, sets cm->error.error_code and |
641 | | // returns -1. |
642 | | int aom_decode_frame_from_obus(struct AV1Decoder *pbi, const uint8_t *data, |
643 | | const uint8_t *data_end, |
644 | 0 | const uint8_t **p_data_end) { |
645 | 0 | AV1_COMMON *const cm = &pbi->common; |
646 | 0 | int frame_decoding_finished = 0; |
647 | 0 | int is_first_tg_obu_received = 1; |
648 | 0 | uint32_t frame_header_size = 0; |
649 | 0 | ObuHeader obu_header; |
650 | 0 | memset(&obu_header, 0, sizeof(obu_header)); |
651 | 0 | pbi->seen_frame_header = 0; |
652 | 0 |
|
653 | 0 | if (data_end < data) { |
654 | 0 | cm->error.error_code = AOM_CODEC_CORRUPT_FRAME; |
655 | 0 | return -1; |
656 | 0 | } |
657 | 0 | |
658 | 0 | // Reset pbi->camera_frame_header_ready to 0 if cm->large_scale_tile = 0. |
659 | 0 | if (!cm->large_scale_tile) pbi->camera_frame_header_ready = 0; |
660 | 0 |
|
661 | 0 | // decode frame as a series of OBUs |
662 | 0 | while (!frame_decoding_finished && !cm->error.error_code) { |
663 | 0 | struct aom_read_bit_buffer rb; |
664 | 0 | size_t payload_size = 0; |
665 | 0 | size_t decoded_payload_size = 0; |
666 | 0 | size_t obu_payload_offset = 0; |
667 | 0 | size_t bytes_read = 0; |
668 | 0 | const size_t bytes_available = data_end - data; |
669 | 0 |
|
670 | 0 | if (bytes_available == 0 && !pbi->seen_frame_header) { |
671 | 0 | *p_data_end = data; |
672 | 0 | cm->error.error_code = AOM_CODEC_OK; |
673 | 0 | break; |
674 | 0 | } |
675 | 0 | |
676 | 0 | aom_codec_err_t status = |
677 | 0 | aom_read_obu_header_and_size(data, bytes_available, cm->is_annexb, |
678 | 0 | &obu_header, &payload_size, &bytes_read); |
679 | 0 |
|
680 | 0 | if (status != AOM_CODEC_OK) { |
681 | 0 | cm->error.error_code = status; |
682 | 0 | return -1; |
683 | 0 | } |
684 | 0 | |
685 | 0 | // Record obu size header information. |
686 | 0 | pbi->obu_size_hdr.data = data + obu_header.size; |
687 | 0 | pbi->obu_size_hdr.size = bytes_read - obu_header.size; |
688 | 0 |
|
689 | 0 | // Note: aom_read_obu_header_and_size() takes care of checking that this |
690 | 0 | // doesn't cause 'data' to advance past 'data_end'. |
691 | 0 | data += bytes_read; |
692 | 0 |
|
693 | 0 | if ((size_t)(data_end - data) < payload_size) { |
694 | 0 | cm->error.error_code = AOM_CODEC_CORRUPT_FRAME; |
695 | 0 | return -1; |
696 | 0 | } |
697 | 0 | |
698 | 0 | cm->temporal_layer_id = obu_header.temporal_layer_id; |
699 | 0 | cm->spatial_layer_id = obu_header.spatial_layer_id; |
700 | 0 |
|
701 | 0 | if (obu_header.type != OBU_TEMPORAL_DELIMITER && |
702 | 0 | obu_header.type != OBU_SEQUENCE_HEADER && |
703 | 0 | obu_header.type != OBU_PADDING) { |
704 | 0 | // don't decode obu if it's not in current operating mode |
705 | 0 | if (!is_obu_in_current_operating_point(pbi, obu_header)) { |
706 | 0 | data += payload_size; |
707 | 0 | continue; |
708 | 0 | } |
709 | 0 | } |
710 | 0 | |
711 | 0 | av1_init_read_bit_buffer(pbi, &rb, data, data + payload_size); |
712 | 0 |
|
713 | 0 | switch (obu_header.type) { |
714 | 0 | case OBU_TEMPORAL_DELIMITER: |
715 | 0 | decoded_payload_size = read_temporal_delimiter_obu(); |
716 | 0 | pbi->seen_frame_header = 0; |
717 | 0 | break; |
718 | 0 | case OBU_SEQUENCE_HEADER: |
719 | 0 | decoded_payload_size = read_sequence_header_obu(pbi, &rb); |
720 | 0 | if (cm->error.error_code != AOM_CODEC_OK) return -1; |
721 | 0 | break; |
722 | 0 | case OBU_FRAME_HEADER: |
723 | 0 | case OBU_REDUNDANT_FRAME_HEADER: |
724 | 0 | case OBU_FRAME: |
725 | 0 | // Only decode first frame header received |
726 | 0 | if (!pbi->seen_frame_header || |
727 | 0 | (cm->large_scale_tile && !pbi->camera_frame_header_ready)) { |
728 | 0 | frame_header_size = read_frame_header_obu( |
729 | 0 | pbi, &rb, data, p_data_end, obu_header.type != OBU_FRAME); |
730 | 0 | pbi->seen_frame_header = 1; |
731 | 0 | if (!pbi->ext_tile_debug && cm->large_scale_tile) |
732 | 0 | pbi->camera_frame_header_ready = 1; |
733 | 0 | } else { |
734 | 0 | // TODO(wtc): Verify that the frame_header_obu is identical to the |
735 | 0 | // original frame_header_obu. For now just skip frame_header_size |
736 | 0 | // bytes in the bit buffer. |
737 | 0 | if (frame_header_size > payload_size) { |
738 | 0 | cm->error.error_code = AOM_CODEC_CORRUPT_FRAME; |
739 | 0 | return -1; |
740 | 0 | } |
741 | 0 | assert(rb.bit_offset == 0); |
742 | 0 | rb.bit_offset = 8 * frame_header_size; |
743 | 0 | } |
744 | 0 |
|
745 | 0 | decoded_payload_size = frame_header_size; |
746 | 0 | pbi->frame_header_size = frame_header_size; |
747 | 0 |
|
748 | 0 | if (cm->show_existing_frame) { |
749 | 0 | if (obu_header.type == OBU_FRAME) { |
750 | 0 | cm->error.error_code = AOM_CODEC_UNSUP_BITSTREAM; |
751 | 0 | return -1; |
752 | 0 | } |
753 | 0 | frame_decoding_finished = 1; |
754 | 0 | pbi->seen_frame_header = 0; |
755 | 0 | break; |
756 | 0 | } |
757 | 0 | |
758 | 0 | // In large scale tile coding, decode the common camera frame header |
759 | 0 | // before any tile list OBU. |
760 | 0 | if (!pbi->ext_tile_debug && pbi->camera_frame_header_ready) { |
761 | 0 | frame_decoding_finished = 1; |
762 | 0 | // Skip the rest of the frame data. |
763 | 0 | decoded_payload_size = payload_size; |
764 | 0 | // Update data_end. |
765 | 0 | *p_data_end = data_end; |
766 | 0 | break; |
767 | 0 | } |
768 | 0 | |
769 | 0 | if (obu_header.type != OBU_FRAME) break; |
770 | 0 | obu_payload_offset = frame_header_size; |
771 | 0 | // Byte align the reader before reading the tile group. |
772 | 0 | if (byte_alignment(cm, &rb)) return -1; |
773 | 0 | AOM_FALLTHROUGH_INTENDED; // fall through to read tile group. |
774 | 0 | case OBU_TILE_GROUP: |
775 | 0 | if (!pbi->seen_frame_header) { |
776 | 0 | cm->error.error_code = AOM_CODEC_CORRUPT_FRAME; |
777 | 0 | return -1; |
778 | 0 | } |
779 | 0 | if (obu_payload_offset > payload_size) { |
780 | 0 | cm->error.error_code = AOM_CODEC_CORRUPT_FRAME; |
781 | 0 | return -1; |
782 | 0 | } |
783 | 0 | decoded_payload_size += read_one_tile_group_obu( |
784 | 0 | pbi, &rb, is_first_tg_obu_received, data + obu_payload_offset, |
785 | 0 | data + payload_size, p_data_end, &frame_decoding_finished, |
786 | 0 | obu_header.type == OBU_FRAME); |
787 | 0 | is_first_tg_obu_received = 0; |
788 | 0 | if (frame_decoding_finished) pbi->seen_frame_header = 0; |
789 | 0 | break; |
790 | 0 | case OBU_METADATA: |
791 | 0 | decoded_payload_size = read_metadata(data, payload_size); |
792 | 0 | break; |
793 | 0 | case OBU_TILE_LIST: |
794 | 0 | if (CONFIG_NORMAL_TILE_MODE) { |
795 | 0 | cm->error.error_code = AOM_CODEC_UNSUP_BITSTREAM; |
796 | 0 | return -1; |
797 | 0 | } |
798 | 0 | |
799 | 0 | // This OBU type is purely for the large scale tile coding mode. |
800 | 0 | // The common camera frame header has to be already decoded. |
801 | 0 | if (!pbi->camera_frame_header_ready) { |
802 | 0 | cm->error.error_code = AOM_CODEC_CORRUPT_FRAME; |
803 | 0 | return -1; |
804 | 0 | } |
805 | 0 | |
806 | 0 | cm->large_scale_tile = 1; |
807 | 0 | av1_set_single_tile_decoding_mode(cm); |
808 | 0 | decoded_payload_size = |
809 | 0 | read_and_decode_one_tile_list(pbi, &rb, data, data + payload_size, |
810 | 0 | p_data_end, &frame_decoding_finished); |
811 | 0 | if (cm->error.error_code != AOM_CODEC_OK) return -1; |
812 | 0 | break; |
813 | 0 | case OBU_PADDING: |
814 | 0 | default: |
815 | 0 | // Skip unrecognized OBUs |
816 | 0 | decoded_payload_size = payload_size; |
817 | 0 | break; |
818 | 0 | } |
819 | 0 | |
820 | 0 | // Check that the signalled OBU size matches the actual amount of data read |
821 | 0 | if (decoded_payload_size > payload_size) { |
822 | 0 | cm->error.error_code = AOM_CODEC_CORRUPT_FRAME; |
823 | 0 | return -1; |
824 | 0 | } |
825 | 0 | |
826 | 0 | // If there are extra padding bytes, they should all be zero |
827 | 0 | while (decoded_payload_size < payload_size) { |
828 | 0 | uint8_t padding_byte = data[decoded_payload_size++]; |
829 | 0 | if (padding_byte != 0) { |
830 | 0 | cm->error.error_code = AOM_CODEC_CORRUPT_FRAME; |
831 | 0 | return -1; |
832 | 0 | } |
833 | 0 | } |
834 | 0 |
|
835 | 0 | data += payload_size; |
836 | 0 | } |
837 | 0 |
|
838 | 0 | return frame_decoding_finished; |
839 | 0 | } |