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 "get_bits.h"
00030 #include "mathops.h"
00031 #include "dsputil.h"
00032 #include "lagarithrac.h"
00033
00034 enum LagarithFrameType {
00035 FRAME_RAW = 1,
00036 FRAME_U_RGB24 = 2,
00037 FRAME_ARITH_YUY2 = 3,
00038 FRAME_ARITH_RGB24 = 4,
00039 FRAME_SOLID_GRAY = 5,
00040 FRAME_SOLID_COLOR = 6,
00041 FRAME_OLD_ARITH_RGB = 7,
00042 FRAME_ARITH_RGBA = 8,
00043 FRAME_SOLID_RGBA = 9,
00044 FRAME_ARITH_YV12 = 10,
00045 FRAME_REDUCED_RES = 11,
00046 };
00047
00048 typedef struct LagarithContext {
00049 AVCodecContext *avctx;
00050 AVFrame picture;
00051 DSPContext dsp;
00052 int zeros;
00053 int zeros_rem;
00054 uint8_t *rgb_planes;
00055 int rgb_stride;
00056 } LagarithContext;
00057
00066 static uint64_t softfloat_reciprocal(uint32_t denom)
00067 {
00068 int shift = av_log2(denom - 1) + 1;
00069 uint64_t ret = (1ULL << 52) / denom;
00070 uint64_t err = (1ULL << 52) - ret * denom;
00071 ret <<= shift;
00072 err <<= shift;
00073 err += denom / 2;
00074 return ret + err / denom;
00075 }
00076
00085 static uint32_t softfloat_mul(uint32_t x, uint64_t mantissa)
00086 {
00087 uint64_t l = x * (mantissa & 0xffffffff);
00088 uint64_t h = x * (mantissa >> 32);
00089 h += l >> 32;
00090 l &= 0xffffffff;
00091 l += 1 << av_log2(h >> 21);
00092 h += l >> 32;
00093 return h >> 20;
00094 }
00095
00096 static uint8_t lag_calc_zero_run(int8_t x)
00097 {
00098 return (x << 1) ^ (x >> 7);
00099 }
00100
00101 static int lag_decode_prob(GetBitContext *gb, uint32_t *value)
00102 {
00103 static const uint8_t series[] = { 1, 2, 3, 5, 8, 13, 21 };
00104 int i;
00105 int bit = 0;
00106 int bits = 0;
00107 int prevbit = 0;
00108 unsigned val;
00109
00110 for (i = 0; i < 7; i++) {
00111 if (prevbit && bit)
00112 break;
00113 prevbit = bit;
00114 bit = get_bits1(gb);
00115 if (bit && !prevbit)
00116 bits += series[i];
00117 }
00118 bits--;
00119 if (bits < 0 || bits > 31) {
00120 *value = 0;
00121 return -1;
00122 } else if (bits == 0) {
00123 *value = 0;
00124 return 0;
00125 }
00126
00127 val = get_bits_long(gb, bits);
00128 val |= 1 << bits;
00129
00130 *value = val - 1;
00131
00132 return 0;
00133 }
00134
00135 static int lag_read_prob_header(lag_rac *rac, GetBitContext *gb)
00136 {
00137 int i, j, scale_factor;
00138 unsigned prob, cumulative_target;
00139 unsigned cumul_prob = 0;
00140 unsigned scaled_cumul_prob = 0;
00141
00142 rac->prob[0] = 0;
00143 rac->prob[257] = UINT_MAX;
00144
00145 for (i = 1; i < 257; i++) {
00146 if (lag_decode_prob(gb, &rac->prob[i]) < 0) {
00147 av_log(rac->avctx, AV_LOG_ERROR, "Invalid probability encountered.\n");
00148 return -1;
00149 }
00150 if ((uint64_t)cumul_prob + rac->prob[i] > UINT_MAX) {
00151 av_log(rac->avctx, AV_LOG_ERROR, "Integer overflow encountered in cumulative probability calculation.\n");
00152 return -1;
00153 }
00154 cumul_prob += rac->prob[i];
00155 if (!rac->prob[i]) {
00156 if (lag_decode_prob(gb, &prob)) {
00157 av_log(rac->avctx, AV_LOG_ERROR, "Invalid probability run encountered.\n");
00158 return -1;
00159 }
00160 if (prob > 257 - i)
00161 prob = 257 - i;
00162 for (j = 0; j < prob; j++)
00163 rac->prob[++i] = 0;
00164 }
00165 }
00166
00167 if (!cumul_prob) {
00168 av_log(rac->avctx, AV_LOG_ERROR, "All probabilities are 0!\n");
00169 return -1;
00170 }
00171
00172
00173 scale_factor = av_log2(cumul_prob);
00174
00175 if (cumul_prob & (cumul_prob - 1)) {
00176 uint64_t mul = softfloat_reciprocal(cumul_prob);
00177 for (i = 1; i < 257; i++) {
00178 rac->prob[i] = softfloat_mul(rac->prob[i], mul);
00179 scaled_cumul_prob += rac->prob[i];
00180 }
00181
00182 scale_factor++;
00183 cumulative_target = 1 << scale_factor;
00184
00185 if (scaled_cumul_prob > cumulative_target) {
00186 av_log(rac->avctx, AV_LOG_ERROR,
00187 "Scaled probabilities are larger than target!\n");
00188 return -1;
00189 }
00190
00191 scaled_cumul_prob = cumulative_target - scaled_cumul_prob;
00192
00193 for (i = 1; scaled_cumul_prob; i = (i & 0x7f) + 1) {
00194 if (rac->prob[i]) {
00195 rac->prob[i]++;
00196 scaled_cumul_prob--;
00197 }
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209 }
00210 }
00211
00212 rac->scale = scale_factor;
00213
00214
00215 for (i = 1; i < 257; i++)
00216 rac->prob[i] += rac->prob[i - 1];
00217
00218 return 0;
00219 }
00220
00221 static void add_lag_median_prediction(uint8_t *dst, uint8_t *src1,
00222 uint8_t *diff, int w, int *left,
00223 int *left_top)
00224 {
00225
00226
00227
00228
00229 int i;
00230 uint8_t l, lt;
00231
00232 l = *left;
00233 lt = *left_top;
00234
00235 for (i = 0; i < w; i++) {
00236 l = mid_pred(l, src1[i], l + src1[i] - lt) + diff[i];
00237 lt = src1[i];
00238 dst[i] = l;
00239 }
00240
00241 *left = l;
00242 *left_top = lt;
00243 }
00244
00245 static void lag_pred_line(LagarithContext *l, uint8_t *buf,
00246 int width, int stride, int line)
00247 {
00248 int L, TL;
00249
00250 if (!line) {
00251
00252 L = l->dsp.add_hfyu_left_prediction(buf + 1, buf + 1,
00253 width - 1, buf[0]);
00254 } else {
00255
00256 L = buf[width - stride - 1];
00257
00258 if (line == 1) {
00259
00260
00261 TL = l->avctx->pix_fmt == PIX_FMT_YUV420P ? buf[-stride] : L;
00262 } else {
00263
00264 TL = buf[width - (2 * stride) - 1];
00265 }
00266
00267 add_lag_median_prediction(buf, buf - stride, buf,
00268 width, &L, &TL);
00269 }
00270 }
00271
00272 static int lag_decode_line(LagarithContext *l, lag_rac *rac,
00273 uint8_t *dst, int width, int stride,
00274 int esc_count)
00275 {
00276 int i = 0;
00277 int ret = 0;
00278
00279 if (!esc_count)
00280 esc_count = -1;
00281
00282
00283 handle_zeros:
00284 if (l->zeros_rem) {
00285 int count = FFMIN(l->zeros_rem, width - i);
00286 memset(dst + i, 0, count);
00287 i += count;
00288 l->zeros_rem -= count;
00289 }
00290
00291 while (i < width) {
00292 dst[i] = lag_get_rac(rac);
00293 ret++;
00294
00295 if (dst[i])
00296 l->zeros = 0;
00297 else
00298 l->zeros++;
00299
00300 i++;
00301 if (l->zeros == esc_count) {
00302 int index = lag_get_rac(rac);
00303 ret++;
00304
00305 l->zeros = 0;
00306
00307 l->zeros_rem = lag_calc_zero_run(index);
00308 goto handle_zeros;
00309 }
00310 }
00311 return ret;
00312 }
00313
00314 static int lag_decode_zero_run_line(LagarithContext *l, uint8_t *dst,
00315 const uint8_t *src, const uint8_t *src_end,
00316 int width, int esc_count)
00317 {
00318 int i = 0;
00319 int count;
00320 uint8_t zero_run = 0;
00321 const uint8_t *src_start = src;
00322 uint8_t mask1 = -(esc_count < 2);
00323 uint8_t mask2 = -(esc_count < 3);
00324 uint8_t *end = dst + (width - 2);
00325
00326 output_zeros:
00327 if (l->zeros_rem) {
00328 count = FFMIN(l->zeros_rem, width - i);
00329 if (end - dst < count) {
00330 av_log(l->avctx, AV_LOG_ERROR, "Too many zeros remaining.\n");
00331 return AVERROR_INVALIDDATA;
00332 }
00333
00334 memset(dst, 0, count);
00335 l->zeros_rem -= count;
00336 dst += count;
00337 }
00338
00339 while (dst < end) {
00340 i = 0;
00341 while (!zero_run && dst + i < end) {
00342 i++;
00343 if (src + i >= src_end)
00344 return AVERROR_INVALIDDATA;
00345 zero_run =
00346 !(src[i] | (src[i + 1] & mask1) | (src[i + 2] & mask2));
00347 }
00348 if (zero_run) {
00349 zero_run = 0;
00350 i += esc_count;
00351 memcpy(dst, src, i);
00352 dst += i;
00353 l->zeros_rem = lag_calc_zero_run(src[i]);
00354
00355 src += i + 1;
00356 goto output_zeros;
00357 } else {
00358 memcpy(dst, src, i);
00359 src += i;
00360 dst += i;
00361 }
00362 }
00363 return src_start - src;
00364 }
00365
00366
00367
00368 static int lag_decode_arith_plane(LagarithContext *l, uint8_t *dst,
00369 int width, int height, int stride,
00370 const uint8_t *src, int src_size)
00371 {
00372 int i = 0;
00373 int read = 0;
00374 uint32_t length;
00375 uint32_t offset = 1;
00376 int esc_count = src[0];
00377 GetBitContext gb;
00378 lag_rac rac;
00379 const uint8_t *src_end = src + src_size;
00380
00381 rac.avctx = l->avctx;
00382 l->zeros = 0;
00383
00384 if (esc_count < 4) {
00385 length = width * height;
00386 if (esc_count && AV_RL32(src + 1) < length) {
00387 length = AV_RL32(src + 1);
00388 offset += 4;
00389 }
00390
00391 init_get_bits(&gb, src + offset, src_size * 8);
00392
00393 if (lag_read_prob_header(&rac, &gb) < 0)
00394 return -1;
00395
00396 lag_rac_init(&rac, &gb, length - stride);
00397
00398 for (i = 0; i < height; i++)
00399 read += lag_decode_line(l, &rac, dst + (i * stride), width,
00400 stride, esc_count);
00401
00402 if (read > length)
00403 av_log(l->avctx, AV_LOG_WARNING,
00404 "Output more bytes than length (%d of %d)\n", read,
00405 length);
00406 } else if (esc_count < 8) {
00407 esc_count -= 4;
00408 if (esc_count > 0) {
00409
00410 for (i = 0; i < height; i++) {
00411 int res = lag_decode_zero_run_line(l, dst + (i * stride), src,
00412 src_end, width, esc_count);
00413 if (res < 0)
00414 return res;
00415 src += res;
00416 }
00417 } else {
00418 if (src_size < width * height)
00419 return AVERROR_INVALIDDATA;
00420
00421 for (i = 0; i < height; i++) {
00422 memcpy(dst + (i * stride), src, width);
00423 src += width;
00424 }
00425 }
00426 } else if (esc_count == 0xff) {
00427
00428 for (i = 0; i < height; i++)
00429 memset(dst + i * stride, src[1], width);
00430
00431
00432
00433 return 0;
00434 } else {
00435 av_log(l->avctx, AV_LOG_ERROR,
00436 "Invalid zero run escape code! (%#x)\n", esc_count);
00437 return -1;
00438 }
00439
00440 for (i = 0; i < height; i++) {
00441 lag_pred_line(l, dst, width, stride, i);
00442 dst += stride;
00443 }
00444
00445 return 0;
00446 }
00447
00456 static int lag_decode_frame(AVCodecContext *avctx,
00457 void *data, int *data_size, AVPacket *avpkt)
00458 {
00459 const uint8_t *buf = avpkt->data;
00460 int buf_size = avpkt->size;
00461 LagarithContext *l = avctx->priv_data;
00462 AVFrame *const p = &l->picture;
00463 uint8_t frametype = 0;
00464 uint32_t offset_gu = 0, offset_bv = 0, offset_ry = 9;
00465 int offs[4];
00466 uint8_t *srcs[4], *dst;
00467 int i, j, planes = 3;
00468
00469 AVFrame *picture = data;
00470
00471 if (p->data[0])
00472 avctx->release_buffer(avctx, p);
00473
00474 p->reference = 0;
00475 p->key_frame = 1;
00476
00477 frametype = buf[0];
00478
00479 offset_gu = AV_RL32(buf + 1);
00480 offset_bv = AV_RL32(buf + 5);
00481
00482 switch (frametype) {
00483 case FRAME_SOLID_RGBA:
00484 avctx->pix_fmt = PIX_FMT_RGB32;
00485
00486 if (avctx->get_buffer(avctx, p) < 0) {
00487 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00488 return -1;
00489 }
00490
00491 dst = p->data[0];
00492 for (j = 0; j < avctx->height; j++) {
00493 for (i = 0; i < avctx->width; i++)
00494 AV_WN32(dst + i * 4, offset_gu);
00495 dst += p->linesize[0];
00496 }
00497 break;
00498 case FRAME_ARITH_RGBA:
00499 avctx->pix_fmt = PIX_FMT_RGB32;
00500 planes = 4;
00501 offset_ry += 4;
00502 offs[3] = AV_RL32(buf + 9);
00503 case FRAME_ARITH_RGB24:
00504 if (frametype == FRAME_ARITH_RGB24)
00505 avctx->pix_fmt = PIX_FMT_RGB24;
00506
00507 if (avctx->get_buffer(avctx, p) < 0) {
00508 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00509 return -1;
00510 }
00511
00512 offs[0] = offset_bv;
00513 offs[1] = offset_gu;
00514 offs[2] = offset_ry;
00515
00516 if (!l->rgb_planes) {
00517 l->rgb_stride = FFALIGN(avctx->width, 16);
00518 l->rgb_planes = av_malloc(l->rgb_stride * avctx->height * planes);
00519 if (!l->rgb_planes) {
00520 av_log(avctx, AV_LOG_ERROR, "cannot allocate temporary buffer\n");
00521 return AVERROR(ENOMEM);
00522 }
00523 }
00524 for (i = 0; i < planes; i++)
00525 srcs[i] = l->rgb_planes + (i + 1) * l->rgb_stride * avctx->height - l->rgb_stride;
00526 if (offset_ry >= buf_size ||
00527 offset_gu >= buf_size ||
00528 offset_bv >= buf_size ||
00529 (planes == 4 && offs[3] >= buf_size)) {
00530 av_log(avctx, AV_LOG_ERROR,
00531 "Invalid frame offsets\n");
00532 return AVERROR_INVALIDDATA;
00533 }
00534 for (i = 0; i < planes; i++)
00535 lag_decode_arith_plane(l, srcs[i],
00536 avctx->width, avctx->height,
00537 -l->rgb_stride, buf + offs[i],
00538 buf_size - offs[i]);
00539 dst = p->data[0];
00540 for (i = 0; i < planes; i++)
00541 srcs[i] = l->rgb_planes + i * l->rgb_stride * avctx->height;
00542 for (j = 0; j < avctx->height; j++) {
00543 for (i = 0; i < avctx->width; i++) {
00544 uint8_t r, g, b, a;
00545 r = srcs[0][i];
00546 g = srcs[1][i];
00547 b = srcs[2][i];
00548 r += g;
00549 b += g;
00550 if (frametype == FRAME_ARITH_RGBA) {
00551 a = srcs[3][i];
00552 AV_WN32(dst + i * 4, MKBETAG(a, r, g, b));
00553 } else {
00554 dst[i * 3 + 0] = r;
00555 dst[i * 3 + 1] = g;
00556 dst[i * 3 + 2] = b;
00557 }
00558 }
00559 dst += p->linesize[0];
00560 for (i = 0; i < planes; i++)
00561 srcs[i] += l->rgb_stride;
00562 }
00563 break;
00564 case FRAME_ARITH_YV12:
00565 avctx->pix_fmt = PIX_FMT_YUV420P;
00566
00567 if (avctx->get_buffer(avctx, p) < 0) {
00568 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00569 return -1;
00570 }
00571
00572 if (offset_ry >= buf_size ||
00573 offset_gu >= buf_size ||
00574 offset_bv >= buf_size) {
00575 av_log(avctx, AV_LOG_ERROR,
00576 "Invalid frame offsets\n");
00577 return AVERROR_INVALIDDATA;
00578 }
00579
00580 lag_decode_arith_plane(l, p->data[0], avctx->width, avctx->height,
00581 p->linesize[0], buf + offset_ry,
00582 buf_size - offset_ry);
00583 lag_decode_arith_plane(l, p->data[2], avctx->width / 2,
00584 avctx->height / 2, p->linesize[2],
00585 buf + offset_gu, buf_size - offset_gu);
00586 lag_decode_arith_plane(l, p->data[1], avctx->width / 2,
00587 avctx->height / 2, p->linesize[1],
00588 buf + offset_bv, buf_size - offset_bv);
00589 break;
00590 default:
00591 av_log(avctx, AV_LOG_ERROR,
00592 "Unsupported Lagarith frame type: %#x\n", frametype);
00593 return -1;
00594 }
00595
00596 *picture = *p;
00597 *data_size = sizeof(AVFrame);
00598
00599 return buf_size;
00600 }
00601
00602 static av_cold int lag_decode_init(AVCodecContext *avctx)
00603 {
00604 LagarithContext *l = avctx->priv_data;
00605 l->avctx = avctx;
00606
00607 dsputil_init(&l->dsp, avctx);
00608
00609 return 0;
00610 }
00611
00612 static av_cold int lag_decode_end(AVCodecContext *avctx)
00613 {
00614 LagarithContext *l = avctx->priv_data;
00615
00616 if (l->picture.data[0])
00617 avctx->release_buffer(avctx, &l->picture);
00618 av_freep(&l->rgb_planes);
00619
00620 return 0;
00621 }
00622
00623 AVCodec ff_lagarith_decoder = {
00624 .name = "lagarith",
00625 .type = AVMEDIA_TYPE_VIDEO,
00626 .id = CODEC_ID_LAGARITH,
00627 .priv_data_size = sizeof(LagarithContext),
00628 .init = lag_decode_init,
00629 .close = lag_decode_end,
00630 .decode = lag_decode_frame,
00631 .capabilities = CODEC_CAP_DR1,
00632 .long_name = NULL_IF_CONFIG_SMALL("Lagarith lossless"),
00633 };