3 * Copyright (c) 2007 Marco Gerards.
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 #include "allformats.h"
26 typedef struct ThpDemuxContext {
37 int video_stream_index;
39 unsigned char components[16];
45 static int thp_probe(AVProbeData *p)
47 /* check file header */
51 if (AV_RL32(p->buf) == MKTAG('T', 'H', 'P', '\0'))
52 return AVPROBE_SCORE_MAX;
57 static int thp_read_header(AVFormatContext *s,
58 AVFormatParameters *ap)
60 ThpDemuxContext *thp = s->priv_data;
62 ByteIOContext *pb = &s->pb;
65 /* Read the file header. */
67 get_be32(pb); /* Skip Magic. */
68 thp->version = get_be32(pb);
70 get_be32(pb); /* Max buf size. */
71 get_be32(pb); /* Max samples. */
73 thp->fps = av_d2q(av_int2flt(get_be32(pb)), INT_MAX);
74 thp->framecnt = get_be32(pb);
75 thp->first_framesz = get_be32(pb);
76 get_be32(pb); /* Data size. */
78 thp->compoff = get_be32(pb);
79 get_be32(pb); /* offsetDataOffset. */
80 thp->first_frame = get_be32(pb);
81 thp->last_frame = get_be32(pb);
83 thp->next_framesz = thp->first_framesz;
84 thp->next_frame = thp->first_frame;
86 /* Read the component structure. */
87 url_fseek (pb, thp->compoff, SEEK_SET);
88 thp->compcount = get_be32(pb);
90 /* Read the list of component types. */
91 get_buffer(pb, thp->components, 16);
93 for (i = 0; i < thp->compcount; i++) {
94 if (thp->components[i] == 0) {
98 /* Video component. */
99 st = av_new_stream(s, 0);
101 return AVERROR_NOMEM;
103 /* The denominator and numerator are switched because 1/fps
105 av_set_pts_info(st, 64, thp->fps.den, thp->fps.num);
106 st->codec->codec_type = CODEC_TYPE_VIDEO;
107 st->codec->codec_id = CODEC_ID_THP;
108 st->codec->codec_tag = 0; /* no fourcc */
109 st->codec->width = get_be32(pb);
110 st->codec->height = get_be32(pb);
111 st->codec->sample_rate = av_q2d(thp->fps);
113 thp->video_stream_index = st->index;
115 if (thp->version == 0x11000)
116 get_be32(pb); /* Unknown. */
118 else if (thp->components[i] == 1) {
119 /* XXX: Required for audio playback. */
127 static int thp_read_packet(AVFormatContext *s,
130 ThpDemuxContext *thp = s->priv_data;
131 ByteIOContext *pb = &s->pb;
135 /* Terminate when last frame is reached. */
136 if (thp->frame >= thp->framecnt)
139 url_fseek(pb, thp->next_frame, SEEK_SET);
141 /* Locate the next frame and read out its size. */
142 thp->next_frame += thp->next_framesz;
143 thp->next_framesz = get_be32(pb);
145 get_be32(pb); /* Previous total size. */
146 size = get_be32(pb); /* Total size of this frame. */
149 get_be32(pb); /* Audio size. */
151 ret = av_get_packet(pb, pkt, size);
157 pkt->stream_index = thp->video_stream_index;
163 AVInputFormat thp_demuxer = {
166 sizeof(ThpDemuxContext),