FFmpeg  2.8.17
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
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(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  if (thp->fps.den <= 0 || thp->fps.num < 0)
79  return AVERROR_INVALIDDATA;
80  thp->framecnt = avio_rb32(pb);
81  thp->first_framesz = avio_rb32(pb);
82  pb->maxsize = avio_rb32(pb);
83  if(fsize>0 && (!pb->maxsize || fsize < pb->maxsize))
84  pb->maxsize= fsize;
85 
86  thp->compoff = avio_rb32(pb);
87  avio_rb32(pb); /* offsetDataOffset. */
88  thp->first_frame = avio_rb32(pb);
89  thp->last_frame = avio_rb32(pb);
90 
91  thp->next_framesz = thp->first_framesz;
92  thp->next_frame = thp->first_frame;
93 
94  /* Read the component structure. */
95  avio_seek (pb, thp->compoff, SEEK_SET);
96  thp->compcount = avio_rb32(pb);
97 
98  if (thp->compcount > FF_ARRAY_ELEMS(thp->components))
99  return AVERROR_INVALIDDATA;
100 
101  /* Read the list of component types. */
102  avio_read(pb, thp->components, 16);
103 
104  for (i = 0; i < thp->compcount; i++) {
105  if (thp->components[i] == 0) {
106  if (thp->vst)
107  break;
108 
109  /* Video component. */
110  st = avformat_new_stream(s, NULL);
111  if (!st)
112  return AVERROR(ENOMEM);
113 
114  /* The denominator and numerator are switched because 1/fps
115  is required. */
116  avpriv_set_pts_info(st, 64, thp->fps.den, thp->fps.num);
119  st->codec->codec_tag = 0; /* no fourcc */
120  st->codec->width = avio_rb32(pb);
121  st->codec->height = avio_rb32(pb);
122  st->nb_frames =
123  st->duration = thp->framecnt;
124  thp->vst = st;
125  thp->video_stream_index = st->index;
126 
127  if (thp->version == 0x11000)
128  avio_rb32(pb); /* Unknown. */
129  } else if (thp->components[i] == 1) {
130  if (thp->has_audio != 0)
131  break;
132 
133  /* Audio component. */
134  st = avformat_new_stream(s, NULL);
135  if (!st)
136  return AVERROR(ENOMEM);
137 
140  st->codec->codec_tag = 0; /* no fourcc */
141  st->codec->channels = avio_rb32(pb); /* numChannels. */
142  st->codec->sample_rate = avio_rb32(pb); /* Frequency. */
143 
144  avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
145 
146  thp->audio_stream_index = st->index;
147  thp->has_audio = 1;
148  }
149  }
150 
151  if (!thp->vst)
152  return AVERROR_INVALIDDATA;
153 
154  return 0;
155 }
156 
158  AVPacket *pkt)
159 {
160  ThpDemuxContext *thp = s->priv_data;
161  AVIOContext *pb = s->pb;
162  unsigned int size;
163  int ret;
164 
165  if (thp->audiosize == 0) {
166  /* Terminate when last frame is reached. */
167  if (thp->frame >= thp->framecnt)
168  return AVERROR_EOF;
169 
170  avio_seek(pb, thp->next_frame, SEEK_SET);
171 
172  /* Locate the next frame and read out its size. */
173  thp->next_frame += FFMAX(thp->next_framesz, 1);
174  thp->next_framesz = avio_rb32(pb);
175 
176  avio_rb32(pb); /* Previous total size. */
177  size = avio_rb32(pb); /* Total size of this frame. */
178 
179  /* Store the audiosize so the next time this function is called,
180  the audio can be read. */
181  if (thp->has_audio)
182  thp->audiosize = avio_rb32(pb); /* Audio size. */
183  else
184  thp->frame++;
185 
186  ret = av_get_packet(pb, pkt, size);
187  if (ret < 0)
188  return ret;
189  if (ret != size) {
190  av_free_packet(pkt);
191  return AVERROR(EIO);
192  }
193 
194  pkt->stream_index = thp->video_stream_index;
195  } else {
196  ret = av_get_packet(pb, pkt, thp->audiosize);
197  if (ret < 0)
198  return ret;
199  if (ret != thp->audiosize) {
200  av_free_packet(pkt);
201  return AVERROR(EIO);
202  }
203 
204  pkt->stream_index = thp->audio_stream_index;
205  if (thp->audiosize >= 8)
206  pkt->duration = AV_RB32(&pkt->data[4]);
207 
208  thp->audiosize = 0;
209  thp->frame++;
210  }
211 
212  return 0;
213 }
214 
216  .name = "thp",
217  .long_name = NULL_IF_CONFIG_SMALL("THP"),
218  .priv_data_size = sizeof(ThpDemuxContext),
222 };
unsigned framecnt
Definition: thp.c:33
#define NULL
Definition: coverity.c:32
const char * s
Definition: avisynth_c.h:631
Bytestream IO Context.
Definition: avio.h:111
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
int64_t avio_size(AVIOContext *s)
Get the filesize.
Definition: aviobuf.c:284
void av_free_packet(AVPacket *pkt)
Free a packet.
Definition: avpacket.c:284
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:4098
int video_stream_index
Definition: thp.c:38
AVInputFormat ff_thp_demuxer
Definition: thp.c:215
static av_always_inline float av_int2float(uint32_t i)
Reinterpret a 32-bit integer as a float.
Definition: intfloat.h:40
int num
numerator
Definition: rational.h:44
int index
stream index in AVFormatContext
Definition: avformat.h:855
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:204
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:166
Format I/O context.
Definition: avformat.h:1285
static av_always_inline av_const int isnan(float x)
Definition: libm.h:96
unsigned int avio_rb32(AVIOContext *s)
Definition: aviobuf.c:694
#define AV_RB32
Definition: intreadwrite.h:130
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:3761
uint8_t * data
Definition: avcodec.h:1433
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:244
ptrdiff_t size
Definition: opengl_enc.c:101
AVStream * vst
Definition: thp.c:42
int duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: avcodec.h:1451
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:542
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:178
unsigned next_framesz
Definition: thp.c:37
static int thp_probe(AVProbeData *p)
Definition: thp.c:48
#define FFMAX(a, b)
Definition: common.h:90
int64_t next_frame
Definition: thp.c:36
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:873
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:462
AVRational av_d2q(double d, int max)
Convert a double precision floating point number to a rational.
Definition: rational.c:106
unsigned first_frame
Definition: thp.c:29
AVRational fps
Definition: thp.c:34
static int read_probe(AVProbeData *pd)
Definition: jvdec.c:55
int width
picture width / height.
Definition: avcodec.h:1691
#define AV_RL32
Definition: intreadwrite.h:146
#define FF_ARRAY_ELEMS(a)
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:634
Stream structure.
Definition: avformat.h:854
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_reading.c:42
enum AVMediaType codec_type
Definition: avcodec.h:1520
enum AVCodecID codec_id
Definition: avcodec.h:1529
int sample_rate
samples per second
Definition: avcodec.h:2272
AVIOContext * pb
I/O context.
Definition: avformat.h:1327
unsigned first_framesz
Definition: thp.c:30
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
Definition: avcodec.h:1544
rational number numerator/denominator
Definition: rational.h:43
This structure contains the data a format has to probe a file.
Definition: avformat.h:460
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:913
unsigned frame
Definition: thp.c:35
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:472
Main libavformat public API header.
int version
Definition: thp.c:28
static int thp_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: thp.c:157
int64_t nb_frames
number of frames in this stream if known or 0
Definition: avformat.h:915
int den
denominator
Definition: rational.h:45
int channels
number of audio channels
Definition: avcodec.h:2273
void * priv_data
Format private data.
Definition: avformat.h:1313
int audio_stream_index
Definition: thp.c:39
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:640
int stream_index
Definition: avcodec.h:1435
unsigned audiosize
Definition: thp.c:44
int has_audio
Definition: thp.c:43
#define MKTAG(a, b, c, d)
Definition: common.h:341
unsigned last_frame
Definition: thp.c:31
This structure stores compressed data.
Definition: avcodec.h:1410
int compcount
Definition: thp.c:40