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.

1343 lines
49KB

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