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.

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