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