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.

242 lines
7.2KB

  1. /*
  2. * PC Paintbrush PCX (.pcx) image decoder
  3. * Copyright (c) 2007, 2008 Ivo van Poorten
  4. *
  5. * This decoder does not support CGA palettes. I am unable to find samples
  6. * and Netpbm cannot generate them.
  7. *
  8. * This file is part of Libav.
  9. *
  10. * Libav is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU Lesser General Public
  12. * License as published by the Free Software Foundation; either
  13. * version 2.1 of the License, or (at your option) any later version.
  14. *
  15. * Libav is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public
  21. * License along with Libav; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  23. */
  24. #include "libavutil/imgutils.h"
  25. #include "avcodec.h"
  26. #include "bitstream.h"
  27. #include "bytestream.h"
  28. #include "internal.h"
  29. #define PCX_HEADER_SIZE 128
  30. /**
  31. * @return advanced src pointer
  32. */
  33. static void pcx_rle_decode(GetByteContext *gb,
  34. uint8_t *dst,
  35. unsigned int bytes_per_scanline,
  36. int compressed)
  37. {
  38. unsigned int i = 0;
  39. unsigned char run, value;
  40. if (compressed) {
  41. while (i < bytes_per_scanline && bytestream2_get_bytes_left(gb)) {
  42. run = 1;
  43. value = bytestream2_get_byte(gb);
  44. if (value >= 0xc0 && bytestream2_get_bytes_left(gb)) {
  45. run = value & 0x3f;
  46. value = bytestream2_get_byte(gb);
  47. }
  48. while (i < bytes_per_scanline && run--)
  49. dst[i++] = value;
  50. }
  51. } else {
  52. bytestream2_get_buffer(gb, dst, bytes_per_scanline);
  53. }
  54. }
  55. static void pcx_palette(GetByteContext *gb, uint32_t *dst,
  56. unsigned int pallen)
  57. {
  58. unsigned int i;
  59. for (i = 0; i < pallen; i++)
  60. *dst++ = bytestream2_get_be24(gb);
  61. if (pallen < 256)
  62. memset(dst, 0, (256 - pallen) * sizeof(*dst));
  63. }
  64. static int pcx_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
  65. AVPacket *avpkt)
  66. {
  67. const uint8_t *buf = avpkt->data;
  68. int buf_size = avpkt->size;
  69. AVFrame *const p = data;
  70. GetByteContext gb;
  71. int compressed, xmin, ymin, xmax, ymax;
  72. unsigned int w, h, bits_per_pixel, bytes_per_line, nplanes, stride, y, x,
  73. bytes_per_scanline;
  74. uint8_t *ptr;
  75. uint8_t *scanline;
  76. int ret = -1;
  77. if (buf_size < PCX_HEADER_SIZE) {
  78. av_log(avctx, AV_LOG_ERROR, "Packet too small\n");
  79. return AVERROR_INVALIDDATA;
  80. }
  81. if (buf[0] != 0x0a || buf[1] > 5) {
  82. av_log(avctx, AV_LOG_ERROR, "this is not PCX encoded data\n");
  83. return AVERROR_INVALIDDATA;
  84. }
  85. compressed = buf[2];
  86. xmin = AV_RL16(buf + 4);
  87. ymin = AV_RL16(buf + 6);
  88. xmax = AV_RL16(buf + 8);
  89. ymax = AV_RL16(buf + 10);
  90. if (xmax < xmin || ymax < ymin) {
  91. av_log(avctx, AV_LOG_ERROR, "invalid image dimensions\n");
  92. return AVERROR_INVALIDDATA;
  93. }
  94. w = xmax - xmin + 1;
  95. h = ymax - ymin + 1;
  96. bits_per_pixel = buf[3];
  97. bytes_per_line = AV_RL16(buf + 66);
  98. nplanes = buf[65];
  99. bytes_per_scanline = nplanes * bytes_per_line;
  100. if (bytes_per_scanline < (w * bits_per_pixel * nplanes + 7) / 8 ||
  101. (!compressed && bytes_per_scanline > buf_size / h)) {
  102. av_log(avctx, AV_LOG_ERROR, "PCX data is corrupted\n");
  103. return AVERROR_INVALIDDATA;
  104. }
  105. switch ((nplanes << 8) + bits_per_pixel) {
  106. case 0x0308:
  107. avctx->pix_fmt = AV_PIX_FMT_RGB24;
  108. break;
  109. case 0x0108:
  110. case 0x0104:
  111. case 0x0102:
  112. case 0x0101:
  113. case 0x0401:
  114. case 0x0301:
  115. case 0x0201:
  116. avctx->pix_fmt = AV_PIX_FMT_PAL8;
  117. break;
  118. default:
  119. av_log(avctx, AV_LOG_ERROR, "invalid PCX file\n");
  120. return AVERROR_INVALIDDATA;
  121. }
  122. bytestream2_init(&gb, buf + PCX_HEADER_SIZE, buf_size - PCX_HEADER_SIZE);
  123. if ((ret = ff_set_dimensions(avctx, w, h)) < 0)
  124. return ret;
  125. if ((ret = ff_get_buffer(avctx, p, 0)) < 0) {
  126. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  127. return ret;
  128. }
  129. p->pict_type = AV_PICTURE_TYPE_I;
  130. ptr = p->data[0];
  131. stride = p->linesize[0];
  132. scanline = av_malloc(bytes_per_scanline + AV_INPUT_BUFFER_PADDING_SIZE);
  133. if (!scanline)
  134. return AVERROR(ENOMEM);
  135. if (nplanes == 3 && bits_per_pixel == 8) {
  136. for (y = 0; y < h; y++) {
  137. pcx_rle_decode(&gb, scanline, bytes_per_scanline, compressed);
  138. for (x = 0; x < w; x++) {
  139. ptr[3 * x] = scanline[x];
  140. ptr[3 * x + 1] = scanline[x + bytes_per_line];
  141. ptr[3 * x + 2] = scanline[x + (bytes_per_line << 1)];
  142. }
  143. ptr += stride;
  144. }
  145. } else if (nplanes == 1 && bits_per_pixel == 8) {
  146. for (y = 0; y < h; y++, ptr += stride) {
  147. pcx_rle_decode(&gb, scanline, bytes_per_scanline, compressed);
  148. memcpy(ptr, scanline, w);
  149. }
  150. if (bytestream2_get_byte(&gb) != 12) {
  151. av_log(avctx, AV_LOG_ERROR, "expected palette after image data\n");
  152. ret = avctx->err_recognition & AV_EF_EXPLODE ?
  153. AVERROR_INVALIDDATA : buf_size;
  154. goto end;
  155. }
  156. } else if (nplanes == 1) { /* all packed formats, max. 16 colors */
  157. BitstreamContext s;
  158. for (y = 0; y < h; y++) {
  159. bitstream_init8(&s, scanline, bytes_per_scanline);
  160. pcx_rle_decode(&gb, scanline, bytes_per_scanline, compressed);
  161. for (x = 0; x < w; x++)
  162. ptr[x] = bitstream_read(&s, bits_per_pixel);
  163. ptr += stride;
  164. }
  165. } else { /* planar, 4, 8 or 16 colors */
  166. int i;
  167. for (y = 0; y < h; y++) {
  168. pcx_rle_decode(&gb, scanline, bytes_per_scanline, compressed);
  169. for (x = 0; x < w; x++) {
  170. int m = 0x80 >> (x & 7), v = 0;
  171. for (i = nplanes - 1; i >= 0; i--) {
  172. v <<= 1;
  173. v += !!(scanline[i * bytes_per_line + (x >> 3)] & m);
  174. }
  175. ptr[x] = v;
  176. }
  177. ptr += stride;
  178. }
  179. }
  180. if (nplanes == 1 && bits_per_pixel == 8) {
  181. if (bytestream2_get_bytes_left(&gb) < 768) {
  182. av_log(avctx, AV_LOG_ERROR, "Palette truncated\n");
  183. ret = AVERROR_INVALIDDATA;
  184. goto end;
  185. }
  186. pcx_palette(&gb, (uint32_t *)p->data[1], 256);
  187. } else if (bits_per_pixel < 8) {
  188. GetByteContext gb1;
  189. bytestream2_init(&gb1, avpkt->data + 16, 48);
  190. pcx_palette(&gb1, (uint32_t *)p->data[1], 16);
  191. }
  192. *got_frame = 1;
  193. ret = bytestream2_tell(&gb);
  194. end:
  195. av_free(scanline);
  196. return ret;
  197. }
  198. AVCodec ff_pcx_decoder = {
  199. .name = "pcx",
  200. .long_name = NULL_IF_CONFIG_SMALL("PC Paintbrush PCX image"),
  201. .type = AVMEDIA_TYPE_VIDEO,
  202. .id = AV_CODEC_ID_PCX,
  203. .decode = pcx_decode_frame,
  204. .capabilities = AV_CODEC_CAP_DR1,
  205. };