00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00028 #include "avcodec.h"
00029 #include "bytestream.h"
00030 #include "dsputil.h"
00031 #include "get_bits.h"
00032 #include "mss34dsp.h"
00033 #include "unary.h"
00034
00035 #define HEADER_SIZE 8
00036
00037 enum FrameType {
00038 INTRA_FRAME = 0,
00039 INTER_FRAME,
00040 SKIP_FRAME
00041 };
00042
00043 enum BlockType {
00044 SKIP_BLOCK = 0,
00045 DCT_BLOCK,
00046 IMAGE_BLOCK,
00047 };
00048
00049 enum CachePos {
00050 LEFT = 0,
00051 TOP_LEFT,
00052 TOP,
00053 };
00054
00055 static const uint8_t mss4_dc_vlc_lens[2][16] = {
00056 { 0, 1, 5, 1, 1, 1, 1, 2, 0, 0, 0, 0, 0, 0, 0, 0 },
00057 { 0, 3, 1, 1, 1, 1, 1, 1, 1, 2, 0, 0, 0, 0, 0, 0 }
00058 };
00059
00060 static const uint8_t mss4_ac_vlc_lens[2][16] = {
00061 { 0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 125 },
00062 { 0, 2, 1, 2, 4, 4, 3, 4, 7, 5, 4, 4, 0, 1, 2, 119 }
00063 };
00064
00065 static const uint8_t mss4_ac_vlc_syms[2][162] = {
00066 { 0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12,
00067 0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07,
00068 0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xA1, 0x08,
00069 0x23, 0x42, 0xB1, 0xC1, 0x15, 0x52, 0xD1, 0xF0,
00070 0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0A, 0x16,
00071 0x17, 0x18, 0x19, 0x1A, 0x25, 0x26, 0x27, 0x28,
00072 0x29, 0x2A, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
00073 0x3A, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,
00074 0x4A, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
00075 0x5A, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
00076 0x6A, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79,
00077 0x7A, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
00078 0x8A, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
00079 0x99, 0x9A, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7,
00080 0xA8, 0xA9, 0xAA, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6,
00081 0xB7, 0xB8, 0xB9, 0xBA, 0xC2, 0xC3, 0xC4, 0xC5,
00082 0xC6, 0xC7, 0xC8, 0xC9, 0xCA, 0xD2, 0xD3, 0xD4,
00083 0xD5, 0xD6, 0xD7, 0xD8, 0xD9, 0xDA, 0xE1, 0xE2,
00084 0xE3, 0xE4, 0xE5, 0xE6, 0xE7, 0xE8, 0xE9, 0xEA,
00085 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7, 0xF8,
00086 0xF9, 0xFA },
00087 { 0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21,
00088 0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71,
00089 0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91,
00090 0xA1, 0xB1, 0xC1, 0x09, 0x23, 0x33, 0x52, 0xF0,
00091 0x15, 0x62, 0x72, 0xD1, 0x0A, 0x16, 0x24, 0x34,
00092 0xE1, 0x25, 0xF1, 0x17, 0x18, 0x19, 0x1A, 0x26,
00093 0x27, 0x28, 0x29, 0x2A, 0x35, 0x36, 0x37, 0x38,
00094 0x39, 0x3A, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
00095 0x49, 0x4A, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
00096 0x59, 0x5A, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
00097 0x69, 0x6A, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
00098 0x79, 0x7A, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
00099 0x88, 0x89, 0x8A, 0x92, 0x93, 0x94, 0x95, 0x96,
00100 0x97, 0x98, 0x99, 0x9A, 0xA2, 0xA3, 0xA4, 0xA5,
00101 0xA6, 0xA7, 0xA8, 0xA9, 0xAA, 0xB2, 0xB3, 0xB4,
00102 0xB5, 0xB6, 0xB7, 0xB8, 0xB9, 0xBA, 0xC2, 0xC3,
00103 0xC4, 0xC5, 0xC6, 0xC7, 0xC8, 0xC9, 0xCA, 0xD2,
00104 0xD3, 0xD4, 0xD5, 0xD6, 0xD7, 0xD8, 0xD9, 0xDA,
00105 0xE2, 0xE3, 0xE4, 0xE5, 0xE6, 0xE7, 0xE8, 0xE9,
00106 0xEA, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7, 0xF8,
00107 0xF9, 0xFA }
00108 };
00109
00110 static const uint8_t vec_len_syms[2][4] = {
00111 { 4, 2, 3, 1 },
00112 { 4, 1, 2, 3 }
00113 };
00114
00115 static const uint8_t mss4_vec_entry_vlc_lens[2][16] = {
00116 { 0, 2, 2, 3, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
00117 { 0, 1, 5, 1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
00118 };
00119
00120 static const uint8_t mss4_vec_entry_vlc_syms[2][9] = {
00121 { 0, 7, 6, 5, 8, 4, 3, 1, 2 },
00122 { 0, 2, 3, 4, 5, 6, 7, 1, 8 }
00123 };
00124
00125 #define MAX_ENTRIES 162
00126
00127 typedef struct MSS4Context {
00128 AVFrame pic;
00129 DSPContext dsp;
00130
00131 VLC dc_vlc[2], ac_vlc[2];
00132 VLC vec_entry_vlc[2];
00133 int block[64];
00134 uint8_t imgbuf[3][16 * 16];
00135
00136 int quality;
00137 uint16_t quant_mat[2][64];
00138
00139 int *prev_dc[3];
00140 int dc_stride[3];
00141 int dc_cache[4][4];
00142
00143 int prev_vec[3][4];
00144 } MSS4Context;
00145
00146 static av_cold int mss4_init_vlc(VLC *vlc, const uint8_t *lens,
00147 const uint8_t *syms, int num_syms)
00148 {
00149 uint8_t bits[MAX_ENTRIES];
00150 uint16_t codes[MAX_ENTRIES];
00151 int i, j;
00152 int prefix = 0, max_bits = 0, idx = 0;
00153
00154 for (i = 0; i < 16; i++) {
00155 for (j = 0; j < lens[i]; j++) {
00156 bits[idx] = i + 1;
00157 codes[idx] = prefix++;
00158 max_bits = i + 1;
00159 idx++;
00160 }
00161 prefix <<= 1;
00162 }
00163
00164 return ff_init_vlc_sparse(vlc, FFMIN(max_bits, 9), num_syms, bits, 1, 1,
00165 codes, 2, 2, syms, 1, 1, 0);
00166 }
00167
00168 static av_cold int mss4_init_vlcs(MSS4Context *ctx)
00169 {
00170 int ret, i;
00171
00172 for (i = 0; i < 2; i++) {
00173 ret = mss4_init_vlc(&ctx->dc_vlc[i], mss4_dc_vlc_lens[i], NULL, 12);
00174 if (ret)
00175 return ret;
00176 ret = mss4_init_vlc(&ctx->ac_vlc[i], mss4_ac_vlc_lens[i],
00177 mss4_ac_vlc_syms[i], 162);
00178 if (ret)
00179 return ret;
00180 ret = mss4_init_vlc(&ctx->vec_entry_vlc[i], mss4_vec_entry_vlc_lens[i],
00181 mss4_vec_entry_vlc_syms[i], 9);
00182 if (ret)
00183 return ret;
00184 }
00185 return 0;
00186 }
00187
00188 static av_cold void mss4_free_vlcs(MSS4Context *ctx)
00189 {
00190 int i;
00191
00192 for (i = 0; i < 2; i++) {
00193 ff_free_vlc(&ctx->dc_vlc[i]);
00194 ff_free_vlc(&ctx->ac_vlc[i]);
00195 ff_free_vlc(&ctx->vec_entry_vlc[i]);
00196 }
00197 }
00198
00199
00200
00201
00202
00203
00204
00205
00206 static av_always_inline int get_coeff_bits(GetBitContext *gb, int nbits)
00207 {
00208 int val;
00209
00210 if (!nbits)
00211 return 0;
00212
00213 val = get_bits(gb, nbits);
00214 if (val < (1 << (nbits - 1)))
00215 val -= (1 << nbits) - 1;
00216
00217 return val;
00218 }
00219
00220 static inline int get_coeff(GetBitContext *gb, VLC *vlc)
00221 {
00222 int val = get_vlc2(gb, vlc->table, vlc->bits, 2);
00223
00224 return get_coeff_bits(gb, val);
00225 }
00226
00227 static int mss4_decode_dct(GetBitContext *gb, VLC *dc_vlc, VLC *ac_vlc,
00228 int *block, int *dc_cache,
00229 int bx, int by, uint16_t *quant_mat)
00230 {
00231 int skip, val, pos = 1, zz_pos, dc;
00232
00233 memset(block, 0, sizeof(*block) * 64);
00234
00235 dc = get_coeff(gb, dc_vlc);
00236
00237 if (by) {
00238 if (bx) {
00239 int l, tl, t;
00240
00241 l = dc_cache[LEFT];
00242 tl = dc_cache[TOP_LEFT];
00243 t = dc_cache[TOP];
00244
00245 if (FFABS(t - tl) <= FFABS(l - tl))
00246 dc += l;
00247 else
00248 dc += t;
00249 } else {
00250 dc += dc_cache[TOP];
00251 }
00252 } else if (bx) {
00253 dc += dc_cache[LEFT];
00254 }
00255 dc_cache[LEFT] = dc;
00256 block[0] = dc * quant_mat[0];
00257
00258 while (pos < 64) {
00259 val = get_vlc2(gb, ac_vlc->table, 9, 2);
00260 if (!val)
00261 return 0;
00262 if (val == -1)
00263 return -1;
00264 if (val == 0xF0) {
00265 pos += 16;
00266 continue;
00267 }
00268 skip = val >> 4;
00269 val = get_coeff_bits(gb, val & 0xF);
00270 pos += skip;
00271 if (pos >= 64)
00272 return -1;
00273
00274 zz_pos = ff_zigzag_direct[pos];
00275 block[zz_pos] = val * quant_mat[zz_pos];
00276 pos++;
00277 }
00278
00279 return pos == 64 ? 0 : -1;
00280 }
00281
00282 static int mss4_decode_dct_block(MSS4Context *c, GetBitContext *gb,
00283 uint8_t *dst[3], int mb_x, int mb_y)
00284 {
00285 int i, j, k, ret;
00286 uint8_t *out = dst[0];
00287
00288 for (j = 0; j < 2; j++) {
00289 for (i = 0; i < 2; i++) {
00290 int xpos = mb_x * 2 + i;
00291 c->dc_cache[j][TOP_LEFT] = c->dc_cache[j][TOP];
00292 c->dc_cache[j][TOP] = c->prev_dc[0][mb_x * 2 + i];
00293 ret = mss4_decode_dct(gb, c->dc_vlc, c->ac_vlc, c->block,
00294 c->dc_cache[j],
00295 xpos, mb_y * 2 + j, c->quant_mat[0]);
00296 if (ret)
00297 return ret;
00298 c->prev_dc[0][mb_x * 2 + i] = c->dc_cache[j][LEFT];
00299
00300 ff_mss34_dct_put(out + xpos * 8, c->pic.linesize[0],
00301 c->block);
00302 }
00303 out += 8 * c->pic.linesize[0];
00304 }
00305
00306 for (i = 1; i < 3; i++) {
00307 c->dc_cache[i + 1][TOP_LEFT] = c->dc_cache[i + 1][TOP];
00308 c->dc_cache[i + 1][TOP] = c->prev_dc[i][mb_x];
00309 ret = mss4_decode_dct(gb, c->dc_vlc + 1, c->ac_vlc + 1,
00310 c->block, c->dc_cache[i + 1], mb_x, mb_y,
00311 c->quant_mat[1]);
00312 if (ret)
00313 return ret;
00314 c->prev_dc[i][mb_x] = c->dc_cache[i + 1][LEFT];
00315
00316 ff_mss34_dct_put(c->imgbuf[i], 8, c->block);
00317 out = dst[i] + mb_x * 16;
00318
00319
00320 for (j = 0; j < 16; j++) {
00321 for (k = 0; k < 8; k++)
00322 AV_WN16A(out + k * 2, c->imgbuf[i][k + (j & ~1) * 4] * 0x101);
00323 out += c->pic.linesize[i];
00324 }
00325 }
00326
00327 return 0;
00328 }
00329
00330 static void read_vec_pos(GetBitContext *gb, int *vec_pos, int *sel_flag,
00331 int *sel_len, int *prev)
00332 {
00333 int i, y_flag = 0;
00334
00335 for (i = 2; i >= 0; i--) {
00336 if (!sel_flag[i]) {
00337 vec_pos[i] = 0;
00338 continue;
00339 }
00340 if ((!i && !y_flag) || get_bits1(gb)) {
00341 if (sel_len[i] > 0) {
00342 int pval = prev[i];
00343 vec_pos[i] = get_bits(gb, sel_len[i]);
00344 if (vec_pos[i] >= pval)
00345 vec_pos[i]++;
00346 } else {
00347 vec_pos[i] = !prev[i];
00348 }
00349 y_flag = 1;
00350 } else {
00351 vec_pos[i] = prev[i];
00352 }
00353 }
00354 }
00355
00356 static int get_value_cached(GetBitContext *gb, int vec_pos, uint8_t *vec,
00357 int vec_size, int component, int shift, int *prev)
00358 {
00359 if (vec_pos < vec_size)
00360 return vec[vec_pos];
00361 if (!get_bits1(gb))
00362 return prev[component];
00363 prev[component] = get_bits(gb, 8 - shift) << shift;
00364 return prev[component];
00365 }
00366
00367 #define MKVAL(vals) (vals[0] | (vals[1] << 3) | (vals[2] << 6))
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380 static int mss4_decode_image_block(MSS4Context *ctx, GetBitContext *gb,
00381 uint8_t *picdst[3], int mb_x, int mb_y)
00382 {
00383 uint8_t vec[3][4];
00384 int vec_len[3];
00385 int sel_len[3], sel_flag[3];
00386 int i, j, k, mode, split;
00387 int prev_vec1 = 0, prev_split = 0;
00388 int vals[3] = { 0 };
00389 int prev_pix[3] = { 0 };
00390 int prev_mode[16] = { 0 };
00391 uint8_t *dst[3];
00392
00393 const int val_shift = ctx->quality == 100 ? 0 : 2;
00394
00395 for (i = 0; i < 3; i++)
00396 dst[i] = ctx->imgbuf[i];
00397
00398 for (i = 0; i < 3; i++) {
00399 vec_len[i] = vec_len_syms[!!i][get_unary(gb, 0, 3)];
00400 for (j = 0; j < vec_len[i]; j++) {
00401 vec[i][j] = get_coeff(gb, &ctx->vec_entry_vlc[!!i]);
00402 vec[i][j] += ctx->prev_vec[i][j];
00403 ctx->prev_vec[i][j] = vec[i][j];
00404 }
00405 sel_flag[i] = vec_len[i] > 1;
00406 sel_len[i] = vec_len[i] > 2 ? vec_len[i] - 2 : 0;
00407 }
00408
00409 for (j = 0; j < 16; j++) {
00410 if (get_bits1(gb)) {
00411 split = 0;
00412 if (get_bits1(gb)) {
00413 prev_mode[0] = 0;
00414 vals[0] = vals[1] = vals[2] = 0;
00415 mode = 2;
00416 } else {
00417 mode = get_bits1(gb);
00418 if (mode)
00419 split = get_bits(gb, 4);
00420 }
00421 for (i = 0; i < 16; i++) {
00422 if (mode <= 1) {
00423 vals[0] = prev_mode[i] & 7;
00424 vals[1] = (prev_mode[i] >> 3) & 7;
00425 vals[2] = prev_mode[i] >> 6;
00426 if (mode == 1 && i == split) {
00427 read_vec_pos(gb, vals, sel_flag, sel_len, vals);
00428 }
00429 } else if (mode == 2) {
00430 if (get_bits1(gb))
00431 read_vec_pos(gb, vals, sel_flag, sel_len, vals);
00432 }
00433 for (k = 0; k < 3; k++)
00434 *dst[k]++ = get_value_cached(gb, vals[k], vec[k],
00435 vec_len[k], k,
00436 val_shift, prev_pix);
00437 prev_mode[i] = MKVAL(vals);
00438 }
00439 } else {
00440 if (get_bits1(gb)) {
00441 split = get_bits(gb, 4);
00442 if (split >= prev_split)
00443 split++;
00444 prev_split = split;
00445 } else {
00446 split = prev_split;
00447 }
00448 if (split) {
00449 vals[0] = prev_mode[0] & 7;
00450 vals[1] = (prev_mode[0] >> 3) & 7;
00451 vals[2] = prev_mode[0] >> 6;
00452 for (i = 0; i < 3; i++) {
00453 for (k = 0; k < split; k++) {
00454 *dst[i]++ = get_value_cached(gb, vals[i], vec[i],
00455 vec_len[i], i, val_shift,
00456 prev_pix);
00457 prev_mode[k] = MKVAL(vals);
00458 }
00459 }
00460 }
00461
00462 if (split != 16) {
00463 vals[0] = prev_vec1 & 7;
00464 vals[1] = (prev_vec1 >> 3) & 7;
00465 vals[2] = prev_vec1 >> 6;
00466 if (get_bits1(gb)) {
00467 read_vec_pos(gb, vals, sel_flag, sel_len, vals);
00468 prev_vec1 = MKVAL(vals);
00469 }
00470 for (i = 0; i < 3; i++) {
00471 for (k = 0; k < 16 - split; k++) {
00472 *dst[i]++ = get_value_cached(gb, vals[i], vec[i],
00473 vec_len[i], i, val_shift,
00474 prev_pix);
00475 prev_mode[split + k] = MKVAL(vals);
00476 }
00477 }
00478 }
00479 }
00480 }
00481
00482 for (i = 0; i < 3; i++)
00483 for (j = 0; j < 16; j++)
00484 memcpy(picdst[i] + mb_x * 16 + j * ctx->pic.linesize[i],
00485 ctx->imgbuf[i] + j * 16, 16);
00486
00487 return 0;
00488 }
00489
00490 static inline void mss4_update_dc_cache(MSS4Context *c, int mb_x)
00491 {
00492 int i;
00493
00494 c->dc_cache[0][TOP] = c->prev_dc[0][mb_x * 2 + 1];
00495 c->dc_cache[0][LEFT] = 0;
00496 c->dc_cache[1][TOP] = 0;
00497 c->dc_cache[1][LEFT] = 0;
00498
00499 for (i = 0; i < 2; i++)
00500 c->prev_dc[0][mb_x * 2 + i] = 0;
00501
00502 for (i = 1; i < 3; i++) {
00503 c->dc_cache[i + 1][TOP] = c->prev_dc[i][mb_x];
00504 c->dc_cache[i + 1][LEFT] = 0;
00505 c->prev_dc[i][mb_x] = 0;
00506 }
00507 }
00508
00509 static int mss4_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
00510 AVPacket *avpkt)
00511 {
00512 const uint8_t *buf = avpkt->data;
00513 int buf_size = avpkt->size;
00514 MSS4Context *c = avctx->priv_data;
00515 GetBitContext gb;
00516 GetByteContext bc;
00517 uint8_t *dst[3];
00518 int width, height, quality, frame_type;
00519 int x, y, i, mb_width, mb_height, blk_type;
00520 int ret;
00521
00522 if (buf_size < HEADER_SIZE) {
00523 av_log(avctx, AV_LOG_ERROR,
00524 "Frame should have at least %d bytes, got %d instead\n",
00525 HEADER_SIZE, buf_size);
00526 return AVERROR_INVALIDDATA;
00527 }
00528
00529 bytestream2_init(&bc, buf, buf_size);
00530 width = bytestream2_get_be16(&bc);
00531 height = bytestream2_get_be16(&bc);
00532 bytestream2_skip(&bc, 2);
00533 quality = bytestream2_get_byte(&bc);
00534 frame_type = bytestream2_get_byte(&bc);
00535
00536 if (width > avctx->width ||
00537 height != avctx->height) {
00538 av_log(avctx, AV_LOG_ERROR, "Invalid frame dimensions %dx%d\n",
00539 width, height);
00540 return AVERROR_INVALIDDATA;
00541 }
00542 if (quality < 1 || quality > 100) {
00543 av_log(avctx, AV_LOG_ERROR, "Invalid quality setting %d\n", quality);
00544 return AVERROR_INVALIDDATA;
00545 }
00546 if ((frame_type & ~3) || frame_type == 3) {
00547 av_log(avctx, AV_LOG_ERROR, "Invalid frame type %d\n", frame_type);
00548 return AVERROR_INVALIDDATA;
00549 }
00550
00551 if (frame_type != SKIP_FRAME && !bytestream2_get_bytes_left(&bc)) {
00552 av_log(avctx, AV_LOG_ERROR,
00553 "Empty frame found but it is not a skip frame.\n");
00554 return AVERROR_INVALIDDATA;
00555 }
00556
00557 c->pic.reference = 3;
00558 c->pic.buffer_hints = FF_BUFFER_HINTS_VALID |
00559 FF_BUFFER_HINTS_PRESERVE |
00560 FF_BUFFER_HINTS_REUSABLE;
00561 if ((ret = avctx->reget_buffer(avctx, &c->pic)) < 0) {
00562 av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
00563 return ret;
00564 }
00565 c->pic.key_frame = (frame_type == INTRA_FRAME);
00566 c->pic.pict_type = (frame_type == INTRA_FRAME) ? AV_PICTURE_TYPE_I
00567 : AV_PICTURE_TYPE_P;
00568 if (frame_type == SKIP_FRAME) {
00569 *data_size = sizeof(AVFrame);
00570 *(AVFrame*)data = c->pic;
00571
00572 return buf_size;
00573 }
00574
00575 if (c->quality != quality) {
00576 c->quality = quality;
00577 for (i = 0; i < 2; i++)
00578 ff_mss34_gen_quant_mat(c->quant_mat[i], quality, !i);
00579 }
00580
00581 init_get_bits(&gb, buf + HEADER_SIZE, (buf_size - HEADER_SIZE) * 8);
00582
00583 mb_width = FFALIGN(width, 16) >> 4;
00584 mb_height = FFALIGN(height, 16) >> 4;
00585 dst[0] = c->pic.data[0];
00586 dst[1] = c->pic.data[1];
00587 dst[2] = c->pic.data[2];
00588
00589 memset(c->prev_vec, 0, sizeof(c->prev_vec));
00590 for (y = 0; y < mb_height; y++) {
00591 memset(c->dc_cache, 0, sizeof(c->dc_cache));
00592 for (x = 0; x < mb_width; x++) {
00593 blk_type = decode012(&gb);
00594 switch (blk_type) {
00595 case DCT_BLOCK:
00596 if (mss4_decode_dct_block(c, &gb, dst, x, y) < 0) {
00597 av_log(avctx, AV_LOG_ERROR,
00598 "Error decoding DCT block %d,%d\n",
00599 x, y);
00600 return AVERROR_INVALIDDATA;
00601 }
00602 break;
00603 case IMAGE_BLOCK:
00604 if (mss4_decode_image_block(c, &gb, dst, x, y) < 0) {
00605 av_log(avctx, AV_LOG_ERROR,
00606 "Error decoding VQ block %d,%d\n",
00607 x, y);
00608 return AVERROR_INVALIDDATA;
00609 }
00610 break;
00611 case SKIP_BLOCK:
00612 if (frame_type == INTRA_FRAME) {
00613 av_log(avctx, AV_LOG_ERROR, "Skip block in intra frame\n");
00614 return AVERROR_INVALIDDATA;
00615 }
00616 break;
00617 }
00618 if (blk_type != DCT_BLOCK)
00619 mss4_update_dc_cache(c, x);
00620 }
00621 dst[0] += c->pic.linesize[0] * 16;
00622 dst[1] += c->pic.linesize[1] * 16;
00623 dst[2] += c->pic.linesize[2] * 16;
00624 }
00625
00626 *data_size = sizeof(AVFrame);
00627 *(AVFrame*)data = c->pic;
00628
00629 return buf_size;
00630 }
00631
00632 static av_cold int mss4_decode_init(AVCodecContext *avctx)
00633 {
00634 MSS4Context * const c = avctx->priv_data;
00635 int i;
00636
00637 if (mss4_init_vlcs(c)) {
00638 av_log(avctx, AV_LOG_ERROR, "Cannot initialise VLCs\n");
00639 mss4_free_vlcs(c);
00640 return AVERROR(ENOMEM);
00641 }
00642 for (i = 0; i < 3; i++) {
00643 c->dc_stride[i] = FFALIGN(avctx->width, 16) >> (2 + !!i);
00644 c->prev_dc[i] = av_malloc(sizeof(**c->prev_dc) * c->dc_stride[i]);
00645 if (!c->prev_dc[i]) {
00646 av_log(avctx, AV_LOG_ERROR, "Cannot allocate buffer\n");
00647 mss4_free_vlcs(c);
00648 return AVERROR(ENOMEM);
00649 }
00650 }
00651
00652 avctx->pix_fmt = PIX_FMT_YUV444P;
00653 avctx->coded_frame = &c->pic;
00654
00655 return 0;
00656 }
00657
00658 static av_cold int mss4_decode_end(AVCodecContext *avctx)
00659 {
00660 MSS4Context * const c = avctx->priv_data;
00661 int i;
00662
00663 if (c->pic.data[0])
00664 avctx->release_buffer(avctx, &c->pic);
00665 for (i = 0; i < 3; i++)
00666 av_freep(&c->prev_dc[i]);
00667 mss4_free_vlcs(c);
00668
00669 return 0;
00670 }
00671
00672 AVCodec ff_mts2_decoder = {
00673 .name = "mts2",
00674 .type = AVMEDIA_TYPE_VIDEO,
00675 .id = AV_CODEC_ID_MTS2,
00676 .priv_data_size = sizeof(MSS4Context),
00677 .init = mss4_decode_init,
00678 .close = mss4_decode_end,
00679 .decode = mss4_decode_frame,
00680 .capabilities = CODEC_CAP_DR1,
00681 .long_name = NULL_IF_CONFIG_SMALL("MS Expression Encoder Screen"),
00682 };