2 * Canopus Lossless Codec decoder
4 * Copyright (c) 2012 Derek Buitenhuis
6 * This file is part of FFmpeg.
8 * FFmpeg 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 * FFmpeg 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 FFmpeg; 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);
78 * Unlike the RGB24 read/restore, which reads in a component at a time,
79 * ARGB read/restore reads in ARGB quads.
81 static int read_argb_line(CLLCContext *ctx, GetBitContext *gb, int *top_left,
82 VLC *vlc, uint8_t *outbuf)
89 OPEN_READER(bits, gb);
92 pred[0] = top_left[0];
93 pred[1] = top_left[1];
94 pred[2] = top_left[2];
95 pred[3] = top_left[3];
97 for (i = 0; i < ctx->avctx->width; i++) {
98 /* Always get the alpha component */
99 UPDATE_CACHE(bits, gb);
100 GET_VLC(code, bits, gb, vlc[0].table, 7, 2);
105 /* Skip the components if they are entirely transparent */
108 UPDATE_CACHE(bits, gb);
109 GET_VLC(code, bits, gb, vlc[1].table, 7, 2);
115 UPDATE_CACHE(bits, gb);
116 GET_VLC(code, bits, gb, vlc[2].table, 7, 2);
122 UPDATE_CACHE(bits, gb);
123 GET_VLC(code, bits, gb, vlc[3].table, 7, 2);
136 CLOSE_READER(bits, gb);
138 dst -= 4 * ctx->avctx->width;
139 top_left[0] = dst[0];
141 /* Only stash components if they are not transparent */
143 top_left[1] = dst[1];
144 top_left[2] = dst[2];
145 top_left[3] = dst[3];
151 static int read_rgb24_component_line(CLLCContext *ctx, GetBitContext *gb,
152 int *top_left, VLC *vlc, uint8_t *outbuf)
158 OPEN_READER(bits, gb);
163 /* Simultaneously read and restore the line */
164 for (i = 0; i < ctx->avctx->width; i++) {
165 UPDATE_CACHE(bits, gb);
166 GET_VLC(code, bits, gb, vlc->table, 7, 2);
173 CLOSE_READER(bits, gb);
175 /* Stash the first pixel */
176 *top_left = dst[-3 * ctx->avctx->width];
181 static int decode_argb_frame(CLLCContext *ctx, GetBitContext *gb, AVFrame *pic)
183 AVCodecContext *avctx = ctx->avctx;
199 /* Read in code table for each plane */
200 for (i = 0; i < 4; i++) {
201 ret = read_code_table(ctx, gb, &vlc[i]);
203 for (j = 0; j <= i; j++)
204 ff_free_vlc(&vlc[j]);
206 av_log(ctx->avctx, AV_LOG_ERROR,
207 "Could not read code table %d.\n", i);
212 /* Read in and restore every line */
213 for (i = 0; i < avctx->height; i++) {
214 read_argb_line(ctx, gb, pred, vlc, dst);
216 dst += pic->linesize[0];
219 for (i = 0; i < 4; i++)
220 ff_free_vlc(&vlc[i]);
225 static int decode_rgb24_frame(CLLCContext *ctx, GetBitContext *gb, AVFrame *pic)
227 AVCodecContext *avctx = ctx->avctx;
242 /* Read in code table for each plane */
243 for (i = 0; i < 3; i++) {
244 ret = read_code_table(ctx, gb, &vlc[i]);
246 for (j = 0; j <= i; j++)
247 ff_free_vlc(&vlc[j]);
249 av_log(ctx->avctx, AV_LOG_ERROR,
250 "Could not read code table %d.\n", i);
255 /* Read in and restore every line */
256 for (i = 0; i < avctx->height; i++) {
257 for (j = 0; j < 3; j++)
258 read_rgb24_component_line(ctx, gb, &pred[j], &vlc[j], &dst[j]);
260 dst += pic->linesize[0];
263 for (i = 0; i < 3; i++)
264 ff_free_vlc(&vlc[i]);
269 static int cllc_decode_frame(AVCodecContext *avctx, void *data,
270 int *got_picture_ptr, AVPacket *avpkt)
272 CLLCContext *ctx = avctx->priv_data;
273 AVFrame *pic = avctx->coded_frame;
274 uint8_t *src = avpkt->data;
275 uint32_t info_tag, info_offset;
278 int coding_type, ret;
281 avctx->release_buffer(avctx, pic);
285 /* Skip the INFO header if present */
287 info_tag = AV_RL32(src);
288 if (info_tag == MKTAG('I', 'N', 'F', 'O')) {
289 info_offset = AV_RL32(src + 4);
290 if (info_offset > UINT32_MAX - 8 || info_offset + 8 > avpkt->size) {
291 av_log(avctx, AV_LOG_ERROR,
292 "Invalid INFO header offset: 0x%08X is too large.\n",
294 return AVERROR_INVALIDDATA;
300 av_log(avctx, AV_LOG_DEBUG, "Skipping INFO chunk.\n");
303 data_size = (avpkt->size - info_offset) & ~1;
305 /* Make sure our bswap16'd buffer is big enough */
306 av_fast_padded_malloc(&ctx->swapped_buf,
307 &ctx->swapped_buf_size, data_size);
308 if (!ctx->swapped_buf) {
309 av_log(avctx, AV_LOG_ERROR, "Could not allocate swapped buffer.\n");
310 return AVERROR(ENOMEM);
313 /* bswap16 the buffer since CLLC's bitreader works in 16-bit words */
314 ctx->dsp.bswap16_buf((uint16_t *) ctx->swapped_buf, (uint16_t *) src,
317 init_get_bits(&gb, ctx->swapped_buf, data_size * 8);
320 * Read in coding type. The types are as follows:
323 * 1 - BGR24 (Triples)
327 coding_type = (AV_RL32(src) >> 8) & 0xFF;
328 av_log(avctx, AV_LOG_DEBUG, "Frame coding type: %d\n", coding_type);
330 switch (coding_type) {
333 avctx->pix_fmt = AV_PIX_FMT_RGB24;
334 avctx->bits_per_raw_sample = 8;
336 ret = avctx->get_buffer(avctx, pic);
338 av_log(avctx, AV_LOG_ERROR, "Could not allocate buffer.\n");
342 ret = decode_rgb24_frame(ctx, &gb, pic);
348 avctx->pix_fmt = AV_PIX_FMT_ARGB;
349 avctx->bits_per_raw_sample = 8;
351 ret = avctx->get_buffer(avctx, pic);
353 av_log(avctx, AV_LOG_ERROR, "Could not allocate buffer.\n");
357 ret = decode_argb_frame(ctx, &gb, pic);
363 av_log(avctx, AV_LOG_ERROR, "Unknown coding type: %d.\n", coding_type);
364 return AVERROR_INVALIDDATA;
368 pic->pict_type = AV_PICTURE_TYPE_I;
370 *got_picture_ptr = 1;
371 *(AVFrame *)data = *pic;
376 static av_cold int cllc_decode_close(AVCodecContext *avctx)
378 CLLCContext *ctx = avctx->priv_data;
380 if (avctx->coded_frame->data[0])
381 avctx->release_buffer(avctx, avctx->coded_frame);
383 av_freep(&avctx->coded_frame);
384 av_freep(&ctx->swapped_buf);
389 static av_cold int cllc_decode_init(AVCodecContext *avctx)
391 CLLCContext *ctx = avctx->priv_data;
393 /* Initialize various context values */
395 ctx->swapped_buf = NULL;
396 ctx->swapped_buf_size = 0;
398 ff_dsputil_init(&ctx->dsp, avctx);
400 avctx->coded_frame = avcodec_alloc_frame();
401 if (!avctx->coded_frame) {
402 av_log(avctx, AV_LOG_ERROR, "Could not allocate frame.\n");
403 return AVERROR(ENOMEM);
409 AVCodec ff_cllc_decoder = {
411 .type = AVMEDIA_TYPE_VIDEO,
412 .id = AV_CODEC_ID_CLLC,
413 .priv_data_size = sizeof(CLLCContext),
414 .init = cllc_decode_init,
415 .decode = cllc_decode_frame,
416 .close = cllc_decode_close,
417 .capabilities = CODEC_CAP_DR1,
418 .long_name = NULL_IF_CONFIG_SMALL("Canopus Lossless Codec"),