Statistics
| Branch: | Revision:

ffmpeg / libavformat / avs.c @ e356fc57

History | View | Annotate | Download (6.5 KB)

1 26376701 Mike Melanson
/*
2
 * AVS demuxer.
3
 * Copyright (c) 2006  Aurelien Jacobs <aurel@gnuage.org>
4
 *
5 b78e7197 Diego Biurrun
 * This file is part of FFmpeg.
6
 *
7
 * FFmpeg is free software; you can redistribute it and/or
8 26376701 Mike Melanson
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10 b78e7197 Diego Biurrun
 * version 2.1 of the License, or (at your option) any later version.
11 26376701 Mike Melanson
 *
12 b78e7197 Diego Biurrun
 * FFmpeg is distributed in the hope that it will be useful,
13 26376701 Mike Melanson
 * 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 b78e7197 Diego Biurrun
 * License along with FFmpeg; if not, write to the Free Software
19 e5a389a1 Diego Biurrun
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 26376701 Mike Melanson
 */
21
22
#include "avformat.h"
23
#include "voc.h"
24
25
26
typedef struct avs_format {
27 e998ba4f Aurelien Jacobs
    VocDecContext voc;
28 26376701 Mike Melanson
    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 e998ba4f Aurelien Jacobs
} AvsFormat;
38 26376701 Mike Melanson
39
typedef enum avs_block_type {
40 696c3068 Carl Eugen Hoyos
    AVS_NONE      = 0x00,
41 26376701 Mike Melanson
    AVS_VIDEO     = 0x01,
42
    AVS_AUDIO     = 0x02,
43
    AVS_PALETTE   = 0x03,
44
    AVS_GAME_DATA = 0x04,
45 e998ba4f Aurelien Jacobs
} AvsBlockType;
46 26376701 Mike Melanson
47
static int avs_probe(AVProbeData * p)
48
{
49
    const uint8_t *d;
50
51
    d = p->buf;
52
    if (d[0] == 'w' && d[1] == 'W' && d[2] == 0x10 && d[3] == 0)
53
        return 50;
54
55
    return 0;
56
}
57
58
static int avs_read_header(AVFormatContext * s, AVFormatParameters * ap)
59
{
60 e998ba4f Aurelien Jacobs
    AvsFormat *avs = s->priv_data;
61 26376701 Mike Melanson
62
    s->ctx_flags |= AVFMTCTX_NOHEADER;
63
64 e356fc57 Anton Khirnov
    avio_seek(s->pb, 4, SEEK_CUR);
65 b7effd4e Anton Khirnov
    avs->width = avio_rl16(s->pb);
66
    avs->height = avio_rl16(s->pb);
67
    avs->bits_per_sample = avio_rl16(s->pb);
68
    avs->fps = avio_rl16(s->pb);
69
    avs->nb_frames = avio_rl32(s->pb);
70 26376701 Mike Melanson
    avs->remaining_frame_size = 0;
71
    avs->remaining_audio_size = 0;
72
73
    avs->st_video = avs->st_audio = NULL;
74
75
    if (avs->width != 318 || avs->height != 198)
76
        av_log(s, AV_LOG_ERROR, "This avs pretend to be %dx%d "
77
               "when the avs format is supposed to be 318x198 only.\n",
78
               avs->width, avs->height);
79
80
    return 0;
81
}
82
83
static int
84
avs_read_video_packet(AVFormatContext * s, AVPacket * pkt,
85 e998ba4f Aurelien Jacobs
                      AvsBlockType type, int sub_type, int size,
86 26376701 Mike Melanson
                      uint8_t * palette, int palette_size)
