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