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 "internal.h"
26 
27 typedef struct ThpDemuxContext {
28  int version;
29  unsigned first_frame;
30  unsigned first_framesz;
31  unsigned last_frame;
32  int compoff;
33  unsigned framecnt;
35  unsigned frame;
36  int64_t next_frame;
37  unsigned next_framesz;
40  int compcount;
41  unsigned char components[16];
43  int has_audio;
44  unsigned audiosize;
46 
47 
48 static int thp_probe(const AVProbeData *p)
49 {
50  double d;
51  /* check file header */
52  if (AV_RL32(p->buf) != MKTAG('T', 'H', 'P', '\0'))
53  return 0;
54 
55  d = av_int2float(AV_RB32(p->buf + 16));
56  if (d < 0.1 || d > 1000 || isnan(d))
57  return AVPROBE_SCORE_MAX/4;
58 
59  return AVPROBE_SCORE_MAX;
60 }
61 
63 {
64  ThpDemuxContext *thp = s->priv_data;
65  AVStream *st;
66  AVIOContext *pb = s->pb;
67  int64_t fsize= avio_size(pb);
68  int i;
69 
70  /* Read the file header. */
71  avio_rb32(pb); /* Skip Magic. */
72  thp->version = avio_rb32(pb);
73 
74  avio_rb32(pb); /* Max buf size. */
75  avio_rb32(pb); /* Max samples. */
76 
77  thp->fps = av_d2q(av_int2float(avio_rb32(pb)), INT_MAX);
78  thp->framecnt = avio_rb32(pb);
79  thp->first_framesz = avio_rb32(pb);
80  pb->maxsize = avio_rb32(pb);
81  if(fsize>0 && (!pb->maxsize || fsize < pb->maxsize))
82  pb->maxsize= fsize;
83 
84  thp->compoff = avio_rb32(pb);
85  avio_rb32(pb); /* offsetDataOffset. */
86  thp->first_frame = avio_rb32(pb);
87  thp->last_frame = avio_rb32(pb);
88 
89  thp->next_framesz = thp->first_framesz;
90  thp->next_frame = thp->first_frame;
91 
92  /* 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  for (i = 0; i < thp->compcount; i++) {
100  if (thp->components[i] == 0) {
101  if (thp->vst)
102  break;
103 
104  /* Video component. */
105  st = avformat_new_stream(s, NULL);
106  if (!st)
107  return AVERROR(ENOMEM);
108 
109  /* The denominator and numerator are switched because 1/fps
110  is required. */
111  avpriv_set_pts_info(st, 64, thp->fps.den, thp->fps.num);
114  st->codecpar->codec_tag = 0; /* no fourcc */
115  st->codecpar->width = avio_rb32(pb);
116  st->codecpar->height = avio_rb32(pb);
117  st->codecpar->sample_rate = av_q2d(thp->fps);
118  st->nb_frames =
119  st->duration = thp->framecnt;
120  thp->vst = st;
121  thp->video_stream_index = st->index;
122 
123  if (thp->version == 0x11000)
124  avio_rb32(pb); /* Unknown. */
125  } else if (thp->components[i] == 1) {
126  if (thp->has_audio != 0)
127  break;
128 
129  /* Audio component. */
130  st = avformat_new_stream(s, NULL);
131  if (!st)
132  return AVERROR(ENOMEM);
133 
136  st->codecpar->codec_tag = 0; /* no fourcc */
137  st->codecpar->channels = avio_rb32(pb); /* numChannels. */
138  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  thp->audio_stream_index = st->index;
144  thp->has_audio = 1;
145  }
146  }
147 
148  return 0;
149 }
150 
152  AVPacket *pkt)
153 {
154  ThpDemuxContext *thp = s->priv_data;
155  AVIOContext *pb = s->pb;
156  unsigned int size;
157  int ret;
158 
159  if (thp->audiosize == 0) {
160  /* Terminate when last frame is reached. */
161  if (thp->frame >= thp->framecnt)
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  avio_rb32(pb); /* Previous total size. */
171  size = avio_rb32(pb); /* Total size of this frame. */
172 
173  /* Store the audiosize so the next time this function is called,
174  the audio can be read. */
175  if (thp->has_audio)
176  thp->audiosize = avio_rb32(pb); /* Audio size. */
177  else
178  thp->frame++;
179 
180  ret = av_get_packet(pb, pkt, size);
181  if (ret < 0)
182  return ret;
183  if (ret != size) {
184  av_packet_unref(pkt);
185  return AVERROR(EIO);
186  }
187 
188  pkt->stream_index = thp->video_stream_index;
189  } else {
190  ret = av_get_packet(pb, pkt, thp->audiosize);
191  if (ret < 0)
192  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  if (thp->audiosize >= 8)
200  pkt->duration = AV_RB32(&pkt->data[4]);
201 
202  thp->audiosize = 0;
203  thp->frame++;
204  }
205 
206  return 0;
207 }
208 
210  .name = "thp",
211  .long_name = NULL_IF_CONFIG_SMALL("THP"),
212  .priv_data_size = sizeof(ThpDemuxContext),
216 };
unsigned framecnt
Definition: thp.c:33
#define NULL
Definition: coverity.c:32
Bytestream IO Context.
Definition: avio.h:161
int64_t avio_size(AVIOContext *s)
Get the filesize.
Definition: aviobuf.c:339
static int thp_read_header(AVFormatContext *s)
Definition: thp.c:62
void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: utils.c:4926
int video_stream_index
Definition: thp.c:38
AVInputFormat ff_thp_demuxer
Definition: thp.c:209
static av_always_inline float av_int2float(uint32_t i)
Reinterpret a 32-bit integer as a float.
Definition: intfloat.h:40
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:3968
int num
Numerator.
Definition: rational.h:59
int index
stream index in AVFormatContext
Definition: avformat.h:882
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:246
static int thp_probe(const AVProbeData *p)
Definition: thp.c:48
static AVPacket pkt
int64_t maxsize
max filesize, used to limit allocations This field is internal to libavformat and access from outside...
Definition: avio.h:266
Format I/O context.
Definition: avformat.h:1358
int width
Video only.
Definition: avcodec.h:4034
unsigned int avio_rb32(AVIOContext *s)
Definition: aviobuf.c:803
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: avcodec.h:1498
int compoff
Definition: thp.c:32
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4499
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:87
uint8_t * data
Definition: avcodec.h:1480
static double av_q2d(AVRational a)
Convert an AVRational to a double.
Definition: rational.h:104
unsigned char components[16]
Definition: thp.c:41
#define AVERROR_EOF
End of file.
Definition: error.h:55
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:310
ptrdiff_t size
Definition: opengl_enc.c:100
AVStream * vst
Definition: thp.c:42
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:650
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:259
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:186
enum AVMediaType codec_type
General type of the encoded data.
Definition: avcodec.h:3964
unsigned next_framesz
Definition: thp.c:37
#define FFMAX(a, b)
Definition: common.h:94
int64_t next_frame
Definition: thp.c:36
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:448
unsigned first_frame
Definition: thp.c:29
AVRational fps
Definition: thp.c:34
#define s(width, name)
Definition: cbs_vp9.c:257
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:530
Stream structure.
Definition: avformat.h:881
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_reading.c:42
AVIOContext * pb
I/O context.
Definition: avformat.h:1400
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:599
unsigned first_framesz
Definition: thp.c:30
Rational number (pair of numerator and denominator).
Definition: rational.h:58
#define isnan(x)
Definition: libm.h:340
This structure contains the data a format has to probe a file.
Definition: avformat.h:446
static int read_probe(const AVProbeData *pd)
Definition: jvdec.c:55
AVRational av_d2q(double d, int max)
Convert a double precision floating point number to a rational.
Definition: rational.c:106
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:930
int sample_rate
Audio only.
Definition: avcodec.h:4078
unsigned frame
Definition: thp.c:35
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:458
Main libavformat public API header.
int version
Definition: thp.c:28
static int thp_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: thp.c:151
int64_t nb_frames
number of frames in this stream if known or 0
Definition: avformat.h:932
int den
Denominator.
Definition: rational.h:60
void * priv_data
Format private data.
Definition: avformat.h:1386
int audio_stream_index
Definition: thp.c:39
int channels
Audio only.
Definition: avcodec.h:4074
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:654
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1028
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition: avcodec.h:3972
static int64_t fsize(FILE *f)
Definition: audiomatch.c:28
int stream_index
Definition: avcodec.h:1482
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
unsigned audiosize
Definition: thp.c:44
int has_audio
Definition: thp.c:43
#define MKTAG(a, b, c, d)
Definition: common.h:366
unsigned last_frame
Definition: thp.c:31
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:87
This structure stores compressed data.
Definition: avcodec.h:1457
int compcount
Definition: thp.c:40