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.

207 lines
6.2KB

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