FFmpeg
thp.c
Go to the documentation of this file.
1 /*
2  * THP Demuxer
3  * Copyright (c) 2007 Marco Gerards
4  *
5  * This file is part of FFmpeg.
6  *
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.
11  *
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.
16  *
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
20  */
21 
22 #include "libavutil/intreadwrite.h"
23 #include "libavutil/intfloat.h"
24 #include "avformat.h"
25 #include "avio_internal.h"
26 #include "demux.h"
27 #include "internal.h"
28 
29 typedef struct ThpDemuxContext {
30  int version;
31  unsigned first_frame;
32  unsigned first_framesz;
33  unsigned last_frame;
34  int compoff;
35  unsigned framecnt;
37  unsigned frame;
38  int64_t next_frame;
39  unsigned next_framesz;
42  int compcount;
43  unsigned char components[16];
45  int has_audio;
46  unsigned audiosize;
48 
49 
50 static int thp_probe(const AVProbeData *p)
51 {
52  double d;
53  /* check file header */
54  if (AV_RL32(p->buf) != MKTAG('T', 'H', 'P', '\0'))
55  return 0;
56 
57  d = av_int2float(AV_RB32(p->buf + 16));
58  if (d < 0.1 || d > 1000 || isnan(d))
59  return AVPROBE_SCORE_MAX/4;
60 
61  return AVPROBE_SCORE_MAX;
62 }
63 
65 {
66  ThpDemuxContext *thp = s->priv_data;
67  AVStream *st;
68  AVIOContext *pb = s->pb;
69  int64_t fsize= avio_size(pb);
70  uint32_t maxsize;
71  int i;
72 
73  /* Read the file header. */
74  avio_rb32(pb); /* Skip Magic. */
75  thp->version = avio_rb32(pb);
76 
77  avio_rb32(pb); /* Max buf size. */
78  avio_rb32(pb); /* Max samples. */
79 
80  thp->fps = av_d2q(av_int2float(avio_rb32(pb)), INT_MAX);
81  if (thp->fps.den <= 0 || thp->fps.num < 0)
82  return AVERROR_INVALIDDATA;
83  thp->framecnt = avio_rb32(pb);
84  thp->first_framesz = avio_rb32(pb);
85  maxsize = avio_rb32(pb);
86  if (fsize > 0 && (!maxsize || fsize < maxsize))
87  maxsize = fsize;
88  ffiocontext(pb)->maxsize = fsize;
89 
90  thp->compoff = avio_rb32(pb);
91  avio_rb32(pb); /* offsetDataOffset. */
92  thp->first_frame = avio_rb32(pb);
93  thp->last_frame = avio_rb32(pb);
94 
95  thp->next_framesz = thp->first_framesz;
96  thp->next_frame = thp->first_frame;
97 
98  /* Read the component structure. */
99  avio_seek (pb, thp->compoff, SEEK_SET);
100  thp->compcount = avio_rb32(pb);
101 
102  if (thp->compcount > FF_ARRAY_ELEMS(thp->components))
103  return AVERROR_INVALIDDATA;
104 
105  /* Read the list of component types. */
106  avio_read(pb, thp->components, 16);
107 
108  for (i = 0; i < thp->compcount; i++) {
109  if (thp->components[i] == 0) {
110  if (thp->vst)
111  break;
112 
113  /* Video component. */
114  st = avformat_new_stream(s, NULL);
115  if (!st)
116  return AVERROR(ENOMEM);
117 
118  /* The denominator and numerator are switched because 1/fps
119  is required. */
120  avpriv_set_pts_info(st, 64, thp->fps.den, thp->fps.num);
123  st->codecpar->codec_tag = 0; /* no fourcc */
124  st->codecpar->width = avio_rb32(pb);
125  st->codecpar->height = avio_rb32(pb);
126  st->codecpar->sample_rate = av_q2d(thp->fps);
127  st->nb_frames =
128  st->duration = thp->framecnt;
129  thp->vst = st;
130  thp->video_stream_index = st->index;
131 
132  if (thp->version == 0x11000)
133  avio_rb32(pb); /* Unknown. */
134  } else if (thp->components[i] == 1) {
135  if (thp->has_audio != 0)
136  break;
137 
138  /* Audio component. */
139  st = avformat_new_stream(s, NULL);
140  if (!st)
141  return AVERROR(ENOMEM);
142 
145  st->codecpar->codec_tag = 0; /* no fourcc */
147  st->codecpar->sample_rate = avio_rb32(pb); /* Frequency. */
148  st->duration = avio_rb32(pb);
149 
150  avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
151 
152  thp->audio_stream_index = st->index;
153  thp->has_audio = 1;
154  }
155  }
156 
157  if (!thp->vst)
158  return AVERROR_INVALIDDATA;
159 
160  return 0;
161 }
162 
164  AVPacket *pkt)
165 {
166  ThpDemuxContext *thp = s->priv_data;
167  AVIOContext *pb = s->pb;
168  unsigned int size;
169  int ret;
170 
171  if (thp->audiosize == 0) {
172  /* Terminate when last frame is reached. */
173  if (thp->frame >= thp->framecnt)
174  return AVERROR_EOF;
175 
176  avio_seek(pb, thp->next_frame, SEEK_SET);
177 
178  /* Locate the next frame and read out its size. */
179  thp->next_frame += FFMAX(thp->next_framesz, 1);
180  thp->next_framesz = avio_rb32(pb);
181 
182  avio_rb32(pb); /* Previous total size. */
183  size = avio_rb32(pb); /* Total size of this frame. */
184 
185  /* Store the audiosize so the next time this function is called,
186  the audio can be read. */
187  if (thp->has_audio)
188  thp->audiosize = avio_rb32(pb); /* Audio size. */
189  else
190  thp->frame++;
191 
192  ret = av_get_packet(pb, pkt, size);
193  if (ret < 0)
194  return ret;
195  if (ret != size) {
196  return AVERROR(EIO);
197  }
198 
200  } else {
201  ret = av_get_packet(pb, pkt, thp->audiosize);
202  if (ret < 0)
203  return ret;
204  if (ret != thp->audiosize) {
205  return AVERROR(EIO);
206  }
207 
209  if (thp->audiosize >= 8)
210  pkt->duration = AV_RB32(&pkt->data[4]);
211 
212  thp->audiosize = 0;
213  thp->frame++;
214  }
215 
216  return 0;
217 }
218 
220  .p.name = "thp",
221  .p.long_name = NULL_IF_CONFIG_SMALL("THP"),
222  .priv_data_size = sizeof(ThpDemuxContext),
226 };
ThpDemuxContext
Definition: thp.c:29
ThpDemuxContext::audio_stream_index
int audio_stream_index
Definition: thp.c:41
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
AVCodecParameters::codec_type
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:51
ffiocontext
static av_always_inline FFIOContext * ffiocontext(AVIOContext *ctx)
Definition: avio_internal.h:81
avformat_new_stream
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:57
ThpDemuxContext::vst
AVStream * vst
Definition: thp.c:44
thp_probe
static int thp_probe(const AVProbeData *p)
Definition: thp.c:50
AVPacket::data
uint8_t * data
Definition: packet.h:522
AVPacket::duration
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: packet.h:540
AVCodecParameters::codec_tag
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition: codec_par.h:59
FFMAX
#define FFMAX(a, b)
Definition: macros.h:47
AV_CODEC_ID_THP
@ AV_CODEC_ID_THP
Definition: codec_id.h:152
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:313
intfloat.h
avio_size
int64_t avio_size(AVIOContext *s)
Get the filesize.
Definition: aviobuf.c:322
AVPROBE_SCORE_MAX
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:463
avpriv_set_pts_info
void avpriv_set_pts_info(AVStream *st, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: avformat.c:853
av_int2float
static av_always_inline float av_int2float(uint32_t i)
Reinterpret a 32-bit integer as a float.
Definition: intfloat.h:40
AVStream::duration
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:802
AVRational::num
int num
Numerator.
Definition: rational.h:59
avio_rb32
unsigned int avio_rb32(AVIOContext *s)
Definition: aviobuf.c:760
pkt
AVPacket * pkt
Definition: movenc.c:59
FF_ARRAY_ELEMS
#define FF_ARRAY_ELEMS(a)
Definition: sinewin_tablegen.c:29
read_packet
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_read_callback.c:41
intreadwrite.h
s
#define s(width, name)
Definition: cbs_vp9.c:198
AVInputFormat::name
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:553
AVProbeData::buf
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:453
ThpDemuxContext::first_frame
unsigned first_frame
Definition: thp.c:31
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
AVCodecParameters::width
int width
Video only.
Definition: codec_par.h:134
av_q2d
static double av_q2d(AVRational a)
Convert an AVRational to a double.
Definition: rational.h:104
ThpDemuxContext::next_framesz
unsigned next_framesz
Definition: thp.c:39
thp_read_header
static int thp_read_header(AVFormatContext *s)
Definition: thp.c:64
fsize
static int64_t fsize(FILE *f)
Definition: audiomatch.c:29
ThpDemuxContext::next_frame
int64_t next_frame
Definition: thp.c:38
AVFormatContext
Format I/O context.
Definition: avformat.h:1255
internal.h
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:766
read_header
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:550
ThpDemuxContext::components
unsigned char components[16]
Definition: thp.c:43
NULL
#define NULL
Definition: coverity.c:32
AVRational
Rational number (pair of numerator and denominator).
Definition: rational.h:58
isnan
#define isnan(x)
Definition: libm.h:340
AVProbeData
This structure contains the data a format has to probe a file.
Definition: avformat.h:451
AVCodecParameters::ch_layout
AVChannelLayout ch_layout
Audio only.
Definition: codec_par.h:180
AVCodecParameters::sample_rate
int sample_rate
Audio only.
Definition: codec_par.h:184
FFIOContext::maxsize
int64_t maxsize
max filesize, used to limit allocations
Definition: avio_internal.h:46
AVStream::nb_frames
int64_t nb_frames
number of frames in this stream if known or 0
Definition: avformat.h:804
AVIOContext
Bytestream IO Context.
Definition: avio.h:160
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:106
ThpDemuxContext::audiosize
unsigned audiosize
Definition: thp.c:46
size
int size
Definition: twinvq_data.h:10344
AV_RB32
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_RB32
Definition: bytestream.h:96
ThpDemuxContext::compoff
int compoff
Definition: thp.c:34
FFInputFormat::p
AVInputFormat p
The public AVInputFormat.
Definition: demux.h:35
ThpDemuxContext::first_framesz
unsigned first_framesz
Definition: thp.c:32
thp_read_packet
static int thp_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: thp.c:163
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:255
avio_internal.h
AVCodecParameters::height
int height
Definition: codec_par.h:135
av_d2q
AVRational av_d2q(double d, int max)
Convert a double precision floating point number to a rational.
Definition: rational.c:106
ThpDemuxContext::compcount
int compcount
Definition: thp.c:42
ff_thp_demuxer
const FFInputFormat ff_thp_demuxer
Definition: thp.c:219
demux.h
av_get_packet
int av_get_packet(AVIOContext *s, AVPacket *pkt, int size)
Allocate and read the payload of a packet and initialize its fields with default values.
Definition: utils.c:103
ThpDemuxContext::video_stream_index
int video_stream_index
Definition: thp.c:40
ret
ret
Definition: filter_design.txt:187
AVStream
Stream structure.
Definition: avformat.h:743
avio_seek
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:230
avformat.h
ThpDemuxContext::fps
AVRational fps
Definition: thp.c:36
ThpDemuxContext::version
int version
Definition: thp.c:30
AV_RL32
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:92
AVStream::index
int index
stream index in AVFormatContext
Definition: avformat.h:749
AVRational::den
int den
Denominator.
Definition: rational.h:60
avio_read
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:611
ThpDemuxContext::frame
unsigned frame
Definition: thp.c:37
AVPacket::stream_index
int stream_index
Definition: packet.h:524
AV_CODEC_ID_ADPCM_THP
@ AV_CODEC_ID_ADPCM_THP
Definition: codec_id.h:385
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
read_probe
static int read_probe(const AVProbeData *p)
Definition: cdg.c:30
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:55
AVPacket
This structure stores compressed data.
Definition: packet.h:499
FFInputFormat
Definition: demux.h:31
ThpDemuxContext::has_audio
int has_audio
Definition: thp.c:45
d
d
Definition: ffmpeg_filter.c:425
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
MKTAG
#define MKTAG(a, b, c, d)
Definition: macros.h:55
ThpDemuxContext::last_frame
unsigned last_frame
Definition: thp.c:33
ThpDemuxContext::framecnt
unsigned framecnt
Definition: thp.c:35