blob: 4c902d5f1763581ec52bb6bb8c1c486fedf341b3 [file] [log] [blame]
Måns Rullgård00eb27f2007-07-12 22:40:331/*
2 * LCL (LossLess Codec Library) Codec
3 * Copyright (c) 2002-2004 Roberto Togni
4 *
Mans Rullgard2912e872011-03-18 17:35:105 * This file is part of Libav.
Måns Rullgård00eb27f2007-07-12 22:40:336 *
Mans Rullgard2912e872011-03-18 17:35:107 * Libav is free software; you can redistribute it and/or
Måns Rullgård00eb27f2007-07-12 22:40:338 * 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 *
Mans Rullgard2912e872011-03-18 17:35:1012 * Libav is distributed in the hope that it will be useful,
Måns Rullgård00eb27f2007-07-12 22:40:3313 * 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
Mans Rullgard2912e872011-03-18 17:35:1018 * License along with Libav; if not, write to the Free Software
Måns Rullgård00eb27f2007-07-12 22:40:3319 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22/**
Diego Biurrunba87f082010-04-20 14:45:3423 * @file
Måns Rullgård00eb27f2007-07-12 22:40:3324 * 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
41#include <stdio.h>
42#include <stdlib.h>
43
44#include "avcodec.h"
Måns Rullgård00eb27f2007-07-12 22:40:3345#include "lcl.h"
46
Måns Rullgård00eb27f2007-07-12 22:40:3347#include <zlib.h>
Måns Rullgård00eb27f2007-07-12 22:40:3348
49/*
50 * Decoder context
51 */
52typedef struct LclEncContext {
53
Reimar Döffinger8f033e32009-05-31 08:38:5154 AVCodecContext *avctx;
55 AVFrame pic;
Måns Rullgård00eb27f2007-07-12 22:40:3356
57 // Image type
58 int imgtype;
59 // Compression type
60 int compression;
61 // Flags
62 int flags;
Måns Rullgård00eb27f2007-07-12 22:40:3363 z_stream zstream;
Måns Rullgård00eb27f2007-07-12 22:40:3364} LclEncContext;
65
66/*
67 *
68 * Encode a frame
69 *
70 */
71static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
72 LclEncContext *c = avctx->priv_data;
73 AVFrame *pict = data;
74 AVFrame * const p = &c->pic;
75 int i;
76 int zret; // Zlib return code
77
Måns Rullgård00eb27f2007-07-12 22:40:3378 *p = *pict;
Stefano Sabatini975a1442011-04-29 16:53:5779 p->pict_type= AV_PICTURE_TYPE_I;
Måns Rullgård00eb27f2007-07-12 22:40:3380 p->key_frame= 1;
81
82 if(avctx->pix_fmt != PIX_FMT_BGR24){
83 av_log(avctx, AV_LOG_ERROR, "Format not supported!\n");
84 return -1;
85 }
86
Reimar Döffinger32b3ab92009-05-31 08:41:1187 zret = deflateReset(&c->zstream);
Måns Rullgård00eb27f2007-07-12 22:40:3388 if (zret != Z_OK) {
89 av_log(avctx, AV_LOG_ERROR, "Deflate reset error: %d\n", zret);
90 return -1;
91 }
Reimar Döffingere786d3c2009-05-31 08:49:2792 c->zstream.next_out = buf;
93 c->zstream.avail_out = buf_size;
Måns Rullgård00eb27f2007-07-12 22:40:3394
95 for(i = avctx->height - 1; i >= 0; i--) {
96 c->zstream.next_in = p->data[0]+p->linesize[0]*i;
97 c->zstream.avail_in = avctx->width*3;
Reimar Döffinger32b3ab92009-05-31 08:41:1198 zret = deflate(&c->zstream, Z_NO_FLUSH);
Måns Rullgård00eb27f2007-07-12 22:40:3399 if (zret != Z_OK) {
100 av_log(avctx, AV_LOG_ERROR, "Deflate error: %d\n", zret);
101 return -1;
102 }
103 }
Reimar Döffinger32b3ab92009-05-31 08:41:11104 zret = deflate(&c->zstream, Z_FINISH);
Måns Rullgård00eb27f2007-07-12 22:40:33105 if (zret != Z_STREAM_END) {
106 av_log(avctx, AV_LOG_ERROR, "Deflate error: %d\n", zret);
107 return -1;
108 }
109
Måns Rullgård00eb27f2007-07-12 22:40:33110 return c->zstream.total_out;
Måns Rullgård00eb27f2007-07-12 22:40:33111}
112
113/*
114 *
115 * Init lcl encoder
116 *
117 */
Zuxy Meng98a6fff2008-03-21 03:11:20118static av_cold int encode_init(AVCodecContext *avctx)
Måns Rullgård00eb27f2007-07-12 22:40:33119{
120 LclEncContext *c = avctx->priv_data;
121 int zret; // Zlib return code
122
Måns Rullgård00eb27f2007-07-12 22:40:33123 c->avctx= avctx;
124
125 assert(avctx->width && avctx->height);
126
127 avctx->extradata= av_mallocz(8);
128 avctx->coded_frame= &c->pic;
129
130 // Will be user settable someday
131 c->compression = 6;
132 c->flags = 0;
Jason Garrett-Glaser32282b52010-12-31 04:56:44133 c->imgtype = IMGTYPE_RGB24;
134 avctx->bits_per_coded_sample= 24;
Måns Rullgård00eb27f2007-07-12 22:40:33135
Reimar Döffinger3b855102009-05-31 08:51:30136 avctx->extradata[0]= 4;
137 avctx->extradata[1]= 0;
138 avctx->extradata[2]= 0;
139 avctx->extradata[3]= 0;
140 avctx->extradata[4]= c->imgtype;
141 avctx->extradata[5]= c->compression;
142 avctx->extradata[6]= c->flags;
143 avctx->extradata[7]= CODEC_ZLIB;
Måns Rullgård00eb27f2007-07-12 22:40:33144 c->avctx->extradata_size= 8;
145
146 c->zstream.zalloc = Z_NULL;
147 c->zstream.zfree = Z_NULL;
148 c->zstream.opaque = Z_NULL;
Reimar Döffinger32b3ab92009-05-31 08:41:11149 zret = deflateInit(&c->zstream, c->compression);
Måns Rullgård00eb27f2007-07-12 22:40:33150 if (zret != Z_OK) {
151 av_log(avctx, AV_LOG_ERROR, "Deflate init error: %d\n", zret);
152 return 1;
153 }
154
Måns Rullgård00eb27f2007-07-12 22:40:33155 return 0;
Måns Rullgård00eb27f2007-07-12 22:40:33156}
157
158/*
159 *
160 * Uninit lcl encoder
161 *
162 */
Zuxy Meng98a6fff2008-03-21 03:11:20163static av_cold int encode_end(AVCodecContext *avctx)
Måns Rullgård00eb27f2007-07-12 22:40:33164{
165 LclEncContext *c = avctx->priv_data;
166
167 av_freep(&avctx->extradata);
Reimar Döffinger32b3ab92009-05-31 08:41:11168 deflateEnd(&c->zstream);
Måns Rullgård00eb27f2007-07-12 22:40:33169
170 return 0;
171}
172
Diego Elio Pettenòd36beb32011-01-25 21:40:11173AVCodec ff_zlib_encoder = {
Anton Khirnovec6402b2011-07-17 10:54:31174 .name = "zlib",
175 .type = AVMEDIA_TYPE_VIDEO,
176 .id = CODEC_ID_ZLIB,
177 .priv_data_size = sizeof(LclEncContext),
178 .init = encode_init,
179 .encode = encode_frame,
180 .close = encode_end,
Jason Garrett-Glaser32282b52010-12-31 04:56:44181 .pix_fmts = (const enum PixelFormat[]) { PIX_FMT_BGR24, PIX_FMT_NONE },
Stefano Sabatinife4bf372008-06-12 21:50:13182 .long_name = NULL_IF_CONFIG_SMALL("LCL (LossLess Codec Library) ZLIB"),
Måns Rullgård00eb27f2007-07-12 22:40:33183};