• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

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

Generated on Fri Feb 22 2013 07:24:30 for FFmpeg by  doxygen 1.7.1