2 * Canopus Lossless Codec decoder
4 * Copyright (c) 2012-2013 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"
29 typedef struct CLLCContext {
31 AVCodecContext *avctx;
37 static int read_code_table(CLLCContext *ctx, GetBitContext *gb, VLC *vlc)
42 int num_lens, num_codes, num_codes_sum, prefix;
49 num_lens = get_bits(gb, 5);
51 for (i = 0; i < num_lens; i++) {
52 num_codes = get_bits(gb, 9);
53 num_codes_sum += num_codes;
55 if (num_codes_sum > 256) {
58 av_log(ctx->avctx, AV_LOG_ERROR,
59 "Too many VLCs (%d) to be read.\n", num_codes_sum);
60 return AVERROR_INVALIDDATA;
63 for (j = 0; j < num_codes; j++) {
64 symbols[count] = get_bits(gb, 8);
66 codes[count] = prefix++;
74 return ff_init_vlc_sparse(vlc, 7, count, bits, 1, 1,
75 codes, 2, 2, symbols, 1, 1, 0);
79 * Unlike the RGB24 read/restore, which reads in a component at a time,
80 * ARGB read/restore reads in ARGB quads.
82 static int read_argb_line(CLLCContext *ctx, GetBitContext *gb, int *top_left,
83 VLC *vlc, uint8_t *outbuf)
90 OPEN_READER(bits, gb);
93 pred[0] = top_left[0];
94 pred[1] = top_left[1];
95 pred[2] = top_left[2];
96 pred[3] = top_left[3];
98 for (i = 0; i < ctx->avctx->width; i++) {
99 /* Always get the alpha component */
100 UPDATE_CACHE(bits, gb);
101 GET_VLC(code, bits, gb, vlc[0].table, 7, 2);
106 /* Skip the components if they are entirely transparent */
109 UPDATE_CACHE(bits, gb);
110 GET_VLC(code, bits, gb, vlc[1].table, 7, 2);
116 UPDATE_CACHE(bits, gb);
117 GET_VLC(code, bits, gb, vlc[2].table, 7, 2);
123 UPDATE_CACHE(bits, gb);
124 GET_VLC(code, bits, gb, vlc[3].table, 7, 2);
137 CLOSE_READER(bits, gb);
139 top_left[0] = outbuf[0];
141 /* Only stash components if they are not transparent */
143 top_left[1] = outbuf[1];
144 top_left[2] = outbuf[2];
145 top_left[3] = outbuf[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 = outbuf[0];
181 static int read_yuv_component_line(CLLCContext *ctx, GetBitContext *gb,
182 int *top_left, VLC *vlc, uint8_t *outbuf,
188 OPEN_READER(bits, gb);
192 /* Simultaneously read and restore the line */
193 for (i = 0; i < ctx->avctx->width >> is_chroma; i++) {
194 UPDATE_CACHE(bits, gb);
195 GET_VLC(code, bits, gb, vlc->table, 7, 2);
201 CLOSE_READER(bits, gb);
203 /* Stash the first pixel */
204 *top_left = outbuf[0];
209 static int decode_argb_frame(CLLCContext *ctx, GetBitContext *gb, AVFrame *pic)
211 AVCodecContext *avctx = ctx->avctx;
227 /* Read in code table for each plane */
228 for (i = 0; i < 4; i++) {
229 ret = read_code_table(ctx, gb, &vlc[i]);
231 for (j = 0; j <= i; j++)
232 ff_free_vlc(&vlc[j]);
234 av_log(ctx->avctx, AV_LOG_ERROR,
235 "Could not read code table %d.\n", i);
240 /* Read in and restore every line */
241 for (i = 0; i < avctx->height; i++) {
242 read_argb_line(ctx, gb, pred, vlc, dst);
244 dst += pic->linesize[0];
247 for (i = 0; i < 4; i++)
248 ff_free_vlc(&vlc[i]);
253 static int decode_rgb24_frame(CLLCContext *ctx, GetBitContext *gb, AVFrame *pic)
255 AVCodecContext *avctx = ctx->avctx;
270 /* Read in code table for each plane */
271 for (i = 0; i < 3; i++) {
272 ret = read_code_table(ctx, gb, &vlc[i]);
274 for (j = 0; j <= i; j++)
275 ff_free_vlc(&vlc[j]);
277 av_log(ctx->avctx, AV_LOG_ERROR,
278 "Could not read code table %d.\n", i);
283 /* Read in and restore every line */
284 for (i = 0; i < avctx->height; i++) {
285 for (j = 0; j < 3; j++)
286 read_rgb24_component_line(ctx, gb, &pred[j], &vlc[j], &dst[j]);
288 dst += pic->linesize[0];
291 for (i = 0; i < 3; i++)
292 ff_free_vlc(&vlc[i]);
297 static int decode_yuv_frame(CLLCContext *ctx, GetBitContext *gb, AVFrame *pic)
299 AVCodecContext *avctx = ctx->avctx;
311 dst[0] = pic->data[0];
312 dst[1] = pic->data[1];
313 dst[2] = pic->data[2];
317 block = get_bits(gb, 8);
319 avpriv_request_sample(ctx->avctx, "Blocked YUV");
320 return AVERROR_PATCHWELCOME;
323 /* Read in code table for luma and chroma */
324 for (i = 0; i < 2; i++) {
325 ret = read_code_table(ctx, gb, &vlc[i]);
327 for (j = 0; j <= i; j++)
328 ff_free_vlc(&vlc[j]);
330 av_log(ctx->avctx, AV_LOG_ERROR,
331 "Could not read code table %d.\n", i);
336 /* Read in and restore every line */
337 for (i = 0; i < avctx->height; i++) {
338 read_yuv_component_line(ctx, gb, &pred[0], &vlc[0], dst[0], 0); /* Y */
339 read_yuv_component_line(ctx, gb, &pred[1], &vlc[1], dst[1], 1); /* U */
340 read_yuv_component_line(ctx, gb, &pred[2], &vlc[1], dst[2], 1); /* V */
342 for (j = 0; j < 3; j++)
343 dst[j] += pic->linesize[j];
346 for (i = 0; i < 2; i++)
347 ff_free_vlc(&vlc[i]);
352 static int cllc_decode_frame(AVCodecContext *avctx, void *data,
353 int *got_picture_ptr, AVPacket *avpkt)
355 CLLCContext *ctx = avctx->priv_data;
357 uint8_t *src = avpkt->data;
358 uint32_t info_tag, info_offset;
361 int coding_type, ret;
363 /* Skip the INFO header if present */
365 info_tag = AV_RL32(src);
366 if (info_tag == MKTAG('I', 'N', 'F', 'O')) {
367 info_offset = AV_RL32(src + 4);
368 if (info_offset > UINT32_MAX - 8 || info_offset + 8 > avpkt->size) {
369 av_log(avctx, AV_LOG_ERROR,
370 "Invalid INFO header offset: 0x%08X is too large.\n",
372 return AVERROR_INVALIDDATA;
378 av_log(avctx, AV_LOG_DEBUG, "Skipping INFO chunk.\n");
381 data_size = (avpkt->size - info_offset) & ~1;
383 /* Make sure our bswap16'd buffer is big enough */
384 av_fast_padded_malloc(&ctx->swapped_buf,
385 &ctx->swapped_buf_size, data_size);
386 if (!ctx->swapped_buf) {
387 av_log(avctx, AV_LOG_ERROR, "Could not allocate swapped buffer.\n");
388 return AVERROR(ENOMEM);
391 /* bswap16 the buffer since CLLC's bitreader works in 16-bit words */
392 ctx->dsp.bswap16_buf((uint16_t *) ctx->swapped_buf, (uint16_t *) src,
395 init_get_bits(&gb, ctx->swapped_buf, data_size * 8);
398 * Read in coding type. The types are as follows:
401 * 1 - BGR24 (Triples)
405 coding_type = (AV_RL32(src) >> 8) & 0xFF;
406 av_log(avctx, AV_LOG_DEBUG, "Frame coding type: %d\n", coding_type);
408 switch (coding_type) {
410 avctx->pix_fmt = AV_PIX_FMT_YUV422P;
411 avctx->bits_per_raw_sample = 8;
413 if ((ret = ff_get_buffer(avctx, pic, 0)) < 0)
416 ret = decode_yuv_frame(ctx, &gb, pic);
423 avctx->pix_fmt = AV_PIX_FMT_RGB24;
424 avctx->bits_per_raw_sample = 8;
426 if ((ret = ff_get_buffer(avctx, pic, 0)) < 0)
429 ret = decode_rgb24_frame(ctx, &gb, pic);
435 avctx->pix_fmt = AV_PIX_FMT_ARGB;
436 avctx->bits_per_raw_sample = 8;
438 if ((ret = ff_get_buffer(avctx, pic, 0)) < 0)
441 ret = decode_argb_frame(ctx, &gb, pic);
447 av_log(avctx, AV_LOG_ERROR, "Unknown coding type: %d.\n", coding_type);
448 return AVERROR_INVALIDDATA;
452 pic->pict_type = AV_PICTURE_TYPE_I;
454 *got_picture_ptr = 1;
459 static av_cold int cllc_decode_close(AVCodecContext *avctx)
461 CLLCContext *ctx = avctx->priv_data;
463 av_freep(&ctx->swapped_buf);
468 static av_cold int cllc_decode_init(AVCodecContext *avctx)
470 CLLCContext *ctx = avctx->priv_data;
472 /* Initialize various context values */
474 ctx->swapped_buf = NULL;
475 ctx->swapped_buf_size = 0;
477 ff_dsputil_init(&ctx->dsp, avctx);
482 AVCodec ff_cllc_decoder = {
484 .long_name = NULL_IF_CONFIG_SMALL("Canopus Lossless Codec"),
485 .type = AVMEDIA_TYPE_VIDEO,
486 .id = AV_CODEC_ID_CLLC,
487 .priv_data_size = sizeof(CLLCContext),
488 .init = cllc_decode_init,
489 .decode = cllc_decode_frame,
490 .close = cllc_decode_close,
491 .capabilities = CODEC_CAP_DR1,