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.

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