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.

175 lines
5.7KB

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