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