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.

716 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 libavcodec/cavsdec.c
  23. * Chinese AVS video (AVS1-P2, JiZhun profile) decoder
  24. * @author Stefan Gehrer <stefan.gehrer@gmx.de>
  25. */
  26. #include "avcodec.h"
  27. #include "bitstream.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->s.dsp.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 void check_for_slice(AVSContext *h) {
  398. GetBitContext *gb = &h->s.gb;
  399. int align;
  400. if(h->mbx)
  401. return;
  402. align = (-get_bits_count(gb)) & 7;
  403. if((show_bits_long(gb,24+align) & 0xFFFFFF) == 0x000001) {
  404. skip_bits_long(gb,24+align);
  405. h->stc = get_bits(gb,8);
  406. decode_slice_header(h,gb);
  407. }
  408. }
  409. /*****************************************************************************
  410. *
  411. * frame level
  412. *
  413. ****************************************************************************/
  414. static int decode_pic(AVSContext *h) {
  415. MpegEncContext *s = &h->s;
  416. int skip_count;
  417. enum cavs_mb mb_type;
  418. if (!s->context_initialized) {
  419. s->avctx->idct_algo = FF_IDCT_CAVS;
  420. if (MPV_common_init(s) < 0)
  421. return -1;
  422. ff_init_scantable(s->dsp.idct_permutation,&h->scantable,ff_zigzag_direct);
  423. }
  424. skip_bits(&s->gb,16);//bbv_dwlay
  425. if(h->stc == PIC_PB_START_CODE) {
  426. h->pic_type = get_bits(&s->gb,2) + FF_I_TYPE;
  427. if(h->pic_type > FF_B_TYPE) {
  428. av_log(s->avctx, AV_LOG_ERROR, "illegal picture type\n");
  429. return -1;
  430. }
  431. /* make sure we have the reference frames we need */
  432. if(!h->DPB[0].data[0] ||
  433. (!h->DPB[1].data[0] && h->pic_type == FF_B_TYPE))
  434. return -1;
  435. } else {
  436. h->pic_type = FF_I_TYPE;
  437. if(get_bits1(&s->gb))
  438. skip_bits(&s->gb,24);//time_code
  439. }
  440. /* release last B frame */
  441. if(h->picture.data[0])
  442. s->avctx->release_buffer(s->avctx, (AVFrame *)&h->picture);
  443. s->avctx->get_buffer(s->avctx, (AVFrame *)&h->picture);
  444. ff_cavs_init_pic(h);
  445. h->picture.poc = get_bits(&s->gb,8)*2;
  446. /* get temporal distances and MV scaling factors */
  447. if(h->pic_type != FF_B_TYPE) {
  448. h->dist[0] = (h->picture.poc - h->DPB[0].poc + 512) % 512;
  449. } else {
  450. h->dist[0] = (h->DPB[0].poc - h->picture.poc + 512) % 512;
  451. }
  452. h->dist[1] = (h->picture.poc - h->DPB[1].poc + 512) % 512;
  453. h->scale_den[0] = h->dist[0] ? 512/h->dist[0] : 0;
  454. h->scale_den[1] = h->dist[1] ? 512/h->dist[1] : 0;
  455. if(h->pic_type == FF_B_TYPE) {
  456. h->sym_factor = h->dist[0]*h->scale_den[1];
  457. } else {
  458. h->direct_den[0] = h->dist[0] ? 16384/h->dist[0] : 0;
  459. h->direct_den[1] = h->dist[1] ? 16384/h->dist[1] : 0;
  460. }
  461. if(s->low_delay)
  462. get_ue_golomb(&s->gb); //bbv_check_times
  463. h->progressive = get_bits1(&s->gb);
  464. h->pic_structure = 1;
  465. if(!h->progressive)
  466. h->pic_structure = get_bits1(&s->gb);
  467. if(!h->pic_structure && h->stc == PIC_PB_START_CODE)
  468. skip_bits1(&s->gb); //advanced_pred_mode_disable
  469. skip_bits1(&s->gb); //top_field_first
  470. skip_bits1(&s->gb); //repeat_first_field
  471. h->qp_fixed = get_bits1(&s->gb);
  472. h->qp = get_bits(&s->gb,6);
  473. if(h->pic_type == FF_I_TYPE) {
  474. if(!h->progressive && !h->pic_structure)
  475. skip_bits1(&s->gb);//what is this?
  476. skip_bits(&s->gb,4); //reserved bits
  477. } else {
  478. if(!(h->pic_type == FF_B_TYPE && h->pic_structure == 1))
  479. h->ref_flag = get_bits1(&s->gb);
  480. skip_bits(&s->gb,4); //reserved bits
  481. h->skip_mode_flag = get_bits1(&s->gb);
  482. }
  483. h->loop_filter_disable = get_bits1(&s->gb);
  484. if(!h->loop_filter_disable && get_bits1(&s->gb)) {
  485. h->alpha_offset = get_se_golomb(&s->gb);
  486. h->beta_offset = get_se_golomb(&s->gb);
  487. } else {
  488. h->alpha_offset = h->beta_offset = 0;
  489. }
  490. if(h->pic_type == FF_I_TYPE) {
  491. do {
  492. check_for_slice(h);
  493. decode_mb_i(h, 0);
  494. } while(ff_cavs_next_mb(h));
  495. } else if(h->pic_type == FF_P_TYPE) {
  496. do {
  497. check_for_slice(h);
  498. if(h->skip_mode_flag) {
  499. skip_count = get_ue_golomb(&s->gb);
  500. while(skip_count--) {
  501. decode_mb_p(h,P_SKIP);
  502. if(!ff_cavs_next_mb(h))
  503. goto done;
  504. }
  505. check_for_slice(h);
  506. mb_type = get_ue_golomb(&s->gb) + P_16X16;
  507. } else
  508. mb_type = get_ue_golomb(&s->gb) + P_SKIP;
  509. if(mb_type > P_8X8) {
  510. decode_mb_i(h, mb_type - P_8X8 - 1);
  511. } else
  512. decode_mb_p(h,mb_type);
  513. } while(ff_cavs_next_mb(h));
  514. } else { /* FF_B_TYPE */
  515. do {
  516. check_for_slice(h);
  517. if(h->skip_mode_flag) {
  518. skip_count = get_ue_golomb(&s->gb);
  519. while(skip_count--) {
  520. decode_mb_b(h,B_SKIP);
  521. if(!ff_cavs_next_mb(h))
  522. goto done;
  523. }
  524. check_for_slice(h);
  525. mb_type = get_ue_golomb(&s->gb) + B_DIRECT;
  526. } else
  527. mb_type = get_ue_golomb(&s->gb) + B_SKIP;
  528. if(mb_type > B_8X8) {
  529. decode_mb_i(h, mb_type - B_8X8 - 1);
  530. } else
  531. decode_mb_b(h,mb_type);
  532. } while(ff_cavs_next_mb(h));
  533. }
  534. done:
  535. if(h->pic_type != FF_B_TYPE) {
  536. if(h->DPB[1].data[0])
  537. s->avctx->release_buffer(s->avctx, (AVFrame *)&h->DPB[1]);
  538. h->DPB[1] = h->DPB[0];
  539. h->DPB[0] = h->picture;
  540. memset(&h->picture,0,sizeof(Picture));
  541. }
  542. return 0;
  543. }
  544. /*****************************************************************************
  545. *
  546. * headers and interface
  547. *
  548. ****************************************************************************/
  549. static int decode_seq_header(AVSContext *h) {
  550. MpegEncContext *s = &h->s;
  551. int frame_rate_code;
  552. h->profile = get_bits(&s->gb,8);
  553. h->level = get_bits(&s->gb,8);
  554. skip_bits1(&s->gb); //progressive sequence
  555. s->width = get_bits(&s->gb,14);
  556. s->height = get_bits(&s->gb,14);
  557. skip_bits(&s->gb,2); //chroma format
  558. skip_bits(&s->gb,3); //sample_precision
  559. h->aspect_ratio = get_bits(&s->gb,4);
  560. frame_rate_code = get_bits(&s->gb,4);
  561. skip_bits(&s->gb,18);//bit_rate_lower
  562. skip_bits1(&s->gb); //marker_bit
  563. skip_bits(&s->gb,12);//bit_rate_upper
  564. s->low_delay = get_bits1(&s->gb);
  565. h->mb_width = (s->width + 15) >> 4;
  566. h->mb_height = (s->height + 15) >> 4;
  567. h->s.avctx->time_base.den = ff_frame_rate_tab[frame_rate_code].num;
  568. h->s.avctx->time_base.num = ff_frame_rate_tab[frame_rate_code].den;
  569. h->s.avctx->width = s->width;
  570. h->s.avctx->height = s->height;
  571. if(!h->top_qp)
  572. ff_cavs_init_top_lines(h);
  573. return 0;
  574. }
  575. static void cavs_flush(AVCodecContext * avctx) {
  576. AVSContext *h = avctx->priv_data;
  577. h->got_keyframe = 0;
  578. }
  579. static int cavs_decode_frame(AVCodecContext * avctx,void *data, int *data_size,
  580. const uint8_t * buf, int buf_size) {
  581. AVSContext *h = avctx->priv_data;
  582. MpegEncContext *s = &h->s;
  583. int input_size;
  584. const uint8_t *buf_end;
  585. const uint8_t *buf_ptr;
  586. AVFrame *picture = data;
  587. uint32_t stc = -1;
  588. s->avctx = avctx;
  589. if (buf_size == 0) {
  590. if(!s->low_delay && h->DPB[0].data[0]) {
  591. *data_size = sizeof(AVPicture);
  592. *picture = *(AVFrame *) &h->DPB[0];
  593. }
  594. return 0;
  595. }
  596. buf_ptr = buf;
  597. buf_end = buf + buf_size;
  598. for(;;) {
  599. buf_ptr = ff_find_start_code(buf_ptr,buf_end, &stc);
  600. if(stc & 0xFFFFFE00)
  601. return FFMAX(0, buf_ptr - buf - s->parse_context.last_index);
  602. input_size = (buf_end - buf_ptr)*8;
  603. switch(stc) {
  604. case CAVS_START_CODE:
  605. init_get_bits(&s->gb, buf_ptr, input_size);
  606. decode_seq_header(h);
  607. break;
  608. case PIC_I_START_CODE:
  609. if(!h->got_keyframe) {
  610. if(h->DPB[0].data[0])
  611. avctx->release_buffer(avctx, (AVFrame *)&h->DPB[0]);
  612. if(h->DPB[1].data[0])
  613. avctx->release_buffer(avctx, (AVFrame *)&h->DPB[1]);
  614. h->got_keyframe = 1;
  615. }
  616. case PIC_PB_START_CODE:
  617. *data_size = 0;
  618. if(!h->got_keyframe)
  619. break;
  620. init_get_bits(&s->gb, buf_ptr, input_size);
  621. h->stc = stc;
  622. if(decode_pic(h))
  623. break;
  624. *data_size = sizeof(AVPicture);
  625. if(h->pic_type != FF_B_TYPE) {
  626. if(h->DPB[1].data[0]) {
  627. *picture = *(AVFrame *) &h->DPB[1];
  628. } else {
  629. *data_size = 0;
  630. }
  631. } else
  632. *picture = *(AVFrame *) &h->picture;
  633. break;
  634. case EXT_START_CODE:
  635. //mpeg_decode_extension(avctx,buf_ptr, input_size);
  636. break;
  637. case USER_START_CODE:
  638. //mpeg_decode_user_data(avctx,buf_ptr, input_size);
  639. break;
  640. default:
  641. if (stc <= SLICE_MAX_START_CODE) {
  642. init_get_bits(&s->gb, buf_ptr, input_size);
  643. decode_slice_header(h, &s->gb);
  644. }
  645. break;
  646. }
  647. }
  648. }
  649. AVCodec cavs_decoder = {
  650. "cavs",
  651. CODEC_TYPE_VIDEO,
  652. CODEC_ID_CAVS,
  653. sizeof(AVSContext),
  654. ff_cavs_init,
  655. NULL,
  656. ff_cavs_end,
  657. cavs_decode_frame,
  658. CODEC_CAP_DR1 | CODEC_CAP_DELAY,
  659. .flush= cavs_flush,
  660. .long_name= NULL_IF_CONFIG_SMALL("Chinese AVS video (AVS1-P2, JiZhun profile)"),
  661. };