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"
42 typedef struct UtvideoContext {
43 AVCodecContext *avctx;
47 uint32_t frame_info_size, flags, frame_info;
58 typedef struct HuffEntry {
63 static int huff_cmp(const void *a, const void *b)
65 const HuffEntry *aa = a, *bb = b;
66 return (aa->len - bb->len)*256 + aa->sym - bb->sym;
69 static int build_huff(const uint8_t *src, VLC *vlc, int *fsym)
80 for (i = 0; i < 256; i++) {
84 qsort(he, 256, sizeof(*he), huff_cmp);
94 while (he[last].len == 255 && last)
98 for (i = last; i >= 0; i--) {
99 codes[i] = code >> (32 - he[i].len);
102 code += 0x80000000u >> (he[i].len - 1);
105 return init_vlc_sparse(vlc, FFMIN(he[last].len, 9), last + 1,
106 bits, sizeof(*bits), sizeof(*bits),
107 codes, sizeof(*codes), sizeof(*codes),
108 syms, sizeof(*syms), sizeof(*syms), 0);
111 static int decode_plane(UtvideoContext *c, int plane_no,
112 uint8_t *dst, int step, int stride,
113 int width, int height,
114 const uint8_t *src, int src_size, int use_pred)
116 int i, j, slice, pix;
121 const int cmask = ~(!plane_no && c->avctx->pix_fmt == PIX_FMT_YUV420P);
123 if (build_huff(src, &vlc, &fsym)) {
124 av_log(c->avctx, AV_LOG_ERROR, "Cannot build Huffman codes\n");
125 return AVERROR_INVALIDDATA;
127 if (fsym >= 0) { // build_huff reported a symbol to fill slices with
129 for (slice = 0; slice < c->slices; slice++) {
133 send = (height * (slice + 1) / c->slices) & cmask;
134 dest = dst + sstart * stride;
137 for (j = sstart; j < send; j++) {
138 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, slice_size);
179 memset(c->slice_bits + slice_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
180 c->dsp.bswap_buf((uint32_t*)c->slice_bits, (uint32_t*)c->slice_bits,
181 (slice_data_end - slice_data_start + 3) >> 2);
182 init_get_bits(&gb, c->slice_bits, slice_size * 8);
185 for (j = sstart; j < send; j++) {
186 for (i = 0; i < width * step; i += step) {
187 if (get_bits_left(&gb) <= 0) {
188 av_log(c->avctx, AV_LOG_ERROR, "Slice decoding ran out of bits\n");
191 pix = get_vlc2(&gb, vlc.table, vlc.bits, 4);
193 av_log(c->avctx, AV_LOG_ERROR, "Decoding error\n");
204 if (get_bits_left(&gb) > 32)
205 av_log(c->avctx, AV_LOG_WARNING, "%d bits left after decoding slice\n",
214 return AVERROR_INVALIDDATA;
217 static const int rgb_order[4] = { 1, 2, 0, 3 };
219 static void restore_rgb_planes(uint8_t *src, int step, int stride, int width, int height)
224 for (j = 0; j < height; j++) {
225 for (i = 0; i < width * step; i += step) {
229 src[i] = r + g - 0x80;
230 src[i + 2] = b + g - 0x80;
236 static void restore_median(uint8_t *src, int step, int stride,
237 int width, int height, int slices, int rmode)
242 int slice_start, slice_height;
243 const int cmask = ~rmode;
245 for (slice = 0; slice < slices; slice++) {
246 slice_start = ((slice * height) / slices) & cmask;
247 slice_height = ((((slice + 1) * height) / slices) & cmask) - slice_start;
249 bsrc = src + slice_start * stride;
251 // first line - left neighbour prediction
254 for (i = step; i < width * step; i += step) {
259 if (slice_height == 1)
261 // second line - first element has top predition, the rest uses median
265 for (i = step; i < width * step; i += step) {
266 B = bsrc[i - stride];
267 bsrc[i] += mid_pred(A, B, (uint8_t)(A + B - C));
272 // the rest of lines use continuous median prediction
273 for (j = 2; j < slice_height; j++) {
274 for (i = 0; i < width * step; i += step) {
275 B = bsrc[i - stride];
276 bsrc[i] += mid_pred(A, B, (uint8_t)(A + B - C));
285 /* UtVideo interlaced mode treats every two lines as a single one,
286 * so restoring function should take care of possible padding between
287 * two parts of the same "line".
289 static void restore_median_il(uint8_t *src, int step, int stride,
290 int width, int height, int slices, int rmode)
295 int slice_start, slice_height;
296 const int cmask = ~(rmode ? 3 : 1);
297 const int stride2 = stride << 1;
299 for (slice = 0; slice < slices; slice++) {
300 slice_start = ((slice * height) / slices) & cmask;
301 slice_height = ((((slice + 1) * height) / slices) & cmask) - slice_start;
304 bsrc = src + slice_start * stride;
306 // first line - left neighbour prediction
309 for (i = step; i < width * step; i += step) {
313 for (i = 0; i < width * step; i += step) {
314 bsrc[stride + i] += A;
315 A = bsrc[stride + i];
318 if (slice_height == 1)
320 // second line - first element has top predition, the rest uses median
324 for (i = step; i < width * step; i += step) {
325 B = bsrc[i - stride2];
326 bsrc[i] += mid_pred(A, B, (uint8_t)(A + B - C));
330 for (i = 0; i < width * step; i += step) {
331 B = bsrc[i - stride];
332 bsrc[stride + i] += mid_pred(A, B, (uint8_t)(A + B - C));
334 A = bsrc[stride + i];
337 // the rest of lines use continuous median prediction
338 for (j = 2; j < slice_height; j++) {
339 for (i = 0; i < width * step; i += step) {
340 B = bsrc[i - stride2];
341 bsrc[i] += mid_pred(A, B, (uint8_t)(A + B - C));
345 for (i = 0; i < width * step; i += step) {
346 B = bsrc[i - stride];
347 bsrc[i + stride] += mid_pred(A, B, (uint8_t)(A + B - C));
349 A = bsrc[i + stride];
356 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt)
358 const uint8_t *buf = avpkt->data;
359 int buf_size = avpkt->size;
360 const uint8_t *buf_end = buf + buf_size;
361 UtvideoContext *c = avctx->priv_data;
364 const uint8_t *plane_start[5];
365 int plane_size, max_slice_size = 0, slice_start, slice_end, slice_size;
369 avctx->release_buffer(avctx, &c->pic);
371 c->pic.reference = 3;
372 c->pic.buffer_hints = FF_BUFFER_HINTS_VALID;
373 if ((ret = avctx->get_buffer(avctx, &c->pic)) < 0) {
374 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
378 /* parse plane structure to retrieve frame flags and validate slice offsets */
380 for (i = 0; i < c->planes; i++) {
381 plane_start[i] = ptr;
382 if (buf_end - ptr < 256 + 4 * c->slices) {
383 av_log(avctx, AV_LOG_ERROR, "Insufficient data for a plane\n");
384 return AVERROR_INVALIDDATA;
389 for (j = 0; j < c->slices; j++) {
390 slice_end = bytestream_get_le32(&ptr);
391 slice_size = slice_end - slice_start;
392 if (slice_size < 0) {
393 av_log(avctx, AV_LOG_ERROR, "Incorrect slice size\n");
394 return AVERROR_INVALIDDATA;
396 slice_start = slice_end;
397 max_slice_size = FFMAX(max_slice_size, slice_size);
399 plane_size = slice_end;
400 if (buf_end - ptr < plane_size) {
401 av_log(avctx, AV_LOG_ERROR, "Plane size is bigger than available data\n");
402 return AVERROR_INVALIDDATA;
406 plane_start[c->planes] = ptr;
407 if (buf_end - ptr < c->frame_info_size) {
408 av_log(avctx, AV_LOG_ERROR, "Not enough data for frame information\n");
409 return AVERROR_INVALIDDATA;
411 c->frame_info = AV_RL32(ptr);
412 av_log(avctx, AV_LOG_DEBUG, "frame information flags %X\n", c->frame_info);
414 c->frame_pred = (c->frame_info >> 8) & 3;
416 if (c->frame_pred == PRED_GRADIENT) {
417 av_log_ask_for_sample(avctx, "Frame uses gradient prediction\n");
418 return AVERROR_PATCHWELCOME;
421 av_fast_malloc(&c->slice_bits, &c->slice_bits_size,
422 max_slice_size + FF_INPUT_BUFFER_PADDING_SIZE);
424 if (!c->slice_bits) {
425 av_log(avctx, AV_LOG_ERROR, "Cannot allocate temporary buffer\n");
426 return AVERROR(ENOMEM);
429 switch (c->avctx->pix_fmt) {
432 for (i = 0; i < c->planes; i++) {
433 ret = decode_plane(c, i, c->pic.data[0] + rgb_order[i], c->planes,
434 c->pic.linesize[0], avctx->width, avctx->height,
435 plane_start[i], plane_start[i + 1] - plane_start[i],
436 c->frame_pred == PRED_LEFT);
439 if (c->frame_pred == PRED_MEDIAN)
440 restore_median(c->pic.data[0] + rgb_order[i], c->planes,
441 c->pic.linesize[0], avctx->width, avctx->height,
444 restore_rgb_planes(c->pic.data[0], c->planes, c->pic.linesize[0],
445 avctx->width, avctx->height);
447 case PIX_FMT_YUV420P:
448 for (i = 0; i < 3; i++) {
449 ret = decode_plane(c, i, c->pic.data[i], 1,
450 c->pic.linesize[i], avctx->width >> !!i, avctx->height >> !!i,
451 plane_start[i], plane_start[i + 1] - plane_start[i],
452 c->frame_pred == PRED_LEFT);
455 if (c->frame_pred == PRED_MEDIAN) {
456 if (!c->interlaced) {
457 restore_median(c->pic.data[i], 1, c->pic.linesize[i],
458 avctx->width >> !!i, avctx->height >> !!i,
461 restore_median_il(c->pic.data[i], 1, c->pic.linesize[i],
463 avctx->height >> !!i,
469 case PIX_FMT_YUV422P:
470 for (i = 0; i < 3; i++) {
471 ret = decode_plane(c, i, c->pic.data[i], 1,
472 c->pic.linesize[i], avctx->width >> !!i, avctx->height,
473 plane_start[i], plane_start[i + 1] - plane_start[i],
474 c->frame_pred == PRED_LEFT);
477 if (c->frame_pred == PRED_MEDIAN) {
478 if (!c->interlaced) {
479 restore_median(c->pic.data[i], 1, c->pic.linesize[i],
480 avctx->width >> !!i, avctx->height,
483 restore_median_il(c->pic.data[i], 1, c->pic.linesize[i],
484 avctx->width >> !!i, avctx->height,
492 *data_size = sizeof(AVFrame);
493 *(AVFrame*)data = c->pic;
495 /* always report that the buffer was completely consumed */
499 static av_cold int decode_init(AVCodecContext *avctx)
501 UtvideoContext * const c = avctx->priv_data;
505 dsputil_init(&c->dsp, avctx);
507 if (avctx->extradata_size < 16) {
508 av_log(avctx, AV_LOG_ERROR, "Insufficient extradata size %d, should be at least 16\n",
509 avctx->extradata_size);
510 return AVERROR_INVALIDDATA;
513 av_log(avctx, AV_LOG_DEBUG, "Encoder version %d.%d.%d.%d\n",
514 avctx->extradata[3], avctx->extradata[2],
515 avctx->extradata[1], avctx->extradata[0]);
516 av_log(avctx, AV_LOG_DEBUG, "Original format %X\n", AV_RB32(avctx->extradata + 4));
517 c->frame_info_size = AV_RL32(avctx->extradata + 8);
518 c->flags = AV_RL32(avctx->extradata + 12);
520 if (c->frame_info_size != 4)
521 av_log_ask_for_sample(avctx, "Frame info is not 4 bytes\n");
522 av_log(avctx, AV_LOG_DEBUG, "Encoding parameters %08X\n", c->flags);
523 c->slices = (c->flags >> 24) + 1;
524 c->compression = c->flags & 1;
525 c->interlaced = c->flags & 0x800;
527 c->slice_bits_size = 0;
529 switch (avctx->codec_tag) {
530 case MKTAG('U', 'L', 'R', 'G'):
532 avctx->pix_fmt = PIX_FMT_RGB24;
534 case MKTAG('U', 'L', 'R', 'A'):
536 avctx->pix_fmt = PIX_FMT_RGBA;
538 case MKTAG('U', 'L', 'Y', '0'):
540 avctx->pix_fmt = PIX_FMT_YUV420P;
542 case MKTAG('U', 'L', 'Y', '2'):
544 avctx->pix_fmt = PIX_FMT_YUV422P;
547 av_log(avctx, AV_LOG_ERROR, "Unknown Ut Video FOURCC provided (%08X)\n",
549 return AVERROR_INVALIDDATA;
555 static av_cold int decode_end(AVCodecContext *avctx)
557 UtvideoContext * const c = avctx->priv_data;
560 avctx->release_buffer(avctx, &c->pic);
562 av_freep(&c->slice_bits);
567 AVCodec ff_utvideo_decoder = {
569 .type = AVMEDIA_TYPE_VIDEO,
570 .id = CODEC_ID_UTVIDEO,
571 .priv_data_size = sizeof(UtvideoContext),
574 .decode = decode_frame,
575 .capabilities = CODEC_CAP_DR1,
576 .long_name = NULL_IF_CONFIG_SMALL("Ut Video"),