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.

194 lines
4.7KB

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