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 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 src_size, 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) {
157 for (slice = 0; slice < c->slices; slice++) {
159 int slice_data_start, slice_data_end, slice_size;
162 send = (height * (slice + 1) / c->slices) & cmask;
163 dest = dst + sstart * stride;
165 // slice offset and size validation was done earlier
166 slice_data_start = slice ? AV_RL32(src + slice * 4 - 4) : 0;
167 slice_data_end = AV_RL32(src + slice * 4);
168 slice_size = slice_data_end - slice_data_start;
171 for (j = sstart; j < send; j++) {
172 for (i = 0; i < width * step; i += step)
179 memcpy(c->slice_bits, src + slice_data_start + c->slices * 4, slice_size);
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, "Slice decoding ran out of bits\n");
192 pix = get_vlc2(&gb, vlc.table, vlc.bits, 4);
194 av_log(c->avctx, AV_LOG_ERROR, "Decoding error\n");
205 if (get_bits_left(&gb) > 32)
206 av_log(c->avctx, AV_LOG_WARNING, "%d bits left after decoding slice\n",
215 return AVERROR_INVALIDDATA;
218 static const int rgb_order[4] = { 1, 2, 0, 3 };
220 static void restore_rgb_planes(uint8_t *src, int step, int stride, int width, int height)
225 for (j = 0; j < height; j++) {
226 for (i = 0; i < width * step; i += step) {
230 src[i] = r + g - 0x80;
231 src[i + 2] = b + g - 0x80;
237 static void restore_median(uint8_t *src, int step, int stride,
238 int width, int height, int slices, int rmode)
243 int slice_start, slice_height;
244 const int cmask = ~rmode;
246 for (slice = 0; slice < slices; slice++) {
247 slice_start = ((slice * height) / slices) & cmask;
248 slice_height = ((((slice + 1) * height) / slices) & cmask) - slice_start;
250 bsrc = src + slice_start * stride;
252 // first line - left neighbour prediction
255 for (i = step; i < width * step; i += step) {
260 if (slice_height == 1)
262 // second line - first element has top predition, the rest uses median
266 for (i = step; i < width * step; i += step) {
267 B = bsrc[i - stride];
268 bsrc[i] += mid_pred(A, B, (uint8_t)(A + B - C));
273 // the rest of lines use continuous median prediction
274 for (j = 2; j < slice_height; j++) {
275 for (i = 0; i < width * step; i += step) {
276 B = bsrc[i - stride];
277 bsrc[i] += mid_pred(A, B, (uint8_t)(A + B - C));
286 /* UtVideo interlaced mode treats every two lines as a single one,
287 * so restoring function should take care of possible padding between
288 * two parts of the same "line".
290 static void restore_median_il(uint8_t *src, int step, int stride,
291 int width, int height, int slices, int rmode)
296 int slice_start, slice_height;
297 const int cmask = ~(rmode ? 3 : 1);
298 const int stride2 = stride << 1;
300 for (slice = 0; slice < slices; slice++) {
301 slice_start = ((slice * height) / slices) & cmask;
302 slice_height = ((((slice + 1) * height) / slices) & cmask) - slice_start;
305 bsrc = src + slice_start * stride;
307 // first line - left neighbour prediction
310 for (i = step; i < width * step; i += step) {
314 for (i = 0; i < width * step; i += step) {
315 bsrc[stride + i] += A;
316 A = bsrc[stride + i];
319 if (slice_height == 1)
321 // second line - first element has top predition, the rest uses median
325 for (i = step; i < width * step; i += step) {
326 B = bsrc[i - stride2];
327 bsrc[i] += mid_pred(A, B, (uint8_t)(A + B - C));
331 for (i = 0; i < width * step; i += step) {
332 B = bsrc[i - stride];
333 bsrc[stride + i] += mid_pred(A, B, (uint8_t)(A + B - C));
335 A = bsrc[stride + i];
338 // the rest of lines use continuous median prediction
339 for (j = 2; j < slice_height; j++) {
340 for (i = 0; i < width * step; i += step) {
341 B = bsrc[i - stride2];
342 bsrc[i] += mid_pred(A, B, (uint8_t)(A + B - C));
346 for (i = 0; i < width * step; i += step) {
347 B = bsrc[i - stride];
348 bsrc[i + stride] += mid_pred(A, B, (uint8_t)(A + B - C));
350 A = bsrc[i + stride];
357 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt)
359 const uint8_t *buf = avpkt->data;
360 int buf_size = avpkt->size;
361 const uint8_t *buf_end = buf + buf_size;
362 UtvideoContext *c = avctx->priv_data;
365 const uint8_t *plane_start[5];
366 int plane_size, max_slice_size = 0, slice_start, slice_end, slice_size;
370 ff_thread_release_buffer(avctx, &c->pic);
372 c->pic.reference = 3;
373 c->pic.buffer_hints = FF_BUFFER_HINTS_VALID;
374 if ((ret = ff_thread_get_buffer(avctx, &c->pic)) < 0) {
375 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
379 /* parse plane structure to retrieve frame flags and validate slice offsets */
381 for (i = 0; i < c->planes; i++) {
382 plane_start[i] = ptr;
383 if (buf_end - ptr < 256 + 4 * c->slices) {
384 av_log(avctx, AV_LOG_ERROR, "Insufficient data for a plane\n");
385 return AVERROR_INVALIDDATA;
390 for (j = 0; j < c->slices; j++) {
391 slice_end = bytestream_get_le32(&ptr);
392 slice_size = slice_end - slice_start;
393 if (slice_size < 0) {
394 av_log(avctx, AV_LOG_ERROR, "Incorrect slice size\n");
395 return AVERROR_INVALIDDATA;
397 slice_start = slice_end;
398 max_slice_size = FFMAX(max_slice_size, slice_size);
400 plane_size = slice_end;
401 if (buf_end - ptr < plane_size) {
402 av_log(avctx, AV_LOG_ERROR, "Plane size is bigger than available data\n");
403 return AVERROR_INVALIDDATA;
407 plane_start[c->planes] = ptr;
408 if (buf_end - ptr < c->frame_info_size) {
409 av_log(avctx, AV_LOG_ERROR, "Not enough data for frame information\n");
410 return AVERROR_INVALIDDATA;
412 c->frame_info = AV_RL32(ptr);
413 av_log(avctx, AV_LOG_DEBUG, "frame information flags %X\n", c->frame_info);
415 c->frame_pred = (c->frame_info >> 8) & 3;
417 if (c->frame_pred == PRED_GRADIENT) {
418 av_log_ask_for_sample(avctx, "Frame uses gradient prediction\n");
419 return AVERROR_PATCHWELCOME;
422 av_fast_malloc(&c->slice_bits, &c->slice_bits_size,
423 max_slice_size + FF_INPUT_BUFFER_PADDING_SIZE);
425 if (!c->slice_bits) {
426 av_log(avctx, AV_LOG_ERROR, "Cannot allocate temporary buffer\n");
427 return AVERROR(ENOMEM);
430 switch (c->avctx->pix_fmt) {
433 for (i = 0; i < c->planes; i++) {
434 ret = decode_plane(c, i, c->pic.data[0] + rgb_order[i], c->planes,
435 c->pic.linesize[0], avctx->width, avctx->height,
436 plane_start[i], plane_start[i + 1] - plane_start[i],
437 c->frame_pred == PRED_LEFT);
440 if (c->frame_pred == PRED_MEDIAN)
441 restore_median(c->pic.data[0] + rgb_order[i], c->planes,
442 c->pic.linesize[0], avctx->width, avctx->height,
445 restore_rgb_planes(c->pic.data[0], c->planes, c->pic.linesize[0],
446 avctx->width, avctx->height);
448 case PIX_FMT_YUV420P:
449 for (i = 0; i < 3; i++) {
450 ret = decode_plane(c, i, c->pic.data[i], 1,
451 c->pic.linesize[i], avctx->width >> !!i, avctx->height >> !!i,
452 plane_start[i], plane_start[i + 1] - plane_start[i],
453 c->frame_pred == PRED_LEFT);
456 if (c->frame_pred == PRED_MEDIAN) {
457 if (!c->interlaced) {
458 restore_median(c->pic.data[i], 1, c->pic.linesize[i],
459 avctx->width >> !!i, avctx->height >> !!i,
462 restore_median_il(c->pic.data[i], 1, c->pic.linesize[i],
464 avctx->height >> !!i,
470 case PIX_FMT_YUV422P:
471 for (i = 0; i < 3; i++) {
472 ret = decode_plane(c, i, c->pic.data[i], 1,
473 c->pic.linesize[i], avctx->width >> !!i, avctx->height,
474 plane_start[i], plane_start[i + 1] - plane_start[i],
475 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 *data_size = sizeof(AVFrame);
494 *(AVFrame*)data = c->pic;
496 /* always report that the buffer was completely consumed */
500 static av_cold int decode_init(AVCodecContext *avctx)
502 UtvideoContext * const c = avctx->priv_data;
506 dsputil_init(&c->dsp, avctx);
508 if (avctx->extradata_size < 16) {
509 av_log(avctx, AV_LOG_ERROR, "Insufficient extradata size %d, should be at least 16\n",
510 avctx->extradata_size);
511 return AVERROR_INVALIDDATA;
514 av_log(avctx, AV_LOG_DEBUG, "Encoder version %d.%d.%d.%d\n",
515 avctx->extradata[3], avctx->extradata[2],
516 avctx->extradata[1], avctx->extradata[0]);
517 av_log(avctx, AV_LOG_DEBUG, "Original format %X\n", AV_RB32(avctx->extradata + 4));
518 c->frame_info_size = AV_RL32(avctx->extradata + 8);
519 c->flags = AV_RL32(avctx->extradata + 12);
521 if (c->frame_info_size != 4)
522 av_log_ask_for_sample(avctx, "Frame info is not 4 bytes\n");
523 av_log(avctx, AV_LOG_DEBUG, "Encoding parameters %08X\n", c->flags);
524 c->slices = (c->flags >> 24) + 1;
525 c->compression = c->flags & 1;
526 c->interlaced = c->flags & 0x800;
528 c->slice_bits_size = 0;
530 switch (avctx->codec_tag) {
531 case MKTAG('U', 'L', 'R', 'G'):
533 avctx->pix_fmt = PIX_FMT_RGB24;
535 case MKTAG('U', 'L', 'R', 'A'):
537 avctx->pix_fmt = PIX_FMT_RGBA;
539 case MKTAG('U', 'L', 'Y', '0'):
541 avctx->pix_fmt = PIX_FMT_YUV420P;
543 case MKTAG('U', 'L', 'Y', '2'):
545 avctx->pix_fmt = PIX_FMT_YUV422P;
548 av_log(avctx, AV_LOG_ERROR, "Unknown Ut Video FOURCC provided (%08X)\n",
550 return AVERROR_INVALIDDATA;
556 static av_cold int decode_end(AVCodecContext *avctx)
558 UtvideoContext * const c = avctx->priv_data;
561 ff_thread_release_buffer(avctx, &c->pic);
563 av_freep(&c->slice_bits);
568 AVCodec ff_utvideo_decoder = {
570 .type = AVMEDIA_TYPE_VIDEO,
571 .id = CODEC_ID_UTVIDEO,
572 .priv_data_size = sizeof(UtvideoContext),
575 .decode = decode_frame,
576 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_FRAME_THREADS,
577 .long_name = NULL_IF_CONFIG_SMALL("Ut Video"),