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