Statistics
| Branch: | Revision:

ffmpeg / libavcodec / roqvideo.c @ bb54f6ab

History | View | Annotate | Download (14.5 KB)

1 3ef8be2b Mike Melanson
/*
2
 * Copyright (C) 2003 the ffmpeg project
3
 *
4 b78e7197 Diego Biurrun
 * This file is part of FFmpeg.
5
 *
6
 * FFmpeg is free software; you can redistribute it and/or
7 3ef8be2b Mike Melanson
 * modify it under the terms of the GNU Lesser General Public
8
 * License as published by the Free Software Foundation; either
9 b78e7197 Diego Biurrun
 * version 2.1 of the License, or (at your option) any later version.
10 3ef8be2b Mike Melanson
 *
11 b78e7197 Diego Biurrun
 * FFmpeg is distributed in the hope that it will be useful,
12 3ef8be2b Mike Melanson
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
 * Lesser General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public
17 b78e7197 Diego Biurrun
 * License along with FFmpeg; if not, write to the Free Software
18 5509bffa Diego Biurrun
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 3ef8be2b Mike Melanson
 *
20
 */
21
22
/**
23
 * @file roqvideo.c
24
 * Id RoQ Video Decoder by Dr. Tim Ferguson
25
 * For more information about the Id RoQ format, visit:
26
 *   http://www.csse.monash.edu.au/~timf/
27
 */
28
29
#include <stdio.h>
30
#include <stdlib.h>
31
#include <string.h>
32
#include <unistd.h>
33
34
#include "common.h"
35
#include "avcodec.h"
36
#include "dsputil.h"
37
38
typedef struct {
39
  unsigned char y0, y1, y2, y3, u, v;
40
} roq_cell;
41
42
typedef struct {
43
  int idx[4];
44
} roq_qcell;
45
46 ec59fd53 Mike Melanson
static int uiclip[1024], *uiclp;  /* clipping table */
47
#define avg2(a,b) uiclp[(((int)(a)+(int)(b)+1)>>1)]
48
#define avg4(a,b,c,d) uiclp[(((int)(a)+(int)(b)+(int)(c)+(int)(d)+2)>>2)]
49 3ef8be2b Mike Melanson
50
typedef struct RoqContext {
51
52
    AVCodecContext *avctx;
53
    DSPContext dsp;
54
    AVFrame last_frame;
55
    AVFrame current_frame;
56
    int first_frame;
57
    int y_stride;
58
    int c_stride;
59
60
    roq_cell cells[256];
61
    roq_qcell qcells[256];
62
63
    unsigned char *buf;
64
    int size;
65
66
} RoqContext;
67
68
#define RoQ_INFO              0x1001
69
#define RoQ_QUAD_CODEBOOK     0x1002
70
#define RoQ_QUAD_VQ           0x1011
71
#define RoQ_SOUND_MONO        0x1020
72
#define RoQ_SOUND_STEREO      0x1021
73
74
#define RoQ_ID_MOT              0x00
75
#define RoQ_ID_FCC              0x01
76
#define RoQ_ID_SLD              0x02
77
#define RoQ_ID_CCC              0x03
78
79
#define get_byte(in_buffer) *(in_buffer++)
80
#define get_word(in_buffer) ((unsigned short)(in_buffer += 2, \
81
  (in_buffer[-1] << 8 | in_buffer[-2])))
82
#define get_long(in_buffer) ((unsigned long)(in_buffer += 4, \
83
  (in_buffer[-1] << 24 | in_buffer[-2] << 16 | in_buffer[-3] << 8 | in_buffer[-4])))
