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