PageRenderTime 66ms CodeModel.GetById 40ms RepoModel.GetById 0ms app.codeStats 0ms

/deps/ffmpeg/libavcodec/rl2.c

https://github.com/lince/libffmpeg-c
C | 235 lines | 140 code | 38 blank | 57 comment | 21 complexity | 760ee51d3d3a528283baa03a417ce25b 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. avcodec_get_frame_defaults(&s->frame);
  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. static int rl2_decode_frame(AVCodecContext *avctx,
  150. void *data, int *data_size,
  151. AVPacket *avpkt)
  152. {
  153. const uint8_t *buf = avpkt->data;
  154. int buf_size = avpkt->size;
  155. Rl2Context *s = avctx->priv_data;
  156. if(s->frame.data[0])
  157. avctx->release_buffer(avctx, &s->frame);
  158. /** get buffer */
  159. s->frame.reference= 0;
  160. if(avctx->get_buffer(avctx, &s->frame)) {
  161. av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  162. return -1;
  163. }
  164. /** run length decode */
  165. rl2_rle_decode(s,buf,buf_size,s->frame.data[0],s->frame.linesize[0],s->video_base);
  166. /** make the palette available on the way out */
  167. memcpy(s->frame.data[1], s->palette, AVPALETTE_SIZE);
  168. *data_size = sizeof(AVFrame);
  169. *(AVFrame*)data = s->frame;
  170. /** report that the buffer was completely consumed */
  171. return buf_size;
  172. }
  173. /**
  174. * Uninit decoder
  175. * @param avctx decoder context
  176. * @return 0 success, -1 on error
  177. */
  178. static av_cold int rl2_decode_end(AVCodecContext *avctx)
  179. {
  180. Rl2Context *s = avctx->priv_data;
  181. if(s->frame.data[0])
  182. avctx->release_buffer(avctx, &s->frame);
  183. av_free(s->back_frame);
  184. return 0;
  185. }
  186. AVCodec ff_rl2_decoder = {
  187. "rl2",
  188. AVMEDIA_TYPE_VIDEO,
  189. CODEC_ID_RL2,
  190. sizeof(Rl2Context),
  191. rl2_decode_init,
  192. NULL,
  193. rl2_decode_end,
  194. rl2_decode_frame,
  195. CODEC_CAP_DR1,
  196. .long_name = NULL_IF_CONFIG_SMALL("RL2 video"),
  197. };