Libav 0.7.1
|
00001 /* 00002 * Delphine Software International CIN File Demuxer 00003 * Copyright (c) 2006 Gregory Montoir (cyx@users.sourceforge.net) 00004 * 00005 * This file is part of Libav. 00006 * 00007 * Libav is free software; you can redistribute it and/or 00008 * modify it under the terms of the GNU Lesser General Public 00009 * License as published by the Free Software Foundation; either 00010 * version 2.1 of the License, or (at your option) any later version. 00011 * 00012 * Libav is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 * Lesser General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU Lesser General Public 00018 * License along with Libav; if not, write to the Free Software 00019 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 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 /* header starts with this special marker */ 00063 if (AV_RL32(&p->buf[0]) != 0x55AA0000) 00064 return 0; 00065 00066 /* for accuracy, check some header field values */ 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, AVIOContext *pb) { 00074 CinFileHeader *hdr = &cin->file_header; 00075 00076 if (avio_rl32(pb) != 0x55AA0000) 00077 return AVERROR_INVALIDDATA; 00078 00079 hdr->video_frame_size = avio_rl32(pb); 00080 hdr->video_frame_width = avio_rl16(pb); 00081 hdr->video_frame_height = avio_rl16(pb); 00082 hdr->audio_frequency = avio_rl32(pb); 00083 hdr->audio_bits = avio_r8(pb); 00084 hdr->audio_stereo = avio_r8(pb); 00085 hdr->audio_frame_size = avio_rl16(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 AVIOContext *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 /* initialize the video decoder stream */ 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 = AVMEDIA_TYPE_VIDEO; 00117 st->codec->codec_id = CODEC_ID_DSICINVIDEO; 00118 st->codec->codec_tag = 0; /* no fourcc */ 00119 st->codec->width = hdr->video_frame_width; 00120 st->codec->height = hdr->video_frame_height; 00121 00122 /* initialize the audio decoder stream */ 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 = AVMEDIA_TYPE_AUDIO; 00130 st->codec->codec_id = CODEC_ID_DSICINAUDIO; 00131 st->codec->codec_tag = 0; /* no tag */ 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, 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 00156 return 0; 00157 } 00158 00159 static int cin_read_packet(AVFormatContext *s, AVPacket *pkt) 00160 { 00161 CinDemuxContext *cin = s->priv_data; 00162 AVIOContext *pb = s->pb; 00163 CinFrameHeader *hdr = &cin->frame_header; 00164 int rc, palette_type, pkt_size; 00165 int ret; 00166 00167 if (cin->audio_buffer_size == 0) { 00168 rc = cin_read_frame_header(cin, pb); 00169 if (rc) 00170 return rc; 00171 00172 if ((int16_t)hdr->pal_colors_count < 0) { 00173 hdr->pal_colors_count = -(int16_t)hdr->pal_colors_count; 00174 palette_type = 1; 00175 } else { 00176 palette_type = 0; 00177 } 00178 00179 /* palette and video packet */ 00180 pkt_size = (palette_type + 3) * hdr->pal_colors_count + hdr->video_frame_size; 00181 00182 ret = av_new_packet(pkt, 4 + pkt_size); 00183 if (ret < 0) 00184 return ret; 00185 00186 pkt->stream_index = cin->video_stream_index; 00187 pkt->pts = cin->video_stream_pts++; 00188 00189 pkt->data[0] = palette_type; 00190 pkt->data[1] = hdr->pal_colors_count & 0xFF; 00191 pkt->data[2] = hdr->pal_colors_count >> 8; 00192 pkt->data[3] = hdr->video_frame_type; 00193 00194 ret = avio_read(pb, &pkt->data[4], pkt_size); 00195 if (ret < 0) { 00196 av_free_packet(pkt); 00197 return ret; 00198 } 00199 if (ret < pkt_size) 00200 av_shrink_packet(pkt, 4 + ret); 00201 00202 /* sound buffer will be processed on next read_packet() call */ 00203 cin->audio_buffer_size = hdr->audio_frame_size; 00204 return 0; 00205 } 00206 00207 /* audio packet */ 00208 ret = av_get_packet(pb, pkt, cin->audio_buffer_size); 00209 if (ret < 0) 00210 return ret; 00211 00212 pkt->stream_index = cin->audio_stream_index; 00213 pkt->pts = cin->audio_stream_pts; 00214 cin->audio_stream_pts += cin->audio_buffer_size * 2 / cin->file_header.audio_frame_size; 00215 cin->audio_buffer_size = 0; 00216 return 0; 00217 } 00218 00219 AVInputFormat ff_dsicin_demuxer = { 00220 "dsicin", 00221 NULL_IF_CONFIG_SMALL("Delphine Software International CIN format"), 00222 sizeof(CinDemuxContext), 00223 cin_probe, 00224 cin_read_header, 00225 cin_read_packet, 00226 };