cavs.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /*
  2. * Chinese AVS video (AVS1-P2, JiZhun profile) decoder.
  3. * Copyright (c) 2006 Stefan Gehrer <stefan.gehrer@gmx.de>
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #ifndef AVCODEC_CAVS_H
  22. #define AVCODEC_CAVS_H
  23. #include "dsputil.h"
  24. #include "mpegvideo.h"
  25. #define SLICE_MAX_START_CODE 0x000001af
  26. #define EXT_START_CODE 0x000001b5
  27. #define USER_START_CODE 0x000001b2
  28. #define CAVS_START_CODE 0x000001b0
  29. #define PIC_I_START_CODE 0x000001b3
  30. #define PIC_PB_START_CODE 0x000001b6
  31. #define A_AVAIL 1
  32. #define B_AVAIL 2
  33. #define C_AVAIL 4
  34. #define D_AVAIL 8
  35. #define NOT_AVAIL -1
  36. #define REF_INTRA -2
  37. #define REF_DIR -3
  38. #define ESCAPE_CODE 59
  39. #define FWD0 0x01
  40. #define FWD1 0x02
  41. #define BWD0 0x04
  42. #define BWD1 0x08
  43. #define SYM0 0x10
  44. #define SYM1 0x20
  45. #define SPLITH 0x40
  46. #define SPLITV 0x80
  47. #define MV_BWD_OFFS 12
  48. #define MV_STRIDE 4
  49. enum cavs_mb {
  50. I_8X8 = 0,
  51. P_SKIP,
  52. P_16X16,
  53. P_16X8,
  54. P_8X16,
  55. P_8X8,
  56. B_SKIP,
  57. B_DIRECT,
  58. B_FWD_16X16,
  59. B_BWD_16X16,
  60. B_SYM_16X16,
  61. B_8X8 = 29
  62. };
  63. enum cavs_sub_mb {
  64. B_SUB_DIRECT,
  65. B_SUB_FWD,
  66. B_SUB_BWD,
  67. B_SUB_SYM
  68. };
  69. enum cavs_intra_luma {
  70. INTRA_L_VERT,
  71. INTRA_L_HORIZ,
  72. INTRA_L_LP,
  73. INTRA_L_DOWN_LEFT,
  74. INTRA_L_DOWN_RIGHT,
  75. INTRA_L_LP_LEFT,
  76. INTRA_L_LP_TOP,
  77. INTRA_L_DC_128
  78. };
  79. enum cavs_intra_chroma {
  80. INTRA_C_LP,
  81. INTRA_C_HORIZ,
  82. INTRA_C_VERT,
  83. INTRA_C_PLANE,
  84. INTRA_C_LP_LEFT,
  85. INTRA_C_LP_TOP,
  86. INTRA_C_DC_128,
  87. };
  88. enum cavs_mv_pred {
  89. MV_PRED_MEDIAN,
  90. MV_PRED_LEFT,
  91. MV_PRED_TOP,
  92. MV_PRED_TOPRIGHT,
  93. MV_PRED_PSKIP,
  94. MV_PRED_BSKIP
  95. };
  96. enum cavs_block {
  97. BLK_16X16,
  98. BLK_16X8,
  99. BLK_8X16,
  100. BLK_8X8
  101. };
  102. enum cavs_mv_loc {
  103. MV_FWD_D3 = 0,
  104. MV_FWD_B2,
  105. MV_FWD_B3,
  106. MV_FWD_C2,
  107. MV_FWD_A1,
  108. MV_FWD_X0,
  109. MV_FWD_X1,
  110. MV_FWD_A3 = 8,
  111. MV_FWD_X2,
  112. MV_FWD_X3,
  113. MV_BWD_D3 = MV_BWD_OFFS,
  114. MV_BWD_B2,
  115. MV_BWD_B3,
  116. MV_BWD_C2,
  117. MV_BWD_A1,
  118. MV_BWD_X0,
  119. MV_BWD_X1,
  120. MV_BWD_A3 = MV_BWD_OFFS+8,
  121. MV_BWD_X2,
  122. MV_BWD_X3
  123. };
  124. DECLARE_ALIGNED_8(typedef, struct) {
  125. int16_t x;
  126. int16_t y;
  127. int16_t dist;
  128. int16_t ref;
  129. } cavs_vector;
  130. struct dec_2dvlc {
  131. int8_t rltab[59][3];
  132. int8_t level_add[27];
  133. int8_t golomb_order;
  134. int inc_limit;
  135. int8_t max_run;
  136. };
  137. typedef struct {
  138. MpegEncContext s;
  139. Picture picture; ///< currently decoded frame
  140. Picture DPB[2]; ///< reference frames
  141. int dist[2]; ///< temporal distances from current frame to ref frames
  142. int profile, level;
  143. int aspect_ratio;
  144. int mb_width, mb_height;
  145. int pic_type;
  146. int progressive;
  147. int pic_structure;
  148. int skip_mode_flag; ///< select between skip_count or one skip_flag per MB
  149. int loop_filter_disable;
  150. int alpha_offset, beta_offset;
  151. int ref_flag;
  152. int mbx, mby, mbidx; ///< macroblock coordinates
  153. int flags; ///< availability flags of neighbouring macroblocks
  154. int stc; ///< last start code
  155. uint8_t *cy, *cu, *cv; ///< current MB sample pointers
  156. int left_qp;
  157. uint8_t *top_qp;
  158. /** mv motion vector cache
  159. 0: D3 B2 B3 C2
  160. 4: A1 X0 X1 -
  161. 8: A3 X2 X3 -
  162. X are the vectors in the current macroblock (5,6,9,10)
  163. A is the macroblock to the left (4,8)
  164. B is the macroblock to the top (1,2)
  165. C is the macroblock to the top-right (3)
  166. D is the macroblock to the top-left (0)
  167. the same is repeated for backward motion vectors */
  168. cavs_vector mv[2*4*3];
  169. cavs_vector *top_mv[2];
  170. cavs_vector *col_mv;
  171. /** luma pred mode cache
  172. 0: -- B2 B3
  173. 3: A1 X0 X1
  174. 6: A3 X2 X3 */
  175. int pred_mode_Y[3*3];
  176. int *top_pred_Y;
  177. int l_stride, c_stride;
  178. int luma_scan[4];
  179. int qp;
  180. int qp_fixed;
  181. int cbp;
  182. ScanTable scantable;
  183. /** intra prediction is done with un-deblocked samples
  184. they are saved here before deblocking the MB */
  185. uint8_t *top_border_y, *top_border_u, *top_border_v;
  186. uint8_t left_border_y[26], left_border_u[10], left_border_v[10];
  187. uint8_t intern_border_y[26];
  188. uint8_t topleft_border_y, topleft_border_u, topleft_border_v;
  189. void (*intra_pred_l[8])(uint8_t *d,uint8_t *top,uint8_t *left,int stride);
  190. void (*intra_pred_c[7])(uint8_t *d,uint8_t *top,uint8_t *left,int stride);
  191. uint8_t *col_type_base;
  192. /* scaling factors for MV prediction */
  193. int sym_factor; ///< for scaling in symmetrical B block
  194. int direct_den[2]; ///< for scaling in direct B block
  195. int scale_den[2]; ///< for scaling neighbouring MVs
  196. int got_keyframe;
  197. DCTELEM *block;
  198. } AVSContext;
  199. extern const uint8_t ff_cavs_dequant_shift[64];
  200. extern const uint16_t ff_cavs_dequant_mul[64];
  201. extern const struct dec_2dvlc ff_cavs_intra_dec[7];
  202. extern const struct dec_2dvlc ff_cavs_inter_dec[7];
  203. extern const struct dec_2dvlc ff_cavs_chroma_dec[5];
  204. extern const uint8_t ff_cavs_chroma_qp[64];
  205. extern const uint8_t ff_cavs_scan3x3[4];
  206. extern const uint8_t ff_cavs_partition_flags[30];
  207. extern const int_fast8_t ff_left_modifier_l[8];
  208. extern const int_fast8_t ff_top_modifier_l[8];
  209. extern const int_fast8_t ff_left_modifier_c[7];
  210. extern const int_fast8_t ff_top_modifier_c[7];
  211. extern const cavs_vector ff_cavs_intra_mv;
  212. extern const cavs_vector ff_cavs_un_mv;
  213. extern const cavs_vector ff_cavs_dir_mv;
  214. static inline void modify_pred(const int_fast8_t *mod_table, int *mode) {
  215. *mode = mod_table[*mode];
  216. if(*mode < 0) {
  217. av_log(NULL, AV_LOG_ERROR, "Illegal intra prediction mode\n");
  218. *mode = 0;
  219. }
  220. }
  221. static inline void set_intra_mode_default(AVSContext *h) {
  222. h->pred_mode_Y[3] = h->pred_mode_Y[6] = INTRA_L_LP;
  223. h->top_pred_Y[h->mbx*2+0] = h->top_pred_Y[h->mbx*2+1] = INTRA_L_LP;
  224. }
  225. static inline void set_mvs(cavs_vector *mv, enum cavs_block size) {
  226. switch(size) {
  227. case BLK_16X16:
  228. mv[MV_STRIDE ] = mv[0];
  229. mv[MV_STRIDE+1] = mv[0];
  230. case BLK_16X8:
  231. mv[1] = mv[0];
  232. break;
  233. case BLK_8X16:
  234. mv[MV_STRIDE] = mv[0];
  235. break;
  236. }
  237. }
  238. static inline void set_mv_intra(AVSContext *h) {
  239. h->mv[MV_FWD_X0] = ff_cavs_intra_mv;
  240. set_mvs(&h->mv[MV_FWD_X0], BLK_16X16);
  241. h->mv[MV_BWD_X0] = ff_cavs_intra_mv;
  242. set_mvs(&h->mv[MV_BWD_X0], BLK_16X16);
  243. if(h->pic_type != FF_B_TYPE)
  244. h->col_type_base[h->mbidx] = I_8X8;
  245. }
  246. static inline int dequant(AVSContext *h, DCTELEM *level_buf, uint8_t *run_buf,
  247. DCTELEM *dst, int mul, int shift, int coeff_num) {
  248. int round = 1 << (shift - 1);
  249. int pos = -1;
  250. const uint8_t *scantab = h->scantable.permutated;
  251. /* inverse scan and dequantization */
  252. while(--coeff_num >= 0){
  253. pos += run_buf[coeff_num];
  254. if(pos > 63) {
  255. av_log(h->s.avctx, AV_LOG_ERROR,
  256. "position out of block bounds at pic %d MB(%d,%d)\n",
  257. h->picture.poc, h->mbx, h->mby);
  258. return -1;
  259. }
  260. dst[scantab[pos]] = (level_buf[coeff_num]*mul + round) >> shift;
  261. }
  262. return 0;
  263. }
  264. void ff_cavs_filter(AVSContext *h, enum cavs_mb mb_type);
  265. void ff_cavs_load_intra_pred_luma(AVSContext *h, uint8_t *top, uint8_t **left,
  266. int block);
  267. void ff_cavs_load_intra_pred_chroma(AVSContext *h);
  268. void ff_cavs_modify_mb_i(AVSContext *h, int *pred_mode_uv);
  269. void ff_cavs_inter(AVSContext *h, enum cavs_mb mb_type);
  270. void ff_cavs_mv(AVSContext *h, enum cavs_mv_loc nP, enum cavs_mv_loc nC,
  271. enum cavs_mv_pred mode, enum cavs_block size, int ref);
  272. void ff_cavs_init_mb(AVSContext *h);
  273. int ff_cavs_next_mb(AVSContext *h);
  274. void ff_cavs_init_pic(AVSContext *h);
  275. void ff_cavs_init_top_lines(AVSContext *h);
  276. int ff_cavs_init(AVCodecContext *avctx);
  277. int ff_cavs_end (AVCodecContext *avctx);
  278. #endif /* AVCODEC_CAVS_H */