FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 <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 
32 #define VLC_BITS 7
33 #define VLC_DEPTH 2
34 
35 
36 typedef struct CLLCContext {
39 
42 } CLLCContext;
43 
45 {
46  uint8_t symbols[256];
47  uint8_t bits[256];
48  uint16_t codes[256];
49  int num_lens, num_codes, num_codes_sum, prefix;
50  int i, j, count;
51 
52  prefix = 0;
53  count = 0;
54  num_codes_sum = 0;
55 
56  num_lens = get_bits(gb, 5);
57 
58  if (num_lens > VLC_BITS * VLC_DEPTH) {
59  vlc->table = NULL;
60 
61  av_log(ctx->avctx, AV_LOG_ERROR, "To long VLCs %d\n", num_lens);
62  return AVERROR_INVALIDDATA;
63  }
64 
65  for (i = 0; i < num_lens; i++) {
66  num_codes = get_bits(gb, 9);
67  num_codes_sum += num_codes;
68 
69  if (num_codes_sum > 256) {
70  vlc->table = NULL;
71 
73  "Too many VLCs (%d) to be read.\n", num_codes_sum);
74  return AVERROR_INVALIDDATA;
75  }
76 
77  for (j = 0; j < num_codes; j++) {
78  symbols[count] = get_bits(gb, 8);
79  bits[count] = i + 1;
80  codes[count] = prefix++;
81 
82  count++;
83  }
84  if (prefix > (65535 - 256)/2) {
85  vlc->table = NULL;
86  return AVERROR_INVALIDDATA;
87  }
88 
89  prefix <<= 1;
90  }
91 
92  return ff_init_vlc_sparse(vlc, VLC_BITS, count, bits, 1, 1,
93  codes, 2, 2, symbols, 1, 1, 0);
94 }
95 
96 /*
97  * Unlike the RGB24 read/restore, which reads in a component at a time,
98  * ARGB read/restore reads in ARGB quads.
99  */
100 static int read_argb_line(CLLCContext *ctx, GetBitContext *gb, int *top_left,
101  VLC *vlc, uint8_t *outbuf)
102 {
103  uint8_t *dst;
104  int pred[4];
105  int code;
106  int i;
107 
108  OPEN_READER(bits, gb);
109 
110  dst = outbuf;
111  pred[0] = top_left[0];
112  pred[1] = top_left[1];
113  pred[2] = top_left[2];
114  pred[3] = top_left[3];
115 
116  for (i = 0; i < ctx->avctx->width; i++) {
117  /* Always get the alpha component */
118  UPDATE_CACHE(bits, gb);
119  GET_VLC(code, bits, gb, vlc[0].table, VLC_BITS, VLC_DEPTH);
120 
121  pred[0] += code;
122  dst[0] = pred[0];
123 
124  /* Skip the components if they are entirely transparent */
125  if (dst[0]) {
126  /* Red */
127  UPDATE_CACHE(bits, gb);
128  GET_VLC(code, bits, gb, vlc[1].table, VLC_BITS, VLC_DEPTH);
129 
130  pred[1] += code;
131  dst[1] = pred[1];
132 
133  /* Green */
134  UPDATE_CACHE(bits, gb);
135  GET_VLC(code, bits, gb, vlc[2].table, VLC_BITS, VLC_DEPTH);
136 
137  pred[2] += code;
138  dst[2] = pred[2];
139 
140  /* Blue */
141  UPDATE_CACHE(bits, gb);
142  GET_VLC(code, bits, gb, vlc[3].table, VLC_BITS, VLC_DEPTH);
143 
144  pred[3] += code;
145  dst[3] = pred[3];
146  } else {
147  dst[1] = 0;
148  dst[2] = 0;
149  dst[3] = 0;
150  }
151 
152  dst += 4;
153  }
154 
155  CLOSE_READER(bits, gb);
156 
157  top_left[0] = outbuf[0];
158 
159  /* Only stash components if they are not transparent */
160  if (top_left[0]) {
161  top_left[1] = outbuf[1];
162  top_left[2] = outbuf[2];
163  top_left[3] = outbuf[3];
164  }
165 
166  return 0;
167 }
168 
170  int *top_left, VLC *vlc, uint8_t *outbuf)
171 {
172  uint8_t *dst;
173  int pred, code;
174  int i;
175 
176  OPEN_READER(bits, gb);
177 
178  dst = outbuf;
179  pred = *top_left;
180 
181  /* Simultaneously read and restore the line */
182  for (i = 0; i < ctx->avctx->width; i++) {
183  UPDATE_CACHE(bits, gb);
184  GET_VLC(code, bits, gb, vlc->table, VLC_BITS, VLC_DEPTH);
185 
186  pred += code;
187  dst[0] = pred;
188  dst += 3;
189  }
190 
191  CLOSE_READER(bits, gb);
192 
193  /* Stash the first pixel */
194  *top_left = outbuf[0];
195 
196  return 0;
197 }
198 
200  int *top_left, VLC *vlc, uint8_t *outbuf,
201  int is_chroma)
202 {
203  int pred, code;
204  int i;
205 
206  OPEN_READER(bits, gb);
207 
208  pred = *top_left;
209 
210  /* Simultaneously read and restore the line */
211  for (i = 0; i < ctx->avctx->width >> is_chroma; i++) {
212  UPDATE_CACHE(bits, gb);
213  GET_VLC(code, bits, gb, vlc->table, VLC_BITS, VLC_DEPTH);
214 
215  pred += code;
216  outbuf[i] = pred;
217  }
218 
219  CLOSE_READER(bits, gb);
220 
221  /* Stash the first pixel */
222  *top_left = outbuf[0];
223 
224  return 0;
225 }
226 
228 {
229  AVCodecContext *avctx = ctx->avctx;
230  uint8_t *dst;
231  int pred[4];
232  int ret;
233  int i, j;
234  VLC vlc[4];
235 
236  pred[0] = 0;
237  pred[1] = 0x80;
238  pred[2] = 0x80;
239  pred[3] = 0x80;
240 
241  dst = pic->data[0];
242 
243  skip_bits(gb, 16);
244 
245  /* Read in code table for each plane */
246  for (i = 0; i < 4; i++) {
247  ret = read_code_table(ctx, gb, &vlc[i]);
248  if (ret < 0) {
249  for (j = 0; j <= i; j++)
250  ff_free_vlc(&vlc[j]);
251 
252  av_log(ctx->avctx, AV_LOG_ERROR,
253  "Could not read code table %d.\n", i);
254  return ret;
255  }
256  }
257 
258  /* Read in and restore every line */
259  for (i = 0; i < avctx->height; i++) {
260  read_argb_line(ctx, gb, pred, vlc, dst);
261 
262  dst += pic->linesize[0];
263  }
264 
265  for (i = 0; i < 4; i++)
266  ff_free_vlc(&vlc[i]);
267 
268  return 0;
269 }
270 
272 {
273  AVCodecContext *avctx = ctx->avctx;
274  uint8_t *dst;
275  int pred[3];
276  int ret;
277  int i, j;
278  VLC vlc[3];
279 
280  pred[0] = 0x80;
281  pred[1] = 0x80;
282  pred[2] = 0x80;
283 
284  dst = pic->data[0];
285 
286  skip_bits(gb, 16);
287 
288  /* Read in code table for each plane */
289  for (i = 0; i < 3; i++) {
290  ret = read_code_table(ctx, gb, &vlc[i]);
291  if (ret < 0) {
292  for (j = 0; j <= i; j++)
293  ff_free_vlc(&vlc[j]);
294 
295  av_log(ctx->avctx, AV_LOG_ERROR,
296  "Could not read code table %d.\n", i);
297  return ret;
298  }
299  }
300 
301  /* Read in and restore every line */
302  for (i = 0; i < avctx->height; i++) {
303  for (j = 0; j < 3; j++)
304  read_rgb24_component_line(ctx, gb, &pred[j], &vlc[j], &dst[j]);
305 
306  dst += pic->linesize[0];
307  }
308 
309  for (i = 0; i < 3; i++)
310  ff_free_vlc(&vlc[i]);
311 
312  return 0;
313 }
314 
316 {
317  AVCodecContext *avctx = ctx->avctx;
318  uint8_t block;
319  uint8_t *dst[3];
320  int pred[3];
321  int ret;
322  int i, j;
323  VLC vlc[2];
324 
325  pred[0] = 0x80;
326  pred[1] = 0x80;
327  pred[2] = 0x80;
328 
329  dst[0] = pic->data[0];
330  dst[1] = pic->data[1];
331  dst[2] = pic->data[2];
332 
333  skip_bits(gb, 8);
334 
335  block = get_bits(gb, 8);
336  if (block) {
337  avpriv_request_sample(ctx->avctx, "Blocked YUV");
338  return AVERROR_PATCHWELCOME;
339  }
340 
341  /* Read in code table for luma and chroma */
342  for (i = 0; i < 2; i++) {
343  ret = read_code_table(ctx, gb, &vlc[i]);
344  if (ret < 0) {
345  for (j = 0; j <= i; j++)
346  ff_free_vlc(&vlc[j]);
347 
348  av_log(ctx->avctx, AV_LOG_ERROR,
349  "Could not read code table %d.\n", i);
350  return ret;
351  }
352  }
353 
354  /* Read in and restore every line */
355  for (i = 0; i < avctx->height; i++) {
356  read_yuv_component_line(ctx, gb, &pred[0], &vlc[0], dst[0], 0); /* Y */
357  read_yuv_component_line(ctx, gb, &pred[1], &vlc[1], dst[1], 1); /* U */
358  read_yuv_component_line(ctx, gb, &pred[2], &vlc[1], dst[2], 1); /* V */
359 
360  for (j = 0; j < 3; j++)
361  dst[j] += pic->linesize[j];
362  }
363 
364  for (i = 0; i < 2; i++)
365  ff_free_vlc(&vlc[i]);
366 
367  return 0;
368 }
369 
370 static int cllc_decode_frame(AVCodecContext *avctx, void *data,
371  int *got_picture_ptr, AVPacket *avpkt)
372 {
373  CLLCContext *ctx = avctx->priv_data;
374  AVFrame *pic = data;
375  uint8_t *src = avpkt->data;
376  uint32_t info_tag, info_offset;
377  int data_size;
378  GetBitContext gb;
379  int coding_type, ret;
380 
381  if (avpkt->size < 4 + 4) {
382  av_log(avctx, AV_LOG_ERROR, "Frame is too small %d.\n", avpkt->size);
383  return AVERROR_INVALIDDATA;
384  }
385 
386  info_offset = 0;
387  info_tag = AV_RL32(src);
388  if (info_tag == MKTAG('I', 'N', 'F', 'O')) {
389  info_offset = AV_RL32(src + 4);
390  if (info_offset > UINT32_MAX - 8 || info_offset + 8 > avpkt->size) {
391  av_log(avctx, AV_LOG_ERROR,
392  "Invalid INFO header offset: 0x%08"PRIX32" is too large.\n",
393  info_offset);
394  return AVERROR_INVALIDDATA;
395  }
396  ff_canopus_parse_info_tag(avctx, src + 8, info_offset);
397 
398  info_offset += 8;
399  src += info_offset;
400  }
401 
402  data_size = (avpkt->size - info_offset) & ~1;
403 
404  /* Make sure our bswap16'd buffer is big enough */
406  &ctx->swapped_buf_size, data_size);
407  if (!ctx->swapped_buf) {
408  av_log(avctx, AV_LOG_ERROR, "Could not allocate swapped buffer.\n");
409  return AVERROR(ENOMEM);
410  }
411 
412  /* bswap16 the buffer since CLLC's bitreader works in 16-bit words */
413  ctx->bdsp.bswap16_buf((uint16_t *) ctx->swapped_buf, (uint16_t *) src,
414  data_size / 2);
415 
416  if ((ret = init_get_bits8(&gb, ctx->swapped_buf, data_size)) < 0)
417  return ret;
418 
419  /*
420  * Read in coding type. The types are as follows:
421  *
422  * 0 - YUY2
423  * 1 - BGR24 (Triples)
424  * 2 - BGR24 (Quads)
425  * 3 - BGRA
426  */
427  coding_type = (AV_RL32(src) >> 8) & 0xFF;
428  av_log(avctx, AV_LOG_DEBUG, "Frame coding type: %d\n", coding_type);
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_get_buffer(avctx, pic, 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_get_buffer(avctx, pic, 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_get_buffer(avctx, pic, 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;
475 
476  *got_picture_ptr = 1;
477 
478  return avpkt->size;
479 }
480 
482 {
483  CLLCContext *ctx = avctx->priv_data;
484 
485  av_freep(&ctx->swapped_buf);
486 
487  return 0;
488 }
489 
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 
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),
512  .close = cllc_decode_close,
513  .capabilities = AV_CODEC_CAP_DR1,
514  .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
515 };
#define NULL
Definition: coverity.c:32
static int read_rgb24_component_line(CLLCContext *ctx, GetBitContext *gb, int *top_left, VLC *vlc, uint8_t *outbuf)
Definition: cllc.c:169
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
This structure describes decoded (raw) audio or video data.
Definition: frame.h:187
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:261
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:64
void(* bswap16_buf)(uint16_t *dst, const uint16_t *src, int len)
Definition: bswapdsp.h:26
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
int ff_canopus_parse_info_tag(AVCodecContext *avctx, const uint8_t *src, size_t size)
Definition: canopus.c:30
int ff_init_vlc_sparse(VLC *vlc_arg, 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:268
int size
Definition: avcodec.h:1658
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1960
void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
Same behaviour av_fast_malloc but the buffer has additional AV_INPUT_BUFFER_PADDING_SIZE at the end w...
Definition: utils.c:120
static int cllc_decode_frame(AVCodecContext *avctx, void *data, int *got_picture_ptr, AVPacket *avpkt)
Definition: cllc.c:370
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
Definition: avcodec.h:3133
#define src
Definition: vp8dsp.c:254
AVCodec.
Definition: avcodec.h:3681
static int16_t block[64]
Definition: dct.c:115
void void avpriv_request_sample(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
#define FF_CODEC_CAP_INIT_THREADSAFE
The codec does not modify any global variables in the init function, allowing to call the init functi...
Definition: internal.h:40
uint8_t bits
Definition: crc.c:296
uint8_t
#define av_cold
Definition: attributes.h:82
uint8_t * data
Definition: avcodec.h:1657
bitstream reader API header.
static av_cold int cllc_decode_close(AVCodecContext *avctx)
Definition: cllc.c:481
#define av_log(a,...)
static int read_code_table(CLLCContext *ctx, GetBitContext *gb, VLC *vlc)
Definition: cllc.c:44
#define UPDATE_CACHE(name, gb)
Definition: get_bits.h:160
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
BswapDSPContext bdsp
Definition: cllc.c:38
#define AVERROR(e)
Definition: error.h:43
static const struct endianess table[]
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:179
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
uint8_t * swapped_buf
Definition: cllc.c:40
const char * name
Name of the codec implementation.
Definition: avcodec.h:3688
int swapped_buf_size
Definition: cllc.c:41
GLsizei count
Definition: opengl_enc.c:109
#define CLOSE_READER(name, gb)
Definition: get_bits.h:131
packed ARGB 8:8:8:8, 32bpp, ARGBARGB...
Definition: pixfmt.h:93
Definition: vlc.h:26
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:66
static int decode_rgb24_frame(CLLCContext *ctx, GetBitContext *gb, AVFrame *pic)
Definition: cllc.c:271
static int decode_argb_frame(CLLCContext *ctx, GetBitContext *gb, AVFrame *pic)
Definition: cllc.c:227
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:261
AVCodecContext * avctx
Definition: cllc.c:37
int width
picture width / height.
Definition: avcodec.h:1919
AVFormatContext * ctx
Definition: movenc.c:48
#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:477
static const float pred[4]
Definition: siprdata.h:259
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
Libavcodec external API header.
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:218
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:456
main external API structure.
Definition: avcodec.h:1732
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: utils.c:953
#define OPEN_READER(name, gb)
Definition: get_bits.h:120
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:306
static int read_yuv_component_line(CLLCContext *ctx, GetBitContext *gb, int *top_left, VLC *vlc, uint8_t *outbuf, int is_chroma)
Definition: cllc.c:199
#define VLC_DEPTH
Definition: cllc.c:33
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:201
static av_cold int cllc_decode_init(AVCodecContext *avctx)
Definition: cllc.c:490
common internal api header.
static int read_argb_line(CLLCContext *ctx, GetBitContext *gb, int *top_left, VLC *vlc, uint8_t *outbuf)
Definition: cllc.c:100
av_cold void ff_bswapdsp_init(BswapDSPContext *c)
Definition: bswapdsp.c:49
void * priv_data
Definition: avcodec.h:1774
VLC_TYPE(* table)[2]
code, bits
Definition: vlc.h:28
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:256
#define av_freep(p)
static int decode(AVCodecContext *avctx, AVFrame *frame, int *got_frame, AVPacket *pkt)
Definition: ffmpeg.c:2257
AVCodec ff_cllc_decoder
Definition: cllc.c:504
#define MKTAG(a, b, c, d)
Definition: common.h:342
static int decode_yuv_frame(CLLCContext *ctx, GetBitContext *gb, AVFrame *pic)
Definition: cllc.c:315
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:87
This structure stores compressed data.
Definition: avcodec.h:1634
void ff_free_vlc(VLC *vlc)
Definition: bitstream.c:354
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:994
#define VLC_BITS
Definition: cllc.c:32