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"
33 typedef struct CLLCContext {
34 AVCodecContext *avctx;
41 static int read_code_table(CLLCContext *ctx, GetBitContext *gb, VLC *vlc)
46 int num_lens, num_codes, num_codes_sum, prefix;
53 num_lens = get_bits(gb, 5);
55 for (i = 0; i < num_lens; i++) {
56 num_codes = get_bits(gb, 9);
57 num_codes_sum += num_codes;
59 if (num_codes_sum > 256) {
62 av_log(ctx->avctx, AV_LOG_ERROR,
63 "Too many VLCs (%d) to be read.\n", num_codes_sum);
64 return AVERROR_INVALIDDATA;
67 for (j = 0; j < num_codes; j++) {
68 symbols[count] = get_bits(gb, 8);
70 codes[count] = prefix++;
74 if (prefix > (65535 - 256)/2) {
76 return AVERROR_INVALIDDATA;
82 return ff_init_vlc_sparse(vlc, 7, count, bits, 1, 1,
83 codes, 2, 2, symbols, 1, 1, 0);
87 * Unlike the RGB24 read/restore, which reads in a component at a time,
88 * ARGB read/restore reads in ARGB quads.
90 static int read_argb_line(CLLCContext *ctx, GetBitContext *gb, int *top_left,
91 VLC *vlc, uint8_t *outbuf)
98 OPEN_READER(bits, gb);
101 pred[0] = top_left[0];
102 pred[1] = top_left[1];
103 pred[2] = top_left[2];
104 pred[3] = top_left[3];
106 for (i = 0; i < ctx->avctx->width; i++) {
107 /* Always get the alpha component */
108 UPDATE_CACHE(bits, gb);
109 GET_VLC(code, bits, gb, vlc[0].table, 7, 2);
114 /* Skip the components if they are entirely transparent */
117 UPDATE_CACHE(bits, gb);
118 GET_VLC(code, bits, gb, vlc[1].table, 7, 2);
124 UPDATE_CACHE(bits, gb);
125 GET_VLC(code, bits, gb, vlc[2].table, 7, 2);
131 UPDATE_CACHE(bits, gb);
132 GET_VLC(code, bits, gb, vlc[3].table, 7, 2);
145 CLOSE_READER(bits, gb);
147 top_left[0] = outbuf[0];
149 /* Only stash components if they are not transparent */
151 top_left[1] = outbuf[1];
152 top_left[2] = outbuf[2];
153 top_left[3] = outbuf[3];
159 static int read_rgb24_component_line(CLLCContext *ctx, GetBitContext *gb,
160 int *top_left, VLC *vlc, uint8_t *outbuf)
166 OPEN_READER(bits, gb);
171 /* Simultaneously read and restore the line */
172 for (i = 0; i < ctx->avctx->width; i++) {
173 UPDATE_CACHE(bits, gb);
174 GET_VLC(code, bits, gb, vlc->table, 7, 2);
181 CLOSE_READER(bits, gb);
183 /* Stash the first pixel */
184 *top_left = outbuf[0];
189 static int read_yuv_component_line(CLLCContext *ctx, GetBitContext *gb,
190 int *top_left, VLC *vlc, uint8_t *outbuf,
196 OPEN_READER(bits, gb);
200 /* Simultaneously read and restore the line */
201 for (i = 0; i < ctx->avctx->width >> is_chroma; i++) {
202 UPDATE_CACHE(bits, gb);
203 GET_VLC(code, bits, gb, vlc->table, 7, 2);
209 CLOSE_READER(bits, gb);
211 /* Stash the first pixel */
212 *top_left = outbuf[0];
217 static int decode_argb_frame(CLLCContext *ctx, GetBitContext *gb, AVFrame *pic)
219 AVCodecContext *avctx = ctx->avctx;
235 /* Read in code table for each plane */
236 for (i = 0; i < 4; i++) {
237 ret = read_code_table(ctx, gb, &vlc[i]);
239 for (j = 0; j <= i; j++)
240 ff_free_vlc(&vlc[j]);
242 av_log(ctx->avctx, AV_LOG_ERROR,
243 "Could not read code table %d.\n", i);
248 /* Read in and restore every line */
249 for (i = 0; i < avctx->height; i++) {
250 read_argb_line(ctx, gb, pred, vlc, dst);
252 dst += pic->linesize[0];
255 for (i = 0; i < 4; i++)
256 ff_free_vlc(&vlc[i]);
261 static int decode_rgb24_frame(CLLCContext *ctx, GetBitContext *gb, AVFrame *pic)
263 AVCodecContext *avctx = ctx->avctx;
278 /* Read in code table for each plane */
279 for (i = 0; i < 3; i++) {
280 ret = read_code_table(ctx, gb, &vlc[i]);
282 for (j = 0; j <= i; j++)
283 ff_free_vlc(&vlc[j]);
285 av_log(ctx->avctx, AV_LOG_ERROR,
286 "Could not read code table %d.\n", i);
291 /* Read in and restore every line */
292 for (i = 0; i < avctx->height; i++) {
293 for (j = 0; j < 3; j++)
294 read_rgb24_component_line(ctx, gb, &pred[j], &vlc[j], &dst[j]);
296 dst += pic->linesize[0];
299 for (i = 0; i < 3; i++)
300 ff_free_vlc(&vlc[i]);
305 static int decode_yuv_frame(CLLCContext *ctx, GetBitContext *gb, AVFrame *pic)
307 AVCodecContext *avctx = ctx->avctx;
319 dst[0] = pic->data[0];
320 dst[1] = pic->data[1];
321 dst[2] = pic->data[2];
325 block = get_bits(gb, 8);
327 avpriv_request_sample(ctx->avctx, "Blocked YUV");
328 return AVERROR_PATCHWELCOME;
331 /* Read in code table for luma and chroma */
332 for (i = 0; i < 2; i++) {
333 ret = read_code_table(ctx, gb, &vlc[i]);
335 for (j = 0; j <= i; j++)
336 ff_free_vlc(&vlc[j]);
338 av_log(ctx->avctx, AV_LOG_ERROR,
339 "Could not read code table %d.\n", i);
344 /* Read in and restore every line */
345 for (i = 0; i < avctx->height; i++) {
346 read_yuv_component_line(ctx, gb, &pred[0], &vlc[0], dst[0], 0); /* Y */
347 read_yuv_component_line(ctx, gb, &pred[1], &vlc[1], dst[1], 1); /* U */
348 read_yuv_component_line(ctx, gb, &pred[2], &vlc[1], dst[2], 1); /* V */
350 for (j = 0; j < 3; j++)
351 dst[j] += pic->linesize[j];
354 for (i = 0; i < 2; i++)
355 ff_free_vlc(&vlc[i]);
360 static int cllc_decode_frame(AVCodecContext *avctx, void *data,
361 int *got_picture_ptr, AVPacket *avpkt)
363 CLLCContext *ctx = avctx->priv_data;
365 ThreadFrame frame = { .f = data };
366 uint8_t *src = avpkt->data;
367 uint32_t info_tag, info_offset;
370 int coding_type, ret;
372 if (avpkt->size < 4 + 4) {
373 av_log(avctx, AV_LOG_ERROR, "Frame is too small %d.\n", avpkt->size);
374 return AVERROR_INVALIDDATA;
378 info_tag = AV_RL32(src);
379 if (info_tag == MKTAG('I', 'N', 'F', 'O')) {
380 info_offset = AV_RL32(src + 4);
381 if (info_offset > UINT32_MAX - 8 || info_offset + 8 > avpkt->size) {
382 av_log(avctx, AV_LOG_ERROR,
383 "Invalid INFO header offset: 0x%08"PRIX32" is too large.\n",
385 return AVERROR_INVALIDDATA;
387 ff_canopus_parse_info_tag(avctx, src + 8, info_offset);
393 data_size = (avpkt->size - info_offset) & ~1;
395 /* Make sure our bswap16'd buffer is big enough */
396 av_fast_padded_malloc(&ctx->swapped_buf,
397 &ctx->swapped_buf_size, data_size);
398 if (!ctx->swapped_buf) {
399 av_log(avctx, AV_LOG_ERROR, "Could not allocate swapped buffer.\n");
400 return AVERROR(ENOMEM);
403 /* bswap16 the buffer since CLLC's bitreader works in 16-bit words */
404 ctx->bdsp.bswap16_buf((uint16_t *) ctx->swapped_buf, (uint16_t *) src,
407 if ((ret = init_get_bits8(&gb, ctx->swapped_buf, data_size)) < 0)
411 * Read in coding type. The types are as follows:
414 * 1 - BGR24 (Triples)
418 coding_type = (AV_RL32(src) >> 8) & 0xFF;
419 av_log(avctx, AV_LOG_DEBUG, "Frame coding type: %d\n", coding_type);
421 switch (coding_type) {
423 avctx->pix_fmt = AV_PIX_FMT_YUV422P;
424 avctx->bits_per_raw_sample = 8;
426 if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
429 ret = decode_yuv_frame(ctx, &gb, pic);
436 avctx->pix_fmt = AV_PIX_FMT_RGB24;
437 avctx->bits_per_raw_sample = 8;
439 if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
442 ret = decode_rgb24_frame(ctx, &gb, pic);
448 avctx->pix_fmt = AV_PIX_FMT_ARGB;
449 avctx->bits_per_raw_sample = 8;
451 if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
454 ret = decode_argb_frame(ctx, &gb, pic);
460 av_log(avctx, AV_LOG_ERROR, "Unknown coding type: %d.\n", coding_type);
461 return AVERROR_INVALIDDATA;
465 pic->pict_type = AV_PICTURE_TYPE_I;
467 *got_picture_ptr = 1;
473 static int cllc_init_thread_copy(AVCodecContext *avctx)
475 CLLCContext *ctx = avctx->priv_data;
478 ctx->swapped_buf = NULL;
479 ctx->swapped_buf_size = 0;
485 static av_cold int cllc_decode_close(AVCodecContext *avctx)
487 CLLCContext *ctx = avctx->priv_data;
489 av_freep(&ctx->swapped_buf);
494 static av_cold int cllc_decode_init(AVCodecContext *avctx)
496 CLLCContext *ctx = avctx->priv_data;
498 /* Initialize various context values */
500 ctx->swapped_buf = NULL;
501 ctx->swapped_buf_size = 0;
503 ff_bswapdsp_init(&ctx->bdsp);
508 AVCodec ff_cllc_decoder = {
510 .long_name = NULL_IF_CONFIG_SMALL("Canopus Lossless Codec"),
511 .type = AVMEDIA_TYPE_VIDEO,
512 .id = AV_CODEC_ID_CLLC,
513 .priv_data_size = sizeof(CLLCContext),
514 .init = cllc_decode_init,
515 .init_thread_copy = ONLY_IF_THREADS_ENABLED(cllc_init_thread_copy),
516 .decode = cllc_decode_frame,
517 .close = cllc_decode_close,
518 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
519 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,