]> git.sesse.net Git - ffmpeg/blob - libavcodec/cllc.c
cllc: Implement YUV support
[ffmpeg] / libavcodec / cllc.c
1 /*
2  * Canopus Lossless Codec decoder
3  *
4  * Copyright (c) 2012-2013 Derek Buitenhuis
5  *
6  * This file is part of Libav.
7  *
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.
12  *
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.
17  *
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
21  */
22
23 #include "libavutil/intreadwrite.h"
24 #include "dsputil.h"
25 #include "get_bits.h"
26 #include "avcodec.h"
27 #include "internal.h"
28
29 typedef struct CLLCContext {
30     DSPContext dsp;
31     AVCodecContext *avctx;
32
33     uint8_t *swapped_buf;
34     int      swapped_buf_size;
35 } CLLCContext;
36
37 static int read_code_table(CLLCContext *ctx, GetBitContext *gb, VLC *vlc)
38 {
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;
44
45     prefix        = 0;
46     count         = 0;
47     num_codes_sum = 0;
48
49     num_lens = get_bits(gb, 5);
50
51     for (i = 0; i < num_lens; i++) {
52         num_codes      = get_bits(gb, 9);
53         num_codes_sum += num_codes;
54
55         if (num_codes_sum > 256) {
56             vlc->table = NULL;
57
58             av_log(ctx->avctx, AV_LOG_ERROR,
59                    "Too many VLCs (%d) to be read.\n", num_codes_sum);
60             return AVERROR_INVALIDDATA;
61         }
62
63         for (j = 0; j < num_codes; j++) {
64             symbols[count] = get_bits(gb, 8);
65             bits[count]    = i + 1;
66             codes[count]   = prefix++;
67
68             count++;
69         }
70
71         prefix <<= 1;
72     }
73
74     return ff_init_vlc_sparse(vlc, 7, count, bits, 1, 1,
75                               codes, 2, 2, symbols, 1, 1, 0);
76 }
77
78 /*
79  * Unlike the RGB24 read/restore, which reads in a component at a time,
80  * ARGB read/restore reads in ARGB quads.
81  */
82 static int read_argb_line(CLLCContext *ctx, GetBitContext *gb, int *top_left,
83                           VLC *vlc, uint8_t *outbuf)
84 {
85     uint8_t *dst;
86     int pred[4];
87     int code;
88     int i;
89
90     OPEN_READER(bits, gb);
91
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];
97
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);
102
103         pred[0] += code;
104         dst[0]   = pred[0];
105
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);
111
112             pred[1] += code;
113             dst[1]   = pred[1];
114
115             /* Green */
116             UPDATE_CACHE(bits, gb);
117             GET_VLC(code, bits, gb, vlc[2].table, 7, 2);
118
119             pred[2] += code;
120             dst[2]   = pred[2];
121
122             /* Blue */
123             UPDATE_CACHE(bits, gb);
124             GET_VLC(code, bits, gb, vlc[3].table, 7, 2);
125
126             pred[3] += code;
127             dst[3]   = pred[3];
128         } else {
129             dst[1] = 0;
130             dst[2] = 0;
131             dst[3] = 0;
132         }
133
134         dst += 4;
135     }
136
137     CLOSE_READER(bits, gb);
138
139     dst         -= 4 * ctx->avctx->width;
140     top_left[0]  = dst[0];
141
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];
147     }
148
149     return 0;
150 }
151
152 static int read_rgb24_component_line(CLLCContext *ctx, GetBitContext *gb,
153                                      int *top_left, VLC *vlc, uint8_t *outbuf)
154 {
155     uint8_t *dst;
156     int pred, code;
157     int i;
158
159     OPEN_READER(bits, gb);
160
161     dst  = outbuf;
162     pred = *top_left;
163
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);
168
169         pred  += code;
170         dst[0] = pred;
171         dst   += 3;
172     }
173
174     CLOSE_READER(bits, gb);
175
176     /* Stash the first pixel */
177     *top_left = dst[-3 * ctx->avctx->width];
178
179     return 0;
180 }
181
182 static int read_yuv_component_line(CLLCContext *ctx, GetBitContext *gb,
183                                    int *top_left, VLC *vlc, uint8_t *outbuf,
184                                    int is_chroma)
185 {
186     int pred, code;
187     int i;
188
189     OPEN_READER(bits, gb);
190
191     pred = *top_left;
192
193     /* Simultaneously read and restore the line */
194     for (i = 0; i < ctx->avctx->width >> is_chroma; i++) {
195         UPDATE_CACHE(bits, gb);
196         GET_VLC(code, bits, gb, vlc->table, 7, 2);
197
198         pred     += code;
199         outbuf[i] = pred;
200     }
201
202     CLOSE_READER(bits, gb);
203
204     /* Stash the first pixel */
205     *top_left = outbuf[0];
206
207     return 0;
208 }
209
210 static int decode_argb_frame(CLLCContext *ctx, GetBitContext *gb, AVFrame *pic)
211 {
212     AVCodecContext *avctx = ctx->avctx;
213     uint8_t *dst;
214     int pred[4];
215     int ret;
216     int i, j;
217     VLC vlc[4];
218
219     pred[0] = 0;
220     pred[1] = 0x80;
221     pred[2] = 0x80;
222     pred[3] = 0x80;
223
224     dst = pic->data[0];
225
226     skip_bits(gb, 16);
227
228     /* Read in code table for each plane */
229     for (i = 0; i < 4; i++) {
230         ret = read_code_table(ctx, gb, &vlc[i]);
231         if (ret < 0) {
232             for (j = 0; j <= i; j++)
233                 ff_free_vlc(&vlc[j]);
234
235             av_log(ctx->avctx, AV_LOG_ERROR,
236                    "Could not read code table %d.\n", i);
237             return ret;
238         }
239     }
240
241     /* Read in and restore every line */
242     for (i = 0; i < avctx->height; i++) {
243         read_argb_line(ctx, gb, pred, vlc, dst);
244
245         dst += pic->linesize[0];
246     }
247
248     for (i = 0; i < 4; i++)
249         ff_free_vlc(&vlc[i]);
250
251     return 0;
252 }
253
254 static int decode_rgb24_frame(CLLCContext *ctx, GetBitContext *gb, AVFrame *pic)
255 {
256     AVCodecContext *avctx = ctx->avctx;
257     uint8_t *dst;
258     int pred[3];
259     int ret;
260     int i, j;
261     VLC vlc[3];
262
263     pred[0] = 0x80;
264     pred[1] = 0x80;
265     pred[2] = 0x80;
266
267     dst = pic->data[0];
268
269     skip_bits(gb, 16);
270
271     /* Read in code table for each plane */
272     for (i = 0; i < 3; i++) {
273         ret = read_code_table(ctx, gb, &vlc[i]);
274         if (ret < 0) {
275             for (j = 0; j <= i; j++)
276                 ff_free_vlc(&vlc[j]);
277
278             av_log(ctx->avctx, AV_LOG_ERROR,
279                    "Could not read code table %d.\n", i);
280             return ret;
281         }
282     }
283
284     /* Read in and restore every line */
285     for (i = 0; i < avctx->height; i++) {
286         for (j = 0; j < 3; j++)
287             read_rgb24_component_line(ctx, gb, &pred[j], &vlc[j], &dst[j]);
288
289         dst += pic->linesize[0];
290     }
291
292     for (i = 0; i < 3; i++)
293         ff_free_vlc(&vlc[i]);
294
295     return 0;
296 }
297
298 static int decode_yuv_frame(CLLCContext *ctx, GetBitContext *gb, AVFrame *pic)
299 {
300     AVCodecContext *avctx = ctx->avctx;
301     uint8_t block;
302     uint8_t *dst[3];
303     int pred[3];
304     int ret;
305     int i, j;
306     VLC vlc[2];
307
308     pred[0] = 0x80;
309     pred[1] = 0x80;
310     pred[2] = 0x80;
311
312     dst[0] = pic->data[0];
313     dst[1] = pic->data[1];
314     dst[2] = pic->data[2];
315
316     skip_bits(gb, 8);
317
318     block = get_bits(gb, 8);
319     if (block) {
320         avpriv_request_sample(ctx->avctx, "Blocked YUV");
321         return AVERROR_PATCHWELCOME;
322     }
323
324     /* Read in code table for luma and chroma */
325     for (i = 0; i < 2; i++) {
326         ret = read_code_table(ctx, gb, &vlc[i]);
327         if (ret < 0) {
328             for (j = 0; j <= i; j++)
329                 ff_free_vlc(&vlc[j]);
330
331             av_log(ctx->avctx, AV_LOG_ERROR,
332                    "Could not read code table %d.\n", i);
333             return ret;
334         }
335     }
336
337     /* Read in and restore every line */
338     for (i = 0; i < avctx->height; i++) {
339         read_yuv_component_line(ctx, gb, &pred[0], &vlc[0], dst[0], 0); /* Y */
340         read_yuv_component_line(ctx, gb, &pred[1], &vlc[1], dst[1], 1); /* U */
341         read_yuv_component_line(ctx, gb, &pred[2], &vlc[1], dst[2], 1); /* V */
342
343         for (j = 0; j < 3; j++)
344             dst[j] += pic->linesize[j];
345     }
346
347     for (i = 0; i < 2; i++)
348         ff_free_vlc(&vlc[i]);
349
350     return 0;
351 }
352
353 static int cllc_decode_frame(AVCodecContext *avctx, void *data,
354                              int *got_picture_ptr, AVPacket *avpkt)
355 {
356     CLLCContext *ctx = avctx->priv_data;
357     AVFrame *pic = data;
358     uint8_t *src = avpkt->data;
359     uint32_t info_tag, info_offset;
360     int data_size;
361     GetBitContext gb;
362     int coding_type, ret;
363
364     /* Skip the INFO header if present */
365     info_offset = 0;
366     info_tag    = AV_RL32(src);
367     if (info_tag == MKTAG('I', 'N', 'F', 'O')) {
368         info_offset = AV_RL32(src + 4);
369         if (info_offset > UINT32_MAX - 8 || info_offset + 8 > avpkt->size) {
370             av_log(avctx, AV_LOG_ERROR,
371                    "Invalid INFO header offset: 0x%08X is too large.\n",
372                    info_offset);
373             return AVERROR_INVALIDDATA;
374         }
375
376         info_offset += 8;
377         src         += info_offset;
378
379         av_log(avctx, AV_LOG_DEBUG, "Skipping INFO chunk.\n");
380     }
381
382     data_size = (avpkt->size - info_offset) & ~1;
383
384     /* Make sure our bswap16'd buffer is big enough */
385     av_fast_padded_malloc(&ctx->swapped_buf,
386                           &ctx->swapped_buf_size, data_size);
387     if (!ctx->swapped_buf) {
388         av_log(avctx, AV_LOG_ERROR, "Could not allocate swapped buffer.\n");
389         return AVERROR(ENOMEM);
390     }
391
392     /* bswap16 the buffer since CLLC's bitreader works in 16-bit words */
393     ctx->dsp.bswap16_buf((uint16_t *) ctx->swapped_buf, (uint16_t *) src,
394                          data_size / 2);
395
396     init_get_bits(&gb, ctx->swapped_buf, data_size * 8);
397
398     /*
399      * Read in coding type. The types are as follows:
400      *
401      * 0 - YUY2
402      * 1 - BGR24 (Triples)
403      * 2 - BGR24 (Quads)
404      * 3 - BGRA
405      */
406     coding_type = (AV_RL32(src) >> 8) & 0xFF;
407     av_log(avctx, AV_LOG_DEBUG, "Frame coding type: %d\n", coding_type);
408
409     switch (coding_type) {
410     case 0:
411         avctx->pix_fmt             = AV_PIX_FMT_YUV422P;
412         avctx->bits_per_raw_sample = 8;
413
414         ret = ff_get_buffer(avctx, pic, 0);
415         if (ret < 0) {
416             av_log(avctx, AV_LOG_ERROR, "Could not allocate buffer.\n");
417             return ret;
418         }
419
420         ret = decode_yuv_frame(ctx, &gb, pic);
421         if (ret < 0)
422             return ret;
423
424         break;
425     case 1:
426     case 2:
427         avctx->pix_fmt             = AV_PIX_FMT_RGB24;
428         avctx->bits_per_raw_sample = 8;
429
430         ret = ff_get_buffer(avctx, pic, 0);
431         if (ret < 0) {
432             av_log(avctx, AV_LOG_ERROR, "Could not allocate buffer.\n");
433             return ret;
434         }
435
436         ret = decode_rgb24_frame(ctx, &gb, pic);
437         if (ret < 0)
438             return ret;
439
440         break;
441     case 3:
442         avctx->pix_fmt             = AV_PIX_FMT_ARGB;
443         avctx->bits_per_raw_sample = 8;
444
445         ret = ff_get_buffer(avctx, pic, 0);
446         if (ret < 0) {
447             av_log(avctx, AV_LOG_ERROR, "Could not allocate buffer.\n");
448             return ret;
449         }
450
451         ret = decode_argb_frame(ctx, &gb, pic);
452         if (ret < 0)
453             return ret;
454
455         break;
456     default:
457         av_log(avctx, AV_LOG_ERROR, "Unknown coding type: %d.\n", coding_type);
458         return AVERROR_INVALIDDATA;
459     }
460
461     pic->key_frame = 1;
462     pic->pict_type = AV_PICTURE_TYPE_I;
463
464     *got_picture_ptr = 1;
465
466     return avpkt->size;
467 }
468
469 static av_cold int cllc_decode_close(AVCodecContext *avctx)
470 {
471     CLLCContext *ctx = avctx->priv_data;
472
473     av_freep(&ctx->swapped_buf);
474
475     return 0;
476 }
477
478 static av_cold int cllc_decode_init(AVCodecContext *avctx)
479 {
480     CLLCContext *ctx = avctx->priv_data;
481
482     /* Initialize various context values */
483     ctx->avctx            = avctx;
484     ctx->swapped_buf      = NULL;
485     ctx->swapped_buf_size = 0;
486
487     ff_dsputil_init(&ctx->dsp, avctx);
488
489     return 0;
490 }
491
492 AVCodec ff_cllc_decoder = {
493     .name           = "cllc",
494     .type           = AVMEDIA_TYPE_VIDEO,
495     .id             = AV_CODEC_ID_CLLC,
496     .priv_data_size = sizeof(CLLCContext),
497     .init           = cllc_decode_init,
498     .decode         = cllc_decode_frame,
499     .close          = cllc_decode_close,
500     .capabilities   = CODEC_CAP_DR1,
501     .long_name      = NULL_IF_CONFIG_SMALL("Canopus Lossless Codec"),
502 };