FFmpeg
idroqdec.c
Go to the documentation of this file.
1 /*
2  * id RoQ (.roq) File Demuxer
3  * Copyright (c) 2003 The FFmpeg project
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 /**
23  * @file
24  * id RoQ format file demuxer
25  * by Mike Melanson (melanson@pcisys.net)
26  * for more information on the .roq file format, visit:
27  * http://www.csse.monash.edu.au/~timf/
28  */
29 
31 #include "libavutil/intreadwrite.h"
32 #include "avformat.h"
33 #include "demux.h"
34 #include "internal.h"
35 #include "avio_internal.h"
36 
37 #define RoQ_MAGIC_NUMBER 0x1084
38 #define RoQ_CHUNK_PREAMBLE_SIZE 8
39 #define RoQ_AUDIO_SAMPLE_RATE 22050
40 #define RoQ_CHUNKS_TO_SCAN 30
41 
42 #define RoQ_INFO 0x1001
43 #define RoQ_QUAD_CODEBOOK 0x1002
44 #define RoQ_QUAD_VQ 0x1011
45 #define RoQ_SOUND_MONO 0x1020
46 #define RoQ_SOUND_STEREO 0x1021
47 
48 typedef struct RoqDemuxContext {
49 
51  int width;
52  int height;
54 
57 
59  unsigned int audio_frame_count;
60 
62 
63 static int roq_probe(const AVProbeData *p)
64 {
65  if ((AV_RL16(&p->buf[0]) != RoQ_MAGIC_NUMBER) ||
66  (AV_RL32(&p->buf[2]) != 0xFFFFFFFF))
67  return 0;
68 
69  return AVPROBE_SCORE_MAX;
70 }
71 
73 {
74  RoqDemuxContext *roq = s->priv_data;
75  AVIOContext *pb = s->pb;
76  unsigned char preamble[RoQ_CHUNK_PREAMBLE_SIZE];
77 
78  /* get the main header */
79  if (avio_read(pb, preamble, RoQ_CHUNK_PREAMBLE_SIZE) !=
81  return AVERROR(EIO);
82  roq->frame_rate = AV_RL16(&preamble[6]);
83 
84  /* init private context parameters */
85  roq->width = roq->height = roq->audio_channels = roq->video_pts =
86  roq->audio_frame_count = 0;
87  roq->audio_stream_index = -1;
88  roq->video_stream_index = -1;
89 
90  s->ctx_flags |= AVFMTCTX_NOHEADER;
91 
92  return 0;
93 }
94 
96  AVPacket *pkt)
97 {
98  RoqDemuxContext *roq = s->priv_data;
99  AVIOContext *pb = s->pb;
100  int ret = 0;
101  unsigned int chunk_size;
102  unsigned int chunk_type;
103  unsigned int codebook_size;
104  unsigned char preamble[RoQ_CHUNK_PREAMBLE_SIZE];
105  int packet_read = 0;
106  int64_t codebook_offset;
107 
108  while (!packet_read) {
109 
110  if (avio_feof(s->pb))
111  return AVERROR_EOF;
112 
113  /* get the next chunk preamble */
114  if ((ret = avio_read(pb, preamble, RoQ_CHUNK_PREAMBLE_SIZE)) !=
116  return AVERROR(EIO);
117 
118  chunk_type = AV_RL16(&preamble[0]);
119  chunk_size = AV_RL32(&preamble[2]);
120  if(chunk_size > INT_MAX)
121  return AVERROR_INVALIDDATA;
122 
123  chunk_size = ffio_limit(pb, chunk_size);
124 
125  switch (chunk_type) {
126 
127  case RoQ_INFO:
128  if (roq->video_stream_index == -1) {
130  if (!st)
131  return AVERROR(ENOMEM);
132  avpriv_set_pts_info(st, 63, 1, roq->frame_rate);
133  roq->video_stream_index = st->index;
136  st->codecpar->codec_tag = 0; /* no fourcc */
137 
139  return AVERROR(EIO);
140  st->codecpar->width = roq->width = AV_RL16(preamble);
141  st->codecpar->height = roq->height = AV_RL16(preamble + 2);
142  break;
143  }
144  /* don't care about this chunk anymore */
146  break;
147 
148  case RoQ_QUAD_CODEBOOK:
149  if (roq->video_stream_index < 0)
150  return AVERROR_INVALIDDATA;
151  /* packet needs to contain both this codebook and next VQ chunk */
152  codebook_offset = avio_tell(pb) - RoQ_CHUNK_PREAMBLE_SIZE;
153  codebook_size = chunk_size;
154  avio_skip(pb, codebook_size);
155  if (avio_read(pb, preamble, RoQ_CHUNK_PREAMBLE_SIZE) !=
157  return AVERROR(EIO);
158  chunk_size = AV_RL32(&preamble[2]) + RoQ_CHUNK_PREAMBLE_SIZE * 2 +
159  codebook_size;
160 
161  if (chunk_size > INT_MAX)
162  return AVERROR_INVALIDDATA;
163 
164  /* rewind */
165  avio_seek(pb, codebook_offset, SEEK_SET);
166 
167  /* load up the packet */
168  ret= av_get_packet(pb, pkt, chunk_size);
169  if (ret != chunk_size)
170  return AVERROR(EIO);
172  pkt->pts = roq->video_pts++;
173 
174  packet_read = 1;
175  break;
176 
177  case RoQ_SOUND_MONO:
178  case RoQ_SOUND_STEREO:
179  if (roq->audio_stream_index == -1) {
181  if (!st)
182  return AVERROR(ENOMEM);
184  roq->audio_stream_index = st->index;
187  st->codecpar->codec_tag = 0; /* no tag */
188  if (chunk_type == RoQ_SOUND_STEREO) {
190  } else {
192  }
199  }
200  case RoQ_QUAD_VQ:
201  if (chunk_type == RoQ_QUAD_VQ) {
202  if (roq->video_stream_index < 0)
203  return AVERROR_INVALIDDATA;
204  }
205 
206  /* load up the packet */
207  ret = av_new_packet(pkt, chunk_size + RoQ_CHUNK_PREAMBLE_SIZE);
208  if (ret < 0)
209  return ret;
210  /* copy over preamble */
211  memcpy(pkt->data, preamble, RoQ_CHUNK_PREAMBLE_SIZE);
212 
213  if (chunk_type == RoQ_QUAD_VQ) {
215  pkt->pts = roq->video_pts++;
216  } else {
218  pkt->pts = roq->audio_frame_count;
219  roq->audio_frame_count += (chunk_size / roq->audio_channels);
220  }
221 
222  pkt->pos= avio_tell(pb);
224  chunk_size);
225  if (ret != chunk_size) {
226  return AVERROR(EIO);
227  }
228 
229  packet_read = 1;
230  break;
231 
232  default:
233  av_log(s, AV_LOG_ERROR, " unknown RoQ chunk (%04X)\n", chunk_type);
234  return AVERROR_INVALIDDATA;
235  }
236  }
237 
238  return ret;
239 }
240 
242  .p.name = "roq",
243  .p.long_name = NULL_IF_CONFIG_SMALL("id RoQ"),
244  .priv_data_size = sizeof(RoqDemuxContext),
248 };
RoqDemuxContext::audio_channels
int audio_channels
Definition: idroqdec.c:53
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
AV_CHANNEL_LAYOUT_STEREO
#define AV_CHANNEL_LAYOUT_STEREO
Definition: channel_layout.h:379
RoqDemuxContext::width
int width
Definition: idroqdec.c:51
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
int64_t
long long int64_t
Definition: coverity.c:34
AVPacket::data
uint8_t * data
Definition: packet.h:522
AVCodecParameters::codec_tag
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition: codec_par.h:59
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:313
RoqDemuxContext
Definition: idroqdec.c:48
ff_roq_demuxer
const FFInputFormat ff_roq_demuxer
Definition: idroqdec.c:241
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
RoQ_MAGIC_NUMBER
#define RoQ_MAGIC_NUMBER
Definition: idroqdec.c:37
avio_tell
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:494
RoqDemuxContext::audio_stream_index
int audio_stream_index
Definition: idroqdec.c:56
RoQ_INFO
#define RoQ_INFO
Definition: idroqdec.c:42
pkt
AVPacket * pkt
Definition: movenc.c:59
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
read_packet
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_read_callback.c:41
roq_read_packet
static int roq_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: idroqdec.c:95
intreadwrite.h
s
#define s(width, name)
Definition: cbs_vp9.c:198
av_new_packet
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:98
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
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
AVCodecParameters::width
int width
Video only.
Definition: codec_par.h:134
RoqDemuxContext::height
int height
Definition: idroqdec.c:52
AV_RL16
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_RL16
Definition: bytestream.h:94
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
NULL
#define NULL
Definition: coverity.c:32
AVFMTCTX_NOHEADER
#define AVFMTCTX_NOHEADER
signal that no header is present (streams are added dynamically)
Definition: avformat.h:1206
RoqDemuxContext::video_pts
int64_t video_pts
Definition: idroqdec.c:58
AVProbeData
This structure contains the data a format has to probe a file.
Definition: avformat.h:451
AV_CODEC_ID_ROQ_DPCM
@ AV_CODEC_ID_ROQ_DPCM
Definition: codec_id.h:429
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
RoQ_QUAD_CODEBOOK
#define RoQ_QUAD_CODEBOOK
Definition: idroqdec.c:43
AVIOContext
Bytestream IO Context.
Definition: avio.h:160
AV_CODEC_ID_ROQ
@ AV_CODEC_ID_ROQ
Definition: codec_id.h:90
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
AVChannelLayout
An AVChannelLayout holds information about the channel layout of audio data.
Definition: channel_layout.h:303
FFInputFormat::p
AVInputFormat p
The public AVInputFormat.
Definition: demux.h:41
ffio_limit
int ffio_limit(AVIOContext *s, int size)
Definition: aviobuf.c:1060
AVPacket::pts
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:515
avio_internal.h
AVCodecParameters::height
int height
Definition: codec_par.h:135
AVCodecParameters::block_align
int block_align
Audio only.
Definition: codec_par.h:191
demux.h
roq_read_header
static int roq_read_header(AVFormatContext *s)
Definition: idroqdec.c:72
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
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
roq_probe
static int roq_probe(const AVProbeData *p)
Definition: idroqdec.c:63
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
channel_layout.h
RoqDemuxContext::frame_rate
int frame_rate
Definition: idroqdec.c:50
RoQ_CHUNK_PREAMBLE_SIZE
#define RoQ_CHUNK_PREAMBLE_SIZE
Definition: idroqdec.c:38
avio_read
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:611
AVPacket::stream_index
int stream_index
Definition: packet.h:524
avio_skip
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:317
RoQ_QUAD_VQ
#define RoQ_QUAD_VQ
Definition: idroqdec.c:44
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
read_probe
static int read_probe(const AVProbeData *p)
Definition: cdg.c:30
AVCodecParameters::bits_per_coded_sample
int bits_per_coded_sample
The number of bits per sample in the codedwords.
Definition: codec_par.h:110
AV_CHANNEL_LAYOUT_MONO
#define AV_CHANNEL_LAYOUT_MONO
Definition: channel_layout.h:378
RoQ_SOUND_STEREO
#define RoQ_SOUND_STEREO
Definition: idroqdec.c:46
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
RoqDemuxContext::video_stream_index
int video_stream_index
Definition: idroqdec.c:55
AVPacket::pos
int64_t pos
byte position in stream, -1 if unknown
Definition: packet.h:542
FFInputFormat
Definition: demux.h:37
RoQ_SOUND_MONO
#define RoQ_SOUND_MONO
Definition: idroqdec.c:45
AVCodecParameters::bit_rate
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
Definition: codec_par.h:97
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
RoQ_AUDIO_SAMPLE_RATE
#define RoQ_AUDIO_SAMPLE_RATE
Definition: idroqdec.c:39
RoqDemuxContext::audio_frame_count
unsigned int audio_frame_count
Definition: idroqdec.c:59
avio_feof
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition: aviobuf.c:345