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