vp9dec.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*
  2. * VP9 compatible video decoder
  3. *
  4. * Copyright (C) 2013 Ronald S. Bultje <rsbultje gmail com>
  5. * Copyright (C) 2013 Clément Bœsch <u pkh me>
  6. *
  7. * This file is part of FFmpeg.
  8. *
  9. * FFmpeg is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2.1 of the License, or (at your option) any later version.
  13. *
  14. * FFmpeg is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with FFmpeg; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. */
  23. #ifndef AVCODEC_VP9DEC_H
  24. #define AVCODEC_VP9DEC_H
  25. #include <stddef.h>
  26. #include <stdint.h>
  27. #include <stdatomic.h>
  28. #include "libavutil/buffer.h"
  29. #include "libavutil/thread.h"
  30. #include "libavutil/internal.h"
  31. #include "vp9.h"
  32. #include "vp9dsp.h"
  33. #include "vp9shared.h"
  34. enum MVJoint {
  35. MV_JOINT_ZERO,
  36. MV_JOINT_H,
  37. MV_JOINT_V,
  38. MV_JOINT_HV,
  39. };
  40. typedef struct ProbContext {
  41. uint8_t y_mode[4][9];
  42. uint8_t uv_mode[10][9];
  43. uint8_t filter[4][2];
  44. uint8_t mv_mode[7][3];
  45. uint8_t intra[4];
  46. uint8_t comp[5];
  47. uint8_t single_ref[5][2];
  48. uint8_t comp_ref[5];
  49. uint8_t tx32p[2][3];
  50. uint8_t tx16p[2][2];
  51. uint8_t tx8p[2];
  52. uint8_t skip[3];
  53. uint8_t mv_joint[3];
  54. struct {
  55. uint8_t sign;
  56. uint8_t classes[10];
  57. uint8_t class0;
  58. uint8_t bits[10];
  59. uint8_t class0_fp[2][3];
  60. uint8_t fp[3];
  61. uint8_t class0_hp;
  62. uint8_t hp;
  63. } mv_comp[2];
  64. uint8_t partition[4][4][3];
  65. } ProbContext;
  66. typedef struct VP9Filter {
  67. uint8_t level[8 * 8];
  68. uint8_t /* bit=col */ mask[2 /* 0=y, 1=uv */][2 /* 0=col, 1=row */]
  69. [8 /* rows */][4 /* 0=16, 1=8, 2=4, 3=inner4 */];
  70. } VP9Filter;
  71. typedef struct VP9Block {
  72. uint8_t seg_id, intra, comp, ref[2], mode[4], uvmode, skip;
  73. enum FilterMode filter;
  74. VP56mv mv[4 /* b_idx */][2 /* ref */];
  75. enum BlockSize bs;
  76. enum TxfmMode tx, uvtx;
  77. enum BlockLevel bl;
  78. enum BlockPartition bp;
  79. } VP9Block;
  80. typedef struct VP9TileData VP9TileData;
  81. typedef struct VP9Context {
  82. VP9SharedContext s;
  83. VP9TileData *td;
  84. VP9DSPContext dsp;
  85. VideoDSPContext vdsp;
  86. GetBitContext gb;
  87. VP56RangeCoder c;
  88. int pass, active_tile_cols;
  89. #if HAVE_THREADS
  90. pthread_mutex_t progress_mutex;
  91. pthread_cond_t progress_cond;
  92. atomic_int *entries;
  93. #endif
  94. uint8_t ss_h, ss_v;
  95. uint8_t last_bpp, bpp_index, bytesperpixel;
  96. uint8_t last_keyframe;
  97. // sb_cols/rows, rows/cols and last_fmt are used for allocating all internal
  98. // arrays, and are thus per-thread. w/h and gf_fmt are synced between threads
  99. // and are therefore per-stream. pix_fmt represents the value in the header
  100. // of the currently processed frame.
  101. int w, h;
  102. enum AVPixelFormat pix_fmt, last_fmt, gf_fmt;
  103. unsigned sb_cols, sb_rows, rows, cols;
  104. ThreadFrame next_refs[8];
  105. struct {
  106. uint8_t lim_lut[64];
  107. uint8_t mblim_lut[64];
  108. } filter_lut;
  109. struct {
  110. ProbContext p;
  111. uint8_t coef[4][2][2][6][6][3];
  112. } prob_ctx[4];
  113. struct {
  114. ProbContext p;
  115. uint8_t coef[4][2][2][6][6][11];
  116. } prob;
  117. // contextual (above) cache
  118. uint8_t *above_partition_ctx;
  119. uint8_t *above_mode_ctx;
  120. // FIXME maybe merge some of the below in a flags field?
  121. uint8_t *above_y_nnz_ctx;
  122. uint8_t *above_uv_nnz_ctx[2];
  123. uint8_t *above_skip_ctx; // 1bit
  124. uint8_t *above_txfm_ctx; // 2bit
  125. uint8_t *above_segpred_ctx; // 1bit
  126. uint8_t *above_intra_ctx; // 1bit
  127. uint8_t *above_comp_ctx; // 1bit
  128. uint8_t *above_ref_ctx; // 2bit
  129. uint8_t *above_filter_ctx;
  130. VP56mv (*above_mv_ctx)[2];
  131. // whole-frame cache
  132. uint8_t *intra_pred_data[3];
  133. VP9Filter *lflvl;
  134. // block reconstruction intermediates
  135. int block_alloc_using_2pass;
  136. uint16_t mvscale[3][2];
  137. uint8_t mvstep[3][2];
  138. // frame specific buffer pools
  139. AVBufferPool *frame_extradata_pool;
  140. int frame_extradata_pool_size;
  141. } VP9Context;
  142. struct VP9TileData {
  143. //VP9Context should be const, but because of the threading API(generates
  144. //a lot of warnings) it's not.
  145. VP9Context *s;
  146. VP56RangeCoder *c_b;
  147. VP56RangeCoder *c;
  148. int row, row7, col, col7;
  149. uint8_t *dst[3];
  150. ptrdiff_t y_stride, uv_stride;
  151. VP9Block *b_base, *b;
  152. unsigned tile_col_start;
  153. struct {
  154. unsigned y_mode[4][10];
  155. unsigned uv_mode[10][10];
  156. unsigned filter[4][3];
  157. unsigned mv_mode[7][4];
  158. unsigned intra[4][2];
  159. unsigned comp[5][2];
  160. unsigned single_ref[5][2][2];
  161. unsigned comp_ref[5][2];
  162. unsigned tx32p[2][4];
  163. unsigned tx16p[2][3];
  164. unsigned tx8p[2][2];
  165. unsigned skip[3][2];
  166. unsigned mv_joint[4];
  167. struct {
  168. unsigned sign[2];
  169. unsigned classes[11];
  170. unsigned class0[2];
  171. unsigned bits[10][2];
  172. unsigned class0_fp[2][4];
  173. unsigned fp[4];
  174. unsigned class0_hp[2];
  175. unsigned hp[2];
  176. } mv_comp[2];
  177. unsigned partition[4][4][4];
  178. unsigned coef[4][2][2][6][6][3];
  179. unsigned eob[4][2][2][6][6][2];
  180. } counts;
  181. // whole-frame cache
  182. DECLARE_ALIGNED(32, uint8_t, edge_emu_buffer)[135 * 144 * 2];
  183. // contextual (left) cache
  184. DECLARE_ALIGNED(16, uint8_t, left_y_nnz_ctx)[16];
  185. DECLARE_ALIGNED(16, uint8_t, left_mode_ctx)[16];
  186. DECLARE_ALIGNED(16, VP56mv, left_mv_ctx)[16][2];
  187. DECLARE_ALIGNED(16, uint8_t, left_uv_nnz_ctx)[2][16];
  188. DECLARE_ALIGNED(8, uint8_t, left_partition_ctx)[8];
  189. DECLARE_ALIGNED(8, uint8_t, left_skip_ctx)[8];
  190. DECLARE_ALIGNED(8, uint8_t, left_txfm_ctx)[8];
  191. DECLARE_ALIGNED(8, uint8_t, left_segpred_ctx)[8];
  192. DECLARE_ALIGNED(8, uint8_t, left_intra_ctx)[8];
  193. DECLARE_ALIGNED(8, uint8_t, left_comp_ctx)[8];
  194. DECLARE_ALIGNED(8, uint8_t, left_ref_ctx)[8];
  195. DECLARE_ALIGNED(8, uint8_t, left_filter_ctx)[8];
  196. // block reconstruction intermediates
  197. DECLARE_ALIGNED(32, uint8_t, tmp_y)[64 * 64 * 2];
  198. DECLARE_ALIGNED(32, uint8_t, tmp_uv)[2][64 * 64 * 2];
  199. struct { int x, y; } min_mv, max_mv;
  200. int16_t *block_base, *block, *uvblock_base[2], *uvblock[2];
  201. uint8_t *eob_base, *uveob_base[2], *eob, *uveob[2];
  202. };
  203. void ff_vp9_fill_mv(VP9TileData *td, VP56mv *mv, int mode, int sb);
  204. void ff_vp9_adapt_probs(VP9Context *s);
  205. void ff_vp9_decode_block(VP9TileData *td, int row, int col,
  206. VP9Filter *lflvl, ptrdiff_t yoff, ptrdiff_t uvoff,
  207. enum BlockLevel bl, enum BlockPartition bp);
  208. void ff_vp9_loopfilter_sb(AVCodecContext *avctx, VP9Filter *lflvl,
  209. int row, int col, ptrdiff_t yoff, ptrdiff_t uvoff);
  210. void ff_vp9_intra_recon_8bpp(VP9TileData *td,
  211. ptrdiff_t y_off, ptrdiff_t uv_off);
  212. void ff_vp9_intra_recon_16bpp(VP9TileData *td,
  213. ptrdiff_t y_off, ptrdiff_t uv_off);
  214. void ff_vp9_inter_recon_8bpp(VP9TileData *td);
  215. void ff_vp9_inter_recon_16bpp(VP9TileData *td);
  216. #endif /* AVCODEC_VP9DEC_H */