cscd.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. const uint8_t *buf, int buf_size) {
  129. CamStudioContext *c = avctx->priv_data;
  130. AVFrame *picture = data;
  131. if (buf_size < 2) {
  132. av_log(avctx, AV_LOG_ERROR, "coded frame too small\n");
  133. return -1;
  134. }
  135. if (c->pic.data[0])
  136. avctx->release_buffer(avctx, &c->pic);
  137. c->pic.reference = 1;
  138. c->pic.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_READABLE |
  139. FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
  140. if (avctx->get_buffer(avctx, &c->pic) < 0) {
  141. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  142. return -1;
  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 -1;
  161. #endif
  162. }
  163. default:
  164. av_log(avctx, AV_LOG_ERROR, "unknown compression\n");
  165. return -1;
  166. }
  167. // flip upside down, add difference frame
  168. if (buf[0] & 1) { // keyframe
  169. c->pic.pict_type = FF_I_TYPE;
  170. c->pic.key_frame = 1;
  171. switch (c->bpp) {
  172. case 16:
  173. copy_frame_16(&c->pic, c->decomp_buf, c->linelen, c->height);
  174. break;
  175. case 32:
  176. copy_frame_32(&c->pic, c->decomp_buf, c->linelen, c->height);
  177. break;
  178. default:
  179. copy_frame_default(&c->pic, c->decomp_buf, c->linelen, c->height);
  180. }
  181. } else {
  182. c->pic.pict_type = FF_P_TYPE;
  183. c->pic.key_frame = 0;
  184. switch (c->bpp) {
  185. case 16:
  186. add_frame_16(&c->pic, c->decomp_buf, c->linelen, c->height);
  187. break;
  188. case 32:
  189. add_frame_32(&c->pic, c->decomp_buf, c->linelen, c->height);
  190. break;
  191. default:
  192. add_frame_default(&c->pic, c->decomp_buf, c->linelen, c->height);
  193. }
  194. }
  195. *picture = c->pic;
  196. *data_size = sizeof(AVFrame);
  197. return buf_size;
  198. }
  199. static av_cold int decode_init(AVCodecContext *avctx) {
  200. CamStudioContext *c = avctx->priv_data;
  201. if (avcodec_check_dimensions(avctx, avctx->height, avctx->width) < 0) {
  202. return 1;
  203. }
  204. switch (avctx->bits_per_coded_sample) {
  205. case 16: avctx->pix_fmt = PIX_FMT_RGB555; break;
  206. case 24: avctx->pix_fmt = PIX_FMT_BGR24; break;
  207. case 32: avctx->pix_fmt = PIX_FMT_RGB32; break;
  208. default:
  209. av_log(avctx, AV_LOG_ERROR,
  210. "CamStudio codec error: invalid depth %i bpp\n",
  211. avctx->bits_per_coded_sample);
  212. return 1;
  213. }
  214. c->bpp = avctx->bits_per_coded_sample;
  215. c->pic.data[0] = NULL;
  216. c->linelen = avctx->width * avctx->bits_per_coded_sample / 8;
  217. c->height = avctx->height;
  218. c->decomp_size = c->height * c->linelen;
  219. c->decomp_buf = av_malloc(c->decomp_size + AV_LZO_OUTPUT_PADDING);
  220. if (!c->decomp_buf) {
  221. av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
  222. return 1;
  223. }
  224. return 0;
  225. }
  226. static av_cold int decode_end(AVCodecContext *avctx) {
  227. CamStudioContext *c = avctx->priv_data;
  228. av_freep(&c->decomp_buf);
  229. if (c->pic.data[0])
  230. avctx->release_buffer(avctx, &c->pic);
  231. return 0;
  232. }
  233. AVCodec cscd_decoder = {
  234. "camstudio",
  235. CODEC_TYPE_VIDEO,
  236. CODEC_ID_CSCD,
  237. sizeof(CamStudioContext),
  238. decode_init,
  239. NULL,
  240. decode_end,
  241. decode_frame,
  242. CODEC_CAP_DR1,
  243. .long_name = NULL_IF_CONFIG_SMALL("CamStudio"),
  244. };