5faaa15a0c40b737b18170393537b2177678e38f
[libav.git] / libavcodec / utvideo.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 <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"
34
35 enum {
36 PRED_NONE = 0,
37 PRED_LEFT,
38 PRED_GRADIENT,
39 PRED_MEDIAN,
40 };
41
42 typedef struct UtvideoContext {
43 AVCodecContext *avctx;
44 AVFrame pic;
45 DSPContext dsp;
46
47 uint32_t frame_info_size, flags, frame_info;
48 int planes;
49 int slices;
50 int compression;
51 int interlaced;
52 int frame_pred;
53
54 uint8_t *slice_bits;
55 int slice_bits_size;
56 } UtvideoContext;
57
58 typedef struct HuffEntry {
59 uint8_t sym;
60 uint8_t len;
61 } HuffEntry;
62
63 static int huff_cmp(const void *a, const void *b)
64 {
65 const HuffEntry *aa = a, *bb = b;
66 return (aa->len - bb->len)*256 + aa->sym - bb->sym;
67 }
68
69 static int build_huff(const uint8_t *src, VLC *vlc)
70 {
71 int i;
72 HuffEntry he[256];
73 int last;
74 uint32_t codes[256];
75 uint8_t bits[256];
76 uint8_t syms[256];
77 uint32_t code;
78
79 for (i = 0; i < 256; i++) {
80 he[i].sym = i;
81 he[i].len = *src++;
82 }
83 qsort(he, 256, sizeof(*he), huff_cmp);
84
85 if (!he[0].len || he[0].len > 32)
86 return -1;
87
88 last = 255;
89 while (he[last].len == 255 && last)
90 last--;
91
92 code = 1;
93 for (i = last; i >= 0; i--) {
94 codes[i] = code >> (32 - he[i].len);
95 bits[i] = he[i].len;
96 syms[i] = he[i].sym;
97 code += 0x80000000u >> (he[i].len - 1);
98 }
99
100 return init_vlc_sparse(vlc, FFMIN(he[last].len, 9), last + 1,
101 bits, sizeof(*bits), sizeof(*bits),
102 codes, sizeof(*codes), sizeof(*codes),
103 syms, sizeof(*syms), sizeof(*syms), 0);
104 }
105
106 static int decode_plane(UtvideoContext *c, int plane_no,
107 uint8_t *dst, int step, int stride,
108 int width, int height,
109 const uint8_t *src, int src_size, int use_pred)
110 {
111 int i, j, slice, pix;
112 int sstart, send;
113 VLC vlc;
114 GetBitContext gb;
115 int prev;
116 const int cmask = ~(!plane_no && c->avctx->pix_fmt == PIX_FMT_YUV420P);
117
118 if (build_huff(src, &vlc)) {
119 av_log(c->avctx, AV_LOG_ERROR, "Cannot build Huffman codes\n");
120 return AVERROR_INVALIDDATA;
121 }
122
123 src += 256;
124 src_size -= 256;
125
126 send = 0;
127 for (slice = 0; slice < c->slices; slice++) {
128 uint8_t *dest;
129 int slice_data_start, slice_data_end, slice_size;
130
131 sstart = send;
132 send = (height * (slice + 1) / c->slices) & cmask;
133 dest = dst + sstart * stride;
134
135 // slice offset and size validation was done earlier
136 slice_data_start = slice ? AV_RL32(src + slice * 4 - 4) : 0;
137 slice_data_end = AV_RL32(src + slice * 4);
138 slice_size = slice_data_end - slice_data_start;
139
140 if (!slice_size) {
141 for (j = sstart; j < send; j++) {
142 for (i = 0; i < width * step; i += step)
143 dest[i] = 0x80;
144 dest += stride;
145 }
146 continue;
147 }
148
149 memcpy(c->slice_bits, src + slice_data_start + c->slices * 4, slice_size);
150 memset(c->slice_bits + slice_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
151 c->dsp.bswap_buf((uint32_t*)c->slice_bits, (uint32_t*)c->slice_bits,
152 (slice_data_end - slice_data_start + 3) >> 2);
153 init_get_bits(&gb, c->slice_bits, slice_size * 8);
154
155 prev = 0x80;
156 for (j = sstart; j < send; j++) {
157 for (i = 0; i < width * step; i += step) {
158 if (get_bits_left(&gb) <= 0) {
159 av_log(c->avctx, AV_LOG_ERROR, "Slice decoding ran out of bits\n");
160 goto fail;
161 }
162 pix = get_vlc2(&gb, vlc.table, vlc.bits, 4);
163 if (pix < 0) {
164 av_log(c->avctx, AV_LOG_ERROR, "Decoding error\n");
165 goto fail;
166 }
167 if (use_pred) {
168 prev += pix;
169 pix = prev;
170 }
171 dest[i] = pix;
172 }
173 dest += stride;
174 }
175 if (get_bits_left(&gb) > 32)
176 av_log(c->avctx, AV_LOG_WARNING, "%d bits left after decoding slice\n",
177 get_bits_left(&gb));
178 }
179
180 free_vlc(&vlc);
181
182 return 0;
183 fail:
184 free_vlc(&vlc);
185 return AVERROR_INVALIDDATA;
186 }
187
188 static const int rgb_order[4] = { 1, 2, 0, 3 };
189
190 static void restore_rgb_planes(uint8_t *src, int step, int stride, int width, int height)
191 {
192 int i, j;
193 uint8_t r, g, b;
194
195 for (j = 0; j < height; j++) {
196 for (i = 0; i < width * step; i += step) {
197 r = src[i];
198 g = src[i + 1];
199 b = src[i + 2];
200 src[i] = r + g - 0x80;
201 src[i + 2] = b + g - 0x80;
202 }
203 src += stride;
204 }
205 }
206
207 static void restore_median(uint8_t *src, int step, int stride,
208 int width, int height, int slices, int rmode)
209 {
210 int i, j, slice;
211 int A, B, C;
212 uint8_t *bsrc;
213 int slice_start, slice_height;
214 const int cmask = ~rmode;
215
216 for (slice = 0; slice < slices; slice++) {
217 slice_start = ((slice * height) / slices) & cmask;
218 slice_height = ((((slice + 1) * height) / slices) & cmask) - slice_start;
219
220 bsrc = src + slice_start * stride;
221
222 // first line - left neighbour prediction
223 bsrc[0] += 0x80;
224 A = bsrc[0];
225 for (i = step; i < width * step; i += step) {
226 bsrc[i] += A;
227 A = bsrc[i];
228 }
229 bsrc += stride;
230 if (slice_height == 1)
231 continue;
232 // second line - first element has top predition, the rest uses median
233 C = bsrc[-stride];
234 bsrc[0] += C;
235 A = bsrc[0];
236 for (i = step; i < width * step; i += step) {
237 B = bsrc[i - stride];
238 bsrc[i] += mid_pred(A, B, (uint8_t)(A + B - C));
239 C = B;
240 A = bsrc[i];
241 }
242 bsrc += stride;
243 // the rest of lines use continuous median prediction
244 for (j = 2; j < slice_height; j++) {
245 for (i = 0; i < width * step; i += step) {
246 B = bsrc[i - stride];
247 bsrc[i] += mid_pred(A, B, (uint8_t)(A + B - C));
248 C = B;
249 A = bsrc[i];
250 }
251 bsrc += stride;
252 }
253 }
254 }
255
256 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt)
257 {
258 const uint8_t *buf = avpkt->data;
259 int buf_size = avpkt->size;
260 const uint8_t *buf_end = buf + buf_size;
261 UtvideoContext *c = avctx->priv_data;
262 const uint8_t *ptr;
263 int i, j;
264 const uint8_t *plane_start[5];
265 int plane_size, max_slice_size = 0, slice_start, slice_end, slice_size;
266 int ret;
267
268 if (c->pic.data[0])
269 avctx->release_buffer(avctx, &c->pic);
270
271 c->pic.reference = 1;
272 c->pic.buffer_hints = FF_BUFFER_HINTS_VALID;
273 if ((ret = avctx->get_buffer(avctx, &c->pic)) < 0) {
274 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
275 return ret;
276 }
277
278 /* parse plane structure to retrieve frame flags and validate slice offsets */
279 ptr = buf;
280 for (i = 0; i < c->planes; i++) {
281 plane_start[i] = ptr;
282 if (buf_end - ptr < 256 + 4 * c->slices) {
283 av_log(avctx, AV_LOG_ERROR, "Insufficient data for a plane\n");
284 return AVERROR_INVALIDDATA;
285 }
286 ptr += 256;
287 slice_start = 0;
288 slice_end = 0;
289 for (j = 0; j < c->slices; j++) {
290 slice_end = bytestream_get_le32(&ptr);
291 slice_size = slice_end - slice_start;
292 if (slice_size < 0) {
293 av_log(avctx, AV_LOG_ERROR, "Incorrect slice size\n");
294 return AVERROR_INVALIDDATA;
295 }
296 slice_start = slice_end;
297 max_slice_size = FFMAX(max_slice_size, slice_size);
298 }
299 plane_size = slice_end;
300 if (buf_end - ptr < plane_size) {
301 av_log(avctx, AV_LOG_ERROR, "Plane size is bigger than available data\n");
302 return AVERROR_INVALIDDATA;
303 }
304 ptr += plane_size;
305 }
306 plane_start[c->planes] = ptr;
307 if (buf_end - ptr < c->frame_info_size) {
308 av_log(avctx, AV_LOG_ERROR, "Not enough data for frame information\n");
309 return AVERROR_INVALIDDATA;
310 }
311 c->frame_info = AV_RL32(ptr);
312 av_log(avctx, AV_LOG_DEBUG, "frame information flags %X\n", c->frame_info);
313
314 c->frame_pred = (c->frame_info >> 8) & 3;
315
316 if (c->frame_pred == PRED_GRADIENT) {
317 av_log_ask_for_sample(avctx, "Frame uses gradient prediction\n");
318 return AVERROR_PATCHWELCOME;
319 }
320
321 av_fast_malloc(&c->slice_bits, &c->slice_bits_size,
322 max_slice_size + FF_INPUT_BUFFER_PADDING_SIZE);
323
324 if (!c->slice_bits) {
325 av_log(avctx, AV_LOG_ERROR, "Cannot allocate temporary buffer\n");
326 return AVERROR(ENOMEM);
327 }
328
329 switch (c->avctx->pix_fmt) {
330 case PIX_FMT_RGB24:
331 case PIX_FMT_RGBA:
332 for (i = 0; i < c->planes; i++) {
333 ret = decode_plane(c, i, c->pic.data[0] + rgb_order[i], c->planes,
334 c->pic.linesize[0], avctx->width, avctx->height,
335 plane_start[i], plane_start[i + 1] - plane_start[i],
336 c->frame_pred == PRED_LEFT);
337 if (ret)
338 return ret;
339 if (c->frame_pred == PRED_MEDIAN)
340 restore_median(c->pic.data[0] + rgb_order[i], c->planes,
341 c->pic.linesize[0], avctx->width, avctx->height,
342 c->slices, 0);
343 }
344 restore_rgb_planes(c->pic.data[0], c->planes, c->pic.linesize[0],
345 avctx->width, avctx->height);
346 break;
347 case PIX_FMT_YUV420P:
348 for (i = 0; i < 3; i++) {
349 ret = decode_plane(c, i, c->pic.data[i], 1,
350 c->pic.linesize[i], avctx->width >> !!i, avctx->height >> !!i,
351 plane_start[i], plane_start[i + 1] - plane_start[i],
352 c->frame_pred == PRED_LEFT);
353 if (ret)
354 return ret;
355 if (c->frame_pred == PRED_MEDIAN)
356 restore_median(c->pic.data[i], 1, c->pic.linesize[i],
357 avctx->width >> !!i, avctx->height >> !!i,
358 c->slices, !i);
359 }
360 break;
361 case PIX_FMT_YUV422P:
362 for (i = 0; i < 3; i++) {
363 ret = decode_plane(c, i, c->pic.data[i], 1,
364 c->pic.linesize[i], avctx->width >> !!i, avctx->height,
365 plane_start[i], plane_start[i + 1] - plane_start[i],
366 c->frame_pred == PRED_LEFT);
367 if (ret)
368 return ret;
369 if (c->frame_pred == PRED_MEDIAN)
370 restore_median(c->pic.data[i], 1, c->pic.linesize[i],
371 avctx->width >> !!i, avctx->height, c->slices, 0);
372 }
373 break;
374 }
375
376 *data_size = sizeof(AVFrame);
377 *(AVFrame*)data = c->pic;
378
379 /* always report that the buffer was completely consumed */
380 return buf_size;
381 }
382
383 static av_cold int decode_init(AVCodecContext *avctx)
384 {
385 UtvideoContext * const c = avctx->priv_data;
386
387 c->avctx = avctx;
388
389 dsputil_init(&c->dsp, avctx);
390
391 if (avctx->extradata_size < 16) {
392 av_log(avctx, AV_LOG_ERROR, "Insufficient extradata size %d, should be at least 16\n",
393 avctx->extradata_size);
394 return AVERROR_INVALIDDATA;
395 }
396
397 av_log(avctx, AV_LOG_DEBUG, "Encoder version %d.%d.%d.%d\n",
398 avctx->extradata[3], avctx->extradata[2],
399 avctx->extradata[1], avctx->extradata[0]);
400 av_log(avctx, AV_LOG_DEBUG, "Original format %X\n", AV_RB32(avctx->extradata + 4));
401 c->frame_info_size = AV_RL32(avctx->extradata + 8);
402 c->flags = AV_RL32(avctx->extradata + 12);
403
404 if (c->frame_info_size != 4)
405 av_log_ask_for_sample(avctx, "Frame info is not 4 bytes\n");
406 av_log(avctx, AV_LOG_DEBUG, "Encoding parameters %08X\n", c->flags);
407 c->slices = (c->flags >> 24) + 1;
408 c->compression = c->flags & 1;
409 c->interlaced = c->flags & 0x800;
410
411 c->slice_bits_size = 0;
412
413 switch (avctx->codec_tag) {
414 case MKTAG('U', 'L', 'R', 'G'):
415 c->planes = 3;
416 avctx->pix_fmt = PIX_FMT_RGB24;
417 break;
418 case MKTAG('U', 'L', 'R', 'A'):
419 c->planes = 4;
420 avctx->pix_fmt = PIX_FMT_RGBA;
421 break;
422 case MKTAG('U', 'L', 'Y', '0'):
423 c->planes = 3;
424 avctx->pix_fmt = PIX_FMT_YUV420P;
425 break;
426 case MKTAG('U', 'L', 'Y', '2'):
427 c->planes = 3;
428 avctx->pix_fmt = PIX_FMT_YUV422P;
429 break;
430 default:
431 av_log(avctx, AV_LOG_ERROR, "Unknown Ut Video FOURCC provided (%08X)\n",
432 avctx->codec_tag);
433 return AVERROR_INVALIDDATA;
434 }
435
436 return 0;
437 }
438
439 static av_cold int decode_end(AVCodecContext *avctx)
440 {
441 UtvideoContext * const c = avctx->priv_data;
442
443 if (c->pic.data[0])
444 avctx->release_buffer(avctx, &c->pic);
445
446 av_freep(&c->slice_bits);
447
448 return 0;
449 }
450
451 AVCodec ff_utvideo_decoder = {
452 .name = "utvideo",
453 .type = AVMEDIA_TYPE_VIDEO,
454 .id = CODEC_ID_UTVIDEO,
455 .priv_data_size = sizeof(UtvideoContext),
456 .init = decode_init,
457 .close = decode_end,
458 .decode = decode_frame,
459 .capabilities = CODEC_CAP_DR1,
460 .long_name = NULL_IF_CONFIG_SMALL("Ut Video"),
461 };
462