1/* 2 * Psygnosis YOP decoder 3 * 4 * Copyright (C) 2010 Mohamed Naufal Basheer <naufal11@gmail.com> 5 * derived from the code by 6 * Copyright (C) 2009 Thomas P. Higdon <thomas.p.higdon@gmail.com> 7 * 8 * This file is part of FFmpeg. 9 * 10 * FFmpeg is free software; you can redistribute it and/or 11 * modify it under the terms of the GNU Lesser General Public 12 * License as published by the Free Software Foundation; either 13 * version 2.1 of the License, or (at your option) any later version. 14 * 15 * FFmpeg is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 18 * Lesser General Public License for more details. 19 * 20 * You should have received a copy of the GNU Lesser General Public 21 * License along with FFmpeg; if not, write to the Free Software 22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 23 */ 24 25#include <string.h> 26 27#include "libavutil/imgutils.h" 28#include "libavutil/internal.h" 29#include "libavutil/intreadwrite.h" 30 31#include "avcodec.h" 32#include "codec_internal.h" 33#include "internal.h" 34 35typedef struct YopDecContext { 36 AVCodecContext *avctx; 37 AVFrame *frame; 38 39 int num_pal_colors; 40 int first_color[2]; 41 int frame_data_length; 42 43 const uint8_t *low_nibble; 44 const uint8_t *srcptr; 45 const uint8_t *src_end; 46 uint8_t *dstptr; 47 uint8_t *dstbuf; 48} YopDecContext; 49 50// These tables are taken directly from: 51// http://wiki.multimedia.cx/index.php?title=Psygnosis_YOP 52 53/** 54 * Lookup table for painting macroblocks. Bytes 0-2 of each entry contain 55 * the macroblock positions to be painted (taken as (0, B0, B1, B2)). 56 * Byte 3 contains the number of bytes consumed on the input, 57 * equal to max(bytes 0-2) + 1. 58 */ 59static const uint8_t paint_lut[15][4] = 60 {{1, 2, 3, 4}, {1, 2, 0, 3}, 61 {1, 2, 1, 3}, {1, 2, 2, 3}, 62 {1, 0, 2, 3}, {1, 0, 0, 2}, 63 {1, 0, 1, 2}, {1, 1, 2, 3}, 64 {0, 1, 2, 3}, {0, 1, 0, 2}, 65 {1, 1, 0, 2}, {0, 1, 1, 2}, 66 {0, 0, 1, 2}, {0, 0, 0, 1}, 67 {1, 1, 1, 2}, 68 }; 69 70/** 71 * Lookup table for copying macroblocks. Each entry contains the respective 72 * x and y pixel offset for the copy source. 73 */ 74static const int8_t motion_vector[16][2] = 75 {{-4, -4}, {-2, -4}, 76 { 0, -4}, { 2, -4}, 77 {-4, -2}, {-4, 0}, 78 {-3, -3}, {-1, -3}, 79 { 1, -3}, { 3, -3}, 80 {-3, -1}, {-2, -2}, 81 { 0, -2}, { 2, -2}, 82 { 4, -2}, {-2, 0}, 83 }; 84 85static av_cold int yop_decode_close(AVCodecContext *avctx) 86{ 87 YopDecContext *s = avctx->priv_data; 88 89 av_frame_free(&s->frame); 90 91 return 0; 92} 93 94static av_cold int yop_decode_init(AVCodecContext *avctx) 95{ 96 YopDecContext *s = avctx->priv_data; 97 s->avctx = avctx; 98 99 if (avctx->width & 1 || avctx->height & 1 || 100 av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0) { 101 av_log(avctx, AV_LOG_ERROR, "YOP has invalid dimensions\n"); 102 return AVERROR_INVALIDDATA; 103 } 104 105 if (avctx->extradata_size < 3) { 106 av_log(avctx, AV_LOG_ERROR, "Missing or incomplete extradata.\n"); 107 return AVERROR_INVALIDDATA; 108 } 109 110 avctx->pix_fmt = AV_PIX_FMT_PAL8; 111 112 s->num_pal_colors = avctx->extradata[0]; 113 s->first_color[0] = avctx->extradata[1]; 114 s->first_color[1] = avctx->extradata[2]; 115 116 if (s->num_pal_colors + s->first_color[0] > 256 || 117 s->num_pal_colors + s->first_color[1] > 256) { 118 av_log(avctx, AV_LOG_ERROR, 119 "Palette parameters invalid, header probably corrupt\n"); 120 return AVERROR_INVALIDDATA; 121 } 122 123 s->frame = av_frame_alloc(); 124 if (!s->frame) 125 return AVERROR(ENOMEM); 126 127 return 0; 128} 129 130/** 131 * Paint a macroblock using the pattern in paint_lut. 132 * @param s codec context 133 * @param tag the tag that was in the nibble 134 */ 135static int yop_paint_block(YopDecContext *s, int linesize, int tag) 136{ 137 if (s->src_end - s->srcptr < paint_lut[tag][3]) { 138 av_log(s->avctx, AV_LOG_ERROR, "Packet too small.\n"); 139 return AVERROR_INVALIDDATA; 140 } 141 142 s->dstptr[0] = s->srcptr[0]; 143 s->dstptr[1] = s->srcptr[paint_lut[tag][0]]; 144 s->dstptr[linesize] = s->srcptr[paint_lut[tag][1]]; 145 s->dstptr[linesize + 1] = s->srcptr[paint_lut[tag][2]]; 146 147 // The number of src bytes consumed is in the last part of the lut entry. 148 s->srcptr += paint_lut[tag][3]; 149 return 0; 150} 151 152/** 153 * Copy a previously painted macroblock to the current_block. 154 * @param copy_tag the tag that was in the nibble 155 */ 156static int yop_copy_previous_block(YopDecContext *s, int linesize, int copy_tag) 157{ 158 uint8_t *bufptr; 159 160 // Calculate position for the copy source 161 bufptr = s->dstptr + motion_vector[copy_tag][0] + 162 linesize * motion_vector[copy_tag][1]; 163 if (bufptr < s->dstbuf) { 164 av_log(s->avctx, AV_LOG_ERROR, "File probably corrupt\n"); 165 return AVERROR_INVALIDDATA; 166 } 167 168 s->dstptr[0] = bufptr[0]; 169 s->dstptr[1] = bufptr[1]; 170 s->dstptr[linesize] = bufptr[linesize]; 171 s->dstptr[linesize + 1] = bufptr[linesize + 1]; 172 173 return 0; 174} 175 176/** 177 * Return the next nibble in sequence, consuming a new byte on the input 178 * only if necessary. 179 */ 180static uint8_t yop_get_next_nibble(YopDecContext *s) 181{ 182 int ret; 183 184 if (s->low_nibble) { 185 ret = *s->low_nibble & 0xf; 186 s->low_nibble = NULL; 187 }else { 188 s->low_nibble = s->srcptr++; 189 ret = *s->low_nibble >> 4; 190 } 191 return ret; 192} 193 194static int yop_decode_frame(AVCodecContext *avctx, AVFrame *rframe, 195 int *got_frame, AVPacket *avpkt) 196{ 197 YopDecContext *s = avctx->priv_data; 198 AVFrame *frame = s->frame; 199 int tag, firstcolor, is_odd_frame; 200 int ret, i, x, y; 201 uint32_t *palette; 202 203 if (avpkt->size < 4 + 3 * s->num_pal_colors) { 204 av_log(avctx, AV_LOG_ERROR, "Packet too small.\n"); 205 return AVERROR_INVALIDDATA; 206 } 207 208 if ((ret = ff_reget_buffer(avctx, frame, 0)) < 0) 209 return ret; 210 211 if (!avctx->frame_number) 212 memset(frame->data[1], 0, AVPALETTE_SIZE); 213 214 s->dstbuf = frame->data[0]; 215 s->dstptr = frame->data[0]; 216 s->srcptr = avpkt->data + 4; 217 s->src_end = avpkt->data + avpkt->size; 218 s->low_nibble = NULL; 219 220 is_odd_frame = avpkt->data[0]; 221 if(is_odd_frame>1){ 222 av_log(avctx, AV_LOG_ERROR, "frame is too odd %d\n", is_odd_frame); 223 return AVERROR_INVALIDDATA; 224 } 225 firstcolor = s->first_color[is_odd_frame]; 226 palette = (uint32_t *)frame->data[1]; 227 228 for (i = 0; i < s->num_pal_colors; i++, s->srcptr += 3) { 229 palette[i + firstcolor] = (s->srcptr[0] << 18) | 230 (s->srcptr[1] << 10) | 231 (s->srcptr[2] << 2); 232 palette[i + firstcolor] |= 0xFFU << 24 | 233 (palette[i + firstcolor] >> 6) & 0x30303; 234 } 235 236 frame->palette_has_changed = 1; 237 238 for (y = 0; y < avctx->height; y += 2) { 239 for (x = 0; x < avctx->width; x += 2) { 240 if (s->srcptr - avpkt->data >= avpkt->size) { 241 av_log(avctx, AV_LOG_ERROR, "Packet too small.\n"); 242 return AVERROR_INVALIDDATA; 243 } 244 245 tag = yop_get_next_nibble(s); 246 247 if (tag != 0xf) { 248 ret = yop_paint_block(s, frame->linesize[0], tag); 249 if (ret < 0) 250 return ret; 251 } else { 252 tag = yop_get_next_nibble(s); 253 ret = yop_copy_previous_block(s, frame->linesize[0], tag); 254 if (ret < 0) 255 return ret; 256 } 257 s->dstptr += 2; 258 } 259 s->dstptr += 2*frame->linesize[0] - x; 260 } 261 262 if ((ret = av_frame_ref(rframe, s->frame)) < 0) 263 return ret; 264 265 *got_frame = 1; 266 return avpkt->size; 267} 268 269const FFCodec ff_yop_decoder = { 270 .p.name = "yop", 271 .p.long_name = NULL_IF_CONFIG_SMALL("Psygnosis YOP Video"), 272 .p.type = AVMEDIA_TYPE_VIDEO, 273 .p.id = AV_CODEC_ID_YOP, 274 .priv_data_size = sizeof(YopDecContext), 275 .init = yop_decode_init, 276 .close = yop_decode_close, 277 FF_CODEC_DECODE_CB(yop_decode_frame), 278 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE, 279}; 280