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.

725 lines
25KB

  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. /**
  22. * @file
  23. * Chinese AVS video (AVS1-P2, JiZhun profile) decoder
  24. * @author Stefan Gehrer <stefan.gehrer@gmx.de>
  25. */
  26. #include "avcodec.h"
  27. #include "get_bits.h"
  28. #include "golomb.h"
  29. #include "cavs.h"
  30. static const uint8_t mv_scan[4] = {
  31. MV_FWD_X0,MV_FWD_X1,
  32. MV_FWD_X2,MV_FWD_X3
  33. };
  34. static const uint8_t cbp_tab[64][2] = {
  35. {63, 0},{15,15},{31,63},{47,31},{ 0,16},{14,32},{13,47},{11,13},
  36. { 7,14},{ 5,11},{10,12},{ 8, 5},{12,10},{61, 7},{ 4,48},{55, 3},
  37. { 1, 2},{ 2, 8},{59, 4},{ 3, 1},{62,61},{ 9,55},{ 6,59},{29,62},
  38. {45,29},{51,27},{23,23},{39,19},{27,30},{46,28},{53, 9},{30, 6},
  39. {43,60},{37,21},{60,44},{16,26},{21,51},{28,35},{19,18},{35,20},
  40. {42,24},{26,53},{44,17},{32,37},{58,39},{24,45},{20,58},{17,43},
  41. {18,42},{48,46},{22,36},{33,33},{25,34},{49,40},{40,52},{36,49},
  42. {34,50},{50,56},{52,25},{54,22},{41,54},{56,57},{38,41},{57,38}
  43. };
  44. /*****************************************************************************
  45. *
  46. * motion vector prediction
  47. *
  48. ****************************************************************************/
  49. static inline void store_mvs(AVSContext *h) {
  50. h->col_mv[h->mbidx*4 + 0] = h->mv[MV_FWD_X0];
  51. h->col_mv[h->mbidx*4 + 1] = h->mv[MV_FWD_X1];
  52. h->col_mv[h->mbidx*4 + 2] = h->mv[MV_FWD_X2];
  53. h->col_mv[h->mbidx*4 + 3] = h->mv[MV_FWD_X3];
  54. }
  55. static inline void mv_pred_direct(AVSContext *h, cavs_vector *pmv_fw,
  56. cavs_vector *col_mv) {
  57. cavs_vector *pmv_bw = pmv_fw + MV_BWD_OFFS;
  58. int den = h->direct_den[col_mv->ref];
  59. int m = col_mv->x >> 31;
  60. pmv_fw->dist = h->dist[1];
  61. pmv_bw->dist = h->dist[0];
  62. pmv_fw->ref = 1;
  63. pmv_bw->ref = 0;
  64. /* scale the co-located motion vector according to its temporal span */
  65. pmv_fw->x = (((den+(den*col_mv->x*pmv_fw->dist^m)-m-1)>>14)^m)-m;
  66. pmv_bw->x = m-(((den+(den*col_mv->x*pmv_bw->dist^m)-m-1)>>14)^m);
  67. m = col_mv->y >> 31;
  68. pmv_fw->y = (((den+(den*col_mv->y*pmv_fw->dist^m)-m-1)>>14)^m)-m;
  69. pmv_bw->y = m-(((den+(den*col_mv->y*pmv_bw->dist^m)-m-1)>>14)^m);
  70. }
  71. static inline void mv_pred_sym(AVSContext *h, cavs_vector *src, enum cavs_block size) {
  72. cavs_vector *dst = src + MV_BWD_OFFS;
  73. /* backward mv is the scaled and negated forward mv */
  74. dst->x = -((src->x * h->sym_factor + 256) >> 9);
  75. dst->y = -((src->y * h->sym_factor + 256) >> 9);
  76. dst->ref = 0;
  77. dst->dist = h->dist[0];
  78. set_mvs(dst, size);
  79. }
  80. /*****************************************************************************
  81. *
  82. * residual data decoding
  83. *
  84. ****************************************************************************/
  85. /** kth-order exponential golomb code */
  86. static inline int get_ue_code(GetBitContext *gb, int order) {
  87. if(order) {
  88. int ret = get_ue_golomb(gb) << order;
  89. return ret + get_bits(gb,order);
  90. }
  91. return get_ue_golomb(gb);
  92. }
  93. /**
  94. * decode coefficients from one 8x8 block, dequantize, inverse transform
  95. * and add them to sample block
  96. * @param r pointer to 2D VLC table
  97. * @param esc_golomb_order escape codes are k-golomb with this order k
  98. * @param qp quantizer
  99. * @param dst location of sample block
  100. * @param stride line stride in frame buffer
  101. */
  102. static int decode_residual_block(AVSContext *h, GetBitContext *gb,
  103. const struct dec_2dvlc *r, int esc_golomb_order,
  104. int qp, uint8_t *dst, int stride) {
  105. int i, level_code, esc_code, level, run, mask;
  106. DCTELEM level_buf[65];
  107. uint8_t run_buf[65];
  108. DCTELEM *block = h->block;
  109. for(i=0;i<65;i++) {
  110. level_code = get_ue_code(gb,r->golomb_order);
  111. if(level_code >= ESCAPE_CODE) {
  112. run = ((level_code - ESCAPE_CODE) >> 1) + 1;
  113. esc_code = get_ue_code(gb,esc_golomb_order);
  114. level = esc_code + (run > r->max_run ? 1 : r->level_add[run]);
  115. while(level > r->inc_limit)
  116. r++;
  117. mask = -(level_code & 1);
  118. level = (level^mask) - mask;
  119. } else {
  120. level = r->rltab[level_code][0];
  121. if(!level) //end of block signal
  122. break;
  123. run = r->rltab[level_code][1];
  124. r += r->rltab[level_code][2];
  125. }
  126. level_buf[i] = level;
  127. run_buf[i] = run;
  128. }
  129. if(dequant(h,level_buf, run_buf, block, ff_cavs_dequant_mul[qp],
  130. ff_cavs_dequant_shift[qp], i))
  131. return -1;
  132. h->cdsp.cavs_idct8_add(dst,block,stride);
  133. h->s.dsp.clear_block(block);
  134. return 0;
  135. }
  136. static inline void decode_residual_chroma(AVSContext *h) {
  137. if(h->cbp & (1<<4))
  138. decode_residual_block(h,&h->s.gb,ff_cavs_chroma_dec,0,
  139. ff_cavs_chroma_qp[h->qp],h->cu,h->c_stride);
  140. if(h->cbp & (1<<5))
  141. decode_residual_block(h,&h->s.gb,ff_cavs_chroma_dec,0,
  142. ff_cavs_chroma_qp[h->qp],h->cv,h->c_stride);
  143. }
  144. static inline int decode_residual_inter(AVSContext *h) {
  145. int block;
  146. /* get coded block pattern */
  147. int cbp= get_ue_golomb(&h->s.gb);
  148. if(cbp > 63){
  149. av_log(h->s.avctx, AV_LOG_ERROR, "illegal inter cbp\n");
  150. return -1;
  151. }
  152. h->cbp = cbp_tab[cbp][1];
  153. /* get quantizer */
  154. if(h->cbp && !h->qp_fixed)
  155. h->qp = (h->qp + get_se_golomb(&h->s.gb)) & 63;
  156. for(block=0;block<4;block++)
  157. if(h->cbp & (1<<block))
  158. decode_residual_block(h,&h->s.gb,ff_cavs_inter_dec,0,h->qp,
  159. h->cy + h->luma_scan[block], h->l_stride);
  160. decode_residual_chroma(h);
  161. return 0;
  162. }
  163. /*****************************************************************************
  164. *
  165. * macroblock level
  166. *
  167. ****************************************************************************/
  168. static int decode_mb_i(AVSContext *h, int cbp_code) {
  169. GetBitContext *gb = &h->s.gb;
  170. int block, pred_mode_uv;
  171. uint8_t top[18];
  172. uint8_t *left = NULL;
  173. uint8_t *d;
  174. ff_cavs_init_mb(h);
  175. /* get intra prediction modes from stream */
  176. for(block=0;block<4;block++) {
  177. int nA,nB,predpred;
  178. int pos = ff_cavs_scan3x3[block];
  179. nA = h->pred_mode_Y[pos-1];
  180. nB = h->pred_mode_Y[pos-3];
  181. predpred = FFMIN(nA,nB);
  182. if(predpred == NOT_AVAIL) // if either is not available
  183. predpred = INTRA_L_LP;
  184. if(!get_bits1(gb)){
  185. int rem_mode= get_bits(gb, 2);
  186. predpred = rem_mode + (rem_mode >= predpred);
  187. }
  188. h->pred_mode_Y[pos] = predpred;
  189. }
  190. pred_mode_uv = get_ue_golomb(gb);
  191. if(pred_mode_uv > 6) {
  192. av_log(h->s.avctx, AV_LOG_ERROR, "illegal intra chroma pred mode\n");
  193. return -1;
  194. }
  195. ff_cavs_modify_mb_i(h, &pred_mode_uv);
  196. /* get coded block pattern */
  197. if(h->pic_type == FF_I_TYPE)
  198. cbp_code = get_ue_golomb(gb);
  199. if(cbp_code > 63){
  200. av_log(h->s.avctx, AV_LOG_ERROR, "illegal intra cbp\n");
  201. return -1;
  202. }
  203. h->cbp = cbp_tab[cbp_code][0];
  204. if(h->cbp && !h->qp_fixed)
  205. h->qp = (h->qp + get_se_golomb(gb)) & 63; //qp_delta
  206. /* luma intra prediction interleaved with residual decode/transform/add */
  207. for(block=0;block<4;block++) {
  208. d = h->cy + h->luma_scan[block];
  209. ff_cavs_load_intra_pred_luma(h, top, &left, block);
  210. h->intra_pred_l[h->pred_mode_Y[ff_cavs_scan3x3[block]]]
  211. (d, top, left, h->l_stride);
  212. if(h->cbp & (1<<block))
  213. decode_residual_block(h,gb,ff_cavs_intra_dec,1,h->qp,d,h->l_stride);
  214. }
  215. /* chroma intra prediction */
  216. ff_cavs_load_intra_pred_chroma(h);
  217. h->intra_pred_c[pred_mode_uv](h->cu, &h->top_border_u[h->mbx*10],
  218. h->left_border_u, h->c_stride);
  219. h->intra_pred_c[pred_mode_uv](h->cv, &h->top_border_v[h->mbx*10],
  220. h->left_border_v, h->c_stride);
  221. decode_residual_chroma(h);
  222. ff_cavs_filter(h,I_8X8);
  223. set_mv_intra(h);
  224. return 0;
  225. }
  226. static void decode_mb_p(AVSContext *h, enum cavs_mb mb_type) {
  227. GetBitContext *gb = &h->s.gb;
  228. int ref[4];
  229. ff_cavs_init_mb(h);
  230. switch(mb_type) {
  231. case P_SKIP:
  232. ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_PSKIP, BLK_16X16, 0);
  233. break;
  234. case P_16X16:
  235. ref[0] = h->ref_flag ? 0 : get_bits1(gb);
  236. ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_MEDIAN, BLK_16X16,ref[0]);
  237. break;
  238. case P_16X8:
  239. ref[0] = h->ref_flag ? 0 : get_bits1(gb);
  240. ref[2] = h->ref_flag ? 0 : get_bits1(gb);
  241. ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_TOP, BLK_16X8, ref[0]);
  242. ff_cavs_mv(h, MV_FWD_X2, MV_FWD_A1, MV_PRED_LEFT, BLK_16X8, ref[2]);
  243. break;
  244. case P_8X16:
  245. ref[0] = h->ref_flag ? 0 : get_bits1(gb);
  246. ref[1] = h->ref_flag ? 0 : get_bits1(gb);
  247. ff_cavs_mv(h, MV_FWD_X0, MV_FWD_B3, MV_PRED_LEFT, BLK_8X16, ref[0]);
  248. ff_cavs_mv(h, MV_FWD_X1, MV_FWD_C2, MV_PRED_TOPRIGHT,BLK_8X16, ref[1]);
  249. break;
  250. case P_8X8:
  251. ref[0] = h->ref_flag ? 0 : get_bits1(gb);
  252. ref[1] = h->ref_flag ? 0 : get_bits1(gb);
  253. ref[2] = h->ref_flag ? 0 : get_bits1(gb);
  254. ref[3] = h->ref_flag ? 0 : get_bits1(gb);
  255. ff_cavs_mv(h, MV_FWD_X0, MV_FWD_B3, MV_PRED_MEDIAN, BLK_8X8, ref[0]);
  256. ff_cavs_mv(h, MV_FWD_X1, MV_FWD_C2, MV_PRED_MEDIAN, BLK_8X8, ref[1]);
  257. ff_cavs_mv(h, MV_FWD_X2, MV_FWD_X1, MV_PRED_MEDIAN, BLK_8X8, ref[2]);
  258. ff_cavs_mv(h, MV_FWD_X3, MV_FWD_X0, MV_PRED_MEDIAN, BLK_8X8, ref[3]);
  259. }
  260. ff_cavs_inter(h, mb_type);
  261. set_intra_mode_default(h);
  262. store_mvs(h);
  263. if(mb_type != P_SKIP)
  264. decode_residual_inter(h);
  265. ff_cavs_filter(h,mb_type);
  266. h->col_type_base[h->mbidx] = mb_type;
  267. }
  268. static void decode_mb_b(AVSContext *h, enum cavs_mb mb_type) {
  269. int block;
  270. enum cavs_sub_mb sub_type[4];
  271. int flags;
  272. ff_cavs_init_mb(h);
  273. /* reset all MVs */
  274. h->mv[MV_FWD_X0] = ff_cavs_dir_mv;
  275. set_mvs(&h->mv[MV_FWD_X0], BLK_16X16);
  276. h->mv[MV_BWD_X0] = ff_cavs_dir_mv;
  277. set_mvs(&h->mv[MV_BWD_X0], BLK_16X16);
  278. switch(mb_type) {
  279. case B_SKIP:
  280. case B_DIRECT:
  281. if(!h->col_type_base[h->mbidx]) {
  282. /* intra MB at co-location, do in-plane prediction */
  283. ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_BSKIP, BLK_16X16, 1);
  284. ff_cavs_mv(h, MV_BWD_X0, MV_BWD_C2, MV_PRED_BSKIP, BLK_16X16, 0);
  285. } else
  286. /* direct prediction from co-located P MB, block-wise */
  287. for(block=0;block<4;block++)
  288. mv_pred_direct(h,&h->mv[mv_scan[block]],
  289. &h->col_mv[h->mbidx*4 + block]);
  290. break;
  291. case B_FWD_16X16:
  292. ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_MEDIAN, BLK_16X16, 1);
  293. break;
  294. case B_SYM_16X16:
  295. ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_MEDIAN, BLK_16X16, 1);
  296. mv_pred_sym(h, &h->mv[MV_FWD_X0], BLK_16X16);
  297. break;
  298. case B_BWD_16X16:
  299. ff_cavs_mv(h, MV_BWD_X0, MV_BWD_C2, MV_PRED_MEDIAN, BLK_16X16, 0);
  300. break;
  301. case B_8X8:
  302. for(block=0;block<4;block++)
  303. sub_type[block] = get_bits(&h->s.gb,2);
  304. for(block=0;block<4;block++) {
  305. switch(sub_type[block]) {
  306. case B_SUB_DIRECT:
  307. if(!h->col_type_base[h->mbidx]) {
  308. /* intra MB at co-location, do in-plane prediction */
  309. ff_cavs_mv(h, mv_scan[block], mv_scan[block]-3,
  310. MV_PRED_BSKIP, BLK_8X8, 1);
  311. ff_cavs_mv(h, mv_scan[block]+MV_BWD_OFFS,
  312. mv_scan[block]-3+MV_BWD_OFFS,
  313. MV_PRED_BSKIP, BLK_8X8, 0);
  314. } else
  315. mv_pred_direct(h,&h->mv[mv_scan[block]],
  316. &h->col_mv[h->mbidx*4 + block]);
  317. break;
  318. case B_SUB_FWD:
  319. ff_cavs_mv(h, mv_scan[block], mv_scan[block]-3,
  320. MV_PRED_MEDIAN, BLK_8X8, 1);
  321. break;
  322. case B_SUB_SYM:
  323. ff_cavs_mv(h, mv_scan[block], mv_scan[block]-3,
  324. MV_PRED_MEDIAN, BLK_8X8, 1);
  325. mv_pred_sym(h, &h->mv[mv_scan[block]], BLK_8X8);
  326. break;
  327. }
  328. }
  329. for(block=0;block<4;block++) {
  330. if(sub_type[block] == B_SUB_BWD)
  331. ff_cavs_mv(h, mv_scan[block]+MV_BWD_OFFS,
  332. mv_scan[block]+MV_BWD_OFFS-3,
  333. MV_PRED_MEDIAN, BLK_8X8, 0);
  334. }
  335. break;
  336. default:
  337. assert((mb_type > B_SYM_16X16) && (mb_type < B_8X8));
  338. flags = ff_cavs_partition_flags[mb_type];
  339. if(mb_type & 1) { /* 16x8 macroblock types */
  340. if(flags & FWD0)
  341. ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_TOP, BLK_16X8, 1);
  342. if(flags & SYM0)
  343. mv_pred_sym(h, &h->mv[MV_FWD_X0], BLK_16X8);
  344. if(flags & FWD1)
  345. ff_cavs_mv(h, MV_FWD_X2, MV_FWD_A1, MV_PRED_LEFT, BLK_16X8, 1);
  346. if(flags & SYM1)
  347. mv_pred_sym(h, &h->mv[MV_FWD_X2], BLK_16X8);
  348. if(flags & BWD0)
  349. ff_cavs_mv(h, MV_BWD_X0, MV_BWD_C2, MV_PRED_TOP, BLK_16X8, 0);
  350. if(flags & BWD1)
  351. ff_cavs_mv(h, MV_BWD_X2, MV_BWD_A1, MV_PRED_LEFT, BLK_16X8, 0);
  352. } else { /* 8x16 macroblock types */
  353. if(flags & FWD0)
  354. ff_cavs_mv(h, MV_FWD_X0, MV_FWD_B3, MV_PRED_LEFT, BLK_8X16, 1);
  355. if(flags & SYM0)
  356. mv_pred_sym(h, &h->mv[MV_FWD_X0], BLK_8X16);
  357. if(flags & FWD1)
  358. ff_cavs_mv(h,MV_FWD_X1,MV_FWD_C2,MV_PRED_TOPRIGHT,BLK_8X16,1);
  359. if(flags & SYM1)
  360. mv_pred_sym(h, &h->mv[MV_FWD_X1], BLK_8X16);
  361. if(flags & BWD0)
  362. ff_cavs_mv(h, MV_BWD_X0, MV_BWD_B3, MV_PRED_LEFT, BLK_8X16, 0);
  363. if(flags & BWD1)
  364. ff_cavs_mv(h,MV_BWD_X1,MV_BWD_C2,MV_PRED_TOPRIGHT,BLK_8X16,0);
  365. }
  366. }
  367. ff_cavs_inter(h, mb_type);
  368. set_intra_mode_default(h);
  369. if(mb_type != B_SKIP)
  370. decode_residual_inter(h);
  371. ff_cavs_filter(h,mb_type);
  372. }
  373. /*****************************************************************************
  374. *
  375. * slice level
  376. *
  377. ****************************************************************************/
  378. static inline int decode_slice_header(AVSContext *h, GetBitContext *gb) {
  379. if(h->stc > 0xAF)
  380. av_log(h->s.avctx, AV_LOG_ERROR, "unexpected start code 0x%02x\n", h->stc);
  381. h->mby = h->stc;
  382. h->mbidx = h->mby*h->mb_width;
  383. /* mark top macroblocks as unavailable */
  384. h->flags &= ~(B_AVAIL|C_AVAIL);
  385. if((h->mby == 0) && (!h->qp_fixed)){
  386. h->qp_fixed = get_bits1(gb);
  387. h->qp = get_bits(gb,6);
  388. }
  389. /* inter frame or second slice can have weighting params */
  390. if((h->pic_type != FF_I_TYPE) || (!h->pic_structure && h->mby >= h->mb_width/2))
  391. if(get_bits1(gb)) { //slice_weighting_flag
  392. av_log(h->s.avctx, AV_LOG_ERROR,
  393. "weighted prediction not yet supported\n");
  394. }
  395. return 0;
  396. }
  397. static inline int check_for_slice(AVSContext *h) {
  398. GetBitContext *gb = &h->s.gb;
  399. int align;
  400. if(h->mbx)
  401. return 0;
  402. align = (-get_bits_count(gb)) & 7;
  403. /* check for stuffing byte */
  404. if(!align && (show_bits(gb,8) == 0x80))
  405. align = 8;
  406. if((show_bits_long(gb,24+align) & 0xFFFFFF) == 0x000001) {
  407. skip_bits_long(gb,24+align);
  408. h->stc = get_bits(gb,8);
  409. decode_slice_header(h,gb);
  410. return 1;
  411. }
  412. return 0;
  413. }
  414. /*****************************************************************************
  415. *
  416. * frame level
  417. *
  418. ****************************************************************************/
  419. static int decode_pic(AVSContext *h) {
  420. MpegEncContext *s = &h->s;
  421. int skip_count = -1;
  422. enum cavs_mb mb_type;
  423. if (!s->context_initialized) {
  424. s->avctx->idct_algo = FF_IDCT_CAVS;
  425. if (MPV_common_init(s) < 0)
  426. return -1;
  427. ff_init_scantable(s->dsp.idct_permutation,&h->scantable,ff_zigzag_direct);
  428. }
  429. skip_bits(&s->gb,16);//bbv_dwlay
  430. if(h->stc == PIC_PB_START_CODE) {
  431. h->pic_type = get_bits(&s->gb,2) + FF_I_TYPE;
  432. if(h->pic_type > FF_B_TYPE) {
  433. av_log(s->avctx, AV_LOG_ERROR, "illegal picture type\n");
  434. return -1;
  435. }
  436. /* make sure we have the reference frames we need */
  437. if(!h->DPB[0].data[0] ||
  438. (!h->DPB[1].data[0] && h->pic_type == FF_B_TYPE))
  439. return -1;
  440. } else {
  441. h->pic_type = FF_I_TYPE;
  442. if(get_bits1(&s->gb))
  443. skip_bits(&s->gb,24);//time_code
  444. /* old sample clips were all progressive and no low_delay,
  445. bump stream revision if detected otherwise */
  446. if((s->low_delay) || !(show_bits(&s->gb,9) & 1))
  447. h->stream_revision = 1;
  448. /* similarly test top_field_first and repeat_first_field */
  449. else if(show_bits(&s->gb,11) & 3)
  450. h->stream_revision = 1;
  451. if(h->stream_revision > 0)
  452. skip_bits(&s->gb,1); //marker_bit
  453. }
  454. /* release last B frame */
  455. if(h->picture.data[0])
  456. s->avctx->release_buffer(s->avctx, (AVFrame *)&h->picture);
  457. s->avctx->get_buffer(s->avctx, (AVFrame *)&h->picture);
  458. ff_cavs_init_pic(h);
  459. h->picture.poc = get_bits(&s->gb,8)*2;
  460. /* get temporal distances and MV scaling factors */
  461. if(h->pic_type != FF_B_TYPE) {
  462. h->dist[0] = (h->picture.poc - h->DPB[0].poc + 512) % 512;
  463. } else {
  464. h->dist[0] = (h->DPB[0].poc - h->picture.poc + 512) % 512;
  465. }
  466. h->dist[1] = (h->picture.poc - h->DPB[1].poc + 512) % 512;
  467. h->scale_den[0] = h->dist[0] ? 512/h->dist[0] : 0;
  468. h->scale_den[1] = h->dist[1] ? 512/h->dist[1] : 0;
  469. if(h->pic_type == FF_B_TYPE) {
  470. h->sym_factor = h->dist[0]*h->scale_den[1];
  471. } else {
  472. h->direct_den[0] = h->dist[0] ? 16384/h->dist[0] : 0;
  473. h->direct_den[1] = h->dist[1] ? 16384/h->dist[1] : 0;
  474. }
  475. if(s->low_delay)
  476. get_ue_golomb(&s->gb); //bbv_check_times
  477. h->progressive = get_bits1(&s->gb);
  478. h->pic_structure = 1;
  479. if(!h->progressive)
  480. h->pic_structure = get_bits1(&s->gb);
  481. if(!h->pic_structure && h->stc == PIC_PB_START_CODE)
  482. skip_bits1(&s->gb); //advanced_pred_mode_disable
  483. skip_bits1(&s->gb); //top_field_first
  484. skip_bits1(&s->gb); //repeat_first_field
  485. h->qp_fixed = get_bits1(&s->gb);
  486. h->qp = get_bits(&s->gb,6);
  487. if(h->pic_type == FF_I_TYPE) {
  488. if(!h->progressive && !h->pic_structure)
  489. skip_bits1(&s->gb);//what is this?
  490. skip_bits(&s->gb,4); //reserved bits
  491. } else {
  492. if(!(h->pic_type == FF_B_TYPE && h->pic_structure == 1))
  493. h->ref_flag = get_bits1(&s->gb);
  494. skip_bits(&s->gb,4); //reserved bits
  495. h->skip_mode_flag = get_bits1(&s->gb);
  496. }
  497. h->loop_filter_disable = get_bits1(&s->gb);
  498. if(!h->loop_filter_disable && get_bits1(&s->gb)) {
  499. h->alpha_offset = get_se_golomb(&s->gb);
  500. h->beta_offset = get_se_golomb(&s->gb);
  501. } else {
  502. h->alpha_offset = h->beta_offset = 0;
  503. }
  504. if(h->pic_type == FF_I_TYPE) {
  505. do {
  506. check_for_slice(h);
  507. decode_mb_i(h, 0);
  508. } while(ff_cavs_next_mb(h));
  509. } else if(h->pic_type == FF_P_TYPE) {
  510. do {
  511. if(check_for_slice(h))
  512. skip_count = -1;
  513. if(h->skip_mode_flag && (skip_count < 0))
  514. skip_count = get_ue_golomb(&s->gb);
  515. if(h->skip_mode_flag && skip_count--) {
  516. decode_mb_p(h,P_SKIP);
  517. } else {
  518. mb_type = get_ue_golomb(&s->gb) + P_SKIP + h->skip_mode_flag;
  519. if(mb_type > P_8X8)
  520. decode_mb_i(h, mb_type - P_8X8 - 1);
  521. else
  522. decode_mb_p(h,mb_type);
  523. }
  524. } while(ff_cavs_next_mb(h));
  525. } else { /* FF_B_TYPE */
  526. do {
  527. if(check_for_slice(h))
  528. skip_count = -1;
  529. if(h->skip_mode_flag && (skip_count < 0))
  530. skip_count = get_ue_golomb(&s->gb);
  531. if(h->skip_mode_flag && skip_count--) {
  532. decode_mb_b(h,B_SKIP);
  533. } else {
  534. mb_type = get_ue_golomb(&s->gb) + B_SKIP + h->skip_mode_flag;
  535. if(mb_type > B_8X8)
  536. decode_mb_i(h, mb_type - B_8X8 - 1);
  537. else
  538. decode_mb_b(h,mb_type);
  539. }
  540. } while(ff_cavs_next_mb(h));
  541. }
  542. if(h->pic_type != FF_B_TYPE) {
  543. if(h->DPB[1].data[0])
  544. s->avctx->release_buffer(s->avctx, (AVFrame *)&h->DPB[1]);
  545. h->DPB[1] = h->DPB[0];
  546. h->DPB[0] = h->picture;
  547. memset(&h->picture,0,sizeof(Picture));
  548. }
  549. return 0;
  550. }
  551. /*****************************************************************************
  552. *
  553. * headers and interface
  554. *
  555. ****************************************************************************/
  556. static int decode_seq_header(AVSContext *h) {
  557. MpegEncContext *s = &h->s;
  558. int frame_rate_code;
  559. h->profile = get_bits(&s->gb,8);
  560. h->level = get_bits(&s->gb,8);
  561. skip_bits1(&s->gb); //progressive sequence
  562. s->width = get_bits(&s->gb,14);
  563. s->height = get_bits(&s->gb,14);
  564. skip_bits(&s->gb,2); //chroma format
  565. skip_bits(&s->gb,3); //sample_precision
  566. h->aspect_ratio = get_bits(&s->gb,4);
  567. frame_rate_code = get_bits(&s->gb,4);
  568. skip_bits(&s->gb,18);//bit_rate_lower
  569. skip_bits1(&s->gb); //marker_bit
  570. skip_bits(&s->gb,12);//bit_rate_upper
  571. s->low_delay = get_bits1(&s->gb);
  572. h->mb_width = (s->width + 15) >> 4;
  573. h->mb_height = (s->height + 15) >> 4;
  574. h->s.avctx->time_base.den = ff_frame_rate_tab[frame_rate_code].num;
  575. h->s.avctx->time_base.num = ff_frame_rate_tab[frame_rate_code].den;
  576. h->s.avctx->width = s->width;
  577. h->s.avctx->height = s->height;
  578. if(!h->top_qp)
  579. ff_cavs_init_top_lines(h);
  580. return 0;
  581. }
  582. static void cavs_flush(AVCodecContext * avctx) {
  583. AVSContext *h = avctx->priv_data;
  584. h->got_keyframe = 0;
  585. }
  586. static int cavs_decode_frame(AVCodecContext * avctx,void *data, int *data_size,
  587. AVPacket *avpkt) {
  588. const uint8_t *buf = avpkt->data;
  589. int buf_size = avpkt->size;
  590. AVSContext *h = avctx->priv_data;
  591. MpegEncContext *s = &h->s;
  592. int input_size;
  593. const uint8_t *buf_end;
  594. const uint8_t *buf_ptr;
  595. AVFrame *picture = data;
  596. uint32_t stc = -1;
  597. s->avctx = avctx;
  598. if (buf_size == 0) {
  599. if(!s->low_delay && h->DPB[0].data[0]) {
  600. *data_size = sizeof(AVPicture);
  601. *picture = *(AVFrame *) &h->DPB[0];
  602. }
  603. return 0;
  604. }
  605. buf_ptr = buf;
  606. buf_end = buf + buf_size;
  607. for(;;) {
  608. buf_ptr = ff_find_start_code(buf_ptr,buf_end, &stc);
  609. if(stc & 0xFFFFFE00)
  610. return FFMAX(0, buf_ptr - buf - s->parse_context.last_index);
  611. input_size = (buf_end - buf_ptr)*8;
  612. switch(stc) {
  613. case CAVS_START_CODE:
  614. init_get_bits(&s->gb, buf_ptr, input_size);
  615. decode_seq_header(h);
  616. break;
  617. case PIC_I_START_CODE:
  618. if(!h->got_keyframe) {
  619. if(h->DPB[0].data[0])
  620. avctx->release_buffer(avctx, (AVFrame *)&h->DPB[0]);
  621. if(h->DPB[1].data[0])
  622. avctx->release_buffer(avctx, (AVFrame *)&h->DPB[1]);
  623. h->got_keyframe = 1;
  624. }
  625. case PIC_PB_START_CODE:
  626. *data_size = 0;
  627. if(!h->got_keyframe)
  628. break;
  629. init_get_bits(&s->gb, buf_ptr, input_size);
  630. h->stc = stc;
  631. if(decode_pic(h))
  632. break;
  633. *data_size = sizeof(AVPicture);
  634. if(h->pic_type != FF_B_TYPE) {
  635. if(h->DPB[1].data[0]) {
  636. *picture = *(AVFrame *) &h->DPB[1];
  637. } else {
  638. *data_size = 0;
  639. }
  640. } else
  641. *picture = *(AVFrame *) &h->picture;
  642. break;
  643. case EXT_START_CODE:
  644. //mpeg_decode_extension(avctx,buf_ptr, input_size);
  645. break;
  646. case USER_START_CODE:
  647. //mpeg_decode_user_data(avctx,buf_ptr, input_size);
  648. break;
  649. default:
  650. if (stc <= SLICE_MAX_START_CODE) {
  651. init_get_bits(&s->gb, buf_ptr, input_size);
  652. decode_slice_header(h, &s->gb);
  653. }
  654. break;
  655. }
  656. }
  657. }
  658. AVCodec ff_cavs_decoder = {
  659. "cavs",
  660. AVMEDIA_TYPE_VIDEO,
  661. CODEC_ID_CAVS,
  662. sizeof(AVSContext),
  663. ff_cavs_init,
  664. NULL,
  665. ff_cavs_end,
  666. cavs_decode_frame,
  667. CODEC_CAP_DR1 | CODEC_CAP_DELAY,
  668. .flush= cavs_flush,
  669. .long_name= NULL_IF_CONFIG_SMALL("Chinese AVS video (AVS1-P2, JiZhun profile)"),
  670. };