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.

376 lines
10KB

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