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.

191 lines
4.5KB

  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 "lcl.h"
  43. #include <zlib.h>
  44. /*
  45. * Decoder context
  46. */
  47. typedef struct LclEncContext {
  48. AVCodecContext *avctx;
  49. AVFrame pic;
  50. // Image type
  51. int imgtype;
  52. // Compression type
  53. int compression;
  54. // Flags
  55. int flags;
  56. z_stream zstream;
  57. } LclEncContext;
  58. /*
  59. *
  60. * Encode a frame
  61. *
  62. */
  63. static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
  64. LclEncContext *c = avctx->priv_data;
  65. AVFrame *pict = data;
  66. AVFrame * const p = &c->pic;
  67. int i;
  68. int zret; // Zlib return code
  69. *p = *pict;
  70. p->pict_type= FF_I_TYPE;
  71. p->key_frame= 1;
  72. if(avctx->pix_fmt != PIX_FMT_BGR24){
  73. av_log(avctx, AV_LOG_ERROR, "Format not supported!\n");
  74. return -1;
  75. }
  76. zret = deflateReset(&c->zstream);
  77. if (zret != Z_OK) {
  78. av_log(avctx, AV_LOG_ERROR, "Deflate reset error: %d\n", zret);
  79. return -1;
  80. }
  81. c->zstream.next_out = buf;
  82. c->zstream.avail_out = buf_size;
  83. for(i = avctx->height - 1; i >= 0; i--) {
  84. c->zstream.next_in = p->data[0]+p->linesize[0]*i;
  85. c->zstream.avail_in = avctx->width*3;
  86. zret = deflate(&c->zstream, Z_NO_FLUSH);
  87. if (zret != Z_OK) {
  88. av_log(avctx, AV_LOG_ERROR, "Deflate error: %d\n", zret);
  89. return -1;
  90. }
  91. }
  92. zret = deflate(&c->zstream, Z_FINISH);
  93. if (zret != Z_STREAM_END) {
  94. av_log(avctx, AV_LOG_ERROR, "Deflate error: %d\n", zret);
  95. return -1;
  96. }
  97. return c->zstream.total_out;
  98. }
  99. /*
  100. *
  101. * Init lcl encoder
  102. *
  103. */
  104. static av_cold int encode_init(AVCodecContext *avctx)
  105. {
  106. LclEncContext *c = avctx->priv_data;
  107. int zret; // Zlib return code
  108. c->avctx= avctx;
  109. assert(avctx->width && avctx->height);
  110. avctx->extradata= av_mallocz(8);
  111. avctx->coded_frame= &c->pic;
  112. // Will be user settable someday
  113. c->compression = 6;
  114. c->flags = 0;
  115. switch(avctx->pix_fmt){
  116. case PIX_FMT_BGR24:
  117. c->imgtype = IMGTYPE_RGB24;
  118. avctx->bits_per_coded_sample= 24;
  119. break;
  120. default:
  121. av_log(avctx, AV_LOG_ERROR, "Input pixel format %s not supported\n", avcodec_get_pix_fmt_name(avctx->pix_fmt));
  122. return -1;
  123. }
  124. avctx->extradata[0]= 4;
  125. avctx->extradata[1]= 0;
  126. avctx->extradata[2]= 0;
  127. avctx->extradata[3]= 0;
  128. avctx->extradata[4]= c->imgtype;
  129. avctx->extradata[5]= c->compression;
  130. avctx->extradata[6]= c->flags;
  131. avctx->extradata[7]= CODEC_ZLIB;
  132. c->avctx->extradata_size= 8;
  133. c->zstream.zalloc = Z_NULL;
  134. c->zstream.zfree = Z_NULL;
  135. c->zstream.opaque = Z_NULL;
  136. zret = deflateInit(&c->zstream, c->compression);
  137. if (zret != Z_OK) {
  138. av_log(avctx, AV_LOG_ERROR, "Deflate init error: %d\n", zret);
  139. return 1;
  140. }
  141. return 0;
  142. }
  143. /*
  144. *
  145. * Uninit lcl encoder
  146. *
  147. */
  148. static av_cold int encode_end(AVCodecContext *avctx)
  149. {
  150. LclEncContext *c = avctx->priv_data;
  151. av_freep(&avctx->extradata);
  152. deflateEnd(&c->zstream);
  153. return 0;
  154. }
  155. AVCodec zlib_encoder = {
  156. "zlib",
  157. CODEC_TYPE_VIDEO,
  158. CODEC_ID_ZLIB,
  159. sizeof(LclEncContext),
  160. encode_init,
  161. encode_frame,
  162. encode_end,
  163. .long_name = NULL_IF_CONFIG_SMALL("LCL (LossLess Codec Library) ZLIB"),
  164. };