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.

184 lines
5.1KB

  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. #include <stdio.h>
  39. #include <stdlib.h>
  40. #include "libavutil/avassert.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. typedef struct LclEncContext {
  48. AVCodecContext *avctx;
  49. // Image type
  50. int imgtype;
  51. // Compression type
  52. int compression;
  53. // Flags
  54. int flags;
  55. z_stream zstream;
  56. } LclEncContext;
  57. static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
  58. const AVFrame *p, int *got_packet)
  59. {
  60. LclEncContext *c = avctx->priv_data;
  61. int i, ret;
  62. int zret; // Zlib return code
  63. int max_size = deflateBound(&c->zstream, avctx->width * avctx->height * 3);
  64. if ((ret = ff_alloc_packet2(avctx, pkt, max_size, 0)) < 0)
  65. return ret;
  66. if(avctx->pix_fmt != AV_PIX_FMT_BGR24){
  67. av_log(avctx, AV_LOG_ERROR, "Format not supported!\n");
  68. return -1;
  69. }
  70. zret = deflateReset(&c->zstream);
  71. if (zret != Z_OK) {
  72. av_log(avctx, AV_LOG_ERROR, "Deflate reset error: %d\n", zret);
  73. return -1;
  74. }
  75. c->zstream.next_out = pkt->data;
  76. c->zstream.avail_out = pkt->size;
  77. for(i = avctx->height - 1; i >= 0; i--) {
  78. c->zstream.next_in = p->data[0]+p->linesize[0]*i;
  79. c->zstream.avail_in = avctx->width*3;
  80. zret = deflate(&c->zstream, Z_NO_FLUSH);
  81. if (zret != Z_OK) {
  82. av_log(avctx, AV_LOG_ERROR, "Deflate error: %d\n", zret);
  83. return -1;
  84. }
  85. }
  86. zret = deflate(&c->zstream, Z_FINISH);
  87. if (zret != Z_STREAM_END) {
  88. av_log(avctx, AV_LOG_ERROR, "Deflate error: %d\n", zret);
  89. return -1;
  90. }
  91. pkt->size = c->zstream.total_out;
  92. pkt->flags |= AV_PKT_FLAG_KEY;
  93. *got_packet = 1;
  94. return 0;
  95. }
  96. static av_cold int encode_init(AVCodecContext *avctx)
  97. {
  98. LclEncContext *c = avctx->priv_data;
  99. int zret; // Zlib return code
  100. c->avctx= avctx;
  101. av_assert0(avctx->width && avctx->height);
  102. avctx->extradata = av_mallocz(8 + AV_INPUT_BUFFER_PADDING_SIZE);
  103. if (!avctx->extradata)
  104. return AVERROR(ENOMEM);
  105. #if FF_API_CODED_FRAME
  106. FF_DISABLE_DEPRECATION_WARNINGS
  107. avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
  108. avctx->coded_frame->key_frame = 1;
  109. FF_ENABLE_DEPRECATION_WARNINGS
  110. #endif
  111. c->compression = avctx->compression_level == FF_COMPRESSION_DEFAULT ?
  112. COMP_ZLIB_NORMAL :
  113. av_clip(avctx->compression_level, 0, 9);
  114. c->flags = 0;
  115. c->imgtype = IMGTYPE_RGB24;
  116. avctx->bits_per_coded_sample= 24;
  117. avctx->extradata[0]= 4;
  118. avctx->extradata[1]= 0;
  119. avctx->extradata[2]= 0;
  120. avctx->extradata[3]= 0;
  121. avctx->extradata[4]= c->imgtype;
  122. avctx->extradata[5]= c->compression;
  123. avctx->extradata[6]= c->flags;
  124. avctx->extradata[7]= CODEC_ZLIB;
  125. c->avctx->extradata_size= 8;
  126. c->zstream.zalloc = Z_NULL;
  127. c->zstream.zfree = Z_NULL;
  128. c->zstream.opaque = Z_NULL;
  129. zret = deflateInit(&c->zstream, c->compression);
  130. if (zret != Z_OK) {
  131. av_log(avctx, AV_LOG_ERROR, "Deflate init error: %d\n", zret);
  132. return AVERROR_UNKNOWN;
  133. }
  134. return 0;
  135. }
  136. static av_cold int encode_end(AVCodecContext *avctx)
  137. {
  138. LclEncContext *c = avctx->priv_data;
  139. av_freep(&avctx->extradata);
  140. deflateEnd(&c->zstream);
  141. return 0;
  142. }
  143. AVCodec ff_zlib_encoder = {
  144. .name = "zlib",
  145. .long_name = NULL_IF_CONFIG_SMALL("LCL (LossLess Codec Library) ZLIB"),
  146. .type = AVMEDIA_TYPE_VIDEO,
  147. .id = AV_CODEC_ID_ZLIB,
  148. .priv_data_size = sizeof(LclEncContext),
  149. .init = encode_init,
  150. .encode2 = encode_frame,
  151. .close = encode_end,
  152. .capabilities = AV_CODEC_CAP_FRAME_THREADS | AV_CODEC_CAP_INTRA_ONLY,
  153. .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_BGR24, AV_PIX_FMT_NONE },
  154. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
  155. FF_CODEC_CAP_INIT_CLEANUP,
  156. };