23
23
#include "libavutil/intreadwrite.h"
24
24
#include "libavutil/dict.h"
25
25
#include "avformat.h"
30
#define DEINT_ID_GENR MKTAG('g', 'e', 'n', 'r') ///< interleaving for Cooker/Atrac
31
#define DEINT_ID_INT0 MKTAG('I', 'n', 't', '0') ///< no interleaving needed
32
#define DEINT_ID_INT4 MKTAG('I', 'n', 't', '4') ///< interleaving for 28.8
33
#define DEINT_ID_SIPR MKTAG('s', 'i', 'p', 'r') ///< interleaving for Sipro
34
#define DEINT_ID_VBRF MKTAG('v', 'b', 'r', 'f') ///< VBR case for AAC
35
#define DEINT_ID_VBRS MKTAG('v', 'b', 'r', 's') ///< VBR case for AAC
30
38
AVPacket pkt; ///< place to store merged video frame / reordered audio data
31
39
int videobufsize; ///< current assembled frame size
39
47
int sub_packet_size, sub_packet_h, coded_framesize; ///< Descrambling parameters from container
40
48
int audio_framesize; /// Audio frame size from container
41
49
int sub_packet_lengths[16]; /// Length of each subpacket
50
int32_t deint_id; ///< deinterleaver used in audio stream
147
156
st->codec->channels = 1;
148
157
st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
149
158
st->codec->codec_id = CODEC_ID_RA_144;
159
ast->deint_id = DEINT_ID_INT0;
151
161
int flavor, sub_packet_h, coded_framesize, sub_packet_size;
152
162
int codecdata_length;
173
183
st->codec->channels = avio_rb16(pb);
174
184
if (version == 5) {
185
ast->deint_id = avio_rl32(pb);
176
186
avio_read(pb, buf, 4);
179
189
get_str8(pb, buf, sizeof(buf)); /* desc */
190
ast->deint_id = AV_RL32(buf);
180
191
get_str8(pb, buf, sizeof(buf)); /* desc */
182
193
st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
183
194
st->codec->codec_tag = AV_RL32(buf);
184
195
st->codec->codec_id = ff_codec_get_id(ff_rm_codec_tags,
185
196
st->codec->codec_tag);
186
198
switch (st->codec->codec_id) {
187
199
case CODEC_ID_AC3:
188
200
st->need_parsing = AVSTREAM_PARSE_FULL;
191
203
st->codec->extradata_size= 0;
192
204
ast->audio_framesize = st->codec->block_align;
193
205
st->codec->block_align = coded_framesize;
195
if(ast->audio_framesize >= UINT_MAX / sub_packet_h){
196
av_log(s, AV_LOG_ERROR, "ast->audio_framesize * sub_packet_h too large\n");
200
av_new_packet(&ast->pkt, ast->audio_framesize * sub_packet_h);
202
207
case CODEC_ID_COOK:
203
208
case CODEC_ID_ATRAC3:
229
234
if ((ret = rm_read_extradata(pb, st->codec, codecdata_length)) < 0)
232
if(ast->audio_framesize >= UINT_MAX / sub_packet_h){
233
av_log(s, AV_LOG_ERROR, "rm->audio_framesize * sub_packet_h too large\n");
237
av_new_packet(&ast->pkt, ast->audio_framesize * sub_packet_h);
239
237
case CODEC_ID_AAC:
240
238
avio_rb16(pb); avio_r8(pb);
255
253
av_strlcpy(st->codec->codec_name, buf, sizeof(st->codec->codec_name));
255
if (ast->deint_id == DEINT_ID_INT4 ||
256
ast->deint_id == DEINT_ID_GENR ||
257
ast->deint_id == DEINT_ID_SIPR) {
258
if (st->codec->block_align <= 0 ||
259
ast->audio_framesize * sub_packet_h > (unsigned)INT_MAX ||
260
ast->audio_framesize * sub_packet_h < st->codec->block_align)
261
return AVERROR_INVALIDDATA;
262
if (av_new_packet(&ast->pkt, ast->audio_framesize * sub_packet_h) < 0)
263
return AVERROR(ENOMEM);
265
switch (ast->deint_id) {
267
if (ast->coded_framesize > ast->audio_framesize ||
268
ast->coded_framesize * sub_packet_h > (2 + (sub_packet_h & 1)) * ast->audio_framesize)
269
return AVERROR_INVALIDDATA;
272
if (ast->sub_packet_size <= 0 ||
273
ast->sub_packet_size > ast->audio_framesize)
274
return AVERROR_INVALIDDATA;
282
av_log(NULL,0,"Unknown interleaver %X\n", ast->deint_id);
283
return AVERROR_INVALIDDATA;
273
302
int64_t codec_pos;
276
av_set_pts_info(st, 64, 1, 1000);
305
avpriv_set_pts_info(st, 64, 1, 1000);
277
306
codec_pos = avio_tell(pb);
278
307
v = avio_rb32(pb);
279
308
if (v == MKTAG(0xfd, 'a', 'r', '.')) {
293
322
// av_log(s, AV_LOG_DEBUG, "%X %X\n", st->codec->codec_tag, MKTAG('R', 'V', '2', '0'));
294
323
if (st->codec->codec_id == CODEC_ID_NONE)
296
st->codec->width = avio_rb16(pb);
325
st->codec->width = avio_rb16(pb);
297
326
st->codec->height = avio_rb16(pb);
298
st->codec->time_base.num= 1;
327
avio_skip(pb, 2); // looks like bits per sample
328
avio_skip(pb, 4); // always zero?
300
329
st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
330
st->need_parsing = AVSTREAM_PARSE_TIMESTAMPS;
305
333
if ((ret = rm_read_extradata(pb, st->codec, codec_data_size - (avio_tell(pb) - codec_pos))) < 0)
308
// av_log(s, AV_LOG_DEBUG, "fps= %d fps2= %d\n", fps, fps2);
309
st->codec->time_base.den = fps * st->codec->time_base.num;
310
//XXX: do we really need that?
311
switch(st->codec->extradata[4]>>4){
312
case 1: st->codec->codec_id = CODEC_ID_RV10; break;
313
case 2: st->codec->codec_id = CODEC_ID_RV20; break;
314
case 3: st->codec->codec_id = CODEC_ID_RV30; break;
315
case 4: st->codec->codec_id = CODEC_ID_RV40; break;
317
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]);
336
av_reduce(&st->r_frame_rate.den, &st->r_frame_rate.num,
337
0x10000, fps, (1 << 30) - 1);
338
st->avg_frame_rate = st->r_frame_rate;
374
static int rm_read_header_old(AVFormatContext *s, AVFormatParameters *ap)
393
static int rm_read_header_old(AVFormatContext *s)
376
395
RMDemuxContext *rm = s->priv_data;
379
398
rm->old_format = 1;
380
st = av_new_stream(s, 0);
399
st = avformat_new_stream(s, NULL);
383
402
st->priv_data = ff_rm_alloc_rmstream();
399
418
tag = avio_rl32(pb);
400
419
if (tag == MKTAG('.', 'r', 'a', 0xfd)) {
401
420
/* very old .ra format */
402
return rm_read_header_old(s, ap);
421
return rm_read_header_old(s);
403
422
} else if (tag != MKTAG('.', 'R', 'M', 'F')) {
404
423
return AVERROR(EIO);
443
462
rm_read_metadata(s, 1);
445
464
case MKTAG('M', 'D', 'P', 'R'):
446
st = av_new_stream(s, 0);
465
st = avformat_new_stream(s, NULL);
448
467
return AVERROR(ENOMEM);
449
468
st->id = avio_rb16(pb);
579
598
static int rm_assemble_video_frame(AVFormatContext *s, AVIOContext *pb,
580
599
RMDemuxContext *rm, RMStream *vst,
581
AVPacket *pkt, int len, int *pseq)
600
AVPacket *pkt, int len, int *pseq,
583
603
int hdr, seq, pic_num, len2, pos;
599
619
rm->remaining_len = len;
600
620
if(type&1){ // frame, not slice
601
if(type == 3) // frame as a part of packet
621
if(type == 3){ // frame as a part of packet
603
625
if(rm->remaining_len < len)
605
627
rm->remaining_len -= len;
708
730
if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
709
731
rm->current_stream= st->id;
710
if(rm_assemble_video_frame(s, pb, rm, ast, pkt, len, seq))
732
if(rm_assemble_video_frame(s, pb, rm, ast, pkt, len, seq, ×tamp))
711
733
return -1; //got partial frame
712
734
} else if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
713
if ((st->codec->codec_id == CODEC_ID_RA_288) ||
714
(st->codec->codec_id == CODEC_ID_COOK) ||
715
(st->codec->codec_id == CODEC_ID_ATRAC3) ||
716
(st->codec->codec_id == CODEC_ID_SIPR)) {
735
if ((ast->deint_id == DEINT_ID_GENR) ||
736
(ast->deint_id == DEINT_ID_INT4) ||
737
(ast->deint_id == DEINT_ID_SIPR)) {
718
739
int sps = ast->sub_packet_size;
719
740
int cfs = ast->coded_framesize;
727
748
ast->audiotimestamp = timestamp;
729
switch(st->codec->codec_id) {
730
case CODEC_ID_RA_288:
750
switch (ast->deint_id) {
731
752
for (x = 0; x < h/2; x++)
732
753
avio_read(pb, ast->pkt.data+x*2*w+y*cfs, cfs);
734
case CODEC_ID_ATRAC3:
736
756
for (x = 0; x < w/sps; x++)
737
757
avio_read(pb, ast->pkt.data+sps*(h*x+((h+1)/2)*(y&1)+(y>>1)), sps);
740
760
avio_read(pb, ast->pkt.data + y * w, w);
744
764
if (++(ast->sub_packet_cnt) < h)
746
if (st->codec->codec_id == CODEC_ID_SIPR)
766
if (ast->deint_id == DEINT_ID_SIPR)
747
767
ff_rm_reorder_sipr_data(ast->pkt.data, h, w);
749
769
ast->sub_packet_cnt = 0;
750
770
rm->audio_stream_num = st->index;
751
771
rm->audio_pkt_cnt = h * w / st->codec->block_align;
752
} else if (st->codec->codec_id == CODEC_ID_AAC) {
772
} else if ((ast->deint_id == DEINT_ID_VBRF) ||
773
(ast->deint_id == DEINT_ID_VBRS)) {
754
775
rm->audio_stream_num = st->index;
755
776
ast->sub_packet_cnt = (avio_rb16(pb) & 0xf0) >> 4;
798
819
assert (rm->audio_pkt_cnt > 0);
800
if (st->codec->codec_id == CODEC_ID_AAC)
821
if (ast->deint_id == DEINT_ID_VBRF ||
822
ast->deint_id == DEINT_ID_VBRS)
801
823
av_get_packet(pb, pkt, ast->sub_packet_lengths[ast->sub_packet_cnt - rm->audio_pkt_cnt]);
803
825
av_new_packet(pkt, st->codec->block_align);
937
959
AVInputFormat ff_rm_demuxer = {
939
NULL_IF_CONFIG_SMALL("RealMedia format"),
940
sizeof(RMDemuxContext),
961
.long_name = NULL_IF_CONFIG_SMALL("RealMedia format"),
962
.priv_data_size = sizeof(RMDemuxContext),
963
.read_probe = rm_probe,
964
.read_header = rm_read_header,
965
.read_packet = rm_read_packet,
966
.read_close = rm_read_close,
967
.read_timestamp = rm_read_dts,
949
970
AVInputFormat ff_rdt_demuxer = {
951
NULL_IF_CONFIG_SMALL("RDT demuxer"),
952
sizeof(RMDemuxContext),
972
.long_name = NULL_IF_CONFIG_SMALL("RDT demuxer"),
973
.priv_data_size = sizeof(RMDemuxContext),
974
.read_close = rm_read_close,
975
.flags = AVFMT_NOFILE,