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) {
62 av_log(ctx->avctx, AV_LOG_ERROR, "To long VLCs %d\n", num_lens);
63 return AVERROR_INVALIDDATA;
66 for (i = 0; i < num_lens; i++) {
67 num_codes = get_bits(gb, 9);
68 num_codes_sum += num_codes;
70 if (num_codes_sum > 256) {
73 av_log(ctx->avctx, AV_LOG_ERROR,
74 "Too many VLCs (%d) to be read.\n", num_codes_sum);
75 return AVERROR_INVALIDDATA;
78 for (j = 0; j < num_codes; j++) {
79 symbols[count] = get_bits(gb, 8);
81 codes[count] = prefix++;
85 if (prefix > (65535 - 256)/2) {
87 return AVERROR_INVALIDDATA;
93 return ff_init_vlc_sparse(vlc, VLC_BITS, count, bits, 1, 1,
94 codes, 2, 2, symbols, 1, 1, 0);
98 * Unlike the RGB24 read/restore, which reads in a component at a time,
99 * ARGB read/restore reads in ARGB quads.
101 static int read_argb_line(CLLCContext *ctx, GetBitContext *gb, int *top_left,
102 VLC *vlc, uint8_t *outbuf)
109 OPEN_READER(bits, gb);
112 pred[0] = top_left[0];
113 pred[1] = top_left[1];
114 pred[2] = top_left[2];
115 pred[3] = top_left[3];
117 for (i = 0; i < ctx->avctx->width; i++) {
118 /* Always get the alpha component */
119 UPDATE_CACHE(bits, gb);
120 GET_VLC(code, bits, gb, vlc[0].table, VLC_BITS, VLC_DEPTH);
125 /* Skip the components if they are entirely transparent */
128 UPDATE_CACHE(bits, gb);
129 GET_VLC(code, bits, gb, vlc[1].table, VLC_BITS, VLC_DEPTH);
135 UPDATE_CACHE(bits, gb);
136 GET_VLC(code, bits, gb, vlc[2].table, VLC_BITS, VLC_DEPTH);
142 UPDATE_CACHE(bits, gb);
143 GET_VLC(code, bits, gb, vlc[3].table, VLC_BITS, VLC_DEPTH);
156 CLOSE_READER(bits, gb);
158 top_left[0] = outbuf[0];
160 /* Only stash components if they are not transparent */
162 top_left[1] = outbuf[1];
163 top_left[2] = outbuf[2];
164 top_left[3] = outbuf[3];
170 static int read_rgb24_component_line(CLLCContext *ctx, GetBitContext *gb,
171 int *top_left, VLC *vlc, uint8_t *outbuf)
177 OPEN_READER(bits, gb);
182 /* Simultaneously read and restore the line */
183 for (i = 0; i < ctx->avctx->width; i++) {
184 UPDATE_CACHE(bits, gb);
185 GET_VLC(code, bits, gb, vlc->table, VLC_BITS, VLC_DEPTH);
192 CLOSE_READER(bits, gb);
194 /* Stash the first pixel */
195 *top_left = outbuf[0];
200 static int read_yuv_component_line(CLLCContext *ctx, GetBitContext *gb,
201 int *top_left, VLC *vlc, uint8_t *outbuf,
207 OPEN_READER(bits, gb);
211 /* Simultaneously read and restore the line */
212 for (i = 0; i < ctx->avctx->width >> is_chroma; i++) {
213 UPDATE_CACHE(bits, gb);
214 GET_VLC(code, bits, gb, vlc->table, VLC_BITS, VLC_DEPTH);
220 CLOSE_READER(bits, gb);
222 /* Stash the first pixel */
223 *top_left = outbuf[0];
228 static int decode_argb_frame(CLLCContext *ctx, GetBitContext *gb, AVFrame *pic)
230 AVCodecContext *avctx = ctx->avctx;
246 /* Read in code table for each plane */
247 for (i = 0; i < 4; i++) {
248 ret = read_code_table(ctx, gb, &vlc[i]);
250 for (j = 0; j <= i; j++)
251 ff_free_vlc(&vlc[j]);
253 av_log(ctx->avctx, AV_LOG_ERROR,
254 "Could not read code table %d.\n", i);
259 /* Read in and restore every line */
260 for (i = 0; i < avctx->height; i++) {
261 read_argb_line(ctx, gb, pred, vlc, dst);
263 dst += pic->linesize[0];
266 for (i = 0; i < 4; i++)
267 ff_free_vlc(&vlc[i]);
272 static int decode_rgb24_frame(CLLCContext *ctx, GetBitContext *gb, AVFrame *pic)
274 AVCodecContext *avctx = ctx->avctx;
289 /* Read in code table for each plane */
290 for (i = 0; i < 3; i++) {
291 ret = read_code_table(ctx, gb, &vlc[i]);
293 for (j = 0; j <= i; j++)
294 ff_free_vlc(&vlc[j]);
296 av_log(ctx->avctx, AV_LOG_ERROR,
297 "Could not read code table %d.\n", i);
302 /* Read in and restore every line */
303 for (i = 0; i < avctx->height; i++) {
304 for (j = 0; j < 3; j++)
305 read_rgb24_component_line(ctx, gb, &pred[j], &vlc[j], &dst[j]);
307 dst += pic->linesize[0];
310 for (i = 0; i < 3; i++)
311 ff_free_vlc(&vlc[i]);
316 static int decode_yuv_frame(CLLCContext *ctx, GetBitContext *gb, AVFrame *pic)
318 AVCodecContext *avctx = ctx->avctx;
330 dst[0] = pic->data[0];
331 dst[1] = pic->data[1];
332 dst[2] = pic->data[2];
336 block = get_bits(gb, 8);
338 avpriv_request_sample(ctx->avctx, "Blocked YUV");
339 return AVERROR_PATCHWELCOME;
342 /* Read in code table for luma and chroma */
343 for (i = 0; i < 2; i++) {
344 ret = read_code_table(ctx, gb, &vlc[i]);
346 for (j = 0; j <= i; j++)
347 ff_free_vlc(&vlc[j]);
349 av_log(ctx->avctx, AV_LOG_ERROR,
350 "Could not read code table %d.\n", i);
355 /* Read in and restore every line */
356 for (i = 0; i < avctx->height; i++) {
357 read_yuv_component_line(ctx, gb, &pred[0], &vlc[0], dst[0], 0); /* Y */
358 read_yuv_component_line(ctx, gb, &pred[1], &vlc[1], dst[1], 1); /* U */
359 read_yuv_component_line(ctx, gb, &pred[2], &vlc[1], dst[2], 1); /* V */
361 for (j = 0; j < 3; j++)
362 dst[j] += pic->linesize[j];
365 for (i = 0; i < 2; i++)
366 ff_free_vlc(&vlc[i]);
371 static int cllc_decode_frame(AVCodecContext *avctx, void *data,
372 int *got_picture_ptr, AVPacket *avpkt)
374 CLLCContext *ctx = avctx->priv_data;
376 ThreadFrame frame = { .f = data };
377 uint8_t *src = avpkt->data;
378 uint32_t info_tag, info_offset;
381 int coding_type, ret;
383 if (avpkt->size < 4 + 4) {
384 av_log(avctx, AV_LOG_ERROR, "Frame is too small %d.\n", avpkt->size);
385 return AVERROR_INVALIDDATA;
389 info_tag = AV_RL32(src);
390 if (info_tag == MKTAG('I', 'N', 'F', 'O')) {
391 info_offset = AV_RL32(src + 4);
392 if (info_offset > UINT32_MAX - 8 || info_offset + 8 > avpkt->size) {
393 av_log(avctx, AV_LOG_ERROR,
394 "Invalid INFO header offset: 0x%08"PRIX32" is too large.\n",
396 return AVERROR_INVALIDDATA;
398 ff_canopus_parse_info_tag(avctx, src + 8, info_offset);
404 data_size = (avpkt->size - info_offset) & ~1;
406 /* Make sure our bswap16'd buffer is big enough */
407 av_fast_padded_malloc(&ctx->swapped_buf,
408 &ctx->swapped_buf_size, data_size);
409 if (!ctx->swapped_buf) {
410 av_log(avctx, AV_LOG_ERROR, "Could not allocate swapped buffer.\n");
411 return AVERROR(ENOMEM);
414 /* bswap16 the buffer since CLLC's bitreader works in 16-bit words */
415 ctx->bdsp.bswap16_buf((uint16_t *) ctx->swapped_buf, (uint16_t *) src,
418 if ((ret = init_get_bits8(&gb, ctx->swapped_buf, data_size)) < 0)
422 * Read in coding type. The types are as follows:
425 * 1 - BGR24 (Triples)
429 coding_type = (AV_RL32(src) >> 8) & 0xFF;
430 av_log(avctx, AV_LOG_DEBUG, "Frame coding type: %d\n", coding_type);
432 if(get_bits_left(&gb) < avctx->height * avctx->width)
433 return AVERROR_INVALIDDATA;
435 switch (coding_type) {
437 avctx->pix_fmt = AV_PIX_FMT_YUV422P;
438 avctx->bits_per_raw_sample = 8;
440 if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
443 ret = decode_yuv_frame(ctx, &gb, pic);
450 avctx->pix_fmt = AV_PIX_FMT_RGB24;
451 avctx->bits_per_raw_sample = 8;
453 if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
456 ret = decode_rgb24_frame(ctx, &gb, pic);
462 avctx->pix_fmt = AV_PIX_FMT_ARGB;
463 avctx->bits_per_raw_sample = 8;
465 if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
468 ret = decode_argb_frame(ctx, &gb, pic);
474 av_log(avctx, AV_LOG_ERROR, "Unknown coding type: %d.\n", coding_type);
475 return AVERROR_INVALIDDATA;
479 pic->pict_type = AV_PICTURE_TYPE_I;
481 *got_picture_ptr = 1;
487 static int cllc_init_thread_copy(AVCodecContext *avctx)
489 CLLCContext *ctx = avctx->priv_data;
492 ctx->swapped_buf = NULL;
493 ctx->swapped_buf_size = 0;
499 static av_cold int cllc_decode_close(AVCodecContext *avctx)
501 CLLCContext *ctx = avctx->priv_data;
503 av_freep(&ctx->swapped_buf);
508 static av_cold int cllc_decode_init(AVCodecContext *avctx)
510 CLLCContext *ctx = avctx->priv_data;
512 /* Initialize various context values */
514 ctx->swapped_buf = NULL;
515 ctx->swapped_buf_size = 0;
517 ff_bswapdsp_init(&ctx->bdsp);
522 AVCodec ff_cllc_decoder = {
524 .long_name = NULL_IF_CONFIG_SMALL("Canopus Lossless Codec"),
525 .type = AVMEDIA_TYPE_VIDEO,
526 .id = AV_CODEC_ID_CLLC,
527 .priv_data_size = sizeof(CLLCContext),
528 .init = cllc_decode_init,
529 .init_thread_copy = ONLY_IF_THREADS_ENABLED(cllc_init_thread_copy),
530 .decode = cllc_decode_frame,
531 .close = cllc_decode_close,
532 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
533 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,