ffmpeg / libavformat / rmdec.c @ a2704c97
History | View | Annotate | Download (30.7 KB)
1 |
/*
|
---|---|
2 |
* "Real" compatible demuxer.
|
3 |
* Copyright (c) 2000, 2001 Fabrice Bellard
|
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/avstring.h" |
23 |
#include "libavutil/intreadwrite.h" |
24 |
#include "avformat.h" |
25 |
#include "riff.h" |
26 |
#include "rm.h" |
27 |
|
28 |
struct RMStream {
|
29 |
AVPacket pkt; ///< place to store merged video frame / reordered audio data
|
30 |
int videobufsize; ///< current assembled frame size |
31 |
int videobufpos; ///< position for the next slice in the video buffer |
32 |
int curpic_num; ///< picture number of current frame |
33 |
int cur_slice, slices;
|
34 |
int64_t pktpos; ///< first slice position in file
|
35 |
/// Audio descrambling matrix parameters
|
36 |
int64_t audiotimestamp; ///< Audio packet timestamp
|
37 |
int sub_packet_cnt; // Subpacket counter, used while reading |
38 |
int sub_packet_size, sub_packet_h, coded_framesize; ///< Descrambling parameters from container |
39 |
int audio_framesize; /// Audio frame size from container |
40 |
int sub_packet_lengths[16]; /// Length of each subpacket |
41 |
}; |
42 |
|
43 |
typedef struct { |
44 |
int nb_packets;
|
45 |
int old_format;
|
46 |
int current_stream;
|
47 |
int remaining_len;
|
48 |
int audio_stream_num; ///< Stream number for audio packets |
49 |
int audio_pkt_cnt; ///< Output packet counter |
50 |
} RMDemuxContext; |
51 |
|
52 |
static const unsigned char sipr_swaps[38][2] = { |
53 |
{ 0, 63 }, { 1, 22 }, { 2, 44 }, { 3, 90 }, |
54 |
{ 5, 81 }, { 7, 31 }, { 8, 86 }, { 9, 58 }, |
55 |
{ 10, 36 }, { 12, 68 }, { 13, 39 }, { 14, 73 }, |
56 |
{ 15, 53 }, { 16, 69 }, { 17, 57 }, { 19, 88 }, |
57 |
{ 20, 34 }, { 21, 71 }, { 24, 46 }, { 25, 94 }, |
58 |
{ 26, 54 }, { 28, 75 }, { 29, 50 }, { 32, 70 }, |
59 |
{ 33, 92 }, { 35, 74 }, { 38, 85 }, { 40, 56 }, |
60 |
{ 42, 87 }, { 43, 65 }, { 45, 59 }, { 48, 79 }, |
61 |
{ 49, 93 }, { 51, 89 }, { 55, 95 }, { 61, 76 }, |
62 |
{ 67, 83 }, { 77, 80 } |
63 |
}; |
64 |
|
65 |
const unsigned char ff_sipr_subpk_size[4] = { 29, 19, 37, 20 }; |
66 |
|
67 |
static inline void get_strl(AVIOContext *pb, char *buf, int buf_size, int len) |
68 |
{ |
69 |
int i;
|
70 |
char *q, r;
|
71 |
|
72 |
q = buf; |
73 |
for(i=0;i<len;i++) { |
74 |
r = avio_r8(pb); |
75 |
if (i < buf_size - 1) |
76 |
*q++ = r; |
77 |
} |
78 |
if (buf_size > 0) *q = '\0'; |
79 |
} |
80 |
|
81 |
static void get_str8(AVIOContext *pb, char *buf, int buf_size) |
82 |
{ |
83 |
get_strl(pb, buf, buf_size, avio_r8(pb)); |
84 |
} |
85 |
|
86 |
static int rm_read_extradata(AVIOContext *pb, AVCodecContext *avctx, unsigned size) |
87 |
{ |
88 |
if (size >= 1<<24) |
89 |
return -1; |
90 |
avctx->extradata = av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE); |
91 |
if (!avctx->extradata)
|
92 |
return AVERROR(ENOMEM);
|
93 |
avctx->extradata_size = avio_read(pb, avctx->extradata, size); |
94 |
memset(avctx->extradata + avctx->extradata_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
|
95 |
if (avctx->extradata_size != size)
|
96 |
return AVERROR(EIO);
|
97 |
return 0; |
98 |
} |
99 |
|
100 |
static void rm_read_metadata(AVFormatContext *s, int wide) |
101 |
{ |
102 |
char buf[1024]; |
103 |
int i;
|
104 |
for (i=0; i<FF_ARRAY_ELEMS(ff_rm_metadata); i++) { |
105 |
int len = wide ? avio_rb16(s->pb) : avio_r8(s->pb);
|
106 |
get_strl(s->pb, buf, sizeof(buf), len);
|
107 |
av_metadata_set2(&s->metadata, ff_rm_metadata[i], buf, 0);
|
108 |
} |
109 |
} |
110 |
|
111 |
RMStream *ff_rm_alloc_rmstream (void)
|
112 |
{ |
113 |
RMStream *rms = av_mallocz(sizeof(RMStream));
|
114 |
rms->curpic_num = -1;
|
115 |
return rms;
|
116 |
} |
117 |
|
118 |
void ff_rm_free_rmstream (RMStream *rms)
|
119 |
{ |
120 |
av_free_packet(&rms->pkt); |
121 |
} |
122 |
|
123 |
static int rm_read_audio_stream_info(AVFormatContext *s, AVIOContext *pb, |
124 |
AVStream *st, RMStream *ast, int read_all)
|
125 |
{ |
126 |
char buf[256]; |
127 |
uint32_t version; |
128 |
int ret;
|
129 |
|
130 |
/* ra type header */
|
131 |
version = avio_rb16(pb); /* version */
|
132 |
if (version == 3) { |
133 |
int header_size = avio_rb16(pb);
|
134 |
int64_t startpos = avio_tell(pb); |
135 |
avio_seek(pb, 14, SEEK_CUR);
|
136 |
rm_read_metadata(s, 0);
|
137 |
if ((startpos + header_size) >= avio_tell(pb) + 2) { |
138 |
// fourcc (should always be "lpcJ")
|
139 |
avio_r8(pb); |
140 |
get_str8(pb, buf, sizeof(buf));
|
141 |
} |
142 |
// Skip extra header crap (this should never happen)
|
143 |
if ((startpos + header_size) > avio_tell(pb))
|
144 |
avio_seek(pb, header_size + startpos - avio_tell(pb), SEEK_CUR); |
145 |
st->codec->sample_rate = 8000;
|
146 |
st->codec->channels = 1;
|
147 |
st->codec->codec_type = AVMEDIA_TYPE_AUDIO; |
148 |
st->codec->codec_id = CODEC_ID_RA_144; |
149 |
} else {
|
150 |
int flavor, sub_packet_h, coded_framesize, sub_packet_size;
|
151 |
int codecdata_length;
|
152 |
/* old version (4) */
|
153 |
avio_seek(pb, 2, SEEK_CUR); /* unused */ |
154 |
avio_rb32(pb); /* .ra4 */
|
155 |
avio_rb32(pb); /* data size */
|
156 |
avio_rb16(pb); /* version2 */
|
157 |
avio_rb32(pb); /* header size */
|
158 |
flavor= avio_rb16(pb); /* add codec info / flavor */
|
159 |
ast->coded_framesize = coded_framesize = avio_rb32(pb); /* coded frame size */
|
160 |
avio_rb32(pb); /* ??? */
|
161 |
avio_rb32(pb); /* ??? */
|
162 |
avio_rb32(pb); /* ??? */
|
163 |
ast->sub_packet_h = sub_packet_h = avio_rb16(pb); /* 1 */
|
164 |
st->codec->block_align= avio_rb16(pb); /* frame size */
|
165 |
ast->sub_packet_size = sub_packet_size = avio_rb16(pb); /* sub packet size */
|
166 |
avio_rb16(pb); /* ??? */
|
167 |
if (version == 5) { |
168 |
avio_rb16(pb); avio_rb16(pb); avio_rb16(pb); |
169 |
} |
170 |
st->codec->sample_rate = avio_rb16(pb); |
171 |
avio_rb32(pb); |
172 |
st->codec->channels = avio_rb16(pb); |
173 |
if (version == 5) { |
174 |
avio_rb32(pb); |
175 |
avio_read(pb, buf, 4);
|
176 |
buf[4] = 0; |
177 |
} else {
|
178 |
get_str8(pb, buf, sizeof(buf)); /* desc */ |
179 |
get_str8(pb, buf, sizeof(buf)); /* desc */ |
180 |
} |
181 |
st->codec->codec_type = AVMEDIA_TYPE_AUDIO; |
182 |
st->codec->codec_tag = AV_RL32(buf); |
183 |
st->codec->codec_id = ff_codec_get_id(ff_rm_codec_tags, |
184 |
st->codec->codec_tag); |
185 |
switch (st->codec->codec_id) {
|
186 |
case CODEC_ID_AC3:
|
187 |
st->need_parsing = AVSTREAM_PARSE_FULL; |
188 |
break;
|
189 |
case CODEC_ID_RA_288:
|
190 |
st->codec->extradata_size= 0;
|
191 |
ast->audio_framesize = st->codec->block_align; |
192 |
st->codec->block_align = coded_framesize; |
193 |
|
194 |
if(ast->audio_framesize >= UINT_MAX / sub_packet_h){
|
195 |
av_log(s, AV_LOG_ERROR, "ast->audio_framesize * sub_packet_h too large\n");
|
196 |
return -1; |
197 |
} |
198 |
|
199 |
av_new_packet(&ast->pkt, ast->audio_framesize * sub_packet_h); |
200 |
break;
|
201 |
case CODEC_ID_COOK:
|
202 |
case CODEC_ID_ATRAC3:
|
203 |
case CODEC_ID_SIPR:
|
204 |
avio_rb16(pb); avio_r8(pb); |
205 |
if (version == 5) |
206 |
avio_r8(pb); |
207 |
codecdata_length = avio_rb32(pb); |
208 |
if(codecdata_length + FF_INPUT_BUFFER_PADDING_SIZE <= (unsigned)codecdata_length){ |
209 |
av_log(s, AV_LOG_ERROR, "codecdata_length too large\n");
|
210 |
return -1; |
211 |
} |
212 |
|
213 |
ast->audio_framesize = st->codec->block_align; |
214 |
if (st->codec->codec_id == CODEC_ID_SIPR) {
|
215 |
if (flavor > 3) { |
216 |
av_log(s, AV_LOG_ERROR, "bad SIPR file flavor %d\n",
|
217 |
flavor); |
218 |
return -1; |
219 |
} |
220 |
st->codec->block_align = ff_sipr_subpk_size[flavor]; |
221 |
} else {
|
222 |
if(sub_packet_size <= 0){ |
223 |
av_log(s, AV_LOG_ERROR, "sub_packet_size is invalid\n");
|
224 |
return -1; |
225 |
} |
226 |
st->codec->block_align = ast->sub_packet_size; |
227 |
} |
228 |
if ((ret = rm_read_extradata(pb, st->codec, codecdata_length)) < 0) |
229 |
return ret;
|
230 |
|
231 |
if(ast->audio_framesize >= UINT_MAX / sub_packet_h){
|
232 |
av_log(s, AV_LOG_ERROR, "rm->audio_framesize * sub_packet_h too large\n");
|
233 |
return -1; |
234 |
} |
235 |
|
236 |
av_new_packet(&ast->pkt, ast->audio_framesize * sub_packet_h); |
237 |
break;
|
238 |
case CODEC_ID_AAC:
|
239 |
avio_rb16(pb); avio_r8(pb); |
240 |
if (version == 5) |
241 |
avio_r8(pb); |
242 |
codecdata_length = avio_rb32(pb); |
243 |
if(codecdata_length + FF_INPUT_BUFFER_PADDING_SIZE <= (unsigned)codecdata_length){ |
244 |
av_log(s, AV_LOG_ERROR, "codecdata_length too large\n");
|
245 |
return -1; |
246 |
} |
247 |
if (codecdata_length >= 1) { |
248 |
avio_r8(pb); |
249 |
if ((ret = rm_read_extradata(pb, st->codec, codecdata_length - 1)) < 0) |
250 |
return ret;
|
251 |
} |
252 |
break;
|
253 |
default:
|
254 |
av_strlcpy(st->codec->codec_name, buf, sizeof(st->codec->codec_name));
|
255 |
} |
256 |
if (read_all) {
|
257 |
avio_r8(pb); |
258 |
avio_r8(pb); |
259 |
avio_r8(pb); |
260 |
rm_read_metadata(s, 0);
|
261 |
} |
262 |
} |
263 |
return 0; |
264 |
} |
265 |
|
266 |
int
|
267 |
ff_rm_read_mdpr_codecdata (AVFormatContext *s, AVIOContext *pb, |
268 |
AVStream *st, RMStream *rst, int codec_data_size)
|
269 |
{ |
270 |
unsigned int v; |
271 |
int size;
|
272 |
int64_t codec_pos; |
273 |
int ret;
|
274 |
|
275 |
av_set_pts_info(st, 64, 1, 1000); |
276 |
codec_pos = avio_tell(pb); |
277 |
v = avio_rb32(pb); |
278 |
if (v == MKTAG(0xfd, 'a', 'r', '.')) { |
279 |
/* ra type header */
|
280 |
if (rm_read_audio_stream_info(s, pb, st, rst, 0)) |
281 |
return -1; |
282 |
} else {
|
283 |
int fps, fps2;
|
284 |
if (avio_rl32(pb) != MKTAG('V', 'I', 'D', 'O')) { |
285 |
fail1:
|
286 |
av_log(st->codec, AV_LOG_ERROR, "Unsupported video codec\n");
|
287 |
goto skip;
|
288 |
} |
289 |
st->codec->codec_tag = avio_rl32(pb); |
290 |
st->codec->codec_id = ff_codec_get_id(ff_rm_codec_tags, |
291 |
st->codec->codec_tag); |
292 |
// av_log(s, AV_LOG_DEBUG, "%X %X\n", st->codec->codec_tag, MKTAG('R', 'V', '2', '0'));
|
293 |
if (st->codec->codec_id == CODEC_ID_NONE)
|
294 |
goto fail1;
|
295 |
st->codec->width = avio_rb16(pb); |
296 |
st->codec->height = avio_rb16(pb); |
297 |
st->codec->time_base.num= 1;
|
298 |
fps= avio_rb16(pb); |
299 |
st->codec->codec_type = AVMEDIA_TYPE_VIDEO; |
300 |
avio_rb32(pb); |
301 |
fps2= avio_rb16(pb); |
302 |
avio_rb16(pb); |
303 |
|
304 |
if ((ret = rm_read_extradata(pb, st->codec, codec_data_size - (avio_tell(pb) - codec_pos))) < 0) |
305 |
return ret;
|
306 |
|
307 |
// av_log(s, AV_LOG_DEBUG, "fps= %d fps2= %d\n", fps, fps2);
|
308 |
st->codec->time_base.den = fps * st->codec->time_base.num; |
309 |
//XXX: do we really need that?
|
310 |
switch(st->codec->extradata[4]>>4){ |
311 |
case 1: st->codec->codec_id = CODEC_ID_RV10; break; |
312 |
case 2: st->codec->codec_id = CODEC_ID_RV20; break; |
313 |
case 3: st->codec->codec_id = CODEC_ID_RV30; break; |
314 |
case 4: st->codec->codec_id = CODEC_ID_RV40; break; |
315 |
default:
|
316 |
av_log(st->codec, AV_LOG_ERROR, "extra:%02X %02X %02X %02X %02X\n", st->codec->extradata[0], st->codec->extradata[1], st->codec->extradata[2], st->codec->extradata[3], st->codec->extradata[4]); |
317 |
goto fail1;
|
318 |
} |
319 |
} |
320 |
|
321 |
skip:
|
322 |
/* skip codec info */
|
323 |
size = avio_tell(pb) - codec_pos; |
324 |
avio_seek(pb, codec_data_size - size, SEEK_CUR); |
325 |
|
326 |
return 0; |
327 |
} |
328 |
|
329 |
/** this function assumes that the demuxer has already seeked to the start
|
330 |
* of the INDX chunk, and will bail out if not. */
|
331 |
static int rm_read_index(AVFormatContext *s) |
332 |
{ |
333 |
AVIOContext *pb = s->pb; |
334 |
unsigned int size, n_pkts, str_id, next_off, n, pos, pts; |
335 |
AVStream *st; |
336 |
|
337 |
do {
|
338 |
if (avio_rl32(pb) != MKTAG('I','N','D','X')) |
339 |
return -1; |
340 |
size = avio_rb32(pb); |
341 |
if (size < 20) |
342 |
return -1; |
343 |
avio_seek(pb, 2, SEEK_CUR);
|
344 |
n_pkts = avio_rb32(pb); |
345 |
str_id = avio_rb16(pb); |
346 |
next_off = avio_rb32(pb); |
347 |
for (n = 0; n < s->nb_streams; n++) |
348 |
if (s->streams[n]->id == str_id) {
|
349 |
st = s->streams[n]; |
350 |
break;
|
351 |
} |
352 |
if (n == s->nb_streams)
|
353 |
goto skip;
|
354 |
|
355 |
for (n = 0; n < n_pkts; n++) { |
356 |
avio_seek(pb, 2, SEEK_CUR);
|
357 |
pts = avio_rb32(pb); |
358 |
pos = avio_rb32(pb); |
359 |
avio_seek(pb, 4, SEEK_CUR); /* packet no. */ |
360 |
|
361 |
av_add_index_entry(st, pos, pts, 0, 0, AVINDEX_KEYFRAME); |
362 |
} |
363 |
|
364 |
skip:
|
365 |
if (next_off && avio_tell(pb) != next_off &&
|
366 |
avio_seek(pb, next_off, SEEK_SET) < 0)
|
367 |
return -1; |
368 |
} while (next_off);
|
369 |
|
370 |
return 0; |
371 |
} |
372 |
|
373 |
static int rm_read_header_old(AVFormatContext *s, AVFormatParameters *ap) |
374 |
{ |
375 |
RMDemuxContext *rm = s->priv_data; |
376 |
AVStream *st; |
377 |
|
378 |
rm->old_format = 1;
|
379 |
st = av_new_stream(s, 0);
|
380 |
if (!st)
|
381 |
return -1; |
382 |
st->priv_data = ff_rm_alloc_rmstream(); |
383 |
return rm_read_audio_stream_info(s, s->pb, st, st->priv_data, 1); |
384 |
} |
385 |
|
386 |
static int rm_read_header(AVFormatContext *s, AVFormatParameters *ap) |
387 |
{ |
388 |
RMDemuxContext *rm = s->priv_data; |
389 |
AVStream *st; |
390 |
AVIOContext *pb = s->pb; |
391 |
unsigned int tag; |
392 |
int tag_size;
|
393 |
unsigned int start_time, duration; |
394 |
unsigned int data_off = 0, indx_off = 0; |
395 |
char buf[128]; |
396 |
int flags = 0; |
397 |
|
398 |
tag = avio_rl32(pb); |
399 |
if (tag == MKTAG('.', 'r', 'a', 0xfd)) { |
400 |
/* very old .ra format */
|
401 |
return rm_read_header_old(s, ap);
|
402 |
} else if (tag != MKTAG('.', 'R', 'M', 'F')) { |
403 |
return AVERROR(EIO);
|
404 |
} |
405 |
|
406 |
avio_rb32(pb); /* header size */
|
407 |
avio_rb16(pb); |
408 |
avio_rb32(pb); |
409 |
avio_rb32(pb); /* number of headers */
|
410 |
|
411 |
for(;;) {
|
412 |
if (url_feof(pb))
|
413 |
return -1; |
414 |
tag = avio_rl32(pb); |
415 |
tag_size = avio_rb32(pb); |
416 |
avio_rb16(pb); |
417 |
#if 0
|
418 |
printf("tag=%c%c%c%c (%08x) size=%d\n",
|
419 |
(tag) & 0xff,
|
420 |
(tag >> 8) & 0xff,
|
421 |
(tag >> 16) & 0xff,
|
422 |
(tag >> 24) & 0xff,
|
423 |
tag,
|
424 |
tag_size);
|
425 |
#endif
|
426 |
if (tag_size < 10 && tag != MKTAG('D', 'A', 'T', 'A')) |
427 |
return -1; |
428 |
switch(tag) {
|
429 |
case MKTAG('P', 'R', 'O', 'P'): |
430 |
/* file header */
|
431 |
avio_rb32(pb); /* max bit rate */
|
432 |
avio_rb32(pb); /* avg bit rate */
|
433 |
avio_rb32(pb); /* max packet size */
|
434 |
avio_rb32(pb); /* avg packet size */
|
435 |
avio_rb32(pb); /* nb packets */
|
436 |
avio_rb32(pb); /* duration */
|
437 |
avio_rb32(pb); /* preroll */
|
438 |
indx_off = avio_rb32(pb); /* index offset */
|
439 |
data_off = avio_rb32(pb); /* data offset */
|
440 |
avio_rb16(pb); /* nb streams */
|
441 |
flags = avio_rb16(pb); /* flags */
|
442 |
break;
|
443 |
case MKTAG('C', 'O', 'N', 'T'): |
444 |
rm_read_metadata(s, 1);
|
445 |
break;
|
446 |
case MKTAG('M', 'D', 'P', 'R'): |
447 |
st = av_new_stream(s, 0);
|
448 |
if (!st)
|
449 |
return AVERROR(ENOMEM);
|
450 |
st->id = avio_rb16(pb); |
451 |
avio_rb32(pb); /* max bit rate */
|
452 |
st->codec->bit_rate = avio_rb32(pb); /* bit rate */
|
453 |
avio_rb32(pb); /* max packet size */
|
454 |
avio_rb32(pb); /* avg packet size */
|
455 |
start_time = avio_rb32(pb); /* start time */
|
456 |
avio_rb32(pb); /* preroll */
|
457 |
duration = avio_rb32(pb); /* duration */
|
458 |
st->start_time = start_time; |
459 |
st->duration = duration; |
460 |
get_str8(pb, buf, sizeof(buf)); /* desc */ |
461 |
get_str8(pb, buf, sizeof(buf)); /* mimetype */ |
462 |
st->codec->codec_type = AVMEDIA_TYPE_DATA; |
463 |
st->priv_data = ff_rm_alloc_rmstream(); |
464 |
if (ff_rm_read_mdpr_codecdata(s, s->pb, st, st->priv_data,
|
465 |
avio_rb32(pb)) < 0)
|
466 |
return -1; |
467 |
break;
|
468 |
case MKTAG('D', 'A', 'T', 'A'): |
469 |
goto header_end;
|
470 |
default:
|
471 |
/* unknown tag: skip it */
|
472 |
avio_seek(pb, tag_size - 10, SEEK_CUR);
|
473 |
break;
|
474 |
} |
475 |
} |
476 |
header_end:
|
477 |
rm->nb_packets = avio_rb32(pb); /* number of packets */
|
478 |
if (!rm->nb_packets && (flags & 4)) |
479 |
rm->nb_packets = 3600 * 25; |
480 |
avio_rb32(pb); /* next data header */
|
481 |
|
482 |
if (!data_off)
|
483 |
data_off = avio_tell(pb) - 18;
|
484 |
if (indx_off && !url_is_streamed(pb) && !(s->flags & AVFMT_FLAG_IGNIDX) &&
|
485 |
avio_seek(pb, indx_off, SEEK_SET) >= 0) {
|
486 |
rm_read_index(s); |
487 |
avio_seek(pb, data_off + 18, SEEK_SET);
|
488 |
} |
489 |
|
490 |
return 0; |
491 |
} |
492 |
|
493 |
static int get_num(AVIOContext *pb, int *len) |
494 |
{ |
495 |
int n, n1;
|
496 |
|
497 |
n = avio_rb16(pb); |
498 |
(*len)-=2;
|
499 |
n &= 0x7FFF;
|
500 |
if (n >= 0x4000) { |
501 |
return n - 0x4000; |
502 |
} else {
|
503 |
n1 = avio_rb16(pb); |
504 |
(*len)-=2;
|
505 |
return (n << 16) | n1; |
506 |
} |
507 |
} |
508 |
|
509 |
/* multiple of 20 bytes for ra144 (ugly) */
|
510 |
#define RAW_PACKET_SIZE 1000 |
511 |
|
512 |
static int sync(AVFormatContext *s, int64_t *timestamp, int *flags, int *stream_index, int64_t *pos){ |
513 |
RMDemuxContext *rm = s->priv_data; |
514 |
AVIOContext *pb = s->pb; |
515 |
AVStream *st; |
516 |
uint32_t state=0xFFFFFFFF;
|
517 |
|
518 |
while(!url_feof(pb)){
|
519 |
int len, num, i;
|
520 |
*pos= avio_tell(pb) - 3;
|
521 |
if(rm->remaining_len > 0){ |
522 |
num= rm->current_stream; |
523 |
len= rm->remaining_len; |
524 |
*timestamp = AV_NOPTS_VALUE; |
525 |
*flags= 0;
|
526 |
}else{
|
527 |
state= (state<<8) + avio_r8(pb);
|
528 |
|
529 |
if(state == MKBETAG('I', 'N', 'D', 'X')){ |
530 |
int n_pkts, expected_len;
|
531 |
len = avio_rb32(pb); |
532 |
avio_seek(pb, 2, SEEK_CUR);
|
533 |
n_pkts = avio_rb32(pb); |
534 |
expected_len = 20 + n_pkts * 14; |
535 |
if (len == 20) |
536 |
/* some files don't add index entries to chunk size... */
|
537 |
len = expected_len; |
538 |
else if (len != expected_len) |
539 |
av_log(s, AV_LOG_WARNING, |
540 |
"Index size %d (%d pkts) is wrong, should be %d.\n",
|
541 |
len, n_pkts, expected_len); |
542 |
len -= 14; // we already read part of the index header |
543 |
if(len<0) |
544 |
continue;
|
545 |
goto skip;
|
546 |
} else if (state == MKBETAG('D','A','T','A')) { |
547 |
av_log(s, AV_LOG_WARNING, |
548 |
"DATA tag in middle of chunk, file may be broken.\n");
|
549 |
} |
550 |
|
551 |
if(state > (unsigned)0xFFFF || state <= 12) |
552 |
continue;
|
553 |
len=state - 12;
|
554 |
state= 0xFFFFFFFF;
|
555 |
|
556 |
num = avio_rb16(pb); |
557 |
*timestamp = avio_rb32(pb); |
558 |
avio_r8(pb); /* reserved */
|
559 |
*flags = avio_r8(pb); /* flags */
|
560 |
} |
561 |
for(i=0;i<s->nb_streams;i++) { |
562 |
st = s->streams[i]; |
563 |
if (num == st->id)
|
564 |
break;
|
565 |
} |
566 |
if (i == s->nb_streams) {
|
567 |
skip:
|
568 |
/* skip packet if unknown number */
|
569 |
avio_seek(pb, len, SEEK_CUR); |
570 |
rm->remaining_len = 0;
|
571 |
continue;
|
572 |
} |
573 |
*stream_index= i; |
574 |
|
575 |
return len;
|
576 |
} |
577 |
return -1; |
578 |
} |
579 |
|
580 |
static int rm_assemble_video_frame(AVFormatContext *s, AVIOContext *pb, |
581 |
RMDemuxContext *rm, RMStream *vst, |
582 |
AVPacket *pkt, int len, int *pseq) |
583 |
{ |
584 |
int hdr, seq, pic_num, len2, pos;
|
585 |
int type;
|
586 |
|
587 |
hdr = avio_r8(pb); len--; |
588 |
type = hdr >> 6;
|
589 |
|
590 |
if(type != 3){ // not frame as a part of packet |
591 |
seq = avio_r8(pb); len--; |
592 |
} |
593 |
if(type != 1){ // not whole frame |
594 |
len2 = get_num(pb, &len); |
595 |
pos = get_num(pb, &len); |
596 |
pic_num = avio_r8(pb); len--; |
597 |
} |
598 |
if(len<0) |
599 |
return -1; |
600 |
rm->remaining_len = len; |
601 |
if(type&1){ // frame, not slice |
602 |
if(type == 3) // frame as a part of packet |
603 |
len= len2; |
604 |
if(rm->remaining_len < len)
|
605 |
return -1; |
606 |
rm->remaining_len -= len; |
607 |
if(av_new_packet(pkt, len + 9) < 0) |
608 |
return AVERROR(EIO);
|
609 |
pkt->data[0] = 0; |
610 |
AV_WL32(pkt->data + 1, 1); |
611 |
AV_WL32(pkt->data + 5, 0); |
612 |
avio_read(pb, pkt->data + 9, len);
|
613 |
return 0; |
614 |
} |
615 |
//now we have to deal with single slice
|
616 |
|
617 |
*pseq = seq; |
618 |
if((seq & 0x7F) == 1 || vst->curpic_num != pic_num){ |
619 |
vst->slices = ((hdr & 0x3F) << 1) + 1; |
620 |
vst->videobufsize = len2 + 8*vst->slices + 1; |
621 |
av_free_packet(&vst->pkt); //FIXME this should be output.
|
622 |
if(av_new_packet(&vst->pkt, vst->videobufsize) < 0) |
623 |
return AVERROR(ENOMEM);
|
624 |
vst->videobufpos = 8*vst->slices + 1; |
625 |
vst->cur_slice = 0;
|
626 |
vst->curpic_num = pic_num; |
627 |
vst->pktpos = avio_tell(pb); |
628 |
} |
629 |
if(type == 2) |
630 |
len = FFMIN(len, pos); |
631 |
|
632 |
if(++vst->cur_slice > vst->slices)
|
633 |
return 1; |
634 |
AV_WL32(vst->pkt.data - 7 + 8*vst->cur_slice, 1); |
635 |
AV_WL32(vst->pkt.data - 3 + 8*vst->cur_slice, vst->videobufpos - 8*vst->slices - 1); |
636 |
if(vst->videobufpos + len > vst->videobufsize)
|
637 |
return 1; |
638 |
if (avio_read(pb, vst->pkt.data + vst->videobufpos, len) != len)
|
639 |
return AVERROR(EIO);
|
640 |
vst->videobufpos += len; |
641 |
rm->remaining_len-= len; |
642 |
|
643 |
if(type == 2 || (vst->videobufpos) == vst->videobufsize){ |
644 |
vst->pkt.data[0] = vst->cur_slice-1; |
645 |
*pkt= vst->pkt; |
646 |
vst->pkt.data= NULL;
|
647 |
vst->pkt.size= 0;
|
648 |
if(vst->slices != vst->cur_slice) //FIXME find out how to set slices correct from the begin |
649 |
memmove(pkt->data + 1 + 8*vst->cur_slice, pkt->data + 1 + 8*vst->slices, |
650 |
vst->videobufpos - 1 - 8*vst->slices); |
651 |
pkt->size = vst->videobufpos + 8*(vst->cur_slice - vst->slices);
|
652 |
pkt->pts = AV_NOPTS_VALUE; |
653 |
pkt->pos = vst->pktpos; |
654 |
vst->slices = 0;
|
655 |
return 0; |
656 |
} |
657 |
|
658 |
return 1; |
659 |
} |
660 |
|
661 |
static inline void |
662 |
rm_ac3_swap_bytes (AVStream *st, AVPacket *pkt) |
663 |
{ |
664 |
uint8_t *ptr; |
665 |
int j;
|
666 |
|
667 |
if (st->codec->codec_id == CODEC_ID_AC3) {
|
668 |
ptr = pkt->data; |
669 |
for (j=0;j<pkt->size;j+=2) { |
670 |
FFSWAP(int, ptr[0], ptr[1]); |
671 |
ptr += 2;
|
672 |
} |
673 |
} |
674 |
} |
675 |
|
676 |
/**
|
677 |
* Perform 4-bit block reordering for SIPR data.
|
678 |
* @todo This can be optimized, e.g. use memcpy() if data blocks are aligned
|
679 |
*/
|
680 |
void ff_rm_reorder_sipr_data(uint8_t *buf, int sub_packet_h, int framesize) |
681 |
{ |
682 |
int n, bs = sub_packet_h * framesize * 2 / 96; // nibbles per subpacket |
683 |
|
684 |
for (n = 0; n < 38; n++) { |
685 |
int j;
|
686 |
int i = bs * sipr_swaps[n][0]; |
687 |
int o = bs * sipr_swaps[n][1]; |
688 |
|
689 |
/* swap 4bit-nibbles of block 'i' with 'o' */
|
690 |
for (j = 0; j < bs; j++, i++, o++) { |
691 |
int x = (buf[i >> 1] >> (4 * (i & 1))) & 0xF, |
692 |
y = (buf[o >> 1] >> (4 * (o & 1))) & 0xF; |
693 |
|
694 |
buf[o >> 1] = (x << (4 * (o & 1))) | |
695 |
(buf[o >> 1] & (0xF << (4 * !(o & 1)))); |
696 |
buf[i >> 1] = (y << (4 * (i & 1))) | |
697 |
(buf[i >> 1] & (0xF << (4 * !(i & 1)))); |
698 |
} |
699 |
} |
700 |
} |
701 |
|
702 |
int
|
703 |
ff_rm_parse_packet (AVFormatContext *s, AVIOContext *pb, |
704 |
AVStream *st, RMStream *ast, int len, AVPacket *pkt,
|
705 |
int *seq, int flags, int64_t timestamp) |
706 |
{ |
707 |
RMDemuxContext *rm = s->priv_data; |
708 |
|
709 |
if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
|
710 |
rm->current_stream= st->id; |
711 |
if(rm_assemble_video_frame(s, pb, rm, ast, pkt, len, seq))
|
712 |
return -1; //got partial frame |
713 |
} else if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) { |
714 |
if ((st->codec->codec_id == CODEC_ID_RA_288) ||
|
715 |
(st->codec->codec_id == CODEC_ID_COOK) || |
716 |
(st->codec->codec_id == CODEC_ID_ATRAC3) || |
717 |
(st->codec->codec_id == CODEC_ID_SIPR)) { |
718 |
int x;
|
719 |
int sps = ast->sub_packet_size;
|
720 |
int cfs = ast->coded_framesize;
|
721 |
int h = ast->sub_packet_h;
|
722 |
int y = ast->sub_packet_cnt;
|
723 |
int w = ast->audio_framesize;
|
724 |
|
725 |
if (flags & 2) |
726 |
y = ast->sub_packet_cnt = 0;
|
727 |
if (!y)
|
728 |
ast->audiotimestamp = timestamp; |
729 |
|
730 |
switch(st->codec->codec_id) {
|
731 |
case CODEC_ID_RA_288:
|
732 |
for (x = 0; x < h/2; x++) |
733 |
avio_read(pb, ast->pkt.data+x*2*w+y*cfs, cfs);
|
734 |
break;
|
735 |
case CODEC_ID_ATRAC3:
|
736 |
case CODEC_ID_COOK:
|
737 |
for (x = 0; x < w/sps; x++) |
738 |
avio_read(pb, ast->pkt.data+sps*(h*x+((h+1)/2)*(y&1)+(y>>1)), sps); |
739 |
break;
|
740 |
case CODEC_ID_SIPR:
|
741 |
avio_read(pb, ast->pkt.data + y * w, w); |
742 |
break;
|
743 |
} |
744 |
|
745 |
if (++(ast->sub_packet_cnt) < h)
|
746 |
return -1; |
747 |
if (st->codec->codec_id == CODEC_ID_SIPR)
|
748 |
ff_rm_reorder_sipr_data(ast->pkt.data, h, w); |
749 |
|
750 |
ast->sub_packet_cnt = 0;
|
751 |
rm->audio_stream_num = st->index; |
752 |
rm->audio_pkt_cnt = h * w / st->codec->block_align; |
753 |
} else if (st->codec->codec_id == CODEC_ID_AAC) { |
754 |
int x;
|
755 |
rm->audio_stream_num = st->index; |
756 |
ast->sub_packet_cnt = (avio_rb16(pb) & 0xf0) >> 4; |
757 |
if (ast->sub_packet_cnt) {
|
758 |
for (x = 0; x < ast->sub_packet_cnt; x++) |
759 |
ast->sub_packet_lengths[x] = avio_rb16(pb); |
760 |
rm->audio_pkt_cnt = ast->sub_packet_cnt; |
761 |
ast->audiotimestamp = timestamp; |
762 |
} else
|
763 |
return -1; |
764 |
} else {
|
765 |
av_get_packet(pb, pkt, len); |
766 |
rm_ac3_swap_bytes(st, pkt); |
767 |
} |
768 |
} else
|
769 |
av_get_packet(pb, pkt, len); |
770 |
|
771 |
pkt->stream_index = st->index; |
772 |
|
773 |
#if 0
|
774 |
if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
|
775 |
if(st->codec->codec_id == CODEC_ID_RV20){
|
776 |
int seq= 128*(pkt->data[2]&0x7F) + (pkt->data[3]>>1);
|
777 |
av_log(s, AV_LOG_DEBUG, "%d %"PRId64" %d\n", *timestamp, *timestamp*512LL/25, seq);
|
778 |
|
779 |
seq |= (timestamp&~0x3FFF);
|
780 |
if(seq - timestamp > 0x2000) seq -= 0x4000;
|
781 |
if(seq - timestamp < -0x2000) seq += 0x4000;
|
782 |
}
|
783 |
}
|
784 |
#endif
|
785 |
|
786 |
pkt->pts= timestamp; |
787 |
if (flags & 2) |
788 |
pkt->flags |= AV_PKT_FLAG_KEY; |
789 |
|
790 |
return st->codec->codec_type == AVMEDIA_TYPE_AUDIO ? rm->audio_pkt_cnt : 0; |
791 |
} |
792 |
|
793 |
int
|
794 |
ff_rm_retrieve_cache (AVFormatContext *s, AVIOContext *pb, |
795 |
AVStream *st, RMStream *ast, AVPacket *pkt) |
796 |
{ |
797 |
RMDemuxContext *rm = s->priv_data; |
798 |
|
799 |
assert (rm->audio_pkt_cnt > 0);
|
800 |
|
801 |
if (st->codec->codec_id == CODEC_ID_AAC)
|
802 |
av_get_packet(pb, pkt, ast->sub_packet_lengths[ast->sub_packet_cnt - rm->audio_pkt_cnt]); |
803 |
else {
|
804 |
av_new_packet(pkt, st->codec->block_align); |
805 |
memcpy(pkt->data, ast->pkt.data + st->codec->block_align * //FIXME avoid this
|
806 |
(ast->sub_packet_h * ast->audio_framesize / st->codec->block_align - rm->audio_pkt_cnt), |
807 |
st->codec->block_align); |
808 |
} |
809 |
rm->audio_pkt_cnt--; |
810 |
if ((pkt->pts = ast->audiotimestamp) != AV_NOPTS_VALUE) {
|
811 |
ast->audiotimestamp = AV_NOPTS_VALUE; |
812 |
pkt->flags = AV_PKT_FLAG_KEY; |
813 |
} else
|
814 |
pkt->flags = 0;
|
815 |
pkt->stream_index = st->index; |
816 |
|
817 |
return rm->audio_pkt_cnt;
|
818 |
} |
819 |
|
820 |
static int rm_read_packet(AVFormatContext *s, AVPacket *pkt) |
821 |
{ |
822 |
RMDemuxContext *rm = s->priv_data; |
823 |
AVStream *st; |
824 |
int i, len, res, seq = 1; |
825 |
int64_t timestamp, pos; |
826 |
int flags;
|
827 |
|
828 |
for (;;) {
|
829 |
if (rm->audio_pkt_cnt) {
|
830 |
// If there are queued audio packet return them first
|
831 |
st = s->streams[rm->audio_stream_num]; |
832 |
ff_rm_retrieve_cache(s, s->pb, st, st->priv_data, pkt); |
833 |
flags = 0;
|
834 |
} else {
|
835 |
if (rm->old_format) {
|
836 |
RMStream *ast; |
837 |
|
838 |
st = s->streams[0];
|
839 |
ast = st->priv_data; |
840 |
timestamp = AV_NOPTS_VALUE; |
841 |
len = !ast->audio_framesize ? RAW_PACKET_SIZE : |
842 |
ast->coded_framesize * ast->sub_packet_h / 2;
|
843 |
flags = (seq++ == 1) ? 2 : 0; |
844 |
pos = avio_tell(s->pb); |
845 |
} else {
|
846 |
len=sync(s, ×tamp, &flags, &i, &pos); |
847 |
if (len > 0) |
848 |
st = s->streams[i]; |
849 |
} |
850 |
|
851 |
if(len<0 || url_feof(s->pb)) |
852 |
return AVERROR(EIO);
|
853 |
|
854 |
res = ff_rm_parse_packet (s, s->pb, st, st->priv_data, len, pkt, |
855 |
&seq, flags, timestamp); |
856 |
if((flags&2) && (seq&0x7F) == 1) |
857 |
av_add_index_entry(st, pos, timestamp, 0, 0, AVINDEX_KEYFRAME); |
858 |
if (res)
|
859 |
continue;
|
860 |
} |
861 |
|
862 |
if( (st->discard >= AVDISCARD_NONKEY && !(flags&2)) |
863 |
|| st->discard >= AVDISCARD_ALL){ |
864 |
av_free_packet(pkt); |
865 |
} else
|
866 |
break;
|
867 |
} |
868 |
|
869 |
return 0; |
870 |
} |
871 |
|
872 |
static int rm_read_close(AVFormatContext *s) |
873 |
{ |
874 |
int i;
|
875 |
|
876 |
for (i=0;i<s->nb_streams;i++) |
877 |
ff_rm_free_rmstream(s->streams[i]->priv_data); |
878 |
|
879 |
return 0; |
880 |
} |
881 |
|
882 |
static int rm_probe(AVProbeData *p) |
883 |
{ |
884 |
/* check file header */
|
885 |
if ((p->buf[0] == '.' && p->buf[1] == 'R' && |
886 |
p->buf[2] == 'M' && p->buf[3] == 'F' && |
887 |
p->buf[4] == 0 && p->buf[5] == 0) || |
888 |
(p->buf[0] == '.' && p->buf[1] == 'r' && |
889 |
p->buf[2] == 'a' && p->buf[3] == 0xfd)) |
890 |
return AVPROBE_SCORE_MAX;
|
891 |
else
|
892 |
return 0; |
893 |
} |
894 |
|
895 |
static int64_t rm_read_dts(AVFormatContext *s, int stream_index, |
896 |
int64_t *ppos, int64_t pos_limit) |
897 |
{ |
898 |
RMDemuxContext *rm = s->priv_data; |
899 |
int64_t pos, dts; |
900 |
int stream_index2, flags, len, h;
|
901 |
|
902 |
pos = *ppos; |
903 |
|
904 |
if(rm->old_format)
|
905 |
return AV_NOPTS_VALUE;
|
906 |
|
907 |
avio_seek(s->pb, pos, SEEK_SET); |
908 |
rm->remaining_len=0;
|
909 |
for(;;){
|
910 |
int seq=1; |
911 |
AVStream *st; |
912 |
|
913 |
len=sync(s, &dts, &flags, &stream_index2, &pos); |
914 |
if(len<0) |
915 |
return AV_NOPTS_VALUE;
|
916 |
|
917 |
st = s->streams[stream_index2]; |
918 |
if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
|
919 |
h= avio_r8(s->pb); len--; |
920 |
if(!(h & 0x40)){ |
921 |
seq = avio_r8(s->pb); len--; |
922 |
} |
923 |
} |
924 |
|
925 |
if((flags&2) && (seq&0x7F) == 1){ |
926 |
// av_log(s, AV_LOG_DEBUG, "%d %d-%d %"PRId64" %d\n", flags, stream_index2, stream_index, dts, seq);
|
927 |
av_add_index_entry(st, pos, dts, 0, 0, AVINDEX_KEYFRAME); |
928 |
if(stream_index2 == stream_index)
|
929 |
break;
|
930 |
} |
931 |
|
932 |
avio_seek(s->pb, len, SEEK_CUR); |
933 |
} |
934 |
*ppos = pos; |
935 |
return dts;
|
936 |
} |
937 |
|
938 |
AVInputFormat ff_rm_demuxer = { |
939 |
"rm",
|
940 |
NULL_IF_CONFIG_SMALL("RealMedia format"),
|
941 |
sizeof(RMDemuxContext),
|
942 |
rm_probe, |
943 |
rm_read_header, |
944 |
rm_read_packet, |
945 |
rm_read_close, |
946 |
NULL,
|
947 |
rm_read_dts, |
948 |
}; |
949 |
|
950 |
AVInputFormat ff_rdt_demuxer = { |
951 |
"rdt",
|
952 |
NULL_IF_CONFIG_SMALL("RDT demuxer"),
|
953 |
sizeof(RMDemuxContext),
|
954 |
NULL,
|
955 |
NULL,
|
956 |
NULL,
|
957 |
rm_read_close, |
958 |
}; |