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