00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00027 #include "avformat.h"
00028
00029
00030 typedef struct CinFileHeader {
00031 int video_frame_size;
00032 int video_frame_width;
00033 int video_frame_height;
00034 int audio_frequency;
00035 int audio_bits;
00036 int audio_stereo;
00037 int audio_frame_size;
00038 } CinFileHeader;
00039
00040 typedef struct CinFrameHeader {
00041 int audio_frame_type;
00042 int video_frame_type;
00043 int pal_colors_count;
00044 int audio_frame_size;
00045 int video_frame_size;
00046 } CinFrameHeader;
00047
00048 typedef struct CinDemuxContext {
00049 int audio_stream_index;
00050 int video_stream_index;
00051 CinFileHeader file_header;
00052 int64_t audio_stream_pts;
00053 int64_t video_stream_pts;
00054 CinFrameHeader frame_header;
00055 int audio_buffer_size;
00056 } CinDemuxContext;
00057
00058
00059 static int cin_probe(AVProbeData *p)
00060 {
00061
00062 if (AV_RL32(&p->buf[0]) != 0x55AA0000)
00063 return 0;
00064
00065
00066 if (AV_RL32(&p->buf[12]) != 22050 || p->buf[16] != 16 || p->buf[17] != 0)
00067 return 0;
00068
00069 return AVPROBE_SCORE_MAX;
00070 }
00071
00072 static int cin_read_file_header(CinDemuxContext *cin, ByteIOContext *pb) {
00073 CinFileHeader *hdr = &cin->file_header;
00074
00075 if (get_le32(pb) != 0x55AA0000)
00076 return AVERROR_INVALIDDATA;
00077
00078 hdr->video_frame_size = get_le32(pb);
00079 hdr->video_frame_width = get_le16(pb);
00080 hdr->video_frame_height = get_le16(pb);
00081 hdr->audio_frequency = get_le32(pb);
00082 hdr->audio_bits = get_byte(pb);
00083 hdr->audio_stereo = get_byte(pb);
00084 hdr->audio_frame_size = get_le16(pb);
00085
00086 if (hdr->audio_frequency != 22050 || hdr->audio_bits != 16 || hdr->audio_stereo != 0)
00087 return AVERROR_INVALIDDATA;
00088
00089 return 0;
00090 }
00091
00092 static int cin_read_header(AVFormatContext *s, AVFormatParameters *ap)
00093 {
00094 int rc;
00095 CinDemuxContext *cin = s->priv_data;
00096 CinFileHeader *hdr = &cin->file_header;
00097 ByteIOContext *pb = s->pb;
00098 AVStream *st;
00099
00100 rc = cin_read_file_header(cin, pb);
00101 if (rc)
00102 return rc;
00103
00104 cin->video_stream_pts = 0;
00105 cin->audio_stream_pts = 0;
00106 cin->audio_buffer_size = 0;
00107
00108
00109 st = av_new_stream(s, 0);
00110 if (!st)
00111 return AVERROR(ENOMEM);
00112
00113 av_set_pts_info(st, 32, 1, 12);
00114 cin->video_stream_index = st->index;
00115 st->codec->codec_type = CODEC_TYPE_VIDEO;
00116 st->codec->codec_id = CODEC_ID_DSICINVIDEO;
00117 st->codec->codec_tag = 0;
00118 st->codec->width = hdr->video_frame_width;
00119 st->codec->height = hdr->video_frame_height;
00120
00121
00122 st = av_new_stream(s, 0);
00123 if (!st)
00124 return AVERROR(ENOMEM);
00125
00126 av_set_pts_info(st, 32, 1, 22050);
00127 cin->audio_stream_index = st->index;
00128 st->codec->codec_type = CODEC_TYPE_AUDIO;
00129 st->codec->codec_id = CODEC_ID_DSICINAUDIO;
00130 st->codec->codec_tag = 0;
00131 st->codec->channels = 1;
00132 st->codec->sample_rate = 22050;
00133 st->codec->bits_per_sample = 16;
00134 st->codec->bit_rate = st->codec->sample_rate * st->codec->bits_per_sample * st->codec->channels;
00135 st->codec->block_align = st->codec->channels * st->codec->bits_per_sample;
00136
00137 return 0;
00138 }
00139
00140 static int cin_read_frame_header(CinDemuxContext *cin, ByteIOContext *pb) {
00141 CinFrameHeader *hdr = &cin->frame_header;
00142
00143 hdr->video_frame_type = get_byte(pb);
00144 hdr->audio_frame_type = get_byte(pb);
00145 hdr->pal_colors_count = get_le16(pb);
00146 hdr->video_frame_size = get_le32(pb);
00147 hdr->audio_frame_size = get_le32(pb);
00148
00149 if (url_feof(pb) || url_ferror(pb))
00150 return AVERROR(EIO);
00151
00152 if (get_le32(pb) != 0xAA55AA55)
00153 return AVERROR_INVALIDDATA;
00154
00155 return 0;
00156 }
00157
00158 static int cin_read_packet(AVFormatContext *s, AVPacket *pkt)
00159 {
00160 CinDemuxContext *cin = s->priv_data;
00161 ByteIOContext *pb = s->pb;
00162 CinFrameHeader *hdr = &cin->frame_header;
00163 int rc, palette_type, pkt_size;
00164
00165 if (cin->audio_buffer_size == 0) {
00166 rc = cin_read_frame_header(cin, pb);
00167 if (rc)
00168 return rc;
00169
00170 if ((int16_t)hdr->pal_colors_count < 0) {
00171 hdr->pal_colors_count = -(int16_t)hdr->pal_colors_count;
00172 palette_type = 1;
00173 } else {
00174 palette_type = 0;
00175 }
00176
00177
00178 pkt_size = (palette_type + 3) * hdr->pal_colors_count + hdr->video_frame_size;
00179
00180 if (av_new_packet(pkt, 4 + pkt_size))
00181 return AVERROR(ENOMEM);
00182
00183 pkt->stream_index = cin->video_stream_index;
00184 pkt->pts = cin->video_stream_pts++;
00185
00186 pkt->data[0] = palette_type;
00187 pkt->data[1] = hdr->pal_colors_count & 0xFF;
00188 pkt->data[2] = hdr->pal_colors_count >> 8;
00189 pkt->data[3] = hdr->video_frame_type;
00190
00191 if (get_buffer(pb, &pkt->data[4], pkt_size) != pkt_size)
00192 return AVERROR(EIO);
00193
00194
00195 cin->audio_buffer_size = hdr->audio_frame_size;
00196 return 0;
00197 }
00198
00199
00200 if (av_new_packet(pkt, cin->audio_buffer_size))
00201 return AVERROR(ENOMEM);
00202
00203 pkt->stream_index = cin->audio_stream_index;
00204 pkt->pts = cin->audio_stream_pts;
00205 cin->audio_stream_pts += cin->audio_buffer_size * 2 / cin->file_header.audio_frame_size;
00206
00207 if (get_buffer(pb, pkt->data, cin->audio_buffer_size) != cin->audio_buffer_size)
00208 return AVERROR(EIO);
00209
00210 cin->audio_buffer_size = 0;
00211 return 0;
00212 }
00213
00214 AVInputFormat dsicin_demuxer = {
00215 "dsicin",
00216 "Delphine Software International CIN format",
00217 sizeof(CinDemuxContext),
00218 cin_probe,
00219 cin_read_header,
00220 cin_read_packet,
00221 };