pictordec.c 7.5 KB

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