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.

1541 lines
53KB

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