Libav
|
00001 00026 #include "libavutil/intreadwrite.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 // These tables are taken directly from: 00047 // http://wiki.multimedia.cx/index.php?title=Psygnosis_YOP 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 avcodec_check_dimensions(avctx, avctx->width, avctx->height) < 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 s->num_pal_colors = avctx->extradata[0]; 00095 s->first_color[0] = avctx->extradata[1]; 00096 s->first_color[1] = avctx->extradata[2]; 00097 00098 if (s->num_pal_colors + s->first_color[0] > 256 || 00099 s->num_pal_colors + s->first_color[1] > 256) { 00100 av_log(avctx, AV_LOG_ERROR, 00101 "YOP: palette parameters invalid, header probably corrupt\n"); 00102 return AVERROR_INVALIDDATA; 00103 } 00104 00105 return 0; 00106 } 00107 00108 static av_cold int yop_decode_close(AVCodecContext *avctx) 00109 { 00110 YopDecContext *s = avctx->priv_data; 00111 if (s->frame.data[0]) 00112 avctx->release_buffer(avctx, &s->frame); 00113 return 0; 00114 } 00115 00121 static void yop_paint_block(YopDecContext *s, int tag) 00122 { 00123 s->dstptr[0] = s->srcptr[0]; 00124 s->dstptr[1] = s->srcptr[paint_lut[tag][0]]; 00125 s->dstptr[s->frame.linesize[0]] = s->srcptr[paint_lut[tag][1]]; 00126 s->dstptr[s->frame.linesize[0] + 1] = s->srcptr[paint_lut[tag][2]]; 00127 00128 // The number of src bytes consumed is in the last part of the lut entry. 00129 s->srcptr += paint_lut[tag][3]; 00130 } 00131 00136 static int yop_copy_previous_block(YopDecContext *s, int copy_tag) 00137 { 00138 uint8_t *bufptr; 00139 00140 // Calculate position for the copy source 00141 bufptr = s->dstptr + motion_vector[copy_tag][0] + 00142 s->frame.linesize[0] * motion_vector[copy_tag][1]; 00143 if (bufptr < s->dstbuf) { 00144 av_log(s->avctx, AV_LOG_ERROR, 00145 "YOP: cannot decode, file probably corrupt\n"); 00146 return AVERROR_INVALIDDATA; 00147 } 00148 00149 s->dstptr[0] = bufptr[0]; 00150 s->dstptr[1] = bufptr[1]; 00151 s->dstptr[s->frame.linesize[0]] = bufptr[s->frame.linesize[0]]; 00152 s->dstptr[s->frame.linesize[0] + 1] = bufptr[s->frame.linesize[0] + 1]; 00153 00154 return 0; 00155 } 00156 00161 static uint8_t yop_get_next_nibble(YopDecContext *s) 00162 { 00163 int ret; 00164 00165 if (s->low_nibble) { 00166 ret = *s->low_nibble & 0xf; 00167 s->low_nibble = NULL; 00168 }else { 00169 s->low_nibble = s->srcptr++; 00170 ret = *s->low_nibble >> 4; 00171 } 00172 return ret; 00173 } 00174 00178 static void yop_next_macroblock(YopDecContext *s) 00179 { 00180 // If we are advancing to the next row of macroblocks 00181 if (s->row_pos == s->frame.linesize[0] - 2) { 00182 s->dstptr += s->frame.linesize[0]; 00183 s->row_pos = 0; 00184 }else { 00185 s->row_pos += 2; 00186 } 00187 s->dstptr += 2; 00188 } 00189 00190 static int yop_decode_frame(AVCodecContext *avctx, void *data, int *data_size, 00191 AVPacket *avpkt) 00192 { 00193 YopDecContext *s = avctx->priv_data; 00194 int tag, firstcolor, is_odd_frame; 00195 int ret, i; 00196 uint32_t *palette; 00197 00198 if (s->frame.data[0]) 00199 avctx->release_buffer(avctx, &s->frame); 00200 00201 ret = avctx->get_buffer(avctx, &s->frame); 00202 if (ret < 0) { 00203 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n"); 00204 return ret; 00205 } 00206 00207 s->frame.linesize[0] = avctx->width; 00208 00209 s->dstbuf = s->frame.data[0]; 00210 s->dstptr = s->frame.data[0]; 00211 s->srcptr = avpkt->data + 4; 00212 s->row_pos = 0; 00213 s->low_nibble = NULL; 00214 00215 is_odd_frame = avpkt->data[0]; 00216 firstcolor = s->first_color[is_odd_frame]; 00217 palette = (uint32_t *)s->frame.data[1]; 00218 00219 for (i = 0; i < s->num_pal_colors; i++, s->srcptr += 3) 00220 palette[i + firstcolor] = (s->srcptr[0] << 18) | 00221 (s->srcptr[1] << 10) | 00222 (s->srcptr[2] << 2); 00223 00224 s->frame.palette_has_changed = 1; 00225 00226 while (s->dstptr - s->dstbuf < 00227 avctx->width * avctx->height && 00228 s->srcptr - avpkt->data < avpkt->size) { 00229 00230 tag = yop_get_next_nibble(s); 00231 00232 if (tag != 0xf) { 00233 yop_paint_block(s, tag); 00234 }else { 00235 tag = yop_get_next_nibble(s); 00236 ret = yop_copy_previous_block(s, tag); 00237 if (ret < 0) { 00238 avctx->release_buffer(avctx, &s->frame); 00239 return ret; 00240 } 00241 } 00242 yop_next_macroblock(s); 00243 } 00244 00245 *data_size = sizeof(AVFrame); 00246 *(AVFrame *) data = s->frame; 00247 return avpkt->size; 00248 } 00249 00250 AVCodec yop_decoder = { 00251 "yop", 00252 AVMEDIA_TYPE_VIDEO, 00253 CODEC_ID_YOP, 00254 sizeof(YopDecContext), 00255 yop_decode_init, 00256 NULL, 00257 yop_decode_close, 00258 yop_decode_frame, 00259 .long_name = NULL_IF_CONFIG_SMALL("Psygnosis YOP Video"), 00260 };