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.

1570 lines
54KB

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