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.

1229 lines
43KB

  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. #include "cavsdata.h"
  31. /*****************************************************************************
  32. *
  33. * in-loop deblocking filter
  34. *
  35. ****************************************************************************/
  36. static inline int get_bs(vector_t *mvP, vector_t *mvQ, int b) {
  37. if((mvP->ref == REF_INTRA) || (mvQ->ref == REF_INTRA))
  38. return 2;
  39. if( (abs(mvP->x - mvQ->x) >= 4) || (abs(mvP->y - mvQ->y) >= 4) )
  40. return 1;
  41. if(b){
  42. mvP += MV_BWD_OFFS;
  43. mvQ += MV_BWD_OFFS;
  44. if( (abs(mvP->x - mvQ->x) >= 4) || (abs(mvP->y - mvQ->y) >= 4) )
  45. return 1;
  46. }else{
  47. if(mvP->ref != mvQ->ref)
  48. return 1;
  49. }
  50. return 0;
  51. }
  52. #define SET_PARAMS \
  53. alpha = alpha_tab[av_clip(qp_avg + h->alpha_offset,0,63)]; \
  54. beta = beta_tab[av_clip(qp_avg + h->beta_offset, 0,63)]; \
  55. tc = tc_tab[av_clip(qp_avg + h->alpha_offset,0,63)];
  56. /**
  57. * in-loop deblocking filter for a single macroblock
  58. *
  59. * boundary strength (bs) mapping:
  60. *
  61. * --4---5--
  62. * 0 2 |
  63. * | 6 | 7 |
  64. * 1 3 |
  65. * ---------
  66. *
  67. */
  68. static void filter_mb(AVSContext *h, enum mb_t mb_type) {
  69. DECLARE_ALIGNED_8(uint8_t, bs[8]);
  70. int qp_avg, alpha, beta, tc;
  71. int i;
  72. /* save un-deblocked lines */
  73. h->topleft_border_y = h->top_border_y[h->mbx*16+15];
  74. h->topleft_border_u = h->top_border_u[h->mbx*10+8];
  75. h->topleft_border_v = h->top_border_v[h->mbx*10+8];
  76. memcpy(&h->top_border_y[h->mbx*16], h->cy + 15* h->l_stride,16);
  77. memcpy(&h->top_border_u[h->mbx*10+1], h->cu + 7* h->c_stride,8);
  78. memcpy(&h->top_border_v[h->mbx*10+1], h->cv + 7* h->c_stride,8);
  79. for(i=0;i<8;i++) {
  80. h->left_border_y[i*2+1] = *(h->cy + 15 + (i*2+0)*h->l_stride);
  81. h->left_border_y[i*2+2] = *(h->cy + 15 + (i*2+1)*h->l_stride);
  82. h->left_border_u[i+1] = *(h->cu + 7 + i*h->c_stride);
  83. h->left_border_v[i+1] = *(h->cv + 7 + i*h->c_stride);
  84. }
  85. if(!h->loop_filter_disable) {
  86. /* determine bs */
  87. if(mb_type == I_8X8)
  88. *((uint64_t *)bs) = 0x0202020202020202ULL;
  89. else{
  90. *((uint64_t *)bs) = 0;
  91. if(partition_flags[mb_type] & SPLITV){
  92. bs[2] = get_bs(&h->mv[MV_FWD_X0], &h->mv[MV_FWD_X1], mb_type > P_8X8);
  93. bs[3] = get_bs(&h->mv[MV_FWD_X2], &h->mv[MV_FWD_X3], mb_type > P_8X8);
  94. }
  95. if(partition_flags[mb_type] & SPLITH){
  96. bs[6] = get_bs(&h->mv[MV_FWD_X0], &h->mv[MV_FWD_X2], mb_type > P_8X8);
  97. bs[7] = get_bs(&h->mv[MV_FWD_X1], &h->mv[MV_FWD_X3], mb_type > P_8X8);
  98. }
  99. bs[0] = get_bs(&h->mv[MV_FWD_A1], &h->mv[MV_FWD_X0], mb_type > P_8X8);
  100. bs[1] = get_bs(&h->mv[MV_FWD_A3], &h->mv[MV_FWD_X2], mb_type > P_8X8);
  101. bs[4] = get_bs(&h->mv[MV_FWD_B2], &h->mv[MV_FWD_X0], mb_type > P_8X8);
  102. bs[5] = get_bs(&h->mv[MV_FWD_B3], &h->mv[MV_FWD_X1], mb_type > P_8X8);
  103. }
  104. if( *((uint64_t *)bs) ) {
  105. if(h->flags & A_AVAIL) {
  106. qp_avg = (h->qp + h->left_qp + 1) >> 1;
  107. SET_PARAMS;
  108. h->s.dsp.cavs_filter_lv(h->cy,h->l_stride,alpha,beta,tc,bs[0],bs[1]);
  109. h->s.dsp.cavs_filter_cv(h->cu,h->c_stride,alpha,beta,tc,bs[0],bs[1]);
  110. h->s.dsp.cavs_filter_cv(h->cv,h->c_stride,alpha,beta,tc,bs[0],bs[1]);
  111. }
  112. qp_avg = h->qp;
  113. SET_PARAMS;
  114. h->s.dsp.cavs_filter_lv(h->cy + 8,h->l_stride,alpha,beta,tc,bs[2],bs[3]);
  115. h->s.dsp.cavs_filter_lh(h->cy + 8*h->l_stride,h->l_stride,alpha,beta,tc,
  116. bs[6],bs[7]);
  117. if(h->flags & B_AVAIL) {
  118. qp_avg = (h->qp + h->top_qp[h->mbx] + 1) >> 1;
  119. SET_PARAMS;
  120. h->s.dsp.cavs_filter_lh(h->cy,h->l_stride,alpha,beta,tc,bs[4],bs[5]);
  121. h->s.dsp.cavs_filter_ch(h->cu,h->c_stride,alpha,beta,tc,bs[4],bs[5]);
  122. h->s.dsp.cavs_filter_ch(h->cv,h->c_stride,alpha,beta,tc,bs[4],bs[5]);
  123. }
  124. }
  125. }
  126. h->left_qp = h->qp;
  127. h->top_qp[h->mbx] = h->qp;
  128. }
  129. #undef SET_PARAMS
  130. /*****************************************************************************
  131. *
  132. * spatial intra prediction
  133. *
  134. ****************************************************************************/
  135. static void intra_pred_vert(uint8_t *d,uint8_t *top,uint8_t *left,int stride) {
  136. int y;
  137. uint64_t a = unaligned64(&top[1]);
  138. for(y=0;y<8;y++) {
  139. *((uint64_t *)(d+y*stride)) = a;
  140. }
  141. }
  142. static void intra_pred_horiz(uint8_t *d,uint8_t *top,uint8_t *left,int stride) {
  143. int y;
  144. uint64_t a;
  145. for(y=0;y<8;y++) {
  146. a = left[y+1] * 0x0101010101010101ULL;
  147. *((uint64_t *)(d+y*stride)) = a;
  148. }
  149. }
  150. static void intra_pred_dc_128(uint8_t *d,uint8_t *top,uint8_t *left,int stride) {
  151. int y;
  152. uint64_t a = 0x8080808080808080ULL;
  153. for(y=0;y<8;y++)
  154. *((uint64_t *)(d+y*stride)) = a;
  155. }
  156. static void intra_pred_plane(uint8_t *d,uint8_t *top,uint8_t *left,int stride) {
  157. int x,y,ia;
  158. int ih = 0;
  159. int iv = 0;
  160. uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
  161. for(x=0; x<4; x++) {
  162. ih += (x+1)*(top[5+x]-top[3-x]);
  163. iv += (x+1)*(left[5+x]-left[3-x]);
  164. }
  165. ia = (top[8]+left[8])<<4;
  166. ih = (17*ih+16)>>5;
  167. iv = (17*iv+16)>>5;
  168. for(y=0; y<8; y++)
  169. for(x=0; x<8; x++)
  170. d[y*stride+x] = cm[(ia+(x-3)*ih+(y-3)*iv+16)>>5];
  171. }
  172. #define LOWPASS(ARRAY,INDEX) \
  173. (( ARRAY[(INDEX)-1] + 2*ARRAY[(INDEX)] + ARRAY[(INDEX)+1] + 2) >> 2)
  174. static void intra_pred_lp(uint8_t *d,uint8_t *top,uint8_t *left,int stride) {
  175. int x,y;
  176. for(y=0; y<8; y++)
  177. for(x=0; x<8; x++)
  178. d[y*stride+x] = (LOWPASS(top,x+1) + LOWPASS(left,y+1)) >> 1;
  179. }
  180. static void intra_pred_down_left(uint8_t *d,uint8_t *top,uint8_t *left,int stride) {
  181. int x,y;
  182. for(y=0; y<8; y++)
  183. for(x=0; x<8; x++)
  184. d[y*stride+x] = (LOWPASS(top,x+y+2) + LOWPASS(left,x+y+2)) >> 1;
  185. }
  186. static void intra_pred_down_right(uint8_t *d,uint8_t *top,uint8_t *left,int stride) {
  187. int x,y;
  188. for(y=0; y<8; y++)
  189. for(x=0; x<8; x++)
  190. if(x==y)
  191. d[y*stride+x] = (left[1]+2*top[0]+top[1]+2)>>2;
  192. else if(x>y)
  193. d[y*stride+x] = LOWPASS(top,x-y);
  194. else
  195. d[y*stride+x] = LOWPASS(left,y-x);
  196. }
  197. static void intra_pred_lp_left(uint8_t *d,uint8_t *top,uint8_t *left,int stride) {
  198. int x,y;
  199. for(y=0; y<8; y++)
  200. for(x=0; x<8; x++)
  201. d[y*stride+x] = LOWPASS(left,y+1);
  202. }
  203. static void intra_pred_lp_top(uint8_t *d,uint8_t *top,uint8_t *left,int stride) {
  204. int x,y;
  205. for(y=0; y<8; y++)
  206. for(x=0; x<8; x++)
  207. d[y*stride+x] = LOWPASS(top,x+1);
  208. }
  209. #undef LOWPASS
  210. /*****************************************************************************
  211. *
  212. * motion compensation
  213. *
  214. ****************************************************************************/
  215. static inline void mc_dir_part(AVSContext *h,Picture *pic,int square,
  216. int chroma_height,int delta,int list,uint8_t *dest_y,
  217. uint8_t *dest_cb,uint8_t *dest_cr,int src_x_offset,
  218. int src_y_offset,qpel_mc_func *qpix_op,
  219. h264_chroma_mc_func chroma_op,vector_t *mv){
  220. MpegEncContext * const s = &h->s;
  221. const int mx= mv->x + src_x_offset*8;
  222. const int my= mv->y + src_y_offset*8;
  223. const int luma_xy= (mx&3) + ((my&3)<<2);
  224. uint8_t * src_y = pic->data[0] + (mx>>2) + (my>>2)*h->l_stride;
  225. uint8_t * src_cb= pic->data[1] + (mx>>3) + (my>>3)*h->c_stride;
  226. uint8_t * src_cr= pic->data[2] + (mx>>3) + (my>>3)*h->c_stride;
  227. int extra_width= 0; //(s->flags&CODEC_FLAG_EMU_EDGE) ? 0 : 16;
  228. int extra_height= extra_width;
  229. int emu=0;
  230. const int full_mx= mx>>2;
  231. const int full_my= my>>2;
  232. const int pic_width = 16*h->mb_width;
  233. const int pic_height = 16*h->mb_height;
  234. if(!pic->data[0])
  235. return;
  236. if(mx&7) extra_width -= 3;
  237. if(my&7) extra_height -= 3;
  238. if( full_mx < 0-extra_width
  239. || full_my < 0-extra_height
  240. || full_mx + 16/*FIXME*/ > pic_width + extra_width
  241. || full_my + 16/*FIXME*/ > pic_height + extra_height){
  242. ff_emulated_edge_mc(s->edge_emu_buffer, src_y - 2 - 2*h->l_stride, h->l_stride,
  243. 16+5, 16+5/*FIXME*/, full_mx-2, full_my-2, pic_width, pic_height);
  244. src_y= s->edge_emu_buffer + 2 + 2*h->l_stride;
  245. emu=1;
  246. }
  247. qpix_op[luma_xy](dest_y, src_y, h->l_stride); //FIXME try variable height perhaps?
  248. if(!square){
  249. qpix_op[luma_xy](dest_y + delta, src_y + delta, h->l_stride);
  250. }
  251. if(emu){
  252. ff_emulated_edge_mc(s->edge_emu_buffer, src_cb, h->c_stride,
  253. 9, 9/*FIXME*/, (mx>>3), (my>>3), pic_width>>1, pic_height>>1);
  254. src_cb= s->edge_emu_buffer;
  255. }
  256. chroma_op(dest_cb, src_cb, h->c_stride, chroma_height, mx&7, my&7);
  257. if(emu){
  258. ff_emulated_edge_mc(s->edge_emu_buffer, src_cr, h->c_stride,
  259. 9, 9/*FIXME*/, (mx>>3), (my>>3), pic_width>>1, pic_height>>1);
  260. src_cr= s->edge_emu_buffer;
  261. }
  262. chroma_op(dest_cr, src_cr, h->c_stride, chroma_height, mx&7, my&7);
  263. }
  264. static inline void mc_part_std(AVSContext *h,int square,int chroma_height,int delta,
  265. uint8_t *dest_y,uint8_t *dest_cb,uint8_t *dest_cr,
  266. int x_offset, int y_offset,qpel_mc_func *qpix_put,
  267. h264_chroma_mc_func chroma_put,qpel_mc_func *qpix_avg,
  268. h264_chroma_mc_func chroma_avg, vector_t *mv){
  269. qpel_mc_func *qpix_op= qpix_put;
  270. h264_chroma_mc_func chroma_op= chroma_put;
  271. dest_y += 2*x_offset + 2*y_offset*h->l_stride;
  272. dest_cb += x_offset + y_offset*h->c_stride;
  273. dest_cr += x_offset + y_offset*h->c_stride;
  274. x_offset += 8*h->mbx;
  275. y_offset += 8*h->mby;
  276. if(mv->ref >= 0){
  277. Picture *ref= &h->DPB[mv->ref];
  278. mc_dir_part(h, ref, square, chroma_height, delta, 0,
  279. dest_y, dest_cb, dest_cr, x_offset, y_offset,
  280. qpix_op, chroma_op, mv);
  281. qpix_op= qpix_avg;
  282. chroma_op= chroma_avg;
  283. }
  284. if((mv+MV_BWD_OFFS)->ref >= 0){
  285. Picture *ref= &h->DPB[0];
  286. mc_dir_part(h, ref, square, chroma_height, delta, 1,
  287. dest_y, dest_cb, dest_cr, x_offset, y_offset,
  288. qpix_op, chroma_op, mv+MV_BWD_OFFS);
  289. }
  290. }
  291. static void inter_pred(AVSContext *h, enum mb_t mb_type) {
  292. if(partition_flags[mb_type] == 0){ // 16x16
  293. mc_part_std(h, 1, 8, 0, h->cy, h->cu, h->cv, 0, 0,
  294. h->s.dsp.put_cavs_qpel_pixels_tab[0],
  295. h->s.dsp.put_h264_chroma_pixels_tab[0],
  296. h->s.dsp.avg_cavs_qpel_pixels_tab[0],
  297. h->s.dsp.avg_h264_chroma_pixels_tab[0],&h->mv[MV_FWD_X0]);
  298. }else{
  299. mc_part_std(h, 1, 4, 0, h->cy, h->cu, h->cv, 0, 0,
  300. h->s.dsp.put_cavs_qpel_pixels_tab[1],
  301. h->s.dsp.put_h264_chroma_pixels_tab[1],
  302. h->s.dsp.avg_cavs_qpel_pixels_tab[1],
  303. h->s.dsp.avg_h264_chroma_pixels_tab[1],&h->mv[MV_FWD_X0]);
  304. mc_part_std(h, 1, 4, 0, h->cy, h->cu, h->cv, 4, 0,
  305. h->s.dsp.put_cavs_qpel_pixels_tab[1],
  306. h->s.dsp.put_h264_chroma_pixels_tab[1],
  307. h->s.dsp.avg_cavs_qpel_pixels_tab[1],
  308. h->s.dsp.avg_h264_chroma_pixels_tab[1],&h->mv[MV_FWD_X1]);
  309. mc_part_std(h, 1, 4, 0, h->cy, h->cu, h->cv, 0, 4,
  310. h->s.dsp.put_cavs_qpel_pixels_tab[1],
  311. h->s.dsp.put_h264_chroma_pixels_tab[1],
  312. h->s.dsp.avg_cavs_qpel_pixels_tab[1],
  313. h->s.dsp.avg_h264_chroma_pixels_tab[1],&h->mv[MV_FWD_X2]);
  314. mc_part_std(h, 1, 4, 0, h->cy, h->cu, h->cv, 4, 4,
  315. h->s.dsp.put_cavs_qpel_pixels_tab[1],
  316. h->s.dsp.put_h264_chroma_pixels_tab[1],
  317. h->s.dsp.avg_cavs_qpel_pixels_tab[1],
  318. h->s.dsp.avg_h264_chroma_pixels_tab[1],&h->mv[MV_FWD_X3]);
  319. }
  320. /* set intra prediction modes to default values */
  321. h->pred_mode_Y[3] = h->pred_mode_Y[6] = INTRA_L_LP;
  322. h->top_pred_Y[h->mbx*2+0] = h->top_pred_Y[h->mbx*2+1] = INTRA_L_LP;
  323. }
  324. /*****************************************************************************
  325. *
  326. * motion vector prediction
  327. *
  328. ****************************************************************************/
  329. static inline void store_mvs(AVSContext *h) {
  330. h->col_mv[(h->mby*h->mb_width + h->mbx)*4 + 0] = h->mv[MV_FWD_X0];
  331. h->col_mv[(h->mby*h->mb_width + h->mbx)*4 + 1] = h->mv[MV_FWD_X1];
  332. h->col_mv[(h->mby*h->mb_width + h->mbx)*4 + 2] = h->mv[MV_FWD_X2];
  333. h->col_mv[(h->mby*h->mb_width + h->mbx)*4 + 3] = h->mv[MV_FWD_X3];
  334. }
  335. static inline void scale_mv(AVSContext *h, int *d_x, int *d_y, vector_t *src, int distp) {
  336. int den = h->scale_den[src->ref];
  337. *d_x = (src->x*distp*den + 256 + (src->x>>31)) >> 9;
  338. *d_y = (src->y*distp*den + 256 + (src->y>>31)) >> 9;
  339. }
  340. static inline void mv_pred_median(AVSContext *h, vector_t *mvP, vector_t *mvA, vector_t *mvB, vector_t *mvC) {
  341. int ax, ay, bx, by, cx, cy;
  342. int len_ab, len_bc, len_ca, len_mid;
  343. /* scale candidates according to their temporal span */
  344. scale_mv(h, &ax, &ay, mvA, mvP->dist);
  345. scale_mv(h, &bx, &by, mvB, mvP->dist);
  346. scale_mv(h, &cx, &cy, mvC, mvP->dist);
  347. /* find the geometrical median of the three candidates */
  348. len_ab = abs(ax - bx) + abs(ay - by);
  349. len_bc = abs(bx - cx) + abs(by - cy);
  350. len_ca = abs(cx - ax) + abs(cy - ay);
  351. len_mid = mid_pred(len_ab, len_bc, len_ca);
  352. if(len_mid == len_ab) {
  353. mvP->x = cx;
  354. mvP->y = cy;
  355. } else if(len_mid == len_bc) {
  356. mvP->x = ax;
  357. mvP->y = ay;
  358. } else {
  359. mvP->x = bx;
  360. mvP->y = by;
  361. }
  362. }
  363. static inline void mv_pred_direct(AVSContext *h, vector_t *pmv_fw,
  364. vector_t *col_mv) {
  365. vector_t *pmv_bw = pmv_fw + MV_BWD_OFFS;
  366. int den = h->direct_den[col_mv->ref];
  367. int m = col_mv->x >> 31;
  368. pmv_fw->dist = h->dist[1];
  369. pmv_bw->dist = h->dist[0];
  370. pmv_fw->ref = 1;
  371. pmv_bw->ref = 0;
  372. /* scale the co-located motion vector according to its temporal span */
  373. pmv_fw->x = (((den+(den*col_mv->x*pmv_fw->dist^m)-m-1)>>14)^m)-m;
  374. pmv_bw->x = m-(((den+(den*col_mv->x*pmv_bw->dist^m)-m-1)>>14)^m);
  375. m = col_mv->y >> 31;
  376. pmv_fw->y = (((den+(den*col_mv->y*pmv_fw->dist^m)-m-1)>>14)^m)-m;
  377. pmv_bw->y = m-(((den+(den*col_mv->y*pmv_bw->dist^m)-m-1)>>14)^m);
  378. }
  379. static inline void mv_pred_sym(AVSContext *h, vector_t *src, enum block_t size) {
  380. vector_t *dst = src + MV_BWD_OFFS;
  381. /* backward mv is the scaled and negated forward mv */
  382. dst->x = -((src->x * h->sym_factor + 256) >> 9);
  383. dst->y = -((src->y * h->sym_factor + 256) >> 9);
  384. dst->ref = 0;
  385. dst->dist = h->dist[0];
  386. set_mvs(dst, size);
  387. }
  388. static void mv_pred(AVSContext *h, enum mv_loc_t nP, enum mv_loc_t nC,
  389. enum mv_pred_t mode, enum block_t size, int ref) {
  390. vector_t *mvP = &h->mv[nP];
  391. vector_t *mvA = &h->mv[nP-1];
  392. vector_t *mvB = &h->mv[nP-4];
  393. vector_t *mvC = &h->mv[nC];
  394. const vector_t *mvP2 = NULL;
  395. mvP->ref = ref;
  396. mvP->dist = h->dist[mvP->ref];
  397. if(mvC->ref == NOT_AVAIL)
  398. mvC = &h->mv[nP-5]; // set to top-left (mvD)
  399. if((mode == MV_PRED_PSKIP) &&
  400. ((mvA->ref == NOT_AVAIL) || (mvB->ref == NOT_AVAIL) ||
  401. ((mvA->x | mvA->y | mvA->ref) == 0) ||
  402. ((mvB->x | mvB->y | mvB->ref) == 0) )) {
  403. mvP2 = &ff_cavs_un_mv;
  404. /* if there is only one suitable candidate, take it */
  405. } else if((mvA->ref >= 0) && (mvB->ref < 0) && (mvC->ref < 0)) {
  406. mvP2= mvA;
  407. } else if((mvA->ref < 0) && (mvB->ref >= 0) && (mvC->ref < 0)) {
  408. mvP2= mvB;
  409. } else if((mvA->ref < 0) && (mvB->ref < 0) && (mvC->ref >= 0)) {
  410. mvP2= mvC;
  411. } else if(mode == MV_PRED_LEFT && mvA->ref == ref){
  412. mvP2= mvA;
  413. } else if(mode == MV_PRED_TOP && mvB->ref == ref){
  414. mvP2= mvB;
  415. } else if(mode == MV_PRED_TOPRIGHT && mvC->ref == ref){
  416. mvP2= mvC;
  417. }
  418. if(mvP2){
  419. mvP->x = mvP2->x;
  420. mvP->y = mvP2->y;
  421. }else
  422. mv_pred_median(h, mvP, mvA, mvB, mvC);
  423. if(mode < MV_PRED_PSKIP) {
  424. mvP->x += get_se_golomb(&h->s.gb);
  425. mvP->y += get_se_golomb(&h->s.gb);
  426. }
  427. set_mvs(mvP,size);
  428. }
  429. /*****************************************************************************
  430. *
  431. * residual data decoding
  432. *
  433. ****************************************************************************/
  434. /** kth-order exponential golomb code */
  435. static inline int get_ue_code(GetBitContext *gb, int order) {
  436. if(order) {
  437. int ret = get_ue_golomb(gb) << order;
  438. return ret + get_bits(gb,order);
  439. }
  440. return get_ue_golomb(gb);
  441. }
  442. /**
  443. * decode coefficients from one 8x8 block, dequantize, inverse transform
  444. * and add them to sample block
  445. * @param r pointer to 2D VLC table
  446. * @param esc_golomb_order escape codes are k-golomb with this order k
  447. * @param qp quantizer
  448. * @param dst location of sample block
  449. * @param stride line stride in frame buffer
  450. */
  451. static int decode_residual_block(AVSContext *h, GetBitContext *gb,
  452. const residual_vlc_t *r, int esc_golomb_order,
  453. int qp, uint8_t *dst, int stride) {
  454. int i,pos = -1;
  455. int level_code, esc_code, level, run, mask;
  456. int level_buf[64];
  457. int run_buf[64];
  458. int dqm = dequant_mul[qp];
  459. int dqs = dequant_shift[qp];
  460. int dqa = 1 << (dqs - 1);
  461. const uint8_t *scantab = h->scantable.permutated;
  462. DCTELEM *block = h->block;
  463. for(i=0;i<65;i++) {
  464. level_code = get_ue_code(gb,r->golomb_order);
  465. if(level_code >= ESCAPE_CODE) {
  466. run = ((level_code - ESCAPE_CODE) >> 1) + 1;
  467. esc_code = get_ue_code(gb,esc_golomb_order);
  468. level = esc_code + (run > r->max_run ? 1 : r->level_add[run]);
  469. while(level > r->inc_limit)
  470. r++;
  471. mask = -(level_code & 1);
  472. level = (level^mask) - mask;
  473. } else {
  474. level = r->rltab[level_code][0];
  475. if(!level) //end of block signal
  476. break;
  477. run = r->rltab[level_code][1];
  478. r += r->rltab[level_code][2];
  479. }
  480. level_buf[i] = level;
  481. run_buf[i] = run;
  482. }
  483. /* inverse scan and dequantization */
  484. while(--i >= 0){
  485. pos += run_buf[i];
  486. if(pos > 63) {
  487. av_log(h->s.avctx, AV_LOG_ERROR,
  488. "position out of block bounds at pic %d MB(%d,%d)\n",
  489. h->picture.poc, h->mbx, h->mby);
  490. return -1;
  491. }
  492. block[scantab[pos]] = (level_buf[i]*dqm + dqa) >> dqs;
  493. }
  494. h->s.dsp.cavs_idct8_add(dst,block,stride);
  495. return 0;
  496. }
  497. static inline void decode_residual_chroma(AVSContext *h) {
  498. if(h->cbp & (1<<4))
  499. decode_residual_block(h,&h->s.gb,chroma_2dvlc,0, chroma_qp[h->qp],
  500. h->cu,h->c_stride);
  501. if(h->cbp & (1<<5))
  502. decode_residual_block(h,&h->s.gb,chroma_2dvlc,0, chroma_qp[h->qp],
  503. h->cv,h->c_stride);
  504. }
  505. static inline int decode_residual_inter(AVSContext *h) {
  506. int block;
  507. /* get coded block pattern */
  508. int cbp= get_ue_golomb(&h->s.gb);
  509. if(cbp > 63){
  510. av_log(h->s.avctx, AV_LOG_ERROR, "illegal inter cbp\n");
  511. return -1;
  512. }
  513. h->cbp = cbp_tab[cbp][1];
  514. /* get quantizer */
  515. if(h->cbp && !h->qp_fixed)
  516. h->qp = (h->qp + get_se_golomb(&h->s.gb)) & 63;
  517. for(block=0;block<4;block++)
  518. if(h->cbp & (1<<block))
  519. decode_residual_block(h,&h->s.gb,inter_2dvlc,0,h->qp,
  520. h->cy + h->luma_scan[block], h->l_stride);
  521. decode_residual_chroma(h);
  522. return 0;
  523. }
  524. /*****************************************************************************
  525. *
  526. * macroblock level
  527. *
  528. ****************************************************************************/
  529. static int decode_mb_i(AVSContext *h, int cbp_code) {
  530. GetBitContext *gb = &h->s.gb;
  531. int block, pred_mode_uv;
  532. uint8_t top[18];
  533. uint8_t *left = NULL;
  534. uint8_t *d;
  535. init_mb(h);
  536. /* get intra prediction modes from stream */
  537. for(block=0;block<4;block++) {
  538. int nA,nB,predpred;
  539. int pos = scan3x3[block];
  540. nA = h->pred_mode_Y[pos-1];
  541. nB = h->pred_mode_Y[pos-3];
  542. predpred = FFMIN(nA,nB);
  543. if(predpred == NOT_AVAIL) // if either is not available
  544. predpred = INTRA_L_LP;
  545. if(!get_bits1(gb)){
  546. int rem_mode= get_bits(gb, 2);
  547. predpred = rem_mode + (rem_mode >= predpred);
  548. }
  549. h->pred_mode_Y[pos] = predpred;
  550. }
  551. pred_mode_uv = get_ue_golomb(gb);
  552. if(pred_mode_uv > 6) {
  553. av_log(h->s.avctx, AV_LOG_ERROR, "illegal intra chroma pred mode\n");
  554. return -1;
  555. }
  556. /* save pred modes before they get modified */
  557. h->pred_mode_Y[3] = h->pred_mode_Y[5];
  558. h->pred_mode_Y[6] = h->pred_mode_Y[8];
  559. h->top_pred_Y[h->mbx*2+0] = h->pred_mode_Y[7];
  560. h->top_pred_Y[h->mbx*2+1] = h->pred_mode_Y[8];
  561. /* modify pred modes according to availability of neighbour samples */
  562. if(!(h->flags & A_AVAIL)) {
  563. modify_pred(left_modifier_l, &h->pred_mode_Y[4] );
  564. modify_pred(left_modifier_l, &h->pred_mode_Y[7] );
  565. modify_pred(left_modifier_c, &pred_mode_uv );
  566. }
  567. if(!(h->flags & B_AVAIL)) {
  568. modify_pred(top_modifier_l, &h->pred_mode_Y[4] );
  569. modify_pred(top_modifier_l, &h->pred_mode_Y[5] );
  570. modify_pred(top_modifier_c, &pred_mode_uv );
  571. }
  572. /* get coded block pattern */
  573. if(h->pic_type == FF_I_TYPE)
  574. cbp_code = get_ue_golomb(gb);
  575. if(cbp_code > 63){
  576. av_log(h->s.avctx, AV_LOG_ERROR, "illegal intra cbp\n");
  577. return -1;
  578. }
  579. h->cbp = cbp_tab[cbp_code][0];
  580. if(h->cbp && !h->qp_fixed)
  581. h->qp = (h->qp + get_se_golomb(gb)) & 63; //qp_delta
  582. /* luma intra prediction interleaved with residual decode/transform/add */
  583. for(block=0;block<4;block++) {
  584. d = h->cy + h->luma_scan[block];
  585. load_intra_pred_luma(h, top, &left, block);
  586. h->intra_pred_l[h->pred_mode_Y[scan3x3[block]]]
  587. (d, top, left, h->l_stride);
  588. if(h->cbp & (1<<block))
  589. decode_residual_block(h,gb,intra_2dvlc,1,h->qp,d,h->l_stride);
  590. }
  591. /* chroma intra prediction */
  592. /* extend borders by one pixel */
  593. h->left_border_u[9] = h->left_border_u[8];
  594. h->left_border_v[9] = h->left_border_v[8];
  595. h->top_border_u[h->mbx*10+9] = h->top_border_u[h->mbx*10+8];
  596. h->top_border_v[h->mbx*10+9] = h->top_border_v[h->mbx*10+8];
  597. if(h->mbx && h->mby) {
  598. h->top_border_u[h->mbx*10] = h->left_border_u[0] = h->topleft_border_u;
  599. h->top_border_v[h->mbx*10] = h->left_border_v[0] = h->topleft_border_v;
  600. } else {
  601. h->left_border_u[0] = h->left_border_u[1];
  602. h->left_border_v[0] = h->left_border_v[1];
  603. h->top_border_u[h->mbx*10] = h->top_border_u[h->mbx*10+1];
  604. h->top_border_v[h->mbx*10] = h->top_border_v[h->mbx*10+1];
  605. }
  606. h->intra_pred_c[pred_mode_uv](h->cu, &h->top_border_u[h->mbx*10],
  607. h->left_border_u, h->c_stride);
  608. h->intra_pred_c[pred_mode_uv](h->cv, &h->top_border_v[h->mbx*10],
  609. h->left_border_v, h->c_stride);
  610. decode_residual_chroma(h);
  611. filter_mb(h,I_8X8);
  612. /* mark motion vectors as intra */
  613. h->mv[MV_FWD_X0] = ff_cavs_intra_mv;
  614. set_mvs(&h->mv[MV_FWD_X0], BLK_16X16);
  615. h->mv[MV_BWD_X0] = ff_cavs_intra_mv;
  616. set_mvs(&h->mv[MV_BWD_X0], BLK_16X16);
  617. if(h->pic_type != FF_B_TYPE)
  618. *h->col_type = I_8X8;
  619. return 0;
  620. }
  621. static void decode_mb_p(AVSContext *h, enum mb_t mb_type) {
  622. GetBitContext *gb = &h->s.gb;
  623. int ref[4];
  624. init_mb(h);
  625. switch(mb_type) {
  626. case P_SKIP:
  627. mv_pred(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_PSKIP, BLK_16X16, 0);
  628. break;
  629. case P_16X16:
  630. ref[0] = h->ref_flag ? 0 : get_bits1(gb);
  631. mv_pred(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_MEDIAN, BLK_16X16,ref[0]);
  632. break;
  633. case P_16X8:
  634. ref[0] = h->ref_flag ? 0 : get_bits1(gb);
  635. ref[2] = h->ref_flag ? 0 : get_bits1(gb);
  636. mv_pred(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_TOP, BLK_16X8, ref[0]);
  637. mv_pred(h, MV_FWD_X2, MV_FWD_A1, MV_PRED_LEFT, BLK_16X8, ref[2]);
  638. break;
  639. case P_8X16:
  640. ref[0] = h->ref_flag ? 0 : get_bits1(gb);
  641. ref[1] = h->ref_flag ? 0 : get_bits1(gb);
  642. mv_pred(h, MV_FWD_X0, MV_FWD_B3, MV_PRED_LEFT, BLK_8X16, ref[0]);
  643. mv_pred(h, MV_FWD_X1, MV_FWD_C2, MV_PRED_TOPRIGHT, BLK_8X16, ref[1]);
  644. break;
  645. case P_8X8:
  646. ref[0] = h->ref_flag ? 0 : get_bits1(gb);
  647. ref[1] = h->ref_flag ? 0 : get_bits1(gb);
  648. ref[2] = h->ref_flag ? 0 : get_bits1(gb);
  649. ref[3] = h->ref_flag ? 0 : get_bits1(gb);
  650. mv_pred(h, MV_FWD_X0, MV_FWD_B3, MV_PRED_MEDIAN, BLK_8X8, ref[0]);
  651. mv_pred(h, MV_FWD_X1, MV_FWD_C2, MV_PRED_MEDIAN, BLK_8X8, ref[1]);
  652. mv_pred(h, MV_FWD_X2, MV_FWD_X1, MV_PRED_MEDIAN, BLK_8X8, ref[2]);
  653. mv_pred(h, MV_FWD_X3, MV_FWD_X0, MV_PRED_MEDIAN, BLK_8X8, ref[3]);
  654. }
  655. inter_pred(h, mb_type);
  656. store_mvs(h);
  657. if(mb_type != P_SKIP)
  658. decode_residual_inter(h);
  659. filter_mb(h,mb_type);
  660. *h->col_type = mb_type;
  661. }
  662. static void decode_mb_b(AVSContext *h, enum mb_t mb_type) {
  663. int block;
  664. enum sub_mb_t sub_type[4];
  665. int flags;
  666. init_mb(h);
  667. /* reset all MVs */
  668. h->mv[MV_FWD_X0] = ff_cavs_dir_mv;
  669. set_mvs(&h->mv[MV_FWD_X0], BLK_16X16);
  670. h->mv[MV_BWD_X0] = ff_cavs_dir_mv;
  671. set_mvs(&h->mv[MV_BWD_X0], BLK_16X16);
  672. switch(mb_type) {
  673. case B_SKIP:
  674. case B_DIRECT:
  675. if(!(*h->col_type)) {
  676. /* intra MB at co-location, do in-plane prediction */
  677. mv_pred(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_BSKIP, BLK_16X16, 1);
  678. mv_pred(h, MV_BWD_X0, MV_BWD_C2, MV_PRED_BSKIP, BLK_16X16, 0);
  679. } else
  680. /* direct prediction from co-located P MB, block-wise */
  681. for(block=0;block<4;block++)
  682. mv_pred_direct(h,&h->mv[mv_scan[block]],
  683. &h->col_mv[(h->mby*h->mb_width+h->mbx)*4 + block]);
  684. break;
  685. case B_FWD_16X16:
  686. mv_pred(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_MEDIAN, BLK_16X16, 1);
  687. break;
  688. case B_SYM_16X16:
  689. mv_pred(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_MEDIAN, BLK_16X16, 1);
  690. mv_pred_sym(h, &h->mv[MV_FWD_X0], BLK_16X16);
  691. break;
  692. case B_BWD_16X16:
  693. mv_pred(h, MV_BWD_X0, MV_BWD_C2, MV_PRED_MEDIAN, BLK_16X16, 0);
  694. break;
  695. case B_8X8:
  696. for(block=0;block<4;block++)
  697. sub_type[block] = get_bits(&h->s.gb,2);
  698. for(block=0;block<4;block++) {
  699. switch(sub_type[block]) {
  700. case B_SUB_DIRECT:
  701. if(!(*h->col_type)) {
  702. /* intra MB at co-location, do in-plane prediction */
  703. mv_pred(h, mv_scan[block], mv_scan[block]-3,
  704. MV_PRED_BSKIP, BLK_8X8, 1);
  705. mv_pred(h, mv_scan[block]+MV_BWD_OFFS,
  706. mv_scan[block]-3+MV_BWD_OFFS,
  707. MV_PRED_BSKIP, BLK_8X8, 0);
  708. } else
  709. mv_pred_direct(h,&h->mv[mv_scan[block]],
  710. &h->col_mv[(h->mby*h->mb_width + h->mbx)*4 + block]);
  711. break;
  712. case B_SUB_FWD:
  713. mv_pred(h, mv_scan[block], mv_scan[block]-3,
  714. MV_PRED_MEDIAN, BLK_8X8, 1);
  715. break;
  716. case B_SUB_SYM:
  717. mv_pred(h, mv_scan[block], mv_scan[block]-3,
  718. MV_PRED_MEDIAN, BLK_8X8, 1);
  719. mv_pred_sym(h, &h->mv[mv_scan[block]], BLK_8X8);
  720. break;
  721. }
  722. }
  723. for(block=0;block<4;block++) {
  724. if(sub_type[block] == B_SUB_BWD)
  725. mv_pred(h, mv_scan[block]+MV_BWD_OFFS,
  726. mv_scan[block]+MV_BWD_OFFS-3,
  727. MV_PRED_MEDIAN, BLK_8X8, 0);
  728. }
  729. break;
  730. default:
  731. assert((mb_type > B_SYM_16X16) && (mb_type < B_8X8));
  732. flags = partition_flags[mb_type];
  733. if(mb_type & 1) { /* 16x8 macroblock types */
  734. if(flags & FWD0)
  735. mv_pred(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_TOP, BLK_16X8, 1);
  736. if(flags & SYM0)
  737. mv_pred_sym(h, &h->mv[MV_FWD_X0], BLK_16X8);
  738. if(flags & FWD1)
  739. mv_pred(h, MV_FWD_X2, MV_FWD_A1, MV_PRED_LEFT, BLK_16X8, 1);
  740. if(flags & SYM1)
  741. mv_pred_sym(h, &h->mv[MV_FWD_X2], BLK_16X8);
  742. if(flags & BWD0)
  743. mv_pred(h, MV_BWD_X0, MV_BWD_C2, MV_PRED_TOP, BLK_16X8, 0);
  744. if(flags & BWD1)
  745. mv_pred(h, MV_BWD_X2, MV_BWD_A1, MV_PRED_LEFT, BLK_16X8, 0);
  746. } else { /* 8x16 macroblock types */
  747. if(flags & FWD0)
  748. mv_pred(h, MV_FWD_X0, MV_FWD_B3, MV_PRED_LEFT, BLK_8X16, 1);
  749. if(flags & SYM0)
  750. mv_pred_sym(h, &h->mv[MV_FWD_X0], BLK_8X16);
  751. if(flags & FWD1)
  752. mv_pred(h, MV_FWD_X1, MV_FWD_C2, MV_PRED_TOPRIGHT,BLK_8X16, 1);
  753. if(flags & SYM1)
  754. mv_pred_sym(h, &h->mv[MV_FWD_X1], BLK_8X16);
  755. if(flags & BWD0)
  756. mv_pred(h, MV_BWD_X0, MV_BWD_B3, MV_PRED_LEFT, BLK_8X16, 0);
  757. if(flags & BWD1)
  758. mv_pred(h, MV_BWD_X1, MV_BWD_C2, MV_PRED_TOPRIGHT,BLK_8X16, 0);
  759. }
  760. }
  761. inter_pred(h, mb_type);
  762. if(mb_type != B_SKIP)
  763. decode_residual_inter(h);
  764. filter_mb(h,mb_type);
  765. }
  766. /*****************************************************************************
  767. *
  768. * slice level
  769. *
  770. ****************************************************************************/
  771. static inline int decode_slice_header(AVSContext *h, GetBitContext *gb) {
  772. if(h->stc > 0xAF)
  773. av_log(h->s.avctx, AV_LOG_ERROR, "unexpected start code 0x%02x\n", h->stc);
  774. h->mby = h->stc;
  775. if((h->mby == 0) && (!h->qp_fixed)){
  776. h->qp_fixed = get_bits1(gb);
  777. h->qp = get_bits(gb,6);
  778. }
  779. /* inter frame or second slice can have weighting params */
  780. if((h->pic_type != FF_I_TYPE) || (!h->pic_structure && h->mby >= h->mb_width/2))
  781. if(get_bits1(gb)) { //slice_weighting_flag
  782. av_log(h->s.avctx, AV_LOG_ERROR,
  783. "weighted prediction not yet supported\n");
  784. }
  785. return 0;
  786. }
  787. static inline void check_for_slice(AVSContext *h) {
  788. GetBitContext *gb = &h->s.gb;
  789. int align;
  790. align = (-get_bits_count(gb)) & 7;
  791. if((show_bits_long(gb,24+align) & 0xFFFFFF) == 0x000001) {
  792. get_bits_long(gb,24+align);
  793. h->stc = get_bits(gb,8);
  794. decode_slice_header(h,gb);
  795. }
  796. }
  797. /*****************************************************************************
  798. *
  799. * frame level
  800. *
  801. ****************************************************************************/
  802. static void init_pic(AVSContext *h) {
  803. int i;
  804. /* clear some predictors */
  805. for(i=0;i<=20;i+=4)
  806. h->mv[i] = ff_cavs_un_mv;
  807. h->mv[MV_BWD_X0] = ff_cavs_dir_mv;
  808. set_mvs(&h->mv[MV_BWD_X0], BLK_16X16);
  809. h->mv[MV_FWD_X0] = ff_cavs_dir_mv;
  810. set_mvs(&h->mv[MV_FWD_X0], BLK_16X16);
  811. h->pred_mode_Y[3] = h->pred_mode_Y[6] = NOT_AVAIL;
  812. h->cy = h->picture.data[0];
  813. h->cu = h->picture.data[1];
  814. h->cv = h->picture.data[2];
  815. h->l_stride = h->picture.linesize[0];
  816. h->c_stride = h->picture.linesize[1];
  817. h->luma_scan[2] = 8*h->l_stride;
  818. h->luma_scan[3] = 8*h->l_stride+8;
  819. h->mbx = h->mby = 0;
  820. h->flags = 0;
  821. }
  822. static int decode_pic(AVSContext *h) {
  823. MpegEncContext *s = &h->s;
  824. int skip_count;
  825. enum mb_t mb_type;
  826. if (!s->context_initialized) {
  827. s->avctx->idct_algo = FF_IDCT_CAVS;
  828. if (MPV_common_init(s) < 0)
  829. return -1;
  830. ff_init_scantable(s->dsp.idct_permutation,&h->scantable,ff_zigzag_direct);
  831. }
  832. get_bits(&s->gb,16);//bbv_dwlay
  833. if(h->stc == PIC_PB_START_CODE) {
  834. h->pic_type = get_bits(&s->gb,2) + FF_I_TYPE;
  835. if(h->pic_type > FF_B_TYPE) {
  836. av_log(s->avctx, AV_LOG_ERROR, "illegal picture type\n");
  837. return -1;
  838. }
  839. /* make sure we have the reference frames we need */
  840. if(!h->DPB[0].data[0] ||
  841. (!h->DPB[1].data[0] && h->pic_type == FF_B_TYPE))
  842. return -1;
  843. } else {
  844. h->pic_type = FF_I_TYPE;
  845. if(get_bits1(&s->gb))
  846. get_bits(&s->gb,16);//time_code
  847. }
  848. /* release last B frame */
  849. if(h->picture.data[0])
  850. s->avctx->release_buffer(s->avctx, (AVFrame *)&h->picture);
  851. s->avctx->get_buffer(s->avctx, (AVFrame *)&h->picture);
  852. init_pic(h);
  853. h->picture.poc = get_bits(&s->gb,8)*2;
  854. /* get temporal distances and MV scaling factors */
  855. if(h->pic_type != FF_B_TYPE) {
  856. h->dist[0] = (h->picture.poc - h->DPB[0].poc + 512) % 512;
  857. } else {
  858. h->dist[0] = (h->DPB[0].poc - h->picture.poc + 512) % 512;
  859. }
  860. h->dist[1] = (h->picture.poc - h->DPB[1].poc + 512) % 512;
  861. h->scale_den[0] = h->dist[0] ? 512/h->dist[0] : 0;
  862. h->scale_den[1] = h->dist[1] ? 512/h->dist[1] : 0;
  863. if(h->pic_type == FF_B_TYPE) {
  864. h->sym_factor = h->dist[0]*h->scale_den[1];
  865. } else {
  866. h->direct_den[0] = h->dist[0] ? 16384/h->dist[0] : 0;
  867. h->direct_den[1] = h->dist[1] ? 16384/h->dist[1] : 0;
  868. }
  869. if(s->low_delay)
  870. get_ue_golomb(&s->gb); //bbv_check_times
  871. h->progressive = get_bits1(&s->gb);
  872. if(h->progressive)
  873. h->pic_structure = 1;
  874. else if(!(h->pic_structure = get_bits1(&s->gb) && (h->stc == PIC_PB_START_CODE)) )
  875. get_bits1(&s->gb); //advanced_pred_mode_disable
  876. skip_bits1(&s->gb); //top_field_first
  877. skip_bits1(&s->gb); //repeat_first_field
  878. h->qp_fixed = get_bits1(&s->gb);
  879. h->qp = get_bits(&s->gb,6);
  880. if(h->pic_type == FF_I_TYPE) {
  881. if(!h->progressive && !h->pic_structure)
  882. skip_bits1(&s->gb);//what is this?
  883. skip_bits(&s->gb,4); //reserved bits
  884. } else {
  885. if(!(h->pic_type == FF_B_TYPE && h->pic_structure == 1))
  886. h->ref_flag = get_bits1(&s->gb);
  887. skip_bits(&s->gb,4); //reserved bits
  888. h->skip_mode_flag = get_bits1(&s->gb);
  889. }
  890. h->loop_filter_disable = get_bits1(&s->gb);
  891. if(!h->loop_filter_disable && get_bits1(&s->gb)) {
  892. h->alpha_offset = get_se_golomb(&s->gb);
  893. h->beta_offset = get_se_golomb(&s->gb);
  894. } else {
  895. h->alpha_offset = h->beta_offset = 0;
  896. }
  897. check_for_slice(h);
  898. if(h->pic_type == FF_I_TYPE) {
  899. do {
  900. decode_mb_i(h, 0);
  901. } while(next_mb(h));
  902. } else if(h->pic_type == FF_P_TYPE) {
  903. do {
  904. if(h->skip_mode_flag) {
  905. skip_count = get_ue_golomb(&s->gb);
  906. while(skip_count--) {
  907. decode_mb_p(h,P_SKIP);
  908. if(!next_mb(h))
  909. goto done;
  910. }
  911. mb_type = get_ue_golomb(&s->gb) + P_16X16;
  912. } else
  913. mb_type = get_ue_golomb(&s->gb) + P_SKIP;
  914. if(mb_type > P_8X8) {
  915. decode_mb_i(h, mb_type - P_8X8 - 1);
  916. } else
  917. decode_mb_p(h,mb_type);
  918. } while(next_mb(h));
  919. } else { /* FF_B_TYPE */
  920. do {
  921. if(h->skip_mode_flag) {
  922. skip_count = get_ue_golomb(&s->gb);
  923. while(skip_count--) {
  924. decode_mb_b(h,B_SKIP);
  925. if(!next_mb(h))
  926. goto done;
  927. }
  928. mb_type = get_ue_golomb(&s->gb) + B_DIRECT;
  929. } else
  930. mb_type = get_ue_golomb(&s->gb) + B_SKIP;
  931. if(mb_type > B_8X8) {
  932. decode_mb_i(h, mb_type - B_8X8 - 1);
  933. } else
  934. decode_mb_b(h,mb_type);
  935. } while(next_mb(h));
  936. }
  937. done:
  938. if(h->pic_type != FF_B_TYPE) {
  939. if(h->DPB[1].data[0])
  940. s->avctx->release_buffer(s->avctx, (AVFrame *)&h->DPB[1]);
  941. memcpy(&h->DPB[1], &h->DPB[0], sizeof(Picture));
  942. memcpy(&h->DPB[0], &h->picture, sizeof(Picture));
  943. memset(&h->picture,0,sizeof(Picture));
  944. }
  945. return 0;
  946. }
  947. /*****************************************************************************
  948. *
  949. * headers and interface
  950. *
  951. ****************************************************************************/
  952. /**
  953. * some predictions require data from the top-neighbouring macroblock.
  954. * this data has to be stored for one complete row of macroblocks
  955. * and this storage space is allocated here
  956. */
  957. static void init_top_lines(AVSContext *h) {
  958. /* alloc top line of predictors */
  959. h->top_qp = av_malloc( h->mb_width);
  960. h->top_mv[0] = av_malloc((h->mb_width*2+1)*sizeof(vector_t));
  961. h->top_mv[1] = av_malloc((h->mb_width*2+1)*sizeof(vector_t));
  962. h->top_pred_Y = av_malloc( h->mb_width*2*sizeof(*h->top_pred_Y));
  963. h->top_border_y = av_malloc((h->mb_width+1)*16);
  964. h->top_border_u = av_malloc((h->mb_width)*10);
  965. h->top_border_v = av_malloc((h->mb_width)*10);
  966. /* alloc space for co-located MVs and types */
  967. h->col_mv = av_malloc( h->mb_width*h->mb_height*4*sizeof(vector_t));
  968. h->col_type_base = av_malloc(h->mb_width*h->mb_height);
  969. h->block = av_mallocz(64*sizeof(DCTELEM));
  970. }
  971. static int decode_seq_header(AVSContext *h) {
  972. MpegEncContext *s = &h->s;
  973. int frame_rate_code;
  974. h->profile = get_bits(&s->gb,8);
  975. h->level = get_bits(&s->gb,8);
  976. skip_bits1(&s->gb); //progressive sequence
  977. s->width = get_bits(&s->gb,14);
  978. s->height = get_bits(&s->gb,14);
  979. skip_bits(&s->gb,2); //chroma format
  980. skip_bits(&s->gb,3); //sample_precision
  981. h->aspect_ratio = get_bits(&s->gb,4);
  982. frame_rate_code = get_bits(&s->gb,4);
  983. skip_bits(&s->gb,18);//bit_rate_lower
  984. skip_bits1(&s->gb); //marker_bit
  985. skip_bits(&s->gb,12);//bit_rate_upper
  986. s->low_delay = get_bits1(&s->gb);
  987. h->mb_width = (s->width + 15) >> 4;
  988. h->mb_height = (s->height + 15) >> 4;
  989. h->s.avctx->time_base.den = ff_frame_rate_tab[frame_rate_code].num;
  990. h->s.avctx->time_base.num = ff_frame_rate_tab[frame_rate_code].den;
  991. h->s.avctx->width = s->width;
  992. h->s.avctx->height = s->height;
  993. if(!h->top_qp)
  994. init_top_lines(h);
  995. return 0;
  996. }
  997. static void cavs_flush(AVCodecContext * avctx) {
  998. AVSContext *h = avctx->priv_data;
  999. h->got_keyframe = 0;
  1000. }
  1001. static int cavs_decode_frame(AVCodecContext * avctx,void *data, int *data_size,
  1002. uint8_t * buf, int buf_size) {
  1003. AVSContext *h = avctx->priv_data;
  1004. MpegEncContext *s = &h->s;
  1005. int input_size;
  1006. const uint8_t *buf_end;
  1007. const uint8_t *buf_ptr;
  1008. AVFrame *picture = data;
  1009. uint32_t stc;
  1010. s->avctx = avctx;
  1011. if (buf_size == 0) {
  1012. if(!s->low_delay && h->DPB[0].data[0]) {
  1013. *data_size = sizeof(AVPicture);
  1014. *picture = *(AVFrame *) &h->DPB[0];
  1015. }
  1016. return 0;
  1017. }
  1018. buf_ptr = buf;
  1019. buf_end = buf + buf_size;
  1020. for(;;) {
  1021. buf_ptr = ff_find_start_code(buf_ptr,buf_end, &stc);
  1022. if(stc & 0xFFFFFE00)
  1023. return FFMAX(0, buf_ptr - buf - s->parse_context.last_index);
  1024. input_size = (buf_end - buf_ptr)*8;
  1025. switch(stc) {
  1026. case CAVS_START_CODE:
  1027. init_get_bits(&s->gb, buf_ptr, input_size);
  1028. decode_seq_header(h);
  1029. break;
  1030. case PIC_I_START_CODE:
  1031. if(!h->got_keyframe) {
  1032. if(h->DPB[0].data[0])
  1033. avctx->release_buffer(avctx, (AVFrame *)&h->DPB[0]);
  1034. if(h->DPB[1].data[0])
  1035. avctx->release_buffer(avctx, (AVFrame *)&h->DPB[1]);
  1036. h->got_keyframe = 1;
  1037. }
  1038. case PIC_PB_START_CODE:
  1039. *data_size = 0;
  1040. if(!h->got_keyframe)
  1041. break;
  1042. init_get_bits(&s->gb, buf_ptr, input_size);
  1043. h->stc = stc;
  1044. if(decode_pic(h))
  1045. break;
  1046. *data_size = sizeof(AVPicture);
  1047. if(h->pic_type != FF_B_TYPE) {
  1048. if(h->DPB[1].data[0]) {
  1049. *picture = *(AVFrame *) &h->DPB[1];
  1050. } else {
  1051. *data_size = 0;
  1052. }
  1053. } else
  1054. *picture = *(AVFrame *) &h->picture;
  1055. break;
  1056. case EXT_START_CODE:
  1057. //mpeg_decode_extension(avctx,buf_ptr, input_size);
  1058. break;
  1059. case USER_START_CODE:
  1060. //mpeg_decode_user_data(avctx,buf_ptr, input_size);
  1061. break;
  1062. default:
  1063. if (stc >= SLICE_MIN_START_CODE &&
  1064. stc <= SLICE_MAX_START_CODE) {
  1065. init_get_bits(&s->gb, buf_ptr, input_size);
  1066. decode_slice_header(h, &s->gb);
  1067. }
  1068. break;
  1069. }
  1070. }
  1071. }
  1072. static int cavs_decode_init(AVCodecContext * avctx) {
  1073. AVSContext *h = avctx->priv_data;
  1074. MpegEncContext * const s = &h->s;
  1075. MPV_decode_defaults(s);
  1076. s->avctx = avctx;
  1077. avctx->pix_fmt= PIX_FMT_YUV420P;
  1078. h->luma_scan[0] = 0;
  1079. h->luma_scan[1] = 8;
  1080. h->intra_pred_l[ INTRA_L_VERT] = intra_pred_vert;
  1081. h->intra_pred_l[ INTRA_L_HORIZ] = intra_pred_horiz;
  1082. h->intra_pred_l[ INTRA_L_LP] = intra_pred_lp;
  1083. h->intra_pred_l[ INTRA_L_DOWN_LEFT] = intra_pred_down_left;
  1084. h->intra_pred_l[INTRA_L_DOWN_RIGHT] = intra_pred_down_right;
  1085. h->intra_pred_l[ INTRA_L_LP_LEFT] = intra_pred_lp_left;
  1086. h->intra_pred_l[ INTRA_L_LP_TOP] = intra_pred_lp_top;
  1087. h->intra_pred_l[ INTRA_L_DC_128] = intra_pred_dc_128;
  1088. h->intra_pred_c[ INTRA_C_LP] = intra_pred_lp;
  1089. h->intra_pred_c[ INTRA_C_HORIZ] = intra_pred_horiz;
  1090. h->intra_pred_c[ INTRA_C_VERT] = intra_pred_vert;
  1091. h->intra_pred_c[ INTRA_C_PLANE] = intra_pred_plane;
  1092. h->intra_pred_c[ INTRA_C_LP_LEFT] = intra_pred_lp_left;
  1093. h->intra_pred_c[ INTRA_C_LP_TOP] = intra_pred_lp_top;
  1094. h->intra_pred_c[ INTRA_C_DC_128] = intra_pred_dc_128;
  1095. h->mv[ 7] = ff_cavs_un_mv;
  1096. h->mv[19] = ff_cavs_un_mv;
  1097. return 0;
  1098. }
  1099. static int cavs_decode_end(AVCodecContext * avctx) {
  1100. AVSContext *h = avctx->priv_data;
  1101. av_free(h->top_qp);
  1102. av_free(h->top_mv[0]);
  1103. av_free(h->top_mv[1]);
  1104. av_free(h->top_pred_Y);
  1105. av_free(h->top_border_y);
  1106. av_free(h->top_border_u);
  1107. av_free(h->top_border_v);
  1108. av_free(h->col_mv);
  1109. av_free(h->col_type_base);
  1110. av_free(h->block);
  1111. return 0;
  1112. }
  1113. AVCodec cavs_decoder = {
  1114. "cavs",
  1115. CODEC_TYPE_VIDEO,
  1116. CODEC_ID_CAVS,
  1117. sizeof(AVSContext),
  1118. cavs_decode_init,
  1119. NULL,
  1120. cavs_decode_end,
  1121. cavs_decode_frame,
  1122. CODEC_CAP_DR1 | CODEC_CAP_DELAY,
  1123. .flush= cavs_flush,
  1124. };