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 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 avcodec_get_frame_defaults(&s->frame);
00100 s->num_pal_colors = avctx->extradata[0];
00101 s->first_color[0] = avctx->extradata[1];
00102 s->first_color[1] = avctx->extradata[2];
00103
00104 if (s->num_pal_colors + s->first_color[0] > 256 ||
00105 s->num_pal_colors + s->first_color[1] > 256) {
00106 av_log(avctx, AV_LOG_ERROR,
00107 "YOP: palette parameters invalid, header probably corrupt\n");
00108 return AVERROR_INVALIDDATA;
00109 }
00110
00111 return 0;
00112 }
00113
00114 static av_cold int yop_decode_close(AVCodecContext *avctx)
00115 {
00116 YopDecContext *s = avctx->priv_data;
00117 if (s->frame.data[0])
00118 avctx->release_buffer(avctx, &s->frame);
00119 return 0;
00120 }
00121
00127 static void yop_paint_block(YopDecContext *s, int tag)
00128 {
00129 s->dstptr[0] = s->srcptr[0];
00130 s->dstptr[1] = s->srcptr[paint_lut[tag][0]];
00131 s->dstptr[s->frame.linesize[0]] = s->srcptr[paint_lut[tag][1]];
00132 s->dstptr[s->frame.linesize[0] + 1] = s->srcptr[paint_lut[tag][2]];
00133
00134
00135 s->srcptr += paint_lut[tag][3];
00136 }
00137
00142 static int yop_copy_previous_block(YopDecContext *s, int copy_tag)
00143 {
00144 uint8_t *bufptr;
00145
00146
00147 bufptr = s->dstptr + motion_vector[copy_tag][0] +
00148 s->frame.linesize[0] * motion_vector[copy_tag][1];
00149 if (bufptr < s->dstbuf) {
00150 av_log(s->avctx, AV_LOG_ERROR,
00151 "YOP: cannot decode, file probably corrupt\n");
00152 return AVERROR_INVALIDDATA;
00153 }
00154
00155 s->dstptr[0] = bufptr[0];
00156 s->dstptr[1] = bufptr[1];
00157 s->dstptr[s->frame.linesize[0]] = bufptr[s->frame.linesize[0]];
00158 s->dstptr[s->frame.linesize[0] + 1] = bufptr[s->frame.linesize[0] + 1];
00159
00160 return 0;
00161 }
00162
00167 static uint8_t yop_get_next_nibble(YopDecContext *s)
00168 {
00169 int ret;
00170
00171 if (s->low_nibble) {
00172 ret = *s->low_nibble & 0xf;
00173 s->low_nibble = NULL;
00174 }else {
00175 s->low_nibble = s->srcptr++;
00176 ret = *s->low_nibble >> 4;
00177 }
00178 return ret;
00179 }
00180
00184 static void yop_next_macroblock(YopDecContext *s)
00185 {
00186
00187 if (s->row_pos == s->frame.linesize[0] - 2) {
00188 s->dstptr += s->frame.linesize[0];
00189 s->row_pos = 0;
00190 }else {
00191 s->row_pos += 2;
00192 }
00193 s->dstptr += 2;
00194 }
00195
00196 static int yop_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
00197 AVPacket *avpkt)
00198 {
00199 YopDecContext *s = avctx->priv_data;
00200 int tag, firstcolor, is_odd_frame;
00201 int ret, i;
00202 uint32_t *palette;
00203
00204 if (s->frame.data[0])
00205 avctx->release_buffer(avctx, &s->frame);
00206
00207 if (avpkt->size < 4 + 3*s->num_pal_colors) {
00208 av_log(avctx, AV_LOG_ERROR, "packet of size %d too small\n", avpkt->size);
00209 return AVERROR_INVALIDDATA;
00210 }
00211
00212 ret = avctx->get_buffer(avctx, &s->frame);
00213 if (ret < 0) {
00214 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00215 return ret;
00216 }
00217
00218 s->frame.linesize[0] = avctx->width;
00219
00220 s->dstbuf = s->frame.data[0];
00221 s->dstptr = s->frame.data[0];
00222 s->srcptr = avpkt->data + 4;
00223 s->row_pos = 0;
00224 s->low_nibble = NULL;
00225
00226 is_odd_frame = avpkt->data[0];
00227 if(is_odd_frame>1){
00228 av_log(avctx, AV_LOG_ERROR, "frame is too odd %d\n", is_odd_frame);
00229 return AVERROR_INVALIDDATA;
00230 }
00231 firstcolor = s->first_color[is_odd_frame];
00232 palette = (uint32_t *)s->frame.data[1];
00233
00234 for (i = 0; i < s->num_pal_colors; i++, s->srcptr += 3) {
00235 palette[i + firstcolor] = (s->srcptr[0] << 18) |
00236 (s->srcptr[1] << 10) |
00237 (s->srcptr[2] << 2);
00238 palette[i + firstcolor] |= 0xFF << 24 |
00239 (palette[i + firstcolor] >> 6) & 0x30303;
00240 }
00241
00242 s->frame.palette_has_changed = 1;
00243
00244 while (s->dstptr - s->dstbuf <
00245 avctx->width * avctx->height &&
00246 s->srcptr - avpkt->data < avpkt->size) {
00247
00248 tag = yop_get_next_nibble(s);
00249
00250 if (tag != 0xf) {
00251 yop_paint_block(s, tag);
00252 }else {
00253 tag = yop_get_next_nibble(s);
00254 ret = yop_copy_previous_block(s, tag);
00255 if (ret < 0) {
00256 avctx->release_buffer(avctx, &s->frame);
00257 return ret;
00258 }
00259 }
00260 yop_next_macroblock(s);
00261 }
00262
00263 *data_size = sizeof(AVFrame);
00264 *(AVFrame *) data = s->frame;
00265 return avpkt->size;
00266 }
00267
00268 AVCodec ff_yop_decoder = {
00269 .name = "yop",
00270 .type = AVMEDIA_TYPE_VIDEO,
00271 .id = AV_CODEC_ID_YOP,
00272 .priv_data_size = sizeof(YopDecContext),
00273 .init = yop_decode_init,
00274 .close = yop_decode_close,
00275 .decode = yop_decode_frame,
00276 .long_name = NULL_IF_CONFIG_SMALL("Psygnosis YOP Video"),
00277 };