Libav
cllc.c
Go to the documentation of this file.
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 {
32 
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 
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  top_left[0] = outbuf[0];
140 
141  /* Only stash components if they are not transparent */
142  if (top_left[0]) {
143  top_left[1] = outbuf[1];
144  top_left[2] = outbuf[2];
145  top_left[3] = outbuf[3];
146  }
147 
148  return 0;
149 }
150 
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 = outbuf[0];
177 
178  return 0;
179 }
180 
182  int *top_left, VLC *vlc, uint8_t *outbuf,
183  int is_chroma)
184 {
185  int pred, code;
186  int i;
187 
188  OPEN_READER(bits, gb);
189 
190  pred = *top_left;
191 
192  /* Simultaneously read and restore the line */
193  for (i = 0; i < ctx->avctx->width >> is_chroma; i++) {
194  UPDATE_CACHE(bits, gb);
195  GET_VLC(code, bits, gb, vlc->table, 7, 2);
196 
197  pred += code;
198  outbuf[i] = pred;
199  }
200 
201  CLOSE_READER(bits, gb);
202 
203  /* Stash the first pixel */
204  *top_left = outbuf[0];
205 
206  return 0;
207 }
208 
210 {
211  AVCodecContext *avctx = ctx->avctx;
212  uint8_t *dst;
213  int pred[4];
214  int ret;
215  int i, j;
216  VLC vlc[4];
217 
218  pred[0] = 0;
219  pred[1] = 0x80;
220  pred[2] = 0x80;
221  pred[3] = 0x80;
222 
223  dst = pic->data[0];
224 
225  skip_bits(gb, 16);
226 
227  /* Read in code table for each plane */
228  for (i = 0; i < 4; i++) {
229  ret = read_code_table(ctx, gb, &vlc[i]);
230  if (ret < 0) {
231  for (j = 0; j <= i; j++)
232  ff_free_vlc(&vlc[j]);
233 
234  av_log(ctx->avctx, AV_LOG_ERROR,
235  "Could not read code table %d.\n", i);
236  return ret;
237  }
238  }
239 
240  /* Read in and restore every line */
241  for (i = 0; i < avctx->height; i++) {
242  read_argb_line(ctx, gb, pred, vlc, dst);
243 
244  dst += pic->linesize[0];
245  }
246 
247  for (i = 0; i < 4; i++)
248  ff_free_vlc(&vlc[i]);
249 
250  return 0;
251 }
252 
254 {
255  AVCodecContext *avctx = ctx->avctx;
256  uint8_t *dst;
257  int pred[3];
258  int ret;
259  int i, j;
260  VLC vlc[3];
261 
262  pred[0] = 0x80;
263  pred[1] = 0x80;
264  pred[2] = 0x80;
265 
266  dst = pic->data[0];
267 
268  skip_bits(gb, 16);
269 
270  /* Read in code table for each plane */
271  for (i = 0; i < 3; i++) {
272  ret = read_code_table(ctx, gb, &vlc[i]);
273  if (ret < 0) {
274  for (j = 0; j <= i; j++)
275  ff_free_vlc(&vlc[j]);
276 
277  av_log(ctx->avctx, AV_LOG_ERROR,
278  "Could not read code table %d.\n", i);
279  return ret;
280  }
281  }
282 
283  /* Read in and restore every line */
284  for (i = 0; i < avctx->height; i++) {
285  for (j = 0; j < 3; j++)
286  read_rgb24_component_line(ctx, gb, &pred[j], &vlc[j], &dst[j]);
287 
288  dst += pic->linesize[0];
289  }
290 
291  for (i = 0; i < 3; i++)
292  ff_free_vlc(&vlc[i]);
293 
294  return 0;
295 }
296 
298 {
299  AVCodecContext *avctx = ctx->avctx;
300  uint8_t block;
301  uint8_t *dst[3];
302  int pred[3];
303  int ret;
304  int i, j;
305  VLC vlc[2];
306 
307  pred[0] = 0x80;
308  pred[1] = 0x80;
309  pred[2] = 0x80;
310 
311  dst[0] = pic->data[0];
312  dst[1] = pic->data[1];
313  dst[2] = pic->data[2];
314 
315  skip_bits(gb, 8);
316 
317  block = get_bits(gb, 8);
318  if (block) {
319  avpriv_request_sample(ctx->avctx, "Blocked YUV");
320  return AVERROR_PATCHWELCOME;
321  }
322 
323  /* Read in code table for luma and chroma */
324  for (i = 0; i < 2; i++) {
325  ret = read_code_table(ctx, gb, &vlc[i]);
326  if (ret < 0) {
327  for (j = 0; j <= i; j++)
328  ff_free_vlc(&vlc[j]);
329 
330  av_log(ctx->avctx, AV_LOG_ERROR,
331  "Could not read code table %d.\n", i);
332  return ret;
333  }
334  }
335 
336  /* Read in and restore every line */
337  for (i = 0; i < avctx->height; i++) {
338  read_yuv_component_line(ctx, gb, &pred[0], &vlc[0], dst[0], 0); /* Y */
339  read_yuv_component_line(ctx, gb, &pred[1], &vlc[1], dst[1], 1); /* U */
340  read_yuv_component_line(ctx, gb, &pred[2], &vlc[1], dst[2], 1); /* V */
341 
342  for (j = 0; j < 3; j++)
343  dst[j] += pic->linesize[j];
344  }
345 
346  for (i = 0; i < 2; i++)
347  ff_free_vlc(&vlc[i]);
348 
349  return 0;
350 }
351 
352 static int cllc_decode_frame(AVCodecContext *avctx, void *data,
353  int *got_picture_ptr, AVPacket *avpkt)
354 {
355  CLLCContext *ctx = avctx->priv_data;
356  AVFrame *pic = data;
357  uint8_t *src = avpkt->data;
358  uint32_t info_tag, info_offset;
359  int data_size;
360  GetBitContext gb;
361  int coding_type, ret;
362 
363  /* Skip the INFO header if present */
364  info_offset = 0;
365  info_tag = AV_RL32(src);
366  if (info_tag == MKTAG('I', 'N', 'F', 'O')) {
367  info_offset = AV_RL32(src + 4);
368  if (info_offset > UINT32_MAX - 8 || info_offset + 8 > avpkt->size) {
369  av_log(avctx, AV_LOG_ERROR,
370  "Invalid INFO header offset: 0x%08X is too large.\n",
371  info_offset);
372  return AVERROR_INVALIDDATA;
373  }
374 
375  info_offset += 8;
376  src += info_offset;
377 
378  av_log(avctx, AV_LOG_DEBUG, "Skipping INFO chunk.\n");
379  }
380 
381  data_size = (avpkt->size - info_offset) & ~1;
382 
383  /* Make sure our bswap16'd buffer is big enough */
385  &ctx->swapped_buf_size, data_size);
386  if (!ctx->swapped_buf) {
387  av_log(avctx, AV_LOG_ERROR, "Could not allocate swapped buffer.\n");
388  return AVERROR(ENOMEM);
389  }
390 
391  /* bswap16 the buffer since CLLC's bitreader works in 16-bit words */
392  ctx->dsp.bswap16_buf((uint16_t *) ctx->swapped_buf, (uint16_t *) src,
393  data_size / 2);
394 
395  init_get_bits(&gb, ctx->swapped_buf, data_size * 8);
396 
397  /*
398  * Read in coding type. The types are as follows:
399  *
400  * 0 - YUY2
401  * 1 - BGR24 (Triples)
402  * 2 - BGR24 (Quads)
403  * 3 - BGRA
404  */
405  coding_type = (AV_RL32(src) >> 8) & 0xFF;
406  av_log(avctx, AV_LOG_DEBUG, "Frame coding type: %d\n", coding_type);
407 
408  switch (coding_type) {
409  case 0:
410  avctx->pix_fmt = AV_PIX_FMT_YUV422P;
411  avctx->bits_per_raw_sample = 8;
412 
413  ret = ff_get_buffer(avctx, pic, 0);
414  if (ret < 0) {
415  av_log(avctx, AV_LOG_ERROR, "Could not allocate buffer.\n");
416  return ret;
417  }
418 
419  ret = decode_yuv_frame(ctx, &gb, pic);
420  if (ret < 0)
421  return ret;
422 
423  break;
424  case 1:
425  case 2:
426  avctx->pix_fmt = AV_PIX_FMT_RGB24;
427  avctx->bits_per_raw_sample = 8;
428 
429  ret = ff_get_buffer(avctx, pic, 0);
430  if (ret < 0) {
431  av_log(avctx, AV_LOG_ERROR, "Could not allocate buffer.\n");
432  return ret;
433  }
434 
435  ret = decode_rgb24_frame(ctx, &gb, pic);
436  if (ret < 0)
437  return ret;
438 
439  break;
440  case 3:
441  avctx->pix_fmt = AV_PIX_FMT_ARGB;
442  avctx->bits_per_raw_sample = 8;
443 
444  ret = ff_get_buffer(avctx, pic, 0);
445  if (ret < 0) {
446  av_log(avctx, AV_LOG_ERROR, "Could not allocate buffer.\n");
447  return ret;
448  }
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;
462 
463  *got_picture_ptr = 1;
464 
465  return avpkt->size;
466 }
467 
469 {
470  CLLCContext *ctx = avctx->priv_data;
471 
472  av_freep(&ctx->swapped_buf);
473 
474  return 0;
475 }
476 
478 {
479  CLLCContext *ctx = avctx->priv_data;
480 
481  /* Initialize various context values */
482  ctx->avctx = avctx;
483  ctx->swapped_buf = NULL;
484  ctx->swapped_buf_size = 0;
485 
486  ff_dsputil_init(&ctx->dsp, avctx);
487 
488  return 0;
489 }
490 
492  .name = "cllc",
493  .long_name = NULL_IF_CONFIG_SMALL("Canopus Lossless Codec"),
494  .type = AVMEDIA_TYPE_VIDEO,
495  .id = AV_CODEC_ID_CLLC,
496  .priv_data_size = sizeof(CLLCContext),
500  .capabilities = CODEC_CAP_DR1,
501 };
av_cold void ff_dsputil_init(DSPContext *c, AVCodecContext *avctx)
Definition: dsputil.c:2440
static int read_rgb24_component_line(CLLCContext *ctx, GetBitContext *gb, int *top_left, VLC *vlc, uint8_t *outbuf)
Definition: cllc.c:151
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
This structure describes decoded (raw) audio or video data.
Definition: frame.h:107
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:240
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:67
int size
Definition: avcodec.h:974
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1247
void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
Allocate a buffer with padding, reusing the given one if large enough.
Definition: utils.c:70
static int cllc_decode_frame(AVCodecContext *avctx, void *data, int *got_picture_ptr, AVPacket *avpkt)
Definition: cllc.c:352
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
Definition: avcodec.h:2488
AVCodec.
Definition: avcodec.h:2755
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:198
static int decode(MimicContext *ctx, int quality, int num_coeffs, int is_iframe)
Definition: mimic.c:269
void void avpriv_request_sample(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
int ff_init_vlc_sparse(VLC *vlc, int nb_bits, int nb_codes, const void *bits, int bits_wrap, int bits_size, const void *codes, int codes_wrap, int codes_size, const void *symbols, int symbols_wrap, int symbols_size, int flags)
Definition: bitstream.c:266
uint8_t bits
Definition: crc.c:216
uint8_t
#define av_cold
Definition: attributes.h:66
#define CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:711
const char data[16]
Definition: mxf.c:66
uint8_t * data
Definition: avcodec.h:973
bitstream reader API header.
static av_cold int cllc_decode_close(AVCodecContext *avctx)
Definition: cllc.c:468
static int read_code_table(CLLCContext *ctx, GetBitContext *gb, VLC *vlc)
Definition: cllc.c:37
#define UPDATE_CACHE(name, gb)
Definition: get_bits.h:161
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:123
void(* bswap16_buf)(uint16_t *dst, const uint16_t *src, int len)
Definition: dsputil.h:203
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:142
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:144
uint8_t * swapped_buf
Definition: cllc.c:33
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:148
const char * name
Name of the codec implementation.
Definition: avcodec.h:2762
int swapped_buf_size
Definition: cllc.c:34
#define CLOSE_READER(name, gb)
Definition: get_bits.h:141
packed ARGB 8:8:8:8, 32bpp, ARGBARGB...
Definition: pixfmt.h:95
Definition: get_bits.h:64
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:69
static int decode_rgb24_frame(CLLCContext *ctx, GetBitContext *gb, AVFrame *pic)
Definition: cllc.c:253
static int decode_argb_frame(CLLCContext *ctx, GetBitContext *gb, AVFrame *pic)
Definition: cllc.c:209
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:168
AVCodecContext * avctx
Definition: cllc.c:31
int width
picture width / height.
Definition: avcodec.h:1217
#define AV_RL32
Definition: intreadwrite.h:146
#define GET_VLC(code, name, gb, table, bits, max_depth)
If the vlc code is invalid and max_depth=1, then no bits will be removed.
Definition: get_bits.h:456
static const float pred[4]
Definition: siprdata.h:259
#define AVERROR_PATCHWELCOME
Not yet implemented in Libav, patches welcome.
Definition: error.h:57
NULL
Definition: eval.c:55
DSPContext dsp
Definition: cllc.c:30
Libavcodec external API header.
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:125
main external API structure.
Definition: avcodec.h:1054
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:489
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: utils.c:575
#define OPEN_READER(name, gb)
Definition: get_bits.h:127
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:263
static int read_yuv_component_line(CLLCContext *ctx, GetBitContext *gb, int *top_left, VLC *vlc, uint8_t *outbuf, int is_chroma)
Definition: cllc.c:181
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:375
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:113
static av_cold int cllc_decode_init(AVCodecContext *avctx)
Definition: cllc.c:477
common internal api header.
static av_cold int init(AVCodecParserContext *s)
Definition: h264_parser.c:498
static int read_argb_line(CLLCContext *ctx, GetBitContext *gb, int *top_left, VLC *vlc, uint8_t *outbuf)
Definition: cllc.c:82
DSP utils.
void * priv_data
Definition: avcodec.h:1090
VLC_TYPE(* table)[2]
code, bits
Definition: get_bits.h:66
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:163
AVCodec ff_cllc_decoder
Definition: cllc.c:491
#define MKTAG(a, b, c, d)
Definition: common.h:238
static int decode_yuv_frame(CLLCContext *ctx, GetBitContext *gb, AVFrame *pic)
Definition: cllc.c:297
This structure stores compressed data.
Definition: avcodec.h:950
void ff_free_vlc(VLC *vlc)
Definition: bitstream.c:333
DSPContext.
Definition: dsputil.h:124
static int16_t block[64]
Definition: dct-test.c:170