rl2.c 6.6 KB

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