00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00030 #include "libavutil/intreadwrite.h"
00031 #include "avformat.h"
00032
00033 #define FILM_TAG MKBETAG('F', 'I', 'L', 'M')
00034 #define FDSC_TAG MKBETAG('F', 'D', 'S', 'C')
00035 #define STAB_TAG MKBETAG('S', 'T', 'A', 'B')
00036 #define CVID_TAG MKBETAG('c', 'v', 'i', 'd')
00037
00038 typedef struct {
00039 int stream;
00040 int64_t sample_offset;
00041 unsigned int sample_size;
00042 int64_t pts;
00043 int keyframe;
00044 } film_sample;
00045
00046 typedef struct FilmDemuxContext {
00047 int video_stream_index;
00048 int audio_stream_index;
00049
00050 enum CodecID audio_type;
00051 unsigned int audio_samplerate;
00052 unsigned int audio_bits;
00053 unsigned int audio_channels;
00054
00055 enum CodecID video_type;
00056 unsigned int sample_count;
00057 film_sample *sample_table;
00058 unsigned int current_sample;
00059
00060 unsigned int base_clock;
00061 unsigned int version;
00062
00063
00064 unsigned char *stereo_buffer;
00065 int stereo_buffer_size;
00066 } FilmDemuxContext;
00067
00068 static int film_probe(AVProbeData *p)
00069 {
00070 if (AV_RB32(&p->buf[0]) != FILM_TAG)
00071 return 0;
00072
00073 return AVPROBE_SCORE_MAX;
00074 }
00075
00076 static int film_read_header(AVFormatContext *s,
00077 AVFormatParameters *ap)
00078 {
00079 FilmDemuxContext *film = s->priv_data;
00080 AVIOContext *pb = s->pb;
00081 AVStream *st;
00082 unsigned char scratch[256];
00083 int i;
00084 unsigned int data_offset;
00085 unsigned int audio_frame_counter;
00086
00087 film->sample_table = NULL;
00088 film->stereo_buffer = NULL;
00089 film->stereo_buffer_size = 0;
00090
00091
00092 if (avio_read(pb, scratch, 16) != 16)
00093 return AVERROR(EIO);
00094 data_offset = AV_RB32(&scratch[4]);
00095 film->version = AV_RB32(&scratch[8]);
00096
00097
00098 if (film->version == 0) {
00099
00100 if (avio_read(pb, scratch, 20) != 20)
00101 return AVERROR(EIO);
00102
00103 film->audio_type = CODEC_ID_PCM_S8;
00104 film->audio_samplerate = 22050;
00105 film->audio_channels = 1;
00106 film->audio_bits = 8;
00107 } else {
00108
00109 if (avio_read(pb, scratch, 32) != 32)
00110 return AVERROR(EIO);
00111 film->audio_samplerate = AV_RB16(&scratch[24]);
00112 film->audio_channels = scratch[21];
00113 film->audio_bits = scratch[22];
00114 if (scratch[23] == 2)
00115 film->audio_type = CODEC_ID_ADPCM_ADX;
00116 else if (film->audio_channels > 0) {
00117 if (film->audio_bits == 8)
00118 film->audio_type = CODEC_ID_PCM_S8;
00119 else if (film->audio_bits == 16)
00120 film->audio_type = CODEC_ID_PCM_S16BE;
00121 else
00122 film->audio_type = CODEC_ID_NONE;
00123 } else
00124 film->audio_type = CODEC_ID_NONE;
00125 }
00126
00127 if (AV_RB32(&scratch[0]) != FDSC_TAG)
00128 return AVERROR_INVALIDDATA;
00129
00130 if (AV_RB32(&scratch[8]) == CVID_TAG) {
00131 film->video_type = CODEC_ID_CINEPAK;
00132 } else
00133 film->video_type = CODEC_ID_NONE;
00134
00135
00136 if (film->video_type) {
00137 st = av_new_stream(s, 0);
00138 if (!st)
00139 return AVERROR(ENOMEM);
00140 film->video_stream_index = st->index;
00141 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
00142 st->codec->codec_id = film->video_type;
00143 st->codec->codec_tag = 0;
00144 st->codec->width = AV_RB32(&scratch[16]);
00145 st->codec->height = AV_RB32(&scratch[12]);
00146 }
00147
00148 if (film->audio_type) {
00149 st = av_new_stream(s, 0);
00150 if (!st)
00151 return AVERROR(ENOMEM);
00152 film->audio_stream_index = st->index;
00153 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
00154 st->codec->codec_id = film->audio_type;
00155 st->codec->codec_tag = 1;
00156 st->codec->channels = film->audio_channels;
00157 st->codec->bits_per_coded_sample = film->audio_bits;
00158 st->codec->sample_rate = film->audio_samplerate;
00159 st->codec->bit_rate = st->codec->channels * st->codec->sample_rate *
00160 st->codec->bits_per_coded_sample;
00161 st->codec->block_align = st->codec->channels *
00162 st->codec->bits_per_coded_sample / 8;
00163 }
00164
00165
00166 if (avio_read(pb, scratch, 16) != 16)
00167 return AVERROR(EIO);
00168 if (AV_RB32(&scratch[0]) != STAB_TAG)
00169 return AVERROR_INVALIDDATA;
00170 film->base_clock = AV_RB32(&scratch[8]);
00171 film->sample_count = AV_RB32(&scratch[12]);
00172 if(film->sample_count >= UINT_MAX / sizeof(film_sample))
00173 return -1;
00174 film->sample_table = av_malloc(film->sample_count * sizeof(film_sample));
00175 if (!film->sample_table)
00176 return AVERROR(ENOMEM);
00177
00178 for(i=0; i<s->nb_streams; i++)
00179 av_set_pts_info(s->streams[i], 33, 1, film->base_clock);
00180
00181 audio_frame_counter = 0;
00182 for (i = 0; i < film->sample_count; i++) {
00183
00184 if (avio_read(pb, scratch, 16) != 16) {
00185 av_free(film->sample_table);
00186 return AVERROR(EIO);
00187 }
00188 film->sample_table[i].sample_offset =
00189 data_offset + AV_RB32(&scratch[0]);
00190 film->sample_table[i].sample_size = AV_RB32(&scratch[4]);
00191 if (AV_RB32(&scratch[8]) == 0xFFFFFFFF) {
00192 film->sample_table[i].stream = film->audio_stream_index;
00193 film->sample_table[i].pts = audio_frame_counter;
00194 film->sample_table[i].pts *= film->base_clock;
00195 film->sample_table[i].pts /= film->audio_samplerate;
00196
00197 if (film->audio_type == CODEC_ID_ADPCM_ADX)
00198 audio_frame_counter += (film->sample_table[i].sample_size * 32 /
00199 (18 * film->audio_channels));
00200 else if (film->audio_type != CODEC_ID_NONE)
00201 audio_frame_counter += (film->sample_table[i].sample_size /
00202 (film->audio_channels * film->audio_bits / 8));
00203 } else {
00204 film->sample_table[i].stream = film->video_stream_index;
00205 film->sample_table[i].pts = AV_RB32(&scratch[8]) & 0x7FFFFFFF;
00206 film->sample_table[i].keyframe = (scratch[8] & 0x80) ? 0 : 1;
00207 }
00208 }
00209
00210 film->current_sample = 0;
00211
00212 return 0;
00213 }
00214
00215 static int film_read_packet(AVFormatContext *s,
00216 AVPacket *pkt)
00217 {
00218 FilmDemuxContext *film = s->priv_data;
00219 AVIOContext *pb = s->pb;
00220 film_sample *sample;
00221 int ret = 0;
00222 int i;
00223 int left, right;
00224
00225 if (film->current_sample >= film->sample_count)
00226 return AVERROR(EIO);
00227
00228 sample = &film->sample_table[film->current_sample];
00229
00230
00231 avio_seek(pb, sample->sample_offset, SEEK_SET);
00232
00233
00234 if ((sample->stream == film->video_stream_index) &&
00235 (film->video_type == CODEC_ID_CINEPAK)) {
00236 pkt->pos= avio_tell(pb);
00237 if (av_new_packet(pkt, sample->sample_size))
00238 return AVERROR(ENOMEM);
00239 avio_read(pb, pkt->data, sample->sample_size);
00240 } else if ((sample->stream == film->audio_stream_index) &&
00241 (film->audio_channels == 2)) {
00242
00243
00244 if (av_new_packet(pkt, sample->sample_size))
00245 return AVERROR(ENOMEM);
00246
00247
00248 if (sample->sample_size > film->stereo_buffer_size) {
00249 av_free(film->stereo_buffer);
00250 film->stereo_buffer_size = sample->sample_size;
00251 film->stereo_buffer = av_malloc(film->stereo_buffer_size);
00252 if (!film->stereo_buffer) {
00253 film->stereo_buffer_size = 0;
00254 return AVERROR(ENOMEM);
00255 }
00256 }
00257
00258 pkt->pos= avio_tell(pb);
00259 ret = avio_read(pb, film->stereo_buffer, sample->sample_size);
00260 if (ret != sample->sample_size)
00261 ret = AVERROR(EIO);
00262
00263 left = 0;
00264 right = sample->sample_size / 2;
00265 for (i = 0; i < sample->sample_size; ) {
00266 if (film->audio_bits == 8) {
00267 pkt->data[i++] = film->stereo_buffer[left++];
00268 pkt->data[i++] = film->stereo_buffer[right++];
00269 } else {
00270 pkt->data[i++] = film->stereo_buffer[left++];
00271 pkt->data[i++] = film->stereo_buffer[left++];
00272 pkt->data[i++] = film->stereo_buffer[right++];
00273 pkt->data[i++] = film->stereo_buffer[right++];
00274 }
00275 }
00276 } else {
00277 ret= av_get_packet(pb, pkt, sample->sample_size);
00278 if (ret != sample->sample_size)
00279 ret = AVERROR(EIO);
00280 }
00281
00282 pkt->stream_index = sample->stream;
00283 pkt->pts = sample->pts;
00284
00285 film->current_sample++;
00286
00287 return ret;
00288 }
00289
00290 static int film_read_close(AVFormatContext *s)
00291 {
00292 FilmDemuxContext *film = s->priv_data;
00293
00294 av_free(film->sample_table);
00295 av_free(film->stereo_buffer);
00296
00297 return 0;
00298 }
00299
00300 AVInputFormat ff_segafilm_demuxer = {
00301 "film_cpk",
00302 NULL_IF_CONFIG_SMALL("Sega FILM/CPK format"),
00303 sizeof(FilmDemuxContext),
00304 film_probe,
00305 film_read_header,
00306 film_read_packet,
00307 film_read_close,
00308 };