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.

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