h264: remove obsolete comment.
[FFMpeg-mirror/mplayer-patches.git] / libavcodec / cllc.c
blob30dc53c6e11b58bfbe696c36a7f12c030daf9af6
1 /*
2 * Canopus Lossless Codec decoder
4 * Copyright (c) 2012 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
23 #include "libavutil/intreadwrite.h"
24 #include "dsputil.h"
25 #include "get_bits.h"
26 #include "avcodec.h"
27 #include "internal.h"
29 typedef struct CLLCContext {
30 DSPContext dsp;
31 AVCodecContext *avctx;
33 uint8_t *swapped_buf;
34 int swapped_buf_size;
35 } CLLCContext;
37 static int read_code_table(CLLCContext *ctx, GetBitContext *gb, VLC *vlc)
39 uint8_t symbols[256];
40 uint8_t bits[256];
41 uint16_t codes[256];
42 int num_lens, num_codes, num_codes_sum, prefix;
43 int i, j, count;
45 prefix = 0;
46 count = 0;
47 num_codes_sum = 0;
49 num_lens = get_bits(gb, 5);
51 for (i = 0; i < num_lens; i++) {
52 num_codes = get_bits(gb, 9);
53 num_codes_sum += num_codes;
55 if (num_codes_sum > 256) {
56 vlc->table = NULL;
58 av_log(ctx->avctx, AV_LOG_ERROR,
59 "Too many VLCs (%d) to be read.\n", num_codes_sum);
60 return AVERROR_INVALIDDATA;
63 for (j = 0; j < num_codes; j++) {
64 symbols[count] = get_bits(gb, 8);
65 bits[count] = i + 1;
66 codes[count] = prefix++;
68 count++;
71 prefix <<= 1;
74 return ff_init_vlc_sparse(vlc, 7, count, bits, 1, 1,
75 codes, 2, 2, symbols, 1, 1, 0);
79 * Unlike the RGB24 read/restore, which reads in a component at a time,
80 * ARGB read/restore reads in ARGB quads.
82 static int read_argb_line(CLLCContext *ctx, GetBitContext *gb, int *top_left,
83 VLC *vlc, uint8_t *outbuf)
85 uint8_t *dst;
86 int pred[4];
87 int code;
88 int i;
90 OPEN_READER(bits, gb);
92 dst = outbuf;
93 pred[0] = top_left[0];
94 pred[1] = top_left[1];
95 pred[2] = top_left[2];
96 pred[3] = top_left[3];
98 for (i = 0; i < ctx->avctx->width; i++) {
99 /* Always get the alpha component */
100 UPDATE_CACHE(bits, gb);
101 GET_VLC(code, bits, gb, vlc[0].table, 7, 2);
103 pred[0] += code;
104 dst[0] = pred[0];
106 /* Skip the components if they are entirely transparent */
107 if (dst[0]) {
108 /* Red */
109 UPDATE_CACHE(bits, gb);
110 GET_VLC(code, bits, gb, vlc[1].table, 7, 2);
112 pred[1] += code;
113 dst[1] = pred[1];
115 /* Green */
116 UPDATE_CACHE(bits, gb);
117 GET_VLC(code, bits, gb, vlc[2].table, 7, 2);
119 pred[2] += code;
120 dst[2] = pred[2];
122 /* Blue */
123 UPDATE_CACHE(bits, gb);
124 GET_VLC(code, bits, gb, vlc[3].table, 7, 2);
126 pred[3] += code;
127 dst[3] = pred[3];
128 } else {
129 dst[1] = 0;
130 dst[2] = 0;
131 dst[3] = 0;
134 dst += 4;
137 CLOSE_READER(bits, gb);
139 dst -= 4 * ctx->avctx->width;
140 top_left[0] = dst[0];
142 /* Only stash components if they are not transparent */
143 if (top_left[0]) {
144 top_left[1] = dst[1];
145 top_left[2] = dst[2];
146 top_left[3] = dst[3];
149 return 0;
152 static int read_rgb24_component_line(CLLCContext *ctx, GetBitContext *gb,
153 int *top_left, VLC *vlc, uint8_t *outbuf)
155 uint8_t *dst;
156 int pred, code;
157 int i;
159 OPEN_READER(bits, gb);
161 dst = outbuf;
162 pred = *top_left;
164 /* Simultaneously read and restore the line */
165 for (i = 0; i < ctx->avctx->width; i++) {
166 UPDATE_CACHE(bits, gb);
167 GET_VLC(code, bits, gb, vlc->table, 7, 2);
169 pred += code;
170 dst[0] = pred;
171 dst += 3;
174 CLOSE_READER(bits, gb);
176 /* Stash the first pixel */
177 *top_left = dst[-3 * ctx->avctx->width];
179 return 0;
182 static int decode_argb_frame(CLLCContext *ctx, GetBitContext *gb, AVFrame *pic)
184 AVCodecContext *avctx = ctx->avctx;
185 uint8_t *dst;
186 int pred[4];
187 int ret;
188 int i, j;
189 VLC vlc[4];
191 pred[0] = 0;
192 pred[1] = 0x80;
193 pred[2] = 0x80;
194 pred[3] = 0x80;
196 dst = pic->data[0];
198 skip_bits(gb, 16);
200 /* Read in code table for each plane */
201 for (i = 0; i < 4; i++) {
202 ret = read_code_table(ctx, gb, &vlc[i]);
203 if (ret < 0) {
204 for (j = 0; j <= i; j++)
205 ff_free_vlc(&vlc[j]);
207 av_log(ctx->avctx, AV_LOG_ERROR,
208 "Could not read code table %d.\n", i);
209 return ret;
213 /* Read in and restore every line */
214 for (i = 0; i < avctx->height; i++) {
215 read_argb_line(ctx, gb, pred, vlc, dst);
217 dst += pic->linesize[0];
220 for (i = 0; i < 4; i++)
221 ff_free_vlc(&vlc[i]);
223 return 0;
226 static int decode_rgb24_frame(CLLCContext *ctx, GetBitContext *gb, AVFrame *pic)
228 AVCodecContext *avctx = ctx->avctx;
229 uint8_t *dst;
230 int pred[3];
231 int ret;
232 int i, j;
233 VLC vlc[3];
235 pred[0] = 0x80;
236 pred[1] = 0x80;
237 pred[2] = 0x80;
239 dst = pic->data[0];
241 skip_bits(gb, 16);
243 /* Read in code table for each plane */
244 for (i = 0; i < 3; i++) {
245 ret = read_code_table(ctx, gb, &vlc[i]);
246 if (ret < 0) {
247 for (j = 0; j <= i; j++)
248 ff_free_vlc(&vlc[j]);
250 av_log(ctx->avctx, AV_LOG_ERROR,
251 "Could not read code table %d.\n", i);
252 return ret;
256 /* Read in and restore every line */
257 for (i = 0; i < avctx->height; i++) {
258 for (j = 0; j < 3; j++)
259 read_rgb24_component_line(ctx, gb, &pred[j], &vlc[j], &dst[j]);
261 dst += pic->linesize[0];
264 for (i = 0; i < 3; i++)
265 ff_free_vlc(&vlc[i]);
267 return 0;
270 static int cllc_decode_frame(AVCodecContext *avctx, void *data,
271 int *got_picture_ptr, AVPacket *avpkt)
273 CLLCContext *ctx = avctx->priv_data;
274 AVFrame *pic = avctx->coded_frame;
275 uint8_t *src = avpkt->data;
276 uint32_t info_tag, info_offset;
277 int data_size;
278 GetBitContext gb;
279 int coding_type, ret;
281 if (pic->data[0])
282 avctx->release_buffer(avctx, pic);
284 pic->reference = 0;
286 /* Skip the INFO header if present */
287 info_offset = 0;
288 info_tag = AV_RL32(src);
289 if (info_tag == MKTAG('I', 'N', 'F', 'O')) {
290 info_offset = AV_RL32(src + 4);
291 if (info_offset > UINT32_MAX - 8 || info_offset + 8 > avpkt->size) {
292 av_log(avctx, AV_LOG_ERROR,
293 "Invalid INFO header offset: 0x%08X is too large.\n",
294 info_offset);
295 return AVERROR_INVALIDDATA;
298 info_offset += 8;
299 src += info_offset;
301 av_log(avctx, AV_LOG_DEBUG, "Skipping INFO chunk.\n");
304 data_size = (avpkt->size - info_offset) & ~1;
306 /* Make sure our bswap16'd buffer is big enough */
307 av_fast_padded_malloc(&ctx->swapped_buf,
308 &ctx->swapped_buf_size, data_size);
309 if (!ctx->swapped_buf) {
310 av_log(avctx, AV_LOG_ERROR, "Could not allocate swapped buffer.\n");
311 return AVERROR(ENOMEM);
314 /* bswap16 the buffer since CLLC's bitreader works in 16-bit words */
315 ctx->dsp.bswap16_buf((uint16_t *) ctx->swapped_buf, (uint16_t *) src,
316 data_size / 2);
318 init_get_bits(&gb, ctx->swapped_buf, data_size * 8);
321 * Read in coding type. The types are as follows:
323 * 0 - YUY2
324 * 1 - BGR24 (Triples)
325 * 2 - BGR24 (Quads)
326 * 3 - BGRA
328 coding_type = (AV_RL32(src) >> 8) & 0xFF;
329 av_log(avctx, AV_LOG_DEBUG, "Frame coding type: %d\n", coding_type);
331 switch (coding_type) {
332 case 1:
333 case 2:
334 avctx->pix_fmt = AV_PIX_FMT_RGB24;
335 avctx->bits_per_raw_sample = 8;
337 ret = ff_get_buffer(avctx, pic);
338 if (ret < 0) {
339 av_log(avctx, AV_LOG_ERROR, "Could not allocate buffer.\n");
340 return ret;
343 ret = decode_rgb24_frame(ctx, &gb, pic);
344 if (ret < 0)
345 return ret;
347 break;
348 case 3:
349 avctx->pix_fmt = AV_PIX_FMT_ARGB;
350 avctx->bits_per_raw_sample = 8;
352 ret = ff_get_buffer(avctx, pic);
353 if (ret < 0) {
354 av_log(avctx, AV_LOG_ERROR, "Could not allocate buffer.\n");
355 return ret;
358 ret = decode_argb_frame(ctx, &gb, pic);
359 if (ret < 0)
360 return ret;
362 break;
363 default:
364 av_log(avctx, AV_LOG_ERROR, "Unknown coding type: %d.\n", coding_type);
365 return AVERROR_INVALIDDATA;
368 pic->key_frame = 1;
369 pic->pict_type = AV_PICTURE_TYPE_I;
371 *got_picture_ptr = 1;
372 *(AVFrame *)data = *pic;
374 return avpkt->size;
377 static av_cold int cllc_decode_close(AVCodecContext *avctx)
379 CLLCContext *ctx = avctx->priv_data;
381 if (avctx->coded_frame->data[0])
382 avctx->release_buffer(avctx, avctx->coded_frame);
384 av_freep(&avctx->coded_frame);
385 av_freep(&ctx->swapped_buf);
387 return 0;
390 static av_cold int cllc_decode_init(AVCodecContext *avctx)
392 CLLCContext *ctx = avctx->priv_data;
394 /* Initialize various context values */
395 ctx->avctx = avctx;
396 ctx->swapped_buf = NULL;
397 ctx->swapped_buf_size = 0;
399 ff_dsputil_init(&ctx->dsp, avctx);
401 avctx->coded_frame = avcodec_alloc_frame();
402 if (!avctx->coded_frame) {
403 av_log(avctx, AV_LOG_ERROR, "Could not allocate frame.\n");
404 return AVERROR(ENOMEM);
407 return 0;
410 AVCodec ff_cllc_decoder = {
411 .name = "cllc",
412 .type = AVMEDIA_TYPE_VIDEO,
413 .id = AV_CODEC_ID_CLLC,
414 .priv_data_size = sizeof(CLLCContext),
415 .init = cllc_decode_init,
416 .decode = cllc_decode_frame,
417 .close = cllc_decode_close,
418 .capabilities = CODEC_CAP_DR1,
419 .long_name = NULL_IF_CONFIG_SMALL("Canopus Lossless Codec"),