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