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 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
00155 send = 0;
00156 for (slice = 0; slice < c->slices; slice++) {
00157 uint8_t *dest;
00158 int slice_data_start, slice_data_end, slice_size;
00159
00160 sstart = send;
00161 send = (height * (slice + 1) / c->slices) & cmask;
00162 dest = dst + sstart * stride;
00163
00164
00165 slice_data_start = slice ? AV_RL32(src + slice * 4 - 4) : 0;
00166 slice_data_end = AV_RL32(src + slice * 4);
00167 slice_size = slice_data_end - slice_data_start;
00168
00169 if (!slice_size) {
00170 for (j = sstart; j < send; j++) {
00171 for (i = 0; i < width * step; i += step)
00172 dest[i] = 0x80;
00173 dest += stride;
00174 }
00175 continue;
00176 }
00177
00178 memcpy(c->slice_bits, src + slice_data_start + c->slices * 4,
00179 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,
00190 "Slice decoding ran out of bits\n");
00191 goto fail;
00192 }
00193 pix = get_vlc2(&gb, vlc.table, vlc.bits, 4);
00194 if (pix < 0) {
00195 av_log(c->avctx, AV_LOG_ERROR, "Decoding error\n");
00196 goto fail;
00197 }
00198 if (use_pred) {
00199 prev += pix;
00200 pix = prev;
00201 }
00202 dest[i] = pix;
00203 }
00204 dest += stride;
00205 }
00206 if (get_bits_left(&gb) > 32)
00207 av_log(c->avctx, AV_LOG_WARNING,
00208 "%d bits left after decoding slice\n", get_bits_left(&gb));
00209 }
00210
00211 ff_free_vlc(&vlc);
00212
00213 return 0;
00214 fail:
00215 ff_free_vlc(&vlc);
00216 return AVERROR_INVALIDDATA;
00217 }
00218
00219 static const int rgb_order[4] = { 1, 2, 0, 3 };
00220
00221 static void restore_rgb_planes(uint8_t *src, int step, int stride, int width,
00222 int height)
00223 {
00224 int i, j;
00225 uint8_t r, g, b;
00226
00227 for (j = 0; j < height; j++) {
00228 for (i = 0; i < width * step; i += step) {
00229 r = src[i];
00230 g = src[i + 1];
00231 b = src[i + 2];
00232 src[i] = r + g - 0x80;
00233 src[i + 2] = b + g - 0x80;
00234 }
00235 src += stride;
00236 }
00237 }
00238
00239 static void restore_median(uint8_t *src, int step, int stride,
00240 int width, int height, int slices, int rmode)
00241 {
00242 int i, j, slice;
00243 int A, B, C;
00244 uint8_t *bsrc;
00245 int slice_start, slice_height;
00246 const int cmask = ~rmode;
00247
00248 for (slice = 0; slice < slices; slice++) {
00249 slice_start = ((slice * height) / slices) & cmask;
00250 slice_height = ((((slice + 1) * height) / slices) & cmask) -
00251 slice_start;
00252
00253 bsrc = src + slice_start * stride;
00254
00255
00256 bsrc[0] += 0x80;
00257 A = bsrc[0];
00258 for (i = step; i < width * step; i += step) {
00259 bsrc[i] += A;
00260 A = bsrc[i];
00261 }
00262 bsrc += stride;
00263 if (slice_height == 1)
00264 continue;
00265
00266 C = bsrc[-stride];
00267 bsrc[0] += C;
00268 A = bsrc[0];
00269 for (i = step; i < width * step; i += step) {
00270 B = bsrc[i - stride];
00271 bsrc[i] += mid_pred(A, B, (uint8_t)(A + B - C));
00272 C = B;
00273 A = bsrc[i];
00274 }
00275 bsrc += stride;
00276
00277 for (j = 2; j < slice_height; j++) {
00278 for (i = 0; i < width * step; i += step) {
00279 B = bsrc[i - stride];
00280 bsrc[i] += mid_pred(A, B, (uint8_t)(A + B - C));
00281 C = B;
00282 A = bsrc[i];
00283 }
00284 bsrc += stride;
00285 }
00286 }
00287 }
00288
00289
00290
00291
00292
00293 static void restore_median_il(uint8_t *src, int step, int stride,
00294 int width, int height, int slices, int rmode)
00295 {
00296 int i, j, slice;
00297 int A, B, C;
00298 uint8_t *bsrc;
00299 int slice_start, slice_height;
00300 const int cmask = ~(rmode ? 3 : 1);
00301 const int stride2 = stride << 1;
00302
00303 for (slice = 0; slice < slices; slice++) {
00304 slice_start = ((slice * height) / slices) & cmask;
00305 slice_height = ((((slice + 1) * height) / slices) & cmask) -
00306 slice_start;
00307 slice_height >>= 1;
00308
00309 bsrc = src + slice_start * stride;
00310
00311
00312 bsrc[0] += 0x80;
00313 A = bsrc[0];
00314 for (i = step; i < width * step; i += step) {
00315 bsrc[i] += A;
00316 A = bsrc[i];
00317 }
00318 for (i = 0; i < width * step; i += step) {
00319 bsrc[stride + i] += A;
00320 A = bsrc[stride + i];
00321 }
00322 bsrc += stride2;
00323 if (slice_height == 1)
00324 continue;
00325
00326 C = bsrc[-stride2];
00327 bsrc[0] += C;
00328 A = bsrc[0];
00329 for (i = step; i < width * step; i += step) {
00330 B = bsrc[i - stride2];
00331 bsrc[i] += mid_pred(A, B, (uint8_t)(A + B - C));
00332 C = B;
00333 A = bsrc[i];
00334 }
00335 for (i = 0; i < width * step; i += step) {
00336 B = bsrc[i - stride];
00337 bsrc[stride + i] += mid_pred(A, B, (uint8_t)(A + B - C));
00338 C = B;
00339 A = bsrc[stride + i];
00340 }
00341 bsrc += stride2;
00342
00343 for (j = 2; j < slice_height; j++) {
00344 for (i = 0; i < width * step; i += step) {
00345 B = bsrc[i - stride2];
00346 bsrc[i] += mid_pred(A, B, (uint8_t)(A + B - C));
00347 C = B;
00348 A = bsrc[i];
00349 }
00350 for (i = 0; i < width * step; i += step) {
00351 B = bsrc[i - stride];
00352 bsrc[i + stride] += mid_pred(A, B, (uint8_t)(A + B - C));
00353 C = B;
00354 A = bsrc[i + stride];
00355 }
00356 bsrc += stride2;
00357 }
00358 }
00359 }
00360
00361 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size,
00362 AVPacket *avpkt)
00363 {
00364 const uint8_t *buf = avpkt->data;
00365 int buf_size = avpkt->size;
00366 UtvideoContext *c = avctx->priv_data;
00367 int i, j;
00368 const uint8_t *plane_start[5];
00369 int plane_size, max_slice_size = 0, slice_start, slice_end, slice_size;
00370 int ret;
00371 GetByteContext gb;
00372
00373 if (c->pic.data[0])
00374 ff_thread_release_buffer(avctx, &c->pic);
00375
00376 c->pic.reference = 3;
00377 c->pic.buffer_hints = FF_BUFFER_HINTS_VALID;
00378 if ((ret = ff_thread_get_buffer(avctx, &c->pic)) < 0) {
00379 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00380 return ret;
00381 }
00382
00383
00384 bytestream2_init(&gb, buf, buf_size);
00385 for (i = 0; i < c->planes; i++) {
00386 plane_start[i] = gb.buffer;
00387 if (bytestream2_get_bytes_left(&gb) < 256 + 4 * c->slices) {
00388 av_log(avctx, AV_LOG_ERROR, "Insufficient data for a plane\n");
00389 return AVERROR_INVALIDDATA;
00390 }
00391 bytestream2_skipu(&gb, 256);
00392 slice_start = 0;
00393 slice_end = 0;
00394 for (j = 0; j < c->slices; j++) {
00395 slice_end = bytestream2_get_le32u(&gb);
00396 slice_size = slice_end - slice_start;
00397 if (slice_end <= 0 || slice_size <= 0 ||
00398 bytestream2_get_bytes_left(&gb) < slice_end) {
00399 av_log(avctx, AV_LOG_ERROR, "Incorrect slice size\n");
00400 return AVERROR_INVALIDDATA;
00401 }
00402 slice_start = slice_end;
00403 max_slice_size = FFMAX(max_slice_size, slice_size);
00404 }
00405 plane_size = slice_end;
00406 bytestream2_skipu(&gb, plane_size);
00407 }
00408 plane_start[c->planes] = gb.buffer;
00409 if (bytestream2_get_bytes_left(&gb) < c->frame_info_size) {
00410 av_log(avctx, AV_LOG_ERROR, "Not enough data for frame information\n");
00411 return AVERROR_INVALIDDATA;
00412 }
00413 c->frame_info = bytestream2_get_le32u(&gb);
00414 av_log(avctx, AV_LOG_DEBUG, "frame information flags %X\n", c->frame_info);
00415
00416 c->frame_pred = (c->frame_info >> 8) & 3;
00417
00418 if (c->frame_pred == PRED_GRADIENT) {
00419 av_log_ask_for_sample(avctx, "Frame uses gradient prediction\n");
00420 return AVERROR_PATCHWELCOME;
00421 }
00422
00423 av_fast_malloc(&c->slice_bits, &c->slice_bits_size,
00424 max_slice_size + FF_INPUT_BUFFER_PADDING_SIZE);
00425
00426 if (!c->slice_bits) {
00427 av_log(avctx, AV_LOG_ERROR, "Cannot allocate temporary buffer\n");
00428 return AVERROR(ENOMEM);
00429 }
00430
00431 switch (c->avctx->pix_fmt) {
00432 case PIX_FMT_RGB24:
00433 case PIX_FMT_RGBA:
00434 for (i = 0; i < c->planes; i++) {
00435 ret = decode_plane(c, i, c->pic.data[0] + rgb_order[i], c->planes,
00436 c->pic.linesize[0], avctx->width, avctx->height,
00437 plane_start[i], c->frame_pred == PRED_LEFT);
00438 if (ret)
00439 return ret;
00440 if (c->frame_pred == PRED_MEDIAN)
00441 restore_median(c->pic.data[0] + rgb_order[i], c->planes,
00442 c->pic.linesize[0], avctx->width, avctx->height,
00443 c->slices, 0);
00444 }
00445 restore_rgb_planes(c->pic.data[0], c->planes, c->pic.linesize[0],
00446 avctx->width, avctx->height);
00447 break;
00448 case PIX_FMT_YUV420P:
00449 for (i = 0; i < 3; i++) {
00450 ret = decode_plane(c, i, c->pic.data[i], 1, c->pic.linesize[i],
00451 avctx->width >> !!i, avctx->height >> !!i,
00452 plane_start[i], c->frame_pred == PRED_LEFT);
00453 if (ret)
00454 return ret;
00455 if (c->frame_pred == PRED_MEDIAN) {
00456 if (!c->interlaced) {
00457 restore_median(c->pic.data[i], 1, c->pic.linesize[i],
00458 avctx->width >> !!i, avctx->height >> !!i,
00459 c->slices, !i);
00460 } else {
00461 restore_median_il(c->pic.data[i], 1, c->pic.linesize[i],
00462 avctx->width >> !!i,
00463 avctx->height >> !!i,
00464 c->slices, !i);
00465 }
00466 }
00467 }
00468 break;
00469 case PIX_FMT_YUV422P:
00470 for (i = 0; i < 3; i++) {
00471 ret = decode_plane(c, i, c->pic.data[i], 1, c->pic.linesize[i],
00472 avctx->width >> !!i, avctx->height,
00473 plane_start[i], 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 c->pic.key_frame = 1;
00492 c->pic.pict_type = AV_PICTURE_TYPE_I;
00493 *data_size = sizeof(AVFrame);
00494 *(AVFrame*)data = c->pic;
00495
00496
00497 return buf_size;
00498 }
00499
00500 static av_cold int decode_init(AVCodecContext *avctx)
00501 {
00502 UtvideoContext * const c = avctx->priv_data;
00503
00504 c->avctx = avctx;
00505
00506 ff_dsputil_init(&c->dsp, avctx);
00507
00508 if (avctx->extradata_size < 16) {
00509 av_log(avctx, AV_LOG_ERROR,
00510 "Insufficient extradata size %d, should be at least 16\n",
00511 avctx->extradata_size);
00512 return AVERROR_INVALIDDATA;
00513 }
00514
00515 av_log(avctx, AV_LOG_DEBUG, "Encoder version %d.%d.%d.%d\n",
00516 avctx->extradata[3], avctx->extradata[2],
00517 avctx->extradata[1], avctx->extradata[0]);
00518 av_log(avctx, AV_LOG_DEBUG, "Original format %X\n",
00519 AV_RB32(avctx->extradata + 4));
00520 c->frame_info_size = AV_RL32(avctx->extradata + 8);
00521 c->flags = AV_RL32(avctx->extradata + 12);
00522
00523 if (c->frame_info_size != 4)
00524 av_log_ask_for_sample(avctx, "Frame info is not 4 bytes\n");
00525 av_log(avctx, AV_LOG_DEBUG, "Encoding parameters %08X\n", c->flags);
00526 c->slices = (c->flags >> 24) + 1;
00527 c->compression = c->flags & 1;
00528 c->interlaced = c->flags & 0x800;
00529
00530 c->slice_bits_size = 0;
00531
00532 switch (avctx->codec_tag) {
00533 case MKTAG('U', 'L', 'R', 'G'):
00534 c->planes = 3;
00535 avctx->pix_fmt = PIX_FMT_RGB24;
00536 break;
00537 case MKTAG('U', 'L', 'R', 'A'):
00538 c->planes = 4;
00539 avctx->pix_fmt = PIX_FMT_RGBA;
00540 break;
00541 case MKTAG('U', 'L', 'Y', '0'):
00542 c->planes = 3;
00543 avctx->pix_fmt = PIX_FMT_YUV420P;
00544 break;
00545 case MKTAG('U', 'L', 'Y', '2'):
00546 c->planes = 3;
00547 avctx->pix_fmt = PIX_FMT_YUV422P;
00548 break;
00549 default:
00550 av_log(avctx, AV_LOG_ERROR, "Unknown Ut Video FOURCC provided (%08X)\n",
00551 avctx->codec_tag);
00552 return AVERROR_INVALIDDATA;
00553 }
00554
00555 return 0;
00556 }
00557
00558 static av_cold int decode_end(AVCodecContext *avctx)
00559 {
00560 UtvideoContext * const c = avctx->priv_data;
00561
00562 if (c->pic.data[0])
00563 ff_thread_release_buffer(avctx, &c->pic);
00564
00565 av_freep(&c->slice_bits);
00566
00567 return 0;
00568 }
00569
00570 AVCodec ff_utvideo_decoder = {
00571 .name = "utvideo",
00572 .type = AVMEDIA_TYPE_VIDEO,
00573 .id = CODEC_ID_UTVIDEO,
00574 .priv_data_size = sizeof(UtvideoContext),
00575 .init = decode_init,
00576 .close = decode_end,
00577 .decode = decode_frame,
00578 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_FRAME_THREADS,
00579 .long_name = NULL_IF_CONFIG_SMALL("Ut Video"),
00580 };