00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00027 #include <stdlib.h>
00028
00029 #include "libavutil/intreadwrite.h"
00030 #include "avcodec.h"
00031 #include "bytestream.h"
00032 #include "get_bits.h"
00033 #include "dsputil.h"
00034 #include "thread.h"
00035
00036 enum {
00037 PRED_NONE = 0,
00038 PRED_LEFT,
00039 PRED_GRADIENT,
00040 PRED_MEDIAN,
00041 };
00042
00043 typedef struct UtvideoContext {
00044 AVCodecContext *avctx;
00045 AVFrame pic;
00046 DSPContext dsp;
00047
00048 uint32_t frame_info_size, flags, frame_info;
00049 int planes;
00050 int slices;
00051 int compression;
00052 int interlaced;
00053 int frame_pred;
00054
00055 uint8_t *slice_bits;
00056 int slice_bits_size;
00057 } UtvideoContext;
00058
00059 typedef struct HuffEntry {
00060 uint8_t sym;
00061 uint8_t len;
00062 } HuffEntry;
00063
00064 static int huff_cmp(const void *a, const void *b)
00065 {
00066 const HuffEntry *aa = a, *bb = b;
00067 return (aa->len - bb->len)*256 + aa->sym - bb->sym;
00068 }
00069
00070 static int build_huff(const uint8_t *src, VLC *vlc, int *fsym)
00071 {
00072 int i;
00073 HuffEntry he[256];
00074 int last;
00075 uint32_t codes[256];
00076 uint8_t bits[256];
00077 uint8_t syms[256];
00078 uint32_t code;
00079
00080 *fsym = -1;
00081 for (i = 0; i < 256; i++) {
00082 he[i].sym = i;
00083 he[i].len = *src++;
00084 }
00085 qsort(he, 256, sizeof(*he), huff_cmp);
00086
00087 if (!he[0].len) {
00088 *fsym = he[0].sym;
00089 return 0;
00090 }
00091 if (he[0].len > 32)
00092 return -1;
00093
00094 last = 255;
00095 while (he[last].len == 255 && last)
00096 last--;
00097
00098 code = 1;
00099 for (i = last; i >= 0; i--) {
00100 codes[i] = code >> (32 - he[i].len);
00101 bits[i] = he[i].len;
00102 syms[i] = he[i].sym;
00103 code += 0x80000000u >> (he[i].len - 1);
00104 }
00105
00106 return ff_init_vlc_sparse(vlc, FFMIN(he[last].len, 9), last + 1,
00107 bits, sizeof(*bits), sizeof(*bits),
00108 codes, sizeof(*codes), sizeof(*codes),
00109 syms, sizeof(*syms), sizeof(*syms), 0);
00110 }
00111
00112 static int decode_plane(UtvideoContext *c, int plane_no,
00113 uint8_t *dst, int step, int stride,
00114 int width, int height,
00115 const uint8_t *src, int src_size, int use_pred)
00116 {
00117 int i, j, slice, pix;
00118 int sstart, send;
00119 VLC vlc;
00120 GetBitContext gb;
00121 int prev, fsym;
00122 const int cmask = ~(!plane_no && c->avctx->pix_fmt == PIX_FMT_YUV420P);
00123
00124 if (build_huff(src, &vlc, &fsym)) {
00125 av_log(c->avctx, AV_LOG_ERROR, "Cannot build Huffman codes\n");
00126 return AVERROR_INVALIDDATA;
00127 }
00128 if (fsym >= 0) {
00129 send = 0;
00130 for (slice = 0; slice < c->slices; slice++) {
00131 uint8_t *dest;
00132
00133 sstart = send;
00134 send = (height * (slice + 1) / c->slices) & cmask;
00135 dest = dst + sstart * stride;
00136
00137 prev = 0x80;
00138 for (j = sstart; j < send; j++) {
00139 for (i = 0; i < width * step; i += step) {
00140 pix = fsym;
00141 if (use_pred) {
00142 prev += pix;
00143 pix = prev;
00144 }
00145 dest[i] = pix;
00146 }
00147 dest += stride;
00148 }
00149 }
00150 return 0;
00151 }
00152
00153 src += 256;
00154 src_size -= 256;
00155
00156 send = 0;
00157 for (slice = 0; slice < c->slices; slice++) {
00158 uint8_t *dest;
00159 int slice_data_start, slice_data_end, slice_size;
00160
00161 sstart = send;
00162 send = (height * (slice + 1) / c->slices) & cmask;
00163 dest = dst + sstart * stride;
00164
00165
00166 slice_data_start = slice ? AV_RL32(src + slice * 4 - 4) : 0;
00167 slice_data_end = AV_RL32(src + slice * 4);
00168 slice_size = slice_data_end - slice_data_start;
00169
00170 if (!slice_size) {
00171 for (j = sstart; j < send; j++) {
00172 for (i = 0; i < width * step; i += step)
00173 dest[i] = 0x80;
00174 dest += stride;
00175 }
00176 continue;
00177 }
00178
00179 memcpy(c->slice_bits, src + slice_data_start + c->slices * 4, slice_size);
00180 memset(c->slice_bits + slice_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
00181 c->dsp.bswap_buf((uint32_t*)c->slice_bits, (uint32_t*)c->slice_bits,
00182 (slice_data_end - slice_data_start + 3) >> 2);
00183 init_get_bits(&gb, c->slice_bits, slice_size * 8);
00184
00185 prev = 0x80;
00186 for (j = sstart; j < send; j++) {
00187 for (i = 0; i < width * step; i += step) {
00188 if (get_bits_left(&gb) <= 0) {
00189 av_log(c->avctx, AV_LOG_ERROR, "Slice decoding ran out of bits\n");
00190 goto fail;
00191 }
00192 pix = get_vlc2(&gb, vlc.table, vlc.bits, 4);
00193 if (pix < 0) {
00194 av_log(c->avctx, AV_LOG_ERROR, "Decoding error\n");
00195 goto fail;
00196 }
00197 if (use_pred) {
00198 prev += pix;
00199 pix = prev;
00200 }
00201 dest[i] = pix;
00202 }
00203 dest += stride;
00204 }
00205 if (get_bits_left(&gb) > 32)
00206 av_log(c->avctx, AV_LOG_WARNING, "%d bits left after decoding slice\n",
00207 get_bits_left(&gb));
00208 }
00209
00210 ff_free_vlc(&vlc);
00211
00212 return 0;
00213 fail:
00214 ff_free_vlc(&vlc);
00215 return AVERROR_INVALIDDATA;
00216 }
00217
00218 static const int rgb_order[4] = { 1, 2, 0, 3 };
00219
00220 static void restore_rgb_planes(uint8_t *src, int step, int stride, int width, int height)
00221 {
00222 int i, j;
00223 uint8_t r, g, b;
00224
00225 for (j = 0; j < height; j++) {
00226 for (i = 0; i < width * step; i += step) {
00227 r = src[i];
00228 g = src[i + 1];
00229 b = src[i + 2];
00230 src[i] = r + g - 0x80;
00231 src[i + 2] = b + g - 0x80;
00232 }
00233 src += stride;
00234 }
00235 }
00236
00237 static void restore_median(uint8_t *src, int step, int stride,
00238 int width, int height, int slices, int rmode)
00239 {
00240 int i, j, slice;
00241 int A, B, C;
00242 uint8_t *bsrc;
00243 int slice_start, slice_height;
00244 const int cmask = ~rmode;
00245
00246 for (slice = 0; slice < slices; slice++) {
00247 slice_start = ((slice * height) / slices) & cmask;
00248 slice_height = ((((slice + 1) * height) / slices) & cmask) - slice_start;
00249
00250 bsrc = src + slice_start * stride;
00251
00252
00253 bsrc[0] += 0x80;
00254 A = bsrc[0];
00255 for (i = step; i < width * step; i += step) {
00256 bsrc[i] += A;
00257 A = bsrc[i];
00258 }
00259 bsrc += stride;
00260 if (slice_height == 1)
00261 continue;
00262
00263 C = bsrc[-stride];
00264 bsrc[0] += C;
00265 A = bsrc[0];
00266 for (i = step; i < width * step; i += step) {
00267 B = bsrc[i - stride];
00268 bsrc[i] += mid_pred(A, B, (uint8_t)(A + B - C));
00269 C = B;
00270 A = bsrc[i];
00271 }
00272 bsrc += stride;
00273
00274 for (j = 2; j < slice_height; j++) {
00275 for (i = 0; i < width * step; i += step) {
00276 B = bsrc[i - stride];
00277 bsrc[i] += mid_pred(A, B, (uint8_t)(A + B - C));
00278 C = B;
00279 A = bsrc[i];
00280 }
00281 bsrc += stride;
00282 }
00283 }
00284 }
00285
00286
00287
00288
00289
00290 static void restore_median_il(uint8_t *src, int step, int stride,
00291 int width, int height, int slices, int rmode)
00292 {
00293 int i, j, slice;
00294 int A, B, C;
00295 uint8_t *bsrc;
00296 int slice_start, slice_height;
00297 const int cmask = ~(rmode ? 3 : 1);
00298 const int stride2 = stride << 1;
00299
00300 for (slice = 0; slice < slices; slice++) {
00301 slice_start = ((slice * height) / slices) & cmask;
00302 slice_height = ((((slice + 1) * height) / slices) & cmask) - slice_start;
00303 slice_height >>= 1;
00304
00305 bsrc = src + slice_start * stride;
00306
00307
00308 bsrc[0] += 0x80;
00309 A = bsrc[0];
00310 for (i = step; i < width * step; i += step) {
00311 bsrc[i] += A;
00312 A = bsrc[i];
00313 }
00314 for (i = 0; i < width * step; i += step) {
00315 bsrc[stride + i] += A;
00316 A = bsrc[stride + i];
00317 }
00318 bsrc += stride2;
00319 if (slice_height == 1)
00320 continue;
00321
00322 C = bsrc[-stride2];
00323 bsrc[0] += C;
00324 A = bsrc[0];
00325 for (i = step; i < width * step; i += step) {
00326 B = bsrc[i - stride2];
00327 bsrc[i] += mid_pred(A, B, (uint8_t)(A + B - C));
00328 C = B;
00329 A = bsrc[i];
00330 }
00331 for (i = 0; i < width * step; i += step) {
00332 B = bsrc[i - stride];
00333 bsrc[stride + i] += mid_pred(A, B, (uint8_t)(A + B - C));
00334 C = B;
00335 A = bsrc[stride + i];
00336 }
00337 bsrc += stride2;
00338
00339 for (j = 2; j < slice_height; j++) {
00340 for (i = 0; i < width * step; i += step) {
00341 B = bsrc[i - stride2];
00342 bsrc[i] += mid_pred(A, B, (uint8_t)(A + B - C));
00343 C = B;
00344 A = bsrc[i];
00345 }
00346 for (i = 0; i < width * step; i += step) {
00347 B = bsrc[i - stride];
00348 bsrc[i + stride] += mid_pred(A, B, (uint8_t)(A + B - C));
00349 C = B;
00350 A = bsrc[i + stride];
00351 }
00352 bsrc += stride2;
00353 }
00354 }
00355 }
00356
00357 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt)
00358 {
00359 const uint8_t *buf = avpkt->data;
00360 int buf_size = avpkt->size;
00361 UtvideoContext *c = avctx->priv_data;
00362 int i, j;
00363 const uint8_t *plane_start[5];
00364 int plane_size, max_slice_size = 0, slice_start, slice_end, slice_size;
00365 int ret;
00366 GetByteContext gb;
00367
00368 if (c->pic.data[0])
00369 ff_thread_release_buffer(avctx, &c->pic);
00370
00371 c->pic.reference = 3;
00372 c->pic.buffer_hints = FF_BUFFER_HINTS_VALID;
00373 if ((ret = ff_thread_get_buffer(avctx, &c->pic)) < 0) {
00374 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00375 return ret;
00376 }
00377
00378 ff_thread_finish_setup(avctx);
00379
00380
00381 bytestream2_init(&gb, buf, buf_size);
00382 for (i = 0; i < c->planes; i++) {
00383 plane_start[i] = gb.buffer;
00384 if (bytestream2_get_bytes_left(&gb) < 256 + 4 * c->slices) {
00385 av_log(avctx, AV_LOG_ERROR, "Insufficient data for a plane\n");
00386 return AVERROR_INVALIDDATA;
00387 }
00388 bytestream2_skipu(&gb, 256);
00389 slice_start = 0;
00390 slice_end = 0;
00391 for (j = 0; j < c->slices; j++) {
00392 slice_end = bytestream2_get_le32u(&gb);
00393 slice_size = slice_end - slice_start;
00394 if (slice_end <= 0 || slice_size <= 0 ||
00395 bytestream2_get_bytes_left(&gb) < slice_end) {
00396 av_log(avctx, AV_LOG_ERROR, "Incorrect slice size\n");
00397 return AVERROR_INVALIDDATA;
00398 }
00399 slice_start = slice_end;
00400 max_slice_size = FFMAX(max_slice_size, slice_size);
00401 }
00402 plane_size = slice_end;
00403 bytestream2_skipu(&gb, plane_size);
00404 }
00405 plane_start[c->planes] = gb.buffer;
00406 if (bytestream2_get_bytes_left(&gb) < c->frame_info_size) {
00407 av_log(avctx, AV_LOG_ERROR, "Not enough data for frame information\n");
00408 return AVERROR_INVALIDDATA;
00409 }
00410 c->frame_info = bytestream2_get_le32u(&gb);
00411 av_log(avctx, AV_LOG_DEBUG, "frame information flags %X\n", c->frame_info);
00412
00413 c->frame_pred = (c->frame_info >> 8) & 3;
00414
00415 if (c->frame_pred == PRED_GRADIENT) {
00416 av_log_ask_for_sample(avctx, "Frame uses gradient prediction\n");
00417 return AVERROR_PATCHWELCOME;
00418 }
00419
00420 av_fast_malloc(&c->slice_bits, &c->slice_bits_size,
00421 max_slice_size + FF_INPUT_BUFFER_PADDING_SIZE);
00422
00423 if (!c->slice_bits) {
00424 av_log(avctx, AV_LOG_ERROR, "Cannot allocate temporary buffer\n");
00425 return AVERROR(ENOMEM);
00426 }
00427
00428 switch (c->avctx->pix_fmt) {
00429 case PIX_FMT_RGB24:
00430 case PIX_FMT_RGBA:
00431 for (i = 0; i < c->planes; i++) {
00432 ret = decode_plane(c, i, c->pic.data[0] + rgb_order[i], c->planes,
00433 c->pic.linesize[0], avctx->width, avctx->height,
00434 plane_start[i], plane_start[i + 1] - plane_start[i],
00435 c->frame_pred == PRED_LEFT);
00436 if (ret)
00437 return ret;
00438 if (c->frame_pred == PRED_MEDIAN)
00439 restore_median(c->pic.data[0] + rgb_order[i], c->planes,
00440 c->pic.linesize[0], avctx->width, avctx->height,
00441 c->slices, 0);
00442 }
00443 restore_rgb_planes(c->pic.data[0], c->planes, c->pic.linesize[0],
00444 avctx->width, avctx->height);
00445 break;
00446 case PIX_FMT_YUV420P:
00447 for (i = 0; i < 3; i++) {
00448 ret = decode_plane(c, i, c->pic.data[i], 1,
00449 c->pic.linesize[i], avctx->width >> !!i, avctx->height >> !!i,
00450 plane_start[i], plane_start[i + 1] - plane_start[i],
00451 c->frame_pred == PRED_LEFT);
00452 if (ret)
00453 return ret;
00454 if (c->frame_pred == PRED_MEDIAN) {
00455 if (!c->interlaced) {
00456 restore_median(c->pic.data[i], 1, c->pic.linesize[i],
00457 avctx->width >> !!i, avctx->height >> !!i,
00458 c->slices, !i);
00459 } else {
00460 restore_median_il(c->pic.data[i], 1, c->pic.linesize[i],
00461 avctx->width >> !!i,
00462 avctx->height >> !!i,
00463 c->slices, !i);
00464 }
00465 }
00466 }
00467 break;
00468 case PIX_FMT_YUV422P:
00469 for (i = 0; i < 3; i++) {
00470 ret = decode_plane(c, i, c->pic.data[i], 1,
00471 c->pic.linesize[i], avctx->width >> !!i, avctx->height,
00472 plane_start[i], plane_start[i + 1] - plane_start[i],
00473 c->frame_pred == PRED_LEFT);
00474 if (ret)
00475 return ret;
00476 if (c->frame_pred == PRED_MEDIAN) {
00477 if (!c->interlaced) {
00478 restore_median(c->pic.data[i], 1, c->pic.linesize[i],
00479 avctx->width >> !!i, avctx->height,
00480 c->slices, 0);
00481 } else {
00482 restore_median_il(c->pic.data[i], 1, c->pic.linesize[i],
00483 avctx->width >> !!i, avctx->height,
00484 c->slices, 0);
00485 }
00486 }
00487 }
00488 break;
00489 }
00490
00491 *data_size = sizeof(AVFrame);
00492 *(AVFrame*)data = c->pic;
00493
00494
00495 return buf_size;
00496 }
00497
00498 static av_cold int decode_init(AVCodecContext *avctx)
00499 {
00500 UtvideoContext * const c = avctx->priv_data;
00501
00502 c->avctx = avctx;
00503
00504 dsputil_init(&c->dsp, avctx);
00505
00506 if (avctx->extradata_size < 16) {
00507 av_log(avctx, AV_LOG_ERROR, "Insufficient extradata size %d, should be at least 16\n",
00508 avctx->extradata_size);
00509 return AVERROR_INVALIDDATA;
00510 }
00511
00512 av_log(avctx, AV_LOG_DEBUG, "Encoder version %d.%d.%d.%d\n",
00513 avctx->extradata[3], avctx->extradata[2],
00514 avctx->extradata[1], avctx->extradata[0]);
00515 av_log(avctx, AV_LOG_DEBUG, "Original format %X\n", AV_RB32(avctx->extradata + 4));
00516 c->frame_info_size = AV_RL32(avctx->extradata + 8);
00517 c->flags = AV_RL32(avctx->extradata + 12);
00518
00519 if (c->frame_info_size != 4)
00520 av_log_ask_for_sample(avctx, "Frame info is not 4 bytes\n");
00521 av_log(avctx, AV_LOG_DEBUG, "Encoding parameters %08X\n", c->flags);
00522 c->slices = (c->flags >> 24) + 1;
00523 c->compression = c->flags & 1;
00524 c->interlaced = c->flags & 0x800;
00525
00526 c->slice_bits_size = 0;
00527
00528 switch (avctx->codec_tag) {
00529 case MKTAG('U', 'L', 'R', 'G'):
00530 c->planes = 3;
00531 avctx->pix_fmt = PIX_FMT_RGB24;
00532 break;
00533 case MKTAG('U', 'L', 'R', 'A'):
00534 c->planes = 4;
00535 avctx->pix_fmt = PIX_FMT_RGBA;
00536 break;
00537 case MKTAG('U', 'L', 'Y', '0'):
00538 c->planes = 3;
00539 avctx->pix_fmt = PIX_FMT_YUV420P;
00540 break;
00541 case MKTAG('U', 'L', 'Y', '2'):
00542 c->planes = 3;
00543 avctx->pix_fmt = PIX_FMT_YUV422P;
00544 break;
00545 default:
00546 av_log(avctx, AV_LOG_ERROR, "Unknown Ut Video FOURCC provided (%08X)\n",
00547 avctx->codec_tag);
00548 return AVERROR_INVALIDDATA;
00549 }
00550
00551 return 0;
00552 }
00553
00554 static av_cold int decode_end(AVCodecContext *avctx)
00555 {
00556 UtvideoContext * const c = avctx->priv_data;
00557
00558 if (c->pic.data[0])
00559 ff_thread_release_buffer(avctx, &c->pic);
00560
00561 av_freep(&c->slice_bits);
00562
00563 return 0;
00564 }
00565
00566 AVCodec ff_utvideo_decoder = {
00567 .name = "utvideo",
00568 .type = AVMEDIA_TYPE_VIDEO,
00569 .id = CODEC_ID_UTVIDEO,
00570 .priv_data_size = sizeof(UtvideoContext),
00571 .init = decode_init,
00572 .close = decode_end,
00573 .decode = decode_frame,
00574 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_FRAME_THREADS,
00575 .long_name = NULL_IF_CONFIG_SMALL("Ut Video"),
00576 };
00577