Statistics
| Branch: | Revision:

ffmpeg / libavcodec / ulti.c @ 94d85eaf

History | View | Annotate | Download (14.4 KB)

1 d0a0bbd2 Mike Melanson
/*
2 282152b1 Alex Beregszaszi
 * IBM Ultimotion Video Decoder
3 d0a0bbd2 Mike Melanson
 * Copyright (C) 2004 Konstantin Shishkov
4
 *
5
 * This library is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU Lesser General Public
7
 * License as published by the Free Software Foundation; either
8
 * version 2 of the License, or (at your option) any later version.
9
 *
10
 * This library is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
 * Lesser General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU Lesser General Public
16
 * License along with this library; if not, write to the Free Software
17 5509bffa Diego Biurrun
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 d0a0bbd2 Mike Melanson
 *
19
 */
20
21
/**
22 115329f1 Diego Biurrun
 * @file ulti.c
23 d0a0bbd2 Mike Melanson
 * IBM Ultimotion Video Decoder.
24
 */
25 115329f1 Diego Biurrun
26 d0a0bbd2 Mike Melanson
#include <stdio.h>
27
#include <stdlib.h>
28
#include <string.h>
29
#include <unistd.h>
30
31
#include "common.h"
32
#include "avcodec.h"
33
34
#include "ulti_cb.h"
35
36
typedef struct UltimotionDecodeContext {
37
    AVCodecContext *avctx;
38
    int width, height, blocks;
39
    AVFrame frame;
40 bf4e3bd2 Måns Rullgård
    const uint8_t *ulti_codebook;
41 d0a0bbd2 Mike Melanson
} UltimotionDecodeContext;
42
43
static int ulti_decode_init(AVCodecContext *avctx)
44
{
45
    UltimotionDecodeContext *s = avctx->priv_data;
46
47
    s->avctx = avctx;
48
    s->width = avctx->width;
49
    s->height = avctx->height;
50
    s->blocks = (s->width / 8) * (s->height / 8);
51
    avctx->pix_fmt = PIX_FMT_YUV410P;
52
    avctx->has_b_frames = 0;
53
    avctx->coded_frame = (AVFrame*) &s->frame;
54
    s->ulti_codebook = ulti_codebook;
55
56
    return 0;
57
}
58
59
static int block_coords[8] = // 4x4 block coords in 8x8 superblock
60
    { 0, 0, 0, 4, 4, 4, 4, 0};
61
62
static int angle_by_index[4] = { 0, 2, 6, 12};
63
64
/* Lookup tables for luma and chroma - used by ulti_convert_yuv() */
65
static uint8_t ulti_lumas[64] =
66
    { 0x10, 0x13, 0x17, 0x1A, 0x1E, 0x21, 0x25, 0x28,
67
      0x2C, 0x2F, 0x33, 0x36, 0x3A, 0x3D, 0x41, 0x44,
68
      0x48, 0x4B, 0x4F, 0x52, 0x56, 0x59, 0x5C, 0x60,
69
      0x63, 0x67, 0x6A, 0x6E, 0x71, 0x75, 0x78, 0x7C,
70
      0x7F, 0x83, 0x86, 0x8A, 0x8D, 0x91, 0x94, 0x98,
71
      0x9B, 0x9F, 0xA2, 0xA5, 0xA9, 0xAC, 0xB0, 0xB3,
72
      0xB7, 0xBA, 0xBE, 0xC1, 0xC5, 0xC8, 0xCC, 0xCF,
73
      0xD3, 0xD6, 0xDA, 0xDD, 0xE1, 0xE4, 0xE8, 0xEB};
74 115329f1 Diego Biurrun
75 d0a0bbd2 Mike Melanson
static uint8_t ulti_chromas[16] =
76
    { 0x60, 0x67, 0x6D, 0x73, 0x7A, 0x80, 0x86, 0x8D,
77
      0x93, 0x99, 0xA0, 0xA6, 0xAC, 0xB3, 0xB9, 0xC0};
78 115329f1 Diego Biurrun
79 d0a0bbd2 Mike Melanson
/* convert Ultimotion YUV block (sixteen 6-bit Y samples and
80
 two 4-bit chroma samples) into standard YUV and put it into frame */
81
static void ulti_convert_yuv(AVFrame *frame, int x, int y,
82 bb270c08 Diego Biurrun
                             uint8_t *luma,int chroma)
