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
8.4KB

  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 {
  31. AVFrame pic;
  32. int linelen, height, bpp;
  33. unsigned int decomp_size;
  34. unsigned char* decomp_buf;
  35. } CamStudioContext;
  36. static void copy_frame_default(AVFrame *f, const uint8_t *src, int src_stride,
  37. int linelen, int height) {
  38. int i;
  39. uint8_t *dst = f->data[0];
  40. dst += (height - 1) * f->linesize[0];
  41. for (i = height; i; i--) {
  42. memcpy(dst, src, linelen);
  43. src += src_stride;
  44. dst -= f->linesize[0];
  45. }
  46. }
  47. static void add_frame_default(AVFrame *f, const uint8_t *src, int src_stride,
  48. int linelen, int height) {
  49. int i, j;
  50. uint8_t *dst = f->data[0];
  51. dst += (height - 1) * f->linesize[0];
  52. for (i = height; i; i--) {
  53. for (j = linelen; j; j--)
  54. *dst++ += *src++;
  55. src += src_stride - linelen;
  56. dst -= f->linesize[0] + linelen;
  57. }
  58. }
  59. #if !HAVE_BIGENDIAN
  60. #define copy_frame_16(f, s, l, h) copy_frame_default(f, s, l, l, h)
  61. #define copy_frame_32(f, s, l, h) copy_frame_default(f, s, l, l, h)
  62. #define add_frame_16(f, s, l, h) add_frame_default(f, s, l, l, h)
  63. #define add_frame_32(f, s, l, h) add_frame_default(f, s, l, l, h)
  64. #else
  65. static void copy_frame_16(AVFrame *f, const uint8_t *src,
  66. int linelen, int height) {
  67. int i, j;
  68. uint8_t *dst = f->data[0];
  69. dst += (height - 1) * f->linesize[0];
  70. for (i = height; i; i--) {
  71. for (j = linelen / 2; j; j--) {
  72. dst[0] = src[1];
  73. dst[1] = src[0];
  74. src += 2;
  75. dst += 2;
  76. }
  77. dst -= f->linesize[0] + linelen;
  78. }
  79. }
  80. static void copy_frame_32(AVFrame *f, const uint8_t *src,
  81. int linelen, int height) {
  82. int i, j;
  83. uint8_t *dst = f->data[0];
  84. dst += (height - 1) * f->linesize[0];
  85. for (i = height; i; i--) {
  86. for (j = linelen / 4; j; j--) {
  87. dst[0] = src[3];
  88. dst[1] = src[2];
  89. dst[2] = src[1];
  90. dst[3] = src[0];
  91. src += 4;
  92. dst += 4;
  93. }
  94. dst -= f->linesize[0] + linelen;
  95. }
  96. }
  97. static void add_frame_16(AVFrame *f, const uint8_t *src,
  98. int linelen, int height) {
  99. int i, j;
  100. uint8_t *dst = f->data[0];
  101. dst += (height - 1) * f->linesize[0];
  102. for (i = height; i; i--) {
  103. for (j = linelen / 2; j; j--) {
  104. dst[0] += src[1];
  105. dst[1] += src[0];
  106. src += 2;
  107. dst += 2;
  108. }
  109. dst -= f->linesize[0] + linelen;
  110. }
  111. }
  112. static void add_frame_32(AVFrame *f, const uint8_t *src,
  113. int linelen, int height) {
  114. int i, j;
  115. uint8_t *dst = f->data[0];
  116. dst += (height - 1) * f->linesize[0];
  117. for (i = height; i; i--) {
  118. for (j = linelen / 4; j; j--) {
  119. dst[0] += src[3];
  120. dst[1] += src[2];
  121. dst[2] += src[1];
  122. dst[3] += src[0];
  123. src += 4;
  124. dst += 4;
  125. }
  126. dst -= f->linesize[0] + linelen;
  127. }
  128. }
  129. #endif
  130. static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
  131. AVPacket *avpkt) {
  132. const uint8_t *buf = avpkt->data;
  133. int buf_size = avpkt->size;
  134. CamStudioContext *c = avctx->priv_data;
  135. AVFrame *picture = data;
  136. int ret;
  137. if (buf_size < 2) {
  138. av_log(avctx, AV_LOG_ERROR, "coded frame too small\n");
  139. return AVERROR_INVALIDDATA;
  140. }
  141. if (c->pic.data[0])
  142. avctx->release_buffer(avctx, &c->pic);
  143. c->pic.reference = 1;
  144. c->pic.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_READABLE |
  145. FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
  146. if ((ret = ff_get_buffer(avctx, &c->pic)) < 0) {
  147. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  148. return ret;
  149. }
  150. // decompress data
  151. switch ((buf[0] >> 1) & 7) {
  152. case 0: { // lzo compression
  153. int outlen = c->decomp_size, inlen = buf_size - 2;
  154. if (av_lzo1x_decode(c->decomp_buf, &outlen, &buf[2], &inlen))
  155. av_log(avctx, AV_LOG_ERROR, "error during lzo decompression\n");
  156. break;
  157. }
  158. case 1: { // zlib compression
  159. #if CONFIG_ZLIB
  160. unsigned long dlen = c->decomp_size;
  161. if (uncompress(c->decomp_buf, &dlen, &buf[2], buf_size - 2) != Z_OK)
  162. av_log(avctx, AV_LOG_ERROR, "error during zlib decompression\n");
  163. break;
  164. #else
  165. av_log(avctx, AV_LOG_ERROR, "compiled without zlib support\n");
  166. return AVERROR(ENOSYS);
  167. #endif
  168. }
  169. default:
  170. av_log(avctx, AV_LOG_ERROR, "unknown compression\n");
  171. return AVERROR_INVALIDDATA;
  172. }
  173. // flip upside down, add difference frame
  174. if (buf[0] & 1) { // keyframe
  175. c->pic.pict_type = AV_PICTURE_TYPE_I;
  176. c->pic.key_frame = 1;
  177. switch (c->bpp) {
  178. case 16:
  179. copy_frame_16(&c->pic, c->decomp_buf, c->linelen, c->height);
  180. break;
  181. case 32:
  182. copy_frame_32(&c->pic, c->decomp_buf, c->linelen, c->height);
  183. break;
  184. default:
  185. copy_frame_default(&c->pic, c->decomp_buf, FFALIGN(c->linelen, 4),
  186. c->linelen, c->height);
  187. }
  188. } else {
  189. c->pic.pict_type = AV_PICTURE_TYPE_P;
  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, FFALIGN(c->linelen, 4),
  200. c->linelen, c->height);
  201. }
  202. }
  203. *picture = c->pic;
  204. *got_frame = 1;
  205. return buf_size;
  206. }
  207. static av_cold int decode_init(AVCodecContext *avctx) {
  208. CamStudioContext *c = avctx->priv_data;
  209. int stride;
  210. switch (avctx->bits_per_coded_sample) {
  211. case 16: avctx->pix_fmt = AV_PIX_FMT_RGB555; break;
  212. case 24: avctx->pix_fmt = AV_PIX_FMT_BGR24; break;
  213. case 32: avctx->pix_fmt = AV_PIX_FMT_RGB32; break;
  214. default:
  215. av_log(avctx, AV_LOG_ERROR,
  216. "CamStudio codec error: invalid depth %i bpp\n",
  217. avctx->bits_per_coded_sample);
  218. return AVERROR_INVALIDDATA;
  219. }
  220. c->bpp = avctx->bits_per_coded_sample;
  221. c->pic.data[0] = NULL;
  222. c->linelen = avctx->width * avctx->bits_per_coded_sample / 8;
  223. c->height = avctx->height;
  224. stride = c->linelen;
  225. if (avctx->bits_per_coded_sample == 24)
  226. stride = FFALIGN(stride, 4);
  227. c->decomp_size = c->height * stride;
  228. c->decomp_buf = av_malloc(c->decomp_size + AV_LZO_OUTPUT_PADDING);
  229. if (!c->decomp_buf) {
  230. av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
  231. return AVERROR(ENOMEM);
  232. }
  233. return 0;
  234. }
  235. static av_cold int decode_end(AVCodecContext *avctx) {
  236. CamStudioContext *c = avctx->priv_data;
  237. av_freep(&c->decomp_buf);
  238. if (c->pic.data[0])
  239. avctx->release_buffer(avctx, &c->pic);
  240. return 0;
  241. }
  242. AVCodec ff_cscd_decoder = {
  243. .name = "camstudio",
  244. .type = AVMEDIA_TYPE_VIDEO,
  245. .id = AV_CODEC_ID_CSCD,
  246. .priv_data_size = sizeof(CamStudioContext),
  247. .init = decode_init,
  248. .close = decode_end,
  249. .decode = decode_frame,
  250. .capabilities = CODEC_CAP_DR1,
  251. .long_name = NULL_IF_CONFIG_SMALL("CamStudio"),
  252. };