2 * Canopus Lossless Codec decoder
4 * Copyright (c) 2012-2013 Derek Buitenhuis
6 * This file is part of Libav.
8 * Libav 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 * Libav 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 Libav; 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"
27 #include "bitstream.h"
34 typedef struct CLLCContext
{
35 AVCodecContext
*avctx
;
42 static int read_code_table(CLLCContext
*ctx
, BitstreamContext
*bc
, VLC
*vlc
)
47 int num_lens
, num_codes
, num_codes_sum
, prefix
;
54 num_lens
= bitstream_read(bc
, 5);
56 for (i
= 0; i
< num_lens
; i
++) {
57 num_codes
= bitstream_read(bc
, 9);
58 num_codes_sum
+= num_codes
;
60 if (num_codes_sum
> 256) {
63 av_log(ctx
->avctx
, AV_LOG_ERROR
,
64 "Too many VLCs (%d) to be read.\n", num_codes_sum
);
65 return AVERROR_INVALIDDATA
;
68 for (j
= 0; j
< num_codes
; j
++) {
69 symbols
[count
] = bitstream_read(bc
, 8);
71 codes
[count
] = prefix
++;
79 return ff_init_vlc_sparse(vlc
, 7, count
, bits
, 1, 1,
80 codes
, 2, 2, symbols
, 1, 1, 0);
84 * Unlike the RGB24 read/restore, which reads in a component at a time,
85 * ARGB read/restore reads in ARGB quads.
87 static int read_argb_line(CLLCContext
*ctx
, BitstreamContext
*bc
, int *top_left
,
88 VLC
*vlc
, uint8_t *outbuf
)
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 code
= bitstream_read_vlc(bc
, vlc
[0].table
, 7, 2);
108 /* Skip the components if they are entirely transparent */
111 code
= bitstream_read_vlc(bc
, vlc
[1].table
, 7, 2);
117 code
= bitstream_read_vlc(bc
, vlc
[2].table
, 7, 2);
123 code
= bitstream_read_vlc(bc
, vlc
[3].table
, 7, 2);
136 top_left
[0] = outbuf
[0];
138 /* Only stash components if they are not transparent */
140 top_left
[1] = outbuf
[1];
141 top_left
[2] = outbuf
[2];
142 top_left
[3] = outbuf
[3];
148 static int read_rgb24_component_line(CLLCContext
*ctx
, BitstreamContext
*bc
,
149 int *top_left
, VLC
*vlc
, uint8_t *outbuf
)
158 /* Simultaneously read and restore the line */
159 for (i
= 0; i
< ctx
->avctx
->width
; i
++) {
160 code
= bitstream_read_vlc(bc
, vlc
->table
, 7, 2);
167 /* Stash the first pixel */
168 *top_left
= outbuf
[0];
173 static int read_yuv_component_line(CLLCContext
*ctx
, BitstreamContext
*bc
,
174 int *top_left
, VLC
*vlc
, uint8_t *outbuf
,
182 /* Simultaneously read and restore the line */
183 for (i
= 0; i
< ctx
->avctx
->width
>> is_chroma
; i
++) {
184 code
= bitstream_read_vlc(bc
, vlc
->table
, 7, 2);
190 /* Stash the first pixel */
191 *top_left
= outbuf
[0];
196 static int decode_argb_frame(CLLCContext
*ctx
, BitstreamContext
*bc
, AVFrame
*pic
)
198 AVCodecContext
*avctx
= ctx
->avctx
;
212 bitstream_skip(bc
, 16);
214 /* Read in code table for each plane */
215 for (i
= 0; i
< 4; i
++) {
216 ret
= read_code_table(ctx
, bc
, &vlc
[i
]);
218 for (j
= 0; j
<= i
; j
++)
219 ff_free_vlc(&vlc
[j
]);
221 av_log(ctx
->avctx
, AV_LOG_ERROR
,
222 "Could not read code table %d.\n", i
);
227 /* Read in and restore every line */
228 for (i
= 0; i
< avctx
->height
; i
++) {
229 read_argb_line(ctx
, bc
, pred
, vlc
, dst
);
231 dst
+= pic
->linesize
[0];
234 for (i
= 0; i
< 4; i
++)
235 ff_free_vlc(&vlc
[i
]);
240 static int decode_rgb24_frame(CLLCContext
*ctx
, BitstreamContext
*bc
, AVFrame
*pic
)
242 AVCodecContext
*avctx
= ctx
->avctx
;
255 bitstream_skip(bc
, 16);
257 /* Read in code table for each plane */
258 for (i
= 0; i
< 3; i
++) {
259 ret
= read_code_table(ctx
, bc
, &vlc
[i
]);
261 for (j
= 0; j
<= i
; j
++)
262 ff_free_vlc(&vlc
[j
]);
264 av_log(ctx
->avctx
, AV_LOG_ERROR
,
265 "Could not read code table %d.\n", i
);
270 /* Read in and restore every line */
271 for (i
= 0; i
< avctx
->height
; i
++) {
272 for (j
= 0; j
< 3; j
++)
273 read_rgb24_component_line(ctx
, bc
, &pred
[j
], &vlc
[j
], &dst
[j
]);
275 dst
+= pic
->linesize
[0];
278 for (i
= 0; i
< 3; i
++)
279 ff_free_vlc(&vlc
[i
]);
284 static int decode_yuv_frame(CLLCContext
*ctx
, BitstreamContext
*bc
, AVFrame
*pic
)
286 AVCodecContext
*avctx
= ctx
->avctx
;
298 dst
[0] = pic
->data
[0];
299 dst
[1] = pic
->data
[1];
300 dst
[2] = pic
->data
[2];
302 bitstream_skip(bc
, 8);
304 block
= bitstream_read(bc
, 8);
306 avpriv_request_sample(ctx
->avctx
, "Blocked YUV");
307 return AVERROR_PATCHWELCOME
;
310 /* Read in code table for luma and chroma */
311 for (i
= 0; i
< 2; i
++) {
312 ret
= read_code_table(ctx
, bc
, &vlc
[i
]);
314 for (j
= 0; j
<= i
; j
++)
315 ff_free_vlc(&vlc
[j
]);
317 av_log(ctx
->avctx
, AV_LOG_ERROR
,
318 "Could not read code table %d.\n", i
);
323 /* Read in and restore every line */
324 for (i
= 0; i
< avctx
->height
; i
++) {
325 read_yuv_component_line(ctx
, bc
, &pred
[0], &vlc
[0], dst
[0], 0); /* Y */
326 read_yuv_component_line(ctx
, bc
, &pred
[1], &vlc
[1], dst
[1], 1); /* U */
327 read_yuv_component_line(ctx
, bc
, &pred
[2], &vlc
[1], dst
[2], 1); /* V */
329 for (j
= 0; j
< 3; j
++)
330 dst
[j
] += pic
->linesize
[j
];
333 for (i
= 0; i
< 2; i
++)
334 ff_free_vlc(&vlc
[i
]);
339 static int cllc_decode_frame(AVCodecContext
*avctx
, void *data
,
340 int *got_picture_ptr
, AVPacket
*avpkt
)
342 CLLCContext
*ctx
= avctx
->priv_data
;
344 uint8_t *src
= avpkt
->data
;
345 uint32_t info_tag
, info_offset
;
348 int coding_type
, ret
;
350 if (avpkt
->size
< 4 + 4) {
351 av_log(avctx
, AV_LOG_ERROR
, "Frame is too small %d.\n", avpkt
->size
);
352 return AVERROR_INVALIDDATA
;
356 info_tag
= AV_RL32(src
);
357 if (info_tag
== MKTAG('I', 'N', 'F', 'O')) {
358 info_offset
= AV_RL32(src
+ 4);
359 if (info_offset
> UINT32_MAX
- 8 || info_offset
+ 8 > avpkt
->size
) {
360 av_log(avctx
, AV_LOG_ERROR
,
361 "Invalid INFO header offset: 0x%08"PRIX32
" is too large.\n",
363 return AVERROR_INVALIDDATA
;
365 ff_canopus_parse_info_tag(avctx
, src
+ 8, info_offset
);
371 data_size
= (avpkt
->size
- info_offset
) & ~1;
373 /* Make sure our bswap16'd buffer is big enough */
374 av_fast_padded_malloc(&ctx
->swapped_buf
,
375 &ctx
->swapped_buf_size
, data_size
);
376 if (!ctx
->swapped_buf
) {
377 av_log(avctx
, AV_LOG_ERROR
, "Could not allocate swapped buffer.\n");
378 return AVERROR(ENOMEM
);
381 /* bswap16 the buffer since CLLC's bitreader works in 16-bit words */
382 ctx
->bdsp
.bswap16_buf((uint16_t *) ctx
->swapped_buf
, (uint16_t *) src
,
385 bitstream_init8(&bc
, ctx
->swapped_buf
, data_size
);
388 * Read in coding type. The types are as follows:
391 * 1 - BGR24 (Triples)
395 coding_type
= (AV_RL32(src
) >> 8) & 0xFF;
396 av_log(avctx
, AV_LOG_DEBUG
, "Frame coding type: %d\n", coding_type
);
398 switch (coding_type
) {
400 avctx
->pix_fmt
= AV_PIX_FMT_YUV422P
;
401 avctx
->bits_per_raw_sample
= 8;
403 ret
= ff_get_buffer(avctx
, pic
, 0);
405 av_log(avctx
, AV_LOG_ERROR
, "Could not allocate buffer.\n");
409 ret
= decode_yuv_frame(ctx
, &bc
, pic
);
416 avctx
->pix_fmt
= AV_PIX_FMT_RGB24
;
417 avctx
->bits_per_raw_sample
= 8;
419 ret
= ff_get_buffer(avctx
, pic
, 0);
421 av_log(avctx
, AV_LOG_ERROR
, "Could not allocate buffer.\n");
425 ret
= decode_rgb24_frame(ctx
, &bc
, pic
);
431 avctx
->pix_fmt
= AV_PIX_FMT_ARGB
;
432 avctx
->bits_per_raw_sample
= 8;
434 ret
= ff_get_buffer(avctx
, pic
, 0);
436 av_log(avctx
, AV_LOG_ERROR
, "Could not allocate buffer.\n");
440 ret
= decode_argb_frame(ctx
, &bc
, pic
);
446 av_log(avctx
, AV_LOG_ERROR
, "Unknown coding type: %d.\n", coding_type
);
447 return AVERROR_INVALIDDATA
;
451 pic
->pict_type
= AV_PICTURE_TYPE_I
;
453 *got_picture_ptr
= 1;
458 static av_cold
int cllc_decode_close(AVCodecContext
*avctx
)
460 CLLCContext
*ctx
= avctx
->priv_data
;
462 av_freep(&ctx
->swapped_buf
);
467 static av_cold
int cllc_decode_init(AVCodecContext
*avctx
)
469 CLLCContext
*ctx
= avctx
->priv_data
;
471 /* Initialize various context values */
473 ctx
->swapped_buf
= NULL
;
474 ctx
->swapped_buf_size
= 0;
476 ff_bswapdsp_init(&ctx
->bdsp
);
481 AVCodec ff_cllc_decoder
= {
483 .long_name
= NULL_IF_CONFIG_SMALL("Canopus Lossless Codec"),
484 .type
= AVMEDIA_TYPE_VIDEO
,
485 .id
= AV_CODEC_ID_CLLC
,
486 .priv_data_size
= sizeof(CLLCContext
),
487 .init
= cllc_decode_init
,
488 .decode
= cllc_decode_frame
,
489 .close
= cllc_decode_close
,
490 .capabilities
= AV_CODEC_CAP_DR1
,
491 .caps_internal
= FF_CODEC_CAP_INIT_THREADSAFE
,