cllc.c
Go to the documentation of this file.
1 /*
2  * Canopus Lossless Codec decoder
3  *
4  * Copyright (c) 2012 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  dst -= 4 * ctx->avctx->width;
140  top_left[0] = dst[0];
141 
142  /* Only stash components if they are not transparent */
143  if (top_left[0]) {
144  top_left[1] = dst[1];
145  top_left[2] = dst[2];
146  top_left[3] = dst[3];
147  }
148 
149  return 0;
150 }
151 
153  int *top_left, VLC *vlc, uint8_t *outbuf)
154 {
155  uint8_t *dst;
156  int pred, code;
157  int i;
158 
159  OPEN_READER(bits, gb);
160 
161  dst = outbuf;
162  pred = *top_left;
163 
164  /* Simultaneously read and restore the line */
165  for (i = 0; i < ctx->avctx->width; i++) {
166  UPDATE_CACHE(bits, gb);
167  GET_VLC(code, bits, gb, vlc->table, 7, 2);
168 
169  pred += code;
170  dst[0] = pred;
171  dst += 3;
172  }
173 
174  CLOSE_READER(bits, gb);
175 
176  /* Stash the first pixel */
177  *top_left = dst[-3 * ctx->avctx->width];
178 
179  return 0;
180 }
181 
183 {
184  AVCodecContext *avctx = ctx->avctx;
185  uint8_t *dst;
186  int pred[4];
187  int ret;
188  int i, j;
189  VLC vlc[4];
190 
191  pred[0] = 0;
192  pred[1] = 0x80;
193  pred[2] = 0x80;
194  pred[3] = 0x80;
195 
196  dst = pic->data[0];
197 
198  skip_bits(gb, 16);
199 
200  /* Read in code table for each plane */
201  for (i = 0; i < 4; i++) {
202  ret = read_code_table(ctx, gb, &vlc[i]);
203  if (ret < 0) {
204  for (j = 0; j <= i; j++)
205  ff_free_vlc(&vlc[j]);
206 
207  av_log(ctx->avctx, AV_LOG_ERROR,
208  "Could not read code table %d.\n", i);
209  return ret;
210  }
211  }
212 
213  /* Read in and restore every line */
214  for (i = 0; i < avctx->height; i++) {
215  read_argb_line(ctx, gb, pred, vlc, dst);
216 
217  dst += pic->linesize[0];
218  }
219 
220  for (i = 0; i < 4; i++)
221  ff_free_vlc(&vlc[i]);
222 
223  return 0;
224 }
225 
227 {
228  AVCodecContext *avctx = ctx->avctx;
229  uint8_t *dst;
230  int pred[3];
231  int ret;
232  int i, j;
233  VLC vlc[3];
234 
235  pred[0] = 0x80;
236  pred[1] = 0x80;
237  pred[2] = 0x80;
238 
239  dst = pic->data[0];
240 
241  skip_bits(gb, 16);
242 
243  /* Read in code table for each plane */
244  for (i = 0; i < 3; i++) {
245  ret = read_code_table(ctx, gb, &vlc[i]);
246  if (ret < 0) {
247  for (j = 0; j <= i; j++)
248  ff_free_vlc(&vlc[j]);
249 
250  av_log(ctx->avctx, AV_LOG_ERROR,
251  "Could not read code table %d.\n", i);
252  return ret;
253  }
254  }
255 
256  /* Read in and restore every line */
257  for (i = 0; i < avctx->height; i++) {
258  for (j = 0; j < 3; j++)
259  read_rgb24_component_line(ctx, gb, &pred[j], &vlc[j], &dst[j]);
260 
261  dst += pic->linesize[0];
262  }
263 
264  for (i = 0; i < 3; i++)
265  ff_free_vlc(&vlc[i]);
266 
267  return 0;
268 }
269 
271  int *got_picture_ptr, AVPacket *avpkt)
272 {
273  CLLCContext *ctx = avctx->priv_data;
274  AVFrame *pic = data;
275  uint8_t *src = avpkt->data;
276  uint32_t info_tag, info_offset;
277  int data_size;
278  GetBitContext gb;
279  int coding_type, ret;
280 
281  /* Skip the INFO header if present */
282  info_offset = 0;
283  info_tag = AV_RL32(src);
284  if (info_tag == MKTAG('I', 'N', 'F', 'O')) {
285  info_offset = AV_RL32(src + 4);
286  if (info_offset > UINT32_MAX - 8 || info_offset + 8 > avpkt->size) {
287  av_log(avctx, AV_LOG_ERROR,
288  "Invalid INFO header offset: 0x%08X is too large.\n",
289  info_offset);
290  return AVERROR_INVALIDDATA;
291  }
292 
293  info_offset += 8;
294  src += info_offset;
295 
296  av_log(avctx, AV_LOG_DEBUG, "Skipping INFO chunk.\n");
297  }
298 
299  data_size = (avpkt->size - info_offset) & ~1;
300 
301  /* Make sure our bswap16'd buffer is big enough */
303  &ctx->swapped_buf_size, data_size);
304  if (!ctx->swapped_buf) {
305  av_log(avctx, AV_LOG_ERROR, "Could not allocate swapped buffer.\n");
306  return AVERROR(ENOMEM);
307  }
308 
309  /* bswap16 the buffer since CLLC's bitreader works in 16-bit words */
310  ctx->dsp.bswap16_buf((uint16_t *) ctx->swapped_buf, (uint16_t *) src,
311  data_size / 2);
312 
313  init_get_bits(&gb, ctx->swapped_buf, data_size * 8);
314 
315  /*
316  * Read in coding type. The types are as follows:
317  *
318  * 0 - YUY2
319  * 1 - BGR24 (Triples)
320  * 2 - BGR24 (Quads)
321  * 3 - BGRA
322  */
323  coding_type = (AV_RL32(src) >> 8) & 0xFF;
324  av_log(avctx, AV_LOG_DEBUG, "Frame coding type: %d\n", coding_type);
325 
326  switch (coding_type) {
327  case 1:
328  case 2:
329  avctx->pix_fmt = AV_PIX_FMT_RGB24;
330  avctx->bits_per_raw_sample = 8;
331 
332  if ((ret = ff_get_buffer(avctx, pic, 0)) < 0)
333  return ret;
334 
335  ret = decode_rgb24_frame(ctx, &gb, pic);
336  if (ret < 0)
337  return ret;
338 
339  break;
340  case 3:
341  avctx->pix_fmt = AV_PIX_FMT_ARGB;
342  avctx->bits_per_raw_sample = 8;
343 
344  if ((ret = ff_get_buffer(avctx, pic, 0)) < 0)
345  return ret;
346 
347  ret = decode_argb_frame(ctx, &gb, pic);
348  if (ret < 0)
349  return ret;
350 
351  break;
352  default:
353  av_log(avctx, AV_LOG_ERROR, "Unknown coding type: %d.\n", coding_type);
354  return AVERROR_INVALIDDATA;
355  }
356 
357  pic->key_frame = 1;
359 
360  *got_picture_ptr = 1;
361 
362  return avpkt->size;
363 }
364 
366 {
367  CLLCContext *ctx = avctx->priv_data;
368 
369  av_freep(&ctx->swapped_buf);
370 
371  return 0;
372 }
373 
375 {
376  CLLCContext *ctx = avctx->priv_data;
377 
378  /* Initialize various context values */
379  ctx->avctx = avctx;
380  ctx->swapped_buf = NULL;
381  ctx->swapped_buf_size = 0;
382 
383  ff_dsputil_init(&ctx->dsp, avctx);
384 
385  return 0;
386 }
387 
389  .name = "cllc",
390  .type = AVMEDIA_TYPE_VIDEO,
391  .id = AV_CODEC_ID_CLLC,
392  .priv_data_size = sizeof(CLLCContext),
396  .capabilities = CODEC_CAP_DR1,
397  .long_name = NULL_IF_CONFIG_SMALL("Canopus Lossless Codec"),
398 };
av_cold void ff_dsputil_init(DSPContext *c, AVCodecContext *avctx)
Definition: dsputil.c:2675
static int read_rgb24_component_line(CLLCContext *ctx, GetBitContext *gb, int *top_left, VLC *vlc, uint8_t *outbuf)
Definition: cllc.c:152
#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:76
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:70
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
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...
static int cllc_decode_frame(AVCodecContext *avctx, void *data, int *got_picture_ptr, AVPacket *avpkt)
Definition: cllc.c:270
void(* bswap16_buf)(uint16_t *dst, const uint16_t *src, int len)
Definition: dsputil.h:209
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
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
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:262
uint8_t bits
Definition: crc.c:216
uint8_t
#define av_cold
Definition: attributes.h:78
#define CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
uint8_t * data
bitstream reader API header.
static av_cold int cllc_decode_close(AVCodecContext *avctx)
Definition: cllc.c:365
static int read_code_table(CLLCContext *ctx, GetBitContext *gb, VLC *vlc)
Definition: cllc.c:37
#define UPDATE_CACHE(name, gb)
Definition: get_bits.h:160
static const struct endianess table[]
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Spectrum Plot time data
uint8_t * swapped_buf
Definition: cllc.c:33
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:246
const char * name
Name of the codec implementation.
int swapped_buf_size
Definition: cllc.c:34
#define CLOSE_READER(name, gb)
Definition: get_bits.h:140
external API header
packed ARGB 8:8:8:8, 32bpp, ARGBARGB...
Definition: pixfmt.h:96
Definition: get_bits.h:63
static int decode_rgb24_frame(CLLCContext *ctx, GetBitContext *gb, AVFrame *pic)
Definition: cllc.c:226
static int decode_argb_frame(CLLCContext *ctx, GetBitContext *gb, AVFrame *pic)
Definition: cllc.c:182
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:144
AVCodecContext * avctx
Definition: cllc.c:31
ret
Definition: avfilter.c:821
int width
picture width / height.
#define AV_RL32
#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:458
static const float pred[4]
Definition: siprdata.h:259
NULL
Definition: eval.c:55
or the Software in violation of any applicable export control laws in any jurisdiction Except as provided by mandatorily applicable UPF has no obligation to provide you with source code to the Software In the event Software contains any source code
DSPContext dsp
Definition: cllc.c:30
AVS_Value src
Definition: avisynth_c.h:523
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:101
main external API structure.
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:375
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:148
#define OPEN_READER(name, gb)
Definition: get_bits.h:126
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:265
synthesis window for stochastic i
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:379
struct CLLCContext CLLCContext
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFilterBuffer structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later.That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another.Buffer references ownership and permissions
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:87
static av_cold int cllc_decode_init(AVCodecContext *avctx)
Definition: cllc.c:374
common internal api header.
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:162
static int read_argb_line(CLLCContext *ctx, GetBitContext *gb, int *top_left, VLC *vlc, uint8_t *outbuf)
Definition: cllc.c:82
DSP utils.
else dst[i][x+y *dst_stride[i]]
Definition: vf_mcdeint.c:160
VLC_TYPE(* table)[2]
code, bits
Definition: get_bits.h:65
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:139
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:388
#define MKTAG(a, b, c, d)
Definition: common.h:282
This structure stores compressed data.
void ff_free_vlc(VLC *vlc)
Definition: bitstream.c:344
DSPContext.
Definition: dsputil.h:127