]> git.sesse.net Git - ffmpeg/blob - libavformat/dsicin.c
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / libavformat / dsicin.c
1 /*
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
24  * Delphine Software International CIN file demuxer
25  */
26
27 #include "libavutil/intreadwrite.h"
28 #include "avformat.h"
29 #include "internal.h"
30 #include "avio_internal.h"
31
32
33 typedef struct CinFileHeader {
34     int video_frame_size;
35     int video_frame_width;
36     int video_frame_height;
37     int audio_frequency;
38     int audio_bits;
39     int audio_stereo;
40     int audio_frame_size;
41 } CinFileHeader;
42
43 typedef struct CinFrameHeader {
44     int audio_frame_type;
45     int video_frame_type;
46     int pal_colors_count;
47     int audio_frame_size;
48     int video_frame_size;
49 } CinFrameHeader;
50
51 typedef struct CinDemuxContext {
52     int audio_stream_index;
53     int video_stream_index;
54     CinFileHeader file_header;
55     int64_t audio_stream_pts;
56     int64_t video_stream_pts;
57     CinFrameHeader frame_header;
58     int audio_buffer_size;
59 } CinDemuxContext;
60
61
62 static int cin_probe(AVProbeData *p)
63 {
64     /* header starts with this special marker */
65     if (AV_RL32(&p->buf[0]) != 0x55AA0000)
66         return 0;
67
68     /* for accuracy, check some header field values */
69     if (AV_RL32(&p->buf[12]) != 22050 || p->buf[16] != 16 || p->buf[17] != 0)
70         return 0;
71
72     return AVPROBE_SCORE_MAX;
73 }
74
75 static int cin_read_file_header(CinDemuxContext *cin, AVIOContext *pb) {
76     CinFileHeader *hdr = &cin->file_header;
77
78     if (avio_rl32(pb) != 0x55AA0000)
79         return AVERROR_INVALIDDATA;
80
81     hdr->video_frame_size   = avio_rl32(pb);
82     hdr->video_frame_width  = avio_rl16(pb);
83     hdr->video_frame_height = avio_rl16(pb);
84     hdr->audio_frequency    = avio_rl32(pb);
85     hdr->audio_bits         = avio_r8(pb);
86     hdr->audio_stereo       = avio_r8(pb);
87     hdr->audio_frame_size   = avio_rl16(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)
96 {
97     int rc;
98     CinDemuxContext *cin = s->priv_data;
99     CinFileHeader *hdr = &cin->file_header;
100     AVIOContext *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 = avformat_new_stream(s, NULL);
113     if (!st)
114         return AVERROR(ENOMEM);
115
116     avpriv_set_pts_info(st, 32, 1, 12);
117     cin->video_stream_index = st->index;
118     st->codec->codec_type = AVMEDIA_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 = avformat_new_stream(s, NULL);
126     if (!st)
127         return AVERROR(ENOMEM);
128
129     avpriv_set_pts_info(st, 32, 1, 22050);
130     cin->audio_stream_index = st->index;
131     st->codec->codec_type = AVMEDIA_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_coded_sample = 8;
137     st->codec->bit_rate = st->codec->sample_rate * st->codec->bits_per_coded_sample * st->codec->channels;
138
139     return 0;
140 }
141
142 static int cin_read_frame_header(CinDemuxContext *cin, AVIOContext *pb) {
143     CinFrameHeader *hdr = &cin->frame_header;
144
145     hdr->video_frame_type = avio_r8(pb);
146     hdr->audio_frame_type = avio_r8(pb);
147     hdr->pal_colors_count = avio_rl16(pb);
148     hdr->video_frame_size = avio_rl32(pb);
149     hdr->audio_frame_size = avio_rl32(pb);
150
151     if (url_feof(pb) || pb->error)
152         return AVERROR(EIO);
153
154     if (avio_rl32(pb) != 0xAA55AA55)
155         return AVERROR_INVALIDDATA;
156
157     return 0;
158 }
159
160 static int cin_read_packet(AVFormatContext *s, AVPacket *pkt)
161 {
162     CinDemuxContext *cin = s->priv_data;
163     AVIOContext *pb = s->pb;
164     CinFrameHeader *hdr = &cin->frame_header;
165     int rc, palette_type, pkt_size;
166     int ret;
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         pkt_size = ffio_limit(pb, pkt_size);
184
185         ret = av_new_packet(pkt, 4 + pkt_size);
186         if (ret < 0)
187             return ret;
188
189         pkt->stream_index = cin->video_stream_index;
190         pkt->pts = cin->video_stream_pts++;
191
192         pkt->data[0] = palette_type;
193         pkt->data[1] = hdr->pal_colors_count & 0xFF;
194         pkt->data[2] = hdr->pal_colors_count >> 8;
195         pkt->data[3] = hdr->video_frame_type;
196
197         ret = avio_read(pb, &pkt->data[4], pkt_size);
198         if (ret < 0) {
199             av_free_packet(pkt);
200             return ret;
201         }
202         if (ret < pkt_size)
203             av_shrink_packet(pkt, 4 + ret);
204
205         /* sound buffer will be processed on next read_packet() call */
206         cin->audio_buffer_size = hdr->audio_frame_size;
207         return 0;
208     }
209
210     /* audio packet */
211     ret = av_get_packet(pb, pkt, cin->audio_buffer_size);
212     if (ret < 0)
213         return ret;
214
215     pkt->stream_index = cin->audio_stream_index;
216     pkt->pts = cin->audio_stream_pts;
217     pkt->duration = cin->audio_buffer_size - (pkt->pts == 0);
218     cin->audio_stream_pts += pkt->duration;
219     cin->audio_buffer_size = 0;
220     return 0;
221 }
222
223 AVInputFormat ff_dsicin_demuxer = {
224     .name           = "dsicin",
225     .long_name      = NULL_IF_CONFIG_SMALL("Delphine Software International CIN format"),
226     .priv_data_size = sizeof(CinDemuxContext),
227     .read_probe     = cin_probe,
228     .read_header    = cin_read_header,
229     .read_packet    = cin_read_packet,
230 };