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 ff_apply_motion_8x8(ri, xp, yp, 0, 0);
|
|
96 break;
|
|
97 case RoQ_ID_FCC:
|
|
98 mx = 8 - (buf[bpos] >> 4) - ((signed char) (chunk_arg >> 8));
|
|
99 my = 8 - (buf[bpos++] & 0xf) - ((signed char) chunk_arg);
|
|
100 ff_apply_motion_8x8(ri, xp, yp, mx, my);
|
|
101 break;
|
|
102 case RoQ_ID_SLD:
|
|
103 qcell = ri->qcells + buf[bpos++];
|
|
104 ff_apply_vector_4x4(ri, xp, yp, ri->cells + qcell->idx[0]);
|
|
105 ff_apply_vector_4x4(ri, xp+4, yp, ri->cells + qcell->idx[1]);
|
|
106 ff_apply_vector_4x4(ri, xp, yp+4, ri->cells + qcell->idx[2]);
|
|
107 ff_apply_vector_4x4(ri, xp+4, yp+4, ri->cells + qcell->idx[3]);
|
|
108 break;
|
|
109 case RoQ_ID_CCC:
|
|
110 for (k = 0; k < 4; k++) {
|
|
111 x = xp; y = yp;
|
|
112 if(k & 0x01) x += 4;
|
|
113 if(k & 0x02) y += 4;
|
|
114
|
|
115 if (vqflg_pos < 0) {
|
|
116 vqflg = buf[bpos++];
|
|
117 vqflg |= (buf[bpos++] << 8);
|
|
118 vqflg_pos = 7;
|
|
119 }
|
|
120 vqid = (vqflg >> (vqflg_pos * 2)) & 0x3;
|
|
121 frame_stats[1][vqid]++;
|
|
122 vqflg_pos--;
|
|
123 switch(vqid) {
|
|
124 case RoQ_ID_MOT:
|
|
125 ff_apply_motion_4x4(ri, x, y, 0, 0);
|
|
126 break;
|
|
127 case RoQ_ID_FCC:
|
|
128 mx = 8 - (buf[bpos] >> 4) - ((signed char) (chunk_arg >> 8));
|
|
129 my = 8 - (buf[bpos++] & 0xf) - ((signed char) chunk_arg);
|
|
130 ff_apply_motion_4x4(ri, x, y, mx, my);
|
|
131 break;
|
|
132 case RoQ_ID_SLD:
|
|
133 qcell = ri->qcells + buf[bpos++];
|
|
134 ff_apply_vector_2x2(ri, x, y, ri->cells + qcell->idx[0]);
|
|
135 ff_apply_vector_2x2(ri, x+2, y, ri->cells + qcell->idx[1]);
|
|
136 ff_apply_vector_2x2(ri, x, y+2, ri->cells + qcell->idx[2]);
|
|
137 ff_apply_vector_2x2(ri, x+2, y+2, ri->cells + qcell->idx[3]);
|
|
138 break;
|
|
139 case RoQ_ID_CCC:
|
|
140 ff_apply_vector_2x2(ri, x, y, ri->cells + buf[bpos]);
|
|
141 ff_apply_vector_2x2(ri, x+2, y, ri->cells + buf[bpos+1]);
|
|
142 ff_apply_vector_2x2(ri, x, y+2, ri->cells + buf[bpos+2]);
|
|
143 ff_apply_vector_2x2(ri, x+2, y+2, ri->cells + buf[bpos+3]);
|
|
144 bpos += 4;
|
|
145 break;
|
|
146 }
|
|
147 }
|
|
148 break;
|
|
149 default:
|
|
150 av_log(ri->avctx, AV_LOG_ERROR, "Unknown vq code: %d\n", vqid);
|
|
151 }
|
|
152 }
|
|
153
|
|
154 xpos += 16;
|
|
155 if (xpos >= ri->avctx->width) {
|
|
156 xpos -= ri->avctx->width;
|
|
157 ypos += 16;
|
|
158 }
|
|
159 if(ypos >= ri->avctx->height)
|
|
160 break;
|
|
161 }
|
|
162 }
|
|
163
|
|
164
|
|
165 static int roq_decode_init(AVCodecContext *avctx)
|
|
166 {
|
|
167 RoqContext *s = avctx->priv_data;
|
|
168
|
|
169 s->avctx = avctx;
|
|
170 s->first_frame = 1;
|
|
171 s->last_frame = &s->frames[0];
|
|
172 s->current_frame = &s->frames[1];
|
|
173 avctx->pix_fmt = PIX_FMT_YUV420P;
|
|
174 dsputil_init(&s->dsp, avctx);
|
|
175
|
|
176 return 0;
|
|
177 }
|
|
178
|
|
179 static int roq_decode_frame(AVCodecContext *avctx,
|
|
180 void *data, int *data_size,
|
|
181 uint8_t *buf, int buf_size)
|
|
182 {
|
|
183 RoqContext *s = avctx->priv_data;
|
|
184
|
|
185 if (avctx->get_buffer(avctx, s->current_frame)) {
|
|
186 av_log(avctx, AV_LOG_ERROR, " RoQ: get_buffer() failed\n");
|
|
187 return -1;
|
|
188 }
|
|
189 s->y_stride = s->current_frame->linesize[0];
|
|
190 s->c_stride = s->current_frame->linesize[1];
|
|
191
|
|
192 s->buf = buf;
|
|
193 s->size = buf_size;
|
|
194 roqvideo_decode_frame(s);
|
|
195
|
|
196 /* release the last frame if it is allocated */
|
|
197 if (s->first_frame)
|
|
198 s->first_frame = 0;
|
|
199 else
|
|
200 avctx->release_buffer(avctx, s->last_frame);
|
|
201
|
|
202 *data_size = sizeof(AVFrame);
|
|
203 *(AVFrame*)data = *s->current_frame;
|
|
204
|
|
205 /* shuffle frames */
|
|
206 FFSWAP(AVFrame *, s->current_frame, s->last_frame);
|
|
207
|
|
208 return buf_size;
|
|
209 }
|
|
210
|
|
211 static int roq_decode_end(AVCodecContext *avctx)
|
|
212 {
|
|
213 RoqContext *s = avctx->priv_data;
|
|
214
|
|
215 /* release the last frame */
|
|
216 if (s->last_frame->data[0])
|
|
217 avctx->release_buffer(avctx, s->last_frame);
|
|
218
|
|
219 return 0;
|
|
220 }
|
|
221
|
|
222 AVCodec roq_decoder = {
|
|
223 "roqvideo",
|
|
224 CODEC_TYPE_VIDEO,
|
|
225 CODEC_ID_ROQ,
|
|
226 sizeof(RoqContext),
|
|
227 roq_decode_init,
|
|
228 NULL,
|
|
229 roq_decode_end,
|
|
230 roq_decode_frame,
|
|
231 CODEC_CAP_DR1,
|
|
232 };
|