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