annotate ffmpeg/libavcodec/bethsoftvideo.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 * Bethesda VID video decoder
yading@10 3 * Copyright (C) 2007 Nicholas Tung
yading@10 4 *
yading@10 5 * This file is part of FFmpeg.
yading@10 6 *
yading@10 7 * FFmpeg is free software; you can redistribute it and/or
yading@10 8 * modify it under the terms of the GNU Lesser General Public
yading@10 9 * License as published by the Free Software Foundation; either
yading@10 10 * version 2.1 of the License, or (at your option) any later version.
yading@10 11 *
yading@10 12 * FFmpeg is distributed in the hope that it will be useful,
yading@10 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
yading@10 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
yading@10 15 * Lesser General Public License for more details.
yading@10 16 *
yading@10 17 * You should have received a copy of the GNU Lesser General Public
yading@10 18 * License along with FFmpeg; if not, write to the Free Software
yading@10 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
yading@10 20 */
yading@10 21
yading@10 22 /**
yading@10 23 * @file
yading@10 24 * @brief Bethesda Softworks VID Video Decoder
yading@10 25 * @author Nicholas Tung [ntung (at. ntung com] (2007-03)
yading@10 26 * @see http://wiki.multimedia.cx/index.php?title=Bethsoft_VID
yading@10 27 * @see http://www.svatopluk.com/andux/docs/dfvid.html
yading@10 28 */
yading@10 29
yading@10 30 #include "libavutil/common.h"
yading@10 31 #include "avcodec.h"
yading@10 32 #include "bethsoftvideo.h"
yading@10 33 #include "bytestream.h"
yading@10 34 #include "internal.h"
yading@10 35
yading@10 36 typedef struct BethsoftvidContext {
yading@10 37 AVFrame frame;
yading@10 38 GetByteContext g;
yading@10 39 } BethsoftvidContext;
yading@10 40
yading@10 41 static av_cold int bethsoftvid_decode_init(AVCodecContext *avctx)
yading@10 42 {
yading@10 43 BethsoftvidContext *vid = avctx->priv_data;
yading@10 44 avcodec_get_frame_defaults(&vid->frame);
yading@10 45 avctx->pix_fmt = AV_PIX_FMT_PAL8;
yading@10 46 return 0;
yading@10 47 }
yading@10 48
yading@10 49 static int set_palette(BethsoftvidContext *ctx)
yading@10 50 {
yading@10 51 uint32_t *palette = (uint32_t *)ctx->frame.data[1];
yading@10 52 int a;
yading@10 53
yading@10 54 if (bytestream2_get_bytes_left(&ctx->g) < 256*3)
yading@10 55 return AVERROR_INVALIDDATA;
yading@10 56
yading@10 57 for(a = 0; a < 256; a++){
yading@10 58 palette[a] = 0xFFU << 24 | bytestream2_get_be24u(&ctx->g) * 4;
yading@10 59 palette[a] |= palette[a] >> 6 & 0x30303;
yading@10 60 }
yading@10 61 ctx->frame.palette_has_changed = 1;
yading@10 62 return 0;
yading@10 63 }
yading@10 64
yading@10 65 static int bethsoftvid_decode_frame(AVCodecContext *avctx,
yading@10 66 void *data, int *got_frame,
yading@10 67 AVPacket *avpkt)
yading@10 68 {
yading@10 69 BethsoftvidContext * vid = avctx->priv_data;
yading@10 70 char block_type;
yading@10 71 uint8_t * dst;
yading@10 72 uint8_t * frame_end;
yading@10 73 int remaining = avctx->width; // number of bytes remaining on a line
yading@10 74 int wrap_to_next_line;
yading@10 75 int code, ret;
yading@10 76 int yoffset;
yading@10 77
yading@10 78 if ((ret = ff_reget_buffer(avctx, &vid->frame)) < 0)
yading@10 79 return ret;
yading@10 80 wrap_to_next_line = vid->frame.linesize[0] - avctx->width;
yading@10 81
yading@10 82 if (avpkt->side_data_elems > 0 &&
yading@10 83 avpkt->side_data[0].type == AV_PKT_DATA_PALETTE) {
yading@10 84 bytestream2_init(&vid->g, avpkt->side_data[0].data,
yading@10 85 avpkt->side_data[0].size);
yading@10 86 if ((ret = set_palette(vid)) < 0)
yading@10 87 return ret;
yading@10 88 }
yading@10 89
yading@10 90 bytestream2_init(&vid->g, avpkt->data, avpkt->size);
yading@10 91 dst = vid->frame.data[0];
yading@10 92 frame_end = vid->frame.data[0] + vid->frame.linesize[0] * avctx->height;
yading@10 93
yading@10 94 switch(block_type = bytestream2_get_byte(&vid->g)){
yading@10 95 case PALETTE_BLOCK: {
yading@10 96 *got_frame = 0;
yading@10 97 if ((ret = set_palette(vid)) < 0) {
yading@10 98 av_log(avctx, AV_LOG_ERROR, "error reading palette\n");
yading@10 99 return ret;
yading@10 100 }
yading@10 101 return bytestream2_tell(&vid->g);
yading@10 102 }
yading@10 103 case VIDEO_YOFF_P_FRAME:
yading@10 104 yoffset = bytestream2_get_le16(&vid->g);
yading@10 105 if(yoffset >= avctx->height)
yading@10 106 return AVERROR_INVALIDDATA;
yading@10 107 dst += vid->frame.linesize[0] * yoffset;
yading@10 108 }
yading@10 109
yading@10 110 // main code
yading@10 111 while((code = bytestream2_get_byte(&vid->g))){
yading@10 112 int length = code & 0x7f;
yading@10 113
yading@10 114 // copy any bytes starting at the current position, and ending at the frame width
yading@10 115 while(length > remaining){
yading@10 116 if(code < 0x80)
yading@10 117 bytestream2_get_buffer(&vid->g, dst, remaining);
yading@10 118 else if(block_type == VIDEO_I_FRAME)
yading@10 119 memset(dst, bytestream2_peek_byte(&vid->g), remaining);
yading@10 120 length -= remaining; // decrement the number of bytes to be copied
yading@10 121 dst += remaining + wrap_to_next_line; // skip over extra bytes at end of frame
yading@10 122 remaining = avctx->width;
yading@10 123 if(dst == frame_end)
yading@10 124 goto end;
yading@10 125 }
yading@10 126
yading@10 127 // copy any remaining bytes after / if line overflows
yading@10 128 if(code < 0x80)
yading@10 129 bytestream2_get_buffer(&vid->g, dst, length);
yading@10 130 else if(block_type == VIDEO_I_FRAME)
yading@10 131 memset(dst, bytestream2_get_byte(&vid->g), length);
yading@10 132 remaining -= length;
yading@10 133 dst += length;
yading@10 134 }
yading@10 135 end:
yading@10 136
yading@10 137 if ((ret = av_frame_ref(data, &vid->frame)) < 0)
yading@10 138 return ret;
yading@10 139
yading@10 140 *got_frame = 1;
yading@10 141
yading@10 142 return avpkt->size;
yading@10 143 }
yading@10 144
yading@10 145 static av_cold int bethsoftvid_decode_end(AVCodecContext *avctx)
yading@10 146 {
yading@10 147 BethsoftvidContext * vid = avctx->priv_data;
yading@10 148 av_frame_unref(&vid->frame);
yading@10 149 return 0;
yading@10 150 }
yading@10 151
yading@10 152 AVCodec ff_bethsoftvid_decoder = {
yading@10 153 .name = "bethsoftvid",
yading@10 154 .type = AVMEDIA_TYPE_VIDEO,
yading@10 155 .id = AV_CODEC_ID_BETHSOFTVID,
yading@10 156 .priv_data_size = sizeof(BethsoftvidContext),
yading@10 157 .init = bethsoftvid_decode_init,
yading@10 158 .close = bethsoftvid_decode_end,
yading@10 159 .decode = bethsoftvid_decode_frame,
yading@10 160 .capabilities = CODEC_CAP_DR1,
yading@10 161 .long_name = NULL_IF_CONFIG_SMALL("Bethesda VID video"),
yading@10 162 };