00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include "libavutil/intreadwrite.h"
00026 #include "libavutil/imgutils.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 av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0) {
00088 av_log(avctx, AV_LOG_ERROR, "YOP has invalid dimensions\n");
00089 return -1;
00090 }
00091
00092 avctx->pix_fmt = PIX_FMT_PAL8;
00093
00094 avcodec_get_frame_defaults(&s->frame);
00095 s->num_pal_colors = avctx->extradata[0];
00096 s->first_color[0] = avctx->extradata[1];
00097 s->first_color[1] = avctx->extradata[2];
00098
00099 if (s->num_pal_colors + s->first_color[0] > 256 ||
00100 s->num_pal_colors + s->first_color[1] > 256) {
00101 av_log(avctx, AV_LOG_ERROR,
00102 "YOP: palette parameters invalid, header probably corrupt\n");
00103 return AVERROR_INVALIDDATA;
00104 }
00105
00106 return 0;
00107 }
00108
00109 static av_cold int yop_decode_close(AVCodecContext *avctx)
00110 {
00111 YopDecContext *s = avctx->priv_data;
00112 if (s->frame.data[0])
00113 avctx->release_buffer(avctx, &s->frame);
00114 return 0;
00115 }
00116
00122 static void yop_paint_block(YopDecContext *s, int tag)
00123 {
00124 s->dstptr[0] = s->srcptr[0];
00125 s->dstptr[1] = s->srcptr[paint_lut[tag][0]];
00126 s->dstptr[s->frame.linesize[0]] = s->srcptr[paint_lut[tag][1]];
00127 s->dstptr[s->frame.linesize[0] + 1] = s->srcptr[paint_lut[tag][2]];
00128
00129
00130 s->srcptr += paint_lut[tag][3];
00131 }
00132
00137 static int yop_copy_previous_block(YopDecContext *s, int copy_tag)
00138 {
00139 uint8_t *bufptr;
00140
00141
00142 bufptr = s->dstptr + motion_vector[copy_tag][0] +
00143 s->frame.linesize[0] * motion_vector[copy_tag][1];
00144 if (bufptr < s->dstbuf) {
00145 av_log(s->avctx, AV_LOG_ERROR,
00146 "YOP: cannot decode, file probably corrupt\n");
00147 return AVERROR_INVALIDDATA;
00148 }
00149
00150 s->dstptr[0] = bufptr[0];
00151 s->dstptr[1] = bufptr[1];
00152 s->dstptr[s->frame.linesize[0]] = bufptr[s->frame.linesize[0]];
00153 s->dstptr[s->frame.linesize[0] + 1] = bufptr[s->frame.linesize[0] + 1];
00154
00155 return 0;
00156 }
00157
00162 static uint8_t yop_get_next_nibble(YopDecContext *s)
00163 {
00164 int ret;
00165
00166 if (s->low_nibble) {
00167 ret = *s->low_nibble & 0xf;
00168 s->low_nibble = NULL;
00169 }else {
00170 s->low_nibble = s->srcptr++;
00171 ret = *s->low_nibble >> 4;
00172 }
00173 return ret;
00174 }
00175
00179 static void yop_next_macroblock(YopDecContext *s)
00180 {
00181
00182 if (s->row_pos == s->frame.linesize[0] - 2) {
00183 s->dstptr += s->frame.linesize[0];
00184 s->row_pos = 0;
00185 }else {
00186 s->row_pos += 2;
00187 }
00188 s->dstptr += 2;
00189 }
00190
00191 static int yop_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
00192 AVPacket *avpkt)
00193 {
00194 YopDecContext *s = avctx->priv_data;
00195 int tag, firstcolor, is_odd_frame;
00196 int ret, i;
00197 uint32_t *palette;
00198
00199 if (s->frame.data[0])
00200 avctx->release_buffer(avctx, &s->frame);
00201
00202 ret = avctx->get_buffer(avctx, &s->frame);
00203 if (ret < 0) {
00204 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00205 return ret;
00206 }
00207
00208 s->frame.linesize[0] = avctx->width;
00209
00210 s->dstbuf = s->frame.data[0];
00211 s->dstptr = s->frame.data[0];
00212 s->srcptr = avpkt->data + 4;
00213 s->row_pos = 0;
00214 s->low_nibble = NULL;
00215
00216 is_odd_frame = avpkt->data[0];
00217 firstcolor = s->first_color[is_odd_frame];
00218 palette = (uint32_t *)s->frame.data[1];
00219
00220 for (i = 0; i < s->num_pal_colors; i++, s->srcptr += 3) {
00221 palette[i + firstcolor] = (s->srcptr[0] << 18) |
00222 (s->srcptr[1] << 10) |
00223 (s->srcptr[2] << 2);
00224 palette[i + firstcolor] |= 0xFF << 24 |
00225 (palette[i + firstcolor] >> 6) & 0x30303;
00226 }
00227
00228 s->frame.palette_has_changed = 1;
00229
00230 while (s->dstptr - s->dstbuf <
00231 avctx->width * avctx->height &&
00232 s->srcptr - avpkt->data < avpkt->size) {
00233
00234 tag = yop_get_next_nibble(s);
00235
00236 if (tag != 0xf) {
00237 yop_paint_block(s, tag);
00238 }else {
00239 tag = yop_get_next_nibble(s);
00240 ret = yop_copy_previous_block(s, tag);
00241 if (ret < 0) {
00242 avctx->release_buffer(avctx, &s->frame);
00243 return ret;
00244 }
00245 }
00246 yop_next_macroblock(s);
00247 }
00248
00249 *data_size = sizeof(AVFrame);
00250 *(AVFrame *) data = s->frame;
00251 return avpkt->size;
00252 }
00253
00254 AVCodec ff_yop_decoder = {
00255 .name = "yop",
00256 .type = AVMEDIA_TYPE_VIDEO,
00257 .id = CODEC_ID_YOP,
00258 .priv_data_size = sizeof(YopDecContext),
00259 .init = yop_decode_init,
00260 .close = yop_decode_close,
00261 .decode = yop_decode_frame,
00262 .long_name = NULL_IF_CONFIG_SMALL("Psygnosis YOP Video"),
00263 };