cavs.h 9.0 KB

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