2 * "Real" compatible demuxer.
3 * Copyright (c) 2000, 2001 Fabrice Bellard
5 * This file is part of Libav.
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.
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.
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
24 #include "libavutil/avstring.h"
25 #include "libavutil/channel_layout.h"
26 #include "libavutil/internal.h"
27 #include "libavutil/intreadwrite.h"
28 #include "libavutil/dict.h"
34 #define DEINT_ID_GENR MKTAG('g', 'e', 'n', 'r') ///< interleaving for Cooker/ATRAC
35 #define DEINT_ID_INT0 MKTAG('I', 'n', 't', '0') ///< no interleaving needed
36 #define DEINT_ID_INT4 MKTAG('I', 'n', 't', '4') ///< interleaving for 28.8
37 #define DEINT_ID_SIPR MKTAG('s', 'i', 'p', 'r') ///< interleaving for Sipro
38 #define DEINT_ID_VBRF MKTAG('v', 'b', 'r', 'f') ///< VBR case for AAC
39 #define DEINT_ID_VBRS MKTAG('v', 'b', 'r', 's') ///< VBR case for AAC
42 AVPacket pkt; ///< place to store merged video frame / reordered audio data
43 int videobufsize; ///< current assembled frame size
44 int videobufpos; ///< position for the next slice in the video buffer
45 int curpic_num; ///< picture number of current frame
46 int cur_slice, slices;
47 int64_t pktpos; ///< first slice position in file
48 /// Audio descrambling matrix parameters
49 int64_t audiotimestamp; ///< Audio packet timestamp
50 int sub_packet_cnt; // Subpacket counter, used while reading
51 int sub_packet_size, sub_packet_h, coded_framesize; ///< Descrambling parameters from container
52 int audio_framesize; /// Audio frame size from container
53 int sub_packet_lengths[16]; /// Length of each subpacket
54 int32_t deint_id; ///< deinterleaver used in audio stream
62 int audio_stream_num; ///< Stream number for audio packets
63 int audio_pkt_cnt; ///< Output packet counter
66 static inline void get_strl(AVIOContext *pb, char *buf, int buf_size, int len)
77 if (buf_size > 0) *q = '\0';
80 static void get_str8(AVIOContext *pb, char *buf, int buf_size)
82 get_strl(pb, buf, buf_size, avio_r8(pb));
85 static int rm_read_extradata(AVIOContext *pb, AVCodecContext *avctx, unsigned size)
89 avctx->extradata = av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE);
90 if (!avctx->extradata)
91 return AVERROR(ENOMEM);
92 avctx->extradata_size = avio_read(pb, avctx->extradata, size);
93 memset(avctx->extradata + avctx->extradata_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
94 if (avctx->extradata_size != size)
99 static void rm_read_metadata(AVFormatContext *s, AVIOContext *pb, int wide)
103 for (i=0; i<FF_ARRAY_ELEMS(ff_rm_metadata); i++) {
104 int len = wide ? avio_rb16(pb) : avio_r8(pb);
105 get_strl(pb, buf, sizeof(buf), len);
106 av_dict_set(&s->metadata, ff_rm_metadata[i], buf, 0);
110 RMStream *ff_rm_alloc_rmstream (void)
112 RMStream *rms = av_mallocz(sizeof(RMStream));
113 rms->curpic_num = -1;
117 void ff_rm_free_rmstream (RMStream *rms)
119 av_free_packet(&rms->pkt);
122 static int rm_read_audio_stream_info(AVFormatContext *s, AVIOContext *pb,
123 AVStream *st, RMStream *ast, int read_all)
130 version = avio_rb16(pb); /* version */
132 int header_size = avio_rb16(pb);
133 int64_t startpos = avio_tell(pb);
135 rm_read_metadata(s, pb, 0);
136 if ((startpos + header_size) >= avio_tell(pb) + 2) {
137 // fourcc (should always be "lpcJ")
139 get_str8(pb, buf, sizeof(buf));
141 // Skip extra header crap (this should never happen)
142 if ((startpos + header_size) > avio_tell(pb))
143 avio_skip(pb, header_size + startpos - avio_tell(pb));
144 st->codec->sample_rate = 8000;
145 st->codec->channels = 1;
146 st->codec->channel_layout = AV_CH_LAYOUT_MONO;
147 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
148 st->codec->codec_id = AV_CODEC_ID_RA_144;
149 ast->deint_id = DEINT_ID_INT0;
151 int flavor, sub_packet_h, coded_framesize, sub_packet_size;
152 int codecdata_length;
153 /* old version (4) */
154 avio_skip(pb, 2); /* unused */
155 avio_rb32(pb); /* .ra4 */
156 avio_rb32(pb); /* data size */
157 avio_rb16(pb); /* version2 */
158 avio_rb32(pb); /* header size */
159 flavor= avio_rb16(pb); /* add codec info / flavor */
160 ast->coded_framesize = coded_framesize = avio_rb32(pb); /* coded frame size */
161 avio_rb32(pb); /* ??? */
162 avio_rb32(pb); /* ??? */
163 avio_rb32(pb); /* ??? */
164 ast->sub_packet_h = sub_packet_h = avio_rb16(pb); /* 1 */
165 st->codec->block_align= avio_rb16(pb); /* frame size */
166 ast->sub_packet_size = sub_packet_size = avio_rb16(pb); /* sub packet size */
167 avio_rb16(pb); /* ??? */
169 avio_rb16(pb); avio_rb16(pb); avio_rb16(pb);
171 st->codec->sample_rate = avio_rb16(pb);
173 st->codec->channels = avio_rb16(pb);
175 ast->deint_id = avio_rl32(pb);
176 avio_read(pb, buf, 4);
179 get_str8(pb, buf, sizeof(buf)); /* desc */
180 ast->deint_id = AV_RL32(buf);
181 get_str8(pb, buf, sizeof(buf)); /* desc */
183 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
184 st->codec->codec_tag = AV_RL32(buf);
185 st->codec->codec_id = ff_codec_get_id(ff_rm_codec_tags,
186 st->codec->codec_tag);
188 switch (st->codec->codec_id) {
189 case AV_CODEC_ID_AC3:
190 st->need_parsing = AVSTREAM_PARSE_FULL;
192 case AV_CODEC_ID_RA_288:
193 st->codec->extradata_size= 0;
194 ast->audio_framesize = st->codec->block_align;
195 st->codec->block_align = coded_framesize;
197 case AV_CODEC_ID_COOK:
198 st->need_parsing = AVSTREAM_PARSE_HEADERS;
199 case AV_CODEC_ID_ATRAC3:
200 case AV_CODEC_ID_SIPR:
201 avio_rb16(pb); avio_r8(pb);
204 codecdata_length = avio_rb32(pb);
205 if(codecdata_length + FF_INPUT_BUFFER_PADDING_SIZE <= (unsigned)codecdata_length){
206 av_log(s, AV_LOG_ERROR, "codecdata_length too large\n");
210 ast->audio_framesize = st->codec->block_align;
211 if (st->codec->codec_id == AV_CODEC_ID_SIPR) {
213 av_log(s, AV_LOG_ERROR, "bad SIPR file flavor %d\n",
217 st->codec->block_align = ff_sipr_subpk_size[flavor];
219 if(sub_packet_size <= 0){
220 av_log(s, AV_LOG_ERROR, "sub_packet_size is invalid\n");
223 st->codec->block_align = ast->sub_packet_size;
225 if ((ret = rm_read_extradata(pb, st->codec, codecdata_length)) < 0)
228 case AV_CODEC_ID_AAC:
229 avio_rb16(pb); avio_r8(pb);
232 codecdata_length = avio_rb32(pb);
233 if(codecdata_length + FF_INPUT_BUFFER_PADDING_SIZE <= (unsigned)codecdata_length){
234 av_log(s, AV_LOG_ERROR, "codecdata_length too large\n");
237 if (codecdata_length >= 1) {
239 if ((ret = rm_read_extradata(pb, st->codec, codecdata_length - 1)) < 0)
244 if (ast->deint_id == DEINT_ID_INT4 ||
245 ast->deint_id == DEINT_ID_GENR ||
246 ast->deint_id == DEINT_ID_SIPR) {
247 if (st->codec->block_align <= 0 ||
248 ast->audio_framesize * sub_packet_h > (unsigned)INT_MAX ||
249 ast->audio_framesize * sub_packet_h < st->codec->block_align)
250 return AVERROR_INVALIDDATA;
251 if (av_new_packet(&ast->pkt, ast->audio_framesize * sub_packet_h) < 0)
252 return AVERROR(ENOMEM);
254 switch (ast->deint_id) {
256 if (ast->coded_framesize > ast->audio_framesize ||
258 ast->coded_framesize * sub_packet_h > (2 + (sub_packet_h & 1)) * ast->audio_framesize)
259 return AVERROR_INVALIDDATA;
262 if (ast->sub_packet_size <= 0 ||
263 ast->sub_packet_size > ast->audio_framesize)
264 return AVERROR_INVALIDDATA;
272 av_log(NULL, 0 ,"Unknown interleaver %"PRIX32"\n", ast->deint_id);
273 return AVERROR_INVALIDDATA;
280 rm_read_metadata(s, pb, 0);
287 ff_rm_read_mdpr_codecdata (AVFormatContext *s, AVIOContext *pb,
288 AVStream *st, RMStream *rst, int codec_data_size)
295 avpriv_set_pts_info(st, 64, 1, 1000);
296 codec_pos = avio_tell(pb);
298 if (v == MKTAG(0xfd, 'a', 'r', '.')) {
300 if (rm_read_audio_stream_info(s, pb, st, rst, 0))
302 } else if (v == MKBETAG('L', 'S', 'D', ':')) {
303 avio_seek(pb, -4, SEEK_CUR);
304 if ((ret = rm_read_extradata(pb, st->codec, codec_data_size)) < 0)
307 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
308 st->codec->codec_tag = AV_RL32(st->codec->extradata);
309 st->codec->codec_id = ff_codec_get_id(ff_rm_codec_tags,
310 st->codec->codec_tag);
313 if (avio_rl32(pb) != MKTAG('V', 'I', 'D', 'O')) {
315 av_log(st->codec, AV_LOG_ERROR, "Unsupported video codec\n");
318 st->codec->codec_tag = avio_rl32(pb);
319 st->codec->codec_id = ff_codec_get_id(ff_rm_codec_tags,
320 st->codec->codec_tag);
321 av_dlog(s, "%X %X\n", st->codec->codec_tag, MKTAG('R', 'V', '2', '0'));
322 if (st->codec->codec_id == AV_CODEC_ID_NONE)
324 st->codec->width = avio_rb16(pb);
325 st->codec->height = avio_rb16(pb);
326 avio_skip(pb, 2); // looks like bits per sample
327 avio_skip(pb, 4); // always zero?
328 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
329 st->need_parsing = AVSTREAM_PARSE_TIMESTAMPS;
332 if ((ret = rm_read_extradata(pb, st->codec, codec_data_size - (avio_tell(pb) - codec_pos))) < 0)
336 av_reduce(&st->avg_frame_rate.den, &st->avg_frame_rate.num,
337 0x10000, fps, (1 << 30) - 1);
338 } else if (s->error_recognition & AV_EF_EXPLODE) {
339 av_log(s, AV_LOG_ERROR, "Invalid framerate\n");
340 return AVERROR_INVALIDDATA;
345 /* skip codec info */
346 size = avio_tell(pb) - codec_pos;
347 avio_skip(pb, codec_data_size - size);
352 /** this function assumes that the demuxer has already seeked to the start
353 * of the INDX chunk, and will bail out if not. */
354 static int rm_read_index(AVFormatContext *s)
356 AVIOContext *pb = s->pb;
357 unsigned int size, n_pkts, str_id, next_off, n, pos, pts;
361 if (avio_rl32(pb) != MKTAG('I','N','D','X'))
363 size = avio_rb32(pb);
367 n_pkts = avio_rb32(pb);
368 str_id = avio_rb16(pb);
369 next_off = avio_rb32(pb);
370 for (n = 0; n < s->nb_streams; n++)
371 if (s->streams[n]->id == str_id) {
375 if (n == s->nb_streams) {
376 av_log(s, AV_LOG_ERROR,
377 "Invalid stream index %d for index at pos %"PRId64"\n",
378 str_id, avio_tell(pb));
380 } else if ((avio_size(pb) - avio_tell(pb)) / 14 < n_pkts) {
381 av_log(s, AV_LOG_ERROR,
382 "Nr. of packets in packet index for stream index %d "
383 "exceeds filesize (%"PRId64" at %"PRId64" = %"PRId64")\n",
384 str_id, avio_size(pb), avio_tell(pb),
385 (avio_size(pb) - avio_tell(pb)) / 14);
389 for (n = 0; n < n_pkts; n++) {
393 avio_skip(pb, 4); /* packet no. */
395 av_add_index_entry(st, pos, pts, 0, 0, AVINDEX_KEYFRAME);
399 if (next_off && avio_tell(pb) < next_off &&
400 avio_seek(pb, next_off, SEEK_SET) < 0) {
401 av_log(s, AV_LOG_ERROR,
402 "Non-linear index detected, not supported\n");
410 static int rm_read_header_old(AVFormatContext *s)
412 RMDemuxContext *rm = s->priv_data;
416 st = avformat_new_stream(s, NULL);
419 st->priv_data = ff_rm_alloc_rmstream();
420 return rm_read_audio_stream_info(s, s->pb, st, st->priv_data, 1);
423 static int rm_read_header(AVFormatContext *s)
425 RMDemuxContext *rm = s->priv_data;
427 AVIOContext *pb = s->pb;
430 unsigned int start_time, duration;
431 unsigned int data_off = 0, indx_off = 0;
436 if (tag == MKTAG('.', 'r', 'a', 0xfd)) {
437 /* very old .ra format */
438 return rm_read_header_old(s);
439 } else if (tag != MKTAG('.', 'R', 'M', 'F')) {
443 tag_size = avio_rb32(pb);
444 avio_skip(pb, tag_size - 8);
450 tag_size = avio_rb32(pb);
452 av_dlog(s, "tag=%c%c%c%c (%08x) size=%d\n",
459 if (tag_size < 10 && tag != MKTAG('D', 'A', 'T', 'A'))
462 case MKTAG('P', 'R', 'O', 'P'):
464 avio_rb32(pb); /* max bit rate */
465 avio_rb32(pb); /* avg bit rate */
466 avio_rb32(pb); /* max packet size */
467 avio_rb32(pb); /* avg packet size */
468 avio_rb32(pb); /* nb packets */
469 avio_rb32(pb); /* duration */
470 avio_rb32(pb); /* preroll */
471 indx_off = avio_rb32(pb); /* index offset */
472 data_off = avio_rb32(pb); /* data offset */
473 avio_rb16(pb); /* nb streams */
474 flags = avio_rb16(pb); /* flags */
476 case MKTAG('C', 'O', 'N', 'T'):
477 rm_read_metadata(s, pb, 1);
479 case MKTAG('M', 'D', 'P', 'R'):
480 st = avformat_new_stream(s, NULL);
482 return AVERROR(ENOMEM);
483 st->id = avio_rb16(pb);
484 avio_rb32(pb); /* max bit rate */
485 st->codec->bit_rate = avio_rb32(pb); /* bit rate */
486 avio_rb32(pb); /* max packet size */
487 avio_rb32(pb); /* avg packet size */
488 start_time = avio_rb32(pb); /* start time */
489 avio_rb32(pb); /* preroll */
490 duration = avio_rb32(pb); /* duration */
491 st->start_time = start_time;
492 st->duration = duration;
493 get_str8(pb, buf, sizeof(buf)); /* desc */
494 get_str8(pb, buf, sizeof(buf)); /* mimetype */
495 st->codec->codec_type = AVMEDIA_TYPE_DATA;
496 st->priv_data = ff_rm_alloc_rmstream();
497 if (ff_rm_read_mdpr_codecdata(s, s->pb, st, st->priv_data,
501 case MKTAG('D', 'A', 'T', 'A'):
504 /* unknown tag: skip it */
505 avio_skip(pb, tag_size - 10);
510 rm->nb_packets = avio_rb32(pb); /* number of packets */
511 if (!rm->nb_packets && (flags & 4))
512 rm->nb_packets = 3600 * 25;
513 avio_rb32(pb); /* next data header */
516 data_off = avio_tell(pb) - 18;
517 if (indx_off && pb->seekable && !(s->flags & AVFMT_FLAG_IGNIDX) &&
518 avio_seek(pb, indx_off, SEEK_SET) >= 0) {
520 avio_seek(pb, data_off + 18, SEEK_SET);
526 static int get_num(AVIOContext *pb, int *len)
538 return (n << 16) | n1;
542 /* multiple of 20 bytes for ra144 (ugly) */
543 #define RAW_PACKET_SIZE 1000
545 static int sync(AVFormatContext *s, int64_t *timestamp, int *flags, int *stream_index, int64_t *pos){
546 RMDemuxContext *rm = s->priv_data;
547 AVIOContext *pb = s->pb;
549 uint32_t state=0xFFFFFFFF;
551 while(!pb->eof_reached){
553 *pos= avio_tell(pb) - 3;
554 if(rm->remaining_len > 0){
555 num= rm->current_stream;
556 len= rm->remaining_len;
557 *timestamp = AV_NOPTS_VALUE;
560 state= (state<<8) + avio_r8(pb);
562 if(state == MKBETAG('I', 'N', 'D', 'X')){
563 int n_pkts, expected_len;
566 n_pkts = avio_rb32(pb);
567 expected_len = 20 + n_pkts * 14;
569 /* some files don't add index entries to chunk size... */
571 else if (len != expected_len)
572 av_log(s, AV_LOG_WARNING,
573 "Index size %d (%d pkts) is wrong, should be %d.\n",
574 len, n_pkts, expected_len);
575 len -= 14; // we already read part of the index header
579 } else if (state == MKBETAG('D','A','T','A')) {
580 av_log(s, AV_LOG_WARNING,
581 "DATA tag in middle of chunk, file may be broken.\n");
584 if(state > (unsigned)0xFFFF || state <= 12)
590 *timestamp = avio_rb32(pb);
591 avio_r8(pb); /* reserved */
592 *flags = avio_r8(pb); /* flags */
594 for(i=0;i<s->nb_streams;i++) {
599 if (i == s->nb_streams) {
601 /* skip packet if unknown number */
603 rm->remaining_len = 0;
613 static int rm_assemble_video_frame(AVFormatContext *s, AVIOContext *pb,
614 RMDemuxContext *rm, RMStream *vst,
615 AVPacket *pkt, int len, int *pseq,
618 int hdr, seq, pic_num, len2, pos;
621 hdr = avio_r8(pb); len--;
624 if(type != 3){ // not frame as a part of packet
625 seq = avio_r8(pb); len--;
627 if(type != 1){ // not whole frame
628 len2 = get_num(pb, &len);
629 pos = get_num(pb, &len);
630 pic_num = avio_r8(pb); len--;
634 rm->remaining_len = len;
635 if(type&1){ // frame, not slice
636 if(type == 3){ // frame as a part of packet
640 if(rm->remaining_len < len)
642 rm->remaining_len -= len;
643 if(av_new_packet(pkt, len + 9) < 0)
646 AV_WL32(pkt->data + 1, 1);
647 AV_WL32(pkt->data + 5, 0);
648 avio_read(pb, pkt->data + 9, len);
651 //now we have to deal with single slice
654 if((seq & 0x7F) == 1 || vst->curpic_num != pic_num){
655 vst->slices = ((hdr & 0x3F) << 1) + 1;
656 vst->videobufsize = len2 + 8*vst->slices + 1;
657 av_free_packet(&vst->pkt); //FIXME this should be output.
658 if(av_new_packet(&vst->pkt, vst->videobufsize) < 0)
659 return AVERROR(ENOMEM);
660 vst->videobufpos = 8*vst->slices + 1;
662 vst->curpic_num = pic_num;
663 vst->pktpos = avio_tell(pb);
666 len = FFMIN(len, pos);
668 if(++vst->cur_slice > vst->slices)
670 AV_WL32(vst->pkt.data - 7 + 8*vst->cur_slice, 1);
671 AV_WL32(vst->pkt.data - 3 + 8*vst->cur_slice, vst->videobufpos - 8*vst->slices - 1);
672 if(vst->videobufpos + len > vst->videobufsize)
674 if (avio_read(pb, vst->pkt.data + vst->videobufpos, len) != len)
676 vst->videobufpos += len;
677 rm->remaining_len-= len;
679 if (type == 2 || vst->videobufpos == vst->videobufsize) {
680 vst->pkt.data[0] = vst->cur_slice-1;
685 #if FF_API_DESTRUCT_PACKET
686 FF_DISABLE_DEPRECATION_WARNINGS
687 vst->pkt.destruct = NULL;
688 FF_ENABLE_DEPRECATION_WARNINGS
690 if(vst->slices != vst->cur_slice) //FIXME find out how to set slices correct from the begin
691 memmove(pkt->data + 1 + 8*vst->cur_slice, pkt->data + 1 + 8*vst->slices,
692 vst->videobufpos - 1 - 8*vst->slices);
693 pkt->size = vst->videobufpos + 8*(vst->cur_slice - vst->slices);
694 pkt->pts = AV_NOPTS_VALUE;
695 pkt->pos = vst->pktpos;
704 rm_ac3_swap_bytes (AVStream *st, AVPacket *pkt)
709 if (st->codec->codec_id == AV_CODEC_ID_AC3) {
711 for (j=0;j<pkt->size;j+=2) {
712 FFSWAP(int, ptr[0], ptr[1]);
719 ff_rm_parse_packet (AVFormatContext *s, AVIOContext *pb,
720 AVStream *st, RMStream *ast, int len, AVPacket *pkt,
721 int *seq, int flags, int64_t timestamp)
723 RMDemuxContext *rm = s->priv_data;
725 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
726 rm->current_stream= st->id;
727 if(rm_assemble_video_frame(s, pb, rm, ast, pkt, len, seq, ×tamp))
728 return -1; //got partial frame
729 } else if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
730 if ((ast->deint_id == DEINT_ID_GENR) ||
731 (ast->deint_id == DEINT_ID_INT4) ||
732 (ast->deint_id == DEINT_ID_SIPR)) {
734 int sps = ast->sub_packet_size;
735 int cfs = ast->coded_framesize;
736 int h = ast->sub_packet_h;
737 int y = ast->sub_packet_cnt;
738 int w = ast->audio_framesize;
741 y = ast->sub_packet_cnt = 0;
743 ast->audiotimestamp = timestamp;
745 switch (ast->deint_id) {
747 for (x = 0; x < h/2; x++)
748 avio_read(pb, ast->pkt.data+x*2*w+y*cfs, cfs);
751 for (x = 0; x < w/sps; x++)
752 avio_read(pb, ast->pkt.data+sps*(h*x+((h+1)/2)*(y&1)+(y>>1)), sps);
755 avio_read(pb, ast->pkt.data + y * w, w);
759 if (++(ast->sub_packet_cnt) < h)
761 if (ast->deint_id == DEINT_ID_SIPR)
762 ff_rm_reorder_sipr_data(ast->pkt.data, h, w);
764 ast->sub_packet_cnt = 0;
765 rm->audio_stream_num = st->index;
766 rm->audio_pkt_cnt = h * w / st->codec->block_align;
767 } else if ((ast->deint_id == DEINT_ID_VBRF) ||
768 (ast->deint_id == DEINT_ID_VBRS)) {
770 rm->audio_stream_num = st->index;
771 ast->sub_packet_cnt = (avio_rb16(pb) & 0xf0) >> 4;
772 if (ast->sub_packet_cnt) {
773 for (x = 0; x < ast->sub_packet_cnt; x++)
774 ast->sub_packet_lengths[x] = avio_rb16(pb);
775 rm->audio_pkt_cnt = ast->sub_packet_cnt;
776 ast->audiotimestamp = timestamp;
780 av_get_packet(pb, pkt, len);
781 rm_ac3_swap_bytes(st, pkt);
784 av_get_packet(pb, pkt, len);
786 pkt->stream_index = st->index;
789 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
790 if(st->codec->codec_id == AV_CODEC_ID_RV20){
791 int seq= 128*(pkt->data[2]&0x7F) + (pkt->data[3]>>1);
792 av_log(s, AV_LOG_DEBUG, "%d %"PRId64" %d\n", *timestamp, *timestamp*512LL/25, seq);
794 seq |= (timestamp&~0x3FFF);
795 if(seq - timestamp > 0x2000) seq -= 0x4000;
796 if(seq - timestamp < -0x2000) seq += 0x4000;
801 pkt->pts = timestamp;
803 pkt->flags |= AV_PKT_FLAG_KEY;
805 return st->codec->codec_type == AVMEDIA_TYPE_AUDIO ? rm->audio_pkt_cnt : 0;
809 ff_rm_retrieve_cache (AVFormatContext *s, AVIOContext *pb,
810 AVStream *st, RMStream *ast, AVPacket *pkt)
812 RMDemuxContext *rm = s->priv_data;
814 assert (rm->audio_pkt_cnt > 0);
816 if (ast->deint_id == DEINT_ID_VBRF ||
817 ast->deint_id == DEINT_ID_VBRS)
818 av_get_packet(pb, pkt, ast->sub_packet_lengths[ast->sub_packet_cnt - rm->audio_pkt_cnt]);
820 av_new_packet(pkt, st->codec->block_align);
821 memcpy(pkt->data, ast->pkt.data + st->codec->block_align * //FIXME avoid this
822 (ast->sub_packet_h * ast->audio_framesize / st->codec->block_align - rm->audio_pkt_cnt),
823 st->codec->block_align);
826 if ((pkt->pts = ast->audiotimestamp) != AV_NOPTS_VALUE) {
827 ast->audiotimestamp = AV_NOPTS_VALUE;
828 pkt->flags = AV_PKT_FLAG_KEY;
831 pkt->stream_index = st->index;
833 return rm->audio_pkt_cnt;
836 static int rm_read_packet(AVFormatContext *s, AVPacket *pkt)
838 RMDemuxContext *rm = s->priv_data;
840 int i, len, res, seq = 1;
841 int64_t timestamp, pos;
845 if (rm->audio_pkt_cnt) {
846 // If there are queued audio packet return them first
847 st = s->streams[rm->audio_stream_num];
848 ff_rm_retrieve_cache(s, s->pb, st, st->priv_data, pkt);
851 if (rm->old_format) {
856 timestamp = AV_NOPTS_VALUE;
857 len = !ast->audio_framesize ? RAW_PACKET_SIZE :
858 ast->coded_framesize * ast->sub_packet_h / 2;
859 flags = (seq++ == 1) ? 2 : 0;
860 pos = avio_tell(s->pb);
862 len=sync(s, ×tamp, &flags, &i, &pos);
867 if(len<0 || s->pb->eof_reached)
870 res = ff_rm_parse_packet (s, s->pb, st, st->priv_data, len, pkt,
871 &seq, flags, timestamp);
872 if((flags&2) && (seq&0x7F) == 1)
873 av_add_index_entry(st, pos, timestamp, 0, 0, AVINDEX_KEYFRAME);
878 if( (st->discard >= AVDISCARD_NONKEY && !(flags&2))
879 || st->discard >= AVDISCARD_ALL){
888 static int rm_read_close(AVFormatContext *s)
892 for (i=0;i<s->nb_streams;i++)
893 ff_rm_free_rmstream(s->streams[i]->priv_data);
898 static int rm_probe(AVProbeData *p)
900 /* check file header */
901 if ((p->buf[0] == '.' && p->buf[1] == 'R' &&
902 p->buf[2] == 'M' && p->buf[3] == 'F' &&
903 p->buf[4] == 0 && p->buf[5] == 0) ||
904 (p->buf[0] == '.' && p->buf[1] == 'r' &&
905 p->buf[2] == 'a' && p->buf[3] == 0xfd))
906 return AVPROBE_SCORE_MAX;
911 static int64_t rm_read_dts(AVFormatContext *s, int stream_index,
912 int64_t *ppos, int64_t pos_limit)
914 RMDemuxContext *rm = s->priv_data;
916 int stream_index2, flags, len, h;
921 return AV_NOPTS_VALUE;
923 avio_seek(s->pb, pos, SEEK_SET);
929 len=sync(s, &dts, &flags, &stream_index2, &pos);
931 return AV_NOPTS_VALUE;
933 st = s->streams[stream_index2];
934 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
935 h= avio_r8(s->pb); len--;
937 seq = avio_r8(s->pb); len--;
941 if((flags&2) && (seq&0x7F) == 1){
942 av_dlog(s, "%d %d-%d %"PRId64" %d\n",
943 flags, stream_index2, stream_index, dts, seq);
944 av_add_index_entry(st, pos, dts, 0, 0, AVINDEX_KEYFRAME);
945 if(stream_index2 == stream_index)
949 avio_skip(s->pb, len);
955 AVInputFormat ff_rm_demuxer = {
957 .long_name = NULL_IF_CONFIG_SMALL("RealMedia"),
958 .priv_data_size = sizeof(RMDemuxContext),
959 .read_probe = rm_probe,
960 .read_header = rm_read_header,
961 .read_packet = rm_read_packet,
962 .read_close = rm_read_close,
963 .read_timestamp = rm_read_dts,
966 AVInputFormat ff_rdt_demuxer = {
968 .long_name = NULL_IF_CONFIG_SMALL("RDT demuxer"),
969 .priv_data_size = sizeof(RMDemuxContext),
970 .read_close = rm_read_close,
971 .flags = AVFMT_NOFILE,