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.

839 lines
30KB

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