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 while (bytestream2_tell(&ri->gb) < chunk_start + chunk_size) {
00075 for (yp = ypos; yp < ypos + 16; yp += 8)
00076 for (xp = xpos; xp < xpos + 16; xp += 8) {
00077 if (vqflg_pos < 0) {
00078 vqflg = bytestream2_get_le16(&ri->gb);
00079 vqflg_pos = 7;
00080 }
00081 vqid = (vqflg >> (vqflg_pos * 2)) & 0x3;
00082 frame_stats[0][vqid]++;
00083 vqflg_pos--;
00084
00085 switch(vqid) {
00086 case RoQ_ID_MOT:
00087 break;
00088 case RoQ_ID_FCC: {
00089 int byte = bytestream2_get_byte(&ri->gb);
00090 mx = 8 - (byte >> 4) - ((signed char) (chunk_arg >> 8));
00091 my = 8 - (byte & 0xf) - ((signed char) chunk_arg);
00092 ff_apply_motion_8x8(ri, xp, yp, mx, my);
00093 break;
00094 }
00095 case RoQ_ID_SLD:
00096 qcell = ri->cb4x4 + bytestream2_get_byte(&ri->gb);
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 = bytestream2_get_le16(&ri->gb);
00110 vqflg_pos = 7;
00111 }
00112 vqid = (vqflg >> (vqflg_pos * 2)) & 0x3;
00113 frame_stats[1][vqid]++;
00114 vqflg_pos--;
00115 switch(vqid) {
00116 case RoQ_ID_MOT:
00117 break;
00118 case RoQ_ID_FCC: {
00119 int byte = bytestream2_get_byte(&ri->gb);
00120 mx = 8 - (byte >> 4) - ((signed char) (chunk_arg >> 8));
00121 my = 8 - (byte & 0xf) - ((signed char) chunk_arg);
00122 ff_apply_motion_4x4(ri, x, y, mx, my);
00123 break;
00124 }
00125 case RoQ_ID_SLD:
00126 qcell = ri->cb4x4 + bytestream2_get_byte(&ri->gb);
00127 ff_apply_vector_2x2(ri, x, y, ri->cb2x2 + qcell->idx[0]);
00128 ff_apply_vector_2x2(ri, x+2, y, ri->cb2x2 + qcell->idx[1]);
00129 ff_apply_vector_2x2(ri, x, y+2, ri->cb2x2 + qcell->idx[2]);
00130 ff_apply_vector_2x2(ri, x+2, y+2, ri->cb2x2 + qcell->idx[3]);
00131 break;
00132 case RoQ_ID_CCC:
00133 ff_apply_vector_2x2(ri, x, y, ri->cb2x2 + bytestream2_get_byte(&ri->gb));
00134 ff_apply_vector_2x2(ri, x+2, y, ri->cb2x2 + bytestream2_get_byte(&ri->gb));
00135 ff_apply_vector_2x2(ri, x, y+2, ri->cb2x2 + bytestream2_get_byte(&ri->gb));
00136 ff_apply_vector_2x2(ri, x+2, y+2, ri->cb2x2 + bytestream2_get_byte(&ri->gb));
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 av_cold int roq_decode_init(AVCodecContext *avctx)
00158 {
00159 RoqContext *s = avctx->priv_data;
00160
00161 s->avctx = avctx;
00162
00163 if (avctx->width % 16 || avctx->height % 16) {
00164 av_log(avctx, AV_LOG_ERROR,
00165 "Dimensions must be a multiple of 16\n");
00166 return AVERROR_PATCHWELCOME;
00167 }
00168
00169 s->width = avctx->width;
00170 s->height = avctx->height;
00171 s->last_frame = &s->frames[0];
00172 s->current_frame = &s->frames[1];
00173 avctx->pix_fmt = PIX_FMT_YUV444P;
00174
00175 return 0;
00176 }
00177
00178 static int roq_decode_frame(AVCodecContext *avctx,
00179 void *data, int *data_size,
00180 AVPacket *avpkt)
00181 {
00182 const uint8_t *buf = avpkt->data;
00183 int buf_size = avpkt->size;
00184 RoqContext *s = avctx->priv_data;
00185 int copy= !s->current_frame->data[0];
00186
00187 s->current_frame->reference = 3;
00188 if (avctx->reget_buffer(avctx, s->current_frame)) {
00189 av_log(avctx, AV_LOG_ERROR, " RoQ: get_buffer() failed\n");
00190 return -1;
00191 }
00192
00193 if(copy)
00194 av_picture_copy((AVPicture*)s->current_frame, (AVPicture*)s->last_frame,
00195 avctx->pix_fmt, avctx->width, avctx->height);
00196
00197 bytestream2_init(&s->gb, buf, buf_size);
00198 roqvideo_decode_frame(s);
00199
00200 *data_size = sizeof(AVFrame);
00201 *(AVFrame*)data = *s->current_frame;
00202
00203
00204 FFSWAP(AVFrame *, s->current_frame, s->last_frame);
00205
00206 return buf_size;
00207 }
00208
00209 static av_cold int roq_decode_end(AVCodecContext *avctx)
00210 {
00211 RoqContext *s = avctx->priv_data;
00212
00213
00214 if (s->last_frame->data[0])
00215 avctx->release_buffer(avctx, s->last_frame);
00216 if (s->current_frame->data[0])
00217 avctx->release_buffer(avctx, s->current_frame);
00218
00219 return 0;
00220 }
00221
00222 AVCodec ff_roq_decoder = {
00223 .name = "roqvideo",
00224 .type = AVMEDIA_TYPE_VIDEO,
00225 .id = CODEC_ID_ROQ,
00226 .priv_data_size = sizeof(RoqContext),
00227 .init = roq_decode_init,
00228 .close = roq_decode_end,
00229 .decode = roq_decode_frame,
00230 .capabilities = CODEC_CAP_DR1,
00231 .long_name = NULL_IF_CONFIG_SMALL("id RoQ video"),
00232 };