cavsdec.c 25 KB

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