3 * Copyright (c) 2011 Konstantin Shishkov
5 * This file is part of Libav.
7 * Libav is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * Libav is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
29 #include "libavutil/intreadwrite.h"
31 #include "bytestream.h"
43 typedef struct UtvideoContext {
44 AVCodecContext *avctx;
48 uint32_t frame_info_size, flags, frame_info;
59 typedef struct HuffEntry {
64 static int huff_cmp(const void *a, const void *b)
66 const HuffEntry *aa = a, *bb = b;
67 return (aa->len - bb->len)*256 + aa->sym - bb->sym;
70 static int build_huff(const uint8_t *src, VLC *vlc, int *fsym)
81 for (i = 0; i < 256; i++) {
85 qsort(he, 256, sizeof(*he), huff_cmp);
95 while (he[last].len == 255 && last)
99 for (i = last; i >= 0; i--) {
100 codes[i] = code >> (32 - he[i].len);
103 code += 0x80000000u >> (he[i].len - 1);
106 return ff_init_vlc_sparse(vlc, FFMIN(he[last].len, 9), last + 1,
107 bits, sizeof(*bits), sizeof(*bits),
108 codes, sizeof(*codes), sizeof(*codes),
109 syms, sizeof(*syms), sizeof(*syms), 0);
112 static int decode_plane(UtvideoContext *c, int plane_no,
113 uint8_t *dst, int step, int stride,
114 int width, int height,
115 const uint8_t *src, int use_pred)
117 int i, j, slice, pix;
122 const int cmask = ~(!plane_no && c->avctx->pix_fmt == PIX_FMT_YUV420P);
124 if (build_huff(src, &vlc, &fsym)) {
125 av_log(c->avctx, AV_LOG_ERROR, "Cannot build Huffman codes\n");
126 return AVERROR_INVALIDDATA;
128 if (fsym >= 0) { // build_huff reported a symbol to fill slices with
130 for (slice = 0; slice < c->slices; slice++) {
134 send = (height * (slice + 1) / c->slices) & cmask;
135 dest = dst + sstart * stride;
138 for (j = sstart; j < send; j++) {
139 for (i = 0; i < width * step; i += step) {
156 for (slice = 0; slice < c->slices; slice++) {
158 int slice_data_start, slice_data_end, slice_size;
161 send = (height * (slice + 1) / c->slices) & cmask;
162 dest = dst + sstart * stride;
164 // slice offset and size validation was done earlier
165 slice_data_start = slice ? AV_RL32(src + slice * 4 - 4) : 0;
166 slice_data_end = AV_RL32(src + slice * 4);
167 slice_size = slice_data_end - slice_data_start;
170 for (j = sstart; j < send; j++) {
171 for (i = 0; i < width * step; i += step)
178 memcpy(c->slice_bits, src + slice_data_start + c->slices * 4,
180 memset(c->slice_bits + slice_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
181 c->dsp.bswap_buf((uint32_t *) c->slice_bits, (uint32_t *) c->slice_bits,
182 (slice_data_end - slice_data_start + 3) >> 2);
183 init_get_bits(&gb, c->slice_bits, slice_size * 8);
186 for (j = sstart; j < send; j++) {
187 for (i = 0; i < width * step; i += step) {
188 if (get_bits_left(&gb) <= 0) {
189 av_log(c->avctx, AV_LOG_ERROR,
190 "Slice decoding ran out of bits\n");
193 pix = get_vlc2(&gb, vlc.table, vlc.bits, 4);
195 av_log(c->avctx, AV_LOG_ERROR, "Decoding error\n");
206 if (get_bits_left(&gb) > 32)
207 av_log(c->avctx, AV_LOG_WARNING,
208 "%d bits left after decoding slice\n", get_bits_left(&gb));
216 return AVERROR_INVALIDDATA;
219 static const int rgb_order[4] = { 1, 2, 0, 3 };
221 static void restore_rgb_planes(uint8_t *src, int step, int stride, int width,
227 for (j = 0; j < height; j++) {
228 for (i = 0; i < width * step; i += step) {
232 src[i] = r + g - 0x80;
233 src[i + 2] = b + g - 0x80;
239 static void restore_median(uint8_t *src, int step, int stride,
240 int width, int height, int slices, int rmode)
245 int slice_start, slice_height;
246 const int cmask = ~rmode;
248 for (slice = 0; slice < slices; slice++) {
249 slice_start = ((slice * height) / slices) & cmask;
250 slice_height = ((((slice + 1) * height) / slices) & cmask) -
253 bsrc = src + slice_start * stride;
255 // first line - left neighbour prediction
258 for (i = step; i < width * step; i += step) {
263 if (slice_height == 1)
265 // second line - first element has top prediction, the rest uses median
269 for (i = step; i < width * step; i += step) {
270 B = bsrc[i - stride];
271 bsrc[i] += mid_pred(A, B, (uint8_t)(A + B - C));
276 // the rest of lines use continuous median prediction
277 for (j = 2; j < slice_height; j++) {
278 for (i = 0; i < width * step; i += step) {
279 B = bsrc[i - stride];
280 bsrc[i] += mid_pred(A, B, (uint8_t)(A + B - C));
289 /* UtVideo interlaced mode treats every two lines as a single one,
290 * so restoring function should take care of possible padding between
291 * two parts of the same "line".
293 static void restore_median_il(uint8_t *src, int step, int stride,
294 int width, int height, int slices, int rmode)
299 int slice_start, slice_height;
300 const int cmask = ~(rmode ? 3 : 1);
301 const int stride2 = stride << 1;
303 for (slice = 0; slice < slices; slice++) {
304 slice_start = ((slice * height) / slices) & cmask;
305 slice_height = ((((slice + 1) * height) / slices) & cmask) -
309 bsrc = src + slice_start * stride;
311 // first line - left neighbour prediction
314 for (i = step; i < width * step; i += step) {
318 for (i = 0; i < width * step; i += step) {
319 bsrc[stride + i] += A;
320 A = bsrc[stride + i];
323 if (slice_height == 1)
325 // second line - first element has top prediction, the rest uses median
329 for (i = step; i < width * step; i += step) {
330 B = bsrc[i - stride2];
331 bsrc[i] += mid_pred(A, B, (uint8_t)(A + B - C));
335 for (i = 0; i < width * step; i += step) {
336 B = bsrc[i - stride];
337 bsrc[stride + i] += mid_pred(A, B, (uint8_t)(A + B - C));
339 A = bsrc[stride + i];
342 // the rest of lines use continuous median prediction
343 for (j = 2; j < slice_height; j++) {
344 for (i = 0; i < width * step; i += step) {
345 B = bsrc[i - stride2];
346 bsrc[i] += mid_pred(A, B, (uint8_t)(A + B - C));
350 for (i = 0; i < width * step; i += step) {
351 B = bsrc[i - stride];
352 bsrc[i + stride] += mid_pred(A, B, (uint8_t)(A + B - C));
354 A = bsrc[i + stride];
361 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size,
364 const uint8_t *buf = avpkt->data;
365 int buf_size = avpkt->size;
366 UtvideoContext *c = avctx->priv_data;
368 const uint8_t *plane_start[5];
369 int plane_size, max_slice_size = 0, slice_start, slice_end, slice_size;
374 ff_thread_release_buffer(avctx, &c->pic);
376 c->pic.reference = 1;
377 c->pic.buffer_hints = FF_BUFFER_HINTS_VALID;
378 if ((ret = ff_thread_get_buffer(avctx, &c->pic)) < 0) {
379 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
383 ff_thread_finish_setup(avctx);
385 /* parse plane structure to get frame flags and validate slice offsets */
386 bytestream2_init(&gb, buf, buf_size);
387 for (i = 0; i < c->planes; i++) {
388 plane_start[i] = gb.buffer;
389 if (bytestream2_get_bytes_left(&gb) < 256 + 4 * c->slices) {
390 av_log(avctx, AV_LOG_ERROR, "Insufficient data for a plane\n");
391 return AVERROR_INVALIDDATA;
393 bytestream2_skipu(&gb, 256);
396 for (j = 0; j < c->slices; j++) {
397 slice_end = bytestream2_get_le32u(&gb);
398 slice_size = slice_end - slice_start;
399 if (slice_end <= 0 || slice_size <= 0 ||
400 bytestream2_get_bytes_left(&gb) < slice_end) {
401 av_log(avctx, AV_LOG_ERROR, "Incorrect slice size\n");
402 return AVERROR_INVALIDDATA;
404 slice_start = slice_end;
405 max_slice_size = FFMAX(max_slice_size, slice_size);
407 plane_size = slice_end;
408 bytestream2_skipu(&gb, plane_size);
410 plane_start[c->planes] = gb.buffer;
411 if (bytestream2_get_bytes_left(&gb) < c->frame_info_size) {
412 av_log(avctx, AV_LOG_ERROR, "Not enough data for frame information\n");
413 return AVERROR_INVALIDDATA;
415 c->frame_info = bytestream2_get_le32u(&gb);
416 av_log(avctx, AV_LOG_DEBUG, "frame information flags %X\n", c->frame_info);
418 c->frame_pred = (c->frame_info >> 8) & 3;
420 if (c->frame_pred == PRED_GRADIENT) {
421 av_log_ask_for_sample(avctx, "Frame uses gradient prediction\n");
422 return AVERROR_PATCHWELCOME;
425 av_fast_malloc(&c->slice_bits, &c->slice_bits_size,
426 max_slice_size + FF_INPUT_BUFFER_PADDING_SIZE);
428 if (!c->slice_bits) {
429 av_log(avctx, AV_LOG_ERROR, "Cannot allocate temporary buffer\n");
430 return AVERROR(ENOMEM);
433 switch (c->avctx->pix_fmt) {
436 for (i = 0; i < c->planes; i++) {
437 ret = decode_plane(c, i, c->pic.data[0] + rgb_order[i], c->planes,
438 c->pic.linesize[0], avctx->width, avctx->height,
439 plane_start[i], c->frame_pred == PRED_LEFT);
442 if (c->frame_pred == PRED_MEDIAN)
443 restore_median(c->pic.data[0] + rgb_order[i], c->planes,
444 c->pic.linesize[0], avctx->width, avctx->height,
447 restore_rgb_planes(c->pic.data[0], c->planes, c->pic.linesize[0],
448 avctx->width, avctx->height);
450 case PIX_FMT_YUV420P:
451 for (i = 0; i < 3; i++) {
452 ret = decode_plane(c, i, c->pic.data[i], 1, c->pic.linesize[i],
453 avctx->width >> !!i, avctx->height >> !!i,
454 plane_start[i], c->frame_pred == PRED_LEFT);
457 if (c->frame_pred == PRED_MEDIAN) {
458 if (!c->interlaced) {
459 restore_median(c->pic.data[i], 1, c->pic.linesize[i],
460 avctx->width >> !!i, avctx->height >> !!i,
463 restore_median_il(c->pic.data[i], 1, c->pic.linesize[i],
465 avctx->height >> !!i,
471 case PIX_FMT_YUV422P:
472 for (i = 0; i < 3; i++) {
473 ret = decode_plane(c, i, c->pic.data[i], 1, c->pic.linesize[i],
474 avctx->width >> !!i, avctx->height,
475 plane_start[i], c->frame_pred == PRED_LEFT);
478 if (c->frame_pred == PRED_MEDIAN) {
479 if (!c->interlaced) {
480 restore_median(c->pic.data[i], 1, c->pic.linesize[i],
481 avctx->width >> !!i, avctx->height,
484 restore_median_il(c->pic.data[i], 1, c->pic.linesize[i],
485 avctx->width >> !!i, avctx->height,
493 c->pic.key_frame = 1;
494 c->pic.pict_type = AV_PICTURE_TYPE_I;
495 *data_size = sizeof(AVFrame);
496 *(AVFrame*)data = c->pic;
498 /* always report that the buffer was completely consumed */
502 static av_cold int decode_init(AVCodecContext *avctx)
504 UtvideoContext * const c = avctx->priv_data;
508 ff_dsputil_init(&c->dsp, avctx);
510 if (avctx->extradata_size < 16) {
511 av_log(avctx, AV_LOG_ERROR,
512 "Insufficient extradata size %d, should be at least 16\n",
513 avctx->extradata_size);
514 return AVERROR_INVALIDDATA;
517 av_log(avctx, AV_LOG_DEBUG, "Encoder version %d.%d.%d.%d\n",
518 avctx->extradata[3], avctx->extradata[2],
519 avctx->extradata[1], avctx->extradata[0]);
520 av_log(avctx, AV_LOG_DEBUG, "Original format %X\n",
521 AV_RB32(avctx->extradata + 4));
522 c->frame_info_size = AV_RL32(avctx->extradata + 8);
523 c->flags = AV_RL32(avctx->extradata + 12);
525 if (c->frame_info_size != 4)
526 av_log_ask_for_sample(avctx, "Frame info is not 4 bytes\n");
527 av_log(avctx, AV_LOG_DEBUG, "Encoding parameters %08X\n", c->flags);
528 c->slices = (c->flags >> 24) + 1;
529 c->compression = c->flags & 1;
530 c->interlaced = c->flags & 0x800;
532 c->slice_bits_size = 0;
534 switch (avctx->codec_tag) {
535 case MKTAG('U', 'L', 'R', 'G'):
537 avctx->pix_fmt = PIX_FMT_RGB24;
539 case MKTAG('U', 'L', 'R', 'A'):
541 avctx->pix_fmt = PIX_FMT_RGBA;
543 case MKTAG('U', 'L', 'Y', '0'):
545 avctx->pix_fmt = PIX_FMT_YUV420P;
547 case MKTAG('U', 'L', 'Y', '2'):
549 avctx->pix_fmt = PIX_FMT_YUV422P;
552 av_log(avctx, AV_LOG_ERROR, "Unknown Ut Video FOURCC provided (%08X)\n",
554 return AVERROR_INVALIDDATA;
560 static av_cold int decode_end(AVCodecContext *avctx)
562 UtvideoContext * const c = avctx->priv_data;
565 ff_thread_release_buffer(avctx, &c->pic);
567 av_freep(&c->slice_bits);
572 AVCodec ff_utvideo_decoder = {
574 .type = AVMEDIA_TYPE_VIDEO,
575 .id = CODEC_ID_UTVIDEO,
576 .priv_data_size = sizeof(UtvideoContext),
579 .decode = decode_frame,
580 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_FRAME_THREADS,
581 .long_name = NULL_IF_CONFIG_SMALL("Ut Video"),