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.

716 lines
25KB

  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 Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * Chinese AVS video (AVS1-P2, JiZhun profile) decoder
  24. * @author Stefan Gehrer <stefan.gehrer@gmx.de>
  25. */
  26. #include "avcodec.h"
  27. #include "get_bits.h"
  28. #include "golomb.h"
  29. #include "mathops.h"
  30. #include "cavs.h"
  31. #include "cavsdata.h"
  32. /*****************************************************************************
  33. *
  34. * in-loop deblocking filter
  35. *
  36. ****************************************************************************/
  37. static inline int get_bs(cavs_vector *mvP, cavs_vector *mvQ, int b) {
  38. if((mvP->ref == REF_INTRA) || (mvQ->ref == REF_INTRA))
  39. return 2;
  40. if( (abs(mvP->x - mvQ->x) >= 4) || (abs(mvP->y - mvQ->y) >= 4) )
  41. return 1;
  42. if(b){
  43. mvP += MV_BWD_OFFS;
  44. mvQ += MV_BWD_OFFS;
  45. if( (abs(mvP->x - mvQ->x) >= 4) || (abs(mvP->y - mvQ->y) >= 4) )
  46. return 1;
  47. }else{
  48. if(mvP->ref != mvQ->ref)
  49. return 1;
  50. }
  51. return 0;
  52. }
  53. #define SET_PARAMS \
  54. alpha = alpha_tab[av_clip(qp_avg + h->alpha_offset,0,63)]; \
  55. beta = beta_tab[av_clip(qp_avg + h->beta_offset, 0,63)]; \
  56. tc = tc_tab[av_clip(qp_avg + h->alpha_offset,0,63)];
  57. /**
  58. * in-loop deblocking filter for a single macroblock
  59. *
  60. * boundary strength (bs) mapping:
  61. *
  62. * --4---5--
  63. * 0 2 |
  64. * | 6 | 7 |
  65. * 1 3 |
  66. * ---------
  67. *
  68. */
  69. void ff_cavs_filter(AVSContext *h, enum cavs_mb mb_type) {
  70. uint8_t bs[8];
  71. int qp_avg, alpha, beta, tc;
  72. int i;
  73. /* save un-deblocked lines */
  74. h->topleft_border_y = h->top_border_y[h->mbx*16+15];
  75. h->topleft_border_u = h->top_border_u[h->mbx*10+8];
  76. h->topleft_border_v = h->top_border_v[h->mbx*10+8];
  77. memcpy(&h->top_border_y[h->mbx*16], h->cy + 15* h->l_stride,16);
  78. memcpy(&h->top_border_u[h->mbx*10+1], h->cu + 7* h->c_stride,8);
  79. memcpy(&h->top_border_v[h->mbx*10+1], h->cv + 7* h->c_stride,8);
  80. for(i=0;i<8;i++) {
  81. h->left_border_y[i*2+1] = *(h->cy + 15 + (i*2+0)*h->l_stride);
  82. h->left_border_y[i*2+2] = *(h->cy + 15 + (i*2+1)*h->l_stride);
  83. h->left_border_u[i+1] = *(h->cu + 7 + i*h->c_stride);
  84. h->left_border_v[i+1] = *(h->cv + 7 + i*h->c_stride);
  85. }
  86. if(!h->loop_filter_disable) {
  87. /* determine bs */
  88. if(mb_type == I_8X8)
  89. memset(bs,2,8);
  90. else{
  91. memset(bs,0,8);
  92. if(ff_cavs_partition_flags[mb_type] & SPLITV){
  93. bs[2] = get_bs(&h->mv[MV_FWD_X0], &h->mv[MV_FWD_X1], mb_type > P_8X8);
  94. bs[3] = get_bs(&h->mv[MV_FWD_X2], &h->mv[MV_FWD_X3], mb_type > P_8X8);
  95. }
  96. if(ff_cavs_partition_flags[mb_type] & SPLITH){
  97. bs[6] = get_bs(&h->mv[MV_FWD_X0], &h->mv[MV_FWD_X2], mb_type > P_8X8);
  98. bs[7] = get_bs(&h->mv[MV_FWD_X1], &h->mv[MV_FWD_X3], mb_type > P_8X8);
  99. }
  100. bs[0] = get_bs(&h->mv[MV_FWD_A1], &h->mv[MV_FWD_X0], mb_type > P_8X8);
  101. bs[1] = get_bs(&h->mv[MV_FWD_A3], &h->mv[MV_FWD_X2], mb_type > P_8X8);
  102. bs[4] = get_bs(&h->mv[MV_FWD_B2], &h->mv[MV_FWD_X0], mb_type > P_8X8);
  103. bs[5] = get_bs(&h->mv[MV_FWD_B3], &h->mv[MV_FWD_X1], mb_type > P_8X8);
  104. }
  105. if(AV_RN64(bs)) {
  106. if(h->flags & A_AVAIL) {
  107. qp_avg = (h->qp + h->left_qp + 1) >> 1;
  108. SET_PARAMS;
  109. h->cdsp.cavs_filter_lv(h->cy,h->l_stride,alpha,beta,tc,bs[0],bs[1]);
  110. h->cdsp.cavs_filter_cv(h->cu,h->c_stride,alpha,beta,tc,bs[0],bs[1]);
  111. h->cdsp.cavs_filter_cv(h->cv,h->c_stride,alpha,beta,tc,bs[0],bs[1]);
  112. }
  113. qp_avg = h->qp;
  114. SET_PARAMS;
  115. h->cdsp.cavs_filter_lv(h->cy + 8,h->l_stride,alpha,beta,tc,bs[2],bs[3]);
  116. h->cdsp.cavs_filter_lh(h->cy + 8*h->l_stride,h->l_stride,alpha,beta,tc,
  117. bs[6],bs[7]);
  118. if(h->flags & B_AVAIL) {
  119. qp_avg = (h->qp + h->top_qp[h->mbx] + 1) >> 1;
  120. SET_PARAMS;
  121. h->cdsp.cavs_filter_lh(h->cy,h->l_stride,alpha,beta,tc,bs[4],bs[5]);
  122. h->cdsp.cavs_filter_ch(h->cu,h->c_stride,alpha,beta,tc,bs[4],bs[5]);
  123. h->cdsp.cavs_filter_ch(h->cv,h->c_stride,alpha,beta,tc,bs[4],bs[5]);
  124. }
  125. }
  126. }
  127. h->left_qp = h->qp;
  128. h->top_qp[h->mbx] = h->qp;
  129. }
  130. #undef SET_PARAMS
  131. /*****************************************************************************
  132. *
  133. * spatial intra prediction
  134. *
  135. ****************************************************************************/
  136. void ff_cavs_load_intra_pred_luma(AVSContext *h, uint8_t *top,
  137. uint8_t **left, int block) {
  138. int i;
  139. switch(block) {
  140. case 0:
  141. *left = h->left_border_y;
  142. h->left_border_y[0] = h->left_border_y[1];
  143. memset(&h->left_border_y[17],h->left_border_y[16],9);
  144. memcpy(&top[1],&h->top_border_y[h->mbx*16],16);
  145. top[17] = top[16];
  146. top[0] = top[1];
  147. if((h->flags & A_AVAIL) && (h->flags & B_AVAIL))
  148. h->left_border_y[0] = top[0] = h->topleft_border_y;
  149. break;
  150. case 1:
  151. *left = h->intern_border_y;
  152. for(i=0;i<8;i++)
  153. h->intern_border_y[i+1] = *(h->cy + 7 + i*h->l_stride);
  154. memset(&h->intern_border_y[9],h->intern_border_y[8],9);
  155. h->intern_border_y[0] = h->intern_border_y[1];
  156. memcpy(&top[1],&h->top_border_y[h->mbx*16+8],8);
  157. if(h->flags & C_AVAIL)
  158. memcpy(&top[9],&h->top_border_y[(h->mbx + 1)*16],8);
  159. else
  160. memset(&top[9],top[8],9);
  161. top[17] = top[16];
  162. top[0] = top[1];
  163. if(h->flags & B_AVAIL)
  164. h->intern_border_y[0] = top[0] = h->top_border_y[h->mbx*16+7];
  165. break;
  166. case 2:
  167. *left = &h->left_border_y[8];
  168. memcpy(&top[1],h->cy + 7*h->l_stride,16);
  169. top[17] = top[16];
  170. top[0] = top[1];
  171. if(h->flags & A_AVAIL)
  172. top[0] = h->left_border_y[8];
  173. break;
  174. case 3:
  175. *left = &h->intern_border_y[8];
  176. for(i=0;i<8;i++)
  177. h->intern_border_y[i+9] = *(h->cy + 7 + (i+8)*h->l_stride);
  178. memset(&h->intern_border_y[17],h->intern_border_y[16],9);
  179. memcpy(&top[0],h->cy + 7 + 7*h->l_stride,9);
  180. memset(&top[9],top[8],9);
  181. break;
  182. }
  183. }
  184. void ff_cavs_load_intra_pred_chroma(AVSContext *h) {
  185. /* extend borders by one pixel */
  186. h->left_border_u[9] = h->left_border_u[8];
  187. h->left_border_v[9] = h->left_border_v[8];
  188. h->top_border_u[h->mbx*10+9] = h->top_border_u[h->mbx*10+8];
  189. h->top_border_v[h->mbx*10+9] = h->top_border_v[h->mbx*10+8];
  190. if(h->mbx && h->mby) {
  191. h->top_border_u[h->mbx*10] = h->left_border_u[0] = h->topleft_border_u;
  192. h->top_border_v[h->mbx*10] = h->left_border_v[0] = h->topleft_border_v;
  193. } else {
  194. h->left_border_u[0] = h->left_border_u[1];
  195. h->left_border_v[0] = h->left_border_v[1];
  196. h->top_border_u[h->mbx*10] = h->top_border_u[h->mbx*10+1];
  197. h->top_border_v[h->mbx*10] = h->top_border_v[h->mbx*10+1];
  198. }
  199. }
  200. static void intra_pred_vert(uint8_t *d,uint8_t *top,uint8_t *left,int stride) {
  201. int y;
  202. uint64_t a = AV_RN64(&top[1]);
  203. for(y=0;y<8;y++) {
  204. *((uint64_t *)(d+y*stride)) = a;
  205. }
  206. }
  207. static void intra_pred_horiz(uint8_t *d,uint8_t *top,uint8_t *left,int stride) {
  208. int y;
  209. uint64_t a;
  210. for(y=0;y<8;y++) {
  211. a = left[y+1] * 0x0101010101010101ULL;
  212. *((uint64_t *)(d+y*stride)) = a;
  213. }
  214. }
  215. static void intra_pred_dc_128(uint8_t *d,uint8_t *top,uint8_t *left,int stride) {
  216. int y;
  217. uint64_t a = 0x8080808080808080ULL;
  218. for(y=0;y<8;y++)
  219. *((uint64_t *)(d+y*stride)) = a;
  220. }
  221. static void intra_pred_plane(uint8_t *d,uint8_t *top,uint8_t *left,int stride) {
  222. int x,y,ia;
  223. int ih = 0;
  224. int iv = 0;
  225. uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
  226. for(x=0; x<4; x++) {
  227. ih += (x+1)*(top[5+x]-top[3-x]);
  228. iv += (x+1)*(left[5+x]-left[3-x]);
  229. }
  230. ia = (top[8]+left[8])<<4;
  231. ih = (17*ih+16)>>5;
  232. iv = (17*iv+16)>>5;
  233. for(y=0; y<8; y++)
  234. for(x=0; x<8; x++)
  235. d[y*stride+x] = cm[(ia+(x-3)*ih+(y-3)*iv+16)>>5];
  236. }
  237. #define LOWPASS(ARRAY,INDEX) \
  238. (( ARRAY[(INDEX)-1] + 2*ARRAY[(INDEX)] + ARRAY[(INDEX)+1] + 2) >> 2)
  239. static void intra_pred_lp(uint8_t *d,uint8_t *top,uint8_t *left,int stride) {
  240. int x,y;
  241. for(y=0; y<8; y++)
  242. for(x=0; x<8; x++)
  243. d[y*stride+x] = (LOWPASS(top,x+1) + LOWPASS(left,y+1)) >> 1;
  244. }
  245. static void intra_pred_down_left(uint8_t *d,uint8_t *top,uint8_t *left,int stride) {
  246. int x,y;
  247. for(y=0; y<8; y++)
  248. for(x=0; x<8; x++)
  249. d[y*stride+x] = (LOWPASS(top,x+y+2) + LOWPASS(left,x+y+2)) >> 1;
  250. }
  251. static void intra_pred_down_right(uint8_t *d,uint8_t *top,uint8_t *left,int stride) {
  252. int x,y;
  253. for(y=0; y<8; y++)
  254. for(x=0; x<8; x++)
  255. if(x==y)
  256. d[y*stride+x] = (left[1]+2*top[0]+top[1]+2)>>2;
  257. else if(x>y)
  258. d[y*stride+x] = LOWPASS(top,x-y);
  259. else
  260. d[y*stride+x] = LOWPASS(left,y-x);
  261. }
  262. static void intra_pred_lp_left(uint8_t *d,uint8_t *top,uint8_t *left,int stride) {
  263. int x,y;
  264. for(y=0; y<8; y++)
  265. for(x=0; x<8; x++)
  266. d[y*stride+x] = LOWPASS(left,y+1);
  267. }
  268. static void intra_pred_lp_top(uint8_t *d,uint8_t *top,uint8_t *left,int stride) {
  269. int x,y;
  270. for(y=0; y<8; y++)
  271. for(x=0; x<8; x++)
  272. d[y*stride+x] = LOWPASS(top,x+1);
  273. }
  274. #undef LOWPASS
  275. void ff_cavs_modify_mb_i(AVSContext *h, int *pred_mode_uv) {
  276. /* save pred modes before they get modified */
  277. h->pred_mode_Y[3] = h->pred_mode_Y[5];
  278. h->pred_mode_Y[6] = h->pred_mode_Y[8];
  279. h->top_pred_Y[h->mbx*2+0] = h->pred_mode_Y[7];
  280. h->top_pred_Y[h->mbx*2+1] = h->pred_mode_Y[8];
  281. /* modify pred modes according to availability of neighbour samples */
  282. if(!(h->flags & A_AVAIL)) {
  283. modify_pred(ff_left_modifier_l, &h->pred_mode_Y[4] );
  284. modify_pred(ff_left_modifier_l, &h->pred_mode_Y[7] );
  285. modify_pred(ff_left_modifier_c, pred_mode_uv );
  286. }
  287. if(!(h->flags & B_AVAIL)) {
  288. modify_pred(ff_top_modifier_l, &h->pred_mode_Y[4] );
  289. modify_pred(ff_top_modifier_l, &h->pred_mode_Y[5] );
  290. modify_pred(ff_top_modifier_c, pred_mode_uv );
  291. }
  292. }
  293. /*****************************************************************************
  294. *
  295. * motion compensation
  296. *
  297. ****************************************************************************/
  298. static inline void mc_dir_part(AVSContext *h,Picture *pic,
  299. int chroma_height,int delta,int list,uint8_t *dest_y,
  300. uint8_t *dest_cb,uint8_t *dest_cr,int src_x_offset,
  301. int src_y_offset,qpel_mc_func *qpix_op,
  302. h264_chroma_mc_func chroma_op,cavs_vector *mv)
  303. {
  304. MpegEncContext * const s = &h->s;
  305. const int mx= mv->x + src_x_offset*8;
  306. const int my= mv->y + src_y_offset*8;
  307. const int luma_xy= (mx&3) + ((my&3)<<2);
  308. uint8_t * src_y = pic->f.data[0] + (mx >> 2) + (my >> 2) * h->l_stride;
  309. uint8_t * src_cb = pic->f.data[1] + (mx >> 3) + (my >> 3) * h->c_stride;
  310. uint8_t * src_cr = pic->f.data[2] + (mx >> 3) + (my >> 3) * h->c_stride;
  311. int extra_width= 0; //(s->flags&CODEC_FLAG_EMU_EDGE) ? 0 : 16;
  312. int extra_height= extra_width;
  313. int emu=0;
  314. const int full_mx= mx>>2;
  315. const int full_my= my>>2;
  316. const int pic_width = 16*h->mb_width;
  317. const int pic_height = 16*h->mb_height;
  318. if(!pic->f.data[0])
  319. return;
  320. if(mx&7) extra_width -= 3;
  321. if(my&7) extra_height -= 3;
  322. if( full_mx < 0-extra_width
  323. || full_my < 0-extra_height
  324. || full_mx + 16/*FIXME*/ > pic_width + extra_width
  325. || full_my + 16/*FIXME*/ > pic_height + extra_height){
  326. s->dsp.emulated_edge_mc(s->edge_emu_buffer, src_y - 2 - 2*h->l_stride, h->l_stride,
  327. 16+5, 16+5/*FIXME*/, full_mx-2, full_my-2, pic_width, pic_height);
  328. src_y= s->edge_emu_buffer + 2 + 2*h->l_stride;
  329. emu=1;
  330. }
  331. qpix_op[luma_xy](dest_y, src_y, h->l_stride); //FIXME try variable height perhaps?
  332. if(emu){
  333. s->dsp.emulated_edge_mc(s->edge_emu_buffer, src_cb, h->c_stride,
  334. 9, 9/*FIXME*/, (mx>>3), (my>>3), pic_width>>1, pic_height>>1);
  335. src_cb= s->edge_emu_buffer;
  336. }
  337. chroma_op(dest_cb, src_cb, h->c_stride, chroma_height, mx&7, my&7);
  338. if(emu){
  339. s->dsp.emulated_edge_mc(s->edge_emu_buffer, src_cr, h->c_stride,
  340. 9, 9/*FIXME*/, (mx>>3), (my>>3), pic_width>>1, pic_height>>1);
  341. src_cr= s->edge_emu_buffer;
  342. }
  343. chroma_op(dest_cr, src_cr, h->c_stride, chroma_height, mx&7, my&7);
  344. }
  345. static inline void mc_part_std(AVSContext *h,int chroma_height,int delta,
  346. uint8_t *dest_y,uint8_t *dest_cb,uint8_t *dest_cr,
  347. int x_offset, int y_offset,qpel_mc_func *qpix_put,
  348. h264_chroma_mc_func chroma_put,qpel_mc_func *qpix_avg,
  349. h264_chroma_mc_func chroma_avg, cavs_vector *mv)
  350. {
  351. qpel_mc_func *qpix_op= qpix_put;
  352. h264_chroma_mc_func chroma_op= chroma_put;
  353. dest_y += 2*x_offset + 2*y_offset*h->l_stride;
  354. dest_cb += x_offset + y_offset*h->c_stride;
  355. dest_cr += x_offset + y_offset*h->c_stride;
  356. x_offset += 8*h->mbx;
  357. y_offset += 8*h->mby;
  358. if(mv->ref >= 0){
  359. Picture *ref= &h->DPB[mv->ref];
  360. mc_dir_part(h, ref, chroma_height, delta, 0,
  361. dest_y, dest_cb, dest_cr, x_offset, y_offset,
  362. qpix_op, chroma_op, mv);
  363. qpix_op= qpix_avg;
  364. chroma_op= chroma_avg;
  365. }
  366. if((mv+MV_BWD_OFFS)->ref >= 0){
  367. Picture *ref= &h->DPB[0];
  368. mc_dir_part(h, ref, chroma_height, delta, 1,
  369. dest_y, dest_cb, dest_cr, x_offset, y_offset,
  370. qpix_op, chroma_op, mv+MV_BWD_OFFS);
  371. }
  372. }
  373. void ff_cavs_inter(AVSContext *h, enum cavs_mb mb_type) {
  374. if(ff_cavs_partition_flags[mb_type] == 0){ // 16x16
  375. mc_part_std(h, 8, 0, h->cy, h->cu, h->cv, 0, 0,
  376. h->cdsp.put_cavs_qpel_pixels_tab[0],
  377. h->s.dsp.put_h264_chroma_pixels_tab[0],
  378. h->cdsp.avg_cavs_qpel_pixels_tab[0],
  379. h->s.dsp.avg_h264_chroma_pixels_tab[0],&h->mv[MV_FWD_X0]);
  380. }else{
  381. mc_part_std(h, 4, 0, h->cy, h->cu, h->cv, 0, 0,
  382. h->cdsp.put_cavs_qpel_pixels_tab[1],
  383. h->s.dsp.put_h264_chroma_pixels_tab[1],
  384. h->cdsp.avg_cavs_qpel_pixels_tab[1],
  385. h->s.dsp.avg_h264_chroma_pixels_tab[1],&h->mv[MV_FWD_X0]);
  386. mc_part_std(h, 4, 0, h->cy, h->cu, h->cv, 4, 0,
  387. h->cdsp.put_cavs_qpel_pixels_tab[1],
  388. h->s.dsp.put_h264_chroma_pixels_tab[1],
  389. h->cdsp.avg_cavs_qpel_pixels_tab[1],
  390. h->s.dsp.avg_h264_chroma_pixels_tab[1],&h->mv[MV_FWD_X1]);
  391. mc_part_std(h, 4, 0, h->cy, h->cu, h->cv, 0, 4,
  392. h->cdsp.put_cavs_qpel_pixels_tab[1],
  393. h->s.dsp.put_h264_chroma_pixels_tab[1],
  394. h->cdsp.avg_cavs_qpel_pixels_tab[1],
  395. h->s.dsp.avg_h264_chroma_pixels_tab[1],&h->mv[MV_FWD_X2]);
  396. mc_part_std(h, 4, 0, h->cy, h->cu, h->cv, 4, 4,
  397. h->cdsp.put_cavs_qpel_pixels_tab[1],
  398. h->s.dsp.put_h264_chroma_pixels_tab[1],
  399. h->cdsp.avg_cavs_qpel_pixels_tab[1],
  400. h->s.dsp.avg_h264_chroma_pixels_tab[1],&h->mv[MV_FWD_X3]);
  401. }
  402. }
  403. /*****************************************************************************
  404. *
  405. * motion vector prediction
  406. *
  407. ****************************************************************************/
  408. static inline void scale_mv(AVSContext *h, int *d_x, int *d_y, cavs_vector *src, int distp) {
  409. int den = h->scale_den[src->ref];
  410. *d_x = (src->x*distp*den + 256 + (src->x>>31)) >> 9;
  411. *d_y = (src->y*distp*den + 256 + (src->y>>31)) >> 9;
  412. }
  413. static inline void mv_pred_median(AVSContext *h, cavs_vector *mvP,
  414. cavs_vector *mvA, cavs_vector *mvB, cavs_vector *mvC) {
  415. int ax, ay, bx, by, cx, cy;
  416. int len_ab, len_bc, len_ca, len_mid;
  417. /* scale candidates according to their temporal span */
  418. scale_mv(h, &ax, &ay, mvA, mvP->dist);
  419. scale_mv(h, &bx, &by, mvB, mvP->dist);
  420. scale_mv(h, &cx, &cy, mvC, mvP->dist);
  421. /* find the geometrical median of the three candidates */
  422. len_ab = abs(ax - bx) + abs(ay - by);
  423. len_bc = abs(bx - cx) + abs(by - cy);
  424. len_ca = abs(cx - ax) + abs(cy - ay);
  425. len_mid = mid_pred(len_ab, len_bc, len_ca);
  426. if(len_mid == len_ab) {
  427. mvP->x = cx;
  428. mvP->y = cy;
  429. } else if(len_mid == len_bc) {
  430. mvP->x = ax;
  431. mvP->y = ay;
  432. } else {
  433. mvP->x = bx;
  434. mvP->y = by;
  435. }
  436. }
  437. void ff_cavs_mv(AVSContext *h, enum cavs_mv_loc nP, enum cavs_mv_loc nC,
  438. enum cavs_mv_pred mode, enum cavs_block size, int ref) {
  439. cavs_vector *mvP = &h->mv[nP];
  440. cavs_vector *mvA = &h->mv[nP-1];
  441. cavs_vector *mvB = &h->mv[nP-4];
  442. cavs_vector *mvC = &h->mv[nC];
  443. const cavs_vector *mvP2 = NULL;
  444. mvP->ref = ref;
  445. mvP->dist = h->dist[mvP->ref];
  446. if(mvC->ref == NOT_AVAIL)
  447. mvC = &h->mv[nP-5]; // set to top-left (mvD)
  448. if((mode == MV_PRED_PSKIP) &&
  449. ((mvA->ref == NOT_AVAIL) || (mvB->ref == NOT_AVAIL) ||
  450. ((mvA->x | mvA->y | mvA->ref) == 0) ||
  451. ((mvB->x | mvB->y | mvB->ref) == 0) )) {
  452. mvP2 = &ff_cavs_un_mv;
  453. /* if there is only one suitable candidate, take it */
  454. } else if((mvA->ref >= 0) && (mvB->ref < 0) && (mvC->ref < 0)) {
  455. mvP2= mvA;
  456. } else if((mvA->ref < 0) && (mvB->ref >= 0) && (mvC->ref < 0)) {
  457. mvP2= mvB;
  458. } else if((mvA->ref < 0) && (mvB->ref < 0) && (mvC->ref >= 0)) {
  459. mvP2= mvC;
  460. } else if(mode == MV_PRED_LEFT && mvA->ref == ref){
  461. mvP2= mvA;
  462. } else if(mode == MV_PRED_TOP && mvB->ref == ref){
  463. mvP2= mvB;
  464. } else if(mode == MV_PRED_TOPRIGHT && mvC->ref == ref){
  465. mvP2= mvC;
  466. }
  467. if(mvP2){
  468. mvP->x = mvP2->x;
  469. mvP->y = mvP2->y;
  470. }else
  471. mv_pred_median(h, mvP, mvA, mvB, mvC);
  472. if(mode < MV_PRED_PSKIP) {
  473. mvP->x += get_se_golomb(&h->s.gb);
  474. mvP->y += get_se_golomb(&h->s.gb);
  475. }
  476. set_mvs(mvP,size);
  477. }
  478. /*****************************************************************************
  479. *
  480. * macroblock level
  481. *
  482. ****************************************************************************/
  483. /**
  484. * initialise predictors for motion vectors and intra prediction
  485. */
  486. void ff_cavs_init_mb(AVSContext *h) {
  487. int i;
  488. /* copy predictors from top line (MB B and C) into cache */
  489. for(i=0;i<3;i++) {
  490. h->mv[MV_FWD_B2+i] = h->top_mv[0][h->mbx*2+i];
  491. h->mv[MV_BWD_B2+i] = h->top_mv[1][h->mbx*2+i];
  492. }
  493. h->pred_mode_Y[1] = h->top_pred_Y[h->mbx*2+0];
  494. h->pred_mode_Y[2] = h->top_pred_Y[h->mbx*2+1];
  495. /* clear top predictors if MB B is not available */
  496. if(!(h->flags & B_AVAIL)) {
  497. h->mv[MV_FWD_B2] = ff_cavs_un_mv;
  498. h->mv[MV_FWD_B3] = ff_cavs_un_mv;
  499. h->mv[MV_BWD_B2] = ff_cavs_un_mv;
  500. h->mv[MV_BWD_B3] = ff_cavs_un_mv;
  501. h->pred_mode_Y[1] = h->pred_mode_Y[2] = NOT_AVAIL;
  502. h->flags &= ~(C_AVAIL|D_AVAIL);
  503. } else if(h->mbx) {
  504. h->flags |= D_AVAIL;
  505. }
  506. if(h->mbx == h->mb_width-1) //MB C not available
  507. h->flags &= ~C_AVAIL;
  508. /* clear top-right predictors if MB C is not available */
  509. if(!(h->flags & C_AVAIL)) {
  510. h->mv[MV_FWD_C2] = ff_cavs_un_mv;
  511. h->mv[MV_BWD_C2] = ff_cavs_un_mv;
  512. }
  513. /* clear top-left predictors if MB D is not available */
  514. if(!(h->flags & D_AVAIL)) {
  515. h->mv[MV_FWD_D3] = ff_cavs_un_mv;
  516. h->mv[MV_BWD_D3] = ff_cavs_un_mv;
  517. }
  518. }
  519. /**
  520. * save predictors for later macroblocks and increase
  521. * macroblock address
  522. * @return 0 if end of frame is reached, 1 otherwise
  523. */
  524. int ff_cavs_next_mb(AVSContext *h) {
  525. int i;
  526. h->flags |= A_AVAIL;
  527. h->cy += 16;
  528. h->cu += 8;
  529. h->cv += 8;
  530. /* copy mvs as predictors to the left */
  531. for(i=0;i<=20;i+=4)
  532. h->mv[i] = h->mv[i+2];
  533. /* copy bottom mvs from cache to top line */
  534. h->top_mv[0][h->mbx*2+0] = h->mv[MV_FWD_X2];
  535. h->top_mv[0][h->mbx*2+1] = h->mv[MV_FWD_X3];
  536. h->top_mv[1][h->mbx*2+0] = h->mv[MV_BWD_X2];
  537. h->top_mv[1][h->mbx*2+1] = h->mv[MV_BWD_X3];
  538. /* next MB address */
  539. h->mbidx++;
  540. h->mbx++;
  541. if(h->mbx == h->mb_width) { //new mb line
  542. h->flags = B_AVAIL|C_AVAIL;
  543. /* clear left pred_modes */
  544. h->pred_mode_Y[3] = h->pred_mode_Y[6] = NOT_AVAIL;
  545. /* clear left mv predictors */
  546. for(i=0;i<=20;i+=4)
  547. h->mv[i] = ff_cavs_un_mv;
  548. h->mbx = 0;
  549. h->mby++;
  550. /* re-calculate sample pointers */
  551. h->cy = h->picture.f.data[0] + h->mby * 16 * h->l_stride;
  552. h->cu = h->picture.f.data[1] + h->mby * 8 * h->c_stride;
  553. h->cv = h->picture.f.data[2] + h->mby * 8 * h->c_stride;
  554. if(h->mby == h->mb_height) { //frame end
  555. return 0;
  556. }
  557. }
  558. return 1;
  559. }
  560. /*****************************************************************************
  561. *
  562. * frame level
  563. *
  564. ****************************************************************************/
  565. void ff_cavs_init_pic(AVSContext *h) {
  566. int i;
  567. /* clear some predictors */
  568. for(i=0;i<=20;i+=4)
  569. h->mv[i] = ff_cavs_un_mv;
  570. h->mv[MV_BWD_X0] = ff_cavs_dir_mv;
  571. set_mvs(&h->mv[MV_BWD_X0], BLK_16X16);
  572. h->mv[MV_FWD_X0] = ff_cavs_dir_mv;
  573. set_mvs(&h->mv[MV_FWD_X0], BLK_16X16);
  574. h->pred_mode_Y[3] = h->pred_mode_Y[6] = NOT_AVAIL;
  575. h->cy = h->picture.f.data[0];
  576. h->cu = h->picture.f.data[1];
  577. h->cv = h->picture.f.data[2];
  578. h->l_stride = h->picture.f.linesize[0];
  579. h->c_stride = h->picture.f.linesize[1];
  580. h->luma_scan[2] = 8*h->l_stride;
  581. h->luma_scan[3] = 8*h->l_stride+8;
  582. h->mbx = h->mby = h->mbidx = 0;
  583. h->flags = 0;
  584. }
  585. /*****************************************************************************
  586. *
  587. * headers and interface
  588. *
  589. ****************************************************************************/
  590. /**
  591. * some predictions require data from the top-neighbouring macroblock.
  592. * this data has to be stored for one complete row of macroblocks
  593. * and this storage space is allocated here
  594. */
  595. void ff_cavs_init_top_lines(AVSContext *h) {
  596. /* alloc top line of predictors */
  597. h->top_qp = av_malloc( h->mb_width);
  598. h->top_mv[0] = av_malloc((h->mb_width*2+1)*sizeof(cavs_vector));
  599. h->top_mv[1] = av_malloc((h->mb_width*2+1)*sizeof(cavs_vector));
  600. h->top_pred_Y = av_malloc( h->mb_width*2*sizeof(*h->top_pred_Y));
  601. h->top_border_y = av_malloc((h->mb_width+1)*16);
  602. h->top_border_u = av_malloc( h->mb_width * 10);
  603. h->top_border_v = av_malloc( h->mb_width * 10);
  604. /* alloc space for co-located MVs and types */
  605. h->col_mv = av_malloc( h->mb_width*h->mb_height*4*sizeof(cavs_vector));
  606. h->col_type_base = av_malloc(h->mb_width*h->mb_height);
  607. h->block = av_mallocz(64*sizeof(DCTELEM));
  608. }
  609. av_cold int ff_cavs_init(AVCodecContext *avctx) {
  610. AVSContext *h = avctx->priv_data;
  611. MpegEncContext * const s = &h->s;
  612. ff_MPV_decode_defaults(s);
  613. ff_cavsdsp_init(&h->cdsp, avctx);
  614. s->avctx = avctx;
  615. avctx->pix_fmt= PIX_FMT_YUV420P;
  616. h->luma_scan[0] = 0;
  617. h->luma_scan[1] = 8;
  618. h->intra_pred_l[ INTRA_L_VERT] = intra_pred_vert;
  619. h->intra_pred_l[ INTRA_L_HORIZ] = intra_pred_horiz;
  620. h->intra_pred_l[ INTRA_L_LP] = intra_pred_lp;
  621. h->intra_pred_l[ INTRA_L_DOWN_LEFT] = intra_pred_down_left;
  622. h->intra_pred_l[INTRA_L_DOWN_RIGHT] = intra_pred_down_right;
  623. h->intra_pred_l[ INTRA_L_LP_LEFT] = intra_pred_lp_left;
  624. h->intra_pred_l[ INTRA_L_LP_TOP] = intra_pred_lp_top;
  625. h->intra_pred_l[ INTRA_L_DC_128] = intra_pred_dc_128;
  626. h->intra_pred_c[ INTRA_C_LP] = intra_pred_lp;
  627. h->intra_pred_c[ INTRA_C_HORIZ] = intra_pred_horiz;
  628. h->intra_pred_c[ INTRA_C_VERT] = intra_pred_vert;
  629. h->intra_pred_c[ INTRA_C_PLANE] = intra_pred_plane;
  630. h->intra_pred_c[ INTRA_C_LP_LEFT] = intra_pred_lp_left;
  631. h->intra_pred_c[ INTRA_C_LP_TOP] = intra_pred_lp_top;
  632. h->intra_pred_c[ INTRA_C_DC_128] = intra_pred_dc_128;
  633. h->mv[ 7] = ff_cavs_un_mv;
  634. h->mv[19] = ff_cavs_un_mv;
  635. return 0;
  636. }
  637. av_cold int ff_cavs_end(AVCodecContext *avctx) {
  638. AVSContext *h = avctx->priv_data;
  639. av_free(h->top_qp);
  640. av_free(h->top_mv[0]);
  641. av_free(h->top_mv[1]);
  642. av_free(h->top_pred_Y);
  643. av_free(h->top_border_y);
  644. av_free(h->top_border_u);
  645. av_free(h->top_border_v);
  646. av_free(h->col_mv);
  647. av_free(h->col_type_base);
  648. av_free(h->block);
  649. return 0;
  650. }