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.

198 lines
5.7KB

  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. 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. memcpy(*dst, *buf, striplen);
  75. *buf += striplen;
  76. } else if (pixel >= 0)
  77. memset(*dst, pixel, striplen);
  78. *dst += striplen;
  79. remaining -= striplen;
  80. count -= striplen;
  81. if (remaining <= 0) {
  82. *dst += linesize - width;
  83. remaining = width;
  84. }
  85. if (linesize > 0) {
  86. if (*dst >= dst_end) goto exhausted;
  87. } else {
  88. if (*dst <= dst_end) goto exhausted;
  89. }
  90. }
  91. *x = width - remaining;
  92. return 0;
  93. exhausted:
  94. *x = width - remaining;
  95. return 1;
  96. }
  97. static int decode_frame(AVCodecContext *avctx,
  98. void *data, int *data_size,
  99. AVPacket *avpkt)
  100. {
  101. AnmContext *s = avctx->priv_data;
  102. const uint8_t *buf = avpkt->data;
  103. const int buf_size = avpkt->size;
  104. const uint8_t *buf_end = buf + buf_size;
  105. uint8_t *dst, *dst_end;
  106. int count;
  107. if(avctx->reget_buffer(avctx, &s->frame) < 0){
  108. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  109. return -1;
  110. }
  111. dst = s->frame.data[0];
  112. dst_end = s->frame.data[0] + s->frame.linesize[0]*avctx->height;
  113. if (buf[0] != 0x42) {
  114. av_log_ask_for_sample(avctx, "unknown record type\n");
  115. return buf_size;
  116. }
  117. if (buf[1]) {
  118. av_log_ask_for_sample(avctx, "padding bytes not supported\n");
  119. return buf_size;
  120. }
  121. buf += 4;
  122. s->x = 0;
  123. do {
  124. /* if statements are ordered by probability */
  125. #define OP(buf, pixel, count) \
  126. op(&dst, dst_end, (buf), buf_end, (pixel), (count), &s->x, avctx->width, s->frame.linesize[0])
  127. int type = bytestream_get_byte(&buf);
  128. count = type & 0x7F;
  129. type >>= 7;
  130. if (count) {
  131. if (OP(type ? NULL : &buf, -1, count)) break;
  132. } else if (!type) {
  133. int pixel;
  134. count = bytestream_get_byte(&buf); /* count==0 gives nop */
  135. pixel = bytestream_get_byte(&buf);
  136. if (OP(NULL, pixel, count)) break;
  137. } else {
  138. int pixel;
  139. type = bytestream_get_le16(&buf);
  140. count = type & 0x3FFF;
  141. type >>= 14;
  142. if (!count) {
  143. if (type == 0)
  144. break; // stop
  145. if (type == 2) {
  146. av_log_ask_for_sample(avctx, "unknown opcode");
  147. return AVERROR_INVALIDDATA;
  148. }
  149. continue;
  150. }
  151. pixel = type == 3 ? bytestream_get_byte(&buf) : -1;
  152. if (type == 1) count += 0x4000;
  153. if (OP(type == 2 ? &buf : NULL, pixel, count)) break;
  154. }
  155. } while (buf + 1 < buf_end);
  156. *data_size = sizeof(AVFrame);
  157. *(AVFrame*)data = s->frame;
  158. return buf_size;
  159. }
  160. static av_cold int decode_end(AVCodecContext *avctx)
  161. {
  162. AnmContext *s = avctx->priv_data;
  163. if (s->frame.data[0])
  164. avctx->release_buffer(avctx, &s->frame);
  165. return 0;
  166. }
  167. AVCodec ff_anm_decoder = {
  168. "anm",
  169. AVMEDIA_TYPE_VIDEO,
  170. CODEC_ID_ANM,
  171. sizeof(AnmContext),
  172. decode_init,
  173. NULL,
  174. decode_end,
  175. decode_frame,
  176. CODEC_CAP_DR1,
  177. .long_name = NULL_IF_CONFIG_SMALL("Deluxe Paint Animation"),
  178. };