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"
38 #include "codec_internal.h"
43 typedef struct HuffEntry
{
48 static int build_huff(UtvideoContext
*c
, const uint8_t *src
, VLC
*vlc
,
49 int *fsym
, unsigned nb_elems
)
54 uint16_t codes_count
[33] = { 0 };
57 for (i
= 0; i
< nb_elems
; i
++) {
61 } else if (src
[i
] == 255) {
63 } else if (src
[i
] <= 32) {
66 return AVERROR_INVALIDDATA
;
68 codes_count
[bits
[i
]]++;
70 if (codes_count
[0] == nb_elems
)
71 return AVERROR_INVALIDDATA
;
73 /* For Ut Video, longer codes are to the left of the tree and
74 * for codes with the same length the symbol is descending from
75 * left to right. So after the next loop --codes_count[i] will
76 * be the index of the first (lowest) symbol of length i when
77 * indexed by the position in the tree with left nodes being first. */
78 for (int i
= 31; i
>= 0; i
--)
79 codes_count
[i
] += codes_count
[i
+ 1];
81 for (unsigned i
= 0; i
< nb_elems
; i
++)
82 he
[--codes_count
[bits
[i
]]] = (HuffEntry
) { bits
[i
], i
};
85 return ff_init_vlc_from_lengths(vlc
, VLC_BITS
, codes_count
[0],
86 &he
[0].len
, sizeof(*he
),
87 &he
[0].sym
, sizeof(*he
), 2, 0, 0, c
->avctx
);
90 static int decode_plane10(UtvideoContext
*c
, int plane_no
,
91 uint16_t *dst
, ptrdiff_t stride
,
92 int width
, int height
,
93 const uint8_t *src
, const uint8_t *huff
,
96 int i
, j
, slice
, pix
, ret
;
102 if ((ret
= build_huff(c
, huff
, &vlc
, &fsym
, 1024)) < 0) {
103 av_log(c
->avctx
, AV_LOG_ERROR
, "Cannot build Huffman codes\n");
106 if (fsym
>= 0) { // build_huff reported a symbol to fill slices with
108 for (slice
= 0; slice
< c
->slices
; slice
++) {
112 send
= (height
* (slice
+ 1) / c
->slices
);
113 dest
= dst
+ sstart
* stride
;
116 for (j
= sstart
; j
< send
; j
++) {
117 for (i
= 0; i
< width
; i
++) {
133 for (slice
= 0; slice
< c
->slices
; slice
++) {
135 int slice_data_start
, slice_data_end
, slice_size
;
138 send
= (height
* (slice
+ 1) / c
->slices
);
139 dest
= dst
+ sstart
* stride
;
141 // slice offset and size validation was done earlier
142 slice_data_start
= slice
? AV_RL32(src
+ slice
* 4 - 4) : 0;
143 slice_data_end
= AV_RL32(src
+ slice
* 4);
144 slice_size
= slice_data_end
- slice_data_start
;
147 av_log(c
->avctx
, AV_LOG_ERROR
, "Plane has more than one symbol "
148 "yet a slice has a length of zero.\n");
152 memset(c
->slice_bits
+ slice_size
, 0, AV_INPUT_BUFFER_PADDING_SIZE
);
153 c
->bdsp
.bswap_buf((uint32_t *) c
->slice_bits
,
154 (uint32_t *)(src
+ slice_data_start
+ c
->slices
* 4),
155 (slice_data_end
- slice_data_start
+ 3) >> 2);
156 init_get_bits(&gb
, c
->slice_bits
, slice_size
* 8);
159 for (j
= sstart
; j
< send
; j
++) {
160 for (i
= 0; i
< width
; i
++) {
161 pix
= get_vlc2(&gb
, vlc
.table
, VLC_BITS
, 3);
163 av_log(c
->avctx
, AV_LOG_ERROR
, "Decoding error\n");
174 if (get_bits_left(&gb
) < 0) {
175 av_log(c
->avctx
, AV_LOG_ERROR
,
176 "Slice decoding ran out of bits\n");
180 if (get_bits_left(&gb
) > 32)
181 av_log(c
->avctx
, AV_LOG_WARNING
,
182 "%d bits left after decoding slice\n", get_bits_left(&gb
));
190 return AVERROR_INVALIDDATA
;
193 static int compute_cmask(int plane_no
, int interlaced
, enum AVPixelFormat pix_fmt
)
195 const int is_luma
= (pix_fmt
== AV_PIX_FMT_YUV420P
) && !plane_no
;
198 return ~(1 + 2 * is_luma
);
203 static int decode_plane(UtvideoContext
*c
, int plane_no
,
204 uint8_t *dst
, ptrdiff_t stride
,
205 int width
, int height
,
206 const uint8_t *src
, int use_pred
)
208 int i
, j
, slice
, pix
;
213 const int cmask
= compute_cmask(plane_no
, c
->interlaced
, c
->avctx
->pix_fmt
);
217 for (slice
= 0; slice
< c
->slices
; slice
++) {
218 GetBitContext cbit
, pbit
;
221 ret
= init_get_bits8_le(&cbit
, c
->control_stream
[plane_no
][slice
], c
->control_stream_size
[plane_no
][slice
]);
225 ret
= init_get_bits8_le(&pbit
, c
->packed_stream
[plane_no
][slice
], c
->packed_stream_size
[plane_no
][slice
]);
230 send
= (height
* (slice
+ 1) / c
->slices
) & cmask
;
231 dest
= dst
+ sstart
* stride
;
233 if (3 * ((dst
+ send
* stride
- dest
+ 7)/8) > get_bits_left(&cbit
))
234 return AVERROR_INVALIDDATA
;
236 for (p
= dest
; p
< dst
+ send
* stride
; p
+= 8) {
237 int bits
= get_bits_le(&cbit
, 3);
242 uint32_t sub
= 0x80 >> (8 - (bits
+ 1)), add
;
245 if ((bits
+ 1) * 8 > get_bits_left(&pbit
))
246 return AVERROR_INVALIDDATA
;
248 for (k
= 0; k
< 8; k
++) {
250 p
[k
] = get_bits_le(&pbit
, bits
+ 1);
251 add
= (~p
[k
] & sub
) << (8 - bits
);
262 if (build_huff(c
, src
, &vlc
, &fsym
, 256)) {
263 av_log(c
->avctx
, AV_LOG_ERROR
, "Cannot build Huffman codes\n");
264 return AVERROR_INVALIDDATA
;
266 if (fsym
>= 0) { // build_huff reported a symbol to fill slices with
268 for (slice
= 0; slice
< c
->slices
; slice
++) {
272 send
= (height
* (slice
+ 1) / c
->slices
) & cmask
;
273 dest
= dst
+ sstart
* stride
;
276 for (j
= sstart
; j
< send
; j
++) {
277 for (i
= 0; i
< width
; i
++) {
280 prev
+= (unsigned)pix
;
294 for (slice
= 0; slice
< c
->slices
; slice
++) {
296 int slice_data_start
, slice_data_end
, slice_size
;
299 send
= (height
* (slice
+ 1) / c
->slices
) & cmask
;
300 dest
= dst
+ sstart
* stride
;
302 // slice offset and size validation was done earlier
303 slice_data_start
= slice
? AV_RL32(src
+ slice
* 4 - 4) : 0;
304 slice_data_end
= AV_RL32(src
+ slice
* 4);
305 slice_size
= slice_data_end
- slice_data_start
;
308 av_log(c
->avctx
, AV_LOG_ERROR
, "Plane has more than one symbol "
309 "yet a slice has a length of zero.\n");
313 memset(c
->slice_bits
+ slice_size
, 0, AV_INPUT_BUFFER_PADDING_SIZE
);
314 c
->bdsp
.bswap_buf((uint32_t *) c
->slice_bits
,
315 (uint32_t *)(src
+ slice_data_start
+ c
->slices
* 4),
316 (slice_data_end
- slice_data_start
+ 3) >> 2);
317 init_get_bits(&gb
, c
->slice_bits
, slice_size
* 8);
320 for (j
= sstart
; j
< send
; j
++) {
321 for (i
= 0; i
< width
; i
++) {
322 pix
= get_vlc2(&gb
, vlc
.table
, VLC_BITS
, 3);
324 av_log(c
->avctx
, AV_LOG_ERROR
, "Decoding error\n");
333 if (get_bits_left(&gb
) < 0) {
334 av_log(c
->avctx
, AV_LOG_ERROR
,
335 "Slice decoding ran out of bits\n");
340 if (get_bits_left(&gb
) > 32)
341 av_log(c
->avctx
, AV_LOG_WARNING
,
342 "%d bits left after decoding slice\n", get_bits_left(&gb
));
350 return AVERROR_INVALIDDATA
;
357 static void restore_median_planar(UtvideoContext
*c
, uint8_t *src
, ptrdiff_t stride
,
358 int width
, int height
, int slices
, int rmode
)
363 int slice_start
, slice_height
;
364 const int cmask
= ~rmode
;
366 for (slice
= 0; slice
< slices
; slice
++) {
367 slice_start
= ((slice
* height
) / slices
) & cmask
;
368 slice_height
= ((((slice
+ 1) * height
) / slices
) & cmask
) -
373 bsrc
= src
+ slice_start
* stride
;
375 // first line - left neighbour prediction
377 c
->llviddsp
.add_left_pred(bsrc
, bsrc
, width
, 0);
379 if (slice_height
<= 1)
381 // second line - first element has top prediction, the rest uses median
385 for (i
= 1; i
< FFMIN(width
, 16); i
++) { /* scalar loop (DSP need align 16) */
386 B
= bsrc
[i
- stride
];
387 bsrc
[i
] += mid_pred(A
, B
, (uint8_t)(A
+ B
- C
));
392 c
->llviddsp
.add_median_pred(bsrc
+ 16, bsrc
- stride
+ 16,
393 bsrc
+ 16, width
- 16, &A
, &B
);
396 // the rest of lines use continuous median prediction
397 for (j
= 2; j
< slice_height
; j
++) {
398 c
->llviddsp
.add_median_pred(bsrc
, bsrc
- stride
,
399 bsrc
, width
, &A
, &B
);
405 /* UtVideo interlaced mode treats every two lines as a single one,
406 * so restoring function should take care of possible padding between
407 * two parts of the same "line".
409 static void restore_median_planar_il(UtvideoContext
*c
, uint8_t *src
, ptrdiff_t stride
,
410 int width
, int height
, int slices
, int rmode
)
415 int slice_start
, slice_height
;
416 const int cmask
= ~(rmode
? 3 : 1);
417 const ptrdiff_t stride2
= stride
<< 1;
419 for (slice
= 0; slice
< slices
; slice
++) {
420 slice_start
= ((slice
* height
) / slices
) & cmask
;
421 slice_height
= ((((slice
+ 1) * height
) / slices
) & cmask
) -
427 bsrc
= src
+ slice_start
* stride
;
429 // first line - left neighbour prediction
431 A
= c
->llviddsp
.add_left_pred(bsrc
, bsrc
, width
, 0);
432 c
->llviddsp
.add_left_pred(bsrc
+ stride
, bsrc
+ stride
, width
, A
);
434 if (slice_height
<= 1)
436 // second line - first element has top prediction, the rest uses median
440 for (i
= 1; i
< FFMIN(width
, 16); i
++) { /* scalar loop (DSP need align 16) */
441 B
= bsrc
[i
- stride2
];
442 bsrc
[i
] += mid_pred(A
, B
, (uint8_t)(A
+ B
- C
));
447 c
->llviddsp
.add_median_pred(bsrc
+ 16, bsrc
- stride2
+ 16,
448 bsrc
+ 16, width
- 16, &A
, &B
);
450 c
->llviddsp
.add_median_pred(bsrc
+ stride
, bsrc
- stride
,
451 bsrc
+ stride
, width
, &A
, &B
);
453 // the rest of lines use continuous median prediction
454 for (j
= 2; j
< slice_height
; j
++) {
455 c
->llviddsp
.add_median_pred(bsrc
, bsrc
- stride2
,
456 bsrc
, width
, &A
, &B
);
457 c
->llviddsp
.add_median_pred(bsrc
+ stride
, bsrc
- stride
,
458 bsrc
+ stride
, width
, &A
, &B
);
464 static void restore_gradient_planar(UtvideoContext
*c
, uint8_t *src
, ptrdiff_t stride
,
465 int width
, int height
, int slices
, int rmode
)
470 int slice_start
, slice_height
;
471 const int cmask
= ~rmode
;
472 int min_width
= FFMIN(width
, 32);
474 for (slice
= 0; slice
< slices
; slice
++) {
475 slice_start
= ((slice
* height
) / slices
) & cmask
;
476 slice_height
= ((((slice
+ 1) * height
) / slices
) & cmask
) -
481 bsrc
= src
+ slice_start
* stride
;
483 // first line - left neighbour prediction
485 c
->llviddsp
.add_left_pred(bsrc
, bsrc
, width
, 0);
487 if (slice_height
<= 1)
489 for (j
= 1; j
< slice_height
; j
++) {
490 // second line - first element has top prediction, the rest uses gradient
491 bsrc
[0] = (bsrc
[0] + bsrc
[-stride
]) & 0xFF;
492 for (i
= 1; i
< min_width
; i
++) { /* dsp need align 32 */
493 A
= bsrc
[i
- stride
];
494 B
= bsrc
[i
- (stride
+ 1)];
496 bsrc
[i
] = (A
- B
+ C
+ bsrc
[i
]) & 0xFF;
499 c
->llviddsp
.add_gradient_pred(bsrc
+ 32, stride
, width
- 32);
505 static void restore_gradient_planar_il(UtvideoContext
*c
, uint8_t *src
, ptrdiff_t stride
,
506 int width
, int height
, int slices
, int rmode
)
511 int slice_start
, slice_height
;
512 const int cmask
= ~(rmode
? 3 : 1);
513 const ptrdiff_t stride2
= stride
<< 1;
514 int min_width
= FFMIN(width
, 32);
516 for (slice
= 0; slice
< slices
; slice
++) {
517 slice_start
= ((slice
* height
) / slices
) & cmask
;
518 slice_height
= ((((slice
+ 1) * height
) / slices
) & cmask
) -
524 bsrc
= src
+ slice_start
* stride
;
526 // first line - left neighbour prediction
528 A
= c
->llviddsp
.add_left_pred(bsrc
, bsrc
, width
, 0);
529 c
->llviddsp
.add_left_pred(bsrc
+ stride
, bsrc
+ stride
, width
, A
);
531 if (slice_height
<= 1)
533 for (j
= 1; j
< slice_height
; j
++) {
534 // second line - first element has top prediction, the rest uses gradient
535 bsrc
[0] = (bsrc
[0] + bsrc
[-stride2
]) & 0xFF;
536 for (i
= 1; i
< min_width
; i
++) { /* dsp need align 32 */
537 A
= bsrc
[i
- stride2
];
538 B
= bsrc
[i
- (stride2
+ 1)];
540 bsrc
[i
] = (A
- B
+ C
+ bsrc
[i
]) & 0xFF;
543 c
->llviddsp
.add_gradient_pred(bsrc
+ 32, stride2
, width
- 32);
546 B
= bsrc
[-(1 + stride
+ stride
- width
)];
548 bsrc
[stride
] = (A
- B
+ C
+ bsrc
[stride
]) & 0xFF;
549 for (i
= 1; i
< width
; i
++) {
550 A
= bsrc
[i
- stride
];
551 B
= bsrc
[i
- (1 + stride
)];
552 C
= bsrc
[i
- 1 + stride
];
553 bsrc
[i
+ stride
] = (A
- B
+ C
+ bsrc
[i
+ stride
]) & 0xFF;
560 static int decode_frame(AVCodecContext
*avctx
, AVFrame
*frame
,
561 int *got_frame
, AVPacket
*avpkt
)
563 const uint8_t *buf
= avpkt
->data
;
564 int buf_size
= avpkt
->size
;
565 UtvideoContext
*c
= avctx
->priv_data
;
567 const uint8_t *plane_start
[5] = {NULL
};
568 int plane_size
, max_slice_size
= 0, slice_start
, slice_end
, slice_size
;
572 if ((ret
= ff_thread_get_buffer(avctx
, frame
, 0)) < 0)
575 /* parse plane structure to get frame flags and validate slice offsets */
576 bytestream2_init(&gb
, buf
, buf_size
);
579 const uint8_t *packed_stream
;
580 const uint8_t *control_stream
;
585 c
->frame_info
= PRED_GRADIENT
<< 8;
587 if (bytestream2_get_byte(&gb
) != 1)
588 return AVERROR_INVALIDDATA
;
589 bytestream2_skip(&gb
, 3);
590 c
->offset
= bytestream2_get_le32(&gb
);
592 if (buf_size
<= c
->offset
+ 8LL)
593 return AVERROR_INVALIDDATA
;
595 bytestream2_init(&pb
, buf
+ 8 + c
->offset
, buf_size
- 8 - c
->offset
);
597 nb_cbs
= bytestream2_get_le32(&pb
);
598 if (nb_cbs
> c
->offset
)
599 return AVERROR_INVALIDDATA
;
601 packed_stream
= buf
+ 8;
602 control_stream
= packed_stream
+ (c
->offset
- nb_cbs
);
603 left
= control_stream
- packed_stream
;
605 for (i
= 0; i
< c
->planes
; i
++) {
606 for (j
= 0; j
< c
->slices
; j
++) {
607 c
->packed_stream
[i
][j
] = packed_stream
;
608 c
->packed_stream_size
[i
][j
] = bytestream2_get_le32(&pb
);
609 if (c
->packed_stream_size
[i
][j
] > left
)
610 return AVERROR_INVALIDDATA
;
611 left
-= c
->packed_stream_size
[i
][j
];
612 packed_stream
+= c
->packed_stream_size
[i
][j
];
616 left
= buf
+ buf_size
- control_stream
;
618 for (i
= 0; i
< c
->planes
; i
++) {
619 for (j
= 0; j
< c
->slices
; j
++) {
620 c
->control_stream
[i
][j
] = control_stream
;
621 c
->control_stream_size
[i
][j
] = bytestream2_get_le32(&pb
);
622 if (c
->control_stream_size
[i
][j
] > left
)
623 return AVERROR_INVALIDDATA
;
624 left
-= c
->control_stream_size
[i
][j
];
625 control_stream
+= c
->control_stream_size
[i
][j
];
629 if (bytestream2_get_bytes_left(&gb
) < c
->frame_info_size
) {
630 av_log(avctx
, AV_LOG_ERROR
, "Not enough data for frame information\n");
631 return AVERROR_INVALIDDATA
;
633 c
->frame_info
= bytestream2_get_le32u(&gb
);
634 c
->slices
= ((c
->frame_info
>> 16) & 0xff) + 1;
635 for (i
= 0; i
< c
->planes
; i
++) {
636 plane_start
[i
] = gb
.buffer
;
637 if (bytestream2_get_bytes_left(&gb
) < 1024 + 4 * c
->slices
) {
638 av_log(avctx
, AV_LOG_ERROR
, "Insufficient data for a plane\n");
639 return AVERROR_INVALIDDATA
;
643 for (j
= 0; j
< c
->slices
; j
++) {
644 slice_end
= bytestream2_get_le32u(&gb
);
645 if (slice_end
< 0 || slice_end
< slice_start
||
646 bytestream2_get_bytes_left(&gb
) < slice_end
+ 1024LL) {
647 av_log(avctx
, AV_LOG_ERROR
, "Incorrect slice size\n");
648 return AVERROR_INVALIDDATA
;
650 slice_size
= slice_end
- slice_start
;
651 slice_start
= slice_end
;
652 max_slice_size
= FFMAX(max_slice_size
, slice_size
);
654 plane_size
= slice_end
;
655 bytestream2_skipu(&gb
, plane_size
);
656 bytestream2_skipu(&gb
, 1024);
658 plane_start
[c
->planes
] = gb
.buffer
;
660 for (i
= 0; i
< c
->planes
; i
++) {
661 plane_start
[i
] = gb
.buffer
;
662 if (bytestream2_get_bytes_left(&gb
) < 256 + 4 * c
->slices
) {
663 av_log(avctx
, AV_LOG_ERROR
, "Insufficient data for a plane\n");
664 return AVERROR_INVALIDDATA
;
666 bytestream2_skipu(&gb
, 256);
669 for (j
= 0; j
< c
->slices
; j
++) {
670 slice_end
= bytestream2_get_le32u(&gb
);
671 if (slice_end
< 0 || slice_end
< slice_start
||
672 bytestream2_get_bytes_left(&gb
) < slice_end
) {
673 av_log(avctx
, AV_LOG_ERROR
, "Incorrect slice size\n");
674 return AVERROR_INVALIDDATA
;
676 slice_size
= slice_end
- slice_start
;
677 slice_start
= slice_end
;
678 max_slice_size
= FFMAX(max_slice_size
, slice_size
);
680 plane_size
= slice_end
;
681 bytestream2_skipu(&gb
, plane_size
);
683 plane_start
[c
->planes
] = gb
.buffer
;
684 if (bytestream2_get_bytes_left(&gb
) < c
->frame_info_size
) {
685 av_log(avctx
, AV_LOG_ERROR
, "Not enough data for frame information\n");
686 return AVERROR_INVALIDDATA
;
688 c
->frame_info
= bytestream2_get_le32u(&gb
);
690 av_log(avctx
, AV_LOG_DEBUG
, "frame information flags %"PRIX32
"\n",
693 c
->frame_pred
= (c
->frame_info
>> 8) & 3;
695 max_slice_size
+= 4*avctx
->width
;
698 av_fast_malloc(&c
->slice_bits
, &c
->slice_bits_size
,
699 max_slice_size
+ AV_INPUT_BUFFER_PADDING_SIZE
);
701 if (!c
->slice_bits
) {
702 av_log(avctx
, AV_LOG_ERROR
, "Cannot allocate temporary buffer\n");
703 return AVERROR(ENOMEM
);
707 switch (c
->avctx
->pix_fmt
) {
708 case AV_PIX_FMT_GBRP
:
709 case AV_PIX_FMT_GBRAP
:
710 for (i
= 0; i
< c
->planes
; i
++) {
711 ret
= decode_plane(c
, i
, frame
->data
[i
],
712 frame
->linesize
[i
], avctx
->width
,
713 avctx
->height
, plane_start
[i
],
714 c
->frame_pred
== PRED_LEFT
);
717 if (c
->frame_pred
== PRED_MEDIAN
) {
718 if (!c
->interlaced
) {
719 restore_median_planar(c
, frame
->data
[i
],
720 frame
->linesize
[i
], avctx
->width
,
721 avctx
->height
, c
->slices
, 0);
723 restore_median_planar_il(c
, frame
->data
[i
],
725 avctx
->width
, avctx
->height
, c
->slices
,
728 } else if (c
->frame_pred
== PRED_GRADIENT
) {
729 if (!c
->interlaced
) {
730 restore_gradient_planar(c
, frame
->data
[i
],
731 frame
->linesize
[i
], avctx
->width
,
732 avctx
->height
, c
->slices
, 0);
734 restore_gradient_planar_il(c
, frame
->data
[i
],
736 avctx
->width
, avctx
->height
, c
->slices
,
741 c
->utdsp
.restore_rgb_planes(frame
->data
[2], frame
->data
[0], frame
->data
[1],
742 frame
->linesize
[2], frame
->linesize
[0], frame
->linesize
[1],
743 avctx
->width
, avctx
->height
);
745 case AV_PIX_FMT_GBRAP10
:
746 case AV_PIX_FMT_GBRP10
:
747 for (i
= 0; i
< c
->planes
; i
++) {
748 ret
= decode_plane10(c
, i
, (uint16_t *)frame
->data
[i
],
749 frame
->linesize
[i
] / 2, avctx
->width
,
750 avctx
->height
, plane_start
[i
],
751 plane_start
[i
+ 1] - 1024,
752 c
->frame_pred
== PRED_LEFT
);
756 c
->utdsp
.restore_rgb_planes10((uint16_t *)frame
->data
[2], (uint16_t *)frame
->data
[0], (uint16_t *)frame
->data
[1],
757 frame
->linesize
[2] / 2, frame
->linesize
[0] / 2, frame
->linesize
[1] / 2,
758 avctx
->width
, avctx
->height
);
760 case AV_PIX_FMT_YUV420P
:
761 for (i
= 0; i
< 3; i
++) {
762 ret
= decode_plane(c
, i
, frame
->data
[i
], frame
->linesize
[i
],
763 avctx
->width
>> !!i
, avctx
->height
>> !!i
,
764 plane_start
[i
], c
->frame_pred
== PRED_LEFT
);
767 if (c
->frame_pred
== PRED_MEDIAN
) {
768 if (!c
->interlaced
) {
769 restore_median_planar(c
, frame
->data
[i
], frame
->linesize
[i
],
770 avctx
->width
>> !!i
, avctx
->height
>> !!i
,
773 restore_median_planar_il(c
, frame
->data
[i
], frame
->linesize
[i
],
775 avctx
->height
>> !!i
,
778 } else if (c
->frame_pred
== PRED_GRADIENT
) {
779 if (!c
->interlaced
) {
780 restore_gradient_planar(c
, frame
->data
[i
], frame
->linesize
[i
],
781 avctx
->width
>> !!i
, avctx
->height
>> !!i
,
784 restore_gradient_planar_il(c
, frame
->data
[i
], frame
->linesize
[i
],
786 avctx
->height
>> !!i
,
792 case AV_PIX_FMT_YUV422P
:
793 for (i
= 0; i
< 3; i
++) {
794 ret
= decode_plane(c
, i
, frame
->data
[i
], frame
->linesize
[i
],
795 avctx
->width
>> !!i
, avctx
->height
,
796 plane_start
[i
], c
->frame_pred
== PRED_LEFT
);
799 if (c
->frame_pred
== PRED_MEDIAN
) {
800 if (!c
->interlaced
) {
801 restore_median_planar(c
, frame
->data
[i
], frame
->linesize
[i
],
802 avctx
->width
>> !!i
, avctx
->height
,
805 restore_median_planar_il(c
, frame
->data
[i
], frame
->linesize
[i
],
806 avctx
->width
>> !!i
, avctx
->height
,
809 } else if (c
->frame_pred
== PRED_GRADIENT
) {
810 if (!c
->interlaced
) {
811 restore_gradient_planar(c
, frame
->data
[i
], frame
->linesize
[i
],
812 avctx
->width
>> !!i
, avctx
->height
,
815 restore_gradient_planar_il(c
, frame
->data
[i
], frame
->linesize
[i
],
816 avctx
->width
>> !!i
, avctx
->height
,
822 case AV_PIX_FMT_YUV444P
:
823 for (i
= 0; i
< 3; i
++) {
824 ret
= decode_plane(c
, i
, frame
->data
[i
], frame
->linesize
[i
],
825 avctx
->width
, avctx
->height
,
826 plane_start
[i
], c
->frame_pred
== PRED_LEFT
);
829 if (c
->frame_pred
== PRED_MEDIAN
) {
830 if (!c
->interlaced
) {
831 restore_median_planar(c
, frame
->data
[i
], frame
->linesize
[i
],
832 avctx
->width
, avctx
->height
,
835 restore_median_planar_il(c
, frame
->data
[i
], frame
->linesize
[i
],
836 avctx
->width
, avctx
->height
,
839 } else if (c
->frame_pred
== PRED_GRADIENT
) {
840 if (!c
->interlaced
) {
841 restore_gradient_planar(c
, frame
->data
[i
], frame
->linesize
[i
],
842 avctx
->width
, avctx
->height
,
845 restore_gradient_planar_il(c
, frame
->data
[i
], frame
->linesize
[i
],
846 avctx
->width
, avctx
->height
,
852 case AV_PIX_FMT_YUV420P10
:
853 for (i
= 0; i
< 3; i
++) {
854 ret
= decode_plane10(c
, i
, (uint16_t *)frame
->data
[i
], frame
->linesize
[i
] / 2,
855 avctx
->width
>> !!i
, avctx
->height
>> !!i
,
856 plane_start
[i
], plane_start
[i
+ 1] - 1024, c
->frame_pred
== PRED_LEFT
);
861 case AV_PIX_FMT_YUV422P10
:
862 for (i
= 0; i
< 3; i
++) {
863 ret
= decode_plane10(c
, i
, (uint16_t *)frame
->data
[i
], frame
->linesize
[i
] / 2,
864 avctx
->width
>> !!i
, avctx
->height
,
865 plane_start
[i
], plane_start
[i
+ 1] - 1024, c
->frame_pred
== PRED_LEFT
);
872 frame
->key_frame
= 1;
873 frame
->pict_type
= AV_PICTURE_TYPE_I
;
874 frame
->interlaced_frame
= !!c
->interlaced
;
878 /* always report that the buffer was completely consumed */
882 static av_cold
int decode_init(AVCodecContext
*avctx
)
884 UtvideoContext
* const c
= avctx
->priv_data
;
885 int h_shift
, v_shift
;
889 ff_utvideodsp_init(&c
->utdsp
);
890 ff_bswapdsp_init(&c
->bdsp
);
891 ff_llviddsp_init(&c
->llviddsp
);
893 c
->slice_bits_size
= 0;
895 switch (avctx
->codec_tag
) {
896 case MKTAG('U', 'L', 'R', 'G'):
898 avctx
->pix_fmt
= AV_PIX_FMT_GBRP
;
900 case MKTAG('U', 'L', 'R', 'A'):
902 avctx
->pix_fmt
= AV_PIX_FMT_GBRAP
;
904 case MKTAG('U', 'L', 'Y', '0'):
906 avctx
->pix_fmt
= AV_PIX_FMT_YUV420P
;
907 avctx
->colorspace
= AVCOL_SPC_BT470BG
;
909 case MKTAG('U', 'L', 'Y', '2'):
911 avctx
->pix_fmt
= AV_PIX_FMT_YUV422P
;
912 avctx
->colorspace
= AVCOL_SPC_BT470BG
;
914 case MKTAG('U', 'L', 'Y', '4'):
916 avctx
->pix_fmt
= AV_PIX_FMT_YUV444P
;
917 avctx
->colorspace
= AVCOL_SPC_BT470BG
;
919 case MKTAG('U', 'Q', 'Y', '0'):
922 avctx
->pix_fmt
= AV_PIX_FMT_YUV420P10
;
924 case MKTAG('U', 'Q', 'Y', '2'):
927 avctx
->pix_fmt
= AV_PIX_FMT_YUV422P10
;
929 case MKTAG('U', 'Q', 'R', 'G'):
932 avctx
->pix_fmt
= AV_PIX_FMT_GBRP10
;
934 case MKTAG('U', 'Q', 'R', 'A'):
937 avctx
->pix_fmt
= AV_PIX_FMT_GBRAP10
;
939 case MKTAG('U', 'L', 'H', '0'):
941 avctx
->pix_fmt
= AV_PIX_FMT_YUV420P
;
942 avctx
->colorspace
= AVCOL_SPC_BT709
;
944 case MKTAG('U', 'L', 'H', '2'):
946 avctx
->pix_fmt
= AV_PIX_FMT_YUV422P
;
947 avctx
->colorspace
= AVCOL_SPC_BT709
;
949 case MKTAG('U', 'L', 'H', '4'):
951 avctx
->pix_fmt
= AV_PIX_FMT_YUV444P
;
952 avctx
->colorspace
= AVCOL_SPC_BT709
;
954 case MKTAG('U', 'M', 'Y', '2'):
957 avctx
->pix_fmt
= AV_PIX_FMT_YUV422P
;
958 avctx
->colorspace
= AVCOL_SPC_BT470BG
;
960 case MKTAG('U', 'M', 'H', '2'):
963 avctx
->pix_fmt
= AV_PIX_FMT_YUV422P
;
964 avctx
->colorspace
= AVCOL_SPC_BT709
;
966 case MKTAG('U', 'M', 'Y', '4'):
969 avctx
->pix_fmt
= AV_PIX_FMT_YUV444P
;
970 avctx
->colorspace
= AVCOL_SPC_BT470BG
;
972 case MKTAG('U', 'M', 'H', '4'):
975 avctx
->pix_fmt
= AV_PIX_FMT_YUV444P
;
976 avctx
->colorspace
= AVCOL_SPC_BT709
;
978 case MKTAG('U', 'M', 'R', 'G'):
981 avctx
->pix_fmt
= AV_PIX_FMT_GBRP
;
983 case MKTAG('U', 'M', 'R', 'A'):
986 avctx
->pix_fmt
= AV_PIX_FMT_GBRAP
;
989 av_log(avctx
, AV_LOG_ERROR
, "Unknown Ut Video FOURCC provided (%08X)\n",
991 return AVERROR_INVALIDDATA
;
994 av_pix_fmt_get_chroma_sub_sample(avctx
->pix_fmt
, &h_shift
, &v_shift
);
995 if ((avctx
->width
& ((1<<h_shift
)-1)) ||
996 (avctx
->height
& ((1<<v_shift
)-1))) {
997 avpriv_request_sample(avctx
, "Odd dimensions");
998 return AVERROR_PATCHWELCOME
;
1001 if (c
->pack
&& avctx
->extradata_size
>= 16) {
1002 av_log(avctx
, AV_LOG_DEBUG
, "Encoder version %d.%d.%d.%d\n",
1003 avctx
->extradata
[3], avctx
->extradata
[2],
1004 avctx
->extradata
[1], avctx
->extradata
[0]);
1005 av_log(avctx
, AV_LOG_DEBUG
, "Original format %"PRIX32
"\n",
1006 AV_RB32(avctx
->extradata
+ 4));
1007 c
->compression
= avctx
->extradata
[8];
1008 if (c
->compression
!= 2)
1009 avpriv_request_sample(avctx
, "Unknown compression type");
1010 c
->slices
= avctx
->extradata
[9] + 1;
1011 } else if (!c
->pro
&& avctx
->extradata_size
>= 16) {
1012 av_log(avctx
, AV_LOG_DEBUG
, "Encoder version %d.%d.%d.%d\n",
1013 avctx
->extradata
[3], avctx
->extradata
[2],
1014 avctx
->extradata
[1], avctx
->extradata
[0]);
1015 av_log(avctx
, AV_LOG_DEBUG
, "Original format %"PRIX32
"\n",
1016 AV_RB32(avctx
->extradata
+ 4));
1017 c
->frame_info_size
= AV_RL32(avctx
->extradata
+ 8);
1018 c
->flags
= AV_RL32(avctx
->extradata
+ 12);
1020 if (c
->frame_info_size
!= 4)
1021 avpriv_request_sample(avctx
, "Frame info not 4 bytes");
1022 av_log(avctx
, AV_LOG_DEBUG
, "Encoding parameters %08"PRIX32
"\n", c
->flags
);
1023 c
->slices
= (c
->flags
>> 24) + 1;
1024 c
->compression
= c
->flags
& 1;
1025 c
->interlaced
= c
->flags
& 0x800;
1026 } else if (c
->pro
&& avctx
->extradata_size
== 8) {
1027 av_log(avctx
, AV_LOG_DEBUG
, "Encoder version %d.%d.%d.%d\n",
1028 avctx
->extradata
[3], avctx
->extradata
[2],
1029 avctx
->extradata
[1], avctx
->extradata
[0]);
1030 av_log(avctx
, AV_LOG_DEBUG
, "Original format %"PRIX32
"\n",
1031 AV_RB32(avctx
->extradata
+ 4));
1033 c
->frame_info_size
= 4;
1035 av_log(avctx
, AV_LOG_ERROR
,
1036 "Insufficient extradata size %d, should be at least 16\n",
1037 avctx
->extradata_size
);
1038 return AVERROR_INVALIDDATA
;
1044 static av_cold
int decode_end(AVCodecContext
*avctx
)
1046 UtvideoContext
* const c
= avctx
->priv_data
;
1048 av_freep(&c
->slice_bits
);
1053 const FFCodec ff_utvideo_decoder
= {
1054 .p
.name
= "utvideo",
1055 .p
.long_name
= NULL_IF_CONFIG_SMALL("Ut Video"),
1056 .p
.type
= AVMEDIA_TYPE_VIDEO
,
1057 .p
.id
= AV_CODEC_ID_UTVIDEO
,
1058 .priv_data_size
= sizeof(UtvideoContext
),
1059 .init
= decode_init
,
1060 .close
= decode_end
,
1061 FF_CODEC_DECODE_CB(decode_frame
),
1062 .p
.capabilities
= AV_CODEC_CAP_DR1
| AV_CODEC_CAP_FRAME_THREADS
,
1063 .caps_internal
= FF_CODEC_CAP_INIT_THREADSAFE
,