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"
32 typedef struct CLLCContext {
33 AVCodecContext *avctx;
40 static int read_code_table(CLLCContext *ctx, GetBitContext *gb, VLC *vlc)
45 int num_lens, num_codes, num_codes_sum, prefix;
52 num_lens = get_bits(gb, 5);
54 for (i = 0; i < num_lens; i++) {
55 num_codes = get_bits(gb, 9);
56 num_codes_sum += num_codes;
58 if (num_codes_sum > 256) {
61 av_log(ctx->avctx, AV_LOG_ERROR,
62 "Too many VLCs (%d) to be read.\n", num_codes_sum);
63 return AVERROR_INVALIDDATA;
66 for (j = 0; j < num_codes; j++) {
67 symbols[count] = get_bits(gb, 8);
69 codes[count] = prefix++;
77 return ff_init_vlc_sparse(vlc, 7, count, bits, 1, 1,
78 codes, 2, 2, symbols, 1, 1, 0);
82 * Unlike the RGB24 read/restore, which reads in a component at a time,
83 * ARGB read/restore reads in ARGB quads.
85 static int read_argb_line(CLLCContext *ctx, GetBitContext *gb, int *top_left,
86 VLC *vlc, uint8_t *outbuf)
93 OPEN_READER(bits, gb);
96 pred[0] = top_left[0];
97 pred[1] = top_left[1];
98 pred[2] = top_left[2];
99 pred[3] = top_left[3];
101 for (i = 0; i < ctx->avctx->width; i++) {
102 /* Always get the alpha component */
103 UPDATE_CACHE(bits, gb);
104 GET_VLC(code, bits, gb, vlc[0].table, 7, 2);
109 /* Skip the components if they are entirely transparent */
112 UPDATE_CACHE(bits, gb);
113 GET_VLC(code, bits, gb, vlc[1].table, 7, 2);
119 UPDATE_CACHE(bits, gb);
120 GET_VLC(code, bits, gb, vlc[2].table, 7, 2);
126 UPDATE_CACHE(bits, gb);
127 GET_VLC(code, bits, gb, vlc[3].table, 7, 2);
140 CLOSE_READER(bits, gb);
142 top_left[0] = outbuf[0];
144 /* Only stash components if they are not transparent */
146 top_left[1] = outbuf[1];
147 top_left[2] = outbuf[2];
148 top_left[3] = outbuf[3];
154 static int read_rgb24_component_line(CLLCContext *ctx, GetBitContext *gb,
155 int *top_left, VLC *vlc, uint8_t *outbuf)
161 OPEN_READER(bits, gb);
166 /* Simultaneously read and restore the line */
167 for (i = 0; i < ctx->avctx->width; i++) {
168 UPDATE_CACHE(bits, gb);
169 GET_VLC(code, bits, gb, vlc->table, 7, 2);
176 CLOSE_READER(bits, gb);
178 /* Stash the first pixel */
179 *top_left = outbuf[0];
184 static int read_yuv_component_line(CLLCContext *ctx, GetBitContext *gb,
185 int *top_left, VLC *vlc, uint8_t *outbuf,
191 OPEN_READER(bits, gb);
195 /* Simultaneously read and restore the line */
196 for (i = 0; i < ctx->avctx->width >> is_chroma; i++) {
197 UPDATE_CACHE(bits, gb);
198 GET_VLC(code, bits, gb, vlc->table, 7, 2);
204 CLOSE_READER(bits, gb);
206 /* Stash the first pixel */
207 *top_left = outbuf[0];
212 static int decode_argb_frame(CLLCContext *ctx, GetBitContext *gb, AVFrame *pic)
214 AVCodecContext *avctx = ctx->avctx;
230 /* Read in code table for each plane */
231 for (i = 0; i < 4; i++) {
232 ret = read_code_table(ctx, gb, &vlc[i]);
234 for (j = 0; j <= i; j++)
235 ff_free_vlc(&vlc[j]);
237 av_log(ctx->avctx, AV_LOG_ERROR,
238 "Could not read code table %d.\n", i);
243 /* Read in and restore every line */
244 for (i = 0; i < avctx->height; i++) {
245 read_argb_line(ctx, gb, pred, vlc, dst);
247 dst += pic->linesize[0];
250 for (i = 0; i < 4; i++)
251 ff_free_vlc(&vlc[i]);
256 static int decode_rgb24_frame(CLLCContext *ctx, GetBitContext *gb, AVFrame *pic)
258 AVCodecContext *avctx = ctx->avctx;
273 /* Read in code table for each plane */
274 for (i = 0; i < 3; i++) {
275 ret = read_code_table(ctx, gb, &vlc[i]);
277 for (j = 0; j <= i; j++)
278 ff_free_vlc(&vlc[j]);
280 av_log(ctx->avctx, AV_LOG_ERROR,
281 "Could not read code table %d.\n", i);
286 /* Read in and restore every line */
287 for (i = 0; i < avctx->height; i++) {
288 for (j = 0; j < 3; j++)
289 read_rgb24_component_line(ctx, gb, &pred[j], &vlc[j], &dst[j]);
291 dst += pic->linesize[0];
294 for (i = 0; i < 3; i++)
295 ff_free_vlc(&vlc[i]);
300 static int decode_yuv_frame(CLLCContext *ctx, GetBitContext *gb, AVFrame *pic)
302 AVCodecContext *avctx = ctx->avctx;
314 dst[0] = pic->data[0];
315 dst[1] = pic->data[1];
316 dst[2] = pic->data[2];
320 block = get_bits(gb, 8);
322 avpriv_request_sample(ctx->avctx, "Blocked YUV");
323 return AVERROR_PATCHWELCOME;
326 /* Read in code table for luma and chroma */
327 for (i = 0; i < 2; i++) {
328 ret = read_code_table(ctx, gb, &vlc[i]);
330 for (j = 0; j <= i; j++)
331 ff_free_vlc(&vlc[j]);
333 av_log(ctx->avctx, AV_LOG_ERROR,
334 "Could not read code table %d.\n", i);
339 /* Read in and restore every line */
340 for (i = 0; i < avctx->height; i++) {
341 read_yuv_component_line(ctx, gb, &pred[0], &vlc[0], dst[0], 0); /* Y */
342 read_yuv_component_line(ctx, gb, &pred[1], &vlc[1], dst[1], 1); /* U */
343 read_yuv_component_line(ctx, gb, &pred[2], &vlc[1], dst[2], 1); /* V */
345 for (j = 0; j < 3; j++)
346 dst[j] += pic->linesize[j];
349 for (i = 0; i < 2; i++)
350 ff_free_vlc(&vlc[i]);
355 static int cllc_decode_frame(AVCodecContext *avctx, void *data,
356 int *got_picture_ptr, AVPacket *avpkt)
358 CLLCContext *ctx = avctx->priv_data;
360 uint8_t *src = avpkt->data;
361 uint32_t info_tag, info_offset;
364 int coding_type, ret;
366 if (avpkt->size < 4 + 4) {
367 av_log(avctx, AV_LOG_ERROR, "Frame is too small %d.\n", avpkt->size);
368 return AVERROR_INVALIDDATA;
372 info_tag = AV_RL32(src);
373 if (info_tag == MKTAG('I', 'N', 'F', 'O')) {
374 info_offset = AV_RL32(src + 4);
375 if (info_offset > UINT32_MAX - 8 || info_offset + 8 > avpkt->size) {
376 av_log(avctx, AV_LOG_ERROR,
377 "Invalid INFO header offset: 0x%08"PRIX32" is too large.\n",
379 return AVERROR_INVALIDDATA;
381 ff_canopus_parse_info_tag(avctx, src + 8, info_offset);
387 data_size = (avpkt->size - info_offset) & ~1;
389 /* Make sure our bswap16'd buffer is big enough */
390 av_fast_padded_malloc(&ctx->swapped_buf,
391 &ctx->swapped_buf_size, data_size);
392 if (!ctx->swapped_buf) {
393 av_log(avctx, AV_LOG_ERROR, "Could not allocate swapped buffer.\n");
394 return AVERROR(ENOMEM);
397 /* bswap16 the buffer since CLLC's bitreader works in 16-bit words */
398 ctx->bdsp.bswap16_buf((uint16_t *) ctx->swapped_buf, (uint16_t *) src,
401 if ((ret = init_get_bits8(&gb, ctx->swapped_buf, data_size)) < 0)
405 * Read in coding type. The types are as follows:
408 * 1 - BGR24 (Triples)
412 coding_type = (AV_RL32(src) >> 8) & 0xFF;
413 av_log(avctx, AV_LOG_DEBUG, "Frame coding type: %d\n", coding_type);
415 switch (coding_type) {
417 avctx->pix_fmt = AV_PIX_FMT_YUV422P;
418 avctx->bits_per_raw_sample = 8;
420 if ((ret = ff_get_buffer(avctx, pic, 0)) < 0)
423 ret = decode_yuv_frame(ctx, &gb, pic);
430 avctx->pix_fmt = AV_PIX_FMT_RGB24;
431 avctx->bits_per_raw_sample = 8;
433 if ((ret = ff_get_buffer(avctx, pic, 0)) < 0)
436 ret = decode_rgb24_frame(ctx, &gb, pic);
442 avctx->pix_fmt = AV_PIX_FMT_ARGB;
443 avctx->bits_per_raw_sample = 8;
445 if ((ret = ff_get_buffer(avctx, pic, 0)) < 0)
448 ret = decode_argb_frame(ctx, &gb, pic);
454 av_log(avctx, AV_LOG_ERROR, "Unknown coding type: %d.\n", coding_type);
455 return AVERROR_INVALIDDATA;
459 pic->pict_type = AV_PICTURE_TYPE_I;
461 *got_picture_ptr = 1;
466 static av_cold int cllc_decode_close(AVCodecContext *avctx)
468 CLLCContext *ctx = avctx->priv_data;
470 av_freep(&ctx->swapped_buf);
475 static av_cold int cllc_decode_init(AVCodecContext *avctx)
477 CLLCContext *ctx = avctx->priv_data;
479 /* Initialize various context values */
481 ctx->swapped_buf = NULL;
482 ctx->swapped_buf_size = 0;
484 ff_bswapdsp_init(&ctx->bdsp);
489 AVCodec ff_cllc_decoder = {
491 .long_name = NULL_IF_CONFIG_SMALL("Canopus Lossless Codec"),
492 .type = AVMEDIA_TYPE_VIDEO,
493 .id = AV_CODEC_ID_CLLC,
494 .priv_data_size = sizeof(CLLCContext),
495 .init = cllc_decode_init,
496 .decode = cllc_decode_frame,
497 .close = cllc_decode_close,
498 .capabilities = AV_CODEC_CAP_DR1,