2 * Canopus Lossless Codec decoder
4 * Copyright (c) 2012 Derek Buitenhuis
6 * This file is part of Libav.
8 * Libav is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * Libav is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with Libav; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 #include "libavutil/intreadwrite.h"
28 typedef struct CLLCContext {
30 AVCodecContext *avctx;
36 static int read_code_table(CLLCContext *ctx, GetBitContext *gb, VLC *vlc)
41 int num_lens, num_codes, num_codes_sum, prefix;
48 num_lens = get_bits(gb, 5);
50 for (i = 0; i < num_lens; i++) {
51 num_codes = get_bits(gb, 9);
52 num_codes_sum += num_codes;
54 if (num_codes_sum > 256) {
57 av_log(ctx->avctx, AV_LOG_ERROR,
58 "Too many VLCs (%d) to be read.\n", num_codes_sum);
59 return AVERROR_INVALIDDATA;
62 for (j = 0; j < num_codes; j++) {
63 symbols[count] = get_bits(gb, 8);
65 codes[count] = prefix++;
73 return ff_init_vlc_sparse(vlc, 7, count, bits, 1, 1,
74 codes, 2, 2, symbols, 1, 1, 0);
77 static int read_line(CLLCContext *ctx, GetBitContext *gb, int *top_left,
78 VLC *vlc, uint8_t *outbuf)
84 OPEN_READER(bits, gb);
89 /* Simultaneously read and restore the line */
90 for (i = 0; i < ctx->avctx->width; i++) {
91 UPDATE_CACHE(bits, gb);
92 GET_VLC(code, bits, gb, vlc->table, 7, 2);
99 CLOSE_READER(bits, gb);
101 /* Stash the first pixel */
102 *top_left = dst[-3 * ctx->avctx->width];
107 static int decode_bgr24_frame(CLLCContext *ctx, GetBitContext *gb, AVFrame *pic)
109 AVCodecContext *avctx = ctx->avctx;
124 /* Read in code table for each plane */
125 for (i = 0; i < 3; i++) {
126 ret = read_code_table(ctx, gb, &vlc[i]);
128 for (j = 0; j <= i; j++)
129 ff_free_vlc(&vlc[j]);
131 av_log(ctx->avctx, AV_LOG_ERROR,
132 "Could not read code table %d.\n", i);
137 /* Read in and restore every line */
138 for (i = 0; i < avctx->height; i++) {
139 for (j = 0; j < 3; j++)
140 read_line(ctx, gb, &pred[j], &vlc[j], &dst[j]);
142 dst += pic->linesize[0];
145 for (i = 0; i < 3; i++)
146 ff_free_vlc(&vlc[i]);
151 static int cllc_decode_frame(AVCodecContext *avctx, void *data,
152 int *got_picture_ptr, AVPacket *avpkt)
154 CLLCContext *ctx = avctx->priv_data;
155 AVFrame *pic = avctx->coded_frame;
156 uint8_t *src = avpkt->data;
157 uint8_t *swapped_buf_new;
158 uint32_t info_tag, info_offset;
160 int coding_type, ret;
163 avctx->release_buffer(avctx, pic);
167 /* Make sure our bswap16'd buffer is big enough */
168 swapped_buf_new = av_fast_realloc(ctx->swapped_buf,
169 &ctx->swapped_buf_size, avpkt->size);
170 if (!swapped_buf_new) {
171 av_log(avctx, AV_LOG_ERROR, "Could not realloc swapped buffer.\n");
172 return AVERROR(ENOMEM);
174 ctx->swapped_buf = swapped_buf_new;
176 /* Skip the INFO header if present */
178 info_tag = AV_RL32(src);
179 if (info_tag == MKTAG('I', 'N', 'F', 'O')) {
180 info_offset = AV_RL32(src + 4);
181 if (info_offset > UINT32_MAX - 8 || info_offset + 8 > avpkt->size) {
182 av_log(avctx, AV_LOG_ERROR,
183 "Invalid INFO header offset: 0x%08X is too large.\n",
185 return AVERROR_INVALIDDATA;
191 av_log(avctx, AV_LOG_DEBUG, "Skipping INFO chunk.\n");
194 /* bswap16 the buffer since CLLC's bitreader works in 16-bit words */
195 ctx->dsp.bswap16_buf((uint16_t *) ctx->swapped_buf, (uint16_t *) src,
196 (avpkt->size - info_offset) / 2);
198 init_get_bits(&gb, ctx->swapped_buf, (avpkt->size - info_offset) * 8);
201 * Read in coding type. The types are as follows:
204 * 1 - BGR24 (Triples)
208 coding_type = (AV_RL32(src) >> 8) & 0xFF;
209 av_log(avctx, AV_LOG_DEBUG, "Frame coding type: %d\n", coding_type);
211 switch (coding_type) {
213 avctx->pix_fmt = PIX_FMT_RGB24;
214 avctx->bits_per_raw_sample = 8;
216 ret = avctx->get_buffer(avctx, pic);
218 av_log(avctx, AV_LOG_ERROR, "Could not allocate buffer.\n");
222 ret = decode_bgr24_frame(ctx, &gb, pic);
228 av_log(avctx, AV_LOG_ERROR, "Unknown coding type: %d\n.", coding_type);
229 return AVERROR_INVALIDDATA;
233 pic->pict_type = AV_PICTURE_TYPE_I;
235 *got_picture_ptr = 1;
236 *(AVFrame *)data = *pic;
241 static av_cold int cllc_decode_close(AVCodecContext *avctx)
243 CLLCContext *ctx = avctx->priv_data;
245 if (avctx->coded_frame->data[0])
246 avctx->release_buffer(avctx, avctx->coded_frame);
248 av_freep(&avctx->coded_frame);
249 av_freep(&ctx->swapped_buf);
254 static av_cold int cllc_decode_init(AVCodecContext *avctx)
256 CLLCContext *ctx = avctx->priv_data;
258 /* Initialize various context values */
260 ctx->swapped_buf = NULL;
261 ctx->swapped_buf_size = 0;
263 ff_dsputil_init(&ctx->dsp, avctx);
265 avctx->coded_frame = avcodec_alloc_frame();
266 if (!avctx->coded_frame) {
267 av_log(avctx, AV_LOG_ERROR, "Could not allocate frame.\n");
268 return AVERROR(ENOMEM);
274 AVCodec ff_cllc_decoder = {
276 .type = AVMEDIA_TYPE_VIDEO,
278 .priv_data_size = sizeof(CLLCContext),
279 .init = cllc_decode_init,
280 .decode = cllc_decode_frame,
281 .close = cllc_decode_close,
282 .capabilities = CODEC_CAP_DR1,
283 .long_name = NULL_IF_CONFIG_SMALL("Canopus Lossless Codec"),