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.

1491 lines
60KB

  1. /*
  2. * HEVC video decoder
  3. *
  4. * Copyright (C) 2012 - 2013 Guillaume Martres
  5. *
  6. * This file is part of Libav.
  7. *
  8. * Libav is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * Libav is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with Libav; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #include "get_bits.h"
  23. #include "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 av_always_inline void
  689. FUNC(put_hevc_qpel_pixels)(int16_t *dst, ptrdiff_t dststride,
  690. uint8_t *_src, ptrdiff_t _srcstride,
  691. int width, int height, int mx, int my,
  692. int16_t* mcbuffer)
  693. {
  694. int x, y;
  695. pixel *src = (pixel *)_src;
  696. ptrdiff_t srcstride = _srcstride / sizeof(pixel);
  697. dststride /= sizeof(*dst);
  698. for (y = 0; y < height; y++) {
  699. for (x = 0; x < width; x++)
  700. dst[x] = src[x] << (14 - BIT_DEPTH);
  701. src += srcstride;
  702. dst += dststride;
  703. }
  704. }
  705. #define QPEL_FILTER_1(src, stride) \
  706. (1 * -src[x - 3 * stride] + \
  707. 4 * src[x - 2 * stride] - \
  708. 10 * src[x - stride] + \
  709. 58 * src[x] + \
  710. 17 * src[x + stride] - \
  711. 5 * src[x + 2 * stride] + \
  712. 1 * src[x + 3 * stride])
  713. #define QPEL_FILTER_2(src, stride) \
  714. (1 * -src[x - 3 * stride] + \
  715. 4 * src[x - 2 * stride] - \
  716. 11 * src[x - stride] + \
  717. 40 * src[x] + \
  718. 40 * src[x + stride] - \
  719. 11 * src[x + 2 * stride] + \
  720. 4 * src[x + 3 * stride] - \
  721. 1 * src[x + 4 * stride])
  722. #define QPEL_FILTER_3(src, stride) \
  723. (1 * src[x - 2 * stride] - \
  724. 5 * src[x - stride] + \
  725. 17 * src[x] + \
  726. 58 * src[x + stride] - \
  727. 10 * src[x + 2 * stride] + \
  728. 4 * src[x + 3 * stride] - \
  729. 1 * src[x + 4 * stride])
  730. #define PUT_HEVC_QPEL_H(H) \
  731. static void FUNC(put_hevc_qpel_h ## H)(int16_t *dst, ptrdiff_t dststride, \
  732. uint8_t *_src, ptrdiff_t _srcstride, \
  733. int width, int height, \
  734. int16_t* mcbuffer) \
  735. { \
  736. int x, y; \
  737. pixel *src = (pixel*)_src; \
  738. ptrdiff_t srcstride = _srcstride / sizeof(pixel); \
  739. \
  740. dststride /= sizeof(*dst); \
  741. for (y = 0; y < height; y++) { \
  742. for (x = 0; x < width; x++) \
  743. dst[x] = QPEL_FILTER_ ## H(src, 1) >> (BIT_DEPTH - 8); \
  744. src += srcstride; \
  745. dst += dststride; \
  746. } \
  747. }
  748. #define PUT_HEVC_QPEL_V(V) \
  749. static void FUNC(put_hevc_qpel_v ## V)(int16_t *dst, ptrdiff_t dststride, \
  750. uint8_t *_src, ptrdiff_t _srcstride, \
  751. int width, int height, \
  752. int16_t* mcbuffer) \
  753. { \
  754. int x, y; \
  755. pixel *src = (pixel*)_src; \
  756. ptrdiff_t srcstride = _srcstride / sizeof(pixel); \
  757. \
  758. dststride /= sizeof(*dst); \
  759. for (y = 0; y < height; y++) { \
  760. for (x = 0; x < width; x++) \
  761. dst[x] = QPEL_FILTER_ ## V(src, srcstride) >> (BIT_DEPTH - 8); \
  762. src += srcstride; \
  763. dst += dststride; \
  764. } \
  765. }
  766. #define PUT_HEVC_QPEL_HV(H, V) \
  767. static void FUNC(put_hevc_qpel_h ## H ## v ## V)(int16_t *dst, \
  768. ptrdiff_t dststride, \
  769. uint8_t *_src, \
  770. ptrdiff_t _srcstride, \
  771. int width, int height, \
  772. int16_t* mcbuffer) \
  773. { \
  774. int x, y; \
  775. pixel *src = (pixel*)_src; \
  776. ptrdiff_t srcstride = _srcstride / sizeof(pixel); \
  777. \
  778. int16_t tmp_array[(MAX_PB_SIZE + 7) * MAX_PB_SIZE]; \
  779. int16_t *tmp = tmp_array; \
  780. \
  781. dststride /= sizeof(*dst); \
  782. src -= ff_hevc_qpel_extra_before[V] * srcstride; \
  783. \
  784. for (y = 0; y < height + ff_hevc_qpel_extra[V]; y++) { \
  785. for (x = 0; x < width; x++) \
  786. tmp[x] = QPEL_FILTER_ ## H(src, 1) >> (BIT_DEPTH - 8); \
  787. src += srcstride; \
  788. tmp += MAX_PB_SIZE; \
  789. } \
  790. \
  791. tmp = tmp_array + ff_hevc_qpel_extra_before[V] * MAX_PB_SIZE; \
  792. \
  793. for (y = 0; y < height; y++) { \
  794. for (x = 0; x < width; x++) \
  795. dst[x] = QPEL_FILTER_ ## V(tmp, MAX_PB_SIZE) >> 6; \
  796. tmp += MAX_PB_SIZE; \
  797. dst += dststride; \
  798. } \
  799. }
  800. PUT_HEVC_QPEL_H(1)
  801. PUT_HEVC_QPEL_H(2)
  802. PUT_HEVC_QPEL_H(3)
  803. PUT_HEVC_QPEL_V(1)
  804. PUT_HEVC_QPEL_V(2)
  805. PUT_HEVC_QPEL_V(3)
  806. PUT_HEVC_QPEL_HV(1, 1)
  807. PUT_HEVC_QPEL_HV(1, 2)
  808. PUT_HEVC_QPEL_HV(1, 3)
  809. PUT_HEVC_QPEL_HV(2, 1)
  810. PUT_HEVC_QPEL_HV(2, 2)
  811. PUT_HEVC_QPEL_HV(2, 3)
  812. PUT_HEVC_QPEL_HV(3, 1)
  813. PUT_HEVC_QPEL_HV(3, 2)
  814. PUT_HEVC_QPEL_HV(3, 3)
  815. #define QPEL(W) \
  816. static void FUNC(put_hevc_qpel_pixels_ ## W)(int16_t *dst, ptrdiff_t dststride, \
  817. uint8_t *src, ptrdiff_t srcstride, \
  818. int height, int mx, int my, \
  819. int16_t *mcbuffer) \
  820. { \
  821. FUNC(put_hevc_qpel_pixels)(dst, dststride, src, srcstride, W, height, \
  822. mx, my, mcbuffer); \
  823. } \
  824. \
  825. static void FUNC(put_hevc_qpel_h_ ## W)(int16_t *dst, ptrdiff_t dststride, \
  826. uint8_t *src, ptrdiff_t srcstride, \
  827. int height, int mx, int my, \
  828. int16_t *mcbuffer) \
  829. { \
  830. if (mx == 1) \
  831. FUNC(put_hevc_qpel_h1)(dst, dststride, src, srcstride, W, height, mcbuffer); \
  832. else if (mx == 2) \
  833. FUNC(put_hevc_qpel_h2)(dst, dststride, src, srcstride, W, height, mcbuffer); \
  834. else \
  835. FUNC(put_hevc_qpel_h3)(dst, dststride, src, srcstride, W, height, mcbuffer); \
  836. } \
  837. \
  838. static void FUNC(put_hevc_qpel_v_ ## W)(int16_t *dst, ptrdiff_t dststride, \
  839. uint8_t *src, ptrdiff_t srcstride, \
  840. int height, int mx, int my, \
  841. int16_t *mcbuffer) \
  842. { \
  843. if (my == 1) \
  844. FUNC(put_hevc_qpel_v1)(dst, dststride, src, srcstride, W, height, mcbuffer); \
  845. else if (my == 2) \
  846. FUNC(put_hevc_qpel_v2)(dst, dststride, src, srcstride, W, height, mcbuffer); \
  847. else \
  848. FUNC(put_hevc_qpel_v3)(dst, dststride, src, srcstride, W, height, mcbuffer); \
  849. } \
  850. \
  851. static void FUNC(put_hevc_qpel_hv_ ## W)(int16_t *dst, ptrdiff_t dststride, \
  852. uint8_t *src, ptrdiff_t srcstride, \
  853. int height, int mx, int my, \
  854. int16_t *mcbuffer) \
  855. { \
  856. if (my == 1) { \
  857. if (mx == 1) \
  858. FUNC(put_hevc_qpel_h1v1)(dst, dststride, src, srcstride, W, height, mcbuffer); \
  859. else if (mx == 2) \
  860. FUNC(put_hevc_qpel_h2v1)(dst, dststride, src, srcstride, W, height, mcbuffer); \
  861. else \
  862. FUNC(put_hevc_qpel_h3v1)(dst, dststride, src, srcstride, W, height, mcbuffer); \
  863. } else if (my == 2) { \
  864. if (mx == 1) \
  865. FUNC(put_hevc_qpel_h1v2)(dst, dststride, src, srcstride, W, height, mcbuffer); \
  866. else if (mx == 2) \
  867. FUNC(put_hevc_qpel_h2v2)(dst, dststride, src, srcstride, W, height, mcbuffer); \
  868. else \
  869. FUNC(put_hevc_qpel_h3v2)(dst, dststride, src, srcstride, W, height, mcbuffer); \
  870. } else { \
  871. if (mx == 1) \
  872. FUNC(put_hevc_qpel_h1v3)(dst, dststride, src, srcstride, W, height, mcbuffer); \
  873. else if (mx == 2) \
  874. FUNC(put_hevc_qpel_h2v3)(dst, dststride, src, srcstride, W, height, mcbuffer); \
  875. else \
  876. FUNC(put_hevc_qpel_h3v3)(dst, dststride, src, srcstride, W, height, mcbuffer); \
  877. } \
  878. }
  879. QPEL(64)
  880. QPEL(48)
  881. QPEL(32)
  882. QPEL(24)
  883. QPEL(16)
  884. QPEL(12)
  885. QPEL(8)
  886. QPEL(4)
  887. static inline void FUNC(put_hevc_epel_pixels)(int16_t *dst, ptrdiff_t dststride,
  888. uint8_t *_src, ptrdiff_t _srcstride,
  889. int width, int height, int mx, int my,
  890. int16_t* mcbuffer)
  891. {
  892. int x, y;
  893. pixel *src = (pixel *)_src;
  894. ptrdiff_t srcstride = _srcstride / sizeof(pixel);
  895. dststride /= sizeof(*dst);
  896. for (y = 0; y < height; y++) {
  897. for (x = 0; x < width; x++)
  898. dst[x] = src[x] << (14 - BIT_DEPTH);
  899. src += srcstride;
  900. dst += dststride;
  901. }
  902. }
  903. #define EPEL_FILTER(src, stride) \
  904. (filter_0 * src[x - stride] + \
  905. filter_1 * src[x] + \
  906. filter_2 * src[x + stride] + \
  907. filter_3 * src[x + 2 * stride])
  908. static inline void FUNC(put_hevc_epel_h)(int16_t *dst, ptrdiff_t dststride,
  909. uint8_t *_src, ptrdiff_t _srcstride,
  910. int width, int height, int mx, int my,
  911. int16_t* mcbuffer)
  912. {
  913. int x, y;
  914. pixel *src = (pixel *)_src;
  915. ptrdiff_t srcstride = _srcstride / sizeof(pixel);
  916. const int16_t *filter = ff_hevc_epel_coeffs[mx - 1];
  917. int8_t filter_0 = filter[0];
  918. int8_t filter_1 = filter[1];
  919. int8_t filter_2 = filter[2];
  920. int8_t filter_3 = filter[3];
  921. dststride /= sizeof(*dst);
  922. for (y = 0; y < height; y++) {
  923. for (x = 0; x < width; x++)
  924. dst[x] = EPEL_FILTER(src, 1) >> (BIT_DEPTH - 8);
  925. src += srcstride;
  926. dst += dststride;
  927. }
  928. }
  929. static inline void FUNC(put_hevc_epel_v)(int16_t *dst, ptrdiff_t dststride,
  930. uint8_t *_src, ptrdiff_t _srcstride,
  931. int width, int height, int mx, int my,
  932. int16_t* mcbuffer)
  933. {
  934. int x, y;
  935. pixel *src = (pixel *)_src;
  936. ptrdiff_t srcstride = _srcstride / sizeof(pixel);
  937. const int16_t *filter = ff_hevc_epel_coeffs[my - 1];
  938. int8_t filter_0 = filter[0];
  939. int8_t filter_1 = filter[1];
  940. int8_t filter_2 = filter[2];
  941. int8_t filter_3 = filter[3];
  942. dststride /= sizeof(*dst);
  943. for (y = 0; y < height; y++) {
  944. for (x = 0; x < width; x++)
  945. dst[x] = EPEL_FILTER(src, srcstride) >> (BIT_DEPTH - 8);
  946. src += srcstride;
  947. dst += dststride;
  948. }
  949. }
  950. static inline void FUNC(put_hevc_epel_hv)(int16_t *dst, ptrdiff_t dststride,
  951. uint8_t *_src, ptrdiff_t _srcstride,
  952. int width, int height, int mx, int my,
  953. int16_t* mcbuffer)
  954. {
  955. int x, y;
  956. pixel *src = (pixel *)_src;
  957. ptrdiff_t srcstride = _srcstride / sizeof(pixel);
  958. const int16_t *filter_h = ff_hevc_epel_coeffs[mx - 1];
  959. const int16_t *filter_v = ff_hevc_epel_coeffs[my - 1];
  960. int8_t filter_0 = filter_h[0];
  961. int8_t filter_1 = filter_h[1];
  962. int8_t filter_2 = filter_h[2];
  963. int8_t filter_3 = filter_h[3];
  964. int16_t tmp_array[(MAX_PB_SIZE + 3) * MAX_PB_SIZE];
  965. int16_t *tmp = tmp_array;
  966. dststride /= sizeof(*dst);
  967. src -= EPEL_EXTRA_BEFORE * srcstride;
  968. for (y = 0; y < height + EPEL_EXTRA; y++) {
  969. for (x = 0; x < width; x++)
  970. tmp[x] = EPEL_FILTER(src, 1) >> (BIT_DEPTH - 8);
  971. src += srcstride;
  972. tmp += MAX_PB_SIZE;
  973. }
  974. tmp = tmp_array + EPEL_EXTRA_BEFORE * MAX_PB_SIZE;
  975. filter_0 = filter_v[0];
  976. filter_1 = filter_v[1];
  977. filter_2 = filter_v[2];
  978. filter_3 = filter_v[3];
  979. for (y = 0; y < height; y++) {
  980. for (x = 0; x < width; x++)
  981. dst[x] = EPEL_FILTER(tmp, MAX_PB_SIZE) >> 6;
  982. tmp += MAX_PB_SIZE;
  983. dst += dststride;
  984. }
  985. }
  986. #define EPEL(W) \
  987. static void FUNC(put_hevc_epel_pixels_ ## W)(int16_t *dst, ptrdiff_t dststride, \
  988. uint8_t *src, ptrdiff_t srcstride, \
  989. int height, int mx, int my, \
  990. int16_t *mcbuffer) \
  991. { \
  992. FUNC(put_hevc_epel_pixels)(dst, dststride, src, srcstride, \
  993. W, height, mx, my, mcbuffer); \
  994. } \
  995. static void FUNC(put_hevc_epel_h_ ## W)(int16_t *dst, ptrdiff_t dststride, \
  996. uint8_t *src, ptrdiff_t srcstride, \
  997. int height, int mx, int my, \
  998. int16_t *mcbuffer) \
  999. { \
  1000. FUNC(put_hevc_epel_h)(dst, dststride, src, srcstride, \
  1001. W, height, mx, my, mcbuffer); \
  1002. } \
  1003. static void FUNC(put_hevc_epel_v_ ## W)(int16_t *dst, ptrdiff_t dststride, \
  1004. uint8_t *src, ptrdiff_t srcstride, \
  1005. int height, int mx, int my, \
  1006. int16_t *mcbuffer) \
  1007. { \
  1008. FUNC(put_hevc_epel_v)(dst, dststride, src, srcstride, \
  1009. W, height, mx, my, mcbuffer); \
  1010. } \
  1011. static void FUNC(put_hevc_epel_hv_ ## W)(int16_t *dst, ptrdiff_t dststride, \
  1012. uint8_t *src, ptrdiff_t srcstride, \
  1013. int height, int mx, int my, \
  1014. int16_t *mcbuffer) \
  1015. { \
  1016. FUNC(put_hevc_epel_hv)(dst, dststride, src, srcstride, \
  1017. W, height, mx, my, mcbuffer); \
  1018. }
  1019. EPEL(32)
  1020. EPEL(24)
  1021. EPEL(16)
  1022. EPEL(12)
  1023. EPEL(8)
  1024. EPEL(6)
  1025. EPEL(4)
  1026. EPEL(2)
  1027. static av_always_inline void
  1028. FUNC(put_unweighted_pred)(uint8_t *_dst, ptrdiff_t _dststride,
  1029. int16_t *src, ptrdiff_t srcstride,
  1030. int width, int height)
  1031. {
  1032. int x, y;
  1033. pixel *dst = (pixel *)_dst;
  1034. ptrdiff_t dststride = _dststride / sizeof(pixel);
  1035. int shift = 14 - BIT_DEPTH;
  1036. #if BIT_DEPTH < 14
  1037. int offset = 1 << (shift - 1);
  1038. #else
  1039. int offset = 0;
  1040. #endif
  1041. srcstride /= sizeof(*src);
  1042. for (y = 0; y < height; y++) {
  1043. for (x = 0; x < width; x++)
  1044. dst[x] = av_clip_pixel((src[x] + offset) >> shift);
  1045. dst += dststride;
  1046. src += srcstride;
  1047. }
  1048. }
  1049. static av_always_inline void
  1050. FUNC(put_unweighted_pred_avg)(uint8_t *_dst, ptrdiff_t _dststride,
  1051. int16_t *src1, int16_t *src2,
  1052. ptrdiff_t srcstride,
  1053. int width, int height)
  1054. {
  1055. int x, y;
  1056. pixel *dst = (pixel *)_dst;
  1057. ptrdiff_t dststride = _dststride / sizeof(pixel);
  1058. int shift = 14 + 1 - BIT_DEPTH;
  1059. #if BIT_DEPTH < 14
  1060. int offset = 1 << (shift - 1);
  1061. #else
  1062. int offset = 0;
  1063. #endif
  1064. srcstride /= sizeof(*src1);
  1065. for (y = 0; y < height; y++) {
  1066. for (x = 0; x < width; x++)
  1067. dst[x] = av_clip_pixel((src1[x] + src2[x] + offset) >> shift);
  1068. dst += dststride;
  1069. src1 += srcstride;
  1070. src2 += srcstride;
  1071. }
  1072. }
  1073. static av_always_inline void
  1074. FUNC(weighted_pred)(uint8_t denom, int16_t wlxFlag, int16_t olxFlag,
  1075. uint8_t *_dst, ptrdiff_t _dststride,
  1076. int16_t *src, ptrdiff_t srcstride,
  1077. int width, int height)
  1078. {
  1079. int shift, log2Wd, wx, ox, x, y, offset;
  1080. pixel *dst = (pixel *)_dst;
  1081. ptrdiff_t dststride = _dststride / sizeof(pixel);
  1082. shift = 14 - BIT_DEPTH;
  1083. log2Wd = denom + shift;
  1084. offset = 1 << (log2Wd - 1);
  1085. wx = wlxFlag;
  1086. ox = olxFlag * (1 << (BIT_DEPTH - 8));
  1087. srcstride /= sizeof(*src);
  1088. for (y = 0; y < height; y++) {
  1089. for (x = 0; x < width; x++) {
  1090. if (log2Wd >= 1) {
  1091. dst[x] = av_clip_pixel(((src[x] * wx + offset) >> log2Wd) + ox);
  1092. } else {
  1093. dst[x] = av_clip_pixel(src[x] * wx + ox);
  1094. }
  1095. }
  1096. dst += dststride;
  1097. src += srcstride;
  1098. }
  1099. }
  1100. static av_always_inline void
  1101. FUNC(weighted_pred_avg)(uint8_t denom,
  1102. int16_t wl0Flag, int16_t wl1Flag,
  1103. int16_t ol0Flag, int16_t ol1Flag,
  1104. uint8_t *_dst, ptrdiff_t _dststride,
  1105. int16_t *src1, int16_t *src2,
  1106. ptrdiff_t srcstride,
  1107. int width, int height)
  1108. {
  1109. int shift, log2Wd, w0, w1, o0, o1, x, y;
  1110. pixel *dst = (pixel *)_dst;
  1111. ptrdiff_t dststride = _dststride / sizeof(pixel);
  1112. shift = 14 - BIT_DEPTH;
  1113. log2Wd = denom + shift;
  1114. w0 = wl0Flag;
  1115. w1 = wl1Flag;
  1116. o0 = ol0Flag * (1 << (BIT_DEPTH - 8));
  1117. o1 = ol1Flag * (1 << (BIT_DEPTH - 8));
  1118. srcstride /= sizeof(*src1);
  1119. for (y = 0; y < height; y++) {
  1120. for (x = 0; x < width; x++)
  1121. dst[x] = av_clip_pixel((src1[x] * w0 + src2[x] * w1 +
  1122. ((o0 + o1 + 1) << log2Wd)) >> (log2Wd + 1));
  1123. dst += dststride;
  1124. src1 += srcstride;
  1125. src2 += srcstride;
  1126. }
  1127. }
  1128. #define PUT_PRED(w) \
  1129. static void FUNC(put_unweighted_pred_ ## w)(uint8_t *dst, ptrdiff_t dststride, \
  1130. int16_t *src, ptrdiff_t srcstride, \
  1131. int height) \
  1132. { \
  1133. FUNC(put_unweighted_pred)(dst, dststride, src, srcstride, w, height); \
  1134. } \
  1135. static void FUNC(put_unweighted_pred_avg_ ## w)(uint8_t *dst, ptrdiff_t dststride, \
  1136. int16_t *src1, int16_t *src2, \
  1137. ptrdiff_t srcstride, int height) \
  1138. { \
  1139. FUNC(put_unweighted_pred_avg)(dst, dststride, src1, src2, srcstride, w, height); \
  1140. } \
  1141. static void FUNC(put_weighted_pred_ ## w)(uint8_t denom, int16_t weight, int16_t offset, \
  1142. uint8_t *dst, ptrdiff_t dststride, \
  1143. int16_t *src, ptrdiff_t srcstride, int height) \
  1144. { \
  1145. FUNC(weighted_pred)(denom, weight, offset, \
  1146. dst, dststride, src, srcstride, w, height); \
  1147. } \
  1148. static void FUNC(put_weighted_pred_avg_ ## w)(uint8_t denom, int16_t weight0, int16_t weight1, \
  1149. int16_t offset0, int16_t offset1, \
  1150. uint8_t *dst, ptrdiff_t dststride, \
  1151. int16_t *src1, int16_t *src2, \
  1152. ptrdiff_t srcstride, int height) \
  1153. { \
  1154. FUNC(weighted_pred_avg)(denom, weight0, weight1, offset0, offset1, \
  1155. dst, dststride, src1, src2, srcstride, w, height); \
  1156. }
  1157. PUT_PRED(64)
  1158. PUT_PRED(48)
  1159. PUT_PRED(32)
  1160. PUT_PRED(24)
  1161. PUT_PRED(16)
  1162. PUT_PRED(12)
  1163. PUT_PRED(8)
  1164. PUT_PRED(6)
  1165. PUT_PRED(4)
  1166. PUT_PRED(2)
  1167. // line zero
  1168. #define P3 pix[-4 * xstride]
  1169. #define P2 pix[-3 * xstride]
  1170. #define P1 pix[-2 * xstride]
  1171. #define P0 pix[-1 * xstride]
  1172. #define Q0 pix[0 * xstride]
  1173. #define Q1 pix[1 * xstride]
  1174. #define Q2 pix[2 * xstride]
  1175. #define Q3 pix[3 * xstride]
  1176. // line three. used only for deblocking decision
  1177. #define TP3 pix[-4 * xstride + 3 * ystride]
  1178. #define TP2 pix[-3 * xstride + 3 * ystride]
  1179. #define TP1 pix[-2 * xstride + 3 * ystride]
  1180. #define TP0 pix[-1 * xstride + 3 * ystride]
  1181. #define TQ0 pix[0 * xstride + 3 * ystride]
  1182. #define TQ1 pix[1 * xstride + 3 * ystride]
  1183. #define TQ2 pix[2 * xstride + 3 * ystride]
  1184. #define TQ3 pix[3 * xstride + 3 * ystride]
  1185. static void FUNC(hevc_loop_filter_luma)(uint8_t *_pix,
  1186. ptrdiff_t _xstride, ptrdiff_t _ystride,
  1187. int beta, int *_tc,
  1188. uint8_t *_no_p, uint8_t *_no_q)
  1189. {
  1190. int d, j;
  1191. pixel *pix = (pixel *)_pix;
  1192. ptrdiff_t xstride = _xstride / sizeof(pixel);
  1193. ptrdiff_t ystride = _ystride / sizeof(pixel);
  1194. beta <<= BIT_DEPTH - 8;
  1195. for (j = 0; j < 2; j++) {
  1196. const int dp0 = abs(P2 - 2 * P1 + P0);
  1197. const int dq0 = abs(Q2 - 2 * Q1 + Q0);
  1198. const int dp3 = abs(TP2 - 2 * TP1 + TP0);
  1199. const int dq3 = abs(TQ2 - 2 * TQ1 + TQ0);
  1200. const int d0 = dp0 + dq0;
  1201. const int d3 = dp3 + dq3;
  1202. const int tc = _tc[j] << (BIT_DEPTH - 8);
  1203. const int no_p = _no_p[j];
  1204. const int no_q = _no_q[j];
  1205. if (d0 + d3 >= beta) {
  1206. pix += 4 * ystride;
  1207. continue;
  1208. } else {
  1209. const int beta_3 = beta >> 3;
  1210. const int beta_2 = beta >> 2;
  1211. const int tc25 = ((tc * 5 + 1) >> 1);
  1212. if (abs(P3 - P0) + abs(Q3 - Q0) < beta_3 && abs(P0 - Q0) < tc25 &&
  1213. abs(TP3 - TP0) + abs(TQ3 - TQ0) < beta_3 && abs(TP0 - TQ0) < tc25 &&
  1214. (d0 << 1) < beta_2 && (d3 << 1) < beta_2) {
  1215. // strong filtering
  1216. const int tc2 = tc << 1;
  1217. for (d = 0; d < 4; d++) {
  1218. const int p3 = P3;
  1219. const int p2 = P2;
  1220. const int p1 = P1;
  1221. const int p0 = P0;
  1222. const int q0 = Q0;
  1223. const int q1 = Q1;
  1224. const int q2 = Q2;
  1225. const int q3 = Q3;
  1226. if (!no_p) {
  1227. P0 = p0 + av_clip(((p2 + 2 * p1 + 2 * p0 + 2 * q0 + q1 + 4) >> 3) - p0, -tc2, tc2);
  1228. P1 = p1 + av_clip(((p2 + p1 + p0 + q0 + 2) >> 2) - p1, -tc2, tc2);
  1229. P2 = p2 + av_clip(((2 * p3 + 3 * p2 + p1 + p0 + q0 + 4) >> 3) - p2, -tc2, tc2);
  1230. }
  1231. if (!no_q) {
  1232. Q0 = q0 + av_clip(((p1 + 2 * p0 + 2 * q0 + 2 * q1 + q2 + 4) >> 3) - q0, -tc2, tc2);
  1233. Q1 = q1 + av_clip(((p0 + q0 + q1 + q2 + 2) >> 2) - q1, -tc2, tc2);
  1234. Q2 = q2 + av_clip(((2 * q3 + 3 * q2 + q1 + q0 + p0 + 4) >> 3) - q2, -tc2, tc2);
  1235. }
  1236. pix += ystride;
  1237. }
  1238. } else { // normal filtering
  1239. int nd_p = 1;
  1240. int nd_q = 1;
  1241. const int tc_2 = tc >> 1;
  1242. if (dp0 + dp3 < ((beta + (beta >> 1)) >> 3))
  1243. nd_p = 2;
  1244. if (dq0 + dq3 < ((beta + (beta >> 1)) >> 3))
  1245. nd_q = 2;
  1246. for (d = 0; d < 4; d++) {
  1247. const int p2 = P2;
  1248. const int p1 = P1;
  1249. const int p0 = P0;
  1250. const int q0 = Q0;
  1251. const int q1 = Q1;
  1252. const int q2 = Q2;
  1253. int delta0 = (9 * (q0 - p0) - 3 * (q1 - p1) + 8) >> 4;
  1254. if (abs(delta0) < 10 * tc) {
  1255. delta0 = av_clip(delta0, -tc, tc);
  1256. if (!no_p)
  1257. P0 = av_clip_pixel(p0 + delta0);
  1258. if (!no_q)
  1259. Q0 = av_clip_pixel(q0 - delta0);
  1260. if (!no_p && nd_p > 1) {
  1261. const int deltap1 = av_clip((((p2 + p0 + 1) >> 1) - p1 + delta0) >> 1, -tc_2, tc_2);
  1262. P1 = av_clip_pixel(p1 + deltap1);
  1263. }
  1264. if (!no_q && nd_q > 1) {
  1265. const int deltaq1 = av_clip((((q2 + q0 + 1) >> 1) - q1 - delta0) >> 1, -tc_2, tc_2);
  1266. Q1 = av_clip_pixel(q1 + deltaq1);
  1267. }
  1268. }
  1269. pix += ystride;
  1270. }
  1271. }
  1272. }
  1273. }
  1274. }
  1275. static void FUNC(hevc_loop_filter_chroma)(uint8_t *_pix, ptrdiff_t _xstride,
  1276. ptrdiff_t _ystride, int *_tc,
  1277. uint8_t *_no_p, uint8_t *_no_q)
  1278. {
  1279. int d, j, no_p, no_q;
  1280. pixel *pix = (pixel *)_pix;
  1281. ptrdiff_t xstride = _xstride / sizeof(pixel);
  1282. ptrdiff_t ystride = _ystride / sizeof(pixel);
  1283. for (j = 0; j < 2; j++) {
  1284. const int tc = _tc[j] << (BIT_DEPTH - 8);
  1285. if (tc <= 0) {
  1286. pix += 4 * ystride;
  1287. continue;
  1288. }
  1289. no_p = _no_p[j];
  1290. no_q = _no_q[j];
  1291. for (d = 0; d < 4; d++) {
  1292. int delta0;
  1293. const int p1 = P1;
  1294. const int p0 = P0;
  1295. const int q0 = Q0;
  1296. const int q1 = Q1;
  1297. delta0 = av_clip((((q0 - p0) * 4) + p1 - q1 + 4) >> 3, -tc, tc);
  1298. if (!no_p)
  1299. P0 = av_clip_pixel(p0 + delta0);
  1300. if (!no_q)
  1301. Q0 = av_clip_pixel(q0 - delta0);
  1302. pix += ystride;
  1303. }
  1304. }
  1305. }
  1306. static void FUNC(hevc_h_loop_filter_chroma)(uint8_t *pix, ptrdiff_t stride,
  1307. int *tc, uint8_t *no_p,
  1308. uint8_t *no_q)
  1309. {
  1310. FUNC(hevc_loop_filter_chroma)(pix, stride, sizeof(pixel), tc, no_p, no_q);
  1311. }
  1312. static void FUNC(hevc_v_loop_filter_chroma)(uint8_t *pix, ptrdiff_t stride,
  1313. int *tc, uint8_t *no_p,
  1314. uint8_t *no_q)
  1315. {
  1316. FUNC(hevc_loop_filter_chroma)(pix, sizeof(pixel), stride, tc, no_p, no_q);
  1317. }
  1318. static void FUNC(hevc_h_loop_filter_luma)(uint8_t *pix, ptrdiff_t stride,
  1319. int beta, int *tc, uint8_t *no_p,
  1320. uint8_t *no_q)
  1321. {
  1322. FUNC(hevc_loop_filter_luma)(pix, stride, sizeof(pixel),
  1323. beta, tc, no_p, no_q);
  1324. }
  1325. static void FUNC(hevc_v_loop_filter_luma)(uint8_t *pix, ptrdiff_t stride,
  1326. int beta, int *tc, uint8_t *no_p,
  1327. uint8_t *no_q)
  1328. {
  1329. FUNC(hevc_loop_filter_luma)(pix, sizeof(pixel), stride,
  1330. beta, tc, no_p, no_q);
  1331. }
  1332. #undef P3
  1333. #undef P2
  1334. #undef P1
  1335. #undef P0
  1336. #undef Q0
  1337. #undef Q1
  1338. #undef Q2
  1339. #undef Q3
  1340. #undef TP3
  1341. #undef TP2
  1342. #undef TP1
  1343. #undef TP0
  1344. #undef TQ0
  1345. #undef TQ1
  1346. #undef TQ2
  1347. #undef TQ3