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.

261 lines
8.0KB

  1. /*
  2. * CamStudio decoder
  3. * Copyright (c) 2006 Reimar Doeffinger
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include "avcodec.h"
  24. #include "internal.h"
  25. #include "libavutil/common.h"
  26. #if CONFIG_ZLIB
  27. #include <zlib.h>
  28. #endif
  29. #include "libavutil/lzo.h"
  30. typedef struct CamStudioContext {
  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, const uint8_t *src, int src_stride,
  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 += src_stride;
  43. dst -= f->linesize[0];
  44. }
  45. }
  46. static void add_frame_default(AVFrame *f, const uint8_t *src, int src_stride,
  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. src += src_stride - linelen;
  55. dst -= f->linesize[0] + linelen;
  56. }
  57. }
  58. #if !HAVE_BIGENDIAN
  59. #define copy_frame_16(f, s, l, h) copy_frame_default(f, s, l, l, h)
  60. #define copy_frame_32(f, s, l, h) copy_frame_default(f, s, l, l, h)
  61. #define add_frame_16(f, s, l, h) add_frame_default(f, s, l, l, h)
  62. #define add_frame_32(f, s, l, h) add_frame_default(f, s, l, l, h)
  63. #else
  64. static void copy_frame_16(AVFrame *f, const uint8_t *src,
  65. int linelen, int height) {
  66. int i, j;
  67. uint8_t *dst = f->data[0];
  68. dst += (height - 1) * f->linesize[0];
  69. for (i = height; i; i--) {
  70. for (j = linelen / 2; j; j--) {
  71. dst[0] = src[1];
  72. dst[1] = src[0];
  73. src += 2;
  74. dst += 2;
  75. }
  76. dst -= f->linesize[0] + linelen;
  77. }
  78. }
  79. static void copy_frame_32(AVFrame *f, const uint8_t *src,
  80. int linelen, int height) {
  81. int i, j;
  82. uint8_t *dst = f->data[0];
  83. dst += (height - 1) * f->linesize[0];
  84. for (i = height; i; i--) {
  85. for (j = linelen / 4; j; j--) {
  86. dst[0] = src[3];
  87. dst[1] = src[2];
  88. dst[2] = src[1];
  89. dst[3] = src[0];
  90. src += 4;
  91. dst += 4;
  92. }
  93. dst -= f->linesize[0] + linelen;
  94. }
  95. }
  96. static void add_frame_16(AVFrame *f, const uint8_t *src,
  97. int linelen, int height) {
  98. int i, j;
  99. uint8_t *dst = f->data[0];
  100. dst += (height - 1) * f->linesize[0];
  101. for (i = height; i; i--) {
  102. for (j = linelen / 2; j; j--) {
  103. dst[0] += src[1];
  104. dst[1] += src[0];
  105. src += 2;
  106. dst += 2;
  107. }
  108. dst -= f->linesize[0] + linelen;
  109. }
  110. }
  111. static void add_frame_32(AVFrame *f, const uint8_t *src,
  112. int linelen, int height) {
  113. int i, j;
  114. uint8_t *dst = f->data[0];
  115. dst += (height - 1) * f->linesize[0];
  116. for (i = height; i; i--) {
  117. for (j = linelen / 4; j; j--) {
  118. dst[0] += src[3];
  119. dst[1] += src[2];
  120. dst[2] += src[1];
  121. dst[3] += src[0];
  122. src += 4;
  123. dst += 4;
  124. }
  125. dst -= f->linesize[0] + linelen;
  126. }
  127. }
  128. #endif
  129. static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
  130. AVPacket *avpkt) {
  131. const uint8_t *buf = avpkt->data;
  132. int buf_size = avpkt->size;
  133. CamStudioContext *c = avctx->priv_data;
  134. AVFrame *picture = data;
  135. int ret;
  136. if (buf_size < 2) {
  137. av_log(avctx, AV_LOG_ERROR, "coded frame too small\n");
  138. return AVERROR_INVALIDDATA;
  139. }
  140. if ((ret = ff_get_buffer(avctx, picture, 0)) < 0) {
  141. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  142. return ret;
  143. }
  144. // decompress data
  145. switch ((buf[0] >> 1) & 7) {
  146. case 0: { // lzo compression
  147. int outlen = c->decomp_size, inlen = buf_size - 2;
  148. if (av_lzo1x_decode(c->decomp_buf, &outlen, &buf[2], &inlen))
  149. av_log(avctx, AV_LOG_ERROR, "error during lzo decompression\n");
  150. break;
  151. }
  152. case 1: { // zlib compression
  153. #if CONFIG_ZLIB
  154. unsigned long dlen = c->decomp_size;
  155. if (uncompress(c->decomp_buf, &dlen, &buf[2], buf_size - 2) != Z_OK)
  156. av_log(avctx, AV_LOG_ERROR, "error during zlib decompression\n");
  157. break;
  158. #else
  159. av_log(avctx, AV_LOG_ERROR, "compiled without zlib support\n");
  160. return AVERROR(ENOSYS);
  161. #endif
  162. }
  163. default:
  164. av_log(avctx, AV_LOG_ERROR, "unknown compression\n");
  165. return AVERROR_INVALIDDATA;
  166. }
  167. // flip upside down, add difference frame
  168. if (buf[0] & 1) { // keyframe
  169. picture->pict_type = AV_PICTURE_TYPE_I;
  170. picture->key_frame = 1;
  171. switch (c->bpp) {
  172. case 16:
  173. copy_frame_16(picture, c->decomp_buf, c->linelen, c->height);
  174. break;
  175. case 32:
  176. copy_frame_32(picture, c->decomp_buf, c->linelen, c->height);
  177. break;
  178. default:
  179. copy_frame_default(picture, c->decomp_buf, FFALIGN(c->linelen, 4),
  180. c->linelen, c->height);
  181. }
  182. } else {
  183. picture->pict_type = AV_PICTURE_TYPE_P;
  184. picture->key_frame = 0;
  185. switch (c->bpp) {
  186. case 16:
  187. add_frame_16(picture, c->decomp_buf, c->linelen, c->height);
  188. break;
  189. case 32:
  190. add_frame_32(picture, c->decomp_buf, c->linelen, c->height);
  191. break;
  192. default:
  193. add_frame_default(picture, c->decomp_buf, FFALIGN(c->linelen, 4),
  194. c->linelen, c->height);
  195. }
  196. }
  197. *got_frame = 1;
  198. return buf_size;
  199. }
  200. static av_cold int decode_init(AVCodecContext *avctx) {
  201. CamStudioContext *c = avctx->priv_data;
  202. int stride;
  203. switch (avctx->bits_per_coded_sample) {
  204. case 16: avctx->pix_fmt = AV_PIX_FMT_RGB555; break;
  205. case 24: avctx->pix_fmt = AV_PIX_FMT_BGR24; break;
  206. case 32: avctx->pix_fmt = AV_PIX_FMT_RGB32; break;
  207. default:
  208. av_log(avctx, AV_LOG_ERROR,
  209. "CamStudio codec error: invalid depth %i bpp\n",
  210. avctx->bits_per_coded_sample);
  211. return AVERROR_INVALIDDATA;
  212. }
  213. c->bpp = avctx->bits_per_coded_sample;
  214. c->linelen = avctx->width * avctx->bits_per_coded_sample / 8;
  215. c->height = avctx->height;
  216. stride = c->linelen;
  217. if (avctx->bits_per_coded_sample == 24)
  218. stride = FFALIGN(stride, 4);
  219. c->decomp_size = c->height * stride;
  220. c->decomp_buf = av_malloc(c->decomp_size + AV_LZO_OUTPUT_PADDING);
  221. if (!c->decomp_buf) {
  222. av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
  223. return AVERROR(ENOMEM);
  224. }
  225. return 0;
  226. }
  227. static av_cold int decode_end(AVCodecContext *avctx) {
  228. CamStudioContext *c = avctx->priv_data;
  229. av_freep(&c->decomp_buf);
  230. return 0;
  231. }
  232. AVCodec ff_cscd_decoder = {
  233. .name = "camstudio",
  234. .long_name = NULL_IF_CONFIG_SMALL("CamStudio"),
  235. .type = AVMEDIA_TYPE_VIDEO,
  236. .id = AV_CODEC_ID_CSCD,
  237. .priv_data_size = sizeof(CamStudioContext),
  238. .init = decode_init,
  239. .close = decode_end,
  240. .decode = decode_frame,
  241. .capabilities = AV_CODEC_CAP_DR1,
  242. };