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.

235 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 FFmpeg.
  9. *
  10. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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 "bytestream.h"
  27. #include "get_bits.h"
  28. #include "internal.h"
  29. static void pcx_rle_decode(GetByteContext *gb, uint8_t *dst,
  30. unsigned int bytes_per_scanline, int compressed)
  31. {
  32. unsigned int i = 0;
  33. unsigned char run, value;
  34. if (compressed) {
  35. while (i<bytes_per_scanline) {
  36. run = 1;
  37. value = bytestream2_get_byte(gb);
  38. if (value >= 0xc0) {
  39. run = value & 0x3f;
  40. value = bytestream2_get_byte(gb);
  41. }
  42. while (i<bytes_per_scanline && run--)
  43. dst[i++] = value;
  44. }
  45. } else {
  46. bytestream2_get_buffer(gb, dst, bytes_per_scanline);
  47. }
  48. }
  49. static void pcx_palette(GetByteContext *gb, uint32_t *dst, int pallen)
  50. {
  51. int i;
  52. pallen = FFMIN(pallen, bytestream2_get_bytes_left(gb) / 3);
  53. for (i=0; i<pallen; i++)
  54. *dst++ = 0xFF000000 | bytestream2_get_be24u(gb);
  55. if (pallen < 256)
  56. memset(dst, 0, (256 - pallen) * sizeof(*dst));
  57. }
  58. static int pcx_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
  59. AVPacket *avpkt) {
  60. GetByteContext gb;
  61. AVFrame * const p = data;
  62. int compressed, xmin, ymin, xmax, ymax, ret;
  63. unsigned int w, h, bits_per_pixel, bytes_per_line, nplanes, stride, y, x,
  64. bytes_per_scanline;
  65. uint8_t *ptr, *scanline;
  66. if (avpkt->size < 128)
  67. return AVERROR_INVALIDDATA;
  68. bytestream2_init(&gb, avpkt->data, avpkt->size);
  69. if (bytestream2_get_byteu(&gb) != 0x0a || bytestream2_get_byteu(&gb) > 5) {
  70. av_log(avctx, AV_LOG_ERROR, "this is not PCX encoded data\n");
  71. return AVERROR_INVALIDDATA;
  72. }
  73. compressed = bytestream2_get_byteu(&gb);
  74. bits_per_pixel = bytestream2_get_byteu(&gb);
  75. xmin = bytestream2_get_le16u(&gb);
  76. ymin = bytestream2_get_le16u(&gb);
  77. xmax = bytestream2_get_le16u(&gb);
  78. ymax = bytestream2_get_le16u(&gb);
  79. avctx->sample_aspect_ratio.num = bytestream2_get_le16u(&gb);
  80. avctx->sample_aspect_ratio.den = bytestream2_get_le16u(&gb);
  81. if (xmax < xmin || ymax < ymin) {
  82. av_log(avctx, AV_LOG_ERROR, "invalid image dimensions\n");
  83. return AVERROR_INVALIDDATA;
  84. }
  85. w = xmax - xmin + 1;
  86. h = ymax - ymin + 1;
  87. bytestream2_skipu(&gb, 49);
  88. nplanes = bytestream2_get_byteu(&gb);
  89. bytes_per_line = bytestream2_get_le16u(&gb);
  90. bytes_per_scanline = nplanes * bytes_per_line;
  91. if (bytes_per_scanline < (w * bits_per_pixel * nplanes + 7) / 8) {
  92. av_log(avctx, AV_LOG_ERROR, "PCX data is corrupted\n");
  93. return AVERROR_INVALIDDATA;
  94. }
  95. switch ((nplanes<<8) + bits_per_pixel) {
  96. case 0x0308:
  97. avctx->pix_fmt = AV_PIX_FMT_RGB24;
  98. break;
  99. case 0x0108:
  100. case 0x0104:
  101. case 0x0102:
  102. case 0x0101:
  103. case 0x0401:
  104. case 0x0301:
  105. case 0x0201:
  106. avctx->pix_fmt = AV_PIX_FMT_PAL8;
  107. break;
  108. default:
  109. av_log(avctx, AV_LOG_ERROR, "invalid PCX file\n");
  110. return AVERROR_INVALIDDATA;
  111. }
  112. bytestream2_skipu(&gb, 60);
  113. if ((ret = av_image_check_size(w, h, 0, avctx)) < 0)
  114. return ret;
  115. if (w != avctx->width || h != avctx->height)
  116. avcodec_set_dimensions(avctx, w, h);
  117. if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
  118. return ret;
  119. p->pict_type = AV_PICTURE_TYPE_I;
  120. ptr = p->data[0];
  121. stride = p->linesize[0];
  122. scanline = av_malloc(bytes_per_scanline + FF_INPUT_BUFFER_PADDING_SIZE);
  123. if (!scanline)
  124. return AVERROR(ENOMEM);
  125. if (nplanes == 3 && bits_per_pixel == 8) {
  126. for (y=0; y<h; y++) {
  127. pcx_rle_decode(&gb, scanline, bytes_per_scanline, compressed);
  128. for (x=0; x<w; x++) {
  129. ptr[3*x ] = scanline[x ];
  130. ptr[3*x+1] = scanline[x+ bytes_per_line ];
  131. ptr[3*x+2] = scanline[x+(bytes_per_line<<1)];
  132. }
  133. ptr += stride;
  134. }
  135. } else if (nplanes == 1 && bits_per_pixel == 8) {
  136. int palstart = avpkt->size - 769;
  137. for (y=0; y<h; y++, ptr+=stride) {
  138. pcx_rle_decode(&gb, scanline, bytes_per_scanline, compressed);
  139. memcpy(ptr, scanline, w);
  140. }
  141. if (bytestream2_tell(&gb) != palstart) {
  142. av_log(avctx, AV_LOG_WARNING, "image data possibly corrupted\n");
  143. bytestream2_seek(&gb, palstart, SEEK_SET);
  144. }
  145. if (bytestream2_get_byte(&gb) != 12) {
  146. av_log(avctx, AV_LOG_ERROR, "expected palette after image data\n");
  147. ret = AVERROR_INVALIDDATA;
  148. goto end;
  149. }
  150. } else if (nplanes == 1) { /* all packed formats, max. 16 colors */
  151. GetBitContext s;
  152. for (y=0; y<h; y++) {
  153. init_get_bits8(&s, scanline, bytes_per_scanline);
  154. pcx_rle_decode(&gb, scanline, bytes_per_scanline, compressed);
  155. for (x=0; x<w; x++)
  156. ptr[x] = get_bits(&s, bits_per_pixel);
  157. ptr += stride;
  158. }
  159. } else { /* planar, 4, 8 or 16 colors */
  160. int i;
  161. for (y=0; y<h; y++) {
  162. pcx_rle_decode(&gb, scanline, bytes_per_scanline, compressed);
  163. for (x=0; x<w; x++) {
  164. int m = 0x80 >> (x&7), v = 0;
  165. for (i=nplanes - 1; i>=0; i--) {
  166. v <<= 1;
  167. v += !!(scanline[i*bytes_per_line + (x>>3)] & m);
  168. }
  169. ptr[x] = v;
  170. }
  171. ptr += stride;
  172. }
  173. }
  174. ret = bytestream2_tell(&gb);
  175. if (nplanes == 1 && bits_per_pixel == 8) {
  176. pcx_palette(&gb, (uint32_t *) p->data[1], 256);
  177. ret += 256 * 3;
  178. } else if (bits_per_pixel * nplanes == 1) {
  179. AV_WN32A(p->data[1] , 0xFF000000);
  180. AV_WN32A(p->data[1]+4, 0xFFFFFFFF);
  181. } else if (bits_per_pixel < 8) {
  182. bytestream2_seek(&gb, 16, SEEK_SET);
  183. pcx_palette(&gb, (uint32_t *) p->data[1], 16);
  184. }
  185. *got_frame = 1;
  186. end:
  187. av_free(scanline);
  188. return ret;
  189. }
  190. AVCodec ff_pcx_decoder = {
  191. .name = "pcx",
  192. .type = AVMEDIA_TYPE_VIDEO,
  193. .id = AV_CODEC_ID_PCX,
  194. .decode = pcx_decode_frame,
  195. .capabilities = CODEC_CAP_DR1,
  196. .long_name = NULL_IF_CONFIG_SMALL("PC Paintbrush PCX image"),
  197. };