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