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