2012-07-08 20:51:08 +03:00
|
|
|
/*
|
|
|
|
* TechSmith Screen Codec 2 (aka Dora) decoder
|
|
|
|
* Copyright (c) 2012 Konstantin Shishkov
|
|
|
|
*
|
2012-07-12 00:56:02 +03:00
|
|
|
* This file is part of FFmpeg.
|
2012-07-08 20:51:08 +03:00
|
|
|
*
|
2012-07-12 00:56:02 +03:00
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
2012-07-08 20:51:08 +03:00
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
2012-07-12 00:56:02 +03:00
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
2012-07-08 20:51:08 +03:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2012-07-12 00:56:02 +03:00
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
2012-07-08 20:51:08 +03:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* TechSmith Screen Codec 2 decoder
|
|
|
|
*/
|
|
|
|
|
2014-03-13 14:13:33 +03:00
|
|
|
#include <inttypes.h>
|
|
|
|
|
2020-11-03 21:11:18 +02:00
|
|
|
#include "libavutil/thread.h"
|
|
|
|
|
2012-07-08 20:51:08 +03:00
|
|
|
#define BITSTREAM_READER_LE
|
|
|
|
#include "avcodec.h"
|
|
|
|
#include "bytestream.h"
|
2016-06-04 15:07:30 +02:00
|
|
|
#include "get_bits.h"
|
2012-11-21 23:34:46 +03:00
|
|
|
#include "internal.h"
|
2016-03-24 12:16:53 +02:00
|
|
|
#include "mathops.h"
|
2012-07-08 20:51:08 +03:00
|
|
|
#include "tscc2data.h"
|
|
|
|
|
2020-11-03 21:11:18 +02:00
|
|
|
#define TSCC2_VLC_BITS 9
|
|
|
|
|
2012-07-08 20:51:08 +03:00
|
|
|
typedef struct TSCC2Context {
|
|
|
|
AVCodecContext *avctx;
|
2013-03-18 17:53:51 +03:00
|
|
|
AVFrame *pic;
|
2012-07-08 20:51:08 +03:00
|
|
|
int mb_width, mb_height;
|
|
|
|
uint8_t *slice_quants;
|
|
|
|
int quant[2];
|
|
|
|
int q[2][3];
|
|
|
|
GetBitContext gb;
|
|
|
|
|
|
|
|
int block[16];
|
|
|
|
} TSCC2Context;
|
|
|
|
|
2020-11-03 21:11:18 +02:00
|
|
|
static VLC dc_vlc, nc_vlc[NUM_VLC_SETS], ac_vlc[NUM_VLC_SETS];
|
2012-07-08 20:51:08 +03:00
|
|
|
|
2020-11-03 21:11:18 +02:00
|
|
|
static av_cold void tscc2_init_vlc(VLC *vlc, int *offset, int nb_codes,
|
|
|
|
const uint8_t *lens, const void *syms,
|
|
|
|
int sym_length)
|
|
|
|
{
|
|
|
|
static VLC_TYPE vlc_buf[15442][2];
|
|
|
|
|
|
|
|
vlc->table = &vlc_buf[*offset];
|
|
|
|
vlc->table_allocated = FF_ARRAY_ELEMS(vlc_buf) - *offset;
|
|
|
|
ff_init_vlc_from_lengths(vlc, TSCC2_VLC_BITS, nb_codes,
|
|
|
|
lens, 1, syms, sym_length, sym_length, 0,
|
|
|
|
INIT_VLC_STATIC_OVERLONG | INIT_VLC_OUTPUT_LE, NULL);
|
|
|
|
*offset += vlc->table_size;
|
2012-07-08 20:51:08 +03:00
|
|
|
}
|
|
|
|
|
2020-11-03 21:11:18 +02:00
|
|
|
static av_cold void tscc2_init_vlcs(void)
|
2012-07-08 20:51:08 +03:00
|
|
|
{
|
2020-10-29 18:59:14 +02:00
|
|
|
const uint16_t *ac_vlc_syms = tscc2_ac_vlc_syms;
|
|
|
|
const uint8_t *ac_vlc_lens = tscc2_ac_vlc_lens;
|
2020-11-03 21:11:18 +02:00
|
|
|
int i, offset = 0;
|
2012-07-08 20:51:08 +03:00
|
|
|
|
2020-11-03 21:11:18 +02:00
|
|
|
tscc2_init_vlc(&dc_vlc, &offset, DC_VLC_COUNT,
|
|
|
|
tscc2_dc_vlc_lens, tscc2_dc_vlc_syms, 2);
|
2012-07-08 20:51:08 +03:00
|
|
|
|
|
|
|
for (i = 0; i < NUM_VLC_SETS; i++) {
|
2020-11-03 21:11:18 +02:00
|
|
|
tscc2_init_vlc(&nc_vlc[i], &offset, 16,
|
|
|
|
tscc2_nc_vlc_lens[i], tscc2_nc_vlc_syms[i], 1);
|
|
|
|
|
|
|
|
tscc2_init_vlc(&ac_vlc[i], &offset, tscc2_ac_vlc_sizes[i],
|
|
|
|
ac_vlc_lens, ac_vlc_syms, 2);
|
2020-10-29 18:59:14 +02:00
|
|
|
ac_vlc_lens += tscc2_ac_vlc_sizes[i];
|
|
|
|
ac_vlc_syms += tscc2_ac_vlc_sizes[i];
|
2012-07-08 20:51:08 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-07 18:44:23 +03:00
|
|
|
#define DEQUANT(val, q) (((q) * (val) + 0x80) >> 8)
|
2012-07-08 20:51:08 +03:00
|
|
|
#define DCT1D(d0, d1, d2, d3, s0, s1, s2, s3, OP) \
|
|
|
|
OP(d0, 5 * ((s0) + (s1) + (s2)) + 2 * (s3)); \
|
|
|
|
OP(d1, 5 * ((s0) - (s2) - (s3)) + 2 * (s1)); \
|
2012-07-17 18:34:58 +03:00
|
|
|
OP(d2, 5 * ((s0) - (s2) + (s3)) - 2 * (s1)); \
|
2012-07-08 20:51:08 +03:00
|
|
|
OP(d3, 5 * ((s0) - (s1) + (s2)) - 2 * (s3)); \
|
|
|
|
|
2014-06-07 18:44:23 +03:00
|
|
|
#define COL_OP(a, b) a = (b)
|
2012-07-17 19:12:41 +03:00
|
|
|
#define ROW_OP(a, b) a = ((b) + 0x20) >> 6
|
2012-07-08 20:51:08 +03:00
|
|
|
|
|
|
|
static void tscc2_idct4_put(int *in, int q[3], uint8_t *dst, int stride)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int tblk[4 * 4];
|
|
|
|
int t0, t1, t2, t3;
|
|
|
|
|
|
|
|
for (i = 0; i < 4; i++) {
|
|
|
|
t0 = DEQUANT(q[0 + (i & 1)], in[0 * 4 + i]);
|
|
|
|
t1 = DEQUANT(q[1 + (i & 1)], in[1 * 4 + i]);
|
|
|
|
t2 = DEQUANT(q[0 + (i & 1)], in[2 * 4 + i]);
|
|
|
|
t3 = DEQUANT(q[1 + (i & 1)], in[3 * 4 + i]);
|
|
|
|
DCT1D(tblk[0 * 4 + i], tblk[1 * 4 + i],
|
|
|
|
tblk[2 * 4 + i], tblk[3 * 4 + i],
|
|
|
|
t0, t1, t2, t3, COL_OP);
|
|
|
|
}
|
|
|
|
for (i = 0; i < 4; i++) {
|
|
|
|
DCT1D(dst[0], dst[1], dst[2], dst[3],
|
|
|
|
tblk[i * 4 + 0], tblk[i * 4 + 1],
|
|
|
|
tblk[i * 4 + 2], tblk[i * 4 + 3], ROW_OP);
|
|
|
|
dst += stride;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int tscc2_decode_mb(TSCC2Context *c, int *q, int vlc_set,
|
|
|
|
uint8_t *dst, int stride, int plane)
|
|
|
|
{
|
|
|
|
GetBitContext *gb = &c->gb;
|
|
|
|
int prev_dc, dc, nc, ac, bpos, val;
|
|
|
|
int i, j, k, l;
|
|
|
|
|
|
|
|
if (get_bits1(gb)) {
|
|
|
|
if (get_bits1(gb)) {
|
|
|
|
val = get_bits(gb, 8);
|
|
|
|
for (i = 0; i < 8; i++, dst += stride)
|
|
|
|
memset(dst, val, 16);
|
|
|
|
} else {
|
|
|
|
if (get_bits_left(gb) < 16 * 8 * 8)
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
for (i = 0; i < 8; i++) {
|
|
|
|
for (j = 0; j < 16; j++)
|
|
|
|
dst[j] = get_bits(gb, 8);
|
|
|
|
dst += stride;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
prev_dc = 0;
|
|
|
|
for (j = 0; j < 2; j++) {
|
|
|
|
for (k = 0; k < 4; k++) {
|
|
|
|
if (!(j | k)) {
|
|
|
|
dc = get_bits(gb, 8);
|
|
|
|
} else {
|
2020-11-03 21:11:18 +02:00
|
|
|
dc = get_vlc2(gb, dc_vlc.table, TSCC2_VLC_BITS, 2);
|
2012-07-08 20:51:08 +03:00
|
|
|
if (dc == 0x100)
|
|
|
|
dc = get_bits(gb, 8);
|
|
|
|
}
|
|
|
|
dc = (dc + prev_dc) & 0xFF;
|
|
|
|
prev_dc = dc;
|
2012-07-17 19:12:41 +03:00
|
|
|
c->block[0] = dc;
|
2012-07-08 20:51:08 +03:00
|
|
|
|
2020-11-03 21:11:18 +02:00
|
|
|
nc = get_vlc2(gb, nc_vlc[vlc_set].table, TSCC2_VLC_BITS, 1);
|
2012-07-08 20:51:08 +03:00
|
|
|
|
|
|
|
bpos = 1;
|
|
|
|
memset(c->block + 1, 0, 15 * sizeof(*c->block));
|
|
|
|
for (l = 0; l < nc; l++) {
|
2020-11-03 21:11:18 +02:00
|
|
|
ac = get_vlc2(gb, ac_vlc[vlc_set].table, TSCC2_VLC_BITS, 2);
|
2012-07-08 20:51:08 +03:00
|
|
|
if (ac == 0x1000)
|
|
|
|
ac = get_bits(gb, 12);
|
|
|
|
bpos += ac & 0xF;
|
2012-10-12 03:26:50 +03:00
|
|
|
if (bpos >= 16)
|
2012-07-08 20:51:08 +03:00
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
val = sign_extend(ac >> 4, 8);
|
2016-03-24 12:16:53 +02:00
|
|
|
c->block[ff_zigzag_scan[bpos++]] = val;
|
2012-07-08 20:51:08 +03:00
|
|
|
}
|
|
|
|
tscc2_idct4_put(c->block, q, dst + k * 4, stride);
|
|
|
|
}
|
|
|
|
dst += 4 * stride;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int tscc2_decode_slice(TSCC2Context *c, int mb_y,
|
|
|
|
const uint8_t *buf, int buf_size)
|
|
|
|
{
|
|
|
|
int i, mb_x, q, ret;
|
|
|
|
int off;
|
|
|
|
|
2013-11-05 15:44:20 +03:00
|
|
|
if ((ret = init_get_bits8(&c->gb, buf, buf_size)) < 0)
|
|
|
|
return ret;
|
2012-07-08 20:51:08 +03:00
|
|
|
|
|
|
|
for (mb_x = 0; mb_x < c->mb_width; mb_x++) {
|
|
|
|
q = c->slice_quants[mb_x + c->mb_width * mb_y];
|
|
|
|
|
|
|
|
if (q == 0 || q == 3) // skip block
|
|
|
|
continue;
|
|
|
|
for (i = 0; i < 3; i++) {
|
2013-03-18 17:53:51 +03:00
|
|
|
off = mb_x * 16 + mb_y * 8 * c->pic->linesize[i];
|
2012-07-08 20:51:08 +03:00
|
|
|
ret = tscc2_decode_mb(c, c->q[q - 1], c->quant[q - 1] - 2,
|
2013-03-18 17:53:51 +03:00
|
|
|
c->pic->data[i] + off, c->pic->linesize[i], i);
|
2012-07-08 20:51:08 +03:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int tscc2_decode_frame(AVCodecContext *avctx, void *data,
|
2012-11-13 21:35:22 +03:00
|
|
|
int *got_frame, AVPacket *avpkt)
|
2012-07-08 20:51:08 +03:00
|
|
|
{
|
|
|
|
const uint8_t *buf = avpkt->data;
|
|
|
|
int buf_size = avpkt->size;
|
|
|
|
TSCC2Context *c = avctx->priv_data;
|
|
|
|
GetByteContext gb;
|
|
|
|
uint32_t frame_type, size;
|
|
|
|
int i, val, len, pos = 0;
|
|
|
|
int num_mb = c->mb_width * c->mb_height;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
bytestream2_init(&gb, buf, buf_size);
|
|
|
|
frame_type = bytestream2_get_byte(&gb);
|
|
|
|
if (frame_type > 1) {
|
2014-03-13 14:13:33 +03:00
|
|
|
av_log(avctx, AV_LOG_ERROR, "Incorrect frame type %"PRIu32"\n",
|
|
|
|
frame_type);
|
2012-07-08 20:51:08 +03:00
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (frame_type == 0) {
|
2017-05-21 15:34:21 +02:00
|
|
|
// Skip duplicate frames
|
2012-07-08 20:51:08 +03:00
|
|
|
return buf_size;
|
|
|
|
}
|
|
|
|
|
2019-08-30 16:37:25 +02:00
|
|
|
if ((ret = ff_reget_buffer(avctx, c->pic, 0)) < 0) {
|
2017-05-21 15:34:21 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-07-08 20:51:08 +03:00
|
|
|
if (bytestream2_get_bytes_left(&gb) < 4) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Frame is too short\n");
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
|
|
|
|
|
|
|
c->quant[0] = bytestream2_get_byte(&gb);
|
|
|
|
c->quant[1] = bytestream2_get_byte(&gb);
|
|
|
|
if (c->quant[0] < 2 || c->quant[0] > NUM_VLC_SETS + 1 ||
|
|
|
|
c->quant[1] < 2 || c->quant[1] > NUM_VLC_SETS + 1) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Invalid quantisers %d / %d\n",
|
|
|
|
c->quant[0], c->quant[1]);
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < 3; i++) {
|
|
|
|
c->q[0][i] = tscc2_quants[c->quant[0] - 2][i];
|
|
|
|
c->q[1][i] = tscc2_quants[c->quant[1] - 2][i];
|
|
|
|
}
|
|
|
|
|
|
|
|
bytestream2_skip(&gb, 1);
|
|
|
|
|
|
|
|
size = bytestream2_get_le32(&gb);
|
|
|
|
if (size > bytestream2_get_bytes_left(&gb)) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Slice properties chunk is too large\n");
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < size; i++) {
|
|
|
|
val = bytestream2_get_byte(&gb);
|
|
|
|
len = val & 0x3F;
|
|
|
|
val >>= 6;
|
|
|
|
if (pos + len > num_mb) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Too many slice properties\n");
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
|
|
|
memset(c->slice_quants + pos, val, len);
|
|
|
|
pos += len;
|
|
|
|
}
|
|
|
|
if (pos < num_mb) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Too few slice properties (%d / %d)\n",
|
|
|
|
pos, num_mb);
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < c->mb_height; i++) {
|
|
|
|
size = bytestream2_peek_byte(&gb);
|
|
|
|
if (size & 1) {
|
|
|
|
size = bytestream2_get_byte(&gb) - 1;
|
|
|
|
} else {
|
|
|
|
size = bytestream2_get_le32(&gb) >> 1;
|
|
|
|
}
|
|
|
|
if (!size) {
|
|
|
|
int skip_row = 1, j, off = i * c->mb_width;
|
|
|
|
for (j = 0; j < c->mb_width; j++) {
|
2012-07-26 20:42:22 +03:00
|
|
|
if (c->slice_quants[off + j] == 1 ||
|
|
|
|
c->slice_quants[off + j] == 2) {
|
2012-07-08 20:51:08 +03:00
|
|
|
skip_row = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!skip_row) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Non-skip row with zero size\n");
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (bytestream2_get_bytes_left(&gb) < size) {
|
2014-03-13 14:13:33 +03:00
|
|
|
av_log(avctx, AV_LOG_ERROR, "Invalid slice size (%"PRIu32"/%u)\n",
|
2012-07-08 20:51:08 +03:00
|
|
|
size, bytestream2_get_bytes_left(&gb));
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
|
|
|
ret = tscc2_decode_slice(c, i, buf + bytestream2_tell(&gb), size);
|
|
|
|
if (ret) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Error decoding slice %d\n", i);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
bytestream2_skip(&gb, size);
|
|
|
|
}
|
|
|
|
|
2012-11-13 21:35:22 +03:00
|
|
|
*got_frame = 1;
|
2013-03-18 17:53:51 +03:00
|
|
|
if ((ret = av_frame_ref(data, c->pic)) < 0)
|
2012-11-21 23:34:46 +03:00
|
|
|
return ret;
|
2012-07-08 20:51:08 +03:00
|
|
|
|
|
|
|
/* always report that the buffer was completely consumed */
|
|
|
|
return buf_size;
|
|
|
|
}
|
|
|
|
|
2013-03-18 17:53:51 +03:00
|
|
|
static av_cold int tscc2_decode_end(AVCodecContext *avctx)
|
|
|
|
{
|
|
|
|
TSCC2Context * const c = avctx->priv_data;
|
|
|
|
|
|
|
|
av_frame_free(&c->pic);
|
|
|
|
av_freep(&c->slice_quants);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-07-08 20:51:08 +03:00
|
|
|
static av_cold int tscc2_decode_init(AVCodecContext *avctx)
|
|
|
|
{
|
|
|
|
TSCC2Context * const c = avctx->priv_data;
|
2020-11-03 21:11:18 +02:00
|
|
|
static AVOnce init_static_once = AV_ONCE_INIT;
|
2012-07-08 20:51:08 +03:00
|
|
|
|
|
|
|
c->avctx = avctx;
|
|
|
|
|
2012-10-06 13:10:34 +03:00
|
|
|
avctx->pix_fmt = AV_PIX_FMT_YUV444P;
|
2012-07-08 20:51:08 +03:00
|
|
|
|
|
|
|
c->mb_width = FFALIGN(avctx->width, 16) >> 4;
|
|
|
|
c->mb_height = FFALIGN(avctx->height, 8) >> 3;
|
|
|
|
c->slice_quants = av_malloc(c->mb_width * c->mb_height);
|
|
|
|
if (!c->slice_quants) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Cannot allocate slice information\n");
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
}
|
|
|
|
|
2013-03-18 17:53:51 +03:00
|
|
|
c->pic = av_frame_alloc();
|
2020-09-13 03:34:19 +02:00
|
|
|
if (!c->pic)
|
2013-03-18 17:53:51 +03:00
|
|
|
return AVERROR(ENOMEM);
|
2012-07-08 20:51:08 +03:00
|
|
|
|
2020-11-03 21:11:18 +02:00
|
|
|
ff_thread_once(&init_static_once, tscc2_init_vlcs);
|
|
|
|
|
2012-07-08 20:51:08 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
AVCodec ff_tscc2_decoder = {
|
|
|
|
.name = "tscc2",
|
2013-10-03 23:57:53 +03:00
|
|
|
.long_name = NULL_IF_CONFIG_SMALL("TechSmith Screen Codec 2"),
|
2012-07-08 20:51:08 +03:00
|
|
|
.type = AVMEDIA_TYPE_VIDEO,
|
2012-08-05 12:11:04 +03:00
|
|
|
.id = AV_CODEC_ID_TSCC2,
|
2012-07-08 20:51:08 +03:00
|
|
|
.priv_data_size = sizeof(TSCC2Context),
|
|
|
|
.init = tscc2_decode_init,
|
|
|
|
.close = tscc2_decode_end,
|
|
|
|
.decode = tscc2_decode_frame,
|
2015-07-07 02:41:27 +02:00
|
|
|
.capabilities = AV_CODEC_CAP_DR1,
|
2020-11-15 10:53:00 +02:00
|
|
|
.caps_internal = FF_CODEC_CAP_INIT_CLEANUP | FF_CODEC_CAP_INIT_THREADSAFE,
|
2012-07-08 20:51:08 +03:00
|
|
|
};
|