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.

174 lines
5.6KB

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