annotate ffmpeg/libavcodec/lclenc.c @ 13:844d341cf643 tip

Back up before ISMIR
author Yading Song <yading.song@eecs.qmul.ac.uk>
date Thu, 31 Oct 2013 13:17:06 +0000
parents 6840f77b83aa
children
rev   line source
yading@10 1 /*
yading@10 2 * LCL (LossLess Codec Library) Codec
yading@10 3 * Copyright (c) 2002-2004 Roberto Togni
yading@10 4 *
yading@10 5 * This file is part of FFmpeg.
yading@10 6 *
yading@10 7 * FFmpeg is free software; you can redistribute it and/or
yading@10 8 * modify it under the terms of the GNU Lesser General Public
yading@10 9 * License as published by the Free Software Foundation; either
yading@10 10 * version 2.1 of the License, or (at your option) any later version.
yading@10 11 *
yading@10 12 * FFmpeg is distributed in the hope that it will be useful,
yading@10 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
yading@10 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
yading@10 15 * Lesser General Public License for more details.
yading@10 16 *
yading@10 17 * You should have received a copy of the GNU Lesser General Public
yading@10 18 * License along with FFmpeg; if not, write to the Free Software
yading@10 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
yading@10 20 */
yading@10 21
yading@10 22 /**
yading@10 23 * @file
yading@10 24 * LCL (LossLess Codec Library) Video Codec
yading@10 25 * Decoder for MSZH and ZLIB codecs
yading@10 26 * Experimental encoder for ZLIB RGB24
yading@10 27 *
yading@10 28 * Fourcc: MSZH, ZLIB
yading@10 29 *
yading@10 30 * Original Win32 dll:
yading@10 31 * Ver2.23 By Kenji Oshima 2000.09.20
yading@10 32 * avimszh.dll, avizlib.dll
yading@10 33 *
yading@10 34 * A description of the decoding algorithm can be found here:
yading@10 35 * http://www.pcisys.net/~melanson/codecs
yading@10 36 *
yading@10 37 * Supports: BGR24 (RGB 24bpp)
yading@10 38 *
yading@10 39 */
yading@10 40
yading@10 41 #include <stdio.h>
yading@10 42 #include <stdlib.h>
yading@10 43
yading@10 44 #include "libavutil/avassert.h"
yading@10 45 #include "avcodec.h"
yading@10 46 #include "internal.h"
yading@10 47 #include "lcl.h"
yading@10 48 #include "libavutil/internal.h"
yading@10 49 #include "libavutil/mem.h"
yading@10 50
yading@10 51 #include <zlib.h>
yading@10 52
yading@10 53 /*
yading@10 54 * Decoder context
yading@10 55 */
yading@10 56 typedef struct LclEncContext {
yading@10 57
yading@10 58 AVCodecContext *avctx;
yading@10 59 AVFrame pic;
yading@10 60
yading@10 61 // Image type
yading@10 62 int imgtype;
yading@10 63 // Compression type
yading@10 64 int compression;
yading@10 65 // Flags
yading@10 66 int flags;
yading@10 67 z_stream zstream;
yading@10 68 } LclEncContext;
yading@10 69
yading@10 70 /*
yading@10 71 *
yading@10 72 * Encode a frame
yading@10 73 *
yading@10 74 */
yading@10 75 static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
yading@10 76 const AVFrame *pict, int *got_packet)
yading@10 77 {
yading@10 78 LclEncContext *c = avctx->priv_data;
yading@10 79 AVFrame * const p = &c->pic;
yading@10 80 int i, ret;
yading@10 81 int zret; // Zlib return code
yading@10 82 int max_size = deflateBound(&c->zstream, avctx->width * avctx->height * 3);
yading@10 83
yading@10 84 if ((ret = ff_alloc_packet2(avctx, pkt, max_size)) < 0)
yading@10 85 return ret;
yading@10 86
yading@10 87 *p = *pict;
yading@10 88 p->pict_type= AV_PICTURE_TYPE_I;
yading@10 89 p->key_frame= 1;
yading@10 90
yading@10 91 if(avctx->pix_fmt != AV_PIX_FMT_BGR24){
yading@10 92 av_log(avctx, AV_LOG_ERROR, "Format not supported!\n");
yading@10 93 return -1;
yading@10 94 }
yading@10 95
yading@10 96 zret = deflateReset(&c->zstream);
yading@10 97 if (zret != Z_OK) {
yading@10 98 av_log(avctx, AV_LOG_ERROR, "Deflate reset error: %d\n", zret);
yading@10 99 return -1;
yading@10 100 }
yading@10 101 c->zstream.next_out = pkt->data;
yading@10 102 c->zstream.avail_out = pkt->size;
yading@10 103
yading@10 104 for(i = avctx->height - 1; i >= 0; i--) {
yading@10 105 c->zstream.next_in = p->data[0]+p->linesize[0]*i;
yading@10 106 c->zstream.avail_in = avctx->width*3;
yading@10 107 zret = deflate(&c->zstream, Z_NO_FLUSH);
yading@10 108 if (zret != Z_OK) {
yading@10 109 av_log(avctx, AV_LOG_ERROR, "Deflate error: %d\n", zret);
yading@10 110 return -1;
yading@10 111 }
yading@10 112 }
yading@10 113 zret = deflate(&c->zstream, Z_FINISH);
yading@10 114 if (zret != Z_STREAM_END) {
yading@10 115 av_log(avctx, AV_LOG_ERROR, "Deflate error: %d\n", zret);
yading@10 116 return -1;
yading@10 117 }
yading@10 118
yading@10 119 pkt->size = c->zstream.total_out;
yading@10 120 pkt->flags |= AV_PKT_FLAG_KEY;
yading@10 121 *got_packet = 1;
yading@10 122
yading@10 123 return 0;
yading@10 124 }
yading@10 125
yading@10 126 /*
yading@10 127 *
yading@10 128 * Init lcl encoder
yading@10 129 *
yading@10 130 */
yading@10 131 static av_cold int encode_init(AVCodecContext *avctx)
yading@10 132 {
yading@10 133 LclEncContext *c = avctx->priv_data;
yading@10 134 int zret; // Zlib return code
yading@10 135
yading@10 136 c->avctx= avctx;
yading@10 137
yading@10 138 av_assert0(avctx->width && avctx->height);
yading@10 139
yading@10 140 avctx->extradata= av_mallocz(8);
yading@10 141 avctx->coded_frame= &c->pic;
yading@10 142
yading@10 143 c->compression = avctx->compression_level == FF_COMPRESSION_DEFAULT ?
yading@10 144 COMP_ZLIB_NORMAL :
yading@10 145 av_clip(avctx->compression_level, 0, 9);
yading@10 146 c->flags = 0;
yading@10 147 c->imgtype = IMGTYPE_RGB24;
yading@10 148 avctx->bits_per_coded_sample= 24;
yading@10 149
yading@10 150 avctx->extradata[0]= 4;
yading@10 151 avctx->extradata[1]= 0;
yading@10 152 avctx->extradata[2]= 0;
yading@10 153 avctx->extradata[3]= 0;
yading@10 154 avctx->extradata[4]= c->imgtype;
yading@10 155 avctx->extradata[5]= c->compression;
yading@10 156 avctx->extradata[6]= c->flags;
yading@10 157 avctx->extradata[7]= CODEC_ZLIB;
yading@10 158 c->avctx->extradata_size= 8;
yading@10 159
yading@10 160 c->zstream.zalloc = Z_NULL;
yading@10 161 c->zstream.zfree = Z_NULL;
yading@10 162 c->zstream.opaque = Z_NULL;
yading@10 163 zret = deflateInit(&c->zstream, c->compression);
yading@10 164 if (zret != Z_OK) {
yading@10 165 av_log(avctx, AV_LOG_ERROR, "Deflate init error: %d\n", zret);
yading@10 166 return 1;
yading@10 167 }
yading@10 168
yading@10 169 return 0;
yading@10 170 }
yading@10 171
yading@10 172 /*
yading@10 173 *
yading@10 174 * Uninit lcl encoder
yading@10 175 *
yading@10 176 */
yading@10 177 static av_cold int encode_end(AVCodecContext *avctx)
yading@10 178 {
yading@10 179 LclEncContext *c = avctx->priv_data;
yading@10 180
yading@10 181 av_freep(&avctx->extradata);
yading@10 182 deflateEnd(&c->zstream);
yading@10 183
yading@10 184 return 0;
yading@10 185 }
yading@10 186
yading@10 187 AVCodec ff_zlib_encoder = {
yading@10 188 .name = "zlib",
yading@10 189 .type = AVMEDIA_TYPE_VIDEO,
yading@10 190 .id = AV_CODEC_ID_ZLIB,
yading@10 191 .priv_data_size = sizeof(LclEncContext),
yading@10 192 .init = encode_init,
yading@10 193 .encode2 = encode_frame,
yading@10 194 .close = encode_end,
yading@10 195 .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_BGR24, AV_PIX_FMT_NONE },
yading@10 196 .long_name = NULL_IF_CONFIG_SMALL("LCL (LossLess Codec Library) ZLIB"),
yading@10 197 };