Statistics
| Branch: | Revision:

ffmpeg / libavformat / dsicin.c @ 80a289b9

History | View | Annotate | Download (6.45 KB)

1 72450e50 Baptiste Coudurier
/*
2
 * Delphine Software International CIN File Demuxer
3
 * Copyright (c) 2006 Gregory Montoir (cyx@users.sourceforge.net)
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
 * @file dsicin.c
24
 * Delphine Software International CIN file demuxer
25
 */
26
27
#include "avformat.h"
28
29
30
typedef struct CinFileHeader {
31
    int video_frame_size;
32
    int video_frame_width;
33
    int video_frame_height;
34
    int audio_frequency;
35
    int audio_bits;
36
    int audio_stereo;
37
    int audio_frame_size;
38
} CinFileHeader;
39
40
typedef struct CinFrameHeader {
41
    int audio_frame_type;
42
    int video_frame_type;
43
    int pal_colors_count;
44
    int audio_frame_size;
45
    int video_frame_size;
46
} CinFrameHeader;
47
48
typedef struct CinDemuxContext {
49
    int audio_stream_index;
50
    int video_stream_index;
51
    CinFileHeader file_header;
52
    int64_t audio_stream_pts;
53
    int64_t video_stream_pts;
54
    CinFrameHeader frame_header;
55
    int audio_buffer_size;
56
} CinDemuxContext;
57
58
59
static int cin_probe(AVProbeData *p)
60
{
61
    if (p->buf_size < 18)
62
        return 0;
63
64
    /* header starts with this special marker */
65 fead30d4 Alex Beregszaszi
    if (AV_RL32(&p->buf[0]) != 0x55AA0000)
66 72450e50 Baptiste Coudurier
        return 0;
67
68
    /* for accuracy, check some header field values */
69 fead30d4 Alex Beregszaszi
    if (AV_RL32(&p->buf[12]) != 22050 || p->buf[16] != 16 || p->buf[17] != 0)
70 72450e50 Baptiste Coudurier
        return 0;
71
72
    return AVPROBE_SCORE_MAX;
73
}
74
75
static int cin_read_file_header(CinDemuxContext *cin, ByteIOContext *pb) {
76
    CinFileHeader *hdr = &cin->file_header;
77
78
    if (get_le32(pb) != 0x55AA0000)
79
        return AVERROR_INVALIDDATA;
80
81
    hdr->video_frame_size   = get_le32(pb);
82
    hdr->video_frame_width  = get_le16(pb);
83
    hdr->video_frame_height = get_le16(pb);
84
    hdr->audio_frequency    = get_le32(pb);
85
    hdr->audio_bits         = get_byte(pb);
86
    hdr->audio_stereo       = get_byte(pb);
87
    hdr->audio_frame_size   = get_le16(pb);
88
89
    if (hdr->audio_frequency != 22050 || hdr->audio_bits != 16 || hdr->audio_stereo != 0)
90
        return AVERROR_INVALIDDATA;
91
92
    return 0;
93
}
94
95
static int cin_read_header(AVFormatContext *s, AVFormatParameters *ap)
96
{
97
    int rc;
98
    CinDemuxContext *cin = (CinDemuxContext *)s->priv_data;
99
    CinFileHeader *hdr = &cin->file_header;
100
    ByteIOContext *pb = &s->pb;
101
    AVStream *st;
102
103
    rc = cin_read_file_header(cin, pb);
104
    if (rc)
105
        return rc;
106
107
    cin->video_stream_pts = 0;
108
    cin->audio_stream_pts = 0;
109
    cin->audio_buffer_size = 0;
110
111
    /* initialize the video decoder stream */
112
    st = av_new_stream(s, 0);
113
    if (!st)
114
        return AVERROR_NOMEM;
115
116
    av_set_pts_info(st, 32, 1, 12);
117
    cin->video_stream_index = st->index;
118
    st->codec->codec_type = CODEC_TYPE_VIDEO;
119
    st->codec->codec_id = CODEC_ID_DSICINVIDEO;
120
    st->codec->codec_tag = 0;  /* no fourcc */
121
    st->codec->width = hdr->video_frame_width;
122
    st->codec->height = hdr->video_frame_height;
123
124
    /* initialize the audio decoder stream */
125
    st = av_new_stream(s, 0);
126
    if (!st)
127
        return AVERROR_NOMEM;
128
129
    av_set_pts_info(st, 32, 1, 22050);
130
    cin->audio_stream_index = st->index;
131
    st->codec->codec_type = CODEC_TYPE_AUDIO;
132
    st->codec->codec_id = CODEC_ID_DSICINAUDIO;
133
    st->codec->codec_tag = 0;  /* no tag */
134
    st->codec->channels = 1;
135
    st->codec->sample_rate = 22050;
136
    st->codec->bits_per_sample = 16;
137
    st->codec->bit_rate = st->codec->sample_rate * st->codec->bits_per_sample * st->codec->channels;
138
    st->codec->block_align = st->codec->channels * st->codec->bits_per_sample;
139
140
    return 0;
141
}
142
143
static int cin_read_frame_header(CinDemuxContext *cin, ByteIOContext *pb) {
144
    CinFrameHeader *hdr = &cin->frame_header;
145
146
    hdr->video_frame_type = get_byte(pb);
147
    hdr->audio_frame_type = get_byte(pb);
148
    hdr->pal_colors_count = get_le16(pb);
149
    hdr->video_frame_size = get_le32(pb);
150
    hdr->audio_frame_size = get_le32(pb);
151
152
    if (url_feof(pb) || url_ferror(pb))
153
        return AVERROR_IO;
154
155
    if (get_le32(pb) != 0xAA55AA55)
156
        return AVERROR_INVALIDDATA;
157
158
    return 0;
159
}
160
161
static int cin_read_packet(AVFormatContext *s, AVPacket *pkt)
162
{
163
    CinDemuxContext *cin = (CinDemuxContext *)s->priv_data;
164
    ByteIOContext *pb = &s->pb;
165
    CinFrameHeader *hdr = &cin->frame_header;
166
    int rc, palette_type, pkt_size;
167
168
    if (cin->audio_buffer_size == 0) {
169
        rc = cin_read_frame_header(cin, pb);
170
        if (rc)
171
            return rc;
172
173
        if ((int16_t)hdr->pal_colors_count < 0) {
174
            hdr->pal_colors_count = -(int16_t)hdr->pal_colors_count;
175
            palette_type = 1;
176
        } else {
177
            palette_type = 0;
178
        }
179
180
        /* palette and video packet */
181
        pkt_size = (palette_type + 3) * hdr->pal_colors_count + hdr->video_frame_size;
182
183
        if (av_new_packet(pkt, 4 + pkt_size))
184
            return AVERROR_NOMEM;
185
186
        pkt->stream_index = cin->video_stream_index;
187
        pkt->pts = cin->video_stream_pts++;
188
189
        pkt->data[0] = palette_type;
190
        pkt->data[1] = hdr->pal_colors_count & 0xFF;
191
        pkt->data[2] = hdr->pal_colors_count >> 8;
192
        pkt->data[3] = hdr->video_frame_type;
193
194
        if (get_buffer(pb, &pkt->data[4], pkt_size) != pkt_size)
195
            return AVERROR_IO;
196
197
        /* sound buffer will be processed on next read_packet() call */
198
        cin->audio_buffer_size = hdr->audio_frame_size;
199
        return 0;
200
    }
201
202
    /* audio packet */
203
    if (av_new_packet(pkt, cin->audio_buffer_size))
204
        return AVERROR_NOMEM;
205
206
    pkt->stream_index = cin->audio_stream_index;
207
    pkt->pts = cin->audio_stream_pts;
208
    cin->audio_stream_pts += cin->audio_buffer_size * 2 / cin->file_header.audio_frame_size;
209
210
    if (get_buffer(pb, pkt->data, cin->audio_buffer_size) != cin->audio_buffer_size)
211
        return AVERROR_IO;
212
213
    cin->audio_buffer_size = 0;
214
    return 0;
215
}
216
217
AVInputFormat dsicin_demuxer = {
218
    "dsicin",
219
    "Delphine Software International CIN format",
220
    sizeof(CinDemuxContext),
221
    cin_probe,
222
    cin_read_header,
223
    cin_read_packet,
224
};