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