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 #define CACHED_BITSTREAM_READER !ARCH_X86_32
31 #define UNCHECKED_BITSTREAM_READER 1
33 #include "libavutil/intreadwrite.h"
34 #include "libavutil/pixdesc.h"
37 #include "bytestream.h"
43 static int build_huff10(const uint8_t *src, VLC *vlc, int *fsym)
54 for (i = 0; i < 1024; i++) {
58 qsort(he, 1024, sizeof(*he), ff_ut10_huff_cmp_len);
66 while (he[last].len == 255 && last)
69 if (he[last].len > 32) {
74 for (i = last; i >= 0; i--) {
75 codes[i] = code >> (32 - he[i].len);
78 code += 0x80000000u >> (he[i].len - 1);
81 return ff_init_vlc_sparse(vlc, VLC_BITS, last + 1,
82 bits, sizeof(*bits), sizeof(*bits),
83 codes, sizeof(*codes), sizeof(*codes),
84 syms, sizeof(*syms), sizeof(*syms), 0);
87 static int build_huff(const uint8_t *src, VLC *vlc, int *fsym)
98 for (i = 0; i < 256; i++) {
102 qsort(he, 256, sizeof(*he), ff_ut_huff_cmp_len);
110 while (he[last].len == 255 && last)
113 if (he[last].len > 32)
117 for (i = last; i >= 0; i--) {
118 codes[i] = code >> (32 - he[i].len);
121 code += 0x80000000u >> (he[i].len - 1);
124 return ff_init_vlc_sparse(vlc, VLC_BITS, last + 1,
125 bits, sizeof(*bits), sizeof(*bits),
126 codes, sizeof(*codes), sizeof(*codes),
127 syms, sizeof(*syms), sizeof(*syms), 0);
130 static int decode_plane10(UtvideoContext *c, int plane_no,
131 uint16_t *dst, ptrdiff_t stride,
132 int width, int height,
133 const uint8_t *src, const uint8_t *huff,
136 int i, j, slice, pix, ret;
142 if ((ret = build_huff10(huff, &vlc, &fsym)) < 0) {
143 av_log(c->avctx, AV_LOG_ERROR, "Cannot build Huffman codes\n");
146 if (fsym >= 0) { // build_huff reported a symbol to fill slices with
148 for (slice = 0; slice < c->slices; slice++) {
152 send = (height * (slice + 1) / c->slices);
153 dest = dst + sstart * stride;
156 for (j = sstart; j < send; j++) {
157 for (i = 0; i < width; i++) {
173 for (slice = 0; slice < c->slices; slice++) {
175 int slice_data_start, slice_data_end, slice_size;
178 send = (height * (slice + 1) / c->slices);
179 dest = dst + sstart * stride;
181 // slice offset and size validation was done earlier
182 slice_data_start = slice ? AV_RL32(src + slice * 4 - 4) : 0;
183 slice_data_end = AV_RL32(src + slice * 4);
184 slice_size = slice_data_end - slice_data_start;
187 av_log(c->avctx, AV_LOG_ERROR, "Plane has more than one symbol "
188 "yet a slice has a length of zero.\n");
192 memset(c->slice_bits + slice_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
193 c->bdsp.bswap_buf((uint32_t *) c->slice_bits,
194 (uint32_t *)(src + slice_data_start + c->slices * 4),
195 (slice_data_end - slice_data_start + 3) >> 2);
196 init_get_bits(&gb, c->slice_bits, slice_size * 8);
199 for (j = sstart; j < send; j++) {
200 for (i = 0; i < width; i++) {
201 pix = get_vlc2(&gb, vlc.table, VLC_BITS, 3);
203 av_log(c->avctx, AV_LOG_ERROR, "Decoding error\n");
214 if (get_bits_left(&gb) < 0) {
215 av_log(c->avctx, AV_LOG_ERROR,
216 "Slice decoding ran out of bits\n");
220 if (get_bits_left(&gb) > 32)
221 av_log(c->avctx, AV_LOG_WARNING,
222 "%d bits left after decoding slice\n", get_bits_left(&gb));
230 return AVERROR_INVALIDDATA;
233 static int compute_cmask(int plane_no, int interlaced, enum AVPixelFormat pix_fmt)
235 const int is_luma = (pix_fmt == AV_PIX_FMT_YUV420P) && !plane_no;
238 return ~(1 + 2 * is_luma);
243 static int decode_plane(UtvideoContext *c, int plane_no,
244 uint8_t *dst, ptrdiff_t stride,
245 int width, int height,
246 const uint8_t *src, int use_pred)
248 int i, j, slice, pix;
253 const int cmask = compute_cmask(plane_no, c->interlaced, c->avctx->pix_fmt);
257 for (slice = 0; slice < c->slices; slice++) {
258 GetBitContext cbit, pbit;
261 ret = init_get_bits8(&cbit, c->control_stream[plane_no][slice], c->control_stream_size[plane_no][slice]);
265 ret = init_get_bits8(&pbit, c->packed_stream[plane_no][slice], c->packed_stream_size[plane_no][slice]);
270 send = (height * (slice + 1) / c->slices) & cmask;
271 dest = dst + sstart * stride;
273 if (3 * ((dst + send * stride - dest + 7)/8) > get_bits_left(&cbit))
274 return AVERROR_INVALIDDATA;
276 for (p = dest; p < dst + send * stride; p += 8) {
277 int bits = get_bits_le(&cbit, 3);
282 uint32_t sub = 0x80 >> (8 - (bits + 1)), add;
285 if ((bits + 1) * 8 > get_bits_left(&pbit))
286 return AVERROR_INVALIDDATA;
288 for (k = 0; k < 8; k++) {
290 p[k] = get_bits_le(&pbit, bits + 1);
291 add = (~p[k] & sub) << (8 - bits);
302 if (build_huff(src, &vlc, &fsym)) {
303 av_log(c->avctx, AV_LOG_ERROR, "Cannot build Huffman codes\n");
304 return AVERROR_INVALIDDATA;
306 if (fsym >= 0) { // build_huff reported a symbol to fill slices with
308 for (slice = 0; slice < c->slices; slice++) {
312 send = (height * (slice + 1) / c->slices) & cmask;
313 dest = dst + sstart * stride;
316 for (j = sstart; j < send; j++) {
317 for (i = 0; i < width; i++) {
334 for (slice = 0; slice < c->slices; slice++) {
336 int slice_data_start, slice_data_end, slice_size;
339 send = (height * (slice + 1) / c->slices) & cmask;
340 dest = dst + sstart * stride;
342 // slice offset and size validation was done earlier
343 slice_data_start = slice ? AV_RL32(src + slice * 4 - 4) : 0;
344 slice_data_end = AV_RL32(src + slice * 4);
345 slice_size = slice_data_end - slice_data_start;
348 av_log(c->avctx, AV_LOG_ERROR, "Plane has more than one symbol "
349 "yet a slice has a length of zero.\n");
353 memset(c->slice_bits + slice_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
354 c->bdsp.bswap_buf((uint32_t *) c->slice_bits,
355 (uint32_t *)(src + slice_data_start + c->slices * 4),
356 (slice_data_end - slice_data_start + 3) >> 2);
357 init_get_bits(&gb, c->slice_bits, slice_size * 8);
360 for (j = sstart; j < send; j++) {
361 for (i = 0; i < width; i++) {
362 pix = get_vlc2(&gb, vlc.table, VLC_BITS, 3);
364 av_log(c->avctx, AV_LOG_ERROR, "Decoding error\n");
373 if (get_bits_left(&gb) < 0) {
374 av_log(c->avctx, AV_LOG_ERROR,
375 "Slice decoding ran out of bits\n");
380 if (get_bits_left(&gb) > 32)
381 av_log(c->avctx, AV_LOG_WARNING,
382 "%d bits left after decoding slice\n", get_bits_left(&gb));
390 return AVERROR_INVALIDDATA;
397 static void restore_median_planar(UtvideoContext *c, uint8_t *src, ptrdiff_t stride,
398 int width, int height, int slices, int rmode)
403 int slice_start, slice_height;
404 const int cmask = ~rmode;
406 for (slice = 0; slice < slices; slice++) {
407 slice_start = ((slice * height) / slices) & cmask;
408 slice_height = ((((slice + 1) * height) / slices) & cmask) -
413 bsrc = src + slice_start * stride;
415 // first line - left neighbour prediction
417 c->llviddsp.add_left_pred(bsrc, bsrc, width, 0);
419 if (slice_height <= 1)
421 // second line - first element has top prediction, the rest uses median
425 for (i = 1; i < FFMIN(width, 16); i++) { /* scalar loop (DSP need align 16) */
426 B = bsrc[i - stride];
427 bsrc[i] += mid_pred(A, B, (uint8_t)(A + B - C));
432 c->llviddsp.add_median_pred(bsrc + 16, bsrc - stride + 16,
433 bsrc + 16, width - 16, &A, &B);
436 // the rest of lines use continuous median prediction
437 for (j = 2; j < slice_height; j++) {
438 c->llviddsp.add_median_pred(bsrc, bsrc - stride,
439 bsrc, width, &A, &B);
445 /* UtVideo interlaced mode treats every two lines as a single one,
446 * so restoring function should take care of possible padding between
447 * two parts of the same "line".
449 static void restore_median_planar_il(UtvideoContext *c, uint8_t *src, ptrdiff_t stride,
450 int width, int height, int slices, int rmode)
455 int slice_start, slice_height;
456 const int cmask = ~(rmode ? 3 : 1);
457 const ptrdiff_t stride2 = stride << 1;
459 for (slice = 0; slice < slices; slice++) {
460 slice_start = ((slice * height) / slices) & cmask;
461 slice_height = ((((slice + 1) * height) / slices) & cmask) -
467 bsrc = src + slice_start * stride;
469 // first line - left neighbour prediction
471 A = c->llviddsp.add_left_pred(bsrc, bsrc, width, 0);
472 c->llviddsp.add_left_pred(bsrc + stride, bsrc + stride, width, A);
474 if (slice_height <= 1)
476 // second line - first element has top prediction, the rest uses median
480 for (i = 1; i < FFMIN(width, 16); i++) { /* scalar loop (DSP need align 16) */
481 B = bsrc[i - stride2];
482 bsrc[i] += mid_pred(A, B, (uint8_t)(A + B - C));
487 c->llviddsp.add_median_pred(bsrc + 16, bsrc - stride2 + 16,
488 bsrc + 16, width - 16, &A, &B);
490 c->llviddsp.add_median_pred(bsrc + stride, bsrc - stride,
491 bsrc + stride, width, &A, &B);
493 // the rest of lines use continuous median prediction
494 for (j = 2; j < slice_height; j++) {
495 c->llviddsp.add_median_pred(bsrc, bsrc - stride2,
496 bsrc, width, &A, &B);
497 c->llviddsp.add_median_pred(bsrc + stride, bsrc - stride,
498 bsrc + stride, width, &A, &B);
504 static void restore_gradient_planar(UtvideoContext *c, uint8_t *src, ptrdiff_t stride,
505 int width, int height, int slices, int rmode)
510 int slice_start, slice_height;
511 const int cmask = ~rmode;
512 int min_width = FFMIN(width, 32);
514 for (slice = 0; slice < slices; slice++) {
515 slice_start = ((slice * height) / slices) & cmask;
516 slice_height = ((((slice + 1) * height) / slices) & cmask) -
521 bsrc = src + slice_start * stride;
523 // first line - left neighbour prediction
525 c->llviddsp.add_left_pred(bsrc, bsrc, width, 0);
527 if (slice_height <= 1)
529 for (j = 1; j < slice_height; j++) {
530 // second line - first element has top prediction, the rest uses gradient
531 bsrc[0] = (bsrc[0] + bsrc[-stride]) & 0xFF;
532 for (i = 1; i < min_width; i++) { /* dsp need align 32 */
533 A = bsrc[i - stride];
534 B = bsrc[i - (stride + 1)];
536 bsrc[i] = (A - B + C + bsrc[i]) & 0xFF;
539 c->llviddsp.add_gradient_pred(bsrc + 32, stride, width - 32);
545 static void restore_gradient_planar_il(UtvideoContext *c, uint8_t *src, ptrdiff_t stride,
546 int width, int height, int slices, int rmode)
551 int slice_start, slice_height;
552 const int cmask = ~(rmode ? 3 : 1);
553 const ptrdiff_t stride2 = stride << 1;
554 int min_width = FFMIN(width, 32);
556 for (slice = 0; slice < slices; slice++) {
557 slice_start = ((slice * height) / slices) & cmask;
558 slice_height = ((((slice + 1) * height) / slices) & cmask) -
564 bsrc = src + slice_start * stride;
566 // first line - left neighbour prediction
568 A = c->llviddsp.add_left_pred(bsrc, bsrc, width, 0);
569 c->llviddsp.add_left_pred(bsrc + stride, bsrc + stride, width, A);
571 if (slice_height <= 1)
573 for (j = 1; j < slice_height; j++) {
574 // second line - first element has top prediction, the rest uses gradient
575 bsrc[0] = (bsrc[0] + bsrc[-stride2]) & 0xFF;
576 for (i = 1; i < min_width; i++) { /* dsp need align 32 */
577 A = bsrc[i - stride2];
578 B = bsrc[i - (stride2 + 1)];
580 bsrc[i] = (A - B + C + bsrc[i]) & 0xFF;
583 c->llviddsp.add_gradient_pred(bsrc + 32, stride2, width - 32);
586 B = bsrc[-(1 + stride + stride - width)];
588 bsrc[stride] = (A - B + C + bsrc[stride]) & 0xFF;
589 for (i = 1; i < width; i++) {
590 A = bsrc[i - stride];
591 B = bsrc[i - (1 + stride)];
592 C = bsrc[i - 1 + stride];
593 bsrc[i + stride] = (A - B + C + bsrc[i + stride]) & 0xFF;
600 static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
603 const uint8_t *buf = avpkt->data;
604 int buf_size = avpkt->size;
605 UtvideoContext *c = avctx->priv_data;
607 const uint8_t *plane_start[5];
608 int plane_size, max_slice_size = 0, slice_start, slice_end, slice_size;
611 ThreadFrame frame = { .f = data };
613 if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
616 /* parse plane structure to get frame flags and validate slice offsets */
617 bytestream2_init(&gb, buf, buf_size);
620 const uint8_t *packed_stream;
621 const uint8_t *control_stream;
626 c->frame_info = PRED_GRADIENT << 8;
628 if (bytestream2_get_byte(&gb) != 1)
629 return AVERROR_INVALIDDATA;
630 bytestream2_skip(&gb, 3);
631 c->offset = bytestream2_get_le32(&gb);
633 if (buf_size <= c->offset + 8LL)
634 return AVERROR_INVALIDDATA;
636 bytestream2_init(&pb, buf + 8 + c->offset, buf_size - 8 - c->offset);
638 nb_cbs = bytestream2_get_le32(&pb);
639 if (nb_cbs > c->offset)
640 return AVERROR_INVALIDDATA;
642 packed_stream = buf + 8;
643 control_stream = packed_stream + (c->offset - nb_cbs);
644 left = control_stream - packed_stream;
646 for (i = 0; i < c->planes; i++) {
647 for (j = 0; j < c->slices; j++) {
648 c->packed_stream[i][j] = packed_stream;
649 c->packed_stream_size[i][j] = bytestream2_get_le32(&pb);
650 if (c->packed_stream_size[i][j] > left)
651 return AVERROR_INVALIDDATA;
652 left -= c->packed_stream_size[i][j];
653 packed_stream += c->packed_stream_size[i][j];
657 left = buf + buf_size - control_stream;
659 for (i = 0; i < c->planes; i++) {
660 for (j = 0; j < c->slices; j++) {
661 c->control_stream[i][j] = control_stream;
662 c->control_stream_size[i][j] = bytestream2_get_le32(&pb);
663 if (c->control_stream_size[i][j] > left)
664 return AVERROR_INVALIDDATA;
665 left -= c->control_stream_size[i][j];
666 control_stream += c->control_stream_size[i][j];
670 if (bytestream2_get_bytes_left(&gb) < c->frame_info_size) {
671 av_log(avctx, AV_LOG_ERROR, "Not enough data for frame information\n");
672 return AVERROR_INVALIDDATA;
674 c->frame_info = bytestream2_get_le32u(&gb);
675 c->slices = ((c->frame_info >> 16) & 0xff) + 1;
676 for (i = 0; i < c->planes; i++) {
677 plane_start[i] = gb.buffer;
678 if (bytestream2_get_bytes_left(&gb) < 1024 + 4 * c->slices) {
679 av_log(avctx, AV_LOG_ERROR, "Insufficient data for a plane\n");
680 return AVERROR_INVALIDDATA;
684 for (j = 0; j < c->slices; j++) {
685 slice_end = bytestream2_get_le32u(&gb);
686 if (slice_end < 0 || slice_end < slice_start ||
687 bytestream2_get_bytes_left(&gb) < slice_end + 1024LL) {
688 av_log(avctx, AV_LOG_ERROR, "Incorrect slice size\n");
689 return AVERROR_INVALIDDATA;
691 slice_size = slice_end - slice_start;
692 slice_start = slice_end;
693 max_slice_size = FFMAX(max_slice_size, slice_size);
695 plane_size = slice_end;
696 bytestream2_skipu(&gb, plane_size);
697 bytestream2_skipu(&gb, 1024);
699 plane_start[c->planes] = gb.buffer;
701 for (i = 0; i < c->planes; i++) {
702 plane_start[i] = gb.buffer;
703 if (bytestream2_get_bytes_left(&gb) < 256 + 4 * c->slices) {
704 av_log(avctx, AV_LOG_ERROR, "Insufficient data for a plane\n");
705 return AVERROR_INVALIDDATA;
707 bytestream2_skipu(&gb, 256);
710 for (j = 0; j < c->slices; j++) {
711 slice_end = bytestream2_get_le32u(&gb);
712 if (slice_end < 0 || slice_end < slice_start ||
713 bytestream2_get_bytes_left(&gb) < slice_end) {
714 av_log(avctx, AV_LOG_ERROR, "Incorrect slice size\n");
715 return AVERROR_INVALIDDATA;
717 slice_size = slice_end - slice_start;
718 slice_start = slice_end;
719 max_slice_size = FFMAX(max_slice_size, slice_size);
721 plane_size = slice_end;
722 bytestream2_skipu(&gb, plane_size);
724 plane_start[c->planes] = gb.buffer;
725 if (bytestream2_get_bytes_left(&gb) < c->frame_info_size) {
726 av_log(avctx, AV_LOG_ERROR, "Not enough data for frame information\n");
727 return AVERROR_INVALIDDATA;
729 c->frame_info = bytestream2_get_le32u(&gb);
731 av_log(avctx, AV_LOG_DEBUG, "frame information flags %"PRIX32"\n",
734 c->frame_pred = (c->frame_info >> 8) & 3;
736 max_slice_size += 4*avctx->width;
739 av_fast_malloc(&c->slice_bits, &c->slice_bits_size,
740 max_slice_size + AV_INPUT_BUFFER_PADDING_SIZE);
742 if (!c->slice_bits) {
743 av_log(avctx, AV_LOG_ERROR, "Cannot allocate temporary buffer\n");
744 return AVERROR(ENOMEM);
748 switch (c->avctx->pix_fmt) {
749 case AV_PIX_FMT_GBRP:
750 case AV_PIX_FMT_GBRAP:
751 for (i = 0; i < c->planes; i++) {
752 ret = decode_plane(c, i, frame.f->data[i],
753 frame.f->linesize[i], avctx->width,
754 avctx->height, plane_start[i],
755 c->frame_pred == PRED_LEFT);
758 if (c->frame_pred == PRED_MEDIAN) {
759 if (!c->interlaced) {
760 restore_median_planar(c, frame.f->data[i],
761 frame.f->linesize[i], avctx->width,
762 avctx->height, c->slices, 0);
764 restore_median_planar_il(c, frame.f->data[i],
765 frame.f->linesize[i],
766 avctx->width, avctx->height, c->slices,
769 } else if (c->frame_pred == PRED_GRADIENT) {
770 if (!c->interlaced) {
771 restore_gradient_planar(c, frame.f->data[i],
772 frame.f->linesize[i], avctx->width,
773 avctx->height, c->slices, 0);
775 restore_gradient_planar_il(c, frame.f->data[i],
776 frame.f->linesize[i],
777 avctx->width, avctx->height, c->slices,
782 c->utdsp.restore_rgb_planes(frame.f->data[2], frame.f->data[0], frame.f->data[1],
783 frame.f->linesize[2], frame.f->linesize[0], frame.f->linesize[1],
784 avctx->width, avctx->height);
786 case AV_PIX_FMT_GBRAP10:
787 case AV_PIX_FMT_GBRP10:
788 for (i = 0; i < c->planes; i++) {
789 ret = decode_plane10(c, i, (uint16_t *)frame.f->data[i],
790 frame.f->linesize[i] / 2, avctx->width,
791 avctx->height, plane_start[i],
792 plane_start[i + 1] - 1024,
793 c->frame_pred == PRED_LEFT);
797 c->utdsp.restore_rgb_planes10((uint16_t *)frame.f->data[2], (uint16_t *)frame.f->data[0], (uint16_t *)frame.f->data[1],
798 frame.f->linesize[2] / 2, frame.f->linesize[0] / 2, frame.f->linesize[1] / 2,
799 avctx->width, avctx->height);
801 case AV_PIX_FMT_YUV420P:
802 for (i = 0; i < 3; i++) {
803 ret = decode_plane(c, i, frame.f->data[i], frame.f->linesize[i],
804 avctx->width >> !!i, avctx->height >> !!i,
805 plane_start[i], c->frame_pred == PRED_LEFT);
808 if (c->frame_pred == PRED_MEDIAN) {
809 if (!c->interlaced) {
810 restore_median_planar(c, frame.f->data[i], frame.f->linesize[i],
811 avctx->width >> !!i, avctx->height >> !!i,
814 restore_median_planar_il(c, frame.f->data[i], frame.f->linesize[i],
816 avctx->height >> !!i,
819 } else if (c->frame_pred == PRED_GRADIENT) {
820 if (!c->interlaced) {
821 restore_gradient_planar(c, frame.f->data[i], frame.f->linesize[i],
822 avctx->width >> !!i, avctx->height >> !!i,
825 restore_gradient_planar_il(c, frame.f->data[i], frame.f->linesize[i],
827 avctx->height >> !!i,
833 case AV_PIX_FMT_YUV422P:
834 for (i = 0; i < 3; i++) {
835 ret = decode_plane(c, i, frame.f->data[i], frame.f->linesize[i],
836 avctx->width >> !!i, avctx->height,
837 plane_start[i], c->frame_pred == PRED_LEFT);
840 if (c->frame_pred == PRED_MEDIAN) {
841 if (!c->interlaced) {
842 restore_median_planar(c, frame.f->data[i], frame.f->linesize[i],
843 avctx->width >> !!i, avctx->height,
846 restore_median_planar_il(c, frame.f->data[i], frame.f->linesize[i],
847 avctx->width >> !!i, avctx->height,
850 } else if (c->frame_pred == PRED_GRADIENT) {
851 if (!c->interlaced) {
852 restore_gradient_planar(c, frame.f->data[i], frame.f->linesize[i],
853 avctx->width >> !!i, avctx->height,
856 restore_gradient_planar_il(c, frame.f->data[i], frame.f->linesize[i],
857 avctx->width >> !!i, avctx->height,
863 case AV_PIX_FMT_YUV444P:
864 for (i = 0; i < 3; i++) {
865 ret = decode_plane(c, i, frame.f->data[i], frame.f->linesize[i],
866 avctx->width, avctx->height,
867 plane_start[i], c->frame_pred == PRED_LEFT);
870 if (c->frame_pred == PRED_MEDIAN) {
871 if (!c->interlaced) {
872 restore_median_planar(c, frame.f->data[i], frame.f->linesize[i],
873 avctx->width, avctx->height,
876 restore_median_planar_il(c, frame.f->data[i], frame.f->linesize[i],
877 avctx->width, avctx->height,
880 } else if (c->frame_pred == PRED_GRADIENT) {
881 if (!c->interlaced) {
882 restore_gradient_planar(c, frame.f->data[i], frame.f->linesize[i],
883 avctx->width, avctx->height,
886 restore_gradient_planar_il(c, frame.f->data[i], frame.f->linesize[i],
887 avctx->width, avctx->height,
893 case AV_PIX_FMT_YUV422P10:
894 for (i = 0; i < 3; i++) {
895 ret = decode_plane10(c, i, (uint16_t *)frame.f->data[i], frame.f->linesize[i] / 2,
896 avctx->width >> !!i, avctx->height,
897 plane_start[i], plane_start[i + 1] - 1024, c->frame_pred == PRED_LEFT);
904 frame.f->key_frame = 1;
905 frame.f->pict_type = AV_PICTURE_TYPE_I;
906 frame.f->interlaced_frame = !!c->interlaced;
910 /* always report that the buffer was completely consumed */
914 static av_cold int decode_init(AVCodecContext *avctx)
916 UtvideoContext * const c = avctx->priv_data;
917 int h_shift, v_shift;
921 ff_utvideodsp_init(&c->utdsp);
922 ff_bswapdsp_init(&c->bdsp);
923 ff_llviddsp_init(&c->llviddsp);
925 c->slice_bits_size = 0;
927 switch (avctx->codec_tag) {
928 case MKTAG('U', 'L', 'R', 'G'):
930 avctx->pix_fmt = AV_PIX_FMT_GBRP;
932 case MKTAG('U', 'L', 'R', 'A'):
934 avctx->pix_fmt = AV_PIX_FMT_GBRAP;
936 case MKTAG('U', 'L', 'Y', '0'):
938 avctx->pix_fmt = AV_PIX_FMT_YUV420P;
939 avctx->colorspace = AVCOL_SPC_BT470BG;
941 case MKTAG('U', 'L', 'Y', '2'):
943 avctx->pix_fmt = AV_PIX_FMT_YUV422P;
944 avctx->colorspace = AVCOL_SPC_BT470BG;
946 case MKTAG('U', 'L', 'Y', '4'):
948 avctx->pix_fmt = AV_PIX_FMT_YUV444P;
949 avctx->colorspace = AVCOL_SPC_BT470BG;
951 case MKTAG('U', 'Q', 'Y', '2'):
954 avctx->pix_fmt = AV_PIX_FMT_YUV422P10;
956 case MKTAG('U', 'Q', 'R', 'G'):
959 avctx->pix_fmt = AV_PIX_FMT_GBRP10;
961 case MKTAG('U', 'Q', 'R', 'A'):
964 avctx->pix_fmt = AV_PIX_FMT_GBRAP10;
966 case MKTAG('U', 'L', 'H', '0'):
968 avctx->pix_fmt = AV_PIX_FMT_YUV420P;
969 avctx->colorspace = AVCOL_SPC_BT709;
971 case MKTAG('U', 'L', 'H', '2'):
973 avctx->pix_fmt = AV_PIX_FMT_YUV422P;
974 avctx->colorspace = AVCOL_SPC_BT709;
976 case MKTAG('U', 'L', 'H', '4'):
978 avctx->pix_fmt = AV_PIX_FMT_YUV444P;
979 avctx->colorspace = AVCOL_SPC_BT709;
981 case MKTAG('U', 'M', 'Y', '2'):
984 avctx->pix_fmt = AV_PIX_FMT_YUV422P;
985 avctx->colorspace = AVCOL_SPC_BT470BG;
987 case MKTAG('U', 'M', 'H', '2'):
990 avctx->pix_fmt = AV_PIX_FMT_YUV422P;
991 avctx->colorspace = AVCOL_SPC_BT709;
993 case MKTAG('U', 'M', 'Y', '4'):
996 avctx->pix_fmt = AV_PIX_FMT_YUV444P;
997 avctx->colorspace = AVCOL_SPC_BT470BG;
999 case MKTAG('U', 'M', 'H', '4'):
1002 avctx->pix_fmt = AV_PIX_FMT_YUV444P;
1003 avctx->colorspace = AVCOL_SPC_BT709;
1005 case MKTAG('U', 'M', 'R', 'G'):
1008 avctx->pix_fmt = AV_PIX_FMT_GBRP;
1010 case MKTAG('U', 'M', 'R', 'A'):
1013 avctx->pix_fmt = AV_PIX_FMT_GBRAP;
1016 av_log(avctx, AV_LOG_ERROR, "Unknown Ut Video FOURCC provided (%08X)\n",
1018 return AVERROR_INVALIDDATA;
1021 av_pix_fmt_get_chroma_sub_sample(avctx->pix_fmt, &h_shift, &v_shift);
1022 if ((avctx->width & ((1<<h_shift)-1)) ||
1023 (avctx->height & ((1<<v_shift)-1))) {
1024 avpriv_request_sample(avctx, "Odd dimensions");
1025 return AVERROR_PATCHWELCOME;
1028 if (c->pack && avctx->extradata_size >= 16) {
1029 av_log(avctx, AV_LOG_DEBUG, "Encoder version %d.%d.%d.%d\n",
1030 avctx->extradata[3], avctx->extradata[2],
1031 avctx->extradata[1], avctx->extradata[0]);
1032 av_log(avctx, AV_LOG_DEBUG, "Original format %"PRIX32"\n",
1033 AV_RB32(avctx->extradata + 4));
1034 c->compression = avctx->extradata[8];
1035 if (c->compression != 2)
1036 avpriv_request_sample(avctx, "Unknown compression type");
1037 c->slices = avctx->extradata[9] + 1;
1038 } else if (!c->pro && avctx->extradata_size >= 16) {
1039 av_log(avctx, AV_LOG_DEBUG, "Encoder version %d.%d.%d.%d\n",
1040 avctx->extradata[3], avctx->extradata[2],
1041 avctx->extradata[1], avctx->extradata[0]);
1042 av_log(avctx, AV_LOG_DEBUG, "Original format %"PRIX32"\n",
1043 AV_RB32(avctx->extradata + 4));
1044 c->frame_info_size = AV_RL32(avctx->extradata + 8);
1045 c->flags = AV_RL32(avctx->extradata + 12);
1047 if (c->frame_info_size != 4)
1048 avpriv_request_sample(avctx, "Frame info not 4 bytes");
1049 av_log(avctx, AV_LOG_DEBUG, "Encoding parameters %08"PRIX32"\n", c->flags);
1050 c->slices = (c->flags >> 24) + 1;
1051 c->compression = c->flags & 1;
1052 c->interlaced = c->flags & 0x800;
1053 } else if (c->pro && avctx->extradata_size == 8) {
1054 av_log(avctx, AV_LOG_DEBUG, "Encoder version %d.%d.%d.%d\n",
1055 avctx->extradata[3], avctx->extradata[2],
1056 avctx->extradata[1], avctx->extradata[0]);
1057 av_log(avctx, AV_LOG_DEBUG, "Original format %"PRIX32"\n",
1058 AV_RB32(avctx->extradata + 4));
1060 c->frame_info_size = 4;
1062 av_log(avctx, AV_LOG_ERROR,
1063 "Insufficient extradata size %d, should be at least 16\n",
1064 avctx->extradata_size);
1065 return AVERROR_INVALIDDATA;
1071 static av_cold int decode_end(AVCodecContext *avctx)
1073 UtvideoContext * const c = avctx->priv_data;
1075 av_freep(&c->slice_bits);
1080 AVCodec ff_utvideo_decoder = {
1082 .long_name = NULL_IF_CONFIG_SMALL("Ut Video"),
1083 .type = AVMEDIA_TYPE_VIDEO,
1084 .id = AV_CODEC_ID_UTVIDEO,
1085 .priv_data_size = sizeof(UtvideoContext),
1086 .init = decode_init,
1087 .close = decode_end,
1088 .decode = decode_frame,
1089 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
1090 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,