motionpixels_tablegen.h 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * Header file for hardcoded motion pixels RGB to YUV table
  3. *
  4. * Copyright (c) 2009 Reimar Döffinger <Reimar.Doeffinger@gmx.de>
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #ifndef AVCODEC_MOTIONPIXELS_TABLEGEN_H
  23. #define AVCODEC_MOTIONPIXELS_TABLEGEN_H
  24. #include <stdint.h>
  25. #include "libavutil/attributes.h"
  26. typedef struct YuvPixel {
  27. int8_t y, v, u;
  28. } YuvPixel;
  29. static int mp_yuv_to_rgb(int y, int v, int u, int clip_rgb) {
  30. const uint8_t *cm = ff_crop_tab + MAX_NEG_CROP;
  31. int r, g, b;
  32. r = (1000 * y + 701 * v) / 1000;
  33. g = (1000 * y - 357 * v - 172 * u) / 1000;
  34. b = (1000 * y + 886 * u) / 1000;
  35. if (clip_rgb)
  36. return ((cm[r * 8] & 0xF8) << 7) | ((cm[g * 8] & 0xF8) << 2) | (cm[b * 8] >> 3);
  37. if ((unsigned)r < 32 && (unsigned)g < 32 && (unsigned)b < 32)
  38. return (r << 10) | (g << 5) | b;
  39. return 1 << 15;
  40. }
  41. #if CONFIG_HARDCODED_TABLES
  42. #define motionpixels_tableinit()
  43. #include "libavcodec/motionpixels_tables.h"
  44. #else
  45. static YuvPixel mp_rgb_yuv_table[1 << 15];
  46. static av_cold void mp_set_zero_yuv(YuvPixel *p)
  47. {
  48. int i, j;
  49. for (i = 0; i < 31; ++i) {
  50. for (j = 31; j > i; --j)
  51. if (!(p[j].u | p[j].v | p[j].y))
  52. p[j] = p[j - 1];
  53. for (j = 0; j < 31 - i; ++j)
  54. if (!(p[j].u | p[j].v | p[j].y))
  55. p[j] = p[j + 1];
  56. }
  57. }
  58. static av_cold void mp_build_rgb_yuv_table(YuvPixel *p)
  59. {
  60. int y, v, u, i;
  61. for (y = 0; y <= 31; ++y)
  62. for (v = -31; v <= 31; ++v)
  63. for (u = -31; u <= 31; ++u) {
  64. i = mp_yuv_to_rgb(y, v, u, 0);
  65. if (i < (1 << 15) && !(p[i].u | p[i].v | p[i].y)) {
  66. p[i].y = y;
  67. p[i].v = v;
  68. p[i].u = u;
  69. }
  70. }
  71. for (i = 0; i < 1024; ++i)
  72. mp_set_zero_yuv(p + i * 32);
  73. }
  74. static av_cold void motionpixels_tableinit(void)
  75. {
  76. if (!mp_rgb_yuv_table[0].u)
  77. mp_build_rgb_yuv_table(mp_rgb_yuv_table);
  78. }
  79. #endif /* CONFIG_HARDCODED_TABLES */
  80. #endif /* AVCODEC_MOTIONPIXELS_TABLEGEN_H */