Libav
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 Libav.
6  *
7  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
31 #include "libavutil/intreadwrite.h"
32 #include "avformat.h"
33 #include "internal.h"
34 
35 #define RoQ_MAGIC_NUMBER 0x1084
36 #define RoQ_CHUNK_PREAMBLE_SIZE 8
37 #define RoQ_AUDIO_SAMPLE_RATE 22050
38 #define RoQ_CHUNKS_TO_SCAN 30
39 
40 #define RoQ_INFO 0x1001
41 #define RoQ_QUAD_CODEBOOK 0x1002
42 #define RoQ_QUAD_VQ 0x1011
43 #define RoQ_SOUND_MONO 0x1020
44 #define RoQ_SOUND_STEREO 0x1021
45 
46 typedef struct RoqDemuxContext {
47 
49  int width;
50  int height;
52 
55 
56  int64_t video_pts;
57  unsigned int audio_frame_count;
58 
60 
61 static int roq_probe(AVProbeData *p)
62 {
63  if ((AV_RL16(&p->buf[0]) != RoQ_MAGIC_NUMBER) ||
64  (AV_RL32(&p->buf[2]) != 0xFFFFFFFF))
65  return 0;
66 
67  return AVPROBE_SCORE_MAX;
68 }
69 
71 {
72  RoqDemuxContext *roq = s->priv_data;
73  AVIOContext *pb = s->pb;
74  unsigned char preamble[RoQ_CHUNK_PREAMBLE_SIZE];
75 
76  /* get the main header */
77  if (avio_read(pb, preamble, RoQ_CHUNK_PREAMBLE_SIZE) !=
79  return AVERROR(EIO);
80  roq->frame_rate = AV_RL16(&preamble[6]);
81 
82  /* init private context parameters */
83  roq->width = roq->height = roq->audio_channels = roq->video_pts =
84  roq->audio_frame_count = 0;
85  roq->audio_stream_index = -1;
86  roq->video_stream_index = -1;
87 
89 
90  return 0;
91 }
92 
94  AVPacket *pkt)
95 {
96  RoqDemuxContext *roq = s->priv_data;
97  AVIOContext *pb = s->pb;
98  int ret = 0;
99  unsigned int chunk_size;
100  unsigned int chunk_type;
101  unsigned int codebook_size;
102  unsigned char preamble[RoQ_CHUNK_PREAMBLE_SIZE];
103  int packet_read = 0;
104  int64_t codebook_offset;
105 
106  while (!packet_read) {
107 
108  if (s->pb->eof_reached)
109  return AVERROR(EIO);
110 
111  /* get the next chunk preamble */
112  if ((ret = avio_read(pb, preamble, RoQ_CHUNK_PREAMBLE_SIZE)) !=
114  return AVERROR(EIO);
115 
116  chunk_type = AV_RL16(&preamble[0]);
117  chunk_size = AV_RL32(&preamble[2]);
118  if(chunk_size > INT_MAX)
119  return AVERROR_INVALIDDATA;
120 
121  switch (chunk_type) {
122 
123  case RoQ_INFO:
124  if (roq->video_stream_index == -1) {
126  if (!st)
127  return AVERROR(ENOMEM);
128  avpriv_set_pts_info(st, 63, 1, roq->frame_rate);
129  roq->video_stream_index = st->index;
132  st->codec->codec_tag = 0; /* no fourcc */
133 
135  return AVERROR(EIO);
136  st->codec->width = roq->width = AV_RL16(preamble);
137  st->codec->height = roq->height = AV_RL16(preamble + 2);
138  break;
139  }
140  /* don't care about this chunk anymore */
142  break;
143 
144  case RoQ_QUAD_CODEBOOK:
145  if (roq->video_stream_index < 0)
146  return AVERROR_INVALIDDATA;
147  /* packet needs to contain both this codebook and next VQ chunk */
148  codebook_offset = avio_tell(pb) - RoQ_CHUNK_PREAMBLE_SIZE;
149  codebook_size = chunk_size;
150  avio_skip(pb, codebook_size);
151  if (avio_read(pb, preamble, RoQ_CHUNK_PREAMBLE_SIZE) !=
153  return AVERROR(EIO);
154  chunk_size = AV_RL32(&preamble[2]) + RoQ_CHUNK_PREAMBLE_SIZE * 2 +
155  codebook_size;
156 
157  /* rewind */
158  avio_seek(pb, codebook_offset, SEEK_SET);
159 
160  /* load up the packet */
161  ret= av_get_packet(pb, pkt, chunk_size);
162  if (ret != chunk_size)
163  return AVERROR(EIO);
164  pkt->stream_index = roq->video_stream_index;
165  pkt->pts = roq->video_pts++;
166 
167  packet_read = 1;
168  break;
169 
170  case RoQ_SOUND_MONO:
171  case RoQ_SOUND_STEREO:
172  if (roq->audio_stream_index == -1) {
174  if (!st)
175  return AVERROR(ENOMEM);
177  roq->audio_stream_index = st->index;
180  st->codec->codec_tag = 0; /* no tag */
181  if (chunk_type == RoQ_SOUND_STEREO) {
182  st->codec->channels = 2;
184  } else {
185  st->codec->channels = 1;
187  }
188  roq->audio_channels = st->codec->channels;
190  st->codec->bits_per_coded_sample = 16;
191  st->codec->bit_rate = st->codec->channels * st->codec->sample_rate *
194  }
195  case RoQ_QUAD_VQ:
196  if (chunk_type == RoQ_QUAD_VQ) {
197  if (roq->video_stream_index < 0)
198  return AVERROR_INVALIDDATA;
199  }
200 
201  /* load up the packet */
202  if (av_new_packet(pkt, chunk_size + RoQ_CHUNK_PREAMBLE_SIZE))
203  return AVERROR(EIO);
204  /* copy over preamble */
205  memcpy(pkt->data, preamble, RoQ_CHUNK_PREAMBLE_SIZE);
206 
207  if (chunk_type == RoQ_QUAD_VQ) {
208  pkt->stream_index = roq->video_stream_index;
209  pkt->pts = roq->video_pts++;
210  } else {
211  pkt->stream_index = roq->audio_stream_index;
212  pkt->pts = roq->audio_frame_count;
213  roq->audio_frame_count += (chunk_size / roq->audio_channels);
214  }
215 
216  pkt->pos= avio_tell(pb);
217  ret = avio_read(pb, pkt->data + RoQ_CHUNK_PREAMBLE_SIZE,
218  chunk_size);
219  if (ret != chunk_size)
220  ret = AVERROR(EIO);
221 
222  packet_read = 1;
223  break;
224 
225  default:
226  av_log(s, AV_LOG_ERROR, " unknown RoQ chunk (%04X)\n", chunk_type);
227  return AVERROR_INVALIDDATA;
228  }
229  }
230 
231  return ret;
232 }
233 
235  .name = "roq",
236  .long_name = NULL_IF_CONFIG_SMALL("id RoQ"),
237  .priv_data_size = sizeof(RoqDemuxContext),
241 };
int audio_channels
Definition: idroqdec.c:51
Bytestream IO Context.
Definition: avio.h:68
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
unsigned int audio_frame_count
Definition: idroqdec.c:57
int64_t pos
byte position in stream, -1 if unknown
Definition: avcodec.h:998
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:2829
int index
stream index in AVFormatContext
Definition: avformat.h:700
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:186
static int roq_probe(AVProbeData *p)
Definition: idroqdec.c:61
int64_t video_pts
Definition: idroqdec.c:56
#define AV_RL16
Definition: intreadwrite.h:42
#define AV_CH_LAYOUT_STEREO
int ctx_flags
Flags signalling stream properties.
Definition: avformat.h:971
#define RoQ_SOUND_STEREO
Definition: idroqdec.c:44
int block_align
number of bytes per packet if constant and known or 0 Used by some WAV based audio codecs...
Definition: avcodec.h:1828
#define RoQ_MAGIC_NUMBER
Definition: idroqdec.c:35
Format I/O context.
Definition: avformat.h:922
#define AVFMTCTX_NOHEADER
signal that no header is present (streams are added dynamically)
Definition: avformat.h:901
static int roq_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: idroqdec.c:93
#define RoQ_INFO
Definition: idroqdec.c:40
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:2521
uint8_t * data
Definition: avcodec.h:973
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:117
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:219
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition: avcodec.h:2507
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:452
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:81
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:123
#define AVERROR(e)
Definition: error.h:43
int audio_stream_index
Definition: idroqdec.c:54
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:150
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:168
#define RoQ_SOUND_MONO
Definition: idroqdec.c:43
uint64_t channel_layout
Audio channel layout.
Definition: avcodec.h:1852
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:718
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:397
int bit_rate
the average bitrate
Definition: avcodec.h:1114
audio channel layout utility functions
static int read_probe(AVProbeData *pd)
Definition: jvdec.c:55
int width
picture width / height.
Definition: avcodec.h:1224
static av_always_inline int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: avio.h:210
#define AV_RL32
Definition: intreadwrite.h:146
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:544
Stream structure.
Definition: avformat.h:699
NULL
Definition: eval.c:55
enum AVMediaType codec_type
Definition: avcodec.h:1058
enum AVCodecID codec_id
Definition: avcodec.h:1067
int sample_rate
samples per second
Definition: avcodec.h:1791
AVIOContext * pb
I/O context.
Definition: avformat.h:964
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
Definition: avcodec.h:1082
static int read_packet(AVFormatContext *ctx, AVPacket *pkt)
Definition: libcdio.c:114
#define RoQ_CHUNK_PREAMBLE_SIZE
Definition: idroqdec.c:36
#define RoQ_AUDIO_SAMPLE_RATE
Definition: idroqdec.c:37
This structure contains the data a format has to probe a file.
Definition: avformat.h:395
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:404
Main libavformat public API header.
int eof_reached
true if eof reached
Definition: avio.h:96
int channels
number of audio channels
Definition: avcodec.h:1792
int video_stream_index
Definition: idroqdec.c:53
void * priv_data
Format private data.
Definition: avformat.h:950
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:525
#define RoQ_QUAD_CODEBOOK
Definition: idroqdec.c:41
int stream_index
Definition: avcodec.h:975
#define AV_CH_LAYOUT_MONO
AVInputFormat ff_roq_demuxer
Definition: idroqdec.c:234
This structure stores compressed data.
Definition: avcodec.h:950
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:966
static int roq_read_header(AVFormatContext *s)
Definition: idroqdec.c:70
#define RoQ_QUAD_VQ
Definition: idroqdec.c:42