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.

270 lines
8.0KB

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