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.

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