83 d0a0bbd2 Mike Melanson
{
84
    uint8_t *y_plane, *cr_plane, *cb_plane;
85
    int i;
86 115329f1 Diego Biurrun
87 d0a0bbd2 Mike Melanson
    y_plane = frame->data[0] + x + y * frame->linesize[0];
88
    cr_plane = frame->data[1] + (x / 4) + (y / 4) * frame->linesize[1];
89
    cb_plane = frame->data[2] + (x / 4) + (y / 4) * frame->linesize[2];
90 115329f1 Diego Biurrun
91 d0a0bbd2 Mike Melanson
    cr_plane[0] = ulti_chromas[chroma >> 4];
92 115329f1 Diego Biurrun
93 d0a0bbd2 Mike Melanson
    cb_plane[0] = ulti_chromas[chroma & 0xF];
94
95 115329f1 Diego Biurrun
96 d0a0bbd2 Mike Melanson
    for(i = 0; i < 16; i++){
97 bb270c08 Diego Biurrun
        y_plane[i & 3] = ulti_lumas[luma[i]];
98
        if((i & 3) == 3) { //next row
99
            y_plane += frame->linesize[0];
100
        }
101 d0a0bbd2 Mike Melanson
    }
102
}
103
104
/* generate block like in MS Video1 */
105
static void ulti_pattern(AVFrame *frame, int x, int y,
106 bb270c08 Diego Biurrun
                         int f0, int f1, int Y0, int Y1, int chroma)
