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.

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