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.

270 lines
8.1KB

  1. /*
  2. * CamStudio decoder
  3. * Copyright (c) 2006 Reimar Doeffinger
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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. #if CONFIG_ZLIB
  25. #include <zlib.h>
  26. #endif
  27. #include "libavutil/lzo.h"
  28. typedef struct {
  29. AVFrame pic;
  30. int linelen, height, bpp;
  31. unsigned int decomp_size;
  32. unsigned char* decomp_buf;
  33. } CamStudioContext;
  34. static void copy_frame_default(AVFrame *f, const uint8_t *src, int src_stride,
  35. int linelen, int height) {
  36. int i;
  37. uint8_t *dst = f->data[0];
  38. dst += (height - 1) * f->linesize[0];
  39. for (i = height; i; i--) {
  40. memcpy(dst, src, linelen);
  41. src += src_stride;
  42. dst -= f->linesize[0];
  43. }
  44. }
  45. static void add_frame_default(AVFrame *f, const uint8_t *src, int src_stride,
  46. int linelen, int height) {
  47. int i, j;
  48. uint8_t *dst = f->data[0];
  49. dst += (height - 1) * f->linesize[0];
  50. for (i = height; i; i--) {
  51. for (j = linelen; j; j--)
  52. *dst++ += *src++;
  53. src += src_stride - linelen;
  54. dst -= f->linesize[0] + linelen;
  55. }
  56. }
  57. #if !HAVE_BIGENDIAN
  58. #define copy_frame_16(f, s, l, h) copy_frame_default(f, s, l, l, h)
  59. #define copy_frame_32(f, s, l, h) copy_frame_default(f, s, l, l, h)
  60. #define add_frame_16(f, s, l, h) add_frame_default(f, s, l, l, h)
  61. #define add_frame_32(f, s, l, h) add_frame_default(f, s, l, l, h)
  62. #else
  63. static void copy_frame_16(AVFrame *f, const 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, const 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, const 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, const 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. AVPacket *avpkt) {
  130. const uint8_t *buf = avpkt->data;
  131. int buf_size = avpkt->size;
  132. CamStudioContext *c = avctx->priv_data;
  133. AVFrame *picture = data;
  134. if (buf_size < 2) {
  135. av_log(avctx, AV_LOG_ERROR, "coded frame too small\n");
  136. return -1;
  137. }
  138. if (c->pic.data[0])
  139. avctx->release_buffer(avctx, &c->pic);
  140. c->pic.reference = 1;
  141. c->pic.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_READABLE |
  142. FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
  143. if (avctx->get_buffer(avctx, &c->pic) < 0) {
  144. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  145. return -1;
  146. }
  147. // decompress data
  148. switch ((buf[0] >> 1) & 7) {
  149. case 0: { // lzo compression
  150. int outlen = c->decomp_size, inlen = buf_size - 2;
  151. if (av_lzo1x_decode(c->decomp_buf, &outlen, &buf[2], &inlen))
  152. av_log(avctx, AV_LOG_ERROR, "error during lzo decompression\n");
  153. break;
  154. }
  155. case 1: { // zlib compression
  156. #if CONFIG_ZLIB
  157. unsigned long dlen = c->decomp_size;
  158. if (uncompress(c->decomp_buf, &dlen, &buf[2], buf_size - 2) != Z_OK)
  159. av_log(avctx, AV_LOG_ERROR, "error during zlib decompression\n");
  160. break;
  161. #else
  162. av_log(avctx, AV_LOG_ERROR, "compiled without zlib support\n");
  163. return -1;
  164. #endif
  165. }
  166. default:
  167. av_log(avctx, AV_LOG_ERROR, "unknown compression\n");
  168. return -1;
  169. }
  170. // flip upside down, add difference frame
  171. if (buf[0] & 1) { // keyframe
  172. c->pic.pict_type = FF_I_TYPE;
  173. c->pic.key_frame = 1;
  174. switch (c->bpp) {
  175. case 16:
  176. copy_frame_16(&c->pic, c->decomp_buf, c->linelen, c->height);
  177. break;
  178. case 32:
  179. copy_frame_32(&c->pic, c->decomp_buf, c->linelen, c->height);
  180. break;
  181. default:
  182. copy_frame_default(&c->pic, c->decomp_buf, FFALIGN(c->linelen, 4),
  183. c->linelen, c->height);
  184. }
  185. } else {
  186. c->pic.pict_type = FF_P_TYPE;
  187. c->pic.key_frame = 0;
  188. switch (c->bpp) {
  189. case 16:
  190. add_frame_16(&c->pic, c->decomp_buf, c->linelen, c->height);
  191. break;
  192. case 32:
  193. add_frame_32(&c->pic, c->decomp_buf, c->linelen, c->height);
  194. break;
  195. default:
  196. add_frame_default(&c->pic, c->decomp_buf, FFALIGN(c->linelen, 4),
  197. c->linelen, c->height);
  198. }
  199. }
  200. *picture = c->pic;
  201. *data_size = sizeof(AVFrame);
  202. return buf_size;
  203. }
  204. static av_cold int decode_init(AVCodecContext *avctx) {
  205. CamStudioContext *c = avctx->priv_data;
  206. int stride;
  207. switch (avctx->bits_per_coded_sample) {
  208. case 16: avctx->pix_fmt = PIX_FMT_RGB555; break;
  209. case 24: avctx->pix_fmt = PIX_FMT_BGR24; break;
  210. case 32: avctx->pix_fmt = PIX_FMT_RGB32; break;
  211. default:
  212. av_log(avctx, AV_LOG_ERROR,
  213. "CamStudio codec error: invalid depth %i bpp\n",
  214. avctx->bits_per_coded_sample);
  215. return 1;
  216. }
  217. c->bpp = avctx->bits_per_coded_sample;
  218. c->pic.data[0] = NULL;
  219. c->linelen = avctx->width * avctx->bits_per_coded_sample / 8;
  220. c->height = avctx->height;
  221. stride = c->linelen;
  222. if (avctx->bits_per_coded_sample == 24)
  223. stride = FFALIGN(stride, 4);
  224. c->decomp_size = c->height * stride;
  225. c->decomp_buf = av_malloc(c->decomp_size + AV_LZO_OUTPUT_PADDING);
  226. if (!c->decomp_buf) {
  227. av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
  228. return 1;
  229. }
  230. return 0;
  231. }
  232. static av_cold int decode_end(AVCodecContext *avctx) {
  233. CamStudioContext *c = avctx->priv_data;
  234. av_freep(&c->decomp_buf);
  235. if (c->pic.data[0])
  236. avctx->release_buffer(avctx, &c->pic);
  237. return 0;
  238. }
  239. AVCodec ff_cscd_decoder = {
  240. "camstudio",
  241. AVMEDIA_TYPE_VIDEO,
  242. CODEC_ID_CSCD,
  243. sizeof(CamStudioContext),
  244. decode_init,
  245. NULL,
  246. decode_end,
  247. decode_frame,
  248. CODEC_CAP_DR1,
  249. .long_name = NULL_IF_CONFIG_SMALL("CamStudio"),
  250. };