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)
49 int num_lens, num_codes, num_codes_sum;
55 num_lens = get_bits(gb, 5);
57 if (num_lens > VLC_BITS * VLC_DEPTH) {
58 av_log(ctx->avctx, AV_LOG_ERROR, "To long VLCs %d\n", num_lens);
59 return AVERROR_INVALIDDATA;
62 for (i = 0; i < num_lens; i++) {
63 num_codes = get_bits(gb, 9);
64 num_codes_sum += num_codes;
66 if (num_codes_sum > 256) {
67 av_log(ctx->avctx, AV_LOG_ERROR,
68 "Too many VLCs (%d) to be read.\n", num_codes_sum);
69 return AVERROR_INVALIDDATA;
72 for (j = 0; j < num_codes; j++) {
73 symbols[count] = get_bits(gb, 8);
80 return ff_init_vlc_from_lengths(vlc, VLC_BITS, count, bits, 1,
81 symbols, 1, 1, 0, 0, ctx->avctx);
85 * Unlike the RGB24 read/restore, which reads in a component at a time,
86 * ARGB read/restore reads in ARGB quads.
88 static int read_argb_line(CLLCContext *ctx, GetBitContext *gb, int *top_left,
89 VLC *vlc, uint8_t *outbuf)
96 OPEN_READER(bits, gb);
99 pred[0] = top_left[0];
100 pred[1] = top_left[1];
101 pred[2] = top_left[2];
102 pred[3] = top_left[3];
104 for (i = 0; i < ctx->avctx->width; i++) {
105 /* Always get the alpha component */
106 UPDATE_CACHE(bits, gb);
107 GET_VLC(code, bits, gb, vlc[0].table, VLC_BITS, VLC_DEPTH);
112 /* Skip the components if they are entirely transparent */
115 UPDATE_CACHE(bits, gb);
116 GET_VLC(code, bits, gb, vlc[1].table, VLC_BITS, VLC_DEPTH);
122 UPDATE_CACHE(bits, gb);
123 GET_VLC(code, bits, gb, vlc[2].table, VLC_BITS, VLC_DEPTH);
129 UPDATE_CACHE(bits, gb);
130 GET_VLC(code, bits, gb, vlc[3].table, VLC_BITS, VLC_DEPTH);
143 CLOSE_READER(bits, gb);
145 top_left[0] = outbuf[0];
147 /* Only stash components if they are not transparent */
149 top_left[1] = outbuf[1];
150 top_left[2] = outbuf[2];
151 top_left[3] = outbuf[3];
157 static int read_rgb24_component_line(CLLCContext *ctx, GetBitContext *gb,
158 int *top_left, VLC *vlc, uint8_t *outbuf)
164 OPEN_READER(bits, gb);
169 /* Simultaneously read and restore the line */
170 for (i = 0; i < ctx->avctx->width; i++) {
171 UPDATE_CACHE(bits, gb);
172 GET_VLC(code, bits, gb, vlc->table, VLC_BITS, VLC_DEPTH);
179 CLOSE_READER(bits, gb);
181 /* Stash the first pixel */
182 *top_left = outbuf[0];
187 static int read_yuv_component_line(CLLCContext *ctx, GetBitContext *gb,
188 int *top_left, VLC *vlc, uint8_t *outbuf,
194 OPEN_READER(bits, gb);
198 /* Simultaneously read and restore the line */
199 for (i = 0; i < ctx->avctx->width >> is_chroma; i++) {
200 UPDATE_CACHE(bits, gb);
201 GET_VLC(code, bits, gb, vlc->table, VLC_BITS, VLC_DEPTH);
207 CLOSE_READER(bits, gb);
209 /* Stash the first pixel */
210 *top_left = outbuf[0];
215 static int decode_argb_frame(CLLCContext *ctx, GetBitContext *gb, AVFrame *pic)
217 AVCodecContext *avctx = ctx->avctx;
233 /* Read in code table for each plane */
234 for (i = 0; i < 4; i++) {
235 ret = read_code_table(ctx, gb, &vlc[i]);
237 for (j = 0; j < i; j++)
238 ff_free_vlc(&vlc[j]);
240 av_log(ctx->avctx, AV_LOG_ERROR,
241 "Could not read code table %d.\n", i);
246 /* Read in and restore every line */
247 for (i = 0; i < avctx->height; i++) {
248 read_argb_line(ctx, gb, pred, vlc, dst);
250 dst += pic->linesize[0];
253 for (i = 0; i < 4; i++)
254 ff_free_vlc(&vlc[i]);
259 static int decode_rgb24_frame(CLLCContext *ctx, GetBitContext *gb, AVFrame *pic)
261 AVCodecContext *avctx = ctx->avctx;
276 /* Read in code table for each plane */
277 for (i = 0; i < 3; i++) {
278 ret = read_code_table(ctx, gb, &vlc[i]);
280 for (j = 0; j < i; j++)
281 ff_free_vlc(&vlc[j]);
283 av_log(ctx->avctx, AV_LOG_ERROR,
284 "Could not read code table %d.\n", i);
289 /* Read in and restore every line */
290 for (i = 0; i < avctx->height; i++) {
291 for (j = 0; j < 3; j++)
292 read_rgb24_component_line(ctx, gb, &pred[j], &vlc[j], &dst[j]);
294 dst += pic->linesize[0];
297 for (i = 0; i < 3; i++)
298 ff_free_vlc(&vlc[i]);
303 static int decode_yuv_frame(CLLCContext *ctx, GetBitContext *gb, AVFrame *pic)
305 AVCodecContext *avctx = ctx->avctx;
317 dst[0] = pic->data[0];
318 dst[1] = pic->data[1];
319 dst[2] = pic->data[2];
323 block = get_bits(gb, 8);
325 avpriv_request_sample(ctx->avctx, "Blocked YUV");
326 return AVERROR_PATCHWELCOME;
329 /* Read in code table for luma and chroma */
330 for (i = 0; i < 2; i++) {
331 ret = read_code_table(ctx, gb, &vlc[i]);
333 for (j = 0; j < i; j++)
334 ff_free_vlc(&vlc[j]);
336 av_log(ctx->avctx, AV_LOG_ERROR,
337 "Could not read code table %d.\n", i);
342 /* Read in and restore every line */
343 for (i = 0; i < avctx->height; i++) {
344 read_yuv_component_line(ctx, gb, &pred[0], &vlc[0], dst[0], 0); /* Y */
345 read_yuv_component_line(ctx, gb, &pred[1], &vlc[1], dst[1], 1); /* U */
346 read_yuv_component_line(ctx, gb, &pred[2], &vlc[1], dst[2], 1); /* V */
348 for (j = 0; j < 3; j++)
349 dst[j] += pic->linesize[j];
352 for (i = 0; i < 2; i++)
353 ff_free_vlc(&vlc[i]);
358 static int cllc_decode_frame(AVCodecContext *avctx, void *data,
359 int *got_picture_ptr, AVPacket *avpkt)
361 CLLCContext *ctx = avctx->priv_data;
363 ThreadFrame frame = { .f = data };
364 uint8_t *src = avpkt->data;
365 uint32_t info_tag, info_offset;
368 int coding_type, ret;
370 if (avpkt->size < 4 + 4) {
371 av_log(avctx, AV_LOG_ERROR, "Frame is too small %d.\n", avpkt->size);
372 return AVERROR_INVALIDDATA;
376 info_tag = AV_RL32(src);
377 if (info_tag == MKTAG('I', 'N', 'F', 'O')) {
378 info_offset = AV_RL32(src + 4);
379 if (info_offset > UINT32_MAX - 8 || info_offset + 8 > avpkt->size) {
380 av_log(avctx, AV_LOG_ERROR,
381 "Invalid INFO header offset: 0x%08"PRIX32" is too large.\n",
383 return AVERROR_INVALIDDATA;
385 ff_canopus_parse_info_tag(avctx, src + 8, info_offset);
391 data_size = (avpkt->size - info_offset) & ~1;
393 /* Make sure our bswap16'd buffer is big enough */
394 av_fast_padded_malloc(&ctx->swapped_buf,
395 &ctx->swapped_buf_size, data_size);
396 if (!ctx->swapped_buf) {
397 av_log(avctx, AV_LOG_ERROR, "Could not allocate swapped buffer.\n");
398 return AVERROR(ENOMEM);
401 /* bswap16 the buffer since CLLC's bitreader works in 16-bit words */
402 ctx->bdsp.bswap16_buf((uint16_t *) ctx->swapped_buf, (uint16_t *) src,
405 if ((ret = init_get_bits8(&gb, ctx->swapped_buf, data_size)) < 0)
409 * Read in coding type. The types are as follows:
412 * 1 - BGR24 (Triples)
416 coding_type = (AV_RL32(src) >> 8) & 0xFF;
417 av_log(avctx, AV_LOG_DEBUG, "Frame coding type: %d\n", coding_type);
419 if(get_bits_left(&gb) < avctx->height * avctx->width)
420 return AVERROR_INVALIDDATA;
422 switch (coding_type) {
424 avctx->pix_fmt = AV_PIX_FMT_YUV422P;
425 avctx->bits_per_raw_sample = 8;
427 if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
430 ret = decode_yuv_frame(ctx, &gb, pic);
437 avctx->pix_fmt = AV_PIX_FMT_RGB24;
438 avctx->bits_per_raw_sample = 8;
440 if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
443 ret = decode_rgb24_frame(ctx, &gb, pic);
449 avctx->pix_fmt = AV_PIX_FMT_ARGB;
450 avctx->bits_per_raw_sample = 8;
452 if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
455 ret = decode_argb_frame(ctx, &gb, pic);
461 av_log(avctx, AV_LOG_ERROR, "Unknown coding type: %d.\n", coding_type);
462 return AVERROR_INVALIDDATA;
466 pic->pict_type = AV_PICTURE_TYPE_I;
468 *got_picture_ptr = 1;
473 static av_cold int cllc_decode_close(AVCodecContext *avctx)
475 CLLCContext *ctx = avctx->priv_data;
477 av_freep(&ctx->swapped_buf);
482 static av_cold int cllc_decode_init(AVCodecContext *avctx)
484 CLLCContext *ctx = avctx->priv_data;
486 /* Initialize various context values */
488 ctx->swapped_buf = NULL;
489 ctx->swapped_buf_size = 0;
491 ff_bswapdsp_init(&ctx->bdsp);
496 AVCodec ff_cllc_decoder = {
498 .long_name = NULL_IF_CONFIG_SMALL("Canopus Lossless Codec"),
499 .type = AVMEDIA_TYPE_VIDEO,
500 .id = AV_CODEC_ID_CLLC,
501 .priv_data_size = sizeof(CLLCContext),
502 .init = cllc_decode_init,
503 .decode = cllc_decode_frame,
504 .close = cllc_decode_close,
505 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
506 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,