annotate ffmpeg/libavcodec/yop.c @ 13:844d341cf643 tip

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