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.

251 lines
7.5KB

  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 libavcodec/picdec.c
  23. * Pictor/PC Paint decoder
  24. */
  25. #include "avcodec.h"
  26. #include "bytestream.h"
  27. #include "cga_data.h"
  28. typedef struct PicContext {
  29. AVFrame frame;
  30. int width, height;
  31. int nb_planes;
  32. } PicContext;
  33. static void picmemset_8bpp(PicContext *s, int value, int run, int *x, int *y)
  34. {
  35. while (run > 0) {
  36. uint8_t *d = s->frame.data[0] + *y * s->frame.linesize[0];
  37. if (*x + run >= s->width) {
  38. int n = s->width - *x;
  39. memset(d + *x, value, n);
  40. run -= n;
  41. *x = 0;
  42. *y -= 1;
  43. if (*y < 0)
  44. break;
  45. } else {
  46. memset(d + *x, value, run);
  47. *x += run;
  48. break;
  49. }
  50. }
  51. }
  52. static void picmemset(PicContext *s, int value, int run, int *x, int *y, int *plane, int bits_per_plane)
  53. {
  54. uint8_t *d;
  55. int shift = *plane * bits_per_plane;
  56. int mask = ((1 << bits_per_plane) - 1) << shift;
  57. value <<= shift;
  58. while (run > 0) {
  59. int j;
  60. for (j = 8-bits_per_plane; j >= 0; j -= bits_per_plane) {
  61. d = s->frame.data[0] + *y * s->frame.linesize[0];
  62. d[*x] |= (value >> j) & mask;
  63. *x += 1;
  64. if (*x == s->width) {
  65. *y -= 1;
  66. *x = 0;
  67. if (*y < 0) {
  68. *y = s->height - 1;
  69. *plane += 1;
  70. value <<= bits_per_plane;
  71. mask <<= bits_per_plane;
  72. if (*plane >= s->nb_planes)
  73. break;
  74. }
  75. }
  76. }
  77. run--;
  78. }
  79. }
  80. static const uint8_t cga_mode45_index[6][4] = {
  81. [0] = { 0, 3, 5, 7 }, // mode4, palette#1, low intensity
  82. [1] = { 0, 2, 4, 6 }, // mode4, palette#2, low intensity
  83. [2] = { 0, 3, 4, 7 }, // mode5, low intensity
  84. [3] = { 0, 11, 13, 15 }, // mode4, palette#1, high intensity
  85. [4] = { 0, 10, 12, 14 }, // mode4, palette#2, high intensity
  86. [5] = { 0, 11, 12, 15 }, // mode5, high intensity
  87. };
  88. static int decode_frame(AVCodecContext *avctx,
  89. void *data, int *data_size,
  90. AVPacket *avpkt)
  91. {
  92. PicContext *s = avctx->priv_data;
  93. int buf_size = avpkt->size;
  94. const uint8_t *buf = avpkt->data;
  95. const uint8_t *buf_end = avpkt->data + buf_size;
  96. uint32_t *palette;
  97. int bits_per_plane, bpp, etype, esize, npal;
  98. int i, x, y, plane;
  99. if (buf_size < 11)
  100. return AVERROR_INVALIDDATA;
  101. if (bytestream_get_le16(&buf) != 0x1234)
  102. return AVERROR_INVALIDDATA;
  103. s->width = bytestream_get_le16(&buf);
  104. s->height = bytestream_get_le16(&buf);
  105. buf += 4;
  106. bits_per_plane = *buf & 0xF;
  107. s->nb_planes = (*buf++ >> 4) + 1;
  108. bpp = s->nb_planes ? bits_per_plane*s->nb_planes : bits_per_plane;
  109. if (bits_per_plane > 8 || bpp < 1 || bpp > 32) {
  110. av_log_ask_for_sample(s, "unsupported bit depth\n");
  111. return AVERROR_INVALIDDATA;
  112. }
  113. if (*buf == 0xFF) {
  114. buf += 2;
  115. etype = bytestream_get_le16(&buf);
  116. esize = bytestream_get_le16(&buf);
  117. if (buf_end - buf < esize)
  118. return AVERROR_INVALIDDATA;
  119. } else {
  120. etype = -1;
  121. esize = 0;
  122. }
  123. avctx->pix_fmt = PIX_FMT_PAL8;
  124. if (s->width != avctx->width && s->height != avctx->height) {
  125. if (avcodec_check_dimensions(avctx, s->width, s->height) < 0)
  126. return -1;
  127. avcodec_set_dimensions(avctx, s->width, s->height);
  128. if (s->frame.data[0])
  129. avctx->release_buffer(avctx, &s->frame);
  130. }
  131. if (avctx->get_buffer(avctx, &s->frame) < 0){
  132. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  133. return -1;
  134. }
  135. memset(s->frame.data[0], 0, s->height * s->frame.linesize[0]);
  136. s->frame.pict_type = FF_I_TYPE;
  137. s->frame.palette_has_changed = 1;
  138. palette = (uint32_t*)s->frame.data[1];
  139. if (etype == 1 && esize > 1 && *buf < 6) {
  140. int idx = *buf;
  141. npal = 4;
  142. for (i = 0; i < npal; i++)
  143. palette[i] = ff_cga_palette[ cga_mode45_index[idx][i] ];
  144. } else if (etype == 2) {
  145. npal = FFMIN(esize, 16);
  146. for (i = 0; i < npal; i++)
  147. palette[i] = ff_cga_palette[ FFMIN(buf[i], 16)];
  148. } else if (etype == 3) {
  149. npal = FFMIN(esize, 16);
  150. for (i = 0; i < npal; i++)
  151. palette[i] = ff_ega_palette[ FFMIN(buf[i], 63)];
  152. } else if (etype == 4 || etype == 5) {
  153. npal = FFMIN(esize / 3, 256);
  154. for (i = 0; i < npal; i++)
  155. palette[i] = AV_RB24(buf + i*3) << 2;
  156. } else {
  157. if (bpp == 1) {
  158. npal = 2;
  159. palette[0] = 0x000000;
  160. palette[1] = 0xFFFFFF;
  161. } else if (bpp == 2) {
  162. npal = 4;
  163. for (i = 0; i < npal; i++)
  164. palette[i] = ff_cga_palette[ cga_mode45_index[0][i] ];
  165. } else {
  166. npal = 16;
  167. memcpy(palette, ff_cga_palette, npal * 4);
  168. }
  169. }
  170. // fill remaining palette entries
  171. memset(palette + npal, 0, AVPALETTE_SIZE - npal * 4);
  172. buf += esize;
  173. x = 0;
  174. y = s->height - 1;
  175. plane = 0;
  176. if (bytestream_get_le16(&buf)) {
  177. while (buf_end - buf >= 6) {
  178. const uint8_t *buf_pend = buf + FFMIN(AV_RL16(buf), buf_end - buf);
  179. //ignore uncompressed block size reported at buf[2]
  180. int marker = buf[4];
  181. buf += 5;
  182. while (plane < s->nb_planes && buf_pend - buf >= 1) {
  183. int run = 1;
  184. int val = *buf++;
  185. if (val == marker) {
  186. run = *buf++;
  187. if (run == 0)
  188. run = bytestream_get_le16(&buf);
  189. val = *buf++;
  190. }
  191. if (buf > buf_end)
  192. break;
  193. if (bits_per_plane == 8) {
  194. picmemset_8bpp(s, val, run, &x, &y);
  195. if (y < 0)
  196. break;
  197. } else {
  198. picmemset(s, val, run, &x, &y, &plane, bits_per_plane);
  199. }
  200. }
  201. }
  202. } else {
  203. av_log_ask_for_sample(s, "uncompressed image\n");
  204. return buf_size;
  205. }
  206. *data_size = sizeof(AVFrame);
  207. *(AVFrame*)data = s->frame;
  208. return buf_size;
  209. }
  210. static av_cold int decode_end(AVCodecContext *avctx)
  211. {
  212. PicContext *s = avctx->priv_data;
  213. if (s->frame.data[0])
  214. avctx->release_buffer(avctx, &s->frame);
  215. return 0;
  216. }
  217. AVCodec pictor_decoder = {
  218. "pictor",
  219. CODEC_TYPE_VIDEO,
  220. CODEC_ID_PICTOR,
  221. sizeof(PicContext),
  222. NULL,
  223. NULL,
  224. decode_end,
  225. decode_frame,
  226. CODEC_CAP_DR1,
  227. .long_name = NULL_IF_CONFIG_SMALL("Pictor/PC Paint"),
  228. };