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.

248 lines
7.9KB

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