Måns Rullgård | 00eb27f | 2007-07-12 22:40:33 | [diff] [blame] | 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 | /** |
Diego Biurrun | ba87f08 | 2010-04-20 14:45:34 | [diff] [blame] | 23 | * @file |
Måns Rullgård | 00eb27f | 2007-07-12 22:40:33 | [diff] [blame] | 24 | * LCL (LossLess Codec Library) Video Codec |
| 25 | * Decoder for MSZH and ZLIB codecs |
| 26 | * Experimental encoder for ZLIB RGB24 |
| 27 | * |
| 28 | * Fourcc: MSZH, ZLIB |
| 29 | * |
| 30 | * Original Win32 dll: |
| 31 | * Ver2.23 By Kenji Oshima 2000.09.20 |
| 32 | * avimszh.dll, avizlib.dll |
| 33 | * |
| 34 | * A description of the decoding algorithm can be found here: |
| 35 | * http://www.pcisys.net/~melanson/codecs |
| 36 | * |
| 37 | * Supports: BGR24 (RGB 24bpp) |
Måns Rullgård | 00eb27f | 2007-07-12 22:40:33 | [diff] [blame] | 38 | */ |
| 39 | |
| 40 | #include <stdio.h> |
| 41 | #include <stdlib.h> |
| 42 | |
Michael Niedermayer | f9c823d | 2012-07-12 21:18:48 | [diff] [blame] | 43 | #include "libavutil/avassert.h" |
Måns Rullgård | 00eb27f | 2007-07-12 22:40:33 | [diff] [blame] | 44 | #include "avcodec.h" |
Andreas Rheinhardt | a688f3c | 2022-03-16 17:18:28 | [diff] [blame] | 45 | #include "codec_internal.h" |
Andreas Rheinhardt | 56e9e02 | 2021-05-11 13:17:13 | [diff] [blame] | 46 | #include "encode.h" |
Måns Rullgård | 00eb27f | 2007-07-12 22:40:33 | [diff] [blame] | 47 | #include "lcl.h" |
Andreas Rheinhardt | c3c2b54 | 2022-03-15 17:28:54 | [diff] [blame] | 48 | #include "zlib_wrapper.h" |
Martin Storsjö | 1d9c2dc | 2012-08-06 13:49:32 | [diff] [blame] | 49 | #include "libavutil/internal.h" |
| 50 | #include "libavutil/mem.h" |
Måns Rullgård | 00eb27f | 2007-07-12 22:40:33 | [diff] [blame] | 51 | |
Måns Rullgård | 00eb27f | 2007-07-12 22:40:33 | [diff] [blame] | 52 | #include <zlib.h> |
Måns Rullgård | 00eb27f | 2007-07-12 22:40:33 | [diff] [blame] | 53 | |
Måns Rullgård | 00eb27f | 2007-07-12 22:40:33 | [diff] [blame] | 54 | typedef struct LclEncContext { |
| 55 | |
Reimar Döffinger | 8f033e3 | 2009-05-31 08:38:51 | [diff] [blame] | 56 | AVCodecContext *avctx; |
Måns Rullgård | 00eb27f | 2007-07-12 22:40:33 | [diff] [blame] | 57 | |
| 58 | // Image type |
| 59 | int imgtype; |
| 60 | // Compression type |
| 61 | int compression; |
| 62 | // Flags |
| 63 | int flags; |
Andreas Rheinhardt | c3c2b54 | 2022-03-15 17:28:54 | [diff] [blame] | 64 | FFZStream zstream; |
Måns Rullgård | 00eb27f | 2007-07-12 22:40:33 | [diff] [blame] | 65 | } LclEncContext; |
| 66 | |
Anton Khirnov | 148fc99 | 2012-02-20 12:21:58 | [diff] [blame] | 67 | static int encode_frame(AVCodecContext *avctx, AVPacket *pkt, |
Paul B Mahol | 69fe25c | 2013-07-26 21:45:10 | [diff] [blame] | 68 | const AVFrame *p, int *got_packet) |
Anton Khirnov | 148fc99 | 2012-02-20 12:21:58 | [diff] [blame] | 69 | { |
Måns Rullgård | 00eb27f | 2007-07-12 22:40:33 | [diff] [blame] | 70 | LclEncContext *c = avctx->priv_data; |
Andreas Rheinhardt | c3c2b54 | 2022-03-15 17:28:54 | [diff] [blame] | 71 | z_stream *const zstream = &c->zstream.zstream; |
Anton Khirnov | 148fc99 | 2012-02-20 12:21:58 | [diff] [blame] | 72 | int i, ret; |
Måns Rullgård | 00eb27f | 2007-07-12 22:40:33 | [diff] [blame] | 73 | int zret; // Zlib return code |
Andreas Rheinhardt | c3c2b54 | 2022-03-15 17:28:54 | [diff] [blame] | 74 | int max_size = deflateBound(zstream, avctx->width * avctx->height * 3); |
Anton Khirnov | 148fc99 | 2012-02-20 12:21:58 | [diff] [blame] | 75 | |
Andreas Rheinhardt | 56e9e02 | 2021-05-11 13:17:13 | [diff] [blame] | 76 | if ((ret = ff_alloc_packet(avctx, pkt, max_size)) < 0) |
Paul B Mahol | 573018c | 2013-07-26 21:42:08 | [diff] [blame] | 77 | return ret; |
Måns Rullgård | 00eb27f | 2007-07-12 22:40:33 | [diff] [blame] | 78 | |
Anton Khirnov | 716d413 | 2012-10-06 10:10:34 | [diff] [blame] | 79 | if(avctx->pix_fmt != AV_PIX_FMT_BGR24){ |
Måns Rullgård | 00eb27f | 2007-07-12 22:40:33 | [diff] [blame] | 80 | av_log(avctx, AV_LOG_ERROR, "Format not supported!\n"); |
| 81 | return -1; |
| 82 | } |
| 83 | |
Andreas Rheinhardt | c3c2b54 | 2022-03-15 17:28:54 | [diff] [blame] | 84 | zret = deflateReset(zstream); |
Måns Rullgård | 00eb27f | 2007-07-12 22:40:33 | [diff] [blame] | 85 | if (zret != Z_OK) { |
| 86 | av_log(avctx, AV_LOG_ERROR, "Deflate reset error: %d\n", zret); |
| 87 | return -1; |
| 88 | } |
Andreas Rheinhardt | c3c2b54 | 2022-03-15 17:28:54 | [diff] [blame] | 89 | zstream->next_out = pkt->data; |
| 90 | zstream->avail_out = pkt->size; |
Måns Rullgård | 00eb27f | 2007-07-12 22:40:33 | [diff] [blame] | 91 | |
| 92 | for(i = avctx->height - 1; i >= 0; i--) { |
Andreas Rheinhardt | c3c2b54 | 2022-03-15 17:28:54 | [diff] [blame] | 93 | zstream->next_in = p->data[0] + p->linesize[0] * i; |
| 94 | zstream->avail_in = avctx->width * 3; |
| 95 | zret = deflate(zstream, Z_NO_FLUSH); |
Måns Rullgård | 00eb27f | 2007-07-12 22:40:33 | [diff] [blame] | 96 | if (zret != Z_OK) { |
| 97 | av_log(avctx, AV_LOG_ERROR, "Deflate error: %d\n", zret); |
| 98 | return -1; |
| 99 | } |
| 100 | } |
Andreas Rheinhardt | c3c2b54 | 2022-03-15 17:28:54 | [diff] [blame] | 101 | zret = deflate(zstream, Z_FINISH); |
Måns Rullgård | 00eb27f | 2007-07-12 22:40:33 | [diff] [blame] | 102 | if (zret != Z_STREAM_END) { |
| 103 | av_log(avctx, AV_LOG_ERROR, "Deflate error: %d\n", zret); |
| 104 | return -1; |
| 105 | } |
| 106 | |
Andreas Rheinhardt | c3c2b54 | 2022-03-15 17:28:54 | [diff] [blame] | 107 | pkt->size = zstream->total_out; |
Anton Khirnov | 148fc99 | 2012-02-20 12:21:58 | [diff] [blame] | 108 | *got_packet = 1; |
| 109 | |
| 110 | return 0; |
Måns Rullgård | 00eb27f | 2007-07-12 22:40:33 | [diff] [blame] | 111 | } |
| 112 | |
Zuxy Meng | 98a6fff | 2008-03-21 03:11:20 | [diff] [blame] | 113 | static av_cold int encode_init(AVCodecContext *avctx) |
Måns Rullgård | 00eb27f | 2007-07-12 22:40:33 | [diff] [blame] | 114 | { |
| 115 | LclEncContext *c = avctx->priv_data; |
Måns Rullgård | 00eb27f | 2007-07-12 22:40:33 | [diff] [blame] | 116 | |
Måns Rullgård | 00eb27f | 2007-07-12 22:40:33 | [diff] [blame] | 117 | c->avctx= avctx; |
| 118 | |
Michael Niedermayer | f9c823d | 2012-07-12 21:18:48 | [diff] [blame] | 119 | av_assert0(avctx->width && avctx->height); |
Måns Rullgård | 00eb27f | 2007-07-12 22:40:33 | [diff] [blame] | 120 | |
Michael Niedermayer | 29d147c | 2015-07-27 20:53:16 | [diff] [blame] | 121 | avctx->extradata = av_mallocz(8 + AV_INPUT_BUFFER_PADDING_SIZE); |
Paul B Mahol | 6dbcecd | 2013-07-26 21:50:28 | [diff] [blame] | 122 | if (!avctx->extradata) |
| 123 | return AVERROR(ENOMEM); |
Måns Rullgård | 00eb27f | 2007-07-12 22:40:33 | [diff] [blame] | 124 | |
Paul B Mahol | f58f600 | 2012-11-02 21:54:44 | [diff] [blame] | 125 | c->compression = avctx->compression_level == FF_COMPRESSION_DEFAULT ? |
| 126 | COMP_ZLIB_NORMAL : |
| 127 | av_clip(avctx->compression_level, 0, 9); |
Måns Rullgård | 00eb27f | 2007-07-12 22:40:33 | [diff] [blame] | 128 | c->flags = 0; |
Jason Garrett-Glaser | 32282b5 | 2010-12-31 04:56:44 | [diff] [blame] | 129 | c->imgtype = IMGTYPE_RGB24; |
| 130 | avctx->bits_per_coded_sample= 24; |
Måns Rullgård | 00eb27f | 2007-07-12 22:40:33 | [diff] [blame] | 131 | |
Reimar Döffinger | 3b85510 | 2009-05-31 08:51:30 | [diff] [blame] | 132 | avctx->extradata[0]= 4; |
| 133 | avctx->extradata[1]= 0; |
| 134 | avctx->extradata[2]= 0; |
| 135 | avctx->extradata[3]= 0; |
| 136 | avctx->extradata[4]= c->imgtype; |
| 137 | avctx->extradata[5]= c->compression; |
| 138 | avctx->extradata[6]= c->flags; |
| 139 | avctx->extradata[7]= CODEC_ZLIB; |
Måns Rullgård | 00eb27f | 2007-07-12 22:40:33 | [diff] [blame] | 140 | c->avctx->extradata_size= 8; |
| 141 | |
Andreas Rheinhardt | c3c2b54 | 2022-03-15 17:28:54 | [diff] [blame] | 142 | return ff_deflate_init(&c->zstream, c->compression, avctx); |
Måns Rullgård | 00eb27f | 2007-07-12 22:40:33 | [diff] [blame] | 143 | } |
| 144 | |
Zuxy Meng | 98a6fff | 2008-03-21 03:11:20 | [diff] [blame] | 145 | static av_cold int encode_end(AVCodecContext *avctx) |
Måns Rullgård | 00eb27f | 2007-07-12 22:40:33 | [diff] [blame] | 146 | { |
| 147 | LclEncContext *c = avctx->priv_data; |
| 148 | |
Andreas Rheinhardt | c3c2b54 | 2022-03-15 17:28:54 | [diff] [blame] | 149 | ff_deflate_end(&c->zstream); |
Måns Rullgård | 00eb27f | 2007-07-12 22:40:33 | [diff] [blame] | 150 | |
Måns Rullgård | 00eb27f | 2007-07-12 22:40:33 | [diff] [blame] | 151 | return 0; |
| 152 | } |
| 153 | |
Andreas Rheinhardt | 20f9727 | 2022-03-16 20:09:54 | [diff] [blame] | 154 | const FFCodec ff_zlib_encoder = { |
| 155 | .p.name = "zlib", |
Andreas Rheinhardt | 48286d4 | 2022-08-29 11:38:02 | [diff] [blame^] | 156 | CODEC_LONG_NAME("LCL (LossLess Codec Library) ZLIB"), |
Andreas Rheinhardt | 20f9727 | 2022-03-16 20:09:54 | [diff] [blame] | 157 | .p.type = AVMEDIA_TYPE_VIDEO, |
| 158 | .p.id = AV_CODEC_ID_ZLIB, |
Andreas Rheinhardt | a499b43 | 2021-05-11 18:52:13 | [diff] [blame] | 159 | .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS, |
Anton Khirnov | ec6402b | 2011-07-17 10:54:31 | [diff] [blame] | 160 | .priv_data_size = sizeof(LclEncContext), |
| 161 | .init = encode_init, |
Andreas Rheinhardt | 4243da4 | 2022-03-30 21:28:24 | [diff] [blame] | 162 | FF_CODEC_ENCODE_CB(encode_frame), |
Anton Khirnov | ec6402b | 2011-07-17 10:54:31 | [diff] [blame] | 163 | .close = encode_end, |
Andreas Rheinhardt | 20f9727 | 2022-03-16 20:09:54 | [diff] [blame] | 164 | .p.pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_BGR24, AV_PIX_FMT_NONE }, |
Andreas Rheinhardt | 21b23ce | 2022-07-09 22:05:45 | [diff] [blame] | 165 | .caps_internal = FF_CODEC_CAP_INIT_CLEANUP, |
Måns Rullgård | 00eb27f | 2007-07-12 22:40:33 | [diff] [blame] | 166 | }; |