00001
00026 #include "libavutil/intreadwrite.h"
00027
00028 #include "avcodec.h"
00029 #include "get_bits.h"
00030
00031 typedef struct YopDecContext {
00032 AVFrame frame;
00033 AVCodecContext *avctx;
00034
00035 int num_pal_colors;
00036 int first_color[2];
00037 int frame_data_length;
00038 int row_pos;
00039
00040 uint8_t *low_nibble;
00041 uint8_t *srcptr;
00042 uint8_t *dstptr;
00043 uint8_t *dstbuf;
00044 } YopDecContext;
00045
00046
00047
00048
00055 static const uint8_t paint_lut[15][4] =
00056 {{1, 2, 3, 4}, {1, 2, 0, 3},
00057 {1, 2, 1, 3}, {1, 2, 2, 3},
00058 {1, 0, 2, 3}, {1, 0, 0, 2},
00059 {1, 0, 1, 2}, {1, 1, 2, 3},
00060 {0, 1, 2, 3}, {0, 1, 0, 2},
00061 {1, 1, 0, 2}, {0, 1, 1, 2},
00062 {0, 0, 1, 2}, {0, 0, 0, 1},
00063 {1, 1, 1, 2},
00064 };
00065
00070 static const int8_t motion_vector[16][2] =
00071 {{-4, -4}, {-2, -4},
00072 { 0, -4}, { 2, -4},
00073 {-4, -2}, {-4, 0},
00074 {-3, -3}, {-1, -3},
00075 { 1, -3}, { 3, -3},
00076 {-3, -1}, {-2, -2},
00077 { 0, -2}, { 2, -2},
00078 { 4, -2}, {-2, 0},
00079 };
00080
00081 static av_cold int yop_decode_init(AVCodecContext *avctx)
00082 {
00083 YopDecContext *s = avctx->priv_data;
00084 s->avctx = avctx;
00085
00086 if (avctx->width & 1 || avctx->height & 1 ||
00087 avcodec_check_dimensions(avctx, avctx->width, avctx->height) < 0) {
00088 av_log(avctx, AV_LOG_ERROR, "YOP has invalid dimensions\n");
00089 return -1;
00090 }
00091
00092 if (!avctx->extradata) {
00093 av_log(avctx, AV_LOG_ERROR, "extradata missing\n");
00094 return AVERROR_INVALIDDATA;
00095 }
00096
00097 avctx->pix_fmt = PIX_FMT_PAL8;
00098
00099 s->num_pal_colors = avctx->extradata[0];
00100 s->first_color[0] = avctx->extradata[1];
00101 s->first_color[1] = avctx->extradata[2];
00102
00103 if (s->num_pal_colors + s->first_color[0] > 256 ||
00104 s->num_pal_colors + s->first_color[1] > 256) {
00105 av_log(avctx, AV_LOG_ERROR,
00106 "YOP: palette parameters invalid, header probably corrupt\n");
00107 return AVERROR_INVALIDDATA;
00108 }
00109
00110 return 0;
00111 }
00112
00113 static av_cold int yop_decode_close(AVCodecContext *avctx)
00114 {
00115 YopDecContext *s = avctx->priv_data;
00116 if (s->frame.data[0])
00117 avctx->release_buffer(avctx, &s->frame);
00118 return 0;
00119 }
00120
00126 static void yop_paint_block(YopDecContext *s, int tag)
00127 {
00128 s->dstptr[0] = s->srcptr[0];
00129 s->dstptr[1] = s->srcptr[paint_lut[tag][0]];
00130 s->dstptr[s->frame.linesize[0]] = s->srcptr[paint_lut[tag][1]];
00131 s->dstptr[s->frame.linesize[0] + 1] = s->srcptr[paint_lut[tag][2]];
00132
00133
00134 s->srcptr += paint_lut[tag][3];
00135 }
00136
00141 static int yop_copy_previous_block(YopDecContext *s, int copy_tag)
00142 {
00143 uint8_t *bufptr;
00144
00145
00146 bufptr = s->dstptr + motion_vector[copy_tag][0] +
00147 s->frame.linesize[0] * motion_vector[copy_tag][1];
00148 if (bufptr < s->dstbuf) {
00149 av_log(s->avctx, AV_LOG_ERROR,
00150 "YOP: cannot decode, file probably corrupt\n");
00151 return AVERROR_INVALIDDATA;
00152 }
00153
00154 s->dstptr[0] = bufptr[0];
00155 s->dstptr[1] = bufptr[1];
00156 s->dstptr[s->frame.linesize[0]] = bufptr[s->frame.linesize[0]];
00157 s->dstptr[s->frame.linesize[0] + 1] = bufptr[s->frame.linesize[0] + 1];
00158
00159 return 0;
00160 }
00161
00166 static uint8_t yop_get_next_nibble(YopDecContext *s)
00167 {
00168 int ret;
00169
00170 if (s->low_nibble) {
00171 ret = *s->low_nibble & 0xf;
00172 s->low_nibble = NULL;
00173 }else {
00174 s->low_nibble = s->srcptr++;
00175 ret = *s->low_nibble >> 4;
00176 }
00177 return ret;
00178 }
00179
00183 static void yop_next_macroblock(YopDecContext *s)
00184 {
00185
00186 if (s->row_pos == s->frame.linesize[0] - 2) {
00187 s->dstptr += s->frame.linesize[0];
00188 s->row_pos = 0;
00189 }else {
00190 s->row_pos += 2;
00191 }
00192 s->dstptr += 2;
00193 }
00194
00195 static int yop_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
00196 AVPacket *avpkt)
00197 {
00198 YopDecContext *s = avctx->priv_data;
00199 int tag, firstcolor, is_odd_frame;
00200 int ret, i;
00201 uint32_t *palette;
00202
00203 if (s->frame.data[0])
00204 avctx->release_buffer(avctx, &s->frame);
00205
00206 if (avpkt->size < 4 + 3*s->num_pal_colors) {
00207 av_log(avctx, AV_LOG_ERROR, "packet of size %d too small\n", avpkt->size);
00208 return AVERROR_INVALIDDATA;
00209 }
00210
00211 ret = avctx->get_buffer(avctx, &s->frame);
00212 if (ret < 0) {
00213 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00214 return ret;
00215 }
00216
00217 s->frame.linesize[0] = avctx->width;
00218
00219 s->dstbuf = s->frame.data[0];
00220 s->dstptr = s->frame.data[0];
00221 s->srcptr = avpkt->data + 4;
00222 s->row_pos = 0;
00223 s->low_nibble = NULL;
00224
00225 is_odd_frame = avpkt->data[0];
00226 if(is_odd_frame>1){
00227 av_log(avctx, AV_LOG_ERROR, "frame is too odd %d\n", is_odd_frame);
00228 return AVERROR_INVALIDDATA;
00229 }
00230 firstcolor = s->first_color[is_odd_frame];
00231 palette = (uint32_t *)s->frame.data[1];
00232
00233 for (i = 0; i < s->num_pal_colors; i++, s->srcptr += 3)
00234 palette[i + firstcolor] = (s->srcptr[0] << 18) |
00235 (s->srcptr[1] << 10) |
00236 (s->srcptr[2] << 2);
00237
00238 s->frame.palette_has_changed = 1;
00239
00240 while (s->dstptr - s->dstbuf <
00241 avctx->width * avctx->height &&
00242 s->srcptr - avpkt->data < avpkt->size) {
00243
00244 tag = yop_get_next_nibble(s);
00245
00246 if (tag != 0xf) {
00247 yop_paint_block(s, tag);
00248 }else {
00249 tag = yop_get_next_nibble(s);
00250 ret = yop_copy_previous_block(s, tag);
00251 if (ret < 0) {
00252 avctx->release_buffer(avctx, &s->frame);
00253 return ret;
00254 }
00255 }
00256 yop_next_macroblock(s);
00257 }
00258
00259 *data_size = sizeof(AVFrame);
00260 *(AVFrame *) data = s->frame;
00261 return avpkt->size;
00262 }
00263
00264 AVCodec yop_decoder = {
00265 "yop",
00266 AVMEDIA_TYPE_VIDEO,
00267 CODEC_ID_YOP,
00268 sizeof(YopDecContext),
00269 yop_decode_init,
00270 NULL,
00271 yop_decode_close,
00272 yop_decode_frame,
00273 .long_name = NULL_IF_CONFIG_SMALL("Psygnosis YOP Video"),
00274 };