Statistics
| Branch: | Revision:

ffmpeg / libavformat / avs.c @ e19fbbec

History | View | Annotate | Download (6.49 KB)

1
/*
2
 * AVS demuxer.
3
 * Copyright (c) 2006  Aurelien Jacobs <aurel@gnuage.org>
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 St, Fifth Floor, Boston, MA  02110-1301  USA
20
 */
21

    
22
#include "avformat.h"
23
#include "voc.h"
24

    
25

    
26
typedef struct avs_format {
27
    voc_dec_context_t voc;
28
    AVStream *st_video;
29
    AVStream *st_audio;
30
    int width;
31
    int height;
32
    int bits_per_sample;
33
    int fps;
34
    int nb_frames;
35
    int remaining_frame_size;
36
    int remaining_audio_size;
37
} avs_format_t;
38

    
39
typedef enum avs_block_type {
40
    AVS_VIDEO     = 0x01,
41
    AVS_AUDIO     = 0x02,
42
    AVS_PALETTE   = 0x03,
43
    AVS_GAME_DATA = 0x04,
44
} avs_block_type_t;
45

    
46
static int avs_probe(AVProbeData * p)
47
{
48
    const uint8_t *d;
49

    
50
    if (p->buf_size < 2)
51
        return 0;
52
    d = p->buf;
53
    if (d[0] == 'w' && d[1] == 'W' && d[2] == 0x10 && d[3] == 0)
54
        return 50;
55

    
56
    return 0;
57
}
58

    
59
static int avs_read_header(AVFormatContext * s, AVFormatParameters * ap)
60
{
61
    avs_format_t *avs = s->priv_data;
62

    
63
    s->ctx_flags |= AVFMTCTX_NOHEADER;
64

    
65
    url_fskip(&s->pb, 4);
66
    avs->width = get_le16(&s->pb);
67
    avs->height = get_le16(&s->pb);
68
    avs->bits_per_sample = get_le16(&s->pb);
69
    avs->fps = get_le16(&s->pb);
70
    avs->nb_frames = get_le32(&s->pb);
71
    avs->remaining_frame_size = 0;
72
    avs->remaining_audio_size = 0;
73

    
74
    avs->st_video = avs->st_audio = NULL;
75

    
76
    if (avs->width != 318 || avs->height != 198)
77
        av_log(s, AV_LOG_ERROR, "This avs pretend to be %dx%d "
78
               "when the avs format is supposed to be 318x198 only.\n",
79
               avs->width, avs->height);
80

    
81
    return 0;
82
}
83

    
84
static int
85
avs_read_video_packet(AVFormatContext * s, AVPacket * pkt,
86
                      avs_block_type_t type, int sub_type, int size,
87
                      uint8_t * palette, int palette_size)
