PageRenderTime 50ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/ffmpeg/libavcodec/rl2.c

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