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