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.

198 lines
5.8KB

  1. /*
  2. * Deluxe Paint Animation decoder
  3. * Copyright (c) 2009 Peter Ross
  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. * Deluxe Paint Animation decoder
  24. */
  25. #include "avcodec.h"
  26. #include "bytestream.h"
  27. typedef struct AnmContext {
  28. AVFrame frame;
  29. int palette[AVPALETTE_COUNT];
  30. int x; ///< x coordinate position
  31. } AnmContext;
  32. static av_cold int decode_init(AVCodecContext *avctx)
  33. {
  34. AnmContext *s = avctx->priv_data;
  35. const uint8_t *buf;
  36. int i;
  37. avctx->pix_fmt = PIX_FMT_PAL8;
  38. if (avctx->extradata_size != 16*8 + 4*256)
  39. return -1;
  40. s->frame.reference = 1;
  41. buf = avctx->extradata + 16*8;
  42. for (i = 0; i < 256; i++)
  43. s->palette[i] = bytestream_get_le32(&buf);
  44. return 0;
  45. }
  46. /**
  47. * Perform decode operation
  48. * @param dst, dst_end Destination image buffer
  49. * @param buf, buf_end Source buffer (optional, see below)
  50. * @param pixel Fill color (optional, see below)
  51. * @param count Pixel count
  52. * @param x Pointer to x-axis counter
  53. * @param width Image width
  54. * @param linesize Destination image buffer linesize
  55. * @return non-zero if destination buffer is exhausted
  56. *
  57. * a copy operation is achieved when 'buf' is set
  58. * a fill operation is acheived when 'buf' is null and pixel is >= 0
  59. * a skip operation is acheived when 'buf' is null and pixel is < 0
  60. */
  61. static inline int op(uint8_t **dst, const uint8_t *dst_end,
  62. const uint8_t **buf, const uint8_t *buf_end,
  63. int pixel, int count,
  64. int *x, int width, int linesize)
  65. {
  66. int remaining = width - *x;
  67. while(count > 0) {
  68. int striplen = FFMIN(count, remaining);
  69. if (buf) {
  70. striplen = FFMIN(striplen, buf_end - *buf);
  71. if (*buf >= buf_end)
  72. goto exhausted;
  73. memcpy(*dst, *buf, striplen);
  74. *buf += striplen;
  75. } else if (pixel >= 0)
  76. memset(*dst, pixel, striplen);
  77. *dst += striplen;
  78. remaining -= striplen;
  79. count -= striplen;
  80. if (remaining <= 0) {
  81. *dst += linesize - width;
  82. remaining = width;
  83. }
  84. if (linesize > 0) {
  85. if (*dst >= dst_end) goto exhausted;
  86. } else {
  87. if (*dst <= dst_end) goto exhausted;
  88. }
  89. }
  90. *x = width - remaining;
  91. return 0;
  92. exhausted:
  93. *x = width - remaining;
  94. return 1;
  95. }
  96. static int decode_frame(AVCodecContext *avctx,
  97. void *data, int *data_size,
  98. AVPacket *avpkt)
  99. {
  100. AnmContext *s = avctx->priv_data;
  101. const uint8_t *buf = avpkt->data;
  102. const int buf_size = avpkt->size;
  103. const uint8_t *buf_end = buf + buf_size;
  104. uint8_t *dst, *dst_end;
  105. int count;
  106. if(avctx->reget_buffer(avctx, &s->frame) < 0){
  107. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  108. return -1;
  109. }
  110. dst = s->frame.data[0];
  111. dst_end = s->frame.data[0] + s->frame.linesize[0]*avctx->height;
  112. if (buf[0] != 0x42) {
  113. av_log_ask_for_sample(avctx, "unknown record type\n");
  114. return buf_size;
  115. }
  116. if (buf[1]) {
  117. av_log_ask_for_sample(avctx, "padding bytes not supported\n");
  118. return buf_size;
  119. }
  120. buf += 4;
  121. s->x = 0;
  122. do {
  123. /* if statements are ordered by probability */
  124. #define OP(buf, pixel, count) \
  125. op(&dst, dst_end, (buf), buf_end, (pixel), (count), &s->x, avctx->width, s->frame.linesize[0])
  126. int type = bytestream_get_byte(&buf);
  127. count = type & 0x7F;
  128. type >>= 7;
  129. if (count) {
  130. if (OP(type ? NULL : &buf, -1, count)) break;
  131. } else if (!type) {
  132. int pixel;
  133. count = bytestream_get_byte(&buf); /* count==0 gives nop */
  134. pixel = bytestream_get_byte(&buf);
  135. if (OP(NULL, pixel, count)) break;
  136. } else {
  137. int pixel;
  138. type = bytestream_get_le16(&buf);
  139. count = type & 0x3FFF;
  140. type >>= 14;
  141. if (!count) {
  142. if (type == 0)
  143. break; // stop
  144. if (type == 2) {
  145. av_log_ask_for_sample(avctx, "unknown opcode");
  146. return AVERROR_INVALIDDATA;
  147. }
  148. continue;
  149. }
  150. pixel = type == 3 ? bytestream_get_byte(&buf) : -1;
  151. if (type == 1) count += 0x4000;
  152. if (OP(type == 2 ? &buf : NULL, pixel, count)) break;
  153. }
  154. } while (buf + 1 < buf_end);
  155. memcpy(s->frame.data[1], s->palette, AVPALETTE_SIZE);
  156. *data_size = sizeof(AVFrame);
  157. *(AVFrame*)data = s->frame;
  158. return buf_size;
  159. }
  160. static av_cold int decode_end(AVCodecContext *avctx)
  161. {
  162. AnmContext *s = avctx->priv_data;
  163. if (s->frame.data[0])
  164. avctx->release_buffer(avctx, &s->frame);
  165. return 0;
  166. }
  167. AVCodec ff_anm_decoder = {
  168. .name = "anm",
  169. .type = AVMEDIA_TYPE_VIDEO,
  170. .id = CODEC_ID_ANM,
  171. .priv_data_size = sizeof(AnmContext),
  172. .init = decode_init,
  173. .close = decode_end,
  174. .decode = decode_frame,
  175. .capabilities = CODEC_CAP_DR1,
  176. .long_name = NULL_IF_CONFIG_SMALL("Deluxe Paint Animation"),
  177. };