vp9_decoder.c 14 KB

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