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.

285 lines
9.1KB

  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. AVFrame frame;
  32. int width, height;
  33. int nb_planes;
  34. GetByteContext g;
  35. } PicContext;
  36. static void picmemset_8bpp(PicContext *s, int value, int run, int *x, int *y)
  37. {
  38. while (run > 0) {
  39. uint8_t *d = s->frame.data[0] + *y * s->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, 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 = s->frame.data[0] + *y * s->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 av_cold int decode_init(AVCodecContext *avctx)
  93. {
  94. PicContext *s = avctx->priv_data;
  95. avcodec_get_frame_defaults(&s->frame);
  96. return 0;
  97. }
  98. static int decode_frame(AVCodecContext *avctx,
  99. void *data, int *got_frame,
  100. AVPacket *avpkt)
  101. {
  102. PicContext *s = avctx->priv_data;
  103. uint32_t *palette;
  104. int bits_per_plane, bpp, etype, esize, npal, pos_after_pal;
  105. int i, x, y, plane, tmp, val;
  106. bytestream2_init(&s->g, avpkt->data, avpkt->size);
  107. if (bytestream2_get_bytes_left(&s->g) < 11)
  108. return AVERROR_INVALIDDATA;
  109. if (bytestream2_get_le16u(&s->g) != 0x1234)
  110. return AVERROR_INVALIDDATA;
  111. s->width = bytestream2_get_le16u(&s->g);
  112. s->height = bytestream2_get_le16u(&s->g);
  113. bytestream2_skip(&s->g, 4);
  114. tmp = bytestream2_get_byteu(&s->g);
  115. bits_per_plane = tmp & 0xF;
  116. s->nb_planes = (tmp >> 4) + 1;
  117. bpp = bits_per_plane * s->nb_planes;
  118. if (bits_per_plane > 8 || bpp < 1 || bpp > 32) {
  119. av_log_ask_for_sample(avctx, "unsupported bit depth\n");
  120. return AVERROR_PATCHWELCOME;
  121. }
  122. if (bytestream2_peek_byte(&s->g) == 0xFF || bpp == 1 || bpp == 4 || bpp == 8) {
  123. bytestream2_skip(&s->g, 2);
  124. etype = bytestream2_get_le16(&s->g);
  125. esize = bytestream2_get_le16(&s->g);
  126. if (bytestream2_get_bytes_left(&s->g) < esize)
  127. return AVERROR_INVALIDDATA;
  128. } else {
  129. etype = -1;
  130. esize = 0;
  131. }
  132. avctx->pix_fmt = AV_PIX_FMT_PAL8;
  133. if (s->width != avctx->width && s->height != avctx->height) {
  134. if (av_image_check_size(s->width, s->height, 0, avctx) < 0)
  135. return -1;
  136. avcodec_set_dimensions(avctx, s->width, s->height);
  137. if (s->frame.data[0])
  138. avctx->release_buffer(avctx, &s->frame);
  139. }
  140. if (ff_get_buffer(avctx, &s->frame) < 0){
  141. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  142. return -1;
  143. }
  144. memset(s->frame.data[0], 0, s->height * s->frame.linesize[0]);
  145. s->frame.pict_type = AV_PICTURE_TYPE_I;
  146. s->frame.palette_has_changed = 1;
  147. pos_after_pal = bytestream2_tell(&s->g) + esize;
  148. palette = (uint32_t*)s->frame.data[1];
  149. if (etype == 1 && esize > 1 && bytestream2_peek_byte(&s->g) < 6) {
  150. int idx = bytestream2_get_byte(&s->g);
  151. npal = 4;
  152. for (i = 0; i < npal; i++)
  153. palette[i] = ff_cga_palette[ cga_mode45_index[idx][i] ];
  154. } else if (etype == 2) {
  155. npal = FFMIN(esize, 16);
  156. for (i = 0; i < npal; i++) {
  157. int pal_idx = bytestream2_get_byte(&s->g);
  158. palette[i] = ff_cga_palette[FFMIN(pal_idx, 16)];
  159. }
  160. } else if (etype == 3) {
  161. npal = FFMIN(esize, 16);
  162. for (i = 0; i < npal; i++) {
  163. int pal_idx = bytestream2_get_byte(&s->g);
  164. palette[i] = ff_ega_palette[FFMIN(pal_idx, 63)];
  165. }
  166. } else if (etype == 4 || etype == 5) {
  167. npal = FFMIN(esize / 3, 256);
  168. for (i = 0; i < npal; i++) {
  169. palette[i] = bytestream2_get_be24(&s->g) << 2;
  170. palette[i] |= 0xFFU << 24 | palette[i] >> 6 & 0x30303;
  171. }
  172. } else {
  173. if (bpp == 1) {
  174. npal = 2;
  175. palette[0] = 0xFF000000;
  176. palette[1] = 0xFFFFFFFF;
  177. } else if (bpp == 2) {
  178. npal = 4;
  179. for (i = 0; i < npal; i++)
  180. palette[i] = ff_cga_palette[ cga_mode45_index[0][i] ];
  181. } else {
  182. npal = 16;
  183. memcpy(palette, ff_cga_palette, npal * 4);
  184. }
  185. }
  186. // fill remaining palette entries
  187. memset(palette + npal, 0, AVPALETTE_SIZE - npal * 4);
  188. // skip remaining palette bytes
  189. bytestream2_seek(&s->g, pos_after_pal, SEEK_SET);
  190. val = 0;
  191. y = s->height - 1;
  192. if (bytestream2_get_le16(&s->g)) {
  193. x = 0;
  194. plane = 0;
  195. while (y >= 0 && bytestream2_get_bytes_left(&s->g) >= 6) {
  196. int stop_size, marker, t1, t2;
  197. t1 = bytestream2_get_bytes_left(&s->g);
  198. t2 = bytestream2_get_le16(&s->g);
  199. stop_size = t1 - FFMIN(t1, t2);
  200. // ignore uncompressed block size
  201. bytestream2_skip(&s->g, 2);
  202. marker = bytestream2_get_byte(&s->g);
  203. while (plane < s->nb_planes && y >= 0 &&
  204. bytestream2_get_bytes_left(&s->g) > stop_size) {
  205. int run = 1;
  206. val = bytestream2_get_byte(&s->g);
  207. if (val == marker) {
  208. run = bytestream2_get_byte(&s->g);
  209. if (run == 0)
  210. run = bytestream2_get_le16(&s->g);
  211. val = bytestream2_get_byte(&s->g);
  212. }
  213. if (!bytestream2_get_bytes_left(&s->g))
  214. break;
  215. if (bits_per_plane == 8) {
  216. picmemset_8bpp(s, val, run, &x, &y);
  217. } else {
  218. picmemset(s, val, run, &x, &y, &plane, bits_per_plane);
  219. }
  220. }
  221. }
  222. if (x < avctx->width && y >= 0) {
  223. int run = (y + 1) * avctx->width - x;
  224. if (bits_per_plane == 8)
  225. picmemset_8bpp(s, val, run, &x, &y);
  226. else
  227. picmemset(s, val, run / (8 / bits_per_plane), &x, &y, &plane, bits_per_plane);
  228. }
  229. } else {
  230. while (y >= 0 && bytestream2_get_bytes_left(&s->g) > 0) {
  231. memcpy(s->frame.data[0] + y * s->frame.linesize[0], s->g.buffer, FFMIN(avctx->width, bytestream2_get_bytes_left(&s->g)));
  232. bytestream2_skip(&s->g, avctx->width);
  233. y--;
  234. }
  235. }
  236. *got_frame = 1;
  237. *(AVFrame*)data = s->frame;
  238. return avpkt->size;
  239. }
  240. static av_cold int decode_end(AVCodecContext *avctx)
  241. {
  242. PicContext *s = avctx->priv_data;
  243. if (s->frame.data[0])
  244. avctx->release_buffer(avctx, &s->frame);
  245. return 0;
  246. }
  247. AVCodec ff_pictor_decoder = {
  248. .name = "pictor",
  249. .type = AVMEDIA_TYPE_VIDEO,
  250. .id = AV_CODEC_ID_PICTOR,
  251. .priv_data_size = sizeof(PicContext),
  252. .init = decode_init,
  253. .close = decode_end,
  254. .decode = decode_frame,
  255. .capabilities = CODEC_CAP_DR1,
  256. .long_name = NULL_IF_CONFIG_SMALL("Pictor/PC Paint"),
  257. };