vp9_decoder.c 16 KB

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