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.

319 lines
8.9KB

  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 stream_revision; ///<0 for samples from 2006, 1 for rm52j encoder
  147. int progressive;
  148. int pic_structure;
  149. int skip_mode_flag; ///< select between skip_count or one skip_flag per MB
  150. int loop_filter_disable;
  151. int alpha_offset, beta_offset;
  152. int ref_flag;
  153. int mbx, mby, mbidx; ///< macroblock coordinates
  154. int flags; ///< availability flags of neighbouring macroblocks
  155. int stc; ///< last start code
  156. uint8_t *cy, *cu, *cv; ///< current MB sample pointers
  157. int left_qp;
  158. uint8_t *top_qp;
  159. /** mv motion vector cache
  160. 0: D3 B2 B3 C2
  161. 4: A1 X0 X1 -
  162. 8: A3 X2 X3 -
  163. X are the vectors in the current macroblock (5,6,9,10)
  164. A is the macroblock to the left (4,8)
  165. B is the macroblock to the top (1,2)
  166. C is the macroblock to the top-right (3)
  167. D is the macroblock to the top-left (0)
  168. the same is repeated for backward motion vectors */
  169. cavs_vector mv[2*4*3];
  170. cavs_vector *top_mv[2];
  171. cavs_vector *col_mv;
  172. /** luma pred mode cache
  173. 0: -- B2 B3
  174. 3: A1 X0 X1
  175. 6: A3 X2 X3 */
  176. int pred_mode_Y[3*3];
  177. int *top_pred_Y;
  178. int l_stride, c_stride;
  179. int luma_scan[4];
  180. int qp;
  181. int qp_fixed;
  182. int cbp;
  183. ScanTable scantable;
  184. /** intra prediction is done with un-deblocked samples
  185. they are saved here before deblocking the MB */
  186. uint8_t *top_border_y, *top_border_u, *top_border_v;
  187. uint8_t left_border_y[26], left_border_u[10], left_border_v[10];
  188. uint8_t intern_border_y[26];
  189. uint8_t topleft_border_y, topleft_border_u, topleft_border_v;
  190. void (*intra_pred_l[8])(uint8_t *d,uint8_t *top,uint8_t *left,int stride);
  191. void (*intra_pred_c[7])(uint8_t *d,uint8_t *top,uint8_t *left,int stride);
  192. uint8_t *col_type_base;
  193. /* scaling factors for MV prediction */
  194. int sym_factor; ///< for scaling in symmetrical B block
  195. int direct_den[2]; ///< for scaling in direct B block
  196. int scale_den[2]; ///< for scaling neighbouring MVs
  197. int got_keyframe;
  198. DCTELEM *block;
  199. } AVSContext;
  200. extern const uint8_t ff_cavs_dequant_shift[64];
  201. extern const uint16_t ff_cavs_dequant_mul[64];
  202. extern const struct dec_2dvlc ff_cavs_intra_dec[7];
  203. extern const struct dec_2dvlc ff_cavs_inter_dec[7];
  204. extern const struct dec_2dvlc ff_cavs_chroma_dec[5];
  205. extern const uint8_t ff_cavs_chroma_qp[64];
  206. extern const uint8_t ff_cavs_scan3x3[4];
  207. extern const uint8_t ff_cavs_partition_flags[30];
  208. extern const int_fast8_t ff_left_modifier_l[8];
  209. extern const int_fast8_t ff_top_modifier_l[8];
  210. extern const int_fast8_t ff_left_modifier_c[7];
  211. extern const int_fast8_t ff_top_modifier_c[7];
  212. extern const cavs_vector ff_cavs_intra_mv;
  213. extern const cavs_vector ff_cavs_un_mv;
  214. extern const cavs_vector ff_cavs_dir_mv;
  215. static inline void modify_pred(const int_fast8_t *mod_table, int *mode) {
  216. *mode = mod_table[*mode];
  217. if(*mode < 0) {
  218. av_log(NULL, AV_LOG_ERROR, "Illegal intra prediction mode\n");
  219. *mode = 0;
  220. }
  221. }
  222. static inline void set_intra_mode_default(AVSContext *h) {
  223. if(h->stream_revision > 0) {
  224. h->pred_mode_Y[3] = h->pred_mode_Y[6] = NOT_AVAIL;
  225. h->top_pred_Y[h->mbx*2+0] = h->top_pred_Y[h->mbx*2+1] = NOT_AVAIL;
  226. } else {
  227. h->pred_mode_Y[3] = h->pred_mode_Y[6] = INTRA_L_LP;
  228. h->top_pred_Y[h->mbx*2+0] = h->top_pred_Y[h->mbx*2+1] = INTRA_L_LP;
  229. }
  230. }
  231. static inline void set_mvs(cavs_vector *mv, enum cavs_block size) {
  232. switch(size) {
  233. case BLK_16X16:
  234. mv[MV_STRIDE ] = mv[0];
  235. mv[MV_STRIDE+1] = mv[0];
  236. case BLK_16X8:
  237. mv[1] = mv[0];
  238. break;
  239. case BLK_8X16:
  240. mv[MV_STRIDE] = mv[0];
  241. break;
  242. }
  243. }
  244. static inline void set_mv_intra(AVSContext *h) {
  245. h->mv[MV_FWD_X0] = ff_cavs_intra_mv;
  246. set_mvs(&h->mv[MV_FWD_X0], BLK_16X16);
  247. h->mv[MV_BWD_X0] = ff_cavs_intra_mv;
  248. set_mvs(&h->mv[MV_BWD_X0], BLK_16X16);
  249. if(h->pic_type != FF_B_TYPE)
  250. h->col_type_base[h->mbidx] = I_8X8;
  251. }
  252. static inline int dequant(AVSContext *h, DCTELEM *level_buf, uint8_t *run_buf,
  253. DCTELEM *dst, int mul, int shift, int coeff_num) {
  254. int round = 1 << (shift - 1);
  255. int pos = -1;
  256. const uint8_t *scantab = h->scantable.permutated;
  257. /* inverse scan and dequantization */
  258. while(--coeff_num >= 0){
  259. pos += run_buf[coeff_num];
  260. if(pos > 63) {
  261. av_log(h->s.avctx, AV_LOG_ERROR,
  262. "position out of block bounds at pic %d MB(%d,%d)\n",
  263. h->picture.poc, h->mbx, h->mby);
  264. return -1;
  265. }
  266. dst[scantab[pos]] = (level_buf[coeff_num]*mul + round) >> shift;
  267. }
  268. return 0;
  269. }
  270. void ff_cavs_filter(AVSContext *h, enum cavs_mb mb_type);
  271. void ff_cavs_load_intra_pred_luma(AVSContext *h, uint8_t *top, uint8_t **left,
  272. int block);
  273. void ff_cavs_load_intra_pred_chroma(AVSContext *h);
  274. void ff_cavs_modify_mb_i(AVSContext *h, int *pred_mode_uv);
  275. void ff_cavs_inter(AVSContext *h, enum cavs_mb mb_type);
  276. void ff_cavs_mv(AVSContext *h, enum cavs_mv_loc nP, enum cavs_mv_loc nC,
  277. enum cavs_mv_pred mode, enum cavs_block size, int ref);
  278. void ff_cavs_init_mb(AVSContext *h);
  279. int ff_cavs_next_mb(AVSContext *h);
  280. void ff_cavs_init_pic(AVSContext *h);
  281. void ff_cavs_init_top_lines(AVSContext *h);
  282. int ff_cavs_init(AVCodecContext *avctx);
  283. int ff_cavs_end (AVCodecContext *avctx);
  284. #endif /* AVCODEC_CAVS_H */