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.

197 lines
4.8KB

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