thp.c 6.66 KB
Newer Older
1 2
/*
 * THP Demuxer
3
 * Copyright (c) 2007 Marco Gerards
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 *
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * FFmpeg is distributed in the hope that it will be useful,
 * 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
 * License along with FFmpeg; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

22
#include "libavutil/intreadwrite.h"
23
#include "libavutil/intfloat.h"
24
#include "avformat.h"
25
#include "internal.h"
26 27 28

typedef struct ThpDemuxContext {
    int              version;
29 30 31
    unsigned         first_frame;
    unsigned         first_framesz;
    unsigned         last_frame;
32
    int              compoff;
33
    unsigned         framecnt;
34
    AVRational       fps;
35 36 37
    unsigned         frame;
    int64_t          next_frame;
    unsigned         next_framesz;
38
    int              video_stream_index;
39
    int              audio_stream_index;
40 41 42 43
    int              compcount;
    unsigned char    components[16];
    AVStream*        vst;
    int              has_audio;
44
    unsigned         audiosize;
45 46 47 48 49
} ThpDemuxContext;


static int thp_probe(AVProbeData *p)
{
50
    double d;
51
    /* check file header */
52
    if (AV_RL32(p->buf) != MKTAG('T', 'H', 'P', '\0'))
53
        return 0;
54 55 56 57 58 59

    d = av_int2float(AV_RB32(p->buf + 16));
    if (d < 0.1 || d > 1000 || isnan(d))
        return AVPROBE_SCORE_MAX/4;

    return AVPROBE_SCORE_MAX;
60 61
}

62
static int thp_read_header(AVFormatContext *s)
63
{
64 65
    ThpDemuxContext *thp = s->priv_data;
    AVStream *st;
66
    AVIOContext *pb = s->pb;
67
    int64_t fsize= avio_size(pb);
68 69 70
    int i;

    /* Read the file header.  */
71 72
                           avio_rb32(pb); /* Skip Magic.  */
    thp->version         = avio_rb32(pb);
73

74 75
                           avio_rb32(pb); /* Max buf size.  */
                           avio_rb32(pb); /* Max samples.  */
76

77
    thp->fps             = av_d2q(av_int2float(avio_rb32(pb)), INT_MAX);
78 79
    thp->framecnt        = avio_rb32(pb);
    thp->first_framesz   = avio_rb32(pb);
80 81 82
    pb->maxsize          = avio_rb32(pb);
    if(fsize>0 && (!pb->maxsize || fsize < pb->maxsize))
        pb->maxsize= fsize;
83

84 85 86 87
    thp->compoff         = avio_rb32(pb);
                           avio_rb32(pb); /* offsetDataOffset.  */
    thp->first_frame     = avio_rb32(pb);
    thp->last_frame      = avio_rb32(pb);
88 89 90 91 92

    thp->next_framesz    = thp->first_framesz;
    thp->next_frame      = thp->first_frame;

    /* Read the component structure.  */
93
    avio_seek (pb, thp->compoff, SEEK_SET);
94
    thp->compcount       = avio_rb32(pb);
95 96

    /* Read the list of component types.  */
97
    avio_read(pb, thp->components, 16);
98 99 100

    for (i = 0; i < thp->compcount; i++) {
        if (thp->components[i] == 0) {
101
            if (thp->vst)
102 103 104
                break;

            /* Video component.  */
105
            st = avformat_new_stream(s, NULL);
106
            if (!st)
107
                return AVERROR(ENOMEM);
108 109 110

            /* The denominator and numerator are switched because 1/fps
               is required.  */
111
            avpriv_set_pts_info(st, 64, thp->fps.den, thp->fps.num);
112 113 114 115 116 117
            st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
            st->codecpar->codec_id = AV_CODEC_ID_THP;
            st->codecpar->codec_tag = 0;  /* no fourcc */
            st->codecpar->width = avio_rb32(pb);
            st->codecpar->height = avio_rb32(pb);
            st->codecpar->sample_rate = av_q2d(thp->fps);
Piotr Bandurski's avatar
Piotr Bandurski committed
118 119
            st->nb_frames =
            st->duration = thp->framecnt;
120 121 122 123
            thp->vst = st;
            thp->video_stream_index = st->index;

            if (thp->version == 0x11000)
124
                avio_rb32(pb); /* Unknown.  */
125 126 127 128 129
        } else if (thp->components[i] == 1) {
            if (thp->has_audio != 0)
                break;

            /* Audio component.  */
130
            st = avformat_new_stream(s, NULL);
131
            if (!st)
132
                return AVERROR(ENOMEM);
133

134 135 136 137 138
            st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
            st->codecpar->codec_id = AV_CODEC_ID_ADPCM_THP;
            st->codecpar->codec_tag = 0;  /* no fourcc */
            st->codecpar->channels    = avio_rb32(pb); /* numChannels.  */
            st->codecpar->sample_rate = avio_rb32(pb); /* Frequency.  */
139
            st->duration           = avio_rb32(pb);
140

141
            avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
142 143 144

            thp->audio_stream_index = st->index;
            thp->has_audio = 1;
145 146 147
        }
    }

148
    return 0;
149 150 151 152 153 154
}

static int thp_read_packet(AVFormatContext *s,
                            AVPacket *pkt)
{
    ThpDemuxContext *thp = s->priv_data;
155
    AVIOContext *pb = s->pb;
156
    unsigned int size;
157 158
    int ret;

159
    if (thp->audiosize == 0) {
160 161
        /* Terminate when last frame is reached.  */
        if (thp->frame >= thp->framecnt)
Piotr Bandurski's avatar
Piotr Bandurski committed
162
            return AVERROR_EOF;
163

164
        avio_seek(pb, thp->next_frame, SEEK_SET);
165

166
        /* Locate the next frame and read out its size.  */
167
        thp->next_frame += FFMAX(thp->next_framesz, 1);
168
        thp->next_framesz = avio_rb32(pb);
169

170 171
                        avio_rb32(pb); /* Previous total size.  */
        size          = avio_rb32(pb); /* Total size of this frame.  */
172

173 174 175
        /* Store the audiosize so the next time this function is called,
           the audio can be read.  */
        if (thp->has_audio)
176
            thp->audiosize = avio_rb32(pb); /* Audio size.  */
177 178
        else
            thp->frame++;
179

180
        ret = av_get_packet(pb, pkt, size);
181 182
        if (ret < 0)
            return ret;
183
        if (ret != size) {
184
            av_packet_unref(pkt);
185
            return AVERROR(EIO);
186
        }
187

188 189
        pkt->stream_index = thp->video_stream_index;
    } else {
190
        ret = av_get_packet(pb, pkt, thp->audiosize);
191 192
        if (ret < 0)
            return ret;
193
        if (ret != thp->audiosize) {
194
            av_packet_unref(pkt);
195
            return AVERROR(EIO);
196 197 198
        }

        pkt->stream_index = thp->audio_stream_index;
199 200 201
        if (thp->audiosize >= 8)
            pkt->duration = AV_RB32(&pkt->data[4]);

202 203 204
        thp->audiosize = 0;
        thp->frame++;
    }
205 206 207 208

    return 0;
}

209
AVInputFormat ff_thp_demuxer = {
210 211 212 213 214 215
    .name           = "thp",
    .long_name      = NULL_IF_CONFIG_SMALL("THP"),
    .priv_data_size = sizeof(ThpDemuxContext),
    .read_probe     = thp_probe,
    .read_header    = thp_read_header,
    .read_packet    = thp_read_packet
216
};