00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00027 #include "avcodec.h"
00028 #include "bytestream.h"
00029 #include "dsputil.h"
00030 #include "mss34dsp.h"
00031
00032 #define HEADER_SIZE 27
00033
00034 #define MODEL2_SCALE 13
00035 #define MODEL_SCALE 15
00036 #define MODEL256_SEC_SCALE 9
00037
00038 typedef struct Model2 {
00039 int upd_val, till_rescale;
00040 unsigned zero_freq, zero_weight;
00041 unsigned total_freq, total_weight;
00042 } Model2;
00043
00044 typedef struct Model {
00045 int weights[16], freqs[16];
00046 int num_syms;
00047 int tot_weight;
00048 int upd_val, max_upd_val, till_rescale;
00049 } Model;
00050
00051 typedef struct Model256 {
00052 int weights[256], freqs[256];
00053 int tot_weight;
00054 int secondary[68];
00055 int sec_size;
00056 int upd_val, max_upd_val, till_rescale;
00057 } Model256;
00058
00059 #define RAC_BOTTOM 0x01000000
00060 typedef struct RangeCoder {
00061 const uint8_t *src, *src_end;
00062
00063 uint32_t range, low;
00064 int got_error;
00065 } RangeCoder;
00066
00067 enum BlockType {
00068 FILL_BLOCK = 0,
00069 IMAGE_BLOCK,
00070 DCT_BLOCK,
00071 HAAR_BLOCK,
00072 SKIP_BLOCK
00073 };
00074
00075 typedef struct BlockTypeContext {
00076 int last_type;
00077 Model bt_model[5];
00078 } BlockTypeContext;
00079
00080 typedef struct FillBlockCoder {
00081 int fill_val;
00082 Model coef_model;
00083 } FillBlockCoder;
00084
00085 typedef struct ImageBlockCoder {
00086 Model256 esc_model, vec_entry_model;
00087 Model vec_size_model;
00088 Model vq_model[125];
00089 } ImageBlockCoder;
00090
00091 typedef struct DCTBlockCoder {
00092 int *prev_dc;
00093 int prev_dc_stride;
00094 int prev_dc_height;
00095 int quality;
00096 uint16_t qmat[64];
00097 Model dc_model;
00098 Model2 sign_model;
00099 Model256 ac_model;
00100 } DCTBlockCoder;
00101
00102 typedef struct HaarBlockCoder {
00103 int quality, scale;
00104 Model256 coef_model;
00105 Model coef_hi_model;
00106 } HaarBlockCoder;
00107
00108 typedef struct MSS3Context {
00109 AVCodecContext *avctx;
00110 AVFrame pic;
00111
00112 int got_error;
00113 RangeCoder coder;
00114 BlockTypeContext btype[3];
00115 FillBlockCoder fill_coder[3];
00116 ImageBlockCoder image_coder[3];
00117 DCTBlockCoder dct_coder[3];
00118 HaarBlockCoder haar_coder[3];
00119
00120 int dctblock[64];
00121 int hblock[16 * 16];
00122 } MSS3Context;
00123
00124
00125 static void model2_reset(Model2 *m)
00126 {
00127 m->zero_weight = 1;
00128 m->total_weight = 2;
00129 m->zero_freq = 0x1000;
00130 m->total_freq = 0x2000;
00131 m->upd_val = 4;
00132 m->till_rescale = 4;
00133 }
00134
00135 static void model2_update(Model2 *m, int bit)
00136 {
00137 unsigned scale;
00138
00139 if (!bit)
00140 m->zero_weight++;
00141 m->till_rescale--;
00142 if (m->till_rescale)
00143 return;
00144
00145 m->total_weight += m->upd_val;
00146 if (m->total_weight > 0x2000) {
00147 m->total_weight = (m->total_weight + 1) >> 1;
00148 m->zero_weight = (m->zero_weight + 1) >> 1;
00149 if (m->total_weight == m->zero_weight)
00150 m->total_weight = m->zero_weight + 1;
00151 }
00152 m->upd_val = m->upd_val * 5 >> 2;
00153 if (m->upd_val > 64)
00154 m->upd_val = 64;
00155 scale = 0x80000000u / m->total_weight;
00156 m->zero_freq = m->zero_weight * scale >> 18;
00157 m->total_freq = m->total_weight * scale >> 18;
00158 m->till_rescale = m->upd_val;
00159 }
00160
00161 static void model_update(Model *m, int val)
00162 {
00163 int i, sum = 0;
00164 unsigned scale;
00165
00166 m->weights[val]++;
00167 m->till_rescale--;
00168 if (m->till_rescale)
00169 return;
00170 m->tot_weight += m->upd_val;
00171
00172 if (m->tot_weight > 0x8000) {
00173 m->tot_weight = 0;
00174 for (i = 0; i < m->num_syms; i++) {
00175 m->weights[i] = (m->weights[i] + 1) >> 1;
00176 m->tot_weight += m->weights[i];
00177 }
00178 }
00179 scale = 0x80000000u / m->tot_weight;
00180 for (i = 0; i < m->num_syms; i++) {
00181 m->freqs[i] = sum * scale >> 16;
00182 sum += m->weights[i];
00183 }
00184
00185 m->upd_val = m->upd_val * 5 >> 2;
00186 if (m->upd_val > m->max_upd_val)
00187 m->upd_val = m->max_upd_val;
00188 m->till_rescale = m->upd_val;
00189 }
00190
00191 static void model_reset(Model *m)
00192 {
00193 int i;
00194
00195 m->tot_weight = 0;
00196 for (i = 0; i < m->num_syms - 1; i++)
00197 m->weights[i] = 1;
00198 m->weights[m->num_syms - 1] = 0;
00199
00200 m->upd_val = m->num_syms;
00201 m->till_rescale = 1;
00202 model_update(m, m->num_syms - 1);
00203 m->till_rescale =
00204 m->upd_val = (m->num_syms + 6) >> 1;
00205 }
00206
00207 static av_cold void model_init(Model *m, int num_syms)
00208 {
00209 m->num_syms = num_syms;
00210 m->max_upd_val = 8 * num_syms + 48;
00211
00212 model_reset(m);
00213 }
00214
00215 static void model256_update(Model256 *m, int val)
00216 {
00217 int i, sum = 0;
00218 unsigned scale;
00219 int send, sidx = 1;
00220
00221 m->weights[val]++;
00222 m->till_rescale--;
00223 if (m->till_rescale)
00224 return;
00225 m->tot_weight += m->upd_val;
00226
00227 if (m->tot_weight > 0x8000) {
00228 m->tot_weight = 0;
00229 for (i = 0; i < 256; i++) {
00230 m->weights[i] = (m->weights[i] + 1) >> 1;
00231 m->tot_weight += m->weights[i];
00232 }
00233 }
00234 scale = 0x80000000u / m->tot_weight;
00235 m->secondary[0] = 0;
00236 for (i = 0; i < 256; i++) {
00237 m->freqs[i] = sum * scale >> 16;
00238 sum += m->weights[i];
00239 send = m->freqs[i] >> MODEL256_SEC_SCALE;
00240 while (sidx <= send)
00241 m->secondary[sidx++] = i - 1;
00242 }
00243 while (sidx < m->sec_size)
00244 m->secondary[sidx++] = 255;
00245
00246 m->upd_val = m->upd_val * 5 >> 2;
00247 if (m->upd_val > m->max_upd_val)
00248 m->upd_val = m->max_upd_val;
00249 m->till_rescale = m->upd_val;
00250 }
00251
00252 static void model256_reset(Model256 *m)
00253 {
00254 int i;
00255
00256 for (i = 0; i < 255; i++)
00257 m->weights[i] = 1;
00258 m->weights[255] = 0;
00259
00260 m->tot_weight = 0;
00261 m->upd_val = 256;
00262 m->till_rescale = 1;
00263 model256_update(m, 255);
00264 m->till_rescale =
00265 m->upd_val = (256 + 6) >> 1;
00266 }
00267
00268 static av_cold void model256_init(Model256 *m)
00269 {
00270 m->max_upd_val = 8 * 256 + 48;
00271 m->sec_size = (1 << 6) + 2;
00272
00273 model256_reset(m);
00274 }
00275
00276 static void rac_init(RangeCoder *c, const uint8_t *src, int size)
00277 {
00278 int i;
00279
00280 c->src = src;
00281 c->src_end = src + size;
00282 c->low = 0;
00283 for (i = 0; i < FFMIN(size, 4); i++)
00284 c->low = (c->low << 8) | *c->src++;
00285 c->range = 0xFFFFFFFF;
00286 c->got_error = 0;
00287 }
00288
00289 static void rac_normalise(RangeCoder *c)
00290 {
00291 for (;;) {
00292 c->range <<= 8;
00293 c->low <<= 8;
00294 if (c->src < c->src_end) {
00295 c->low |= *c->src++;
00296 } else if (!c->low) {
00297 c->got_error = 1;
00298 return;
00299 }
00300 if (c->range >= RAC_BOTTOM)
00301 return;
00302 }
00303 }
00304
00305 static int rac_get_bit(RangeCoder *c)
00306 {
00307 int bit;
00308
00309 c->range >>= 1;
00310
00311 bit = (c->range <= c->low);
00312 if (bit)
00313 c->low -= c->range;
00314
00315 if (c->range < RAC_BOTTOM)
00316 rac_normalise(c);
00317
00318 return bit;
00319 }
00320
00321 static int rac_get_bits(RangeCoder *c, int nbits)
00322 {
00323 int val;
00324
00325 c->range >>= nbits;
00326 val = c->low / c->range;
00327 c->low -= c->range * val;
00328
00329 if (c->range < RAC_BOTTOM)
00330 rac_normalise(c);
00331
00332 return val;
00333 }
00334
00335 static int rac_get_model2_sym(RangeCoder *c, Model2 *m)
00336 {
00337 int bit, helper;
00338
00339 helper = m->zero_freq * (c->range >> MODEL2_SCALE);
00340 bit = (c->low >= helper);
00341 if (bit) {
00342 c->low -= helper;
00343 c->range -= helper;
00344 } else {
00345 c->range = helper;
00346 }
00347
00348 if (c->range < RAC_BOTTOM)
00349 rac_normalise(c);
00350
00351 model2_update(m, bit);
00352
00353 return bit;
00354 }
00355
00356 static int rac_get_model_sym(RangeCoder *c, Model *m)
00357 {
00358 int prob, prob2, helper, val;
00359 int end, end2;
00360
00361 prob = 0;
00362 prob2 = c->range;
00363 c->range >>= MODEL_SCALE;
00364 val = 0;
00365 end = m->num_syms >> 1;
00366 end2 = m->num_syms;
00367 do {
00368 helper = m->freqs[end] * c->range;
00369 if (helper <= c->low) {
00370 val = end;
00371 prob = helper;
00372 } else {
00373 end2 = end;
00374 prob2 = helper;
00375 }
00376 end = (end2 + val) >> 1;
00377 } while (end != val);
00378 c->low -= prob;
00379 c->range = prob2 - prob;
00380 if (c->range < RAC_BOTTOM)
00381 rac_normalise(c);
00382
00383 model_update(m, val);
00384
00385 return val;
00386 }
00387
00388 static int rac_get_model256_sym(RangeCoder *c, Model256 *m)
00389 {
00390 int prob, prob2, helper, val;
00391 int start, end;
00392 int ssym;
00393
00394 prob2 = c->range;
00395 c->range >>= MODEL_SCALE;
00396
00397 helper = c->low / c->range;
00398 ssym = helper >> MODEL256_SEC_SCALE;
00399 val = m->secondary[ssym];
00400
00401 end = start = m->secondary[ssym + 1] + 1;
00402 while (end > val + 1) {
00403 ssym = (end + val) >> 1;
00404 if (m->freqs[ssym] <= helper) {
00405 end = start;
00406 val = ssym;
00407 } else {
00408 end = (end + val) >> 1;
00409 start = ssym;
00410 }
00411 }
00412 prob = m->freqs[val] * c->range;
00413 if (val != 255)
00414 prob2 = m->freqs[val + 1] * c->range;
00415
00416 c->low -= prob;
00417 c->range = prob2 - prob;
00418 if (c->range < RAC_BOTTOM)
00419 rac_normalise(c);
00420
00421 model256_update(m, val);
00422
00423 return val;
00424 }
00425
00426 static int decode_block_type(RangeCoder *c, BlockTypeContext *bt)
00427 {
00428 bt->last_type = rac_get_model_sym(c, &bt->bt_model[bt->last_type]);
00429
00430 return bt->last_type;
00431 }
00432
00433 static int decode_coeff(RangeCoder *c, Model *m)
00434 {
00435 int val, sign;
00436
00437 val = rac_get_model_sym(c, m);
00438 if (val) {
00439 sign = rac_get_bit(c);
00440 if (val > 1) {
00441 val--;
00442 val = (1 << val) + rac_get_bits(c, val);
00443 }
00444 if (!sign)
00445 val = -val;
00446 }
00447
00448 return val;
00449 }
00450
00451 static void decode_fill_block(RangeCoder *c, FillBlockCoder *fc,
00452 uint8_t *dst, int stride, int block_size)
00453 {
00454 int i;
00455
00456 fc->fill_val += decode_coeff(c, &fc->coef_model);
00457
00458 for (i = 0; i < block_size; i++, dst += stride)
00459 memset(dst, fc->fill_val, block_size);
00460 }
00461
00462 static void decode_image_block(RangeCoder *c, ImageBlockCoder *ic,
00463 uint8_t *dst, int stride, int block_size)
00464 {
00465 int i, j;
00466 int vec_size;
00467 int vec[4];
00468 int prev_line[16];
00469 int A, B, C;
00470
00471 vec_size = rac_get_model_sym(c, &ic->vec_size_model) + 2;
00472 for (i = 0; i < vec_size; i++)
00473 vec[i] = rac_get_model256_sym(c, &ic->vec_entry_model);
00474 for (; i < 4; i++)
00475 vec[i] = 0;
00476 memset(prev_line, 0, sizeof(prev_line));
00477
00478 for (j = 0; j < block_size; j++) {
00479 A = 0;
00480 B = 0;
00481 for (i = 0; i < block_size; i++) {
00482 C = B;
00483 B = prev_line[i];
00484 A = rac_get_model_sym(c, &ic->vq_model[A + B * 5 + C * 25]);
00485
00486 prev_line[i] = A;
00487 if (A < 4)
00488 dst[i] = vec[A];
00489 else
00490 dst[i] = rac_get_model256_sym(c, &ic->esc_model);
00491 }
00492 dst += stride;
00493 }
00494 }
00495
00496 static int decode_dct(RangeCoder *c, DCTBlockCoder *bc, int *block,
00497 int bx, int by)
00498 {
00499 int skip, val, sign, pos = 1, zz_pos, dc;
00500 int blk_pos = bx + by * bc->prev_dc_stride;
00501
00502 memset(block, 0, sizeof(*block) * 64);
00503
00504 dc = decode_coeff(c, &bc->dc_model);
00505 if (by) {
00506 if (bx) {
00507 int l, tl, t;
00508
00509 l = bc->prev_dc[blk_pos - 1];
00510 tl = bc->prev_dc[blk_pos - 1 - bc->prev_dc_stride];
00511 t = bc->prev_dc[blk_pos - bc->prev_dc_stride];
00512
00513 if (FFABS(t - tl) <= FFABS(l - tl))
00514 dc += l;
00515 else
00516 dc += t;
00517 } else {
00518 dc += bc->prev_dc[blk_pos - bc->prev_dc_stride];
00519 }
00520 } else if (bx) {
00521 dc += bc->prev_dc[bx - 1];
00522 }
00523 bc->prev_dc[blk_pos] = dc;
00524 block[0] = dc * bc->qmat[0];
00525
00526 while (pos < 64) {
00527 val = rac_get_model256_sym(c, &bc->ac_model);
00528 if (!val)
00529 return 0;
00530 if (val == 0xF0) {
00531 pos += 16;
00532 continue;
00533 }
00534 skip = val >> 4;
00535 val = val & 0xF;
00536 if (!val)
00537 return -1;
00538 pos += skip;
00539 if (pos >= 64)
00540 return -1;
00541
00542 sign = rac_get_model2_sym(c, &bc->sign_model);
00543 if (val > 1) {
00544 val--;
00545 val = (1 << val) + rac_get_bits(c, val);
00546 }
00547 if (!sign)
00548 val = -val;
00549
00550 zz_pos = ff_zigzag_direct[pos];
00551 block[zz_pos] = val * bc->qmat[zz_pos];
00552 pos++;
00553 }
00554
00555 return pos == 64 ? 0 : -1;
00556 }
00557
00558 static void decode_dct_block(RangeCoder *c, DCTBlockCoder *bc,
00559 uint8_t *dst, int stride, int block_size,
00560 int *block, int mb_x, int mb_y)
00561 {
00562 int i, j;
00563 int bx, by;
00564 int nblocks = block_size >> 3;
00565
00566 bx = mb_x * nblocks;
00567 by = mb_y * nblocks;
00568
00569 for (j = 0; j < nblocks; j++) {
00570 for (i = 0; i < nblocks; i++) {
00571 if (decode_dct(c, bc, block, bx + i, by + j)) {
00572 c->got_error = 1;
00573 return;
00574 }
00575 ff_mss34_dct_put(dst + i * 8, stride, block);
00576 }
00577 dst += 8 * stride;
00578 }
00579 }
00580
00581 static void decode_haar_block(RangeCoder *c, HaarBlockCoder *hc,
00582 uint8_t *dst, int stride, int block_size,
00583 int *block)
00584 {
00585 const int hsize = block_size >> 1;
00586 int A, B, C, D, t1, t2, t3, t4;
00587 int i, j;
00588
00589 for (j = 0; j < block_size; j++) {
00590 for (i = 0; i < block_size; i++) {
00591 if (i < hsize && j < hsize)
00592 block[i] = rac_get_model256_sym(c, &hc->coef_model);
00593 else
00594 block[i] = decode_coeff(c, &hc->coef_hi_model);
00595 block[i] *= hc->scale;
00596 }
00597 block += block_size;
00598 }
00599 block -= block_size * block_size;
00600
00601 for (j = 0; j < hsize; j++) {
00602 for (i = 0; i < hsize; i++) {
00603 A = block[i];
00604 B = block[i + hsize];
00605 C = block[i + hsize * block_size];
00606 D = block[i + hsize * block_size + hsize];
00607
00608 t1 = A - B;
00609 t2 = C - D;
00610 t3 = A + B;
00611 t4 = C + D;
00612 dst[i * 2] = av_clip_uint8(t1 - t2);
00613 dst[i * 2 + stride] = av_clip_uint8(t1 + t2);
00614 dst[i * 2 + 1] = av_clip_uint8(t3 - t4);
00615 dst[i * 2 + 1 + stride] = av_clip_uint8(t3 + t4);
00616 }
00617 block += block_size;
00618 dst += stride * 2;
00619 }
00620 }
00621
00622 static void reset_coders(MSS3Context *ctx, int quality)
00623 {
00624 int i, j;
00625
00626 for (i = 0; i < 3; i++) {
00627 ctx->btype[i].last_type = SKIP_BLOCK;
00628 for (j = 0; j < 5; j++)
00629 model_reset(&ctx->btype[i].bt_model[j]);
00630 ctx->fill_coder[i].fill_val = 0;
00631 model_reset(&ctx->fill_coder[i].coef_model);
00632 model256_reset(&ctx->image_coder[i].esc_model);
00633 model256_reset(&ctx->image_coder[i].vec_entry_model);
00634 model_reset(&ctx->image_coder[i].vec_size_model);
00635 for (j = 0; j < 125; j++)
00636 model_reset(&ctx->image_coder[i].vq_model[j]);
00637 if (ctx->dct_coder[i].quality != quality) {
00638 ctx->dct_coder[i].quality = quality;
00639 ff_mss34_gen_quant_mat(ctx->dct_coder[i].qmat, quality, !i);
00640 }
00641 memset(ctx->dct_coder[i].prev_dc, 0,
00642 sizeof(*ctx->dct_coder[i].prev_dc) *
00643 ctx->dct_coder[i].prev_dc_stride *
00644 ctx->dct_coder[i].prev_dc_height);
00645 model_reset(&ctx->dct_coder[i].dc_model);
00646 model2_reset(&ctx->dct_coder[i].sign_model);
00647 model256_reset(&ctx->dct_coder[i].ac_model);
00648 if (ctx->haar_coder[i].quality != quality) {
00649 ctx->haar_coder[i].quality = quality;
00650 ctx->haar_coder[i].scale = 17 - 7 * quality / 50;
00651 }
00652 model_reset(&ctx->haar_coder[i].coef_hi_model);
00653 model256_reset(&ctx->haar_coder[i].coef_model);
00654 }
00655 }
00656
00657 static av_cold void init_coders(MSS3Context *ctx)
00658 {
00659 int i, j;
00660
00661 for (i = 0; i < 3; i++) {
00662 for (j = 0; j < 5; j++)
00663 model_init(&ctx->btype[i].bt_model[j], 5);
00664 model_init(&ctx->fill_coder[i].coef_model, 12);
00665 model256_init(&ctx->image_coder[i].esc_model);
00666 model256_init(&ctx->image_coder[i].vec_entry_model);
00667 model_init(&ctx->image_coder[i].vec_size_model, 3);
00668 for (j = 0; j < 125; j++)
00669 model_init(&ctx->image_coder[i].vq_model[j], 5);
00670 model_init(&ctx->dct_coder[i].dc_model, 12);
00671 model256_init(&ctx->dct_coder[i].ac_model);
00672 model_init(&ctx->haar_coder[i].coef_hi_model, 12);
00673 model256_init(&ctx->haar_coder[i].coef_model);
00674 }
00675 }
00676
00677 static int mss3_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
00678 AVPacket *avpkt)
00679 {
00680 const uint8_t *buf = avpkt->data;
00681 int buf_size = avpkt->size;
00682 MSS3Context *c = avctx->priv_data;
00683 RangeCoder *acoder = &c->coder;
00684 GetByteContext gb;
00685 uint8_t *dst[3];
00686 int dec_width, dec_height, dec_x, dec_y, quality, keyframe;
00687 int x, y, i, mb_width, mb_height, blk_size, btype;
00688 int ret;
00689
00690 if (buf_size < HEADER_SIZE) {
00691 av_log(avctx, AV_LOG_ERROR,
00692 "Frame should have at least %d bytes, got %d instead\n",
00693 HEADER_SIZE, buf_size);
00694 return AVERROR_INVALIDDATA;
00695 }
00696
00697 bytestream2_init(&gb, buf, buf_size);
00698 keyframe = bytestream2_get_be32(&gb);
00699 if (keyframe & ~0x301) {
00700 av_log(avctx, AV_LOG_ERROR, "Invalid frame type %X\n", keyframe);
00701 return AVERROR_INVALIDDATA;
00702 }
00703 keyframe = !(keyframe & 1);
00704 bytestream2_skip(&gb, 6);
00705 dec_x = bytestream2_get_be16(&gb);
00706 dec_y = bytestream2_get_be16(&gb);
00707 dec_width = bytestream2_get_be16(&gb);
00708 dec_height = bytestream2_get_be16(&gb);
00709
00710 if (dec_x + dec_width > avctx->width ||
00711 dec_y + dec_height > avctx->height ||
00712 (dec_width | dec_height) & 0xF) {
00713 av_log(avctx, AV_LOG_ERROR, "Invalid frame dimensions %dx%d +%d,%d\n",
00714 dec_width, dec_height, dec_x, dec_y);
00715 return AVERROR_INVALIDDATA;
00716 }
00717 bytestream2_skip(&gb, 4);
00718 quality = bytestream2_get_byte(&gb);
00719 if (quality < 1 || quality > 100) {
00720 av_log(avctx, AV_LOG_ERROR, "Invalid quality setting %d\n", quality);
00721 return AVERROR_INVALIDDATA;
00722 }
00723 bytestream2_skip(&gb, 4);
00724
00725 if (keyframe && !bytestream2_get_bytes_left(&gb)) {
00726 av_log(avctx, AV_LOG_ERROR, "Keyframe without data found\n");
00727 return AVERROR_INVALIDDATA;
00728 }
00729 if (!keyframe && c->got_error)
00730 return buf_size;
00731 c->got_error = 0;
00732
00733 c->pic.reference = 3;
00734 c->pic.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE |
00735 FF_BUFFER_HINTS_REUSABLE;
00736 if ((ret = avctx->reget_buffer(avctx, &c->pic)) < 0) {
00737 av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
00738 return ret;
00739 }
00740 c->pic.key_frame = keyframe;
00741 c->pic.pict_type = keyframe ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
00742 if (!bytestream2_get_bytes_left(&gb)) {
00743 *data_size = sizeof(AVFrame);
00744 *(AVFrame*)data = c->pic;
00745
00746 return buf_size;
00747 }
00748
00749 reset_coders(c, quality);
00750
00751 rac_init(acoder, buf + HEADER_SIZE, buf_size - HEADER_SIZE);
00752
00753 mb_width = dec_width >> 4;
00754 mb_height = dec_height >> 4;
00755 dst[0] = c->pic.data[0] + dec_x + dec_y * c->pic.linesize[0];
00756 dst[1] = c->pic.data[1] + dec_x / 2 + (dec_y / 2) * c->pic.linesize[1];
00757 dst[2] = c->pic.data[2] + dec_x / 2 + (dec_y / 2) * c->pic.linesize[2];
00758 for (y = 0; y < mb_height; y++) {
00759 for (x = 0; x < mb_width; x++) {
00760 for (i = 0; i < 3; i++) {
00761 blk_size = 8 << !i;
00762
00763 btype = decode_block_type(acoder, c->btype + i);
00764 switch (btype) {
00765 case FILL_BLOCK:
00766 decode_fill_block(acoder, c->fill_coder + i,
00767 dst[i] + x * blk_size,
00768 c->pic.linesize[i], blk_size);
00769 break;
00770 case IMAGE_BLOCK:
00771 decode_image_block(acoder, c->image_coder + i,
00772 dst[i] + x * blk_size,
00773 c->pic.linesize[i], blk_size);
00774 break;
00775 case DCT_BLOCK:
00776 decode_dct_block(acoder, c->dct_coder + i,
00777 dst[i] + x * blk_size,
00778 c->pic.linesize[i], blk_size,
00779 c->dctblock, x, y);
00780 break;
00781 case HAAR_BLOCK:
00782 decode_haar_block(acoder, c->haar_coder + i,
00783 dst[i] + x * blk_size,
00784 c->pic.linesize[i], blk_size,
00785 c->hblock);
00786 break;
00787 }
00788 if (c->got_error || acoder->got_error) {
00789 av_log(avctx, AV_LOG_ERROR, "Error decoding block %d,%d\n",
00790 x, y);
00791 c->got_error = 1;
00792 return AVERROR_INVALIDDATA;
00793 }
00794 }
00795 }
00796 dst[0] += c->pic.linesize[0] * 16;
00797 dst[1] += c->pic.linesize[1] * 8;
00798 dst[2] += c->pic.linesize[2] * 8;
00799 }
00800
00801 *data_size = sizeof(AVFrame);
00802 *(AVFrame*)data = c->pic;
00803
00804 return buf_size;
00805 }
00806
00807 static av_cold int mss3_decode_init(AVCodecContext *avctx)
00808 {
00809 MSS3Context * const c = avctx->priv_data;
00810 int i;
00811
00812 c->avctx = avctx;
00813
00814 if ((avctx->width & 0xF) || (avctx->height & 0xF)) {
00815 av_log(avctx, AV_LOG_ERROR,
00816 "Image dimensions should be a multiple of 16.\n");
00817 return AVERROR_INVALIDDATA;
00818 }
00819
00820 c->got_error = 0;
00821 for (i = 0; i < 3; i++) {
00822 int b_width = avctx->width >> (2 + !!i);
00823 int b_height = avctx->height >> (2 + !!i);
00824 c->dct_coder[i].prev_dc_stride = b_width;
00825 c->dct_coder[i].prev_dc_height = b_height;
00826 c->dct_coder[i].prev_dc = av_malloc(sizeof(*c->dct_coder[i].prev_dc) *
00827 b_width * b_height);
00828 if (!c->dct_coder[i].prev_dc) {
00829 av_log(avctx, AV_LOG_ERROR, "Cannot allocate buffer\n");
00830 while (i >= 0) {
00831 av_freep(&c->dct_coder[i].prev_dc);
00832 i--;
00833 }
00834 return AVERROR(ENOMEM);
00835 }
00836 }
00837
00838 avctx->pix_fmt = PIX_FMT_YUV420P;
00839 avctx->coded_frame = &c->pic;
00840
00841 init_coders(c);
00842
00843 return 0;
00844 }
00845
00846 static av_cold int mss3_decode_end(AVCodecContext *avctx)
00847 {
00848 MSS3Context * const c = avctx->priv_data;
00849 int i;
00850
00851 if (c->pic.data[0])
00852 avctx->release_buffer(avctx, &c->pic);
00853 for (i = 0; i < 3; i++)
00854 av_freep(&c->dct_coder[i].prev_dc);
00855
00856 return 0;
00857 }
00858
00859 AVCodec ff_msa1_decoder = {
00860 .name = "msa1",
00861 .type = AVMEDIA_TYPE_VIDEO,
00862 .id = AV_CODEC_ID_MSA1,
00863 .priv_data_size = sizeof(MSS3Context),
00864 .init = mss3_decode_init,
00865 .close = mss3_decode_end,
00866 .decode = mss3_decode_frame,
00867 .capabilities = CODEC_CAP_DR1,
00868 .long_name = NULL_IF_CONFIG_SMALL("MS ATC Screen"),
00869 };