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.

1315 lines
48KB

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