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.

263 lines
8.6KB

  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. 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, 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 (s->width != avctx->width && s->height != avctx->height) {
  129. if (av_image_check_size(s->width, s->height, 0, avctx) < 0)
  130. return -1;
  131. avcodec_set_dimensions(avctx, s->width, s->height);
  132. }
  133. if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
  134. return ret;
  135. memset(frame->data[0], 0, s->height * frame->linesize[0]);
  136. frame->pict_type = AV_PICTURE_TYPE_I;
  137. frame->palette_has_changed = 1;
  138. pos_after_pal = bytestream2_tell(&s->g) + esize;
  139. palette = (uint32_t*)frame->data[1];
  140. if (etype == 1 && esize > 1 && bytestream2_peek_byte(&s->g) < 6) {
  141. int idx = bytestream2_get_byte(&s->g);
  142. npal = 4;
  143. for (i = 0; i < npal; i++)
  144. palette[i] = ff_cga_palette[ cga_mode45_index[idx][i] ];
  145. } else if (etype == 2) {
  146. npal = FFMIN(esize, 16);
  147. for (i = 0; i < npal; i++) {
  148. int pal_idx = bytestream2_get_byte(&s->g);
  149. palette[i] = ff_cga_palette[FFMIN(pal_idx, 15)];
  150. }
  151. } else if (etype == 3) {
  152. npal = FFMIN(esize, 16);
  153. for (i = 0; i < npal; i++) {
  154. int pal_idx = bytestream2_get_byte(&s->g);
  155. palette[i] = ff_ega_palette[FFMIN(pal_idx, 63)];
  156. }
  157. } else if (etype == 4 || etype == 5) {
  158. npal = FFMIN(esize / 3, 256);
  159. for (i = 0; i < npal; i++) {
  160. palette[i] = bytestream2_get_be24(&s->g) << 2;
  161. palette[i] |= 0xFFU << 24 | palette[i] >> 6 & 0x30303;
  162. }
  163. } else {
  164. if (bpp == 1) {
  165. npal = 2;
  166. palette[0] = 0xFF000000;
  167. palette[1] = 0xFFFFFFFF;
  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. val = 0;
  182. y = s->height - 1;
  183. if (bytestream2_get_le16(&s->g)) {
  184. x = 0;
  185. plane = 0;
  186. while (y >= 0 && bytestream2_get_bytes_left(&s->g) >= 6) {
  187. int stop_size, marker, t1, t2;
  188. t1 = bytestream2_get_bytes_left(&s->g);
  189. t2 = bytestream2_get_le16(&s->g);
  190. stop_size = t1 - FFMIN(t1, t2);
  191. // ignore uncompressed block size
  192. bytestream2_skip(&s->g, 2);
  193. marker = bytestream2_get_byte(&s->g);
  194. while (plane < s->nb_planes && y >= 0 &&
  195. bytestream2_get_bytes_left(&s->g) > stop_size) {
  196. int run = 1;
  197. val = bytestream2_get_byte(&s->g);
  198. if (val == marker) {
  199. run = bytestream2_get_byte(&s->g);
  200. if (run == 0)
  201. run = bytestream2_get_le16(&s->g);
  202. val = bytestream2_get_byte(&s->g);
  203. }
  204. if (!bytestream2_get_bytes_left(&s->g))
  205. break;
  206. if (bits_per_plane == 8) {
  207. picmemset_8bpp(s, frame, val, run, &x, &y);
  208. } else {
  209. picmemset(s, frame, val, run, &x, &y, &plane, bits_per_plane);
  210. }
  211. }
  212. }
  213. if (x < avctx->width && y >= 0) {
  214. int run = (y + 1) * avctx->width - x;
  215. if (bits_per_plane == 8)
  216. picmemset_8bpp(s, frame, val, run, &x, &y);
  217. else
  218. picmemset(s, frame, val, run / (8 / bits_per_plane), &x, &y, &plane, bits_per_plane);
  219. }
  220. } else {
  221. while (y >= 0 && bytestream2_get_bytes_left(&s->g) > 0) {
  222. memcpy(frame->data[0] + y * frame->linesize[0], s->g.buffer, FFMIN(avctx->width, bytestream2_get_bytes_left(&s->g)));
  223. bytestream2_skip(&s->g, avctx->width);
  224. y--;
  225. }
  226. }
  227. *got_frame = 1;
  228. return avpkt->size;
  229. }
  230. AVCodec ff_pictor_decoder = {
  231. .name = "pictor",
  232. .type = AVMEDIA_TYPE_VIDEO,
  233. .id = AV_CODEC_ID_PICTOR,
  234. .priv_data_size = sizeof(PicContext),
  235. .decode = decode_frame,
  236. .capabilities = CODEC_CAP_DR1,
  237. .long_name = NULL_IF_CONFIG_SMALL("Pictor/PC Paint"),
  238. };