00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00028 #include <stdio.h>
00029 #include <stdlib.h>
00030 #include <string.h>
00031 #include <unistd.h>
00032
00033 #include "avcodec.h"
00034 #include "bytestream.h"
00035 #include "dsputil.h"
00036 #include "roqvideo.h"
00037
00038 static void roqvideo_decode_frame(RoqContext *ri)
00039 {
00040 unsigned int chunk_id = 0, chunk_arg = 0;
00041 unsigned long chunk_size = 0;
00042 int i, j, k, nv1, nv2, vqflg = 0, vqflg_pos = -1;
00043 int vqid, bpos, xpos, ypos, xp, yp, x, y, mx, my;
00044 int frame_stats[2][4] = {{0},{0}};
00045 roq_qcell *qcell;
00046 const unsigned char *buf = ri->buf;
00047 const unsigned char *buf_end = ri->buf + ri->size;
00048
00049 while (buf < buf_end) {
00050 chunk_id = bytestream_get_le16(&buf);
00051 chunk_size = bytestream_get_le32(&buf);
00052 chunk_arg = bytestream_get_le16(&buf);
00053
00054 if(chunk_id == RoQ_QUAD_VQ)
00055 break;
00056 if(chunk_id == RoQ_QUAD_CODEBOOK) {
00057 if((nv1 = chunk_arg >> 8) == 0)
00058 nv1 = 256;
00059 if((nv2 = chunk_arg & 0xff) == 0 && nv1 * 6 < chunk_size)
00060 nv2 = 256;
00061 for(i = 0; i < nv1; i++) {
00062 ri->cb2x2[i].y[0] = *buf++;
00063 ri->cb2x2[i].y[1] = *buf++;
00064 ri->cb2x2[i].y[2] = *buf++;
00065 ri->cb2x2[i].y[3] = *buf++;
00066 ri->cb2x2[i].u = *buf++;
00067 ri->cb2x2[i].v = *buf++;
00068 }
00069 for(i = 0; i < nv2; i++)
00070 for(j = 0; j < 4; j++)
00071 ri->cb4x4[i].idx[j] = *buf++;
00072 }
00073 }
00074
00075 bpos = xpos = ypos = 0;
00076 while(bpos < chunk_size) {
00077 for (yp = ypos; yp < ypos + 16; yp += 8)
00078 for (xp = xpos; xp < xpos + 16; xp += 8) {
00079 if (vqflg_pos < 0) {
00080 vqflg = buf[bpos++]; vqflg |= (buf[bpos++] << 8);
00081 vqflg_pos = 7;
00082 }
00083 vqid = (vqflg >> (vqflg_pos * 2)) & 0x3;
00084 frame_stats[0][vqid]++;
00085 vqflg_pos--;
00086
00087 switch(vqid) {
00088 case RoQ_ID_MOT:
00089 break;
00090 case RoQ_ID_FCC:
00091 mx = 8 - (buf[bpos] >> 4) - ((signed char) (chunk_arg >> 8));
00092 my = 8 - (buf[bpos++] & 0xf) - ((signed char) chunk_arg);
00093 ff_apply_motion_8x8(ri, xp, yp, mx, my);
00094 break;
00095 case RoQ_ID_SLD:
00096 qcell = ri->cb4x4 + buf[bpos++];
00097 ff_apply_vector_4x4(ri, xp, yp, ri->cb2x2 + qcell->idx[0]);
00098 ff_apply_vector_4x4(ri, xp+4, yp, ri->cb2x2 + qcell->idx[1]);
00099 ff_apply_vector_4x4(ri, xp, yp+4, ri->cb2x2 + qcell->idx[2]);
00100 ff_apply_vector_4x4(ri, xp+4, yp+4, ri->cb2x2 + qcell->idx[3]);
00101 break;
00102 case RoQ_ID_CCC:
00103 for (k = 0; k < 4; k++) {
00104 x = xp; y = yp;
00105 if(k & 0x01) x += 4;
00106 if(k & 0x02) y += 4;
00107
00108 if (vqflg_pos < 0) {
00109 vqflg = buf[bpos++];
00110 vqflg |= (buf[bpos++] << 8);
00111 vqflg_pos = 7;
00112 }
00113 vqid = (vqflg >> (vqflg_pos * 2)) & 0x3;
00114 frame_stats[1][vqid]++;
00115 vqflg_pos--;
00116 switch(vqid) {
00117 case RoQ_ID_MOT:
00118 break;
00119 case RoQ_ID_FCC:
00120 mx = 8 - (buf[bpos] >> 4) - ((signed char) (chunk_arg >> 8));
00121 my = 8 - (buf[bpos++] & 0xf) - ((signed char) chunk_arg);
00122 ff_apply_motion_4x4(ri, x, y, mx, my);
00123 break;
00124 case RoQ_ID_SLD:
00125 qcell = ri->cb4x4 + buf[bpos++];
00126 ff_apply_vector_2x2(ri, x, y, ri->cb2x2 + qcell->idx[0]);
00127 ff_apply_vector_2x2(ri, x+2, y, ri->cb2x2 + qcell->idx[1]);
00128 ff_apply_vector_2x2(ri, x, y+2, ri->cb2x2 + qcell->idx[2]);
00129 ff_apply_vector_2x2(ri, x+2, y+2, ri->cb2x2 + qcell->idx[3]);
00130 break;
00131 case RoQ_ID_CCC:
00132 ff_apply_vector_2x2(ri, x, y, ri->cb2x2 + buf[bpos]);
00133 ff_apply_vector_2x2(ri, x+2, y, ri->cb2x2 + buf[bpos+1]);
00134 ff_apply_vector_2x2(ri, x, y+2, ri->cb2x2 + buf[bpos+2]);
00135 ff_apply_vector_2x2(ri, x+2, y+2, ri->cb2x2 + buf[bpos+3]);
00136 bpos += 4;
00137 break;
00138 }
00139 }
00140 break;
00141 default:
00142 av_log(ri->avctx, AV_LOG_ERROR, "Unknown vq code: %d\n", vqid);
00143 }
00144 }
00145
00146 xpos += 16;
00147 if (xpos >= ri->width) {
00148 xpos -= ri->width;
00149 ypos += 16;
00150 }
00151 if(ypos >= ri->height)
00152 break;
00153 }
00154 }
00155
00156
00157 static int roq_decode_init(AVCodecContext *avctx)
00158 {
00159 RoqContext *s = avctx->priv_data;
00160
00161 s->avctx = avctx;
00162 s->width = avctx->width;
00163 s->height = avctx->height;
00164 s->last_frame = &s->frames[0];
00165 s->current_frame = &s->frames[1];
00166 avctx->pix_fmt = PIX_FMT_YUV444P;
00167 dsputil_init(&s->dsp, avctx);
00168
00169 return 0;
00170 }
00171
00172 static int roq_decode_frame(AVCodecContext *avctx,
00173 void *data, int *data_size,
00174 const uint8_t *buf, int buf_size)
00175 {
00176 RoqContext *s = avctx->priv_data;
00177 int copy= !s->current_frame->data[0];
00178
00179 if (avctx->reget_buffer(avctx, s->current_frame)) {
00180 av_log(avctx, AV_LOG_ERROR, " RoQ: get_buffer() failed\n");
00181 return -1;
00182 }
00183
00184 if(copy)
00185 av_picture_copy((AVPicture*)s->current_frame, (AVPicture*)s->last_frame,
00186 avctx->pix_fmt, avctx->width, avctx->height);
00187
00188 s->buf = buf;
00189 s->size = buf_size;
00190 roqvideo_decode_frame(s);
00191
00192 *data_size = sizeof(AVFrame);
00193 *(AVFrame*)data = *s->current_frame;
00194
00195
00196 FFSWAP(AVFrame *, s->current_frame, s->last_frame);
00197
00198 return buf_size;
00199 }
00200
00201 static int roq_decode_end(AVCodecContext *avctx)
00202 {
00203 RoqContext *s = avctx->priv_data;
00204
00205
00206 if (s->last_frame->data[0])
00207 avctx->release_buffer(avctx, s->last_frame);
00208 if (s->current_frame->data[0])
00209 avctx->release_buffer(avctx, s->current_frame);
00210
00211 return 0;
00212 }
00213
00214 AVCodec roq_decoder = {
00215 "roqvideo",
00216 CODEC_TYPE_VIDEO,
00217 CODEC_ID_ROQ,
00218 sizeof(RoqContext),
00219 roq_decode_init,
00220 NULL,
00221 roq_decode_end,
00222 roq_decode_frame,
00223 CODEC_CAP_DR1,
00224 };