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.

229 lines
6.4KB

  1. /*
  2. * RL2 Video Decoder
  3. * Copyright (C) 2008 Sascha Sommer (saschasommer@freenet.de)
  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. * RL2 Video Decoder
  24. * @author Sascha Sommer (saschasommer@freenet.de)
  25. * @see http://wiki.multimedia.cx/index.php?title=RL2
  26. */
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include "libavutil/internal.h"
  31. #include "libavutil/intreadwrite.h"
  32. #include "libavutil/mem.h"
  33. #include "avcodec.h"
  34. #include "internal.h"
  35. #define EXTRADATA1_SIZE (6 + 256 * 3) ///< video base, clr count, palette
  36. typedef struct Rl2Context {
  37. AVCodecContext *avctx;
  38. uint16_t video_base; ///< initial drawing offset
  39. uint32_t clr_count; ///< number of used colors (currently unused)
  40. uint8_t *back_frame; ///< background frame
  41. uint32_t palette[AVPALETTE_COUNT];
  42. } Rl2Context;
  43. /**
  44. * Run Length Decode a single 320x200 frame
  45. * @param s rl2 context
  46. * @param in input buffer
  47. * @param size input buffer size
  48. * @param out output buffer
  49. * @param stride stride of the output buffer
  50. * @param video_base offset of the rle data inside the frame
  51. */
  52. static void rl2_rle_decode(Rl2Context *s, const uint8_t *in, int size,
  53. uint8_t *out, int stride, int video_base)
  54. {
  55. int base_x = video_base % s->avctx->width;
  56. int base_y = video_base / s->avctx->width;
  57. int stride_adj = stride - s->avctx->width;
  58. int i;
  59. const uint8_t *back_frame = s->back_frame;
  60. const uint8_t *in_end = in + size;
  61. const uint8_t *out_end = out + stride * s->avctx->height;
  62. uint8_t *line_end;
  63. /** copy start of the background frame */
  64. for (i = 0; i <= base_y; i++) {
  65. if (s->back_frame)
  66. memcpy(out, back_frame, s->avctx->width);
  67. out += stride;
  68. back_frame += s->avctx->width;
  69. }
  70. back_frame += base_x - s->avctx->width;
  71. line_end = out - stride_adj;
  72. out += base_x - stride;
  73. /** decode the variable part of the frame */
  74. while (in < in_end) {
  75. uint8_t val = *in++;
  76. int len = 1;
  77. if (val >= 0x80) {
  78. if (in >= in_end)
  79. break;
  80. len = *in++;
  81. if (!len)
  82. break;
  83. }
  84. if (len >= out_end - out)
  85. break;
  86. if (s->back_frame)
  87. val |= 0x80;
  88. else
  89. val &= ~0x80;
  90. while (len--) {
  91. *out++ = (val == 0x80) ? *back_frame : val;
  92. back_frame++;
  93. if (out == line_end) {
  94. out += stride_adj;
  95. line_end += stride;
  96. if (len >= out_end - out)
  97. break;
  98. }
  99. }
  100. }
  101. /** copy the rest from the background frame */
  102. if (s->back_frame) {
  103. while (out < out_end) {
  104. memcpy(out, back_frame, line_end - out);
  105. back_frame += line_end - out;
  106. out = line_end + stride_adj;
  107. line_end += stride;
  108. }
  109. }
  110. }
  111. /**
  112. * Initialize the decoder
  113. * @param avctx decoder context
  114. * @return 0 success, -1 on error
  115. */
  116. static av_cold int rl2_decode_init(AVCodecContext *avctx)
  117. {
  118. Rl2Context *s = avctx->priv_data;
  119. int back_size;
  120. int i;
  121. s->avctx = avctx;
  122. avctx->pix_fmt = AV_PIX_FMT_PAL8;
  123. /** parse extra data */
  124. if (!avctx->extradata || avctx->extradata_size < EXTRADATA1_SIZE) {
  125. av_log(avctx, AV_LOG_ERROR, "invalid extradata size\n");
  126. return AVERROR(EINVAL);
  127. }
  128. /** get frame_offset */
  129. s->video_base = AV_RL16(&avctx->extradata[0]);
  130. s->clr_count = AV_RL32(&avctx->extradata[2]);
  131. if (s->video_base >= avctx->width * avctx->height) {
  132. av_log(avctx, AV_LOG_ERROR, "invalid video_base\n");
  133. return AVERROR_INVALIDDATA;
  134. }
  135. /** initialize palette */
  136. for (i = 0; i < AVPALETTE_COUNT; i++)
  137. s->palette[i] = AV_RB24(&avctx->extradata[6 + i * 3]);
  138. /** decode background frame if present */
  139. back_size = avctx->extradata_size - EXTRADATA1_SIZE;
  140. if (back_size > 0) {
  141. uint8_t *back_frame = av_mallocz(avctx->width*avctx->height);
  142. if (!back_frame)
  143. return AVERROR(ENOMEM);
  144. rl2_rle_decode(s, avctx->extradata + EXTRADATA1_SIZE, back_size,
  145. back_frame, avctx->width, 0);
  146. s->back_frame = back_frame;
  147. }
  148. return 0;
  149. }
  150. static int rl2_decode_frame(AVCodecContext *avctx,
  151. void *data, int *got_frame,
  152. AVPacket *avpkt)
  153. {
  154. AVFrame *frame = data;
  155. const uint8_t *buf = avpkt->data;
  156. int ret, buf_size = avpkt->size;
  157. Rl2Context *s = avctx->priv_data;
  158. if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
  159. av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  160. return ret;
  161. }
  162. /** run length decode */
  163. rl2_rle_decode(s, buf, buf_size, frame->data[0], frame->linesize[0],
  164. s->video_base);
  165. /** make the palette available on the way out */
  166. memcpy(frame->data[1], s->palette, AVPALETTE_SIZE);
  167. *got_frame = 1;
  168. /** report that the buffer was completely consumed */
  169. return buf_size;
  170. }
  171. /**
  172. * Uninit decoder
  173. * @param avctx decoder context
  174. * @return 0 success, -1 on error
  175. */
  176. static av_cold int rl2_decode_end(AVCodecContext *avctx)
  177. {
  178. Rl2Context *s = avctx->priv_data;
  179. av_free(s->back_frame);
  180. return 0;
  181. }
  182. AVCodec ff_rl2_decoder = {
  183. .name = "rl2",
  184. .long_name = NULL_IF_CONFIG_SMALL("RL2 video"),
  185. .type = AVMEDIA_TYPE_VIDEO,
  186. .id = AV_CODEC_ID_RL2,
  187. .priv_data_size = sizeof(Rl2Context),
  188. .init = rl2_decode_init,
  189. .close = rl2_decode_end,
  190. .decode = rl2_decode_frame,
  191. .capabilities = AV_CODEC_CAP_DR1,
  192. };