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.

238 lines
6.6KB

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