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.

117 lines
4.3KB

  1. /*
  2. * QuickTime palette handling
  3. * Copyright (c) 2001 Fabrice Bellard
  4. * Copyright (c) 2009 Baptiste Coudurier <baptiste dot coudurier at gmail dot com>
  5. * Copyright (c) 2015 Mats Peterson
  6. *
  7. * This file is part of FFmpeg.
  8. *
  9. * FFmpeg is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2.1 of the License, or (at your option) any later version.
  13. *
  14. * FFmpeg is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with FFmpeg; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. */
  23. #include <stdio.h>
  24. #include <stdint.h>
  25. #include "avformat.h"
  26. #include "libavutil/intreadwrite.h"
  27. #include "qtpalette.h"
  28. int ff_get_qtpalette(int codec_id, AVIOContext *pb, uint32_t *palette)
  29. {
  30. int tmp, bit_depth, color_table_id, greyscale, i;
  31. avio_seek(pb, 82, SEEK_CUR);
  32. /* Get the bit depth and greyscale state */
  33. tmp = avio_rb16(pb);
  34. bit_depth = tmp & 0x1F;
  35. greyscale = tmp & 0x20;
  36. /* Get the color table ID */
  37. color_table_id = avio_rb16(pb);
  38. /* Do not create a greyscale palette for Cinepak */
  39. if (greyscale && codec_id == AV_CODEC_ID_CINEPAK)
  40. return 0;
  41. /* If the depth is 1, 2, 4, or 8 bpp, file is palettized. */
  42. if ((bit_depth == 1 || bit_depth == 2 || bit_depth == 4 || bit_depth == 8)) {
  43. uint32_t color_count, color_start, color_end;
  44. uint32_t r, g, b;
  45. /* Ignore the greyscale bit for 1-bit video and sample
  46. * descriptions containing a color table. */
  47. if (greyscale && bit_depth > 1 && color_table_id) {
  48. int color_index, color_dec;
  49. /* compute the greyscale palette */
  50. color_count = 1 << bit_depth;
  51. color_index = 255;
  52. color_dec = 256 / (color_count - 1);
  53. for (i = 0; i < color_count; i++) {
  54. r = g = b = color_index;
  55. palette[i] = (0xFFU << 24) | (r << 16) | (g << 8) | (b);
  56. color_index -= color_dec;
  57. if (color_index < 0)
  58. color_index = 0;
  59. }
  60. } else if (color_table_id) {
  61. /* The color table ID is non-zero. Interpret this as
  62. * being -1, which means use the default Macintosh
  63. * color table */
  64. const uint8_t *color_table;
  65. color_count = 1 << bit_depth;
  66. if (bit_depth == 1)
  67. color_table = ff_qt_default_palette_2;
  68. else if (bit_depth == 2)
  69. color_table = ff_qt_default_palette_4;
  70. else if (bit_depth == 4)
  71. color_table = ff_qt_default_palette_16;
  72. else
  73. color_table = ff_qt_default_palette_256;
  74. for (i = 0; i < color_count; i++) {
  75. r = color_table[i * 3 + 0];
  76. g = color_table[i * 3 + 1];
  77. b = color_table[i * 3 + 2];
  78. palette[i] = (0xFFU << 24) | (r << 16) | (g << 8) | (b);
  79. }
  80. } else {
  81. /* The color table ID is 0; the color table is in the sample
  82. * description */
  83. color_start = avio_rb32(pb);
  84. avio_rb16(pb); /* color table flags */
  85. color_end = avio_rb16(pb);
  86. if ((color_start <= 255) && (color_end <= 255)) {
  87. for (i = color_start; i <= color_end; i++) {
  88. /* Each color is made of four unsigned 16 bit integers. The
  89. * first integer is 0, the remaining integers are the red,
  90. * the green and the blue values. We only use the top 8 bit. */
  91. avio_skip(pb, 2);
  92. r = avio_r8(pb);
  93. avio_r8(pb);
  94. g = avio_r8(pb);
  95. avio_r8(pb);
  96. b = avio_r8(pb);
  97. avio_r8(pb);
  98. palette[i] = (0xFFU << 24) | (r << 16) | (g << 8) | (b);
  99. }
  100. }
  101. }
  102. return 1;
  103. }
  104. return 0;
  105. }