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.

264 lines
8.3KB

  1. /*
  2. * Pictor/PC Paint decoder
  3. * Copyright (c) 2010 Peter Ross <pross@xvid.org>
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; 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 int decode_frame(AVCodecContext *avctx,
  93. void *data, int *got_frame,
  94. AVPacket *avpkt)
  95. {
  96. PicContext *s = avctx->priv_data;
  97. uint32_t *palette;
  98. int bits_per_plane, bpp, etype, esize, npal, pos_after_pal;
  99. int i, x, y, plane, tmp;
  100. bytestream2_init(&s->g, avpkt->data, avpkt->size);
  101. if (bytestream2_get_bytes_left(&s->g) < 11)
  102. return AVERROR_INVALIDDATA;
  103. if (bytestream2_get_le16u(&s->g) != 0x1234)
  104. return AVERROR_INVALIDDATA;
  105. s->width = bytestream2_get_le16u(&s->g);
  106. s->height = bytestream2_get_le16u(&s->g);
  107. bytestream2_skip(&s->g, 4);
  108. tmp = bytestream2_get_byteu(&s->g);
  109. bits_per_plane = tmp & 0xF;
  110. s->nb_planes = (tmp >> 4) + 1;
  111. bpp = bits_per_plane * s->nb_planes;
  112. if (bits_per_plane > 8 || bpp < 1 || bpp > 32) {
  113. av_log_ask_for_sample(s, "unsupported bit depth\n");
  114. return AVERROR_PATCHWELCOME;
  115. }
  116. if (bytestream2_peek_byte(&s->g) == 0xFF) {
  117. bytestream2_skip(&s->g, 2);
  118. etype = bytestream2_get_le16(&s->g);
  119. esize = bytestream2_get_le16(&s->g);
  120. if (bytestream2_get_bytes_left(&s->g) < esize)
  121. return AVERROR_INVALIDDATA;
  122. } else {
  123. etype = -1;
  124. esize = 0;
  125. }
  126. avctx->pix_fmt = AV_PIX_FMT_PAL8;
  127. if (s->width != avctx->width && s->height != avctx->height) {
  128. if (av_image_check_size(s->width, s->height, 0, avctx) < 0)
  129. return -1;
  130. avcodec_set_dimensions(avctx, s->width, s->height);
  131. if (s->frame.data[0])
  132. avctx->release_buffer(avctx, &s->frame);
  133. }
  134. if (ff_get_buffer(avctx, &s->frame) < 0){
  135. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  136. return -1;
  137. }
  138. memset(s->frame.data[0], 0, s->height * s->frame.linesize[0]);
  139. s->frame.pict_type = AV_PICTURE_TYPE_I;
  140. s->frame.palette_has_changed = 1;
  141. pos_after_pal = bytestream2_tell(&s->g) + esize;
  142. palette = (uint32_t*)s->frame.data[1];
  143. if (etype == 1 && esize > 1 && bytestream2_peek_byte(&s->g) < 6) {
  144. int idx = bytestream2_get_byte(&s->g);
  145. npal = 4;
  146. for (i = 0; i < npal; i++)
  147. palette[i] = ff_cga_palette[ cga_mode45_index[idx][i] ];
  148. } else if (etype == 2) {
  149. npal = FFMIN(esize, 16);
  150. for (i = 0; i < npal; i++) {
  151. int pal_idx = bytestream2_get_byte(&s->g);
  152. palette[i] = ff_cga_palette[FFMIN(pal_idx, 16)];
  153. }
  154. } else if (etype == 3) {
  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_ega_palette[FFMIN(pal_idx, 63)];
  159. }
  160. } else if (etype == 4 || etype == 5) {
  161. npal = FFMIN(esize / 3, 256);
  162. for (i = 0; i < npal; i++)
  163. palette[i] = bytestream2_get_be24(&s->g) << 2;
  164. } else {
  165. if (bpp == 1) {
  166. npal = 2;
  167. palette[0] = 0x000000;
  168. palette[1] = 0xFFFFFF;
  169. } else if (bpp == 2) {
  170. npal = 4;
  171. for (i = 0; i < npal; i++)
  172. palette[i] = ff_cga_palette[ cga_mode45_index[0][i] ];
  173. } else {
  174. npal = 16;
  175. memcpy(palette, ff_cga_palette, npal * 4);
  176. }
  177. }
  178. // fill remaining palette entries
  179. memset(palette + npal, 0, AVPALETTE_SIZE - npal * 4);
  180. // skip remaining palette bytes
  181. bytestream2_seek(&s->g, pos_after_pal, SEEK_SET);
  182. x = 0;
  183. y = s->height - 1;
  184. plane = 0;
  185. if (bytestream2_get_le16(&s->g)) {
  186. while (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 &&
  195. bytestream2_get_bytes_left(&s->g) > stop_size) {
  196. int run = 1;
  197. int 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, val, run, &x, &y);
  208. if (y < 0)
  209. break;
  210. } else {
  211. picmemset(s, val, run, &x, &y, &plane, bits_per_plane);
  212. }
  213. }
  214. }
  215. } else {
  216. av_log_ask_for_sample(s, "uncompressed image\n");
  217. return avpkt->size;
  218. }
  219. *got_frame = 1;
  220. *(AVFrame*)data = s->frame;
  221. return avpkt->size;
  222. }
  223. static av_cold int decode_end(AVCodecContext *avctx)
  224. {
  225. PicContext *s = avctx->priv_data;
  226. if (s->frame.data[0])
  227. avctx->release_buffer(avctx, &s->frame);
  228. return 0;
  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. .close = decode_end,
  236. .decode = decode_frame,
  237. .capabilities = CODEC_CAP_DR1,
  238. .long_name = NULL_IF_CONFIG_SMALL("Pictor/PC Paint"),
  239. };