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.

402 lines
11KB

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