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.

251 lines
6.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 "avcodec.h"
  25. #include "bytestream.h"
  26. #include "get_bits.h"
  27. typedef struct PCXContext {
  28. AVFrame picture;
  29. } PCXContext;
  30. static av_cold int pcx_init(AVCodecContext *avctx) {
  31. PCXContext *s = avctx->priv_data;
  32. avcodec_get_frame_defaults(&s->picture);
  33. avctx->coded_frame= &s->picture;
  34. return 0;
  35. }
  36. /**
  37. * @return advanced src pointer
  38. */
  39. static const uint8_t *pcx_rle_decode(const uint8_t *src, uint8_t *dst,
  40. unsigned int bytes_per_scanline) {
  41. unsigned int i = 0;
  42. unsigned char run, value;
  43. while (i<bytes_per_scanline) {
  44. run = 1;
  45. value = *src++;
  46. if (value >= 0xc0) {
  47. run = value & 0x3f;
  48. value = *src++;
  49. }
  50. while (i<bytes_per_scanline && run--)
  51. dst[i++] = value;
  52. }
  53. return src;
  54. }
  55. static void pcx_palette(const uint8_t **src, uint32_t *dst, unsigned int pallen) {
  56. unsigned int i;
  57. for (i=0; i<pallen; i++)
  58. *dst++ = bytestream_get_be24(src);
  59. memset(dst, 0, (256 - pallen) * sizeof(*dst));
  60. }
  61. static int pcx_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
  62. AVPacket *avpkt) {
  63. const uint8_t *buf = avpkt->data;
  64. int buf_size = avpkt->size;
  65. PCXContext * const s = avctx->priv_data;
  66. AVFrame *picture = data;
  67. AVFrame * const p = &s->picture;
  68. int xmin, ymin, xmax, ymax;
  69. unsigned int w, h, bits_per_pixel, bytes_per_line, nplanes, stride, y, x,
  70. bytes_per_scanline;
  71. uint8_t *ptr;
  72. uint8_t const *bufstart = buf;
  73. if (buf[0] != 0x0a || buf[1] > 5 || buf[1] == 1 || buf[2] != 1) {
  74. av_log(avctx, AV_LOG_ERROR, "this is not PCX encoded data\n");
  75. return -1;
  76. }
  77. xmin = AV_RL16(buf+ 4);
  78. ymin = AV_RL16(buf+ 6);
  79. xmax = AV_RL16(buf+ 8);
  80. ymax = AV_RL16(buf+10);
  81. if (xmax < xmin || ymax < ymin) {
  82. av_log(avctx, AV_LOG_ERROR, "invalid image dimensions\n");
  83. return -1;
  84. }
  85. w = xmax - xmin + 1;
  86. h = ymax - ymin + 1;
  87. bits_per_pixel = buf[3];
  88. bytes_per_line = AV_RL16(buf+66);
  89. nplanes = buf[65];
  90. bytes_per_scanline = nplanes * bytes_per_line;
  91. if (bytes_per_scanline < w * bits_per_pixel * nplanes / 8) {
  92. av_log(avctx, AV_LOG_ERROR, "PCX data is corrupted\n");
  93. return -1;
  94. }
  95. switch ((nplanes<<8) + bits_per_pixel) {
  96. case 0x0308:
  97. avctx->pix_fmt = 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 = PIX_FMT_PAL8;
  107. break;
  108. default:
  109. av_log(avctx, AV_LOG_ERROR, "invalid PCX file\n");
  110. return -1;
  111. }
  112. buf += 128;
  113. if (p->data[0])
  114. avctx->release_buffer(avctx, p);
  115. if (avcodec_check_dimensions(avctx, w, h))
  116. return -1;
  117. if (w != avctx->width || h != avctx->height)
  118. avcodec_set_dimensions(avctx, w, h);
  119. if (avctx->get_buffer(avctx, p) < 0) {
  120. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  121. return -1;
  122. }
  123. p->pict_type = FF_I_TYPE;
  124. ptr = p->data[0];
  125. stride = p->linesize[0];
  126. if (nplanes == 3 && bits_per_pixel == 8) {
  127. uint8_t scanline[bytes_per_scanline];
  128. for (y=0; y<h; y++) {
  129. buf = pcx_rle_decode(buf, scanline, bytes_per_scanline);
  130. for (x=0; x<w; x++) {
  131. ptr[3*x ] = scanline[x ];
  132. ptr[3*x+1] = scanline[x+ bytes_per_line ];
  133. ptr[3*x+2] = scanline[x+(bytes_per_line<<1)];
  134. }
  135. ptr += stride;
  136. }
  137. } else if (nplanes == 1 && bits_per_pixel == 8) {
  138. uint8_t scanline[bytes_per_scanline];
  139. const uint8_t *palstart = bufstart + buf_size - 769;
  140. for (y=0; y<h; y++, ptr+=stride) {
  141. buf = pcx_rle_decode(buf, scanline, bytes_per_scanline);
  142. memcpy(ptr, scanline, w);
  143. }
  144. if (buf != palstart) {
  145. av_log(avctx, AV_LOG_WARNING, "image data possibly corrupted\n");
  146. buf = palstart;
  147. }
  148. if (*buf++ != 12) {
  149. av_log(avctx, AV_LOG_ERROR, "expected palette after image data\n");
  150. return -1;
  151. }
  152. } else if (nplanes == 1) { /* all packed formats, max. 16 colors */
  153. uint8_t scanline[bytes_per_scanline];
  154. GetBitContext s;
  155. for (y=0; y<h; y++) {
  156. init_get_bits(&s, scanline, bytes_per_scanline<<3);
  157. buf = pcx_rle_decode(buf, scanline, bytes_per_scanline);
  158. for (x=0; x<w; x++)
  159. ptr[x] = get_bits(&s, bits_per_pixel);
  160. ptr += stride;
  161. }
  162. } else { /* planar, 4, 8 or 16 colors */
  163. uint8_t scanline[bytes_per_scanline];
  164. int i;
  165. for (y=0; y<h; y++) {
  166. buf = pcx_rle_decode(buf, scanline, bytes_per_scanline);
  167. for (x=0; x<w; x++) {
  168. int m = 0x80 >> (x&7), v = 0;
  169. for (i=nplanes - 1; i>=0; i--) {
  170. v <<= 1;
  171. v += !!(scanline[i*bytes_per_line + (x>>3)] & m);
  172. }
  173. ptr[x] = v;
  174. }
  175. ptr += stride;
  176. }
  177. }
  178. if (nplanes == 1 && bits_per_pixel == 8) {
  179. pcx_palette(&buf, (uint32_t *) p->data[1], 256);
  180. } else if (bits_per_pixel < 8) {
  181. const uint8_t *palette = bufstart+16;
  182. pcx_palette(&palette, (uint32_t *) p->data[1], 16);
  183. }
  184. *picture = s->picture;
  185. *data_size = sizeof(AVFrame);
  186. return buf - bufstart;
  187. }
  188. static av_cold int pcx_end(AVCodecContext *avctx) {
  189. PCXContext *s = avctx->priv_data;
  190. if(s->picture.data[0])
  191. avctx->release_buffer(avctx, &s->picture);
  192. return 0;
  193. }
  194. AVCodec pcx_decoder = {
  195. "pcx",
  196. CODEC_TYPE_VIDEO,
  197. CODEC_ID_PCX,
  198. sizeof(PCXContext),
  199. pcx_init,
  200. NULL,
  201. pcx_end,
  202. pcx_decode_frame,
  203. CODEC_CAP_DR1,
  204. NULL,
  205. .long_name = NULL_IF_CONFIG_SMALL("PC Paintbrush PCX image"),
  206. };