libavformat/segafilm.c
2fdf638b
 /*
  * Sega FILM Format (CPK) Demuxer
6dfa70f2
  * Copyright (c) 2003 The FFmpeg Project
2fdf638b
  *
b78e7197
  * This file is part of FFmpeg.
  *
  * FFmpeg is free software; you can redistribute it and/or
2fdf638b
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
b78e7197
  * version 2.1 of the License, or (at your option) any later version.
2fdf638b
  *
b78e7197
  * FFmpeg is distributed in the hope that it will be useful,
2fdf638b
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
b78e7197
  * License along with FFmpeg; if not, write to the Free Software
5509bffa
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
2fdf638b
  */
 
 /**
ba87f080
  * @file
2fdf638b
  * Sega FILM (.cpk) file demuxer
  * by Mike Melanson (melanson@pcisys.net)
  * For more information regarding the Sega FILM file format, visit:
  *   http://www.pcisys.net/~melanson/codecs/
  */
 
6a5d31ac
 #include "libavutil/intreadwrite.h"
2fdf638b
 #include "avformat.h"
c3f9ebf7
 #include "internal.h"
1795fed7
 #include "avio_internal.h"
2fdf638b
 
3a278992
 #define FILM_TAG MKBETAG('F', 'I', 'L', 'M')
 #define FDSC_TAG MKBETAG('F', 'D', 'S', 'C')
 #define STAB_TAG MKBETAG('S', 'T', 'A', 'B')
 #define CVID_TAG MKBETAG('c', 'v', 'i', 'd')
ddf9b510
 #define RAW_TAG  MKBETAG('r', 'a', 'w', ' ')
2fdf638b
 
 typedef struct {
   int stream;
bc5c918e
   int64_t sample_offset;
2fdf638b
   unsigned int sample_size;
   int64_t pts;
   int keyframe;
02fb2546
 } film_sample;
2fdf638b
 
 typedef struct FilmDemuxContext {
     int video_stream_index;
     int audio_stream_index;
 
36ef5369
     enum AVCodecID audio_type;
2fdf638b
     unsigned int audio_samplerate;
     unsigned int audio_bits;
     unsigned int audio_channels;
 
36ef5369
     enum AVCodecID video_type;
2fdf638b
     unsigned int sample_count;
02fb2546
     film_sample *sample_table;
2fdf638b
     unsigned int current_sample;
 
     unsigned int base_clock;
     unsigned int version;
 } FilmDemuxContext;
 
 static int film_probe(AVProbeData *p)
 {
fead30d4
     if (AV_RB32(&p->buf[0]) != FILM_TAG)
2fdf638b
         return 0;
 
3e6b7bbc
     if (AV_RB32(&p->buf[16]) != FDSC_TAG)
         return 0;
 
2fdf638b
     return AVPROBE_SCORE_MAX;
 }
 
6892d145
 static int film_read_close(AVFormatContext *s)
 {
     FilmDemuxContext *film = s->priv_data;
 
     av_freep(&film->sample_table);
 
     return 0;
 }
 
6e9651d1
 static int film_read_header(AVFormatContext *s)
