You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

423 lines
12KB

  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 Libav.
  8. *
  9. * Libav 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. * Libav 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 Libav; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. */
  23. #ifndef AVCODEC_VP9_H
  24. #define AVCODEC_VP9_H
  25. #include <stddef.h>
  26. #include <stdint.h>
  27. #include "libavutil/internal.h"
  28. #include "avcodec.h"
  29. #include "vp56.h"
  30. enum TxfmMode {
  31. TX_4X4,
  32. TX_8X8,
  33. TX_16X16,
  34. TX_32X32,
  35. N_TXFM_SIZES,
  36. TX_SWITCHABLE = N_TXFM_SIZES,
  37. N_TXFM_MODES
  38. };
  39. enum TxfmType {
  40. DCT_DCT,
  41. DCT_ADST,
  42. ADST_DCT,
  43. ADST_ADST,
  44. N_TXFM_TYPES
  45. };
  46. enum IntraPredMode {
  47. VERT_PRED,
  48. HOR_PRED,
  49. DC_PRED,
  50. DIAG_DOWN_LEFT_PRED,
  51. DIAG_DOWN_RIGHT_PRED,
  52. VERT_RIGHT_PRED,
  53. HOR_DOWN_PRED,
  54. VERT_LEFT_PRED,
  55. HOR_UP_PRED,
  56. TM_VP8_PRED,
  57. LEFT_DC_PRED,
  58. TOP_DC_PRED,
  59. DC_128_PRED,
  60. DC_127_PRED,
  61. DC_129_PRED,
  62. N_INTRA_PRED_MODES
  63. };
  64. enum FilterMode {
  65. FILTER_8TAP_SMOOTH,
  66. FILTER_8TAP_REGULAR,
  67. FILTER_8TAP_SHARP,
  68. FILTER_BILINEAR,
  69. FILTER_SWITCHABLE,
  70. };
  71. enum BlockPartition {
  72. PARTITION_NONE, // [ ] <-.
  73. PARTITION_H, // [-] |
  74. PARTITION_V, // [|] |
  75. PARTITION_SPLIT, // [+] --'
  76. };
  77. enum InterPredMode {
  78. NEARESTMV = 10,
  79. NEARMV = 11,
  80. ZEROMV = 12,
  81. NEWMV = 13,
  82. };
  83. enum MVJoint {
  84. MV_JOINT_ZERO,
  85. MV_JOINT_H,
  86. MV_JOINT_V,
  87. MV_JOINT_HV,
  88. };
  89. typedef struct ProbContext {
  90. uint8_t y_mode[4][9];
  91. uint8_t uv_mode[10][9];
  92. uint8_t filter[4][2];
  93. uint8_t mv_mode[7][3];
  94. uint8_t intra[4];
  95. uint8_t comp[5];
  96. uint8_t single_ref[5][2];
  97. uint8_t comp_ref[5];
  98. uint8_t tx32p[2][3];
  99. uint8_t tx16p[2][2];
  100. uint8_t tx8p[2];
  101. uint8_t skip[3];
  102. uint8_t mv_joint[3];
  103. struct {
  104. uint8_t sign;
  105. uint8_t classes[10];
  106. uint8_t class0;
  107. uint8_t bits[10];
  108. uint8_t class0_fp[2][3];
  109. uint8_t fp[3];
  110. uint8_t class0_hp;
  111. uint8_t hp;
  112. } mv_comp[2];
  113. uint8_t partition[4][4][3];
  114. } ProbContext;
  115. typedef void (*vp9_mc_func)(uint8_t *dst, const uint8_t *ref,
  116. ptrdiff_t dst_stride,
  117. ptrdiff_t ref_stride,
  118. int h, int mx, int my);
  119. typedef struct VP9DSPContext {
  120. /*
  121. * dimension 1: 0=4x4, 1=8x8, 2=16x16, 3=32x32
  122. * dimension 2: intra prediction modes
  123. *
  124. * dst/left/top is aligned by transform-size (i.e. 4, 8, 16 or 32 pixels)
  125. * stride is aligned by 16 pixels
  126. * top[-1] is top/left; top[4,7] is top-right for 4x4
  127. */
  128. // FIXME(rbultje) maybe replace left/top pointers with HAVE_TOP/
  129. // HAVE_LEFT/HAVE_TOPRIGHT flags instead, and then handle it in-place?
  130. // also needs to fit in with what h264/vp8/etc do
  131. void (*intra_pred[N_TXFM_SIZES][N_INTRA_PRED_MODES])(uint8_t *dst,
  132. ptrdiff_t stride,
  133. const uint8_t *left,
  134. const uint8_t *top);
  135. /*
  136. * dimension 1: 0=4x4, 1=8x8, 2=16x16, 3=32x32, 4=lossless (3-4=dct only)
  137. * dimension 2: 0=dct/dct, 1=dct/adst, 2=adst/dct, 3=adst/adst
  138. *
  139. * dst is aligned by transform-size (i.e. 4, 8, 16 or 32 pixels)
  140. * stride is aligned by 16 pixels
  141. * block is 16-byte aligned
  142. * eob indicates the position (+1) of the last non-zero coefficient,
  143. * in scan-order. This can be used to write faster versions, e.g. a
  144. * dc-only 4x4/8x8/16x16/32x32, or a 4x4-only (eob<10) 8x8/16x16/32x32,
  145. * etc.
  146. */
  147. // FIXME also write idct_add_block() versions for whole (inter) pred
  148. // blocks, so we can do 2 4x4s at once
  149. void (*itxfm_add[N_TXFM_SIZES + 1][N_TXFM_TYPES])(uint8_t *dst,
  150. ptrdiff_t stride,
  151. int16_t *block, int eob);
  152. /*
  153. * dimension 1: width of filter (0=4, 1=8, 2=16)
  154. * dimension 2: 0=col-edge filter (h), 1=row-edge filter (v)
  155. *
  156. * dst/stride are aligned by 8
  157. */
  158. void (*loop_filter_8[3][2])(uint8_t *dst, ptrdiff_t stride,
  159. int mb_lim, int lim, int hev_thr);
  160. /*
  161. * dimension 1: 0=col-edge filter (h), 1=row-edge filter (v)
  162. *
  163. * The width of filter is assumed to be 16; dst/stride are aligned by 16
  164. */
  165. void (*loop_filter_16[2])(uint8_t *dst, ptrdiff_t stride,
  166. int mb_lim, int lim, int hev_thr);
  167. /*
  168. * dimension 1/2: width of filter (0=4, 1=8) for each filter half
  169. * dimension 3: 0=col-edge filter (h), 1=row-edge filter (v)
  170. *
  171. * dst/stride are aligned by operation size
  172. * this basically calls loop_filter[d1][d3][0](), followed by
  173. * loop_filter[d2][d3][0]() on the next 8 pixels
  174. * mb_lim/lim/hev_thr contain two values in the lowest two bytes of the
  175. * integer.
  176. */
  177. // FIXME perhaps a mix4 that operates on 32px (for AVX2)
  178. void (*loop_filter_mix2[2][2][2])(uint8_t *dst, ptrdiff_t stride,
  179. int mb_lim, int lim, int hev_thr);
  180. /*
  181. * dimension 1: hsize (0: 64, 1: 32, 2: 16, 3: 8, 4: 4)
  182. * dimension 2: filter type (0: smooth, 1: regular, 2: sharp, 3: bilin)
  183. * dimension 3: averaging type (0: put, 1: avg)
  184. * dimension 4: x subpel interpolation (0: none, 1: 8tap/bilin)
  185. * dimension 5: y subpel interpolation (1: none, 1: 8tap/bilin)
  186. *
  187. * dst/stride are aligned by hsize
  188. */
  189. vp9_mc_func mc[5][4][2][2][2];
  190. } VP9DSPContext;
  191. enum CompPredMode {
  192. PRED_SINGLEREF,
  193. PRED_COMPREF,
  194. PRED_SWITCHABLE,
  195. };
  196. typedef struct VP9MVRefPair {
  197. VP56mv mv[2];
  198. int8_t ref[2];
  199. } VP9MVRefPair;
  200. typedef struct VP9Filter {
  201. uint8_t level[8 * 8];
  202. uint8_t /* bit=col */ mask[2 /* 0=y, 1=uv */][2 /* 0=col, 1=row */]
  203. [8 /* rows */][4 /* 0=16, 1=8, 2=4, 3=inner4 */];
  204. } VP9Filter;
  205. enum BlockLevel {
  206. BL_64X64,
  207. BL_32X32,
  208. BL_16X16,
  209. BL_8X8,
  210. };
  211. enum BlockSize {
  212. BS_64x64,
  213. BS_64x32,
  214. BS_32x64,
  215. BS_32x32,
  216. BS_32x16,
  217. BS_16x32,
  218. BS_16x16,
  219. BS_16x8,
  220. BS_8x16,
  221. BS_8x8,
  222. BS_8x4,
  223. BS_4x8,
  224. BS_4x4,
  225. N_BS_SIZES,
  226. };
  227. typedef struct VP9Block {
  228. uint8_t seg_id, intra, comp, ref[2], mode[4], uvmode, skip;
  229. enum FilterMode filter;
  230. VP56mv mv[4 /* b_idx */][2 /* ref */];
  231. enum BlockSize bs;
  232. enum TxfmMode tx, uvtx;
  233. int row, row7, col, col7;
  234. uint8_t *dst[3];
  235. ptrdiff_t y_stride, uv_stride;
  236. } VP9Block;
  237. typedef struct VP9Context {
  238. VP9DSPContext dsp;
  239. VideoDSPContext vdsp;
  240. GetBitContext gb;
  241. VP56RangeCoder c;
  242. VP56RangeCoder *c_b;
  243. unsigned c_b_size;
  244. VP9Block b;
  245. // bitstream header
  246. uint8_t profile;
  247. uint8_t keyframe, last_keyframe;
  248. uint8_t invisible;
  249. uint8_t use_last_frame_mvs;
  250. uint8_t errorres;
  251. uint8_t colorspace;
  252. uint8_t sub_x;
  253. uint8_t sub_y;
  254. uint8_t fullrange;
  255. uint8_t intraonly;
  256. uint8_t resetctx;
  257. uint8_t refreshrefmask;
  258. uint8_t highprecisionmvs;
  259. enum FilterMode filtermode;
  260. uint8_t allowcompinter;
  261. uint8_t fixcompref;
  262. uint8_t refreshctx;
  263. uint8_t parallelmode;
  264. uint8_t framectxid;
  265. uint8_t refidx[3];
  266. uint8_t signbias[3];
  267. uint8_t varcompref[2];
  268. AVFrame *refs[8];
  269. AVFrame *cur_frame;
  270. struct {
  271. uint8_t level;
  272. int8_t sharpness;
  273. uint8_t lim_lut[64];
  274. uint8_t mblim_lut[64];
  275. } filter;
  276. struct {
  277. uint8_t enabled;
  278. int8_t mode[2];
  279. int8_t ref[4];
  280. } lf_delta;
  281. uint8_t yac_qi;
  282. int8_t ydc_qdelta, uvdc_qdelta, uvac_qdelta;
  283. uint8_t lossless;
  284. struct {
  285. uint8_t enabled;
  286. uint8_t temporal;
  287. uint8_t absolute_vals;
  288. uint8_t update_map;
  289. #define MAX_SEGMENT 8
  290. struct {
  291. uint8_t q_enabled;
  292. uint8_t lf_enabled;
  293. uint8_t ref_enabled;
  294. uint8_t skip_enabled;
  295. uint8_t ref_val;
  296. int16_t q_val;
  297. int8_t lf_val;
  298. int16_t qmul[2][2];
  299. uint8_t lflvl[4][2];
  300. } feat[MAX_SEGMENT];
  301. } segmentation;
  302. struct {
  303. unsigned log2_tile_cols, log2_tile_rows;
  304. unsigned tile_cols, tile_rows;
  305. unsigned tile_row_start, tile_row_end, tile_col_start, tile_col_end;
  306. } tiling;
  307. unsigned sb_cols, sb_rows, rows, cols;
  308. struct {
  309. ProbContext p;
  310. uint8_t coef[4][2][2][6][6][3];
  311. } prob_ctx[4];
  312. struct {
  313. ProbContext p;
  314. uint8_t coef[4][2][2][6][6][11];
  315. uint8_t seg[7];
  316. uint8_t segpred[3];
  317. } prob;
  318. struct {
  319. unsigned y_mode[4][10];
  320. unsigned uv_mode[10][10];
  321. unsigned filter[4][3];
  322. unsigned mv_mode[7][4];
  323. unsigned intra[4][2];
  324. unsigned comp[5][2];
  325. unsigned single_ref[5][2][2];
  326. unsigned comp_ref[5][2];
  327. unsigned tx32p[2][4];
  328. unsigned tx16p[2][3];
  329. unsigned tx8p[2][2];
  330. unsigned skip[3][2];
  331. unsigned mv_joint[4];
  332. struct {
  333. unsigned sign[2];
  334. unsigned classes[11];
  335. unsigned class0[2];
  336. unsigned bits[10][2];
  337. unsigned class0_fp[2][4];
  338. unsigned fp[4];
  339. unsigned class0_hp[2];
  340. unsigned hp[2];
  341. } mv_comp[2];
  342. unsigned partition[4][4][4];
  343. unsigned coef[4][2][2][6][6][3];
  344. unsigned eob[4][2][2][6][6][2];
  345. } counts;
  346. enum TxfmMode txfmmode;
  347. enum CompPredMode comppredmode;
  348. // contextual (left/above) cache
  349. uint8_t left_partition_ctx[8], *above_partition_ctx;
  350. uint8_t left_mode_ctx[16], *above_mode_ctx;
  351. // FIXME maybe merge some of the below in a flags field?
  352. uint8_t left_y_nnz_ctx[16], *above_y_nnz_ctx;
  353. uint8_t left_uv_nnz_ctx[2][8], *above_uv_nnz_ctx[2];
  354. uint8_t left_skip_ctx[8], *above_skip_ctx; // 1bit
  355. uint8_t left_txfm_ctx[8], *above_txfm_ctx; // 2bit
  356. uint8_t left_segpred_ctx[8], *above_segpred_ctx; // 1bit
  357. uint8_t left_intra_ctx[8], *above_intra_ctx; // 1bit
  358. uint8_t left_comp_ctx[8], *above_comp_ctx; // 1bit
  359. uint8_t left_ref_ctx[8], *above_ref_ctx; // 2bit
  360. uint8_t left_filter_ctx[8], *above_filter_ctx;
  361. VP56mv left_mv_ctx[16][2], (*above_mv_ctx)[2];
  362. // whole-frame cache
  363. uint8_t *intra_pred_data[3];
  364. uint8_t *segmentation_map;
  365. VP9MVRefPair *mv[2];
  366. VP9Filter *lflvl;
  367. DECLARE_ALIGNED(32, uint8_t, edge_emu_buffer)[71 * 80];
  368. // block reconstruction intermediates
  369. DECLARE_ALIGNED(32, int16_t, block)[4096];
  370. DECLARE_ALIGNED(32, int16_t, uvblock)[2][1024];
  371. uint8_t eob[256];
  372. uint8_t uveob[2][64];
  373. VP56mv min_mv, max_mv;
  374. DECLARE_ALIGNED(32, uint8_t, tmp_y)[64 * 64];
  375. DECLARE_ALIGNED(32, uint8_t, tmp_uv)[2][32 * 32];
  376. } VP9Context;
  377. void ff_vp9dsp_init(VP9DSPContext *dsp);
  378. void ff_vp9dsp_init_x86(VP9DSPContext *dsp);
  379. void ff_vp9_fill_mv(VP9Context *s, VP56mv *mv, int mode, int sb);
  380. void ff_vp9_adapt_probs(VP9Context *s);
  381. int ff_vp9_decode_block(AVCodecContext *avctx, int row, int col,
  382. VP9Filter *lflvl, ptrdiff_t yoff, ptrdiff_t uvoff,
  383. enum BlockLevel bl, enum BlockPartition bp);
  384. #endif /* AVCODEC_VP9_H */