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