lclenc.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*
  2. * LCL (LossLess Codec Library) Codec
  3. * Copyright (c) 2002-2004 Roberto Togni
  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/lclenc.c
  23. * LCL (LossLess Codec Library) Video Codec
  24. * Decoder for MSZH and ZLIB codecs
  25. * Experimental encoder for ZLIB RGB24
  26. *
  27. * Fourcc: MSZH, ZLIB
  28. *
  29. * Original Win32 dll:
  30. * Ver2.23 By Kenji Oshima 2000.09.20
  31. * avimszh.dll, avizlib.dll
  32. *
  33. * A description of the decoding algorithm can be found here:
  34. * http://www.pcisys.net/~melanson/codecs
  35. *
  36. * Supports: BGR24 (RGB 24bpp)
  37. *
  38. */
  39. #include <stdio.h>
  40. #include <stdlib.h>
  41. #include "avcodec.h"
  42. #include "bitstream.h"
  43. #include "lcl.h"
  44. #if CONFIG_ZLIB
  45. #include <zlib.h>
  46. #endif
  47. /*
  48. * Decoder context
  49. */
  50. typedef struct LclEncContext {
  51. AVCodecContext *avctx;
  52. AVFrame pic;
  53. PutBitContext pb;
  54. // Image type
  55. int imgtype;
  56. // Compression type
  57. int compression;
  58. // Flags
  59. int flags;
  60. // Decompressed data size
  61. unsigned int decomp_size;
  62. // Maximum compressed data size
  63. unsigned int max_comp_size;
  64. // Compression buffer
  65. unsigned char* comp_buf;
  66. #if CONFIG_ZLIB
  67. z_stream zstream;
  68. #endif
  69. } LclEncContext;
  70. /*
  71. *
  72. * Encode a frame
  73. *
  74. */
  75. static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
  76. LclEncContext *c = avctx->priv_data;
  77. AVFrame *pict = data;
  78. AVFrame * const p = &c->pic;
  79. int i;
  80. int zret; // Zlib return code
  81. #if !CONFIG_ZLIB
  82. av_log(avctx, AV_LOG_ERROR, "Zlib support not compiled in.\n");
  83. return -1;
  84. #else
  85. init_put_bits(&c->pb, buf, buf_size);
  86. *p = *pict;
  87. p->pict_type= FF_I_TYPE;
  88. p->key_frame= 1;
  89. if(avctx->pix_fmt != PIX_FMT_BGR24){
  90. av_log(avctx, AV_LOG_ERROR, "Format not supported!\n");
  91. return -1;
  92. }
  93. zret = deflateReset(&(c->zstream));
  94. if (zret != Z_OK) {
  95. av_log(avctx, AV_LOG_ERROR, "Deflate reset error: %d\n", zret);
  96. return -1;
  97. }
  98. c->zstream.next_out = c->comp_buf;
  99. c->zstream.avail_out = c->max_comp_size;
  100. for(i = avctx->height - 1; i >= 0; i--) {
  101. c->zstream.next_in = p->data[0]+p->linesize[0]*i;
  102. c->zstream.avail_in = avctx->width*3;
  103. zret = deflate(&(c->zstream), Z_NO_FLUSH);
  104. if (zret != Z_OK) {
  105. av_log(avctx, AV_LOG_ERROR, "Deflate error: %d\n", zret);
  106. return -1;
  107. }
  108. }
  109. zret = deflate(&(c->zstream), Z_FINISH);
  110. if (zret != Z_STREAM_END) {
  111. av_log(avctx, AV_LOG_ERROR, "Deflate error: %d\n", zret);
  112. return -1;
  113. }
  114. for (i = 0; i < c->zstream.total_out; i++)
  115. put_bits(&c->pb, 8, c->comp_buf[i]);
  116. flush_put_bits(&c->pb);
  117. return c->zstream.total_out;
  118. #endif
  119. }
  120. /*
  121. *
  122. * Init lcl encoder
  123. *
  124. */
  125. static av_cold int encode_init(AVCodecContext *avctx)
  126. {
  127. LclEncContext *c = avctx->priv_data;
  128. int zret; // Zlib return code
  129. #if !CONFIG_ZLIB
  130. av_log(avctx, AV_LOG_ERROR, "Zlib support not compiled.\n");
  131. return 1;
  132. #else
  133. c->avctx= avctx;
  134. assert(avctx->width && avctx->height);
  135. avctx->extradata= av_mallocz(8);
  136. avctx->coded_frame= &c->pic;
  137. // Will be user settable someday
  138. c->compression = 6;
  139. c->flags = 0;
  140. switch(avctx->pix_fmt){
  141. case PIX_FMT_BGR24:
  142. c->imgtype = IMGTYPE_RGB24;
  143. c->decomp_size = avctx->width * avctx->height * 3;
  144. avctx->bits_per_coded_sample= 24;
  145. break;
  146. default:
  147. av_log(avctx, AV_LOG_ERROR, "Input pixel format %s not supported\n", avcodec_get_pix_fmt_name(avctx->pix_fmt));
  148. return -1;
  149. }
  150. ((uint8_t*)avctx->extradata)[0]= 4;
  151. ((uint8_t*)avctx->extradata)[1]= 0;
  152. ((uint8_t*)avctx->extradata)[2]= 0;
  153. ((uint8_t*)avctx->extradata)[3]= 0;
  154. ((uint8_t*)avctx->extradata)[4]= c->imgtype;
  155. ((uint8_t*)avctx->extradata)[5]= c->compression;
  156. ((uint8_t*)avctx->extradata)[6]= c->flags;
  157. ((uint8_t*)avctx->extradata)[7]= CODEC_ZLIB;
  158. c->avctx->extradata_size= 8;
  159. c->zstream.zalloc = Z_NULL;
  160. c->zstream.zfree = Z_NULL;
  161. c->zstream.opaque = Z_NULL;
  162. zret = deflateInit(&(c->zstream), c->compression);
  163. if (zret != Z_OK) {
  164. av_log(avctx, AV_LOG_ERROR, "Deflate init error: %d\n", zret);
  165. return 1;
  166. }
  167. /* Conservative upper bound taken from zlib v1.2.1 source */
  168. c->max_comp_size = c->decomp_size + ((c->decomp_size + 7) >> 3) +
  169. ((c->decomp_size + 63) >> 6) + 11;
  170. if ((c->comp_buf = av_malloc(c->max_comp_size)) == NULL) {
  171. av_log(avctx, AV_LOG_ERROR, "Can't allocate compression buffer.\n");
  172. return 1;
  173. }
  174. return 0;
  175. #endif
  176. }
  177. /*
  178. *
  179. * Uninit lcl encoder
  180. *
  181. */
  182. static av_cold int encode_end(AVCodecContext *avctx)
  183. {
  184. LclEncContext *c = avctx->priv_data;
  185. av_freep(&avctx->extradata);
  186. av_freep(&c->comp_buf);
  187. #if CONFIG_ZLIB
  188. deflateEnd(&(c->zstream));
  189. #endif
  190. return 0;
  191. }
  192. AVCodec zlib_encoder = {
  193. "zlib",
  194. CODEC_TYPE_VIDEO,
  195. CODEC_ID_ZLIB,
  196. sizeof(LclEncContext),
  197. encode_init,
  198. encode_frame,
  199. encode_end,
  200. .long_name = NULL_IF_CONFIG_SMALL("LCL (LossLess Codec Library) ZLIB"),
  201. };