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.

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