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.0KB

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