3 * Copyright (c) 2012-2018 Konstantin Shishkov
5 * This file is part of FFmpeg.
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.
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.
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
28 #include "bytestream.h"
33 #include "clearvideodata.h"
35 #define CLV_VLC_BITS 9
37 typedef struct LevelCodes {
47 static const MV zero_mv = { 0 };
49 typedef struct MVInfo {
58 typedef struct TileInfo {
62 struct TileInfo *child[4];
65 typedef struct CLVContext {
66 AVCodecContext *avctx;
71 int mb_width, mb_height;
72 int pmb_width, pmb_height;
77 LevelCodes lev[4 + 3 + 3]; // 0..3: Y, 4..6: U, 7..9: V
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];
83 static inline int decode_block(CLVContext *ctx, int16_t *blk, int has_ac,
86 GetBitContext *gb = &ctx->gb;
87 int idx = 1, last = 0, val, skip;
89 memset(blk, 0, sizeof(*blk) * 64);
90 blk[0] = get_vlc2(gb, ctx->dc_vlc.table, CLV_VLC_BITS, 3);
95 while (idx < 64 && !last) {
96 val = get_vlc2(gb, ctx->ac_vlc.table, CLV_VLC_BITS, 2);
98 return AVERROR_INVALIDDATA;
101 skip = (val >> 4) & 0xFF;
106 last = get_bits1(gb);
107 skip = get_bits(gb, 6);
108 val = get_sbits(gb, 8);
111 int aval = FFABS(val), sign = val < 0;
112 val = ac_quant * (2 * aval + 1);
120 return AVERROR_INVALIDDATA;
121 blk[ff_zigzag_direct[idx++]] = val;
124 return (idx <= 64 && last) ? 0 : -1;
127 #define DCT_TEMPLATE(blk, step, bias, shift, dshift, OP) \
128 const int t0 = OP(2841 * blk[1 * step] + 565 * blk[7 * step]); \
129 const int t1 = OP( 565 * blk[1 * step] - 2841 * blk[7 * step]); \
130 const int t2 = OP(1609 * blk[5 * step] + 2408 * blk[3 * step]); \
131 const int t3 = OP(2408 * blk[5 * step] - 1609 * blk[3 * step]); \
132 const int t4 = OP(1108 * blk[2 * step] - 2676 * blk[6 * step]); \
133 const int t5 = OP(2676 * blk[2 * step] + 1108 * blk[6 * step]); \
134 const int t6 = ((blk[0 * step] + blk[4 * step]) * (1 << dshift)) + bias; \
135 const int t7 = ((blk[0 * step] - blk[4 * step]) * (1 << dshift)) + bias; \
136 const int t8 = t0 + t2; \
137 const int t9 = t0 - t2; \
138 const int tA = (int)(181U * (t9 + (t1 - t3)) + 0x80) >> 8; \
139 const int tB = (int)(181U * (t9 - (t1 - t3)) + 0x80) >> 8; \
140 const int tC = t1 + t3; \
142 blk[0 * step] = (t6 + t5 + t8) >> shift; \
143 blk[1 * step] = (t7 + t4 + tA) >> shift; \
144 blk[2 * step] = (t7 - t4 + tB) >> shift; \
145 blk[3 * step] = (t6 - t5 + tC) >> shift; \
146 blk[4 * step] = (t6 - t5 - tC) >> shift; \
147 blk[5 * step] = (t7 - t4 - tB) >> shift; \
148 blk[6 * step] = (t7 + t4 - tA) >> shift; \
149 blk[7 * step] = (t6 + t5 - t8) >> shift; \
152 #define COP(x) (((x) + 4) >> 3)
154 static void clv_dct(int16_t *block)
160 for (i = 0; i < 8; i++) {
161 DCT_TEMPLATE(ptr, 1, 0x80, 8, 11, ROP);
166 for (i = 0; i < 8; i++) {
167 DCT_TEMPLATE(ptr, 8, 0x2000, 14, 8, COP);
172 static int decode_mb(CLVContext *c, int x, int y)
174 int i, has_ac[6], off;
176 for (i = 0; i < 6; i++)
177 has_ac[i] = get_bits1(&c->gb);
179 off = x * 16 + y * 16 * c->pic->linesize[0];
180 for (i = 0; i < 4; i++) {
181 if (decode_block(c, c->block, has_ac[i], c->ac_quant) < 0)
182 return AVERROR_INVALIDDATA;
183 if (!x && !(i & 1)) {
184 c->block[0] += c->top_dc[0];
185 c->top_dc[0] = c->block[0];
187 c->block[0] += c->left_dc[(i & 2) >> 1];
189 c->left_dc[(i & 2) >> 1] = c->block[0];
190 c->block[0] *= c->luma_dc_quant;
193 off += c->pic->linesize[0] * 8;
194 c->idsp.put_pixels_clamped(c->block,
195 c->pic->data[0] + off + (i & 1) * 8,
196 c->pic->linesize[0]);
199 off = x * 8 + y * 8 * c->pic->linesize[1];
200 for (i = 1; i < 3; i++) {
201 if (decode_block(c, c->block, has_ac[i + 3], c->ac_quant) < 0)
202 return AVERROR_INVALIDDATA;
204 c->block[0] += c->top_dc[i];
205 c->top_dc[i] = c->block[0];
207 c->block[0] += c->left_dc[i + 1];
209 c->left_dc[i + 1] = c->block[0];
210 c->block[0] *= c->chroma_dc_quant;
212 c->idsp.put_pixels_clamped(c->block, c->pic->data[i] + off,
213 c->pic->linesize[i]);
219 static int copy_block(AVCodecContext *avctx, AVFrame *dst, AVFrame *src,
220 int plane, int x, int y, int dx, int dy, int size)
222 int shift = plane > 0;
225 int sstride, dstride, soff, doff;
226 uint8_t *sbuf, *dbuf;
229 if (x < 0 || sx < 0 || y < 0 || sy < 0 ||
230 x + size > avctx->coded_width >> shift ||
231 y + size > avctx->coded_height >> shift ||
232 sx + size > avctx->coded_width >> shift ||
233 sy + size > avctx->coded_height >> shift)
234 return AVERROR_INVALIDDATA;
236 sstride = src->linesize[plane];
237 dstride = dst->linesize[plane];
238 soff = sx + sy * sstride;
239 sbuf = src->data[plane];
240 doff = x + y * dstride;
241 dbuf = dst->data[plane];
243 for (i = 0; i < size; i++) {
244 uint8_t *dptr = &dbuf[doff];
245 uint8_t *sptr = &sbuf[soff];
247 memcpy(dptr, sptr, size);
255 static int copyadd_block(AVCodecContext *avctx, AVFrame *dst, AVFrame *src,
256 int plane, int x, int y, int dx, int dy, int size, int bias)
258 int shift = plane > 0;
261 int sstride = src->linesize[plane];
262 int dstride = dst->linesize[plane];
263 int soff = sx + sy * sstride;
264 uint8_t *sbuf = src->data[plane];
265 int doff = x + y * dstride;
266 uint8_t *dbuf = dst->data[plane];
269 if (x < 0 || sx < 0 || y < 0 || sy < 0 ||
270 x + size > avctx->coded_width >> shift ||
271 y + size > avctx->coded_height >> shift ||
272 sx + size > avctx->coded_width >> shift ||
273 sy + size > avctx->coded_height >> shift)
274 return AVERROR_INVALIDDATA;
276 for (j = 0; j < size; j++) {
277 uint8_t *dptr = &dbuf[doff];
278 uint8_t *sptr = &sbuf[soff];
280 for (i = 0; i < size; i++) {
281 int val = sptr[i] + bias;
283 dptr[i] = av_clip_uint8(val);
293 static MV mvi_predict(MVInfo *mvi, int mb_x, int mb_y, MV diff)
296 int left_mv, right_mv, top_mv, bot_mv;
300 pred_mv = mvi->mv[mvi->mb_stride + mb_x - 1];
304 } else if ((mb_x == 0) || (mb_x == mvi->mb_w - 1)) {
305 pred_mv = mvi->mv[mb_x];
307 MV A = mvi->mv[mvi->mb_stride + mb_x - 1];
308 MV B = mvi->mv[ mb_x ];
309 MV C = mvi->mv[ mb_x + 1];
310 pred_mv.x = mid_pred(A.x, B.x, C.x);
311 pred_mv.y = mid_pred(A.y, B.y, C.y);
316 left_mv = -((mb_x * mvi->mb_size));
317 right_mv = ((mvi->mb_w - mb_x - 1) * mvi->mb_size);
318 if (res.x < left_mv) {
321 if (res.x > right_mv) {
324 top_mv = -((mb_y * mvi->mb_size));
325 bot_mv = ((mvi->mb_h - mb_y - 1) * mvi->mb_size);
326 if (res.y < top_mv) {
329 if (res.y > bot_mv) {
333 mvi->mv[mvi->mb_stride + mb_x].x = res.x + diff.x;
334 mvi->mv[mvi->mb_stride + mb_x].y = res.y + diff.y;
339 static void mvi_reset(MVInfo *mvi, int mb_w, int mb_h, int mb_size)
344 mvi->mb_size = mb_size;
345 mvi->mb_stride = mb_w;
346 memset(mvi->mv, 0, sizeof(MV) * mvi->mb_stride * 2);
349 static void mvi_update_row(MVInfo *mvi)
354 for (i = 0 ; i < mvi->mb_stride; i++) {
355 mvi->mv[i] = mvi->mv[mvi->mb_stride + i];
359 static TileInfo* decode_tile_info(GetBitContext *gb, LevelCodes *lc, int level)
366 if (lc[level].flags_cb.table) {
367 flags = get_vlc2(gb, lc[level].flags_cb.table, CLV_VLC_BITS, 2);
370 if (lc[level].mv_cb.table) {
371 uint16_t mv_code = get_vlc2(gb, lc[level].mv_cb.table, CLV_VLC_BITS, 2);
373 if (mv_code != MV_ESC) {
374 mv.x = (int8_t)(mv_code & 0xff);
375 mv.y = (int8_t)(mv_code >> 8);
377 mv.x = get_sbits(gb, 8);
378 mv.y = get_sbits(gb, 8);
382 if (lc[level].bias_cb.table) {
383 uint16_t bias_val = get_vlc2(gb, lc[level].bias_cb.table, CLV_VLC_BITS, 2);
385 if (bias_val != BIAS_ESC) {
386 bias = (int16_t)(bias_val);
388 bias = get_sbits(gb, 16);
392 ti = av_calloc(1, sizeof(*ti));
401 for (i = 0; i < 4; i++) {
402 if (ti->flags & (1 << i)) {
403 TileInfo *subti = decode_tile_info(gb, lc, level + 1);
404 ti->child[i] = subti;
412 static int tile_do_block(AVCodecContext *avctx, AVFrame *dst, AVFrame *src,
413 int plane, int x, int y, int dx, int dy, int size, int bias)
418 ret = copy_block(avctx, dst, src, plane, x, y, dx, dy, size);
420 ret = copyadd_block(avctx, dst, src, plane, x, y, dx, dy, size, bias);
426 static int restore_tree(AVCodecContext *avctx, AVFrame *dst, AVFrame *src,
427 int plane, int x, int y, int size,
428 TileInfo *tile, MV root_mv)
433 mv.x = root_mv.x + tile->mv.x;
434 mv.y = root_mv.y + tile->mv.y;
437 ret = tile_do_block(avctx, dst, src, plane, x, y, mv.x, mv.y, size, tile->bias);
439 int i, hsize = size >> 1;
441 for (i = 0; i < 4; i++) {
442 int xoff = (i & 2) == 0 ? 0 : hsize;
443 int yoff = (i & 1) == 0 ? 0 : hsize;
445 if (tile->child[i]) {
446 ret = restore_tree(avctx, dst, src, plane, x + xoff, y + yoff, hsize, tile->child[i], root_mv);
447 av_freep(&tile->child[i]);
449 ret = tile_do_block(avctx, dst, src, plane, x + xoff, y + yoff, mv.x, mv.y, hsize, tile->bias);
457 static void extend_edges(AVFrame *buf, int tile_size)
461 for (comp = 0; comp < 3; comp++) {
462 int shift = comp > 0;
463 int w = buf->width >> shift;
464 int h = buf->height >> shift;
465 int size = comp == 0 ? tile_size : tile_size >> 1;
466 int stride = buf->linesize[comp];
467 uint8_t *framebuf = buf->data[comp];
469 int right = size - (w & (size - 1));
470 int bottom = size - (h & (size - 1));
472 if ((right == size) && (bottom == size)) {
477 for (j = 0; j < h; j++) {
478 for (i = 0; i < right; i++) {
479 framebuf[off + i] = 0x80;
484 if (bottom != size) {
485 int off = h * stride;
486 for (j = 0; j < bottom; j++) {
487 for (i = 0; i < stride; i++) {
488 framebuf[off + i] = 0x80;
496 static int clv_decode_frame(AVCodecContext *avctx, void *data,
497 int *got_frame, AVPacket *avpkt)
499 const uint8_t *buf = avpkt->data;
500 int buf_size = avpkt->size;
501 CLVContext *c = avctx->priv_data;
507 bytestream2_init(&gb, buf, buf_size);
508 if (avctx->codec_tag == MKTAG('C', 'L', 'V', '1')) {
509 int skip = bytestream2_get_byte(&gb);
510 bytestream2_skip(&gb, (skip + 1) * 8);
513 frame_type = bytestream2_get_byte(&gb);
515 if ((frame_type & 0x7f) == 0x30) {
518 } else if (frame_type & 0x2) {
519 if (buf_size < c->mb_width * c->mb_height) {
520 av_log(avctx, AV_LOG_ERROR, "Packet too small\n");
521 return AVERROR_INVALIDDATA;
524 if ((ret = ff_reget_buffer(avctx, c->pic, 0)) < 0)
527 c->pic->key_frame = 1;
528 c->pic->pict_type = AV_PICTURE_TYPE_I;
530 bytestream2_get_be32(&gb); // frame size;
531 c->ac_quant = bytestream2_get_byte(&gb);
532 c->luma_dc_quant = 32;
533 c->chroma_dc_quant = 32;
535 if ((ret = init_get_bits8(&c->gb, buf + bytestream2_tell(&gb),
536 buf_size - bytestream2_tell(&gb))) < 0)
539 for (i = 0; i < 3; i++)
541 for (i = 0; i < 4; i++)
544 for (j = 0; j < c->mb_height; j++) {
545 for (i = 0; i < c->mb_width; i++) {
546 ret = decode_mb(c, i, j);
551 extend_edges(c->pic, c->tile_size);
555 if (c->pmb_width * c->pmb_height > 8LL*(buf_size - bytestream2_tell(&gb)))
556 return AVERROR_INVALIDDATA;
558 if ((ret = ff_reget_buffer(avctx, c->pic, 0)) < 0)
561 ret = av_frame_copy(c->pic, c->prev);
565 if ((ret = init_get_bits8(&c->gb, buf + bytestream2_tell(&gb),
566 buf_size - bytestream2_tell(&gb))) < 0)
569 mvi_reset(&c->mvi, c->pmb_width, c->pmb_height, 1 << c->tile_shift);
571 for (j = 0; j < c->pmb_height; j++) {
572 for (i = 0; i < c->pmb_width; i++) {
573 if (get_bits_left(&c->gb) <= 0)
574 return AVERROR_INVALIDDATA;
575 if (get_bits1(&c->gb)) {
576 MV mv = mvi_predict(&c->mvi, i, j, zero_mv);
578 for (plane = 0; plane < 3; plane++) {
579 int16_t x = plane == 0 ? i << c->tile_shift : i << (c->tile_shift - 1);
580 int16_t y = plane == 0 ? j << c->tile_shift : j << (c->tile_shift - 1);
581 int16_t size = plane == 0 ? 1 << c->tile_shift : 1 << (c->tile_shift - 1);
582 int16_t mx = plane == 0 ? mv.x : mv.x / 2;
583 int16_t my = plane == 0 ? mv.y : mv.y / 2;
585 ret = copy_block(avctx, c->pic, c->prev, plane, x, y, mx, my, size);
590 int x = i << c->tile_shift;
591 int y = j << c->tile_shift;
592 int size = 1 << c->tile_shift;
596 tile = decode_tile_info(&c->gb, &c->lev[0], 0); // Y
598 return AVERROR(ENOMEM);
599 mv = mvi_predict(&c->mvi, i, j, tile->mv);
600 ret = restore_tree(avctx, c->pic, c->prev, 0, x, y, size, tile, mv);
603 x = i << (c->tile_shift - 1);
604 y = j << (c->tile_shift - 1);
605 size = 1 << (c->tile_shift - 1);
606 cmv.x = mv.x + tile->mv.x;
607 cmv.y = mv.y + tile->mv.y;
611 tile = decode_tile_info(&c->gb, &c->lev[4], 0); // U
613 return AVERROR(ENOMEM);
614 ret = restore_tree(avctx, c->pic, c->prev, 1, x, y, size, tile, cmv);
618 tile = decode_tile_info(&c->gb, &c->lev[7], 0); // V
620 return AVERROR(ENOMEM);
621 ret = restore_tree(avctx, c->pic, c->prev, 2, x, y, size, tile, cmv);
627 mvi_update_row(&c->mvi);
629 extend_edges(c->pic, c->tile_size);
631 c->pic->key_frame = 0;
632 c->pic->pict_type = AV_PICTURE_TYPE_P;
635 if ((ret = av_frame_ref(data, c->pic)) < 0)
638 FFSWAP(AVFrame *, c->pic, c->prev);
642 if (get_bits_left(&c->gb) < 0)
643 av_log(c->avctx, AV_LOG_WARNING, "overread %d\n", -get_bits_left(&c->gb));
645 return mb_ret < 0 ? mb_ret : buf_size;
648 static av_cold int build_vlc(VLC *vlc, const uint8_t counts[16],
649 const uint16_t **syms)
651 uint8_t lens[MAX_VLC_ENTRIES];
655 for (int i = 0; i < 16; i++) {
656 unsigned count = counts[i];
657 if (count == 255) /* Special case for Y_3 table */
659 for (count += num; num < count; num++)
662 ret = ff_init_vlc_from_lengths(vlc, CLV_VLC_BITS, num, lens, 1,
663 *syms, 2, 2, 0, 0, NULL);
670 static av_cold int clv_decode_init(AVCodecContext *avctx)
672 CLVContext *const c = avctx->priv_data;
673 const uint16_t *mv_syms = clv_mv_syms, *bias_syms = clv_bias_syms;
676 if (avctx->extradata_size == 110) {
677 c->tile_size = AV_RL32(&avctx->extradata[94]);
678 } else if (avctx->extradata_size == 150) {
679 c->tile_size = AV_RB32(&avctx->extradata[134]);
680 } else if (!avctx->extradata_size) {
683 av_log(avctx, AV_LOG_ERROR, "Unsupported extradata size: %d\n", avctx->extradata_size);
684 return AVERROR_INVALIDDATA;
687 c->tile_shift = av_log2(c->tile_size);
688 if (1U << c->tile_shift != c->tile_size) {
689 av_log(avctx, AV_LOG_ERROR, "Tile size: %d, is not power of 2.\n", c->tile_size);
690 return AVERROR_INVALIDDATA;
693 avctx->pix_fmt = AV_PIX_FMT_YUV420P;
696 ret = ff_set_dimensions(avctx, FFALIGN(w, 1 << c->tile_shift), FFALIGN(h, 1 << c->tile_shift));
703 c->mb_width = FFALIGN(avctx->width, 16) >> 4;
704 c->mb_height = FFALIGN(avctx->height, 16) >> 4;
705 c->pmb_width = (w + c->tile_size - 1) >> c->tile_shift;
706 c->pmb_height = (h + c->tile_size - 1) >> c->tile_shift;
707 c->pic = av_frame_alloc();
708 c->prev = av_frame_alloc();
709 c->mvi.mv = av_calloc(c->pmb_width * 2, sizeof(*c->mvi.mv));
710 if (!c->pic || !c->prev || !c->mvi.mv)
711 return AVERROR(ENOMEM);
713 ff_idctdsp_init(&c->idsp, avctx);
714 ret = ff_init_vlc_from_lengths(&c->dc_vlc, CLV_VLC_BITS, NUM_DC_CODES,
716 clv_dc_syms, 1, 1, -63, 0, avctx);
718 av_log(avctx, AV_LOG_ERROR, "Error initialising DC VLC\n");
721 ret = ff_init_vlc_from_lengths(&c->ac_vlc, CLV_VLC_BITS, NUM_AC_CODES,
723 clv_ac_syms, 2, 2, 0, 0, avctx);
725 av_log(avctx, AV_LOG_ERROR, "Error initialising AC VLC\n");
729 for (int i = 0, j = 0, k = 0;; i++) {
730 if (0x36F & (1 << i)) {
731 ret = build_vlc(&c->lev[i].mv_cb, clv_mv_len_counts[k], &mv_syms);
736 if (i == FF_ARRAY_ELEMS(c->lev) - 1)
738 if (0x1B7 & (1 << i)) {
739 ret = ff_init_vlc_from_lengths(&c->lev[i].flags_cb, CLV_VLC_BITS, 16,
740 clv_flags_bits[j], 1,
741 clv_flags_syms[j], 1, 1, 0, 0, avctx);
745 ret = build_vlc(&c->lev[i + 1].bias_cb,
746 clv_bias_len_counts[j], &bias_syms);
755 static av_cold int clv_decode_end(AVCodecContext *avctx)
757 CLVContext *const c = avctx->priv_data;
759 av_frame_free(&c->prev);
760 av_frame_free(&c->pic);
762 av_freep(&c->mvi.mv);
764 ff_free_vlc(&c->dc_vlc);
765 ff_free_vlc(&c->ac_vlc);
766 for (int i = 0; i < FF_ARRAY_ELEMS(c->lev); i++) {
767 ff_free_vlc(&c->lev[i].mv_cb);
768 ff_free_vlc(&c->lev[i].flags_cb);
769 ff_free_vlc(&c->lev[i].bias_cb);
775 AVCodec ff_clearvideo_decoder = {
776 .name = "clearvideo",
777 .long_name = NULL_IF_CONFIG_SMALL("Iterated Systems ClearVideo"),
778 .type = AVMEDIA_TYPE_VIDEO,
779 .id = AV_CODEC_ID_CLEARVIDEO,
780 .priv_data_size = sizeof(CLVContext),
781 .init = clv_decode_init,
782 .close = clv_decode_end,
783 .decode = clv_decode_frame,
784 .capabilities = AV_CODEC_CAP_DR1,
785 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,