vp56.h 12 KB

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