utvideo: general cosmetics
[libav.git] / libavcodec / utvideo.c
CommitLineData
0d8506b8
KS
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 <stdlib.h>
28
29#include "libavutil/intreadwrite.h"
30#include "avcodec.h"
31#include "bytestream.h"
32#include "get_bits.h"
33#include "dsputil.h"
12e984ae 34#include "thread.h"
0d8506b8
KS
35
36enum {
37 PRED_NONE = 0,
38 PRED_LEFT,
39 PRED_GRADIENT,
40 PRED_MEDIAN,
41};
42
43typedef struct UtvideoContext {
44 AVCodecContext *avctx;
45 AVFrame pic;
46 DSPContext dsp;
47
48 uint32_t frame_info_size, flags, frame_info;
49 int planes;
50 int slices;
51 int compression;
52 int interlaced;
53 int frame_pred;
54
55 uint8_t *slice_bits;
56 int slice_bits_size;
57} UtvideoContext;
58
59typedef struct HuffEntry {
60 uint8_t sym;
61 uint8_t len;
62} HuffEntry;
63
64static int huff_cmp(const void *a, const void *b)
65{
66 const HuffEntry *aa = a, *bb = b;
67 return (aa->len - bb->len)*256 + aa->sym - bb->sym;
68}
69
46e1af3b 70static int build_huff(const uint8_t *src, VLC *vlc, int *fsym)
0d8506b8
KS
71{
72 int i;
73 HuffEntry he[256];
74 int last;
75 uint32_t codes[256];
76 uint8_t bits[256];
77 uint8_t syms[256];
78 uint32_t code;
79
46e1af3b 80 *fsym = -1;
0d8506b8
KS
81 for (i = 0; i < 256; i++) {
82 he[i].sym = i;
83 he[i].len = *src++;
84 }
85 qsort(he, 256, sizeof(*he), huff_cmp);
86
46e1af3b
KS
87 if (!he[0].len) {
88 *fsym = he[0].sym;
89 return 0;
90 }
91 if (he[0].len > 32)
0d8506b8
KS
92 return -1;
93
94 last = 255;
95 while (he[last].len == 255 && last)
96 last--;
97
98 code = 1;
99 for (i = last; i >= 0; i--) {
100 codes[i] = code >> (32 - he[i].len);
101 bits[i] = he[i].len;
102 syms[i] = he[i].sym;
103 code += 0x80000000u >> (he[i].len - 1);
104 }
105
e96b4a53
MS
106 return ff_init_vlc_sparse(vlc, FFMIN(he[last].len, 9), last + 1,
107 bits, sizeof(*bits), sizeof(*bits),
108 codes, sizeof(*codes), sizeof(*codes),
109 syms, sizeof(*syms), sizeof(*syms), 0);
0d8506b8
KS
110}
111
112static int decode_plane(UtvideoContext *c, int plane_no,
113 uint8_t *dst, int step, int stride,
114 int width, int height,
5096399d 115 const uint8_t *src, int use_pred)
0d8506b8
KS
116{
117 int i, j, slice, pix;
118 int sstart, send;
119 VLC vlc;
120 GetBitContext gb;
46e1af3b 121 int prev, fsym;
9a173575 122 const int cmask = ~(!plane_no && c->avctx->pix_fmt == PIX_FMT_YUV420P);
0d8506b8 123
46e1af3b 124 if (build_huff(src, &vlc, &fsym)) {
0d8506b8
KS
125 av_log(c->avctx, AV_LOG_ERROR, "Cannot build Huffman codes\n");
126 return AVERROR_INVALIDDATA;
127 }
46e1af3b
KS
128 if (fsym >= 0) { // build_huff reported a symbol to fill slices with
129 send = 0;
130 for (slice = 0; slice < c->slices; slice++) {
131 uint8_t *dest;
132
133 sstart = send;
134 send = (height * (slice + 1) / c->slices) & cmask;
135 dest = dst + sstart * stride;
136
137 prev = 0x80;
138 for (j = sstart; j < send; j++) {
139 for (i = 0; i < width * step; i += step) {
140 pix = fsym;
141 if (use_pred) {
142 prev += pix;
143 pix = prev;
144 }
145 dest[i] = pix;
146 }
147 dest += stride;
148 }
149 }
150 return 0;
151 }
0d8506b8
KS
152
153 src += 256;
0d8506b8
KS
154
155 send = 0;
156 for (slice = 0; slice < c->slices; slice++) {
157 uint8_t *dest;
158 int slice_data_start, slice_data_end, slice_size;
159
160 sstart = send;
9a173575 161 send = (height * (slice + 1) / c->slices) & cmask;
0d8506b8
KS
162 dest = dst + sstart * stride;
163
164 // slice offset and size validation was done earlier
165 slice_data_start = slice ? AV_RL32(src + slice * 4 - 4) : 0;
166 slice_data_end = AV_RL32(src + slice * 4);
167 slice_size = slice_data_end - slice_data_start;
168
169 if (!slice_size) {
170 for (j = sstart; j < send; j++) {
171 for (i = 0; i < width * step; i += step)
172 dest[i] = 0x80;
173 dest += stride;
174 }
175 continue;
176 }
177
b5c3f0b9
JE
178 memcpy(c->slice_bits, src + slice_data_start + c->slices * 4,
179 slice_size);
0d8506b8 180 memset(c->slice_bits + slice_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
b5c3f0b9 181 c->dsp.bswap_buf((uint32_t *) c->slice_bits, (uint32_t *) c->slice_bits,
0d8506b8
KS
182 (slice_data_end - slice_data_start + 3) >> 2);
183 init_get_bits(&gb, c->slice_bits, slice_size * 8);
184
185 prev = 0x80;
186 for (j = sstart; j < send; j++) {
187 for (i = 0; i < width * step; i += step) {
188 if (get_bits_left(&gb) <= 0) {
b5c3f0b9
JE
189 av_log(c->avctx, AV_LOG_ERROR,
190 "Slice decoding ran out of bits\n");
0d8506b8
KS
191 goto fail;
192 }
193 pix = get_vlc2(&gb, vlc.table, vlc.bits, 4);
194 if (pix < 0) {
195 av_log(c->avctx, AV_LOG_ERROR, "Decoding error\n");
196 goto fail;
197 }
198 if (use_pred) {
199 prev += pix;
200 pix = prev;
201 }
202 dest[i] = pix;
203 }
204 dest += stride;
205 }
206 if (get_bits_left(&gb) > 32)
b5c3f0b9
JE
207 av_log(c->avctx, AV_LOG_WARNING,
208 "%d bits left after decoding slice\n", get_bits_left(&gb));
0d8506b8
KS
209 }
210
e96b4a53 211 ff_free_vlc(&vlc);
0d8506b8
KS
212
213 return 0;
214fail:
e96b4a53 215 ff_free_vlc(&vlc);
0d8506b8
KS
216 return AVERROR_INVALIDDATA;
217}
218
219static const int rgb_order[4] = { 1, 2, 0, 3 };
220
b5c3f0b9
JE
221static void restore_rgb_planes(uint8_t *src, int step, int stride, int width,
222 int height)
0d8506b8
KS
223{
224 int i, j;
225 uint8_t r, g, b;
226
227 for (j = 0; j < height; j++) {
228 for (i = 0; i < width * step; i += step) {
229 r = src[i];
230 g = src[i + 1];
231 b = src[i + 2];
232 src[i] = r + g - 0x80;
233 src[i + 2] = b + g - 0x80;
234 }
235 src += stride;
236 }
237}
238
239static void restore_median(uint8_t *src, int step, int stride,
9a173575 240 int width, int height, int slices, int rmode)
0d8506b8
KS
241{
242 int i, j, slice;
243 int A, B, C;
244 uint8_t *bsrc;
245 int slice_start, slice_height;
9a173575 246 const int cmask = ~rmode;
0d8506b8
KS
247
248 for (slice = 0; slice < slices; slice++) {
b5c3f0b9
JE
249 slice_start = ((slice * height) / slices) & cmask;
250 slice_height = ((((slice + 1) * height) / slices) & cmask) -
251 slice_start;
0d8506b8
KS
252
253 bsrc = src + slice_start * stride;
254
255 // first line - left neighbour prediction
256 bsrc[0] += 0x80;
257 A = bsrc[0];
258 for (i = step; i < width * step; i += step) {
259 bsrc[i] += A;
b5c3f0b9 260 A = bsrc[i];
0d8506b8
KS
261 }
262 bsrc += stride;
263 if (slice_height == 1)
264 continue;
b5c3f0b9
JE
265 // second line - first element has top prediction, the rest uses median
266 C = bsrc[-stride];
0d8506b8 267 bsrc[0] += C;
b5c3f0b9 268 A = bsrc[0];
0d8506b8 269 for (i = step; i < width * step; i += step) {
b5c3f0b9 270 B = bsrc[i - stride];
0d8506b8 271 bsrc[i] += mid_pred(A, B, (uint8_t)(A + B - C));
b5c3f0b9
JE
272 C = B;
273 A = bsrc[i];
0d8506b8
KS
274 }
275 bsrc += stride;
276 // the rest of lines use continuous median prediction
277 for (j = 2; j < slice_height; j++) {
278 for (i = 0; i < width * step; i += step) {
b5c3f0b9 279 B = bsrc[i - stride];
0d8506b8 280 bsrc[i] += mid_pred(A, B, (uint8_t)(A + B - C));
b5c3f0b9
JE
281 C = B;
282 A = bsrc[i];
0d8506b8
KS
283 }
284 bsrc += stride;
285 }
286 }
287}
288
490dcda6
KS
289/* UtVideo interlaced mode treats every two lines as a single one,
290 * so restoring function should take care of possible padding between
291 * two parts of the same "line".
292 */
293static void restore_median_il(uint8_t *src, int step, int stride,
294 int width, int height, int slices, int rmode)
295{
296 int i, j, slice;
297 int A, B, C;
298 uint8_t *bsrc;
299 int slice_start, slice_height;
b5c3f0b9 300 const int cmask = ~(rmode ? 3 : 1);
490dcda6
KS
301 const int stride2 = stride << 1;
302
303 for (slice = 0; slice < slices; slice++) {
304 slice_start = ((slice * height) / slices) & cmask;
b5c3f0b9
JE
305 slice_height = ((((slice + 1) * height) / slices) & cmask) -
306 slice_start;
490dcda6
KS
307 slice_height >>= 1;
308
309 bsrc = src + slice_start * stride;
310
311 // first line - left neighbour prediction
312 bsrc[0] += 0x80;
b5c3f0b9 313 A = bsrc[0];
490dcda6
KS
314 for (i = step; i < width * step; i += step) {
315 bsrc[i] += A;
b5c3f0b9 316 A = bsrc[i];
490dcda6
KS
317 }
318 for (i = 0; i < width * step; i += step) {
319 bsrc[stride + i] += A;
b5c3f0b9 320 A = bsrc[stride + i];
490dcda6
KS
321 }
322 bsrc += stride2;
323 if (slice_height == 1)
324 continue;
b5c3f0b9
JE
325 // second line - first element has top prediction, the rest uses median
326 C = bsrc[-stride2];
490dcda6 327 bsrc[0] += C;
b5c3f0b9 328 A = bsrc[0];
490dcda6 329 for (i = step; i < width * step; i += step) {
b5c3f0b9 330 B = bsrc[i - stride2];
490dcda6 331 bsrc[i] += mid_pred(A, B, (uint8_t)(A + B - C));
b5c3f0b9
JE
332 C = B;
333 A = bsrc[i];
490dcda6
KS
334 }
335 for (i = 0; i < width * step; i += step) {
b5c3f0b9 336 B = bsrc[i - stride];
490dcda6 337 bsrc[stride + i] += mid_pred(A, B, (uint8_t)(A + B - C));
b5c3f0b9
JE
338 C = B;
339 A = bsrc[stride + i];
490dcda6
KS
340 }
341 bsrc += stride2;
342 // the rest of lines use continuous median prediction
343 for (j = 2; j < slice_height; j++) {
344 for (i = 0; i < width * step; i += step) {
b5c3f0b9 345 B = bsrc[i - stride2];
490dcda6 346 bsrc[i] += mid_pred(A, B, (uint8_t)(A + B - C));
b5c3f0b9
JE
347 C = B;
348 A = bsrc[i];
490dcda6
KS
349 }
350 for (i = 0; i < width * step; i += step) {
b5c3f0b9 351 B = bsrc[i - stride];
490dcda6 352 bsrc[i + stride] += mid_pred(A, B, (uint8_t)(A + B - C));
b5c3f0b9
JE
353 C = B;
354 A = bsrc[i + stride];
490dcda6
KS
355 }
356 bsrc += stride2;
357 }
358 }
359}
360
b5c3f0b9
JE
361static int decode_frame(AVCodecContext *avctx, void *data, int *data_size,
362 AVPacket *avpkt)
0d8506b8
KS
363{
364 const uint8_t *buf = avpkt->data;
365 int buf_size = avpkt->size;
0d8506b8 366 UtvideoContext *c = avctx->priv_data;
0d8506b8
KS
367 int i, j;
368 const uint8_t *plane_start[5];
369 int plane_size, max_slice_size = 0, slice_start, slice_end, slice_size;
370 int ret;
ec0ed97b 371 GetByteContext gb;
0d8506b8
KS
372
373 if (c->pic.data[0])
12e984ae 374 ff_thread_release_buffer(avctx, &c->pic);
0d8506b8
KS
375
376 c->pic.reference = 1;
377 c->pic.buffer_hints = FF_BUFFER_HINTS_VALID;
12e984ae 378 if ((ret = ff_thread_get_buffer(avctx, &c->pic)) < 0) {
0d8506b8
KS
379 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
380 return ret;
381 }
382
12e984ae
AD
383 ff_thread_finish_setup(avctx);
384
b5c3f0b9 385 /* parse plane structure to get frame flags and validate slice offsets */
ec0ed97b 386 bytestream2_init(&gb, buf, buf_size);
0d8506b8 387 for (i = 0; i < c->planes; i++) {
ec0ed97b
RB
388 plane_start[i] = gb.buffer;
389 if (bytestream2_get_bytes_left(&gb) < 256 + 4 * c->slices) {
0d8506b8
KS
390 av_log(avctx, AV_LOG_ERROR, "Insufficient data for a plane\n");
391 return AVERROR_INVALIDDATA;
392 }
ec0ed97b 393 bytestream2_skipu(&gb, 256);
0d8506b8
KS
394 slice_start = 0;
395 slice_end = 0;
396 for (j = 0; j < c->slices; j++) {
ec0ed97b 397 slice_end = bytestream2_get_le32u(&gb);
0d8506b8 398 slice_size = slice_end - slice_start;
ec0ed97b
RB
399 if (slice_end <= 0 || slice_size <= 0 ||
400 bytestream2_get_bytes_left(&gb) < slice_end) {
0d8506b8
KS
401 av_log(avctx, AV_LOG_ERROR, "Incorrect slice size\n");
402 return AVERROR_INVALIDDATA;
403 }
404 slice_start = slice_end;
405 max_slice_size = FFMAX(max_slice_size, slice_size);
406 }
407 plane_size = slice_end;
ec0ed97b 408 bytestream2_skipu(&gb, plane_size);
0d8506b8 409 }
ec0ed97b
RB
410 plane_start[c->planes] = gb.buffer;
411 if (bytestream2_get_bytes_left(&gb) < c->frame_info_size) {
0d8506b8
KS
412 av_log(avctx, AV_LOG_ERROR, "Not enough data for frame information\n");
413 return AVERROR_INVALIDDATA;
414 }
ec0ed97b 415 c->frame_info = bytestream2_get_le32u(&gb);
0d8506b8
KS
416 av_log(avctx, AV_LOG_DEBUG, "frame information flags %X\n", c->frame_info);
417
418 c->frame_pred = (c->frame_info >> 8) & 3;
419
420 if (c->frame_pred == PRED_GRADIENT) {
421 av_log_ask_for_sample(avctx, "Frame uses gradient prediction\n");
422 return AVERROR_PATCHWELCOME;
423 }
424
425 av_fast_malloc(&c->slice_bits, &c->slice_bits_size,
426 max_slice_size + FF_INPUT_BUFFER_PADDING_SIZE);
427
428 if (!c->slice_bits) {
429 av_log(avctx, AV_LOG_ERROR, "Cannot allocate temporary buffer\n");
430 return AVERROR(ENOMEM);
431 }
432
433 switch (c->avctx->pix_fmt) {
434 case PIX_FMT_RGB24:
435 case PIX_FMT_RGBA:
436 for (i = 0; i < c->planes; i++) {
437 ret = decode_plane(c, i, c->pic.data[0] + rgb_order[i], c->planes,
438 c->pic.linesize[0], avctx->width, avctx->height,
5096399d 439 plane_start[i], c->frame_pred == PRED_LEFT);
0d8506b8
KS
440 if (ret)
441 return ret;
442 if (c->frame_pred == PRED_MEDIAN)
443 restore_median(c->pic.data[0] + rgb_order[i], c->planes,
444 c->pic.linesize[0], avctx->width, avctx->height,
9a173575 445 c->slices, 0);
0d8506b8
KS
446 }
447 restore_rgb_planes(c->pic.data[0], c->planes, c->pic.linesize[0],
448 avctx->width, avctx->height);
449 break;
450 case PIX_FMT_YUV420P:
451 for (i = 0; i < 3; i++) {
b5c3f0b9
JE
452 ret = decode_plane(c, i, c->pic.data[i], 1, c->pic.linesize[i],
453 avctx->width >> !!i, avctx->height >> !!i,
5096399d 454 plane_start[i], c->frame_pred == PRED_LEFT);
0d8506b8
KS
455 if (ret)
456 return ret;
490dcda6
KS
457 if (c->frame_pred == PRED_MEDIAN) {
458 if (!c->interlaced) {
459 restore_median(c->pic.data[i], 1, c->pic.linesize[i],
460 avctx->width >> !!i, avctx->height >> !!i,
461 c->slices, !i);
462 } else {
463 restore_median_il(c->pic.data[i], 1, c->pic.linesize[i],
464 avctx->width >> !!i,
465 avctx->height >> !!i,
466 c->slices, !i);
467 }
468 }
0d8506b8
KS
469 }
470 break;
471 case PIX_FMT_YUV422P:
472 for (i = 0; i < 3; i++) {
b5c3f0b9
JE
473 ret = decode_plane(c, i, c->pic.data[i], 1, c->pic.linesize[i],
474 avctx->width >> !!i, avctx->height,
5096399d 475 plane_start[i], c->frame_pred == PRED_LEFT);
0d8506b8
KS
476 if (ret)
477 return ret;
490dcda6
KS
478 if (c->frame_pred == PRED_MEDIAN) {
479 if (!c->interlaced) {
480 restore_median(c->pic.data[i], 1, c->pic.linesize[i],
481 avctx->width >> !!i, avctx->height,
482 c->slices, 0);
483 } else {
484 restore_median_il(c->pic.data[i], 1, c->pic.linesize[i],
485 avctx->width >> !!i, avctx->height,
486 c->slices, 0);
487 }
488 }
0d8506b8
KS
489 }
490 break;
491 }
492
72dadaa9
KS
493 c->pic.key_frame = 1;
494 c->pic.pict_type = AV_PICTURE_TYPE_I;
0d8506b8
KS
495 *data_size = sizeof(AVFrame);
496 *(AVFrame*)data = c->pic;
497
498 /* always report that the buffer was completely consumed */
499 return buf_size;
500}
501
502static av_cold int decode_init(AVCodecContext *avctx)
503{
504 UtvideoContext * const c = avctx->priv_data;
505
506 c->avctx = avctx;
507
9cf0841e 508 ff_dsputil_init(&c->dsp, avctx);
0d8506b8
KS
509
510 if (avctx->extradata_size < 16) {
b5c3f0b9
JE
511 av_log(avctx, AV_LOG_ERROR,
512 "Insufficient extradata size %d, should be at least 16\n",
0d8506b8
KS
513 avctx->extradata_size);
514 return AVERROR_INVALIDDATA;
515 }
516
517 av_log(avctx, AV_LOG_DEBUG, "Encoder version %d.%d.%d.%d\n",
518 avctx->extradata[3], avctx->extradata[2],
519 avctx->extradata[1], avctx->extradata[0]);
b5c3f0b9
JE
520 av_log(avctx, AV_LOG_DEBUG, "Original format %X\n",
521 AV_RB32(avctx->extradata + 4));
0d8506b8
KS
522 c->frame_info_size = AV_RL32(avctx->extradata + 8);
523 c->flags = AV_RL32(avctx->extradata + 12);
524
525 if (c->frame_info_size != 4)
526 av_log_ask_for_sample(avctx, "Frame info is not 4 bytes\n");
527 av_log(avctx, AV_LOG_DEBUG, "Encoding parameters %08X\n", c->flags);
528 c->slices = (c->flags >> 24) + 1;
529 c->compression = c->flags & 1;
530 c->interlaced = c->flags & 0x800;
531
532 c->slice_bits_size = 0;
533
534 switch (avctx->codec_tag) {
535 case MKTAG('U', 'L', 'R', 'G'):
536 c->planes = 3;
537 avctx->pix_fmt = PIX_FMT_RGB24;
538 break;
539 case MKTAG('U', 'L', 'R', 'A'):
540 c->planes = 4;
541 avctx->pix_fmt = PIX_FMT_RGBA;
542 break;
543 case MKTAG('U', 'L', 'Y', '0'):
544 c->planes = 3;
545 avctx->pix_fmt = PIX_FMT_YUV420P;
546 break;
547 case MKTAG('U', 'L', 'Y', '2'):
548 c->planes = 3;
549 avctx->pix_fmt = PIX_FMT_YUV422P;
550 break;
551 default:
552 av_log(avctx, AV_LOG_ERROR, "Unknown Ut Video FOURCC provided (%08X)\n",
553 avctx->codec_tag);
554 return AVERROR_INVALIDDATA;
555 }
556
557 return 0;
558}
559
560static av_cold int decode_end(AVCodecContext *avctx)
561{
562 UtvideoContext * const c = avctx->priv_data;
563
564 if (c->pic.data[0])
12e984ae 565 ff_thread_release_buffer(avctx, &c->pic);
0d8506b8
KS
566
567 av_freep(&c->slice_bits);
568
569 return 0;
570}
571
572AVCodec ff_utvideo_decoder = {
573 .name = "utvideo",
574 .type = AVMEDIA_TYPE_VIDEO,
575 .id = CODEC_ID_UTVIDEO,
576 .priv_data_size = sizeof(UtvideoContext),
577 .init = decode_init,
578 .close = decode_end,
579 .decode = decode_frame,
12e984ae 580 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_FRAME_THREADS,
0d8506b8
KS
581 .long_name = NULL_IF_CONFIG_SMALL("Ut Video"),
582};