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