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.

357 lines
10KB

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