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"
37 typedef struct CLLCContext {
38 AVCodecContext *avctx;
45 static int read_code_table(CLLCContext *ctx, GetBitContext *gb, VLC *vlc)
50 int num_lens, num_codes, num_codes_sum, prefix;
57 num_lens = get_bits(gb, 5);
59 if (num_lens > VLC_BITS * VLC_DEPTH) {
60 av_log(ctx->avctx, AV_LOG_ERROR, "To long VLCs %d\n", num_lens);
61 return AVERROR_INVALIDDATA;
64 for (i = 0; i < num_lens; i++) {
65 num_codes = get_bits(gb, 9);
66 num_codes_sum += num_codes;
68 if (num_codes_sum > 256) {
69 av_log(ctx->avctx, AV_LOG_ERROR,
70 "Too many VLCs (%d) to be read.\n", num_codes_sum);
71 return AVERROR_INVALIDDATA;
74 for (j = 0; j < num_codes; j++) {
75 symbols[count] = get_bits(gb, 8);
77 codes[count] = prefix++;
81 if (prefix > (65535 - 256)/2) {
82 return AVERROR_INVALIDDATA;
88 return ff_init_vlc_sparse(vlc, VLC_BITS, count, bits, 1, 1,
89 codes, 2, 2, symbols, 1, 1, 0);
93 * Unlike the RGB24 read/restore, which reads in a component at a time,
94 * ARGB read/restore reads in ARGB quads.
96 static int read_argb_line(CLLCContext *ctx, GetBitContext *gb, int *top_left,
97 VLC *vlc, uint8_t *outbuf)
104 OPEN_READER(bits, gb);
107 pred[0] = top_left[0];
108 pred[1] = top_left[1];
109 pred[2] = top_left[2];
110 pred[3] = top_left[3];
112 for (i = 0; i < ctx->avctx->width; i++) {
113 /* Always get the alpha component */
114 UPDATE_CACHE(bits, gb);
115 GET_VLC(code, bits, gb, vlc[0].table, VLC_BITS, VLC_DEPTH);
120 /* Skip the components if they are entirely transparent */
123 UPDATE_CACHE(bits, gb);
124 GET_VLC(code, bits, gb, vlc[1].table, VLC_BITS, VLC_DEPTH);
130 UPDATE_CACHE(bits, gb);
131 GET_VLC(code, bits, gb, vlc[2].table, VLC_BITS, VLC_DEPTH);
137 UPDATE_CACHE(bits, gb);
138 GET_VLC(code, bits, gb, vlc[3].table, VLC_BITS, VLC_DEPTH);
151 CLOSE_READER(bits, gb);
153 top_left[0] = outbuf[0];
155 /* Only stash components if they are not transparent */
157 top_left[1] = outbuf[1];
158 top_left[2] = outbuf[2];
159 top_left[3] = outbuf[3];
165 static int read_rgb24_component_line(CLLCContext *ctx, GetBitContext *gb,
166 int *top_left, VLC *vlc, uint8_t *outbuf)
172 OPEN_READER(bits, gb);
177 /* Simultaneously read and restore the line */
178 for (i = 0; i < ctx->avctx->width; i++) {
179 UPDATE_CACHE(bits, gb);
180 GET_VLC(code, bits, gb, vlc->table, VLC_BITS, VLC_DEPTH);
187 CLOSE_READER(bits, gb);
189 /* Stash the first pixel */
190 *top_left = outbuf[0];
195 static int read_yuv_component_line(CLLCContext *ctx, GetBitContext *gb,
196 int *top_left, VLC *vlc, uint8_t *outbuf,
202 OPEN_READER(bits, gb);
206 /* Simultaneously read and restore the line */
207 for (i = 0; i < ctx->avctx->width >> is_chroma; i++) {
208 UPDATE_CACHE(bits, gb);
209 GET_VLC(code, bits, gb, vlc->table, VLC_BITS, VLC_DEPTH);
215 CLOSE_READER(bits, gb);
217 /* Stash the first pixel */
218 *top_left = outbuf[0];
223 static int decode_argb_frame(CLLCContext *ctx, GetBitContext *gb, AVFrame *pic)
225 AVCodecContext *avctx = ctx->avctx;
241 /* Read in code table for each plane */
242 for (i = 0; i < 4; i++) {
243 ret = read_code_table(ctx, gb, &vlc[i]);
245 for (j = 0; j < i; j++)
246 ff_free_vlc(&vlc[j]);
248 av_log(ctx->avctx, AV_LOG_ERROR,
249 "Could not read code table %d.\n", i);
254 /* Read in and restore every line */
255 for (i = 0; i < avctx->height; i++) {
256 read_argb_line(ctx, gb, pred, vlc, dst);
258 dst += pic->linesize[0];
261 for (i = 0; i < 4; i++)
262 ff_free_vlc(&vlc[i]);
267 static int decode_rgb24_frame(CLLCContext *ctx, GetBitContext *gb, AVFrame *pic)
269 AVCodecContext *avctx = ctx->avctx;
284 /* Read in code table for each plane */
285 for (i = 0; i < 3; i++) {
286 ret = read_code_table(ctx, gb, &vlc[i]);
288 for (j = 0; j < i; j++)
289 ff_free_vlc(&vlc[j]);
291 av_log(ctx->avctx, AV_LOG_ERROR,
292 "Could not read code table %d.\n", i);
297 /* Read in and restore every line */
298 for (i = 0; i < avctx->height; i++) {
299 for (j = 0; j < 3; j++)
300 read_rgb24_component_line(ctx, gb, &pred[j], &vlc[j], &dst[j]);
302 dst += pic->linesize[0];
305 for (i = 0; i < 3; i++)
306 ff_free_vlc(&vlc[i]);
311 static int decode_yuv_frame(CLLCContext *ctx, GetBitContext *gb, AVFrame *pic)
313 AVCodecContext *avctx = ctx->avctx;
325 dst[0] = pic->data[0];
326 dst[1] = pic->data[1];
327 dst[2] = pic->data[2];
331 block = get_bits(gb, 8);
333 avpriv_request_sample(ctx->avctx, "Blocked YUV");
334 return AVERROR_PATCHWELCOME;
337 /* Read in code table for luma and chroma */
338 for (i = 0; i < 2; i++) {
339 ret = read_code_table(ctx, gb, &vlc[i]);
341 for (j = 0; j < i; j++)
342 ff_free_vlc(&vlc[j]);
344 av_log(ctx->avctx, AV_LOG_ERROR,
345 "Could not read code table %d.\n", i);
350 /* Read in and restore every line */
351 for (i = 0; i < avctx->height; i++) {
352 read_yuv_component_line(ctx, gb, &pred[0], &vlc[0], dst[0], 0); /* Y */
353 read_yuv_component_line(ctx, gb, &pred[1], &vlc[1], dst[1], 1); /* U */
354 read_yuv_component_line(ctx, gb, &pred[2], &vlc[1], dst[2], 1); /* V */
356 for (j = 0; j < 3; j++)
357 dst[j] += pic->linesize[j];
360 for (i = 0; i < 2; i++)
361 ff_free_vlc(&vlc[i]);
366 static int cllc_decode_frame(AVCodecContext *avctx, void *data,
367 int *got_picture_ptr, AVPacket *avpkt)
369 CLLCContext *ctx = avctx->priv_data;
371 ThreadFrame frame = { .f = data };
372 uint8_t *src = avpkt->data;
373 uint32_t info_tag, info_offset;
376 int coding_type, ret;
378 if (avpkt->size < 4 + 4) {
379 av_log(avctx, AV_LOG_ERROR, "Frame is too small %d.\n", avpkt->size);
380 return AVERROR_INVALIDDATA;
384 info_tag = AV_RL32(src);
385 if (info_tag == MKTAG('I', 'N', 'F', 'O')) {
386 info_offset = AV_RL32(src + 4);
387 if (info_offset > UINT32_MAX - 8 || info_offset + 8 > avpkt->size) {
388 av_log(avctx, AV_LOG_ERROR,
389 "Invalid INFO header offset: 0x%08"PRIX32" is too large.\n",
391 return AVERROR_INVALIDDATA;
393 ff_canopus_parse_info_tag(avctx, src + 8, info_offset);
399 data_size = (avpkt->size - info_offset) & ~1;
401 /* Make sure our bswap16'd buffer is big enough */
402 av_fast_padded_malloc(&ctx->swapped_buf,
403 &ctx->swapped_buf_size, data_size);
404 if (!ctx->swapped_buf) {
405 av_log(avctx, AV_LOG_ERROR, "Could not allocate swapped buffer.\n");
406 return AVERROR(ENOMEM);
409 /* bswap16 the buffer since CLLC's bitreader works in 16-bit words */
410 ctx->bdsp.bswap16_buf((uint16_t *) ctx->swapped_buf, (uint16_t *) src,
413 if ((ret = init_get_bits8(&gb, ctx->swapped_buf, data_size)) < 0)
417 * Read in coding type. The types are as follows:
420 * 1 - BGR24 (Triples)
424 coding_type = (AV_RL32(src) >> 8) & 0xFF;
425 av_log(avctx, AV_LOG_DEBUG, "Frame coding type: %d\n", coding_type);
427 if(get_bits_left(&gb) < avctx->height * avctx->width)
428 return AVERROR_INVALIDDATA;
430 switch (coding_type) {
432 avctx->pix_fmt = AV_PIX_FMT_YUV422P;
433 avctx->bits_per_raw_sample = 8;
435 if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
438 ret = decode_yuv_frame(ctx, &gb, pic);
445 avctx->pix_fmt = AV_PIX_FMT_RGB24;
446 avctx->bits_per_raw_sample = 8;
448 if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
451 ret = decode_rgb24_frame(ctx, &gb, pic);
457 avctx->pix_fmt = AV_PIX_FMT_ARGB;
458 avctx->bits_per_raw_sample = 8;
460 if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
463 ret = decode_argb_frame(ctx, &gb, pic);
469 av_log(avctx, AV_LOG_ERROR, "Unknown coding type: %d.\n", coding_type);
470 return AVERROR_INVALIDDATA;
474 pic->pict_type = AV_PICTURE_TYPE_I;
476 *got_picture_ptr = 1;
481 static av_cold int cllc_decode_close(AVCodecContext *avctx)
483 CLLCContext *ctx = avctx->priv_data;
485 av_freep(&ctx->swapped_buf);
490 static av_cold int cllc_decode_init(AVCodecContext *avctx)
492 CLLCContext *ctx = avctx->priv_data;
494 /* Initialize various context values */
496 ctx->swapped_buf = NULL;
497 ctx->swapped_buf_size = 0;
499 ff_bswapdsp_init(&ctx->bdsp);
504 AVCodec ff_cllc_decoder = {
506 .long_name = NULL_IF_CONFIG_SMALL("Canopus Lossless Codec"),
507 .type = AVMEDIA_TYPE_VIDEO,
508 .id = AV_CODEC_ID_CLLC,
509 .priv_data_size = sizeof(CLLCContext),
510 .init = cllc_decode_init,
511 .decode = cllc_decode_frame,
512 .close = cllc_decode_close,
513 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
514 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,