onyxd_if.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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 "vp8/common/onyxc_int.h"
  11. #if CONFIG_POSTPROC
  12. #include "vp8/common/postproc.h"
  13. #endif
  14. #include "vp8/common/onyxd.h"
  15. #include "onyxd_int.h"
  16. #include "vpx_mem/vpx_mem.h"
  17. #include "vp8/common/alloccommon.h"
  18. #include "vp8/common/loopfilter.h"
  19. #include "vp8/common/swapyv12buffer.h"
  20. #include "vp8/common/threading.h"
  21. #include "decoderthreading.h"
  22. #include <stdio.h>
  23. #include <assert.h>
  24. #include "vp8/common/quant_common.h"
  25. #include "vp8/common/reconintra.h"
  26. #include "./vpx_dsp_rtcd.h"
  27. #include "./vpx_scale_rtcd.h"
  28. #include "vpx_scale/vpx_scale.h"
  29. #include "vp8/common/systemdependent.h"
  30. #include "vpx_ports/vpx_once.h"
  31. #include "vpx_ports/vpx_timer.h"
  32. #include "detokenize.h"
  33. #if CONFIG_ERROR_CONCEALMENT
  34. #include "error_concealment.h"
  35. #endif
  36. #if ARCH_ARM
  37. #include "vpx_ports/arm.h"
  38. #endif
  39. extern void vp8_init_loop_filter(VP8_COMMON *cm);
  40. extern void vp8cx_init_de_quantizer(VP8D_COMP *pbi);
  41. static int get_free_fb(VP8_COMMON *cm);
  42. static void ref_cnt_fb(int *buf, int *idx, int new_idx);
  43. static void initialize_dec(void) {
  44. static volatile int init_done = 0;
  45. if (!init_done) {
  46. vpx_dsp_rtcd();
  47. vp8_init_intra_predictors();
  48. init_done = 1;
  49. }
  50. }
  51. static void remove_decompressor(VP8D_COMP *pbi) {
  52. #if CONFIG_ERROR_CONCEALMENT
  53. vp8_de_alloc_overlap_lists(pbi);
  54. #endif
  55. vp8_remove_common(&pbi->common);
  56. vpx_free(pbi);
  57. }
  58. static struct VP8D_COMP *create_decompressor(VP8D_CONFIG *oxcf) {
  59. VP8D_COMP *pbi = vpx_memalign(32, sizeof(VP8D_COMP));
  60. if (!pbi) return NULL;
  61. memset(pbi, 0, sizeof(VP8D_COMP));
  62. if (setjmp(pbi->common.error.jmp)) {
  63. pbi->common.error.setjmp = 0;
  64. remove_decompressor(pbi);
  65. return 0;
  66. }
  67. pbi->common.error.setjmp = 1;
  68. vp8_create_common(&pbi->common);
  69. pbi->common.current_video_frame = 0;
  70. pbi->ready_for_new_data = 1;
  71. /* vp8cx_init_de_quantizer() is first called here. Add check in
  72. * frame_init_dequantizer() to avoid
  73. * unnecessary calling of vp8cx_init_de_quantizer() for every frame.
  74. */
  75. vp8cx_init_de_quantizer(pbi);
  76. vp8_loop_filter_init(&pbi->common);
  77. pbi->common.error.setjmp = 0;
  78. #if CONFIG_ERROR_CONCEALMENT
  79. pbi->ec_enabled = oxcf->error_concealment;
  80. pbi->overlaps = NULL;
  81. #else
  82. (void)oxcf;
  83. pbi->ec_enabled = 0;
  84. #endif
  85. /* Error concealment is activated after a key frame has been
  86. * decoded without errors when error concealment is enabled.
  87. */
  88. pbi->ec_active = 0;
  89. pbi->decoded_key_frame = 0;
  90. /* Independent partitions is activated when a frame updates the
  91. * token probability table to have equal probabilities over the
  92. * PREV_COEF context.
  93. */
  94. pbi->independent_partitions = 0;
  95. vp8_setup_block_dptrs(&pbi->mb);
  96. once(initialize_dec);
  97. return pbi;
  98. }
  99. vpx_codec_err_t vp8dx_get_reference(VP8D_COMP *pbi,
  100. enum vpx_ref_frame_type ref_frame_flag,
  101. YV12_BUFFER_CONFIG *sd) {
  102. VP8_COMMON *cm = &pbi->common;
  103. int ref_fb_idx;
  104. if (ref_frame_flag == VP8_LAST_FRAME) {
  105. ref_fb_idx = cm->lst_fb_idx;
  106. } else if (ref_frame_flag == VP8_GOLD_FRAME) {
  107. ref_fb_idx = cm->gld_fb_idx;
  108. } else if (ref_frame_flag == VP8_ALTR_FRAME) {
  109. ref_fb_idx = cm->alt_fb_idx;
  110. } else {
  111. vpx_internal_error(&pbi->common.error, VPX_CODEC_ERROR,
  112. "Invalid reference frame");
  113. return pbi->common.error.error_code;
  114. }
  115. if (cm->yv12_fb[ref_fb_idx].y_height != sd->y_height ||
  116. cm->yv12_fb[ref_fb_idx].y_width != sd->y_width ||
  117. cm->yv12_fb[ref_fb_idx].uv_height != sd->uv_height ||
  118. cm->yv12_fb[ref_fb_idx].uv_width != sd->uv_width) {
  119. vpx_internal_error(&pbi->common.error, VPX_CODEC_ERROR,
  120. "Incorrect buffer dimensions");
  121. } else
  122. vp8_yv12_copy_frame(&cm->yv12_fb[ref_fb_idx], sd);
  123. return pbi->common.error.error_code;
  124. }
  125. vpx_codec_err_t vp8dx_set_reference(VP8D_COMP *pbi,
  126. enum vpx_ref_frame_type ref_frame_flag,
  127. YV12_BUFFER_CONFIG *sd) {
  128. VP8_COMMON *cm = &pbi->common;
  129. int *ref_fb_ptr = NULL;
  130. int free_fb;
  131. if (ref_frame_flag == VP8_LAST_FRAME) {
  132. ref_fb_ptr = &cm->lst_fb_idx;
  133. } else if (ref_frame_flag == VP8_GOLD_FRAME) {
  134. ref_fb_ptr = &cm->gld_fb_idx;
  135. } else if (ref_frame_flag == VP8_ALTR_FRAME) {
  136. ref_fb_ptr = &cm->alt_fb_idx;
  137. } else {
  138. vpx_internal_error(&pbi->common.error, VPX_CODEC_ERROR,
  139. "Invalid reference frame");
  140. return pbi->common.error.error_code;
  141. }
  142. if (cm->yv12_fb[*ref_fb_ptr].y_height != sd->y_height ||
  143. cm->yv12_fb[*ref_fb_ptr].y_width != sd->y_width ||
  144. cm->yv12_fb[*ref_fb_ptr].uv_height != sd->uv_height ||
  145. cm->yv12_fb[*ref_fb_ptr].uv_width != sd->uv_width) {
  146. vpx_internal_error(&pbi->common.error, VPX_CODEC_ERROR,
  147. "Incorrect buffer dimensions");
  148. } else {
  149. /* Find an empty frame buffer. */
  150. free_fb = get_free_fb(cm);
  151. /* Decrease fb_idx_ref_cnt since it will be increased again in
  152. * ref_cnt_fb() below. */
  153. cm->fb_idx_ref_cnt[free_fb]--;
  154. /* Manage the reference counters and copy image. */
  155. ref_cnt_fb(cm->fb_idx_ref_cnt, ref_fb_ptr, free_fb);
  156. vp8_yv12_copy_frame(sd, &cm->yv12_fb[*ref_fb_ptr]);
  157. }
  158. return pbi->common.error.error_code;
  159. }
  160. static int get_free_fb(VP8_COMMON *cm) {
  161. int i;
  162. for (i = 0; i < NUM_YV12_BUFFERS; ++i) {
  163. if (cm->fb_idx_ref_cnt[i] == 0) break;
  164. }
  165. assert(i < NUM_YV12_BUFFERS);
  166. cm->fb_idx_ref_cnt[i] = 1;
  167. return i;
  168. }
  169. static void ref_cnt_fb(int *buf, int *idx, int new_idx) {
  170. if (buf[*idx] > 0) buf[*idx]--;
  171. *idx = new_idx;
  172. buf[new_idx]++;
  173. }
  174. /* If any buffer copy / swapping is signalled it should be done here. */
  175. static int swap_frame_buffers(VP8_COMMON *cm) {
  176. int err = 0;
  177. /* The alternate reference frame or golden frame can be updated
  178. * using the new, last, or golden/alt ref frame. If it
  179. * is updated using the newly decoded frame it is a refresh.
  180. * An update using the last or golden/alt ref frame is a copy.
  181. */
  182. if (cm->copy_buffer_to_arf) {
  183. int new_fb = 0;
  184. if (cm->copy_buffer_to_arf == 1) {
  185. new_fb = cm->lst_fb_idx;
  186. } else if (cm->copy_buffer_to_arf == 2) {
  187. new_fb = cm->gld_fb_idx;
  188. } else {
  189. err = -1;
  190. }
  191. ref_cnt_fb(cm->fb_idx_ref_cnt, &cm->alt_fb_idx, new_fb);
  192. }
  193. if (cm->copy_buffer_to_gf) {
  194. int new_fb = 0;
  195. if (cm->copy_buffer_to_gf == 1) {
  196. new_fb = cm->lst_fb_idx;
  197. } else if (cm->copy_buffer_to_gf == 2) {
  198. new_fb = cm->alt_fb_idx;
  199. } else {
  200. err = -1;
  201. }
  202. ref_cnt_fb(cm->fb_idx_ref_cnt, &cm->gld_fb_idx, new_fb);
  203. }
  204. if (cm->refresh_golden_frame) {
  205. ref_cnt_fb(cm->fb_idx_ref_cnt, &cm->gld_fb_idx, cm->new_fb_idx);
  206. }
  207. if (cm->refresh_alt_ref_frame) {
  208. ref_cnt_fb(cm->fb_idx_ref_cnt, &cm->alt_fb_idx, cm->new_fb_idx);
  209. }
  210. if (cm->refresh_last_frame) {
  211. ref_cnt_fb(cm->fb_idx_ref_cnt, &cm->lst_fb_idx, cm->new_fb_idx);
  212. cm->frame_to_show = &cm->yv12_fb[cm->lst_fb_idx];
  213. } else {
  214. cm->frame_to_show = &cm->yv12_fb[cm->new_fb_idx];
  215. }
  216. cm->fb_idx_ref_cnt[cm->new_fb_idx]--;
  217. return err;
  218. }
  219. static int check_fragments_for_errors(VP8D_COMP *pbi) {
  220. if (!pbi->ec_active && pbi->fragments.count <= 1 &&
  221. pbi->fragments.sizes[0] == 0) {
  222. VP8_COMMON *cm = &pbi->common;
  223. /* If error concealment is disabled we won't signal missing frames
  224. * to the decoder.
  225. */
  226. if (cm->fb_idx_ref_cnt[cm->lst_fb_idx] > 1) {
  227. /* The last reference shares buffer with another reference
  228. * buffer. Move it to its own buffer before setting it as
  229. * corrupt, otherwise we will make multiple buffers corrupt.
  230. */
  231. const int prev_idx = cm->lst_fb_idx;
  232. cm->fb_idx_ref_cnt[prev_idx]--;
  233. cm->lst_fb_idx = get_free_fb(cm);
  234. vp8_yv12_copy_frame(&cm->yv12_fb[prev_idx], &cm->yv12_fb[cm->lst_fb_idx]);
  235. }
  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. cm->yv12_fb[cm->lst_fb_idx].corrupted = 1;
  242. /* Signal that we have no frame to show. */
  243. cm->show_frame = 0;
  244. /* Nothing more to do. */
  245. return 0;
  246. }
  247. return 1;
  248. }
  249. int vp8dx_receive_compressed_data(VP8D_COMP *pbi, size_t size,
  250. const uint8_t *source, int64_t time_stamp) {
  251. VP8_COMMON *cm = &pbi->common;
  252. int retcode = -1;
  253. (void)size;
  254. (void)source;
  255. pbi->common.error.error_code = VPX_CODEC_OK;
  256. retcode = check_fragments_for_errors(pbi);
  257. if (retcode <= 0) return retcode;
  258. cm->new_fb_idx = get_free_fb(cm);
  259. /* setup reference frames for vp8_decode_frame */
  260. pbi->dec_fb_ref[INTRA_FRAME] = &cm->yv12_fb[cm->new_fb_idx];
  261. pbi->dec_fb_ref[LAST_FRAME] = &cm->yv12_fb[cm->lst_fb_idx];
  262. pbi->dec_fb_ref[GOLDEN_FRAME] = &cm->yv12_fb[cm->gld_fb_idx];
  263. pbi->dec_fb_ref[ALTREF_FRAME] = &cm->yv12_fb[cm->alt_fb_idx];
  264. if (setjmp(pbi->common.error.jmp)) {
  265. /* We do not know if the missing frame(s) was supposed to update
  266. * any of the reference buffers, but we act conservative and
  267. * mark only the last buffer as corrupted.
  268. */
  269. cm->yv12_fb[cm->lst_fb_idx].corrupted = 1;
  270. if (cm->fb_idx_ref_cnt[cm->new_fb_idx] > 0) {
  271. cm->fb_idx_ref_cnt[cm->new_fb_idx]--;
  272. }
  273. goto decode_exit;
  274. }
  275. pbi->common.error.setjmp = 1;
  276. retcode = vp8_decode_frame(pbi);
  277. if (retcode < 0) {
  278. if (cm->fb_idx_ref_cnt[cm->new_fb_idx] > 0) {
  279. cm->fb_idx_ref_cnt[cm->new_fb_idx]--;
  280. }
  281. pbi->common.error.error_code = VPX_CODEC_ERROR;
  282. goto decode_exit;
  283. }
  284. if (swap_frame_buffers(cm)) {
  285. pbi->common.error.error_code = VPX_CODEC_ERROR;
  286. goto decode_exit;
  287. }
  288. vp8_clear_system_state();
  289. if (cm->show_frame) {
  290. cm->current_video_frame++;
  291. cm->show_frame_mi = cm->mi;
  292. }
  293. #if CONFIG_ERROR_CONCEALMENT
  294. /* swap the mode infos to storage for future error concealment */
  295. if (pbi->ec_enabled && pbi->common.prev_mi) {
  296. MODE_INFO *tmp = pbi->common.prev_mi;
  297. int row, col;
  298. pbi->common.prev_mi = pbi->common.mi;
  299. pbi->common.mi = tmp;
  300. /* Propagate the segment_ids to the next frame */
  301. for (row = 0; row < pbi->common.mb_rows; ++row) {
  302. for (col = 0; col < pbi->common.mb_cols; ++col) {
  303. const int i = row * pbi->common.mode_info_stride + col;
  304. pbi->common.mi[i].mbmi.segment_id =
  305. pbi->common.prev_mi[i].mbmi.segment_id;
  306. }
  307. }
  308. }
  309. #endif
  310. pbi->ready_for_new_data = 0;
  311. pbi->last_time_stamp = time_stamp;
  312. decode_exit:
  313. pbi->common.error.setjmp = 0;
  314. vp8_clear_system_state();
  315. return retcode;
  316. }
  317. int vp8dx_get_raw_frame(VP8D_COMP *pbi, YV12_BUFFER_CONFIG *sd,
  318. int64_t *time_stamp, int64_t *time_end_stamp,
  319. vp8_ppflags_t *flags) {
  320. int ret = -1;
  321. if (pbi->ready_for_new_data == 1) return ret;
  322. /* ie no raw frame to show!!! */
  323. if (pbi->common.show_frame == 0) return ret;
  324. pbi->ready_for_new_data = 1;
  325. *time_stamp = pbi->last_time_stamp;
  326. *time_end_stamp = 0;
  327. #if CONFIG_POSTPROC
  328. ret = vp8_post_proc_frame(&pbi->common, sd, flags);
  329. #else
  330. (void)flags;
  331. if (pbi->common.frame_to_show) {
  332. *sd = *pbi->common.frame_to_show;
  333. sd->y_width = pbi->common.Width;
  334. sd->y_height = pbi->common.Height;
  335. sd->uv_height = pbi->common.Height / 2;
  336. ret = 0;
  337. } else {
  338. ret = -1;
  339. }
  340. #endif /*!CONFIG_POSTPROC*/
  341. vp8_clear_system_state();
  342. return ret;
  343. }
  344. /* This function as written isn't decoder specific, but the encoder has
  345. * much faster ways of computing this, so it's ok for it to live in a
  346. * decode specific file.
  347. */
  348. int vp8dx_references_buffer(VP8_COMMON *oci, int ref_frame) {
  349. const MODE_INFO *mi = oci->mi;
  350. int mb_row, mb_col;
  351. for (mb_row = 0; mb_row < oci->mb_rows; ++mb_row) {
  352. for (mb_col = 0; mb_col < oci->mb_cols; mb_col++, mi++) {
  353. if (mi->mbmi.ref_frame == ref_frame) return 1;
  354. }
  355. mi++;
  356. }
  357. return 0;
  358. }
  359. int vp8_create_decoder_instances(struct frame_buffers *fb, VP8D_CONFIG *oxcf) {
  360. if (!fb->use_frame_threads) {
  361. /* decoder instance for single thread mode */
  362. fb->pbi[0] = create_decompressor(oxcf);
  363. if (!fb->pbi[0]) return VPX_CODEC_ERROR;
  364. #if CONFIG_MULTITHREAD
  365. if (setjmp(fb->pbi[0]->common.error.jmp)) {
  366. vp8_remove_decoder_instances(fb);
  367. memset(fb->pbi, 0, sizeof(fb->pbi) / sizeof(fb->pbi[0]));
  368. vp8_clear_system_state();
  369. return VPX_CODEC_ERROR;
  370. }
  371. fb->pbi[0]->common.error.setjmp = 1;
  372. fb->pbi[0]->max_threads = oxcf->max_threads;
  373. vp8_decoder_create_threads(fb->pbi[0]);
  374. fb->pbi[0]->common.error.setjmp = 0;
  375. #endif
  376. } else {
  377. /* TODO : create frame threads and decoder instances for each
  378. * thread here */
  379. }
  380. return VPX_CODEC_OK;
  381. }
  382. int vp8_remove_decoder_instances(struct frame_buffers *fb) {
  383. if (!fb->use_frame_threads) {
  384. VP8D_COMP *pbi = fb->pbi[0];
  385. if (!pbi) return VPX_CODEC_ERROR;
  386. #if CONFIG_MULTITHREAD
  387. vp8_decoder_remove_threads(pbi);
  388. #endif
  389. /* decoder instance for single thread mode */
  390. remove_decompressor(pbi);
  391. } else {
  392. /* TODO : remove frame threads and decoder instances for each
  393. * thread here */
  394. }
  395. return VPX_CODEC_OK;
  396. }