84
85
86
static void apply_vector_2x2(RoqContext *ri, int x, int y, roq_cell *cell)
87
{
88
    unsigned char *yptr;
89
90
    yptr = ri->current_frame.data[0] + (y * ri->y_stride) + x;
91
    *yptr++ = cell->y0;
92
    *yptr++ = cell->y1;
93
    yptr += (ri->y_stride - 2);
94
    *yptr++ = cell->y2;
95
    *yptr++ = cell->y3;
96
    ri->current_frame.data[1][(y/2) * (ri->c_stride) + x/2] = cell->u;
97
    ri->current_frame.data[2][(y/2) * (ri->c_stride) + x/2] = cell->v;
98
}
99
100
static void apply_vector_4x4(RoqContext *ri, int x, int y, roq_cell *cell)
101
{
102
    unsigned long row_inc, c_row_inc;
103
    register unsigned char y0, y1, u, v;
104
    unsigned char *yptr, *uptr, *vptr;
105
106
    yptr = ri->current_frame.data[0] + (y * ri->y_stride) + x;
107
    uptr = ri->current_frame.data[1] + (y/2) * (ri->c_stride) + x/2;
108
    vptr = ri->current_frame.data[2] + (y/2) * (ri->c_stride) + x/2;
109
110
    row_inc = ri->y_stride - 4;
111
    c_row_inc = (ri->c_stride) - 2;
112
    *yptr++ = y0 = cell->y0; *uptr++ = u = cell->u; *vptr++ = v = cell->v;
113
    *yptr++ = y0;
114
    *yptr++ = y1 = cell->y1; *uptr++ = u; *vptr++ = v;
115
    *yptr++ = y1;
116
117
    yptr += row_inc;
118
119
    *yptr++ = y0;
120
    *yptr++ = y0;
121
    *yptr++ = y1;
122
    *yptr++ = y1;
123
124
    yptr += row_inc; uptr += c_row_inc; vptr += c_row_inc;
125
126
    *yptr++ = y0 = cell->y2; *uptr++ = u; *vptr++ = v;
127
    *yptr++ = y0;
128
    *yptr++ = y1 = cell->y3; *uptr++ = u; *vptr++ = v;
129
    *yptr++ = y1;
130
131
    yptr += row_inc;
132
133
    *yptr++ = y0;
134
    *yptr++ = y0;
135
    *yptr++ = y1;
136
    *yptr++ = y1;
137
}
138
139
static void apply_motion_4x4(RoqContext *ri, int x, int y, unsigned char mv,
140 ec59fd53 Mike Melanson
    signed char mean_x, signed char mean_y)
141 3ef8be2b Mike Melanson
{
142 ec59fd53 Mike Melanson
    int i, hw, mx, my;
143 3ef8be2b Mike Melanson
    unsigned char *pa, *pb;
144
145
    mx = x + 8 - (mv >> 4) - mean_x;
146
    my = y + 8 - (mv & 0xf) - mean_y;
147
148 b9029997 Mike Melanson
    /* check MV against frame boundaries */
149
    if ((mx < 0) || (mx > ri->avctx->width - 4) ||
150
        (my < 0) || (my > ri->avctx->height - 4)) {
151
        av_log(ri->avctx, AV_LOG_ERROR, "motion vector out of bounds: MV = (%d, %d), boundaries = (0, 0, %d, %d)\n",
152
            mx, my, ri->avctx->width, ri->avctx->height);
153
        return;
154
    }
155
156 3ef8be2b Mike Melanson
    pa = ri->current_frame.data[0] + (y * ri->y_stride) + x;
157
    pb = ri->last_frame.data[0] + (my * ri->y_stride) + mx;
158
    for(i = 0; i < 4; i++) {
159
        pa[0] = pb[0];
160
        pa[1] = pb[1];
161
        pa[2] = pb[2];
162
        pa[3] = pb[3];
163
        pa += ri->y_stride;
164
        pb += ri->y_stride;
165
    }
166
167 ec59fd53 Mike Melanson
    hw = ri->y_stride/2;
168
    pa = ri->current_frame.data[1] + (y * ri->y_stride)/4 + x/2;
169
    pb = ri->last_frame.data[1] + (my/2) * (ri->y_stride/2) + (mx + 1)/2;
170
171
    for(i = 0; i < 2; i++) {
172
        switch(((my & 0x01) << 1) | (mx & 0x01)) {
173
174
        case 0:
175
            pa[0] = pb[0];
176
            pa[1] = pb[1];
177
            pa[hw] = pb[hw];
178
            pa[hw+1] = pb[hw+1];
179
            break;
180
181
        case 1:
182
            pa[0] = avg2(pb[0], pb[1]);
183
            pa[1] = avg2(pb[1], pb[2]);
184
            pa[hw] = avg2(pb[hw], pb[hw+1]);
185
            pa[hw+1] = avg2(pb[hw+1], pb[hw+2]);
186
            break;
187
188
        case 2:
189
            pa[0] = avg2(pb[0], pb[hw]);
190
            pa[1] = avg2(pb[1], pb[hw+1]);
191
            pa[hw] = avg2(pb[hw], pb[hw*2]);
192
            pa[hw+1] = avg2(pb[hw+1], pb[(hw*2)+1]);
193
            break;
194
195
        case 3:
196
            pa[0] = avg4(pb[0], pb[1], pb[hw], pb[hw+1]);
197
            pa[1] = avg4(pb[1], pb[2], pb[hw+1], pb[hw+2]);
198
            pa[hw] = avg4(pb[hw], pb[hw+1], pb[hw*2], pb[(hw*2)+1]);
199
            pa[hw+1] = avg4(pb[hw+1], pb[hw+2], pb[(hw*2)+1], pb[(hw*2)+1]);
200
            break;
201
        }
202
203
        pa = ri->current_frame.data[2] + (y * ri->y_stride)/4 + x/2;
204
        pb = ri->last_frame.data[2] + (my/2) * (ri->y_stride/2) + (mx + 1)/2;
205
    }
206 3ef8be2b Mike Melanson
}
207
208
static void apply_motion_8x8(RoqContext *ri, int x, int y,
209 ec59fd53 Mike Melanson
    unsigned char mv, signed char mean_x, signed char mean_y)
