FFmpeg
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 "libavutil/mem_internal.h"
28 #include "libavutil/thread.h"
29 
30 #include "avcodec.h"
31 #include "bytestream.h"
32 #include "codec_internal.h"
33 #include "decode.h"
34 #include "get_bits.h"
35 #include "idctdsp.h"
36 #include "mathops.h"
37 #include "clearvideodata.h"
38 
39 #define CLV_VLC_BITS 9
40 
41 typedef struct LevelCodes {
42  const VLCElem *flags_cb;
43  const VLCElem *mv_cb;
44  const VLCElem *bias_cb;
45 } LevelCodes;
46 
47 typedef struct MV {
48  int16_t x, y;
49 } MV;
50 
51 static const MV zero_mv = { 0 };
52 
53 typedef struct MVInfo {
54  int mb_w;
55  int mb_h;
56  int mb_size;
57  int mb_stride;
58  int top;
59  MV *mv;
60 } MVInfo;
61 
62 typedef struct CLVContext {
71  int tile_size;
74  DECLARE_ALIGNED(16, int16_t, block)[64];
75  int top_dc[3], left_dc[4];
76 } CLVContext;
77 
78 static VLCElem dc_vlc[1104], ac_vlc[554];
79 static LevelCodes lev[4 + 3 + 3]; // 0..3: Y, 4..6: U, 7..9: V
80 
81 static inline int decode_block(CLVContext *ctx, int16_t *blk, int has_ac,
82  int ac_quant)
83 {
84  GetBitContext *gb = &ctx->gb;
85  int idx = 1, last = 0, val, skip;
86 
87  memset(blk, 0, sizeof(*blk) * 64);
88  blk[0] = get_vlc2(gb, dc_vlc, CLV_VLC_BITS, 3);
89 
90  if (!has_ac)
91  return 0;
92 
93  while (idx < 64 && !last) {
94  val = get_vlc2(gb, ac_vlc, CLV_VLC_BITS, 2);
95  if (val < 0)
96  return AVERROR_INVALIDDATA;
97  if (val != 0x1BFF) {
98  last = val >> 12;
99  skip = (val >> 4) & 0xFF;
100  val &= 0xF;
101  if (get_bits1(gb))
102  val = -val;
103  } else {
104  last = get_bits1(gb);
105  skip = get_bits(gb, 6);
106  val = get_sbits(gb, 8);
107  }
108  if (val) {
109  int aval = FFABS(val), sign = val < 0;
110  val = ac_quant * (2 * aval + 1);
111  if (!(ac_quant & 1))
112  val--;
113  if (sign)
114  val = -val;
115  }
116  idx += skip;
117  if (idx >= 64)
118  return AVERROR_INVALIDDATA;
119  blk[ff_zigzag_direct[idx++]] = val;
120  }
121 
122  return (idx <= 64 && last) ? 0 : -1;
123 }
124 
125 #define DCT_TEMPLATE(blk, step, bias, shift, dshift, OP) \
126  const int t0 = OP(2841 * blk[1 * step] + 565 * blk[7 * step]); \
127  const int t1 = OP( 565 * blk[1 * step] - 2841 * blk[7 * step]); \
128  const int t2 = OP(1609 * blk[5 * step] + 2408 * blk[3 * step]); \
129  const int t3 = OP(2408 * blk[5 * step] - 1609 * blk[3 * step]); \
130  const int t4 = OP(1108 * blk[2 * step] - 2676 * blk[6 * step]); \
131  const int t5 = OP(2676 * blk[2 * step] + 1108 * blk[6 * step]); \
132  const int t6 = ((blk[0 * step] + blk[4 * step]) * (1 << dshift)) + bias; \
133  const int t7 = ((blk[0 * step] - blk[4 * step]) * (1 << dshift)) + bias; \
134  const int t8 = t0 + t2; \
135  const int t9 = t0 - t2; \
136  const int tA = (int)(181U * (t9 + (t1 - t3)) + 0x80) >> 8; \
137  const int tB = (int)(181U * (t9 - (t1 - t3)) + 0x80) >> 8; \
138  const int tC = t1 + t3; \
139  \
140  blk[0 * step] = (t6 + t5 + t8) >> shift; \
141  blk[1 * step] = (t7 + t4 + tA) >> shift; \
142  blk[2 * step] = (t7 - t4 + tB) >> shift; \
143  blk[3 * step] = (t6 - t5 + tC) >> shift; \
144  blk[4 * step] = (t6 - t5 - tC) >> shift; \
145  blk[5 * step] = (t7 - t4 - tB) >> shift; \
146  blk[6 * step] = (t7 + t4 - tA) >> shift; \
147  blk[7 * step] = (t6 + t5 - t8) >> shift; \
148 
149 #define ROP(x) x
150 #define COP(x) (((x) + 4) >> 3)
151 
152 static void clv_dct(int16_t *block)
153 {
154  int i;
155  int16_t *ptr;
156 
157  ptr = block;
158  for (i = 0; i < 8; i++) {
159  DCT_TEMPLATE(ptr, 1, 0x80, 8, 11, ROP);
160  ptr += 8;
161  }
162 
163  ptr = block;
164  for (i = 0; i < 8; i++) {
165  DCT_TEMPLATE(ptr, 8, 0x2000, 14, 8, COP);
166  ptr++;
167  }
168 }
169 
170 static int decode_mb(CLVContext *c, int x, int y)
171 {
172  int i, has_ac[6], off;
173 
174  for (i = 0; i < 6; i++)
175  has_ac[i] = get_bits1(&c->gb);
176 
177  off = x * 16 + y * 16 * c->pic->linesize[0];
178  for (i = 0; i < 4; i++) {
179  if (decode_block(c, c->block, has_ac[i], c->ac_quant) < 0)
180  return AVERROR_INVALIDDATA;
181  if (!x && !(i & 1)) {
182  c->block[0] += c->top_dc[0];
183  c->top_dc[0] = c->block[0];
184  } else {
185  c->block[0] += c->left_dc[(i & 2) >> 1];
186  }
187  c->left_dc[(i & 2) >> 1] = c->block[0];
188  c->block[0] *= c->luma_dc_quant;
189  clv_dct(c->block);
190  if (i == 2)
191  off += c->pic->linesize[0] * 8;
192  c->idsp.put_pixels_clamped(c->block,
193  c->pic->data[0] + off + (i & 1) * 8,
194  c->pic->linesize[0]);
195  }
196 
197  off = x * 8 + y * 8 * c->pic->linesize[1];
198  for (i = 1; i < 3; i++) {
199  if (decode_block(c, c->block, has_ac[i + 3], c->ac_quant) < 0)
200  return AVERROR_INVALIDDATA;
201  if (!x) {
202  c->block[0] += c->top_dc[i];
203  c->top_dc[i] = c->block[0];
204  } else {
205  c->block[0] += c->left_dc[i + 1];
206  }
207  c->left_dc[i + 1] = c->block[0];
208  c->block[0] *= c->chroma_dc_quant;
209  clv_dct(c->block);
210  c->idsp.put_pixels_clamped(c->block, c->pic->data[i] + off,
211  c->pic->linesize[i]);
212  }
213 
214  return 0;
215 }
216 
217 static int copy_block(AVCodecContext *avctx, AVFrame *dst, const AVFrame *src,
218  int plane, int x, int y, int dx, int dy, int size)
219 {
220  int shift = plane > 0;
221  int sx = x + dx;
222  int sy = y + dy;
223  int sstride, dstride, soff, doff;
224  uint8_t *dbuf;
225  const uint8_t *sbuf;
226  int i;
227 
228  if (x < 0 || sx < 0 || y < 0 || sy < 0 ||
229  x + size > avctx->coded_width >> shift ||
230  y + size > avctx->coded_height >> shift ||
231  sx + size > avctx->coded_width >> shift ||
232  sy + size > avctx->coded_height >> shift)
233  return AVERROR_INVALIDDATA;
234 
235  sstride = src->linesize[plane];
236  dstride = dst->linesize[plane];
237  soff = sx + sy * sstride;
238  sbuf = src->data[plane];
239  doff = x + y * dstride;
240  dbuf = dst->data[plane];
241 
242  for (i = 0; i < size; i++) {
243  uint8_t *dptr = &dbuf[doff];
244  const uint8_t *sptr = &sbuf[soff];
245 
246  memcpy(dptr, sptr, size);
247  doff += dstride;
248  soff += sstride;
249  }
250 
251  return 0;
252 }
253 
254 static int copyadd_block(AVCodecContext *avctx, AVFrame *dst, const AVFrame *src,
255  int plane, int x, int y, int dx, int dy, int size, int bias)
256 {
257  int shift = plane > 0;
258  int sx = x + dx;
259  int sy = y + dy;
260  int sstride = src->linesize[plane];
261  int dstride = dst->linesize[plane];
262  int soff = sx + sy * sstride;
263  const uint8_t *sbuf = src->data[plane];
264  int doff = x + y * dstride;
265  uint8_t *dbuf = dst->data[plane];
266  int i, j;
267 
268  if (x < 0 || sx < 0 || y < 0 || sy < 0 ||
269  x + size > avctx->coded_width >> shift ||
270  y + size > avctx->coded_height >> shift ||
271  sx + size > avctx->coded_width >> shift ||
272  sy + size > avctx->coded_height >> shift)
273  return AVERROR_INVALIDDATA;
274 
275  for (j = 0; j < size; j++) {
276  uint8_t *dptr = &dbuf[doff];
277  const uint8_t *sptr = &sbuf[soff];
278 
279  for (i = 0; i < size; i++) {
280  int val = sptr[i] + bias;
281 
282  dptr[i] = av_clip_uint8(val);
283  }
284 
285  doff += dstride;
286  soff += sstride;
287  }
288 
289  return 0;
290 }
291 
292 static MV *mvi_predict(MVInfo *mvi, int mb_x, int mb_y)
293 {
294  MV res, pred_mv;
295  int left_mv, right_mv, top_mv, bot_mv;
296 
297  if (mvi->top) {
298  if (mb_x > 0) {
299  pred_mv = mvi->mv[mvi->mb_stride + mb_x - 1];
300  } else {
301  pred_mv = zero_mv;
302  }
303  } else if ((mb_x == 0) || (mb_x == mvi->mb_w - 1)) {
304  pred_mv = mvi->mv[mb_x];
305  } else {
306  MV A = mvi->mv[mvi->mb_stride + mb_x - 1];
307  MV B = mvi->mv[ mb_x ];
308  MV C = mvi->mv[ mb_x + 1];
309  pred_mv.x = mid_pred(A.x, B.x, C.x);
310  pred_mv.y = mid_pred(A.y, B.y, C.y);
311  }
312 
313  res = pred_mv;
314 
315  left_mv = -((mb_x * mvi->mb_size));
316  right_mv = ((mvi->mb_w - mb_x - 1) * mvi->mb_size);
317  if (res.x < left_mv) {
318  res.x = left_mv;
319  }
320  if (res.x > right_mv) {
321  res.x = right_mv;
322  }
323  top_mv = -((mb_y * mvi->mb_size));
324  bot_mv = ((mvi->mb_h - mb_y - 1) * mvi->mb_size);
325  if (res.y < top_mv) {
326  res.y = top_mv;
327  }
328  if (res.y > bot_mv) {
329  res.y = bot_mv;
330  }
331 
332  mvi->mv[mvi->mb_stride + mb_x].x = res.x;
333  mvi->mv[mvi->mb_stride + mb_x].y = res.y;
334 
335  return &mvi->mv[mvi->mb_stride + mb_x];
336 }
337 
339 {
340  mv->x += diff.x;
341  mv->y += diff.y;
342 }
343 
344 static void mvi_reset(MVInfo *mvi, int mb_w, int mb_h, int mb_size)
345 {
346  mvi->top = 1;
347  mvi->mb_w = mb_w;
348  mvi->mb_h = mb_h;
349  mvi->mb_size = mb_size;
350  mvi->mb_stride = mb_w;
351  memset(mvi->mv, 0, sizeof(MV) * mvi->mb_stride * 2);
352 }
353 
354 static void mvi_update_row(MVInfo *mvi)
355 {
356  int i;
357 
358  mvi->top = 0;
359  for (i = 0 ; i < mvi->mb_stride; i++) {
360  mvi->mv[i] = mvi->mv[mvi->mb_stride + i];
361  }
362 }
363 
364 static int tile_do_block(AVCodecContext *avctx, AVFrame *dst, const AVFrame *src,
365  int plane, int x, int y, int dx, int dy, int size, int bias)
366 {
367  int ret;
368 
369  if (!bias) {
370  ret = copy_block(avctx, dst, src, plane, x, y, dx, dy, size);
371  } else {
372  ret = copyadd_block(avctx, dst, src, plane, x, y, dx, dy, size, bias);
373  }
374 
375  return ret;
376 }
377 
378 static int decode_tile(AVCodecContext *avctx, GetBitContext *gb,
379  const LevelCodes *lc,
380  AVFrame *dst, const AVFrame *src,
381  int plane, int x, int y, int size,
382  MV root_mv, MV *pred)
383 {
384  int i, flags = 0;
385  int16_t bias = 0;
386  MV mv = { 0 };
387  int err;
388 
389  if (lc->flags_cb)
390  flags = get_vlc2(gb, lc->flags_cb, CLV_VLC_BITS, 2);
391 
392  if (lc->mv_cb) {
393  uint16_t mv_code = get_vlc2(gb, lc->mv_cb, CLV_VLC_BITS, 2);
394 
395  if (mv_code != MV_ESC) {
396  mv.x = (int8_t)(mv_code & 0xff);
397  mv.y = (int8_t)(mv_code >> 8);
398  } else {
399  mv.x = get_sbits(gb, 8);
400  mv.y = get_sbits(gb, 8);
401  }
402  if (pred)
404  }
405  mv.x += root_mv.x;
406  mv.y += root_mv.y;
407 
408  if (lc->bias_cb) {
409  uint16_t bias_val = get_vlc2(gb, lc->bias_cb, CLV_VLC_BITS, 2);
410 
411  if (bias_val != BIAS_ESC) {
412  bias = (int16_t)(bias_val);
413  } else {
414  bias = get_sbits(gb, 16);
415  }
416  }
417 
418  if (flags) {
419  int hsize = size >> 1;
420  for (i = 0; i < 4; i++) {
421  int xoff = (i & 2) == 0 ? 0 : hsize;
422  int yoff = (i & 1) == 0 ? 0 : hsize;
423 
424  if (flags & (1 << i)) {
425  err = decode_tile(avctx, gb, lc + 1, dst, src, plane,
426  x + xoff, y + yoff, hsize, root_mv, NULL);
427  } else {
428  err = tile_do_block(avctx, dst, src, plane, x + xoff, y + yoff,
429  mv.x, mv.y, hsize, bias);
430  }
431  if (err < 0)
432  return err;
433  }
434  } else {
435  err = tile_do_block(avctx, dst, src, plane, x, y, mv.x, mv.y, size, bias);
436  if (err < 0)
437  return err;
438  }
439 
440  return 0;
441 }
442 
443 static void extend_edges(AVFrame *buf, int tile_size)
444 {
445  int comp, i, j;
446 
447  for (comp = 0; comp < 3; comp++) {
448  int shift = comp > 0;
449  int w = buf->width >> shift;
450  int h = buf->height >> shift;
451  int size = comp == 0 ? tile_size : tile_size >> 1;
452  int stride = buf->linesize[comp];
453  uint8_t *framebuf = buf->data[comp];
454 
455  int right = size - (w & (size - 1));
456  int bottom = size - (h & (size - 1));
457 
458  if ((right == size) && (bottom == size)) {
459  return;
460  }
461  if (right != size) {
462  int off = w;
463  for (j = 0; j < h; j++) {
464  for (i = 0; i < right; i++) {
465  framebuf[off + i] = 0x80;
466  }
467  off += stride;
468  }
469  }
470  if (bottom != size) {
471  int off = h * stride;
472  for (j = 0; j < bottom; j++) {
473  for (i = 0; i < stride; i++) {
474  framebuf[off + i] = 0x80;
475  }
476  off += stride;
477  }
478  }
479  }
480 }
481 
482 static int clv_decode_frame(AVCodecContext *avctx, AVFrame *rframe,
483  int *got_frame, AVPacket *avpkt)
484 {
485  const uint8_t *buf = avpkt->data;
486  int buf_size = avpkt->size;
487  CLVContext *c = avctx->priv_data;
488  GetByteContext gb;
489  uint32_t frame_type;
490  int i, j, ret;
491  int mb_ret = 0;
492 
493  bytestream2_init(&gb, buf, buf_size);
494  if (avctx->codec_tag == MKTAG('C', 'L', 'V', '1')) {
495  int skip = bytestream2_get_byte(&gb);
496  bytestream2_skip(&gb, (skip + 1) * 8);
497  }
498 
499  frame_type = bytestream2_get_byte(&gb);
500 
501  if ((frame_type & 0x7f) == 0x30) {
502  *got_frame = 0;
503  return buf_size;
504  } else if (frame_type & 0x2) {
505  if (buf_size < c->mb_width * c->mb_height) {
506  av_log(avctx, AV_LOG_ERROR, "Packet too small\n");
507  return AVERROR_INVALIDDATA;
508  }
509 
510  if ((ret = ff_reget_buffer(avctx, c->pic, 0)) < 0)
511  return ret;
512 
513  c->pic->flags |= AV_FRAME_FLAG_KEY;
514  c->pic->pict_type = AV_PICTURE_TYPE_I;
515 
516  bytestream2_get_be32(&gb); // frame size;
517  c->ac_quant = bytestream2_get_byte(&gb);
518  c->luma_dc_quant = 32;
519  c->chroma_dc_quant = 32;
520 
521  if ((ret = init_get_bits8(&c->gb, buf + bytestream2_tell(&gb),
522  buf_size - bytestream2_tell(&gb))) < 0)
523  return ret;
524 
525  for (i = 0; i < 3; i++)
526  c->top_dc[i] = 32;
527  for (i = 0; i < 4; i++)
528  c->left_dc[i] = 32;
529 
530  for (j = 0; j < c->mb_height; j++) {
531  for (i = 0; i < c->mb_width; i++) {
532  ret = decode_mb(c, i, j);
533  if (ret < 0)
534  mb_ret = ret;
535  }
536  }
537  extend_edges(c->pic, c->tile_size);
538  } else {
539  int plane;
540 
541  if (c->pmb_width * c->pmb_height > 8LL*(buf_size - bytestream2_tell(&gb)))
542  return AVERROR_INVALIDDATA;
543 
544  if ((ret = ff_reget_buffer(avctx, c->pic, 0)) < 0)
545  return ret;
546 
547  ret = av_frame_copy(c->pic, c->prev);
548  if (ret < 0)
549  return ret;
550 
551  if ((ret = init_get_bits8(&c->gb, buf + bytestream2_tell(&gb),
552  buf_size - bytestream2_tell(&gb))) < 0)
553  return ret;
554 
555  mvi_reset(&c->mvi, c->pmb_width, c->pmb_height, 1 << c->tile_shift);
556 
557  for (j = 0; j < c->pmb_height; j++) {
558  for (i = 0; i < c->pmb_width; i++) {
559  MV *mvp, mv;
560  if (get_bits_left(&c->gb) <= 0)
561  return AVERROR_INVALIDDATA;
562 
563  mvp = mvi_predict(&c->mvi, i, j);
564  mv = *mvp;
565  if (get_bits1(&c->gb)) {
566  for (plane = 0; plane < 3; plane++) {
567  int16_t x = plane == 0 ? i << c->tile_shift : i << (c->tile_shift - 1);
568  int16_t y = plane == 0 ? j << c->tile_shift : j << (c->tile_shift - 1);
569  int16_t size = plane == 0 ? 1 << c->tile_shift : 1 << (c->tile_shift - 1);
570  int16_t mx = plane == 0 ? mv.x : mv.x / 2;
571  int16_t my = plane == 0 ? mv.y : mv.y / 2;
572 
573  ret = copy_block(avctx, c->pic, c->prev, plane, x, y, mx, my, size);
574  if (ret < 0)
575  mb_ret = ret;
576  }
577  } else {
578  int x = i << c->tile_shift;
579  int y = j << c->tile_shift;
580  int size = 1 << c->tile_shift;
581  MV cmv;
582 
583  ret = decode_tile(avctx, &c->gb, &lev[0], c->pic, c->prev, // Y
584  0, x, y, size, mv, mvp);
585  if (ret < 0)
586  mb_ret = ret;
587  x = i << (c->tile_shift - 1);
588  y = j << (c->tile_shift - 1);
589  size = 1 << (c->tile_shift - 1);
590  cmv = *mvp;
591  cmv.x /= 2;
592  cmv.y /= 2;
593  ret = decode_tile(avctx, &c->gb, &lev[4], c->pic, c->prev, // U
594  1, x, y, size, cmv, NULL);
595  if (ret < 0)
596  mb_ret = ret;
597  ret = decode_tile(avctx, &c->gb, &lev[7], c->pic, c->prev, // U
598  2, x, y, size, cmv, NULL);
599  if (ret < 0)
600  mb_ret = ret;
601  }
602  }
603  mvi_update_row(&c->mvi);
604  }
605  extend_edges(c->pic, c->tile_size);
606 
607  c->pic->flags &= ~AV_FRAME_FLAG_KEY;
608  c->pic->pict_type = AV_PICTURE_TYPE_P;
609  }
610 
611  if ((ret = av_frame_ref(rframe, c->pic)) < 0)
612  return ret;
613 
614  FFSWAP(AVFrame *, c->pic, c->prev);
615 
616  *got_frame = 1;
617 
618  if (get_bits_left(&c->gb) < 0)
619  av_log(c->avctx, AV_LOG_WARNING, "overread %d\n", -get_bits_left(&c->gb));
620 
621  return mb_ret < 0 ? mb_ret : buf_size;
622 }
623 
625  const uint8_t counts[16],
626  const uint16_t **syms)
627 {
628  uint8_t lens[MAX_VLC_ENTRIES];
629  const uint16_t *symbols = *syms;
630  unsigned num = 0;
631 
632  for (int i = 0; i < 16; i++) {
633  unsigned count = counts[i];
634  if (count == 255) /* Special case for Y_3 table */
635  count = 303;
636  for (count += num; num < count; num++)
637  lens[num] = i + 1;
638  }
639  *syms += num;
640  return ff_vlc_init_tables_from_lengths(state, CLV_VLC_BITS, num, lens, 1,
641  symbols, 2, 2, 0, 0);
642 }
643 
644 static av_cold void clv_init_static(void)
645 {
646  static VLCElem vlc_buf[16716];
647  VLCInitState state = VLC_INIT_STATE(vlc_buf);
648  const uint16_t *mv_syms = clv_mv_syms, *bias_syms = clv_bias_syms;
649 
651  clv_dc_lens, 1,
652  clv_dc_syms, 1, 1, -63, 0);
654  clv_ac_bits, 1,
655  clv_ac_syms, 2, 2, 0, 0);
656  for (unsigned i = 0, j = 0, k = 0;; i++) {
657  if (0x36F & (1 << i)) {
659  k++;
660  }
661  if (i == FF_ARRAY_ELEMS(lev) - 1)
662  break;
663  if (0x1B7 & (1 << i)) {
664  lev[i].flags_cb =
666  clv_flags_bits[j], 1,
667  clv_flags_syms[j], 1, 1,
668  0, 0);
669 
671  &bias_syms);
672  j++;
673  }
674  }
675 }
676 
678 {
679  static AVOnce init_static_once = AV_ONCE_INIT;
680  CLVContext *const c = avctx->priv_data;
681  int ret, w, h;
682 
683  if (avctx->extradata_size == 110) {
684  c->tile_size = AV_RL32(&avctx->extradata[94]);
685  } else if (avctx->extradata_size == 150) {
686  c->tile_size = AV_RB32(&avctx->extradata[134]);
687  } else if (!avctx->extradata_size) {
688  c->tile_size = 16;
689  } else {
690  av_log(avctx, AV_LOG_ERROR, "Unsupported extradata size: %d\n", avctx->extradata_size);
691  return AVERROR_INVALIDDATA;
692  }
693 
694  c->tile_shift = av_log2(c->tile_size);
695  if (1U << c->tile_shift != c->tile_size || c->tile_shift < 1 || c->tile_shift > 30) {
696  av_log(avctx, AV_LOG_ERROR, "Tile size: %d, is not power of 2 > 1 and < 2^31\n", c->tile_size);
697  return AVERROR_INVALIDDATA;
698  }
699 
700  avctx->pix_fmt = AV_PIX_FMT_YUV420P;
701  w = avctx->width;
702  h = avctx->height;
703  ret = ff_set_dimensions(avctx, FFALIGN(w, 1 << c->tile_shift), FFALIGN(h, 1 << c->tile_shift));
704  if (ret < 0)
705  return ret;
706  avctx->width = w;
707  avctx->height = h;
708 
709  c->avctx = avctx;
710  c->mb_width = FFALIGN(avctx->width, 16) >> 4;
711  c->mb_height = FFALIGN(avctx->height, 16) >> 4;
712  c->pmb_width = (w + c->tile_size - 1) >> c->tile_shift;
713  c->pmb_height = (h + c->tile_size - 1) >> c->tile_shift;
714  c->pic = av_frame_alloc();
715  c->prev = av_frame_alloc();
716  c->mvi.mv = av_calloc(c->pmb_width * 2, sizeof(*c->mvi.mv));
717  if (!c->pic || !c->prev || !c->mvi.mv)
718  return AVERROR(ENOMEM);
719 
720  ff_idctdsp_init(&c->idsp, avctx);
721 
722  ff_thread_once(&init_static_once, clv_init_static);
723 
724  return 0;
725 }
726 
728 {
729  CLVContext *const c = avctx->priv_data;
730 
731  av_frame_free(&c->prev);
732  av_frame_free(&c->pic);
733 
734  av_freep(&c->mvi.mv);
735 
736  return 0;
737 }
738 
740  .p.name = "clearvideo",
741  CODEC_LONG_NAME("Iterated Systems ClearVideo"),
742  .p.type = AVMEDIA_TYPE_VIDEO,
743  .p.id = AV_CODEC_ID_CLEARVIDEO,
744  .priv_data_size = sizeof(CLVContext),
746  .close = clv_decode_end,
748  .p.capabilities = AV_CODEC_CAP_DR1,
749  .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
750 };
A
#define A(x)
Definition: vpx_arith.h:28
MAX_VLC_ENTRIES
#define MAX_VLC_ENTRIES
Definition: clearvideodata.h:27
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:186
FF_CODEC_CAP_INIT_CLEANUP
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
Definition: codec_internal.h:42
get_bits_left
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:694
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame 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. Frame references ownership and permissions
mem_internal.h
comp
static void comp(unsigned char *dst, ptrdiff_t dst_stride, unsigned char *src, ptrdiff_t src_stride, int add)
Definition: eamad.c:80
GetByteContext
Definition: bytestream.h:33
clv_dc_syms
static const uint8_t clv_dc_syms[NUM_DC_CODES]
Definition: clearvideodata.h:41
LevelCodes::mv_cb
const VLCElem * mv_cb
Definition: clearvideo.c:43
thread.h
CLVContext::pmb_width
int pmb_width
Definition: clearvideo.c:69
mv
static const int8_t mv[256][2]
Definition: 4xm.c:80
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:88
MVInfo::mv
MV * mv
Definition: clearvideo.c:59
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:340
clv_mv_syms
static const uint16_t clv_mv_syms[]
Definition: clearvideodata.h:112
MV::y
int16_t y
Definition: clearvideo.c:48
AVFrame::width
int width
Definition: frame.h:412
w
uint8_t w
Definition: llviddspenc.c:38
AVPacket::data
uint8_t * data
Definition: packet.h:522
MVInfo::mb_w
int mb_w
Definition: clearvideo.c:54
clv_decode_frame
static int clv_decode_frame(AVCodecContext *avctx, AVFrame *rframe, int *got_frame, AVPacket *avpkt)
Definition: clearvideo.c:482
mvi_update_prediction
static void mvi_update_prediction(MV *mv, MV diff)
Definition: clearvideo.c:338
FFCodec
Definition: codec_internal.h:127
clearvideodata.h
CLVContext::top_dc
int top_dc[3]
Definition: clearvideo.c:75
CLVContext::mvi
MVInfo mvi
Definition: clearvideo.c:70
clv_flags_syms
static const uint8_t clv_flags_syms[][16]
Definition: clearvideodata.h:90
ff_set_dimensions
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:94
CLVContext::pic
AVFrame * pic
Definition: clearvideo.c:65
ff_idctdsp_init
av_cold void ff_idctdsp_init(IDCTDSPContext *c, AVCodecContext *avctx)
Definition: idctdsp.c:228
clv_decode_init
static av_cold int clv_decode_init(AVCodecContext *avctx)
Definition: clearvideo.c:677
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:361
MVInfo::top
int top
Definition: clearvideo.c:58
ROP
#define ROP(x)
Definition: clearvideo.c:149
bytestream2_skip
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition: bytestream.h:168
get_bits
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:335
extend_edges
static void extend_edges(AVFrame *buf, int tile_size)
Definition: clearvideo.c:443
BIAS_ESC
#define BIAS_ESC
Definition: clearvideodata.h:619
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:131
decode_mb
static int decode_mb(CLVContext *c, int x, int y)
Definition: clearvideo.c:170
GetBitContext
Definition: get_bits.h:108
clv_dc_lens
static const uint8_t clv_dc_lens[NUM_DC_CODES]
Definition: clearvideodata.h:31
CLVContext::mb_width
int mb_width
Definition: clearvideo.c:68
val
static double val(void *priv, double ch)
Definition: aeval.c:78
AVCodecContext::coded_height
int coded_height
Definition: avcodec.h:633
av_frame_alloc
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:76
clv_init_static
static av_cold void clv_init_static(void)
Definition: clearvideo.c:644
C
s EdgeDetect Foobar g libavfilter vf_edgedetect c libavfilter vf_foobar c edit libavfilter and add an entry for foobar following the pattern of the other filters edit libavfilter allfilters and add an entry for foobar following the pattern of the other filters configure make j< whatever > ffmpeg ffmpeg i you should get a foobar png with Lena edge detected That s your new playground is ready Some little details about what s going which in turn will define variables for the build system and the C
Definition: writing_filters.txt:58
ff_thread_once
static int ff_thread_once(char *control, void(*routine)(void))
Definition: thread.h:205
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
clv_bias_syms
static const uint16_t clv_bias_syms[]
Definition: clearvideodata.h:620
FF_ARRAY_ELEMS
#define FF_ARRAY_ELEMS(a)
Definition: sinewin_tablegen.c:29
av_cold
#define av_cold
Definition: attributes.h:90
init_get_bits8
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:545
AV_FRAME_FLAG_KEY
#define AV_FRAME_FLAG_KEY
A flag to mark frames that are keyframes.
Definition: frame.h:591
VLCInitState
For static VLCs, the number of bits can often be hardcoded at each get_vlc2() callsite.
Definition: vlc.h:209
AVCodecContext::extradata_size
int extradata_size
Definition: avcodec.h:524
state
static struct @382 state
FF_CODEC_DECODE_CB
#define FF_CODEC_DECODE_CB(func)
Definition: codec_internal.h:287
CLVContext::avctx
AVCodecContext * avctx
Definition: clearvideo.c:63
clv_dct
static void clv_dct(int16_t *block)
Definition: clearvideo.c:152
CLVContext::gb
GetBitContext gb
Definition: clearvideo.c:67
B
#define B
Definition: huffyuv.h:42
get_sbits
static int get_sbits(GetBitContext *s, int n)
Definition: get_bits.h:320
ctx
AVFormatContext * ctx
Definition: movenc.c:48
decode.h
get_bits.h
MV::x
int16_t x
Definition: clearvideo.c:48
CLVContext::left_dc
int left_dc[4]
Definition: clearvideo.c:75
LevelCodes
Definition: clearvideo.c:41
AV_PIX_FMT_YUV420P
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:73
blk
#define blk(i)
Definition: sha.c:186
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:272
mvi_predict
static MV * mvi_predict(MVInfo *mvi, int mb_x, int mb_y)
Definition: clearvideo.c:292
FFABS
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:72
dc_vlc
static VLCElem dc_vlc[1104]
Definition: clearvideo.c:78
CLVContext::tile_shift
int tile_shift
Definition: clearvideo.c:72
AV_ONCE_INIT
#define AV_ONCE_INIT
Definition: thread.h:203
NULL
#define NULL
Definition: coverity.c:32
bias
static int bias(int x, int c)
Definition: vqcdec.c:114
AV_PICTURE_TYPE_I
@ AV_PICTURE_TYPE_I
Intra.
Definition: avutil.h:279
get_bits1
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:388
mathops.h
clv_decode_end
static av_cold int clv_decode_end(AVCodecContext *avctx)
Definition: clearvideo.c:727
copyadd_block
static int copyadd_block(AVCodecContext *avctx, AVFrame *dst, const AVFrame *src, int plane, int x, int y, int dx, int dy, int size, int bias)
Definition: clearvideo.c:254
MVInfo
Definition: clearvideo.c:53
MVInfo::mb_stride
int mb_stride
Definition: clearvideo.c:57
get_vlc2
static av_always_inline int get_vlc2(GetBitContext *s, const VLCElem *table, int bits, int max_depth)
Parse a vlc code.
Definition: get_bits.h:652
mvi_update_row
static void mvi_update_row(MVInfo *mvi)
Definition: clearvideo.c:354
AVOnce
#define AVOnce
Definition: thread.h:202
c
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
bytestream2_tell
static av_always_inline int bytestream2_tell(GetByteContext *g)
Definition: bytestream.h:192
mvp
static void mvp(const VVCLocalContext *lc, const int mvp_lx_flag, const int lx, const int8_t *ref_idx, const int amvr_shift, Mv *mv)
Definition: vvc_mvs.c:1567
init
int(* init)(AVBSFContext *ctx)
Definition: dts2pts.c:365
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
AVPacket::size
int size
Definition: packet.h:523
av_frame_ref
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:312
MVInfo::mb_h
int mb_h
Definition: clearvideo.c:55
codec_internal.h
DECLARE_ALIGNED
#define DECLARE_ALIGNED(n, t, v)
Definition: mem_internal.h:109
shift
static int shift(int a, int b)
Definition: bonk.c:262
av_frame_copy
int av_frame_copy(AVFrame *dst, const AVFrame *src)
Copy the frame data from src to dst.
Definition: frame.c:769
build_vlc
static const av_cold VLCElem * build_vlc(VLCInitState *state, const uint8_t counts[16], const uint16_t **syms)
Definition: clearvideo.c:624
MV
Definition: clearvideo.c:47
for
for(k=2;k<=8;++k)
Definition: h264pred_template.c:425
size
int size
Definition: twinvq_data.h:10344
VLCElem
Definition: vlc.h:32
AV_RB32
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_RB32
Definition: bytestream.h:96
ff_clearvideo_decoder
const FFCodec ff_clearvideo_decoder
Definition: clearvideo.c:739
diff
static av_always_inline int diff(const struct color_info *a, const struct color_info *b, const int trans_thresh)
Definition: vf_paletteuse.c:164
CLVContext::pmb_height
int pmb_height
Definition: clearvideo.c:69
LevelCodes::bias_cb
const VLCElem * bias_cb
Definition: clearvideo.c:44
NUM_AC_CODES
#define NUM_AC_CODES
Definition: clearvideodata.h:29
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:255
frame_type
frame_type
Definition: jpeg2000_parser.c:31
pred_mv
static void pred_mv(DiracBlock *block, int stride, int x, int y, int ref)
Definition: diracdec.c:1391
AVCodecContext::extradata
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:523
clv_ac_bits
static const uint8_t clv_ac_bits[NUM_AC_CODES]
Definition: clearvideodata.h:71
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:194
LevelCodes::flags_cb
const VLCElem * flags_cb
Definition: clearvideo.c:42
lev
static LevelCodes lev[4+3+3]
Definition: clearvideo.c:79
AVCodecContext::height
int height
Definition: avcodec.h:618
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:657
CLVContext::tile_size
int tile_size
Definition: clearvideo.c:71
av_calloc
void * av_calloc(size_t nmemb, size_t size)
Definition: mem.c:262
clv_flags_bits
static const uint8_t clv_flags_bits[][16]
Definition: clearvideodata.h:80
idctdsp.h
avcodec.h
CLV_VLC_BITS
#define CLV_VLC_BITS
Definition: clearvideo.c:39
stride
#define stride
Definition: h264pred_template.c:537
ff_zigzag_direct
const uint8_t ff_zigzag_direct[64]
Definition: mathtables.c:98
mid_pred
#define mid_pred
Definition: mathops.h:98
ff_reget_buffer
int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Identical in function to ff_get_buffer(), except it reuses the existing buffer if available.
Definition: decode.c:1677
ret
ret
Definition: filter_design.txt:187
pred
static const float pred[4]
Definition: siprdata.h:259
FFSWAP
#define FFSWAP(type, a, b)
Definition: macros.h:52
copy_block
static int copy_block(AVCodecContext *avctx, AVFrame *dst, const AVFrame *src, int plane, int x, int y, int dx, int dy, int size)
Definition: clearvideo.c:217
IDCTDSPContext
Definition: idctdsp.h:43
COP
#define COP(x)
Definition: clearvideo.c:150
AV_RL32
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:92
U
#define U(x)
Definition: vpx_arith.h:37
CLVContext::luma_dc_quant
int luma_dc_quant
Definition: clearvideo.c:73
AVCodecContext
main external API structure.
Definition: avcodec.h:445
AVFrame::height
int height
Definition: frame.h:412
decode_block
static int decode_block(CLVContext *ctx, int16_t *blk, int has_ac, int ac_quant)
Definition: clearvideo.c:81
CLVContext::mb_height
int mb_height
Definition: clearvideo.c:68
MV_ESC
#define MV_ESC
Definition: clearvideodata.h:111
DCT_TEMPLATE
#define DCT_TEMPLATE(blk, step, bias, shift, dshift, OP)
Definition: clearvideo.c:125
ac_vlc
static VLCElem ac_vlc[554]
Definition: clearvideo.c:78
av_clip_uint8
#define av_clip_uint8
Definition: common.h:104
ff_vlc_init_tables_from_lengths
const av_cold VLCElem * ff_vlc_init_tables_from_lengths(VLCInitState *state, int nb_bits, int nb_codes, const int8_t *lens, int lens_wrap, const void *symbols, int symbols_wrap, int symbols_size, int offset, int flags)
Definition: vlc.c:366
AVCodecContext::coded_width
int coded_width
Bitstream width / height, may be different from width/height e.g.
Definition: avcodec.h:633
AV_PICTURE_TYPE_P
@ AV_PICTURE_TYPE_P
Predicted.
Definition: avutil.h:280
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
CLVContext::prev
AVFrame * prev
Definition: clearvideo.c:66
mvi_reset
static void mvi_reset(MVInfo *mvi, int mb_w, int mb_h, int mb_size)
Definition: clearvideo.c:344
VLC_INIT_STATIC_TABLE_FROM_LENGTHS
#define VLC_INIT_STATIC_TABLE_FROM_LENGTHS(vlc_table, nb_bits, nb_codes, lens, lens_wrap, syms, syms_wrap, syms_size, offset, flags)
Definition: vlc.h:277
AVCodecContext::codec_tag
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
Definition: avcodec.h:470
FFALIGN
#define FFALIGN(x, a)
Definition: macros.h:78
VLC_INIT_STATE
#define VLC_INIT_STATE(_table)
Definition: vlc.h:214
CLVContext::idsp
IDCTDSPContext idsp
Definition: clearvideo.c:64
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:472
AVPacket
This structure stores compressed data.
Definition: packet.h:499
clv_mv_len_counts
static const uint8_t clv_mv_len_counts[][16]
Definition: clearvideodata.h:100
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
src
INIT_CLIP pixel * src
Definition: h264pred_template.c:418
CLVContext::chroma_dc_quant
int chroma_dc_quant
Definition: clearvideo.c:73
NUM_DC_CODES
#define NUM_DC_CODES
Definition: clearvideodata.h:28
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:618
bytestream.h
bytestream2_init
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:137
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:474
AVFrame::linesize
int linesize[AV_NUM_DATA_POINTERS]
For video, a positive or negative value, which is typically indicating the size in bytes of each pict...
Definition: frame.h:385
block
The exact code depends on how similar the blocks are and how related they are to the block
Definition: filter_design.txt:207
CLVContext
Definition: clearvideo.c:62
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
CLVContext::block
int16_t block[64]
Definition: clearvideo.c:74
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
MKTAG
#define MKTAG(a, b, c, d)
Definition: macros.h:55
h
h
Definition: vp9dsp_template.c:2038
AV_CODEC_ID_CLEARVIDEO
@ AV_CODEC_ID_CLEARVIDEO
Definition: codec_id.h:278
zero_mv
static const MV zero_mv
Definition: clearvideo.c:51
tile_do_block
static int tile_do_block(AVCodecContext *avctx, AVFrame *dst, const AVFrame *src, int plane, int x, int y, int dx, int dy, int size, int bias)
Definition: clearvideo.c:364
CLVContext::ac_quant
int ac_quant
Definition: clearvideo.c:73
decode_tile
static int decode_tile(AVCodecContext *avctx, GetBitContext *gb, const LevelCodes *lc, AVFrame *dst, const AVFrame *src, int plane, int x, int y, int size, MV root_mv, MV *pred)
Definition: clearvideo.c:378
av_log2
int av_log2(unsigned v)
Definition: intmath.c:26
MVInfo::mb_size
int mb_size
Definition: clearvideo.c:56
skip
static void BS_FUNC() skip(BSCTX *bc, unsigned int n)
Skip n bits in the buffer.
Definition: bitstream_template.h:375
clv_ac_syms
static const uint16_t clv_ac_syms[NUM_AC_CODES]
Definition: clearvideodata.h:55
clv_bias_len_counts
static const uint8_t clv_bias_len_counts[][16]
Definition: clearvideodata.h:609
mv_syms
static const uint8_t mv_syms[2][16][10]
Definition: mobiclip.c:203