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.

315 lines
8.6KB

  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 CAVS_H
  22. #define CAVS_H
  23. #include "dsputil.h"
  24. #include "mpegvideo.h"
  25. #define SLICE_MIN_START_CODE 0x00000101
  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 mb_t {
  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 sub_mb_t {
  65. B_SUB_DIRECT,
  66. B_SUB_FWD,
  67. B_SUB_BWD,
  68. B_SUB_SYM
  69. };
  70. enum intra_luma_t {
  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 intra_chroma_t {
  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 mv_pred_t {
  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 block_t {
  98. BLK_16X16,
  99. BLK_16X8,
  100. BLK_8X16,
  101. BLK_8X8
  102. };
  103. enum mv_loc_t {
  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. } vector_t;
  131. typedef struct dec_2dvlc_t {
  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. } dec_2dvlc_t;
  138. typedef struct {
  139. MpegEncContext s;
  140. Picture picture; ///< currently decoded frame
  141. Picture DPB[2]; ///< reference frames
  142. int dist[2]; ///< temporal distances from current frame to ref frames
  143. int profile, level;
  144. int aspect_ratio;
  145. int mb_width, mb_height;
  146. int pic_type;
  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; ///< 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. vector_t mv[2*4*3];
  170. vector_t *top_mv[2];
  171. vector_t *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. uint8_t *col_type;
  194. /* scaling factors for MV prediction */
  195. int sym_factor; ///< for scaling in symmetrical B block
  196. int direct_den[2]; ///< for scaling in direct B block
  197. int scale_den[2]; ///< for scaling neighbouring MVs
  198. int got_keyframe;
  199. DCTELEM *block;
  200. } AVSContext;
  201. extern const uint8_t ff_cavs_dequant_shift[64];
  202. extern const uint16_t ff_cavs_dequant_mul[64];
  203. extern const dec_2dvlc_t ff_cavs_intra_dec[7];
  204. extern const dec_2dvlc_t ff_cavs_inter_dec[7];
  205. extern const dec_2dvlc_t ff_cavs_chroma_dec[5];
  206. extern const uint8_t ff_cavs_chroma_qp[64];
  207. extern const uint8_t ff_cavs_scan3x3[4];
  208. extern const uint8_t ff_cavs_partition_flags[30];
  209. extern const int_fast8_t ff_left_modifier_l[8];
  210. extern const int_fast8_t ff_top_modifier_l[8];
  211. extern const int_fast8_t ff_left_modifier_c[7];
  212. extern const int_fast8_t ff_top_modifier_c[7];
  213. extern const vector_t ff_cavs_intra_mv;
  214. extern const vector_t ff_cavs_un_mv;
  215. extern const vector_t ff_cavs_dir_mv;
  216. static inline void modify_pred(const int_fast8_t *mod_table, int *mode) {
  217. *mode = mod_table[*mode];
  218. if(*mode < 0) {
  219. av_log(NULL, AV_LOG_ERROR, "Illegal intra prediction mode\n");
  220. *mode = 0;
  221. }
  222. }
  223. static inline void set_intra_mode_default(AVSContext *h) {
  224. h->pred_mode_Y[3] = h->pred_mode_Y[6] = INTRA_L_LP;
  225. h->top_pred_Y[h->mbx*2+0] = h->top_pred_Y[h->mbx*2+1] = INTRA_L_LP;
  226. }
  227. static inline void set_mvs(vector_t *mv, enum block_t size) {
  228. switch(size) {
  229. case BLK_16X16:
  230. mv[MV_STRIDE ] = mv[0];
  231. mv[MV_STRIDE+1] = mv[0];
  232. case BLK_16X8:
  233. mv[1] = mv[0];
  234. break;
  235. case BLK_8X16:
  236. mv[MV_STRIDE] = mv[0];
  237. break;
  238. }
  239. }
  240. static inline void set_mv_intra(AVSContext *h) {
  241. h->mv[MV_FWD_X0] = ff_cavs_intra_mv;
  242. set_mvs(&h->mv[MV_FWD_X0], BLK_16X16);
  243. h->mv[MV_BWD_X0] = ff_cavs_intra_mv;
  244. set_mvs(&h->mv[MV_BWD_X0], BLK_16X16);
  245. if(h->pic_type != FF_B_TYPE)
  246. *h->col_type = I_8X8;
  247. }
  248. static inline int dequant(AVSContext *h, DCTELEM *level_buf, uint8_t *run_buf,
  249. DCTELEM *dst, int mul, int shift, int coeff_num) {
  250. int round = 1 << (shift - 1);
  251. int pos = -1;
  252. const uint8_t *scantab = h->scantable.permutated;
  253. /* inverse scan and dequantization */
  254. while(--coeff_num >= 0){
  255. pos += run_buf[coeff_num];
  256. if(pos > 63) {
  257. av_log(h->s.avctx, AV_LOG_ERROR,
  258. "position out of block bounds at pic %d MB(%d,%d)\n",
  259. h->picture.poc, h->mbx, h->mby);
  260. return -1;
  261. }
  262. dst[scantab[pos]] = (level_buf[coeff_num]*mul + round) >> shift;
  263. }
  264. return 0;
  265. }
  266. void ff_cavs_filter(AVSContext *h, enum mb_t mb_type);
  267. void ff_cavs_load_intra_pred_luma(AVSContext *h, uint8_t *top, uint8_t **left,
  268. int block);
  269. void ff_cavs_load_intra_pred_chroma(AVSContext *h);
  270. void ff_cavs_modify_mb_i(AVSContext *h, int *pred_mode_uv);
  271. void ff_cavs_inter(AVSContext *h, enum mb_t mb_type);
  272. void ff_cavs_mv(AVSContext *h, enum mv_loc_t nP, enum mv_loc_t nC,
  273. enum mv_pred_t mode, enum block_t size, int ref);
  274. void ff_cavs_init_mb(AVSContext *h);
  275. int ff_cavs_next_mb(AVSContext *h);
  276. void ff_cavs_init_pic(AVSContext *h);
  277. void ff_cavs_init_top_lines(AVSContext *h);
  278. int ff_cavs_init(AVCodecContext *avctx);
  279. int ff_cavs_end (AVCodecContext *avctx);
  280. #endif /* CAVS_H */