cavs.c 30 KB

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