2fdf638b
 {
e4141433
     FilmDemuxContext *film = s->priv_data;
471fe57e
     AVIOContext *pb = s->pb;
2fdf638b
     AVStream *st;
     unsigned char scratch[256];
6892d145
     int i, ret;
2fdf638b
     unsigned int data_offset;
     unsigned int audio_frame_counter;
 
     film->sample_table = NULL;
 
     /* load the main FILM header */
e63a3628
     if (avio_read(pb, scratch, 16) != 16)
6f3e0b21
         return AVERROR(EIO);
fead30d4
     data_offset = AV_RB32(&scratch[4]);
     film->version = AV_RB32(&scratch[8]);
2fdf638b
 
     /* load the FDSC chunk */
     if (film->version == 0) {
         /* special case for Lemmings .film files; 20-byte header */
e63a3628
         if (avio_read(pb, scratch, 20) != 20)
6f3e0b21
             return AVERROR(EIO);
2fdf638b
         /* make some assumptions about the audio parameters */
36ef5369
         film->audio_type = AV_CODEC_ID_PCM_S8;
2fdf638b
         film->audio_samplerate = 22050;
         film->audio_channels = 1;
         film->audio_bits = 8;
     } else {
         /* normal Saturn .cpk files; 32-byte header */
e63a3628
         if (avio_read(pb, scratch, 32) != 32)
6f3e0b21
             return AVERROR(EIO);
cea96420
         film->audio_samplerate = AV_RB16(&scratch[24]);
2fdf638b
         film->audio_channels = scratch[21];
         film->audio_bits = scratch[22];
192db16b
         if (scratch[23] == 2 && film->audio_channels > 0)
36ef5369
             film->audio_type = AV_CODEC_ID_ADPCM_ADX;
c58d45e0
         else if (film->audio_channels > 0) {
             if (film->audio_bits == 8)
42b8f5fb
                 film->audio_type = AV_CODEC_ID_PCM_S8_PLANAR;
c58d45e0
             else if (film->audio_bits == 16)
42b8f5fb
                 film->audio_type = AV_CODEC_ID_PCM_S16BE_PLANAR;
c58d45e0
             else
36ef5369
                 film->audio_type = AV_CODEC_ID_NONE;
c58d45e0
         } else
36ef5369
             film->audio_type = AV_CODEC_ID_NONE;
2fdf638b
     }
 
fead30d4
     if (AV_RB32(&scratch[0]) != FDSC_TAG)
2fdf638b
         return AVERROR_INVALIDDATA;
 
fead30d4
     if (AV_RB32(&scratch[8]) == CVID_TAG) {
36ef5369
         film->video_type = AV_CODEC_ID_CINEPAK;
ddf9b510
     } else if (AV_RB32(&scratch[8]) == RAW_TAG) {
36ef5369
         film->video_type = AV_CODEC_ID_RAWVIDEO;
ddf9b510
     } else {
36ef5369
         film->video_type = AV_CODEC_ID_NONE;
ddf9b510
     }
2fdf638b
 
     /* initialize the decoder streams */
     if (film->video_type) {
3b3bbdd3
         st = avformat_new_stream(s, NULL);
2fdf638b
         if (!st)
769e10f0
             return AVERROR(ENOMEM);
2fdf638b
         film->video_stream_index = st->index;
72415b2a
         st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
01f4895c
         st->codec->codec_id = film->video_type;
         st->codec->codec_tag = 0;  /* no fourcc */
fead30d4
         st->codec->width = AV_RB32(&scratch[16]);
         st->codec->height = AV_RB32(&scratch[12]);
ddf9b510
 
36ef5369
         if (film->video_type == AV_CODEC_ID_RAWVIDEO) {
ddf9b510
             if (scratch[20] == 24) {
716d413c
                 st->codec->pix_fmt = AV_PIX_FMT_RGB24;
ddf9b510
             } else {
                 av_log(s, AV_LOG_ERROR, "raw video is using unhandled %dbpp\n", scratch[20]);
                 return -1;
             }
         }
2fdf638b
     }
 
     if (film->audio_type) {
3b3bbdd3
         st = avformat_new_stream(s, NULL);
2fdf638b
         if (!st)
769e10f0
             return AVERROR(ENOMEM);
2fdf638b
         film->audio_stream_index = st->index;
72415b2a
         st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
01f4895c
         st->codec->codec_id = film->audio_type;
         st->codec->codec_tag = 1;
         st->codec->channels = film->audio_channels;
         st->codec->sample_rate = film->audio_samplerate;
84d098d9
 
36ef5369
         if (film->audio_type == AV_CODEC_ID_ADPCM_ADX) {
84d098d9
             st->codec->bits_per_coded_sample = 18 * 8 / 32;
             st->codec->block_align = st->codec->channels * 18;
27360ccc
             st->need_parsing = AVSTREAM_PARSE_FULL;
84d098d9
         } else {
             st->codec->bits_per_coded_sample = film->audio_bits;
             st->codec->block_align = st->codec->channels *
                 st->codec->bits_per_coded_sample / 8;
         }
 
01f4895c
         st->codec->bit_rate = st->codec->channels * st->codec->sample_rate *
dd1c8f3e
             st->codec->bits_per_coded_sample;
2fdf638b
     }
 
     /* load the sample table */
e63a3628
     if (avio_read(pb, scratch, 16) != 16)
6f3e0b21
         return AVERROR(EIO);
fead30d4
     if (AV_RB32(&scratch[0]) != STAB_TAG)
2fdf638b
         return AVERROR_INVALIDDATA;
fead30d4
     film->base_clock = AV_RB32(&scratch[8]);
     film->sample_count = AV_RB32(&scratch[12]);
02fb2546
     if(film->sample_count >= UINT_MAX / sizeof(film_sample))
568e18b1
         return -1;
02fb2546
     film->sample_table = av_malloc(film->sample_count * sizeof(film_sample));
7cbe0257
     if (!film->sample_table)
         return AVERROR(ENOMEM);
115329f1
 
4da374f8
     for (i = 0; i < s->nb_streams; i++) {
         st = s->streams[i];
         if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO)
             avpriv_set_pts_info(st, 33, 1, film->base_clock);
         else
             avpriv_set_pts_info(st, 64, 1, film->audio_samplerate);
     }
