vp9_decoder.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  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)
  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. vpx_get_worker_interface()->init(&pbi->lf_worker);
  98. return pbi;
  99. }
  100. void vp9_decoder_remove(VP9Decoder *pbi) {
  101. int i;
  102. if (!pbi)
  103. return;
  104. vpx_get_worker_interface()->end(&pbi->lf_worker);
  105. vpx_free(pbi->lf_worker.data1);
  106. for (i = 0; i < pbi->num_tile_workers; ++i) {
  107. VPxWorker *const worker = &pbi->tile_workers[i];
  108. vpx_get_worker_interface()->end(worker);
  109. }
  110. vpx_free(pbi->tile_worker_data);
  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. vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
  178. "Unable to find free frame buffer");
  179. return cm->error.error_code;
  180. }
  181. // Decrease ref_count since it will be increased again in
  182. // ref_cnt_fb() below.
  183. --frame_bufs[free_fb].ref_count;
  184. // Manage the reference counters and copy image.
  185. ref_cnt_fb(frame_bufs, ref_fb_ptr, free_fb);
  186. ref_buf->buf = &frame_bufs[*ref_fb_ptr].buf;
  187. vp8_yv12_copy_frame(sd, ref_buf->buf);
  188. }
  189. return cm->error.error_code;
  190. }
  191. /* If any buffer updating is signaled it should be done here. */
  192. static void swap_frame_buffers(VP9Decoder *pbi) {
  193. int ref_index = 0, mask;
  194. VP9_COMMON *const cm = &pbi->common;
  195. BufferPool *const pool = cm->buffer_pool;
  196. RefCntBuffer *const frame_bufs = cm->buffer_pool->frame_bufs;
  197. lock_buffer_pool(pool);
  198. for (mask = pbi->refresh_frame_flags; mask; mask >>= 1) {
  199. const int old_idx = cm->ref_frame_map[ref_index];
  200. // Current thread releases the holding of reference frame.
  201. decrease_ref_count(old_idx, frame_bufs, pool);
  202. // Release the reference frame in reference map.
  203. if (mask & 1) {
  204. decrease_ref_count(old_idx, frame_bufs, pool);
  205. }
  206. cm->ref_frame_map[ref_index] = cm->next_ref_frame_map[ref_index];
  207. ++ref_index;
  208. }
  209. // Current thread releases the holding of reference frame.
  210. for (; ref_index < REF_FRAMES && !cm->show_existing_frame; ++ref_index) {
  211. const int old_idx = cm->ref_frame_map[ref_index];
  212. decrease_ref_count(old_idx, frame_bufs, pool);
  213. cm->ref_frame_map[ref_index] = cm->next_ref_frame_map[ref_index];
  214. }
  215. unlock_buffer_pool(pool);
  216. pbi->hold_ref_buf = 0;
  217. cm->frame_to_show = get_frame_new_buffer(cm);
  218. if (!pbi->frame_parallel_decode || !cm->show_frame) {
  219. lock_buffer_pool(pool);
  220. --frame_bufs[cm->new_fb_idx].ref_count;
  221. unlock_buffer_pool(pool);
  222. }
  223. // Invalidate these references until the next frame starts.
  224. for (ref_index = 0; ref_index < 3; ref_index++)
  225. cm->frame_refs[ref_index].idx = -1;
  226. }
  227. int vp9_receive_compressed_data(VP9Decoder *pbi,
  228. size_t size, const uint8_t **psource) {
  229. VP9_COMMON *volatile const cm = &pbi->common;
  230. BufferPool *volatile const pool = cm->buffer_pool;
  231. RefCntBuffer *volatile const frame_bufs = cm->buffer_pool->frame_bufs;
  232. const uint8_t *source = *psource;
  233. int retcode = 0;
  234. cm->error.error_code = VPX_CODEC_OK;
  235. if (size == 0) {
  236. // This is used to signal that we are missing frames.
  237. // We do not know if the missing frame(s) was supposed to update
  238. // any of the reference buffers, but we act conservative and
  239. // mark only the last buffer as corrupted.
  240. //
  241. // TODO(jkoleszar): Error concealment is undefined and non-normative
  242. // at this point, but if it becomes so, [0] may not always be the correct
  243. // thing to do here.
  244. if (cm->frame_refs[0].idx > 0) {
  245. assert(cm->frame_refs[0].buf != NULL);
  246. cm->frame_refs[0].buf->corrupted = 1;
  247. }
  248. }
  249. pbi->ready_for_new_data = 0;
  250. // Check if the previous frame was a frame without any references to it.
  251. // Release frame buffer if not decoding in frame parallel mode.
  252. if (!pbi->frame_parallel_decode && cm->new_fb_idx >= 0
  253. && frame_bufs[cm->new_fb_idx].ref_count == 0)
  254. pool->release_fb_cb(pool->cb_priv,
  255. &frame_bufs[cm->new_fb_idx].raw_frame_buffer);
  256. // Find a free frame buffer. Return error if can not find any.
  257. cm->new_fb_idx = get_free_fb(cm);
  258. if (cm->new_fb_idx == INVALID_IDX) {
  259. vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
  260. "Unable to find free frame buffer");
  261. return cm->error.error_code;
  262. }
  263. // Assign a MV array to the frame buffer.
  264. cm->cur_frame = &pool->frame_bufs[cm->new_fb_idx];
  265. pbi->hold_ref_buf = 0;
  266. if (pbi->frame_parallel_decode) {
  267. VPxWorker *const worker = pbi->frame_worker_owner;
  268. vp9_frameworker_lock_stats(worker);
  269. frame_bufs[cm->new_fb_idx].frame_worker_owner = worker;
  270. // Reset decoding progress.
  271. pbi->cur_buf = &frame_bufs[cm->new_fb_idx];
  272. pbi->cur_buf->row = -1;
  273. pbi->cur_buf->col = -1;
  274. vp9_frameworker_unlock_stats(worker);
  275. } else {
  276. pbi->cur_buf = &frame_bufs[cm->new_fb_idx];
  277. }
  278. if (setjmp(cm->error.jmp)) {
  279. const VPxWorkerInterface *const winterface = vpx_get_worker_interface();
  280. int i;
  281. cm->error.setjmp = 0;
  282. pbi->ready_for_new_data = 1;
  283. // Synchronize all threads immediately as a subsequent decode call may
  284. // cause a resize invalidating some allocations.
  285. winterface->sync(&pbi->lf_worker);
  286. for (i = 0; i < pbi->num_tile_workers; ++i) {
  287. winterface->sync(&pbi->tile_workers[i]);
  288. }
  289. lock_buffer_pool(pool);
  290. // Release all the reference buffers if worker thread is holding them.
  291. if (pbi->hold_ref_buf == 1) {
  292. int ref_index = 0, mask;
  293. for (mask = pbi->refresh_frame_flags; mask; mask >>= 1) {
  294. const int old_idx = cm->ref_frame_map[ref_index];
  295. // Current thread releases the holding of reference frame.
  296. decrease_ref_count(old_idx, frame_bufs, pool);
  297. // Release the reference frame in reference map.
  298. if (mask & 1) {
  299. decrease_ref_count(old_idx, frame_bufs, pool);
  300. }
  301. ++ref_index;
  302. }
  303. // Current thread releases the holding of reference frame.
  304. for (; ref_index < REF_FRAMES && !cm->show_existing_frame; ++ref_index) {
  305. const int old_idx = cm->ref_frame_map[ref_index];
  306. decrease_ref_count(old_idx, frame_bufs, pool);
  307. }
  308. pbi->hold_ref_buf = 0;
  309. }
  310. // Release current frame.
  311. decrease_ref_count(cm->new_fb_idx, frame_bufs, pool);
  312. unlock_buffer_pool(pool);
  313. vpx_clear_system_state();
  314. return -1;
  315. }
  316. cm->error.setjmp = 1;
  317. vp9_decode_frame(pbi, source, source + size, psource);
  318. swap_frame_buffers(pbi);
  319. vpx_clear_system_state();
  320. if (!cm->show_existing_frame) {
  321. cm->last_show_frame = cm->show_frame;
  322. cm->prev_frame = cm->cur_frame;
  323. if (cm->seg.enabled && !pbi->frame_parallel_decode)
  324. vp9_swap_current_and_last_seg_map(cm);
  325. }
  326. // Update progress in frame parallel decode.
  327. if (pbi->frame_parallel_decode) {
  328. // Need to lock the mutex here as another thread may
  329. // be accessing this buffer.
  330. VPxWorker *const worker = pbi->frame_worker_owner;
  331. FrameWorkerData *const frame_worker_data = worker->data1;
  332. vp9_frameworker_lock_stats(worker);
  333. if (cm->show_frame) {
  334. cm->current_video_frame++;
  335. }
  336. frame_worker_data->frame_decoded = 1;
  337. frame_worker_data->frame_context_ready = 1;
  338. vp9_frameworker_signal_stats(worker);
  339. vp9_frameworker_unlock_stats(worker);
  340. } else {
  341. cm->last_width = cm->width;
  342. cm->last_height = cm->height;
  343. if (cm->show_frame) {
  344. cm->current_video_frame++;
  345. }
  346. }
  347. cm->error.setjmp = 0;
  348. return retcode;
  349. }
  350. int vp9_get_raw_frame(VP9Decoder *pbi, YV12_BUFFER_CONFIG *sd,
  351. vp9_ppflags_t *flags) {
  352. VP9_COMMON *const cm = &pbi->common;
  353. int ret = -1;
  354. #if !CONFIG_VP9_POSTPROC
  355. (void)*flags;
  356. #endif
  357. if (pbi->ready_for_new_data == 1)
  358. return ret;
  359. pbi->ready_for_new_data = 1;
  360. /* no raw frame to show!!! */
  361. if (!cm->show_frame)
  362. return ret;
  363. pbi->ready_for_new_data = 1;
  364. #if CONFIG_VP9_POSTPROC
  365. if (!cm->show_existing_frame) {
  366. ret = vp9_post_proc_frame(cm, sd, flags);
  367. } else {
  368. *sd = *cm->frame_to_show;
  369. ret = 0;
  370. }
  371. #else
  372. *sd = *cm->frame_to_show;
  373. ret = 0;
  374. #endif /*!CONFIG_POSTPROC*/
  375. vpx_clear_system_state();
  376. return ret;
  377. }
  378. vpx_codec_err_t vp9_parse_superframe_index(const uint8_t *data,
  379. size_t data_sz,
  380. uint32_t sizes[8], int *count,
  381. vpx_decrypt_cb decrypt_cb,
  382. void *decrypt_state) {
  383. // A chunk ending with a byte matching 0xc0 is an invalid chunk unless
  384. // it is a super frame index. If the last byte of real video compression
  385. // data is 0xc0 the encoder must add a 0 byte. If we have the marker but
  386. // not the associated matching marker byte at the front of the index we have
  387. // an invalid bitstream and need to return an error.
  388. uint8_t marker;
  389. assert(data_sz);
  390. marker = read_marker(decrypt_cb, decrypt_state, data + data_sz - 1);
  391. *count = 0;
  392. if ((marker & 0xe0) == 0xc0) {
  393. const uint32_t frames = (marker & 0x7) + 1;
  394. const uint32_t mag = ((marker >> 3) & 0x3) + 1;
  395. const size_t index_sz = 2 + mag * frames;
  396. // This chunk is marked as having a superframe index but doesn't have
  397. // enough data for it, thus it's an invalid superframe index.
  398. if (data_sz < index_sz)
  399. return VPX_CODEC_CORRUPT_FRAME;
  400. {
  401. const uint8_t marker2 = read_marker(decrypt_cb, decrypt_state,
  402. data + data_sz - index_sz);
  403. // This chunk is marked as having a superframe index but doesn't have
  404. // the matching marker byte at the front of the index therefore it's an
  405. // invalid chunk.
  406. if (marker != marker2)
  407. return VPX_CODEC_CORRUPT_FRAME;
  408. }
  409. {
  410. // Found a valid superframe index.
  411. uint32_t i, j;
  412. const uint8_t *x = &data[data_sz - index_sz + 1];
  413. // Frames has a maximum of 8 and mag has a maximum of 4.
  414. uint8_t clear_buffer[32];
  415. assert(sizeof(clear_buffer) >= frames * mag);
  416. if (decrypt_cb) {
  417. decrypt_cb(decrypt_state, x, clear_buffer, frames * mag);
  418. x = clear_buffer;
  419. }
  420. for (i = 0; i < frames; ++i) {
  421. uint32_t this_sz = 0;
  422. for (j = 0; j < mag; ++j)
  423. this_sz |= ((uint32_t)(*x++)) << (j * 8);
  424. sizes[i] = this_sz;
  425. }
  426. *count = frames;
  427. }
  428. }
  429. return VPX_CODEC_OK;
  430. }