210 3ef8be2b Mike Melanson
{
211 ec59fd53 Mike Melanson
    int mx, my, i, j, hw;
212 3ef8be2b Mike Melanson
    unsigned char *pa, *pb;
213
214
    mx = x + 8 - (mv >> 4) - mean_x;
215
    my = y + 8 - (mv & 0xf) - mean_y;
216
217 b9029997 Mike Melanson
    /* check MV against frame boundaries */
218
    if ((mx < 0) || (mx > ri->avctx->width - 8) ||
219
        (my < 0) || (my > ri->avctx->height - 8)) {
220
        av_log(ri->avctx, AV_LOG_ERROR, "motion vector out of bounds: MV = (%d, %d), boundaries = (0, 0, %d, %d)\n",
221
            mx, my, ri->avctx->width, ri->avctx->height);
222
        return;
223
    }
224
225 3ef8be2b Mike Melanson
    pa = ri->current_frame.data[0] + (y * ri->y_stride) + x;
226
    pb = ri->last_frame.data[0] + (my * ri->y_stride) + mx;
227
    for(i = 0; i < 8; i++) {
228
        pa[0] = pb[0];
229
        pa[1] = pb[1];
230
        pa[2] = pb[2];
231
        pa[3] = pb[3];
232
        pa[4] = pb[4];
233
        pa[5] = pb[5];
234
        pa[6] = pb[6];
235
        pa[7] = pb[7];
236
        pa += ri->y_stride;
237
        pb += ri->y_stride;
238
    }
239
240 ec59fd53 Mike Melanson
    hw = ri->c_stride;
241
    pa = ri->current_frame.data[1] + (y * ri->y_stride)/4 + x/2;
242
    pb = ri->last_frame.data[1] + (my/2) * (ri->y_stride/2) + (mx + 1)/2;
243
    for(j = 0; j < 2; j++) {
244
        for(i = 0; i < 4; i++) {
245
            switch(((my & 0x01) << 1) | (mx & 0x01)) {
246
247
            case 0:
248
                pa[0] = pb[0];
249
                pa[1] = pb[1];
250
                pa[2] = pb[2];
251
                pa[3] = pb[3];
252
                break;
253
254
            case 1:
255
                pa[0] = avg2(pb[0], pb[1]);
256
                pa[1] = avg2(pb[1], pb[2]);
257
                pa[2] = avg2(pb[2], pb[3]);
258
                pa[3] = avg2(pb[3], pb[4]);
259
                break;
260 115329f1 Diego Biurrun
261 ec59fd53 Mike Melanson
            case 2:
262
                pa[0] = avg2(pb[0], pb[hw]);
263
                pa[1] = avg2(pb[1], pb[hw+1]);
264
                pa[2] = avg2(pb[2], pb[hw+2]);
265
                pa[3] = avg2(pb[3], pb[hw+3]);
266
                break;
267
268
            case 3:
269
                pa[0] = avg4(pb[0], pb[1], pb[hw], pb[hw+1]);
270
                pa[1] = avg4(pb[1], pb[2], pb[hw+1], pb[hw+2]);
271
                pa[2] = avg4(pb[2], pb[3], pb[hw+2], pb[hw+3]);
272
                pa[3] = avg4(pb[3], pb[4], pb[hw+3], pb[hw+4]);
273
                break;
274
            }
275
            pa += ri->c_stride;
276
            pb += ri->c_stride;
277
        }
278
279
        pa = ri->current_frame.data[2] + (y * ri->y_stride)/4 + x/2;
280
        pb = ri->last_frame.data[2] + (my/2) * (ri->y_stride/2) + (mx + 1)/2;
281
    }
282 3ef8be2b Mike Melanson
}
283
284
static void roqvideo_decode_frame(RoqContext *ri)
285
{
286
    unsigned int chunk_id = 0, chunk_arg = 0;
287
    unsigned long chunk_size = 0;
288
    int i, j, k, nv1, nv2, vqflg = 0, vqflg_pos = -1;
289
    int vqid, bpos, xpos, ypos, xp, yp, x, y;
290
    int frame_stats[2][4] = {{0},{0}};
291
    roq_qcell *qcell;
292
    unsigned char *buf = ri->buf;
293
    unsigned char *buf_end = ri->buf + ri->size;
294
295
    while (buf < buf_end) {
296
        chunk_id = get_word(buf);
297
        chunk_size = get_long(buf);
298
        chunk_arg = get_word(buf);
299
300
        if(chunk_id == RoQ_QUAD_VQ)
301
            break;
302
        if(chunk_id == RoQ_QUAD_CODEBOOK) {
303
            if((nv1 = chunk_arg >> 8) == 0)
304
                nv1 = 256;
305
            if((nv2 = chunk_arg & 0xff) == 0 && nv1 * 6 < chunk_size)
306
                nv2 = 256;
307
            for(i = 0; i < nv1; i++) {
308
                ri->cells[i].y0 = get_byte(buf);
309
                ri->cells[i].y1 = get_byte(buf);
310
                ri->cells[i].y2 = get_byte(buf);
311
                ri->cells[i].y3 = get_byte(buf);
312
                ri->cells[i].u = get_byte(buf);
313
                ri->cells[i].v = get_byte(buf);
314
            }
315
            for(i = 0; i < nv2; i++)
316
                for(j = 0; j < 4; j++)
317
                    ri->qcells[i].idx[j] = get_byte(buf);
318
        }
319
    }
320
321
    bpos = xpos = ypos = 0;
322
    while(bpos < chunk_size) {
323
        for (yp = ypos; yp < ypos + 16; yp += 8)
324
            for (xp = xpos; xp < xpos + 16; xp += 8) {
325
                if (vqflg_pos < 0) {
326
                    vqflg = buf[bpos++]; vqflg |= (buf[bpos++] << 8);
327
                    vqflg_pos = 7;
328
                }
329
                vqid = (vqflg >> (vqflg_pos * 2)) & 0x3;
330
                frame_stats[0][vqid]++;
331
                vqflg_pos--;
332
333
                switch(vqid) {
334
                case RoQ_ID_MOT:
335
                    apply_motion_8x8(ri, xp, yp, 0, 8, 8);
336
                    break;
337
                case RoQ_ID_FCC:
338
                    apply_motion_8x8(ri, xp, yp, buf[bpos++], chunk_arg >> 8,
339
                        chunk_arg & 0xff);
340
                    break;
341
                case RoQ_ID_SLD:
342
                    qcell = ri->qcells + buf[bpos++];
343
                    apply_vector_4x4(ri, xp, yp, ri->cells + qcell->idx[0]);
344
                    apply_vector_4x4(ri, xp+4, yp, ri->cells + qcell->idx[1]);
345
                    apply_vector_4x4(ri, xp, yp+4, ri->cells + qcell->idx[2]);
346
                    apply_vector_4x4(ri, xp+4, yp+4, ri->cells + qcell->idx[3]);
347
                    break;
348
                case RoQ_ID_CCC:
349
                    for (k = 0; k < 4; k++) {
350
                        x = xp; y = yp;
351
                        if(k & 0x01) x += 4;
352
                        if(k & 0x02) y += 4;
353
354
                        if (vqflg_pos < 0) {
355
                            vqflg = buf[bpos++];
356
                            vqflg |= (buf[bpos++] << 8);
357
                            vqflg_pos = 7;
358
                        }
359
                        vqid = (vqflg >> (vqflg_pos * 2)) & 0x3;
360
                        frame_stats[1][vqid]++;
361
                        vqflg_pos--;
362
                        switch(vqid) {
363
                        case RoQ_ID_MOT:
364
                            apply_motion_4x4(ri, x, y, 0, 8, 8);
365
                            break;
366
                        case RoQ_ID_FCC:
367 115329f1 Diego Biurrun
                            apply_motion_4x4(ri, x, y, buf[bpos++],
368 3ef8be2b Mike Melanson
                                chunk_arg >> 8, chunk_arg & 0xff);
369
                            break;
370
                        case RoQ_ID_SLD:
371
                            qcell = ri->qcells + buf[bpos++];
372
                            apply_vector_2x2(ri, x, y, ri->cells + qcell->idx[0]);
373
                            apply_vector_2x2(ri, x+2, y, ri->cells + qcell->idx[1]);
374
                            apply_vector_2x2(ri, x, y+2, ri->cells + qcell->idx[2]);
375
                            apply_vector_2x2(ri, x+2, y+2, ri->cells + qcell->idx[3]);
376
                            break;
377
                        case RoQ_ID_CCC:
378
                            apply_vector_2x2(ri, x, y, ri->cells + buf[bpos]);
379
                            apply_vector_2x2(ri, x+2, y, ri->cells + buf[bpos+1]);
380
                            apply_vector_2x2(ri, x, y+2, ri->cells + buf[bpos+2]);
381
                            apply_vector_2x2(ri, x+2, y+2, ri->cells + buf[bpos+3]);
382
                            bpos += 4;
383
                            break;
384
                        }
385
                    }
386
                    break;
387
                default:
388 9b879566 Michel Bardiaux
                    av_log(ri->avctx, AV_LOG_ERROR, "Unknown vq code: %d\n", vqid);
389 3ef8be2b Mike Melanson
            }
390
        }
391
392
        xpos += 16;
393
        if (xpos >= ri->avctx->width) {
394
            xpos -= ri->avctx->width;
395
            ypos += 16;
396
        }
397
        if(ypos >= ri->avctx->height)
398
            break;
399
    }
400
}
401
402
403
static int roq_decode_init(AVCodecContext *avctx)
404
{
405
    RoqContext *s = avctx->priv_data;
406 ec59fd53 Mike Melanson
    int i;
407 3ef8be2b Mike Melanson
408
    s->avctx = avctx;
409
    s->first_frame = 1;
410
    avctx->pix_fmt = PIX_FMT_YUV420P;
411
    avctx->has_b_frames = 0;
412
    dsputil_init(&s->dsp, avctx);
413
414 ec59fd53 Mike Melanson
    uiclp = uiclip+512;
415
    for(i = -512; i < 512; i++)
416
        uiclp[i] = (i < 0 ? 0 : (i > 255 ? 255 : i));
417
418 3ef8be2b Mike Melanson
    return 0;
419
}
420
421
static int roq_decode_frame(AVCodecContext *avctx,
422
                            void *data, int *data_size,
423
                            uint8_t *buf, int buf_size)
