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.

354 lines
9.9KB

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