motionpixels_tablegen.h 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * Header file for hardcoded motionpixels RGB to YUV table
  3. *
  4. * Copyright (c) 2009 Reimar Döffinger <Reimar.Doeffinger@gmx.de>
  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. #ifndef MOTIONPIXELS_TABLEGEN_H
  23. #define MOTIONPIXELS_TABLEGEN_H
  24. #include <stdint.h>
  25. typedef struct YuvPixel {
  26. int8_t y, v, u;
  27. } YuvPixel;
  28. static int mp_yuv_to_rgb(int y, int v, int u, int clip_rgb) {
  29. static const uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
  30. int r, g, b;
  31. r = (1000 * y + 701 * v) / 1000;
  32. g = (1000 * y - 357 * v - 172 * u) / 1000;
  33. b = (1000 * y + 886 * u) / 1000;
  34. if (clip_rgb)
  35. return ((cm[r * 8] & 0xF8) << 7) | ((cm[g * 8] & 0xF8) << 2) | (cm[b * 8] >> 3);
  36. if ((unsigned)r < 32 && (unsigned)g < 32 && (unsigned)b < 32)
  37. return (r << 10) | (g << 5) | b;
  38. return 1 << 15;
  39. }
  40. #if CONFIG_HARDCODED_TABLES
  41. #define motionpixels_tableinit()
  42. #include "libavcodec/motionpixels_tables.h"
  43. #else
  44. static YuvPixel mp_rgb_yuv_table[1 << 15];
  45. static void mp_set_zero_yuv(YuvPixel *p)
  46. {
  47. int i, j;
  48. for (i = 0; i < 31; ++i) {
  49. for (j = 31; j > i; --j)
  50. if (!(p[j].u | p[j].v | p[j].y))
  51. p[j] = p[j - 1];
  52. for (j = 0; j < 31 - i; ++j)
  53. if (!(p[j].u | p[j].v | p[j].y))
  54. p[j] = p[j + 1];
  55. }
  56. }
  57. static void mp_build_rgb_yuv_table(YuvPixel *p)
  58. {
  59. int y, v, u, i;
  60. for (y = 0; y <= 31; ++y)
  61. for (v = -31; v <= 31; ++v)
  62. for (u = -31; u <= 31; ++u) {
  63. i = mp_yuv_to_rgb(y, v, u, 0);
  64. if (i < (1 << 15) && !(p[i].u | p[i].v | p[i].y)) {
  65. p[i].y = y;
  66. p[i].v = v;
  67. p[i].u = u;
  68. }
  69. }
  70. for (i = 0; i < 1024; ++i)
  71. mp_set_zero_yuv(p + i * 32);
  72. }
  73. static void motionpixels_tableinit(void)
  74. {
  75. if (!mp_rgb_yuv_table[0].u)
  76. mp_build_rgb_yuv_table(mp_rgb_yuv_table);
  77. }
  78. #endif /* CONFIG_HARDCODED_TABLES */
  79. #endif /* MOTIONPIXELS_TABLEGEN_H */