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 #include "internal.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 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 = AV_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 "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, "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 *got_frame,
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 = ff_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 palette[i + firstcolor] |= 0xFFU << 24 |
00238 (palette[i + firstcolor] >> 6) & 0x30303;
00239 }
00240
00241 s->frame.palette_has_changed = 1;
00242
00243 while (s->dstptr - s->dstbuf <
00244 avctx->width * avctx->height &&
00245 s->srcptr - avpkt->data < avpkt->size) {
00246
00247 tag = yop_get_next_nibble(s);
00248
00249 if (tag != 0xf) {
00250 yop_paint_block(s, tag);
00251 }else {
00252 tag = yop_get_next_nibble(s);
00253 ret = yop_copy_previous_block(s, tag);
00254 if (ret < 0) {
00255 avctx->release_buffer(avctx, &s->frame);
00256 return ret;
00257 }
00258 }
00259 yop_next_macroblock(s);
00260 }
00261
00262 *got_frame = 1;
00263 *(AVFrame *) data = s->frame;
00264 return avpkt->size;
00265 }
00266
00267 AVCodec ff_yop_decoder = {
00268 .name = "yop",
00269 .type = AVMEDIA_TYPE_VIDEO,
00270 .id = AV_CODEC_ID_YOP,
00271 .priv_data_size = sizeof(YopDecContext),
00272 .init = yop_decode_init,
00273 .close = yop_decode_close,
00274 .decode = yop_decode_frame,
00275 .long_name = NULL_IF_CONFIG_SMALL("Psygnosis YOP Video"),
00276 };