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