00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00027 #define BITSTREAM_READER_LE
00028 #include "avcodec.h"
00029 #include "get_bits.h"
00030 #include "bytestream.h"
00031 #include "tscc2data.h"
00032
00033 typedef struct TSCC2Context {
00034 AVCodecContext *avctx;
00035 AVFrame pic;
00036 int mb_width, mb_height;
00037 uint8_t *slice_quants;
00038 int quant[2];
00039 int q[2][3];
00040 GetBitContext gb;
00041
00042 VLC dc_vlc, nc_vlc[NUM_VLC_SETS], ac_vlc[NUM_VLC_SETS];
00043 int block[16];
00044 } TSCC2Context;
00045
00046 static av_cold void free_vlcs(TSCC2Context *c)
00047 {
00048 int i;
00049
00050 ff_free_vlc(&c->dc_vlc);
00051 for (i = 0; i < NUM_VLC_SETS; i++) {
00052 ff_free_vlc(c->nc_vlc + i);
00053 ff_free_vlc(c->ac_vlc + i);
00054 }
00055 }
00056
00057 static av_cold int init_vlcs(TSCC2Context *c)
00058 {
00059 int i, ret;
00060
00061 ret = ff_init_vlc_sparse(&c->dc_vlc, 9, DC_VLC_COUNT,
00062 tscc2_dc_vlc_bits, 1, 1,
00063 tscc2_dc_vlc_codes, 2, 2,
00064 tscc2_dc_vlc_syms, 2, 2, INIT_VLC_LE);
00065 if (ret)
00066 return ret;
00067
00068 for (i = 0; i < NUM_VLC_SETS; i++) {
00069 ret = ff_init_vlc_sparse(c->nc_vlc + i, 9, 16,
00070 tscc2_nc_vlc_bits[i], 1, 1,
00071 tscc2_nc_vlc_codes[i], 2, 2,
00072 tscc2_nc_vlc_syms, 1, 1, INIT_VLC_LE);
00073 if (ret) {
00074 free_vlcs(c);
00075 return ret;
00076 }
00077 ret = ff_init_vlc_sparse(c->ac_vlc + i, 9, tscc2_ac_vlc_sizes[i],
00078 tscc2_ac_vlc_bits[i], 1, 1,
00079 tscc2_ac_vlc_codes[i], 2, 2,
00080 tscc2_ac_vlc_syms[i], 2, 2, INIT_VLC_LE);
00081 if (ret) {
00082 free_vlcs(c);
00083 return ret;
00084 }
00085 }
00086
00087 return 0;
00088 }
00089
00090 #define DEQUANT(val, q) ((q * val + 0x80) >> 8)
00091 #define DCT1D(d0, d1, d2, d3, s0, s1, s2, s3, OP) \
00092 OP(d0, 5 * ((s0) + (s1) + (s2)) + 2 * (s3)); \
00093 OP(d1, 5 * ((s0) - (s2) - (s3)) + 2 * (s1)); \
00094 OP(d2, 5 * ((s0) - (s2) + (s3)) - 2 * (s1)); \
00095 OP(d3, 5 * ((s0) - (s1) + (s2)) - 2 * (s3)); \
00096
00097 #define COL_OP(a, b) a = b
00098 #define ROW_OP(a, b) a = ((b) + 0x20) >> 6
00099
00100 static void tscc2_idct4_put(int *in, int q[3], uint8_t *dst, int stride)
00101 {
00102 int i;
00103 int tblk[4 * 4];
00104 int t0, t1, t2, t3;
00105
00106 for (i = 0; i < 4; i++) {
00107 t0 = DEQUANT(q[0 + (i & 1)], in[0 * 4 + i]);
00108 t1 = DEQUANT(q[1 + (i & 1)], in[1 * 4 + i]);
00109 t2 = DEQUANT(q[0 + (i & 1)], in[2 * 4 + i]);
00110 t3 = DEQUANT(q[1 + (i & 1)], in[3 * 4 + i]);
00111 DCT1D(tblk[0 * 4 + i], tblk[1 * 4 + i],
00112 tblk[2 * 4 + i], tblk[3 * 4 + i],
00113 t0, t1, t2, t3, COL_OP);
00114 }
00115 for (i = 0; i < 4; i++) {
00116 DCT1D(dst[0], dst[1], dst[2], dst[3],
00117 tblk[i * 4 + 0], tblk[i * 4 + 1],
00118 tblk[i * 4 + 2], tblk[i * 4 + 3], ROW_OP);
00119 dst += stride;
00120 }
00121 }
00122
00123 static int tscc2_decode_mb(TSCC2Context *c, int *q, int vlc_set,
00124 uint8_t *dst, int stride, int plane)
00125 {
00126 GetBitContext *gb = &c->gb;
00127 int prev_dc, dc, nc, ac, bpos, val;
00128 int i, j, k, l;
00129
00130 if (get_bits1(gb)) {
00131 if (get_bits1(gb)) {
00132 val = get_bits(gb, 8);
00133 for (i = 0; i < 8; i++, dst += stride)
00134 memset(dst, val, 16);
00135 } else {
00136 if (get_bits_left(gb) < 16 * 8 * 8)
00137 return AVERROR_INVALIDDATA;
00138 for (i = 0; i < 8; i++) {
00139 for (j = 0; j < 16; j++)
00140 dst[j] = get_bits(gb, 8);
00141 dst += stride;
00142 }
00143 }
00144 return 0;
00145 }
00146
00147 prev_dc = 0;
00148 for (j = 0; j < 2; j++) {
00149 for (k = 0; k < 4; k++) {
00150 if (!(j | k)) {
00151 dc = get_bits(gb, 8);
00152 } else {
00153 dc = get_vlc2(gb, c->dc_vlc.table, 9, 2);
00154 if (dc == -1)
00155 return AVERROR_INVALIDDATA;
00156 if (dc == 0x100)
00157 dc = get_bits(gb, 8);
00158 }
00159 dc = (dc + prev_dc) & 0xFF;
00160 prev_dc = dc;
00161 c->block[0] = dc;
00162
00163 nc = get_vlc2(gb, c->nc_vlc[vlc_set].table, 9, 1);
00164 if (nc == -1)
00165 return AVERROR_INVALIDDATA;
00166
00167 bpos = 1;
00168 memset(c->block + 1, 0, 15 * sizeof(*c->block));
00169 for (l = 0; l < nc; l++) {
00170 ac = get_vlc2(gb, c->ac_vlc[vlc_set].table, 9, 2);
00171 if (ac == -1)
00172 return AVERROR_INVALIDDATA;
00173 if (ac == 0x1000)
00174 ac = get_bits(gb, 12);
00175 bpos += ac & 0xF;
00176 if (bpos >= 64)
00177 return AVERROR_INVALIDDATA;
00178 val = sign_extend(ac >> 4, 8);
00179 c->block[tscc2_zigzag[bpos++]] = val;
00180 }
00181 tscc2_idct4_put(c->block, q, dst + k * 4, stride);
00182 }
00183 dst += 4 * stride;
00184 }
00185 return 0;
00186 }
00187
00188 static int tscc2_decode_slice(TSCC2Context *c, int mb_y,
00189 const uint8_t *buf, int buf_size)
00190 {
00191 int i, mb_x, q, ret;
00192 int off;
00193
00194 init_get_bits(&c->gb, buf, buf_size * 8);
00195
00196 for (mb_x = 0; mb_x < c->mb_width; mb_x++) {
00197 q = c->slice_quants[mb_x + c->mb_width * mb_y];
00198
00199 if (q == 0 || q == 3)
00200 continue;
00201 for (i = 0; i < 3; i++) {
00202 off = mb_x * 16 + mb_y * 8 * c->pic.linesize[i];
00203 ret = tscc2_decode_mb(c, c->q[q - 1], c->quant[q - 1] - 2,
00204 c->pic.data[i] + off, c->pic.linesize[i], i);
00205 if (ret)
00206 return ret;
00207 }
00208 }
00209
00210 return 0;
00211 }
00212
00213 static int tscc2_decode_frame(AVCodecContext *avctx, void *data,
00214 int *data_size, AVPacket *avpkt)
00215 {
00216 const uint8_t *buf = avpkt->data;
00217 int buf_size = avpkt->size;
00218 TSCC2Context *c = avctx->priv_data;
00219 GetByteContext gb;
00220 uint32_t frame_type, size;
00221 int i, val, len, pos = 0;
00222 int num_mb = c->mb_width * c->mb_height;
00223 int ret;
00224
00225 bytestream2_init(&gb, buf, buf_size);
00226 frame_type = bytestream2_get_byte(&gb);
00227 if (frame_type > 1) {
00228 av_log(avctx, AV_LOG_ERROR, "Incorrect frame type %d\n", frame_type);
00229 return AVERROR_INVALIDDATA;
00230 }
00231
00232 c->pic.reference = 3;
00233 c->pic.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE |
00234 FF_BUFFER_HINTS_REUSABLE;
00235 if ((ret = avctx->reget_buffer(avctx, &c->pic)) < 0) {
00236 av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
00237 return ret;
00238 }
00239
00240 if (frame_type == 0) {
00241 *data_size = sizeof(AVFrame);
00242 *(AVFrame*)data = c->pic;
00243
00244 return buf_size;
00245 }
00246
00247 if (bytestream2_get_bytes_left(&gb) < 4) {
00248 av_log(avctx, AV_LOG_ERROR, "Frame is too short\n");
00249 return AVERROR_INVALIDDATA;
00250 }
00251
00252 c->quant[0] = bytestream2_get_byte(&gb);
00253 c->quant[1] = bytestream2_get_byte(&gb);
00254 if (c->quant[0] < 2 || c->quant[0] > NUM_VLC_SETS + 1 ||
00255 c->quant[1] < 2 || c->quant[1] > NUM_VLC_SETS + 1) {
00256 av_log(avctx, AV_LOG_ERROR, "Invalid quantisers %d / %d\n",
00257 c->quant[0], c->quant[1]);
00258 return AVERROR_INVALIDDATA;
00259 }
00260
00261 for (i = 0; i < 3; i++) {
00262 c->q[0][i] = tscc2_quants[c->quant[0] - 2][i];
00263 c->q[1][i] = tscc2_quants[c->quant[1] - 2][i];
00264 }
00265
00266 bytestream2_skip(&gb, 1);
00267
00268 size = bytestream2_get_le32(&gb);
00269 if (size > bytestream2_get_bytes_left(&gb)) {
00270 av_log(avctx, AV_LOG_ERROR, "Slice properties chunk is too large\n");
00271 return AVERROR_INVALIDDATA;
00272 }
00273
00274 for (i = 0; i < size; i++) {
00275 val = bytestream2_get_byte(&gb);
00276 len = val & 0x3F;
00277 val >>= 6;
00278 if (pos + len > num_mb) {
00279 av_log(avctx, AV_LOG_ERROR, "Too many slice properties\n");
00280 return AVERROR_INVALIDDATA;
00281 }
00282 memset(c->slice_quants + pos, val, len);
00283 pos += len;
00284 }
00285 if (pos < num_mb) {
00286 av_log(avctx, AV_LOG_ERROR, "Too few slice properties (%d / %d)\n",
00287 pos, num_mb);
00288 return AVERROR_INVALIDDATA;
00289 }
00290
00291 for (i = 0; i < c->mb_height; i++) {
00292 size = bytestream2_peek_byte(&gb);
00293 if (size & 1) {
00294 size = bytestream2_get_byte(&gb) - 1;
00295 } else {
00296 size = bytestream2_get_le32(&gb) >> 1;
00297 }
00298 if (!size) {
00299 int skip_row = 1, j, off = i * c->mb_width;
00300 for (j = 0; j < c->mb_width; j++) {
00301 if (c->slice_quants[off + j] == 1 ||
00302 c->slice_quants[off + j] == 2) {
00303 skip_row = 0;
00304 break;
00305 }
00306 }
00307 if (!skip_row) {
00308 av_log(avctx, AV_LOG_ERROR, "Non-skip row with zero size\n");
00309 return AVERROR_INVALIDDATA;
00310 }
00311 }
00312 if (bytestream2_get_bytes_left(&gb) < size) {
00313 av_log(avctx, AV_LOG_ERROR, "Invalid slice size (%d/%d)\n",
00314 size, bytestream2_get_bytes_left(&gb));
00315 return AVERROR_INVALIDDATA;
00316 }
00317 ret = tscc2_decode_slice(c, i, buf + bytestream2_tell(&gb), size);
00318 if (ret) {
00319 av_log(avctx, AV_LOG_ERROR, "Error decoding slice %d\n", i);
00320 return ret;
00321 }
00322 bytestream2_skip(&gb, size);
00323 }
00324
00325 *data_size = sizeof(AVFrame);
00326 *(AVFrame*)data = c->pic;
00327
00328
00329 return buf_size;
00330 }
00331
00332 static av_cold int tscc2_decode_init(AVCodecContext *avctx)
00333 {
00334 TSCC2Context * const c = avctx->priv_data;
00335 int ret;
00336
00337 c->avctx = avctx;
00338
00339 avctx->pix_fmt = PIX_FMT_YUV444P;
00340
00341 if ((ret = init_vlcs(c)) < 0) {
00342 av_log(avctx, AV_LOG_ERROR, "Cannot initialise VLCs\n");
00343 return ret;
00344 }
00345
00346 c->mb_width = FFALIGN(avctx->width, 16) >> 4;
00347 c->mb_height = FFALIGN(avctx->height, 8) >> 3;
00348 c->slice_quants = av_malloc(c->mb_width * c->mb_height);
00349 if (!c->slice_quants) {
00350 av_log(avctx, AV_LOG_ERROR, "Cannot allocate slice information\n");
00351 free_vlcs(c);
00352 return AVERROR(ENOMEM);
00353 }
00354
00355 avctx->coded_frame = &c->pic;
00356
00357 return 0;
00358 }
00359
00360 static av_cold int tscc2_decode_end(AVCodecContext *avctx)
00361 {
00362 TSCC2Context * const c = avctx->priv_data;
00363
00364 if (c->pic.data[0])
00365 avctx->release_buffer(avctx, &c->pic);
00366 av_freep(&c->slice_quants);
00367 free_vlcs(c);
00368
00369 return 0;
00370 }
00371
00372 AVCodec ff_tscc2_decoder = {
00373 .name = "tscc2",
00374 .type = AVMEDIA_TYPE_VIDEO,
00375 .id = AV_CODEC_ID_TSCC2,
00376 .priv_data_size = sizeof(TSCC2Context),
00377 .init = tscc2_decode_init,
00378 .close = tscc2_decode_end,
00379 .decode = tscc2_decode_frame,
00380 .capabilities = CODEC_CAP_DR1,
00381 .long_name = NULL_IF_CONFIG_SMALL("TechSmith Screen Codec 2"),
00382 };