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.

1429 lines
59KB

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