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.

271 lines
7.9KB

  1. /*
  2. * CamStudio decoder
  3. * Copyright (c) 2006 Reimar Doeffinger
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include "common.h"
  22. #include "avcodec.h"
  23. #ifdef CONFIG_ZLIB
  24. #include <zlib.h>
  25. #endif
  26. #ifdef CONFIG_LZO
  27. #include <lzo1x.h>
  28. #endif
  29. typedef struct {
  30. AVFrame pic;
  31. int linelen, height, bpp;
  32. unsigned int decomp_size;
  33. unsigned char* decomp_buf;
  34. } CamStudioContext;
  35. static void copy_frame_default(AVFrame *f, uint8_t *src,
  36. int linelen, int height) {
  37. int i;
  38. uint8_t *dst = f->data[0];
  39. dst += (height - 1) * f->linesize[0];
  40. for (i = height; i; i--) {
  41. memcpy(dst, src, linelen);
  42. src += linelen;
  43. dst -= f->linesize[0];
  44. }
  45. }
  46. static void add_frame_default(AVFrame *f, uint8_t *src,
  47. int linelen, int height) {
  48. int i, j;
  49. uint8_t *dst = f->data[0];
  50. dst += (height - 1) * f->linesize[0];
  51. for (i = height; i; i--) {
  52. for (j = linelen; j; j--)
  53. *dst++ += *src++;
  54. dst -= f->linesize[0] + linelen;
  55. }
  56. }
  57. #ifndef WORDS_BIGENDIAN
  58. #define copy_frame_16 copy_frame_default
  59. #define copy_frame_32 copy_frame_default
  60. #define add_frame_16 add_frame_default
  61. #define add_frame_32 add_frame_default
  62. #else
  63. static void copy_frame_16(AVFrame *f, uint8_t *src,
  64. int linelen, int height) {
  65. int i, j;
  66. uint8_t *dst = f->data[0];
  67. dst += (height - 1) * f->linesize[0];
  68. for (i = height; i; i--) {
  69. for (j = linelen / 2; j; j--) {
  70. dst[0] = src[1];
  71. dst[1] = src[0];
  72. src += 2;
  73. dst += 2;
  74. }
  75. dst -= f->linesize[0] + linelen;
  76. }
  77. }
  78. static void copy_frame_32(AVFrame *f, uint8_t *src,
  79. int linelen, int height) {
  80. int i, j;
  81. uint8_t *dst = f->data[0];
  82. dst += (height - 1) * f->linesize[0];
  83. for (i = height; i; i--) {
  84. for (j = linelen / 4; j; j--) {
  85. dst[0] = src[3];
  86. dst[1] = src[2];
  87. dst[2] = src[1];
  88. dst[3] = src[0];
  89. src += 4;
  90. dst += 4;
  91. }
  92. dst -= f->linesize[0] + linelen;
  93. }
  94. }
  95. static void add_frame_16(AVFrame *f, uint8_t *src,
  96. int linelen, int height) {
  97. int i, j;
  98. uint8_t *dst = f->data[0];
  99. dst += (height - 1) * f->linesize[0];
  100. for (i = height; i; i--) {
  101. for (j = linelen / 2; j; j--) {
  102. dst[0] += src[1];
  103. dst[1] += src[0];
  104. src += 2;
  105. dst += 2;
  106. }
  107. dst -= f->linesize[0] + linelen;
  108. }
  109. }
  110. static void add_frame_32(AVFrame *f, uint8_t *src,
  111. int linelen, int height) {
  112. int i, j;
  113. uint8_t *dst = f->data[0];
  114. dst += (height - 1) * f->linesize[0];
  115. for (i = height; i; i--) {
  116. for (j = linelen / 4; j; j--) {
  117. dst[0] += src[3];
  118. dst[1] += src[2];
  119. dst[2] += src[1];
  120. dst[3] += src[0];
  121. src += 4;
  122. dst += 4;
  123. }
  124. dst -= f->linesize[0] + linelen;
  125. }
  126. }
  127. #endif
  128. static int decode_frame(AVCodecContext *avctx, void *data, int *data_size,
  129. uint8_t *buf, int buf_size) {
  130. CamStudioContext *c = (CamStudioContext *)avctx->priv_data;
  131. AVFrame *picture = data;
  132. if (buf_size < 2) {
  133. av_log(avctx, AV_LOG_ERROR, "coded frame too small\n");
  134. return -1;
  135. }
  136. if (c->pic.data[0])
  137. avctx->release_buffer(avctx, &c->pic);
  138. c->pic.reference = 1;
  139. c->pic.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_READABLE |
  140. FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
  141. if (avctx->get_buffer(avctx, &c->pic) < 0) {
  142. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  143. return -1;
  144. }
  145. // decompress data
  146. switch ((buf[0] >> 1) & 7) {
  147. case 0: { // lzo compression
  148. #ifdef CONFIG_LZO
  149. unsigned int dlen = c->decomp_size;
  150. if (lzo1x_decompress_safe(&buf[2], buf_size - 2,
  151. c->decomp_buf, &dlen, NULL) != LZO_E_OK)
  152. av_log(avctx, AV_LOG_ERROR, "error during lzo decompression\n");
  153. break;
  154. #else
  155. av_log(avctx, AV_LOG_ERROR, "compiled without lzo (GPL) support\n");
  156. return -1;
  157. #endif
  158. }
  159. case 1: { // zlib compression
  160. #ifdef CONFIG_ZLIB
  161. unsigned long dlen = c->decomp_size;
  162. if (uncompress(c->decomp_buf, &dlen, &buf[2], buf_size - 2) != Z_OK)
  163. av_log(avctx, AV_LOG_ERROR, "error during zlib decompression\n");
  164. break;
  165. #else
  166. av_log(avctx, AV_LOG_ERROR, "compiled without zlib support\n");
  167. return -1;
  168. #endif
  169. }
  170. default:
  171. av_log(avctx, AV_LOG_ERROR, "unknown compression\n");
  172. return -1;
  173. }
  174. // flip upside down, add difference frame
  175. if (buf[0] & 1) { // keyframe
  176. c->pic.pict_type = FF_I_TYPE;
  177. c->pic.key_frame = 1;
  178. switch (c->bpp) {
  179. case 16:
  180. copy_frame_16(&c->pic, c->decomp_buf, c->linelen, c->height);
  181. break;
  182. case 32:
  183. copy_frame_32(&c->pic, c->decomp_buf, c->linelen, c->height);
  184. break;
  185. default:
  186. copy_frame_default(&c->pic, c->decomp_buf, c->linelen, c->height);
  187. }
  188. } else {
  189. c->pic.pict_type = FF_P_TYPE;
  190. c->pic.key_frame = 0;
  191. switch (c->bpp) {
  192. case 16:
  193. add_frame_16(&c->pic, c->decomp_buf, c->linelen, c->height);
  194. break;
  195. case 32:
  196. add_frame_32(&c->pic, c->decomp_buf, c->linelen, c->height);
  197. break;
  198. default:
  199. add_frame_default(&c->pic, c->decomp_buf, c->linelen, c->height);
  200. }
  201. }
  202. *picture = c->pic;
  203. *data_size = sizeof(AVFrame);
  204. return buf_size;
  205. }
  206. static int decode_init(AVCodecContext *avctx) {
  207. CamStudioContext *c = (CamStudioContext *)avctx->priv_data;
  208. if (avcodec_check_dimensions(avctx, avctx->height, avctx->width) < 0) {
  209. return 1;
  210. }
  211. avctx->has_b_frames = 0;
  212. switch (avctx->bits_per_sample) {
  213. case 16: avctx->pix_fmt = PIX_FMT_RGB565; break;
  214. case 24: avctx->pix_fmt = PIX_FMT_BGR24; break;
  215. case 32: avctx->pix_fmt = PIX_FMT_RGBA32; break;
  216. default:
  217. av_log(avctx, AV_LOG_ERROR,
  218. "CamStudio codec error: unvalid depth %i bpp\n",
  219. avctx->bits_per_sample);
  220. return 1;
  221. }
  222. c->bpp = avctx->bits_per_sample;
  223. c->pic.data[0] = NULL;
  224. c->linelen = avctx->width * avctx->bits_per_sample / 8;
  225. c->height = avctx->height;
  226. c->decomp_size = c->height * c->linelen;
  227. c->decomp_buf = av_malloc(c->decomp_size);
  228. if (!c->decomp_buf) {
  229. av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
  230. return 1;
  231. }
  232. return 0;
  233. }
  234. static int decode_end(AVCodecContext *avctx) {
  235. CamStudioContext *c = (CamStudioContext *)avctx->priv_data;
  236. av_freep(&c->decomp_buf);
  237. if (c->pic.data[0])
  238. avctx->release_buffer(avctx, &c->pic);
  239. return 0;
  240. }
  241. AVCodec cscd_decoder = {
  242. "camstudio",
  243. CODEC_TYPE_VIDEO,
  244. CODEC_ID_CSCD,
  245. sizeof(CamStudioContext),
  246. decode_init,
  247. NULL,
  248. decode_end,
  249. decode_frame,
  250. CODEC_CAP_DR1,
  251. };