]> git.sesse.net Git - ffmpeg/blob - libavcodec/utvideodec.c
fate: add 12 bit framerate filter tests
[ffmpeg] / libavcodec / utvideodec.c
1 /*
2  * Ut Video decoder
3  * Copyright (c) 2011 Konstantin Shishkov
4  *
5  * This file is part of FFmpeg.
6  *
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.
11  *
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.
16  *
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
20  */
21
22 /**
23  * @file
24  * Ut Video decoder
25  */
26
27 #include <inttypes.h>
28 #include <stdlib.h>
29
30 #define UNCHECKED_BITSTREAM_READER 1
31
32 #include "libavutil/intreadwrite.h"
33 #include "avcodec.h"
34 #include "bswapdsp.h"
35 #include "bytestream.h"
36 #include "get_bits.h"
37 #include "internal.h"
38 #include "thread.h"
39 #include "utvideo.h"
40
41 static int build_huff10(const uint8_t *src, VLC *vlc, int *fsym)
42 {
43     int i;
44     HuffEntry he[1024];
45     int last;
46     uint32_t codes[1024];
47     uint8_t bits[1024];
48     uint16_t syms[1024];
49     uint32_t code;
50
51     *fsym = -1;
52     for (i = 0; i < 1024; i++) {
53         he[i].sym = i;
54         he[i].len = *src++;
55     }
56     qsort(he, 1024, sizeof(*he), ff_ut10_huff_cmp_len);
57
58     if (!he[0].len) {
59         *fsym = he[0].sym;
60         return 0;
61     }
62
63     last = 1023;
64     while (he[last].len == 255 && last)
65         last--;
66
67     if (he[last].len > 32) {
68         return -1;
69     }
70
71     code = 1;
72     for (i = last; i >= 0; i--) {
73         codes[i] = code >> (32 - he[i].len);
74         bits[i]  = he[i].len;
75         syms[i]  = he[i].sym;
76         code += 0x80000000u >> (he[i].len - 1);
77     }
78 #define VLC_BITS 11
79     return ff_init_vlc_sparse(vlc, VLC_BITS, last + 1,
80                               bits,  sizeof(*bits),  sizeof(*bits),
81                               codes, sizeof(*codes), sizeof(*codes),
82                               syms,  sizeof(*syms),  sizeof(*syms), 0);
83 }
84
85 static int build_huff(const uint8_t *src, VLC *vlc, int *fsym)
86 {
87     int i;
88     HuffEntry he[256];
89     int last;
90     uint32_t codes[256];
91     uint8_t bits[256];
92     uint8_t syms[256];
93     uint32_t code;
94
95     *fsym = -1;
96     for (i = 0; i < 256; i++) {
97         he[i].sym = i;
98         he[i].len = *src++;
99     }
100     qsort(he, 256, sizeof(*he), ff_ut_huff_cmp_len);
101
102     if (!he[0].len) {
103         *fsym = he[0].sym;
104         return 0;
105     }
106
107     last = 255;
108     while (he[last].len == 255 && last)
109         last--;
110
111     if (he[last].len > 32)
112         return -1;
113
114     code = 1;
115     for (i = last; i >= 0; i--) {
116         codes[i] = code >> (32 - he[i].len);
117         bits[i]  = he[i].len;
118         syms[i]  = he[i].sym;
119         code += 0x80000000u >> (he[i].len - 1);
120     }
121
122     return ff_init_vlc_sparse(vlc, VLC_BITS, last + 1,
123                               bits,  sizeof(*bits),  sizeof(*bits),
124                               codes, sizeof(*codes), sizeof(*codes),
125                               syms,  sizeof(*syms),  sizeof(*syms), 0);
126 }
127
128 static int decode_plane10(UtvideoContext *c, int plane_no,
129                           uint16_t *dst, ptrdiff_t stride,
130                           int width, int height,
131                           const uint8_t *src, const uint8_t *huff,
132                           int use_pred)
133 {
134     int i, j, slice, pix, ret;
135     int sstart, send;
136     VLC vlc;
137     GetBitContext gb;
138     int prev, fsym;
139
140     if ((ret = build_huff10(huff, &vlc, &fsym)) < 0) {
141         av_log(c->avctx, AV_LOG_ERROR, "Cannot build Huffman codes\n");
142         return ret;
143     }
144     if (fsym >= 0) { // build_huff reported a symbol to fill slices with
145         send = 0;
146         for (slice = 0; slice < c->slices; slice++) {
147             uint16_t *dest;
148
149             sstart = send;
150             send   = (height * (slice + 1) / c->slices);
151             dest   = dst + sstart * stride;
152
153             prev = 0x200;
154             for (j = sstart; j < send; j++) {
155                 for (i = 0; i < width; i++) {
156                     pix = fsym;
157                     if (use_pred) {
158                         prev += pix;
159                         prev &= 0x3FF;
160                         pix   = prev;
161                     }
162                     dest[i] = pix;
163                 }
164                 dest += stride;
165             }
166         }
167         return 0;
168     }
169
170     send = 0;
171     for (slice = 0; slice < c->slices; slice++) {
172         uint16_t *dest;
173         int slice_data_start, slice_data_end, slice_size;
174
175         sstart = send;
176         send   = (height * (slice + 1) / c->slices);
177         dest   = dst + sstart * stride;
178
179         // slice offset and size validation was done earlier
180         slice_data_start = slice ? AV_RL32(src + slice * 4 - 4) : 0;
181         slice_data_end   = AV_RL32(src + slice * 4);
182         slice_size       = slice_data_end - slice_data_start;
183
184         if (!slice_size) {
185             av_log(c->avctx, AV_LOG_ERROR, "Plane has more than one symbol "
186                    "yet a slice has a length of zero.\n");
187             goto fail;
188         }
189
190         memset(c->slice_bits + slice_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
191         c->bdsp.bswap_buf((uint32_t *) c->slice_bits,
192                           (uint32_t *)(src + slice_data_start + c->slices * 4),
193                           (slice_data_end - slice_data_start + 3) >> 2);
194         init_get_bits(&gb, c->slice_bits, slice_size * 8);
195
196         prev = 0x200;
197         for (j = sstart; j < send; j++) {
198             for (i = 0; i < width; i++) {
199                 pix = get_vlc2(&gb, vlc.table, VLC_BITS, 3);
200                 if (pix < 0) {
201                     av_log(c->avctx, AV_LOG_ERROR, "Decoding error\n");
202                     goto fail;
203                 }
204                 if (use_pred) {
205                     prev += pix;
206                     prev &= 0x3FF;
207                     pix   = prev;
208                 }
209                 dest[i] = pix;
210             }
211             dest += stride;
212             if (get_bits_left(&gb) < 0) {
213                 av_log(c->avctx, AV_LOG_ERROR,
214                         "Slice decoding ran out of bits\n");
215                 goto fail;
216             }
217         }
218         if (get_bits_left(&gb) > 32)
219             av_log(c->avctx, AV_LOG_WARNING,
220                    "%d bits left after decoding slice\n", get_bits_left(&gb));
221     }
222
223     ff_free_vlc(&vlc);
224
225     return 0;
226 fail:
227     ff_free_vlc(&vlc);
228     return AVERROR_INVALIDDATA;
229 }
230
231 static int compute_cmask(int plane_no, int interlaced, enum AVPixelFormat pix_fmt)
232 {
233     const int is_luma = (pix_fmt == AV_PIX_FMT_YUV420P) && !plane_no;
234
235     if (interlaced)
236         return ~(1 + 2 * is_luma);
237
238     return ~is_luma;
239 }
240
241 static int decode_plane(UtvideoContext *c, int plane_no,
242                         uint8_t *dst, ptrdiff_t stride,
243                         int width, int height,
244                         const uint8_t *src, int use_pred)
245 {
246     int i, j, slice, pix;
247     int sstart, send;
248     VLC vlc;
249     GetBitContext gb;
250     int prev, fsym;
251     const int cmask = compute_cmask(plane_no, c->interlaced, c->avctx->pix_fmt);
252
253     if (build_huff(src, &vlc, &fsym)) {
254         av_log(c->avctx, AV_LOG_ERROR, "Cannot build Huffman codes\n");
255         return AVERROR_INVALIDDATA;
256     }
257     if (fsym >= 0) { // build_huff reported a symbol to fill slices with
258         send = 0;
259         for (slice = 0; slice < c->slices; slice++) {
260             uint8_t *dest;
261
262             sstart = send;
263             send   = (height * (slice + 1) / c->slices) & cmask;
264             dest   = dst + sstart * stride;
265
266             prev = 0x80;
267             for (j = sstart; j < send; j++) {
268                 for (i = 0; i < width; i++) {
269                     pix = fsym;
270                     if (use_pred) {
271                         prev += pix;
272                         pix   = prev;
273                     }
274                     dest[i] = pix;
275                 }
276                 dest += stride;
277             }
278         }
279         return 0;
280     }
281
282     src      += 256;
283
284     send = 0;
285     for (slice = 0; slice < c->slices; slice++) {
286         uint8_t *dest;
287         int slice_data_start, slice_data_end, slice_size;
288
289         sstart = send;
290         send   = (height * (slice + 1) / c->slices) & cmask;
291         dest   = dst + sstart * stride;
292
293         // slice offset and size validation was done earlier
294         slice_data_start = slice ? AV_RL32(src + slice * 4 - 4) : 0;
295         slice_data_end   = AV_RL32(src + slice * 4);
296         slice_size       = slice_data_end - slice_data_start;
297
298         if (!slice_size) {
299             av_log(c->avctx, AV_LOG_ERROR, "Plane has more than one symbol "
300                    "yet a slice has a length of zero.\n");
301             goto fail;
302         }
303
304         memset(c->slice_bits + slice_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
305         c->bdsp.bswap_buf((uint32_t *) c->slice_bits,
306                           (uint32_t *)(src + slice_data_start + c->slices * 4),
307                           (slice_data_end - slice_data_start + 3) >> 2);
308         init_get_bits(&gb, c->slice_bits, slice_size * 8);
309
310         prev = 0x80;
311         for (j = sstart; j < send; j++) {
312             for (i = 0; i < width; i++) {
313                 pix = get_vlc2(&gb, vlc.table, VLC_BITS, 3);
314                 if (pix < 0) {
315                     av_log(c->avctx, AV_LOG_ERROR, "Decoding error\n");
316                     goto fail;
317                 }
318                 if (use_pred) {
319                     prev += pix;
320                     pix   = prev;
321                 }
322                 dest[i] = pix;
323             }
324             if (get_bits_left(&gb) < 0) {
325                 av_log(c->avctx, AV_LOG_ERROR,
326                         "Slice decoding ran out of bits\n");
327                 goto fail;
328             }
329             dest += stride;
330         }
331         if (get_bits_left(&gb) > 32)
332             av_log(c->avctx, AV_LOG_WARNING,
333                    "%d bits left after decoding slice\n", get_bits_left(&gb));
334     }
335
336     ff_free_vlc(&vlc);
337
338     return 0;
339 fail:
340     ff_free_vlc(&vlc);
341     return AVERROR_INVALIDDATA;
342 }
343
344 #undef A
345 #undef B
346 #undef C
347
348 static void restore_median_planar(UtvideoContext *c, uint8_t *src, ptrdiff_t stride,
349                                   int width, int height, int slices, int rmode)
350 {
351     int i, j, slice;
352     int A, B, C;
353     uint8_t *bsrc;
354     int slice_start, slice_height;
355     const int cmask = ~rmode;
356
357     for (slice = 0; slice < slices; slice++) {
358         slice_start  = ((slice * height) / slices) & cmask;
359         slice_height = ((((slice + 1) * height) / slices) & cmask) -
360                        slice_start;
361
362         if (!slice_height)
363             continue;
364         bsrc = src + slice_start * stride;
365
366         // first line - left neighbour prediction
367         bsrc[0] += 0x80;
368         c->llviddsp.add_left_pred(bsrc, bsrc, width, 0);
369         bsrc += stride;
370         if (slice_height <= 1)
371             continue;
372         // second line - first element has top prediction, the rest uses median
373         C        = bsrc[-stride];
374         bsrc[0] += C;
375         A        = bsrc[0];
376         for (i = 1; i < FFMIN(width, 16); i++) { /* scalar loop (DSP need align 16) */
377             B        = bsrc[i - stride];
378             bsrc[i] += mid_pred(A, B, (uint8_t)(A + B - C));
379             C        = B;
380             A        = bsrc[i];
381         }
382         if (width > 16)
383             c->llviddsp.add_median_pred(bsrc + 16, bsrc - stride + 16,
384                                         bsrc + 16, width - 16, &A, &B);
385
386         bsrc += stride;
387         // the rest of lines use continuous median prediction
388         for (j = 2; j < slice_height; j++) {
389             c->llviddsp.add_median_pred(bsrc, bsrc - stride,
390                                             bsrc, width, &A, &B);
391             bsrc += stride;
392         }
393     }
394 }
395
396 /* UtVideo interlaced mode treats every two lines as a single one,
397  * so restoring function should take care of possible padding between
398  * two parts of the same "line".
399  */
400 static void restore_median_planar_il(UtvideoContext *c, uint8_t *src, ptrdiff_t stride,
401                                      int width, int height, int slices, int rmode)
402 {
403     int i, j, slice;
404     int A, B, C;
405     uint8_t *bsrc;
406     int slice_start, slice_height;
407     const int cmask   = ~(rmode ? 3 : 1);
408     const ptrdiff_t stride2 = stride << 1;
409
410     for (slice = 0; slice < slices; slice++) {
411         slice_start    = ((slice * height) / slices) & cmask;
412         slice_height   = ((((slice + 1) * height) / slices) & cmask) -
413                          slice_start;
414         slice_height >>= 1;
415         if (!slice_height)
416             continue;
417
418         bsrc = src + slice_start * stride;
419
420         // first line - left neighbour prediction
421         bsrc[0] += 0x80;
422         A = c->llviddsp.add_left_pred(bsrc, bsrc, width, 0);
423         c->llviddsp.add_left_pred(bsrc + stride, bsrc + stride, width, A);
424         bsrc += stride2;
425         if (slice_height <= 1)
426             continue;
427         // second line - first element has top prediction, the rest uses median
428         C        = bsrc[-stride2];
429         bsrc[0] += C;
430         A        = bsrc[0];
431         for (i = 1; i < FFMIN(width, 16); i++) { /* scalar loop (DSP need align 16) */
432             B        = bsrc[i - stride2];
433             bsrc[i] += mid_pred(A, B, (uint8_t)(A + B - C));
434             C        = B;
435             A        = bsrc[i];
436         }
437         if (width > 16)
438             c->llviddsp.add_median_pred(bsrc + 16, bsrc - stride2 + 16,
439                                         bsrc + 16, width - 16, &A, &B);
440
441         c->llviddsp.add_median_pred(bsrc + stride, bsrc - stride,
442                                         bsrc + stride, width, &A, &B);
443         bsrc += stride2;
444         // the rest of lines use continuous median prediction
445         for (j = 2; j < slice_height; j++) {
446             c->llviddsp.add_median_pred(bsrc, bsrc - stride2,
447                                             bsrc, width, &A, &B);
448             c->llviddsp.add_median_pred(bsrc + stride, bsrc - stride,
449                                             bsrc + stride, width, &A, &B);
450             bsrc += stride2;
451         }
452     }
453 }
454
455 static void restore_gradient_planar(UtvideoContext *c, uint8_t *src, ptrdiff_t stride,
456                                     int width, int height, int slices, int rmode)
457 {
458     int i, j, slice;
459     int A, B, C;
460     uint8_t *bsrc;
461     int slice_start, slice_height;
462     const int cmask = ~rmode;
463     int min_width = FFMIN(width, 32);
464
465     for (slice = 0; slice < slices; slice++) {
466         slice_start  = ((slice * height) / slices) & cmask;
467         slice_height = ((((slice + 1) * height) / slices) & cmask) -
468                        slice_start;
469
470         if (!slice_height)
471             continue;
472         bsrc = src + slice_start * stride;
473
474         // first line - left neighbour prediction
475         bsrc[0] += 0x80;
476         c->llviddsp.add_left_pred(bsrc, bsrc, width, 0);
477         bsrc += stride;
478         if (slice_height <= 1)
479             continue;
480         for (j = 1; j < slice_height; j++) {
481             // second line - first element has top prediction, the rest uses gradient
482             bsrc[0] = (bsrc[0] + bsrc[-stride]) & 0xFF;
483             for (i = 1; i < min_width; i++) { /* dsp need align 32 */
484                 A = bsrc[i - stride];
485                 B = bsrc[i - (stride + 1)];
486                 C = bsrc[i - 1];
487                 bsrc[i] = (A - B + C + bsrc[i]) & 0xFF;
488             }
489             if (width > 32)
490                 c->llviddsp.add_gradient_pred(bsrc + 32, stride, width - 32);
491             bsrc += stride;
492         }
493     }
494 }
495
496 static void restore_gradient_planar_il(UtvideoContext *c, uint8_t *src, ptrdiff_t stride,
497                                       int width, int height, int slices, int rmode)
498 {
499     int i, j, slice;
500     int A, B, C;
501     uint8_t *bsrc;
502     int slice_start, slice_height;
503     const int cmask   = ~(rmode ? 3 : 1);
504     const ptrdiff_t stride2 = stride << 1;
505     int min_width = FFMIN(width, 32);
506
507     for (slice = 0; slice < slices; slice++) {
508         slice_start    = ((slice * height) / slices) & cmask;
509         slice_height   = ((((slice + 1) * height) / slices) & cmask) -
510                          slice_start;
511         slice_height >>= 1;
512         if (!slice_height)
513             continue;
514
515         bsrc = src + slice_start * stride;
516
517         // first line - left neighbour prediction
518         bsrc[0] += 0x80;
519         A = c->llviddsp.add_left_pred(bsrc, bsrc, width, 0);
520         c->llviddsp.add_left_pred(bsrc + stride, bsrc + stride, width, A);
521         bsrc += stride2;
522         if (slice_height <= 1)
523             continue;
524         for (j = 1; j < slice_height; j++) {
525             // second line - first element has top prediction, the rest uses gradient
526             bsrc[0] = (bsrc[0] + bsrc[-stride2]) & 0xFF;
527             for (i = 1; i < min_width; i++) { /* dsp need align 32 */
528                 A = bsrc[i - stride2];
529                 B = bsrc[i - (stride2 + 1)];
530                 C = bsrc[i - 1];
531                 bsrc[i] = (A - B + C + bsrc[i]) & 0xFF;
532             }
533             if (width > 32)
534                 c->llviddsp.add_gradient_pred(bsrc + 32, stride2, width - 32);
535
536             A = bsrc[-stride];
537             B = bsrc[-(1 + stride + stride - width)];
538             C = bsrc[width - 1];
539             bsrc[stride] = (A - B + C + bsrc[stride]) & 0xFF;
540             for (i = 1; i < width; i++) {
541                 A = bsrc[i - stride];
542                 B = bsrc[i - (1 + stride)];
543                 C = bsrc[i - 1 + stride];
544                 bsrc[i + stride] = (A - B + C + bsrc[i + stride]) & 0xFF;
545             }
546             bsrc += stride2;
547         }
548     }
549 }
550
551 static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
552                         AVPacket *avpkt)
553 {
554     const uint8_t *buf = avpkt->data;
555     int buf_size = avpkt->size;
556     UtvideoContext *c = avctx->priv_data;
557     int i, j;
558     const uint8_t *plane_start[5];
559     int plane_size, max_slice_size = 0, slice_start, slice_end, slice_size;
560     int ret;
561     GetByteContext gb;
562     ThreadFrame frame = { .f = data };
563
564     if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
565         return ret;
566
567     /* parse plane structure to get frame flags and validate slice offsets */
568     bytestream2_init(&gb, buf, buf_size);
569     if (c->pro) {
570         if (bytestream2_get_bytes_left(&gb) < c->frame_info_size) {
571             av_log(avctx, AV_LOG_ERROR, "Not enough data for frame information\n");
572             return AVERROR_INVALIDDATA;
573         }
574         c->frame_info = bytestream2_get_le32u(&gb);
575         c->slices = ((c->frame_info >> 16) & 0xff) + 1;
576         for (i = 0; i < c->planes; i++) {
577             plane_start[i] = gb.buffer;
578             if (bytestream2_get_bytes_left(&gb) < 1024 + 4 * c->slices) {
579                 av_log(avctx, AV_LOG_ERROR, "Insufficient data for a plane\n");
580                 return AVERROR_INVALIDDATA;
581             }
582             slice_start = 0;
583             slice_end   = 0;
584             for (j = 0; j < c->slices; j++) {
585                 slice_end   = bytestream2_get_le32u(&gb);
586                 if (slice_end < 0 || slice_end < slice_start ||
587                     bytestream2_get_bytes_left(&gb) < slice_end) {
588                     av_log(avctx, AV_LOG_ERROR, "Incorrect slice size\n");
589                     return AVERROR_INVALIDDATA;
590                 }
591                 slice_size  = slice_end - slice_start;
592                 slice_start = slice_end;
593                 max_slice_size = FFMAX(max_slice_size, slice_size);
594             }
595             plane_size = slice_end;
596             bytestream2_skipu(&gb, plane_size);
597             bytestream2_skipu(&gb, 1024);
598         }
599         plane_start[c->planes] = gb.buffer;
600     } else {
601         for (i = 0; i < c->planes; i++) {
602             plane_start[i] = gb.buffer;
603             if (bytestream2_get_bytes_left(&gb) < 256 + 4 * c->slices) {
604                 av_log(avctx, AV_LOG_ERROR, "Insufficient data for a plane\n");
605                 return AVERROR_INVALIDDATA;
606             }
607             bytestream2_skipu(&gb, 256);
608             slice_start = 0;
609             slice_end   = 0;
610             for (j = 0; j < c->slices; j++) {
611                 slice_end   = bytestream2_get_le32u(&gb);
612                 if (slice_end < 0 || slice_end < slice_start ||
613                     bytestream2_get_bytes_left(&gb) < slice_end) {
614                     av_log(avctx, AV_LOG_ERROR, "Incorrect slice size\n");
615                     return AVERROR_INVALIDDATA;
616                 }
617                 slice_size  = slice_end - slice_start;
618                 slice_start = slice_end;
619                 max_slice_size = FFMAX(max_slice_size, slice_size);
620             }
621             plane_size = slice_end;
622             bytestream2_skipu(&gb, plane_size);
623         }
624         plane_start[c->planes] = gb.buffer;
625         if (bytestream2_get_bytes_left(&gb) < c->frame_info_size) {
626             av_log(avctx, AV_LOG_ERROR, "Not enough data for frame information\n");
627             return AVERROR_INVALIDDATA;
628         }
629         c->frame_info = bytestream2_get_le32u(&gb);
630     }
631     av_log(avctx, AV_LOG_DEBUG, "frame information flags %"PRIX32"\n",
632            c->frame_info);
633
634     c->frame_pred = (c->frame_info >> 8) & 3;
635
636     max_slice_size += 4*avctx->width;
637
638     av_fast_malloc(&c->slice_bits, &c->slice_bits_size,
639                    max_slice_size + AV_INPUT_BUFFER_PADDING_SIZE);
640
641     if (!c->slice_bits) {
642         av_log(avctx, AV_LOG_ERROR, "Cannot allocate temporary buffer\n");
643         return AVERROR(ENOMEM);
644     }
645
646     switch (c->avctx->pix_fmt) {
647     case AV_PIX_FMT_GBRP:
648     case AV_PIX_FMT_GBRAP:
649         for (i = 0; i < c->planes; i++) {
650             ret = decode_plane(c, i, frame.f->data[i],
651                                frame.f->linesize[i], avctx->width,
652                                avctx->height, plane_start[i],
653                                c->frame_pred == PRED_LEFT);
654             if (ret)
655                 return ret;
656             if (c->frame_pred == PRED_MEDIAN) {
657                 if (!c->interlaced) {
658                     restore_median_planar(c, frame.f->data[i],
659                                           frame.f->linesize[i], avctx->width,
660                                           avctx->height, c->slices, 0);
661                 } else {
662                     restore_median_planar_il(c, frame.f->data[i],
663                                              frame.f->linesize[i],
664                                              avctx->width, avctx->height, c->slices,
665                                              0);
666                 }
667             } else if (c->frame_pred == PRED_GRADIENT) {
668                 if (!c->interlaced) {
669                     restore_gradient_planar(c, frame.f->data[i],
670                                             frame.f->linesize[i], avctx->width,
671                                             avctx->height, c->slices, 0);
672                 } else {
673                     restore_gradient_planar_il(c, frame.f->data[i],
674                                                frame.f->linesize[i],
675                                                avctx->width, avctx->height, c->slices,
676                                                0);
677                 }
678             }
679         }
680         c->utdsp.restore_rgb_planes(frame.f->data[2], frame.f->data[0], frame.f->data[1],
681                                     frame.f->linesize[2], frame.f->linesize[0], frame.f->linesize[1],
682                                     avctx->width, avctx->height);
683         break;
684     case AV_PIX_FMT_GBRAP10:
685     case AV_PIX_FMT_GBRP10:
686         for (i = 0; i < c->planes; i++) {
687             ret = decode_plane10(c, i, (uint16_t *)frame.f->data[i],
688                                  frame.f->linesize[i] / 2, avctx->width,
689                                  avctx->height, plane_start[i],
690                                  plane_start[i + 1] - 1024,
691                                  c->frame_pred == PRED_LEFT);
692             if (ret)
693                 return ret;
694         }
695         c->utdsp.restore_rgb_planes10((uint16_t *)frame.f->data[2], (uint16_t *)frame.f->data[0], (uint16_t *)frame.f->data[1],
696                                       frame.f->linesize[2] / 2, frame.f->linesize[0] / 2, frame.f->linesize[1] / 2,
697                                       avctx->width, avctx->height);
698         break;
699     case AV_PIX_FMT_YUV420P:
700         for (i = 0; i < 3; i++) {
701             ret = decode_plane(c, i, frame.f->data[i], frame.f->linesize[i],
702                                avctx->width >> !!i, avctx->height >> !!i,
703                                plane_start[i], c->frame_pred == PRED_LEFT);
704             if (ret)
705                 return ret;
706             if (c->frame_pred == PRED_MEDIAN) {
707                 if (!c->interlaced) {
708                     restore_median_planar(c, frame.f->data[i], frame.f->linesize[i],
709                                           avctx->width >> !!i, avctx->height >> !!i,
710                                           c->slices, !i);
711                 } else {
712                     restore_median_planar_il(c, frame.f->data[i], frame.f->linesize[i],
713                                              avctx->width  >> !!i,
714                                              avctx->height >> !!i,
715                                              c->slices, !i);
716                 }
717             } else if (c->frame_pred == PRED_GRADIENT) {
718                 if (!c->interlaced) {
719                     restore_gradient_planar(c, frame.f->data[i], frame.f->linesize[i],
720                                             avctx->width >> !!i, avctx->height >> !!i,
721                                             c->slices, !i);
722                 } else {
723                     restore_gradient_planar_il(c, frame.f->data[i], frame.f->linesize[i],
724                                                avctx->width  >> !!i,
725                                                avctx->height >> !!i,
726                                                c->slices, !i);
727                 }
728             }
729         }
730         break;
731     case AV_PIX_FMT_YUV422P:
732         for (i = 0; i < 3; i++) {
733             ret = decode_plane(c, i, frame.f->data[i], frame.f->linesize[i],
734                                avctx->width >> !!i, avctx->height,
735                                plane_start[i], c->frame_pred == PRED_LEFT);
736             if (ret)
737                 return ret;
738             if (c->frame_pred == PRED_MEDIAN) {
739                 if (!c->interlaced) {
740                     restore_median_planar(c, frame.f->data[i], frame.f->linesize[i],
741                                           avctx->width >> !!i, avctx->height,
742                                           c->slices, 0);
743                 } else {
744                     restore_median_planar_il(c, frame.f->data[i], frame.f->linesize[i],
745                                              avctx->width >> !!i, avctx->height,
746                                              c->slices, 0);
747                 }
748             } else if (c->frame_pred == PRED_GRADIENT) {
749                 if (!c->interlaced) {
750                     restore_gradient_planar(c, frame.f->data[i], frame.f->linesize[i],
751                                             avctx->width >> !!i, avctx->height,
752                                             c->slices, 0);
753                 } else {
754                     restore_gradient_planar_il(c, frame.f->data[i], frame.f->linesize[i],
755                                                avctx->width  >> !!i, avctx->height,
756                                                c->slices, 0);
757                 }
758             }
759         }
760         break;
761     case AV_PIX_FMT_YUV444P:
762         for (i = 0; i < 3; i++) {
763             ret = decode_plane(c, i, frame.f->data[i], frame.f->linesize[i],
764                                avctx->width, avctx->height,
765                                plane_start[i], c->frame_pred == PRED_LEFT);
766             if (ret)
767                 return ret;
768             if (c->frame_pred == PRED_MEDIAN) {
769                 if (!c->interlaced) {
770                     restore_median_planar(c, frame.f->data[i], frame.f->linesize[i],
771                                           avctx->width, avctx->height,
772                                           c->slices, 0);
773                 } else {
774                     restore_median_planar_il(c, frame.f->data[i], frame.f->linesize[i],
775                                              avctx->width, avctx->height,
776                                              c->slices, 0);
777                 }
778             } else if (c->frame_pred == PRED_GRADIENT) {
779                 if (!c->interlaced) {
780                     restore_gradient_planar(c, frame.f->data[i], frame.f->linesize[i],
781                                             avctx->width, avctx->height,
782                                             c->slices, 0);
783                 } else {
784                     restore_gradient_planar_il(c, frame.f->data[i], frame.f->linesize[i],
785                                                avctx->width, avctx->height,
786                                                c->slices, 0);
787                 }
788             }
789         }
790         break;
791     case AV_PIX_FMT_YUV422P10:
792         for (i = 0; i < 3; i++) {
793             ret = decode_plane10(c, i, (uint16_t *)frame.f->data[i], frame.f->linesize[i] / 2,
794                                  avctx->width >> !!i, avctx->height,
795                                  plane_start[i], plane_start[i + 1] - 1024, c->frame_pred == PRED_LEFT);
796             if (ret)
797                 return ret;
798         }
799         break;
800     }
801
802     frame.f->key_frame = 1;
803     frame.f->pict_type = AV_PICTURE_TYPE_I;
804     frame.f->interlaced_frame = !!c->interlaced;
805
806     *got_frame = 1;
807
808     /* always report that the buffer was completely consumed */
809     return buf_size;
810 }
811
812 static av_cold int decode_init(AVCodecContext *avctx)
813 {
814     UtvideoContext * const c = avctx->priv_data;
815
816     c->avctx = avctx;
817
818     ff_utvideodsp_init(&c->utdsp);
819     ff_bswapdsp_init(&c->bdsp);
820     ff_llviddsp_init(&c->llviddsp);
821
822     if (avctx->extradata_size >= 16) {
823         av_log(avctx, AV_LOG_DEBUG, "Encoder version %d.%d.%d.%d\n",
824                avctx->extradata[3], avctx->extradata[2],
825                avctx->extradata[1], avctx->extradata[0]);
826         av_log(avctx, AV_LOG_DEBUG, "Original format %"PRIX32"\n",
827                AV_RB32(avctx->extradata + 4));
828         c->frame_info_size = AV_RL32(avctx->extradata + 8);
829         c->flags           = AV_RL32(avctx->extradata + 12);
830
831         if (c->frame_info_size != 4)
832             avpriv_request_sample(avctx, "Frame info not 4 bytes");
833         av_log(avctx, AV_LOG_DEBUG, "Encoding parameters %08"PRIX32"\n", c->flags);
834         c->slices      = (c->flags >> 24) + 1;
835         c->compression = c->flags & 1;
836         c->interlaced  = c->flags & 0x800;
837     } else if (avctx->extradata_size == 8) {
838         av_log(avctx, AV_LOG_DEBUG, "Encoder version %d.%d.%d.%d\n",
839                avctx->extradata[3], avctx->extradata[2],
840                avctx->extradata[1], avctx->extradata[0]);
841         av_log(avctx, AV_LOG_DEBUG, "Original format %"PRIX32"\n",
842                AV_RB32(avctx->extradata + 4));
843         c->interlaced  = 0;
844         c->pro         = 1;
845         c->frame_info_size = 4;
846     } else {
847         av_log(avctx, AV_LOG_ERROR,
848                "Insufficient extradata size %d, should be at least 16\n",
849                avctx->extradata_size);
850         return AVERROR_INVALIDDATA;
851     }
852
853     c->slice_bits_size = 0;
854
855     switch (avctx->codec_tag) {
856     case MKTAG('U', 'L', 'R', 'G'):
857         c->planes      = 3;
858         avctx->pix_fmt = AV_PIX_FMT_GBRP;
859         break;
860     case MKTAG('U', 'L', 'R', 'A'):
861         c->planes      = 4;
862         avctx->pix_fmt = AV_PIX_FMT_GBRAP;
863         break;
864     case MKTAG('U', 'L', 'Y', '0'):
865         c->planes      = 3;
866         avctx->pix_fmt = AV_PIX_FMT_YUV420P;
867         avctx->colorspace = AVCOL_SPC_BT470BG;
868         break;
869     case MKTAG('U', 'L', 'Y', '2'):
870         c->planes      = 3;
871         avctx->pix_fmt = AV_PIX_FMT_YUV422P;
872         avctx->colorspace = AVCOL_SPC_BT470BG;
873         break;
874     case MKTAG('U', 'L', 'Y', '4'):
875         c->planes      = 3;
876         avctx->pix_fmt = AV_PIX_FMT_YUV444P;
877         avctx->colorspace = AVCOL_SPC_BT470BG;
878         break;
879     case MKTAG('U', 'Q', 'Y', '2'):
880         c->planes      = 3;
881         avctx->pix_fmt = AV_PIX_FMT_YUV422P10;
882         break;
883     case MKTAG('U', 'Q', 'R', 'G'):
884         c->planes      = 3;
885         avctx->pix_fmt = AV_PIX_FMT_GBRP10;
886         break;
887     case MKTAG('U', 'Q', 'R', 'A'):
888         c->planes      = 4;
889         avctx->pix_fmt = AV_PIX_FMT_GBRAP10;
890         break;
891     case MKTAG('U', 'L', 'H', '0'):
892         c->planes      = 3;
893         avctx->pix_fmt = AV_PIX_FMT_YUV420P;
894         avctx->colorspace = AVCOL_SPC_BT709;
895         break;
896     case MKTAG('U', 'L', 'H', '2'):
897         c->planes      = 3;
898         avctx->pix_fmt = AV_PIX_FMT_YUV422P;
899         avctx->colorspace = AVCOL_SPC_BT709;
900         break;
901     case MKTAG('U', 'L', 'H', '4'):
902         c->planes      = 3;
903         avctx->pix_fmt = AV_PIX_FMT_YUV444P;
904         avctx->colorspace = AVCOL_SPC_BT709;
905         break;
906     default:
907         av_log(avctx, AV_LOG_ERROR, "Unknown Ut Video FOURCC provided (%08X)\n",
908                avctx->codec_tag);
909         return AVERROR_INVALIDDATA;
910     }
911
912     return 0;
913 }
914
915 static av_cold int decode_end(AVCodecContext *avctx)
916 {
917     UtvideoContext * const c = avctx->priv_data;
918
919     av_freep(&c->slice_bits);
920
921     return 0;
922 }
923
924 AVCodec ff_utvideo_decoder = {
925     .name           = "utvideo",
926     .long_name      = NULL_IF_CONFIG_SMALL("Ut Video"),
927     .type           = AVMEDIA_TYPE_VIDEO,
928     .id             = AV_CODEC_ID_UTVIDEO,
929     .priv_data_size = sizeof(UtvideoContext),
930     .init           = decode_init,
931     .close          = decode_end,
932     .decode         = decode_frame,
933     .capabilities   = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
934     .caps_internal  = FF_CODEC_CAP_INIT_THREADSAFE,
935 };