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.

269 lines
8.3KB

  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 "libavutil/common.h"
  25. #if CONFIG_ZLIB
  26. #include <zlib.h>
  27. #endif
  28. #include "libavutil/lzo.h"
  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, 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 *data_size,
  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. if (buf_size < 2) {
  136. av_log(avctx, AV_LOG_ERROR, "coded frame too small\n");
  137. return -1;
  138. }
  139. if (c->pic.data[0])
  140. avctx->release_buffer(avctx, &c->pic);
  141. c->pic.reference = 1;
  142. c->pic.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_READABLE |
  143. FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
  144. if (avctx->get_buffer(avctx, &c->pic) < 0) {
  145. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  146. return -1;
  147. }
  148. // decompress data
  149. switch ((buf[0] >> 1) & 7) {
  150. case 0: { // lzo compression
  151. int outlen = c->decomp_size, inlen = buf_size - 2;
  152. if (av_lzo1x_decode(c->decomp_buf, &outlen, &buf[2], &inlen))
  153. av_log(avctx, AV_LOG_ERROR, "error during lzo decompression\n");
  154. break;
  155. }
  156. case 1: { // zlib compression
  157. #if CONFIG_ZLIB
  158. unsigned long dlen = c->decomp_size;
  159. if (uncompress(c->decomp_buf, &dlen, &buf[2], buf_size - 2) != Z_OK)
  160. av_log(avctx, AV_LOG_ERROR, "error during zlib decompression\n");
  161. break;
  162. #else
  163. av_log(avctx, AV_LOG_ERROR, "compiled without zlib support\n");
  164. return -1;
  165. #endif
  166. }
  167. default:
  168. av_log(avctx, AV_LOG_ERROR, "unknown compression\n");
  169. return -1;
  170. }
  171. // flip upside down, add difference frame
  172. if (buf[0] & 1) { // keyframe
  173. c->pic.pict_type = AV_PICTURE_TYPE_I;
  174. c->pic.key_frame = 1;
  175. switch (c->bpp) {
  176. case 16:
  177. copy_frame_16(&c->pic, c->decomp_buf, c->linelen, c->height);
  178. break;
  179. case 32:
  180. copy_frame_32(&c->pic, c->decomp_buf, c->linelen, c->height);
  181. break;
  182. default:
  183. copy_frame_default(&c->pic, c->decomp_buf, FFALIGN(c->linelen, 4),
  184. c->linelen, c->height);
  185. }
  186. } else {
  187. c->pic.pict_type = AV_PICTURE_TYPE_P;
  188. c->pic.key_frame = 0;
  189. switch (c->bpp) {
  190. case 16:
  191. add_frame_16(&c->pic, c->decomp_buf, c->linelen, c->height);
  192. break;
  193. case 32:
  194. add_frame_32(&c->pic, c->decomp_buf, c->linelen, c->height);
  195. break;
  196. default:
  197. add_frame_default(&c->pic, c->decomp_buf, FFALIGN(c->linelen, 4),
  198. c->linelen, c->height);
  199. }
  200. }
  201. *picture = c->pic;
  202. *data_size = sizeof(AVFrame);
  203. return buf_size;
  204. }
  205. static av_cold int decode_init(AVCodecContext *avctx) {
  206. CamStudioContext *c = avctx->priv_data;
  207. int stride;
  208. switch (avctx->bits_per_coded_sample) {
  209. case 16: avctx->pix_fmt = AV_PIX_FMT_RGB555; break;
  210. case 24: avctx->pix_fmt = AV_PIX_FMT_BGR24; break;
  211. case 32: avctx->pix_fmt = AV_PIX_FMT_RGB32; break;
  212. default:
  213. av_log(avctx, AV_LOG_ERROR,
  214. "CamStudio codec error: invalid depth %i bpp\n",
  215. avctx->bits_per_coded_sample);
  216. return AVERROR_INVALIDDATA;
  217. }
  218. c->bpp = avctx->bits_per_coded_sample;
  219. c->pic.data[0] = NULL;
  220. c->linelen = avctx->width * avctx->bits_per_coded_sample / 8;
  221. c->height = avctx->height;
  222. stride = c->linelen;
  223. if (avctx->bits_per_coded_sample == 24)
  224. stride = FFALIGN(stride, 4);
  225. c->decomp_size = c->height * stride;
  226. c->decomp_buf = av_malloc(c->decomp_size + AV_LZO_OUTPUT_PADDING);
  227. if (!c->decomp_buf) {
  228. av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
  229. return AVERROR(ENOMEM);
  230. }
  231. return 0;
  232. }
  233. static av_cold int decode_end(AVCodecContext *avctx) {
  234. CamStudioContext *c = avctx->priv_data;
  235. av_freep(&c->decomp_buf);
  236. if (c->pic.data[0])
  237. avctx->release_buffer(avctx, &c->pic);
  238. return 0;
  239. }
  240. AVCodec ff_cscd_decoder = {
  241. .name = "camstudio",
  242. .type = AVMEDIA_TYPE_VIDEO,
  243. .id = AV_CODEC_ID_CSCD,
  244. .priv_data_size = sizeof(CamStudioContext),
  245. .init = decode_init,
  246. .close = decode_end,
  247. .decode = decode_frame,
  248. .capabilities = CODEC_CAP_DR1,
  249. .long_name = NULL_IF_CONFIG_SMALL("CamStudio"),
  250. };