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.

254 lines
8.1KB

  1. /*
  2. * Pictor/PC Paint decoder
  3. * Copyright (c) 2010 Peter Ross <pross@xvid.org>
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * Libav is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * Pictor/PC Paint decoder
  24. */
  25. #include "libavutil/imgutils.h"
  26. #include "avcodec.h"
  27. #include "bytestream.h"
  28. #include "cga_data.h"
  29. #include "internal.h"
  30. typedef struct PicContext {
  31. int width, height;
  32. int nb_planes;
  33. GetByteContext g;
  34. } PicContext;
  35. static void picmemset_8bpp(PicContext *s, AVFrame *frame, int value, int run,
  36. int *x, int *y)
  37. {
  38. while (run > 0) {
  39. uint8_t *d = frame->data[0] + *y * frame->linesize[0];
  40. if (*x + run >= s->width) {
  41. int n = s->width - *x;
  42. memset(d + *x, value, n);
  43. run -= n;
  44. *x = 0;
  45. *y -= 1;
  46. if (*y < 0)
  47. break;
  48. } else {
  49. memset(d + *x, value, run);
  50. *x += run;
  51. break;
  52. }
  53. }
  54. }
  55. static void picmemset(PicContext *s, AVFrame *frame, int value, int run,
  56. int *x, int *y, int *plane, int bits_per_plane)
  57. {
  58. uint8_t *d;
  59. int shift = *plane * bits_per_plane;
  60. int mask = ((1 << bits_per_plane) - 1) << shift;
  61. value <<= shift;
  62. while (run > 0) {
  63. int j;
  64. for (j = 8-bits_per_plane; j >= 0; j -= bits_per_plane) {
  65. d = frame->data[0] + *y * frame->linesize[0];
  66. d[*x] |= (value >> j) & mask;
  67. *x += 1;
  68. if (*x == s->width) {
  69. *y -= 1;
  70. *x = 0;
  71. if (*y < 0) {
  72. *y = s->height - 1;
  73. *plane += 1;
  74. value <<= bits_per_plane;
  75. mask <<= bits_per_plane;
  76. if (*plane >= s->nb_planes)
  77. break;
  78. }
  79. }
  80. }
  81. run--;
  82. }
  83. }
  84. static const uint8_t cga_mode45_index[6][4] = {
  85. [0] = { 0, 3, 5, 7 }, // mode4, palette#1, low intensity
  86. [1] = { 0, 2, 4, 6 }, // mode4, palette#2, low intensity
  87. [2] = { 0, 3, 4, 7 }, // mode5, low intensity
  88. [3] = { 0, 11, 13, 15 }, // mode4, palette#1, high intensity
  89. [4] = { 0, 10, 12, 14 }, // mode4, palette#2, high intensity
  90. [5] = { 0, 11, 12, 15 }, // mode5, high intensity
  91. };
  92. static int decode_frame(AVCodecContext *avctx,
  93. void *data, int *got_frame,
  94. AVPacket *avpkt)
  95. {
  96. PicContext *s = avctx->priv_data;
  97. AVFrame *frame = data;
  98. uint32_t *palette;
  99. int bits_per_plane, bpp, etype, esize, npal, pos_after_pal;
  100. int i, x, y, plane, tmp, ret;
  101. bytestream2_init(&s->g, avpkt->data, avpkt->size);
  102. if (bytestream2_get_bytes_left(&s->g) < 11)
  103. return AVERROR_INVALIDDATA;
  104. if (bytestream2_get_le16u(&s->g) != 0x1234)
  105. return AVERROR_INVALIDDATA;
  106. s->width = bytestream2_get_le16u(&s->g);
  107. s->height = bytestream2_get_le16u(&s->g);
  108. bytestream2_skip(&s->g, 4);
  109. tmp = bytestream2_get_byteu(&s->g);
  110. bits_per_plane = tmp & 0xF;
  111. s->nb_planes = (tmp >> 4) + 1;
  112. bpp = bits_per_plane * s->nb_planes;
  113. if (bits_per_plane > 8 || bpp < 1 || bpp > 32) {
  114. avpriv_request_sample(avctx, "Unsupported bit depth");
  115. return AVERROR_PATCHWELCOME;
  116. }
  117. if (bytestream2_peek_byte(&s->g) == 0xFF) {
  118. bytestream2_skip(&s->g, 2);
  119. etype = bytestream2_get_le16(&s->g);
  120. esize = bytestream2_get_le16(&s->g);
  121. if (bytestream2_get_bytes_left(&s->g) < esize)
  122. return AVERROR_INVALIDDATA;
  123. } else {
  124. etype = -1;
  125. esize = 0;
  126. }
  127. avctx->pix_fmt = AV_PIX_FMT_PAL8;
  128. if (s->width != avctx->width && s->height != avctx->height) {
  129. ret = ff_set_dimensions(avctx, s->width, s->height);
  130. if (ret < 0)
  131. return ret;
  132. }
  133. if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
  134. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  135. return ret;
  136. }
  137. memset(frame->data[0], 0, s->height * frame->linesize[0]);
  138. frame->pict_type = AV_PICTURE_TYPE_I;
  139. frame->palette_has_changed = 1;
  140. pos_after_pal = bytestream2_tell(&s->g) + esize;
  141. palette = (uint32_t*)frame->data[1];
  142. if (etype == 1 && esize > 1 && bytestream2_peek_byte(&s->g) < 6) {
  143. int idx = bytestream2_get_byte(&s->g);
  144. npal = 4;
  145. for (i = 0; i < npal; i++)
  146. palette[i] = ff_cga_palette[ cga_mode45_index[idx][i] ];
  147. } else if (etype == 2) {
  148. npal = FFMIN(esize, 16);
  149. for (i = 0; i < npal; i++) {
  150. int pal_idx = bytestream2_get_byte(&s->g);
  151. palette[i] = ff_cga_palette[FFMIN(pal_idx, 16)];
  152. }
  153. } else if (etype == 3) {
  154. npal = FFMIN(esize, 16);
  155. for (i = 0; i < npal; i++) {
  156. int pal_idx = bytestream2_get_byte(&s->g);
  157. palette[i] = ff_ega_palette[FFMIN(pal_idx, 63)];
  158. }
  159. } else if (etype == 4 || etype == 5) {
  160. npal = FFMIN(esize / 3, 256);
  161. for (i = 0; i < npal; i++)
  162. palette[i] = bytestream2_get_be24(&s->g) << 2;
  163. } else {
  164. if (bpp == 1) {
  165. npal = 2;
  166. palette[0] = 0x000000;
  167. palette[1] = 0xFFFFFF;
  168. } else if (bpp == 2) {
  169. npal = 4;
  170. for (i = 0; i < npal; i++)
  171. palette[i] = ff_cga_palette[ cga_mode45_index[0][i] ];
  172. } else {
  173. npal = 16;
  174. memcpy(palette, ff_cga_palette, npal * 4);
  175. }
  176. }
  177. // fill remaining palette entries
  178. memset(palette + npal, 0, AVPALETTE_SIZE - npal * 4);
  179. // skip remaining palette bytes
  180. bytestream2_seek(&s->g, pos_after_pal, SEEK_SET);
  181. x = 0;
  182. y = s->height - 1;
  183. plane = 0;
  184. if (bytestream2_get_le16(&s->g)) {
  185. while (bytestream2_get_bytes_left(&s->g) >= 6) {
  186. int stop_size, marker, t1, t2;
  187. t1 = bytestream2_get_bytes_left(&s->g);
  188. t2 = bytestream2_get_le16(&s->g);
  189. stop_size = t1 - FFMIN(t1, t2);
  190. // ignore uncompressed block size
  191. bytestream2_skip(&s->g, 2);
  192. marker = bytestream2_get_byte(&s->g);
  193. while (plane < s->nb_planes &&
  194. bytestream2_get_bytes_left(&s->g) > stop_size) {
  195. int run = 1;
  196. int val = bytestream2_get_byte(&s->g);
  197. if (val == marker) {
  198. run = bytestream2_get_byte(&s->g);
  199. if (run == 0)
  200. run = bytestream2_get_le16(&s->g);
  201. val = bytestream2_get_byte(&s->g);
  202. }
  203. if (!bytestream2_get_bytes_left(&s->g))
  204. break;
  205. if (bits_per_plane == 8) {
  206. picmemset_8bpp(s, frame, val, run, &x, &y);
  207. if (y < 0)
  208. goto finish;
  209. } else {
  210. picmemset(s, frame, val, run, &x, &y, &plane, bits_per_plane);
  211. }
  212. }
  213. }
  214. } else {
  215. avpriv_request_sample(avctx, "Uncompressed image");
  216. return avpkt->size;
  217. }
  218. finish:
  219. *got_frame = 1;
  220. return avpkt->size;
  221. }
  222. AVCodec ff_pictor_decoder = {
  223. .name = "pictor",
  224. .long_name = NULL_IF_CONFIG_SMALL("Pictor/PC Paint"),
  225. .type = AVMEDIA_TYPE_VIDEO,
  226. .id = AV_CODEC_ID_PICTOR,
  227. .priv_data_size = sizeof(PicContext),
  228. .decode = decode_frame,
  229. .capabilities = AV_CODEC_CAP_DR1,
  230. };