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