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.

481 lines
14KB

  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 load_intra_pred_luma(AVSContext *h, uint8_t *top,
  217. uint8_t **left, int block) {
  218. int i;
  219. switch(block) {
  220. case 0:
  221. *left = h->left_border_y;
  222. h->left_border_y[0] = h->left_border_y[1];
  223. memset(&h->left_border_y[17],h->left_border_y[16],9);
  224. memcpy(&top[1],&h->top_border_y[h->mbx*16],16);
  225. top[17] = top[16];
  226. top[0] = top[1];
  227. if((h->flags & A_AVAIL) && (h->flags & B_AVAIL))
  228. h->left_border_y[0] = top[0] = h->topleft_border_y;
  229. break;
  230. case 1:
  231. *left = h->intern_border_y;
  232. for(i=0;i<8;i++)
  233. h->intern_border_y[i+1] = *(h->cy + 7 + i*h->l_stride);
  234. memset(&h->intern_border_y[9],h->intern_border_y[8],9);
  235. h->intern_border_y[0] = h->intern_border_y[1];
  236. memcpy(&top[1],&h->top_border_y[h->mbx*16+8],8);
  237. if(h->flags & C_AVAIL)
  238. memcpy(&top[9],&h->top_border_y[(h->mbx + 1)*16],8);
  239. else
  240. memset(&top[9],top[8],9);
  241. top[17] = top[16];
  242. top[0] = top[1];
  243. if(h->flags & B_AVAIL)
  244. h->intern_border_y[0] = top[0] = h->top_border_y[h->mbx*16+7];
  245. break;
  246. case 2:
  247. *left = &h->left_border_y[8];
  248. memcpy(&top[1],h->cy + 7*h->l_stride,16);
  249. top[17] = top[16];
  250. top[0] = top[1];
  251. if(h->flags & A_AVAIL)
  252. top[0] = h->left_border_y[8];
  253. break;
  254. case 3:
  255. *left = &h->intern_border_y[8];
  256. for(i=0;i<8;i++)
  257. h->intern_border_y[i+9] = *(h->cy + 7 + (i+8)*h->l_stride);
  258. memset(&h->intern_border_y[17],h->intern_border_y[16],9);
  259. memcpy(&top[0],h->cy + 7 + 7*h->l_stride,9);
  260. memset(&top[9],top[8],9);
  261. break;
  262. }
  263. }
  264. static inline void load_intra_pred_chroma(AVSContext *h) {
  265. /* extend borders by one pixel */
  266. h->left_border_u[9] = h->left_border_u[8];
  267. h->left_border_v[9] = h->left_border_v[8];
  268. h->top_border_u[h->mbx*10+9] = h->top_border_u[h->mbx*10+8];
  269. h->top_border_v[h->mbx*10+9] = h->top_border_v[h->mbx*10+8];
  270. if(h->mbx && h->mby) {
  271. h->top_border_u[h->mbx*10] = h->left_border_u[0] = h->topleft_border_u;
  272. h->top_border_v[h->mbx*10] = h->left_border_v[0] = h->topleft_border_v;
  273. } else {
  274. h->left_border_u[0] = h->left_border_u[1];
  275. h->left_border_v[0] = h->left_border_v[1];
  276. h->top_border_u[h->mbx*10] = h->top_border_u[h->mbx*10+1];
  277. h->top_border_v[h->mbx*10] = h->top_border_v[h->mbx*10+1];
  278. }
  279. }
  280. static inline void modify_pred(const int_fast8_t *mod_table, int *mode) {
  281. *mode = mod_table[*mode];
  282. if(*mode < 0) {
  283. av_log(NULL, AV_LOG_ERROR, "Illegal intra prediction mode\n");
  284. *mode = 0;
  285. }
  286. }
  287. static inline void modify_mb_i(AVSContext *h, int *pred_mode_uv) {
  288. /* save pred modes before they get modified */
  289. h->pred_mode_Y[3] = h->pred_mode_Y[5];
  290. h->pred_mode_Y[6] = h->pred_mode_Y[8];
  291. h->top_pred_Y[h->mbx*2+0] = h->pred_mode_Y[7];
  292. h->top_pred_Y[h->mbx*2+1] = h->pred_mode_Y[8];
  293. /* modify pred modes according to availability of neighbour samples */
  294. if(!(h->flags & A_AVAIL)) {
  295. modify_pred(ff_left_modifier_l, &h->pred_mode_Y[4] );
  296. modify_pred(ff_left_modifier_l, &h->pred_mode_Y[7] );
  297. modify_pred(ff_left_modifier_c, pred_mode_uv );
  298. }
  299. if(!(h->flags & B_AVAIL)) {
  300. modify_pred(ff_top_modifier_l, &h->pred_mode_Y[4] );
  301. modify_pred(ff_top_modifier_l, &h->pred_mode_Y[5] );
  302. modify_pred(ff_top_modifier_c, pred_mode_uv );
  303. }
  304. }
  305. static inline void set_intra_mode_default(AVSContext *h) {
  306. h->pred_mode_Y[3] = h->pred_mode_Y[6] = INTRA_L_LP;
  307. h->top_pred_Y[h->mbx*2+0] = h->top_pred_Y[h->mbx*2+1] = INTRA_L_LP;
  308. }
  309. static inline void set_mvs(vector_t *mv, enum block_t size) {
  310. switch(size) {
  311. case BLK_16X16:
  312. mv[MV_STRIDE ] = mv[0];
  313. mv[MV_STRIDE+1] = mv[0];
  314. case BLK_16X8:
  315. mv[1] = mv[0];
  316. break;
  317. case BLK_8X16:
  318. mv[MV_STRIDE] = mv[0];
  319. break;
  320. }
  321. }
  322. static inline void set_mv_intra(AVSContext *h) {
  323. h->mv[MV_FWD_X0] = ff_cavs_intra_mv;
  324. set_mvs(&h->mv[MV_FWD_X0], BLK_16X16);
  325. h->mv[MV_BWD_X0] = ff_cavs_intra_mv;
  326. set_mvs(&h->mv[MV_BWD_X0], BLK_16X16);
  327. if(h->pic_type != FF_B_TYPE)
  328. *h->col_type = I_8X8;
  329. }
  330. /**
  331. * initialise predictors for motion vectors and intra prediction
  332. */
  333. static inline void init_mb(AVSContext *h) {
  334. int i;
  335. /* copy predictors from top line (MB B and C) into cache */
  336. for(i=0;i<3;i++) {
  337. h->mv[MV_FWD_B2+i] = h->top_mv[0][h->mbx*2+i];
  338. h->mv[MV_BWD_B2+i] = h->top_mv[1][h->mbx*2+i];
  339. }
  340. h->pred_mode_Y[1] = h->top_pred_Y[h->mbx*2+0];
  341. h->pred_mode_Y[2] = h->top_pred_Y[h->mbx*2+1];
  342. /* clear top predictors if MB B is not available */
  343. if(!(h->flags & B_AVAIL)) {
  344. h->mv[MV_FWD_B2] = ff_cavs_un_mv;
  345. h->mv[MV_FWD_B3] = ff_cavs_un_mv;
  346. h->mv[MV_BWD_B2] = ff_cavs_un_mv;
  347. h->mv[MV_BWD_B3] = ff_cavs_un_mv;
  348. h->pred_mode_Y[1] = h->pred_mode_Y[2] = NOT_AVAIL;
  349. h->flags &= ~(C_AVAIL|D_AVAIL);
  350. } else if(h->mbx) {
  351. h->flags |= D_AVAIL;
  352. }
  353. if(h->mbx == h->mb_width-1) //MB C not available
  354. h->flags &= ~C_AVAIL;
  355. /* clear top-right predictors if MB C is not available */
  356. if(!(h->flags & C_AVAIL)) {
  357. h->mv[MV_FWD_C2] = ff_cavs_un_mv;
  358. h->mv[MV_BWD_C2] = ff_cavs_un_mv;
  359. }
  360. /* clear top-left predictors if MB D is not available */
  361. if(!(h->flags & D_AVAIL)) {
  362. h->mv[MV_FWD_D3] = ff_cavs_un_mv;
  363. h->mv[MV_BWD_D3] = ff_cavs_un_mv;
  364. }
  365. /* set pointer for co-located macroblock type */
  366. h->col_type = &h->col_type_base[h->mby*h->mb_width + h->mbx];
  367. }
  368. /**
  369. * save predictors for later macroblocks and increase
  370. * macroblock address
  371. * @returns 0 if end of frame is reached, 1 otherwise
  372. */
  373. static inline int next_mb(AVSContext *h) {
  374. int i;
  375. h->flags |= A_AVAIL;
  376. h->cy += 16;
  377. h->cu += 8;
  378. h->cv += 8;
  379. /* copy mvs as predictors to the left */
  380. for(i=0;i<=20;i+=4)
  381. h->mv[i] = h->mv[i+2];
  382. /* copy bottom mvs from cache to top line */
  383. h->top_mv[0][h->mbx*2+0] = h->mv[MV_FWD_X2];
  384. h->top_mv[0][h->mbx*2+1] = h->mv[MV_FWD_X3];
  385. h->top_mv[1][h->mbx*2+0] = h->mv[MV_BWD_X2];
  386. h->top_mv[1][h->mbx*2+1] = h->mv[MV_BWD_X3];
  387. /* next MB address */
  388. h->mbx++;
  389. if(h->mbx == h->mb_width) { //new mb line
  390. h->flags = B_AVAIL|C_AVAIL;
  391. /* clear left pred_modes */
  392. h->pred_mode_Y[3] = h->pred_mode_Y[6] = NOT_AVAIL;
  393. /* clear left mv predictors */
  394. for(i=0;i<=20;i+=4)
  395. h->mv[i] = ff_cavs_un_mv;
  396. h->mbx = 0;
  397. h->mby++;
  398. /* re-calculate sample pointers */
  399. h->cy = h->picture.data[0] + h->mby*16*h->l_stride;
  400. h->cu = h->picture.data[1] + h->mby*8*h->c_stride;
  401. h->cv = h->picture.data[2] + h->mby*8*h->c_stride;
  402. if(h->mby == h->mb_height) { //frame end
  403. return 0;
  404. } else {
  405. //check_for_slice(h);
  406. }
  407. }
  408. return 1;
  409. }
  410. static inline int dequant(AVSContext *h, DCTELEM *level_buf, uint8_t *run_buf,
  411. DCTELEM *dst, int mul, int shift, int coeff_num) {
  412. int round = 1 << (shift - 1);
  413. int pos = -1;
  414. const uint8_t *scantab = h->scantable.permutated;
  415. /* inverse scan and dequantization */
  416. while(--coeff_num >= 0){
  417. pos += run_buf[coeff_num];
  418. if(pos > 63) {
  419. av_log(h->s.avctx, AV_LOG_ERROR,
  420. "position out of block bounds at pic %d MB(%d,%d)\n",
  421. h->picture.poc, h->mbx, h->mby);
  422. return -1;
  423. }
  424. dst[scantab[pos]] = (level_buf[coeff_num]*mul + round) >> shift;
  425. }
  426. return 0;
  427. }
  428. void ff_cavs_filter(AVSContext *h, enum mb_t mb_type);
  429. void ff_cavs_inter(AVSContext *h, enum mb_t mb_type);
  430. void ff_cavs_mv(AVSContext *h, enum mv_loc_t nP, enum mv_loc_t nC,
  431. enum mv_pred_t mode, enum block_t size, int ref);
  432. void ff_cavs_init_pic(AVSContext *h);
  433. void ff_cavs_init_top_lines(AVSContext *h);
  434. int ff_cavs_init(AVCodecContext *avctx);
  435. int ff_cavs_end (AVCodecContext *avctx);
  436. #endif /* CAVS_H */