107 d0a0bbd2 Mike Melanson
{
108
    uint8_t Luma[16];
109
    int mask, i;
110
    for(mask = 0x80, i = 0; mask; mask >>= 1, i++) {
111 bb270c08 Diego Biurrun
        if(f0 & mask)
112
            Luma[i] = Y1;
113
        else
114
            Luma[i] = Y0;
115 d0a0bbd2 Mike Melanson
    }
116 115329f1 Diego Biurrun
117 d0a0bbd2 Mike Melanson
    for(mask = 0x80, i = 8; mask; mask >>= 1, i++) {
118 bb270c08 Diego Biurrun
        if(f1 & mask)
119
            Luma[i] = Y1;
120
        else
121
            Luma[i] = Y0;
122 d0a0bbd2 Mike Melanson
    }
123 115329f1 Diego Biurrun
124 d0a0bbd2 Mike Melanson
    ulti_convert_yuv(frame, x, y, Luma, chroma);
125
}
126
127
/* fill block with some gradient */
128
static void ulti_grad(AVFrame *frame, int x, int y, uint8_t *Y, int chroma, int angle)
129
{
130
    uint8_t Luma[16];
131
    if(angle & 8) { //reverse order
132 bb270c08 Diego Biurrun
        int t;
133
        angle &= 0x7;
134
        t = Y[0];
135
        Y[0] = Y[3];
136
        Y[3] = t;
137
        t = Y[1];
138
        Y[1] = Y[2];
139
        Y[2] = t;
140 d0a0bbd2 Mike Melanson
    }
141
    switch(angle){
142
    case 0:
143 bb270c08 Diego Biurrun
        Luma[0]  = Y[0]; Luma[1]  = Y[1]; Luma[2]  = Y[2]; Luma[3]  = Y[3];
144
        Luma[4]  = Y[0]; Luma[5]  = Y[1]; Luma[6]  = Y[2]; Luma[7]  = Y[3];
145
        Luma[8]  = Y[0]; Luma[9]  = Y[1]; Luma[10] = Y[2]; Luma[11] = Y[3];
146
        Luma[12] = Y[0]; Luma[13] = Y[1]; Luma[14] = Y[2]; Luma[15] = Y[3];
147
        break;
148 d0a0bbd2 Mike Melanson
    case 1:
149 bb270c08 Diego Biurrun
        Luma[0]  = Y[1]; Luma[1]  = Y[2]; Luma[2]  = Y[3]; Luma[3]  = Y[3];
150
        Luma[4]  = Y[0]; Luma[5]  = Y[1]; Luma[6]  = Y[2]; Luma[7]  = Y[3];
151
        Luma[8]  = Y[0]; Luma[9]  = Y[1]; Luma[10] = Y[2]; Luma[11] = Y[3];
152
        Luma[12] = Y[0]; Luma[13] = Y[0]; Luma[14] = Y[1]; Luma[15] = Y[2];
153
        break;
154 d0a0bbd2 Mike Melanson
    case 2:
155 bb270c08 Diego Biurrun
        Luma[0]  = Y[1]; Luma[1]  = Y[2]; Luma[2]  = Y[3]; Luma[3]  = Y[3];
156
        Luma[4]  = Y[1]; Luma[5]  = Y[2]; Luma[6]  = Y[2]; Luma[7]  = Y[3];
157
        Luma[8]  = Y[0]; Luma[9]  = Y[1]; Luma[10] = Y[1]; Luma[11] = Y[2];
158
        Luma[12] = Y[0]; Luma[13] = Y[0]; Luma[14] = Y[1]; Luma[15] = Y[2];
159
        break;
160 d0a0bbd2 Mike Melanson
    case 3:
161 bb270c08 Diego Biurrun
        Luma[0]  = Y[2]; Luma[1]  = Y[3]; Luma[2]  = Y[3]; Luma[3]  = Y[3];
162
        Luma[4]  = Y[1]; Luma[5]  = Y[2]; Luma[6]  = Y[2]; Luma[7]  = Y[3];
163
        Luma[8]  = Y[0]; Luma[9]  = Y[1]; Luma[10] = Y[1]; Luma[11] = Y[2];
164
        Luma[12] = Y[0]; Luma[13] = Y[0]; Luma[14] = Y[0]; Luma[15] = Y[1];
165
        break;
166 d0a0bbd2 Mike Melanson
    case 4:
167 bb270c08 Diego Biurrun
        Luma[0]  = Y[3]; Luma[1]  = Y[3]; Luma[2]  = Y[3]; Luma[3]  = Y[3];
168
        Luma[4]  = Y[2]; Luma[5]  = Y[2]; Luma[6]  = Y[2]; Luma[7]  = Y[2];
169
        Luma[8]  = Y[1]; Luma[9]  = Y[1]; Luma[10] = Y[1]; Luma[11] = Y[1];
170
        Luma[12] = Y[0]; Luma[13] = Y[0]; Luma[14] = Y[0]; Luma[15] = Y[0];
171
        break;
172 d0a0bbd2 Mike Melanson
    case 5:
173 bb270c08 Diego Biurrun
        Luma[0]  = Y[3]; Luma[1]  = Y[3]; Luma[2]  = Y[3]; Luma[3]  = Y[2];
174
        Luma[4]  = Y[3]; Luma[5]  = Y[2]; Luma[6]  = Y[2]; Luma[7]  = Y[1];
175
        Luma[8]  = Y[2]; Luma[9]  = Y[1]; Luma[10] = Y[1]; Luma[11] = Y[0];
176
        Luma[12] = Y[1]; Luma[13] = Y[0]; Luma[14] = Y[0]; Luma[15] = Y[0];
177
        break;
178 d0a0bbd2 Mike Melanson
    case 6:
179 bb270c08 Diego Biurrun
        Luma[0]  = Y[3]; Luma[1]  = Y[3]; Luma[2]  = Y[2]; Luma[3]  = Y[2];
180
        Luma[4]  = Y[3]; Luma[5]  = Y[2]; Luma[6]  = Y[1]; Luma[7]  = Y[1];
181
        Luma[8]  = Y[2]; Luma[9]  = Y[2]; Luma[10] = Y[1]; Luma[11] = Y[0];
182
        Luma[12] = Y[1]; Luma[13] = Y[1]; Luma[14] = Y[0]; Luma[15] = Y[0];
183
        break;
184 d0a0bbd2 Mike Melanson
    case 7:
185 bb270c08 Diego Biurrun
        Luma[0]  = Y[3]; Luma[1]  = Y[3]; Luma[2]  = Y[2]; Luma[3]  = Y[1];
186
        Luma[4]  = Y[3]; Luma[5]  = Y[2]; Luma[6]  = Y[1]; Luma[7]  = Y[0];
187
        Luma[8]  = Y[3]; Luma[9]  = Y[2]; Luma[10] = Y[1]; Luma[11] = Y[0];
188
        Luma[12] = Y[2]; Luma[13] = Y[1]; Luma[14] = Y[0]; Luma[15] = Y[0];
189
        break;
190 d0a0bbd2 Mike Melanson
    default:
191 bb270c08 Diego Biurrun
        Luma[0]  = Y[0]; Luma[1]  = Y[0]; Luma[2]  = Y[1]; Luma[3]  = Y[1];
192
        Luma[4]  = Y[0]; Luma[5]  = Y[0]; Luma[6]  = Y[1]; Luma[7]  = Y[1];
193
        Luma[8]  = Y[2]; Luma[9]  = Y[2]; Luma[10] = Y[3]; Luma[11] = Y[3];
194
        Luma[12] = Y[2]; Luma[13] = Y[2]; Luma[14] = Y[3]; Luma[15] = Y[3];
195
        break;
196 d0a0bbd2 Mike Melanson
    }
197 115329f1 Diego Biurrun
198 d0a0bbd2 Mike Melanson
    ulti_convert_yuv(frame, x, y, Luma, chroma);
199
}
200
201 115329f1 Diego Biurrun
static int ulti_decode_frame(AVCodecContext *avctx,
202 d0a0bbd2 Mike Melanson
                             void *data, int *data_size,
203
                             uint8_t *buf, int buf_size)
