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.

268 lines
8.7KB

  1. /*
  2. * Pictor/PC Paint decoder
  3. * Copyright (c) 2010 Peter Ross <pross@xvid.org>
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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. if (*plane >= s->nb_planes)
  75. return;
  76. value <<= bits_per_plane;
  77. mask <<= bits_per_plane;
  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, val;
  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 || bpp == 1 || bpp == 4 || bpp == 8) {
  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 (av_image_check_size(s->width, s->height, 0, avctx) < 0)
  129. return -1;
  130. if (s->width != avctx->width || s->height != avctx->height) {
  131. ret = ff_set_dimensions(avctx, s->width, s->height);
  132. if (ret < 0)
  133. return ret;
  134. }
  135. if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
  136. return ret;
  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, 15)];
  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. palette[i] |= 0xFFU << 24 | palette[i] >> 6 & 0x30303;
  164. }
  165. } else {
  166. if (bpp == 1) {
  167. npal = 2;
  168. palette[0] = 0xFF000000;
  169. palette[1] = 0xFFFFFFFF;
  170. } else if (bpp == 2) {
  171. npal = 4;
  172. for (i = 0; i < npal; i++)
  173. palette[i] = ff_cga_palette[ cga_mode45_index[0][i] ];
  174. } else {
  175. npal = 16;
  176. memcpy(palette, ff_cga_palette, npal * 4);
  177. }
  178. }
  179. // fill remaining palette entries
  180. memset(palette + npal, 0, AVPALETTE_SIZE - npal * 4);
  181. // skip remaining palette bytes
  182. bytestream2_seek(&s->g, pos_after_pal, SEEK_SET);
  183. val = 0;
  184. y = s->height - 1;
  185. if (bytestream2_get_le16(&s->g)) {
  186. x = 0;
  187. plane = 0;
  188. while (bytestream2_get_bytes_left(&s->g) >= 6) {
  189. int stop_size, marker, t1, t2;
  190. t1 = bytestream2_get_bytes_left(&s->g);
  191. t2 = bytestream2_get_le16(&s->g);
  192. stop_size = t1 - FFMIN(t1, t2);
  193. // ignore uncompressed block size
  194. bytestream2_skip(&s->g, 2);
  195. marker = bytestream2_get_byte(&s->g);
  196. while (plane < s->nb_planes &&
  197. bytestream2_get_bytes_left(&s->g) > stop_size) {
  198. int run = 1;
  199. val = bytestream2_get_byte(&s->g);
  200. if (val == marker) {
  201. run = bytestream2_get_byte(&s->g);
  202. if (run == 0)
  203. run = bytestream2_get_le16(&s->g);
  204. val = bytestream2_get_byte(&s->g);
  205. }
  206. if (!bytestream2_get_bytes_left(&s->g))
  207. break;
  208. if (bits_per_plane == 8) {
  209. picmemset_8bpp(s, frame, val, run, &x, &y);
  210. if (y < 0)
  211. goto finish;
  212. } else {
  213. picmemset(s, frame, val, run, &x, &y, &plane, bits_per_plane);
  214. }
  215. }
  216. }
  217. if (plane < s->nb_planes && x < avctx->width) {
  218. int run = (y + 1) * avctx->width - x;
  219. if (bits_per_plane == 8)
  220. picmemset_8bpp(s, frame, val, run, &x, &y);
  221. else
  222. picmemset(s, frame, val, run / (8 / bits_per_plane), &x, &y, &plane, bits_per_plane);
  223. }
  224. } else {
  225. while (y >= 0 && bytestream2_get_bytes_left(&s->g) > 0) {
  226. memcpy(frame->data[0] + y * frame->linesize[0], s->g.buffer, FFMIN(avctx->width, bytestream2_get_bytes_left(&s->g)));
  227. bytestream2_skip(&s->g, avctx->width);
  228. y--;
  229. }
  230. }
  231. finish:
  232. *got_frame = 1;
  233. return avpkt->size;
  234. }
  235. AVCodec ff_pictor_decoder = {
  236. .name = "pictor",
  237. .long_name = NULL_IF_CONFIG_SMALL("Pictor/PC Paint"),
  238. .type = AVMEDIA_TYPE_VIDEO,
  239. .id = AV_CODEC_ID_PICTOR,
  240. .priv_data_size = sizeof(PicContext),
  241. .decode = decode_frame,
  242. .capabilities = AV_CODEC_CAP_DR1,
  243. };