FFmpeg  4.2.1
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  *got_frame = 0;
520  return buf_size;
521  } else if (frame_type & 0x2) {
522  if (buf_size < c->mb_width * c->mb_height) {
523  av_log(avctx, AV_LOG_ERROR, "Packet too small\n");
524  return AVERROR_INVALIDDATA;
525  }
526 
527  if ((ret = ff_reget_buffer(avctx, c->pic)) < 0)
528  return ret;
529 
530  c->pic->key_frame = 1;
532 
533  bytestream2_get_be32(&gb); // frame size;
534  c->ac_quant = bytestream2_get_byte(&gb);
535  c->luma_dc_quant = 32;
536  c->chroma_dc_quant = 32;
537 
538  if ((ret = init_get_bits8(&c->gb, buf + bytestream2_tell(&gb),
539  buf_size - bytestream2_tell(&gb))) < 0)
540  return ret;
541 
542  for (i = 0; i < 3; i++)
543  c->top_dc[i] = 32;
544  for (i = 0; i < 4; i++)
545  c->left_dc[i] = 32;
546 
547  for (j = 0; j < c->mb_height; j++) {
548  for (i = 0; i < c->mb_width; i++) {
549  ret = decode_mb(c, i, j);
550  if (ret < 0)
551  mb_ret = ret;
552  }
553  }
554  extend_edges(c->pic, c->tile_size);
555  } else {
556  int plane;
557 
558  if (c->pmb_width * c->pmb_height > 8LL*(buf_size - bytestream2_tell(&gb)))
559  return AVERROR_INVALIDDATA;
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_bits_left(&c->gb) <= 0)
577  return AVERROR_INVALIDDATA;
578  if (get_bits1(&c->gb)) {
579  MV mv = mvi_predict(&c->mvi, i, j, zero_mv);
580 
581  for (plane = 0; plane < 3; plane++) {
582  int16_t x = plane == 0 ? i << c->tile_shift : i << (c->tile_shift - 1);
583  int16_t y = plane == 0 ? j << c->tile_shift : j << (c->tile_shift - 1);
584  int16_t size = plane == 0 ? 1 << c->tile_shift : 1 << (c->tile_shift - 1);
585  int16_t mx = plane == 0 ? mv.x : mv.x / 2;
586  int16_t my = plane == 0 ? mv.y : mv.y / 2;
587 
588  ret = copy_block(avctx, c->pic, c->prev, plane, x, y, mx, my, size);
589  if (ret < 0)
590  mb_ret = ret;
591  }
592  } else {
593  int x = i << c->tile_shift;
594  int y = j << c->tile_shift;
595  int size = 1 << c->tile_shift;
596  TileInfo *tile;
597  MV mv, cmv;
598 
599  tile = decode_tile_info(&c->gb, c->ylev, 0);
600  if (!tile)
601  return AVERROR(ENOMEM);
602  mv = mvi_predict(&c->mvi, i, j, tile->mv);
603  ret = restore_tree(avctx, c->pic, c->prev, 0, x, y, size, tile, mv);
604  if (ret < 0)
605  mb_ret = ret;
606  x = i << (c->tile_shift - 1);
607  y = j << (c->tile_shift - 1);
608  size = 1 << (c->tile_shift - 1);
609  cmv.x = mv.x + tile->mv.x;
610  cmv.y = mv.y + tile->mv.y;
611  cmv.x /= 2;
612  cmv.y /= 2;
613  av_freep(&tile);
614  tile = decode_tile_info(&c->gb, c->ulev, 0);
615  if (!tile)
616  return AVERROR(ENOMEM);
617  ret = restore_tree(avctx, c->pic, c->prev, 1, x, y, size, tile, cmv);
618  if (ret < 0)
619  mb_ret = ret;
620  av_freep(&tile);
621  tile = decode_tile_info(&c->gb, c->vlev, 0);
622  if (!tile)
623  return AVERROR(ENOMEM);
624  ret = restore_tree(avctx, c->pic, c->prev, 2, x, y, size, tile, cmv);
625  if (ret < 0)
626  mb_ret = ret;
627  av_freep(&tile);
628  }
629  }
630  mvi_update_row(&c->mvi);
631  }
632  extend_edges(c->pic, c->tile_size);
633 
634  c->pic->key_frame = 0;
636  }
637 
638  if ((ret = av_frame_ref(data, c->pic)) < 0)
639  return ret;
640 
641  FFSWAP(AVFrame *, c->pic, c->prev);
642 
643  *got_frame = 1;
644 
645  if (get_bits_left(&c->gb) < 0)
646  av_log(c->avctx, AV_LOG_WARNING, "overread %d\n", -get_bits_left(&c->gb));
647 
648  return mb_ret < 0 ? mb_ret : buf_size;
649 }
650 
652 {
653  CLVContext *const c = avctx->priv_data;
654  int ret, w, h;
655 
656  if (avctx->extradata_size == 110) {
657  c->tile_size = AV_RL32(&avctx->extradata[94]);
658  } else if (avctx->extradata_size == 150) {
659  c->tile_size = AV_RB32(&avctx->extradata[134]);
660  } else if (!avctx->extradata_size) {
661  c->tile_size = 16;
662  } else {
663  av_log(avctx, AV_LOG_ERROR, "Unsupported extradata size: %d\n", avctx->extradata_size);
664  return AVERROR_INVALIDDATA;
665  }
666 
667  c->tile_shift = av_log2(c->tile_size);
668  if (1U << c->tile_shift != c->tile_size) {
669  av_log(avctx, AV_LOG_ERROR, "Tile size: %d, is not power of 2.\n", c->tile_size);
670  return AVERROR_INVALIDDATA;
671  }
672 
673  avctx->pix_fmt = AV_PIX_FMT_YUV420P;
674  w = avctx->width;
675  h = avctx->height;
676  ret = ff_set_dimensions(avctx, FFALIGN(w, 1 << c->tile_shift), FFALIGN(h, 1 << c->tile_shift));
677  if (ret < 0)
678  return ret;
679  avctx->width = w;
680  avctx->height = h;
681 
682  c->avctx = avctx;
683  c->mb_width = FFALIGN(avctx->width, 16) >> 4;
684  c->mb_height = FFALIGN(avctx->height, 16) >> 4;
685  c->pmb_width = (w + c->tile_size - 1) >> c->tile_shift;
686  c->pmb_height = (h + c->tile_size - 1) >> c->tile_shift;
687  c->pic = av_frame_alloc();
688  c->prev = av_frame_alloc();
689  c->mvi.mv = av_calloc(c->pmb_width * 2, sizeof(*c->mvi.mv));
690  if (!c->pic || !c->prev || !c->mvi.mv)
691  return AVERROR(ENOMEM);
692 
693  ff_idctdsp_init(&c->idsp, avctx);
694  ret = init_vlc(&c->dc_vlc, 9, NUM_DC_CODES,
695  clv_dc_bits, 1, 1,
696  clv_dc_codes, 1, 1, 0);
697  if (ret) {
698  av_log(avctx, AV_LOG_ERROR, "Error initialising DC VLC\n");
699  return ret;
700  }
701  ret = ff_init_vlc_sparse(&c->ac_vlc, 9, NUM_AC_CODES,
702  clv_ac_bits, 1, 1,
703  clv_ac_codes, 1, 1,
704  clv_ac_syms, 2, 2, 0);
705  if (ret) {
706  av_log(avctx, AV_LOG_ERROR, "Error initialising AC VLC\n");
707  return ret;
708  }
709 
711  clv_flagsy_0_bits, 1, 1,
712  clv_flagsy_0_codes, 2, 2, 0);
713  if (ret)
714  return ret;
715 
717  clv_flagsy_1_bits, 1, 1,
718  clv_flagsy_1_codes, 2, 2, 0);
719  if (ret)
720  return ret;
721 
723  clv_flagsy_2_bits, 1, 1,
724  clv_flagsy_2_codes, 2, 2, 0);
725  if (ret)
726  return ret;
727 
729  clv_flagsu_0_bits, 1, 1,
730  clv_flagsu_0_codes, 2, 2, 0);
731  if (ret)
732  return ret;
733 
735  clv_flagsu_1_bits, 1, 1,
736  clv_flagsu_1_codes, 2, 2, 0);
737  if (ret)
738  return ret;
739 
741  clv_flagsv_0_bits, 1, 1,
742  clv_flagsv_0_codes, 2, 2, 0);
743  if (ret)
744  return ret;
745 
747  clv_flagsv_1_bits, 1, 1,
748  clv_flagsv_1_codes, 2, 2, 0);
749  if (ret)
750  return ret;
751 
753  clv_mvy_0_bits, 1, 1,
754  clv_mvy_0_codes, 2, 2,
755  clv_mvy_0_syms, 2, 2, 0);
756  if (ret)
757  return ret;
758 
760  clv_mvy_1_bits, 1, 1,
761  clv_mvy_1_codes, 2, 2,
762  clv_mvy_1_syms, 2, 2, 0);
763  if (ret)
764  return ret;
765 
767  clv_mvy_2_bits, 1, 1,
768  clv_mvy_2_codes, 2, 2,
769  clv_mvy_2_syms, 2, 2, 0);
770  if (ret)
771  return ret;
772 
774  clv_mvy_3_bits, 1, 1,
775  clv_mvy_3_codes, 2, 2,
776  clv_mvy_3_syms, 2, 2, 0);
777  if (ret)
778  return ret;
779 
781  clv_mvu_1_bits, 1, 1,
782  clv_mvu_1_codes, 2, 2,
783  clv_mvu_1_syms, 2, 2, 0);
784  if (ret)
785  return ret;
786 
788  clv_mvu_2_bits, 1, 1,
789  clv_mvu_2_codes, 2, 2,
790  clv_mvu_2_syms, 2, 2, 0);
791  if (ret)
792  return ret;
793 
795  clv_mvv_1_bits, 1, 1,
796  clv_mvv_1_codes, 2, 2,
797  clv_mvv_1_syms, 2, 2, 0);
798  if (ret)
799  return ret;
800 
802  clv_mvv_2_bits, 1, 1,
803  clv_mvv_2_codes, 2, 2,
804  clv_mvv_2_syms, 2, 2, 0);
805  if (ret)
806  return ret;
807 
809  clv_biasy_1_bits, 1, 1,
810  clv_biasy_1_codes, 2, 2,
811  clv_biasy_1_syms, 2, 2, 0);
812  if (ret)
813  return ret;
814 
816  clv_biasy_2_bits, 1, 1,
817  clv_biasy_2_codes, 2, 2,
818  clv_biasy_2_syms, 2, 2, 0);
819  if (ret)
820  return ret;
821 
823  clv_biasy_3_bits, 1, 1,
824  clv_biasy_3_codes, 2, 2,
825  clv_biasy_3_syms, 2, 2, 0);
826  if (ret)
827  return ret;
828 
830  clv_biasu_1_bits, 1, 1,
831  clv_biasu_1_codes, 2, 2,
832  clv_biasu_1_syms, 2, 2, 0);
833  if (ret)
834  return ret;
835 
837  clv_biasu_2_bits, 1, 1,
838  clv_biasu_2_codes, 2, 2,
839  clv_biasu_2_syms, 2, 2, 0);
840  if (ret)
841  return ret;
842 
844  clv_biasv_1_bits, 1, 1,
845  clv_biasv_1_codes, 2, 2,
846  clv_biasv_1_syms, 2, 2, 0);
847  if (ret)
848  return ret;
849 
851  clv_biasv_2_bits, 1, 1,
852  clv_biasv_2_codes, 2, 2,
853  clv_biasv_2_syms, 2, 2, 0);
854  if (ret)
855  return ret;
856 
857  c->ylev[0].mv_esc = 0x0909;
858  c->ylev[1].mv_esc = 0x0A0A;
859  c->ylev[2].mv_esc = 0x1010;
860  c->ylev[3].mv_esc = 0x1313;
861  c->ulev[1].mv_esc = 0x0808;
862  c->ulev[2].mv_esc = 0x0B0B;
863  c->vlev[1].mv_esc = 0x0808;
864  c->vlev[2].mv_esc = 0x0B0B;
865 
866  c->ylev[1].bias_esc = 0x100;
867  c->ylev[2].bias_esc = 0x100;
868  c->ylev[3].bias_esc = 0x100;
869  c->ulev[1].bias_esc = 0x100;
870  c->ulev[2].bias_esc = 0x100;
871  c->vlev[1].bias_esc = 0x100;
872  c->vlev[2].bias_esc = 0x100;
873 
874  return 0;
875 }
876 
878 {
879  CLVContext *const c = avctx->priv_data;
880  int i;
881 
882  av_frame_free(&c->prev);
883  av_frame_free(&c->pic);
884 
885  av_freep(&c->mvi.mv);
886 
887  ff_free_vlc(&c->dc_vlc);
888  ff_free_vlc(&c->ac_vlc);
889  for (i = 0; i < 4; i++) {
890  ff_free_vlc(&c->ylev[i].mv_cb);
891  ff_free_vlc(&c->ylev[i].flags_cb);
892  ff_free_vlc(&c->ylev[i].bias_cb);
893  }
894  for (i = 0; i < 3; i++) {
895  ff_free_vlc(&c->ulev[i].mv_cb);
896  ff_free_vlc(&c->ulev[i].flags_cb);
897  ff_free_vlc(&c->ulev[i].bias_cb);
898  ff_free_vlc(&c->vlev[i].mv_cb);
899  ff_free_vlc(&c->vlev[i].flags_cb);
900  ff_free_vlc(&c->vlev[i].bias_cb);
901  }
902 
903  return 0;
904 }
905 
907  .name = "clearvideo",
908  .long_name = NULL_IF_CONFIG_SMALL("Iterated Systems ClearVideo"),
909  .type = AVMEDIA_TYPE_VIDEO,
911  .priv_data_size = sizeof(CLVContext),
913  .close = clv_decode_end,
915  .capabilities = AV_CODEC_CAP_DR1,
917 };
#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:384
#define NULL
Definition: coverity.c:32
const char const char void * val
Definition: avisynth_c.h:863
#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:651
int size
int mb_size
Definition: clearvideo.c:52
This structure describes decoded (raw) audio or video data.
Definition: frame.h:295
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:1753
VLC flags_cb
Definition: clearvideo.c:38
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:379
static const uint16_t clv_biasv_1_codes[]
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
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:273
VLC dc_vlc
Definition: clearvideo.c:76
int size
Definition: avcodec.h:1478
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:1775
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:3481
static int get_sbits(GetBitContext *s, int n)
Definition: get_bits.h:359
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:2011
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:877
#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:807
#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:443
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1666
static const uint16_t clv_biasy_1_codes[]
const char data[16]
Definition: mxf.c:91
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:1477
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
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]
#define U(x)
Definition: vp56_arith.h:37
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:849
static const uint8_t clv_biasy_2_bits[]
static const uint8_t clv_mvu_2_bits[]
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:259
int width
Definition: frame.h:353
#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:1393
static const uint8_t clv_biasy_1_bits[]
#define NUM_DC_CODES
const char * name
Name of the codec implementation.
Definition: avcodec.h:3488
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:792
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:378
static const uint8_t clv_biasv_1_bits[]
int width
picture width / height.
Definition: avcodec.h:1738
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:797
#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:326
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:677
int top_dc[3]
Definition: clearvideo.c:80
main external API structure.
Definition: avcodec.h:1565
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:1590
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:766
int16_t y
Definition: clearvideo.c:44
int extradata_size
Definition: avcodec.h:1667
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:498
static const uint16_t clv_flagsy_1_codes[]
int coded_height
Definition: avcodec.h:1753
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 flags(name, subs,...)
Definition: cbs_av1.c:561
#define COP(x)
Definition: clearvideo.c:155
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:309
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:66
static const uint16_t clv_mvu_1_syms[]
common internal api header.
static double c[64]
AVCodec ff_clearvideo_decoder
Definition: clearvideo.c:906
static const uint16_t clv_biasv_1_syms[]
static const uint8_t clv_biasu_2_bits[]
void * priv_data
Definition: avcodec.h:1592
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:373
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:353
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:1454
void ff_free_vlc(VLC *vlc)
Definition: bitstream.c:359
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:981
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[]