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.

199 lines
5.9KB

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