tscc.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. * TechSmith Camtasia decoder
  3. * Copyright (c) 2004 Konstantin Shishkov
  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. /**
  22. * @file libavcodec/tscc.c
  23. * TechSmith Camtasia decoder
  24. *
  25. * Fourcc: TSCC
  26. *
  27. * Codec is very simple:
  28. * it codes picture (picture difference, really)
  29. * with algorithm almost identical to Windows RLE8,
  30. * only without padding and with greater pixel sizes,
  31. * then this coded picture is packed with ZLib
  32. *
  33. * Supports: BGR8,BGR555,BGR24 - only BGR8 and BGR555 tested
  34. *
  35. */
  36. #include <stdio.h>
  37. #include <stdlib.h>
  38. #include "avcodec.h"
  39. #include "msrledec.h"
  40. #include <zlib.h>
  41. /*
  42. * Decoder context
  43. */
  44. typedef struct TsccContext {
  45. AVCodecContext *avctx;
  46. AVFrame pic;
  47. // Bits per pixel
  48. int bpp;
  49. // Decompressed data size
  50. unsigned int decomp_size;
  51. // Decompression buffer
  52. unsigned char* decomp_buf;
  53. int height;
  54. z_stream zstream;
  55. } CamtasiaContext;
  56. /*
  57. *
  58. * Decode a frame
  59. *
  60. */
  61. static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, const uint8_t *buf, int buf_size)
  62. {
  63. CamtasiaContext * const c = avctx->priv_data;
  64. const unsigned char *encoded = buf;
  65. unsigned char *outptr;
  66. int zret; // Zlib return code
  67. int len = buf_size;
  68. if(c->pic.data[0])
  69. avctx->release_buffer(avctx, &c->pic);
  70. c->pic.reference = 1;
  71. c->pic.buffer_hints = FF_BUFFER_HINTS_VALID;
  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. outptr = c->pic.data[0]; // Output image pointer
  77. zret = inflateReset(&(c->zstream));
  78. if (zret != Z_OK) {
  79. av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", zret);
  80. return -1;
  81. }
  82. c->zstream.next_in = encoded;
  83. c->zstream.avail_in = len;
  84. c->zstream.next_out = c->decomp_buf;
  85. c->zstream.avail_out = c->decomp_size;
  86. zret = inflate(&(c->zstream), Z_FINISH);
  87. // Z_DATA_ERROR means empty picture
  88. if ((zret != Z_OK) && (zret != Z_STREAM_END) && (zret != Z_DATA_ERROR)) {
  89. av_log(avctx, AV_LOG_ERROR, "Inflate error: %d\n", zret);
  90. return -1;
  91. }
  92. if(zret != Z_DATA_ERROR)
  93. ff_msrle_decode(avctx, (AVPicture*)&c->pic, c->bpp, c->decomp_buf, c->zstream.avail_out);
  94. /* make the palette available on the way out */
  95. if (c->avctx->pix_fmt == PIX_FMT_PAL8) {
  96. memcpy(c->pic.data[1], c->avctx->palctrl->palette, AVPALETTE_SIZE);
  97. if (c->avctx->palctrl->palette_changed) {
  98. c->pic.palette_has_changed = 1;
  99. c->avctx->palctrl->palette_changed = 0;
  100. }
  101. }
  102. *data_size = sizeof(AVFrame);
  103. *(AVFrame*)data = c->pic;
  104. /* always report that the buffer was completely consumed */
  105. return buf_size;
  106. }
  107. /*
  108. *
  109. * Init tscc decoder
  110. *
  111. */
  112. static av_cold int decode_init(AVCodecContext *avctx)
  113. {
  114. CamtasiaContext * const c = avctx->priv_data;
  115. int zret; // Zlib return code
  116. c->avctx = avctx;
  117. c->pic.data[0] = NULL;
  118. c->height = avctx->height;
  119. if (avcodec_check_dimensions(avctx, avctx->width, avctx->height) < 0) {
  120. return 1;
  121. }
  122. // Needed if zlib unused or init aborted before inflateInit
  123. memset(&(c->zstream), 0, sizeof(z_stream));
  124. switch(avctx->bits_per_coded_sample){
  125. case 8: avctx->pix_fmt = PIX_FMT_PAL8; break;
  126. case 16: avctx->pix_fmt = PIX_FMT_RGB555; break;
  127. case 24:
  128. avctx->pix_fmt = PIX_FMT_BGR24;
  129. break;
  130. case 32: avctx->pix_fmt = PIX_FMT_RGB32; break;
  131. default: av_log(avctx, AV_LOG_ERROR, "Camtasia error: unknown depth %i bpp\n", avctx->bits_per_coded_sample);
  132. return -1;
  133. }
  134. c->bpp = avctx->bits_per_coded_sample;
  135. c->decomp_size = (avctx->width * c->bpp + (avctx->width + 254) / 255 + 2) * avctx->height + 2;//RLE in the 'best' case
  136. /* Allocate decompression buffer */
  137. if (c->decomp_size) {
  138. if ((c->decomp_buf = av_malloc(c->decomp_size)) == NULL) {
  139. av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
  140. return 1;
  141. }
  142. }
  143. c->zstream.zalloc = Z_NULL;
  144. c->zstream.zfree = Z_NULL;
  145. c->zstream.opaque = Z_NULL;
  146. zret = inflateInit(&(c->zstream));
  147. if (zret != Z_OK) {
  148. av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
  149. return 1;
  150. }
  151. return 0;
  152. }
  153. /*
  154. *
  155. * Uninit tscc decoder
  156. *
  157. */
  158. static av_cold int decode_end(AVCodecContext *avctx)
  159. {
  160. CamtasiaContext * const c = avctx->priv_data;
  161. av_freep(&c->decomp_buf);
  162. if (c->pic.data[0])
  163. avctx->release_buffer(avctx, &c->pic);
  164. inflateEnd(&(c->zstream));
  165. return 0;
  166. }
  167. AVCodec tscc_decoder = {
  168. "camtasia",
  169. CODEC_TYPE_VIDEO,
  170. CODEC_ID_TSCC,
  171. sizeof(CamtasiaContext),
  172. decode_init,
  173. NULL,
  174. decode_end,
  175. decode_frame,
  176. CODEC_CAP_DR1,
  177. .long_name = NULL_IF_CONFIG_SMALL("TechSmith Screen Capture Codec"),
  178. };