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.

204 lines
6.1KB

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