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.

703 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 cavs.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->mby*h->mb_width + h->mbx)*4 + 0] = h->mv[MV_FWD_X0];
  51. h->col_mv[(h->mby*h->mb_width + h->mbx)*4 + 1] = h->mv[MV_FWD_X1];
  52. h->col_mv[(h->mby*h->mb_width + h->mbx)*4 + 2] = h->mv[MV_FWD_X2];
  53. h->col_mv[(h->mby*h->mb_width + h->mbx)*4 + 3] = h->mv[MV_FWD_X3];
  54. }
  55. static inline void mv_pred_direct(AVSContext *h, vector_t *pmv_fw,
  56. vector_t *col_mv) {
  57. vector_t *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, vector_t *src, enum block_t size) {
  72. vector_t *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 dec_2dvlc_t *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[64];
  107. uint8_t run_buf[64];
  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. return 0;
  134. }
  135. static inline void decode_residual_chroma(AVSContext *h) {
  136. if(h->cbp & (1<<4))
  137. decode_residual_block(h,&h->s.gb,ff_cavs_chroma_dec,0,
  138. ff_cavs_chroma_qp[h->qp],h->cu,h->c_stride);
  139. if(h->cbp & (1<<5))
  140. decode_residual_block(h,&h->s.gb,ff_cavs_chroma_dec,0,
  141. ff_cavs_chroma_qp[h->qp],h->cv,h->c_stride);
  142. }
  143. static inline int decode_residual_inter(AVSContext *h) {
  144. int block;
  145. /* get coded block pattern */
  146. int cbp= get_ue_golomb(&h->s.gb);
  147. if(cbp > 63){
  148. av_log(h->s.avctx, AV_LOG_ERROR, "illegal inter cbp\n");
  149. return -1;
  150. }
  151. h->cbp = cbp_tab[cbp][1];
  152. /* get quantizer */
  153. if(h->cbp && !h->qp_fixed)
  154. h->qp = (h->qp + get_se_golomb(&h->s.gb)) & 63;
  155. for(block=0;block<4;block++)
  156. if(h->cbp & (1<<block))
  157. decode_residual_block(h,&h->s.gb,ff_cavs_inter_dec,0,h->qp,
  158. h->cy + h->luma_scan[block], h->l_stride);
  159. decode_residual_chroma(h);
  160. return 0;
  161. }
  162. /*****************************************************************************
  163. *
  164. * macroblock level
  165. *
  166. ****************************************************************************/
  167. static int decode_mb_i(AVSContext *h, int cbp_code) {
  168. GetBitContext *gb = &h->s.gb;
  169. int block, pred_mode_uv;
  170. uint8_t top[18];
  171. uint8_t *left = NULL;
  172. uint8_t *d;
  173. ff_cavs_init_mb(h);
  174. /* get intra prediction modes from stream */
  175. for(block=0;block<4;block++) {
  176. int nA,nB,predpred;
  177. int pos = ff_cavs_scan3x3[block];
  178. nA = h->pred_mode_Y[pos-1];
  179. nB = h->pred_mode_Y[pos-3];
  180. predpred = FFMIN(nA,nB);
  181. if(predpred == NOT_AVAIL) // if either is not available
  182. predpred = INTRA_L_LP;
  183. if(!get_bits1(gb)){
  184. int rem_mode= get_bits(gb, 2);
  185. predpred = rem_mode + (rem_mode >= predpred);
  186. }
  187. h->pred_mode_Y[pos] = predpred;
  188. }
  189. pred_mode_uv = get_ue_golomb(gb);
  190. if(pred_mode_uv > 6) {
  191. av_log(h->s.avctx, AV_LOG_ERROR, "illegal intra chroma pred mode\n");
  192. return -1;
  193. }
  194. ff_cavs_modify_mb_i(h, &pred_mode_uv);
  195. /* get coded block pattern */
  196. if(h->pic_type == FF_I_TYPE)
  197. cbp_code = get_ue_golomb(gb);
  198. if(cbp_code > 63){
  199. av_log(h->s.avctx, AV_LOG_ERROR, "illegal intra cbp\n");
  200. return -1;
  201. }
  202. h->cbp = cbp_tab[cbp_code][0];
  203. if(h->cbp && !h->qp_fixed)
  204. h->qp = (h->qp + get_se_golomb(gb)) & 63; //qp_delta
  205. /* luma intra prediction interleaved with residual decode/transform/add */
  206. for(block=0;block<4;block++) {
  207. d = h->cy + h->luma_scan[block];
  208. ff_cavs_load_intra_pred_luma(h, top, &left, block);
  209. h->intra_pred_l[h->pred_mode_Y[ff_cavs_scan3x3[block]]]
  210. (d, top, left, h->l_stride);
  211. if(h->cbp & (1<<block))
  212. decode_residual_block(h,gb,ff_cavs_intra_dec,1,h->qp,d,h->l_stride);
  213. }
  214. /* chroma intra prediction */
  215. ff_cavs_load_intra_pred_chroma(h);
  216. h->intra_pred_c[pred_mode_uv](h->cu, &h->top_border_u[h->mbx*10],
  217. h->left_border_u, h->c_stride);
  218. h->intra_pred_c[pred_mode_uv](h->cv, &h->top_border_v[h->mbx*10],
  219. h->left_border_v, h->c_stride);
  220. decode_residual_chroma(h);
  221. ff_cavs_filter(h,I_8X8);
  222. set_mv_intra(h);
  223. return 0;
  224. }
  225. static void decode_mb_p(AVSContext *h, enum mb_t mb_type) {
  226. GetBitContext *gb = &h->s.gb;
  227. int ref[4];
  228. ff_cavs_init_mb(h);
  229. switch(mb_type) {
  230. case P_SKIP:
  231. ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_PSKIP, BLK_16X16, 0);
  232. break;
  233. case P_16X16:
  234. ref[0] = h->ref_flag ? 0 : get_bits1(gb);
  235. ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_MEDIAN, BLK_16X16,ref[0]);
  236. break;
  237. case P_16X8:
  238. ref[0] = h->ref_flag ? 0 : get_bits1(gb);
  239. ref[2] = h->ref_flag ? 0 : get_bits1(gb);
  240. ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_TOP, BLK_16X8, ref[0]);
  241. ff_cavs_mv(h, MV_FWD_X2, MV_FWD_A1, MV_PRED_LEFT, BLK_16X8, ref[2]);
  242. break;
  243. case P_8X16:
  244. ref[0] = h->ref_flag ? 0 : get_bits1(gb);
  245. ref[1] = h->ref_flag ? 0 : get_bits1(gb);
  246. ff_cavs_mv(h, MV_FWD_X0, MV_FWD_B3, MV_PRED_LEFT, BLK_8X16, ref[0]);
  247. ff_cavs_mv(h, MV_FWD_X1, MV_FWD_C2, MV_PRED_TOPRIGHT,BLK_8X16, ref[1]);
  248. break;
  249. case P_8X8:
  250. ref[0] = h->ref_flag ? 0 : get_bits1(gb);
  251. ref[1] = h->ref_flag ? 0 : get_bits1(gb);
  252. ref[2] = h->ref_flag ? 0 : get_bits1(gb);
  253. ref[3] = h->ref_flag ? 0 : get_bits1(gb);
  254. ff_cavs_mv(h, MV_FWD_X0, MV_FWD_B3, MV_PRED_MEDIAN, BLK_8X8, ref[0]);
  255. ff_cavs_mv(h, MV_FWD_X1, MV_FWD_C2, MV_PRED_MEDIAN, BLK_8X8, ref[1]);
  256. ff_cavs_mv(h, MV_FWD_X2, MV_FWD_X1, MV_PRED_MEDIAN, BLK_8X8, ref[2]);
  257. ff_cavs_mv(h, MV_FWD_X3, MV_FWD_X0, MV_PRED_MEDIAN, BLK_8X8, ref[3]);
  258. }
  259. ff_cavs_inter(h, mb_type);
  260. set_intra_mode_default(h);
  261. store_mvs(h);
  262. if(mb_type != P_SKIP)
  263. decode_residual_inter(h);
  264. ff_cavs_filter(h,mb_type);
  265. *h->col_type = mb_type;
  266. }
  267. static void decode_mb_b(AVSContext *h, enum mb_t mb_type) {
  268. int block;
  269. enum sub_mb_t sub_type[4];
  270. int flags;
  271. ff_cavs_init_mb(h);
  272. /* reset all MVs */
  273. h->mv[MV_FWD_X0] = ff_cavs_dir_mv;
  274. set_mvs(&h->mv[MV_FWD_X0], BLK_16X16);
  275. h->mv[MV_BWD_X0] = ff_cavs_dir_mv;
  276. set_mvs(&h->mv[MV_BWD_X0], BLK_16X16);
  277. switch(mb_type) {
  278. case B_SKIP:
  279. case B_DIRECT:
  280. if(!(*h->col_type)) {
  281. /* intra MB at co-location, do in-plane prediction */
  282. ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_BSKIP, BLK_16X16, 1);
  283. ff_cavs_mv(h, MV_BWD_X0, MV_BWD_C2, MV_PRED_BSKIP, BLK_16X16, 0);
  284. } else
  285. /* direct prediction from co-located P MB, block-wise */
  286. for(block=0;block<4;block++)
  287. mv_pred_direct(h,&h->mv[mv_scan[block]],
  288. &h->col_mv[(h->mby*h->mb_width+h->mbx)*4 + block]);
  289. break;
  290. case B_FWD_16X16:
  291. ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_MEDIAN, BLK_16X16, 1);
  292. break;
  293. case B_SYM_16X16:
  294. ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_MEDIAN, BLK_16X16, 1);
  295. mv_pred_sym(h, &h->mv[MV_FWD_X0], BLK_16X16);
  296. break;
  297. case B_BWD_16X16:
  298. ff_cavs_mv(h, MV_BWD_X0, MV_BWD_C2, MV_PRED_MEDIAN, BLK_16X16, 0);
  299. break;
  300. case B_8X8:
  301. for(block=0;block<4;block++)
  302. sub_type[block] = get_bits(&h->s.gb,2);
  303. for(block=0;block<4;block++) {
  304. switch(sub_type[block]) {
  305. case B_SUB_DIRECT:
  306. if(!(*h->col_type)) {
  307. /* intra MB at co-location, do in-plane prediction */
  308. ff_cavs_mv(h, mv_scan[block], mv_scan[block]-3,
  309. MV_PRED_BSKIP, BLK_8X8, 1);
  310. ff_cavs_mv(h, mv_scan[block]+MV_BWD_OFFS,
  311. mv_scan[block]-3+MV_BWD_OFFS,
  312. MV_PRED_BSKIP, BLK_8X8, 0);
  313. } else
  314. mv_pred_direct(h,&h->mv[mv_scan[block]],
  315. &h->col_mv[(h->mby*h->mb_width + h->mbx)*4 + block]);
  316. break;
  317. case B_SUB_FWD:
  318. ff_cavs_mv(h, mv_scan[block], mv_scan[block]-3,
  319. MV_PRED_MEDIAN, BLK_8X8, 1);
  320. break;
  321. case B_SUB_SYM:
  322. ff_cavs_mv(h, mv_scan[block], mv_scan[block]-3,
  323. MV_PRED_MEDIAN, BLK_8X8, 1);
  324. mv_pred_sym(h, &h->mv[mv_scan[block]], BLK_8X8);
  325. break;
  326. }
  327. }
  328. for(block=0;block<4;block++) {
  329. if(sub_type[block] == B_SUB_BWD)
  330. ff_cavs_mv(h, mv_scan[block]+MV_BWD_OFFS,
  331. mv_scan[block]+MV_BWD_OFFS-3,
  332. MV_PRED_MEDIAN, BLK_8X8, 0);
  333. }
  334. break;
  335. default:
  336. assert((mb_type > B_SYM_16X16) && (mb_type < B_8X8));
  337. flags = ff_cavs_partition_flags[mb_type];
  338. if(mb_type & 1) { /* 16x8 macroblock types */
  339. if(flags & FWD0)
  340. ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_TOP, BLK_16X8, 1);
  341. if(flags & SYM0)
  342. mv_pred_sym(h, &h->mv[MV_FWD_X0], BLK_16X8);
  343. if(flags & FWD1)
  344. ff_cavs_mv(h, MV_FWD_X2, MV_FWD_A1, MV_PRED_LEFT, BLK_16X8, 1);
  345. if(flags & SYM1)
  346. mv_pred_sym(h, &h->mv[MV_FWD_X2], BLK_16X8);
  347. if(flags & BWD0)
  348. ff_cavs_mv(h, MV_BWD_X0, MV_BWD_C2, MV_PRED_TOP, BLK_16X8, 0);
  349. if(flags & BWD1)
  350. ff_cavs_mv(h, MV_BWD_X2, MV_BWD_A1, MV_PRED_LEFT, BLK_16X8, 0);
  351. } else { /* 8x16 macroblock types */
  352. if(flags & FWD0)
  353. ff_cavs_mv(h, MV_FWD_X0, MV_FWD_B3, MV_PRED_LEFT, BLK_8X16, 1);
  354. if(flags & SYM0)
  355. mv_pred_sym(h, &h->mv[MV_FWD_X0], BLK_8X16);
  356. if(flags & FWD1)
  357. ff_cavs_mv(h,MV_FWD_X1,MV_FWD_C2,MV_PRED_TOPRIGHT,BLK_8X16,1);
  358. if(flags & SYM1)
  359. mv_pred_sym(h, &h->mv[MV_FWD_X1], BLK_8X16);
  360. if(flags & BWD0)
  361. ff_cavs_mv(h, MV_BWD_X0, MV_BWD_B3, MV_PRED_LEFT, BLK_8X16, 0);
  362. if(flags & BWD1)
  363. ff_cavs_mv(h,MV_BWD_X1,MV_BWD_C2,MV_PRED_TOPRIGHT,BLK_8X16,0);
  364. }
  365. }
  366. ff_cavs_inter(h, mb_type);
  367. set_intra_mode_default(h);
  368. if(mb_type != B_SKIP)
  369. decode_residual_inter(h);
  370. ff_cavs_filter(h,mb_type);
  371. }
  372. /*****************************************************************************
  373. *
  374. * slice level
  375. *
  376. ****************************************************************************/
  377. static inline int decode_slice_header(AVSContext *h, GetBitContext *gb) {
  378. if(h->stc > 0xAF)
  379. av_log(h->s.avctx, AV_LOG_ERROR, "unexpected start code 0x%02x\n", h->stc);
  380. h->mby = h->stc;
  381. if((h->mby == 0) && (!h->qp_fixed)){
  382. h->qp_fixed = get_bits1(gb);
  383. h->qp = get_bits(gb,6);
  384. }
  385. /* inter frame or second slice can have weighting params */
  386. if((h->pic_type != FF_I_TYPE) || (!h->pic_structure && h->mby >= h->mb_width/2))
  387. if(get_bits1(gb)) { //slice_weighting_flag
  388. av_log(h->s.avctx, AV_LOG_ERROR,
  389. "weighted prediction not yet supported\n");
  390. }
  391. return 0;
  392. }
  393. static inline void check_for_slice(AVSContext *h) {
  394. GetBitContext *gb = &h->s.gb;
  395. int align;
  396. align = (-get_bits_count(gb)) & 7;
  397. if((show_bits_long(gb,24+align) & 0xFFFFFF) == 0x000001) {
  398. skip_bits_long(gb,24+align);
  399. h->stc = get_bits(gb,8);
  400. decode_slice_header(h,gb);
  401. }
  402. }
  403. /*****************************************************************************
  404. *
  405. * frame level
  406. *
  407. ****************************************************************************/
  408. static int decode_pic(AVSContext *h) {
  409. MpegEncContext *s = &h->s;
  410. int skip_count;
  411. enum mb_t mb_type;
  412. if (!s->context_initialized) {
  413. s->avctx->idct_algo = FF_IDCT_CAVS;
  414. if (MPV_common_init(s) < 0)
  415. return -1;
  416. ff_init_scantable(s->dsp.idct_permutation,&h->scantable,ff_zigzag_direct);
  417. }
  418. skip_bits(&s->gb,16);//bbv_dwlay
  419. if(h->stc == PIC_PB_START_CODE) {
  420. h->pic_type = get_bits(&s->gb,2) + FF_I_TYPE;
  421. if(h->pic_type > FF_B_TYPE) {
  422. av_log(s->avctx, AV_LOG_ERROR, "illegal picture type\n");
  423. return -1;
  424. }
  425. /* make sure we have the reference frames we need */
  426. if(!h->DPB[0].data[0] ||
  427. (!h->DPB[1].data[0] && h->pic_type == FF_B_TYPE))
  428. return -1;
  429. } else {
  430. h->pic_type = FF_I_TYPE;
  431. if(get_bits1(&s->gb))
  432. skip_bits(&s->gb,16);//time_code
  433. }
  434. /* release last B frame */
  435. if(h->picture.data[0])
  436. s->avctx->release_buffer(s->avctx, (AVFrame *)&h->picture);
  437. s->avctx->get_buffer(s->avctx, (AVFrame *)&h->picture);
  438. ff_cavs_init_pic(h);
  439. h->picture.poc = get_bits(&s->gb,8)*2;
  440. /* get temporal distances and MV scaling factors */
  441. if(h->pic_type != FF_B_TYPE) {
  442. h->dist[0] = (h->picture.poc - h->DPB[0].poc + 512) % 512;
  443. } else {
  444. h->dist[0] = (h->DPB[0].poc - h->picture.poc + 512) % 512;
  445. }
  446. h->dist[1] = (h->picture.poc - h->DPB[1].poc + 512) % 512;
  447. h->scale_den[0] = h->dist[0] ? 512/h->dist[0] : 0;
  448. h->scale_den[1] = h->dist[1] ? 512/h->dist[1] : 0;
  449. if(h->pic_type == FF_B_TYPE) {
  450. h->sym_factor = h->dist[0]*h->scale_den[1];
  451. } else {
  452. h->direct_den[0] = h->dist[0] ? 16384/h->dist[0] : 0;
  453. h->direct_den[1] = h->dist[1] ? 16384/h->dist[1] : 0;
  454. }
  455. if(s->low_delay)
  456. get_ue_golomb(&s->gb); //bbv_check_times
  457. h->progressive = get_bits1(&s->gb);
  458. if(h->progressive)
  459. h->pic_structure = 1;
  460. else if(!(h->pic_structure = get_bits1(&s->gb) && (h->stc == PIC_PB_START_CODE)) )
  461. skip_bits1(&s->gb); //advanced_pred_mode_disable
  462. skip_bits1(&s->gb); //top_field_first
  463. skip_bits1(&s->gb); //repeat_first_field
  464. h->qp_fixed = get_bits1(&s->gb);
  465. h->qp = get_bits(&s->gb,6);
  466. if(h->pic_type == FF_I_TYPE) {
  467. if(!h->progressive && !h->pic_structure)
  468. skip_bits1(&s->gb);//what is this?
  469. skip_bits(&s->gb,4); //reserved bits
  470. } else {
  471. if(!(h->pic_type == FF_B_TYPE && h->pic_structure == 1))
  472. h->ref_flag = get_bits1(&s->gb);
  473. skip_bits(&s->gb,4); //reserved bits
  474. h->skip_mode_flag = get_bits1(&s->gb);
  475. }
  476. h->loop_filter_disable = get_bits1(&s->gb);
  477. if(!h->loop_filter_disable && get_bits1(&s->gb)) {
  478. h->alpha_offset = get_se_golomb(&s->gb);
  479. h->beta_offset = get_se_golomb(&s->gb);
  480. } else {
  481. h->alpha_offset = h->beta_offset = 0;
  482. }
  483. check_for_slice(h);
  484. if(h->pic_type == FF_I_TYPE) {
  485. do {
  486. decode_mb_i(h, 0);
  487. } while(ff_cavs_next_mb(h));
  488. } else if(h->pic_type == FF_P_TYPE) {
  489. do {
  490. if(h->skip_mode_flag) {
  491. skip_count = get_ue_golomb(&s->gb);
  492. while(skip_count--) {
  493. decode_mb_p(h,P_SKIP);
  494. if(!ff_cavs_next_mb(h))
  495. goto done;
  496. }
  497. mb_type = get_ue_golomb(&s->gb) + P_16X16;
  498. } else
  499. mb_type = get_ue_golomb(&s->gb) + P_SKIP;
  500. if(mb_type > P_8X8) {
  501. decode_mb_i(h, mb_type - P_8X8 - 1);
  502. } else
  503. decode_mb_p(h,mb_type);
  504. } while(ff_cavs_next_mb(h));
  505. } else { /* FF_B_TYPE */
  506. do {
  507. if(h->skip_mode_flag) {
  508. skip_count = get_ue_golomb(&s->gb);
  509. while(skip_count--) {
  510. decode_mb_b(h,B_SKIP);
  511. if(!ff_cavs_next_mb(h))
  512. goto done;
  513. }
  514. mb_type = get_ue_golomb(&s->gb) + B_DIRECT;
  515. } else
  516. mb_type = get_ue_golomb(&s->gb) + B_SKIP;
  517. if(mb_type > B_8X8) {
  518. decode_mb_i(h, mb_type - B_8X8 - 1);
  519. } else
  520. decode_mb_b(h,mb_type);
  521. } while(ff_cavs_next_mb(h));
  522. }
  523. done:
  524. if(h->pic_type != FF_B_TYPE) {
  525. if(h->DPB[1].data[0])
  526. s->avctx->release_buffer(s->avctx, (AVFrame *)&h->DPB[1]);
  527. memcpy(&h->DPB[1], &h->DPB[0], sizeof(Picture));
  528. memcpy(&h->DPB[0], &h->picture, sizeof(Picture));
  529. memset(&h->picture,0,sizeof(Picture));
  530. }
  531. return 0;
  532. }
  533. /*****************************************************************************
  534. *
  535. * headers and interface
  536. *
  537. ****************************************************************************/
  538. static int decode_seq_header(AVSContext *h) {
  539. MpegEncContext *s = &h->s;
  540. int frame_rate_code;
  541. h->profile = get_bits(&s->gb,8);
  542. h->level = get_bits(&s->gb,8);
  543. skip_bits1(&s->gb); //progressive sequence
  544. s->width = get_bits(&s->gb,14);
  545. s->height = get_bits(&s->gb,14);
  546. skip_bits(&s->gb,2); //chroma format
  547. skip_bits(&s->gb,3); //sample_precision
  548. h->aspect_ratio = get_bits(&s->gb,4);
  549. frame_rate_code = get_bits(&s->gb,4);
  550. skip_bits(&s->gb,18);//bit_rate_lower
  551. skip_bits1(&s->gb); //marker_bit
  552. skip_bits(&s->gb,12);//bit_rate_upper
  553. s->low_delay = get_bits1(&s->gb);
  554. h->mb_width = (s->width + 15) >> 4;
  555. h->mb_height = (s->height + 15) >> 4;
  556. h->s.avctx->time_base.den = ff_frame_rate_tab[frame_rate_code].num;
  557. h->s.avctx->time_base.num = ff_frame_rate_tab[frame_rate_code].den;
  558. h->s.avctx->width = s->width;
  559. h->s.avctx->height = s->height;
  560. if(!h->top_qp)
  561. ff_cavs_init_top_lines(h);
  562. return 0;
  563. }
  564. static void cavs_flush(AVCodecContext * avctx) {
  565. AVSContext *h = avctx->priv_data;
  566. h->got_keyframe = 0;
  567. }
  568. static int cavs_decode_frame(AVCodecContext * avctx,void *data, int *data_size,
  569. uint8_t * buf, int buf_size) {
  570. AVSContext *h = avctx->priv_data;
  571. MpegEncContext *s = &h->s;
  572. int input_size;
  573. const uint8_t *buf_end;
  574. const uint8_t *buf_ptr;
  575. AVFrame *picture = data;
  576. uint32_t stc = -1;
  577. s->avctx = avctx;
  578. if (buf_size == 0) {
  579. if(!s->low_delay && h->DPB[0].data[0]) {
  580. *data_size = sizeof(AVPicture);
  581. *picture = *(AVFrame *) &h->DPB[0];
  582. }
  583. return 0;
  584. }
  585. buf_ptr = buf;
  586. buf_end = buf + buf_size;
  587. for(;;) {
  588. buf_ptr = ff_find_start_code(buf_ptr,buf_end, &stc);
  589. if(stc & 0xFFFFFE00)
  590. return FFMAX(0, buf_ptr - buf - s->parse_context.last_index);
  591. input_size = (buf_end - buf_ptr)*8;
  592. switch(stc) {
  593. case CAVS_START_CODE:
  594. init_get_bits(&s->gb, buf_ptr, input_size);
  595. decode_seq_header(h);
  596. break;
  597. case PIC_I_START_CODE:
  598. if(!h->got_keyframe) {
  599. if(h->DPB[0].data[0])
  600. avctx->release_buffer(avctx, (AVFrame *)&h->DPB[0]);
  601. if(h->DPB[1].data[0])
  602. avctx->release_buffer(avctx, (AVFrame *)&h->DPB[1]);
  603. h->got_keyframe = 1;
  604. }
  605. case PIC_PB_START_CODE:
  606. *data_size = 0;
  607. if(!h->got_keyframe)
  608. break;
  609. init_get_bits(&s->gb, buf_ptr, input_size);
  610. h->stc = stc;
  611. if(decode_pic(h))
  612. break;
  613. *data_size = sizeof(AVPicture);
  614. if(h->pic_type != FF_B_TYPE) {
  615. if(h->DPB[1].data[0]) {
  616. *picture = *(AVFrame *) &h->DPB[1];
  617. } else {
  618. *data_size = 0;
  619. }
  620. } else
  621. *picture = *(AVFrame *) &h->picture;
  622. break;
  623. case EXT_START_CODE:
  624. //mpeg_decode_extension(avctx,buf_ptr, input_size);
  625. break;
  626. case USER_START_CODE:
  627. //mpeg_decode_user_data(avctx,buf_ptr, input_size);
  628. break;
  629. default:
  630. if (stc >= SLICE_MIN_START_CODE &&
  631. stc <= SLICE_MAX_START_CODE) {
  632. init_get_bits(&s->gb, buf_ptr, input_size);
  633. decode_slice_header(h, &s->gb);
  634. }
  635. break;
  636. }
  637. }
  638. }
  639. AVCodec cavs_decoder = {
  640. "cavs",
  641. CODEC_TYPE_VIDEO,
  642. CODEC_ID_CAVS,
  643. sizeof(AVSContext),
  644. ff_cavs_init,
  645. NULL,
  646. ff_cavs_end,
  647. cavs_decode_frame,
  648. CODEC_CAP_DR1 | CODEC_CAP_DELAY,
  649. .flush= cavs_flush,
  650. };