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