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.

204 lines
5.1KB

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