Libav 0.7.1
libavcodec/yop.c
Go to the documentation of this file.
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 // These tables are taken directly from:
00048 // http://wiki.multimedia.cx/index.php?title=Psygnosis_YOP
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     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     // The number of src bytes consumed is in the last part of the lut entry.
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     // Calculate position for the copy source
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     // If we are advancing to the next row of macroblocks
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 
00225     s->frame.palette_has_changed = 1;
00226 
00227     while (s->dstptr - s->dstbuf <
00228            avctx->width * avctx->height &&
00229            s->srcptr - avpkt->data < avpkt->size) {
00230 
00231         tag = yop_get_next_nibble(s);
00232 
00233         if (tag != 0xf) {
00234             yop_paint_block(s, tag);
00235         }else {
00236             tag = yop_get_next_nibble(s);
00237             ret = yop_copy_previous_block(s, tag);
00238             if (ret < 0) {
00239                 avctx->release_buffer(avctx, &s->frame);
00240                 return ret;
00241             }
00242         }
00243         yop_next_macroblock(s);
00244     }
00245 
00246     *data_size        = sizeof(AVFrame);
00247     *(AVFrame *) data = s->frame;
00248     return avpkt->size;
00249 }
00250 
00251 AVCodec ff_yop_decoder = {
00252     "yop",
00253     AVMEDIA_TYPE_VIDEO,
00254     CODEC_ID_YOP,
00255     sizeof(YopDecContext),
00256     yop_decode_init,
00257     NULL,
00258     yop_decode_close,
00259     yop_decode_frame,
00260     .long_name = NULL_IF_CONFIG_SMALL("Psygnosis YOP Video"),
00261 };