decoder.c (17481B)
1 /* 2 * Copyright (c) 2016, 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 <limits.h> 14 #include <stdio.h> 15 16 #include "config/av1_rtcd.h" 17 #include "config/aom_dsp_rtcd.h" 18 #include "config/aom_scale_rtcd.h" 19 20 #include "aom_dsp/aom_dsp_common.h" 21 #include "aom_mem/aom_mem.h" 22 #include "aom_ports/aom_timer.h" 23 #include "aom_util/aom_pthread.h" 24 #include "aom_util/aom_thread.h" 25 26 #include "av1/common/alloccommon.h" 27 #include "av1/common/av1_common_int.h" 28 #include "av1/common/av1_loopfilter.h" 29 #include "av1/common/quant_common.h" 30 #include "av1/common/reconinter.h" 31 #include "av1/common/reconintra.h" 32 33 #include "av1/decoder/decodeframe.h" 34 #include "av1/decoder/decoder.h" 35 #include "av1/decoder/detokenize.h" 36 #include "av1/decoder/obu.h" 37 38 static void initialize_dec(void) { 39 av1_rtcd(); 40 aom_dsp_rtcd(); 41 aom_scale_rtcd(); 42 av1_init_intra_predictors(); 43 av1_init_wedge_masks(); 44 } 45 46 static void dec_set_mb_mi(CommonModeInfoParams *mi_params, int width, 47 int height, BLOCK_SIZE min_partition_size) { 48 (void)min_partition_size; 49 // Ensure that the decoded width and height are both multiples of 50 // 8 luma pixels (note: this may only be a multiple of 4 chroma pixels if 51 // subsampling is used). 52 // This simplifies the implementation of various experiments, 53 // eg. cdef, which operates on units of 8x8 luma pixels. 54 const int aligned_width = ALIGN_POWER_OF_TWO(width, 3); 55 const int aligned_height = ALIGN_POWER_OF_TWO(height, 3); 56 57 mi_params->mi_cols = aligned_width >> MI_SIZE_LOG2; 58 mi_params->mi_rows = aligned_height >> MI_SIZE_LOG2; 59 mi_params->mi_stride = calc_mi_size(mi_params->mi_cols); 60 61 mi_params->mb_cols = ROUND_POWER_OF_TWO(mi_params->mi_cols, 2); 62 mi_params->mb_rows = ROUND_POWER_OF_TWO(mi_params->mi_rows, 2); 63 mi_params->MBs = mi_params->mb_rows * mi_params->mb_cols; 64 65 mi_params->mi_alloc_bsize = BLOCK_4X4; 66 mi_params->mi_alloc_stride = mi_params->mi_stride; 67 68 assert(mi_size_wide[mi_params->mi_alloc_bsize] == 69 mi_size_high[mi_params->mi_alloc_bsize]); 70 } 71 72 static void dec_setup_mi(CommonModeInfoParams *mi_params) { 73 const int mi_grid_size = 74 mi_params->mi_stride * calc_mi_size(mi_params->mi_rows); 75 memset(mi_params->mi_grid_base, 0, 76 mi_grid_size * sizeof(*mi_params->mi_grid_base)); 77 } 78 79 static void dec_free_mi(CommonModeInfoParams *mi_params) { 80 aom_free(mi_params->mi_alloc); 81 mi_params->mi_alloc = NULL; 82 mi_params->mi_alloc_size = 0; 83 aom_free(mi_params->mi_grid_base); 84 mi_params->mi_grid_base = NULL; 85 mi_params->mi_grid_size = 0; 86 aom_free(mi_params->tx_type_map); 87 mi_params->tx_type_map = NULL; 88 } 89 90 AV1Decoder *av1_decoder_create(BufferPool *const pool) { 91 AV1Decoder *volatile const pbi = aom_memalign(32, sizeof(*pbi)); 92 if (!pbi) return NULL; 93 av1_zero(*pbi); 94 95 AV1_COMMON *volatile const cm = &pbi->common; 96 cm->seq_params = &pbi->seq_params; 97 cm->error = &pbi->error; 98 99 // The jmp_buf is valid only for the duration of the function that calls 100 // setjmp(). Therefore, this function must reset the 'setjmp' field to 0 101 // before it returns. 102 if (setjmp(pbi->error.jmp)) { 103 pbi->error.setjmp = 0; 104 av1_decoder_remove(pbi); 105 return NULL; 106 } 107 108 pbi->error.setjmp = 1; 109 110 CHECK_MEM_ERROR(cm, cm->fc, 111 (FRAME_CONTEXT *)aom_memalign(32, sizeof(*cm->fc))); 112 CHECK_MEM_ERROR( 113 cm, cm->default_frame_context, 114 (FRAME_CONTEXT *)aom_memalign(32, sizeof(*cm->default_frame_context))); 115 memset(cm->fc, 0, sizeof(*cm->fc)); 116 memset(cm->default_frame_context, 0, sizeof(*cm->default_frame_context)); 117 118 pbi->need_resync = 1; 119 initialize_dec(); 120 121 // Initialize the references to not point to any frame buffers. 122 for (int i = 0; i < REF_FRAMES; i++) { 123 cm->ref_frame_map[i] = NULL; 124 } 125 126 cm->current_frame.frame_number = 0; 127 pbi->decoding_first_frame = 1; 128 pbi->common.buffer_pool = pool; 129 130 cm->seq_params->bit_depth = AOM_BITS_8; 131 132 cm->mi_params.free_mi = dec_free_mi; 133 cm->mi_params.setup_mi = dec_setup_mi; 134 cm->mi_params.set_mb_mi = dec_set_mb_mi; 135 136 av1_loop_filter_init(cm); 137 138 av1_qm_init(&cm->quant_params, av1_num_planes(cm)); 139 av1_loop_restoration_precal(); 140 141 #if CONFIG_ACCOUNTING 142 pbi->acct_enabled = 1; 143 aom_accounting_init(&pbi->accounting); 144 #endif 145 146 pbi->error.setjmp = 0; 147 148 aom_get_worker_interface()->init(&pbi->lf_worker); 149 pbi->lf_worker.thread_name = "aom lf worker"; 150 151 return pbi; 152 } 153 154 void av1_dealloc_dec_jobs(struct AV1DecTileMTData *tile_mt_info) { 155 if (tile_mt_info != NULL) { 156 #if CONFIG_MULTITHREAD 157 if (tile_mt_info->job_mutex != NULL) { 158 pthread_mutex_destroy(tile_mt_info->job_mutex); 159 aom_free(tile_mt_info->job_mutex); 160 } 161 #endif 162 aom_free(tile_mt_info->job_queue); 163 // clear the structure as the source of this call may be a resize in which 164 // case this call will be followed by an _alloc() which may fail. 165 av1_zero(*tile_mt_info); 166 } 167 } 168 169 void av1_dec_free_cb_buf(AV1Decoder *pbi) { 170 aom_free(pbi->cb_buffer_base); 171 pbi->cb_buffer_base = NULL; 172 pbi->cb_buffer_alloc_size = 0; 173 } 174 175 void av1_decoder_remove(AV1Decoder *pbi) { 176 int i; 177 178 if (!pbi) return; 179 180 // Free the tile list output buffer. 181 aom_free_frame_buffer(&pbi->tile_list_outbuf); 182 183 aom_get_worker_interface()->end(&pbi->lf_worker); 184 aom_free(pbi->lf_worker.data1); 185 186 if (pbi->thread_data) { 187 for (int worker_idx = 1; worker_idx < pbi->num_workers; worker_idx++) { 188 DecWorkerData *const thread_data = pbi->thread_data + worker_idx; 189 if (thread_data->td != NULL) { 190 av1_free_mc_tmp_buf(thread_data->td); 191 aom_free(thread_data->td); 192 } 193 } 194 aom_free(pbi->thread_data); 195 } 196 aom_free(pbi->dcb.xd.seg_mask); 197 198 for (i = 0; i < pbi->num_workers; ++i) { 199 AVxWorker *const worker = &pbi->tile_workers[i]; 200 aom_get_worker_interface()->end(worker); 201 } 202 #if CONFIG_MULTITHREAD 203 if (pbi->row_mt_mutex_ != NULL) { 204 pthread_mutex_destroy(pbi->row_mt_mutex_); 205 aom_free(pbi->row_mt_mutex_); 206 } 207 if (pbi->row_mt_cond_ != NULL) { 208 pthread_cond_destroy(pbi->row_mt_cond_); 209 aom_free(pbi->row_mt_cond_); 210 } 211 #endif 212 for (i = 0; i < pbi->allocated_tiles; i++) { 213 TileDataDec *const tile_data = pbi->tile_data + i; 214 av1_dec_row_mt_dealloc(&tile_data->dec_row_mt_sync); 215 } 216 aom_free(pbi->tile_data); 217 aom_free(pbi->tile_workers); 218 219 if (pbi->num_workers > 0) { 220 av1_loop_filter_dealloc(&pbi->lf_row_sync); 221 av1_loop_restoration_dealloc(&pbi->lr_row_sync); 222 av1_dealloc_dec_jobs(&pbi->tile_mt_info); 223 } 224 225 av1_dec_free_cb_buf(pbi); 226 #if CONFIG_ACCOUNTING 227 aom_accounting_clear(&pbi->accounting); 228 #endif 229 av1_free_mc_tmp_buf(&pbi->td); 230 aom_img_metadata_array_free(pbi->metadata); 231 av1_remove_common(&pbi->common); 232 aom_free(pbi); 233 } 234 235 void av1_visit_palette(AV1Decoder *const pbi, MACROBLOCKD *const xd, 236 aom_reader *r, palette_visitor_fn_t visit) { 237 if (!is_inter_block(xd->mi[0])) { 238 for (int plane = 0; plane < AOMMIN(2, av1_num_planes(&pbi->common)); 239 ++plane) { 240 if (plane == 0 || xd->is_chroma_ref) { 241 if (xd->mi[0]->palette_mode_info.palette_size[plane]) 242 visit(xd, plane, r); 243 } else { 244 assert(xd->mi[0]->palette_mode_info.palette_size[plane] == 0); 245 } 246 } 247 } 248 } 249 250 static int equal_dimensions(const YV12_BUFFER_CONFIG *a, 251 const YV12_BUFFER_CONFIG *b) { 252 return a->y_height == b->y_height && a->y_width == b->y_width && 253 a->uv_height == b->uv_height && a->uv_width == b->uv_width; 254 } 255 256 aom_codec_err_t av1_copy_reference_dec(AV1Decoder *pbi, int idx, 257 YV12_BUFFER_CONFIG *sd) { 258 AV1_COMMON *cm = &pbi->common; 259 const int num_planes = av1_num_planes(cm); 260 261 const YV12_BUFFER_CONFIG *const cfg = get_ref_frame(cm, idx); 262 if (cfg == NULL) { 263 aom_internal_error(&pbi->error, AOM_CODEC_ERROR, "No reference frame"); 264 return AOM_CODEC_ERROR; 265 } 266 if (!equal_dimensions(cfg, sd)) 267 aom_internal_error(&pbi->error, AOM_CODEC_ERROR, 268 "Incorrect buffer dimensions"); 269 else 270 aom_yv12_copy_frame(cfg, sd, num_planes); 271 272 return pbi->error.error_code; 273 } 274 275 static int equal_dimensions_and_border(const YV12_BUFFER_CONFIG *a, 276 const YV12_BUFFER_CONFIG *b) { 277 return a->y_height == b->y_height && a->y_width == b->y_width && 278 a->uv_height == b->uv_height && a->uv_width == b->uv_width && 279 a->y_stride == b->y_stride && a->uv_stride == b->uv_stride && 280 a->border == b->border && 281 (a->flags & YV12_FLAG_HIGHBITDEPTH) == 282 (b->flags & YV12_FLAG_HIGHBITDEPTH); 283 } 284 285 aom_codec_err_t av1_set_reference_dec(AV1_COMMON *cm, int idx, 286 int use_external_ref, 287 YV12_BUFFER_CONFIG *sd) { 288 const int num_planes = av1_num_planes(cm); 289 YV12_BUFFER_CONFIG *ref_buf = NULL; 290 291 // Get the destination reference buffer. 292 ref_buf = get_ref_frame(cm, idx); 293 294 if (ref_buf == NULL) { 295 aom_internal_error(cm->error, AOM_CODEC_ERROR, "No reference frame"); 296 return AOM_CODEC_ERROR; 297 } 298 299 if (!use_external_ref) { 300 if (!equal_dimensions(ref_buf, sd)) { 301 aom_internal_error(cm->error, AOM_CODEC_ERROR, 302 "Incorrect buffer dimensions"); 303 } else { 304 // Overwrite the reference frame buffer. 305 aom_yv12_copy_frame(sd, ref_buf, num_planes); 306 } 307 } else { 308 if (!equal_dimensions_and_border(ref_buf, sd)) { 309 aom_internal_error(cm->error, AOM_CODEC_ERROR, 310 "Incorrect buffer dimensions"); 311 } else { 312 // Overwrite the reference frame buffer pointers. 313 // Once we no longer need the external reference buffer, these pointers 314 // are restored. 315 ref_buf->store_buf_adr[0] = ref_buf->y_buffer; 316 ref_buf->store_buf_adr[1] = ref_buf->u_buffer; 317 ref_buf->store_buf_adr[2] = ref_buf->v_buffer; 318 ref_buf->y_buffer = sd->y_buffer; 319 ref_buf->u_buffer = sd->u_buffer; 320 ref_buf->v_buffer = sd->v_buffer; 321 ref_buf->use_external_reference_buffers = 1; 322 } 323 } 324 325 return cm->error->error_code; 326 } 327 328 aom_codec_err_t av1_copy_new_frame_dec(AV1_COMMON *cm, 329 YV12_BUFFER_CONFIG *new_frame, 330 YV12_BUFFER_CONFIG *sd) { 331 const int num_planes = av1_num_planes(cm); 332 333 if (!equal_dimensions_and_border(new_frame, sd)) 334 aom_internal_error(cm->error, AOM_CODEC_ERROR, 335 "Incorrect buffer dimensions"); 336 else 337 aom_yv12_copy_frame(new_frame, sd, num_planes); 338 339 return cm->error->error_code; 340 } 341 342 static void release_current_frame(AV1Decoder *pbi) { 343 AV1_COMMON *const cm = &pbi->common; 344 BufferPool *const pool = cm->buffer_pool; 345 346 cm->cur_frame->buf.corrupted = 1; 347 lock_buffer_pool(pool); 348 decrease_ref_count(cm->cur_frame, pool); 349 unlock_buffer_pool(pool); 350 cm->cur_frame = NULL; 351 } 352 353 // If any buffer updating is signaled it should be done here. 354 // Consumes a reference to cm->cur_frame. 355 // 356 // This functions returns void. It reports failure by setting 357 // pbi->error.error_code. 358 static void update_frame_buffers(AV1Decoder *pbi, int frame_decoded) { 359 int ref_index = 0, mask; 360 AV1_COMMON *const cm = &pbi->common; 361 BufferPool *const pool = cm->buffer_pool; 362 363 if (frame_decoded) { 364 lock_buffer_pool(pool); 365 366 // In ext-tile decoding, the camera frame header is only decoded once. So, 367 // we don't update the references here. 368 if (!pbi->camera_frame_header_ready) { 369 // The following for loop needs to release the reference stored in 370 // cm->ref_frame_map[ref_index] before storing a reference to 371 // cm->cur_frame in cm->ref_frame_map[ref_index]. 372 for (mask = cm->current_frame.refresh_frame_flags; mask; mask >>= 1) { 373 if (mask & 1) { 374 decrease_ref_count(cm->ref_frame_map[ref_index], pool); 375 cm->ref_frame_map[ref_index] = cm->cur_frame; 376 ++cm->cur_frame->ref_count; 377 } 378 ++ref_index; 379 } 380 } 381 382 if (cm->show_existing_frame || cm->show_frame) { 383 if (pbi->output_all_layers) { 384 // Append this frame to the output queue 385 if (pbi->num_output_frames >= MAX_NUM_SPATIAL_LAYERS) { 386 // We can't store the new frame anywhere, so drop it and return an 387 // error 388 cm->cur_frame->buf.corrupted = 1; 389 decrease_ref_count(cm->cur_frame, pool); 390 pbi->error.error_code = AOM_CODEC_UNSUP_BITSTREAM; 391 } else { 392 pbi->output_frames[pbi->num_output_frames] = cm->cur_frame; 393 pbi->num_output_frames++; 394 } 395 } else { 396 // Replace any existing output frame 397 assert(pbi->num_output_frames == 0 || pbi->num_output_frames == 1); 398 if (pbi->num_output_frames > 0) { 399 decrease_ref_count(pbi->output_frames[0], pool); 400 } 401 pbi->output_frames[0] = cm->cur_frame; 402 pbi->num_output_frames = 1; 403 } 404 } else { 405 decrease_ref_count(cm->cur_frame, pool); 406 } 407 408 unlock_buffer_pool(pool); 409 } else { 410 // Nothing was decoded, so just drop this frame buffer 411 lock_buffer_pool(pool); 412 decrease_ref_count(cm->cur_frame, pool); 413 unlock_buffer_pool(pool); 414 } 415 cm->cur_frame = NULL; 416 417 if (!pbi->camera_frame_header_ready) { 418 // Invalidate these references until the next frame starts. 419 for (ref_index = 0; ref_index < INTER_REFS_PER_FRAME; ref_index++) { 420 cm->remapped_ref_idx[ref_index] = INVALID_IDX; 421 } 422 } 423 } 424 425 int av1_receive_compressed_data(AV1Decoder *pbi, size_t size, 426 const uint8_t **psource) { 427 AV1_COMMON *volatile const cm = &pbi->common; 428 const uint8_t *source = *psource; 429 pbi->error.error_code = AOM_CODEC_OK; 430 pbi->error.has_detail = 0; 431 432 if (size == 0) { 433 // This is used to signal that we are missing frames. 434 // We do not know if the missing frame(s) was supposed to update 435 // any of the reference buffers, but we act conservative and 436 // mark only the last buffer as corrupted. 437 // 438 // TODO(jkoleszar): Error concealment is undefined and non-normative 439 // at this point, but if it becomes so, [0] may not always be the correct 440 // thing to do here. 441 RefCntBuffer *ref_buf = get_ref_frame_buf(cm, LAST_FRAME); 442 if (ref_buf != NULL) ref_buf->buf.corrupted = 1; 443 } 444 445 if (assign_cur_frame_new_fb(cm) == NULL) { 446 pbi->error.error_code = AOM_CODEC_MEM_ERROR; 447 return 1; 448 } 449 450 // The jmp_buf is valid only for the duration of the function that calls 451 // setjmp(). Therefore, this function must reset the 'setjmp' field to 0 452 // before it returns. 453 if (setjmp(pbi->error.jmp)) { 454 const AVxWorkerInterface *const winterface = aom_get_worker_interface(); 455 int i; 456 457 pbi->error.setjmp = 0; 458 459 // Synchronize all threads immediately as a subsequent decode call may 460 // cause a resize invalidating some allocations. 461 winterface->sync(&pbi->lf_worker); 462 for (i = 0; i < pbi->num_workers; ++i) { 463 winterface->sync(&pbi->tile_workers[i]); 464 } 465 466 release_current_frame(pbi); 467 return -1; 468 } 469 470 pbi->error.setjmp = 1; 471 472 int frame_decoded = 473 aom_decode_frame_from_obus(pbi, source, source + size, psource); 474 475 if (frame_decoded < 0) { 476 assert(pbi->error.error_code != AOM_CODEC_OK); 477 release_current_frame(pbi); 478 pbi->error.setjmp = 0; 479 return 1; 480 } 481 482 #if TXCOEFF_TIMER 483 cm->cum_txcoeff_timer += cm->txcoeff_timer; 484 fprintf(stderr, 485 "txb coeff block number: %d, frame time: %ld, cum time %ld in us\n", 486 cm->txb_count, cm->txcoeff_timer, cm->cum_txcoeff_timer); 487 cm->txcoeff_timer = 0; 488 cm->txb_count = 0; 489 #endif 490 491 // Note: At this point, this function holds a reference to cm->cur_frame 492 // in the buffer pool. This reference is consumed by update_frame_buffers(). 493 update_frame_buffers(pbi, frame_decoded); 494 495 if (frame_decoded) { 496 pbi->decoding_first_frame = 0; 497 } 498 499 if (pbi->error.error_code != AOM_CODEC_OK) { 500 pbi->error.setjmp = 0; 501 return 1; 502 } 503 504 if (!cm->show_existing_frame) { 505 if (cm->seg.enabled) { 506 if (cm->prev_frame && 507 (cm->mi_params.mi_rows == cm->prev_frame->mi_rows) && 508 (cm->mi_params.mi_cols == cm->prev_frame->mi_cols)) { 509 cm->last_frame_seg_map = cm->prev_frame->seg_map; 510 } else { 511 cm->last_frame_seg_map = NULL; 512 } 513 } 514 } 515 516 // Update progress in frame parallel decode. 517 pbi->error.setjmp = 0; 518 519 return 0; 520 } 521 522 // Get the frame at a particular index in the output queue 523 int av1_get_raw_frame(AV1Decoder *pbi, size_t index, YV12_BUFFER_CONFIG **sd, 524 aom_film_grain_t **grain_params) { 525 if (index >= pbi->num_output_frames) return -1; 526 *sd = &pbi->output_frames[index]->buf; 527 *grain_params = &pbi->output_frames[index]->film_grain_params; 528 return 0; 529 } 530 531 // Get the highest-spatial-layer output 532 // TODO(rachelbarker): What should this do? 533 int av1_get_frame_to_show(AV1Decoder *pbi, YV12_BUFFER_CONFIG *frame) { 534 if (pbi->num_output_frames == 0) return -1; 535 536 *frame = pbi->output_frames[pbi->num_output_frames - 1]->buf; 537 return 0; 538 }