ed6368e4cd8211f9633ec27e5c7547fdf1472fb5
[libav.git] / libavcodec / utvideodec.c
1 /*
2 * Ut Video decoder
3 * Copyright (c) 2011 Konstantin Shishkov
4 *
5 * This file is part of Libav.
6 *
7 * Libav 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 * Libav 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 Libav; 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
32 #include "avcodec.h"
33 #include "bitstream.h"
34 #include "bswapdsp.h"
35 #include "bytestream.h"
36 #include "internal.h"
37 #include "thread.h"
38 #include "utvideo.h"
39
40 static int build_huff10(const uint8_t *src, VLC *vlc, int *fsym)
41 {
42 int i;
43 HuffEntry he[1024];
44 int last;
45 uint32_t codes[1024];
46 uint8_t bits[1024];
47 uint16_t syms[1024];
48 uint32_t code;
49
50 *fsym = -1;
51 for (i = 0; i < 1024; i++) {
52 he[i].sym = i;
53 he[i].len = *src++;
54 }
55 qsort(he, 1024, sizeof(*he), ff_ut10_huff_cmp_len);
56
57 if (!he[0].len) {
58 *fsym = he[0].sym;
59 return 0;
60 }
61
62 last = 1023;
63 while (he[last].len == 255 && last)
64 last--;
65
66 if (he[last].len > 32) {
67 return -1;
68 }
69
70 code = 1;
71 for (i = last; i >= 0; i--) {
72 codes[i] = code >> (32 - he[i].len);
73 bits[i] = he[i].len;
74 syms[i] = he[i].sym;
75 code += 0x80000000u >> (he[i].len - 1);
76 }
77
78 return ff_init_vlc_sparse(vlc, FFMIN(he[last].len, 11), last + 1,
79 bits, sizeof(*bits), sizeof(*bits),
80 codes, sizeof(*codes), sizeof(*codes),
81 syms, sizeof(*syms), sizeof(*syms), 0);
82 }
83
84 static int build_huff(const uint8_t *src, VLC *vlc, int *fsym)
85 {
86 int i;
87 HuffEntry he[256];
88 int last;
89 uint32_t codes[256];
90 uint8_t bits[256];
91 uint8_t syms[256];
92 uint32_t code;
93
94 *fsym = -1;
95 for (i = 0; i < 256; i++) {
96 he[i].sym = i;
97 he[i].len = *src++;
98 }
99 qsort(he, 256, sizeof(*he), ff_ut_huff_cmp_len);
100
101 if (!he[0].len) {
102 *fsym = he[0].sym;
103 return 0;
104 }
105 if (he[0].len > 32)
106 return -1;
107
108 last = 255;
109 while (he[last].len == 255 && last)
110 last--;
111
112 code = 1;
113 for (i = last; i >= 0; i--) {
114 codes[i] = code >> (32 - he[i].len);
115 bits[i] = he[i].len;
116 syms[i] = he[i].sym;
117 code += 0x80000000u >> (he[i].len - 1);
118 }
119
120 return ff_init_vlc_sparse(vlc, FFMIN(he[last].len, 9), last + 1,
121 bits, sizeof(*bits), sizeof(*bits),
122 codes, sizeof(*codes), sizeof(*codes),
123 syms, sizeof(*syms), sizeof(*syms), 0);
124 }
125
126 static int decode_plane10(UtvideoContext *c, int plane_no,
127 uint16_t *dst, int step, int stride,
128 int width, int height,
129 const uint8_t *src, const uint8_t *huff,
130 int use_pred)
131 {
132 BitstreamContext bc;
133 int i, j, slice, pix, ret;
134 int sstart, send;
135 VLC vlc;
136 int prev, fsym;
137
138 if ((ret = build_huff10(huff, &vlc, &fsym)) < 0) {
139 av_log(c->avctx, AV_LOG_ERROR, "Cannot build Huffman codes\n");
140 return ret;
141 }
142 if (fsym >= 0) { // build_huff reported a symbol to fill slices with
143 send = 0;
144 for (slice = 0; slice < c->slices; slice++) {
145 uint16_t *dest;
146
147 sstart = send;
148 send = (height * (slice + 1) / c->slices);
149 dest = dst + sstart * stride;
150
151 prev = 0x200;
152 for (j = sstart; j < send; j++) {
153 for (i = 0; i < width * step; i += step) {
154 pix = fsym;
155 if (use_pred) {
156 prev += pix;
157 prev &= 0x3FF;
158 pix = prev;
159 }
160 dest[i] = pix;
161 }
162 dest += stride;
163 }
164 }
165 return 0;
166 }
167
168 send = 0;
169 for (slice = 0; slice < c->slices; slice++) {
170 uint16_t *dest;
171 int slice_data_start, slice_data_end, slice_size;
172
173 sstart = send;
174 send = (height * (slice + 1) / c->slices);
175 dest = dst + sstart * stride;
176
177 // slice offset and size validation was done earlier
178 slice_data_start = slice ? AV_RL32(src + slice * 4 - 4) : 0;
179 slice_data_end = AV_RL32(src + slice * 4);
180 slice_size = slice_data_end - slice_data_start;
181
182 if (!slice_size) {
183 av_log(c->avctx, AV_LOG_ERROR, "Plane has more than one symbol "
184 "yet a slice has a length of zero.\n");
185 goto fail;
186 }
187
188 memcpy(c->slice_bits, src + slice_data_start + c->slices * 4,
189 slice_size);
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 *) c->slice_bits,
193 (slice_data_end - slice_data_start + 3) >> 2);
194 bitstream_init8(&bc, c->slice_bits, slice_size);
195
196 prev = 0x200;
197 for (j = sstart; j < send; j++) {
198 for (i = 0; i < width * step; i += step) {
199 if (bitstream_bits_left(&bc) <= 0) {
200 av_log(c->avctx, AV_LOG_ERROR,
201 "Slice decoding ran out of bits\n");
202 goto fail;
203 }
204 pix = bitstream_read_vlc(&bc, vlc.table, vlc.bits, 3);
205 if (pix < 0) {
206 av_log(c->avctx, AV_LOG_ERROR, "Decoding error\n");
207 goto fail;
208 }
209 if (use_pred) {
210 prev += pix;
211 prev &= 0x3FF;
212 pix = prev;
213 }
214 dest[i] = pix;
215 }
216 dest += stride;
217 }
218 if (bitstream_bits_left(&bc) > 32)
219 av_log(c->avctx, AV_LOG_WARNING,
220 "%d bits left after decoding slice\n", bitstream_bits_left(&bc));
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, int 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, int step, 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 BitstreamContext bc;
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 * step; i += step) {
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 memcpy(c->slice_bits, src + slice_data_start + c->slices * 4,
305 slice_size);
306 memset(c->slice_bits + slice_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
307 c->bdsp.bswap_buf((uint32_t *) c->slice_bits,
308 (uint32_t *) c->slice_bits,
309 (slice_data_end - slice_data_start + 3) >> 2);
310 bitstream_init8(&bc, c->slice_bits, slice_size);
311
312 prev = 0x80;
313 for (j = sstart; j < send; j++) {
314 for (i = 0; i < width * step; i += step) {
315 if (bitstream_bits_left(&bc) <= 0) {
316 av_log(c->avctx, AV_LOG_ERROR,
317 "Slice decoding ran out of bits\n");
318 goto fail;
319 }
320 pix = bitstream_read_vlc(&bc, vlc.table, vlc.bits, 4);
321 if (pix < 0) {
322 av_log(c->avctx, AV_LOG_ERROR, "Decoding error\n");
323 goto fail;
324 }
325 if (use_pred) {
326 prev += pix;
327 pix = prev;
328 }
329 dest[i] = pix;
330 }
331 dest += stride;
332 }
333 if (bitstream_bits_left(&bc) > 32)
334 av_log(c->avctx, AV_LOG_WARNING,
335 "%d bits left after decoding slice\n", bitstream_bits_left(&bc));
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 static void restore_rgb_planes(uint8_t *src, int step, ptrdiff_t stride,
347 int width, int height)
348 {
349 int i, j;
350 uint8_t r, g, b;
351
352 for (j = 0; j < height; j++) {
353 for (i = 0; i < width * step; i += step) {
354 r = src[i];
355 g = src[i + 1];
356 b = src[i + 2];
357 src[i] = r + g - 0x80;
358 src[i + 2] = b + g - 0x80;
359 }
360 src += stride;
361 }
362 }
363
364 static void restore_rgb_planes10(AVFrame *frame, int width, int height)
365 {
366 uint16_t *src_r = (uint16_t *)frame->data[2];
367 uint16_t *src_g = (uint16_t *)frame->data[0];
368 uint16_t *src_b = (uint16_t *)frame->data[1];
369 int r, g, b;
370 int i, j;
371
372 for (j = 0; j < height; j++) {
373 for (i = 0; i < width; i++) {
374 r = src_r[i];
375 g = src_g[i];
376 b = src_b[i];
377 src_r[i] = (r + g - 0x200) & 0x3FF;
378 src_b[i] = (b + g - 0x200) & 0x3FF;
379 }
380 src_r += frame->linesize[2] / 2;
381 src_g += frame->linesize[0] / 2;
382 src_b += frame->linesize[1] / 2;
383 }
384 }
385
386 static void restore_median_planar(UtvideoContext *c, uint8_t *src,
387 ptrdiff_t stride, int width, int height,
388 int slices, int rmode)
389 {
390 int i, j, slice;
391 int A, B, C;
392 uint8_t *bsrc;
393 int slice_start, slice_height;
394 const int cmask = ~rmode;
395
396 for (slice = 0; slice < slices; slice++) {
397 slice_start = ((slice * height) / slices) & cmask;
398 slice_height = ((((slice + 1) * height) / slices) & cmask) -
399 slice_start;
400
401 if (!slice_height)
402 continue;
403 bsrc = src + slice_start * stride;
404
405 // first line - left neighbour prediction
406 bsrc[0] += 0x80;
407 c->hdspdec.add_hfyu_left_pred(bsrc, bsrc, width, 0);
408 bsrc += stride;
409 if (slice_height <= 1)
410 continue;
411 // second line - first element has top prediction, the rest uses median
412 C = bsrc[-stride];
413 bsrc[0] += C;
414 A = bsrc[0];
415 for (i = 1; i < width; i++) {
416 B = bsrc[i - stride];
417 bsrc[i] += mid_pred(A, B, (uint8_t)(A + B - C));
418 C = B;
419 A = bsrc[i];
420 }
421 bsrc += stride;
422 // the rest of lines use continuous median prediction
423 for (j = 2; j < slice_height; j++) {
424 c->hdspdec.add_hfyu_median_pred(bsrc, bsrc - stride,
425 bsrc, width, &A, &B);
426 bsrc += stride;
427 }
428 }
429 }
430
431 /* UtVideo interlaced mode treats every two lines as a single one,
432 * so restoring function should take care of possible padding between
433 * two parts of the same "line".
434 */
435 static void restore_median_planar_il(UtvideoContext *c, uint8_t *src,
436 ptrdiff_t stride, int width, int height,
437 int slices, int rmode)
438 {
439 int i, j, slice;
440 int A, B, C;
441 uint8_t *bsrc;
442 int slice_start, slice_height;
443 const int cmask = ~(rmode ? 3 : 1);
444 const int stride2 = stride << 1;
445
446 for (slice = 0; slice < slices; slice++) {
447 slice_start = ((slice * height) / slices) & cmask;
448 slice_height = ((((slice + 1) * height) / slices) & cmask) -
449 slice_start;
450 slice_height >>= 1;
451 if (!slice_height)
452 continue;
453
454 bsrc = src + slice_start * stride;
455
456 // first line - left neighbour prediction
457 bsrc[0] += 0x80;
458 A = c->hdspdec.add_hfyu_left_pred(bsrc, bsrc, width, 0);
459 c->hdspdec.add_hfyu_left_pred(bsrc + stride, bsrc + stride, width, A);
460 bsrc += stride2;
461 if (slice_height <= 1)
462 continue;
463 // second line - first element has top prediction, the rest uses median
464 C = bsrc[-stride2];
465 bsrc[0] += C;
466 A = bsrc[0];
467 for (i = 1; i < width; i++) {
468 B = bsrc[i - stride2];
469 bsrc[i] += mid_pred(A, B, (uint8_t)(A + B - C));
470 C = B;
471 A = bsrc[i];
472 }
473 c->hdspdec.add_hfyu_median_pred(bsrc + stride, bsrc - stride,
474 bsrc + stride, width, &A, &B);
475 bsrc += stride2;
476 // the rest of lines use continuous median prediction
477 for (j = 2; j < slice_height; j++) {
478 c->hdspdec.add_hfyu_median_pred(bsrc, bsrc - stride2,
479 bsrc, width, &A, &B);
480 c->hdspdec.add_hfyu_median_pred(bsrc + stride, bsrc - stride,
481 bsrc + stride, width, &A, &B);
482 bsrc += stride2;
483 }
484 }
485 }
486
487 static void restore_median_packed(uint8_t *src, int step, ptrdiff_t stride,
488 int width, int height,
489 int slices, int rmode)
490 {
491 int i, j, slice;
492 int A, B, C;
493 uint8_t *bsrc;
494 int slice_start, slice_height;
495 const int cmask = ~rmode;
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 if (!slice_height)
502 continue;
503
504 bsrc = src + slice_start * stride;
505
506 // first line - left neighbour prediction
507 bsrc[0] += 0x80;
508 A = bsrc[0];
509 for (i = step; i < width * step; i += step) {
510 bsrc[i] += A;
511 A = bsrc[i];
512 }
513 bsrc += stride;
514 if (slice_height == 1)
515 continue;
516 // second line - first element has top prediction, the rest uses median
517 C = bsrc[-stride];
518 bsrc[0] += C;
519 A = bsrc[0];
520 for (i = step; i < width * step; i += step) {
521 B = bsrc[i - stride];
522 bsrc[i] += mid_pred(A, B, (uint8_t)(A + B - C));
523 C = B;
524 A = bsrc[i];
525 }
526 bsrc += stride;
527 // the rest of lines use continuous median prediction
528 for (j = 2; j < slice_height; j++) {
529 for (i = 0; i < width * step; i += step) {
530 B = bsrc[i - stride];
531 bsrc[i] += mid_pred(A, B, (uint8_t)(A + B - C));
532 C = B;
533 A = bsrc[i];
534 }
535 bsrc += stride;
536 }
537 }
538 }
539
540 /* UtVideo interlaced mode treats every two lines as a single one,
541 * so restoring function should take care of possible padding between
542 * two parts of the same "line".
543 */
544 static void restore_median_packed_il(uint8_t *src, int step, ptrdiff_t stride,
545 int width, int height,
546 int slices, int rmode)
547 {
548 int i, j, slice;
549 int A, B, C;
550 uint8_t *bsrc;
551 int slice_start, slice_height;
552 const int cmask = ~(rmode ? 3 : 1);
553 const ptrdiff_t stride2 = stride << 1;
554
555 for (slice = 0; slice < slices; slice++) {
556 slice_start = ((slice * height) / slices) & cmask;
557 slice_height = ((((slice + 1) * height) / slices) & cmask) -
558 slice_start;
559 slice_height >>= 1;
560 if (!slice_height)
561 continue;
562
563 bsrc = src + slice_start * stride;
564
565 // first line - left neighbour prediction
566 bsrc[0] += 0x80;
567 A = bsrc[0];
568 for (i = step; i < width * step; i += step) {
569 bsrc[i] += A;
570 A = bsrc[i];
571 }
572 for (i = 0; i < width * step; i += step) {
573 bsrc[stride + i] += A;
574 A = bsrc[stride + i];
575 }
576 bsrc += stride2;
577 if (slice_height == 1)
578 continue;
579 // second line - first element has top prediction, the rest uses median
580 C = bsrc[-stride2];
581 bsrc[0] += C;
582 A = bsrc[0];
583 for (i = step; i < width * step; i += step) {
584 B = bsrc[i - stride2];
585 bsrc[i] += mid_pred(A, B, (uint8_t)(A + B - C));
586 C = B;
587 A = bsrc[i];
588 }
589 for (i = 0; i < width * step; i += step) {
590 B = bsrc[i - stride];
591 bsrc[stride + i] += mid_pred(A, B, (uint8_t)(A + B - C));
592 C = B;
593 A = bsrc[stride + i];
594 }
595 bsrc += stride2;
596 // the rest of lines use continuous median prediction
597 for (j = 2; j < slice_height; j++) {
598 for (i = 0; i < width * step; i += step) {
599 B = bsrc[i - stride2];
600 bsrc[i] += mid_pred(A, B, (uint8_t)(A + B - C));
601 C = B;
602 A = bsrc[i];
603 }
604 for (i = 0; i < width * step; i += step) {
605 B = bsrc[i - stride];
606 bsrc[i + stride] += mid_pred(A, B, (uint8_t)(A + B - C));
607 C = B;
608 A = bsrc[i + stride];
609 }
610 bsrc += stride2;
611 }
612 }
613 }
614
615 static void restore_gradient_planar(UtvideoContext *c, uint8_t *src, ptrdiff_t stride,
616 int width, int height, int slices, int rmode)
617 {
618 int i, j, slice;
619 int A, B, C;
620 uint8_t *bsrc;
621 int slice_start, slice_height;
622 const int cmask = ~rmode;
623
624 for (slice = 0; slice < slices; slice++) {
625 slice_start = ((slice * height) / slices) & cmask;
626 slice_height = ((((slice + 1) * height) / slices) & cmask) -
627 slice_start;
628
629 if (!slice_height)
630 continue;
631 bsrc = src + slice_start * stride;
632
633 // first line - left neighbour prediction
634 bsrc[0] += 0x80;
635 c->hdspdec.add_hfyu_left_pred(bsrc, bsrc, width, 0);
636 bsrc += stride;
637 if (slice_height <= 1)
638 continue;
639 for (j = 1; j < slice_height; j++) {
640 // second line - first element has top prediction, the rest uses gradient
641 bsrc[0] = (bsrc[0] + bsrc[-stride]) & 0xFF;
642 for (i = 1; i < width; i++) {
643 A = bsrc[i - stride];
644 B = bsrc[i - (stride + 1)];
645 C = bsrc[i - 1];
646 bsrc[i] = (A - B + C + bsrc[i]) & 0xFF;
647 }
648 bsrc += stride;
649 }
650 }
651 }
652
653 static void restore_gradient_planar_il(UtvideoContext *c, uint8_t *src, ptrdiff_t stride,
654 int width, int height, int slices, int rmode)
655 {
656 int i, j, slice;
657 int A, B, C;
658 uint8_t *bsrc;
659 int slice_start, slice_height;
660 const int cmask = ~(rmode ? 3 : 1);
661 const ptrdiff_t stride2 = stride << 1;
662
663 for (slice = 0; slice < slices; slice++) {
664 slice_start = ((slice * height) / slices) & cmask;
665 slice_height = ((((slice + 1) * height) / slices) & cmask) -
666 slice_start;
667 slice_height >>= 1;
668 if (!slice_height)
669 continue;
670
671 bsrc = src + slice_start * stride;
672
673 // first line - left neighbour prediction
674 bsrc[0] += 0x80;
675 A = c->hdspdec.add_hfyu_left_pred(bsrc, bsrc, width, 0);
676 c->hdspdec.add_hfyu_left_pred(bsrc + stride, bsrc + stride, width, A);
677 bsrc += stride2;
678 if (slice_height <= 1)
679 continue;
680 for (j = 1; j < slice_height; j++) {
681 // second line - first element has top prediction, the rest uses gradient
682 bsrc[0] = (bsrc[0] + bsrc[-stride2]) & 0xFF;
683 for (i = 1; i < width; i++) {
684 A = bsrc[i - stride2];
685 B = bsrc[i - (stride2 + 1)];
686 C = bsrc[i - 1];
687 bsrc[i] = (A - B + C + bsrc[i]) & 0xFF;
688 }
689 A = bsrc[-stride];
690 B = bsrc[-(1 + stride + stride - width)];
691 C = bsrc[width - 1];
692 bsrc[stride] = (A - B + C + bsrc[stride]) & 0xFF;
693 for (i = 1; i < width; i++) {
694 A = bsrc[i - stride];
695 B = bsrc[i - (1 + stride)];
696 C = bsrc[i - 1 + stride];
697 bsrc[i + stride] = (A - B + C + bsrc[i + stride]) & 0xFF;
698 }
699 bsrc += stride2;
700 }
701 }
702 }
703
704 static void restore_gradient_packed(uint8_t *src, int step, ptrdiff_t stride,
705 int width, int height, int slices, int rmode)
706 {
707 int i, j, slice;
708 int A, B, C;
709 uint8_t *bsrc;
710 int slice_start, slice_height;
711 const int cmask = ~rmode;
712
713 for (slice = 0; slice < slices; slice++) {
714 slice_start = ((slice * height) / slices) & cmask;
715 slice_height = ((((slice + 1) * height) / slices) & cmask) -
716 slice_start;
717
718 if (!slice_height)
719 continue;
720 bsrc = src + slice_start * stride;
721
722 // first line - left neighbour prediction
723 bsrc[0] += 0x80;
724 A = bsrc[0];
725 for (i = step; i < width * step; i += step) {
726 bsrc[i] += A;
727 A = bsrc[i];
728 }
729 bsrc += stride;
730 if (slice_height <= 1)
731 continue;
732 for (j = 1; j < slice_height; j++) {
733 // second line - first element has top prediction, the rest uses gradient
734 C = bsrc[-stride];
735 bsrc[0] += C;
736 for (i = step; i < width * step; i += step) {
737 A = bsrc[i - stride];
738 B = bsrc[i - (stride + step)];
739 C = bsrc[i - step];
740 bsrc[i] = (A - B + C + bsrc[i]) & 0xFF;
741 }
742 bsrc += stride;
743 }
744 }
745 }
746
747 static void restore_gradient_packed_il(uint8_t *src, int step, ptrdiff_t stride,
748 int width, int height, int slices, int rmode)
749 {
750 int i, j, slice;
751 int A, B, C;
752 uint8_t *bsrc;
753 int slice_start, slice_height;
754 const int cmask = ~(rmode ? 3 : 1);
755 const ptrdiff_t stride2 = stride << 1;
756
757 for (slice = 0; slice < slices; slice++) {
758 slice_start = ((slice * height) / slices) & cmask;
759 slice_height = ((((slice + 1) * height) / slices) & cmask) -
760 slice_start;
761 slice_height >>= 1;
762 if (!slice_height)
763 continue;
764
765 bsrc = src + slice_start * stride;
766
767 // first line - left neighbour prediction
768 bsrc[0] += 0x80;
769 A = bsrc[0];
770 for (i = step; i < width * step; i += step) {
771 bsrc[i] += A;
772 A = bsrc[i];
773 }
774 for (i = 0; i < width * step; i += step) {
775 bsrc[stride + i] += A;
776 A = bsrc[stride + i];
777 }
778 bsrc += stride2;
779 if (slice_height <= 1)
780 continue;
781 for (j = 1; j < slice_height; j++) {
782 // second line - first element has top prediction, the rest uses gradient
783 C = bsrc[-stride2];
784 bsrc[0] += C;
785 for (i = step; i < width * step; i += step) {
786 A = bsrc[i - stride2];
787 B = bsrc[i - (stride2 + step)];
788 C = bsrc[i - step];
789 bsrc[i] = (A - B + C + bsrc[i]) & 0xFF;
790 }
791 A = bsrc[-stride];
792 B = bsrc[-(step + stride + stride - width * step)];
793 C = bsrc[width * step - step];
794 bsrc[stride] = (A - B + C + bsrc[stride]) & 0xFF;
795 for (i = step; i < width * step; i += step) {
796 A = bsrc[i - stride];
797 B = bsrc[i - (step + stride)];
798 C = bsrc[i - step + stride];
799 bsrc[i + stride] = (A - B + C + bsrc[i + stride]) & 0xFF;
800 }
801 bsrc += stride2;
802 }
803 }
804 }
805
806 static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
807 AVPacket *avpkt)
808 {
809 const uint8_t *buf = avpkt->data;
810 int buf_size = avpkt->size;
811 UtvideoContext *c = avctx->priv_data;
812 int i, j;
813 const uint8_t *plane_start[5];
814 int plane_size, max_slice_size = 0, slice_start, slice_end, slice_size;
815 int ret;
816 GetByteContext gb;
817 ThreadFrame frame = { .f = data };
818
819 if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0) {
820 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
821 return ret;
822 }
823
824 ff_thread_finish_setup(avctx);
825
826 /* parse plane structure to get frame flags and validate slice offsets */
827 bytestream2_init(&gb, buf, buf_size);
828 if (c->pro) {
829 if (bytestream2_get_bytes_left(&gb) < c->frame_info_size) {
830 av_log(avctx, AV_LOG_ERROR, "Not enough data for frame information\n");
831 return AVERROR_INVALIDDATA;
832 }
833 c->frame_info = bytestream2_get_le32u(&gb);
834 c->slices = ((c->frame_info >> 16) & 0xff) + 1;
835 for (i = 0; i < c->planes; i++) {
836 plane_start[i] = gb.buffer;
837 if (bytestream2_get_bytes_left(&gb) < 1024 + 4 * c->slices) {
838 av_log(avctx, AV_LOG_ERROR, "Insufficient data for a plane\n");
839 return AVERROR_INVALIDDATA;
840 }
841 slice_start = 0;
842 slice_end = 0;
843 for (j = 0; j < c->slices; j++) {
844 slice_end = bytestream2_get_le32u(&gb);
845 if (slice_end < 0 || slice_end < slice_start ||
846 bytestream2_get_bytes_left(&gb) < slice_end) {
847 av_log(avctx, AV_LOG_ERROR, "Incorrect slice size\n");
848 return AVERROR_INVALIDDATA;
849 }
850 slice_size = slice_end - slice_start;
851 slice_start = slice_end;
852 max_slice_size = FFMAX(max_slice_size, slice_size);
853 }
854 plane_size = slice_end;
855 bytestream2_skipu(&gb, plane_size);
856 bytestream2_skipu(&gb, 1024);
857 }
858 plane_start[c->planes] = gb.buffer;
859 } else {
860 for (i = 0; i < c->planes; i++) {
861 plane_start[i] = gb.buffer;
862 if (bytestream2_get_bytes_left(&gb) < 256 + 4 * c->slices) {
863 av_log(avctx, AV_LOG_ERROR, "Insufficient data for a plane\n");
864 return AVERROR_INVALIDDATA;
865 }
866 bytestream2_skipu(&gb, 256);
867 slice_start = 0;
868 slice_end = 0;
869 for (j = 0; j < c->slices; j++) {
870 slice_end = bytestream2_get_le32u(&gb);
871 if (slice_end < 0 || slice_end < slice_start ||
872 bytestream2_get_bytes_left(&gb) < slice_end) {
873 av_log(avctx, AV_LOG_ERROR, "Incorrect slice size\n");
874 return AVERROR_INVALIDDATA;
875 }
876 slice_size = slice_end - slice_start;
877 slice_start = slice_end;
878 max_slice_size = FFMAX(max_slice_size, slice_size);
879 }
880 plane_size = slice_end;
881 bytestream2_skipu(&gb, plane_size);
882 }
883 plane_start[c->planes] = gb.buffer;
884 if (bytestream2_get_bytes_left(&gb) < c->frame_info_size) {
885 av_log(avctx, AV_LOG_ERROR, "Not enough data for frame information\n");
886 return AVERROR_INVALIDDATA;
887 }
888 c->frame_info = bytestream2_get_le32u(&gb);
889 }
890 av_log(avctx, AV_LOG_DEBUG, "frame information flags %"PRIX32"\n",
891 c->frame_info);
892
893 c->frame_pred = (c->frame_info >> 8) & 3;
894
895 av_fast_malloc(&c->slice_bits, &c->slice_bits_size,
896 max_slice_size + AV_INPUT_BUFFER_PADDING_SIZE);
897
898 if (!c->slice_bits) {
899 av_log(avctx, AV_LOG_ERROR, "Cannot allocate temporary buffer\n");
900 return AVERROR(ENOMEM);
901 }
902
903 switch (c->avctx->pix_fmt) {
904 case AV_PIX_FMT_RGB24:
905 case AV_PIX_FMT_RGBA:
906 for (i = 0; i < c->planes; i++) {
907 ret = decode_plane(c, i, frame.f->data[0] + ff_ut_rgb_order[i],
908 c->planes, frame.f->linesize[0], avctx->width,
909 avctx->height, plane_start[i],
910 c->frame_pred == PRED_LEFT);
911 if (ret)
912 return ret;
913 if (c->frame_pred == PRED_MEDIAN) {
914 if (!c->interlaced) {
915 restore_median_packed(frame.f->data[0] + ff_ut_rgb_order[i],
916 c->planes, frame.f->linesize[0], avctx->width,
917 avctx->height, c->slices, 0);
918 } else {
919 restore_median_packed_il(frame.f->data[0] + ff_ut_rgb_order[i],
920 c->planes, frame.f->linesize[0],
921 avctx->width, avctx->height, c->slices,
922 0);
923 }
924 } else if (c->frame_pred == PRED_GRADIENT) {
925 if (!c->interlaced) {
926 restore_gradient_packed(frame.f->data[0] + ff_ut_rgb_order[i],
927 c->planes, frame.f->linesize[0],
928 avctx->width, avctx->height,
929 c->slices, 0);
930 } else {
931 restore_gradient_packed_il(frame.f->data[0] + ff_ut_rgb_order[i],
932 c->planes, frame.f->linesize[0],
933 avctx->width, avctx->height,
934 c->slices, 0);
935 }
936 }
937 }
938 restore_rgb_planes(frame.f->data[0], c->planes, frame.f->linesize[0],
939 avctx->width, avctx->height);
940 break;
941 case AV_PIX_FMT_GBRAP10:
942 case AV_PIX_FMT_GBRP10:
943 for (i = 0; i < c->planes; i++) {
944 ret = decode_plane10(c, i, (uint16_t *)frame.f->data[i], 1,
945 frame.f->linesize[i] / 2, avctx->width,
946 avctx->height, plane_start[i],
947 plane_start[i + 1] - 1024,
948 c->frame_pred == PRED_LEFT);
949 if (ret)
950 return ret;
951 }
952 restore_rgb_planes10(frame.f, avctx->width, avctx->height);
953 break;
954 case AV_PIX_FMT_YUV420P:
955 for (i = 0; i < 3; i++) {
956 ret = decode_plane(c, i, frame.f->data[i], 1, frame.f->linesize[i],
957 avctx->width >> !!i, avctx->height >> !!i,
958 plane_start[i], c->frame_pred == PRED_LEFT);
959 if (ret)
960 return ret;
961 if (c->frame_pred == PRED_MEDIAN) {
962 if (!c->interlaced) {
963 restore_median_planar(c, frame.f->data[i], frame.f->linesize[i],
964 avctx->width >> !!i, avctx->height >> !!i,
965 c->slices, !i);
966 } else {
967 restore_median_planar_il(c, frame.f->data[i], frame.f->linesize[i],
968 avctx->width >> !!i,
969 avctx->height >> !!i,
970 c->slices, !i);
971 }
972 } else if (c->frame_pred == PRED_GRADIENT) {
973 if (!c->interlaced) {
974 restore_gradient_planar(c, frame.f->data[i], frame.f->linesize[i],
975 avctx->width >> !!i,
976 avctx->height >> !!i,
977 c->slices, !i);
978 } else {
979 restore_gradient_planar_il(c, frame.f->data[i], frame.f->linesize[i],
980 avctx->width >> !!i,
981 avctx->height >> !!i,
982 c->slices, !i);
983 }
984 }
985 }
986 break;
987 case AV_PIX_FMT_YUV422P:
988 for (i = 0; i < 3; i++) {
989 ret = decode_plane(c, i, frame.f->data[i], 1, frame.f->linesize[i],
990 avctx->width >> !!i, avctx->height,
991 plane_start[i], c->frame_pred == PRED_LEFT);
992 if (ret)
993 return ret;
994 if (c->frame_pred == PRED_MEDIAN) {
995 if (!c->interlaced) {
996 restore_median_planar(c, frame.f->data[i], frame.f->linesize[i],
997 avctx->width >> !!i, avctx->height,
998 c->slices, 0);
999 } else {
1000 restore_median_planar_il(c, frame.f->data[i], frame.f->linesize[i],
1001 avctx->width >> !!i, avctx->height,
1002 c->slices, 0);
1003 }
1004 } else if (c->frame_pred == PRED_GRADIENT) {
1005 if (!c->interlaced) {
1006 restore_gradient_planar(c, frame.f->data[i], frame.f->linesize[i],
1007 avctx->width >> !!i, avctx->height,
1008 c->slices, 0);
1009 } else {
1010 restore_gradient_planar_il(c, frame.f->data[i], frame.f->linesize[i],
1011 avctx->width >> !!i, avctx->height,
1012 c->slices, 0);
1013 }
1014 }
1015 }
1016 break;
1017 case AV_PIX_FMT_YUV444P:
1018 for (i = 0; i < 3; i++) {
1019 ret = decode_plane(c, i, frame.f->data[i], 1, frame.f->linesize[i],
1020 avctx->width, avctx->height,
1021 plane_start[i], c->frame_pred == PRED_LEFT);
1022 if (ret)
1023 return ret;
1024 if (c->frame_pred == PRED_MEDIAN) {
1025 if (!c->interlaced) {
1026 restore_median_planar(c, frame.f->data[i], frame.f->linesize[i],
1027 avctx->width, avctx->height,
1028 c->slices, 0);
1029 } else {
1030 restore_median_planar_il(c, frame.f->data[i], frame.f->linesize[i],
1031 avctx->width, avctx->height,
1032 c->slices, 0);
1033 }
1034 } else if (c->frame_pred == PRED_GRADIENT) {
1035 if (!c->interlaced) {
1036 restore_gradient_planar(c, frame.f->data[i], frame.f->linesize[i],
1037 avctx->width, avctx->height,
1038 c->slices, 0);
1039 } else {
1040 restore_gradient_planar_il(c, frame.f->data[i], frame.f->linesize[i],
1041 avctx->width, avctx->height,
1042 c->slices, 0);
1043 }
1044 }
1045 }
1046 break;
1047 case AV_PIX_FMT_YUV422P10:
1048 for (i = 0; i < 3; i++) {
1049 ret = decode_plane10(c, i, (uint16_t *)frame.f->data[i], 1, frame.f->linesize[i] / 2,
1050 avctx->width >> !!i, avctx->height,
1051 plane_start[i], plane_start[i + 1] - 1024, c->frame_pred == PRED_LEFT);
1052 if (ret)
1053 return ret;
1054 }
1055 break;
1056 }
1057
1058 frame.f->key_frame = 1;
1059 frame.f->pict_type = AV_PICTURE_TYPE_I;
1060 frame.f->interlaced_frame = !!c->interlaced;
1061
1062 *got_frame = 1;
1063
1064 /* always report that the buffer was completely consumed */
1065 return buf_size;
1066 }
1067
1068 static av_cold int decode_init(AVCodecContext *avctx)
1069 {
1070 UtvideoContext * const c = avctx->priv_data;
1071
1072 c->avctx = avctx;
1073
1074 ff_bswapdsp_init(&c->bdsp);
1075 ff_huffyuvdsp_init(&c->hdspdec);
1076
1077 if (avctx->extradata_size >= 16) {
1078 av_log(avctx, AV_LOG_DEBUG, "Encoder version %d.%d.%d.%d\n",
1079 avctx->extradata[3], avctx->extradata[2],
1080 avctx->extradata[1], avctx->extradata[0]);
1081 av_log(avctx, AV_LOG_DEBUG, "Original format %"PRIX32"\n",
1082 AV_RB32(avctx->extradata + 4));
1083 c->frame_info_size = AV_RL32(avctx->extradata + 8);
1084 c->flags = AV_RL32(avctx->extradata + 12);
1085
1086 if (c->frame_info_size != 4)
1087 avpriv_request_sample(avctx, "Frame info not 4 bytes");
1088 av_log(avctx, AV_LOG_DEBUG, "Encoding parameters %08"PRIX32"\n", c->flags);
1089 c->slices = (c->flags >> 24) + 1;
1090 c->compression = c->flags & 1;
1091 c->interlaced = c->flags & 0x800;
1092 } else if (avctx->extradata_size == 8) {
1093 av_log(avctx, AV_LOG_DEBUG, "Encoder version %d.%d.%d.%d\n",
1094 avctx->extradata[3], avctx->extradata[2],
1095 avctx->extradata[1], avctx->extradata[0]);
1096 av_log(avctx, AV_LOG_DEBUG, "Original format %"PRIX32"\n",
1097 AV_RB32(avctx->extradata + 4));
1098 c->interlaced = 0;
1099 c->pro = 1;
1100 c->frame_info_size = 4;
1101 } else {
1102 av_log(avctx, AV_LOG_ERROR,
1103 "Insufficient extradata size %d, should be at least 16\n",
1104 avctx->extradata_size);
1105 return AVERROR_INVALIDDATA;
1106 }
1107
1108 c->slice_bits_size = 0;
1109
1110 switch (avctx->codec_tag) {
1111 case MKTAG('U', 'L', 'R', 'G'):
1112 c->planes = 3;
1113 avctx->pix_fmt = AV_PIX_FMT_RGB24;
1114 break;
1115 case MKTAG('U', 'L', 'R', 'A'):
1116 c->planes = 4;
1117 avctx->pix_fmt = AV_PIX_FMT_RGBA;
1118 break;
1119 case MKTAG('U', 'L', 'Y', '0'):
1120 c->planes = 3;
1121 avctx->pix_fmt = AV_PIX_FMT_YUV420P;
1122 avctx->colorspace = AVCOL_SPC_BT470BG;
1123 break;
1124 case MKTAG('U', 'L', 'Y', '2'):
1125 c->planes = 3;
1126 avctx->pix_fmt = AV_PIX_FMT_YUV422P;
1127 avctx->colorspace = AVCOL_SPC_BT470BG;
1128 break;
1129 case MKTAG('U', 'L', 'Y', '4'):
1130 c->planes = 3;
1131 avctx->pix_fmt = AV_PIX_FMT_YUV444P;
1132 avctx->colorspace = AVCOL_SPC_BT470BG;
1133 break;
1134 case MKTAG('U', 'Q', 'Y', '2'):
1135 c->planes = 3;
1136 avctx->pix_fmt = AV_PIX_FMT_YUV422P10;
1137 break;
1138 case MKTAG('U', 'Q', 'R', 'G'):
1139 c->planes = 3;
1140 avctx->pix_fmt = AV_PIX_FMT_GBRP10;
1141 break;
1142 case MKTAG('U', 'Q', 'R', 'A'):
1143 c->planes = 4;
1144 avctx->pix_fmt = AV_PIX_FMT_GBRAP10;
1145 break;
1146 case MKTAG('U', 'L', 'H', '0'):
1147 c->planes = 3;
1148 avctx->pix_fmt = AV_PIX_FMT_YUV420P;
1149 avctx->colorspace = AVCOL_SPC_BT709;
1150 break;
1151 case MKTAG('U', 'L', 'H', '2'):
1152 c->planes = 3;
1153 avctx->pix_fmt = AV_PIX_FMT_YUV422P;
1154 avctx->colorspace = AVCOL_SPC_BT709;
1155 break;
1156 case MKTAG('U', 'L', 'H', '4'):
1157 c->planes = 3;
1158 avctx->pix_fmt = AV_PIX_FMT_YUV444P;
1159 avctx->colorspace = AVCOL_SPC_BT709;
1160 break;
1161 default:
1162 av_log(avctx, AV_LOG_ERROR, "Unknown Ut Video FOURCC provided (%08X)\n",
1163 avctx->codec_tag);
1164 return AVERROR_INVALIDDATA;
1165 }
1166
1167 return 0;
1168 }
1169
1170 static av_cold int decode_end(AVCodecContext *avctx)
1171 {
1172 UtvideoContext * const c = avctx->priv_data;
1173
1174 av_freep(&c->slice_bits);
1175
1176 return 0;
1177 }
1178
1179 AVCodec ff_utvideo_decoder = {
1180 .name = "utvideo",
1181 .long_name = NULL_IF_CONFIG_SMALL("Ut Video"),
1182 .type = AVMEDIA_TYPE_VIDEO,
1183 .id = AV_CODEC_ID_UTVIDEO,
1184 .priv_data_size = sizeof(UtvideoContext),
1185 .init = decode_init,
1186 .close = decode_end,
1187 .decode = decode_frame,
1188 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
1189 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
1190 };