/libavcodec/pcx.c

http://github.com/FFmpeg/FFmpeg · C · 259 lines · 198 code · 38 blank · 23 comment · 62 complexity · 54abfe421fc5b462be4add52367b6159 MD5 · raw file

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