PageRenderTime 97ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/ffmpeg/libavcodec/rl2.c

https://github.com/MaDDoGo/xbmc
C | 234 lines | 139 code | 38 blank | 57 comment | 21 complexity | f71d938aec4f4ed671322ae515dedab3 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. * RL2 Video Decoder
  23. * @file
  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 "libavutil/intreadwrite.h"
  32. #include "avcodec.h"
  33. #define EXTRADATA1_SIZE (6 + 256 * 3) ///< video base, clr count, palette
  34. typedef struct Rl2Context {
  35. AVCodecContext *avctx;
  36. AVFrame frame;
  37. unsigned short video_base; ///< initial drawing offset
  38. unsigned int clr_count; ///< number of used colors (currently unused)
  39. unsigned char* back_frame; ///< background frame
  40. unsigned int palette[AVPALETTE_COUNT];
  41. } Rl2Context;
  42. /**
  43. * Run Length Decode a single 320x200 frame
  44. * @param s rl2 context
  45. * @param in input buffer
  46. * @param size input buffer size
  47. * @param out ouput buffer
  48. * @param stride stride of the output buffer
  49. * @param video_base offset of the rle data inside the frame
  50. */
  51. static void rl2_rle_decode(Rl2Context *s,const unsigned char* in,int size,
  52. unsigned char* out,int stride,int video_base){
  53. int base_x = video_base % s->avctx->width;
  54. int base_y = video_base / s->avctx->width;
  55. int stride_adj = stride - s->avctx->width;
  56. int i;
  57. const unsigned char* back_frame = s->back_frame;
  58. const unsigned char* in_end = in + size;
  59. const unsigned char* out_end = out + stride * s->avctx->height;
  60. unsigned char* line_end = out + s->avctx->width;
  61. /** copy start of the background frame */
  62. for(i=0;i<=base_y;i++){
  63. if(s->back_frame)
  64. memcpy(out,back_frame,s->avctx->width);
  65. out += stride;
  66. back_frame += s->avctx->width;
  67. }
  68. back_frame += base_x - s->avctx->width;
  69. line_end = out - stride_adj;
  70. out += base_x - stride;
  71. /** decode the variable part of the frame */
  72. while(in < in_end){
  73. unsigned char val = *in++;
  74. int len = 1;
  75. if(val >= 0x80){
  76. if(in >= in_end)
  77. break;
  78. len = *in++;
  79. if(!len)
  80. break;
  81. }
  82. if(len >= out_end - out)
  83. break;
  84. if(s->back_frame)
  85. val |= 0x80;
  86. else
  87. val &= ~0x80;
  88. while(len--){
  89. *out++ = (val == 0x80)? *back_frame:val;
  90. back_frame++;
  91. if(out == line_end){
  92. out += stride_adj;
  93. line_end += stride;
  94. if(len >= out_end - out)
  95. break;
  96. }
  97. }
  98. }
  99. /** copy the rest from the background frame */
  100. if(s->back_frame){
  101. while(out < out_end){
  102. memcpy(out, back_frame, line_end - out);
  103. back_frame += line_end - out;
  104. out = line_end + stride_adj;
  105. line_end += stride;
  106. }
  107. }
  108. }
  109. /**
  110. * Initialize the decoder
  111. * @param avctx decoder context
  112. * @return 0 success, -1 on error
  113. */
  114. static av_cold int rl2_decode_init(AVCodecContext *avctx)
  115. {
  116. Rl2Context *s = avctx->priv_data;
  117. int back_size;
  118. int i;
  119. s->avctx = avctx;
  120. avctx->pix_fmt = PIX_FMT_PAL8;
  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] = 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. "rl2",
  187. AVMEDIA_TYPE_VIDEO,
  188. CODEC_ID_RL2,
  189. sizeof(Rl2Context),
  190. rl2_decode_init,
  191. NULL,
  192. rl2_decode_end,
  193. rl2_decode_frame,
  194. CODEC_CAP_DR1,
  195. .long_name = NULL_IF_CONFIG_SMALL("RL2 video"),
  196. };