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
25 #include "libavutil/intreadwrite.h"
31 typedef struct CLLCContext {
32 AVCodecContext *avctx;
39 static int read_code_table(CLLCContext *ctx, GetBitContext *gb, VLC *vlc)
44 int num_lens, num_codes, num_codes_sum, prefix;
51 num_lens = get_bits(gb, 5);
53 for (i = 0; i < num_lens; i++) {
54 num_codes = get_bits(gb, 9);
55 num_codes_sum += num_codes;
57 if (num_codes_sum > 256) {
60 av_log(ctx->avctx, AV_LOG_ERROR,
61 "Too many VLCs (%d) to be read.\n", num_codes_sum);
62 return AVERROR_INVALIDDATA;
65 for (j = 0; j < num_codes; j++) {
66 symbols[count] = get_bits(gb, 8);
68 codes[count] = prefix++;
76 return ff_init_vlc_sparse(vlc, 7, count, bits, 1, 1,
77 codes, 2, 2, symbols, 1, 1, 0);
81 * Unlike the RGB24 read/restore, which reads in a component at a time,
82 * ARGB read/restore reads in ARGB quads.
84 static int read_argb_line(CLLCContext *ctx, GetBitContext *gb, int *top_left,
85 VLC *vlc, uint8_t *outbuf)
92 OPEN_READER(bits, gb);
95 pred[0] = top_left[0];
96 pred[1] = top_left[1];
97 pred[2] = top_left[2];
98 pred[3] = top_left[3];
100 for (i = 0; i < ctx->avctx->width; i++) {
101 /* Always get the alpha component */
102 UPDATE_CACHE(bits, gb);
103 GET_VLC(code, bits, gb, vlc[0].table, 7, 2);
108 /* Skip the components if they are entirely transparent */
111 UPDATE_CACHE(bits, gb);
112 GET_VLC(code, bits, gb, vlc[1].table, 7, 2);
118 UPDATE_CACHE(bits, gb);
119 GET_VLC(code, bits, gb, vlc[2].table, 7, 2);
125 UPDATE_CACHE(bits, gb);
126 GET_VLC(code, bits, gb, vlc[3].table, 7, 2);
139 CLOSE_READER(bits, gb);
141 top_left[0] = outbuf[0];
143 /* Only stash components if they are not transparent */
145 top_left[1] = outbuf[1];
146 top_left[2] = outbuf[2];
147 top_left[3] = outbuf[3];
153 static int read_rgb24_component_line(CLLCContext *ctx, GetBitContext *gb,
154 int *top_left, VLC *vlc, uint8_t *outbuf)
160 OPEN_READER(bits, gb);
165 /* Simultaneously read and restore the line */
166 for (i = 0; i < ctx->avctx->width; i++) {
167 UPDATE_CACHE(bits, gb);
168 GET_VLC(code, bits, gb, vlc->table, 7, 2);
175 CLOSE_READER(bits, gb);
177 /* Stash the first pixel */
178 *top_left = outbuf[0];
183 static int read_yuv_component_line(CLLCContext *ctx, GetBitContext *gb,
184 int *top_left, VLC *vlc, uint8_t *outbuf,
190 OPEN_READER(bits, gb);
194 /* Simultaneously read and restore the line */
195 for (i = 0; i < ctx->avctx->width >> is_chroma; i++) {
196 UPDATE_CACHE(bits, gb);
197 GET_VLC(code, bits, gb, vlc->table, 7, 2);
203 CLOSE_READER(bits, gb);
205 /* Stash the first pixel */
206 *top_left = outbuf[0];
211 static int decode_argb_frame(CLLCContext *ctx, GetBitContext *gb, AVFrame *pic)
213 AVCodecContext *avctx = ctx->avctx;
229 /* Read in code table for each plane */
230 for (i = 0; i < 4; i++) {
231 ret = read_code_table(ctx, gb, &vlc[i]);
233 for (j = 0; j <= i; j++)
234 ff_free_vlc(&vlc[j]);
236 av_log(ctx->avctx, AV_LOG_ERROR,
237 "Could not read code table %d.\n", i);
242 /* Read in and restore every line */
243 for (i = 0; i < avctx->height; i++) {
244 read_argb_line(ctx, gb, pred, vlc, dst);
246 dst += pic->linesize[0];
249 for (i = 0; i < 4; i++)
250 ff_free_vlc(&vlc[i]);
255 static int decode_rgb24_frame(CLLCContext *ctx, GetBitContext *gb, AVFrame *pic)
257 AVCodecContext *avctx = ctx->avctx;
272 /* Read in code table for each plane */
273 for (i = 0; i < 3; i++) {
274 ret = read_code_table(ctx, gb, &vlc[i]);
276 for (j = 0; j <= i; j++)
277 ff_free_vlc(&vlc[j]);
279 av_log(ctx->avctx, AV_LOG_ERROR,
280 "Could not read code table %d.\n", i);
285 /* Read in and restore every line */
286 for (i = 0; i < avctx->height; i++) {
287 for (j = 0; j < 3; j++)
288 read_rgb24_component_line(ctx, gb, &pred[j], &vlc[j], &dst[j]);
290 dst += pic->linesize[0];
293 for (i = 0; i < 3; i++)
294 ff_free_vlc(&vlc[i]);
299 static int decode_yuv_frame(CLLCContext *ctx, GetBitContext *gb, AVFrame *pic)
301 AVCodecContext *avctx = ctx->avctx;
313 dst[0] = pic->data[0];
314 dst[1] = pic->data[1];
315 dst[2] = pic->data[2];
319 block = get_bits(gb, 8);
321 avpriv_request_sample(ctx->avctx, "Blocked YUV");
322 return AVERROR_PATCHWELCOME;
325 /* Read in code table for luma and chroma */
326 for (i = 0; i < 2; i++) {
327 ret = read_code_table(ctx, gb, &vlc[i]);
329 for (j = 0; j <= i; j++)
330 ff_free_vlc(&vlc[j]);
332 av_log(ctx->avctx, AV_LOG_ERROR,
333 "Could not read code table %d.\n", i);
338 /* Read in and restore every line */
339 for (i = 0; i < avctx->height; i++) {
340 read_yuv_component_line(ctx, gb, &pred[0], &vlc[0], dst[0], 0); /* Y */
341 read_yuv_component_line(ctx, gb, &pred[1], &vlc[1], dst[1], 1); /* U */
342 read_yuv_component_line(ctx, gb, &pred[2], &vlc[1], dst[2], 1); /* V */
344 for (j = 0; j < 3; j++)
345 dst[j] += pic->linesize[j];
348 for (i = 0; i < 2; i++)
349 ff_free_vlc(&vlc[i]);
354 static int cllc_decode_frame(AVCodecContext *avctx, void *data,
355 int *got_picture_ptr, AVPacket *avpkt)
357 CLLCContext *ctx = avctx->priv_data;
359 uint8_t *src = avpkt->data;
360 uint32_t info_tag, info_offset;
363 int coding_type, ret;
365 /* Skip the INFO header if present */
367 info_tag = AV_RL32(src);
368 if (info_tag == MKTAG('I', 'N', 'F', 'O')) {
369 info_offset = AV_RL32(src + 4);
370 if (info_offset > UINT32_MAX - 8 || info_offset + 8 > avpkt->size) {
371 av_log(avctx, AV_LOG_ERROR,
372 "Invalid INFO header offset: 0x%08"PRIX32" is too large.\n",
374 return AVERROR_INVALIDDATA;
380 av_log(avctx, AV_LOG_DEBUG, "Skipping INFO chunk.\n");
383 data_size = (avpkt->size - info_offset) & ~1;
385 /* Make sure our bswap16'd buffer is big enough */
386 av_fast_padded_malloc(&ctx->swapped_buf,
387 &ctx->swapped_buf_size, data_size);
388 if (!ctx->swapped_buf) {
389 av_log(avctx, AV_LOG_ERROR, "Could not allocate swapped buffer.\n");
390 return AVERROR(ENOMEM);
393 /* bswap16 the buffer since CLLC's bitreader works in 16-bit words */
394 ctx->bdsp.bswap16_buf((uint16_t *) ctx->swapped_buf, (uint16_t *) src,
397 if ((ret = init_get_bits8(&gb, ctx->swapped_buf, data_size)) < 0)
401 * Read in coding type. The types are as follows:
404 * 1 - BGR24 (Triples)
408 coding_type = (AV_RL32(src) >> 8) & 0xFF;
409 av_log(avctx, AV_LOG_DEBUG, "Frame coding type: %d\n", coding_type);
411 switch (coding_type) {
413 avctx->pix_fmt = AV_PIX_FMT_YUV422P;
414 avctx->bits_per_raw_sample = 8;
416 if ((ret = ff_get_buffer(avctx, pic, 0)) < 0)
419 ret = decode_yuv_frame(ctx, &gb, pic);
426 avctx->pix_fmt = AV_PIX_FMT_RGB24;
427 avctx->bits_per_raw_sample = 8;
429 if ((ret = ff_get_buffer(avctx, pic, 0)) < 0)
432 ret = decode_rgb24_frame(ctx, &gb, pic);
438 avctx->pix_fmt = AV_PIX_FMT_ARGB;
439 avctx->bits_per_raw_sample = 8;
441 if ((ret = ff_get_buffer(avctx, pic, 0)) < 0)
444 ret = decode_argb_frame(ctx, &gb, pic);
450 av_log(avctx, AV_LOG_ERROR, "Unknown coding type: %d.\n", coding_type);
451 return AVERROR_INVALIDDATA;
455 pic->pict_type = AV_PICTURE_TYPE_I;
457 *got_picture_ptr = 1;
462 static av_cold int cllc_decode_close(AVCodecContext *avctx)
464 CLLCContext *ctx = avctx->priv_data;
466 av_freep(&ctx->swapped_buf);
471 static av_cold int cllc_decode_init(AVCodecContext *avctx)
473 CLLCContext *ctx = avctx->priv_data;
475 /* Initialize various context values */
477 ctx->swapped_buf = NULL;
478 ctx->swapped_buf_size = 0;
480 ff_bswapdsp_init(&ctx->bdsp);
485 AVCodec ff_cllc_decoder = {
487 .long_name = NULL_IF_CONFIG_SMALL("Canopus Lossless Codec"),
488 .type = AVMEDIA_TYPE_VIDEO,
489 .id = AV_CODEC_ID_CLLC,
490 .priv_data_size = sizeof(CLLCContext),
491 .init = cllc_decode_init,
492 .decode = cllc_decode_frame,
493 .close = cllc_decode_close,
494 .capabilities = CODEC_CAP_DR1,