204
{
205
    UltimotionDecodeContext *s=avctx->priv_data;
206
    int modifier = 0;
207
    int uniq = 0;
208
    int mode = 0;
209
    int blocks = 0;
210
    int done = 0;
211
    int x = 0, y = 0;
212
    int i;
213
    int skip;
214
    int tmp;
215
216
    if(s->frame.data[0])
217
        avctx->release_buffer(avctx, &s->frame);
218
219
    s->frame.reference = 1;
220
    s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
221
    if(avctx->get_buffer(avctx, &s->frame) < 0) {
222
        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
223
        return -1;
224
    }
225 115329f1 Diego Biurrun
226 d0a0bbd2 Mike Melanson
    while(!done) {
227 bb270c08 Diego Biurrun
        int idx;
228
        if(blocks >= s->blocks || y >= s->height)
229
            break;//all blocks decoded
230
231
        idx = *buf++;
232
        if((idx & 0xF8) == 0x70) {
233
            switch(idx) {
234
            case 0x70: //change modifier
235
                modifier = *buf++;
236
                if(modifier>1)
237
                    av_log(avctx, AV_LOG_INFO, "warning: modifier must be 0 or 1, got %i\n", modifier);
238
                break;
239
            case 0x71: // set uniq flag
240
                uniq = 1;
241
                break;
242
            case 0x72: //toggle mode
243
                mode = !mode;
244
                break;
245
            case 0x73: //end-of-frame
246
                done = 1;
247
                break;
248
            case 0x74: //skip some blocks
249
                skip = *buf++;
250
                if ((blocks + skip) >= s->blocks)
251
                    break;
252
                blocks += skip;
253
                x += skip * 8;
254
                while(x >= s->width) {
255
                    x -= s->width;
256
                    y += 8;
257
                }
258
                break;
259
            default:
260
                av_log(avctx, AV_LOG_INFO, "warning: unknown escape 0x%02X\n", idx);
261
            }
262
        } else { //handle one block
263
            int code;
264
            int cf;
265
            int angle = 0;
266
            uint8_t Y[4]; // luma samples of block
267
            int tx = 0, ty = 0; //coords of subblock
268
            int chroma = 0;
269
            if (mode || uniq) {
270
                uniq = 0;
271
                cf = 1;
272
                chroma = 0;
273
            } else {
274
                cf = 0;
275
                if (idx)
276
                    chroma = *buf++;
277
            }
278
            for (i = 0; i < 4; i++) { // for every subblock
279
                code = (idx >> (6 - i*2)) & 3; //extract 2 bits
280
                if(!code) //skip subblock
281
                    continue;
282
                if(cf)
283
                    chroma = *buf++;
284
                tx = x + block_coords[i * 2];
285
                ty = y + block_coords[(i * 2) + 1];
286
                switch(code) {
287
                case 1:
288
                    tmp = *buf++;
289
290
                    angle = angle_by_index[(tmp >> 6) & 0x3];
291
292
                    Y[0] = tmp & 0x3F;
293
                    Y[1] = Y[0];
294
295
                    if (angle) {
296
                        Y[2] = Y[0]+1;
297
                        if (Y[2] > 0x3F)
298
                            Y[2] = 0x3F;
299
                        Y[3] = Y[2];
300
                    } else {
301
                        Y[2] = Y[0];
302
                        Y[3] = Y[0];
303
                    }
304
                    break;
305
306
                case 2:
307
                    if (modifier) { // unpack four luma samples
308
                        tmp = (*buf++) << 16;
309
                        tmp += (*buf++) << 8;
310
                        tmp += *buf++;
311
312
                        Y[0] = (tmp >> 18) & 0x3F;
313
                        Y[1] = (tmp >> 12) & 0x3F;
314
                        Y[2] = (tmp >> 6) & 0x3F;
315
                        Y[3] = tmp & 0x3F;
316
                        angle = 16;
317
                    } else { // retrieve luma samples from codebook
318
                        tmp = (*buf++) << 8;
319
                        tmp += (*buf++);
320
321
                        angle = (tmp >> 12) & 0xF;
322
                        tmp &= 0xFFF;
323
                        tmp <<= 2;
324
                        Y[0] = s->ulti_codebook[tmp];
325
                        Y[1] = s->ulti_codebook[tmp + 1];
326
                        Y[2] = s->ulti_codebook[tmp + 2];
327
                        Y[3] = s->ulti_codebook[tmp + 3];
328
                    }
329
                    break;
330
331
                case 3:
332
                    if (modifier) { // all 16 luma samples
333
                        uint8_t Luma[16];
334
335
                        tmp = (*buf++) << 16;
336
                        tmp += (*buf++) << 8;
337
                        tmp += *buf++;
338
                        Luma[0] = (tmp >> 18) & 0x3F;
339
                        Luma[1] = (tmp >> 12) & 0x3F;
340
                        Luma[2] = (tmp >> 6) & 0x3F;
341
                        Luma[3] = tmp & 0x3F;
342
343
                        tmp = (*buf++) << 16;
344
                        tmp += (*buf++) << 8;
345
                        tmp += *buf++;
346
                        Luma[4] = (tmp >> 18) & 0x3F;
347
                        Luma[5] = (tmp >> 12) & 0x3F;
348
                        Luma[6] = (tmp >> 6) & 0x3F;
349
                        Luma[7] = tmp & 0x3F;
350
351
                        tmp = (*buf++) << 16;
352
                        tmp += (*buf++) << 8;
353
                        tmp += *buf++;
354
                        Luma[8] = (tmp >> 18) & 0x3F;
355
                        Luma[9] = (tmp >> 12) & 0x3F;
356
                        Luma[10] = (tmp >> 6) & 0x3F;
357
                        Luma[11] = tmp & 0x3F;
358
359
                        tmp = (*buf++) << 16;
360
                        tmp += (*buf++) << 8;
361
                        tmp += *buf++;
362
                        Luma[12] = (tmp >> 18) & 0x3F;
363
                        Luma[13] = (tmp >> 12) & 0x3F;
364
                        Luma[14] = (tmp >> 6) & 0x3F;
365
                        Luma[15] = tmp & 0x3F;
366
367
                        ulti_convert_yuv(&s->frame, tx, ty, Luma, chroma);
368
                    } else {
369
                        tmp = *buf++;
370
                        if(tmp & 0x80) {
371
                            angle = (tmp >> 4) & 0x7;
372
                            tmp = (tmp << 8) + *buf++;
373
                            Y[0] = (tmp >> 6) & 0x3F;
374
                            Y[1] = tmp & 0x3F;
375
                            Y[2] = (*buf++) & 0x3F;
376
                            Y[3] = (*buf++) & 0x3F;
377
                            ulti_grad(&s->frame, tx, ty, Y, chroma, angle); //draw block
378
                        } else { // some patterns
379
                            int f0, f1;
380
                            f0 = *buf++;
381
                            f1 = tmp;
382
                            Y[0] = (*buf++) & 0x3F;
383
                            Y[1] = (*buf++) & 0x3F;
384
                            ulti_pattern(&s->frame, tx, ty, f1, f0, Y[0], Y[1], chroma);
385
                        }
386
                    }
387
                    break;
388
                }
389
                if(code != 3)
390
                    ulti_grad(&s->frame, tx, ty, Y, chroma, angle); // draw block
391
            }
392
            blocks++;
393
                x += 8;
394
            if(x >= s->width) {
395
                x = 0;
396
                y += 8;
397
            }
398
        }
399 d0a0bbd2 Mike Melanson
    }
400 115329f1 Diego Biurrun
401 d0a0bbd2 Mike Melanson
    *data_size=sizeof(AVFrame);
402
    *(AVFrame*)data= s->frame;
403
404
    return buf_size;
405
}
406
407
static int ulti_decode_end(AVCodecContext *avctx)
408
{
409
/*    UltimotionDecodeContext *s = avctx->priv_data;*/
410
411
    return 0;
412
}
413
414
AVCodec ulti_decoder = {
415
    "ultimotion",
416
    CODEC_TYPE_VIDEO,
417
    CODEC_ID_ULTI,
418
    sizeof(UltimotionDecodeContext),
419
    ulti_decode_init,
420
    NULL,
421
    ulti_decode_end,
422
    ulti_decode_frame,
423
    CODEC_CAP_DR1,
424
    NULL
425
};