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.

380 lines
10KB

  1. /*
  2. * Copyright (C) 2006 Aurelien Jacobs <aurel@gnuage.org>
  3. *
  4. * This file is part of Libav.
  5. *
  6. * Libav is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * Libav is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with Libav; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /**
  21. * @file
  22. * VP5 and VP6 compatible video decoder (common features)
  23. */
  24. #ifndef AVCODEC_VP56_H
  25. #define AVCODEC_VP56_H
  26. #include "vp56data.h"
  27. #include "dsputil.h"
  28. #include "get_bits.h"
  29. #include "bytestream.h"
  30. #include "h264chroma.h"
  31. #include "videodsp.h"
  32. #include "vp3dsp.h"
  33. #include "vp56dsp.h"
  34. typedef struct vp56_context VP56Context;
  35. typedef struct VP56mv {
  36. DECLARE_ALIGNED(4, int16_t, x);
  37. int16_t y;
  38. } VP56mv;
  39. #define VP56_SIZE_CHANGE 1
  40. typedef void (*VP56ParseVectorAdjustment)(VP56Context *s,
  41. VP56mv *vect);
  42. typedef void (*VP56Filter)(VP56Context *s, uint8_t *dst, uint8_t *src,
  43. int offset1, int offset2, int stride,
  44. VP56mv mv, int mask, int select, int luma);
  45. typedef void (*VP56ParseCoeff)(VP56Context *s);
  46. typedef void (*VP56DefaultModelsInit)(VP56Context *s);
  47. typedef void (*VP56ParseVectorModels)(VP56Context *s);
  48. typedef int (*VP56ParseCoeffModels)(VP56Context *s);
  49. typedef int (*VP56ParseHeader)(VP56Context *s, const uint8_t *buf,
  50. int buf_size, int *golden_frame);
  51. typedef struct VP56RangeCoder {
  52. int high;
  53. int bits; /* stored negated (i.e. negative "bits" is a positive number of
  54. bits left) in order to eliminate a negate in cache refilling */
  55. const uint8_t *buffer;
  56. const uint8_t *end;
  57. unsigned int code_word;
  58. } VP56RangeCoder;
  59. typedef struct VP56RefDc {
  60. uint8_t not_null_dc;
  61. VP56Frame ref_frame;
  62. int16_t dc_coeff;
  63. } VP56RefDc;
  64. typedef struct VP56Macroblock {
  65. uint8_t type;
  66. VP56mv mv;
  67. } VP56Macroblock;
  68. typedef struct VP56Model {
  69. uint8_t coeff_reorder[64]; /* used in vp6 only */
  70. uint8_t coeff_index_to_pos[64]; /* used in vp6 only */
  71. uint8_t vector_sig[2]; /* delta sign */
  72. uint8_t vector_dct[2]; /* delta coding types */
  73. uint8_t vector_pdi[2][2]; /* predefined delta init */
  74. uint8_t vector_pdv[2][7]; /* predefined delta values */
  75. uint8_t vector_fdv[2][8]; /* 8 bit delta value definition */
  76. uint8_t coeff_dccv[2][11]; /* DC coeff value */
  77. uint8_t coeff_ract[2][3][6][11]; /* Run/AC coding type and AC coeff value */
  78. uint8_t coeff_acct[2][3][3][6][5];/* vp5 only AC coding type for coding group < 3 */
  79. uint8_t coeff_dcct[2][36][5]; /* DC coeff coding type */
  80. uint8_t coeff_runv[2][14]; /* run value (vp6 only) */
  81. uint8_t mb_type[3][10][10]; /* model for decoding MB type */
  82. uint8_t mb_types_stats[3][10][2];/* contextual, next MB type stats */
  83. } VP56Model;
  84. struct vp56_context {
  85. AVCodecContext *avctx;
  86. DSPContext dsp;
  87. H264ChromaContext h264chroma;
  88. VideoDSPContext vdsp;
  89. VP3DSPContext vp3dsp;
  90. VP56DSPContext vp56dsp;
  91. uint8_t idct_scantable[64];
  92. AVFrame *frames[4];
  93. uint8_t *edge_emu_buffer_alloc;
  94. uint8_t *edge_emu_buffer;
  95. VP56RangeCoder c;
  96. VP56RangeCoder cc;
  97. VP56RangeCoder *ccp;
  98. int sub_version;
  99. /* frame info */
  100. int plane_width[4];
  101. int plane_height[4];
  102. int mb_width; /* number of horizontal MB */
  103. int mb_height; /* number of vertical MB */
  104. int block_offset[6];
  105. int quantizer;
  106. uint16_t dequant_dc;
  107. uint16_t dequant_ac;
  108. /* DC predictors management */
  109. VP56RefDc *above_blocks;
  110. VP56RefDc left_block[4];
  111. int above_block_idx[6];
  112. int16_t prev_dc[3][3]; /* [plan][ref_frame] */
  113. /* blocks / macroblock */
  114. VP56mb mb_type;
  115. VP56Macroblock *macroblocks;
  116. DECLARE_ALIGNED(16, int16_t, block_coeff)[6][64];
  117. /* motion vectors */
  118. VP56mv mv[6]; /* vectors for each block in MB */
  119. VP56mv vector_candidate[2];
  120. int vector_candidate_pos;
  121. /* filtering hints */
  122. int filter_header; /* used in vp6 only */
  123. int deblock_filtering;
  124. int filter_selection;
  125. int filter_mode;
  126. int max_vector_length;
  127. int sample_variance_threshold;
  128. uint8_t coeff_ctx[4][64]; /* used in vp5 only */
  129. uint8_t coeff_ctx_last[4]; /* used in vp5 only */
  130. int has_alpha;
  131. /* upside-down flipping hints */
  132. int flip; /* are we flipping ? */
  133. int frbi; /* first row block index in MB */
  134. int srbi; /* second row block index in MB */
  135. int stride[4]; /* stride for each plan */
  136. const uint8_t *vp56_coord_div;
  137. VP56ParseVectorAdjustment parse_vector_adjustment;
  138. VP56Filter filter;
  139. VP56ParseCoeff parse_coeff;
  140. VP56DefaultModelsInit default_models_init;
  141. VP56ParseVectorModels parse_vector_models;
  142. VP56ParseCoeffModels parse_coeff_models;
  143. VP56ParseHeader parse_header;
  144. VP56Model *modelp;
  145. VP56Model models[2];
  146. /* huffman decoding */
  147. int use_huffman;
  148. GetBitContext gb;
  149. VLC dccv_vlc[2];
  150. VLC runv_vlc[2];
  151. VLC ract_vlc[2][3][6];
  152. unsigned int nb_null[2][2]; /* number of consecutive NULL DC/AC */
  153. };
  154. int ff_vp56_init(AVCodecContext *avctx, int flip, int has_alpha);
  155. int ff_vp56_free(AVCodecContext *avctx);
  156. void ff_vp56_init_dequant(VP56Context *s, int quantizer);
  157. int ff_vp56_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
  158. AVPacket *avpkt);
  159. /**
  160. * vp56 specific range coder implementation
  161. */
  162. extern const uint8_t ff_vp56_norm_shift[256];
  163. void ff_vp56_init_range_decoder(VP56RangeCoder *c, const uint8_t *buf, int buf_size);
  164. static av_always_inline unsigned int vp56_rac_renorm(VP56RangeCoder *c)
  165. {
  166. int shift = ff_vp56_norm_shift[c->high];
  167. int bits = c->bits;
  168. unsigned int code_word = c->code_word;
  169. c->high <<= shift;
  170. code_word <<= shift;
  171. bits += shift;
  172. if(bits >= 0 && c->buffer < c->end) {
  173. code_word |= bytestream_get_be16(&c->buffer) << bits;
  174. bits -= 16;
  175. }
  176. c->bits = bits;
  177. return code_word;
  178. }
  179. #if ARCH_ARM
  180. #include "arm/vp56_arith.h"
  181. #elif ARCH_X86
  182. #include "x86/vp56_arith.h"
  183. #endif
  184. #ifndef vp56_rac_get_prob
  185. #define vp56_rac_get_prob vp56_rac_get_prob
  186. static av_always_inline int vp56_rac_get_prob(VP56RangeCoder *c, uint8_t prob)
  187. {
  188. unsigned int code_word = vp56_rac_renorm(c);
  189. unsigned int low = 1 + (((c->high - 1) * prob) >> 8);
  190. unsigned int low_shift = low << 16;
  191. int bit = code_word >= low_shift;
  192. c->high = bit ? c->high - low : low;
  193. c->code_word = bit ? code_word - low_shift : code_word;
  194. return bit;
  195. }
  196. #endif
  197. #ifndef vp56_rac_get_prob_branchy
  198. // branchy variant, to be used where there's a branch based on the bit decoded
  199. static av_always_inline int vp56_rac_get_prob_branchy(VP56RangeCoder *c, int prob)
  200. {
  201. unsigned long code_word = vp56_rac_renorm(c);
  202. unsigned low = 1 + (((c->high - 1) * prob) >> 8);
  203. unsigned low_shift = low << 16;
  204. if (code_word >= low_shift) {
  205. c->high -= low;
  206. c->code_word = code_word - low_shift;
  207. return 1;
  208. }
  209. c->high = low;
  210. c->code_word = code_word;
  211. return 0;
  212. }
  213. #endif
  214. static av_always_inline int vp56_rac_get(VP56RangeCoder *c)
  215. {
  216. unsigned int code_word = vp56_rac_renorm(c);
  217. /* equiprobable */
  218. int low = (c->high + 1) >> 1;
  219. unsigned int low_shift = low << 16;
  220. int bit = code_word >= low_shift;
  221. if (bit) {
  222. c->high -= low;
  223. code_word -= low_shift;
  224. } else {
  225. c->high = low;
  226. }
  227. c->code_word = code_word;
  228. return bit;
  229. }
  230. // rounding is different than vp56_rac_get, is vp56_rac_get wrong?
  231. static av_always_inline int vp8_rac_get(VP56RangeCoder *c)
  232. {
  233. return vp56_rac_get_prob(c, 128);
  234. }
  235. static av_unused int vp56_rac_gets(VP56RangeCoder *c, int bits)
  236. {
  237. int value = 0;
  238. while (bits--) {
  239. value = (value << 1) | vp56_rac_get(c);
  240. }
  241. return value;
  242. }
  243. static av_unused int vp8_rac_get_uint(VP56RangeCoder *c, int bits)
  244. {
  245. int value = 0;
  246. while (bits--) {
  247. value = (value << 1) | vp8_rac_get(c);
  248. }
  249. return value;
  250. }
  251. // fixme: add 1 bit to all the calls to this?
  252. static av_unused int vp8_rac_get_sint(VP56RangeCoder *c, int bits)
  253. {
  254. int v;
  255. if (!vp8_rac_get(c))
  256. return 0;
  257. v = vp8_rac_get_uint(c, bits);
  258. if (vp8_rac_get(c))
  259. v = -v;
  260. return v;
  261. }
  262. // P(7)
  263. static av_unused int vp56_rac_gets_nn(VP56RangeCoder *c, int bits)
  264. {
  265. int v = vp56_rac_gets(c, 7) << 1;
  266. return v + !v;
  267. }
  268. static av_unused int vp8_rac_get_nn(VP56RangeCoder *c)
  269. {
  270. int v = vp8_rac_get_uint(c, 7) << 1;
  271. return v + !v;
  272. }
  273. static av_always_inline
  274. int vp56_rac_get_tree(VP56RangeCoder *c,
  275. const VP56Tree *tree,
  276. const uint8_t *probs)
  277. {
  278. while (tree->val > 0) {
  279. if (vp56_rac_get_prob(c, probs[tree->prob_idx]))
  280. tree += tree->val;
  281. else
  282. tree++;
  283. }
  284. return -tree->val;
  285. }
  286. /**
  287. * This is identical to vp8_rac_get_tree except for the possibility of starting
  288. * on a node other than the root node, needed for coeff decode where this is
  289. * used to save a bit after a 0 token (by disallowing EOB to immediately follow.)
  290. */
  291. static av_always_inline
  292. int vp8_rac_get_tree_with_offset(VP56RangeCoder *c, const int8_t (*tree)[2],
  293. const uint8_t *probs, int i)
  294. {
  295. do {
  296. i = tree[i][vp56_rac_get_prob(c, probs[i])];
  297. } while (i > 0);
  298. return -i;
  299. }
  300. // how probabilities are associated with decisions is different I think
  301. // well, the new scheme fits in the old but this way has one fewer branches per decision
  302. static av_always_inline
  303. int vp8_rac_get_tree(VP56RangeCoder *c, const int8_t (*tree)[2],
  304. const uint8_t *probs)
  305. {
  306. return vp8_rac_get_tree_with_offset(c, tree, probs, 0);
  307. }
  308. // DCTextra
  309. static av_always_inline int vp8_rac_get_coeff(VP56RangeCoder *c, const uint8_t *prob)
  310. {
  311. int v = 0;
  312. do {
  313. v = (v<<1) + vp56_rac_get_prob(c, *prob++);
  314. } while (*prob);
  315. return v;
  316. }
  317. #endif /* AVCODEC_VP56_H */