88
{
89
    avs_format_t *avs = s->priv_data;
90
    int ret;
91

    
92
    ret = av_new_packet(pkt, size + palette_size);
93
    if (ret < 0)
94
        return ret;
95

    
96
    if (palette_size) {
97
        pkt->data[0] = 0x00;
98
        pkt->data[1] = 0x03;
99
        pkt->data[2] = palette_size & 0xFF;
100
        pkt->data[3] = (palette_size >> 8) & 0xFF;
101
        memcpy(pkt->data + 4, palette, palette_size - 4);
102
    }
103

    
104
    pkt->data[palette_size + 0] = sub_type;
105
    pkt->data[palette_size + 1] = type;
106
    pkt->data[palette_size + 2] = size & 0xFF;
107
    pkt->data[palette_size + 3] = (size >> 8) & 0xFF;
108
    ret = get_buffer(&s->pb, pkt->data + palette_size + 4, size - 4) + 4;
109
    if (ret < size) {
110
        av_free_packet(pkt);
111
        return AVERROR_IO;
112
    }
113

    
114
    pkt->size = ret + palette_size;
115
    pkt->stream_index = avs->st_video->index;
116
    if (sub_type == 0)
117
        pkt->flags |= PKT_FLAG_KEY;
118

    
119
    return 0;
120
}
121

    
122
static int avs_read_audio_packet(AVFormatContext * s, AVPacket * pkt)
123
{
124
    avs_format_t *avs = s->priv_data;
125
    int ret, size;
126

    
127
    size = url_ftell(&s->pb);
128
    ret = voc_get_packet(s, pkt, avs->st_audio, avs->remaining_audio_size);
129
    size = url_ftell(&s->pb) - size;
130
    avs->remaining_audio_size -= size;
131

    
132
    if (ret == AVERROR_IO)
133
        return 0;    /* this indicate EOS */
134
    if (ret < 0)
135
        return ret;
136

    
137
    pkt->stream_index = avs->st_audio->index;
138
    pkt->flags |= PKT_FLAG_KEY;
139

    
140
    return size;
141
}
142

    
143
static int avs_read_packet(AVFormatContext * s, AVPacket * pkt)
144
{
145
    avs_format_t *avs = s->priv_data;
146
    int sub_type = 0, size = 0;
147
    avs_block_type_t type = 0;
148
    int palette_size = 0;
149
    uint8_t palette[4 + 3 * 256];
150
    int ret;
151

    
152
    if (avs->remaining_audio_size > 0)
153
        if (avs_read_audio_packet(s, pkt) > 0)
154
            return 0;
155

    
156
    while (1) {
157
        if (avs->remaining_frame_size <= 0) {
158
            if (!get_le16(&s->pb))    /* found EOF */
159
                return AVERROR_IO;
160
            avs->remaining_frame_size = get_le16(&s->pb) - 4;
161
        }
162

    
163
        while (avs->remaining_frame_size > 0) {
164
            sub_type = get_byte(&s->pb);
165
            type = get_byte(&s->pb);
166
            size = get_le16(&s->pb);
167
            avs->remaining_frame_size -= size;
168

    
169
            switch (type) {
170
            case AVS_PALETTE:
171
                ret = get_buffer(&s->pb, palette, size - 4);
172
                if (ret < size - 4)
173
                    return AVERROR_IO;
174
                palette_size = size;
175
                break;
176

    
177
            case AVS_VIDEO:
178
                if (!avs->st_video) {
179
                    avs->st_video = av_new_stream(s, AVS_VIDEO);
180
                    if (avs->st_video == NULL)
181
                        return AVERROR_NOMEM;
182
                    avs->st_video->codec->codec_type = CODEC_TYPE_VIDEO;
183
                    avs->st_video->codec->codec_id = CODEC_ID_AVS;
184
                    avs->st_video->codec->width = avs->width;
185
                    avs->st_video->codec->height = avs->height;
186
                    avs->st_video->codec->bits_per_sample=avs->bits_per_sample;
187
                    avs->st_video->nb_frames = avs->nb_frames;
188
                    avs->st_video->codec->time_base = (AVRational) {
189
                    1, avs->fps};
190
                }
191
                return avs_read_video_packet(s, pkt, type, sub_type, size,
192
                                             palette, palette_size);
193

    
194
            case AVS_AUDIO:
195
                if (!avs->st_audio) {
196
                    avs->st_audio = av_new_stream(s, AVS_AUDIO);
197
                    if (avs->st_audio == NULL)
198
                        return AVERROR_NOMEM;
199
                    avs->st_audio->codec->codec_type = CODEC_TYPE_AUDIO;
200
                }
201
                avs->remaining_audio_size = size - 4;
202
                size = avs_read_audio_packet(s, pkt);
203
                if (size != 0)
204
                    return size;
205
                break;
206

    
207
            default:
208
                url_fskip(&s->pb, size - 4);
209
            }
210
        }
211
    }
212
}
213

    
214
static int avs_read_close(AVFormatContext * s)
215
{
216
    return 0;
217
}
218

    
219
AVInputFormat avs_demuxer = {
220
    "avs",
221
    "avs format",
222
    sizeof(avs_format_t),
223
    avs_probe,
224
    avs_read_header,
225
    avs_read_packet,
226
    avs_read_close,
227
};