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.

266 lines
7.8KB

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