pictordec.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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 "libavutil/mem.h"
  27. #include "avcodec.h"
  28. #include "bytestream.h"
  29. #include "cga_data.h"
  30. #include "codec_internal.h"
  31. #include "decode.h"
  32. typedef struct PicContext {
  33. int width, height;
  34. int nb_planes;
  35. GetByteContext g;
  36. } PicContext;
  37. static void picmemset_8bpp(PicContext *s, AVFrame *frame, int value, int run,
  38. int *x, int *y)
  39. {
  40. while (run > 0) {
  41. uint8_t *d = frame->data[0] + *y * frame->linesize[0];
  42. if (*x + run >= s->width) {
  43. int n = s->width - *x;
  44. memset(d + *x, value, n);
  45. run -= n;
  46. *x = 0;
  47. *y -= 1;
  48. if (*y < 0)
  49. break;
  50. } else {
  51. memset(d + *x, value, run);
  52. *x += run;
  53. break;
  54. }
  55. }
  56. }
  57. static void picmemset(PicContext *s, AVFrame *frame, unsigned value, int run,
  58. int *x, int *y, int *plane, int bits_per_plane)
  59. {
  60. uint8_t *d;
  61. int shift = *plane * bits_per_plane;
  62. unsigned mask = ((1U << bits_per_plane) - 1) << shift;
  63. int xl = *x;
  64. int yl = *y;
  65. int planel = *plane;
  66. int pixels_per_value = 8/bits_per_plane;
  67. value <<= shift;
  68. d = frame->data[0] + yl * frame->linesize[0];
  69. while (run > 0) {
  70. int j;
  71. for (j = 8-bits_per_plane; j >= 0; j -= bits_per_plane) {
  72. d[xl] |= (value >> j) & mask;
  73. xl += 1;
  74. while (xl == s->width) {
  75. yl -= 1;
  76. xl = 0;
  77. if (yl < 0) {
  78. yl = s->height - 1;
  79. planel += 1;
  80. if (planel >= s->nb_planes)
  81. goto end;
  82. value <<= bits_per_plane;
  83. mask <<= bits_per_plane;
  84. }
  85. d = frame->data[0] + yl * frame->linesize[0];
  86. if (s->nb_planes == 1 &&
  87. run*pixels_per_value >= s->width &&
  88. pixels_per_value < (s->width / pixels_per_value * pixels_per_value)
  89. ) {
  90. for (; xl < pixels_per_value; xl ++) {
  91. j = (j < bits_per_plane ? 8 : j) - bits_per_plane;
  92. d[xl] |= (value >> j) & mask;
  93. }
  94. av_memcpy_backptr(d+xl, pixels_per_value, s->width - xl);
  95. run -= s->width / pixels_per_value;
  96. xl = s->width / pixels_per_value * pixels_per_value;
  97. }
  98. }
  99. }
  100. run--;
  101. }
  102. end:
  103. *x = xl;
  104. *y = yl;
  105. *plane = planel;
  106. }
  107. static const uint8_t cga_mode45_index[6][4] = {
  108. [0] = { 0, 3, 5, 7 }, // mode4, palette#1, low intensity
  109. [1] = { 0, 2, 4, 6 }, // mode4, palette#2, low intensity
  110. [2] = { 0, 3, 4, 7 }, // mode5, low intensity
  111. [3] = { 0, 11, 13, 15 }, // mode4, palette#1, high intensity
  112. [4] = { 0, 10, 12, 14 }, // mode4, palette#2, high intensity
  113. [5] = { 0, 11, 12, 15 }, // mode5, high intensity
  114. };
  115. static int decode_frame(AVCodecContext *avctx, AVFrame *frame,
  116. int *got_frame, AVPacket *avpkt)
  117. {
  118. PicContext *s = avctx->priv_data;
  119. uint32_t *palette;
  120. int bits_per_plane, bpp, etype, esize, npal, pos_after_pal;
  121. int i, x, y, plane, tmp, ret, val;
  122. bytestream2_init(&s->g, avpkt->data, avpkt->size);
  123. if (bytestream2_get_bytes_left(&s->g) < 11)
  124. return AVERROR_INVALIDDATA;
  125. if (bytestream2_get_le16u(&s->g) != 0x1234)
  126. return AVERROR_INVALIDDATA;
  127. s->width = bytestream2_get_le16u(&s->g);
  128. s->height = bytestream2_get_le16u(&s->g);
  129. bytestream2_skip(&s->g, 4);
  130. tmp = bytestream2_get_byteu(&s->g);
  131. bits_per_plane = tmp & 0xF;
  132. s->nb_planes = (tmp >> 4) + 1;
  133. bpp = bits_per_plane * s->nb_planes;
  134. if (bits_per_plane > 8 || bpp < 1 || bpp > 32) {
  135. avpriv_request_sample(avctx, "Unsupported bit depth");
  136. return AVERROR_PATCHWELCOME;
  137. }
  138. if (bytestream2_peek_byte(&s->g) == 0xFF || bpp == 1 || bpp == 4 || bpp == 8) {
  139. bytestream2_skip(&s->g, 2);
  140. etype = bytestream2_get_le16(&s->g);
  141. esize = bytestream2_get_le16(&s->g);
  142. if (bytestream2_get_bytes_left(&s->g) < esize)
  143. return AVERROR_INVALIDDATA;
  144. } else {
  145. etype = -1;
  146. esize = 0;
  147. }
  148. avctx->pix_fmt = AV_PIX_FMT_PAL8;
  149. if (av_image_check_size(s->width, s->height, 0, avctx) < 0)
  150. return -1;
  151. /*
  152. There are 2 coding modes, RLE and RAW.
  153. Undamaged RAW should be proportional to W*H and thus bigger than RLE
  154. RLE codes the most compressed runs by
  155. 1 byte for val (=marker)
  156. 1 byte run (=0)
  157. 2 bytes run
  158. 1 byte val
  159. that's 5 bytes and the maximum run we can code is 65535
  160. The RLE decoder can exit prematurly but it does not on any image available
  161. Based on this the formula is assumed correct for undamaged images.
  162. If an image is found which exploits the special end
  163. handling and breaks this formula then this needs to be adapted.
  164. */
  165. if (bytestream2_get_bytes_left(&s->g) < s->width * s->height / 65535 * 5)
  166. return AVERROR_INVALIDDATA;
  167. if (s->width != avctx->width || s->height != avctx->height) {
  168. ret = ff_set_dimensions(avctx, s->width, s->height);
  169. if (ret < 0)
  170. return ret;
  171. }
  172. if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
  173. return ret;
  174. memset(frame->data[0], 0, s->height * frame->linesize[0]);
  175. frame->pict_type = AV_PICTURE_TYPE_I;
  176. pos_after_pal = bytestream2_tell(&s->g) + esize;
  177. palette = (uint32_t*)frame->data[1];
  178. if (etype == 1 && esize > 1 && bytestream2_peek_byte(&s->g) < 6) {
  179. int idx = bytestream2_get_byte(&s->g);
  180. npal = 4;
  181. for (i = 0; i < npal; i++)
  182. palette[i] = ff_cga_palette[ cga_mode45_index[idx][i] ];
  183. } else if (etype == 2) {
  184. npal = FFMIN(esize, 16);
  185. for (i = 0; i < npal; i++) {
  186. int pal_idx = bytestream2_get_byte(&s->g);
  187. palette[i] = ff_cga_palette[FFMIN(pal_idx, 15)];
  188. }
  189. } else if (etype == 3) {
  190. npal = FFMIN(esize, 16);
  191. for (i = 0; i < npal; i++) {
  192. int pal_idx = bytestream2_get_byte(&s->g);
  193. palette[i] = ff_ega_palette[FFMIN(pal_idx, 63)];
  194. }
  195. } else if (etype == 4 || etype == 5) {
  196. npal = FFMIN(esize / 3, 256);
  197. for (i = 0; i < npal; i++) {
  198. palette[i] = bytestream2_get_be24(&s->g) << 2;
  199. palette[i] |= 0xFFU << 24 | palette[i] >> 6 & 0x30303;
  200. }
  201. } else {
  202. if (bpp == 1) {
  203. npal = 2;
  204. palette[0] = 0xFF000000;
  205. palette[1] = 0xFFFFFFFF;
  206. } else if (bpp == 2) {
  207. npal = 4;
  208. for (i = 0; i < npal; i++)
  209. palette[i] = ff_cga_palette[ cga_mode45_index[0][i] ];
  210. } else {
  211. npal = 16;
  212. memcpy(palette, ff_cga_palette, npal * 4);
  213. }
  214. }
  215. // fill remaining palette entries
  216. memset(palette + npal, 0, AVPALETTE_SIZE - npal * 4);
  217. // skip remaining palette bytes
  218. bytestream2_seek(&s->g, pos_after_pal, SEEK_SET);
  219. val = 0;
  220. y = s->height - 1;
  221. if (bytestream2_get_le16(&s->g)) {
  222. x = 0;
  223. plane = 0;
  224. while (bytestream2_get_bytes_left(&s->g) >= 6) {
  225. int stop_size, marker, t1, t2;
  226. t1 = bytestream2_get_bytes_left(&s->g);
  227. t2 = bytestream2_get_le16(&s->g);
  228. stop_size = t1 - FFMIN(t1, t2);
  229. // ignore uncompressed block size
  230. bytestream2_skip(&s->g, 2);
  231. marker = bytestream2_get_byte(&s->g);
  232. while (plane < s->nb_planes &&
  233. bytestream2_get_bytes_left(&s->g) > stop_size) {
  234. int run = 1;
  235. val = bytestream2_get_byte(&s->g);
  236. if (val == marker) {
  237. run = bytestream2_get_byte(&s->g);
  238. if (run == 0)
  239. run = bytestream2_get_le16(&s->g);
  240. val = bytestream2_get_byte(&s->g);
  241. }
  242. if (bits_per_plane == 8) {
  243. picmemset_8bpp(s, frame, val, run, &x, &y);
  244. if (y < 0)
  245. goto finish;
  246. } else {
  247. picmemset(s, frame, val, run, &x, &y, &plane, bits_per_plane);
  248. }
  249. }
  250. }
  251. if (s->nb_planes - plane > 1)
  252. return AVERROR_INVALIDDATA;
  253. if (plane < s->nb_planes && x < avctx->width) {
  254. int run = (y + 1) * avctx->width - x;
  255. if (bits_per_plane == 8)
  256. picmemset_8bpp(s, frame, val, run, &x, &y);
  257. else
  258. picmemset(s, frame, val, run / (8 / bits_per_plane), &x, &y, &plane, bits_per_plane);
  259. }
  260. } else {
  261. while (y >= 0 && bytestream2_get_bytes_left(&s->g) > 0) {
  262. memcpy(frame->data[0] + y * frame->linesize[0], s->g.buffer, FFMIN(avctx->width, bytestream2_get_bytes_left(&s->g)));
  263. bytestream2_skip(&s->g, avctx->width);
  264. y--;
  265. }
  266. }
  267. finish:
  268. *got_frame = 1;
  269. return avpkt->size;
  270. }
  271. const FFCodec ff_pictor_decoder = {
  272. .p.name = "pictor",
  273. CODEC_LONG_NAME("Pictor/PC Paint"),
  274. .p.type = AVMEDIA_TYPE_VIDEO,
  275. .p.id = AV_CODEC_ID_PICTOR,
  276. .p.capabilities = AV_CODEC_CAP_DR1,
  277. .priv_data_size = sizeof(PicContext),
  278. FF_CODEC_DECODE_CB(decode_frame),
  279. };