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.

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