FFmpeg
Loading...
Searching...
No Matches
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
33#include "avformat.h"
34#include "demux.h"
35#include "internal.h"
36#include "avio_internal.h"
37
38#define RoQ_MAGIC_NUMBER 0x1084
39#define RoQ_CHUNK_PREAMBLE_SIZE 8
40#define RoQ_AUDIO_SAMPLE_RATE 22050
41#define RoQ_CHUNKS_TO_SCAN 30
42
43#define RoQ_INFO 0x1001
44#define RoQ_QUAD_CODEBOOK 0x1002
45#define RoQ_QUAD_VQ 0x1011
46#define RoQ_SOUND_MONO 0x1020
47#define RoQ_SOUND_STEREO 0x1021
48
63
64static int roq_probe(const AVProbeData *p)
65{
66 if ((AV_RL16(&p->buf[0]) != RoQ_MAGIC_NUMBER) ||
67 (AV_RL32(&p->buf[2]) != 0xFFFFFFFF))
68 return 0;
69
70 return AVPROBE_SCORE_MAX;
71}
72
74{
75 RoqDemuxContext *roq = s->priv_data;
76 AVIOContext *pb = s->pb;
77 unsigned char preamble[RoQ_CHUNK_PREAMBLE_SIZE];
78 int ret;
79
80 /* get the main header */
81 if ((ret = ffio_read_size(pb, preamble, RoQ_CHUNK_PREAMBLE_SIZE)) < 0)
82 return ret;
83 roq->frame_rate = AV_RL16(&preamble[6]);
84
85 /* init private context parameters */
86 roq->width = roq->height = roq->audio_channels = roq->video_pts =
87 roq->audio_frame_count = 0;
88 roq->audio_stream_index = -1;
89 roq->video_stream_index = -1;
90
91 s->ctx_flags |= AVFMTCTX_NOHEADER;
92
93 return 0;
94}
95
98{
99 RoqDemuxContext *roq = s->priv_data;
100 AVIOContext *pb = s->pb;
101 int ret = 0;
102 unsigned int chunk_size;
103 unsigned int chunk_type;
104 unsigned int codebook_size;
105 unsigned char preamble[RoQ_CHUNK_PREAMBLE_SIZE];
106 int packet_read = 0;
107 int64_t codebook_offset;
108
109 while (!packet_read) {
110
111 if (avio_feof(s->pb))
112 return AVERROR_EOF;
113
114 /* get the next chunk preamble */
115 if ((ret = ffio_read_size(pb, preamble, RoQ_CHUNK_PREAMBLE_SIZE)) < 0)
116 return ret;
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
138 if ((ret = ffio_read_size(pb, preamble, RoQ_CHUNK_PREAMBLE_SIZE)) < 0)
139 return ret;
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
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 ((ret = ffio_read_size(pb, preamble, RoQ_CHUNK_PREAMBLE_SIZE)) < 0)
156 return ret;
157 chunk_size = AV_RL32(&preamble[2]) + RoQ_CHUNK_PREAMBLE_SIZE * 2 +
158 codebook_size;
159
160 if (chunk_size > INT_MAX)
161 return AVERROR_INVALIDDATA;
162
163 /* rewind */
164 avio_seek(pb, codebook_offset, SEEK_SET);
165
166 /* load up the packet */
167 ret= av_get_packet(pb, pkt, chunk_size);
168 if (ret != chunk_size)
169 return AVERROR_INVALIDDATA;
170 pkt->stream_index = roq->video_stream_index;
171 pkt->pts = roq->video_pts++;
172
173 packet_read = 1;
174 break;
175
176 case RoQ_SOUND_MONO:
177 case RoQ_SOUND_STEREO:
178 if (roq->audio_stream_index == -1) {
180 if (!st)
181 return AVERROR(ENOMEM);
183 roq->audio_stream_index = st->index;
186 st->codecpar->codec_tag = 0; /* no tag */
187 if (chunk_type == RoQ_SOUND_STEREO) {
189 } else {
191 }
198 }
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) {
214 pkt->stream_index = roq->video_stream_index;
215 pkt->pts = roq->video_pts++;
216 } else {
217 pkt->stream_index = roq->audio_stream_index;
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 < 0)
226 return ret;
227
228 packet_read = 1;
229 break;
230
231 default:
232 av_log(s, AV_LOG_ERROR, " unknown RoQ chunk (%04X)\n", chunk_type);
233 return AVERROR_INVALIDDATA;
234 }
235 }
236
237 return ret;
238}
239
241 .p.name = "roq",
242 .p.long_name = NULL_IF_CONFIG_SMALL("id RoQ"),
243 .priv_data_size = sizeof(RoqDemuxContext),
247};
const FFInputFormat ff_roq_demuxer
Definition idroqdec.c:240
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:834
Main libavformat public API header.
#define AVPROBE_SCORE_MAX
maximum score
Definition avformat.h:483
#define AVFMTCTX_NOHEADER
signal that no header is present (streams are added dynamically)
Definition avformat.h:1284
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:98
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition aviobuf.c:236
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition aviobuf.c:349
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition avio.h:494
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition aviobuf.c:321
int ffio_read_size(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition aviobuf.c:665
int ffio_limit(AVIOContext *s, int size)
Definition aviobuf.c:1064
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
#define s(width, name)
Definition cbs_vp9.c:198
static int read_probe(const AVProbeData *p)
Definition cdg.c:30
Public libavutil channel layout APIs header.
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
static AVPacket * pkt
static int read_header(FFV1Context *f, RangeCoder *c)
Definition ffv1dec.c:578
@ AV_CODEC_ID_ROQ_DPCM
Definition codec_id.h:442
@ AV_CODEC_ID_ROQ
Definition codec_id.h:88
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition packet.c:98
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
#define AV_CHANNEL_LAYOUT_STEREO
#define AV_CHANNEL_LAYOUT_MONO
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR_EOF
End of file.
Definition error.h:57
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
#define RoQ_MAGIC_NUMBER
Definition idroqdec.c:38
static int roq_read_header(AVFormatContext *s)
Definition idroqdec.c:73
static int roq_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition idroqdec.c:96
#define RoQ_AUDIO_SAMPLE_RATE
Definition idroqdec.c:40
static int roq_probe(const AVProbeData *p)
Definition idroqdec.c:64
#define RoQ_CHUNK_PREAMBLE_SIZE
Definition idroqdec.c:39
#define AV_RL32(p)
#define AV_RL16(p)
Macro definitions for various function/variable attributes.
#define av_fallthrough
Definition attributes.h:67
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
#define RoQ_QUAD_CODEBOOK
Definition roqvideo.h:52
#define RoQ_SOUND_STEREO
Definition roqvideo.h:55
#define RoQ_INFO
Definition roqvideo.h:51
#define RoQ_QUAD_VQ
Definition roqvideo.h:53
#define RoQ_SOUND_MONO
Definition roqvideo.h:54
An AVChannelLayout holds information about the channel layout of audio data.
int nb_channels
Number of channels in this layout.
int height
The height of the video frame in pixels.
Definition codec_par.h:150
int bits_per_coded_sample
The number of bits per sample in the codedwords.
Definition codec_par.h:113
AVChannelLayout ch_layout
The channel layout and number of channels.
Definition codec_par.h:207
int width
The width of the video frame in pixels.
Definition codec_par.h:143
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
Definition codec_par.h:99
enum AVMediaType codec_type
General type of the encoded data.
Definition codec_par.h:53
int block_align
The number of bytes per coded audio frame, required by some formats.
Definition codec_par.h:221
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition codec_par.h:61
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition codec_par.h:57
int sample_rate
The number of audio samples per second.
Definition codec_par.h:213
Format I/O context.
Definition avformat.h:1333
Bytestream IO Context.
Definition avio.h:160
This structure stores compressed data.
Definition packet.h:580
This structure contains the data a format has to probe a file.
Definition avformat.h:471
Stream structure.
Definition avformat.h:766
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition avformat.h:789
int index
stream index in AVFormatContext
Definition avformat.h:772
unsigned int audio_frame_count
Definition idroqdec.c:60
int audio_stream_index
Definition idroqdec.c:57
int video_stream_index
Definition idroqdec.c:56
int64_t video_pts
Definition idroqdec.c:59
#define av_log(a,...)