3 * Copyright (c) 2011 Konstantin Shishkov
5 * This file is part of FFmpeg.
7 * FFmpeg 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 * FFmpeg 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 FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
30 #include "libavutil/intreadwrite.h"
33 #include "bytestream.h"
38 static int build_huff10(const uint8_t *src, VLC *vlc, int *fsym)
49 for (i = 0; i < 1024; i++) {
53 qsort(he, 1024, sizeof(*he), ff_ut10_huff_cmp_len);
61 while (he[last].len == 255 && last)
64 if (he[last].len > 32) {
69 for (i = last; i >= 0; i--) {
70 codes[i] = code >> (32 - he[i].len);
73 code += 0x80000000u >> (he[i].len - 1);
76 return ff_init_vlc_sparse(vlc, FFMIN(he[last].len, 11), last + 1,
77 bits, sizeof(*bits), sizeof(*bits),
78 codes, sizeof(*codes), sizeof(*codes),
79 syms, sizeof(*syms), sizeof(*syms), 0);
82 static int build_huff(const uint8_t *src, VLC *vlc, int *fsym)
93 for (i = 0; i < 256; i++) {
97 qsort(he, 256, sizeof(*he), ff_ut_huff_cmp_len);
105 while (he[last].len == 255 && last)
108 if (he[last].len > 32)
112 for (i = last; i >= 0; i--) {
113 codes[i] = code >> (32 - he[i].len);
116 code += 0x80000000u >> (he[i].len - 1);
119 return ff_init_vlc_sparse(vlc, FFMIN(he[last].len, 11), last + 1,
120 bits, sizeof(*bits), sizeof(*bits),
121 codes, sizeof(*codes), sizeof(*codes),
122 syms, sizeof(*syms), sizeof(*syms), 0);
125 static int decode_plane10(UtvideoContext *c, int plane_no,
126 uint16_t *dst, int step, int stride,
127 int width, int height,
128 const uint8_t *src, const uint8_t *huff,
131 int i, j, slice, pix, ret;
137 if ((ret = build_huff10(huff, &vlc, &fsym)) < 0) {
138 av_log(c->avctx, AV_LOG_ERROR, "Cannot build Huffman codes\n");
141 if (fsym >= 0) { // build_huff reported a symbol to fill slices with
143 for (slice = 0; slice < c->slices; slice++) {
147 send = (height * (slice + 1) / c->slices);
148 dest = dst + sstart * stride;
151 for (j = sstart; j < send; j++) {
152 for (i = 0; i < width * step; i += step) {
168 for (slice = 0; slice < c->slices; slice++) {
170 int slice_data_start, slice_data_end, slice_size;
173 send = (height * (slice + 1) / c->slices);
174 dest = dst + sstart * stride;
176 // slice offset and size validation was done earlier
177 slice_data_start = slice ? AV_RL32(src + slice * 4 - 4) : 0;
178 slice_data_end = AV_RL32(src + slice * 4);
179 slice_size = slice_data_end - slice_data_start;
182 av_log(c->avctx, AV_LOG_ERROR, "Plane has more than one symbol "
183 "yet a slice has a length of zero.\n");
187 memcpy(c->slice_bits, src + slice_data_start + c->slices * 4,
189 memset(c->slice_bits + slice_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
190 c->bdsp.bswap_buf((uint32_t *) c->slice_bits,
191 (uint32_t *) c->slice_bits,
192 (slice_data_end - slice_data_start + 3) >> 2);
193 init_get_bits(&gb, c->slice_bits, slice_size * 8);
196 for (j = sstart; j < send; j++) {
197 for (i = 0; i < width * step; i += step) {
198 if (get_bits_left(&gb) <= 0) {
199 av_log(c->avctx, AV_LOG_ERROR,
200 "Slice decoding ran out of bits\n");
203 pix = get_vlc2(&gb, vlc.table, vlc.bits, 3);
205 av_log(c->avctx, AV_LOG_ERROR, "Decoding error\n");
217 if (get_bits_left(&gb) > 32)
218 av_log(c->avctx, AV_LOG_WARNING,
219 "%d bits left after decoding slice\n", get_bits_left(&gb));
227 return AVERROR_INVALIDDATA;
230 static int decode_plane(UtvideoContext *c, int plane_no,
231 uint8_t *dst, int step, int stride,
232 int width, int height,
233 const uint8_t *src, int use_pred)
235 int i, j, slice, pix;
240 const int cmask = ~(!plane_no && c->avctx->pix_fmt == AV_PIX_FMT_YUV420P);
242 if (build_huff(src, &vlc, &fsym)) {
243 av_log(c->avctx, AV_LOG_ERROR, "Cannot build Huffman codes\n");
244 return AVERROR_INVALIDDATA;
246 if (fsym >= 0) { // build_huff reported a symbol to fill slices with
248 for (slice = 0; slice < c->slices; slice++) {
252 send = (height * (slice + 1) / c->slices) & cmask;
253 dest = dst + sstart * stride;
256 for (j = sstart; j < send; j++) {
257 for (i = 0; i < width * step; i += step) {
274 for (slice = 0; slice < c->slices; slice++) {
276 int slice_data_start, slice_data_end, slice_size;
279 send = (height * (slice + 1) / c->slices) & cmask;
280 dest = dst + sstart * stride;
282 // slice offset and size validation was done earlier
283 slice_data_start = slice ? AV_RL32(src + slice * 4 - 4) : 0;
284 slice_data_end = AV_RL32(src + slice * 4);
285 slice_size = slice_data_end - slice_data_start;
288 av_log(c->avctx, AV_LOG_ERROR, "Plane has more than one symbol "
289 "yet a slice has a length of zero.\n");
293 memcpy(c->slice_bits, src + slice_data_start + c->slices * 4,
295 memset(c->slice_bits + slice_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
296 c->bdsp.bswap_buf((uint32_t *) c->slice_bits,
297 (uint32_t *) c->slice_bits,
298 (slice_data_end - slice_data_start + 3) >> 2);
299 init_get_bits(&gb, c->slice_bits, slice_size * 8);
302 for (j = sstart; j < send; j++) {
303 for (i = 0; i < width * step; i += step) {
304 if (get_bits_left(&gb) <= 0) {
305 av_log(c->avctx, AV_LOG_ERROR,
306 "Slice decoding ran out of bits\n");
309 pix = get_vlc2(&gb, vlc.table, vlc.bits, 3);
311 av_log(c->avctx, AV_LOG_ERROR, "Decoding error\n");
322 if (get_bits_left(&gb) > 32)
323 av_log(c->avctx, AV_LOG_WARNING,
324 "%d bits left after decoding slice\n", get_bits_left(&gb));
332 return AVERROR_INVALIDDATA;
335 static void restore_rgb_planes(uint8_t *src, int step, int stride, int width,
341 for (j = 0; j < height; j++) {
342 for (i = 0; i < width * step; i += step) {
346 src[i] = r + g - 0x80;
347 src[i + 2] = b + g - 0x80;
353 static void restore_rgb_planes10(AVFrame *frame, int width, int height)
355 uint16_t *src_r = (uint16_t *)frame->data[2];
356 uint16_t *src_g = (uint16_t *)frame->data[0];
357 uint16_t *src_b = (uint16_t *)frame->data[1];
361 for (j = 0; j < height; j++) {
362 for (i = 0; i < width; i++) {
366 src_r[i] = (r + g - 0x200) & 0x3FF;
367 src_b[i] = (b + g - 0x200) & 0x3FF;
369 src_r += frame->linesize[2] / 2;
370 src_g += frame->linesize[0] / 2;
371 src_b += frame->linesize[1] / 2;
375 static void restore_median(uint8_t *src, int step, int stride,
376 int width, int height, int slices, int rmode)
381 int slice_start, slice_height;
382 const int cmask = ~rmode;
384 for (slice = 0; slice < slices; slice++) {
385 slice_start = ((slice * height) / slices) & cmask;
386 slice_height = ((((slice + 1) * height) / slices) & cmask) -
391 bsrc = src + slice_start * stride;
393 // first line - left neighbour prediction
396 for (i = step; i < width * step; i += step) {
401 if (slice_height <= 1)
403 // second line - first element has top prediction, the rest uses median
407 for (i = step; i < width * step; i += step) {
408 B = bsrc[i - stride];
409 bsrc[i] += mid_pred(A, B, (uint8_t)(A + B - C));
414 // the rest of lines use continuous median prediction
415 for (j = 2; j < slice_height; j++) {
416 for (i = 0; i < width * step; i += step) {
417 B = bsrc[i - stride];
418 bsrc[i] += mid_pred(A, B, (uint8_t)(A + B - C));
427 /* UtVideo interlaced mode treats every two lines as a single one,
428 * so restoring function should take care of possible padding between
429 * two parts of the same "line".
431 static void restore_median_il(uint8_t *src, int step, int stride,
432 int width, int height, int slices, int rmode)
437 int slice_start, slice_height;
438 const int cmask = ~(rmode ? 3 : 1);
439 const int stride2 = stride << 1;
441 for (slice = 0; slice < slices; slice++) {
442 slice_start = ((slice * height) / slices) & cmask;
443 slice_height = ((((slice + 1) * height) / slices) & cmask) -
449 bsrc = src + slice_start * stride;
451 // first line - left neighbour prediction
454 for (i = step; i < width * step; i += step) {
458 for (i = 0; i < width * step; i += step) {
459 bsrc[stride + i] += A;
460 A = bsrc[stride + i];
463 if (slice_height <= 1)
465 // second line - first element has top prediction, the rest uses median
469 for (i = step; i < width * step; i += step) {
470 B = bsrc[i - stride2];
471 bsrc[i] += mid_pred(A, B, (uint8_t)(A + B - C));
475 for (i = 0; i < width * step; i += step) {
476 B = bsrc[i - stride];
477 bsrc[stride + i] += mid_pred(A, B, (uint8_t)(A + B - C));
479 A = bsrc[stride + i];
482 // the rest of lines use continuous median prediction
483 for (j = 2; j < slice_height; j++) {
484 for (i = 0; i < width * step; i += step) {
485 B = bsrc[i - stride2];
486 bsrc[i] += mid_pred(A, B, (uint8_t)(A + B - C));
490 for (i = 0; i < width * step; i += step) {
491 B = bsrc[i - stride];
492 bsrc[i + stride] += mid_pred(A, B, (uint8_t)(A + B - C));
494 A = bsrc[i + stride];
501 static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
504 const uint8_t *buf = avpkt->data;
505 int buf_size = avpkt->size;
506 UtvideoContext *c = avctx->priv_data;
508 const uint8_t *plane_start[5];
509 int plane_size, max_slice_size = 0, slice_start, slice_end, slice_size;
512 ThreadFrame frame = { .f = data };
514 if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
517 /* parse plane structure to get frame flags and validate slice offsets */
518 bytestream2_init(&gb, buf, buf_size);
520 if (bytestream2_get_bytes_left(&gb) < c->frame_info_size) {
521 av_log(avctx, AV_LOG_ERROR, "Not enough data for frame information\n");
522 return AVERROR_INVALIDDATA;
524 c->frame_info = bytestream2_get_le32u(&gb);
525 c->slices = ((c->frame_info >> 16) & 0xff) + 1;
526 for (i = 0; i < c->planes; i++) {
527 plane_start[i] = gb.buffer;
528 if (bytestream2_get_bytes_left(&gb) < 1024 + 4 * c->slices) {
529 av_log(avctx, AV_LOG_ERROR, "Insufficient data for a plane\n");
530 return AVERROR_INVALIDDATA;
534 for (j = 0; j < c->slices; j++) {
535 slice_end = bytestream2_get_le32u(&gb);
536 if (slice_end < 0 || slice_end < slice_start ||
537 bytestream2_get_bytes_left(&gb) < slice_end) {
538 av_log(avctx, AV_LOG_ERROR, "Incorrect slice size\n");
539 return AVERROR_INVALIDDATA;
541 slice_size = slice_end - slice_start;
542 slice_start = slice_end;
543 max_slice_size = FFMAX(max_slice_size, slice_size);
545 plane_size = slice_end;
546 bytestream2_skipu(&gb, plane_size);
547 bytestream2_skipu(&gb, 1024);
549 plane_start[c->planes] = gb.buffer;
551 for (i = 0; i < c->planes; i++) {
552 plane_start[i] = gb.buffer;
553 if (bytestream2_get_bytes_left(&gb) < 256 + 4 * c->slices) {
554 av_log(avctx, AV_LOG_ERROR, "Insufficient data for a plane\n");
555 return AVERROR_INVALIDDATA;
557 bytestream2_skipu(&gb, 256);
560 for (j = 0; j < c->slices; j++) {
561 slice_end = bytestream2_get_le32u(&gb);
562 if (slice_end < 0 || slice_end < slice_start ||
563 bytestream2_get_bytes_left(&gb) < slice_end) {
564 av_log(avctx, AV_LOG_ERROR, "Incorrect slice size\n");
565 return AVERROR_INVALIDDATA;
567 slice_size = slice_end - slice_start;
568 slice_start = slice_end;
569 max_slice_size = FFMAX(max_slice_size, slice_size);
571 plane_size = slice_end;
572 bytestream2_skipu(&gb, plane_size);
574 plane_start[c->planes] = gb.buffer;
575 if (bytestream2_get_bytes_left(&gb) < c->frame_info_size) {
576 av_log(avctx, AV_LOG_ERROR, "Not enough data for frame information\n");
577 return AVERROR_INVALIDDATA;
579 c->frame_info = bytestream2_get_le32u(&gb);
581 av_log(avctx, AV_LOG_DEBUG, "frame information flags %"PRIX32"\n",
584 c->frame_pred = (c->frame_info >> 8) & 3;
586 if (c->frame_pred == PRED_GRADIENT) {
587 avpriv_request_sample(avctx, "Frame with gradient prediction");
588 return AVERROR_PATCHWELCOME;
591 av_fast_malloc(&c->slice_bits, &c->slice_bits_size,
592 max_slice_size + AV_INPUT_BUFFER_PADDING_SIZE);
594 if (!c->slice_bits) {
595 av_log(avctx, AV_LOG_ERROR, "Cannot allocate temporary buffer\n");
596 return AVERROR(ENOMEM);
599 switch (c->avctx->pix_fmt) {
600 case AV_PIX_FMT_RGB24:
601 case AV_PIX_FMT_RGBA:
602 for (i = 0; i < c->planes; i++) {
603 ret = decode_plane(c, i, frame.f->data[0] + ff_ut_rgb_order[i],
604 c->planes, frame.f->linesize[0], avctx->width,
605 avctx->height, plane_start[i],
606 c->frame_pred == PRED_LEFT);
609 if (c->frame_pred == PRED_MEDIAN) {
610 if (!c->interlaced) {
611 restore_median(frame.f->data[0] + ff_ut_rgb_order[i],
612 c->planes, frame.f->linesize[0], avctx->width,
613 avctx->height, c->slices, 0);
615 restore_median_il(frame.f->data[0] + ff_ut_rgb_order[i],
616 c->planes, frame.f->linesize[0],
617 avctx->width, avctx->height, c->slices,
622 restore_rgb_planes(frame.f->data[0], c->planes, frame.f->linesize[0],
623 avctx->width, avctx->height);
625 case AV_PIX_FMT_GBRAP10:
626 case AV_PIX_FMT_GBRP10:
627 for (i = 0; i < c->planes; i++) {
628 ret = decode_plane10(c, i, (uint16_t *)frame.f->data[i], 1,
629 frame.f->linesize[i] / 2, avctx->width,
630 avctx->height, plane_start[i],
631 plane_start[i + 1] - 1024,
632 c->frame_pred == PRED_LEFT);
636 restore_rgb_planes10(frame.f, avctx->width, avctx->height);
638 case AV_PIX_FMT_YUV420P:
639 for (i = 0; i < 3; i++) {
640 ret = decode_plane(c, i, frame.f->data[i], 1, frame.f->linesize[i],
641 avctx->width >> !!i, avctx->height >> !!i,
642 plane_start[i], c->frame_pred == PRED_LEFT);
645 if (c->frame_pred == PRED_MEDIAN) {
646 if (!c->interlaced) {
647 restore_median(frame.f->data[i], 1, frame.f->linesize[i],
648 avctx->width >> !!i, avctx->height >> !!i,
651 restore_median_il(frame.f->data[i], 1, frame.f->linesize[i],
653 avctx->height >> !!i,
659 case AV_PIX_FMT_YUV422P:
660 for (i = 0; i < 3; i++) {
661 ret = decode_plane(c, i, frame.f->data[i], 1, frame.f->linesize[i],
662 avctx->width >> !!i, avctx->height,
663 plane_start[i], c->frame_pred == PRED_LEFT);
666 if (c->frame_pred == PRED_MEDIAN) {
667 if (!c->interlaced) {
668 restore_median(frame.f->data[i], 1, frame.f->linesize[i],
669 avctx->width >> !!i, avctx->height,
672 restore_median_il(frame.f->data[i], 1, frame.f->linesize[i],
673 avctx->width >> !!i, avctx->height,
679 case AV_PIX_FMT_YUV422P10:
680 for (i = 0; i < 3; i++) {
681 ret = decode_plane10(c, i, (uint16_t *)frame.f->data[i], 1, frame.f->linesize[i] / 2,
682 avctx->width >> !!i, avctx->height,
683 plane_start[i], plane_start[i + 1] - 1024, c->frame_pred == PRED_LEFT);
690 frame.f->key_frame = 1;
691 frame.f->pict_type = AV_PICTURE_TYPE_I;
692 frame.f->interlaced_frame = !!c->interlaced;
696 /* always report that the buffer was completely consumed */
700 static av_cold int decode_init(AVCodecContext *avctx)
702 UtvideoContext * const c = avctx->priv_data;
706 ff_bswapdsp_init(&c->bdsp);
708 if (avctx->extradata_size >= 16) {
709 av_log(avctx, AV_LOG_DEBUG, "Encoder version %d.%d.%d.%d\n",
710 avctx->extradata[3], avctx->extradata[2],
711 avctx->extradata[1], avctx->extradata[0]);
712 av_log(avctx, AV_LOG_DEBUG, "Original format %"PRIX32"\n",
713 AV_RB32(avctx->extradata + 4));
714 c->frame_info_size = AV_RL32(avctx->extradata + 8);
715 c->flags = AV_RL32(avctx->extradata + 12);
717 if (c->frame_info_size != 4)
718 avpriv_request_sample(avctx, "Frame info not 4 bytes");
719 av_log(avctx, AV_LOG_DEBUG, "Encoding parameters %08"PRIX32"\n", c->flags);
720 c->slices = (c->flags >> 24) + 1;
721 c->compression = c->flags & 1;
722 c->interlaced = c->flags & 0x800;
723 } else if (avctx->extradata_size == 8) {
724 av_log(avctx, AV_LOG_DEBUG, "Encoder version %d.%d.%d.%d\n",
725 avctx->extradata[3], avctx->extradata[2],
726 avctx->extradata[1], avctx->extradata[0]);
727 av_log(avctx, AV_LOG_DEBUG, "Original format %"PRIX32"\n",
728 AV_RB32(avctx->extradata + 4));
731 c->frame_info_size = 4;
733 av_log(avctx, AV_LOG_ERROR,
734 "Insufficient extradata size %d, should be at least 16\n",
735 avctx->extradata_size);
736 return AVERROR_INVALIDDATA;
739 c->slice_bits_size = 0;
741 switch (avctx->codec_tag) {
742 case MKTAG('U', 'L', 'R', 'G'):
744 avctx->pix_fmt = AV_PIX_FMT_RGB24;
746 case MKTAG('U', 'L', 'R', 'A'):
748 avctx->pix_fmt = AV_PIX_FMT_RGBA;
750 case MKTAG('U', 'L', 'Y', '0'):
752 avctx->pix_fmt = AV_PIX_FMT_YUV420P;
753 avctx->colorspace = AVCOL_SPC_BT470BG;
755 case MKTAG('U', 'L', 'Y', '2'):
757 avctx->pix_fmt = AV_PIX_FMT_YUV422P;
758 avctx->colorspace = AVCOL_SPC_BT470BG;
760 case MKTAG('U', 'Q', 'Y', '2'):
762 avctx->pix_fmt = AV_PIX_FMT_YUV422P10;
764 case MKTAG('U', 'Q', 'R', 'G'):
766 avctx->pix_fmt = AV_PIX_FMT_GBRP10;
768 case MKTAG('U', 'Q', 'R', 'A'):
770 avctx->pix_fmt = AV_PIX_FMT_GBRAP10;
772 case MKTAG('U', 'L', 'H', '0'):
774 avctx->pix_fmt = AV_PIX_FMT_YUV420P;
775 avctx->colorspace = AVCOL_SPC_BT709;
777 case MKTAG('U', 'L', 'H', '2'):
779 avctx->pix_fmt = AV_PIX_FMT_YUV422P;
780 avctx->colorspace = AVCOL_SPC_BT709;
783 av_log(avctx, AV_LOG_ERROR, "Unknown Ut Video FOURCC provided (%08X)\n",
785 return AVERROR_INVALIDDATA;
791 static av_cold int decode_end(AVCodecContext *avctx)
793 UtvideoContext * const c = avctx->priv_data;
795 av_freep(&c->slice_bits);
800 AVCodec ff_utvideo_decoder = {
802 .long_name = NULL_IF_CONFIG_SMALL("Ut Video"),
803 .type = AVMEDIA_TYPE_VIDEO,
804 .id = AV_CODEC_ID_UTVIDEO,
805 .priv_data_size = sizeof(UtvideoContext),
808 .decode = decode_frame,
809 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,