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.

247 lines
6.7KB

  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 "bitstream.h"
  27. typedef struct PCXContext {
  28. AVFrame picture;
  29. } PCXContext;
  30. static 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 char *pcx_rle_decode(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(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. uint8_t *buf, int buf_size) {
  63. PCXContext * const s = avctx->priv_data;
  64. AVFrame *picture = data;
  65. AVFrame * const p = &s->picture;
  66. int xmin, ymin, xmax, ymax;
  67. unsigned int w, h, bits_per_pixel, bytes_per_line, nplanes, stride, y, x,
  68. bytes_per_scanline;
  69. uint8_t *ptr, *bufstart = buf;
  70. if (buf[0] != 0x0a || buf[1] > 5 || buf[1] == 1 || buf[2] != 1) {
  71. av_log(avctx, AV_LOG_ERROR, "this is not PCX encoded data\n");
  72. return -1;
  73. }
  74. xmin = AV_RL16(buf+ 4);
  75. ymin = AV_RL16(buf+ 6);
  76. xmax = AV_RL16(buf+ 8);
  77. ymax = AV_RL16(buf+10);
  78. if (xmax < xmin || ymax < ymin) {
  79. av_log(avctx, AV_LOG_ERROR, "invalid image dimensions\n");
  80. return -1;
  81. }
  82. w = xmax - xmin + 1;
  83. h = ymax - ymin + 1;
  84. bits_per_pixel = buf[3];
  85. bytes_per_line = AV_RL16(buf+66);
  86. nplanes = buf[65];
  87. bytes_per_scanline = nplanes * bytes_per_line;
  88. if (bytes_per_scanline < w * bits_per_pixel * nplanes / 8) {
  89. av_log(avctx, AV_LOG_ERROR, "PCX data is corrupted\n");
  90. return -1;
  91. }
  92. switch ((nplanes<<8) + bits_per_pixel) {
  93. case 0x0308:
  94. avctx->pix_fmt = PIX_FMT_RGB24;
  95. break;
  96. case 0x0108:
  97. case 0x0104:
  98. case 0x0102:
  99. case 0x0101:
  100. case 0x0401:
  101. case 0x0301:
  102. case 0x0201:
  103. avctx->pix_fmt = PIX_FMT_PAL8;
  104. break;
  105. default:
  106. av_log(avctx, AV_LOG_ERROR, "invalid PCX file\n");
  107. return -1;
  108. }
  109. buf += 128;
  110. if (p->data[0])
  111. avctx->release_buffer(avctx, p);
  112. if (avcodec_check_dimensions(avctx, w, h))
  113. return -1;
  114. if (w != avctx->width || h != avctx->height)
  115. avcodec_set_dimensions(avctx, w, h);
  116. if (avctx->get_buffer(avctx, p) < 0) {
  117. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  118. return -1;
  119. }
  120. p->pict_type = FF_I_TYPE;
  121. ptr = p->data[0];
  122. stride = p->linesize[0];
  123. if (nplanes == 3 && bits_per_pixel == 8) {
  124. uint8_t scanline[bytes_per_scanline];
  125. for (y=0; y<h; y++) {
  126. buf = pcx_rle_decode(buf, scanline, bytes_per_scanline);
  127. for (x=0; x<w; x++) {
  128. ptr[3*x ] = scanline[x ];
  129. ptr[3*x+1] = scanline[x+ bytes_per_line ];
  130. ptr[3*x+2] = scanline[x+(bytes_per_line<<1)];
  131. }
  132. ptr += stride;
  133. }
  134. } else if (nplanes == 1 && bits_per_pixel == 8) {
  135. uint8_t scanline[bytes_per_scanline];
  136. uint8_t *palstart = bufstart + buf_size - 769;
  137. for (y=0; y<h; y++, ptr+=stride) {
  138. buf = pcx_rle_decode(buf, scanline, bytes_per_scanline);
  139. memcpy(ptr, scanline, w);
  140. }
  141. if (buf != palstart) {
  142. av_log(avctx, AV_LOG_WARNING, "image data possibly corrupted\n");
  143. buf = palstart;
  144. }
  145. if (*buf++ != 12) {
  146. av_log(avctx, AV_LOG_ERROR, "expected palette after image data\n");
  147. return -1;
  148. }
  149. } else if (nplanes == 1) { /* all packed formats, max. 16 colors */
  150. uint8_t scanline[bytes_per_scanline];
  151. GetBitContext s;
  152. for (y=0; y<h; y++) {
  153. init_get_bits(&s, scanline, bytes_per_scanline<<3);
  154. buf = pcx_rle_decode(buf, scanline, bytes_per_scanline);
  155. for (x=0; x<w; x++)
  156. ptr[x] = get_bits(&s, bits_per_pixel);
  157. ptr += stride;
  158. }
  159. } else { /* planar, 4, 8 or 16 colors */
  160. uint8_t scanline[bytes_per_scanline];
  161. int i;
  162. for (y=0; y<h; y++) {
  163. buf = pcx_rle_decode(buf, scanline, bytes_per_scanline);
  164. for (x=0; x<w; x++) {
  165. int m = 0x80 >> (x&7), v = 0;
  166. for (i=nplanes - 1; i>=0; i--) {
  167. v <<= 1;
  168. v += !!(scanline[i*bytes_per_line + (x>>3)] & m);
  169. }
  170. ptr[x] = v;
  171. }
  172. ptr += stride;
  173. }
  174. }
  175. if (nplanes == 1 && bits_per_pixel == 8) {
  176. pcx_palette(&buf, (uint32_t *) p->data[1], 256);
  177. } else if (bits_per_pixel < 8) {
  178. uint8_t *palette = bufstart+16;
  179. pcx_palette(&palette, (uint32_t *) p->data[1], 16);
  180. }
  181. *picture = s->picture;
  182. *data_size = sizeof(AVFrame);
  183. return buf - bufstart;
  184. }
  185. static int pcx_end(AVCodecContext *avctx) {
  186. PCXContext *s = avctx->priv_data;
  187. if(s->picture.data[0])
  188. avctx->release_buffer(avctx, &s->picture);
  189. return 0;
  190. }
  191. AVCodec pcx_decoder = {
  192. "pcx",
  193. CODEC_TYPE_VIDEO,
  194. CODEC_ID_PCX,
  195. sizeof(PCXContext),
  196. pcx_init,
  197. NULL,
  198. pcx_end,
  199. pcx_decode_frame,
  200. 0,
  201. NULL
  202. };