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.

447 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, const uint8_t *ref,
  118. ptrdiff_t dst_stride,
  119. ptrdiff_t ref_stride,
  120. int h, int mx, int my);
  121. typedef struct VP9DSPContext {
  122. /*
  123. * dimension 1: 0=4x4, 1=8x8, 2=16x16, 3=32x32
  124. * dimension 2: intra prediction modes
  125. *
  126. * dst/left/top is aligned by transform-size (i.e. 4, 8, 16 or 32 pixels)
  127. * stride is aligned by 16 pixels
  128. * top[-1] is top/left; top[4,7] is top-right for 4x4
  129. */
  130. // FIXME(rbultje) maybe replace left/top pointers with HAVE_TOP/
  131. // HAVE_LEFT/HAVE_TOPRIGHT flags instead, and then handle it in-place?
  132. // also needs to fit in with what H.264/VP8/etc do
  133. void (*intra_pred[N_TXFM_SIZES][N_INTRA_PRED_MODES])(uint8_t *dst,
  134. ptrdiff_t stride,
  135. const uint8_t *left,
  136. const uint8_t *top);
  137. /*
  138. * dimension 1: 0=4x4, 1=8x8, 2=16x16, 3=32x32, 4=lossless (3-4=dct only)
  139. * dimension 2: 0=dct/dct, 1=dct/adst, 2=adst/dct, 3=adst/adst
  140. *
  141. * dst is aligned by transform-size (i.e. 4, 8, 16 or 32 pixels)
  142. * stride is aligned by 16 pixels
  143. * block is 16-byte aligned
  144. * eob indicates the position (+1) of the last non-zero coefficient,
  145. * in scan-order. This can be used to write faster versions, e.g. a
  146. * dc-only 4x4/8x8/16x16/32x32, or a 4x4-only (eob<10) 8x8/16x16/32x32,
  147. * etc.
  148. */
  149. // FIXME also write idct_add_block() versions for whole (inter) pred
  150. // blocks, so we can do 2 4x4s at once
  151. void (*itxfm_add[N_TXFM_SIZES + 1][N_TXFM_TYPES])(uint8_t *dst,
  152. ptrdiff_t stride,
  153. int16_t *block, int eob);
  154. /*
  155. * dimension 1: width of filter (0=4, 1=8, 2=16)
  156. * dimension 2: 0=col-edge filter (h), 1=row-edge filter (v)
  157. *
  158. * dst/stride are aligned by 8
  159. */
  160. void (*loop_filter_8[3][2])(uint8_t *dst, ptrdiff_t stride,
  161. int mb_lim, int lim, int hev_thr);
  162. /*
  163. * dimension 1: 0=col-edge filter (h), 1=row-edge filter (v)
  164. *
  165. * The width of filter is assumed to be 16; dst/stride are aligned by 16
  166. */
  167. void (*loop_filter_16[2])(uint8_t *dst, ptrdiff_t stride,
  168. int mb_lim, int lim, int hev_thr);
  169. /*
  170. * dimension 1/2: width of filter (0=4, 1=8) for each filter half
  171. * dimension 3: 0=col-edge filter (h), 1=row-edge filter (v)
  172. *
  173. * dst/stride are aligned by operation size
  174. * this basically calls loop_filter[d1][d3][0](), followed by
  175. * loop_filter[d2][d3][0]() on the next 8 pixels
  176. * mb_lim/lim/hev_thr contain two values in the lowest two bytes of the
  177. * integer.
  178. */
  179. // FIXME perhaps a mix4 that operates on 32px (for AVX2)
  180. void (*loop_filter_mix2[2][2][2])(uint8_t *dst, ptrdiff_t stride,
  181. int mb_lim, int lim, int hev_thr);
  182. /*
  183. * dimension 1: hsize (0: 64, 1: 32, 2: 16, 3: 8, 4: 4)
  184. * dimension 2: filter type (0: smooth, 1: regular, 2: sharp, 3: bilin)
  185. * dimension 3: averaging type (0: put, 1: avg)
  186. * dimension 4: x subpel interpolation (0: none, 1: 8tap/bilin)
  187. * dimension 5: y subpel interpolation (1: none, 1: 8tap/bilin)
  188. *
  189. * dst/stride are aligned by hsize
  190. */
  191. vp9_mc_func mc[5][4][2][2][2];
  192. } VP9DSPContext;
  193. enum CompPredMode {
  194. PRED_SINGLEREF,
  195. PRED_COMPREF,
  196. PRED_SWITCHABLE,
  197. };
  198. typedef struct VP9MVRefPair {
  199. VP56mv mv[2];
  200. int8_t ref[2];
  201. } VP9MVRefPair;
  202. typedef struct VP9Filter {
  203. uint8_t level[8 * 8];
  204. uint8_t /* bit=col */ mask[2 /* 0=y, 1=uv */][2 /* 0=col, 1=row */]
  205. [8 /* rows */][4 /* 0=16, 1=8, 2=4, 3=inner4 */];
  206. } VP9Filter;
  207. typedef struct VP9Frame {
  208. ThreadFrame tf;
  209. uint8_t *segmentation_map;
  210. VP9MVRefPair *mv;
  211. AVBufferRef *segmentation_map_buf;
  212. AVBufferRef *mv_buf;
  213. } VP9Frame;
  214. enum BlockLevel {
  215. BL_64X64,
  216. BL_32X32,
  217. BL_16X16,
  218. BL_8X8,
  219. };
  220. enum BlockSize {
  221. BS_64x64,
  222. BS_64x32,
  223. BS_32x64,
  224. BS_32x32,
  225. BS_32x16,
  226. BS_16x32,
  227. BS_16x16,
  228. BS_16x8,
  229. BS_8x16,
  230. BS_8x8,
  231. BS_8x4,
  232. BS_4x8,
  233. BS_4x4,
  234. N_BS_SIZES,
  235. };
  236. typedef struct VP9Block {
  237. uint8_t seg_id, intra, comp, ref[2], mode[4], uvmode, skip;
  238. enum FilterMode filter;
  239. VP56mv mv[4 /* b_idx */][2 /* ref */];
  240. enum BlockSize bs;
  241. enum TxfmMode tx, uvtx;
  242. int row, row7, col, col7;
  243. uint8_t *dst[3];
  244. ptrdiff_t y_stride, uv_stride;
  245. enum BlockLevel bl;
  246. enum BlockPartition bp;
  247. } VP9Block;
  248. typedef struct VP9Context {
  249. VP9DSPContext dsp;
  250. VideoDSPContext vdsp;
  251. GetBitContext gb;
  252. VP56RangeCoder c;
  253. VP56RangeCoder *c_b;
  254. unsigned c_b_size;
  255. VP9Block *b;
  256. VP9Block *b_base;
  257. int alloc_width;
  258. int alloc_height;
  259. int pass;
  260. int uses_2pass;
  261. int last_uses_2pass;
  262. int setup_finished;
  263. // bitstream header
  264. uint8_t profile;
  265. uint8_t keyframe, last_keyframe;
  266. uint8_t invisible;
  267. uint8_t use_last_frame_mvs;
  268. uint8_t errorres;
  269. uint8_t colorspace;
  270. uint8_t sub_x;
  271. uint8_t sub_y;
  272. uint8_t fullrange;
  273. uint8_t intraonly;
  274. uint8_t resetctx;
  275. uint8_t refreshrefmask;
  276. uint8_t highprecisionmvs;
  277. enum FilterMode filtermode;
  278. uint8_t allowcompinter;
  279. uint8_t fixcompref;
  280. uint8_t refreshctx;
  281. uint8_t parallelmode;
  282. uint8_t framectxid;
  283. uint8_t refidx[3];
  284. uint8_t signbias[3];
  285. uint8_t varcompref[2];
  286. ThreadFrame refs[8];
  287. #define CUR_FRAME 0
  288. #define LAST_FRAME 1
  289. VP9Frame frames[2];
  290. struct {
  291. uint8_t level;
  292. int8_t sharpness;
  293. uint8_t lim_lut[64];
  294. uint8_t mblim_lut[64];
  295. } filter;
  296. struct {
  297. uint8_t enabled;
  298. int8_t mode[2];
  299. int8_t ref[4];
  300. } lf_delta;
  301. uint8_t yac_qi;
  302. int8_t ydc_qdelta, uvdc_qdelta, uvac_qdelta;
  303. uint8_t lossless;
  304. struct {
  305. uint8_t enabled;
  306. uint8_t temporal;
  307. uint8_t absolute_vals;
  308. uint8_t update_map;
  309. #define MAX_SEGMENT 8
  310. struct {
  311. uint8_t q_enabled;
  312. uint8_t lf_enabled;
  313. uint8_t ref_enabled;
  314. uint8_t skip_enabled;
  315. uint8_t ref_val;
  316. int16_t q_val;
  317. int8_t lf_val;
  318. int16_t qmul[2][2];
  319. uint8_t lflvl[4][2];
  320. } feat[MAX_SEGMENT];
  321. } segmentation;
  322. struct {
  323. unsigned log2_tile_cols, log2_tile_rows;
  324. unsigned tile_cols, tile_rows;
  325. unsigned tile_row_start, tile_row_end, tile_col_start, tile_col_end;
  326. } tiling;
  327. unsigned sb_cols, sb_rows, rows, cols;
  328. struct {
  329. ProbContext p;
  330. uint8_t coef[4][2][2][6][6][3];
  331. } prob_ctx[4];
  332. struct {
  333. ProbContext p;
  334. uint8_t coef[4][2][2][6][6][11];
  335. uint8_t seg[7];
  336. uint8_t segpred[3];
  337. } prob;
  338. struct {
  339. unsigned y_mode[4][10];
  340. unsigned uv_mode[10][10];
  341. unsigned filter[4][3];
  342. unsigned mv_mode[7][4];
  343. unsigned intra[4][2];
  344. unsigned comp[5][2];
  345. unsigned single_ref[5][2][2];
  346. unsigned comp_ref[5][2];
  347. unsigned tx32p[2][4];
  348. unsigned tx16p[2][3];
  349. unsigned tx8p[2][2];
  350. unsigned skip[3][2];
  351. unsigned mv_joint[4];
  352. struct {
  353. unsigned sign[2];
  354. unsigned classes[11];
  355. unsigned class0[2];
  356. unsigned bits[10][2];
  357. unsigned class0_fp[2][4];
  358. unsigned fp[4];
  359. unsigned class0_hp[2];
  360. unsigned hp[2];
  361. } mv_comp[2];
  362. unsigned partition[4][4][4];
  363. unsigned coef[4][2][2][6][6][3];
  364. unsigned eob[4][2][2][6][6][2];
  365. } counts;
  366. enum TxfmMode txfmmode;
  367. enum CompPredMode comppredmode;
  368. // contextual (left/above) cache
  369. uint8_t left_partition_ctx[8], *above_partition_ctx;
  370. uint8_t left_mode_ctx[16], *above_mode_ctx;
  371. // FIXME maybe merge some of the below in a flags field?
  372. uint8_t left_y_nnz_ctx[16], *above_y_nnz_ctx;
  373. uint8_t left_uv_nnz_ctx[2][8], *above_uv_nnz_ctx[2];
  374. uint8_t left_skip_ctx[8], *above_skip_ctx; // 1bit
  375. uint8_t left_txfm_ctx[8], *above_txfm_ctx; // 2bit
  376. uint8_t left_segpred_ctx[8], *above_segpred_ctx; // 1bit
  377. uint8_t left_intra_ctx[8], *above_intra_ctx; // 1bit
  378. uint8_t left_comp_ctx[8], *above_comp_ctx; // 1bit
  379. uint8_t left_ref_ctx[8], *above_ref_ctx; // 2bit
  380. uint8_t left_filter_ctx[8], *above_filter_ctx;
  381. VP56mv left_mv_ctx[16][2], (*above_mv_ctx)[2];
  382. // whole-frame cache
  383. uint8_t *intra_pred_data[3];
  384. VP9Filter *lflvl;
  385. DECLARE_ALIGNED(32, uint8_t, edge_emu_buffer)[71 * 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. void ff_vp9dsp_init(VP9DSPContext *dsp);
  394. void ff_vp9dsp_init_x86(VP9DSPContext *dsp);
  395. void ff_vp9_fill_mv(VP9Context *s, VP56mv *mv, int mode, int sb);
  396. void ff_vp9_adapt_probs(VP9Context *s);
  397. int ff_vp9_decode_block(AVCodecContext *avctx, int row, int col,
  398. VP9Filter *lflvl, ptrdiff_t yoff, ptrdiff_t uvoff,
  399. enum BlockLevel bl, enum BlockPartition bp);
  400. #endif /* AVCODEC_VP9_H */