115329f1
 
2fdf638b
     audio_frame_counter = 0;
     for (i = 0; i < film->sample_count; i++) {
         /* load the next sample record and transfer it to an internal struct */
e63a3628
         if (avio_read(pb, scratch, 16) != 16) {
6892d145
             ret = AVERROR(EIO);
             goto fail;
2fdf638b
         }
115329f1
         film->sample_table[i].sample_offset =
fead30d4
             data_offset + AV_RB32(&scratch[0]);
         film->sample_table[i].sample_size = AV_RB32(&scratch[4]);
6892d145
         if (film->sample_table[i].sample_size > INT_MAX / 4) {
             ret = AVERROR_INVALIDDATA;
             goto fail;
         }
fead30d4
         if (AV_RB32(&scratch[8]) == 0xFFFFFFFF) {
2fdf638b
             film->sample_table[i].stream = film->audio_stream_index;
             film->sample_table[i].pts = audio_frame_counter;
 
36ef5369
             if (film->audio_type == AV_CODEC_ID_ADPCM_ADX)
84d098d9
                 audio_frame_counter += (film->sample_table[i].sample_size * 32 /
                     (18 * film->audio_channels));
36ef5369
             else if (film->audio_type != AV_CODEC_ID_NONE)
84d098d9
                 audio_frame_counter += (film->sample_table[i].sample_size /
                     (film->audio_channels * film->audio_bits / 8));
2fdf638b
         } else {
             film->sample_table[i].stream = film->video_stream_index;
fead30d4
             film->sample_table[i].pts = AV_RB32(&scratch[8]) & 0x7FFFFFFF;
2fdf638b
             film->sample_table[i].keyframe = (scratch[8] & 0x80) ? 0 : 1;
         }
     }
 
     film->current_sample = 0;
 
     return 0;
6892d145
 fail:
     film_read_close(s);
     return ret;
2fdf638b
 }
 
 static int film_read_packet(AVFormatContext *s,
                             AVPacket *pkt)
 {
e4141433
     FilmDemuxContext *film = s->priv_data;
471fe57e
     AVIOContext *pb = s->pb;
02fb2546
     film_sample *sample;
2fdf638b
     int ret = 0;
 
     if (film->current_sample >= film->sample_count)
9530439e
         return AVERROR_EOF;
2fdf638b
 
     sample = &film->sample_table[film->current_sample];
 
     /* position the stream (will probably be there anyway) */
f59d8ff8
     avio_seek(pb, sample->sample_offset, SEEK_SET);
2fdf638b
 
2303b451
 
     ret= av_get_packet(pb, pkt, sample->sample_size);
     if (ret != sample->sample_size)
         ret = AVERROR(EIO);
2fdf638b
 
     pkt->stream_index = sample->stream;
     pkt->pts = sample->pts;
 
     film->current_sample++;
 
     return ret;
 }
 
66355be3
 AVInputFormat ff_segafilm_demuxer = {
dfc2c4d9
     .name           = "film_cpk",
6774247a
     .long_name      = NULL_IF_CONFIG_SMALL("Sega FILM / CPK"),
dfc2c4d9
     .priv_data_size = sizeof(FilmDemuxContext),
     .read_probe     = film_probe,
     .read_header    = film_read_header,
     .read_packet    = film_read_packet,
     .read_close     = film_read_close,
2fdf638b
 };