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.

263 lines
7.7KB

  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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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. #include "lzo.h"
  27. typedef struct {
  28. AVFrame pic;
  29. int linelen, height, bpp;
  30. unsigned int decomp_size;
  31. unsigned char* decomp_buf;
  32. } CamStudioContext;
  33. static void copy_frame_default(AVFrame *f, uint8_t *src,
  34. int linelen, int height) {
  35. int i;
  36. uint8_t *dst = f->data[0];
  37. dst += (height - 1) * f->linesize[0];
  38. for (i = height; i; i--) {
  39. memcpy(dst, src, linelen);
  40. src += linelen;
  41. dst -= f->linesize[0];
  42. }
  43. }
  44. static void add_frame_default(AVFrame *f, uint8_t *src,
  45. int linelen, int height) {
  46. int i, j;
  47. uint8_t *dst = f->data[0];
  48. dst += (height - 1) * f->linesize[0];
  49. for (i = height; i; i--) {
  50. for (j = linelen; j; j--)
  51. *dst++ += *src++;
  52. dst -= f->linesize[0] + linelen;
  53. }
  54. }
  55. #ifndef WORDS_BIGENDIAN
  56. #define copy_frame_16 copy_frame_default
  57. #define copy_frame_32 copy_frame_default
  58. #define add_frame_16 add_frame_default
  59. #define add_frame_32 add_frame_default
  60. #else
  61. static void copy_frame_16(AVFrame *f, uint8_t *src,
  62. int linelen, int height) {
  63. int i, j;
  64. uint8_t *dst = f->data[0];
  65. dst += (height - 1) * f->linesize[0];
  66. for (i = height; i; i--) {
  67. for (j = linelen / 2; j; j--) {
  68. dst[0] = src[1];
  69. dst[1] = src[0];
  70. src += 2;
  71. dst += 2;
  72. }
  73. dst -= f->linesize[0] + linelen;
  74. }
  75. }
  76. static void copy_frame_32(AVFrame *f, uint8_t *src,
  77. int linelen, int height) {
  78. int i, j;
  79. uint8_t *dst = f->data[0];
  80. dst += (height - 1) * f->linesize[0];
  81. for (i = height; i; i--) {
  82. for (j = linelen / 4; j; j--) {
  83. dst[0] = src[3];
  84. dst[1] = src[2];
  85. dst[2] = src[1];
  86. dst[3] = src[0];
  87. src += 4;
  88. dst += 4;
  89. }
  90. dst -= f->linesize[0] + linelen;
  91. }
  92. }
  93. static void add_frame_16(AVFrame *f, uint8_t *src,
  94. int linelen, int height) {
  95. int i, j;
  96. uint8_t *dst = f->data[0];
  97. dst += (height - 1) * f->linesize[0];
  98. for (i = height; i; i--) {
  99. for (j = linelen / 2; j; j--) {
  100. dst[0] += src[1];
  101. dst[1] += src[0];
  102. src += 2;
  103. dst += 2;
  104. }
  105. dst -= f->linesize[0] + linelen;
  106. }
  107. }
  108. static void add_frame_32(AVFrame *f, uint8_t *src,
  109. int linelen, int height) {
  110. int i, j;
  111. uint8_t *dst = f->data[0];
  112. dst += (height - 1) * f->linesize[0];
  113. for (i = height; i; i--) {
  114. for (j = linelen / 4; j; j--) {
  115. dst[0] += src[3];
  116. dst[1] += src[2];
  117. dst[2] += src[1];
  118. dst[3] += src[0];
  119. src += 4;
  120. dst += 4;
  121. }
  122. dst -= f->linesize[0] + linelen;
  123. }
  124. }
  125. #endif
  126. static int decode_frame(AVCodecContext *avctx, void *data, int *data_size,
  127. uint8_t *buf, int buf_size) {
  128. CamStudioContext *c = (CamStudioContext *)avctx->priv_data;
  129. AVFrame *picture = data;
  130. if (buf_size < 2) {
  131. av_log(avctx, AV_LOG_ERROR, "coded frame too small\n");
  132. return -1;
  133. }
  134. if (c->pic.data[0])
  135. avctx->release_buffer(avctx, &c->pic);
  136. c->pic.reference = 1;
  137. c->pic.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_READABLE |
  138. FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
  139. if (avctx->get_buffer(avctx, &c->pic) < 0) {
  140. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  141. return -1;
  142. }
  143. // decompress data
  144. switch ((buf[0] >> 1) & 7) {
  145. case 0: { // lzo compression
  146. int outlen = c->decomp_size, inlen = buf_size - 2;
  147. if (lzo1x_decode(c->decomp_buf, &outlen, &buf[2], &inlen))
  148. av_log(avctx, AV_LOG_ERROR, "error during lzo decompression\n");
  149. break;
  150. }
  151. case 1: { // zlib compression
  152. #ifdef CONFIG_ZLIB
  153. unsigned long dlen = c->decomp_size;
  154. if (uncompress(c->decomp_buf, &dlen, &buf[2], buf_size - 2) != Z_OK)
  155. av_log(avctx, AV_LOG_ERROR, "error during zlib decompression\n");
  156. break;
  157. #else
  158. av_log(avctx, AV_LOG_ERROR, "compiled without zlib support\n");
  159. return -1;
  160. #endif
  161. }
  162. default:
  163. av_log(avctx, AV_LOG_ERROR, "unknown compression\n");
  164. return -1;
  165. }
  166. // flip upside down, add difference frame
  167. if (buf[0] & 1) { // keyframe
  168. c->pic.pict_type = FF_I_TYPE;
  169. c->pic.key_frame = 1;
  170. switch (c->bpp) {
  171. case 16:
  172. copy_frame_16(&c->pic, c->decomp_buf, c->linelen, c->height);
  173. break;
  174. case 32:
  175. copy_frame_32(&c->pic, c->decomp_buf, c->linelen, c->height);
  176. break;
  177. default:
  178. copy_frame_default(&c->pic, c->decomp_buf, c->linelen, c->height);
  179. }
  180. } else {
  181. c->pic.pict_type = FF_P_TYPE;
  182. c->pic.key_frame = 0;
  183. switch (c->bpp) {
  184. case 16:
  185. add_frame_16(&c->pic, c->decomp_buf, c->linelen, c->height);
  186. break;
  187. case 32:
  188. add_frame_32(&c->pic, c->decomp_buf, c->linelen, c->height);
  189. break;
  190. default:
  191. add_frame_default(&c->pic, c->decomp_buf, c->linelen, c->height);
  192. }
  193. }
  194. *picture = c->pic;
  195. *data_size = sizeof(AVFrame);
  196. return buf_size;
  197. }
  198. static int decode_init(AVCodecContext *avctx) {
  199. CamStudioContext *c = (CamStudioContext *)avctx->priv_data;
  200. if (avcodec_check_dimensions(avctx, avctx->height, avctx->width) < 0) {
  201. return 1;
  202. }
  203. avctx->has_b_frames = 0;
  204. switch (avctx->bits_per_sample) {
  205. case 16: avctx->pix_fmt = PIX_FMT_RGB565; break;
  206. case 24: avctx->pix_fmt = PIX_FMT_BGR24; break;
  207. case 32: avctx->pix_fmt = PIX_FMT_RGBA32; break;
  208. default:
  209. av_log(avctx, AV_LOG_ERROR,
  210. "CamStudio codec error: unvalid depth %i bpp\n",
  211. avctx->bits_per_sample);
  212. return 1;
  213. }
  214. c->bpp = avctx->bits_per_sample;
  215. c->pic.data[0] = NULL;
  216. c->linelen = avctx->width * avctx->bits_per_sample / 8;
  217. c->height = avctx->height;
  218. c->decomp_size = c->height * c->linelen;
  219. c->decomp_buf = av_malloc(c->decomp_size + LZO_OUTPUT_PADDING);
  220. if (!c->decomp_buf) {
  221. av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
  222. return 1;
  223. }
  224. return 0;
  225. }
  226. static int decode_end(AVCodecContext *avctx) {
  227. CamStudioContext *c = (CamStudioContext *)avctx->priv_data;
  228. av_freep(&c->decomp_buf);
  229. if (c->pic.data[0])
  230. avctx->release_buffer(avctx, &c->pic);
  231. return 0;
  232. }
  233. AVCodec cscd_decoder = {
  234. "camstudio",
  235. CODEC_TYPE_VIDEO,
  236. CODEC_ID_CSCD,
  237. sizeof(CamStudioContext),
  238. decode_init,
  239. NULL,
  240. decode_end,
  241. decode_frame,
  242. CODEC_CAP_DR1,
  243. };