FFmpeg  4.0
clearvideo.c
Go to the documentation of this file.
1 /*
2  * ClearVideo decoder
3  * Copyright (c) 2012-2018 Konstantin Shishkov
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 /**
23  * @file
24  * ClearVideo decoder
25  */
26 
27 #include "avcodec.h"
28 #include "bytestream.h"
29 #include "get_bits.h"
30 #include "idctdsp.h"
31 #include "internal.h"
32 #include "mathops.h"
33 #include "clearvideodata.h"
34 
35 typedef struct LevelCodes {
36  uint16_t mv_esc;
37  uint16_t bias_esc;
41 } LevelCodes;
42 
43 typedef struct MV {
44  int16_t x, y;
45 } MV;
46 
47 static const MV zero_mv = { 0 };
48 
49 typedef struct MVInfo {
50  int mb_w;
51  int mb_h;
52  int mb_size;
53  int mb_stride;
54  int top;
55  MV *mv;
56 } MVInfo;
57 
58 typedef struct TileInfo {
59  uint16_t flags;
60  int16_t bias;
61  MV mv;
62  struct TileInfo *child[4];
63 } TileInfo;
64 
65 typedef struct CLVContext {
71  int mb_width, mb_height;
72  int pmb_width, pmb_height;
74  int tile_size;
76  VLC dc_vlc, ac_vlc;
77  LevelCodes ylev[4], ulev[3], vlev[3];
78  int luma_dc_quant, chroma_dc_quant, ac_quant;
79  DECLARE_ALIGNED(16, int16_t, block)[64];
80  int top_dc[3], left_dc[4];
81 } CLVContext;
82 
83 static inline int decode_block(CLVContext *ctx, int16_t *blk, int has_ac,
84  int ac_quant)
85 {
86  GetBitContext *gb = &ctx->gb;
87  int idx = 1, last = 0, val, skip;
88 
89  memset(blk, 0, sizeof(*blk) * 64);
90  blk[0] = get_vlc2(gb, ctx->dc_vlc.table, 9, 3);
91  if (blk[0] < 0)
92  return AVERROR_INVALIDDATA;
93  blk[0] -= 63;
94 
95  if (!has_ac)
96  return 0;
97 
98  while (idx < 64 && !last) {
99  val = get_vlc2(gb, ctx->ac_vlc.table, 9, 2);
100  if (val < 0)
101  return AVERROR_INVALIDDATA;
102  if (val != 0x1BFF) {
103  last = val >> 12;
104  skip = (val >> 4) & 0xFF;
105  val &= 0xF;
106  if (get_bits1(gb))
107  val = -val;
108  } else {
109  last = get_bits1(gb);
110  skip = get_bits(gb, 6);
111  val = get_sbits(gb, 8);
112  }
113  if (val) {
114  int aval = FFABS(val), sign = val < 0;
115  val = ac_quant * (2 * aval + 1);
116  if (!(ac_quant & 1))
117  val--;
118  if (sign)
119  val = -val;
120  }
121  idx += skip;
122  if (idx >= 64)
123  return AVERROR_INVALIDDATA;
124  blk[ff_zigzag_direct[idx++]] = val;
125  }
126 
127  return (idx <= 64 && last) ? 0 : -1;
128 }
129 
130 #define DCT_TEMPLATE(blk, step, bias, shift, dshift, OP) \
131  const int t0 = OP(2841 * blk[1 * step] + 565 * blk[7 * step]); \
132  const int t1 = OP( 565 * blk[1 * step] - 2841 * blk[7 * step]); \
133  const int t2 = OP(1609 * blk[5 * step] + 2408 * blk[3 * step]); \
134  const int t3 = OP(2408 * blk[5 * step] - 1609 * blk[3 * step]); \
135  const int t4 = OP(1108 * blk[2 * step] - 2676 * blk[6 * step]); \
136  const int t5 = OP(2676 * blk[2 * step] + 1108 * blk[6 * step]); \
137  const int t6 = ((blk[0 * step] + blk[4 * step]) * (1 << dshift)) + bias; \
138  const int t7 = ((blk[0 * step] - blk[4 * step]) * (1 << dshift)) + bias; \
139  const int t8 = t0 + t2; \
140  const int t9 = t0 - t2; \
141  const int tA = (int)(181U * (t9 + (t1 - t3)) + 0x80) >> 8; \
142  const int tB = (int)(181U * (t9 - (t1 - t3)) + 0x80) >> 8; \
143  const int tC = t1 + t3; \
144  \
145  blk[0 * step] = (t6 + t5 + t8) >> shift; \
146  blk[1 * step] = (t7 + t4 + tA) >> shift; \
147  blk[2 * step] = (t7 - t4 + tB) >> shift; \
148  blk[3 * step] = (t6 - t5 + tC) >> shift; \
149  blk[4 * step] = (t6 - t5 - tC) >> shift; \
150  blk[5 * step] = (t7 - t4 - tB) >> shift; \
151  blk[6 * step] = (t7 + t4 - tA) >> shift; \
152  blk[7 * step] = (t6 + t5 - t8) >> shift; \
153 
154 #define ROP(x) x
155 #define COP(x) (((x) + 4) >> 3)
156 
157 static void clv_dct(int16_t *block)
158 {
159  int i;
160  int16_t *ptr;
161 
162  ptr = block;
163  for (i = 0; i < 8; i++) {
164  DCT_TEMPLATE(ptr, 1, 0x80, 8, 11, ROP);
165  ptr += 8;
166  }
167 
168  ptr = block;
169  for (i = 0; i < 8; i++) {
170  DCT_TEMPLATE(ptr, 8, 0x2000, 14, 8, COP);
171  ptr++;
172  }
173 }
174 
175 static int decode_mb(CLVContext *c, int x, int y)
176 {
177  int i, has_ac[6], off;
178 
179  for (i = 0; i < 6; i++)
180  has_ac[i] = get_bits1(&c->gb);
181 
182  off = x * 16 + y * 16 * c->pic->linesize[0];
183  for (i = 0; i < 4; i++) {
184  if (decode_block(c, c->block, has_ac[i], c->ac_quant) < 0)
185  return AVERROR_INVALIDDATA;
186  if (!x && !(i & 1)) {
187  c->block[0] += c->top_dc[0];
188  c->top_dc[0] = c->block[0];
189  } else {
190  c->block[0] += c->left_dc[(i & 2) >> 1];
191  }
192  c->left_dc[(i & 2) >> 1] = c->block[0];
193  c->block[0] *= c->luma_dc_quant;
194  clv_dct(c->block);
195  if (i == 2)
196  off += c->pic->linesize[0] * 8;
198  c->pic->data[0] + off + (i & 1) * 8,
199  c->pic->linesize[0]);
200  }
201 
202  off = x * 8 + y * 8 * c->pic->linesize[1];
203  for (i = 1; i < 3; i++) {
204  if (decode_block(c, c->block, has_ac[i + 3], c->ac_quant) < 0)
205  return AVERROR_INVALIDDATA;
206  if (!x) {
207  c->block[0] += c->top_dc[i];
208  c->top_dc[i] = c->block[0];
209  } else {
210  c->block[0] += c->left_dc[i + 1];
211  }
212  c->left_dc[i + 1] = c->block[0];
213  c->block[0] *= c->chroma_dc_quant;
214  clv_dct(c->block);
215  c->idsp.put_pixels_clamped(c->block, c->pic->data[i] + off,
216  c->pic->linesize[i]);
217  }
218 
219  return 0;
220 }
221 
222 static int copy_block(AVCodecContext *avctx, AVFrame *dst, AVFrame *src,
223  int plane, int x, int y, int dx, int dy, int size)
224 {
225  int shift = plane > 0;
226  int sx = x + dx;
227  int sy = y + dy;
228  int sstride, dstride, soff, doff;
229  uint8_t *sbuf, *dbuf;
230  int i;
231 
232  if (x < 0 || sx < 0 || y < 0 || sy < 0 ||
233  x + size > avctx->coded_width >> shift ||
234  y + size > avctx->coded_height >> shift ||
235  sx + size > avctx->coded_width >> shift ||
236  sy + size > avctx->coded_height >> shift)
237  return AVERROR_INVALIDDATA;
238 
239  sstride = src->linesize[plane];
240  dstride = dst->linesize[plane];
241  soff = sx + sy * sstride;
242  sbuf = src->data[plane];
243  doff = x + y * dstride;
244  dbuf = dst->data[plane];
245 
246  for (i = 0; i < size; i++) {
247  uint8_t *dptr = &dbuf[doff];
248  uint8_t *sptr = &sbuf[soff];
249 
250  memcpy(dptr, sptr, size);
251  doff += dstride;
252  soff += sstride;
253  }
254 
255  return 0;
256 }
257 
258 static int copyadd_block(AVCodecContext *avctx, AVFrame *dst, AVFrame *src,
259  int plane, int x, int y, int dx, int dy, int size, int bias)
260 {
261  int shift = plane > 0;
262  int sx = x + dx;
263  int sy = y + dy;
264  int sstride = src->linesize[plane];
265  int dstride = dst->linesize[plane];
266  int soff = sx + sy * sstride;
267  uint8_t *sbuf = src->data[plane];
268  int doff = x + y * dstride;
269  uint8_t *dbuf = dst->data[plane];
270  int i, j;
271 
272  if (x < 0 || sx < 0 || y < 0 || sy < 0 ||
273  x + size > avctx->coded_width >> shift ||
274  y + size > avctx->coded_height >> shift ||
275  sx + size > avctx->coded_width >> shift ||
276  sy + size > avctx->coded_height >> shift)
277  return AVERROR_INVALIDDATA;
278 
279  for (j = 0; j < size; j++) {
280  uint8_t *dptr = &dbuf[doff];
281  uint8_t *sptr = &sbuf[soff];
282 
283  for (i = 0; i < size; i++) {
284  int val = sptr[i] + bias;
285 
286  dptr[i] = av_clip_uint8(val);
287  }
288 
289  doff += dstride;
290  soff += sstride;
291  }
292 
293  return 0;
294 }
295 
296 static MV mvi_predict(MVInfo *mvi, int mb_x, int mb_y, MV diff)
297 {
298  MV res, pred_mv;
299  int left_mv, right_mv, top_mv, bot_mv;
300 
301  if (mvi->top) {
302  if (mb_x > 0) {
303  pred_mv = mvi->mv[mvi->mb_stride + mb_x - 1];
304  } else {
305  pred_mv = zero_mv;
306  }
307  } else if ((mb_x == 0) || (mb_x == mvi->mb_w - 1)) {
308  pred_mv = mvi->mv[mb_x];
309  } else {
310  MV A = mvi->mv[mvi->mb_stride + mb_x - 1];
311  MV B = mvi->mv[ mb_x ];
312  MV C = mvi->mv[ mb_x + 1];
313  pred_mv.x = mid_pred(A.x, B.x, C.x);
314  pred_mv.y = mid_pred(A.y, B.y, C.y);
315  }
316 
317  res = pred_mv;
318 
319  left_mv = -((mb_x * mvi->mb_size));
320  right_mv = ((mvi->mb_w - mb_x - 1) * mvi->mb_size);
321  if (res.x < left_mv) {
322  res.x = left_mv;
323  }
324  if (res.x > right_mv) {
325  res.x = right_mv;
326  }
327  top_mv = -((mb_y * mvi->mb_size));
328  bot_mv = ((mvi->mb_h - mb_y - 1) * mvi->mb_size);
329  if (res.y < top_mv) {
330  res.y = top_mv;
331  }
332  if (res.y > bot_mv) {
333  res.y = bot_mv;
334  }
335 
336  mvi->mv[mvi->mb_stride + mb_x].x = res.x + diff.x;
337  mvi->mv[mvi->mb_stride + mb_x].y = res.y + diff.y;
338 
339  return res;
340 }
341 
342 static void mvi_reset(MVInfo *mvi, int mb_w, int mb_h, int mb_size)
343 {
344  mvi->top = 1;
345  mvi->mb_w = mb_w;
346  mvi->mb_h = mb_h;
347  mvi->mb_size = mb_size;
348  mvi->mb_stride = mb_w;
349  memset(mvi->mv, 0, sizeof(MV) * mvi->mb_stride * 2);
350 }
351 
352 static void mvi_update_row(MVInfo *mvi)
353 {
354  int i;
355 
356  mvi->top = 0;
357  for (i = 0 ; i < mvi->mb_stride; i++) {
358  mvi->mv[i] = mvi->mv[mvi->mb_stride + i];
359  }
360 }
361 
363 {
364  TileInfo *ti;
365  int i, flags = 0;
366  int16_t bias = 0;
367  MV mv = { 0 };
368 
369  if (lc[level].flags_cb.table) {
370  flags = get_vlc2(gb, lc[level].flags_cb.table, lc[level].flags_cb.bits, 2);
371  }
372 
373  if (lc[level].mv_cb.table) {
374  uint16_t mv_code = get_vlc2(gb, lc[level].mv_cb.table, lc[level].mv_cb.bits, 3);
375 
376  if (mv_code != lc[level].mv_esc) {
377  mv.x = (int8_t)(mv_code & 0xff);
378  mv.y = (int8_t)(mv_code >> 8);
379  } else {
380  mv.x = get_sbits(gb, 8);
381  mv.y = get_sbits(gb, 8);
382  }
383  }
384 
385  if (lc[level].bias_cb.table) {
386  uint16_t bias_val = get_vlc2(gb, lc[level].bias_cb.table, lc[level].bias_cb.bits, 2);
387 
388  if (bias_val != lc[level].bias_esc) {
389  bias = (int16_t)(bias_val);
390  } else {
391  bias = get_sbits(gb, 16);
392  }
393  }
394 
395  ti = av_calloc(1, sizeof(*ti));
396  if (!ti)
397  return NULL;
398 
399  ti->flags = flags;
400  ti->mv = mv;
401  ti->bias = bias;
402 
403  if (ti->flags) {
404  for (i = 0; i < 4; i++) {
405  if (ti->flags & (1 << i)) {
406  TileInfo *subti = decode_tile_info(gb, lc, level + 1);
407  ti->child[i] = subti;
408  }
409  }
410  }
411 
412  return ti;
413 }
414 
415 static int tile_do_block(AVCodecContext *avctx, AVFrame *dst, AVFrame *src,
416  int plane, int x, int y, int dx, int dy, int size, int bias)
417 {
418  int ret;
419 
420  if (!bias) {
421  ret = copy_block(avctx, dst, src, plane, x, y, dx, dy, size);
422  } else {
423  ret = copyadd_block(avctx, dst, src, plane, x, y, dx, dy, size, bias);
424  }
425 
426  return ret;
427 }
428 
429 static int restore_tree(AVCodecContext *avctx, AVFrame *dst, AVFrame *src,
430  int plane, int x, int y, int size,
431  TileInfo *tile, MV root_mv)
432 {
433  int ret;
434  MV mv;
435 
436  mv.x = root_mv.x + tile->mv.x;
437  mv.y = root_mv.y + tile->mv.y;
438 
439  if (!tile->flags) {
440  ret = tile_do_block(avctx, dst, src, plane, x, y, mv.x, mv.y, size, tile->bias);
441  } else {
442  int i, hsize = size >> 1;
443 
444  for (i = 0; i < 4; i++) {
445  int xoff = (i & 2) == 0 ? 0 : hsize;
446  int yoff = (i & 1) == 0 ? 0 : hsize;
447 
448  if (tile->child[i]) {
449  ret = restore_tree(avctx, dst, src, plane, x + xoff, y + yoff, hsize, tile->child[i], root_mv);
450  av_freep(&tile->child[i]);
451  } else {
452  ret = tile_do_block(avctx, dst, src, plane, x + xoff, y + yoff, mv.x, mv.y, hsize, tile->bias);
453  }
454  }
455  }
456 
457  return ret;
458 }
459 
460 static void extend_edges(AVFrame *buf, int tile_size)
461 {
462  int comp, i, j;
463 
464  for (comp = 0; comp < 3; comp++) {
465  int shift = comp > 0;
466  int w = buf->width >> shift;
467  int h = buf->height >> shift;
468  int size = comp == 0 ? tile_size : tile_size >> 1;
469  int stride = buf->linesize[comp];
470  uint8_t *framebuf = buf->data[comp];
471 
472  int right = size - (w & (size - 1));
473  int bottom = size - (h & (size - 1));
474 
475  if ((right == size) && (bottom == size)) {
476  return;
477  }
478  if (right != size) {
479  int off = w;
480  for (j = 0; j < h; j++) {
481  for (i = 0; i < right; i++) {
482  framebuf[off + i] = 0x80;
483  }
484  off += stride;
485  }
486  }
487  if (bottom != size) {
488  int off = h * stride;
489  for (j = 0; j < bottom; j++) {
490  for (i = 0; i < stride; i++) {
491  framebuf[off + i] = 0x80;
492  }
493  off += stride;
494  }
495  }
496  }
497 }
498 
499 static int clv_decode_frame(AVCodecContext *avctx, void *data,
500  int *got_frame, AVPacket *avpkt)
501 {
502  const uint8_t *buf = avpkt->data;
503  int buf_size = avpkt->size;
504  CLVContext *c = avctx->priv_data;
505  GetByteContext gb;
506  uint32_t frame_type;
507  int i, j, ret;
508  int mb_ret = 0;
509 
510  bytestream2_init(&gb, buf, buf_size);
511  if (avctx->codec_tag == MKTAG('C', 'L', 'V', '1')) {
512  int skip = bytestream2_get_byte(&gb);
513  bytestream2_skip(&gb, (skip + 1) * 8);
514  }
515 
516  frame_type = bytestream2_get_byte(&gb);
517 
518  if ((frame_type & 0x7f) == 0x30) {
519  if ((ret = ff_reget_buffer(avctx, c->pic)) < 0)
520  return ret;
521 
522  c->pic->key_frame = 0;
524  } else if (frame_type & 0x2) {
525  if (buf_size < c->mb_width * c->mb_height) {
526  av_log(avctx, AV_LOG_ERROR, "Packet too small\n");
527  return AVERROR_INVALIDDATA;
528  }
529 
530  if ((ret = ff_reget_buffer(avctx, c->pic)) < 0)
531  return ret;
532 
533  c->pic->key_frame = 1;
535 
536  bytestream2_get_be32(&gb); // frame size;
537  c->ac_quant = bytestream2_get_byte(&gb);
538  c->luma_dc_quant = 32;
539  c->chroma_dc_quant = 32;
540 
541  if ((ret = init_get_bits8(&c->gb, buf + bytestream2_tell(&gb),
542  buf_size - bytestream2_tell(&gb))) < 0)
543  return ret;
544 
545  for (i = 0; i < 3; i++)
546  c->top_dc[i] = 32;
547  for (i = 0; i < 4; i++)
548  c->left_dc[i] = 32;
549 
550  for (j = 0; j < c->mb_height; j++) {
551  for (i = 0; i < c->mb_width; i++) {
552  ret = decode_mb(c, i, j);
553  if (ret < 0)
554  mb_ret = ret;
555  }
556  }
557  extend_edges(c->pic, c->tile_size);
558  } else {
559  int plane;
560 
561  if ((ret = ff_reget_buffer(avctx, c->pic)) < 0)
562  return ret;
563 
564  ret = av_frame_copy(c->pic, c->prev);
565  if (ret < 0)
566  return ret;
567 
568  if ((ret = init_get_bits8(&c->gb, buf + bytestream2_tell(&gb),
569  buf_size - bytestream2_tell(&gb))) < 0)
570  return ret;
571 
572  mvi_reset(&c->mvi, c->pmb_width, c->pmb_height, 1 << c->tile_shift);
573 
574  for (j = 0; j < c->pmb_height; j++) {
575  for (i = 0; i < c->pmb_width; i++) {
576  if (get_bits1(&c->gb)) {
577  MV mv = mvi_predict(&c->mvi, i, j, zero_mv);
578 
579  for (plane = 0; plane < 3; plane++) {
580  int16_t x = plane == 0 ? i << c->tile_shift : i << (c->tile_shift - 1);
581  int16_t y = plane == 0 ? j << c->tile_shift : j << (c->tile_shift - 1);
582  int16_t size = plane == 0 ? 1 << c->tile_shift : 1 << (c->tile_shift - 1);
583  int16_t mx = plane == 0 ? mv.x : mv.x / 2;
584  int16_t my = plane == 0 ? mv.y : mv.y / 2;
585 
586  ret = copy_block(avctx, c->pic, c->prev, plane, x, y, mx, my, size);
587  if (ret < 0)
588  mb_ret = ret;
589  }
590  } else {
591  int x = i << c->tile_shift;
592  int y = j << c->tile_shift;
593  int size = 1 << c->tile_shift;
594  TileInfo *tile;
595  MV mv, cmv;
596 
597  tile = decode_tile_info(&c->gb, c->ylev, 0);
598  if (!tile)
599  return AVERROR(ENOMEM);
600  mv = mvi_predict(&c->mvi, i, j, tile->mv);
601  ret = restore_tree(avctx, c->pic, c->prev, 0, x, y, size, tile, mv);
602  if (ret < 0)
603  mb_ret = ret;
604  x = i << (c->tile_shift - 1);
605  y = j << (c->tile_shift - 1);
606  size = 1 << (c->tile_shift - 1);
607  cmv.x = mv.x + tile->mv.x;
608  cmv.y = mv.y + tile->mv.y;
609  cmv.x /= 2;
610  cmv.y /= 2;
611  av_freep(&tile);
612  tile = decode_tile_info(&c->gb, c->ulev, 0);
613  if (!tile)
614  return AVERROR(ENOMEM);
615  ret = restore_tree(avctx, c->pic, c->prev, 1, x, y, size, tile, cmv);
616  if (ret < 0)
617  mb_ret = ret;
618  av_freep(&tile);
619  tile = decode_tile_info(&c->gb, c->vlev, 0);
620  if (!tile)
621  return AVERROR(ENOMEM);
622  ret = restore_tree(avctx, c->pic, c->prev, 2, x, y, size, tile, cmv);
623  if (ret < 0)
624  mb_ret = ret;
625  av_freep(&tile);
626  }
627  }
628  mvi_update_row(&c->mvi);
629  }
630  extend_edges(c->pic, c->tile_size);
631 
632  c->pic->key_frame = 0;
634  }
635 
636  if ((ret = av_frame_ref(data, c->pic)) < 0)
637  return ret;
638 
639  FFSWAP(AVFrame *, c->pic, c->prev);
640 
641  *got_frame = 1;
642 
643  return mb_ret < 0 ? mb_ret : buf_size;
644 }
645 
647 {
648  CLVContext *const c = avctx->priv_data;
649  int ret, w, h;
650 
651  if (avctx->extradata_size == 110) {
652  c->tile_size = AV_RL32(&avctx->extradata[94]);
653  } else if (avctx->extradata_size == 150) {
654  c->tile_size = AV_RB32(&avctx->extradata[134]);
655  } else if (!avctx->extradata_size) {
656  c->tile_size = 16;
657  } else {
658  av_log(avctx, AV_LOG_ERROR, "Unsupported extradata size: %d\n", avctx->extradata_size);
659  return AVERROR_INVALIDDATA;
660  }
661 
662  c->tile_shift = av_log2(c->tile_size);
663  if (1 << c->tile_shift != c->tile_size) {
664  av_log(avctx, AV_LOG_ERROR, "Tile size: %d, is not power of 2.\n", c->tile_size);
665  return AVERROR_INVALIDDATA;
666  }
667 
668  avctx->pix_fmt = AV_PIX_FMT_YUV420P;
669  w = avctx->width;
670  h = avctx->height;
671  ret = ff_set_dimensions(avctx, FFALIGN(w, 1 << c->tile_shift), FFALIGN(h, 1 << c->tile_shift));
672  if (ret < 0)
673  return ret;
674  avctx->width = w;
675  avctx->height = h;
676 
677  c->avctx = avctx;
678  c->mb_width = FFALIGN(avctx->width, 16) >> 4;
679  c->mb_height = FFALIGN(avctx->height, 16) >> 4;
680  c->pmb_width = (w + c->tile_size - 1) >> c->tile_shift;
681  c->pmb_height = (h + c->tile_size - 1) >> c->tile_shift;
682  c->pic = av_frame_alloc();
683  c->prev = av_frame_alloc();
684  c->mvi.mv = av_calloc(c->pmb_width * 2, sizeof(*c->mvi.mv));
685  if (!c->pic || !c->prev || !c->mvi.mv)
686  return AVERROR(ENOMEM);
687 
688  ff_idctdsp_init(&c->idsp, avctx);
689  ret = init_vlc(&c->dc_vlc, 9, NUM_DC_CODES,
690  clv_dc_bits, 1, 1,
691  clv_dc_codes, 1, 1, 0);
692  if (ret) {
693  av_log(avctx, AV_LOG_ERROR, "Error initialising DC VLC\n");
694  return ret;
695  }
696  ret = ff_init_vlc_sparse(&c->ac_vlc, 9, NUM_AC_CODES,
697  clv_ac_bits, 1, 1,
698  clv_ac_codes, 1, 1,
699  clv_ac_syms, 2, 2, 0);
700  if (ret) {
701  av_log(avctx, AV_LOG_ERROR, "Error initialising AC VLC\n");
702  return ret;
703  }
704 
706  clv_flagsy_0_bits, 1, 1,
707  clv_flagsy_0_codes, 2, 2, 0);
708  if (ret)
709  return ret;
710 
712  clv_flagsy_1_bits, 1, 1,
713  clv_flagsy_1_codes, 2, 2, 0);
714  if (ret)
715  return ret;
716 
718  clv_flagsy_2_bits, 1, 1,
719  clv_flagsy_2_codes, 2, 2, 0);
720  if (ret)
721  return ret;
722 
724  clv_flagsu_0_bits, 1, 1,
725  clv_flagsu_0_codes, 2, 2, 0);
726  if (ret)
727  return ret;
728 
730  clv_flagsu_1_bits, 1, 1,
731  clv_flagsu_1_codes, 2, 2, 0);
732  if (ret)
733  return ret;
734 
736  clv_flagsv_0_bits, 1, 1,
737  clv_flagsv_0_codes, 2, 2, 0);
738  if (ret)
739  return ret;
740 
742  clv_flagsv_1_bits, 1, 1,
743  clv_flagsv_1_codes, 2, 2, 0);
744  if (ret)
745  return ret;
746 
748  clv_mvy_0_bits, 1, 1,
749  clv_mvy_0_codes, 2, 2,
750  clv_mvy_0_syms, 2, 2, 0);
751  if (ret)
752  return ret;
753 
755  clv_mvy_1_bits, 1, 1,
756  clv_mvy_1_codes, 2, 2,
757  clv_mvy_1_syms, 2, 2, 0);
758  if (ret)
759  return ret;
760 
762  clv_mvy_2_bits, 1, 1,
763  clv_mvy_2_codes, 2, 2,
764  clv_mvy_2_syms, 2, 2, 0);
765  if (ret)
766  return ret;
767 
769  clv_mvy_3_bits, 1, 1,
770  clv_mvy_3_codes, 2, 2,
771  clv_mvy_3_syms, 2, 2, 0);
772  if (ret)
773  return ret;
774 
776  clv_mvu_1_bits, 1, 1,
777  clv_mvu_1_codes, 2, 2,
778  clv_mvu_1_syms, 2, 2, 0);
779  if (ret)
780  return ret;
781 
783  clv_mvu_2_bits, 1, 1,
784  clv_mvu_2_codes, 2, 2,
785  clv_mvu_2_syms, 2, 2, 0);
786  if (ret)
787  return ret;
788 
790  clv_mvv_1_bits, 1, 1,
791  clv_mvv_1_codes, 2, 2,
792  clv_mvv_1_syms, 2, 2, 0);
793  if (ret)
794  return ret;
795 
797  clv_mvv_2_bits, 1, 1,
798  clv_mvv_2_codes, 2, 2,
799  clv_mvv_2_syms, 2, 2, 0);
800  if (ret)
801  return ret;
802 
804  clv_biasy_1_bits, 1, 1,
805  clv_biasy_1_codes, 2, 2,
806  clv_biasy_1_syms, 2, 2, 0);
807  if (ret)
808  return ret;
809 
811  clv_biasy_2_bits, 1, 1,
812  clv_biasy_2_codes, 2, 2,
813  clv_biasy_2_syms, 2, 2, 0);
814  if (ret)
815  return ret;
816 
818  clv_biasy_3_bits, 1, 1,
819  clv_biasy_3_codes, 2, 2,
820  clv_biasy_3_syms, 2, 2, 0);
821  if (ret)
822  return ret;
823 
825  clv_biasu_1_bits, 1, 1,
826  clv_biasu_1_codes, 2, 2,
827  clv_biasu_1_syms, 2, 2, 0);
828  if (ret)
829  return ret;
830 
832  clv_biasu_2_bits, 1, 1,
833  clv_biasu_2_codes, 2, 2,
834  clv_biasu_2_syms, 2, 2, 0);
835  if (ret)
836  return ret;
837 
839  clv_biasv_1_bits, 1, 1,
840  clv_biasv_1_codes, 2, 2,
841  clv_biasv_1_syms, 2, 2, 0);
842  if (ret)
843  return ret;
844 
846  clv_biasv_2_bits, 1, 1,
847  clv_biasv_2_codes, 2, 2,
848  clv_biasv_2_syms, 2, 2, 0);
849  if (ret)
850  return ret;
851 
852  c->ylev[0].mv_esc = 0x0909;
853  c->ylev[1].mv_esc = 0x0A0A;
854  c->ylev[2].mv_esc = 0x1010;
855  c->ylev[3].mv_esc = 0x1313;
856  c->ulev[1].mv_esc = 0x0808;
857  c->ulev[2].mv_esc = 0x0B0B;
858  c->vlev[1].mv_esc = 0x0808;
859  c->vlev[2].mv_esc = 0x0B0B;
860 
861  c->ylev[1].bias_esc = 0x100;
862  c->ylev[2].bias_esc = 0x100;
863  c->ylev[3].bias_esc = 0x100;
864  c->ulev[1].bias_esc = 0x100;
865  c->ulev[2].bias_esc = 0x100;
866  c->vlev[1].bias_esc = 0x100;
867  c->vlev[2].bias_esc = 0x100;
868 
869  return 0;
870 }
871 
873 {
874  CLVContext *const c = avctx->priv_data;
875  int i;
876 
877  av_frame_free(&c->prev);
878  av_frame_free(&c->pic);
879 
880  av_freep(&c->mvi.mv);
881 
882  ff_free_vlc(&c->dc_vlc);
883  ff_free_vlc(&c->ac_vlc);
884  for (i = 0; i < 4; i++) {
885  ff_free_vlc(&c->ylev[i].mv_cb);
886  ff_free_vlc(&c->ylev[i].flags_cb);
887  ff_free_vlc(&c->ylev[i].bias_cb);
888  }
889  for (i = 0; i < 3; i++) {
890  ff_free_vlc(&c->ulev[i].mv_cb);
891  ff_free_vlc(&c->ulev[i].flags_cb);
892  ff_free_vlc(&c->ulev[i].bias_cb);
893  ff_free_vlc(&c->vlev[i].mv_cb);
894  ff_free_vlc(&c->vlev[i].flags_cb);
895  ff_free_vlc(&c->vlev[i].bias_cb);
896  }
897 
898  return 0;
899 }
900 
902  .name = "clearvideo",
903  .long_name = NULL_IF_CONFIG_SMALL("Iterated Systems ClearVideo"),
904  .type = AVMEDIA_TYPE_VIDEO,
906  .priv_data_size = sizeof(CLVContext),
908  .close = clv_decode_end,
910  .capabilities = AV_CODEC_CAP_DR1,
912 };
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
Definition: internal.h:48
int plane
Definition: avisynth_c.h:422
#define NULL
Definition: coverity.c:32
const char const char void * val
Definition: avisynth_c.h:771
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
static void mvi_reset(MVInfo *mvi, int mb_w, int mb_h, int mb_size)
Definition: clearvideo.c:342
VLC ac_vlc
Definition: clearvideo.c:76
static int shift(int a, int b)
Definition: sonic.c:82
static av_cold int clv_decode_init(AVCodecContext *avctx)
Definition: clearvideo.c:646
int size
int mb_size
Definition: clearvideo.c:52
This structure describes decoded (raw) audio or video data.
Definition: frame.h:218
int ac_quant
Definition: clearvideo.c:78
static const uint16_t clv_mvu_2_syms[]
AVFrame * prev
Definition: clearvideo.c:69
#define C
static const uint16_t clv_biasy_2_syms[]
static const uint16_t clv_biasv_2_syms[]
int coded_width
Bitstream width / height, may be different from width/height e.g.
Definition: avcodec.h:1705
VLC flags_cb
Definition: clearvideo.c:38
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:269
static const uint16_t clv_biasv_1_codes[]
MVInfo mvi
Definition: clearvideo.c:73
int ff_set_dimensions(AVCodecContext *s, int width, int height)
Check that the provided frame dimensions are valid and set them on the codec context.
Definition: utils.c:104
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
static const uint8_t clv_flagsy_1_bits[]
void(* put_pixels_clamped)(const int16_t *block, uint8_t *av_restrict pixels, ptrdiff_t line_size)
Definition: idctdsp.h:55
int pmb_height
Definition: clearvideo.c:72
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
VLC dc_vlc
Definition: clearvideo.c:76
int size
Definition: avcodec.h:1431
int16_t bias
Definition: clearvideo.c:60
static const uint8_t clv_mvy_3_bits[]
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1727
static const uint16_t clv_ac_syms[NUM_AC_CODES]
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:133
LevelCodes ylev[4]
Definition: clearvideo.c:77
static const uint16_t clv_biasu_1_syms[]
static const uint8_t clv_mvv_2_bits[]
static const MV zero_mv
Definition: clearvideo.c:47
static const uint8_t clv_flagsy_2_bits[]
static const uint16_t clv_mvv_2_syms[]
static const uint8_t clv_flagsy_0_bits[]
#define init_vlc(vlc, nb_bits, nb_codes, bits, bits_wrap, bits_size, codes, codes_wrap, codes_size, flags)
Definition: vlc.h:38
static void mvi_update_row(MVInfo *mvi)
Definition: clearvideo.c:352
#define blk(i)
Definition: sha.c:185
#define src
Definition: vp8dsp.c:254
int stride
Definition: mace.c:144
AVCodec.
Definition: avcodec.h:3408
static int get_sbits(GetBitContext *s, int n)
Definition: get_bits.h:254
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
Definition: decode_audio.c:42
int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame)
Identical in function to av_frame_make_writable(), except it uses ff_get_buffer() to allocate the buf...
Definition: decode.c:1938
static const uint8_t clv_ac_bits[NUM_AC_CODES]
void * av_calloc(size_t nmemb, size_t size)
Non-inlined equivalent of av_mallocz_array().
Definition: mem.c:244
static void extend_edges(AVFrame *buf, int tile_size)
Definition: clearvideo.c:460
static int16_t block[64]
Definition: dct.c:115
static const uint8_t clv_flagsu_0_bits[]
int top
Definition: clearvideo.c:54
static av_cold int clv_decode_end(AVCodecContext *avctx)
Definition: clearvideo.c:872
#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
static const uint8_t clv_flagsu_1_bits[]
static const uint8_t clv_dc_bits[NUM_DC_CODES]
uint8_t
#define av_cold
Definition: attributes.h:82
static const uint16_t clv_biasu_2_codes[]
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:189
AVCodecContext * avctx
Definition: clearvideo.c:66
uint16_t bias_esc
Definition: clearvideo.c:37
static int decode_mb(CLVContext *c, int x, int y)
Definition: clearvideo.c:175
AVS_FilterInfo AVS_Value child
Definition: avisynth_c.h:731
#define AV_RB32
Definition: intreadwrite.h:130
static const uint16_t clv_mvu_2_codes[]
static const uint16_t clv_biasy_3_syms[]
static const uint16_t clv_flagsu_0_codes[]
int av_frame_ref(AVFrame *dst, const AVFrame *src)
Set up a new reference to the data described by the source frame.
Definition: frame.c:441
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1618
static const uint16_t clv_biasy_1_codes[]
const char data[16]
Definition: mxf.c:90
int mb_height
Definition: clearvideo.c:71
#define DECLARE_ALIGNED(n, t, v)
Declare a variable that is aligned in memory.
Definition: mem.h:112
uint8_t * data
Definition: avcodec.h:1430
static const uint16_t clv_flagsy_2_codes[]
int16_t x
Definition: clearvideo.c:44
static int tile_do_block(AVCodecContext *avctx, AVFrame *dst, AVFrame *src, int plane, int x, int y, int dx, int dy, int size, int bias)
Definition: clearvideo.c:415
static int flags
Definition: log.c:55
bitstream reader API header.
static void clv_dct(int16_t *block)
Definition: clearvideo.c:157
int16_t block[64]
Definition: clearvideo.c:79
static const uint16_t clv_mvy_1_codes[]
static int restore_tree(AVCodecContext *avctx, AVFrame *dst, AVFrame *src, int plane, int x, int y, int size, TileInfo *tile, MV root_mv)
Definition: clearvideo.c:429
#define A(x)
Definition: vp56_arith.h:28
#define FFALIGN(x, a)
Definition: macros.h:48
#define av_log(a,...)
#define ROP(x)
Definition: clearvideo.c:154
struct TileInfo * child[4]
Definition: clearvideo.c:62
static const uint8_t clv_biasy_3_bits[]
static const uint8_t clv_ac_codes[NUM_AC_CODES]
static const uint8_t clv_biasy_2_bits[]
static const uint8_t clv_mvu_2_bits[]
int width
Definition: frame.h:276
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
#define AVERROR(e)
Definition: error.h:43
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition: bytestream.h:164
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:202
#define B
Definition: huffyuvdsp.h:32
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:186
static void pred_mv(DiracBlock *block, int stride, int x, int y, int ref)
Definition: diracdec.c:1357
static const uint8_t clv_biasy_1_bits[]
#define NUM_DC_CODES
const char * name
Name of the codec implementation.
Definition: avcodec.h:3415
static const uint8_t clv_biasv_2_bits[]
LevelCodes ulev[3]
Definition: clearvideo.c:77
static const uint16_t clv_mvy_0_codes[]
static const uint16_t clv_biasy_1_syms[]
Definition: clearvideo.c:43
int av_frame_copy(AVFrame *dst, const AVFrame *src)
Copy the frame data from src to dst.
Definition: frame.c:790
Definition: vlc.h:26
static const uint8_t clv_mvy_1_bits[]
static const uint16_t clv_mvy_2_codes[]
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:301
static const uint8_t clv_biasv_1_bits[]
int width
picture width / height.
Definition: avcodec.h:1690
static int clv_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: clearvideo.c:499
uint8_t w
Definition: llviddspenc.c:38
LevelCodes vlev[3]
Definition: clearvideo.c:77
static const uint16_t clv_mvv_2_codes[]
AVFormatContext * ctx
Definition: movenc.c:48
GetBitContext gb
Definition: clearvideo.c:70
int mb_width
Definition: clearvideo.c:71
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:72
static av_always_inline int get_vlc2(GetBitContext *s, VLC_TYPE(*table)[2], int bits, int max_depth)
Parse a vlc code.
Definition: get_bits.h:563
#define AV_RL32
Definition: intreadwrite.h:146
static const uint16_t clv_mvy_2_syms[]
static const uint16_t clv_biasu_1_codes[]
int pmb_width
Definition: clearvideo.c:72
static int decode_block(CLVContext *ctx, int16_t *blk, int has_ac, int ac_quant)
Definition: clearvideo.c:83
int mb_h
Definition: clearvideo.c:51
#define FF_ARRAY_ELEMS(a)
#define av_log2
Definition: intmath.h:83
int bits
Definition: vlc.h:27
static void comp(unsigned char *dst, ptrdiff_t dst_stride, unsigned char *src, ptrdiff_t src_stride, int add)
Definition: eamad.c:83
static const uint8_t clv_flagsv_0_bits[]
static const int8_t mv[256][2]
Definition: 4xm.c:77
static av_always_inline int bytestream2_tell(GetByteContext *g)
Definition: bytestream.h:188
#define DCT_TEMPLATE(blk, step, bias, shift, dshift, OP)
Definition: clearvideo.c:130
static const uint16_t clv_flagsy_0_codes[]
static MV mvi_predict(MVInfo *mvi, int mb_x, int mb_y, MV diff)
Definition: clearvideo.c:296
static const uint16_t clv_mvy_0_syms[]
Libavcodec external API header.
static const uint8_t clv_dc_codes[NUM_DC_CODES]
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:249
int tile_shift
Definition: clearvideo.c:75
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:464
int top_dc[3]
Definition: clearvideo.c:80
main external API structure.
Definition: avcodec.h:1518
int mb_stride
Definition: clearvideo.c:53
MV * mv
Definition: clearvideo.c:55
static const uint16_t clv_biasy_3_codes[]
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> (&#39;D&#39;<<24) + (&#39;C&#39;<<16) + (&#39;B&#39;<<8) + &#39;A&#39;).
Definition: avcodec.h:1543
static const uint8_t clv_mvu_1_bits[]
static const uint16_t clv_flagsv_1_codes[]
static const uint8_t clv_biasu_1_bits[]
void * buf
Definition: avisynth_c.h:690
int16_t y
Definition: clearvideo.c:44
int extradata_size
Definition: avcodec.h:1619
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:321
static const uint16_t clv_flagsy_1_codes[]
int coded_height
Definition: avcodec.h:1705
static const uint16_t clv_flagsu_1_codes[]
static int copy_block(AVCodecContext *avctx, AVFrame *dst, AVFrame *src, int plane, int x, int y, int dx, int dy, int size)
Definition: clearvideo.c:222
int chroma_dc_quant
Definition: clearvideo.c:78
static const uint8_t clv_mvv_1_bits[]
int left_dc[4]
Definition: clearvideo.c:80
static const uint16_t clv_biasu_2_syms[]
const uint8_t ff_zigzag_direct[64]
Definition: mathtables.c:98
#define mid_pred
Definition: mathops.h:97
static TileInfo * decode_tile_info(GetBitContext *gb, LevelCodes *lc, int level)
Definition: clearvideo.c:362
#define COP(x)
Definition: clearvideo.c:155
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:232
uint8_t level
Definition: svq3.c:207
IDCTDSPContext idsp
Definition: clearvideo.c:67
int mb_w
Definition: clearvideo.c:50
int tile_size
Definition: clearvideo.c:74
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:62
static const uint16_t clv_mvu_1_syms[]
common internal api header.
static double c[64]
AVCodec ff_clearvideo_decoder
Definition: clearvideo.c:901
static const uint16_t clv_biasv_1_syms[]
static const uint8_t clv_biasu_2_bits[]
void * priv_data
Definition: avcodec.h:1545
static const uint16_t clv_mvy_3_syms[]
static const uint16_t clv_biasv_2_codes[]
static av_always_inline int diff(const uint32_t a, const uint32_t b)
static const uint16_t clv_biasy_2_codes[]
av_cold void ff_idctdsp_init(IDCTDSPContext *c, AVCodecContext *avctx)
Definition: idctdsp.c:238
int luma_dc_quant
Definition: clearvideo.c:78
VLC_TYPE(* table)[2]
code, bits
Definition: vlc.h:28
#define NUM_AC_CODES
AVFrame * pic
Definition: clearvideo.c:68
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:296
static const uint16_t clv_mvu_1_codes[]
uint16_t flags
Definition: clearvideo.c:59
static const uint16_t clv_mvy_1_syms[]
static const uint8_t clv_mvy_2_bits[]
static const uint16_t clv_flagsv_0_codes[]
int height
Definition: frame.h:276
static const uint16_t clv_mvv_1_syms[]
#define av_freep(p)
static const uint16_t clv_mvy_3_codes[]
#define FFSWAP(type, a, b)
Definition: common.h:99
static const uint8_t clv_flagsv_1_bits[]
static int copyadd_block(AVCodecContext *avctx, AVFrame *dst, AVFrame *src, int plane, int x, int y, int dx, int dy, int size, int bias)
Definition: clearvideo.c:258
#define MKTAG(a, b, c, d)
Definition: common.h:366
uint16_t mv_esc
Definition: clearvideo.c:36
This structure stores compressed data.
Definition: avcodec.h:1407
void ff_free_vlc(VLC *vlc)
Definition: bitstream.c:354
VLC bias_cb
Definition: clearvideo.c:40
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:959
static const uint16_t clv_mvv_1_codes[]
for(j=16;j >0;--j)
Predicted.
Definition: avutil.h:275
static const uint8_t clv_mvy_0_bits[]