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.

346 lines
9.4KB

  1. /**
  2. * @file
  3. * VP5 and VP6 compatible video decoder (common features)
  4. *
  5. * Copyright (C) 2006 Aurelien Jacobs <aurel@gnuage.org>
  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_VP56_H
  24. #define AVCODEC_VP56_H
  25. #include "vp56data.h"
  26. #include "dsputil.h"
  27. #include "get_bits.h"
  28. #include "bytestream.h"
  29. #include "vp56dsp.h"
  30. typedef struct vp56_context VP56Context;
  31. typedef struct vp56_mv VP56mv;
  32. typedef void (*VP56ParseVectorAdjustment)(VP56Context *s,
  33. VP56mv *vect);
  34. typedef void (*VP56Filter)(VP56Context *s, uint8_t *dst, uint8_t *src,
  35. int offset1, int offset2, int stride,
  36. VP56mv mv, int mask, int select, int luma);
  37. typedef void (*VP56ParseCoeff)(VP56Context *s);
  38. typedef void (*VP56DefaultModelsInit)(VP56Context *s);
  39. typedef void (*VP56ParseVectorModels)(VP56Context *s);
  40. typedef void (*VP56ParseCoeffModels)(VP56Context *s);
  41. typedef int (*VP56ParseHeader)(VP56Context *s, const uint8_t *buf,
  42. int buf_size, int *golden_frame);
  43. typedef struct {
  44. int high;
  45. int bits;
  46. const uint8_t *buffer;
  47. const uint8_t *end;
  48. unsigned long code_word;
  49. } VP56RangeCoder;
  50. typedef struct {
  51. uint8_t not_null_dc;
  52. VP56Frame ref_frame;
  53. DCTELEM dc_coeff;
  54. } VP56RefDc;
  55. struct vp56_mv {
  56. int x;
  57. int y;
  58. };
  59. typedef struct {
  60. uint8_t type;
  61. VP56mv mv;
  62. } VP56Macroblock;
  63. typedef struct {
  64. uint8_t coeff_reorder[64]; /* used in vp6 only */
  65. uint8_t coeff_index_to_pos[64]; /* used in vp6 only */
  66. uint8_t vector_sig[2]; /* delta sign */
  67. uint8_t vector_dct[2]; /* delta coding types */
  68. uint8_t vector_pdi[2][2]; /* predefined delta init */
  69. uint8_t vector_pdv[2][7]; /* predefined delta values */
  70. uint8_t vector_fdv[2][8]; /* 8 bit delta value definition */
  71. uint8_t coeff_dccv[2][11]; /* DC coeff value */
  72. uint8_t coeff_ract[2][3][6][11]; /* Run/AC coding type and AC coeff value */
  73. uint8_t coeff_acct[2][3][3][6][5];/* vp5 only AC coding type for coding group < 3 */
  74. uint8_t coeff_dcct[2][36][5]; /* DC coeff coding type */
  75. uint8_t coeff_runv[2][14]; /* run value (vp6 only) */
  76. uint8_t mb_type[3][10][10]; /* model for decoding MB type */
  77. uint8_t mb_types_stats[3][10][2];/* contextual, next MB type stats */
  78. } VP56Model;
  79. struct vp56_context {
  80. AVCodecContext *avctx;
  81. DSPContext dsp;
  82. VP56DSPContext vp56dsp;
  83. ScanTable scantable;
  84. AVFrame frames[4];
  85. AVFrame *framep[6];
  86. uint8_t *edge_emu_buffer_alloc;
  87. uint8_t *edge_emu_buffer;
  88. VP56RangeCoder c;
  89. VP56RangeCoder cc;
  90. VP56RangeCoder *ccp;
  91. int sub_version;
  92. /* frame info */
  93. int plane_width[4];
  94. int plane_height[4];
  95. int mb_width; /* number of horizontal MB */
  96. int mb_height; /* number of vertical MB */
  97. int block_offset[6];
  98. int quantizer;
  99. uint16_t dequant_dc;
  100. uint16_t dequant_ac;
  101. int8_t *qscale_table;
  102. /* DC predictors management */
  103. VP56RefDc *above_blocks;
  104. VP56RefDc left_block[4];
  105. int above_block_idx[6];
  106. DCTELEM prev_dc[3][3]; /* [plan][ref_frame] */
  107. /* blocks / macroblock */
  108. VP56mb mb_type;
  109. VP56Macroblock *macroblocks;
  110. DECLARE_ALIGNED(16, DCTELEM, block_coeff)[6][64];
  111. /* motion vectors */
  112. VP56mv mv[6]; /* vectors for each block in MB */
  113. VP56mv vector_candidate[2];
  114. int vector_candidate_pos;
  115. /* filtering hints */
  116. int filter_header; /* used in vp6 only */
  117. int deblock_filtering;
  118. int filter_selection;
  119. int filter_mode;
  120. int max_vector_length;
  121. int sample_variance_threshold;
  122. uint8_t coeff_ctx[4][64]; /* used in vp5 only */
  123. uint8_t coeff_ctx_last[4]; /* used in vp5 only */
  124. int has_alpha;
  125. /* upside-down flipping hints */
  126. int flip; /* are we flipping ? */
  127. int frbi; /* first row block index in MB */
  128. int srbi; /* second row block index in MB */
  129. int stride[4]; /* stride for each plan */
  130. const uint8_t *vp56_coord_div;
  131. VP56ParseVectorAdjustment parse_vector_adjustment;
  132. VP56Filter filter;
  133. VP56ParseCoeff parse_coeff;
  134. VP56DefaultModelsInit default_models_init;
  135. VP56ParseVectorModels parse_vector_models;
  136. VP56ParseCoeffModels parse_coeff_models;
  137. VP56ParseHeader parse_header;
  138. VP56Model *modelp;
  139. VP56Model models[2];
  140. /* huffman decoding */
  141. int use_huffman;
  142. GetBitContext gb;
  143. VLC dccv_vlc[2];
  144. VLC runv_vlc[2];
  145. VLC ract_vlc[2][3][6];
  146. unsigned int nb_null[2][2]; /* number of consecutive NULL DC/AC */
  147. };
  148. void vp56_init(AVCodecContext *avctx, int flip, int has_alpha);
  149. int vp56_free(AVCodecContext *avctx);
  150. void vp56_init_dequant(VP56Context *s, int quantizer);
  151. int vp56_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
  152. AVPacket *avpkt);
  153. /**
  154. * vp56 specific range coder implementation
  155. */
  156. static inline void vp56_init_range_decoder(VP56RangeCoder *c,
  157. const uint8_t *buf, int buf_size)
  158. {
  159. c->high = 255;
  160. c->bits = 8;
  161. c->buffer = buf;
  162. c->end = buf + buf_size;
  163. c->code_word = bytestream_get_be16(&c->buffer);
  164. }
  165. static inline int vp56_rac_get_prob(VP56RangeCoder *c, uint8_t prob)
  166. {
  167. unsigned int low = 1 + (((c->high - 1) * prob) >> 8);
  168. unsigned int low_shift = low << 8;
  169. int bit = c->code_word >= low_shift;
  170. if (bit) {
  171. c->high -= low;
  172. c->code_word -= low_shift;
  173. } else {
  174. c->high = low;
  175. }
  176. /* normalize */
  177. while (c->high < 128) {
  178. c->high <<= 1;
  179. c->code_word <<= 1;
  180. if (--c->bits == 0 && c->buffer < c->end) {
  181. c->bits = 8;
  182. c->code_word |= *c->buffer++;
  183. }
  184. }
  185. return bit;
  186. }
  187. static inline int vp56_rac_get(VP56RangeCoder *c)
  188. {
  189. /* equiprobable */
  190. int low = (c->high + 1) >> 1;
  191. unsigned int low_shift = low << 8;
  192. int bit = c->code_word >= low_shift;
  193. if (bit) {
  194. c->high = (c->high - low) << 1;
  195. c->code_word -= low_shift;
  196. } else {
  197. c->high = low << 1;
  198. }
  199. /* normalize */
  200. c->code_word <<= 1;
  201. if (--c->bits == 0 && c->buffer < c->end) {
  202. c->bits = 8;
  203. c->code_word |= *c->buffer++;
  204. }
  205. return bit;
  206. }
  207. // rounding is different than vp56_rac_get, is vp56_rac_get wrong?
  208. static inline int vp8_rac_get(VP56RangeCoder *c)
  209. {
  210. return vp56_rac_get_prob(c, 128);
  211. }
  212. static inline int vp56_rac_gets(VP56RangeCoder *c, int bits)
  213. {
  214. int value = 0;
  215. while (bits--) {
  216. value = (value << 1) | vp56_rac_get(c);
  217. }
  218. return value;
  219. }
  220. static inline int vp8_rac_get_uint(VP56RangeCoder *c, int bits)
  221. {
  222. int value = 0;
  223. while (bits--) {
  224. value = (value << 1) | vp8_rac_get(c);
  225. }
  226. return value;
  227. }
  228. // fixme: add 1 bit to all the calls to this?
  229. static inline int vp8_rac_get_sint(VP56RangeCoder *c, int bits)
  230. {
  231. int v;
  232. if (!vp8_rac_get(c))
  233. return 0;
  234. v = vp8_rac_get_uint(c, bits);
  235. if (vp8_rac_get(c))
  236. v = -v;
  237. return v;
  238. }
  239. // P(7)
  240. static inline int vp56_rac_gets_nn(VP56RangeCoder *c, int bits)
  241. {
  242. int v = vp56_rac_gets(c, 7) << 1;
  243. return v + !v;
  244. }
  245. static inline int vp8_rac_get_nn(VP56RangeCoder *c)
  246. {
  247. int v = vp8_rac_get_uint(c, 7) << 1;
  248. return v + !v;
  249. }
  250. static inline int vp56_rac_get_tree(VP56RangeCoder *c,
  251. const VP56Tree *tree,
  252. const uint8_t *probs)
  253. {
  254. while (tree->val > 0) {
  255. if (vp56_rac_get_prob(c, probs[tree->prob_idx]))
  256. tree += tree->val;
  257. else
  258. tree++;
  259. }
  260. return -tree->val;
  261. }
  262. /**
  263. * This is identical to vp8_rac_get_tree except for the possibility of starting
  264. * on a node other than the root node, needed for coeff decode where this is
  265. * used to save a bit after a 0 token (by disallowing EOB to immediately follow.)
  266. */
  267. static inline int vp8_rac_get_tree_with_offset(VP56RangeCoder *c, const int8_t (*tree)[2],
  268. const uint8_t *probs, int i)
  269. {
  270. do {
  271. i = tree[i][vp56_rac_get_prob(c, probs[i])];
  272. } while (i > 0);
  273. return -i;
  274. }
  275. // how probabilities are associated with decisions is different I think
  276. // well, the new scheme fits in the old but this way has one fewer branches per decision
  277. static inline int vp8_rac_get_tree(VP56RangeCoder *c, const int8_t (*tree)[2],
  278. const uint8_t *probs)
  279. {
  280. return vp8_rac_get_tree_with_offset(c, tree, probs, 0);
  281. }
  282. // DCTextra
  283. static inline int vp8_rac_get_coeff(VP56RangeCoder *c, const uint8_t *prob)
  284. {
  285. int v = 0;
  286. do {
  287. v = (v<<1) + vp56_rac_get_prob(c, *prob++);
  288. } while (*prob);
  289. return v;
  290. }
  291. #endif /* AVCODEC_VP56_H */