]> git.sesse.net Git - ffmpeg/blob - libavcodec/cllc.c
cllc: Implement ARGB support
[ffmpeg] / libavcodec / cllc.c
1 /*
2  * Canopus Lossless Codec decoder
3  *
4  * Copyright (c) 2012 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
28 typedef struct CLLCContext {
29     DSPContext dsp;
30     AVCodecContext *avctx;
31
32     uint8_t *swapped_buf;
33     int      swapped_buf_size;
34 } CLLCContext;
35
36 static int read_code_table(CLLCContext *ctx, GetBitContext *gb, VLC *vlc)
37 {
38     uint8_t symbols[256];
39     uint8_t bits[256];
40     uint16_t codes[256];
41     int num_lens, num_codes, num_codes_sum, prefix;
42     int i, j, count;
43
44     prefix        = 0;
45     count         = 0;
46     num_codes_sum = 0;
47
48     num_lens = get_bits(gb, 5);
49
50     for (i = 0; i < num_lens; i++) {
51         num_codes      = get_bits(gb, 9);
52         num_codes_sum += num_codes;
53
54         if (num_codes_sum > 256) {
55             vlc->table = NULL;
56
57             av_log(ctx->avctx, AV_LOG_ERROR,
58                    "Too many VLCs (%d) to be read.\n", num_codes_sum);
59             return AVERROR_INVALIDDATA;
60         }
61
62         for (j = 0; j < num_codes; j++) {
63             symbols[count] = get_bits(gb, 8);
64             bits[count]    = i + 1;
65             codes[count]   = prefix++;
66
67             count++;
68         }
69
70         prefix <<= 1;
71     }
72
73     return ff_init_vlc_sparse(vlc, 7, count, bits, 1, 1,
74                               codes, 2, 2, symbols, 1, 1, 0);
75 }
76
77 /*
78  * Unlike the RGB24 read/restore, which reads in a component at a time,
79  * ARGB read/restore reads in ARGB quads.
80  */
81 static int read_argb_line(CLLCContext *ctx, GetBitContext *gb, int *top_left,
82                           VLC *vlc, uint8_t *outbuf)
83 {
84     uint8_t *dst;
85     int pred[4];
86     int code;
87     int i;
88
89     OPEN_READER(bits, gb);
90
91     dst     = outbuf;
92     pred[0] = top_left[0];
93     pred[1] = top_left[1];
94     pred[2] = top_left[2];
95     pred[3] = top_left[3];
96
97     for (i = 0; i < ctx->avctx->width; i++) {
98         /* Always get the alpha component */
99         UPDATE_CACHE(bits, gb);
100         GET_VLC(code, bits, gb, vlc[0].table, 7, 2);
101
102         pred[0] += code;
103         dst[0]   = pred[0];
104
105         /* Skip the components if they are  entirely transparent */
106         if (dst[0]) {
107             /* Red */
108             UPDATE_CACHE(bits, gb);
109             GET_VLC(code, bits, gb, vlc[1].table, 7, 2);
110
111             pred[1] += code;
112             dst[1]   = pred[1];
113
114             /* Green */
115             UPDATE_CACHE(bits, gb);
116             GET_VLC(code, bits, gb, vlc[2].table, 7, 2);
117
118             pred[2] += code;
119             dst[2]   = pred[2];
120
121             /* Blue */
122             UPDATE_CACHE(bits, gb);
123             GET_VLC(code, bits, gb, vlc[3].table, 7, 2);
124
125             pred[3] += code;
126             dst[3]   = pred[3];
127         } else {
128             dst[1] = 0;
129             dst[2] = 0;
130             dst[3] = 0;
131         }
132
133         dst += 4;
134     }
135
136     CLOSE_READER(bits, gb);
137
138     dst         -= 4 * ctx->avctx->width;
139     top_left[0]  = dst[0];
140
141     /* Only stash components if they are not transparent */
142     if (top_left[0]) {
143         top_left[1] = dst[1];
144         top_left[2] = dst[2];
145         top_left[3] = dst[3];
146     }
147
148     return 0;
149 }
150
151 static int read_rgb24_component_line(CLLCContext *ctx, GetBitContext *gb,
152                                      int *top_left, VLC *vlc, uint8_t *outbuf)
153 {
154     uint8_t *dst;
155     int pred, code;
156     int i;
157
158     OPEN_READER(bits, gb);
159
160     dst  = outbuf;
161     pred = *top_left;
162
163     /* Simultaneously read and restore the line */
164     for (i = 0; i < ctx->avctx->width; i++) {
165         UPDATE_CACHE(bits, gb);
166         GET_VLC(code, bits, gb, vlc->table, 7, 2);
167
168         pred  += code;
169         dst[0] = pred;
170         dst   += 3;
171     }
172
173     CLOSE_READER(bits, gb);
174
175     /* Stash the first pixel */
176     *top_left = dst[-3 * ctx->avctx->width];
177
178     return 0;
179 }
180
181 static int decode_argb_frame(CLLCContext *ctx, GetBitContext *gb, AVFrame *pic)
182 {
183     AVCodecContext *avctx = ctx->avctx;
184     uint8_t *dst;
185     int pred[4];
186     int ret;
187     int i, j;
188     VLC vlc[4];
189
190     pred[0] = 0;
191     pred[1] = 0x80;
192     pred[2] = 0x80;
193     pred[3] = 0x80;
194
195     dst = pic->data[0];
196
197     skip_bits(gb, 16);
198
199     /* Read in code table for each plane */
200     for (i = 0; i < 4; i++) {
201         ret = read_code_table(ctx, gb, &vlc[i]);
202         if (ret < 0) {
203             for (j = 0; j <= i; j++)
204                 ff_free_vlc(&vlc[j]);
205
206             av_log(ctx->avctx, AV_LOG_ERROR,
207                    "Could not read code table %d.\n", i);
208             return ret;
209         }
210     }
211
212     /* Read in and restore every line */
213     for (i = 0; i < avctx->height; i++) {
214         read_argb_line(ctx, gb, pred, vlc, dst);
215
216         dst += pic->linesize[0];
217     }
218
219     for (i = 0; i < 4; i++)
220         ff_free_vlc(&vlc[i]);
221
222     return 0;
223 }
224
225 static int decode_rgb24_frame(CLLCContext *ctx, GetBitContext *gb, AVFrame *pic)
226 {
227     AVCodecContext *avctx = ctx->avctx;
228     uint8_t *dst;
229     int pred[3];
230     int ret;
231     int i, j;
232     VLC vlc[3];
233
234     pred[0] = 0x80;
235     pred[1] = 0x80;
236     pred[2] = 0x80;
237
238     dst = pic->data[0];
239
240     skip_bits(gb, 16);
241
242     /* Read in code table for each plane */
243     for (i = 0; i < 3; i++) {
244         ret = read_code_table(ctx, gb, &vlc[i]);
245         if (ret < 0) {
246             for (j = 0; j <= i; j++)
247                 ff_free_vlc(&vlc[j]);
248
249             av_log(ctx->avctx, AV_LOG_ERROR,
250                    "Could not read code table %d.\n", i);
251             return ret;
252         }
253     }
254
255     /* Read in and restore every line */
256     for (i = 0; i < avctx->height; i++) {
257         for (j = 0; j < 3; j++)
258             read_rgb24_component_line(ctx, gb, &pred[j], &vlc[j], &dst[j]);
259
260         dst += pic->linesize[0];
261     }
262
263     for (i = 0; i < 3; i++)
264         ff_free_vlc(&vlc[i]);
265
266     return 0;
267 }
268
269 static int cllc_decode_frame(AVCodecContext *avctx, void *data,
270                              int *got_picture_ptr, AVPacket *avpkt)
271 {
272     CLLCContext *ctx = avctx->priv_data;
273     AVFrame *pic = avctx->coded_frame;
274     uint8_t *src = avpkt->data;
275     uint8_t *swapped_buf_new;
276     uint32_t info_tag, info_offset;
277     GetBitContext gb;
278     int coding_type, ret;
279
280     if (pic->data[0])
281         avctx->release_buffer(avctx, pic);
282
283     pic->reference = 0;
284
285     /* Make sure our bswap16'd buffer is big enough */
286     swapped_buf_new = av_fast_realloc(ctx->swapped_buf,
287                                       &ctx->swapped_buf_size, avpkt->size);
288     if (!swapped_buf_new) {
289         av_log(avctx, AV_LOG_ERROR, "Could not realloc swapped buffer.\n");
290         return AVERROR(ENOMEM);
291     }
292     ctx->swapped_buf = swapped_buf_new;
293
294     /* Skip the INFO header if present */
295     info_offset = 0;
296     info_tag    = AV_RL32(src);
297     if (info_tag == MKTAG('I', 'N', 'F', 'O')) {
298         info_offset = AV_RL32(src + 4);
299         if (info_offset > UINT32_MAX - 8 || info_offset + 8 > avpkt->size) {
300             av_log(avctx, AV_LOG_ERROR,
301                    "Invalid INFO header offset: 0x%08X is too large.\n",
302                    info_offset);
303             return AVERROR_INVALIDDATA;
304         }
305
306         info_offset += 8;
307         src         += info_offset;
308
309         av_log(avctx, AV_LOG_DEBUG, "Skipping INFO chunk.\n");
310     }
311
312     /* bswap16 the buffer since CLLC's bitreader works in 16-bit words */
313     ctx->dsp.bswap16_buf((uint16_t *) ctx->swapped_buf, (uint16_t *) src,
314                          (avpkt->size - info_offset) / 2);
315
316     init_get_bits(&gb, ctx->swapped_buf, (avpkt->size - info_offset) * 8);
317
318     /*
319      * Read in coding type. The types are as follows:
320      *
321      * 0 - YUY2
322      * 1 - BGR24 (Triples)
323      * 2 - BGR24 (Quads)
324      * 3 - BGRA
325      */
326     coding_type = (AV_RL32(src) >> 8) & 0xFF;
327     av_log(avctx, AV_LOG_DEBUG, "Frame coding type: %d\n", coding_type);
328
329     switch (coding_type) {
330     case 1:
331     case 2:
332         avctx->pix_fmt             = PIX_FMT_RGB24;
333         avctx->bits_per_raw_sample = 8;
334
335         ret = avctx->get_buffer(avctx, pic);
336         if (ret < 0) {
337             av_log(avctx, AV_LOG_ERROR, "Could not allocate buffer.\n");
338             return ret;
339         }
340
341         ret = decode_rgb24_frame(ctx, &gb, pic);
342         if (ret < 0)
343             return ret;
344
345         break;
346     case 3:
347         avctx->pix_fmt             = PIX_FMT_ARGB;
348         avctx->bits_per_raw_sample = 8;
349
350         ret = avctx->get_buffer(avctx, pic);
351         if (ret < 0) {
352             av_log(avctx, AV_LOG_ERROR, "Could not allocate buffer.\n");
353             return ret;
354         }
355
356         ret = decode_argb_frame(ctx, &gb, pic);
357         if (ret < 0)
358             return ret;
359
360         break;
361     default:
362         av_log(avctx, AV_LOG_ERROR, "Unknown coding type: %d.\n", coding_type);
363         return AVERROR_INVALIDDATA;
364     }
365
366     pic->key_frame = 1;
367     pic->pict_type = AV_PICTURE_TYPE_I;
368
369     *got_picture_ptr = 1;
370     *(AVFrame *)data = *pic;
371
372     return avpkt->size;
373 }
374
375 static av_cold int cllc_decode_close(AVCodecContext *avctx)
376 {
377     CLLCContext *ctx = avctx->priv_data;
378
379     if (avctx->coded_frame->data[0])
380         avctx->release_buffer(avctx, avctx->coded_frame);
381
382     av_freep(&avctx->coded_frame);
383     av_freep(&ctx->swapped_buf);
384
385     return 0;
386 }
387
388 static av_cold int cllc_decode_init(AVCodecContext *avctx)
389 {
390     CLLCContext *ctx = avctx->priv_data;
391
392     /* Initialize various context values */
393     ctx->avctx            = avctx;
394     ctx->swapped_buf      = NULL;
395     ctx->swapped_buf_size = 0;
396
397     ff_dsputil_init(&ctx->dsp, avctx);
398
399     avctx->coded_frame = avcodec_alloc_frame();
400     if (!avctx->coded_frame) {
401         av_log(avctx, AV_LOG_ERROR, "Could not allocate frame.\n");
402         return AVERROR(ENOMEM);
403     }
404
405     return 0;
406 }
407
408 AVCodec ff_cllc_decoder = {
409     .name           = "cllc",
410     .type           = AVMEDIA_TYPE_VIDEO,
411     .id             = AV_CODEC_ID_CLLC,
412     .priv_data_size = sizeof(CLLCContext),
413     .init           = cllc_decode_init,
414     .decode         = cllc_decode_frame,
415     .close          = cllc_decode_close,
416     .capabilities   = CODEC_CAP_DR1,
417     .long_name      = NULL_IF_CONFIG_SMALL("Canopus Lossless Codec"),
418 };