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