/media/libvpx/vp8/common/postproc.c

http://github.com/zpao/v8monkey · C · 1109 lines · 865 code · 190 blank · 54 comment · 111 complexity · 7d885558b841afd56596fc6744deaa64 MD5 · raw file

  1. /*
  2. * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #include "vpx_ports/config.h"
  11. #include "vpx_scale/yv12config.h"
  12. #include "postproc.h"
  13. #include "vpx_scale/yv12extend.h"
  14. #include "vpx_scale/vpxscale.h"
  15. #include "systemdependent.h"
  16. #include <math.h>
  17. #include <stdlib.h>
  18. #include <stdio.h>
  19. #define RGB_TO_YUV(t) \
  20. ( (0.257*(float)(t>>16)) + (0.504*(float)(t>>8&0xff)) + (0.098*(float)(t&0xff)) + 16), \
  21. (-(0.148*(float)(t>>16)) - (0.291*(float)(t>>8&0xff)) + (0.439*(float)(t&0xff)) + 128), \
  22. ( (0.439*(float)(t>>16)) - (0.368*(float)(t>>8&0xff)) - (0.071*(float)(t&0xff)) + 128)
  23. /* global constants */
  24. #if CONFIG_POSTPROC_VISUALIZER
  25. static const unsigned char MB_PREDICTION_MODE_colors[MB_MODE_COUNT][3] =
  26. {
  27. { RGB_TO_YUV(0x98FB98) }, /* PaleGreen */
  28. { RGB_TO_YUV(0x00FF00) }, /* Green */
  29. { RGB_TO_YUV(0xADFF2F) }, /* GreenYellow */
  30. { RGB_TO_YUV(0x228B22) }, /* ForestGreen */
  31. { RGB_TO_YUV(0x006400) }, /* DarkGreen */
  32. { RGB_TO_YUV(0x98F5FF) }, /* Cadet Blue */
  33. { RGB_TO_YUV(0x6CA6CD) }, /* Sky Blue */
  34. { RGB_TO_YUV(0x00008B) }, /* Dark blue */
  35. { RGB_TO_YUV(0x551A8B) }, /* Purple */
  36. { RGB_TO_YUV(0xFF0000) } /* Red */
  37. };
  38. static const unsigned char B_PREDICTION_MODE_colors[B_MODE_COUNT][3] =
  39. {
  40. { RGB_TO_YUV(0x6633ff) }, /* Purple */
  41. { RGB_TO_YUV(0xcc33ff) }, /* Magenta */
  42. { RGB_TO_YUV(0xff33cc) }, /* Pink */
  43. { RGB_TO_YUV(0xff3366) }, /* Coral */
  44. { RGB_TO_YUV(0x3366ff) }, /* Blue */
  45. { RGB_TO_YUV(0xed00f5) }, /* Dark Blue */
  46. { RGB_TO_YUV(0x2e00b8) }, /* Dark Purple */
  47. { RGB_TO_YUV(0xff6633) }, /* Orange */
  48. { RGB_TO_YUV(0x33ccff) }, /* Light Blue */
  49. { RGB_TO_YUV(0x8ab800) }, /* Green */
  50. { RGB_TO_YUV(0xffcc33) }, /* Light Orange */
  51. { RGB_TO_YUV(0x33ffcc) }, /* Aqua */
  52. { RGB_TO_YUV(0x66ff33) }, /* Light Green */
  53. { RGB_TO_YUV(0xccff33) }, /* Yellow */
  54. };
  55. static const unsigned char MV_REFERENCE_FRAME_colors[MAX_REF_FRAMES][3] =
  56. {
  57. { RGB_TO_YUV(0x00ff00) }, /* Blue */
  58. { RGB_TO_YUV(0x0000ff) }, /* Green */
  59. { RGB_TO_YUV(0xffff00) }, /* Yellow */
  60. { RGB_TO_YUV(0xff0000) }, /* Red */
  61. };
  62. #endif
  63. static const short kernel5[] =
  64. {
  65. 1, 1, 4, 1, 1
  66. };
  67. const short vp8_rv[] =
  68. {
  69. 8, 5, 2, 2, 8, 12, 4, 9, 8, 3,
  70. 0, 3, 9, 0, 0, 0, 8, 3, 14, 4,
  71. 10, 1, 11, 14, 1, 14, 9, 6, 12, 11,
  72. 8, 6, 10, 0, 0, 8, 9, 0, 3, 14,
  73. 8, 11, 13, 4, 2, 9, 0, 3, 9, 6,
  74. 1, 2, 3, 14, 13, 1, 8, 2, 9, 7,
  75. 3, 3, 1, 13, 13, 6, 6, 5, 2, 7,
  76. 11, 9, 11, 8, 7, 3, 2, 0, 13, 13,
  77. 14, 4, 12, 5, 12, 10, 8, 10, 13, 10,
  78. 4, 14, 4, 10, 0, 8, 11, 1, 13, 7,
  79. 7, 14, 6, 14, 13, 2, 13, 5, 4, 4,
  80. 0, 10, 0, 5, 13, 2, 12, 7, 11, 13,
  81. 8, 0, 4, 10, 7, 2, 7, 2, 2, 5,
  82. 3, 4, 7, 3, 3, 14, 14, 5, 9, 13,
  83. 3, 14, 3, 6, 3, 0, 11, 8, 13, 1,
  84. 13, 1, 12, 0, 10, 9, 7, 6, 2, 8,
  85. 5, 2, 13, 7, 1, 13, 14, 7, 6, 7,
  86. 9, 6, 10, 11, 7, 8, 7, 5, 14, 8,
  87. 4, 4, 0, 8, 7, 10, 0, 8, 14, 11,
  88. 3, 12, 5, 7, 14, 3, 14, 5, 2, 6,
  89. 11, 12, 12, 8, 0, 11, 13, 1, 2, 0,
  90. 5, 10, 14, 7, 8, 0, 4, 11, 0, 8,
  91. 0, 3, 10, 5, 8, 0, 11, 6, 7, 8,
  92. 10, 7, 13, 9, 2, 5, 1, 5, 10, 2,
  93. 4, 3, 5, 6, 10, 8, 9, 4, 11, 14,
  94. 0, 10, 0, 5, 13, 2, 12, 7, 11, 13,
  95. 8, 0, 4, 10, 7, 2, 7, 2, 2, 5,
  96. 3, 4, 7, 3, 3, 14, 14, 5, 9, 13,
  97. 3, 14, 3, 6, 3, 0, 11, 8, 13, 1,
  98. 13, 1, 12, 0, 10, 9, 7, 6, 2, 8,
  99. 5, 2, 13, 7, 1, 13, 14, 7, 6, 7,
  100. 9, 6, 10, 11, 7, 8, 7, 5, 14, 8,
  101. 4, 4, 0, 8, 7, 10, 0, 8, 14, 11,
  102. 3, 12, 5, 7, 14, 3, 14, 5, 2, 6,
  103. 11, 12, 12, 8, 0, 11, 13, 1, 2, 0,
  104. 5, 10, 14, 7, 8, 0, 4, 11, 0, 8,
  105. 0, 3, 10, 5, 8, 0, 11, 6, 7, 8,
  106. 10, 7, 13, 9, 2, 5, 1, 5, 10, 2,
  107. 4, 3, 5, 6, 10, 8, 9, 4, 11, 14,
  108. 3, 8, 3, 7, 8, 5, 11, 4, 12, 3,
  109. 11, 9, 14, 8, 14, 13, 4, 3, 1, 2,
  110. 14, 6, 5, 4, 4, 11, 4, 6, 2, 1,
  111. 5, 8, 8, 12, 13, 5, 14, 10, 12, 13,
  112. 0, 9, 5, 5, 11, 10, 13, 9, 10, 13,
  113. };
  114. extern void vp8_blit_text(const char *msg, unsigned char *address, const int pitch);
  115. extern void vp8_blit_line(int x0, int x1, int y0, int y1, unsigned char *image, const int pitch);
  116. /***********************************************************************************************************
  117. */
  118. void vp8_post_proc_down_and_across_c
  119. (
  120. unsigned char *src_ptr,
  121. unsigned char *dst_ptr,
  122. int src_pixels_per_line,
  123. int dst_pixels_per_line,
  124. int rows,
  125. int cols,
  126. int flimit
  127. )
  128. {
  129. unsigned char *p_src, *p_dst;
  130. int row;
  131. int col;
  132. int i;
  133. int v;
  134. int pitch = src_pixels_per_line;
  135. unsigned char d[8];
  136. (void)dst_pixels_per_line;
  137. for (row = 0; row < rows; row++)
  138. {
  139. /* post_proc_down for one row */
  140. p_src = src_ptr;
  141. p_dst = dst_ptr;
  142. for (col = 0; col < cols; col++)
  143. {
  144. int kernel = 4;
  145. int v = p_src[col];
  146. for (i = -2; i <= 2; i++)
  147. {
  148. if (abs(v - p_src[col+i*pitch]) > flimit)
  149. goto down_skip_convolve;
  150. kernel += kernel5[2+i] * p_src[col+i*pitch];
  151. }
  152. v = (kernel >> 3);
  153. down_skip_convolve:
  154. p_dst[col] = v;
  155. }
  156. /* now post_proc_across */
  157. p_src = dst_ptr;
  158. p_dst = dst_ptr;
  159. for (i = 0; i < 8; i++)
  160. d[i] = p_src[i];
  161. for (col = 0; col < cols; col++)
  162. {
  163. int kernel = 4;
  164. v = p_src[col];
  165. d[col&7] = v;
  166. for (i = -2; i <= 2; i++)
  167. {
  168. if (abs(v - p_src[col+i]) > flimit)
  169. goto across_skip_convolve;
  170. kernel += kernel5[2+i] * p_src[col+i];
  171. }
  172. d[col&7] = (kernel >> 3);
  173. across_skip_convolve:
  174. if (col >= 2)
  175. p_dst[col-2] = d[(col-2)&7];
  176. }
  177. /* handle the last two pixels */
  178. p_dst[col-2] = d[(col-2)&7];
  179. p_dst[col-1] = d[(col-1)&7];
  180. /* next row */
  181. src_ptr += pitch;
  182. dst_ptr += pitch;
  183. }
  184. }
  185. static int q2mbl(int x)
  186. {
  187. if (x < 20) x = 20;
  188. x = 50 + (x - 50) * 10 / 8;
  189. return x * x / 3;
  190. }
  191. void vp8_mbpost_proc_across_ip_c(unsigned char *src, int pitch, int rows, int cols, int flimit)
  192. {
  193. int r, c, i;
  194. unsigned char *s = src;
  195. unsigned char d[16];
  196. for (r = 0; r < rows; r++)
  197. {
  198. int sumsq = 0;
  199. int sum = 0;
  200. for (i = -8; i <= 6; i++)
  201. {
  202. sumsq += s[i] * s[i];
  203. sum += s[i];
  204. d[i+8] = 0;
  205. }
  206. for (c = 0; c < cols + 8; c++)
  207. {
  208. int x = s[c+7] - s[c-8];
  209. int y = s[c+7] + s[c-8];
  210. sum += x;
  211. sumsq += x * y;
  212. d[c&15] = s[c];
  213. if (sumsq * 15 - sum * sum < flimit)
  214. {
  215. d[c&15] = (8 + sum + s[c]) >> 4;
  216. }
  217. s[c-8] = d[(c-8)&15];
  218. }
  219. s += pitch;
  220. }
  221. }
  222. void vp8_mbpost_proc_down_c(unsigned char *dst, int pitch, int rows, int cols, int flimit)
  223. {
  224. int r, c, i;
  225. const short *rv3 = &vp8_rv[63&rand()];
  226. for (c = 0; c < cols; c++)
  227. {
  228. unsigned char *s = &dst[c];
  229. int sumsq = 0;
  230. int sum = 0;
  231. unsigned char d[16];
  232. const short *rv2 = rv3 + ((c * 17) & 127);
  233. for (i = -8; i <= 6; i++)
  234. {
  235. sumsq += s[i*pitch] * s[i*pitch];
  236. sum += s[i*pitch];
  237. }
  238. for (r = 0; r < rows + 8; r++)
  239. {
  240. sumsq += s[7*pitch] * s[ 7*pitch] - s[-8*pitch] * s[-8*pitch];
  241. sum += s[7*pitch] - s[-8*pitch];
  242. d[r&15] = s[0];
  243. if (sumsq * 15 - sum * sum < flimit)
  244. {
  245. d[r&15] = (rv2[r&127] + sum + s[0]) >> 4;
  246. }
  247. s[-8*pitch] = d[(r-8)&15];
  248. s += pitch;
  249. }
  250. }
  251. }
  252. static void vp8_deblock_and_de_macro_block(YV12_BUFFER_CONFIG *source,
  253. YV12_BUFFER_CONFIG *post,
  254. int q,
  255. int low_var_thresh,
  256. int flag,
  257. vp8_postproc_rtcd_vtable_t *rtcd)
  258. {
  259. double level = 6.0e-05 * q * q * q - .0067 * q * q + .306 * q + .0065;
  260. int ppl = (int)(level + .5);
  261. (void) low_var_thresh;
  262. (void) flag;
  263. POSTPROC_INVOKE(rtcd, downacross)(source->y_buffer, post->y_buffer, source->y_stride, post->y_stride, source->y_height, source->y_width, ppl);
  264. POSTPROC_INVOKE(rtcd, across)(post->y_buffer, post->y_stride, post->y_height, post->y_width, q2mbl(q));
  265. POSTPROC_INVOKE(rtcd, down)(post->y_buffer, post->y_stride, post->y_height, post->y_width, q2mbl(q));
  266. POSTPROC_INVOKE(rtcd, downacross)(source->u_buffer, post->u_buffer, source->uv_stride, post->uv_stride, source->uv_height, source->uv_width, ppl);
  267. POSTPROC_INVOKE(rtcd, downacross)(source->v_buffer, post->v_buffer, source->uv_stride, post->uv_stride, source->uv_height, source->uv_width, ppl);
  268. }
  269. void vp8_deblock(YV12_BUFFER_CONFIG *source,
  270. YV12_BUFFER_CONFIG *post,
  271. int q,
  272. int low_var_thresh,
  273. int flag,
  274. vp8_postproc_rtcd_vtable_t *rtcd)
  275. {
  276. double level = 6.0e-05 * q * q * q - .0067 * q * q + .306 * q + .0065;
  277. int ppl = (int)(level + .5);
  278. (void) low_var_thresh;
  279. (void) flag;
  280. POSTPROC_INVOKE(rtcd, downacross)(source->y_buffer, post->y_buffer, source->y_stride, post->y_stride, source->y_height, source->y_width, ppl);
  281. POSTPROC_INVOKE(rtcd, downacross)(source->u_buffer, post->u_buffer, source->uv_stride, post->uv_stride, source->uv_height, source->uv_width, ppl);
  282. POSTPROC_INVOKE(rtcd, downacross)(source->v_buffer, post->v_buffer, source->uv_stride, post->uv_stride, source->uv_height, source->uv_width, ppl);
  283. }
  284. void vp8_de_noise(YV12_BUFFER_CONFIG *source,
  285. YV12_BUFFER_CONFIG *post,
  286. int q,
  287. int low_var_thresh,
  288. int flag,
  289. vp8_postproc_rtcd_vtable_t *rtcd)
  290. {
  291. double level = 6.0e-05 * q * q * q - .0067 * q * q + .306 * q + .0065;
  292. int ppl = (int)(level + .5);
  293. (void) post;
  294. (void) low_var_thresh;
  295. (void) flag;
  296. POSTPROC_INVOKE(rtcd, downacross)(
  297. source->y_buffer + 2 * source->y_stride + 2,
  298. source->y_buffer + 2 * source->y_stride + 2,
  299. source->y_stride,
  300. source->y_stride,
  301. source->y_height - 4,
  302. source->y_width - 4,
  303. ppl);
  304. POSTPROC_INVOKE(rtcd, downacross)(
  305. source->u_buffer + 2 * source->uv_stride + 2,
  306. source->u_buffer + 2 * source->uv_stride + 2,
  307. source->uv_stride,
  308. source->uv_stride,
  309. source->uv_height - 4,
  310. source->uv_width - 4, ppl);
  311. POSTPROC_INVOKE(rtcd, downacross)(
  312. source->v_buffer + 2 * source->uv_stride + 2,
  313. source->v_buffer + 2 * source->uv_stride + 2,
  314. source->uv_stride,
  315. source->uv_stride,
  316. source->uv_height - 4,
  317. source->uv_width - 4, ppl);
  318. }
  319. double vp8_gaussian(double sigma, double mu, double x)
  320. {
  321. return 1 / (sigma * sqrt(2.0 * 3.14159265)) *
  322. (exp(-(x - mu) * (x - mu) / (2 * sigma * sigma)));
  323. }
  324. extern void (*vp8_clear_system_state)(void);
  325. static void fillrd(struct postproc_state *state, int q, int a)
  326. {
  327. char char_dist[300];
  328. double sigma;
  329. int ai = a, qi = q, i;
  330. vp8_clear_system_state();
  331. sigma = ai + .5 + .6 * (63 - qi) / 63.0;
  332. /* set up a lookup table of 256 entries that matches
  333. * a gaussian distribution with sigma determined by q.
  334. */
  335. {
  336. double i;
  337. int next, j;
  338. next = 0;
  339. for (i = -32; i < 32; i++)
  340. {
  341. int a = (int)(.5 + 256 * vp8_gaussian(sigma, 0, i));
  342. if (a)
  343. {
  344. for (j = 0; j < a; j++)
  345. {
  346. char_dist[next+j] = (char) i;
  347. }
  348. next = next + j;
  349. }
  350. }
  351. for (next = next; next < 256; next++)
  352. char_dist[next] = 0;
  353. }
  354. for (i = 0; i < 3072; i++)
  355. {
  356. state->noise[i] = char_dist[rand() & 0xff];
  357. }
  358. for (i = 0; i < 16; i++)
  359. {
  360. state->blackclamp[i] = -char_dist[0];
  361. state->whiteclamp[i] = -char_dist[0];
  362. state->bothclamp[i] = -2 * char_dist[0];
  363. }
  364. state->last_q = q;
  365. state->last_noise = a;
  366. }
  367. /****************************************************************************
  368. *
  369. * ROUTINE : plane_add_noise_c
  370. *
  371. * INPUTS : unsigned char *Start starting address of buffer to add gaussian
  372. * noise to
  373. * unsigned int Width width of plane
  374. * unsigned int Height height of plane
  375. * int Pitch distance between subsequent lines of frame
  376. * int q quantizer used to determine amount of noise
  377. * to add
  378. *
  379. * OUTPUTS : None.
  380. *
  381. * RETURNS : void.
  382. *
  383. * FUNCTION : adds gaussian noise to a plane of pixels
  384. *
  385. * SPECIAL NOTES : None.
  386. *
  387. ****************************************************************************/
  388. void vp8_plane_add_noise_c(unsigned char *Start, char *noise,
  389. char blackclamp[16],
  390. char whiteclamp[16],
  391. char bothclamp[16],
  392. unsigned int Width, unsigned int Height, int Pitch)
  393. {
  394. unsigned int i, j;
  395. for (i = 0; i < Height; i++)
  396. {
  397. unsigned char *Pos = Start + i * Pitch;
  398. char *Ref = (char *)(noise + (rand() & 0xff));
  399. for (j = 0; j < Width; j++)
  400. {
  401. if (Pos[j] < blackclamp[0])
  402. Pos[j] = blackclamp[0];
  403. if (Pos[j] > 255 + whiteclamp[0])
  404. Pos[j] = 255 + whiteclamp[0];
  405. Pos[j] += Ref[j];
  406. }
  407. }
  408. }
  409. /* Blend the macro block with a solid colored square. Leave the
  410. * edges unblended to give distinction to macro blocks in areas
  411. * filled with the same color block.
  412. */
  413. void vp8_blend_mb_inner_c (unsigned char *y, unsigned char *u, unsigned char *v,
  414. int y1, int u1, int v1, int alpha, int stride)
  415. {
  416. int i, j;
  417. int y1_const = y1*((1<<16)-alpha);
  418. int u1_const = u1*((1<<16)-alpha);
  419. int v1_const = v1*((1<<16)-alpha);
  420. y += 2*stride + 2;
  421. for (i = 0; i < 12; i++)
  422. {
  423. for (j = 0; j < 12; j++)
  424. {
  425. y[j] = (y[j]*alpha + y1_const)>>16;
  426. }
  427. y += stride;
  428. }
  429. stride >>= 1;
  430. u += stride + 1;
  431. v += stride + 1;
  432. for (i = 0; i < 6; i++)
  433. {
  434. for (j = 0; j < 6; j++)
  435. {
  436. u[j] = (u[j]*alpha + u1_const)>>16;
  437. v[j] = (v[j]*alpha + v1_const)>>16;
  438. }
  439. u += stride;
  440. v += stride;
  441. }
  442. }
  443. /* Blend only the edge of the macro block. Leave center
  444. * unblended to allow for other visualizations to be layered.
  445. */
  446. void vp8_blend_mb_outer_c (unsigned char *y, unsigned char *u, unsigned char *v,
  447. int y1, int u1, int v1, int alpha, int stride)
  448. {
  449. int i, j;
  450. int y1_const = y1*((1<<16)-alpha);
  451. int u1_const = u1*((1<<16)-alpha);
  452. int v1_const = v1*((1<<16)-alpha);
  453. for (i = 0; i < 2; i++)
  454. {
  455. for (j = 0; j < 16; j++)
  456. {
  457. y[j] = (y[j]*alpha + y1_const)>>16;
  458. }
  459. y += stride;
  460. }
  461. for (i = 0; i < 12; i++)
  462. {
  463. y[0] = (y[0]*alpha + y1_const)>>16;
  464. y[1] = (y[1]*alpha + y1_const)>>16;
  465. y[14] = (y[14]*alpha + y1_const)>>16;
  466. y[15] = (y[15]*alpha + y1_const)>>16;
  467. y += stride;
  468. }
  469. for (i = 0; i < 2; i++)
  470. {
  471. for (j = 0; j < 16; j++)
  472. {
  473. y[j] = (y[j]*alpha + y1_const)>>16;
  474. }
  475. y += stride;
  476. }
  477. stride >>= 1;
  478. for (j = 0; j < 8; j++)
  479. {
  480. u[j] = (u[j]*alpha + u1_const)>>16;
  481. v[j] = (v[j]*alpha + v1_const)>>16;
  482. }
  483. u += stride;
  484. v += stride;
  485. for (i = 0; i < 6; i++)
  486. {
  487. u[0] = (u[0]*alpha + u1_const)>>16;
  488. v[0] = (v[0]*alpha + v1_const)>>16;
  489. u[7] = (u[7]*alpha + u1_const)>>16;
  490. v[7] = (v[7]*alpha + v1_const)>>16;
  491. u += stride;
  492. v += stride;
  493. }
  494. for (j = 0; j < 8; j++)
  495. {
  496. u[j] = (u[j]*alpha + u1_const)>>16;
  497. v[j] = (v[j]*alpha + v1_const)>>16;
  498. }
  499. }
  500. void vp8_blend_b_c (unsigned char *y, unsigned char *u, unsigned char *v,
  501. int y1, int u1, int v1, int alpha, int stride)
  502. {
  503. int i, j;
  504. int y1_const = y1*((1<<16)-alpha);
  505. int u1_const = u1*((1<<16)-alpha);
  506. int v1_const = v1*((1<<16)-alpha);
  507. for (i = 0; i < 4; i++)
  508. {
  509. for (j = 0; j < 4; j++)
  510. {
  511. y[j] = (y[j]*alpha + y1_const)>>16;
  512. }
  513. y += stride;
  514. }
  515. stride >>= 1;
  516. for (i = 0; i < 2; i++)
  517. {
  518. for (j = 0; j < 2; j++)
  519. {
  520. u[j] = (u[j]*alpha + u1_const)>>16;
  521. v[j] = (v[j]*alpha + v1_const)>>16;
  522. }
  523. u += stride;
  524. v += stride;
  525. }
  526. }
  527. static void constrain_line (int x0, int *x1, int y0, int *y1, int width, int height)
  528. {
  529. int dx;
  530. int dy;
  531. if (*x1 > width)
  532. {
  533. dx = *x1 - x0;
  534. dy = *y1 - y0;
  535. *x1 = width;
  536. if (dx)
  537. *y1 = ((width-x0)*dy)/dx + y0;
  538. }
  539. if (*x1 < 0)
  540. {
  541. dx = *x1 - x0;
  542. dy = *y1 - y0;
  543. *x1 = 0;
  544. if (dx)
  545. *y1 = ((0-x0)*dy)/dx + y0;
  546. }
  547. if (*y1 > height)
  548. {
  549. dx = *x1 - x0;
  550. dy = *y1 - y0;
  551. *y1 = height;
  552. if (dy)
  553. *x1 = ((height-y0)*dx)/dy + x0;
  554. }
  555. if (*y1 < 0)
  556. {
  557. dx = *x1 - x0;
  558. dy = *y1 - y0;
  559. *y1 = 0;
  560. if (dy)
  561. *x1 = ((0-y0)*dx)/dy + x0;
  562. }
  563. }
  564. #if CONFIG_RUNTIME_CPU_DETECT
  565. #define RTCD_VTABLE(oci) (&(oci)->rtcd.postproc)
  566. #else
  567. #define RTCD_VTABLE(oci) NULL
  568. #endif
  569. int vp8_post_proc_frame(VP8_COMMON *oci, YV12_BUFFER_CONFIG *dest, vp8_ppflags_t *ppflags)
  570. {
  571. int q = oci->filter_level * 10 / 6;
  572. int flags = ppflags->post_proc_flag;
  573. int deblock_level = ppflags->deblocking_level;
  574. int noise_level = ppflags->noise_level;
  575. if (!oci->frame_to_show)
  576. return -1;
  577. if (q > 63)
  578. q = 63;
  579. if (!flags)
  580. {
  581. *dest = *oci->frame_to_show;
  582. /* handle problem with extending borders */
  583. dest->y_width = oci->Width;
  584. dest->y_height = oci->Height;
  585. dest->uv_height = dest->y_height / 2;
  586. return 0;
  587. }
  588. #if ARCH_X86||ARCH_X86_64
  589. vpx_reset_mmx_state();
  590. #endif
  591. if (flags & VP8D_DEMACROBLOCK)
  592. {
  593. vp8_deblock_and_de_macro_block(oci->frame_to_show, &oci->post_proc_buffer,
  594. q + (deblock_level - 5) * 10, 1, 0, RTCD_VTABLE(oci));
  595. }
  596. else if (flags & VP8D_DEBLOCK)
  597. {
  598. vp8_deblock(oci->frame_to_show, &oci->post_proc_buffer,
  599. q, 1, 0, RTCD_VTABLE(oci));
  600. }
  601. else
  602. {
  603. vp8_yv12_copy_frame_ptr(oci->frame_to_show, &oci->post_proc_buffer);
  604. }
  605. if (flags & VP8D_ADDNOISE)
  606. {
  607. if (oci->postproc_state.last_q != q
  608. || oci->postproc_state.last_noise != noise_level)
  609. {
  610. fillrd(&oci->postproc_state, 63 - q, noise_level);
  611. }
  612. POSTPROC_INVOKE(RTCD_VTABLE(oci), addnoise)
  613. (oci->post_proc_buffer.y_buffer,
  614. oci->postproc_state.noise,
  615. oci->postproc_state.blackclamp,
  616. oci->postproc_state.whiteclamp,
  617. oci->postproc_state.bothclamp,
  618. oci->post_proc_buffer.y_width, oci->post_proc_buffer.y_height,
  619. oci->post_proc_buffer.y_stride);
  620. }
  621. #if CONFIG_POSTPROC_VISUALIZER
  622. if (flags & VP8D_DEBUG_TXT_FRAME_INFO)
  623. {
  624. char message[512];
  625. sprintf(message, "F%1dG%1dQ%3dF%3dP%d_s%dx%d",
  626. (oci->frame_type == KEY_FRAME),
  627. oci->refresh_golden_frame,
  628. oci->base_qindex,
  629. oci->filter_level,
  630. flags,
  631. oci->mb_cols, oci->mb_rows);
  632. vp8_blit_text(message, oci->post_proc_buffer.y_buffer, oci->post_proc_buffer.y_stride);
  633. }
  634. if (flags & VP8D_DEBUG_TXT_MBLK_MODES)
  635. {
  636. int i, j;
  637. unsigned char *y_ptr;
  638. YV12_BUFFER_CONFIG *post = &oci->post_proc_buffer;
  639. int mb_rows = post->y_height >> 4;
  640. int mb_cols = post->y_width >> 4;
  641. int mb_index = 0;
  642. MODE_INFO *mi = oci->mi;
  643. y_ptr = post->y_buffer + 4 * post->y_stride + 4;
  644. /* vp8_filter each macro block */
  645. for (i = 0; i < mb_rows; i++)
  646. {
  647. for (j = 0; j < mb_cols; j++)
  648. {
  649. char zz[4];
  650. sprintf(zz, "%c", mi[mb_index].mbmi.mode + 'a');
  651. vp8_blit_text(zz, y_ptr, post->y_stride);
  652. mb_index ++;
  653. y_ptr += 16;
  654. }
  655. mb_index ++; /* border */
  656. y_ptr += post->y_stride * 16 - post->y_width;
  657. }
  658. }
  659. if (flags & VP8D_DEBUG_TXT_DC_DIFF)
  660. {
  661. int i, j;
  662. unsigned char *y_ptr;
  663. YV12_BUFFER_CONFIG *post = &oci->post_proc_buffer;
  664. int mb_rows = post->y_height >> 4;
  665. int mb_cols = post->y_width >> 4;
  666. int mb_index = 0;
  667. MODE_INFO *mi = oci->mi;
  668. y_ptr = post->y_buffer + 4 * post->y_stride + 4;
  669. /* vp8_filter each macro block */
  670. for (i = 0; i < mb_rows; i++)
  671. {
  672. for (j = 0; j < mb_cols; j++)
  673. {
  674. char zz[4];
  675. int dc_diff = !(mi[mb_index].mbmi.mode != B_PRED &&
  676. mi[mb_index].mbmi.mode != SPLITMV &&
  677. mi[mb_index].mbmi.mb_skip_coeff);
  678. if (oci->frame_type == KEY_FRAME)
  679. sprintf(zz, "a");
  680. else
  681. sprintf(zz, "%c", dc_diff + '0');
  682. vp8_blit_text(zz, y_ptr, post->y_stride);
  683. mb_index ++;
  684. y_ptr += 16;
  685. }
  686. mb_index ++; /* border */
  687. y_ptr += post->y_stride * 16 - post->y_width;
  688. }
  689. }
  690. if (flags & VP8D_DEBUG_TXT_RATE_INFO)
  691. {
  692. char message[512];
  693. sprintf(message, "Bitrate: %10.2f frame_rate: %10.2f ", oci->bitrate, oci->framerate);
  694. vp8_blit_text(message, oci->post_proc_buffer.y_buffer, oci->post_proc_buffer.y_stride);
  695. }
  696. /* Draw motion vectors */
  697. if ((flags & VP8D_DEBUG_DRAW_MV) && ppflags->display_mv_flag)
  698. {
  699. YV12_BUFFER_CONFIG *post = &oci->post_proc_buffer;
  700. int width = post->y_width;
  701. int height = post->y_height;
  702. unsigned char *y_buffer = oci->post_proc_buffer.y_buffer;
  703. int y_stride = oci->post_proc_buffer.y_stride;
  704. MODE_INFO *mi = oci->mi;
  705. int x0, y0;
  706. for (y0 = 0; y0 < height; y0 += 16)
  707. {
  708. for (x0 = 0; x0 < width; x0 += 16)
  709. {
  710. int x1, y1;
  711. if (!(ppflags->display_mv_flag & (1<<mi->mbmi.mode)))
  712. {
  713. mi++;
  714. continue;
  715. }
  716. if (mi->mbmi.mode == SPLITMV)
  717. {
  718. switch (mi->mbmi.partitioning)
  719. {
  720. case 0 : /* mv_top_bottom */
  721. {
  722. union b_mode_info *bmi = &mi->bmi[0];
  723. MV *mv = &bmi->mv.as_mv;
  724. x1 = x0 + 8 + (mv->col >> 3);
  725. y1 = y0 + 4 + (mv->row >> 3);
  726. constrain_line (x0+8, &x1, y0+4, &y1, width, height);
  727. vp8_blit_line (x0+8, x1, y0+4, y1, y_buffer, y_stride);
  728. bmi = &mi->bmi[8];
  729. x1 = x0 + 8 + (mv->col >> 3);
  730. y1 = y0 +12 + (mv->row >> 3);
  731. constrain_line (x0+8, &x1, y0+12, &y1, width, height);
  732. vp8_blit_line (x0+8, x1, y0+12, y1, y_buffer, y_stride);
  733. break;
  734. }
  735. case 1 : /* mv_left_right */
  736. {
  737. union b_mode_info *bmi = &mi->bmi[0];
  738. MV *mv = &bmi->mv.as_mv;
  739. x1 = x0 + 4 + (mv->col >> 3);
  740. y1 = y0 + 8 + (mv->row >> 3);
  741. constrain_line (x0+4, &x1, y0+8, &y1, width, height);
  742. vp8_blit_line (x0+4, x1, y0+8, y1, y_buffer, y_stride);
  743. bmi = &mi->bmi[2];
  744. x1 = x0 +12 + (mv->col >> 3);
  745. y1 = y0 + 8 + (mv->row >> 3);
  746. constrain_line (x0+12, &x1, y0+8, &y1, width, height);
  747. vp8_blit_line (x0+12, x1, y0+8, y1, y_buffer, y_stride);
  748. break;
  749. }
  750. case 2 : /* mv_quarters */
  751. {
  752. union b_mode_info *bmi = &mi->bmi[0];
  753. MV *mv = &bmi->mv.as_mv;
  754. x1 = x0 + 4 + (mv->col >> 3);
  755. y1 = y0 + 4 + (mv->row >> 3);
  756. constrain_line (x0+4, &x1, y0+4, &y1, width, height);
  757. vp8_blit_line (x0+4, x1, y0+4, y1, y_buffer, y_stride);
  758. bmi = &mi->bmi[2];
  759. x1 = x0 +12 + (mv->col >> 3);
  760. y1 = y0 + 4 + (mv->row >> 3);
  761. constrain_line (x0+12, &x1, y0+4, &y1, width, height);
  762. vp8_blit_line (x0+12, x1, y0+4, y1, y_buffer, y_stride);
  763. bmi = &mi->bmi[8];
  764. x1 = x0 + 4 + (mv->col >> 3);
  765. y1 = y0 +12 + (mv->row >> 3);
  766. constrain_line (x0+4, &x1, y0+12, &y1, width, height);
  767. vp8_blit_line (x0+4, x1, y0+12, y1, y_buffer, y_stride);
  768. bmi = &mi->bmi[10];
  769. x1 = x0 +12 + (mv->col >> 3);
  770. y1 = y0 +12 + (mv->row >> 3);
  771. constrain_line (x0+12, &x1, y0+12, &y1, width, height);
  772. vp8_blit_line (x0+12, x1, y0+12, y1, y_buffer, y_stride);
  773. break;
  774. }
  775. default :
  776. {
  777. union b_mode_info *bmi = mi->bmi;
  778. int bx0, by0;
  779. for (by0 = y0; by0 < (y0+16); by0 += 4)
  780. {
  781. for (bx0 = x0; bx0 < (x0+16); bx0 += 4)
  782. {
  783. MV *mv = &bmi->mv.as_mv;
  784. x1 = bx0 + 2 + (mv->col >> 3);
  785. y1 = by0 + 2 + (mv->row >> 3);
  786. constrain_line (bx0+2, &x1, by0+2, &y1, width, height);
  787. vp8_blit_line (bx0+2, x1, by0+2, y1, y_buffer, y_stride);
  788. bmi++;
  789. }
  790. }
  791. }
  792. }
  793. }
  794. else if (mi->mbmi.mode >= NEARESTMV)
  795. {
  796. MV *mv = &mi->mbmi.mv.as_mv;
  797. const int lx0 = x0 + 8;
  798. const int ly0 = y0 + 8;
  799. x1 = lx0 + (mv->col >> 3);
  800. y1 = ly0 + (mv->row >> 3);
  801. if (x1 != lx0 && y1 != ly0)
  802. {
  803. constrain_line (lx0, &x1, ly0-1, &y1, width, height);
  804. vp8_blit_line (lx0, x1, ly0-1, y1, y_buffer, y_stride);
  805. constrain_line (lx0, &x1, ly0+1, &y1, width, height);
  806. vp8_blit_line (lx0, x1, ly0+1, y1, y_buffer, y_stride);
  807. }
  808. else
  809. vp8_blit_line (lx0, x1, ly0, y1, y_buffer, y_stride);
  810. }
  811. mi++;
  812. }
  813. mi++;
  814. }
  815. }
  816. /* Color in block modes */
  817. if ((flags & VP8D_DEBUG_CLR_BLK_MODES)
  818. && (ppflags->display_mb_modes_flag || ppflags->display_b_modes_flag))
  819. {
  820. int y, x;
  821. YV12_BUFFER_CONFIG *post = &oci->post_proc_buffer;
  822. int width = post->y_width;
  823. int height = post->y_height;
  824. unsigned char *y_ptr = oci->post_proc_buffer.y_buffer;
  825. unsigned char *u_ptr = oci->post_proc_buffer.u_buffer;
  826. unsigned char *v_ptr = oci->post_proc_buffer.v_buffer;
  827. int y_stride = oci->post_proc_buffer.y_stride;
  828. MODE_INFO *mi = oci->mi;
  829. for (y = 0; y < height; y += 16)
  830. {
  831. for (x = 0; x < width; x += 16)
  832. {
  833. int Y = 0, U = 0, V = 0;
  834. if (mi->mbmi.mode == B_PRED &&
  835. ((ppflags->display_mb_modes_flag & B_PRED) || ppflags->display_b_modes_flag))
  836. {
  837. int by, bx;
  838. unsigned char *yl, *ul, *vl;
  839. union b_mode_info *bmi = mi->bmi;
  840. yl = y_ptr + x;
  841. ul = u_ptr + (x>>1);
  842. vl = v_ptr + (x>>1);
  843. for (by = 0; by < 16; by += 4)
  844. {
  845. for (bx = 0; bx < 16; bx += 4)
  846. {
  847. if ((ppflags->display_b_modes_flag & (1<<mi->mbmi.mode))
  848. || (ppflags->display_mb_modes_flag & B_PRED))
  849. {
  850. Y = B_PREDICTION_MODE_colors[bmi->as_mode][0];
  851. U = B_PREDICTION_MODE_colors[bmi->as_mode][1];
  852. V = B_PREDICTION_MODE_colors[bmi->as_mode][2];
  853. POSTPROC_INVOKE(RTCD_VTABLE(oci), blend_b)
  854. (yl+bx, ul+(bx>>1), vl+(bx>>1), Y, U, V, 0xc000, y_stride);
  855. }
  856. bmi++;
  857. }
  858. yl += y_stride*4;
  859. ul += y_stride*1;
  860. vl += y_stride*1;
  861. }
  862. }
  863. else if (ppflags->display_mb_modes_flag & (1<<mi->mbmi.mode))
  864. {
  865. Y = MB_PREDICTION_MODE_colors[mi->mbmi.mode][0];
  866. U = MB_PREDICTION_MODE_colors[mi->mbmi.mode][1];
  867. V = MB_PREDICTION_MODE_colors[mi->mbmi.mode][2];
  868. POSTPROC_INVOKE(RTCD_VTABLE(oci), blend_mb_inner)
  869. (y_ptr+x, u_ptr+(x>>1), v_ptr+(x>>1), Y, U, V, 0xc000, y_stride);
  870. }
  871. mi++;
  872. }
  873. y_ptr += y_stride*16;
  874. u_ptr += y_stride*4;
  875. v_ptr += y_stride*4;
  876. mi++;
  877. }
  878. }
  879. /* Color in frame reference blocks */
  880. if ((flags & VP8D_DEBUG_CLR_FRM_REF_BLKS) && ppflags->display_ref_frame_flag)
  881. {
  882. int y, x;
  883. YV12_BUFFER_CONFIG *post = &oci->post_proc_buffer;
  884. int width = post->y_width;
  885. int height = post->y_height;
  886. unsigned char *y_ptr = oci->post_proc_buffer.y_buffer;
  887. unsigned char *u_ptr = oci->post_proc_buffer.u_buffer;
  888. unsigned char *v_ptr = oci->post_proc_buffer.v_buffer;
  889. int y_stride = oci->post_proc_buffer.y_stride;
  890. MODE_INFO *mi = oci->mi;
  891. for (y = 0; y < height; y += 16)
  892. {
  893. for (x = 0; x < width; x +=16)
  894. {
  895. int Y = 0, U = 0, V = 0;
  896. if (ppflags->display_ref_frame_flag & (1<<mi->mbmi.ref_frame))
  897. {
  898. Y = MV_REFERENCE_FRAME_colors[mi->mbmi.ref_frame][0];
  899. U = MV_REFERENCE_FRAME_colors[mi->mbmi.ref_frame][1];
  900. V = MV_REFERENCE_FRAME_colors[mi->mbmi.ref_frame][2];
  901. POSTPROC_INVOKE(RTCD_VTABLE(oci), blend_mb_outer)
  902. (y_ptr+x, u_ptr+(x>>1), v_ptr+(x>>1), Y, U, V, 0xc000, y_stride);
  903. }
  904. mi++;
  905. }
  906. y_ptr += y_stride*16;
  907. u_ptr += y_stride*4;
  908. v_ptr += y_stride*4;
  909. mi++;
  910. }
  911. }
  912. #endif
  913. *dest = oci->post_proc_buffer;
  914. /* handle problem with extending borders */
  915. dest->y_width = oci->Width;
  916. dest->y_height = oci->Height;
  917. dest->uv_height = dest->y_height / 2;
  918. return 0;
  919. }