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.

466 lines
14KB

  1. /*
  2. * This file is part of Libav.
  3. *
  4. * Libav is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * Libav is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with Libav; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. /**
  19. * @file
  20. *@brief IntraX8 frame subdecoder image manipulation routines
  21. */
  22. #include "intrax8dsp.h"
  23. #include "libavutil/common.h"
  24. /*
  25. * area positions, #3 is 1 pixel only, other are 8 pixels
  26. * |66666666|
  27. * 3|44444444|55555555|
  28. * - -+--------+--------+
  29. * 1 2|XXXXXXXX|
  30. * 1 2|XXXXXXXX|
  31. * 1 2|XXXXXXXX|
  32. * 1 2|XXXXXXXX|
  33. * 1 2|XXXXXXXX|
  34. * 1 2|XXXXXXXX|
  35. * 1 2|XXXXXXXX|
  36. * 1 2|XXXXXXXX|
  37. * ^-start
  38. */
  39. #define area1 (0)
  40. #define area2 (8)
  41. #define area3 (8 + 8)
  42. #define area4 (8 + 8 + 1)
  43. #define area5 (8 + 8 + 1 + 8)
  44. #define area6 (8 + 8 + 1 + 16)
  45. /**
  46. Collect statistics and prepare the edge pixels required by the other spatial compensation functions.
  47. * @param src pointer to the beginning of the processed block
  48. * @param dst pointer to emu_edge, edge pixels are stored the way other compensation routines do.
  49. * @param linesize byte offset between 2 vertical pixels in the source image
  50. * @param range pointer to the variable where the edge pixel range is to be stored (max-min values)
  51. * @param psum pointer to the variable where the edge pixel sum is to be stored
  52. * @param edges Informs this routine that the block is on an image border, so it has to interpolate the missing edge pixels.
  53. and some of the edge pixels should be interpolated, the flag has the following meaning:
  54. 1 - mb_x==0 - first block in the row, interpolate area #1,#2,#3;
  55. 2 - mb_y==0 - first row, interpolate area #3,#4,#5,#6;
  56. note: 1|2 - mb_x==mb_y==0 - first block, use 0x80 value for all areas;
  57. 4 - mb_x>= (mb_width-1) last block in the row, interpolate area #5;
  58. -*/
  59. static void x8_setup_spatial_compensation(uint8_t *src, uint8_t *dst,
  60. ptrdiff_t stride, int *range,
  61. int *psum, int edges)
  62. {
  63. uint8_t *ptr;
  64. int sum;
  65. int i;
  66. int min_pix, max_pix;
  67. uint8_t c;
  68. if ((edges & 3) == 3) {
  69. *psum = 0x80 * (8 + 1 + 8 + 2);
  70. *range = 0;
  71. memset(dst, 0x80, 16 + 1 + 16 + 8);
  72. /* this triggers flat_dc for sure. flat_dc avoids all (other)
  73. * prediction modes, but requires dc_level decoding. */
  74. return;
  75. }
  76. min_pix = 256;
  77. max_pix = -1;
  78. sum = 0;
  79. if (!(edges & 1)) { // (mb_x != 0) // there is previous block on this row
  80. ptr = src - 1; // left column, area 2
  81. for (i = 7; i >= 0; i--) {
  82. c = *(ptr - 1); // area1, same mb as area2, no need to check
  83. dst[area1 + i] = c;
  84. c = *ptr;
  85. sum += c;
  86. min_pix = FFMIN(min_pix, c);
  87. max_pix = FFMAX(max_pix, c);
  88. dst[area2 + i] = c;
  89. ptr += stride;
  90. }
  91. }
  92. if (!(edges & 2)) { // (mb_y != 0) // there is row above
  93. ptr = src - stride; // top line
  94. for (i = 0; i < 8; i++) {
  95. c = *(ptr + i);
  96. sum += c;
  97. min_pix = FFMIN(min_pix, c);
  98. max_pix = FFMAX(max_pix, c);
  99. }
  100. if (edges & 4) { // last block on the row?
  101. memset(dst + area5, c, 8); // set with last pixel fr
  102. memcpy(dst + area4, ptr, 8);
  103. } else {
  104. memcpy(dst + area4, ptr, 16); // both area4 and 5
  105. }
  106. // area6 always present in the above block
  107. memcpy(dst + area6, ptr - stride, 8);
  108. }
  109. // now calculate the stuff we need
  110. if (edges & 3) { // mb_x ==0 || mb_y == 0) {
  111. int avg = (sum + 4) >> 3;
  112. if (edges & 1) // (mb_x == 0) { // implies mb_y !=0
  113. memset(dst + area1, avg, 8 + 8 + 1); // areas 1, 2, 3 are averaged
  114. else // implies y == 0 x != 0
  115. memset(dst + area3, avg, 1 + 16 + 8); // areas 3, 4, 5, 6
  116. sum += avg * 9;
  117. } else {
  118. // the edge pixel, in the top line and left column
  119. uint8_t c = *(src - 1 - stride);
  120. dst[area3] = c;
  121. sum += c;
  122. // edge pixel is not part of min/max
  123. }
  124. *range = max_pix - min_pix;
  125. sum += *(dst + area5) + *(dst + area5 + 1);
  126. *psum = sum;
  127. }
  128. static const uint16_t zero_prediction_weights[64 * 2] = {
  129. 640, 640, 669, 480, 708, 354, 748, 257,
  130. 792, 198, 760, 143, 808, 101, 772, 72,
  131. 480, 669, 537, 537, 598, 416, 661, 316,
  132. 719, 250, 707, 185, 768, 134, 745, 97,
  133. 354, 708, 416, 598, 488, 488, 564, 388,
  134. 634, 317, 642, 241, 716, 179, 706, 132,
  135. 257, 748, 316, 661, 388, 564, 469, 469,
  136. 543, 395, 571, 311, 655, 238, 660, 180,
  137. 198, 792, 250, 719, 317, 634, 395, 543,
  138. 469, 469, 507, 380, 597, 299, 616, 231,
  139. 161, 855, 206, 788, 266, 710, 340, 623,
  140. 411, 548, 455, 455, 548, 366, 576, 288,
  141. 122, 972, 159, 914, 211, 842, 276, 758,
  142. 341, 682, 389, 584, 483, 483, 520, 390,
  143. 110, 1172, 144, 1107, 193, 1028, 254, 932,
  144. 317, 846, 366, 731, 458, 611, 499, 499,
  145. };
  146. static void spatial_compensation_0(uint8_t *src, uint8_t *dst, ptrdiff_t stride)
  147. {
  148. int i, j;
  149. int x, y;
  150. unsigned int p; // power divided by 2
  151. int a;
  152. uint16_t left_sum[2][8] = { { 0 } };
  153. uint16_t top_sum[2][8] = { { 0 } };
  154. for (i = 0; i < 8; i++) {
  155. a = src[area2 + 7 - i] << 4;
  156. for (j = 0; j < 8; j++) {
  157. p = abs(i - j);
  158. left_sum[p & 1][j] += a >> (p >> 1);
  159. }
  160. }
  161. for (i = 0; i < 8; i++) {
  162. a = src[area4 + i] << 4;
  163. for (j = 0; j < 8; j++) {
  164. p = abs(i - j);
  165. top_sum[p & 1][j] += a >> (p >> 1);
  166. }
  167. }
  168. for (; i < 10; i++) {
  169. a = src[area4 + i] << 4;
  170. for (j = 5; j < 8; j++) {
  171. p = abs(i - j);
  172. top_sum[p & 1][j] += a >> (p >> 1);
  173. }
  174. }
  175. for (; i < 12; i++) {
  176. a = src[area4 + i] << 4;
  177. for (j = 7; j < 8; j++) {
  178. p = abs(i - j);
  179. top_sum[p & 1][j] += a >> (p >> 1);
  180. }
  181. }
  182. for (i = 0; i < 8; i++) {
  183. top_sum[0][i] += (top_sum[1][i] * 181 + 128) >> 8; // 181 is sqrt(2)/2
  184. left_sum[0][i] += (left_sum[1][i] * 181 + 128) >> 8;
  185. }
  186. for (y = 0; y < 8; y++) {
  187. for (x = 0; x < 8; x++)
  188. dst[x] = ((uint32_t) top_sum[0][x] * zero_prediction_weights[y * 16 + x * 2 + 0] +
  189. (uint32_t) left_sum[0][y] * zero_prediction_weights[y * 16 + x * 2 + 1] +
  190. 0x8000) >> 16;
  191. dst += stride;
  192. }
  193. }
  194. static void spatial_compensation_1(uint8_t *src, uint8_t *dst, ptrdiff_t stride)
  195. {
  196. int x, y;
  197. for (y = 0; y < 8; y++) {
  198. for (x = 0; x < 8; x++)
  199. dst[x] = src[area4 + FFMIN(2 * y + x + 2, 15)];
  200. dst += stride;
  201. }
  202. }
  203. static void spatial_compensation_2(uint8_t *src, uint8_t *dst, ptrdiff_t stride)
  204. {
  205. int x, y;
  206. for (y = 0; y < 8; y++) {
  207. for (x = 0; x < 8; x++)
  208. dst[x] = src[area4 + 1 + y + x];
  209. dst += stride;
  210. }
  211. }
  212. static void spatial_compensation_3(uint8_t *src, uint8_t *dst, ptrdiff_t stride)
  213. {
  214. int x, y;
  215. for (y = 0; y < 8; y++) {
  216. for (x = 0; x < 8; x++)
  217. dst[x] = src[area4 + ((y + 1) >> 1) + x];
  218. dst += stride;
  219. }
  220. }
  221. static void spatial_compensation_4(uint8_t *src, uint8_t *dst, ptrdiff_t stride)
  222. {
  223. int x, y;
  224. for (y = 0; y < 8; y++) {
  225. for (x = 0; x < 8; x++)
  226. dst[x] = (src[area4 + x] + src[area6 + x] + 1) >> 1;
  227. dst += stride;
  228. }
  229. }
  230. static void spatial_compensation_5(uint8_t *src, uint8_t *dst, ptrdiff_t stride)
  231. {
  232. int x, y;
  233. for (y = 0; y < 8; y++) {
  234. for (x = 0; x < 8; x++) {
  235. if (2 * x - y < 0)
  236. dst[x] = src[area2 + 9 + 2 * x - y];
  237. else
  238. dst[x] = src[area4 + x - ((y + 1) >> 1)];
  239. }
  240. dst += stride;
  241. }
  242. }
  243. static void spatial_compensation_6(uint8_t *src, uint8_t *dst, ptrdiff_t stride)
  244. {
  245. int x, y;
  246. for (y = 0; y < 8; y++) {
  247. for (x = 0; x < 8; x++)
  248. dst[x] = src[area3 + x - y];
  249. dst += stride;
  250. }
  251. }
  252. static void spatial_compensation_7(uint8_t *src, uint8_t *dst, ptrdiff_t stride)
  253. {
  254. int x, y;
  255. for (y = 0; y < 8; y++) {
  256. for (x = 0; x < 8; x++) {
  257. if (x - 2 * y > 0)
  258. dst[x] = (src[area3 - 1 + x - 2 * y] + src[area3 + x - 2 * y] + 1) >> 1;
  259. else
  260. dst[x] = src[area2 + 8 - y + (x >> 1)];
  261. }
  262. dst += stride;
  263. }
  264. }
  265. static void spatial_compensation_8(uint8_t *src, uint8_t *dst, ptrdiff_t stride)
  266. {
  267. int x, y;
  268. for (y = 0; y < 8; y++) {
  269. for (x = 0; x < 8; x++)
  270. dst[x] = (src[area1 + 7 - y] + src[area2 + 7 - y] + 1) >> 1;
  271. dst += stride;
  272. }
  273. }
  274. static void spatial_compensation_9(uint8_t *src, uint8_t *dst, ptrdiff_t stride)
  275. {
  276. int x, y;
  277. for (y = 0; y < 8; y++) {
  278. for (x = 0; x < 8; x++)
  279. dst[x] = src[area2 + 6 - FFMIN(x + y, 6)];
  280. dst += stride;
  281. }
  282. }
  283. static void spatial_compensation_10(uint8_t *src, uint8_t *dst, ptrdiff_t stride)
  284. {
  285. int x, y;
  286. for (y = 0; y < 8; y++) {
  287. for (x = 0; x < 8; x++)
  288. dst[x] = (src[area2 + 7 - y] * (8 - x) + src[area4 + x] * x + 4) >> 3;
  289. dst += stride;
  290. }
  291. }
  292. static void spatial_compensation_11(uint8_t *src, uint8_t *dst, ptrdiff_t stride)
  293. {
  294. int x, y;
  295. for (y = 0; y < 8; y++) {
  296. for (x = 0; x < 8; x++)
  297. dst[x] = (src[area2 + 7 - y] * y + src[area4 + x] * (8 - y) + 4) >> 3;
  298. dst += stride;
  299. }
  300. }
  301. static void x8_loop_filter(uint8_t *ptr, const ptrdiff_t a_stride,
  302. const ptrdiff_t b_stride, int quant)
  303. {
  304. int i, t;
  305. int p0, p1, p2, p3, p4, p5, p6, p7, p8, p9;
  306. int ql = (quant + 10) >> 3;
  307. for (i = 0; i < 8; i++, ptr += b_stride) {
  308. p0 = ptr[-5 * a_stride];
  309. p1 = ptr[-4 * a_stride];
  310. p2 = ptr[-3 * a_stride];
  311. p3 = ptr[-2 * a_stride];
  312. p4 = ptr[-1 * a_stride];
  313. p5 = ptr[0];
  314. p6 = ptr[1 * a_stride];
  315. p7 = ptr[2 * a_stride];
  316. p8 = ptr[3 * a_stride];
  317. p9 = ptr[4 * a_stride];
  318. t = (FFABS(p1 - p2) <= ql) +
  319. (FFABS(p2 - p3) <= ql) +
  320. (FFABS(p3 - p4) <= ql) +
  321. (FFABS(p4 - p5) <= ql);
  322. // You need at least 1 to be able to reach a total score of 6.
  323. if (t > 0) {
  324. t += (FFABS(p5 - p6) <= ql) +
  325. (FFABS(p6 - p7) <= ql) +
  326. (FFABS(p7 - p8) <= ql) +
  327. (FFABS(p8 - p9) <= ql) +
  328. (FFABS(p0 - p1) <= ql);
  329. if (t >= 6) {
  330. int min, max;
  331. min = max = p1;
  332. min = FFMIN(min, p3);
  333. max = FFMAX(max, p3);
  334. min = FFMIN(min, p5);
  335. max = FFMAX(max, p5);
  336. min = FFMIN(min, p8);
  337. max = FFMAX(max, p8);
  338. if (max - min < 2 * quant) { // early stop
  339. min = FFMIN(min, p2);
  340. max = FFMAX(max, p2);
  341. min = FFMIN(min, p4);
  342. max = FFMAX(max, p4);
  343. min = FFMIN(min, p6);
  344. max = FFMAX(max, p6);
  345. min = FFMIN(min, p7);
  346. max = FFMAX(max, p7);
  347. if (max - min < 2 * quant) {
  348. ptr[-2 * a_stride] = (4 * p2 + 3 * p3 + 1 * p7 + 4) >> 3;
  349. ptr[-1 * a_stride] = (3 * p2 + 3 * p4 + 2 * p7 + 4) >> 3;
  350. ptr[0] = (2 * p2 + 3 * p5 + 3 * p7 + 4) >> 3;
  351. ptr[1 * a_stride] = (1 * p2 + 3 * p6 + 4 * p7 + 4) >> 3;
  352. continue;
  353. }
  354. }
  355. }
  356. }
  357. {
  358. int x, x0, x1, x2;
  359. int m;
  360. x0 = (2 * p3 - 5 * p4 + 5 * p5 - 2 * p6 + 4) >> 3;
  361. if (FFABS(x0) < quant) {
  362. x1 = (2 * p1 - 5 * p2 + 5 * p3 - 2 * p4 + 4) >> 3;
  363. x2 = (2 * p5 - 5 * p6 + 5 * p7 - 2 * p8 + 4) >> 3;
  364. x = FFABS(x0) - FFMIN(FFABS(x1), FFABS(x2));
  365. m = p4 - p5;
  366. if (x > 0 && (m ^ x0) < 0) {
  367. int32_t sign;
  368. sign = m >> 31;
  369. m = (m ^ sign) - sign; // abs(m)
  370. m >>= 1;
  371. x = 5 * x >> 3;
  372. if (x > m)
  373. x = m;
  374. x = (x ^ sign) - sign;
  375. ptr[-1 * a_stride] -= x;
  376. ptr[0] += x;
  377. }
  378. }
  379. }
  380. }
  381. }
  382. static void x8_h_loop_filter(uint8_t *src, ptrdiff_t stride, int qscale)
  383. {
  384. x8_loop_filter(src, stride, 1, qscale);
  385. }
  386. static void x8_v_loop_filter(uint8_t *src, ptrdiff_t stride, int qscale)
  387. {
  388. x8_loop_filter(src, 1, stride, qscale);
  389. }
  390. av_cold void ff_intrax8dsp_init(IntraX8DSPContext *dsp)
  391. {
  392. dsp->h_loop_filter = x8_h_loop_filter;
  393. dsp->v_loop_filter = x8_v_loop_filter;
  394. dsp->setup_spatial_compensation = x8_setup_spatial_compensation;
  395. dsp->spatial_compensation[0] = spatial_compensation_0;
  396. dsp->spatial_compensation[1] = spatial_compensation_1;
  397. dsp->spatial_compensation[2] = spatial_compensation_2;
  398. dsp->spatial_compensation[3] = spatial_compensation_3;
  399. dsp->spatial_compensation[4] = spatial_compensation_4;
  400. dsp->spatial_compensation[5] = spatial_compensation_5;
  401. dsp->spatial_compensation[6] = spatial_compensation_6;
  402. dsp->spatial_compensation[7] = spatial_compensation_7;
  403. dsp->spatial_compensation[8] = spatial_compensation_8;
  404. dsp->spatial_compensation[9] = spatial_compensation_9;
  405. dsp->spatial_compensation[10] = spatial_compensation_10;
  406. dsp->spatial_compensation[11] = spatial_compensation_11;
  407. }