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
27 #include "libavutil/mem_internal.h"
28 #include "libavutil/thread.h"
31 #include "bytestream.h"
36 #include "clearvideodata.h"
38 #define CLV_VLC_BITS 9
40 typedef struct LevelCodes {
50 static const MV zero_mv = { 0 };
52 typedef struct MVInfo {
61 typedef struct TileInfo {
65 struct TileInfo *child[4];
68 typedef struct CLVContext {
69 AVCodecContext *avctx;
74 int mb_width, mb_height;
75 int pmb_width, pmb_height;
79 int luma_dc_quant, chroma_dc_quant, ac_quant;
80 DECLARE_ALIGNED(16, int16_t, block)[64];
81 int top_dc[3], left_dc[4];
84 static VLC dc_vlc, ac_vlc;
85 static LevelCodes lev[4 + 3 + 3]; // 0..3: Y, 4..6: U, 7..9: V
86 static VLC_TYPE vlc_buf[16716][2];
88 static inline int decode_block(CLVContext *ctx, int16_t *blk, int has_ac,
91 GetBitContext *gb = &ctx->gb;
92 int idx = 1, last = 0, val, skip;
94 memset(blk, 0, sizeof(*blk) * 64);
95 blk[0] = get_vlc2(gb, dc_vlc.table, CLV_VLC_BITS, 3);
100 while (idx < 64 && !last) {
101 val = get_vlc2(gb, ac_vlc.table, CLV_VLC_BITS, 2);
103 return AVERROR_INVALIDDATA;
106 skip = (val >> 4) & 0xFF;
111 last = get_bits1(gb);
112 skip = get_bits(gb, 6);
113 val = get_sbits(gb, 8);
116 int aval = FFABS(val), sign = val < 0;
117 val = ac_quant * (2 * aval + 1);
125 return AVERROR_INVALIDDATA;
126 blk[ff_zigzag_direct[idx++]] = val;
129 return (idx <= 64 && last) ? 0 : -1;
132 #define DCT_TEMPLATE(blk, step, bias, shift, dshift, OP) \
133 const int t0 = OP(2841 * blk[1 * step] + 565 * blk[7 * step]); \
134 const int t1 = OP( 565 * blk[1 * step] - 2841 * blk[7 * step]); \
135 const int t2 = OP(1609 * blk[5 * step] + 2408 * blk[3 * step]); \
136 const int t3 = OP(2408 * blk[5 * step] - 1609 * blk[3 * step]); \
137 const int t4 = OP(1108 * blk[2 * step] - 2676 * blk[6 * step]); \
138 const int t5 = OP(2676 * blk[2 * step] + 1108 * blk[6 * step]); \
139 const int t6 = ((blk[0 * step] + blk[4 * step]) * (1 << dshift)) + bias; \
140 const int t7 = ((blk[0 * step] - blk[4 * step]) * (1 << dshift)) + bias; \
141 const int t8 = t0 + t2; \
142 const int t9 = t0 - t2; \
143 const int tA = (int)(181U * (t9 + (t1 - t3)) + 0x80) >> 8; \
144 const int tB = (int)(181U * (t9 - (t1 - t3)) + 0x80) >> 8; \
145 const int tC = t1 + t3; \
147 blk[0 * step] = (t6 + t5 + t8) >> shift; \
148 blk[1 * step] = (t7 + t4 + tA) >> shift; \
149 blk[2 * step] = (t7 - t4 + tB) >> shift; \
150 blk[3 * step] = (t6 - t5 + tC) >> shift; \
151 blk[4 * step] = (t6 - t5 - tC) >> shift; \
152 blk[5 * step] = (t7 - t4 - tB) >> shift; \
153 blk[6 * step] = (t7 + t4 - tA) >> shift; \
154 blk[7 * step] = (t6 + t5 - t8) >> shift; \
157 #define COP(x) (((x) + 4) >> 3)
159 static void clv_dct(int16_t *block)
165 for (i = 0; i < 8; i++) {
166 DCT_TEMPLATE(ptr, 1, 0x80, 8, 11, ROP);
171 for (i = 0; i < 8; i++) {
172 DCT_TEMPLATE(ptr, 8, 0x2000, 14, 8, COP);
177 static int decode_mb(CLVContext *c, int x, int y)
179 int i, has_ac[6], off;
181 for (i = 0; i < 6; i++)
182 has_ac[i] = get_bits1(&c->gb);
184 off = x * 16 + y * 16 * c->pic->linesize[0];
185 for (i = 0; i < 4; i++) {
186 if (decode_block(c, c->block, has_ac[i], c->ac_quant) < 0)
187 return AVERROR_INVALIDDATA;
188 if (!x && !(i & 1)) {
189 c->block[0] += c->top_dc[0];
190 c->top_dc[0] = c->block[0];
192 c->block[0] += c->left_dc[(i & 2) >> 1];
194 c->left_dc[(i & 2) >> 1] = c->block[0];
195 c->block[0] *= c->luma_dc_quant;
198 off += c->pic->linesize[0] * 8;
199 c->idsp.put_pixels_clamped(c->block,
200 c->pic->data[0] + off + (i & 1) * 8,
201 c->pic->linesize[0]);
204 off = x * 8 + y * 8 * c->pic->linesize[1];
205 for (i = 1; i < 3; i++) {
206 if (decode_block(c, c->block, has_ac[i + 3], c->ac_quant) < 0)
207 return AVERROR_INVALIDDATA;
209 c->block[0] += c->top_dc[i];
210 c->top_dc[i] = c->block[0];
212 c->block[0] += c->left_dc[i + 1];
214 c->left_dc[i + 1] = c->block[0];
215 c->block[0] *= c->chroma_dc_quant;
217 c->idsp.put_pixels_clamped(c->block, c->pic->data[i] + off,
218 c->pic->linesize[i]);
224 static int copy_block(AVCodecContext *avctx, AVFrame *dst, AVFrame *src,
225 int plane, int x, int y, int dx, int dy, int size)
227 int shift = plane > 0;
230 int sstride, dstride, soff, doff;
231 uint8_t *sbuf, *dbuf;
234 if (x < 0 || sx < 0 || y < 0 || sy < 0 ||
235 x + size > avctx->coded_width >> shift ||
236 y + size > avctx->coded_height >> shift ||
237 sx + size > avctx->coded_width >> shift ||
238 sy + size > avctx->coded_height >> shift)
239 return AVERROR_INVALIDDATA;
241 sstride = src->linesize[plane];
242 dstride = dst->linesize[plane];
243 soff = sx + sy * sstride;
244 sbuf = src->data[plane];
245 doff = x + y * dstride;
246 dbuf = dst->data[plane];
248 for (i = 0; i < size; i++) {
249 uint8_t *dptr = &dbuf[doff];
250 uint8_t *sptr = &sbuf[soff];
252 memcpy(dptr, sptr, size);
260 static int copyadd_block(AVCodecContext *avctx, AVFrame *dst, AVFrame *src,
261 int plane, int x, int y, int dx, int dy, int size, int bias)
263 int shift = plane > 0;
266 int sstride = src->linesize[plane];
267 int dstride = dst->linesize[plane];
268 int soff = sx + sy * sstride;
269 uint8_t *sbuf = src->data[plane];
270 int doff = x + y * dstride;
271 uint8_t *dbuf = dst->data[plane];
274 if (x < 0 || sx < 0 || y < 0 || sy < 0 ||
275 x + size > avctx->coded_width >> shift ||
276 y + size > avctx->coded_height >> shift ||
277 sx + size > avctx->coded_width >> shift ||
278 sy + size > avctx->coded_height >> shift)
279 return AVERROR_INVALIDDATA;
281 for (j = 0; j < size; j++) {
282 uint8_t *dptr = &dbuf[doff];
283 uint8_t *sptr = &sbuf[soff];
285 for (i = 0; i < size; i++) {
286 int val = sptr[i] + bias;
288 dptr[i] = av_clip_uint8(val);
298 static MV mvi_predict(MVInfo *mvi, int mb_x, int mb_y, MV diff)
301 int left_mv, right_mv, top_mv, bot_mv;
305 pred_mv = mvi->mv[mvi->mb_stride + mb_x - 1];
309 } else if ((mb_x == 0) || (mb_x == mvi->mb_w - 1)) {
310 pred_mv = mvi->mv[mb_x];
312 MV A = mvi->mv[mvi->mb_stride + mb_x - 1];
313 MV B = mvi->mv[ mb_x ];
314 MV C = mvi->mv[ mb_x + 1];
315 pred_mv.x = mid_pred(A.x, B.x, C.x);
316 pred_mv.y = mid_pred(A.y, B.y, C.y);
321 left_mv = -((mb_x * mvi->mb_size));
322 right_mv = ((mvi->mb_w - mb_x - 1) * mvi->mb_size);
323 if (res.x < left_mv) {
326 if (res.x > right_mv) {
329 top_mv = -((mb_y * mvi->mb_size));
330 bot_mv = ((mvi->mb_h - mb_y - 1) * mvi->mb_size);
331 if (res.y < top_mv) {
334 if (res.y > bot_mv) {
338 mvi->mv[mvi->mb_stride + mb_x].x = res.x + diff.x;
339 mvi->mv[mvi->mb_stride + mb_x].y = res.y + diff.y;
344 static void mvi_reset(MVInfo *mvi, int mb_w, int mb_h, int mb_size)
349 mvi->mb_size = mb_size;
350 mvi->mb_stride = mb_w;
351 memset(mvi->mv, 0, sizeof(MV) * mvi->mb_stride * 2);
354 static void mvi_update_row(MVInfo *mvi)
359 for (i = 0 ; i < mvi->mb_stride; i++) {
360 mvi->mv[i] = mvi->mv[mvi->mb_stride + i];
364 static TileInfo *decode_tile_info(GetBitContext *gb, const LevelCodes *lc, int level)
371 if (lc[level].flags_cb.table) {
372 flags = get_vlc2(gb, lc[level].flags_cb.table, CLV_VLC_BITS, 2);
375 if (lc[level].mv_cb.table) {
376 uint16_t mv_code = get_vlc2(gb, lc[level].mv_cb.table, CLV_VLC_BITS, 2);
378 if (mv_code != MV_ESC) {
379 mv.x = (int8_t)(mv_code & 0xff);
380 mv.y = (int8_t)(mv_code >> 8);
382 mv.x = get_sbits(gb, 8);
383 mv.y = get_sbits(gb, 8);
387 if (lc[level].bias_cb.table) {
388 uint16_t bias_val = get_vlc2(gb, lc[level].bias_cb.table, CLV_VLC_BITS, 2);
390 if (bias_val != BIAS_ESC) {
391 bias = (int16_t)(bias_val);
393 bias = get_sbits(gb, 16);
397 ti = av_calloc(1, sizeof(*ti));
406 for (i = 0; i < 4; i++) {
407 if (ti->flags & (1 << i)) {
408 TileInfo *subti = decode_tile_info(gb, lc, level + 1);
409 ti->child[i] = subti;
417 static int tile_do_block(AVCodecContext *avctx, AVFrame *dst, AVFrame *src,
418 int plane, int x, int y, int dx, int dy, int size, int bias)
423 ret = copy_block(avctx, dst, src, plane, x, y, dx, dy, size);
425 ret = copyadd_block(avctx, dst, src, plane, x, y, dx, dy, size, bias);
431 static int restore_tree(AVCodecContext *avctx, AVFrame *dst, AVFrame *src,
432 int plane, int x, int y, int size,
433 TileInfo *tile, MV root_mv)
438 mv.x = root_mv.x + tile->mv.x;
439 mv.y = root_mv.y + tile->mv.y;
442 ret = tile_do_block(avctx, dst, src, plane, x, y, mv.x, mv.y, size, tile->bias);
444 int i, hsize = size >> 1;
446 for (i = 0; i < 4; i++) {
447 int xoff = (i & 2) == 0 ? 0 : hsize;
448 int yoff = (i & 1) == 0 ? 0 : hsize;
450 if (tile->child[i]) {
451 ret = restore_tree(avctx, dst, src, plane, x + xoff, y + yoff, hsize, tile->child[i], root_mv);
452 av_freep(&tile->child[i]);
454 ret = tile_do_block(avctx, dst, src, plane, x + xoff, y + yoff, mv.x, mv.y, hsize, tile->bias);
462 static void extend_edges(AVFrame *buf, int tile_size)
466 for (comp = 0; comp < 3; comp++) {
467 int shift = comp > 0;
468 int w = buf->width >> shift;
469 int h = buf->height >> shift;
470 int size = comp == 0 ? tile_size : tile_size >> 1;
471 int stride = buf->linesize[comp];
472 uint8_t *framebuf = buf->data[comp];
474 int right = size - (w & (size - 1));
475 int bottom = size - (h & (size - 1));
477 if ((right == size) && (bottom == size)) {
482 for (j = 0; j < h; j++) {
483 for (i = 0; i < right; i++) {
484 framebuf[off + i] = 0x80;
489 if (bottom != size) {
490 int off = h * stride;
491 for (j = 0; j < bottom; j++) {
492 for (i = 0; i < stride; i++) {
493 framebuf[off + i] = 0x80;
501 static int clv_decode_frame(AVCodecContext *avctx, void *data,
502 int *got_frame, AVPacket *avpkt)
504 const uint8_t *buf = avpkt->data;
505 int buf_size = avpkt->size;
506 CLVContext *c = avctx->priv_data;
512 bytestream2_init(&gb, buf, buf_size);
513 if (avctx->codec_tag == MKTAG('C', 'L', 'V', '1')) {
514 int skip = bytestream2_get_byte(&gb);
515 bytestream2_skip(&gb, (skip + 1) * 8);
518 frame_type = bytestream2_get_byte(&gb);
520 if ((frame_type & 0x7f) == 0x30) {
523 } else if (frame_type & 0x2) {
524 if (buf_size < c->mb_width * c->mb_height) {
525 av_log(avctx, AV_LOG_ERROR, "Packet too small\n");
526 return AVERROR_INVALIDDATA;
529 if ((ret = ff_reget_buffer(avctx, c->pic, 0)) < 0)
532 c->pic->key_frame = 1;
533 c->pic->pict_type = AV_PICTURE_TYPE_I;
535 bytestream2_get_be32(&gb); // frame size;
536 c->ac_quant = bytestream2_get_byte(&gb);
537 c->luma_dc_quant = 32;
538 c->chroma_dc_quant = 32;
540 if ((ret = init_get_bits8(&c->gb, buf + bytestream2_tell(&gb),
541 buf_size - bytestream2_tell(&gb))) < 0)
544 for (i = 0; i < 3; i++)
546 for (i = 0; i < 4; i++)
549 for (j = 0; j < c->mb_height; j++) {
550 for (i = 0; i < c->mb_width; i++) {
551 ret = decode_mb(c, i, j);
556 extend_edges(c->pic, c->tile_size);
560 if (c->pmb_width * c->pmb_height > 8LL*(buf_size - bytestream2_tell(&gb)))
561 return AVERROR_INVALIDDATA;
563 if ((ret = ff_reget_buffer(avctx, c->pic, 0)) < 0)
566 ret = av_frame_copy(c->pic, c->prev);
570 if ((ret = init_get_bits8(&c->gb, buf + bytestream2_tell(&gb),
571 buf_size - bytestream2_tell(&gb))) < 0)
574 mvi_reset(&c->mvi, c->pmb_width, c->pmb_height, 1 << c->tile_shift);
576 for (j = 0; j < c->pmb_height; j++) {
577 for (i = 0; i < c->pmb_width; i++) {
578 if (get_bits_left(&c->gb) <= 0)
579 return AVERROR_INVALIDDATA;
580 if (get_bits1(&c->gb)) {
581 MV mv = mvi_predict(&c->mvi, i, j, zero_mv);
583 for (plane = 0; plane < 3; plane++) {
584 int16_t x = plane == 0 ? i << c->tile_shift : i << (c->tile_shift - 1);
585 int16_t y = plane == 0 ? j << c->tile_shift : j << (c->tile_shift - 1);
586 int16_t size = plane == 0 ? 1 << c->tile_shift : 1 << (c->tile_shift - 1);
587 int16_t mx = plane == 0 ? mv.x : mv.x / 2;
588 int16_t my = plane == 0 ? mv.y : mv.y / 2;
590 ret = copy_block(avctx, c->pic, c->prev, plane, x, y, mx, my, size);
595 int x = i << c->tile_shift;
596 int y = j << c->tile_shift;
597 int size = 1 << c->tile_shift;
601 tile = decode_tile_info(&c->gb, &lev[0], 0); // Y
603 return AVERROR(ENOMEM);
604 mv = mvi_predict(&c->mvi, i, j, tile->mv);
605 ret = restore_tree(avctx, c->pic, c->prev, 0, x, y, size, tile, mv);
608 x = i << (c->tile_shift - 1);
609 y = j << (c->tile_shift - 1);
610 size = 1 << (c->tile_shift - 1);
611 cmv.x = mv.x + tile->mv.x;
612 cmv.y = mv.y + tile->mv.y;
616 tile = decode_tile_info(&c->gb, &lev[4], 0); // U
618 return AVERROR(ENOMEM);
619 ret = restore_tree(avctx, c->pic, c->prev, 1, x, y, size, tile, cmv);
623 tile = decode_tile_info(&c->gb, &lev[7], 0); // V
625 return AVERROR(ENOMEM);
626 ret = restore_tree(avctx, c->pic, c->prev, 2, x, y, size, tile, cmv);
632 mvi_update_row(&c->mvi);
634 extend_edges(c->pic, c->tile_size);
636 c->pic->key_frame = 0;
637 c->pic->pict_type = AV_PICTURE_TYPE_P;
640 if ((ret = av_frame_ref(data, c->pic)) < 0)
643 FFSWAP(AVFrame *, c->pic, c->prev);
647 if (get_bits_left(&c->gb) < 0)
648 av_log(c->avctx, AV_LOG_WARNING, "overread %d\n", -get_bits_left(&c->gb));
650 return mb_ret < 0 ? mb_ret : buf_size;
653 static av_cold void build_vlc(VLC *vlc, const uint8_t counts[16],
654 const uint16_t **syms, unsigned *offset)
656 uint8_t lens[MAX_VLC_ENTRIES];
659 for (int i = 0; i < 16; i++) {
660 unsigned count = counts[i];
661 if (count == 255) /* Special case for Y_3 table */
663 for (count += num; num < count; num++)
666 vlc->table = &vlc_buf[*offset];
667 vlc->table_allocated = FF_ARRAY_ELEMS(vlc_buf) - *offset;
668 ff_init_vlc_from_lengths(vlc, CLV_VLC_BITS, num, lens, 1,
669 *syms, 2, 2, 0, INIT_VLC_STATIC_OVERLONG, NULL);
671 *offset += vlc->table_size;
674 static av_cold void clv_init_static(void)
676 const uint16_t *mv_syms = clv_mv_syms, *bias_syms = clv_bias_syms;
678 INIT_VLC_STATIC_FROM_LENGTHS(&dc_vlc, CLV_VLC_BITS, NUM_DC_CODES,
680 clv_dc_syms, 1, 1, -63, 0, 1104);
681 INIT_VLC_STATIC_FROM_LENGTHS(&ac_vlc, CLV_VLC_BITS, NUM_AC_CODES,
683 clv_ac_syms, 2, 2, 0, 0, 554);
684 for (unsigned i = 0, j = 0, k = 0, offset = 0;; i++) {
685 if (0x36F & (1 << i)) {
686 build_vlc(&lev[i].mv_cb, clv_mv_len_counts[k], &mv_syms, &offset);
689 if (i == FF_ARRAY_ELEMS(lev) - 1)
691 if (0x1B7 & (1 << i)) {
692 lev[i].flags_cb.table = &vlc_buf[offset];
693 lev[i].flags_cb.table_allocated = FF_ARRAY_ELEMS(vlc_buf) - offset;
694 ff_init_vlc_from_lengths(&lev[i].flags_cb, CLV_VLC_BITS, 16,
695 clv_flags_bits[j], 1,
696 clv_flags_syms[j], 1, 1,
697 0, INIT_VLC_STATIC_OVERLONG, NULL);
698 offset += lev[i].flags_cb.table_size;
700 build_vlc(&lev[i + 1].bias_cb, clv_bias_len_counts[j],
701 &bias_syms, &offset);
707 static av_cold int clv_decode_init(AVCodecContext *avctx)
709 static AVOnce init_static_once = AV_ONCE_INIT;
710 CLVContext *const c = avctx->priv_data;
713 if (avctx->extradata_size == 110) {
714 c->tile_size = AV_RL32(&avctx->extradata[94]);
715 } else if (avctx->extradata_size == 150) {
716 c->tile_size = AV_RB32(&avctx->extradata[134]);
717 } else if (!avctx->extradata_size) {
720 av_log(avctx, AV_LOG_ERROR, "Unsupported extradata size: %d\n", avctx->extradata_size);
721 return AVERROR_INVALIDDATA;
724 c->tile_shift = av_log2(c->tile_size);
725 if (1U << c->tile_shift != c->tile_size) {
726 av_log(avctx, AV_LOG_ERROR, "Tile size: %d, is not power of 2.\n", c->tile_size);
727 return AVERROR_INVALIDDATA;
730 avctx->pix_fmt = AV_PIX_FMT_YUV420P;
733 ret = ff_set_dimensions(avctx, FFALIGN(w, 1 << c->tile_shift), FFALIGN(h, 1 << c->tile_shift));
740 c->mb_width = FFALIGN(avctx->width, 16) >> 4;
741 c->mb_height = FFALIGN(avctx->height, 16) >> 4;
742 c->pmb_width = (w + c->tile_size - 1) >> c->tile_shift;
743 c->pmb_height = (h + c->tile_size - 1) >> c->tile_shift;
744 c->pic = av_frame_alloc();
745 c->prev = av_frame_alloc();
746 c->mvi.mv = av_calloc(c->pmb_width * 2, sizeof(*c->mvi.mv));
747 if (!c->pic || !c->prev || !c->mvi.mv)
748 return AVERROR(ENOMEM);
750 ff_idctdsp_init(&c->idsp, avctx);
752 ff_thread_once(&init_static_once, clv_init_static);
757 static av_cold int clv_decode_end(AVCodecContext *avctx)
759 CLVContext *const c = avctx->priv_data;
761 av_frame_free(&c->prev);
762 av_frame_free(&c->pic);
764 av_freep(&c->mvi.mv);
769 AVCodec ff_clearvideo_decoder = {
770 .name = "clearvideo",
771 .long_name = NULL_IF_CONFIG_SMALL("Iterated Systems ClearVideo"),
772 .type = AVMEDIA_TYPE_VIDEO,
773 .id = AV_CODEC_ID_CLEARVIDEO,
774 .priv_data_size = sizeof(CLVContext),
775 .init = clv_decode_init,
776 .close = clv_decode_end,
777 .decode = clv_decode_frame,
778 .capabilities = AV_CODEC_CAP_DR1,
779 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,