5080
|
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 roqvideodec.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 #include "roqvideo.h"
|
|
38
|
|
39 #define avg2(a,b) av_clip_uint8(((int)(a)+(int)(b)+1)>>1)
|
|
40 #define avg4(a,b,c,d) av_clip_uint8(((int)(a)+(int)(b)+(int)(c)+(int)(d)+2)>>2)
|
|
41
|
|
42 #define get_byte(in_buffer) *(in_buffer++)
|
|
43
|
|
44 static void roqvideo_decode_frame(RoqContext *ri)
|
|
45 {
|
|
46 unsigned int chunk_id = 0, chunk_arg = 0;
|
|
47 unsigned long chunk_size = 0;
|
|
48 int i, j, k, nv1, nv2, vqflg = 0, vqflg_pos = -1;
|
|
49 int vqid, bpos, xpos, ypos, xp, yp, x, y, mx, my;
|
|
50 int frame_stats[2][4] = {{0},{0}};
|
|
51 roq_qcell *qcell;
|
|
52 unsigned char *buf = ri->buf;
|
|
53 unsigned char *buf_end = ri->buf + ri->size;
|
|
54
|
|
55 while (buf < buf_end) {
|
|
56 chunk_id = bytestream_get_le16(&buf);
|
|
57 chunk_size = bytestream_get_le32(&buf);
|
|
58 chunk_arg = bytestream_get_le16(&buf);
|
|
59
|
|
60 if(chunk_id == RoQ_QUAD_VQ)
|
|
61 break;
|
|
62 if(chunk_id == RoQ_QUAD_CODEBOOK) {
|
|
63 if((nv1 = chunk_arg >> 8) == 0)
|
|
64 nv1 = 256;
|
|
65 if((nv2 = chunk_arg & 0xff) == 0 && nv1 * 6 < chunk_size)
|
|
66 nv2 = 256;
|
|
67 for(i = 0; i < nv1; i++) {
|
|
68 ri->cells[i].y[0] = get_byte(buf);
|
|
69 ri->cells[i].y[1] = get_byte(buf);
|
|
70 ri->cells[i].y[2] = get_byte(buf);
|
|
71 ri->cells[i].y[3] = get_byte(buf);
|
|
72 ri->cells[i].u = get_byte(buf);
|
|
73 ri->cells[i].v = get_byte(buf);
|
|
74 }
|
|
75 for(i = 0; i < nv2; i++)
|
|
76 for(j = 0; j < 4; j++)
|
|
77 ri->qcells[i].idx[j] = get_byte(buf);
|
|
78 }
|
|
79 }
|
|
80
|
|
81 bpos = xpos = ypos = 0;
|
|
82 while(bpos < chunk_size) {
|
|
83 for (yp = ypos; yp < ypos + 16; yp += 8)
|
|
84 for (xp = xpos; xp < xpos + 16; xp += 8) {
|
|
85 if (vqflg_pos < 0) {
|
|
86 vqflg = buf[bpos++]; vqflg |= (buf[bpos++] << 8);
|
|
87 vqflg_pos = 7;
|
|
88 }
|
|
89 vqid = (vqflg >> (vqflg_pos * 2)) & 0x3;
|
|
90 frame_stats[0][vqid]++;
|
|
91 vqflg_pos--;
|
|
92
|
|
93 switch(vqid) {
|
|
94 case RoQ_ID_MOT:
|
|
95 break;
|
|
96 case RoQ_ID_FCC:
|
|
97 mx = 8 - (buf[bpos] >> 4) - ((signed char) (chunk_arg >> 8));
|
|
98 my = 8 - (buf[bpos++] & 0xf) - ((signed char) chunk_arg);
|
|
99 ff_apply_motion_8x8(ri, xp, yp, mx, my);
|
|
100 break;
|
|
101 case RoQ_ID_SLD:
|
|
102 qcell = ri->qcells + buf[bpos++];
|
|
103 ff_apply_vector_4x4(ri, xp, yp, ri->cells + qcell->idx[0]);
|
|
104 ff_apply_vector_4x4(ri, xp+4, yp, ri->cells + qcell->idx[1]);
|
|
105 ff_apply_vector_4x4(ri, xp, yp+4, ri->cells + qcell->idx[2]);
|
|
106 ff_apply_vector_4x4(ri, xp+4, yp+4, ri->cells + qcell->idx[3]);
|
|
107 break;
|
|
108 case RoQ_ID_CCC:
|
|
109 for (k = 0; k < 4; k++) {
|
|
110 x = xp; y = yp;
|
|
111 if(k & 0x01) x += 4;
|
|
112 if(k & 0x02) y += 4;
|
|
113
|
|
114 if (vqflg_pos < 0) {
|
|
115 vqflg = buf[bpos++];
|
|
116 vqflg |= (buf[bpos++] << 8);
|
|
117 vqflg_pos = 7;
|
|
118 }
|
|
119 vqid = (vqflg >> (vqflg_pos * 2)) & 0x3;
|
|
120 frame_stats[1][vqid]++;
|
|
121 vqflg_pos--;
|
|
122 switch(vqid) {
|
|
123 case RoQ_ID_MOT:
|
|
124 break;
|
|
125 case RoQ_ID_FCC:
|
|
126 mx = 8 - (buf[bpos] >> 4) - ((signed char) (chunk_arg >> 8));
|
|
127 my = 8 - (buf[bpos++] & 0xf) - ((signed char) chunk_arg);
|
|
128 ff_apply_motion_4x4(ri, x, y, mx, my);
|
|
129 break;
|
|
130 case RoQ_ID_SLD:
|
|
131 qcell = ri->qcells + buf[bpos++];
|
|
132 ff_apply_vector_2x2(ri, x, y, ri->cells + qcell->idx[0]);
|
|
133 ff_apply_vector_2x2(ri, x+2, y, ri->cells + qcell->idx[1]);
|
|
134 ff_apply_vector_2x2(ri, x, y+2, ri->cells + qcell->idx[2]);
|
|
135 ff_apply_vector_2x2(ri, x+2, y+2, ri->cells + qcell->idx[3]);
|
|
136 break;
|
|
137 case RoQ_ID_CCC:
|
|
138 ff_apply_vector_2x2(ri, x, y, ri->cells + buf[bpos]);
|
|
139 ff_apply_vector_2x2(ri, x+2, y, ri->cells + buf[bpos+1]);
|
|
140 ff_apply_vector_2x2(ri, x, y+2, ri->cells + buf[bpos+2]);
|
|
141 ff_apply_vector_2x2(ri, x+2, y+2, ri->cells + buf[bpos+3]);
|
|
142 bpos += 4;
|
|
143 break;
|
|
144 }
|
|
145 }
|
|
146 break;
|
|
147 default:
|
|
148 av_log(ri->avctx, AV_LOG_ERROR, "Unknown vq code: %d\n", vqid);
|
|
149 }
|
|
150 }
|
|
151
|
|
152 xpos += 16;
|
|
153 if (xpos >= ri->avctx->width) {
|
|
154 xpos -= ri->avctx->width;
|
|
155 ypos += 16;
|
|
156 }
|
|
157 if(ypos >= ri->avctx->height)
|
|
158 break;
|
|
159 }
|
|
160 }
|
|
161
|
|
162
|
|
163 static int roq_decode_init(AVCodecContext *avctx)
|
|
164 {
|
|
165 RoqContext *s = avctx->priv_data;
|
|
166
|
|
167 s->avctx = avctx;
|
|
168 s->last_frame = &s->frames[0];
|
|
169 s->current_frame = &s->frames[1];
|
|
170 avctx->pix_fmt = PIX_FMT_YUV420P;
|
|
171 dsputil_init(&s->dsp, avctx);
|
|
172
|
|
173 return 0;
|
|
174 }
|
|
175
|
|
176 static int roq_decode_frame(AVCodecContext *avctx,
|
|
177 void *data, int *data_size,
|
|
178 uint8_t *buf, int buf_size)
|
|
179 {
|
|
180 RoqContext *s = avctx->priv_data;
|
|
181
|
5088
|
182 if (avctx->reget_buffer(avctx, s->current_frame)) {
|
5080
|
183 av_log(avctx, AV_LOG_ERROR, " RoQ: get_buffer() failed\n");
|
|
184 return -1;
|
|
185 }
|
|
186 s->y_stride = s->current_frame->linesize[0];
|
|
187 s->c_stride = s->current_frame->linesize[1];
|
|
188
|
|
189 s->buf = buf;
|
|
190 s->size = buf_size;
|
|
191 roqvideo_decode_frame(s);
|
|
192
|
|
193 *data_size = sizeof(AVFrame);
|
|
194 *(AVFrame*)data = *s->current_frame;
|
|
195
|
|
196 /* shuffle frames */
|
|
197 FFSWAP(AVFrame *, s->current_frame, s->last_frame);
|
|
198
|
|
199 return buf_size;
|
|
200 }
|
|
201
|
|
202 static int roq_decode_end(AVCodecContext *avctx)
|
|
203 {
|
|
204 RoqContext *s = avctx->priv_data;
|
|
205
|
|
206 /* release the last frame */
|
|
207 if (s->last_frame->data[0])
|
|
208 avctx->release_buffer(avctx, s->last_frame);
|
5088
|
209 if (s->current_frame->data[0])
|
|
210 avctx->release_buffer(avctx, s->current_frame);
|
5080
|
211
|
|
212 return 0;
|
|
213 }
|
|
214
|
|
215 AVCodec roq_decoder = {
|
|
216 "roqvideo",
|
|
217 CODEC_TYPE_VIDEO,
|
|
218 CODEC_ID_ROQ,
|
|
219 sizeof(RoqContext),
|
|
220 roq_decode_init,
|
|
221 NULL,
|
|
222 roq_decode_end,
|
|
223 roq_decode_frame,
|
|
224 CODEC_CAP_DR1,
|
|
225 };
|