Statistics
| Branch: | Revision:

ffmpeg / libavcodec / bethsoftvideo.c @ be7e7932

History | View | Annotate | Download (4.54 KB)

1 1e6c6759 Nicholas Tung
/*
2
 * Bethesda VID video decoder
3
 * Copyright (C) 2007 Nicholas Tung
4
 *
5
 * This file is part of FFmpeg.
6
 *
7
 * FFmpeg is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
11
 *
12
 * FFmpeg is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with FFmpeg; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
 */
21
22
/**
23 bad5537e Diego Biurrun
 * @file libavcodec/bethsoftvideo.c
24 1e6c6759 Nicholas Tung
 * @brief Bethesda Softworks VID Video Decoder
25
 * @author Nicholas Tung [ntung (at. ntung com] (2007-03)
26
 * @sa http://wiki.multimedia.cx/index.php?title=Bethsoft_VID
27
 * @sa http://www.svatopluk.com/andux/docs/dfvid.html
28
 */
29
30 245976da Diego Biurrun
#include "libavutil/common.h"
31 1e6c6759 Nicholas Tung
#include "dsputil.h"
32
#include "bethsoftvideo.h"
33
#include "bytestream.h"
34
35
typedef struct BethsoftvidContext {
36
    AVFrame frame;
37
} BethsoftvidContext;
38
39 98a6fff9 Zuxy Meng
static av_cold int bethsoftvid_decode_init(AVCodecContext *avctx)
40 1e6c6759 Nicholas Tung
{
41
    BethsoftvidContext *vid = avctx->priv_data;
42
    vid->frame.reference = 1;
43
    vid->frame.buffer_hints = FF_BUFFER_HINTS_VALID |
44
        FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
45 bce31749 Michael Niedermayer
    avctx->pix_fmt = PIX_FMT_PAL8;
46 1e6c6759 Nicholas Tung
    return 0;
47
}
48
49 5d9375da Michael Niedermayer
static void set_palette(AVFrame * frame, const uint8_t * palette_buffer)
50 1e6c6759 Nicholas Tung
{
51
    uint32_t * palette = (uint32_t *)frame->data[1];
52
    int a;
53 dc338d17 Michael Niedermayer
    for(a = 0; a < 256; a++){
54 bce31749 Michael Niedermayer
        palette[a] = AV_RB24(&palette_buffer[a * 3]) * 4;
55 1e6c6759 Nicholas Tung
    }
56
    frame->palette_has_changed = 1;
57
}
58
59
static int bethsoftvid_decode_frame(AVCodecContext *avctx,
60
                              void *data, int *data_size,
61 7a00bbad Thilo Borgmann
                              AVPacket *avpkt)
62 1e6c6759 Nicholas Tung
{
63 7a00bbad Thilo Borgmann
    const uint8_t *buf = avpkt->data;
64
    int buf_size = avpkt->size;
65 1e6c6759 Nicholas Tung
    BethsoftvidContext * vid = avctx->priv_data;
66
    char block_type;
67 43769d72 Michael Niedermayer
    uint8_t * dst;
68 1e6c6759 Nicholas Tung
    uint8_t * frame_end;
69 43769d72 Michael Niedermayer
    int remaining = avctx->width;          // number of bytes remaining on a line
70 1e6c6759 Nicholas Tung
    const int wrap_to_next_line = vid->frame.linesize[0] - avctx->width;
71 43769d72 Michael Niedermayer
    int code;
72 1e6c6759 Nicholas Tung
    int yoffset;
73
74
    if (avctx->reget_buffer(avctx, &vid->frame)) {
75
        av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
76
        return -1;
77
    }
78 43769d72 Michael Niedermayer
    dst = vid->frame.data[0];
79 1e6c6759 Nicholas Tung
    frame_end = vid->frame.data[0] + vid->frame.linesize[0] * avctx->height;
80
81 dc338d17 Michael Niedermayer
    switch(block_type = *buf++){
82
        case PALETTE_BLOCK:
83
            set_palette(&vid->frame, buf);
84
            return 0;
85 d7cf4489 Michael Niedermayer
        case VIDEO_YOFF_P_FRAME:
86 1e6c6759 Nicholas Tung
            yoffset = bytestream_get_le16(&buf);
87 dc338d17 Michael Niedermayer
            if(yoffset >= avctx->height)
88
                return -1;
89 43769d72 Michael Niedermayer
            dst += vid->frame.linesize[0] * yoffset;
90 1e6c6759 Nicholas Tung
    }
91
92
    // main code
93 43769d72 Michael Niedermayer
    while((code = *buf++)){
94
        int length = code & 0x7f;
95 1e6c6759 Nicholas Tung
96
        // copy any bytes starting at the current position, and ending at the frame width
97 43769d72 Michael Niedermayer
        while(length > remaining){
98
            if(code < 0x80)
99
                bytestream_get_buffer(&buf, dst, remaining);
100 dc338d17 Michael Niedermayer
            else if(block_type == VIDEO_I_FRAME)
101 43769d72 Michael Niedermayer
                memset(dst, buf[0], remaining);
102
            length -= remaining;      // decrement the number of bytes to be copied
103
            dst += remaining + wrap_to_next_line;    // skip over extra bytes at end of frame
104
            remaining = avctx->width;
105
            if(dst == frame_end)
106 dc338d17 Michael Niedermayer
                goto end;
107 1e6c6759 Nicholas Tung
        }
108
109
        // copy any remaining bytes after / if line overflows
110 43769d72 Michael Niedermayer
        if(code < 0x80)
111
            bytestream_get_buffer(&buf, dst, length);
112 dc338d17 Michael Niedermayer
        else if(block_type == VIDEO_I_FRAME)
113 43769d72 Michael Niedermayer
            memset(dst, *buf++, length);
114
        remaining -= length;
115
        dst += length;
116 1e6c6759 Nicholas Tung
    }
117
    end:
118
119
    *data_size = sizeof(AVFrame);
120
    *(AVFrame*)data = vid->frame;
121
122
    return buf_size;
123
}
124
125 98a6fff9 Zuxy Meng
static av_cold int bethsoftvid_decode_end(AVCodecContext *avctx)
126 1e6c6759 Nicholas Tung
{
127
    BethsoftvidContext * vid = avctx->priv_data;
128 dc338d17 Michael Niedermayer
    if(vid->frame.data[0])
129
        avctx->release_buffer(avctx, &vid->frame);
130 1e6c6759 Nicholas Tung
    return 0;
131
}
132
133
AVCodec bethsoftvid_decoder = {
134
    .name = "bethsoftvid",
135
    .type = CODEC_TYPE_VIDEO,
136
    .id = CODEC_ID_BETHSOFTVID,
137
    .priv_data_size = sizeof(BethsoftvidContext),
138
    .init = bethsoftvid_decode_init,
139
    .close = bethsoftvid_decode_end,
140
    .decode = bethsoftvid_decode_frame,
141 be7e7932 Baptiste Coudurier
    .capabilities = CODEC_CAP_DR1,
142 fe4bf374 Stefano Sabatini
    .long_name = NULL_IF_CONFIG_SMALL("Bethesda VID video"),
143 1e6c6759 Nicholas Tung
};