1 /* 2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved. 3 * 4 * Use of this source code is governed by a BSD-style license 5 * that can be found in the LICENSE file in the root of the source 6 * tree. An additional intellectual property rights grant can be found 7 * in the file PATENTS. All contributing project authors may 8 * be found in the AUTHORS file in the root of the source tree. 9 */ 10 11 #include <assert.h> 12 #include <limits.h> 13 #include <stdio.h> 14 15 #include "./vp9_rtcd.h" 16 #include "./vpx_dsp_rtcd.h" 17 #include "./vpx_scale_rtcd.h" 18 19 #include "vpx_mem/vpx_mem.h" 20 #include "vpx_ports/system_state.h" 21 #include "vpx_ports/vpx_once.h" 22 #include "vpx_ports/vpx_timer.h" 23 #include "vpx_scale/vpx_scale.h" 24 #include "vpx_util/vpx_thread.h" 25 26 #include "vp9/common/vp9_alloccommon.h" 27 #include "vp9/common/vp9_loopfilter.h" 28 #include "vp9/common/vp9_onyxc_int.h" 29 #if CONFIG_VP9_POSTPROC 30 #include "vp9/common/vp9_postproc.h" 31 #endif 32 #include "vp9/common/vp9_quant_common.h" 33 #include "vp9/common/vp9_reconintra.h" 34 35 #include "vp9/decoder/vp9_decodeframe.h" 36 #include "vp9/decoder/vp9_decoder.h" 37 #include "vp9/decoder/vp9_detokenize.h" 38 39 static void initialize_dec(void) { 40 static volatile int init_done = 0; 41 42 if (!init_done) { 43 vp9_rtcd(); 44 vpx_dsp_rtcd(); 45 vpx_scale_rtcd(); 46 vp9_init_intra_predictors(); 47 init_done = 1; 48 } 49 } 50 51 static void vp9_dec_setup_mi(VP9_COMMON *cm) { 52 cm->mi = cm->mip + cm->mi_stride + 1; 53 cm->mi_grid_visible = cm->mi_grid_base + cm->mi_stride + 1; 54 memset(cm->mi_grid_base, 0, 55 cm->mi_stride * (cm->mi_rows + 1) * sizeof(*cm->mi_grid_base)); 56 } 57 58 static int vp9_dec_alloc_mi(VP9_COMMON *cm, int mi_size) { 59 cm->mip = vpx_calloc(mi_size, sizeof(*cm->mip)); 60 if (!cm->mip) return 1; 61 cm->mi_alloc_size = mi_size; 62 cm->mi_grid_base = (MODE_INFO **)vpx_calloc(mi_size, sizeof(MODE_INFO *)); 63 if (!cm->mi_grid_base) return 1; 64 return 0; 65 } 66 67 static void vp9_dec_free_mi(VP9_COMMON *cm) { 68 vpx_free(cm->mip); 69 cm->mip = NULL; 70 vpx_free(cm->mi_grid_base); 71 cm->mi_grid_base = NULL; 72 } 73 74 VP9Decoder *vp9_decoder_create(BufferPool *const pool) { 75 VP9Decoder *volatile const pbi = vpx_memalign(32, sizeof(*pbi)); 76 VP9_COMMON *volatile const cm = pbi ? &pbi->common : NULL; 77 78 if (!cm) return NULL; 79 80 vp9_zero(*pbi); 81 82 if (setjmp(cm->error.jmp)) { 83 cm->error.setjmp = 0; 84 vp9_decoder_remove(pbi); 85 return NULL; 86 } 87 88 cm->error.setjmp = 1; 89 90 CHECK_MEM_ERROR(cm, cm->fc, (FRAME_CONTEXT *)vpx_calloc(1, sizeof(*cm->fc))); 91 CHECK_MEM_ERROR( 92 cm, cm->frame_contexts, 93 (FRAME_CONTEXT *)vpx_calloc(FRAME_CONTEXTS, sizeof(*cm->frame_contexts))); 94 95 pbi->need_resync = 1; 96 once(initialize_dec); 97 98 // Initialize the references to not point to any frame buffers. 99 memset(&cm->ref_frame_map, -1, sizeof(cm->ref_frame_map)); 100 memset(&cm->next_ref_frame_map, -1, sizeof(cm->next_ref_frame_map)); 101 102 cm->current_video_frame = 0; 103 pbi->ready_for_new_data = 1; 104 pbi->common.buffer_pool = pool; 105 106 cm->bit_depth = VPX_BITS_8; 107 cm->dequant_bit_depth = VPX_BITS_8; 108 109 cm->alloc_mi = vp9_dec_alloc_mi; 110 cm->free_mi = vp9_dec_free_mi; 111 cm->setup_mi = vp9_dec_setup_mi; 112 113 vp9_loop_filter_init(cm); 114 115 cm->error.setjmp = 0; 116 117 vpx_get_worker_interface()->init(&pbi->lf_worker); 118 119 return pbi; 120 } 121 122 void vp9_decoder_remove(VP9Decoder *pbi) { 123 int i; 124 125 if (!pbi) return; 126 127 vpx_get_worker_interface()->end(&pbi->lf_worker); 128 vpx_free(pbi->lf_worker.data1); 129 130 for (i = 0; i < pbi->num_tile_workers; ++i) { 131 VPxWorker *const worker = &pbi->tile_workers[i]; 132 vpx_get_worker_interface()->end(worker); 133 } 134 135 vpx_free(pbi->tile_worker_data); 136 vpx_free(pbi->tile_workers); 137 138 if (pbi->num_tile_workers > 0) { 139 vp9_loop_filter_dealloc(&pbi->lf_row_sync); 140 } 141 142 vp9_remove_common(&pbi->common); 143 vpx_free(pbi); 144 } 145 146 static int equal_dimensions(const YV12_BUFFER_CONFIG *a, 147 const YV12_BUFFER_CONFIG *b) { 148 return a->y_height == b->y_height && a->y_width == b->y_width && 149 a->uv_height == b->uv_height && a->uv_width == b->uv_width; 150 } 151 152 vpx_codec_err_t vp9_copy_reference_dec(VP9Decoder *pbi, 153 VP9_REFFRAME ref_frame_flag, 154 YV12_BUFFER_CONFIG *sd) { 155 VP9_COMMON *cm = &pbi->common; 156 157 /* TODO(jkoleszar): The decoder doesn't have any real knowledge of what the 158 * encoder is using the frame buffers for. This is just a stub to keep the 159 * vpxenc --test-decode functionality working, and will be replaced in a 160 * later commit that adds VP9-specific controls for this functionality. 161 */ 162 if (ref_frame_flag == VP9_LAST_FLAG) { 163 const YV12_BUFFER_CONFIG *const cfg = get_ref_frame(cm, 0); 164 if (cfg == NULL) { 165 vpx_internal_error(&cm->error, VPX_CODEC_ERROR, 166 "No 'last' reference frame"); 167 return VPX_CODEC_ERROR; 168 } 169 if (!equal_dimensions(cfg, sd)) 170 vpx_internal_error(&cm->error, VPX_CODEC_ERROR, 171 "Incorrect buffer dimensions"); 172 else 173 vpx_yv12_copy_frame(cfg, sd); 174 } else { 175 vpx_internal_error(&cm->error, VPX_CODEC_ERROR, "Invalid reference frame"); 176 } 177 178 return cm->error.error_code; 179 } 180 181 vpx_codec_err_t vp9_set_reference_dec(VP9_COMMON *cm, 182 VP9_REFFRAME ref_frame_flag, 183 YV12_BUFFER_CONFIG *sd) { 184 int idx; 185 YV12_BUFFER_CONFIG *ref_buf = NULL; 186 187 // TODO(jkoleszar): The decoder doesn't have any real knowledge of what the 188 // encoder is using the frame buffers for. This is just a stub to keep the 189 // vpxenc --test-decode functionality working, and will be replaced in a 190 // later commit that adds VP9-specific controls for this functionality. 191 // (Yunqing) The set_reference control depends on the following setting in 192 // encoder. 193 // cpi->lst_fb_idx = 0; 194 // cpi->gld_fb_idx = 1; 195 // cpi->alt_fb_idx = 2; 196 if (ref_frame_flag == VP9_LAST_FLAG) { 197 idx = cm->ref_frame_map[0]; 198 } else if (ref_frame_flag == VP9_GOLD_FLAG) { 199 idx = cm->ref_frame_map[1]; 200 } else if (ref_frame_flag == VP9_ALT_FLAG) { 201 idx = cm->ref_frame_map[2]; 202 } else { 203 vpx_internal_error(&cm->error, VPX_CODEC_ERROR, "Invalid reference frame"); 204 return cm->error.error_code; 205 } 206 207 if (idx < 0 || idx >= FRAME_BUFFERS) { 208 vpx_internal_error(&cm->error, VPX_CODEC_ERROR, 209 "Invalid reference frame map"); 210 return cm->error.error_code; 211 } 212 213 // Get the destination reference buffer. 214 ref_buf = &cm->buffer_pool->frame_bufs[idx].buf; 215 216 if (!equal_dimensions(ref_buf, sd)) { 217 vpx_internal_error(&cm->error, VPX_CODEC_ERROR, 218 "Incorrect buffer dimensions"); 219 } else { 220 // Overwrite the reference frame buffer. 221 vpx_yv12_copy_frame(sd, ref_buf); 222 } 223 224 return cm->error.error_code; 225 } 226 227 /* If any buffer updating is signaled it should be done here. */ 228 static void swap_frame_buffers(VP9Decoder *pbi) { 229 int ref_index = 0, mask; 230 VP9_COMMON *const cm = &pbi->common; 231 BufferPool *const pool = cm->buffer_pool; 232 RefCntBuffer *const frame_bufs = cm->buffer_pool->frame_bufs; 233 234 for (mask = pbi->refresh_frame_flags; mask; mask >>= 1) { 235 const int old_idx = cm->ref_frame_map[ref_index]; 236 // Current thread releases the holding of reference frame. 237 decrease_ref_count(old_idx, frame_bufs, pool); 238 239 // Release the reference frame in reference map. 240 if (mask & 1) { 241 decrease_ref_count(old_idx, frame_bufs, pool); 242 } 243 cm->ref_frame_map[ref_index] = cm->next_ref_frame_map[ref_index]; 244 ++ref_index; 245 } 246 247 // Current thread releases the holding of reference frame. 248 for (; ref_index < REF_FRAMES && !cm->show_existing_frame; ++ref_index) { 249 const int old_idx = cm->ref_frame_map[ref_index]; 250 decrease_ref_count(old_idx, frame_bufs, pool); 251 cm->ref_frame_map[ref_index] = cm->next_ref_frame_map[ref_index]; 252 } 253 pbi->hold_ref_buf = 0; 254 cm->frame_to_show = get_frame_new_buffer(cm); 255 256 --frame_bufs[cm->new_fb_idx].ref_count; 257 258 // Invalidate these references until the next frame starts. 259 for (ref_index = 0; ref_index < 3; ref_index++) 260 cm->frame_refs[ref_index].idx = -1; 261 } 262 263 int vp9_receive_compressed_data(VP9Decoder *pbi, size_t size, 264 const uint8_t **psource) { 265 VP9_COMMON *volatile const cm = &pbi->common; 266 BufferPool *volatile const pool = cm->buffer_pool; 267 RefCntBuffer *volatile const frame_bufs = cm->buffer_pool->frame_bufs; 268 const uint8_t *source = *psource; 269 int retcode = 0; 270 cm->error.error_code = VPX_CODEC_OK; 271 272 if (size == 0) { 273 // This is used to signal that we are missing frames. 274 // We do not know if the missing frame(s) was supposed to update 275 // any of the reference buffers, but we act conservative and 276 // mark only the last buffer as corrupted. 277 // 278 // TODO(jkoleszar): Error concealment is undefined and non-normative 279 // at this point, but if it becomes so, [0] may not always be the correct 280 // thing to do here. 281 if (cm->frame_refs[0].idx > 0) { 282 assert(cm->frame_refs[0].buf != NULL); 283 cm->frame_refs[0].buf->corrupted = 1; 284 } 285 } 286 287 pbi->ready_for_new_data = 0; 288 289 // Check if the previous frame was a frame without any references to it. 290 if (cm->new_fb_idx >= 0 && frame_bufs[cm->new_fb_idx].ref_count == 0 && 291 !frame_bufs[cm->new_fb_idx].released) { 292 pool->release_fb_cb(pool->cb_priv, 293 &frame_bufs[cm->new_fb_idx].raw_frame_buffer); 294 frame_bufs[cm->new_fb_idx].released = 1; 295 } 296 297 // Find a free frame buffer. Return error if can not find any. 298 cm->new_fb_idx = get_free_fb(cm); 299 if (cm->new_fb_idx == INVALID_IDX) { 300 vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR, 301 "Unable to find free frame buffer"); 302 return cm->error.error_code; 303 } 304 305 // Assign a MV array to the frame buffer. 306 cm->cur_frame = &pool->frame_bufs[cm->new_fb_idx]; 307 308 pbi->hold_ref_buf = 0; 309 pbi->cur_buf = &frame_bufs[cm->new_fb_idx]; 310 311 if (setjmp(cm->error.jmp)) { 312 const VPxWorkerInterface *const winterface = vpx_get_worker_interface(); 313 int i; 314 315 cm->error.setjmp = 0; 316 pbi->ready_for_new_data = 1; 317 318 // Synchronize all threads immediately as a subsequent decode call may 319 // cause a resize invalidating some allocations. 320 winterface->sync(&pbi->lf_worker); 321 for (i = 0; i < pbi->num_tile_workers; ++i) { 322 winterface->sync(&pbi->tile_workers[i]); 323 } 324 325 // Release all the reference buffers if worker thread is holding them. 326 if (pbi->hold_ref_buf == 1) { 327 int ref_index = 0, mask; 328 for (mask = pbi->refresh_frame_flags; mask; mask >>= 1) { 329 const int old_idx = cm->ref_frame_map[ref_index]; 330 // Current thread releases the holding of reference frame. 331 decrease_ref_count(old_idx, frame_bufs, pool); 332 333 // Release the reference frame in reference map. 334 if (mask & 1) { 335 decrease_ref_count(old_idx, frame_bufs, pool); 336 } 337 ++ref_index; 338 } 339 340 // Current thread releases the holding of reference frame. 341 for (; ref_index < REF_FRAMES && !cm->show_existing_frame; ++ref_index) { 342 const int old_idx = cm->ref_frame_map[ref_index]; 343 decrease_ref_count(old_idx, frame_bufs, pool); 344 } 345 pbi->hold_ref_buf = 0; 346 } 347 // Release current frame. 348 decrease_ref_count(cm->new_fb_idx, frame_bufs, pool); 349 350 vpx_clear_system_state(); 351 return -1; 352 } 353 354 cm->error.setjmp = 1; 355 vp9_decode_frame(pbi, source, source + size, psource); 356 357 swap_frame_buffers(pbi); 358 359 vpx_clear_system_state(); 360 361 if (!cm->show_existing_frame) { 362 cm->last_show_frame = cm->show_frame; 363 cm->prev_frame = cm->cur_frame; 364 if (cm->seg.enabled) vp9_swap_current_and_last_seg_map(cm); 365 } 366 367 // Update progress in frame parallel decode. 368 cm->last_width = cm->width; 369 cm->last_height = cm->height; 370 if (cm->show_frame) { 371 cm->current_video_frame++; 372 } 373 374 cm->error.setjmp = 0; 375 return retcode; 376 } 377 378 int vp9_get_raw_frame(VP9Decoder *pbi, YV12_BUFFER_CONFIG *sd, 379 vp9_ppflags_t *flags) { 380 VP9_COMMON *const cm = &pbi->common; 381 int ret = -1; 382 #if !CONFIG_VP9_POSTPROC 383 (void)*flags; 384 #endif 385 386 if (pbi->ready_for_new_data == 1) return ret; 387 388 pbi->ready_for_new_data = 1; 389 390 /* no raw frame to show!!! */ 391 if (!cm->show_frame) return ret; 392 393 pbi->ready_for_new_data = 1; 394 395 #if CONFIG_VP9_POSTPROC 396 if (!cm->show_existing_frame) { 397 ret = vp9_post_proc_frame(cm, sd, flags); 398 } else { 399 *sd = *cm->frame_to_show; 400 ret = 0; 401 } 402 #else 403 *sd = *cm->frame_to_show; 404 ret = 0; 405 #endif /*!CONFIG_POSTPROC*/ 406 vpx_clear_system_state(); 407 return ret; 408 } 409 410 vpx_codec_err_t vp9_parse_superframe_index(const uint8_t *data, size_t data_sz, 411 uint32_t sizes[8], int *count, 412 vpx_decrypt_cb decrypt_cb, 413 void *decrypt_state) { 414 // A chunk ending with a byte matching 0xc0 is an invalid chunk unless 415 // it is a super frame index. If the last byte of real video compression 416 // data is 0xc0 the encoder must add a 0 byte. If we have the marker but 417 // not the associated matching marker byte at the front of the index we have 418 // an invalid bitstream and need to return an error. 419 420 uint8_t marker; 421 422 assert(data_sz); 423 marker = read_marker(decrypt_cb, decrypt_state, data + data_sz - 1); 424 *count = 0; 425 426 if ((marker & 0xe0) == 0xc0) { 427 const uint32_t frames = (marker & 0x7) + 1; 428 const uint32_t mag = ((marker >> 3) & 0x3) + 1; 429 const size_t index_sz = 2 + mag * frames; 430 431 // This chunk is marked as having a superframe index but doesn't have 432 // enough data for it, thus it's an invalid superframe index. 433 if (data_sz < index_sz) return VPX_CODEC_CORRUPT_FRAME; 434 435 { 436 const uint8_t marker2 = 437 read_marker(decrypt_cb, decrypt_state, data + data_sz - index_sz); 438 439 // This chunk is marked as having a superframe index but doesn't have 440 // the matching marker byte at the front of the index therefore it's an 441 // invalid chunk. 442 if (marker != marker2) return VPX_CODEC_CORRUPT_FRAME; 443 } 444 445 { 446 // Found a valid superframe index. 447 uint32_t i, j; 448 const uint8_t *x = &data[data_sz - index_sz + 1]; 449 450 // Frames has a maximum of 8 and mag has a maximum of 4. 451 uint8_t clear_buffer[32]; 452 assert(sizeof(clear_buffer) >= frames * mag); 453 if (decrypt_cb) { 454 decrypt_cb(decrypt_state, x, clear_buffer, frames * mag); 455 x = clear_buffer; 456 } 457 458 for (i = 0; i < frames; ++i) { 459 uint32_t this_sz = 0; 460 461 for (j = 0; j < mag; ++j) this_sz |= ((uint32_t)(*x++)) << (j * 8); 462 sizes[i] = this_sz; 463 } 464 *count = frames; 465 } 466 } 467 return VPX_CODEC_OK; 468 } 469