FFmpeg  2.1.1
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 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 "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  if ((ret = ff_get_buffer(avctx, pic, 0)) < 0)
414  return ret;
415 
416  ret = decode_yuv_frame(ctx, &gb, pic);
417  if (ret < 0)
418  return ret;
419 
420  break;
421  case 1:
422  case 2:
423  avctx->pix_fmt = AV_PIX_FMT_RGB24;
424  avctx->bits_per_raw_sample = 8;
425 
426  if ((ret = ff_get_buffer(avctx, pic, 0)) < 0)
427  return ret;
428 
429  ret = decode_rgb24_frame(ctx, &gb, pic);
430  if (ret < 0)
431  return ret;
432 
433  break;
434  case 3:
435  avctx->pix_fmt = AV_PIX_FMT_ARGB;
436  avctx->bits_per_raw_sample = 8;
437 
438  if ((ret = ff_get_buffer(avctx, pic, 0)) < 0)
439  return ret;
440 
441  ret = decode_argb_frame(ctx, &gb, pic);
442  if (ret < 0)
443  return ret;
444 
445  break;
446  default:
447  av_log(avctx, AV_LOG_ERROR, "Unknown coding type: %d.\n", coding_type);
448  return AVERROR_INVALIDDATA;
449  }
450 
451  pic->key_frame = 1;
453 
454  *got_picture_ptr = 1;
455 
456  return avpkt->size;
457 }
458 
460 {
461  CLLCContext *ctx = avctx->priv_data;
462 
463  av_freep(&ctx->swapped_buf);
464 
465  return 0;
466 }
467 
469 {
470  CLLCContext *ctx = avctx->priv_data;
471 
472  /* Initialize various context values */
473  ctx->avctx = avctx;
474  ctx->swapped_buf = NULL;
475  ctx->swapped_buf_size = 0;
476 
477  ff_dsputil_init(&ctx->dsp, avctx);
478 
479  return 0;
480 }
481 
483  .name = "cllc",
484  .long_name = NULL_IF_CONFIG_SMALL("Canopus Lossless Codec"),
485  .type = AVMEDIA_TYPE_VIDEO,
486  .id = AV_CODEC_ID_CLLC,
487  .priv_data_size = sizeof(CLLCContext),
491  .capabilities = CODEC_CAP_DR1,
492 };
av_cold void ff_dsputil_init(DSPContext *c, AVCodecContext *avctx)
Definition: dsputil.c:2678
static int read_rgb24_component_line(CLLCContext *ctx, GetBitContext *gb, int *top_left, VLC *vlc, uint8_t *outbuf)
Definition: cllc.c:151
#define AVERROR_PATCHWELCOME
This structure describes decoded (raw) audio or video data.
Definition: frame.h:96
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:255
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
static struct endianess table[]
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: avcodec.h:4536
planar YUV 4:2:2, 16bpp, (1 Cr &amp; Cb sample per 2x1 Y samples)
Definition: avcodec.h:4538
int size
Definition: avcodec.h:1064
void av_log(void *avcl, int level, const char *fmt,...) av_printf_format(3
Send the specified message to the log if the level is less than or equal to the current av_log_level...
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1342
void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
Same behaviour av_fast_malloc but the buffer has additional FF_INPUT_BUFFER_PADDING_SIZE at the end w...
Definition: utils.c:158
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:2570
AVCodec.
Definition: avcodec.h:2922
#define av_cold
Definition: avcodec.h:653
void av_freep(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:234
packed ARGB 8:8:8:8, 32bpp, ARGBARGB...
Definition: avcodec.h:4562
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:265
uint8_t bits
Definition: crc.c:260
uint8_t
const char * name
Name of the codec implementation.
Definition: avcodec.h:2929
#define CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:742
const char data[16]
Definition: mxf.c:68
bitstream reader API header.
static av_cold int cllc_decode_close(AVCodecContext *avctx)
Definition: cllc.c:459
static int read_code_table(CLLCContext *ctx, GetBitContext *gb, VLC *vlc)
Definition: cllc.c:37
#define UPDATE_CACHE(name, gb)
Definition: get_bits.h:170
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: avcodec.h:4147
void(* bswap16_buf)(uint16_t *dst, const uint16_t *src, int len)
Definition: dsputil.h:206
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:151
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: avcodec.h:4168
uint8_t * swapped_buf
Definition: cllc.c:33
int swapped_buf_size
Definition: cllc.c:34
#define CLOSE_READER(name, gb)
Definition: get_bits.h:141
Libavcodec external API header.
Definition: get_bits.h:63
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:167
AVCodecContext * avctx
Definition: cllc.c:31
ret
Definition: avfilter.c:961
int width
picture width / height.
Definition: avcodec.h:1314
#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:484
static const float pred[4]
Definition: siprdata.h:259
DSPContext dsp
Definition: cllc.c:30
AVS_Value src
Definition: avisynth_c.h:523
main external API structure.
Definition: avcodec.h:1146
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:538
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: utils.c:941
#define OPEN_READER(name, gb)
Definition: get_bits.h:127
#define MKTAG(a, b, c, d)
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:292
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
uint8_t * data
Definition: avcodec.h:1063
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:405
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:124
void * priv_data
Definition: avcodec.h:1182
static av_cold int cllc_decode_init(AVCodecContext *avctx)
Definition: cllc.c:468
common internal api header.
static int read_argb_line(CLLCContext *ctx, GetBitContext *gb, int *top_left, VLC *vlc, uint8_t *outbuf)
Definition: cllc.c:82
DSP utils.
#define AVERROR_INVALIDDATA
#define AV_RL32(x)
Definition: intreadwrite.h:275
VLC_TYPE(* table)[2]
code, bits
Definition: get_bits.h:65
int key_frame
1 -&gt; keyframe, 0-&gt; not
Definition: frame.h:162
#define AVERROR(e)
void INT64 INT64 count
Definition: avisynth_c.h:594
static int decode(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: crystalhd.c:868
AVCodec ff_cllc_decoder
Definition: cllc.c:482
static int decode_yuv_frame(CLLCContext *ctx, GetBitContext *gb, AVFrame *pic)
Definition: cllc.c:297
This structure stores compressed data.
Definition: avcodec.h:1040
void ff_free_vlc(VLC *vlc)
Definition: bitstream.c:353
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:107
DSPContext.
Definition: dsputil.h:124
static int16_t block[64]
Definition: dct-test.c:198