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.

181 lines
5.5KB

  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. #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 CamStudioContext {
  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,
  37. int linelen, int height)
  38. {
  39. int i, src_stride = FFALIGN(linelen, 4);
  40. uint8_t *dst = f->data[0];
  41. dst += (height - 1) * f->linesize[0];
  42. for (i = height; i; i--) {
  43. memcpy(dst, src, linelen);
  44. src += src_stride;
  45. dst -= f->linesize[0];
  46. }
  47. }
  48. static void add_frame_default(AVFrame *f, const uint8_t *src,
  49. int linelen, int height)
  50. {
  51. int i, j, src_stride = FFALIGN(linelen, 4);
  52. uint8_t *dst = f->data[0];
  53. dst += (height - 1) * f->linesize[0];
  54. for (i = height; i; i--) {
  55. for (j = linelen; j; j--)
  56. *dst++ += *src++;
  57. src += src_stride - linelen;
  58. dst -= f->linesize[0] + linelen;
  59. }
  60. }
  61. static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
  62. AVPacket *avpkt)
  63. {
  64. const uint8_t *buf = avpkt->data;
  65. int buf_size = avpkt->size;
  66. CamStudioContext *c = avctx->priv_data;
  67. int ret;
  68. if (buf_size < 2) {
  69. av_log(avctx, AV_LOG_ERROR, "coded frame too small\n");
  70. return AVERROR_INVALIDDATA;
  71. }
  72. if ((ret = ff_reget_buffer(avctx, c->pic, 0)) < 0)
  73. return ret;
  74. // decompress data
  75. switch ((buf[0] >> 1) & 7) {
  76. case 0: { // lzo compression
  77. int outlen = c->decomp_size, inlen = buf_size - 2;
  78. if (av_lzo1x_decode(c->decomp_buf, &outlen, &buf[2], &inlen) || outlen) {
  79. av_log(avctx, AV_LOG_ERROR, "error during lzo decompression\n");
  80. return AVERROR_INVALIDDATA;
  81. }
  82. break;
  83. }
  84. case 1: { // zlib compression
  85. #if CONFIG_ZLIB
  86. unsigned long dlen = c->decomp_size;
  87. if (uncompress(c->decomp_buf, &dlen, &buf[2], buf_size - 2) != Z_OK || dlen != c->decomp_size) {
  88. av_log(avctx, AV_LOG_ERROR, "error during zlib decompression\n");
  89. return AVERROR_INVALIDDATA;
  90. }
  91. break;
  92. #else
  93. av_log(avctx, AV_LOG_ERROR, "compiled without zlib support\n");
  94. return AVERROR(ENOSYS);
  95. #endif
  96. }
  97. default:
  98. av_log(avctx, AV_LOG_ERROR, "unknown compression\n");
  99. return AVERROR_INVALIDDATA;
  100. }
  101. // flip upside down, add difference frame
  102. if (buf[0] & 1) { // keyframe
  103. c->pic->pict_type = AV_PICTURE_TYPE_I;
  104. c->pic->key_frame = 1;
  105. copy_frame_default(c->pic, c->decomp_buf,
  106. c->linelen, c->height);
  107. } else {
  108. c->pic->pict_type = AV_PICTURE_TYPE_P;
  109. c->pic->key_frame = 0;
  110. add_frame_default(c->pic, c->decomp_buf,
  111. c->linelen, c->height);
  112. }
  113. *got_frame = 1;
  114. if ((ret = av_frame_ref(data, c->pic)) < 0)
  115. return ret;
  116. return buf_size;
  117. }
  118. static av_cold int decode_init(AVCodecContext *avctx)
  119. {
  120. CamStudioContext *c = avctx->priv_data;
  121. int stride;
  122. switch (avctx->bits_per_coded_sample) {
  123. case 16: avctx->pix_fmt = AV_PIX_FMT_RGB555LE; break;
  124. case 24: avctx->pix_fmt = AV_PIX_FMT_BGR24; break;
  125. case 32: avctx->pix_fmt = AV_PIX_FMT_BGR0; break;
  126. default:
  127. av_log(avctx, AV_LOG_ERROR,
  128. "CamStudio codec error: invalid depth %i bpp\n",
  129. avctx->bits_per_coded_sample);
  130. return AVERROR_INVALIDDATA;
  131. }
  132. c->bpp = avctx->bits_per_coded_sample;
  133. c->linelen = avctx->width * avctx->bits_per_coded_sample / 8;
  134. c->height = avctx->height;
  135. stride = FFALIGN(c->linelen, 4);
  136. c->decomp_size = c->height * stride;
  137. c->decomp_buf = av_malloc(c->decomp_size + AV_LZO_OUTPUT_PADDING);
  138. if (!c->decomp_buf) {
  139. av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
  140. return AVERROR(ENOMEM);
  141. }
  142. c->pic = av_frame_alloc();
  143. if (!c->pic)
  144. return AVERROR(ENOMEM);
  145. return 0;
  146. }
  147. static av_cold int decode_end(AVCodecContext *avctx)
  148. {
  149. CamStudioContext *c = avctx->priv_data;
  150. av_freep(&c->decomp_buf);
  151. av_frame_free(&c->pic);
  152. return 0;
  153. }
  154. AVCodec ff_cscd_decoder = {
  155. .name = "camstudio",
  156. .long_name = NULL_IF_CONFIG_SMALL("CamStudio"),
  157. .type = AVMEDIA_TYPE_VIDEO,
  158. .id = AV_CODEC_ID_CSCD,
  159. .priv_data_size = sizeof(CamStudioContext),
  160. .init = decode_init,
  161. .close = decode_end,
  162. .decode = decode_frame,
  163. .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
  164. .capabilities = AV_CODEC_CAP_DR1,
  165. };