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