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.

275 lines
8.8KB

  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. typedef struct PicContext {
  30. AVFrame frame;
  31. int width, height;
  32. int nb_planes;
  33. GetByteContext g;
  34. } PicContext;
  35. static void picmemset_8bpp(PicContext *s, int value, int run, int *x, int *y)
  36. {
  37. while (run > 0) {
  38. uint8_t *d = s->frame.data[0] + *y * s->frame.linesize[0];
  39. if (*x + run >= s->width) {
  40. int n = s->width - *x;
  41. memset(d + *x, value, n);
  42. run -= n;
  43. *x = 0;
  44. *y -= 1;
  45. if (*y < 0)
  46. break;
  47. } else {
  48. memset(d + *x, value, run);
  49. *x += run;
  50. break;
  51. }
  52. }
  53. }
  54. static void picmemset(PicContext *s, int value, int run,
  55. int *x, int *y, int *plane, int bits_per_plane)
  56. {
  57. uint8_t *d;
  58. int shift = *plane * bits_per_plane;
  59. int mask = ((1 << bits_per_plane) - 1) << shift;
  60. value <<= shift;
  61. while (run > 0) {
  62. int j;
  63. for (j = 8-bits_per_plane; j >= 0; j -= bits_per_plane) {
  64. d = s->frame.data[0] + *y * s->frame.linesize[0];
  65. d[*x] |= (value >> j) & mask;
  66. *x += 1;
  67. if (*x == s->width) {
  68. *y -= 1;
  69. *x = 0;
  70. if (*y < 0) {
  71. *y = s->height - 1;
  72. *plane += 1;
  73. value <<= bits_per_plane;
  74. mask <<= bits_per_plane;
  75. if (*plane >= s->nb_planes)
  76. break;
  77. }
  78. }
  79. }
  80. run--;
  81. }
  82. }
  83. static const uint8_t cga_mode45_index[6][4] = {
  84. [0] = { 0, 3, 5, 7 }, // mode4, palette#1, low intensity
  85. [1] = { 0, 2, 4, 6 }, // mode4, palette#2, low intensity
  86. [2] = { 0, 3, 4, 7 }, // mode5, low intensity
  87. [3] = { 0, 11, 13, 15 }, // mode4, palette#1, high intensity
  88. [4] = { 0, 10, 12, 14 }, // mode4, palette#2, high intensity
  89. [5] = { 0, 11, 12, 15 }, // mode5, high intensity
  90. };
  91. static av_cold int decode_init(AVCodecContext *avctx)
  92. {
  93. PicContext *s = avctx->priv_data;
  94. avcodec_get_frame_defaults(&s->frame);
  95. return 0;
  96. }
  97. static int decode_frame(AVCodecContext *avctx,
  98. void *data, int *data_size,
  99. AVPacket *avpkt)
  100. {
  101. PicContext *s = avctx->priv_data;
  102. uint32_t *palette;
  103. int bits_per_plane, bpp, etype, esize, npal, pos_after_pal;
  104. int i, x, y, plane, tmp;
  105. bytestream2_init(&s->g, avpkt->data, avpkt->size);
  106. if (bytestream2_get_bytes_left(&s->g) < 11)
  107. return AVERROR_INVALIDDATA;
  108. if (bytestream2_get_le16u(&s->g) != 0x1234)
  109. return AVERROR_INVALIDDATA;
  110. s->width = bytestream2_get_le16u(&s->g);
  111. s->height = bytestream2_get_le16u(&s->g);
  112. bytestream2_skip(&s->g, 4);
  113. tmp = bytestream2_get_byteu(&s->g);
  114. bits_per_plane = tmp & 0xF;
  115. s->nb_planes = (tmp >> 4) + 1;
  116. bpp = bits_per_plane * s->nb_planes;
  117. if (bits_per_plane > 8 || bpp < 1 || bpp > 32) {
  118. av_log_ask_for_sample(avctx, "unsupported bit depth\n");
  119. return AVERROR_INVALIDDATA;
  120. }
  121. if (bytestream2_peek_byte(&s->g) == 0xFF || bpp == 8) {
  122. bytestream2_skip(&s->g, 2);
  123. etype = bytestream2_get_le16(&s->g);
  124. esize = bytestream2_get_le16(&s->g);
  125. if (bytestream2_get_bytes_left(&s->g) < esize)
  126. return AVERROR_INVALIDDATA;
  127. } else {
  128. etype = -1;
  129. esize = 0;
  130. }
  131. avctx->pix_fmt = PIX_FMT_PAL8;
  132. if (s->width != avctx->width && s->height != avctx->height) {
  133. if (av_image_check_size(s->width, s->height, 0, avctx) < 0)
  134. return -1;
  135. avcodec_set_dimensions(avctx, s->width, s->height);
  136. if (s->frame.data[0])
  137. avctx->release_buffer(avctx, &s->frame);
  138. }
  139. if (avctx->get_buffer(avctx, &s->frame) < 0){
  140. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  141. return -1;
  142. }
  143. memset(s->frame.data[0], 0, s->height * s->frame.linesize[0]);
  144. s->frame.pict_type = AV_PICTURE_TYPE_I;
  145. s->frame.palette_has_changed = 1;
  146. pos_after_pal = bytestream2_tell(&s->g) + esize;
  147. palette = (uint32_t*)s->frame.data[1];
  148. if (etype == 1 && esize > 1 && bytestream2_peek_byte(&s->g) < 6) {
  149. int idx = bytestream2_get_byte(&s->g);
  150. npal = 4;
  151. for (i = 0; i < npal; i++)
  152. palette[i] = ff_cga_palette[ cga_mode45_index[idx][i] ];
  153. } else if (etype == 2) {
  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_cga_palette[FFMIN(pal_idx, 16)];
  158. }
  159. } else if (etype == 3) {
  160. npal = FFMIN(esize, 16);
  161. for (i = 0; i < npal; i++) {
  162. int pal_idx = bytestream2_get_byte(&s->g);
  163. palette[i] = ff_ega_palette[FFMIN(pal_idx, 63)];
  164. }
  165. } else if (etype == 4 || etype == 5) {
  166. npal = FFMIN(esize / 3, 256);
  167. for (i = 0; i < npal; i++) {
  168. palette[i] = bytestream2_get_be24(&s->g) << 2;
  169. palette[i] |= 0xFFU << 24 | palette[i] >> 6 & 0x30303;
  170. }
  171. } else {
  172. if (bpp == 1) {
  173. npal = 2;
  174. palette[0] = 0xFF000000;
  175. palette[1] = 0xFFFFFFFF;
  176. } else if (bpp == 2) {
  177. npal = 4;
  178. for (i = 0; i < npal; i++)
  179. palette[i] = ff_cga_palette[ cga_mode45_index[0][i] ];
  180. } else {
  181. npal = 16;
  182. memcpy(palette, ff_cga_palette, npal * 4);
  183. }
  184. }
  185. // fill remaining palette entries
  186. memset(palette + npal, 0, AVPALETTE_SIZE - npal * 4);
  187. // skip remaining palette bytes
  188. bytestream2_seek(&s->g, pos_after_pal, SEEK_SET);
  189. y = s->height - 1;
  190. if (bytestream2_get_le16(&s->g)) {
  191. x = 0;
  192. plane = 0;
  193. while (y >= 0 && bytestream2_get_bytes_left(&s->g) >= 6) {
  194. int stop_size, marker, t1, t2;
  195. t1 = bytestream2_get_bytes_left(&s->g);
  196. t2 = bytestream2_get_le16(&s->g);
  197. stop_size = t1 - FFMIN(t1, t2);
  198. // ignore uncompressed block size
  199. bytestream2_skip(&s->g, 2);
  200. marker = bytestream2_get_byte(&s->g);
  201. while (plane < s->nb_planes && y >= 0 &&
  202. bytestream2_get_bytes_left(&s->g) > stop_size) {
  203. int run = 1;
  204. int val = bytestream2_get_byte(&s->g);
  205. if (val == marker) {
  206. run = bytestream2_get_byte(&s->g);
  207. if (run == 0)
  208. run = bytestream2_get_le16(&s->g);
  209. val = bytestream2_get_byte(&s->g);
  210. }
  211. if (!bytestream2_get_bytes_left(&s->g))
  212. break;
  213. if (bits_per_plane == 8) {
  214. picmemset_8bpp(s, val, run, &x, &y);
  215. } else {
  216. picmemset(s, val, run, &x, &y, &plane, bits_per_plane);
  217. }
  218. }
  219. }
  220. } else {
  221. while (y >= 0 && bytestream2_get_bytes_left(&s->g) > 0) {
  222. memcpy(s->frame.data[0] + y * s->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. *data_size = sizeof(AVFrame);
  228. *(AVFrame*)data = s->frame;
  229. return avpkt->size;
  230. }
  231. static av_cold int decode_end(AVCodecContext *avctx)
  232. {
  233. PicContext *s = avctx->priv_data;
  234. if (s->frame.data[0])
  235. avctx->release_buffer(avctx, &s->frame);
  236. return 0;
  237. }
  238. AVCodec ff_pictor_decoder = {
  239. .name = "pictor",
  240. .type = AVMEDIA_TYPE_VIDEO,
  241. .id = AV_CODEC_ID_PICTOR,
  242. .priv_data_size = sizeof(PicContext),
  243. .init = decode_init,
  244. .close = decode_end,
  245. .decode = decode_frame,
  246. .capabilities = CODEC_CAP_DR1,
  247. .long_name = NULL_IF_CONFIG_SMALL("Pictor/PC Paint"),
  248. };