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.

1547 lines
52KB

  1. /*
  2. * Chinese AVS video (AVS1-P2, JiZhun profile) decoder.
  3. * Copyright (c) 2006 Stefan Gehrer <stefan.gehrer@gmx.de>
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  18. */
  19. #include "avcodec.h"
  20. #include "bitstream.h"
  21. #include "golomb.h"
  22. #include "mpegvideo.h"
  23. #include "cavsdata.h"
  24. typedef struct {
  25. MpegEncContext s;
  26. Picture picture; //currently decoded frame
  27. Picture DPB[2]; //reference frames
  28. int dist[2]; //temporal distances from current frame to ref frames
  29. int profile, level;
  30. int aspect_ratio;
  31. int mb_width, mb_height;
  32. int pic_type;
  33. int progressive;
  34. int pic_structure;
  35. int skip_mode_flag;
  36. int loop_filter_disable;
  37. int alpha_offset, beta_offset;
  38. int ref_flag;
  39. int mbx, mby;
  40. int flags;
  41. int stc;
  42. uint8_t *cy, *cu, *cv;
  43. int left_qp;
  44. uint8_t *top_qp;
  45. /* mv motion vector cache
  46. 0: D3 B2 B3 C2
  47. 4: A1 X0 X1 -
  48. 8: A3 X2 X3 -
  49. X are the vectors in the current macroblock (5,6,9,10)
  50. A is the macroblock to the left (4,8)
  51. B is the macroblock to the top (1,2)
  52. C is the macroblock to the top-right (3)
  53. D is the macroblock to the top-left (0)
  54. the same is repeated for backward motion vectors */
  55. vector_t mv[2*4*3];
  56. vector_t *top_mv[2];
  57. vector_t *col_mv;
  58. /* luma pred mode cache
  59. 0: -- B2 B3
  60. 3: A1 X0 X1
  61. 6: A3 X2 X3 */
  62. int pred_mode_Y[3*3];
  63. int *top_pred_Y;
  64. int l_stride, c_stride;
  65. int luma_scan[4];
  66. int qp;
  67. int qp_fixed;
  68. int cbp;
  69. /* intra prediction is done with un-deblocked samples
  70. they are saved here before deblocking the MB */
  71. uint8_t *top_border_y, *top_border_u, *top_border_v;
  72. uint8_t left_border_y[16], left_border_u[8], left_border_v[8];
  73. uint8_t topleft_border_y, topleft_border_u, topleft_border_v;
  74. void (*intra_pred_l[8])(uint8_t *d,uint8_t *top,uint8_t *left,int stride);
  75. void (*intra_pred_c[7])(uint8_t *d,uint8_t *top,uint8_t *left,int stride);
  76. uint8_t *col_type_base;
  77. uint8_t *col_type;
  78. int sym_factor;
  79. int direct_den[2];
  80. int scale_den[2];
  81. int got_keyframe;
  82. } AVSContext;
  83. /*****************************************************************************
  84. *
  85. * in-loop deblocking filter
  86. *
  87. ****************************************************************************/
  88. static inline int get_bs_p(vector_t *mvP, vector_t *mvQ) {
  89. if((mvP->ref == REF_INTRA) || (mvQ->ref == REF_INTRA))
  90. return 2;
  91. if(mvP->ref != mvQ->ref)
  92. return 1;
  93. if( (abs(mvP->x - mvQ->x) >= 4) || (abs(mvP->y - mvQ->y) >= 4) )
  94. return 1;
  95. return 0;
  96. }
  97. static inline int get_bs_b(vector_t *mvP, vector_t *mvQ) {
  98. if((mvP->ref == REF_INTRA) || (mvQ->ref == REF_INTRA)) {
  99. return 2;
  100. } else {
  101. vector_t *mvPbw = mvP + MV_BWD_OFFS;
  102. vector_t *mvQbw = mvQ + MV_BWD_OFFS;
  103. if( (abs( mvP->x - mvQ->x) >= 4) ||
  104. (abs( mvP->y - mvQ->y) >= 4) ||
  105. (abs(mvPbw->x - mvQbw->x) >= 4) ||
  106. (abs(mvPbw->y - mvQbw->y) >= 4) )
  107. return 1;
  108. }
  109. return 0;
  110. }
  111. /* boundary strength (bs) mapping:
  112. *
  113. * --4---5--
  114. * 0 2 |
  115. * | 6 | 7 |
  116. * 1 3 |
  117. * ---------
  118. *
  119. */
  120. #define SET_PARAMS \
  121. alpha = alpha_tab[clip(qp_avg + h->alpha_offset,0,63)]; \
  122. beta = beta_tab[clip(qp_avg + h->beta_offset, 0,63)]; \
  123. tc = tc_tab[clip(qp_avg + h->alpha_offset,0,63)];
  124. static void filter_mb(AVSContext *h, enum mb_t mb_type) {
  125. uint8_t bs[8];
  126. int qp_avg, alpha, beta, tc;
  127. int i;
  128. /* save un-deblocked lines */
  129. h->topleft_border_y = h->top_border_y[h->mbx*16+15];
  130. h->topleft_border_u = h->top_border_u[h->mbx*8+7];
  131. h->topleft_border_v = h->top_border_v[h->mbx*8+7];
  132. memcpy(&h->top_border_y[h->mbx*16], h->cy + 15* h->l_stride,16);
  133. memcpy(&h->top_border_u[h->mbx* 8], h->cu + 7* h->c_stride,8);
  134. memcpy(&h->top_border_v[h->mbx* 8], h->cv + 7* h->c_stride,8);
  135. for(i=0;i<8;i++) {
  136. h->left_border_y[i*2+0] = *(h->cy + 15 + (i*2+0)*h->l_stride);
  137. h->left_border_y[i*2+1] = *(h->cy + 15 + (i*2+1)*h->l_stride);
  138. h->left_border_u[i] = *(h->cu + 7 + i*h->c_stride);
  139. h->left_border_v[i] = *(h->cv + 7 + i*h->c_stride);
  140. }
  141. if(!h->loop_filter_disable) {
  142. /* clear bs */
  143. *((uint64_t *)bs) = 0;
  144. /* determine bs */
  145. switch(mb_type) {
  146. case I_8X8:
  147. *((uint64_t *)bs) = 0x0202020202020202ULL;
  148. break;
  149. case P_8X8:
  150. case P_8X16:
  151. bs[2] = get_bs_p(&h->mv[MV_FWD_X0], &h->mv[MV_FWD_X1]);
  152. bs[3] = get_bs_p(&h->mv[MV_FWD_X2], &h->mv[MV_FWD_X3]);
  153. case P_16X8:
  154. bs[6] = get_bs_p(&h->mv[MV_FWD_X0], &h->mv[MV_FWD_X2]);
  155. bs[7] = get_bs_p(&h->mv[MV_FWD_X1], &h->mv[MV_FWD_X3]);
  156. case P_16X16:
  157. case P_SKIP:
  158. bs[0] = get_bs_p(&h->mv[MV_FWD_A1], &h->mv[MV_FWD_X0]);
  159. bs[1] = get_bs_p(&h->mv[MV_FWD_A3], &h->mv[MV_FWD_X2]);
  160. bs[4] = get_bs_p(&h->mv[MV_FWD_B2], &h->mv[MV_FWD_X0]);
  161. bs[5] = get_bs_p(&h->mv[MV_FWD_B3], &h->mv[MV_FWD_X1]);
  162. break;
  163. case B_SKIP:
  164. case B_DIRECT:
  165. case B_8X8:
  166. bs[2] = get_bs_b(&h->mv[MV_FWD_X0], &h->mv[MV_FWD_X1]);
  167. bs[3] = get_bs_b(&h->mv[MV_FWD_X2], &h->mv[MV_FWD_X3]);
  168. bs[6] = get_bs_b(&h->mv[MV_FWD_X0], &h->mv[MV_FWD_X2]);
  169. bs[7] = get_bs_b(&h->mv[MV_FWD_X1], &h->mv[MV_FWD_X3]);
  170. case B_FWD_16X16:
  171. case B_BWD_16X16:
  172. case B_SYM_16X16:
  173. bs[0] = get_bs_b(&h->mv[MV_FWD_A1], &h->mv[MV_FWD_X0]);
  174. bs[1] = get_bs_b(&h->mv[MV_FWD_A3], &h->mv[MV_FWD_X2]);
  175. bs[4] = get_bs_b(&h->mv[MV_FWD_B2], &h->mv[MV_FWD_X0]);
  176. bs[5] = get_bs_b(&h->mv[MV_FWD_B3], &h->mv[MV_FWD_X1]);
  177. break;
  178. default:
  179. if(mb_type & 1) { //16X8
  180. bs[6] = bs[7] = get_bs_b(&h->mv[MV_FWD_X0], &h->mv[MV_FWD_X2]);
  181. } else { //8X16
  182. bs[2] = bs[3] = get_bs_b(&h->mv[MV_FWD_X0], &h->mv[MV_FWD_X1]);
  183. }
  184. bs[0] = get_bs_b(&h->mv[MV_FWD_A1], &h->mv[MV_FWD_X0]);
  185. bs[1] = get_bs_b(&h->mv[MV_FWD_A3], &h->mv[MV_FWD_X2]);
  186. bs[4] = get_bs_b(&h->mv[MV_FWD_B2], &h->mv[MV_FWD_X0]);
  187. bs[5] = get_bs_b(&h->mv[MV_FWD_B3], &h->mv[MV_FWD_X1]);
  188. }
  189. if( *((uint64_t *)bs) ) {
  190. if(h->flags & A_AVAIL) {
  191. qp_avg = (h->qp + h->left_qp + 1) >> 1;
  192. SET_PARAMS;
  193. h->s.dsp.cavs_filter_lv(h->cy,h->l_stride,alpha,beta,tc,bs[0],bs[1]);
  194. h->s.dsp.cavs_filter_cv(h->cu,h->c_stride,alpha,beta,tc,bs[0],bs[1]);
  195. h->s.dsp.cavs_filter_cv(h->cv,h->c_stride,alpha,beta,tc,bs[0],bs[1]);
  196. }
  197. qp_avg = h->qp;
  198. SET_PARAMS;
  199. h->s.dsp.cavs_filter_lv(h->cy + 8,h->l_stride,alpha,beta,tc,bs[2],bs[3]);
  200. h->s.dsp.cavs_filter_lh(h->cy + 8*h->l_stride,h->l_stride,alpha,beta,tc,
  201. bs[6],bs[7]);
  202. if(h->flags & B_AVAIL) {
  203. qp_avg = (h->qp + h->top_qp[h->mbx] + 1) >> 1;
  204. SET_PARAMS;
  205. h->s.dsp.cavs_filter_lh(h->cy,h->l_stride,alpha,beta,tc,bs[4],bs[5]);
  206. h->s.dsp.cavs_filter_ch(h->cu,h->c_stride,alpha,beta,tc,bs[4],bs[5]);
  207. h->s.dsp.cavs_filter_ch(h->cv,h->c_stride,alpha,beta,tc,bs[4],bs[5]);
  208. }
  209. }
  210. }
  211. h->left_qp = h->qp;
  212. h->top_qp[h->mbx] = h->qp;
  213. }
  214. #undef SET_PARAMS
  215. /*****************************************************************************
  216. *
  217. * spatial intra prediction
  218. *
  219. ****************************************************************************/
  220. static inline void load_intra_pred_luma(AVSContext *h, uint8_t *top,
  221. uint8_t *left, int block) {
  222. int i;
  223. switch(block) {
  224. case 0:
  225. memcpy(&left[1],h->left_border_y,16);
  226. left[0] = left[1];
  227. left[17] = left[16];
  228. memcpy(&top[1],&h->top_border_y[h->mbx*16],16);
  229. top[17] = top[16];
  230. top[0] = top[1];
  231. if((h->flags & A_AVAIL) && (h->flags & B_AVAIL))
  232. left[0] = top[0] = h->topleft_border_y;
  233. break;
  234. case 1:
  235. for(i=0;i<8;i++)
  236. left[i+1] = *(h->cy + 7 + i*h->l_stride);
  237. memset(&left[9],left[8],9);
  238. left[0] = left[1];
  239. memcpy(&top[1],&h->top_border_y[h->mbx*16+8],8);
  240. if(h->flags & C_AVAIL)
  241. memcpy(&top[9],&h->top_border_y[(h->mbx + 1)*16],8);
  242. else
  243. memset(&top[9],top[8],9);
  244. top[17] = top[16];
  245. top[0] = top[1];
  246. if(h->flags & B_AVAIL)
  247. left[0] = top[0] = h->top_border_y[h->mbx*16+7];
  248. break;
  249. case 2:
  250. memcpy(&left[1],&h->left_border_y[8],8);
  251. memset(&left[9],left[8],9);
  252. memcpy(&top[1],h->cy + 7*h->l_stride,16);
  253. top[17] = top[16];
  254. left[0] = h->left_border_y[7];
  255. top[0] = top[1];
  256. if(h->flags & A_AVAIL)
  257. top[0] = left[0];
  258. break;
  259. case 3:
  260. for(i=0;i<9;i++)
  261. left[i] = *(h->cy + 7 + (i+7)*h->l_stride);
  262. memset(&left[9],left[8],9);
  263. memcpy(&top[0],h->cy + 7 + 7*h->l_stride,9);
  264. memset(&top[9],top[8],9);
  265. break;
  266. }
  267. }
  268. static inline void load_intra_pred_chroma(uint8_t *stop, uint8_t *sleft,
  269. uint8_t stopleft, uint8_t *dtop,
  270. uint8_t *dleft, int stride, int flags) {
  271. int i;
  272. if(flags & A_AVAIL) {
  273. for(i=0; i<8; i++)
  274. dleft[i+1] = sleft[i];
  275. dleft[0] = dleft[1];
  276. dleft[9] = dleft[8];
  277. }
  278. if(flags & B_AVAIL) {
  279. for(i=0; i<8; i++)
  280. dtop[i+1] = stop[i];
  281. dtop[0] = dtop[1];
  282. dtop[9] = dtop[8];
  283. if(flags & A_AVAIL)
  284. dleft[0] = dtop[0] = stopleft;
  285. }
  286. }
  287. static void intra_pred_vert(uint8_t *d,uint8_t *top,uint8_t *left,int stride) {
  288. int y;
  289. uint64_t a = *((uint64_t *)(&top[1]));
  290. for(y=0;y<8;y++) {
  291. *((uint64_t *)(d+y*stride)) = a;
  292. }
  293. }
  294. static void intra_pred_horiz(uint8_t *d,uint8_t *top,uint8_t *left,int stride) {
  295. int y;
  296. uint64_t a;
  297. for(y=0;y<8;y++) {
  298. a = left[y+1] * 0x0101010101010101ULL;
  299. *((uint64_t *)(d+y*stride)) = a;
  300. }
  301. }
  302. static void intra_pred_dc_128(uint8_t *d,uint8_t *top,uint8_t *left,int stride) {
  303. int y;
  304. uint64_t a = 0x8080808080808080ULL;
  305. for(y=0;y<8;y++)
  306. *((uint64_t *)(d+y*stride)) = a;
  307. }
  308. static void intra_pred_plane(uint8_t *d,uint8_t *top,uint8_t *left,int stride) {
  309. int x,y,ia;
  310. int ih = 0;
  311. int iv = 0;
  312. uint8_t *cm = cropTbl + MAX_NEG_CROP;
  313. for(x=0; x<4; x++) {
  314. ih += (x+1)*(top[5+x]-top[3-x]);
  315. iv += (x+1)*(left[5+x]-left[3-x]);
  316. }
  317. ia = (top[8]+left[8])<<4;
  318. ih = (17*ih+16)>>5;
  319. iv = (17*iv+16)>>5;
  320. for(y=0; y<8; y++)
  321. for(x=0; x<8; x++)
  322. d[y*stride+x] = cm[(ia+(x-3)*ih+(y-3)*iv+16)>>5];
  323. }
  324. #define LOWPASS(ARRAY,INDEX) \
  325. (( ARRAY[(INDEX)-1] + 2*ARRAY[(INDEX)] + ARRAY[(INDEX)+1] + 2) >> 2)
  326. static void intra_pred_lp(uint8_t *d,uint8_t *top,uint8_t *left,int stride) {
  327. int x,y;
  328. for(y=0; y<8; y++)
  329. for(x=0; x<8; x++)
  330. d[y*stride+x] = (LOWPASS(top,x+1) + LOWPASS(left,y+1)) >> 1;
  331. }
  332. static void intra_pred_down_left(uint8_t *d,uint8_t *top,uint8_t *left,int stride) {
  333. int x,y;
  334. for(y=0; y<8; y++)
  335. for(x=0; x<8; x++)
  336. d[y*stride+x] = (LOWPASS(top,x+y+2) + LOWPASS(left,x+y+2)) >> 1;
  337. }
  338. static void intra_pred_down_right(uint8_t *d,uint8_t *top,uint8_t *left,int stride) {
  339. int x,y;
  340. for(y=0; y<8; y++)
  341. for(x=0; x<8; x++)
  342. if(x==y)
  343. d[y*stride+x] = (left[1]+2*top[0]+top[1]+2)>>2;
  344. else if(x>y)
  345. d[y*stride+x] = LOWPASS(top,x-y);
  346. else
  347. d[y*stride+x] = LOWPASS(left,y-x);
  348. }
  349. static void intra_pred_lp_left(uint8_t *d,uint8_t *top,uint8_t *left,int stride) {
  350. int x,y;
  351. for(y=0; y<8; y++)
  352. for(x=0; x<8; x++)
  353. d[y*stride+x] = LOWPASS(left,y+1);
  354. }
  355. static void intra_pred_lp_top(uint8_t *d,uint8_t *top,uint8_t *left,int stride) {
  356. int x,y;
  357. for(y=0; y<8; y++)
  358. for(x=0; x<8; x++)
  359. d[y*stride+x] = LOWPASS(top,x+1);
  360. }
  361. #undef LOWPASS
  362. static inline void modify_pred(const int8_t *mod_table, int *mode) {
  363. int newmode = mod_table[*mode];
  364. if(newmode < 0) {
  365. av_log(NULL, AV_LOG_ERROR, "Illegal intra prediction mode\n");
  366. *mode = 0;
  367. } else {
  368. *mode = newmode;
  369. }
  370. }
  371. /*****************************************************************************
  372. *
  373. * motion compensation
  374. *
  375. ****************************************************************************/
  376. static inline void mc_dir_part(AVSContext *h,Picture *pic,int square,
  377. int chroma_height,int delta,int list,uint8_t *dest_y,
  378. uint8_t *dest_cb,uint8_t *dest_cr,int src_x_offset,
  379. int src_y_offset,qpel_mc_func *qpix_op,
  380. h264_chroma_mc_func chroma_op,vector_t *mv){
  381. MpegEncContext * const s = &h->s;
  382. const int mx= mv->x + src_x_offset*8;
  383. const int my= mv->y + src_y_offset*8;
  384. const int luma_xy= (mx&3) + ((my&3)<<2);
  385. uint8_t * src_y = pic->data[0] + (mx>>2) + (my>>2)*h->l_stride;
  386. uint8_t * src_cb= pic->data[1] + (mx>>3) + (my>>3)*h->c_stride;
  387. uint8_t * src_cr= pic->data[2] + (mx>>3) + (my>>3)*h->c_stride;
  388. int extra_width= 0; //(s->flags&CODEC_FLAG_EMU_EDGE) ? 0 : 16;
  389. int extra_height= extra_width;
  390. int emu=0;
  391. const int full_mx= mx>>2;
  392. const int full_my= my>>2;
  393. const int pic_width = 16*h->mb_width;
  394. const int pic_height = 16*h->mb_height;
  395. if(!pic->data[0])
  396. return;
  397. if(mx&7) extra_width -= 3;
  398. if(my&7) extra_height -= 3;
  399. if( full_mx < 0-extra_width
  400. || full_my < 0-extra_height
  401. || full_mx + 16/*FIXME*/ > pic_width + extra_width
  402. || full_my + 16/*FIXME*/ > pic_height + extra_height){
  403. ff_emulated_edge_mc(s->edge_emu_buffer, src_y - 2 - 2*h->l_stride, h->l_stride,
  404. 16+5, 16+5/*FIXME*/, full_mx-2, full_my-2, pic_width, pic_height);
  405. src_y= s->edge_emu_buffer + 2 + 2*h->l_stride;
  406. emu=1;
  407. }
  408. qpix_op[luma_xy](dest_y, src_y, h->l_stride); //FIXME try variable height perhaps?
  409. if(!square){
  410. qpix_op[luma_xy](dest_y + delta, src_y + delta, h->l_stride);
  411. }
  412. if(emu){
  413. ff_emulated_edge_mc(s->edge_emu_buffer, src_cb, h->c_stride,
  414. 9, 9/*FIXME*/, (mx>>3), (my>>3), pic_width>>1, pic_height>>1);
  415. src_cb= s->edge_emu_buffer;
  416. }
  417. chroma_op(dest_cb, src_cb, h->c_stride, chroma_height, mx&7, my&7);
  418. if(emu){
  419. ff_emulated_edge_mc(s->edge_emu_buffer, src_cr, h->c_stride,
  420. 9, 9/*FIXME*/, (mx>>3), (my>>3), pic_width>>1, pic_height>>1);
  421. src_cr= s->edge_emu_buffer;
  422. }
  423. chroma_op(dest_cr, src_cr, h->c_stride, chroma_height, mx&7, my&7);
  424. }
  425. static inline void mc_part_std(AVSContext *h,int square,int chroma_height,int delta,
  426. uint8_t *dest_y,uint8_t *dest_cb,uint8_t *dest_cr,
  427. int x_offset, int y_offset,qpel_mc_func *qpix_put,
  428. h264_chroma_mc_func chroma_put,qpel_mc_func *qpix_avg,
  429. h264_chroma_mc_func chroma_avg, vector_t *mv){
  430. qpel_mc_func *qpix_op= qpix_put;
  431. h264_chroma_mc_func chroma_op= chroma_put;
  432. dest_y += 2*x_offset + 2*y_offset*h->l_stride;
  433. dest_cb += x_offset + y_offset*h->c_stride;
  434. dest_cr += x_offset + y_offset*h->c_stride;
  435. x_offset += 8*h->mbx;
  436. y_offset += 8*h->mby;
  437. if(mv->ref >= 0){
  438. Picture *ref= &h->DPB[mv->ref];
  439. mc_dir_part(h, ref, square, chroma_height, delta, 0,
  440. dest_y, dest_cb, dest_cr, x_offset, y_offset,
  441. qpix_op, chroma_op, mv);
  442. qpix_op= qpix_avg;
  443. chroma_op= chroma_avg;
  444. }
  445. if((mv+MV_BWD_OFFS)->ref >= 0){
  446. Picture *ref= &h->DPB[0];
  447. mc_dir_part(h, ref, square, chroma_height, delta, 1,
  448. dest_y, dest_cb, dest_cr, x_offset, y_offset,
  449. qpix_op, chroma_op, mv+MV_BWD_OFFS);
  450. }
  451. }
  452. static void inter_pred(AVSContext *h) {
  453. /* always do 8x8 blocks TODO: are larger blocks worth it? */
  454. mc_part_std(h, 1, 4, 0, h->cy, h->cu, h->cv, 0, 0,
  455. h->s.dsp.put_cavs_qpel_pixels_tab[1],
  456. h->s.dsp.put_h264_chroma_pixels_tab[1],
  457. h->s.dsp.avg_cavs_qpel_pixels_tab[1],
  458. h->s.dsp.avg_h264_chroma_pixels_tab[1],&h->mv[MV_FWD_X0]);
  459. mc_part_std(h, 1, 4, 0, h->cy, h->cu, h->cv, 4, 0,
  460. h->s.dsp.put_cavs_qpel_pixels_tab[1],
  461. h->s.dsp.put_h264_chroma_pixels_tab[1],
  462. h->s.dsp.avg_cavs_qpel_pixels_tab[1],
  463. h->s.dsp.avg_h264_chroma_pixels_tab[1],&h->mv[MV_FWD_X1]);
  464. mc_part_std(h, 1, 4, 0, h->cy, h->cu, h->cv, 0, 4,
  465. h->s.dsp.put_cavs_qpel_pixels_tab[1],
  466. h->s.dsp.put_h264_chroma_pixels_tab[1],
  467. h->s.dsp.avg_cavs_qpel_pixels_tab[1],
  468. h->s.dsp.avg_h264_chroma_pixels_tab[1],&h->mv[MV_FWD_X2]);
  469. mc_part_std(h, 1, 4, 0, h->cy, h->cu, h->cv, 4, 4,
  470. h->s.dsp.put_cavs_qpel_pixels_tab[1],
  471. h->s.dsp.put_h264_chroma_pixels_tab[1],
  472. h->s.dsp.avg_cavs_qpel_pixels_tab[1],
  473. h->s.dsp.avg_h264_chroma_pixels_tab[1],&h->mv[MV_FWD_X3]);
  474. /* set intra prediction modes to default values */
  475. h->pred_mode_Y[3] = h->pred_mode_Y[6] = INTRA_L_LP;
  476. h->top_pred_Y[h->mbx*2+0] = h->top_pred_Y[h->mbx*2+1] = INTRA_L_LP;
  477. }
  478. /*****************************************************************************
  479. *
  480. * motion vector prediction
  481. *
  482. ****************************************************************************/
  483. static inline void set_mvs(vector_t *mv, enum block_t size) {
  484. switch(size) {
  485. case BLK_16X16:
  486. mv[MV_STRIDE ] = mv[0];
  487. mv[MV_STRIDE+1] = mv[0];
  488. case BLK_16X8:
  489. mv[1] = mv[0];
  490. break;
  491. case BLK_8X16:
  492. mv[MV_STRIDE] = mv[0];
  493. break;
  494. }
  495. }
  496. static inline void store_mvs(AVSContext *h) {
  497. h->col_mv[(h->mby*h->mb_width + h->mbx)*4 + 0] = h->mv[MV_FWD_X0];
  498. h->col_mv[(h->mby*h->mb_width + h->mbx)*4 + 1] = h->mv[MV_FWD_X1];
  499. h->col_mv[(h->mby*h->mb_width + h->mbx)*4 + 2] = h->mv[MV_FWD_X2];
  500. h->col_mv[(h->mby*h->mb_width + h->mbx)*4 + 3] = h->mv[MV_FWD_X3];
  501. }
  502. static inline void scale_mv(AVSContext *h, int *d_x, int *d_y, vector_t *src, int distp) {
  503. int den = h->scale_den[src->ref];
  504. *d_x = (src->x*distp*den + 256 + (src->x>>31)) >> 9;
  505. *d_y = (src->y*distp*den + 256 + (src->y>>31)) >> 9;
  506. }
  507. static inline void mv_pred_median(AVSContext *h, vector_t *mvP, vector_t *mvA, vector_t *mvB, vector_t *mvC) {
  508. int ax, ay, bx, by, cx, cy;
  509. int len_ab, len_bc, len_ca, len_mid;
  510. /* scale candidates according to their temporal span */
  511. scale_mv(h, &ax, &ay, mvA, mvP->dist);
  512. scale_mv(h, &bx, &by, mvB, mvP->dist);
  513. scale_mv(h, &cx, &cy, mvC, mvP->dist);
  514. /* find the geometrical median of the three candidates */
  515. len_ab = abs(ax - bx) + abs(ay - by);
  516. len_bc = abs(bx - cx) + abs(by - cy);
  517. len_ca = abs(cx - ax) + abs(cy - ay);
  518. len_mid = mid_pred(len_ab, len_bc, len_ca);
  519. if(len_mid == len_ab) {
  520. mvP->x = cx;
  521. mvP->y = cy;
  522. } else if(len_mid == len_bc) {
  523. mvP->x = ax;
  524. mvP->y = ay;
  525. } else {
  526. mvP->x = bx;
  527. mvP->y = by;
  528. }
  529. }
  530. static inline void mv_pred_direct(AVSContext *h, vector_t *pmv_fw,
  531. vector_t *pmv_bw, vector_t *col_mv) {
  532. int den = h->direct_den[col_mv->ref];
  533. int m = col_mv->x >> 31;
  534. pmv_fw->dist = h->dist[1];
  535. pmv_bw->dist = h->dist[0];
  536. pmv_fw->ref = 1;
  537. pmv_bw->ref = 0;
  538. /* scale the co-located motion vector according to its temporal span */
  539. pmv_fw->x = (((den+(den*col_mv->x*pmv_fw->dist^m)-m-1)>>14)^m)-m;
  540. pmv_bw->x = m-(((den+(den*col_mv->x*pmv_bw->dist^m)-m-1)>>14)^m);
  541. m = col_mv->y >> 31;
  542. pmv_fw->y = (((den+(den*col_mv->y*pmv_fw->dist^m)-m-1)>>14)^m)-m;
  543. pmv_bw->y = m-(((den+(den*col_mv->y*pmv_bw->dist^m)-m-1)>>14)^m);
  544. }
  545. static inline void mv_pred_sym(AVSContext *h, vector_t *src, enum block_t size) {
  546. vector_t *dst = src + MV_BWD_OFFS;
  547. /* backward mv is the scaled and negated forward mv */
  548. dst->x = -((src->x * h->sym_factor + 256) >> 9);
  549. dst->y = -((src->y * h->sym_factor + 256) >> 9);
  550. dst->ref = 0;
  551. dst->dist = h->dist[0];
  552. set_mvs(dst, size);
  553. }
  554. static void mv_pred(AVSContext *h, enum mv_loc_t nP, enum mv_loc_t nC,
  555. enum mv_pred_t mode, enum block_t size, int ref) {
  556. vector_t *mvP = &h->mv[nP];
  557. vector_t *mvA = &h->mv[nP-1];
  558. vector_t *mvB = &h->mv[nP-4];
  559. vector_t *mvC = &h->mv[nC];
  560. int mvAref = mvA->ref;
  561. int mvBref = mvB->ref;
  562. int mvCref;
  563. mvP->ref = ref;
  564. mvP->dist = h->dist[mvP->ref];
  565. if(mvC->ref == NOT_AVAIL)
  566. mvC = &h->mv[nP-5]; // set to top-left (mvD)
  567. mvCref = mvC->ref;
  568. if(mode == MV_PRED_PSKIP) {
  569. if((mvAref == NOT_AVAIL) || (mvBref == NOT_AVAIL) ||
  570. ((mvA->x | mvA->y | mvA->ref) == 0) ||
  571. ((mvB->x | mvB->y | mvB->ref) == 0) ) {
  572. mvP->x = mvP->y = 0;
  573. set_mvs(mvP,size);
  574. return;
  575. }
  576. }
  577. /* if there is only one suitable candidate, take it */
  578. if((mvAref >= 0) && (mvBref < 0) && (mvCref < 0)) {
  579. mvP->x = mvA->x;
  580. mvP->y = mvA->y;
  581. } else if((mvAref < 0) && (mvBref >= 0) && (mvCref < 0)) {
  582. mvP->x = mvB->x;
  583. mvP->y = mvB->y;
  584. } else if((mvAref < 0) && (mvBref < 0) && (mvCref >= 0)) {
  585. mvP->x = mvC->x;
  586. mvP->y = mvC->y;
  587. } else {
  588. switch(mode) {
  589. case MV_PRED_LEFT:
  590. if(mvAref == mvP->ref) {
  591. mvP->x = mvA->x;
  592. mvP->y = mvA->y;
  593. } else
  594. mv_pred_median(h, mvP, mvA, mvB, mvC);
  595. break;
  596. case MV_PRED_TOP:
  597. if(mvBref == mvP->ref) {
  598. mvP->x = mvB->x;
  599. mvP->y = mvB->y;
  600. } else
  601. mv_pred_median(h, mvP, mvA, mvB, mvC);
  602. break;
  603. case MV_PRED_TOPRIGHT:
  604. if(mvCref == mvP->ref) {
  605. mvP->x = mvC->x;
  606. mvP->y = mvC->y;
  607. } else
  608. mv_pred_median(h, mvP, mvA, mvB, mvC);
  609. break;
  610. default:
  611. mv_pred_median(h, mvP, mvA, mvB, mvC);
  612. break;
  613. }
  614. }
  615. if(mode < MV_PRED_PSKIP) {
  616. mvP->x += get_se_golomb(&h->s.gb);
  617. mvP->y += get_se_golomb(&h->s.gb);
  618. }
  619. set_mvs(mvP,size);
  620. }
  621. /*****************************************************************************
  622. *
  623. * residual data decoding
  624. *
  625. ****************************************************************************/
  626. /* kth-order exponential golomb code */
  627. static inline int get_ue_code(GetBitContext *gb, int order) {
  628. if(order)
  629. return (get_ue_golomb(gb) << order) + get_bits(gb,order);
  630. return get_ue_golomb(gb);
  631. }
  632. static int decode_residual_block(AVSContext *h, GetBitContext *gb,
  633. const residual_vlc_t *r, int esc_golomb_order,
  634. int qp, uint8_t *dst, int stride) {
  635. int i,pos = -1;
  636. int level_code, esc_code, level, run, mask;
  637. int level_buf[64];
  638. int run_buf[64];
  639. int dqm = dequant_mul[qp];
  640. int dqs = dequant_shift[qp];
  641. int dqa = 1 << (dqs - 1);
  642. const uint8_t *scantab = ff_zigzag_direct;
  643. DCTELEM block[64];
  644. memset(block,0,64*sizeof(DCTELEM));
  645. for(i=0;i<65;i++) {
  646. level_code = get_ue_code(gb,r->golomb_order);
  647. if(level_code >= ESCAPE_CODE) {
  648. run = (level_code - ESCAPE_CODE) >> 1;
  649. esc_code = get_ue_code(gb,esc_golomb_order);
  650. level = esc_code + (run > r->max_run ? 1 : r->level_add[run]);
  651. while(level > r->inc_limit)
  652. r++;
  653. mask = -(level_code & 1);
  654. level = (level^mask) - mask;
  655. } else {
  656. if(level_code < 0)
  657. return -1;
  658. level = r->rltab[level_code][0];
  659. if(!level) //end of block signal
  660. break;
  661. run = r->rltab[level_code][1];
  662. r += r->rltab[level_code][2];
  663. }
  664. level_buf[i] = level;
  665. run_buf[i] = run;
  666. }
  667. /* inverse scan and dequantization */
  668. for(i=i-1;i>=0;i--) {
  669. pos += 1 + run_buf[i];
  670. if(pos > 63) {
  671. av_log(h->s.avctx, AV_LOG_ERROR,
  672. "position out of block bounds at pic %d MB(%d,%d)\n",
  673. h->picture.poc, h->mbx, h->mby);
  674. return -1;
  675. }
  676. block[scantab[pos]] = (level_buf[i]*dqm + dqa) >> dqs;
  677. }
  678. h->s.dsp.cavs_idct8_add(dst,block,stride);
  679. return 0;
  680. }
  681. static inline void decode_residual_chroma(AVSContext *h) {
  682. if(h->cbp & (1<<4))
  683. decode_residual_block(h,&h->s.gb,chroma_2dvlc,0, chroma_qp[h->qp],
  684. h->cu,h->c_stride);
  685. if(h->cbp & (1<<5))
  686. decode_residual_block(h,&h->s.gb,chroma_2dvlc,0, chroma_qp[h->qp],
  687. h->cv,h->c_stride);
  688. }
  689. static inline void decode_residual_inter(AVSContext *h) {
  690. int block;
  691. /* get coded block pattern */
  692. h->cbp = cbp_tab[get_ue_golomb(&h->s.gb)][1];
  693. /* get quantizer */
  694. if(h->cbp && !h->qp_fixed)
  695. h->qp += get_se_golomb(&h->s.gb);
  696. for(block=0;block<4;block++)
  697. if(h->cbp & (1<<block))
  698. decode_residual_block(h,&h->s.gb,inter_2dvlc,0,h->qp,
  699. h->cy + h->luma_scan[block], h->l_stride);
  700. decode_residual_chroma(h);
  701. }
  702. /*****************************************************************************
  703. *
  704. * macroblock level
  705. *
  706. ****************************************************************************/
  707. static inline void init_mb(AVSContext *h) {
  708. int i;
  709. /* copy predictors from top line (MB B and C) into cache */
  710. for(i=0;i<3;i++) {
  711. h->mv[MV_FWD_B2+i] = h->top_mv[0][h->mbx*2+i];
  712. h->mv[MV_BWD_B2+i] = h->top_mv[1][h->mbx*2+i];
  713. }
  714. h->pred_mode_Y[1] = h->top_pred_Y[h->mbx*2+0];
  715. h->pred_mode_Y[2] = h->top_pred_Y[h->mbx*2+1];
  716. /* clear top predictors if MB B is not available */
  717. if(!(h->flags & B_AVAIL)) {
  718. h->mv[MV_FWD_B2] = un_mv;
  719. h->mv[MV_FWD_B3] = un_mv;
  720. h->mv[MV_BWD_B2] = un_mv;
  721. h->mv[MV_BWD_B3] = un_mv;
  722. h->pred_mode_Y[1] = h->pred_mode_Y[2] = NOT_AVAIL;
  723. h->flags &= ~(C_AVAIL|D_AVAIL);
  724. } else if(h->mbx) {
  725. h->flags |= D_AVAIL;
  726. }
  727. if(h->mbx == h->mb_width-1) //MB C not available
  728. h->flags &= ~C_AVAIL;
  729. /* clear top-right predictors if MB C is not available */
  730. if(!(h->flags & C_AVAIL)) {
  731. h->mv[MV_FWD_C2] = un_mv;
  732. h->mv[MV_BWD_C2] = un_mv;
  733. }
  734. /* clear top-left predictors if MB D is not available */
  735. if(!(h->flags & D_AVAIL)) {
  736. h->mv[MV_FWD_D3] = un_mv;
  737. h->mv[MV_BWD_D3] = un_mv;
  738. }
  739. /* set pointer for co-located macroblock type */
  740. h->col_type = &h->col_type_base[h->mby*h->mb_width + h->mbx];
  741. }
  742. static inline void check_for_slice(AVSContext *h);
  743. static inline int next_mb(AVSContext *h) {
  744. int i;
  745. h->flags |= A_AVAIL;
  746. h->cy += 16;
  747. h->cu += 8;
  748. h->cv += 8;
  749. /* copy mvs as predictors to the left */
  750. for(i=0;i<=20;i+=4)
  751. h->mv[i] = h->mv[i+2];
  752. /* copy bottom mvs from cache to top line */
  753. h->top_mv[0][h->mbx*2+0] = h->mv[MV_FWD_X2];
  754. h->top_mv[0][h->mbx*2+1] = h->mv[MV_FWD_X3];
  755. h->top_mv[1][h->mbx*2+0] = h->mv[MV_BWD_X2];
  756. h->top_mv[1][h->mbx*2+1] = h->mv[MV_BWD_X3];
  757. /* next MB address */
  758. h->mbx++;
  759. if(h->mbx == h->mb_width) { //new mb line
  760. h->flags = B_AVAIL|C_AVAIL;
  761. /* clear left pred_modes */
  762. h->pred_mode_Y[3] = h->pred_mode_Y[6] = NOT_AVAIL;
  763. /* clear left mv predictors */
  764. for(i=0;i<=20;i+=4)
  765. h->mv[i] = un_mv;
  766. h->mbx = 0;
  767. h->mby++;
  768. /* re-calculate sample pointers */
  769. h->cy = h->picture.data[0] + h->mby*16*h->l_stride;
  770. h->cu = h->picture.data[1] + h->mby*8*h->c_stride;
  771. h->cv = h->picture.data[2] + h->mby*8*h->c_stride;
  772. if(h->mby == h->mb_height) { //frame end
  773. return 0;
  774. } else {
  775. //check_for_slice(h);
  776. }
  777. }
  778. return 1;
  779. }
  780. static void decode_mb_i(AVSContext *h, int is_i_pic) {
  781. GetBitContext *gb = &h->s.gb;
  782. int block, pred_mode_uv;
  783. uint8_t top[18];
  784. uint8_t left[18];
  785. uint8_t *d;
  786. /* get intra prediction modes from stream */
  787. for(block=0;block<4;block++) {
  788. int nA,nB,predpred;
  789. int pos = scan3x3[block];
  790. nA = h->pred_mode_Y[pos-1];
  791. nB = h->pred_mode_Y[pos-3];
  792. if((nA == NOT_AVAIL) || (nB == NOT_AVAIL))
  793. predpred = 2;
  794. else
  795. predpred = FFMIN(nA,nB);
  796. if(get_bits1(gb))
  797. h->pred_mode_Y[pos] = predpred;
  798. else {
  799. h->pred_mode_Y[pos] = get_bits(gb,2);
  800. if(h->pred_mode_Y[pos] >= predpred)
  801. h->pred_mode_Y[pos]++;
  802. }
  803. }
  804. pred_mode_uv = get_ue_golomb(gb);
  805. if(pred_mode_uv > 6) {
  806. av_log(h->s.avctx, AV_LOG_ERROR, "illegal intra chroma pred mode\n");
  807. pred_mode_uv = 0;
  808. }
  809. /* save pred modes before they get modified */
  810. h->pred_mode_Y[3] = h->pred_mode_Y[5];
  811. h->pred_mode_Y[6] = h->pred_mode_Y[8];
  812. h->top_pred_Y[h->mbx*2+0] = h->pred_mode_Y[7];
  813. h->top_pred_Y[h->mbx*2+1] = h->pred_mode_Y[8];
  814. /* modify pred modes according to availability of neighbour samples */
  815. if(!(h->flags & A_AVAIL)) {
  816. modify_pred(left_modifier_l, &h->pred_mode_Y[4] );
  817. modify_pred(left_modifier_l, &h->pred_mode_Y[7] );
  818. modify_pred(left_modifier_c, &pred_mode_uv );
  819. }
  820. if(!(h->flags & B_AVAIL)) {
  821. modify_pred(top_modifier_l, &h->pred_mode_Y[4] );
  822. modify_pred(top_modifier_l, &h->pred_mode_Y[5] );
  823. modify_pred(top_modifier_c, &pred_mode_uv );
  824. }
  825. /* get coded block pattern */
  826. if(is_i_pic)
  827. h->cbp = cbp_tab[get_ue_golomb(gb)][0];
  828. if(h->cbp && !h->qp_fixed)
  829. h->qp += get_se_golomb(gb); //qp_delta
  830. /* luma intra prediction interleaved with residual decode/transform/add */
  831. for(block=0;block<4;block++) {
  832. d = h->cy + h->luma_scan[block];
  833. load_intra_pred_luma(h, top, left, block);
  834. h->intra_pred_l[h->pred_mode_Y[scan3x3[block]]]
  835. (d, top, left, h->l_stride);
  836. if(h->cbp & (1<<block))
  837. decode_residual_block(h,gb,intra_2dvlc,1,h->qp,d,h->l_stride);
  838. }
  839. /* chroma intra prediction */
  840. load_intra_pred_chroma(&h->top_border_u[h->mbx*8], h->left_border_u,
  841. h->topleft_border_u, top, left, h->c_stride, h->flags);
  842. h->intra_pred_c[pred_mode_uv](h->cu, top, left, h->c_stride);
  843. load_intra_pred_chroma(&h->top_border_v[h->mbx*8], h->left_border_v,
  844. h->topleft_border_v, top, left, h->c_stride, h->flags);
  845. h->intra_pred_c[pred_mode_uv](h->cv, top, left, h->c_stride);
  846. decode_residual_chroma(h);
  847. filter_mb(h,I_8X8);
  848. /* mark motion vectors as intra */
  849. h->mv[MV_FWD_X0] = intra_mv;
  850. set_mvs(&h->mv[MV_FWD_X0], BLK_16X16);
  851. h->mv[MV_BWD_X0] = intra_mv;
  852. set_mvs(&h->mv[MV_BWD_X0], BLK_16X16);
  853. if(h->pic_type != FF_B_TYPE)
  854. *h->col_type = I_8X8;
  855. }
  856. static void mb_skip_p(AVSContext *h) {
  857. mv_pred(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_PSKIP, BLK_16X16, 0);
  858. inter_pred(h);
  859. store_mvs(h);
  860. filter_mb(h,P_SKIP);
  861. *h->col_type = P_SKIP;
  862. }
  863. static void mb_skip_b(AVSContext *h) {
  864. int i;
  865. if(!(*h->col_type)) {
  866. /* intra MB at co-location, do in-plane prediction */
  867. mv_pred(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_BSKIP, BLK_16X16, 1);
  868. mv_pred(h, MV_BWD_X0, MV_BWD_C2, MV_PRED_BSKIP, BLK_16X16, 0);
  869. } else {
  870. /* direct prediction from co-located P MB, block-wise */
  871. for(i=0;i<4;i++)
  872. mv_pred_direct(h,&h->mv[mv_scan[i]],
  873. &h->mv[mv_scan[i]+MV_BWD_OFFS],
  874. &h->col_mv[(h->mby*h->mb_width + h->mbx)*4 + i]);
  875. }
  876. }
  877. static void decode_mb_p(AVSContext *h, enum mb_t mb_type) {
  878. GetBitContext *gb = &h->s.gb;
  879. int ref[4];
  880. switch(mb_type) {
  881. case P_SKIP:
  882. mb_skip_p(h);
  883. return;
  884. case P_16X16:
  885. ref[0] = h->ref_flag ? 0 : get_bits1(gb);
  886. mv_pred(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_MEDIAN, BLK_16X16,ref[0]);
  887. break;
  888. case P_16X8:
  889. ref[0] = h->ref_flag ? 0 : get_bits1(gb);
  890. ref[2] = h->ref_flag ? 0 : get_bits1(gb);
  891. mv_pred(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_TOP, BLK_16X8, ref[0]);
  892. mv_pred(h, MV_FWD_X2, MV_FWD_A1, MV_PRED_LEFT, BLK_16X8, ref[2]);
  893. break;
  894. case P_8X16:
  895. ref[0] = h->ref_flag ? 0 : get_bits1(gb);
  896. ref[1] = h->ref_flag ? 0 : get_bits1(gb);
  897. mv_pred(h, MV_FWD_X0, MV_FWD_B3, MV_PRED_LEFT, BLK_8X16, ref[0]);
  898. mv_pred(h, MV_FWD_X1, MV_FWD_C2, MV_PRED_TOPRIGHT, BLK_8X16, ref[1]);
  899. break;
  900. case P_8X8:
  901. ref[0] = h->ref_flag ? 0 : get_bits1(gb);
  902. ref[1] = h->ref_flag ? 0 : get_bits1(gb);
  903. ref[2] = h->ref_flag ? 0 : get_bits1(gb);
  904. ref[3] = h->ref_flag ? 0 : get_bits1(gb);
  905. mv_pred(h, MV_FWD_X0, MV_FWD_B3, MV_PRED_MEDIAN, BLK_8X8, ref[0]);
  906. mv_pred(h, MV_FWD_X1, MV_FWD_C2, MV_PRED_MEDIAN, BLK_8X8, ref[1]);
  907. mv_pred(h, MV_FWD_X2, MV_FWD_X1, MV_PRED_MEDIAN, BLK_8X8, ref[2]);
  908. mv_pred(h, MV_FWD_X3, MV_FWD_X0, MV_PRED_MEDIAN, BLK_8X8, ref[3]);
  909. }
  910. inter_pred(h);
  911. store_mvs(h);
  912. decode_residual_inter(h);
  913. filter_mb(h,mb_type);
  914. *h->col_type = mb_type;
  915. }
  916. static void decode_mb_b(AVSContext *h, enum mb_t mb_type) {
  917. int block;
  918. enum sub_mb_t sub_type[4];
  919. int flags;
  920. /* reset all MVs */
  921. h->mv[MV_FWD_X0] = dir_mv;
  922. set_mvs(&h->mv[MV_FWD_X0], BLK_16X16);
  923. h->mv[MV_BWD_X0] = dir_mv;
  924. set_mvs(&h->mv[MV_BWD_X0], BLK_16X16);
  925. switch(mb_type) {
  926. case B_SKIP:
  927. mb_skip_b(h);
  928. inter_pred(h);
  929. filter_mb(h,B_SKIP);
  930. return;
  931. case B_DIRECT:
  932. mb_skip_b(h);
  933. break;
  934. case B_FWD_16X16:
  935. mv_pred(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_MEDIAN, BLK_16X16, 1);
  936. break;
  937. case B_SYM_16X16:
  938. mv_pred(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_MEDIAN, BLK_16X16, 1);
  939. mv_pred_sym(h, &h->mv[MV_FWD_X0], BLK_16X16);
  940. break;
  941. case B_BWD_16X16:
  942. mv_pred(h, MV_BWD_X0, MV_BWD_C2, MV_PRED_MEDIAN, BLK_16X16, 0);
  943. break;
  944. case B_8X8:
  945. for(block=0;block<4;block++)
  946. sub_type[block] = get_bits(&h->s.gb,2);
  947. for(block=0;block<4;block++) {
  948. switch(sub_type[block]) {
  949. case B_SUB_DIRECT:
  950. if(!(*h->col_type)) {
  951. /* intra MB at co-location, do in-plane prediction */
  952. mv_pred(h, mv_scan[block], mv_scan[block]-3,
  953. MV_PRED_BSKIP, BLK_8X8, 1);
  954. mv_pred(h, mv_scan[block]+MV_BWD_OFFS,
  955. mv_scan[block]-3+MV_BWD_OFFS,
  956. MV_PRED_BSKIP, BLK_8X8, 0);
  957. } else
  958. mv_pred_direct(h,&h->mv[mv_scan[block]],
  959. &h->mv[mv_scan[block]+MV_BWD_OFFS],
  960. &h->col_mv[(h->mby*h->mb_width + h->mbx)*4 + block]);
  961. break;
  962. case B_SUB_FWD:
  963. mv_pred(h, mv_scan[block], mv_scan[block]-3,
  964. MV_PRED_MEDIAN, BLK_8X8, 1);
  965. break;
  966. case B_SUB_SYM:
  967. mv_pred(h, mv_scan[block], mv_scan[block]-3,
  968. MV_PRED_MEDIAN, BLK_8X8, 1);
  969. mv_pred_sym(h, &h->mv[mv_scan[block]], BLK_8X8);
  970. break;
  971. }
  972. }
  973. for(block=0;block<4;block++) {
  974. if(sub_type[block] == B_SUB_BWD)
  975. mv_pred(h, mv_scan[block]+MV_BWD_OFFS,
  976. mv_scan[block]+MV_BWD_OFFS-3,
  977. MV_PRED_MEDIAN, BLK_8X8, 0);
  978. }
  979. break;
  980. default:
  981. assert((mb_type > B_SYM_16X16) && (mb_type < B_8X8));
  982. flags = b_partition_flags[(mb_type-1)>>1];
  983. if(mb_type & 1) { /* 16x8 macroblock types */
  984. if(flags & FWD0)
  985. mv_pred(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_TOP, BLK_16X8, 1);
  986. if(flags & SYM0) {
  987. mv_pred(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_TOP, BLK_16X8, 1);
  988. mv_pred_sym(h, &h->mv[MV_FWD_X0], BLK_16X8);
  989. }
  990. if(flags & FWD1)
  991. mv_pred(h, MV_FWD_X2, MV_FWD_A1, MV_PRED_LEFT, BLK_16X8, 1);
  992. if(flags & SYM1) {
  993. mv_pred(h, MV_FWD_X2, MV_FWD_A1, MV_PRED_LEFT, BLK_16X8, 1);
  994. mv_pred_sym(h, &h->mv[9], BLK_16X8);
  995. }
  996. if(flags & BWD0)
  997. mv_pred(h, MV_BWD_X0, MV_BWD_C2, MV_PRED_TOP, BLK_16X8, 0);
  998. if(flags & BWD1)
  999. mv_pred(h, MV_BWD_X2, MV_BWD_A1, MV_PRED_LEFT, BLK_16X8, 0);
  1000. } else { /* 8x16 macroblock types */
  1001. if(flags & FWD0)
  1002. mv_pred(h, MV_FWD_X0, MV_FWD_B3, MV_PRED_LEFT, BLK_8X16, 1);
  1003. if(flags & SYM0) {
  1004. mv_pred(h, MV_FWD_X0, MV_FWD_B3, MV_PRED_LEFT, BLK_8X16, 1);
  1005. mv_pred_sym(h, &h->mv[MV_FWD_X0], BLK_8X16);
  1006. }
  1007. if(flags & FWD1)
  1008. mv_pred(h, MV_FWD_X1, MV_FWD_C2, MV_PRED_TOPRIGHT,BLK_8X16, 1);
  1009. if(flags & SYM1) {
  1010. mv_pred(h, MV_FWD_X1, MV_FWD_C2, MV_PRED_TOPRIGHT,BLK_8X16, 1);
  1011. mv_pred_sym(h, &h->mv[6], BLK_8X16);
  1012. }
  1013. if(flags & BWD0)
  1014. mv_pred(h, MV_BWD_X0, MV_BWD_B3, MV_PRED_LEFT, BLK_8X16, 0);
  1015. if(flags & BWD1)
  1016. mv_pred(h, MV_BWD_X1, MV_BWD_C2, MV_PRED_TOPRIGHT,BLK_8X16, 0);
  1017. }
  1018. }
  1019. inter_pred(h);
  1020. decode_residual_inter(h);
  1021. filter_mb(h,mb_type);
  1022. }
  1023. /*****************************************************************************
  1024. *
  1025. * slice level
  1026. *
  1027. ****************************************************************************/
  1028. static inline int decode_slice_header(AVSContext *h, GetBitContext *gb) {
  1029. if(h->stc > 0xAF)
  1030. av_log(h->s.avctx, AV_LOG_ERROR, "unexpected start code 0x%02x\n", h->stc);
  1031. h->mby = h->stc;
  1032. if((h->mby == 0) && (!h->qp_fixed)){
  1033. h->qp_fixed = get_bits1(gb);
  1034. h->qp = get_bits(gb,6);
  1035. }
  1036. /* inter frame or second slice can have weighting params */
  1037. if((h->pic_type != FF_I_TYPE) || (!h->pic_structure && h->mby >= h->mb_width/2))
  1038. if(get_bits1(gb)) { //slice_weighting_flag
  1039. av_log(h->s.avctx, AV_LOG_ERROR,
  1040. "weighted prediction not yet supported\n");
  1041. }
  1042. return 0;
  1043. }
  1044. static inline void check_for_slice(AVSContext *h) {
  1045. GetBitContext *gb = &h->s.gb;
  1046. int align;
  1047. align = (-get_bits_count(gb)) & 7;
  1048. if((show_bits_long(gb,24+align) & 0xFFFFFF) == 0x000001) {
  1049. get_bits_long(gb,24+align);
  1050. h->stc = get_bits(gb,8);
  1051. decode_slice_header(h,gb);
  1052. }
  1053. }
  1054. /*****************************************************************************
  1055. *
  1056. * frame level
  1057. *
  1058. ****************************************************************************/
  1059. static void init_pic(AVSContext *h) {
  1060. int i;
  1061. /* clear some predictors */
  1062. for(i=0;i<=20;i+=4)
  1063. h->mv[i] = un_mv;
  1064. h->mv[MV_BWD_X0] = dir_mv;
  1065. set_mvs(&h->mv[MV_BWD_X0], BLK_16X16);
  1066. h->mv[MV_FWD_X0] = dir_mv;
  1067. set_mvs(&h->mv[MV_FWD_X0], BLK_16X16);
  1068. h->pred_mode_Y[3] = h->pred_mode_Y[6] = NOT_AVAIL;
  1069. h->cy = h->picture.data[0];
  1070. h->cu = h->picture.data[1];
  1071. h->cv = h->picture.data[2];
  1072. h->l_stride = h->picture.linesize[0];
  1073. h->c_stride = h->picture.linesize[1];
  1074. h->luma_scan[2] = 8*h->l_stride;
  1075. h->luma_scan[3] = 8*h->l_stride+8;
  1076. h->mbx = h->mby = 0;
  1077. h->flags = 0;
  1078. }
  1079. static int decode_pic(AVSContext *h) {
  1080. MpegEncContext *s = &h->s;
  1081. int i,skip_count;
  1082. enum mb_t mb_type;
  1083. if (!s->context_initialized) {
  1084. if (MPV_common_init(s) < 0)
  1085. return -1;
  1086. }
  1087. get_bits(&s->gb,16);//bbv_dwlay
  1088. if(h->stc == PIC_PB_START_CODE) {
  1089. h->pic_type = get_bits(&s->gb,2) + FF_I_TYPE;
  1090. /* make sure we have the reference frames we need */
  1091. if(!h->DPB[0].data[0] ||
  1092. (!h->DPB[1].data[0] && h->pic_type == FF_B_TYPE))
  1093. return -1;
  1094. } else {
  1095. h->pic_type = FF_I_TYPE;
  1096. if(get_bits1(&s->gb))
  1097. get_bits(&s->gb,16);//time_code
  1098. }
  1099. /* release last B frame */
  1100. if(h->picture.data[0])
  1101. s->avctx->release_buffer(s->avctx, (AVFrame *)&h->picture);
  1102. s->avctx->get_buffer(s->avctx, (AVFrame *)&h->picture);
  1103. init_pic(h);
  1104. h->picture.poc = get_bits(&s->gb,8)*2;
  1105. /* get temporal distances and MV scaling factors */
  1106. if(h->pic_type != FF_B_TYPE) {
  1107. h->dist[0] = (h->picture.poc - h->DPB[0].poc + 512) % 512;
  1108. } else {
  1109. h->dist[0] = (h->DPB[0].poc - h->picture.poc + 512) % 512;
  1110. }
  1111. h->dist[1] = (h->picture.poc - h->DPB[1].poc + 512) % 512;
  1112. h->scale_den[0] = h->dist[0] ? 512/h->dist[0] : 0;
  1113. h->scale_den[1] = h->dist[1] ? 512/h->dist[1] : 0;
  1114. if(h->pic_type == FF_B_TYPE) {
  1115. h->sym_factor = h->dist[0]*h->scale_den[1];
  1116. } else {
  1117. h->direct_den[0] = h->dist[0] ? 16384/h->dist[0] : 0;
  1118. h->direct_den[1] = h->dist[1] ? 16384/h->dist[1] : 0;
  1119. }
  1120. if(s->low_delay)
  1121. get_ue_golomb(&s->gb); //bbv_check_times
  1122. h->progressive = get_bits1(&s->gb);
  1123. if(h->progressive)
  1124. h->pic_structure = 1;
  1125. else if(!(h->pic_structure = get_bits1(&s->gb) && (h->stc == PIC_PB_START_CODE)) )
  1126. get_bits1(&s->gb); //advanced_pred_mode_disable
  1127. skip_bits1(&s->gb); //top_field_first
  1128. skip_bits1(&s->gb); //repeat_first_field
  1129. h->qp_fixed = get_bits1(&s->gb);
  1130. h->qp = get_bits(&s->gb,6);
  1131. if(h->pic_type == FF_I_TYPE) {
  1132. if(!h->progressive && !h->pic_structure)
  1133. skip_bits1(&s->gb);//what is this?
  1134. skip_bits(&s->gb,4); //reserved bits
  1135. } else {
  1136. if(!(h->pic_type == FF_B_TYPE && h->pic_structure == 1))
  1137. h->ref_flag = get_bits1(&s->gb);
  1138. skip_bits(&s->gb,4); //reserved bits
  1139. h->skip_mode_flag = get_bits1(&s->gb);
  1140. }
  1141. h->loop_filter_disable = get_bits1(&s->gb);
  1142. if(!h->loop_filter_disable && get_bits1(&s->gb)) {
  1143. h->alpha_offset = get_se_golomb(&s->gb);
  1144. h->beta_offset = get_se_golomb(&s->gb);
  1145. } else {
  1146. h->alpha_offset = h->beta_offset = 0;
  1147. }
  1148. check_for_slice(h);
  1149. if(h->pic_type == FF_I_TYPE) {
  1150. do {
  1151. init_mb(h);
  1152. decode_mb_i(h,1);
  1153. } while(next_mb(h));
  1154. } else if(h->pic_type == FF_P_TYPE) {
  1155. do {
  1156. if(h->skip_mode_flag) {
  1157. skip_count = get_ue_golomb(&s->gb);
  1158. for(i=0;i<skip_count;i++) {
  1159. init_mb(h);
  1160. mb_skip_p(h);
  1161. if(!next_mb(h))
  1162. goto done;
  1163. }
  1164. mb_type = get_ue_golomb(&s->gb) + P_16X16;
  1165. } else {
  1166. mb_type = get_ue_golomb(&s->gb) + P_SKIP;
  1167. }
  1168. init_mb(h);
  1169. if(mb_type > P_8X8) {
  1170. h->cbp = cbp_tab[mb_type - P_8X8 - 1][0];
  1171. decode_mb_i(h,0);
  1172. } else {
  1173. decode_mb_p(h,mb_type);
  1174. }
  1175. } while(next_mb(h));
  1176. } else { //FF_B_TYPE
  1177. do {
  1178. if(h->skip_mode_flag) {
  1179. skip_count = get_ue_golomb(&s->gb);
  1180. for(i=0;i<skip_count;i++) {
  1181. init_mb(h);
  1182. mb_skip_b(h);
  1183. inter_pred(h);
  1184. filter_mb(h,B_SKIP);
  1185. if(!next_mb(h))
  1186. goto done;
  1187. }
  1188. mb_type = get_ue_golomb(&s->gb) + B_DIRECT;
  1189. } else {
  1190. mb_type = get_ue_golomb(&s->gb) + B_SKIP;
  1191. }
  1192. init_mb(h);
  1193. if(mb_type > B_8X8) {
  1194. h->cbp = cbp_tab[mb_type - B_8X8 - 1][0];
  1195. decode_mb_i(h,0);
  1196. } else {
  1197. decode_mb_b(h,mb_type);
  1198. }
  1199. } while(next_mb(h));
  1200. }
  1201. done:
  1202. if(h->pic_type != FF_B_TYPE) {
  1203. if(h->DPB[1].data[0])
  1204. s->avctx->release_buffer(s->avctx, (AVFrame *)&h->DPB[1]);
  1205. memcpy(&h->DPB[1], &h->DPB[0], sizeof(Picture));
  1206. memcpy(&h->DPB[0], &h->picture, sizeof(Picture));
  1207. memset(&h->picture,0,sizeof(Picture));
  1208. }
  1209. return 0;
  1210. }
  1211. /*****************************************************************************
  1212. *
  1213. * headers and interface
  1214. *
  1215. ****************************************************************************/
  1216. static void init_top_lines(AVSContext *h) {
  1217. /* alloc top line of predictors */
  1218. h->top_qp = av_malloc( h->mb_width);
  1219. h->top_mv[0] = av_malloc((h->mb_width*2+1)*sizeof(vector_t));
  1220. h->top_mv[1] = av_malloc((h->mb_width*2+1)*sizeof(vector_t));
  1221. h->top_pred_Y = av_malloc( h->mb_width*2*sizeof(int));
  1222. h->top_border_y = av_malloc((h->mb_width+1)*16);
  1223. h->top_border_u = av_malloc((h->mb_width+1)*8);
  1224. h->top_border_v = av_malloc((h->mb_width+1)*8);
  1225. /* alloc space for co-located MVs and types */
  1226. h->col_mv = av_malloc( h->mb_width*h->mb_height*4*sizeof(vector_t));
  1227. h->col_type_base = av_malloc(h->mb_width*h->mb_height);
  1228. }
  1229. static int decode_seq_header(AVSContext *h) {
  1230. MpegEncContext *s = &h->s;
  1231. extern const AVRational frame_rate_tab[];
  1232. int frame_rate_code;
  1233. h->profile = get_bits(&s->gb,8);
  1234. h->level = get_bits(&s->gb,8);
  1235. skip_bits1(&s->gb); //progressive sequence
  1236. s->width = get_bits(&s->gb,14);
  1237. s->height = get_bits(&s->gb,14);
  1238. skip_bits(&s->gb,2); //chroma format
  1239. skip_bits(&s->gb,3); //sample_precision
  1240. h->aspect_ratio = get_bits(&s->gb,4);
  1241. frame_rate_code = get_bits(&s->gb,4);
  1242. skip_bits(&s->gb,18);//bit_rate_lower
  1243. skip_bits1(&s->gb); //marker_bit
  1244. skip_bits(&s->gb,12);//bit_rate_upper
  1245. s->low_delay = get_bits1(&s->gb);
  1246. h->mb_width = (s->width + 15) >> 4;
  1247. h->mb_height = (s->height + 15) >> 4;
  1248. h->s.avctx->time_base.den = frame_rate_tab[frame_rate_code].num;
  1249. h->s.avctx->time_base.num = frame_rate_tab[frame_rate_code].den;
  1250. h->s.avctx->width = s->width;
  1251. h->s.avctx->height = s->height;
  1252. if(!h->top_qp)
  1253. init_top_lines(h);
  1254. return 0;
  1255. }
  1256. /**
  1257. * finds the end of the current frame in the bitstream.
  1258. * @return the position of the first byte of the next frame, or -1
  1259. */
  1260. int ff_cavs_find_frame_end(ParseContext *pc, const uint8_t *buf, int buf_size) {
  1261. int pic_found, i;
  1262. uint32_t state;
  1263. pic_found= pc->frame_start_found;
  1264. state= pc->state;
  1265. i=0;
  1266. if(!pic_found){
  1267. for(i=0; i<buf_size; i++){
  1268. state= (state<<8) | buf[i];
  1269. if(state == PIC_I_START_CODE || state == PIC_PB_START_CODE){
  1270. i++;
  1271. pic_found=1;
  1272. break;
  1273. }
  1274. }
  1275. }
  1276. if(pic_found){
  1277. /* EOF considered as end of frame */
  1278. if (buf_size == 0)
  1279. return 0;
  1280. for(; i<buf_size; i++){
  1281. state= (state<<8) | buf[i];
  1282. if((state&0xFFFFFF00) == 0x100){
  1283. if(state < SLICE_MIN_START_CODE || state > SLICE_MAX_START_CODE){
  1284. pc->frame_start_found=0;
  1285. pc->state=-1;
  1286. return i-3;
  1287. }
  1288. }
  1289. }
  1290. }
  1291. pc->frame_start_found= pic_found;
  1292. pc->state= state;
  1293. return END_NOT_FOUND;
  1294. }
  1295. void ff_cavs_flush(AVCodecContext * avctx) {
  1296. AVSContext *h = avctx->priv_data;
  1297. h->got_keyframe = 0;
  1298. }
  1299. static int cavs_decode_frame(AVCodecContext * avctx,void *data, int *data_size,
  1300. uint8_t * buf, int buf_size) {
  1301. AVSContext *h = avctx->priv_data;
  1302. MpegEncContext *s = &h->s;
  1303. int input_size;
  1304. const uint8_t *buf_end;
  1305. const uint8_t *buf_ptr;
  1306. AVFrame *picture = data;
  1307. uint32_t stc;
  1308. s->avctx = avctx;
  1309. if (buf_size == 0) {
  1310. if(!s->low_delay && h->DPB[0].data[0]) {
  1311. *data_size = sizeof(AVPicture);
  1312. *picture = *(AVFrame *) &h->DPB[0];
  1313. }
  1314. return 0;
  1315. }
  1316. buf_ptr = buf;
  1317. buf_end = buf + buf_size;
  1318. for(;;) {
  1319. buf_ptr = ff_find_start_code(buf_ptr,buf_end, &stc);
  1320. if(stc & 0xFFFFFE00)
  1321. return FFMAX(0, buf_ptr - buf - s->parse_context.last_index);
  1322. input_size = (buf_end - buf_ptr)*8;
  1323. switch(stc) {
  1324. case SEQ_START_CODE:
  1325. init_get_bits(&s->gb, buf_ptr, input_size);
  1326. decode_seq_header(h);
  1327. break;
  1328. case PIC_I_START_CODE:
  1329. if(!h->got_keyframe) {
  1330. if(h->DPB[0].data[0])
  1331. avctx->release_buffer(avctx, (AVFrame *)&h->DPB[0]);
  1332. if(h->DPB[1].data[0])
  1333. avctx->release_buffer(avctx, (AVFrame *)&h->DPB[1]);
  1334. h->got_keyframe = 1;
  1335. }
  1336. case PIC_PB_START_CODE:
  1337. *data_size = 0;
  1338. if(!h->got_keyframe)
  1339. break;
  1340. init_get_bits(&s->gb, buf_ptr, input_size);
  1341. h->stc = stc;
  1342. if(decode_pic(h))
  1343. break;
  1344. *data_size = sizeof(AVPicture);
  1345. if(h->pic_type != FF_B_TYPE) {
  1346. if(h->DPB[1].data[0]) {
  1347. *picture = *(AVFrame *) &h->DPB[1];
  1348. } else {
  1349. *data_size = 0;
  1350. }
  1351. } else
  1352. *picture = *(AVFrame *) &h->picture;
  1353. break;
  1354. case EXT_START_CODE:
  1355. //mpeg_decode_extension(avctx,buf_ptr, input_size);
  1356. break;
  1357. case USER_START_CODE:
  1358. //mpeg_decode_user_data(avctx,buf_ptr, input_size);
  1359. break;
  1360. default:
  1361. if (stc >= SLICE_MIN_START_CODE &&
  1362. stc <= SLICE_MAX_START_CODE) {
  1363. init_get_bits(&s->gb, buf_ptr, input_size);
  1364. decode_slice_header(h, &s->gb);
  1365. }
  1366. break;
  1367. }
  1368. }
  1369. }
  1370. static int cavs_decode_init(AVCodecContext * avctx) {
  1371. AVSContext *h = avctx->priv_data;
  1372. MpegEncContext * const s = &h->s;
  1373. MPV_decode_defaults(s);
  1374. s->avctx = avctx;
  1375. avctx->pix_fmt= PIX_FMT_YUV420P;
  1376. h->luma_scan[0] = 0;
  1377. h->luma_scan[1] = 8;
  1378. h->intra_pred_l[ INTRA_L_VERT] = intra_pred_vert;
  1379. h->intra_pred_l[ INTRA_L_HORIZ] = intra_pred_horiz;
  1380. h->intra_pred_l[ INTRA_L_LP] = intra_pred_lp;
  1381. h->intra_pred_l[ INTRA_L_DOWN_LEFT] = intra_pred_down_left;
  1382. h->intra_pred_l[INTRA_L_DOWN_RIGHT] = intra_pred_down_right;
  1383. h->intra_pred_l[ INTRA_L_LP_LEFT] = intra_pred_lp_left;
  1384. h->intra_pred_l[ INTRA_L_LP_TOP] = intra_pred_lp_top;
  1385. h->intra_pred_l[ INTRA_L_DC_128] = intra_pred_dc_128;
  1386. h->intra_pred_c[ INTRA_C_LP] = intra_pred_lp;
  1387. h->intra_pred_c[ INTRA_C_HORIZ] = intra_pred_horiz;
  1388. h->intra_pred_c[ INTRA_C_VERT] = intra_pred_vert;
  1389. h->intra_pred_c[ INTRA_C_PLANE] = intra_pred_plane;
  1390. h->intra_pred_c[ INTRA_C_LP_LEFT] = intra_pred_lp_left;
  1391. h->intra_pred_c[ INTRA_C_LP_TOP] = intra_pred_lp_top;
  1392. h->intra_pred_c[ INTRA_C_DC_128] = intra_pred_dc_128;
  1393. h->mv[ 7] = un_mv;
  1394. h->mv[19] = un_mv;
  1395. return 0;
  1396. }
  1397. static int cavs_decode_end(AVCodecContext * avctx) {
  1398. AVSContext *h = avctx->priv_data;
  1399. av_free(h->top_qp);
  1400. av_free(h->top_mv[0]);
  1401. av_free(h->top_mv[1]);
  1402. av_free(h->top_pred_Y);
  1403. av_free(h->top_border_y);
  1404. av_free(h->top_border_u);
  1405. av_free(h->top_border_v);
  1406. av_free(h->col_mv);
  1407. av_free(h->col_type_base);
  1408. return 0;
  1409. }
  1410. AVCodec cavs_decoder = {
  1411. "cavs",
  1412. CODEC_TYPE_VIDEO,
  1413. CODEC_ID_CAVS,
  1414. sizeof(AVSContext),
  1415. cavs_decode_init,
  1416. NULL,
  1417. cavs_decode_end,
  1418. cavs_decode_frame,
  1419. CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY, //FIXME is this correct ?
  1420. .flush= ff_cavs_flush,
  1421. };