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