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.

244 lines
7.8KB

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