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.

197 lines
6.0KB

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