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.

1341 lines
49KB

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