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