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.

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