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.

1441 lines
60KB

  1. /*
  2. * HEVC video decoder
  3. *
  4. * Copyright (C) 2012 - 2013 Guillaume Martres
  5. *
  6. * This file is part of Libav.
  7. *
  8. * Libav is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * Libav is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with Libav; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #include "get_bits.h"
  23. #include "hevcdec.h"
  24. #include "bit_depth_template.c"
  25. static void FUNC(put_pcm)(uint8_t *_dst, ptrdiff_t stride, int size,
  26. GetBitContext *gb, int pcm_bit_depth)
  27. {
  28. int x, y;
  29. pixel *dst = (pixel *)_dst;
  30. stride /= sizeof(pixel);
  31. for (y = 0; y < size; y++) {
  32. for (x = 0; x < size; x++)
  33. dst[x] = get_bits(gb, pcm_bit_depth) << (BIT_DEPTH - pcm_bit_depth);
  34. dst += stride;
  35. }
  36. }
  37. static av_always_inline void FUNC(add_residual)(uint8_t *_dst, int16_t *res,
  38. ptrdiff_t stride, int size)
  39. {
  40. int x, y;
  41. pixel *dst = (pixel *)_dst;
  42. stride /= sizeof(pixel);
  43. for (y = 0; y < size; y++) {
  44. for (x = 0; x < size; x++) {
  45. dst[x] = av_clip_pixel(dst[x] + *res);
  46. res++;
  47. }
  48. dst += stride;
  49. }
  50. }
  51. static void FUNC(add_residual4x4)(uint8_t *_dst, int16_t *res,
  52. ptrdiff_t stride)
  53. {
  54. FUNC(add_residual)(_dst, res, stride, 4);
  55. }
  56. static void FUNC(add_residual8x8)(uint8_t *_dst, int16_t *res,
  57. ptrdiff_t stride)
  58. {
  59. FUNC(add_residual)(_dst, res, stride, 8);
  60. }
  61. static void FUNC(add_residual16x16)(uint8_t *_dst, int16_t *res,
  62. ptrdiff_t stride)
  63. {
  64. FUNC(add_residual)(_dst, res, stride, 16);
  65. }
  66. static void FUNC(add_residual32x32)(uint8_t *_dst, int16_t *res,
  67. ptrdiff_t stride)
  68. {
  69. FUNC(add_residual)(_dst, res, stride, 32);
  70. }
  71. static void FUNC(dequant)(int16_t *coeffs)
  72. {
  73. int shift = 13 - BIT_DEPTH;
  74. #if BIT_DEPTH <= 13
  75. int offset = 1 << (shift - 1);
  76. #else
  77. int offset = 0;
  78. #endif
  79. int x, y;
  80. for (y = 0; y < 4 * 4; y += 4) {
  81. for (x = 0; x < 4; x++)
  82. coeffs[y + x] = (coeffs[y + x] + offset) >> shift;
  83. }
  84. }
  85. #define SET(dst, x) (dst) = (x)
  86. #define SCALE(dst, x) (dst) = av_clip_int16(((x) + add) >> shift)
  87. #define TR_4x4_LUMA(dst, src, step, assign) \
  88. do { \
  89. int c0 = src[0 * step] + src[2 * step]; \
  90. int c1 = src[2 * step] + src[3 * step]; \
  91. int c2 = src[0 * step] - src[3 * step]; \
  92. int c3 = 74 * src[1 * step]; \
  93. \
  94. assign(dst[2 * step], 74 * (src[0 * step] - \
  95. src[2 * step] + \
  96. src[3 * step])); \
  97. assign(dst[0 * step], 29 * c0 + 55 * c1 + c3); \
  98. assign(dst[1 * step], 55 * c2 - 29 * c1 + c3); \
  99. assign(dst[3 * step], 55 * c0 + 29 * c2 - c3); \
  100. } while (0)
  101. static void FUNC(transform_4x4_luma)(int16_t *coeffs)
  102. {
  103. int i;
  104. int shift = 7;
  105. int add = 1 << (shift - 1);
  106. int16_t *src = coeffs;
  107. for (i = 0; i < 4; i++) {
  108. TR_4x4_LUMA(src, src, 4, SCALE);
  109. src++;
  110. }
  111. shift = 20 - BIT_DEPTH;
  112. add = 1 << (shift - 1);
  113. for (i = 0; i < 4; i++) {
  114. TR_4x4_LUMA(coeffs, coeffs, 1, SCALE);
  115. coeffs += 4;
  116. }
  117. }
  118. #undef TR_4x4_LUMA
  119. #define TR_4(dst, src, dstep, sstep, assign, end) \
  120. do { \
  121. const int e0 = transform[8 * 0][0] * src[0 * sstep] + \
  122. transform[8 * 2][0] * src[2 * sstep]; \
  123. const int e1 = transform[8 * 0][1] * src[0 * sstep] + \
  124. transform[8 * 2][1] * src[2 * sstep]; \
  125. const int o0 = transform[8 * 1][0] * src[1 * sstep] + \
  126. transform[8 * 3][0] * src[3 * sstep]; \
  127. const int o1 = transform[8 * 1][1] * src[1 * sstep] + \
  128. transform[8 * 3][1] * src[3 * sstep]; \
  129. \
  130. assign(dst[0 * dstep], e0 + o0); \
  131. assign(dst[1 * dstep], e1 + o1); \
  132. assign(dst[2 * dstep], e1 - o1); \
  133. assign(dst[3 * dstep], e0 - o0); \
  134. } while (0)
  135. #define TR_8(dst, src, dstep, sstep, assign, end) \
  136. do { \
  137. int i, j; \
  138. int e_8[4]; \
  139. int o_8[4] = { 0 }; \
  140. for (i = 0; i < 4; i++) \
  141. for (j = 1; j < end; j += 2) \
  142. o_8[i] += transform[4 * j][i] * src[j * sstep]; \
  143. TR_4(e_8, src, 1, 2 * sstep, SET, 4); \
  144. \
  145. for (i = 0; i < 4; i++) { \
  146. assign(dst[i * dstep], e_8[i] + o_8[i]); \
  147. assign(dst[(7 - i) * dstep], e_8[i] - o_8[i]); \
  148. } \
  149. } while (0)
  150. #define TR_16(dst, src, dstep, sstep, assign, end) \
  151. do { \
  152. int i, j; \
  153. int e_16[8]; \
  154. int o_16[8] = { 0 }; \
  155. for (i = 0; i < 8; i++) \
  156. for (j = 1; j < end; j += 2) \
  157. o_16[i] += transform[2 * j][i] * src[j * sstep]; \
  158. TR_8(e_16, src, 1, 2 * sstep, SET, 8); \
  159. \
  160. for (i = 0; i < 8; i++) { \
  161. assign(dst[i * dstep], e_16[i] + o_16[i]); \
  162. assign(dst[(15 - i) * dstep], e_16[i] - o_16[i]); \
  163. } \
  164. } while (0)
  165. #define TR_32(dst, src, dstep, sstep, assign, end) \
  166. do { \
  167. int i, j; \
  168. int e_32[16]; \
  169. int o_32[16] = { 0 }; \
  170. for (i = 0; i < 16; i++) \
  171. for (j = 1; j < end; j += 2) \
  172. o_32[i] += transform[j][i] * src[j * sstep]; \
  173. TR_16(e_32, src, 1, 2 * sstep, SET, end / 2); \
  174. \
  175. for (i = 0; i < 16; i++) { \
  176. assign(dst[i * dstep], e_32[i] + o_32[i]); \
  177. assign(dst[(31 - i) * dstep], e_32[i] - o_32[i]); \
  178. } \
  179. } while (0)
  180. #define IDCT_VAR4(H) \
  181. int limit2 = FFMIN(col_limit + 4, H)
  182. #define IDCT_VAR8(H) \
  183. int limit = FFMIN(col_limit, H); \
  184. int limit2 = FFMIN(col_limit + 4, H)
  185. #define IDCT_VAR16(H) IDCT_VAR8(H)
  186. #define IDCT_VAR32(H) IDCT_VAR8(H)
  187. #define IDCT(H) \
  188. static void FUNC(idct_ ## H ## x ## H )(int16_t *coeffs, \
  189. int col_limit) \
  190. { \
  191. int i; \
  192. int shift = 7; \
  193. int add = 1 << (shift - 1); \
  194. int16_t *src = coeffs; \
  195. IDCT_VAR ## H(H); \
  196. \
  197. for (i = 0; i < H; i++) { \
  198. TR_ ## H(src, src, H, H, SCALE, limit2); \
  199. if (limit2 < H && i%4 == 0 && !!i) \
  200. limit2 -= 4; \
  201. src++; \
  202. } \
  203. \
  204. shift = 20 - BIT_DEPTH; \
  205. add = 1 << (shift - 1); \
  206. for (i = 0; i < H; i++) { \
  207. TR_ ## H(coeffs, coeffs, 1, 1, SCALE, limit); \
  208. coeffs += H; \
  209. } \
  210. }
  211. #define IDCT_DC(H) \
  212. static void FUNC(idct_ ## H ## x ## H ## _dc)(int16_t *coeffs) \
  213. { \
  214. int i, j; \
  215. int shift = 14 - BIT_DEPTH; \
  216. int add = 1 << (shift - 1); \
  217. int coeff = (((coeffs[0] + 1) >> 1) + add) >> shift; \
  218. \
  219. for (j = 0; j < H; j++) { \
  220. for (i = 0; i < H; i++) { \
  221. coeffs[i + j * H] = coeff; \
  222. } \
  223. } \
  224. }
  225. IDCT( 4)
  226. IDCT( 8)
  227. IDCT(16)
  228. IDCT(32)
  229. IDCT_DC( 4)
  230. IDCT_DC( 8)
  231. IDCT_DC(16)
  232. IDCT_DC(32)
  233. #undef TR_4
  234. #undef TR_8
  235. #undef TR_16
  236. #undef TR_32
  237. #undef SET
  238. #undef SCALE
  239. #undef ADD_AND_SCALE
  240. static void FUNC(sao_band_filter)(uint8_t *_dst, uint8_t *_src,
  241. ptrdiff_t stride, SAOParams *sao,
  242. int *borders, int width, int height,
  243. int c_idx, int class)
  244. {
  245. pixel *dst = (pixel *)_dst;
  246. pixel *src = (pixel *)_src;
  247. int offset_table[32] = { 0 };
  248. int k, y, x;
  249. int chroma = !!c_idx;
  250. int shift = BIT_DEPTH - 5;
  251. int *sao_offset_val = sao->offset_val[c_idx];
  252. int sao_left_class = sao->band_position[c_idx];
  253. int init_y = 0, init_x = 0;
  254. stride /= sizeof(pixel);
  255. switch (class) {
  256. case 0:
  257. if (!borders[2])
  258. width -= (8 >> chroma) + 2;
  259. if (!borders[3])
  260. height -= (4 >> chroma) + 2;
  261. break;
  262. case 1:
  263. init_y = -(4 >> chroma) - 2;
  264. if (!borders[2])
  265. width -= (8 >> chroma) + 2;
  266. height = (4 >> chroma) + 2;
  267. break;
  268. case 2:
  269. init_x = -(8 >> chroma) - 2;
  270. width = (8 >> chroma) + 2;
  271. if (!borders[3])
  272. height -= (4 >> chroma) + 2;
  273. break;
  274. case 3:
  275. init_y = -(4 >> chroma) - 2;
  276. init_x = -(8 >> chroma) - 2;
  277. width = (8 >> chroma) + 2;
  278. height = (4 >> chroma) + 2;
  279. break;
  280. }
  281. dst = dst + (init_y * stride + init_x);
  282. src = src + (init_y * stride + init_x);
  283. for (k = 0; k < 4; k++)
  284. offset_table[(k + sao_left_class) & 31] = sao_offset_val[k + 1];
  285. for (y = 0; y < height; y++) {
  286. for (x = 0; x < width; x++)
  287. dst[x] = av_clip_pixel(src[x] + offset_table[src[x] >> shift]);
  288. dst += stride;
  289. src += stride;
  290. }
  291. }
  292. static void FUNC(sao_band_filter_0)(uint8_t *dst, uint8_t *src,
  293. ptrdiff_t stride, SAOParams *sao,
  294. int *borders, int width, int height,
  295. int c_idx)
  296. {
  297. FUNC(sao_band_filter)(dst, src, stride, sao, borders,
  298. width, height, c_idx, 0);
  299. }
  300. static void FUNC(sao_band_filter_1)(uint8_t *dst, uint8_t *src,
  301. ptrdiff_t stride, SAOParams *sao,
  302. int *borders, int width, int height,
  303. int c_idx)
  304. {
  305. FUNC(sao_band_filter)(dst, src, stride, sao, borders,
  306. width, height, c_idx, 1);
  307. }
  308. static void FUNC(sao_band_filter_2)(uint8_t *dst, uint8_t *src,
  309. ptrdiff_t stride, SAOParams *sao,
  310. int *borders, int width, int height,
  311. int c_idx)
  312. {
  313. FUNC(sao_band_filter)(dst, src, stride, sao, borders,
  314. width, height, c_idx, 2);
  315. }
  316. static void FUNC(sao_band_filter_3)(uint8_t *_dst, uint8_t *_src,
  317. ptrdiff_t stride, SAOParams *sao,
  318. int *borders, int width, int height,
  319. int c_idx)
  320. {
  321. FUNC(sao_band_filter)(_dst, _src, stride, sao, borders,
  322. width, height, c_idx, 3);
  323. }
  324. static void FUNC(sao_edge_filter_0)(uint8_t *_dst, uint8_t *_src,
  325. ptrdiff_t stride, SAOParams *sao,
  326. int *borders, int _width, int _height,
  327. int c_idx, uint8_t vert_edge,
  328. uint8_t horiz_edge, uint8_t diag_edge)
  329. {
  330. int x, y;
  331. pixel *dst = (pixel *)_dst;
  332. pixel *src = (pixel *)_src;
  333. int chroma = !!c_idx;
  334. int *sao_offset_val = sao->offset_val[c_idx];
  335. int sao_eo_class = sao->eo_class[c_idx];
  336. int init_x = 0, init_y = 0, width = _width, height = _height;
  337. static const int8_t pos[4][2][2] = {
  338. { { -1, 0 }, { 1, 0 } }, // horizontal
  339. { { 0, -1 }, { 0, 1 } }, // vertical
  340. { { -1, -1 }, { 1, 1 } }, // 45 degree
  341. { { 1, -1 }, { -1, 1 } }, // 135 degree
  342. };
  343. static const uint8_t edge_idx[] = { 1, 2, 0, 3, 4 };
  344. #define CMP(a, b) ((a) > (b) ? 1 : ((a) == (b) ? 0 : -1))
  345. stride /= sizeof(pixel);
  346. if (!borders[2])
  347. width -= (8 >> chroma) + 2;
  348. if (!borders[3])
  349. height -= (4 >> chroma) + 2;
  350. dst = dst + (init_y * stride + init_x);
  351. src = src + (init_y * stride + init_x);
  352. init_y = init_x = 0;
  353. if (sao_eo_class != SAO_EO_VERT) {
  354. if (borders[0]) {
  355. int offset_val = sao_offset_val[0];
  356. ptrdiff_t y_stride = 0;
  357. for (y = 0; y < height; y++) {
  358. dst[y_stride] = av_clip_pixel(src[y_stride] + offset_val);
  359. y_stride += stride;
  360. }
  361. init_x = 1;
  362. }
  363. if (borders[2]) {
  364. int offset_val = sao_offset_val[0];
  365. ptrdiff_t x_stride = width - 1;
  366. for (x = 0; x < height; x++) {
  367. dst[x_stride] = av_clip_pixel(src[x_stride] + offset_val);
  368. x_stride += stride;
  369. }
  370. width--;
  371. }
  372. }
  373. if (sao_eo_class != SAO_EO_HORIZ) {
  374. if (borders[1]) {
  375. int offset_val = sao_offset_val[0];
  376. for (x = init_x; x < width; x++)
  377. dst[x] = av_clip_pixel(src[x] + offset_val);
  378. init_y = 1;
  379. }
  380. if (borders[3]) {
  381. int offset_val = sao_offset_val[0];
  382. ptrdiff_t y_stride = stride * (height - 1);
  383. for (x = init_x; x < width; x++)
  384. dst[x + y_stride] = av_clip_pixel(src[x + y_stride] + offset_val);
  385. height--;
  386. }
  387. }
  388. {
  389. ptrdiff_t y_stride = init_y * stride;
  390. int pos_0_0 = pos[sao_eo_class][0][0];
  391. int pos_0_1 = pos[sao_eo_class][0][1];
  392. int pos_1_0 = pos[sao_eo_class][1][0];
  393. int pos_1_1 = pos[sao_eo_class][1][1];
  394. ptrdiff_t y_stride_0_1 = (init_y + pos_0_1) * stride;
  395. ptrdiff_t y_stride_1_1 = (init_y + pos_1_1) * stride;
  396. for (y = init_y; y < height; y++) {
  397. for (x = init_x; x < width; x++) {
  398. int diff0 = CMP(src[x + y_stride], src[x + pos_0_0 + y_stride_0_1]);
  399. int diff1 = CMP(src[x + y_stride], src[x + pos_1_0 + y_stride_1_1]);
  400. int offset_val = edge_idx[2 + diff0 + diff1];
  401. dst[x + y_stride] = av_clip_pixel(src[x + y_stride] + sao_offset_val[offset_val]);
  402. }
  403. y_stride += stride;
  404. y_stride_0_1 += stride;
  405. y_stride_1_1 += stride;
  406. }
  407. }
  408. {
  409. // Restore pixels that can't be modified
  410. int save_upper_left = !diag_edge && sao_eo_class == SAO_EO_135D && !borders[0] && !borders[1];
  411. if (vert_edge && sao_eo_class != SAO_EO_VERT)
  412. for (y = init_y+save_upper_left; y< height; y++)
  413. dst[y*stride] = src[y*stride];
  414. if(horiz_edge && sao_eo_class != SAO_EO_HORIZ)
  415. for(x = init_x+save_upper_left; x<width; x++)
  416. dst[x] = src[x];
  417. if(diag_edge && sao_eo_class == SAO_EO_135D)
  418. dst[0] = src[0];
  419. }
  420. #undef CMP
  421. }
  422. static void FUNC(sao_edge_filter_1)(uint8_t *_dst, uint8_t *_src,
  423. ptrdiff_t stride, SAOParams *sao,
  424. int *borders, int _width, int _height,
  425. int c_idx, uint8_t vert_edge,
  426. uint8_t horiz_edge, uint8_t diag_edge)
  427. {
  428. int x, y;
  429. pixel *dst = (pixel *)_dst;
  430. pixel *src = (pixel *)_src;
  431. int chroma = !!c_idx;
  432. int *sao_offset_val = sao->offset_val[c_idx];
  433. int sao_eo_class = sao->eo_class[c_idx];
  434. int init_x = 0, init_y = 0, width = _width, height = _height;
  435. static const int8_t pos[4][2][2] = {
  436. { { -1, 0 }, { 1, 0 } }, // horizontal
  437. { { 0, -1 }, { 0, 1 } }, // vertical
  438. { { -1, -1 }, { 1, 1 } }, // 45 degree
  439. { { 1, -1 }, { -1, 1 } }, // 135 degree
  440. };
  441. static const uint8_t edge_idx[] = { 1, 2, 0, 3, 4 };
  442. #define CMP(a, b) ((a) > (b) ? 1 : ((a) == (b) ? 0 : -1))
  443. stride /= sizeof(pixel);
  444. init_y = -(4 >> chroma) - 2;
  445. if (!borders[2])
  446. width -= (8 >> chroma) + 2;
  447. height = (4 >> chroma) + 2;
  448. dst = dst + (init_y * stride + init_x);
  449. src = src + (init_y * stride + init_x);
  450. init_y = init_x = 0;
  451. if (sao_eo_class != SAO_EO_VERT) {
  452. if (borders[0]) {
  453. int offset_val = sao_offset_val[0];
  454. ptrdiff_t y_stride = 0;
  455. for (y = 0; y < height; y++) {
  456. dst[y_stride] = av_clip_pixel(src[y_stride] + offset_val);
  457. y_stride += stride;
  458. }
  459. init_x = 1;
  460. }
  461. if (borders[2]) {
  462. int offset_val = sao_offset_val[0];
  463. ptrdiff_t x_stride = width - 1;
  464. for (x = 0; x < height; x++) {
  465. dst[x_stride] = av_clip_pixel(src[x_stride] + offset_val);
  466. x_stride += stride;
  467. }
  468. width--;
  469. }
  470. }
  471. {
  472. ptrdiff_t y_stride = init_y * stride;
  473. int pos_0_0 = pos[sao_eo_class][0][0];
  474. int pos_0_1 = pos[sao_eo_class][0][1];
  475. int pos_1_0 = pos[sao_eo_class][1][0];
  476. int pos_1_1 = pos[sao_eo_class][1][1];
  477. ptrdiff_t y_stride_0_1 = (init_y + pos_0_1) * stride;
  478. ptrdiff_t y_stride_1_1 = (init_y + pos_1_1) * stride;
  479. for (y = init_y; y < height; y++) {
  480. for (x = init_x; x < width; x++) {
  481. int diff0 = CMP(src[x + y_stride], src[x + pos_0_0 + y_stride_0_1]);
  482. int diff1 = CMP(src[x + y_stride], src[x + pos_1_0 + y_stride_1_1]);
  483. int offset_val = edge_idx[2 + diff0 + diff1];
  484. dst[x + y_stride] = av_clip_pixel(src[x + y_stride] + sao_offset_val[offset_val]);
  485. }
  486. y_stride += stride;
  487. y_stride_0_1 += stride;
  488. y_stride_1_1 += stride;
  489. }
  490. }
  491. {
  492. // Restore pixels that can't be modified
  493. int save_lower_left = !diag_edge && sao_eo_class == SAO_EO_45D && !borders[0];
  494. if(vert_edge && sao_eo_class != SAO_EO_VERT)
  495. for(y = init_y; y< height-save_lower_left; y++)
  496. dst[y*stride] = src[y*stride];
  497. if(horiz_edge && sao_eo_class != SAO_EO_HORIZ)
  498. for(x = init_x+save_lower_left; x<width; x++)
  499. dst[(height-1)*stride+x] = src[(height-1)*stride+x];
  500. if(diag_edge && sao_eo_class == SAO_EO_45D)
  501. dst[stride*(height-1)] = src[stride*(height-1)];
  502. }
  503. #undef CMP
  504. }
  505. static void FUNC(sao_edge_filter_2)(uint8_t *_dst, uint8_t *_src,
  506. ptrdiff_t stride, SAOParams *sao,
  507. int *borders, int _width, int _height,
  508. int c_idx, uint8_t vert_edge,
  509. uint8_t horiz_edge, uint8_t diag_edge)
  510. {
  511. int x, y;
  512. pixel *dst = (pixel *)_dst;
  513. pixel *src = (pixel *)_src;
  514. int chroma = !!c_idx;
  515. int *sao_offset_val = sao->offset_val[c_idx];
  516. int sao_eo_class = sao->eo_class[c_idx];
  517. int init_x = 0, init_y = 0, width = _width, height = _height;
  518. static const int8_t pos[4][2][2] = {
  519. { { -1, 0 }, { 1, 0 } }, // horizontal
  520. { { 0, -1 }, { 0, 1 } }, // vertical
  521. { { -1, -1 }, { 1, 1 } }, // 45 degree
  522. { { 1, -1 }, { -1, 1 } }, // 135 degree
  523. };
  524. static const uint8_t edge_idx[] = { 1, 2, 0, 3, 4 };
  525. #define CMP(a, b) ((a) > (b) ? 1 : ((a) == (b) ? 0 : -1))
  526. stride /= sizeof(pixel);
  527. init_x = -(8 >> chroma) - 2;
  528. width = (8 >> chroma) + 2;
  529. if (!borders[3])
  530. height -= (4 >> chroma) + 2;
  531. dst = dst + (init_y * stride + init_x);
  532. src = src + (init_y * stride + init_x);
  533. init_y = init_x = 0;
  534. if (sao_eo_class != SAO_EO_HORIZ) {
  535. if (borders[1]) {
  536. int offset_val = sao_offset_val[0];
  537. for (x = init_x; x < width; x++)
  538. dst[x] = av_clip_pixel(src[x] + offset_val);
  539. init_y = 1;
  540. }
  541. if (borders[3]) {
  542. int offset_val = sao_offset_val[0];
  543. ptrdiff_t y_stride = stride * (height - 1);
  544. for (x = init_x; x < width; x++)
  545. dst[x + y_stride] = av_clip_pixel(src[x + y_stride] + offset_val);
  546. height--;
  547. }
  548. }
  549. {
  550. ptrdiff_t y_stride = init_y * stride;
  551. int pos_0_0 = pos[sao_eo_class][0][0];
  552. int pos_0_1 = pos[sao_eo_class][0][1];
  553. int pos_1_0 = pos[sao_eo_class][1][0];
  554. int pos_1_1 = pos[sao_eo_class][1][1];
  555. ptrdiff_t y_stride_0_1 = (init_y + pos_0_1) * stride;
  556. ptrdiff_t y_stride_1_1 = (init_y + pos_1_1) * stride;
  557. for (y = init_y; y < height; y++) {
  558. for (x = init_x; x < width; x++) {
  559. int diff0 = CMP(src[x + y_stride], src[x + pos_0_0 + y_stride_0_1]);
  560. int diff1 = CMP(src[x + y_stride], src[x + pos_1_0 + y_stride_1_1]);
  561. int offset_val = edge_idx[2 + diff0 + diff1];
  562. dst[x + y_stride] = av_clip_pixel(src[x + y_stride] + sao_offset_val[offset_val]);
  563. }
  564. y_stride += stride;
  565. y_stride_0_1 += stride;
  566. y_stride_1_1 += stride;
  567. }
  568. }
  569. {
  570. // Restore pixels that can't be modified
  571. int save_upper_right = !diag_edge && sao_eo_class == SAO_EO_45D && !borders[1];
  572. if(vert_edge && sao_eo_class != SAO_EO_VERT)
  573. for(y = init_y+save_upper_right; y< height; y++)
  574. dst[y*stride+width-1] = src[y*stride+width-1];
  575. if(horiz_edge && sao_eo_class != SAO_EO_HORIZ)
  576. for(x = init_x; x<width-save_upper_right; x++)
  577. dst[x] = src[x];
  578. if(diag_edge && sao_eo_class == SAO_EO_45D)
  579. dst[width-1] = src[width-1];
  580. }
  581. #undef CMP
  582. }
  583. static void FUNC(sao_edge_filter_3)(uint8_t *_dst, uint8_t *_src,
  584. ptrdiff_t stride, SAOParams *sao,
  585. int *borders, int _width, int _height,
  586. int c_idx, uint8_t vert_edge,
  587. uint8_t horiz_edge, uint8_t diag_edge)
  588. {
  589. int x, y;
  590. pixel *dst = (pixel *)_dst;
  591. pixel *src = (pixel *)_src;
  592. int chroma = !!c_idx;
  593. int *sao_offset_val = sao->offset_val[c_idx];
  594. int sao_eo_class = sao->eo_class[c_idx];
  595. int init_x = 0, init_y = 0, width = _width, height = _height;
  596. static const int8_t pos[4][2][2] = {
  597. { { -1, 0 }, { 1, 0 } }, // horizontal
  598. { { 0, -1 }, { 0, 1 } }, // vertical
  599. { { -1, -1 }, { 1, 1 } }, // 45 degree
  600. { { 1, -1 }, { -1, 1 } }, // 135 degree
  601. };
  602. static const uint8_t edge_idx[] = { 1, 2, 0, 3, 4 };
  603. #define CMP(a, b) ((a) > (b) ? 1 : ((a) == (b) ? 0 : -1))
  604. stride /= sizeof(pixel);
  605. init_y = -(4 >> chroma) - 2;
  606. init_x = -(8 >> chroma) - 2;
  607. width = (8 >> chroma) + 2;
  608. height = (4 >> chroma) + 2;
  609. dst = dst + (init_y * stride + init_x);
  610. src = src + (init_y * stride + init_x);
  611. init_y = init_x = 0;
  612. {
  613. ptrdiff_t y_stride = init_y * stride;
  614. int pos_0_0 = pos[sao_eo_class][0][0];
  615. int pos_0_1 = pos[sao_eo_class][0][1];
  616. int pos_1_0 = pos[sao_eo_class][1][0];
  617. int pos_1_1 = pos[sao_eo_class][1][1];
  618. ptrdiff_t y_stride_0_1 = (init_y + pos_0_1) * stride;
  619. ptrdiff_t y_stride_1_1 = (init_y + pos_1_1) * stride;
  620. for (y = init_y; y < height; y++) {
  621. for (x = init_x; x < width; x++) {
  622. int diff0 = CMP(src[x + y_stride], src[x + pos_0_0 + y_stride_0_1]);
  623. int diff1 = CMP(src[x + y_stride], src[x + pos_1_0 + y_stride_1_1]);
  624. int offset_val = edge_idx[2 + diff0 + diff1];
  625. dst[x + y_stride] = av_clip_pixel(src[x + y_stride] + sao_offset_val[offset_val]);
  626. }
  627. y_stride += stride;
  628. y_stride_0_1 += stride;
  629. y_stride_1_1 += stride;
  630. }
  631. }
  632. {
  633. // Restore pixels that can't be modified
  634. int save_lower_right = !diag_edge && sao_eo_class == SAO_EO_135D;
  635. if(vert_edge && sao_eo_class != SAO_EO_VERT)
  636. for(y = init_y; y< height-save_lower_right; y++)
  637. dst[y*stride+width-1] = src[y*stride+width-1];
  638. if(horiz_edge && sao_eo_class != SAO_EO_HORIZ)
  639. for(x = init_x; x<width-save_lower_right; x++)
  640. dst[(height-1)*stride+x] = src[(height-1)*stride+x];
  641. if(diag_edge && sao_eo_class == SAO_EO_135D)
  642. dst[stride*(height-1)+width-1] = src[stride*(height-1)+width-1];
  643. }
  644. #undef CMP
  645. }
  646. #undef SET
  647. #undef SCALE
  648. #undef TR_4
  649. #undef TR_8
  650. #undef TR_16
  651. #undef TR_32
  652. static av_always_inline void
  653. FUNC(put_hevc_qpel_pixels)(int16_t *dst, ptrdiff_t dststride,
  654. uint8_t *_src, ptrdiff_t _srcstride,
  655. int width, int height, int mx, int my,
  656. int16_t* mcbuffer)
  657. {
  658. int x, y;
  659. pixel *src = (pixel *)_src;
  660. ptrdiff_t srcstride = _srcstride / sizeof(pixel);
  661. dststride /= sizeof(*dst);
  662. for (y = 0; y < height; y++) {
  663. for (x = 0; x < width; x++)
  664. dst[x] = src[x] << (14 - BIT_DEPTH);
  665. src += srcstride;
  666. dst += dststride;
  667. }
  668. }
  669. #define QPEL_FILTER_1(src, stride) \
  670. (1 * -src[x - 3 * stride] + \
  671. 4 * src[x - 2 * stride] - \
  672. 10 * src[x - stride] + \
  673. 58 * src[x] + \
  674. 17 * src[x + stride] - \
  675. 5 * src[x + 2 * stride] + \
  676. 1 * src[x + 3 * stride])
  677. #define QPEL_FILTER_2(src, stride) \
  678. (1 * -src[x - 3 * stride] + \
  679. 4 * src[x - 2 * stride] - \
  680. 11 * src[x - stride] + \
  681. 40 * src[x] + \
  682. 40 * src[x + stride] - \
  683. 11 * src[x + 2 * stride] + \
  684. 4 * src[x + 3 * stride] - \
  685. 1 * src[x + 4 * stride])
  686. #define QPEL_FILTER_3(src, stride) \
  687. (1 * src[x - 2 * stride] - \
  688. 5 * src[x - stride] + \
  689. 17 * src[x] + \
  690. 58 * src[x + stride] - \
  691. 10 * src[x + 2 * stride] + \
  692. 4 * src[x + 3 * stride] - \
  693. 1 * src[x + 4 * stride])
  694. #define PUT_HEVC_QPEL_H(H) \
  695. static void FUNC(put_hevc_qpel_h ## H)(int16_t *dst, ptrdiff_t dststride, \
  696. uint8_t *_src, ptrdiff_t _srcstride, \
  697. int width, int height, \
  698. int16_t* mcbuffer) \
  699. { \
  700. int x, y; \
  701. pixel *src = (pixel*)_src; \
  702. ptrdiff_t srcstride = _srcstride / sizeof(pixel); \
  703. \
  704. dststride /= sizeof(*dst); \
  705. for (y = 0; y < height; y++) { \
  706. for (x = 0; x < width; x++) \
  707. dst[x] = QPEL_FILTER_ ## H(src, 1) >> (BIT_DEPTH - 8); \
  708. src += srcstride; \
  709. dst += dststride; \
  710. } \
  711. }
  712. #define PUT_HEVC_QPEL_V(V) \
  713. static void FUNC(put_hevc_qpel_v ## V)(int16_t *dst, ptrdiff_t dststride, \
  714. uint8_t *_src, ptrdiff_t _srcstride, \
  715. int width, int height, \
  716. int16_t* mcbuffer) \
  717. { \
  718. int x, y; \
  719. pixel *src = (pixel*)_src; \
  720. ptrdiff_t srcstride = _srcstride / sizeof(pixel); \
  721. \
  722. dststride /= sizeof(*dst); \
  723. for (y = 0; y < height; y++) { \
  724. for (x = 0; x < width; x++) \
  725. dst[x] = QPEL_FILTER_ ## V(src, srcstride) >> (BIT_DEPTH - 8); \
  726. src += srcstride; \
  727. dst += dststride; \
  728. } \
  729. }
  730. #define PUT_HEVC_QPEL_HV(H, V) \
  731. static void FUNC(put_hevc_qpel_h ## H ## v ## V)(int16_t *dst, \
  732. ptrdiff_t dststride, \
  733. uint8_t *_src, \
  734. ptrdiff_t _srcstride, \
  735. int width, int height, \
  736. int16_t* mcbuffer) \
  737. { \
  738. int x, y; \
  739. pixel *src = (pixel*)_src; \
  740. ptrdiff_t srcstride = _srcstride / sizeof(pixel); \
  741. \
  742. int16_t tmp_array[(MAX_PB_SIZE + 7) * MAX_PB_SIZE]; \
  743. int16_t *tmp = tmp_array; \
  744. \
  745. dststride /= sizeof(*dst); \
  746. src -= ff_hevc_qpel_extra_before[V] * srcstride; \
  747. \
  748. for (y = 0; y < height + ff_hevc_qpel_extra[V]; y++) { \
  749. for (x = 0; x < width; x++) \
  750. tmp[x] = QPEL_FILTER_ ## H(src, 1) >> (BIT_DEPTH - 8); \
  751. src += srcstride; \
  752. tmp += MAX_PB_SIZE; \
  753. } \
  754. \
  755. tmp = tmp_array + ff_hevc_qpel_extra_before[V] * MAX_PB_SIZE; \
  756. \
  757. for (y = 0; y < height; y++) { \
  758. for (x = 0; x < width; x++) \
  759. dst[x] = QPEL_FILTER_ ## V(tmp, MAX_PB_SIZE) >> 6; \
  760. tmp += MAX_PB_SIZE; \
  761. dst += dststride; \
  762. } \
  763. }
  764. PUT_HEVC_QPEL_H(1)
  765. PUT_HEVC_QPEL_H(2)
  766. PUT_HEVC_QPEL_H(3)
  767. PUT_HEVC_QPEL_V(1)
  768. PUT_HEVC_QPEL_V(2)
  769. PUT_HEVC_QPEL_V(3)
  770. PUT_HEVC_QPEL_HV(1, 1)
  771. PUT_HEVC_QPEL_HV(1, 2)
  772. PUT_HEVC_QPEL_HV(1, 3)
  773. PUT_HEVC_QPEL_HV(2, 1)
  774. PUT_HEVC_QPEL_HV(2, 2)
  775. PUT_HEVC_QPEL_HV(2, 3)
  776. PUT_HEVC_QPEL_HV(3, 1)
  777. PUT_HEVC_QPEL_HV(3, 2)
  778. PUT_HEVC_QPEL_HV(3, 3)
  779. #define QPEL(W) \
  780. static void FUNC(put_hevc_qpel_pixels_ ## W)(int16_t *dst, ptrdiff_t dststride, \
  781. uint8_t *src, ptrdiff_t srcstride, \
  782. int height, int mx, int my, \
  783. int16_t *mcbuffer) \
  784. { \
  785. FUNC(put_hevc_qpel_pixels)(dst, dststride, src, srcstride, W, height, \
  786. mx, my, mcbuffer); \
  787. } \
  788. \
  789. static void FUNC(put_hevc_qpel_h_ ## W)(int16_t *dst, ptrdiff_t dststride, \
  790. uint8_t *src, ptrdiff_t srcstride, \
  791. int height, int mx, int my, \
  792. int16_t *mcbuffer) \
  793. { \
  794. if (mx == 1) \
  795. FUNC(put_hevc_qpel_h1)(dst, dststride, src, srcstride, W, height, mcbuffer); \
  796. else if (mx == 2) \
  797. FUNC(put_hevc_qpel_h2)(dst, dststride, src, srcstride, W, height, mcbuffer); \
  798. else \
  799. FUNC(put_hevc_qpel_h3)(dst, dststride, src, srcstride, W, height, mcbuffer); \
  800. } \
  801. \
  802. static void FUNC(put_hevc_qpel_v_ ## W)(int16_t *dst, ptrdiff_t dststride, \
  803. uint8_t *src, ptrdiff_t srcstride, \
  804. int height, int mx, int my, \
  805. int16_t *mcbuffer) \
  806. { \
  807. if (my == 1) \
  808. FUNC(put_hevc_qpel_v1)(dst, dststride, src, srcstride, W, height, mcbuffer); \
  809. else if (my == 2) \
  810. FUNC(put_hevc_qpel_v2)(dst, dststride, src, srcstride, W, height, mcbuffer); \
  811. else \
  812. FUNC(put_hevc_qpel_v3)(dst, dststride, src, srcstride, W, height, mcbuffer); \
  813. } \
  814. \
  815. static void FUNC(put_hevc_qpel_hv_ ## W)(int16_t *dst, ptrdiff_t dststride, \
  816. uint8_t *src, ptrdiff_t srcstride, \
  817. int height, int mx, int my, \
  818. int16_t *mcbuffer) \
  819. { \
  820. if (my == 1) { \
  821. if (mx == 1) \
  822. FUNC(put_hevc_qpel_h1v1)(dst, dststride, src, srcstride, W, height, mcbuffer); \
  823. else if (mx == 2) \
  824. FUNC(put_hevc_qpel_h2v1)(dst, dststride, src, srcstride, W, height, mcbuffer); \
  825. else \
  826. FUNC(put_hevc_qpel_h3v1)(dst, dststride, src, srcstride, W, height, mcbuffer); \
  827. } else if (my == 2) { \
  828. if (mx == 1) \
  829. FUNC(put_hevc_qpel_h1v2)(dst, dststride, src, srcstride, W, height, mcbuffer); \
  830. else if (mx == 2) \
  831. FUNC(put_hevc_qpel_h2v2)(dst, dststride, src, srcstride, W, height, mcbuffer); \
  832. else \
  833. FUNC(put_hevc_qpel_h3v2)(dst, dststride, src, srcstride, W, height, mcbuffer); \
  834. } else { \
  835. if (mx == 1) \
  836. FUNC(put_hevc_qpel_h1v3)(dst, dststride, src, srcstride, W, height, mcbuffer); \
  837. else if (mx == 2) \
  838. FUNC(put_hevc_qpel_h2v3)(dst, dststride, src, srcstride, W, height, mcbuffer); \
  839. else \
  840. FUNC(put_hevc_qpel_h3v3)(dst, dststride, src, srcstride, W, height, mcbuffer); \
  841. } \
  842. }
  843. QPEL(64)
  844. QPEL(48)
  845. QPEL(32)
  846. QPEL(24)
  847. QPEL(16)
  848. QPEL(12)
  849. QPEL(8)
  850. QPEL(4)
  851. static inline void FUNC(put_hevc_epel_pixels)(int16_t *dst, ptrdiff_t dststride,
  852. uint8_t *_src, ptrdiff_t _srcstride,
  853. int width, int height, int mx, int my,
  854. int16_t* mcbuffer)
  855. {
  856. int x, y;
  857. pixel *src = (pixel *)_src;
  858. ptrdiff_t srcstride = _srcstride / sizeof(pixel);
  859. dststride /= sizeof(*dst);
  860. for (y = 0; y < height; y++) {
  861. for (x = 0; x < width; x++)
  862. dst[x] = src[x] << (14 - BIT_DEPTH);
  863. src += srcstride;
  864. dst += dststride;
  865. }
  866. }
  867. #define EPEL_FILTER(src, stride) \
  868. (filter_0 * src[x - stride] + \
  869. filter_1 * src[x] + \
  870. filter_2 * src[x + stride] + \
  871. filter_3 * src[x + 2 * stride])
  872. static inline void FUNC(put_hevc_epel_h)(int16_t *dst, ptrdiff_t dststride,
  873. uint8_t *_src, ptrdiff_t _srcstride,
  874. int width, int height, int mx, int my,
  875. int16_t* mcbuffer)
  876. {
  877. int x, y;
  878. pixel *src = (pixel *)_src;
  879. ptrdiff_t srcstride = _srcstride / sizeof(pixel);
  880. const int16_t *filter = ff_hevc_epel_coeffs[mx - 1];
  881. int8_t filter_0 = filter[0];
  882. int8_t filter_1 = filter[1];
  883. int8_t filter_2 = filter[2];
  884. int8_t filter_3 = filter[3];
  885. dststride /= sizeof(*dst);
  886. for (y = 0; y < height; y++) {
  887. for (x = 0; x < width; x++)
  888. dst[x] = EPEL_FILTER(src, 1) >> (BIT_DEPTH - 8);
  889. src += srcstride;
  890. dst += dststride;
  891. }
  892. }
  893. static inline void FUNC(put_hevc_epel_v)(int16_t *dst, ptrdiff_t dststride,
  894. uint8_t *_src, ptrdiff_t _srcstride,
  895. int width, int height, int mx, int my,
  896. int16_t* mcbuffer)
  897. {
  898. int x, y;
  899. pixel *src = (pixel *)_src;
  900. ptrdiff_t srcstride = _srcstride / sizeof(pixel);
  901. const int16_t *filter = ff_hevc_epel_coeffs[my - 1];
  902. int8_t filter_0 = filter[0];
  903. int8_t filter_1 = filter[1];
  904. int8_t filter_2 = filter[2];
  905. int8_t filter_3 = filter[3];
  906. dststride /= sizeof(*dst);
  907. for (y = 0; y < height; y++) {
  908. for (x = 0; x < width; x++)
  909. dst[x] = EPEL_FILTER(src, srcstride) >> (BIT_DEPTH - 8);
  910. src += srcstride;
  911. dst += dststride;
  912. }
  913. }
  914. static inline void FUNC(put_hevc_epel_hv)(int16_t *dst, ptrdiff_t dststride,
  915. uint8_t *_src, ptrdiff_t _srcstride,
  916. int width, int height, int mx, int my,
  917. int16_t* mcbuffer)
  918. {
  919. int x, y;
  920. pixel *src = (pixel *)_src;
  921. ptrdiff_t srcstride = _srcstride / sizeof(pixel);
  922. const int16_t *filter_h = ff_hevc_epel_coeffs[mx - 1];
  923. const int16_t *filter_v = ff_hevc_epel_coeffs[my - 1];
  924. int8_t filter_0 = filter_h[0];
  925. int8_t filter_1 = filter_h[1];
  926. int8_t filter_2 = filter_h[2];
  927. int8_t filter_3 = filter_h[3];
  928. int16_t tmp_array[(MAX_PB_SIZE + 3) * MAX_PB_SIZE];
  929. int16_t *tmp = tmp_array;
  930. dststride /= sizeof(*dst);
  931. src -= EPEL_EXTRA_BEFORE * srcstride;
  932. for (y = 0; y < height + EPEL_EXTRA; y++) {
  933. for (x = 0; x < width; x++)
  934. tmp[x] = EPEL_FILTER(src, 1) >> (BIT_DEPTH - 8);
  935. src += srcstride;
  936. tmp += MAX_PB_SIZE;
  937. }
  938. tmp = tmp_array + EPEL_EXTRA_BEFORE * MAX_PB_SIZE;
  939. filter_0 = filter_v[0];
  940. filter_1 = filter_v[1];
  941. filter_2 = filter_v[2];
  942. filter_3 = filter_v[3];
  943. for (y = 0; y < height; y++) {
  944. for (x = 0; x < width; x++)
  945. dst[x] = EPEL_FILTER(tmp, MAX_PB_SIZE) >> 6;
  946. tmp += MAX_PB_SIZE;
  947. dst += dststride;
  948. }
  949. }
  950. #define EPEL(W) \
  951. static void FUNC(put_hevc_epel_pixels_ ## W)(int16_t *dst, ptrdiff_t dststride, \
  952. uint8_t *src, ptrdiff_t srcstride, \
  953. int height, int mx, int my, \
  954. int16_t *mcbuffer) \
  955. { \
  956. FUNC(put_hevc_epel_pixels)(dst, dststride, src, srcstride, \
  957. W, height, mx, my, mcbuffer); \
  958. } \
  959. static void FUNC(put_hevc_epel_h_ ## W)(int16_t *dst, ptrdiff_t dststride, \
  960. uint8_t *src, ptrdiff_t srcstride, \
  961. int height, int mx, int my, \
  962. int16_t *mcbuffer) \
  963. { \
  964. FUNC(put_hevc_epel_h)(dst, dststride, src, srcstride, \
  965. W, height, mx, my, mcbuffer); \
  966. } \
  967. static void FUNC(put_hevc_epel_v_ ## W)(int16_t *dst, ptrdiff_t dststride, \
  968. uint8_t *src, ptrdiff_t srcstride, \
  969. int height, int mx, int my, \
  970. int16_t *mcbuffer) \
  971. { \
  972. FUNC(put_hevc_epel_v)(dst, dststride, src, srcstride, \
  973. W, height, mx, my, mcbuffer); \
  974. } \
  975. static void FUNC(put_hevc_epel_hv_ ## W)(int16_t *dst, ptrdiff_t dststride, \
  976. uint8_t *src, ptrdiff_t srcstride, \
  977. int height, int mx, int my, \
  978. int16_t *mcbuffer) \
  979. { \
  980. FUNC(put_hevc_epel_hv)(dst, dststride, src, srcstride, \
  981. W, height, mx, my, mcbuffer); \
  982. }
  983. EPEL(32)
  984. EPEL(24)
  985. EPEL(16)
  986. EPEL(12)
  987. EPEL(8)
  988. EPEL(6)
  989. EPEL(4)
  990. EPEL(2)
  991. static av_always_inline void
  992. FUNC(put_unweighted_pred)(uint8_t *_dst, ptrdiff_t _dststride,
  993. int16_t *src, ptrdiff_t srcstride,
  994. int width, int height)
  995. {
  996. int x, y;
  997. pixel *dst = (pixel *)_dst;
  998. ptrdiff_t dststride = _dststride / sizeof(pixel);
  999. int shift = 14 - BIT_DEPTH;
  1000. #if BIT_DEPTH < 14
  1001. int offset = 1 << (shift - 1);
  1002. #else
  1003. int offset = 0;
  1004. #endif
  1005. srcstride /= sizeof(*src);
  1006. for (y = 0; y < height; y++) {
  1007. for (x = 0; x < width; x++)
  1008. dst[x] = av_clip_pixel((src[x] + offset) >> shift);
  1009. dst += dststride;
  1010. src += srcstride;
  1011. }
  1012. }
  1013. static av_always_inline void
  1014. FUNC(put_unweighted_pred_avg)(uint8_t *_dst, ptrdiff_t _dststride,
  1015. int16_t *src1, int16_t *src2,
  1016. ptrdiff_t srcstride,
  1017. int width, int height)
  1018. {
  1019. int x, y;
  1020. pixel *dst = (pixel *)_dst;
  1021. ptrdiff_t dststride = _dststride / sizeof(pixel);
  1022. int shift = 14 + 1 - BIT_DEPTH;
  1023. #if BIT_DEPTH < 14
  1024. int offset = 1 << (shift - 1);
  1025. #else
  1026. int offset = 0;
  1027. #endif
  1028. srcstride /= sizeof(*src1);
  1029. for (y = 0; y < height; y++) {
  1030. for (x = 0; x < width; x++)
  1031. dst[x] = av_clip_pixel((src1[x] + src2[x] + offset) >> shift);
  1032. dst += dststride;
  1033. src1 += srcstride;
  1034. src2 += srcstride;
  1035. }
  1036. }
  1037. static av_always_inline void
  1038. FUNC(weighted_pred)(uint8_t denom, int16_t wlxFlag, int16_t olxFlag,
  1039. uint8_t *_dst, ptrdiff_t _dststride,
  1040. int16_t *src, ptrdiff_t srcstride,
  1041. int width, int height)
  1042. {
  1043. int shift, log2Wd, wx, ox, x, y, offset;
  1044. pixel *dst = (pixel *)_dst;
  1045. ptrdiff_t dststride = _dststride / sizeof(pixel);
  1046. shift = 14 - BIT_DEPTH;
  1047. log2Wd = denom + shift;
  1048. offset = 1 << (log2Wd - 1);
  1049. wx = wlxFlag;
  1050. ox = olxFlag * (1 << (BIT_DEPTH - 8));
  1051. srcstride /= sizeof(*src);
  1052. for (y = 0; y < height; y++) {
  1053. for (x = 0; x < width; x++) {
  1054. if (log2Wd >= 1) {
  1055. dst[x] = av_clip_pixel(((src[x] * wx + offset) >> log2Wd) + ox);
  1056. } else {
  1057. dst[x] = av_clip_pixel(src[x] * wx + ox);
  1058. }
  1059. }
  1060. dst += dststride;
  1061. src += srcstride;
  1062. }
  1063. }
  1064. static av_always_inline void
  1065. FUNC(weighted_pred_avg)(uint8_t denom,
  1066. int16_t wl0Flag, int16_t wl1Flag,
  1067. int16_t ol0Flag, int16_t ol1Flag,
  1068. uint8_t *_dst, ptrdiff_t _dststride,
  1069. int16_t *src1, int16_t *src2,
  1070. ptrdiff_t srcstride,
  1071. int width, int height)
  1072. {
  1073. int shift, log2Wd, w0, w1, o0, o1, x, y;
  1074. pixel *dst = (pixel *)_dst;
  1075. ptrdiff_t dststride = _dststride / sizeof(pixel);
  1076. shift = 14 - BIT_DEPTH;
  1077. log2Wd = denom + shift;
  1078. w0 = wl0Flag;
  1079. w1 = wl1Flag;
  1080. o0 = ol0Flag * (1 << (BIT_DEPTH - 8));
  1081. o1 = ol1Flag * (1 << (BIT_DEPTH - 8));
  1082. srcstride /= sizeof(*src1);
  1083. for (y = 0; y < height; y++) {
  1084. for (x = 0; x < width; x++)
  1085. dst[x] = av_clip_pixel((src1[x] * w0 + src2[x] * w1 +
  1086. ((o0 + o1 + 1) << log2Wd)) >> (log2Wd + 1));
  1087. dst += dststride;
  1088. src1 += srcstride;
  1089. src2 += srcstride;
  1090. }
  1091. }
  1092. #define PUT_PRED(w) \
  1093. static void FUNC(put_unweighted_pred_ ## w)(uint8_t *dst, ptrdiff_t dststride, \
  1094. int16_t *src, ptrdiff_t srcstride, \
  1095. int height) \
  1096. { \
  1097. FUNC(put_unweighted_pred)(dst, dststride, src, srcstride, w, height); \
  1098. } \
  1099. static void FUNC(put_unweighted_pred_avg_ ## w)(uint8_t *dst, ptrdiff_t dststride, \
  1100. int16_t *src1, int16_t *src2, \
  1101. ptrdiff_t srcstride, int height) \
  1102. { \
  1103. FUNC(put_unweighted_pred_avg)(dst, dststride, src1, src2, srcstride, w, height); \
  1104. } \
  1105. static void FUNC(put_weighted_pred_ ## w)(uint8_t denom, int16_t weight, int16_t offset, \
  1106. uint8_t *dst, ptrdiff_t dststride, \
  1107. int16_t *src, ptrdiff_t srcstride, int height) \
  1108. { \
  1109. FUNC(weighted_pred)(denom, weight, offset, \
  1110. dst, dststride, src, srcstride, w, height); \
  1111. } \
  1112. static void FUNC(put_weighted_pred_avg_ ## w)(uint8_t denom, int16_t weight0, int16_t weight1, \
  1113. int16_t offset0, int16_t offset1, \
  1114. uint8_t *dst, ptrdiff_t dststride, \
  1115. int16_t *src1, int16_t *src2, \
  1116. ptrdiff_t srcstride, int height) \
  1117. { \
  1118. FUNC(weighted_pred_avg)(denom, weight0, weight1, offset0, offset1, \
  1119. dst, dststride, src1, src2, srcstride, w, height); \
  1120. }
  1121. PUT_PRED(64)
  1122. PUT_PRED(48)
  1123. PUT_PRED(32)
  1124. PUT_PRED(24)
  1125. PUT_PRED(16)
  1126. PUT_PRED(12)
  1127. PUT_PRED(8)
  1128. PUT_PRED(6)
  1129. PUT_PRED(4)
  1130. PUT_PRED(2)
  1131. // line zero
  1132. #define P3 pix[-4 * xstride]
  1133. #define P2 pix[-3 * xstride]
  1134. #define P1 pix[-2 * xstride]
  1135. #define P0 pix[-1 * xstride]
  1136. #define Q0 pix[0 * xstride]
  1137. #define Q1 pix[1 * xstride]
  1138. #define Q2 pix[2 * xstride]
  1139. #define Q3 pix[3 * xstride]
  1140. // line three. used only for deblocking decision
  1141. #define TP3 pix[-4 * xstride + 3 * ystride]
  1142. #define TP2 pix[-3 * xstride + 3 * ystride]
  1143. #define TP1 pix[-2 * xstride + 3 * ystride]
  1144. #define TP0 pix[-1 * xstride + 3 * ystride]
  1145. #define TQ0 pix[0 * xstride + 3 * ystride]
  1146. #define TQ1 pix[1 * xstride + 3 * ystride]
  1147. #define TQ2 pix[2 * xstride + 3 * ystride]
  1148. #define TQ3 pix[3 * xstride + 3 * ystride]
  1149. static void FUNC(hevc_loop_filter_luma)(uint8_t *_pix,
  1150. ptrdiff_t _xstride, ptrdiff_t _ystride,
  1151. int beta, int *_tc,
  1152. uint8_t *_no_p, uint8_t *_no_q)
  1153. {
  1154. int d, j;
  1155. pixel *pix = (pixel *)_pix;
  1156. ptrdiff_t xstride = _xstride / sizeof(pixel);
  1157. ptrdiff_t ystride = _ystride / sizeof(pixel);
  1158. beta <<= BIT_DEPTH - 8;
  1159. for (j = 0; j < 2; j++) {
  1160. const int dp0 = abs(P2 - 2 * P1 + P0);
  1161. const int dq0 = abs(Q2 - 2 * Q1 + Q0);
  1162. const int dp3 = abs(TP2 - 2 * TP1 + TP0);
  1163. const int dq3 = abs(TQ2 - 2 * TQ1 + TQ0);
  1164. const int d0 = dp0 + dq0;
  1165. const int d3 = dp3 + dq3;
  1166. const int tc = _tc[j] << (BIT_DEPTH - 8);
  1167. const int no_p = _no_p[j];
  1168. const int no_q = _no_q[j];
  1169. if (d0 + d3 >= beta) {
  1170. pix += 4 * ystride;
  1171. continue;
  1172. } else {
  1173. const int beta_3 = beta >> 3;
  1174. const int beta_2 = beta >> 2;
  1175. const int tc25 = ((tc * 5 + 1) >> 1);
  1176. if (abs(P3 - P0) + abs(Q3 - Q0) < beta_3 && abs(P0 - Q0) < tc25 &&
  1177. abs(TP3 - TP0) + abs(TQ3 - TQ0) < beta_3 && abs(TP0 - TQ0) < tc25 &&
  1178. (d0 << 1) < beta_2 && (d3 << 1) < beta_2) {
  1179. // strong filtering
  1180. const int tc2 = tc << 1;
  1181. for (d = 0; d < 4; d++) {
  1182. const int p3 = P3;
  1183. const int p2 = P2;
  1184. const int p1 = P1;
  1185. const int p0 = P0;
  1186. const int q0 = Q0;
  1187. const int q1 = Q1;
  1188. const int q2 = Q2;
  1189. const int q3 = Q3;
  1190. if (!no_p) {
  1191. P0 = p0 + av_clip(((p2 + 2 * p1 + 2 * p0 + 2 * q0 + q1 + 4) >> 3) - p0, -tc2, tc2);
  1192. P1 = p1 + av_clip(((p2 + p1 + p0 + q0 + 2) >> 2) - p1, -tc2, tc2);
  1193. P2 = p2 + av_clip(((2 * p3 + 3 * p2 + p1 + p0 + q0 + 4) >> 3) - p2, -tc2, tc2);
  1194. }
  1195. if (!no_q) {
  1196. Q0 = q0 + av_clip(((p1 + 2 * p0 + 2 * q0 + 2 * q1 + q2 + 4) >> 3) - q0, -tc2, tc2);
  1197. Q1 = q1 + av_clip(((p0 + q0 + q1 + q2 + 2) >> 2) - q1, -tc2, tc2);
  1198. Q2 = q2 + av_clip(((2 * q3 + 3 * q2 + q1 + q0 + p0 + 4) >> 3) - q2, -tc2, tc2);
  1199. }
  1200. pix += ystride;
  1201. }
  1202. } else { // normal filtering
  1203. int nd_p = 1;
  1204. int nd_q = 1;
  1205. const int tc_2 = tc >> 1;
  1206. if (dp0 + dp3 < ((beta + (beta >> 1)) >> 3))
  1207. nd_p = 2;
  1208. if (dq0 + dq3 < ((beta + (beta >> 1)) >> 3))
  1209. nd_q = 2;
  1210. for (d = 0; d < 4; d++) {
  1211. const int p2 = P2;
  1212. const int p1 = P1;
  1213. const int p0 = P0;
  1214. const int q0 = Q0;
  1215. const int q1 = Q1;
  1216. const int q2 = Q2;
  1217. int delta0 = (9 * (q0 - p0) - 3 * (q1 - p1) + 8) >> 4;
  1218. if (abs(delta0) < 10 * tc) {
  1219. delta0 = av_clip(delta0, -tc, tc);
  1220. if (!no_p)
  1221. P0 = av_clip_pixel(p0 + delta0);
  1222. if (!no_q)
  1223. Q0 = av_clip_pixel(q0 - delta0);
  1224. if (!no_p && nd_p > 1) {
  1225. const int deltap1 = av_clip((((p2 + p0 + 1) >> 1) - p1 + delta0) >> 1, -tc_2, tc_2);
  1226. P1 = av_clip_pixel(p1 + deltap1);
  1227. }
  1228. if (!no_q && nd_q > 1) {
  1229. const int deltaq1 = av_clip((((q2 + q0 + 1) >> 1) - q1 - delta0) >> 1, -tc_2, tc_2);
  1230. Q1 = av_clip_pixel(q1 + deltaq1);
  1231. }
  1232. }
  1233. pix += ystride;
  1234. }
  1235. }
  1236. }
  1237. }
  1238. }
  1239. static void FUNC(hevc_loop_filter_chroma)(uint8_t *_pix, ptrdiff_t _xstride,
  1240. ptrdiff_t _ystride, int *_tc,
  1241. uint8_t *_no_p, uint8_t *_no_q)
  1242. {
  1243. int d, j, no_p, no_q;
  1244. pixel *pix = (pixel *)_pix;
  1245. ptrdiff_t xstride = _xstride / sizeof(pixel);
  1246. ptrdiff_t ystride = _ystride / sizeof(pixel);
  1247. for (j = 0; j < 2; j++) {
  1248. const int tc = _tc[j] << (BIT_DEPTH - 8);
  1249. if (tc <= 0) {
  1250. pix += 4 * ystride;
  1251. continue;
  1252. }
  1253. no_p = _no_p[j];
  1254. no_q = _no_q[j];
  1255. for (d = 0; d < 4; d++) {
  1256. int delta0;
  1257. const int p1 = P1;
  1258. const int p0 = P0;
  1259. const int q0 = Q0;
  1260. const int q1 = Q1;
  1261. delta0 = av_clip((((q0 - p0) * 4) + p1 - q1 + 4) >> 3, -tc, tc);
  1262. if (!no_p)
  1263. P0 = av_clip_pixel(p0 + delta0);
  1264. if (!no_q)
  1265. Q0 = av_clip_pixel(q0 - delta0);
  1266. pix += ystride;
  1267. }
  1268. }
  1269. }
  1270. static void FUNC(hevc_h_loop_filter_chroma)(uint8_t *pix, ptrdiff_t stride,
  1271. int *tc, uint8_t *no_p,
  1272. uint8_t *no_q)
  1273. {
  1274. FUNC(hevc_loop_filter_chroma)(pix, stride, sizeof(pixel), tc, no_p, no_q);
  1275. }
  1276. static void FUNC(hevc_v_loop_filter_chroma)(uint8_t *pix, ptrdiff_t stride,
  1277. int *tc, uint8_t *no_p,
  1278. uint8_t *no_q)
  1279. {
  1280. FUNC(hevc_loop_filter_chroma)(pix, sizeof(pixel), stride, tc, no_p, no_q);
  1281. }
  1282. static void FUNC(hevc_h_loop_filter_luma)(uint8_t *pix, ptrdiff_t stride,
  1283. int beta, int *tc, uint8_t *no_p,
  1284. uint8_t *no_q)
  1285. {
  1286. FUNC(hevc_loop_filter_luma)(pix, stride, sizeof(pixel),
  1287. beta, tc, no_p, no_q);
  1288. }
  1289. static void FUNC(hevc_v_loop_filter_luma)(uint8_t *pix, ptrdiff_t stride,
  1290. int beta, int *tc, uint8_t *no_p,
  1291. uint8_t *no_q)
  1292. {
  1293. FUNC(hevc_loop_filter_luma)(pix, sizeof(pixel), stride,
  1294. beta, tc, no_p, no_q);
  1295. }
  1296. #undef P3
  1297. #undef P2
  1298. #undef P1
  1299. #undef P0
  1300. #undef Q0
  1301. #undef Q1
  1302. #undef Q2
  1303. #undef Q3
  1304. #undef TP3
  1305. #undef TP2
  1306. #undef TP1
  1307. #undef TP0
  1308. #undef TQ0
  1309. #undef TQ1
  1310. #undef TQ2
  1311. #undef TQ3