424
{
425
    RoqContext *s = avctx->priv_data;
426
427
    if (avctx->get_buffer(avctx, &s->current_frame)) {
428 9b879566 Michel Bardiaux
        av_log(avctx, AV_LOG_ERROR, "  RoQ: get_buffer() failed\n");
429 3ef8be2b Mike Melanson
        return -1;
430
    }
431
    s->y_stride = s->current_frame.linesize[0];
432
    s->c_stride = s->current_frame.linesize[1];
433
434
    s->buf = buf;
435
    s->size = buf_size;
436
    roqvideo_decode_frame(s);
437
438
    /* release the last frame if it is allocated */
439
    if (s->first_frame)
440
        s->first_frame = 0;
441
    else
442
        avctx->release_buffer(avctx, &s->last_frame);
443
444
    /* shuffle frames */
445
    s->last_frame = s->current_frame;
446
447
    *data_size = sizeof(AVFrame);
448
    *(AVFrame*)data = s->current_frame;
449
450
    return buf_size;
451
}
452
453
static int roq_decode_end(AVCodecContext *avctx)
454
{
455
    RoqContext *s = avctx->priv_data;
456
457
    /* release the last frame */
458 b9029997 Mike Melanson
    if (s->last_frame.data[0])
459
        avctx->release_buffer(avctx, &s->last_frame);
460 3ef8be2b Mike Melanson
461
    return 0;
462
}
463
464
AVCodec roq_decoder = {
465
    "roqvideo",
466
    CODEC_TYPE_VIDEO,
467
    CODEC_ID_ROQ,
468
    sizeof(RoqContext),
469
    roq_decode_init,
470
    NULL,
471
    roq_decode_end,
472
    roq_decode_frame,
473
    CODEC_CAP_DR1,
474
};