87
{
88 e998ba4f Aurelien Jacobs
    AvsFormat *avs = s->priv_data;
89 26376701 Mike Melanson
    int ret;
90
91
    ret = av_new_packet(pkt, size + palette_size);
92
    if (ret < 0)
93
        return ret;
94
95
    if (palette_size) {
96
        pkt->data[0] = 0x00;
97
        pkt->data[1] = 0x03;
98
        pkt->data[2] = palette_size & 0xFF;
99
        pkt->data[3] = (palette_size >> 8) & 0xFF;
100
        memcpy(pkt->data + 4, palette, palette_size - 4);
101
    }
102
103
    pkt->data[palette_size + 0] = sub_type;
104
    pkt->data[palette_size + 1] = type;
105
    pkt->data[palette_size + 2] = size & 0xFF;
106
    pkt->data[palette_size + 3] = (size >> 8) & 0xFF;
107 b7effd4e Anton Khirnov
    ret = avio_read(s->pb, pkt->data + palette_size + 4, size - 4) + 4;
108 26376701 Mike Melanson
    if (ret < size) {
109
        av_free_packet(pkt);
110 6f3e0b21 Panagiotis Issaris
        return AVERROR(EIO);
111 26376701 Mike Melanson
    }
112
113
    pkt->size = ret + palette_size;
114
    pkt->stream_index = avs->st_video->index;
115
    if (sub_type == 0)
116 cc947f04 Jean-Daniel Dupas
        pkt->flags |= AV_PKT_FLAG_KEY;
117 26376701 Mike Melanson
118
    return 0;
119
}
120
121
static int avs_read_audio_packet(AVFormatContext * s, AVPacket * pkt)
122
{
123 e998ba4f Aurelien Jacobs
    AvsFormat *avs = s->priv_data;
124 26376701 Mike Melanson
    int ret, size;
125
126 899681cd Björn Axelsson
    size = url_ftell(s->pb);
127 26376701 Mike Melanson
    ret = voc_get_packet(s, pkt, avs->st_audio, avs->remaining_audio_size);
128 899681cd Björn Axelsson
    size = url_ftell(s->pb) - size;
129 26376701 Mike Melanson
    avs->remaining_audio_size -= size;
130
131 6f3e0b21 Panagiotis Issaris
    if (ret == AVERROR(EIO))
132 26376701 Mike Melanson
        return 0;    /* this indicate EOS */
133
    if (ret < 0)
134
        return ret;
135
136
    pkt->stream_index = avs->st_audio->index;
137 cc947f04 Jean-Daniel Dupas
    pkt->flags |= AV_PKT_FLAG_KEY;
138 26376701 Mike Melanson
139
    return size;
140
}
141
142
static int avs_read_packet(AVFormatContext * s, AVPacket * pkt)
143
{
144 e998ba4f Aurelien Jacobs
    AvsFormat *avs = s->priv_data;
145 26376701 Mike Melanson
    int sub_type = 0, size = 0;
146 e998ba4f Aurelien Jacobs
    AvsBlockType type = AVS_NONE;
147 26376701 Mike Melanson
    int palette_size = 0;
148
    uint8_t palette[4 + 3 * 256];
149
    int ret;
150
151
    if (avs->remaining_audio_size > 0)
152
        if (avs_read_audio_packet(s, pkt) > 0)
153
            return 0;
154
155
    while (1) {
156
        if (avs->remaining_frame_size <= 0) {
157 b7effd4e Anton Khirnov
            if (!avio_rl16(s->pb))    /* found EOF */
158 6f3e0b21 Panagiotis Issaris
                return AVERROR(EIO);
159 b7effd4e Anton Khirnov
            avs->remaining_frame_size = avio_rl16(s->pb) - 4;
160 26376701 Mike Melanson
        }
161
162
        while (avs->remaining_frame_size > 0) {
163 b7effd4e Anton Khirnov
            sub_type = avio_r8(s->pb);
164
            type = avio_r8(s->pb);
165
            size = avio_rl16(s->pb);
166 26376701 Mike Melanson
            avs->remaining_frame_size -= size;
167
168
            switch (type) {
169
            case AVS_PALETTE:
170 b7effd4e Anton Khirnov
                ret = avio_read(s->pb, palette, size - 4);
171 26376701 Mike Melanson
                if (ret < size - 4)
172 6f3e0b21 Panagiotis Issaris
                    return AVERROR(EIO);
173 26376701 Mike Melanson
                palette_size = size;
174
                break;
175
176
            case AVS_VIDEO:
177
                if (!avs->st_video) {
178
                    avs->st_video = av_new_stream(s, AVS_VIDEO);
179
                    if (avs->st_video == NULL)
180 769e10f0 Panagiotis Issaris
                        return AVERROR(ENOMEM);
181 72415b2a Stefano Sabatini
                    avs->st_video->codec->codec_type = AVMEDIA_TYPE_VIDEO;
182 26376701 Mike Melanson
                    avs->st_video->codec->codec_id = CODEC_ID_AVS;
183
                    avs->st_video->codec->width = avs->width;
184
                    avs->st_video->codec->height = avs->height;
185 dd1c8f3e Luca Abeni
                    avs->st_video->codec->bits_per_coded_sample=avs->bits_per_sample;
186 26376701 Mike Melanson
                    avs->st_video->nb_frames = avs->nb_frames;
187
                    avs->st_video->codec->time_base = (AVRational) {
188
                    1, avs->fps};
189
                }
190
                return avs_read_video_packet(s, pkt, type, sub_type, size,
191
                                             palette, palette_size);
192
193
            case AVS_AUDIO:
194
                if (!avs->st_audio) {
195
                    avs->st_audio = av_new_stream(s, AVS_AUDIO);
196
                    if (avs->st_audio == NULL)
197 769e10f0 Panagiotis Issaris
                        return AVERROR(ENOMEM);
198 72415b2a Stefano Sabatini
                    avs->st_audio->codec->codec_type = AVMEDIA_TYPE_AUDIO;
199 26376701 Mike Melanson
                }
200
                avs->remaining_audio_size = size - 4;
201
                size = avs_read_audio_packet(s, pkt);
202
                if (size != 0)
203
                    return size;
204
                break;
205
206
            default:
207 e356fc57 Anton Khirnov
                avio_seek(s->pb, size - 4, SEEK_CUR);
208 26376701 Mike Melanson
            }
209
        }
210
    }
211
}
212
213
static int avs_read_close(AVFormatContext * s)
214
{
215
    return 0;
216
}
217
218 c6610a21 Diego Elio Pettenò
AVInputFormat ff_avs_demuxer = {
219 26376701 Mike Melanson
    "avs",
220 bde15e74 Stefano Sabatini
    NULL_IF_CONFIG_SMALL("AVS format"),
221 e998ba4f Aurelien Jacobs
    sizeof(AvsFormat),
222 26376701 Mike Melanson
    avs_probe,
223
    avs_read_header,
224
    avs_read_packet,
225
    avs_read_close,
226
};