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
22 #include "libavutil/avstring.h"
23 #include "libavutil/intreadwrite.h"
24 #include "libavutil/dict.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
38 AVPacket pkt; ///< place to store merged video frame / reordered audio data
39 int videobufsize; ///< current assembled frame size
40 int videobufpos; ///< position for the next slice in the video buffer
41 int curpic_num; ///< picture number of current frame
42 int cur_slice, slices;
43 int64_t pktpos; ///< first slice position in file
44 /// Audio descrambling matrix parameters
45 int64_t audiotimestamp; ///< Audio packet timestamp
46 int sub_packet_cnt; // Subpacket counter, used while reading
47 int sub_packet_size, sub_packet_h, coded_framesize; ///< Descrambling parameters from container
48 int audio_framesize; /// Audio frame size from container
49 int sub_packet_lengths[16]; /// Length of each subpacket
50 int32_t deint_id; ///< deinterleaver used in audio stream
58 int audio_stream_num; ///< Stream number for audio packets
59 int audio_pkt_cnt; ///< Output packet counter
62 static const unsigned char sipr_swaps[38][2] = {
63 { 0, 63 }, { 1, 22 }, { 2, 44 }, { 3, 90 },
64 { 5, 81 }, { 7, 31 }, { 8, 86 }, { 9, 58 },
65 { 10, 36 }, { 12, 68 }, { 13, 39 }, { 14, 73 },
66 { 15, 53 }, { 16, 69 }, { 17, 57 }, { 19, 88 },
67 { 20, 34 }, { 21, 71 }, { 24, 46 }, { 25, 94 },
68 { 26, 54 }, { 28, 75 }, { 29, 50 }, { 32, 70 },
69 { 33, 92 }, { 35, 74 }, { 38, 85 }, { 40, 56 },
70 { 42, 87 }, { 43, 65 }, { 45, 59 }, { 48, 79 },
71 { 49, 93 }, { 51, 89 }, { 55, 95 }, { 61, 76 },
72 { 67, 83 }, { 77, 80 }
75 const unsigned char ff_sipr_subpk_size[4] = { 29, 19, 37, 20 };
77 static inline void get_strl(AVIOContext *pb, char *buf, int buf_size, int len)
88 if (buf_size > 0) *q = '\0';
91 static void get_str8(AVIOContext *pb, char *buf, int buf_size)
93 get_strl(pb, buf, buf_size, avio_r8(pb));
96 static int rm_read_extradata(AVIOContext *pb, AVCodecContext *avctx, unsigned size)
100 avctx->extradata = av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE);
101 if (!avctx->extradata)
102 return AVERROR(ENOMEM);
103 avctx->extradata_size = avio_read(pb, avctx->extradata, size);
104 memset(avctx->extradata + avctx->extradata_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
105 if (avctx->extradata_size != size)
110 static void rm_read_metadata(AVFormatContext *s, int wide)
114 for (i=0; i<FF_ARRAY_ELEMS(ff_rm_metadata); i++) {
115 int len = wide ? avio_rb16(s->pb) : avio_r8(s->pb);
116 get_strl(s->pb, buf, sizeof(buf), len);
117 av_dict_set(&s->metadata, ff_rm_metadata[i], buf, 0);
121 RMStream *ff_rm_alloc_rmstream (void)
123 RMStream *rms = av_mallocz(sizeof(RMStream));
124 rms->curpic_num = -1;
128 void ff_rm_free_rmstream (RMStream *rms)
130 av_free_packet(&rms->pkt);
133 static int rm_read_audio_stream_info(AVFormatContext *s, AVIOContext *pb,
134 AVStream *st, RMStream *ast, int read_all)
141 version = avio_rb16(pb); /* version */
143 int header_size = avio_rb16(pb);
144 int64_t startpos = avio_tell(pb);
146 rm_read_metadata(s, 0);
147 if ((startpos + header_size) >= avio_tell(pb) + 2) {
148 // fourcc (should always be "lpcJ")
150 get_str8(pb, buf, sizeof(buf));
152 // Skip extra header crap (this should never happen)
153 if ((startpos + header_size) > avio_tell(pb))
154 avio_skip(pb, header_size + startpos - avio_tell(pb));
155 st->codec->sample_rate = 8000;
156 st->codec->channels = 1;
157 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
158 st->codec->codec_id = CODEC_ID_RA_144;
159 ast->deint_id = DEINT_ID_INT0;
161 int flavor, sub_packet_h, coded_framesize, sub_packet_size;
162 int codecdata_length;
163 /* old version (4) */
164 avio_skip(pb, 2); /* unused */
165 avio_rb32(pb); /* .ra4 */
166 avio_rb32(pb); /* data size */
167 avio_rb16(pb); /* version2 */
168 avio_rb32(pb); /* header size */
169 flavor= avio_rb16(pb); /* add codec info / flavor */
170 ast->coded_framesize = coded_framesize = avio_rb32(pb); /* coded frame size */
171 avio_rb32(pb); /* ??? */
172 avio_rb32(pb); /* ??? */
173 avio_rb32(pb); /* ??? */
174 ast->sub_packet_h = sub_packet_h = avio_rb16(pb); /* 1 */
175 st->codec->block_align= avio_rb16(pb); /* frame size */
176 ast->sub_packet_size = sub_packet_size = avio_rb16(pb); /* sub packet size */
177 avio_rb16(pb); /* ??? */
179 avio_rb16(pb); avio_rb16(pb); avio_rb16(pb);
181 st->codec->sample_rate = avio_rb16(pb);
183 st->codec->channels = avio_rb16(pb);
185 ast->deint_id = avio_rl32(pb);
186 avio_read(pb, buf, 4);
189 get_str8(pb, buf, sizeof(buf)); /* desc */
190 ast->deint_id = AV_RL32(buf);
191 get_str8(pb, buf, sizeof(buf)); /* desc */
193 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
194 st->codec->codec_tag = AV_RL32(buf);
195 st->codec->codec_id = ff_codec_get_id(ff_rm_codec_tags,
196 st->codec->codec_tag);
198 switch (st->codec->codec_id) {
200 st->need_parsing = AVSTREAM_PARSE_FULL;
202 case CODEC_ID_RA_288:
203 st->codec->extradata_size= 0;
204 ast->audio_framesize = st->codec->block_align;
205 st->codec->block_align = coded_framesize;
208 case CODEC_ID_ATRAC3:
210 avio_rb16(pb); avio_r8(pb);
213 codecdata_length = avio_rb32(pb);
214 if(codecdata_length + FF_INPUT_BUFFER_PADDING_SIZE <= (unsigned)codecdata_length){
215 av_log(s, AV_LOG_ERROR, "codecdata_length too large\n");
219 ast->audio_framesize = st->codec->block_align;
220 if (st->codec->codec_id == CODEC_ID_SIPR) {
222 av_log(s, AV_LOG_ERROR, "bad SIPR file flavor %d\n",
226 st->codec->block_align = ff_sipr_subpk_size[flavor];
228 if(sub_packet_size <= 0){
229 av_log(s, AV_LOG_ERROR, "sub_packet_size is invalid\n");
232 st->codec->block_align = ast->sub_packet_size;
234 if ((ret = rm_read_extradata(pb, st->codec, codecdata_length)) < 0)
238 avio_rb16(pb); avio_r8(pb);
241 codecdata_length = avio_rb32(pb);
242 if(codecdata_length + FF_INPUT_BUFFER_PADDING_SIZE <= (unsigned)codecdata_length){
243 av_log(s, AV_LOG_ERROR, "codecdata_length too large\n");
246 if (codecdata_length >= 1) {
248 if ((ret = rm_read_extradata(pb, st->codec, codecdata_length - 1)) < 0)
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 ||
269 ast->coded_framesize * sub_packet_h > (2 + (sub_packet_h & 1)) * ast->audio_framesize)
270 return AVERROR_INVALIDDATA;
273 if (ast->sub_packet_size <= 0 ||
274 ast->sub_packet_size > ast->audio_framesize)
275 return AVERROR_INVALIDDATA;
283 av_log(NULL,0,"Unknown interleaver %X\n", ast->deint_id);
284 return AVERROR_INVALIDDATA;
291 rm_read_metadata(s, 0);
298 ff_rm_read_mdpr_codecdata (AVFormatContext *s, AVIOContext *pb,
299 AVStream *st, RMStream *rst, int codec_data_size)
306 avpriv_set_pts_info(st, 64, 1, 1000);
307 codec_pos = avio_tell(pb);
309 if (v == MKTAG(0xfd, 'a', 'r', '.')) {
311 if (rm_read_audio_stream_info(s, pb, st, rst, 0))
313 } else if (v == MKBETAG('L', 'S', 'D', ':')) {
314 avio_seek(pb, -4, SEEK_CUR);
315 if ((ret = rm_read_extradata(pb, st->codec, codec_data_size)) < 0)
318 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
319 st->codec->codec_tag = AV_RL32(st->codec->extradata);
320 st->codec->codec_id = ff_codec_get_id(ff_rm_codec_tags,
321 st->codec->codec_tag);
324 if (avio_rl32(pb) != MKTAG('V', 'I', 'D', 'O')) {
326 av_log(st->codec, AV_LOG_ERROR, "Unsupported video codec\n");
329 st->codec->codec_tag = avio_rl32(pb);
330 st->codec->codec_id = ff_codec_get_id(ff_rm_codec_tags,
331 st->codec->codec_tag);
332 // av_log(s, AV_LOG_DEBUG, "%X %X\n", st->codec->codec_tag, MKTAG('R', 'V', '2', '0'));
333 if (st->codec->codec_id == CODEC_ID_NONE)
335 st->codec->width = avio_rb16(pb);
336 st->codec->height = avio_rb16(pb);
337 avio_skip(pb, 2); // looks like bits per sample
338 avio_skip(pb, 4); // always zero?
339 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
340 st->need_parsing = AVSTREAM_PARSE_TIMESTAMPS;
343 if ((ret = rm_read_extradata(pb, st->codec, codec_data_size - (avio_tell(pb) - codec_pos))) < 0)
346 av_reduce(&st->r_frame_rate.den, &st->r_frame_rate.num,
347 0x10000, fps, (1 << 30) - 1);
348 st->avg_frame_rate = st->r_frame_rate;
352 /* skip codec info */
353 size = avio_tell(pb) - codec_pos;
354 avio_skip(pb, codec_data_size - size);
359 /** this function assumes that the demuxer has already seeked to the start
360 * of the INDX chunk, and will bail out if not. */
361 static int rm_read_index(AVFormatContext *s)
363 AVIOContext *pb = s->pb;
364 unsigned int size, n_pkts, str_id, next_off, n, pos, pts;
368 if (avio_rl32(pb) != MKTAG('I','N','D','X'))
370 size = avio_rb32(pb);
374 n_pkts = avio_rb32(pb);
375 str_id = avio_rb16(pb);
376 next_off = avio_rb32(pb);
377 for (n = 0; n < s->nb_streams; n++)
378 if (s->streams[n]->id == str_id) {
382 if (n == s->nb_streams) {
383 av_log(s, AV_LOG_ERROR,
384 "Invalid stream index %d for index at pos %"PRId64"\n",
385 str_id, avio_tell(pb));
387 } else if ((avio_size(pb) - avio_tell(pb)) / 14 < n_pkts) {
388 av_log(s, AV_LOG_ERROR,
389 "Nr. of packets in packet index for stream index %d "
390 "exceeds filesize (%"PRId64" at %"PRId64" = %"PRId64")\n",
391 str_id, avio_size(pb), avio_tell(pb),
392 (avio_size(pb) - avio_tell(pb)) / 14);
396 for (n = 0; n < n_pkts; n++) {
400 avio_skip(pb, 4); /* packet no. */
402 av_add_index_entry(st, pos, pts, 0, 0, AVINDEX_KEYFRAME);
406 if (next_off && avio_tell(pb) < next_off &&
407 avio_seek(pb, next_off, SEEK_SET) < 0) {
408 av_log(s, AV_LOG_ERROR,
409 "Non-linear index detected, not supported\n");
417 static int rm_read_header_old(AVFormatContext *s)
419 RMDemuxContext *rm = s->priv_data;
423 st = avformat_new_stream(s, NULL);
426 st->priv_data = ff_rm_alloc_rmstream();
427 return rm_read_audio_stream_info(s, s->pb, st, st->priv_data, 1);
430 static int rm_read_header(AVFormatContext *s)
432 RMDemuxContext *rm = s->priv_data;
434 AVIOContext *pb = s->pb;
437 unsigned int start_time, duration;
438 unsigned int data_off = 0, indx_off = 0;
443 if (tag == MKTAG('.', 'r', 'a', 0xfd)) {
444 /* very old .ra format */
445 return rm_read_header_old(s);
446 } else if (tag != MKTAG('.', 'R', 'M', 'F')) {
450 tag_size = avio_rb32(pb);
451 avio_skip(pb, tag_size - 8);
457 tag_size = avio_rb32(pb);
459 av_dlog(s, "tag=%c%c%c%c (%08x) size=%d\n",
466 if (tag_size < 10 && tag != MKTAG('D', 'A', 'T', 'A'))
469 case MKTAG('P', 'R', 'O', 'P'):
471 avio_rb32(pb); /* max bit rate */
472 avio_rb32(pb); /* avg bit rate */
473 avio_rb32(pb); /* max packet size */
474 avio_rb32(pb); /* avg packet size */
475 avio_rb32(pb); /* nb packets */
476 avio_rb32(pb); /* duration */
477 avio_rb32(pb); /* preroll */
478 indx_off = avio_rb32(pb); /* index offset */
479 data_off = avio_rb32(pb); /* data offset */
480 avio_rb16(pb); /* nb streams */
481 flags = avio_rb16(pb); /* flags */
483 case MKTAG('C', 'O', 'N', 'T'):
484 rm_read_metadata(s, 1);
486 case MKTAG('M', 'D', 'P', 'R'):
487 st = avformat_new_stream(s, NULL);
489 return AVERROR(ENOMEM);
490 st->id = avio_rb16(pb);
491 avio_rb32(pb); /* max bit rate */
492 st->codec->bit_rate = avio_rb32(pb); /* bit rate */
493 avio_rb32(pb); /* max packet size */
494 avio_rb32(pb); /* avg packet size */
495 start_time = avio_rb32(pb); /* start time */
496 avio_rb32(pb); /* preroll */
497 duration = avio_rb32(pb); /* duration */
498 st->start_time = start_time;
499 st->duration = duration;
500 get_str8(pb, buf, sizeof(buf)); /* desc */
501 get_str8(pb, buf, sizeof(buf)); /* mimetype */
502 st->codec->codec_type = AVMEDIA_TYPE_DATA;
503 st->priv_data = ff_rm_alloc_rmstream();
504 if (ff_rm_read_mdpr_codecdata(s, s->pb, st, st->priv_data,
508 case MKTAG('D', 'A', 'T', 'A'):
511 /* unknown tag: skip it */
512 avio_skip(pb, tag_size - 10);
517 rm->nb_packets = avio_rb32(pb); /* number of packets */
518 if (!rm->nb_packets && (flags & 4))
519 rm->nb_packets = 3600 * 25;
520 avio_rb32(pb); /* next data header */
523 data_off = avio_tell(pb) - 18;
524 if (indx_off && pb->seekable && !(s->flags & AVFMT_FLAG_IGNIDX) &&
525 avio_seek(pb, indx_off, SEEK_SET) >= 0) {
527 avio_seek(pb, data_off + 18, SEEK_SET);
533 static int get_num(AVIOContext *pb, int *len)
545 return (n << 16) | n1;
549 /* multiple of 20 bytes for ra144 (ugly) */
550 #define RAW_PACKET_SIZE 1000
552 static int sync(AVFormatContext *s, int64_t *timestamp, int *flags, int *stream_index, int64_t *pos){
553 RMDemuxContext *rm = s->priv_data;
554 AVIOContext *pb = s->pb;
556 uint32_t state=0xFFFFFFFF;
558 while(!pb->eof_reached){
560 *pos= avio_tell(pb) - 3;
561 if(rm->remaining_len > 0){
562 num= rm->current_stream;
563 len= rm->remaining_len;
564 *timestamp = AV_NOPTS_VALUE;
567 state= (state<<8) + avio_r8(pb);
569 if(state == MKBETAG('I', 'N', 'D', 'X')){
570 int n_pkts, expected_len;
573 n_pkts = avio_rb32(pb);
574 expected_len = 20 + n_pkts * 14;
576 /* some files don't add index entries to chunk size... */
578 else if (len != expected_len)
579 av_log(s, AV_LOG_WARNING,
580 "Index size %d (%d pkts) is wrong, should be %d.\n",
581 len, n_pkts, expected_len);
582 len -= 14; // we already read part of the index header
586 } else if (state == MKBETAG('D','A','T','A')) {
587 av_log(s, AV_LOG_WARNING,
588 "DATA tag in middle of chunk, file may be broken.\n");
591 if(state > (unsigned)0xFFFF || state <= 12)
597 *timestamp = avio_rb32(pb);
598 avio_r8(pb); /* reserved */
599 *flags = avio_r8(pb); /* flags */
601 for(i=0;i<s->nb_streams;i++) {
606 if (i == s->nb_streams) {
608 /* skip packet if unknown number */
610 rm->remaining_len = 0;
620 static int rm_assemble_video_frame(AVFormatContext *s, AVIOContext *pb,
621 RMDemuxContext *rm, RMStream *vst,
622 AVPacket *pkt, int len, int *pseq,
625 int hdr, seq, pic_num, len2, pos;
628 hdr = avio_r8(pb); len--;
631 if(type != 3){ // not frame as a part of packet
632 seq = avio_r8(pb); len--;
634 if(type != 1){ // not whole frame
635 len2 = get_num(pb, &len);
636 pos = get_num(pb, &len);
637 pic_num = avio_r8(pb); len--;
641 rm->remaining_len = len;
642 if(type&1){ // frame, not slice
643 if(type == 3){ // frame as a part of packet
647 if(rm->remaining_len < len)
649 rm->remaining_len -= len;
650 if(av_new_packet(pkt, len + 9) < 0)
653 AV_WL32(pkt->data + 1, 1);
654 AV_WL32(pkt->data + 5, 0);
655 avio_read(pb, pkt->data + 9, len);
658 //now we have to deal with single slice
661 if((seq & 0x7F) == 1 || vst->curpic_num != pic_num){
662 vst->slices = ((hdr & 0x3F) << 1) + 1;
663 vst->videobufsize = len2 + 8*vst->slices + 1;
664 av_free_packet(&vst->pkt); //FIXME this should be output.
665 if(av_new_packet(&vst->pkt, vst->videobufsize) < 0)
666 return AVERROR(ENOMEM);
667 vst->videobufpos = 8*vst->slices + 1;
669 vst->curpic_num = pic_num;
670 vst->pktpos = avio_tell(pb);
673 len = FFMIN(len, pos);
675 if(++vst->cur_slice > vst->slices)
677 AV_WL32(vst->pkt.data - 7 + 8*vst->cur_slice, 1);
678 AV_WL32(vst->pkt.data - 3 + 8*vst->cur_slice, vst->videobufpos - 8*vst->slices - 1);
679 if(vst->videobufpos + len > vst->videobufsize)
681 if (avio_read(pb, vst->pkt.data + vst->videobufpos, len) != len)
683 vst->videobufpos += len;
684 rm->remaining_len-= len;
686 if (type == 2 || vst->videobufpos == vst->videobufsize) {
687 vst->pkt.data[0] = vst->cur_slice-1;
691 if(vst->slices != vst->cur_slice) //FIXME find out how to set slices correct from the begin
692 memmove(pkt->data + 1 + 8*vst->cur_slice, pkt->data + 1 + 8*vst->slices,
693 vst->videobufpos - 1 - 8*vst->slices);
694 pkt->size = vst->videobufpos + 8*(vst->cur_slice - vst->slices);
695 pkt->pts = AV_NOPTS_VALUE;
696 pkt->pos = vst->pktpos;
705 rm_ac3_swap_bytes (AVStream *st, AVPacket *pkt)
710 if (st->codec->codec_id == CODEC_ID_AC3) {
712 for (j=0;j<pkt->size;j+=2) {
713 FFSWAP(int, ptr[0], ptr[1]);
720 * Perform 4-bit block reordering for SIPR data.
721 * @todo This can be optimized, e.g. use memcpy() if data blocks are aligned
723 void ff_rm_reorder_sipr_data(uint8_t *buf, int sub_packet_h, int framesize)
725 int n, bs = sub_packet_h * framesize * 2 / 96; // nibbles per subpacket
727 for (n = 0; n < 38; n++) {
729 int i = bs * sipr_swaps[n][0];
730 int o = bs * sipr_swaps[n][1];
732 /* swap 4bit-nibbles of block 'i' with 'o' */
733 for (j = 0; j < bs; j++, i++, o++) {
734 int x = (buf[i >> 1] >> (4 * (i & 1))) & 0xF,
735 y = (buf[o >> 1] >> (4 * (o & 1))) & 0xF;
737 buf[o >> 1] = (x << (4 * (o & 1))) |
738 (buf[o >> 1] & (0xF << (4 * !(o & 1))));
739 buf[i >> 1] = (y << (4 * (i & 1))) |
740 (buf[i >> 1] & (0xF << (4 * !(i & 1))));
746 ff_rm_parse_packet (AVFormatContext *s, AVIOContext *pb,
747 AVStream *st, RMStream *ast, int len, AVPacket *pkt,
748 int *seq, int flags, int64_t timestamp)
750 RMDemuxContext *rm = s->priv_data;
752 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
753 rm->current_stream= st->id;
754 if(rm_assemble_video_frame(s, pb, rm, ast, pkt, len, seq, ×tamp))
755 return -1; //got partial frame
756 } else if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
757 if ((ast->deint_id == DEINT_ID_GENR) ||
758 (ast->deint_id == DEINT_ID_INT4) ||
759 (ast->deint_id == DEINT_ID_SIPR)) {
761 int sps = ast->sub_packet_size;
762 int cfs = ast->coded_framesize;
763 int h = ast->sub_packet_h;
764 int y = ast->sub_packet_cnt;
765 int w = ast->audio_framesize;
768 y = ast->sub_packet_cnt = 0;
770 ast->audiotimestamp = timestamp;
772 switch (ast->deint_id) {
774 for (x = 0; x < h/2; x++)
775 avio_read(pb, ast->pkt.data+x*2*w+y*cfs, cfs);
778 for (x = 0; x < w/sps; x++)
779 avio_read(pb, ast->pkt.data+sps*(h*x+((h+1)/2)*(y&1)+(y>>1)), sps);
782 avio_read(pb, ast->pkt.data + y * w, w);
786 if (++(ast->sub_packet_cnt) < h)
788 if (ast->deint_id == DEINT_ID_SIPR)
789 ff_rm_reorder_sipr_data(ast->pkt.data, h, w);
791 ast->sub_packet_cnt = 0;
792 rm->audio_stream_num = st->index;
793 rm->audio_pkt_cnt = h * w / st->codec->block_align;
794 } else if ((ast->deint_id == DEINT_ID_VBRF) ||
795 (ast->deint_id == DEINT_ID_VBRS)) {
797 rm->audio_stream_num = st->index;
798 ast->sub_packet_cnt = (avio_rb16(pb) & 0xf0) >> 4;
799 if (ast->sub_packet_cnt) {
800 for (x = 0; x < ast->sub_packet_cnt; x++)
801 ast->sub_packet_lengths[x] = avio_rb16(pb);
802 rm->audio_pkt_cnt = ast->sub_packet_cnt;
803 ast->audiotimestamp = timestamp;
807 av_get_packet(pb, pkt, len);
808 rm_ac3_swap_bytes(st, pkt);
811 av_get_packet(pb, pkt, len);
813 pkt->stream_index = st->index;
816 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
817 if(st->codec->codec_id == CODEC_ID_RV20){
818 int seq= 128*(pkt->data[2]&0x7F) + (pkt->data[3]>>1);
819 av_log(s, AV_LOG_DEBUG, "%d %"PRId64" %d\n", *timestamp, *timestamp*512LL/25, seq);
821 seq |= (timestamp&~0x3FFF);
822 if(seq - timestamp > 0x2000) seq -= 0x4000;
823 if(seq - timestamp < -0x2000) seq += 0x4000;
828 pkt->pts = timestamp;
830 pkt->flags |= AV_PKT_FLAG_KEY;
832 return st->codec->codec_type == AVMEDIA_TYPE_AUDIO ? rm->audio_pkt_cnt : 0;
836 ff_rm_retrieve_cache (AVFormatContext *s, AVIOContext *pb,
837 AVStream *st, RMStream *ast, AVPacket *pkt)
839 RMDemuxContext *rm = s->priv_data;
841 assert (rm->audio_pkt_cnt > 0);
843 if (ast->deint_id == DEINT_ID_VBRF ||
844 ast->deint_id == DEINT_ID_VBRS)
845 av_get_packet(pb, pkt, ast->sub_packet_lengths[ast->sub_packet_cnt - rm->audio_pkt_cnt]);
847 av_new_packet(pkt, st->codec->block_align);
848 memcpy(pkt->data, ast->pkt.data + st->codec->block_align * //FIXME avoid this
849 (ast->sub_packet_h * ast->audio_framesize / st->codec->block_align - rm->audio_pkt_cnt),
850 st->codec->block_align);
853 if ((pkt->pts = ast->audiotimestamp) != AV_NOPTS_VALUE) {
854 ast->audiotimestamp = AV_NOPTS_VALUE;
855 pkt->flags = AV_PKT_FLAG_KEY;
858 pkt->stream_index = st->index;
860 return rm->audio_pkt_cnt;
863 static int rm_read_packet(AVFormatContext *s, AVPacket *pkt)
865 RMDemuxContext *rm = s->priv_data;
867 int i, len, res, seq = 1;
868 int64_t timestamp, pos;
872 if (rm->audio_pkt_cnt) {
873 // If there are queued audio packet return them first
874 st = s->streams[rm->audio_stream_num];
875 ff_rm_retrieve_cache(s, s->pb, st, st->priv_data, pkt);
878 if (rm->old_format) {
883 timestamp = AV_NOPTS_VALUE;
884 len = !ast->audio_framesize ? RAW_PACKET_SIZE :
885 ast->coded_framesize * ast->sub_packet_h / 2;
886 flags = (seq++ == 1) ? 2 : 0;
887 pos = avio_tell(s->pb);
889 len=sync(s, ×tamp, &flags, &i, &pos);
894 if(len<0 || s->pb->eof_reached)
897 res = ff_rm_parse_packet (s, s->pb, st, st->priv_data, len, pkt,
898 &seq, flags, timestamp);
899 if((flags&2) && (seq&0x7F) == 1)
900 av_add_index_entry(st, pos, timestamp, 0, 0, AVINDEX_KEYFRAME);
905 if( (st->discard >= AVDISCARD_NONKEY && !(flags&2))
906 || st->discard >= AVDISCARD_ALL){
915 static int rm_read_close(AVFormatContext *s)
919 for (i=0;i<s->nb_streams;i++)
920 ff_rm_free_rmstream(s->streams[i]->priv_data);
925 static int rm_probe(AVProbeData *p)
927 /* check file header */
928 if ((p->buf[0] == '.' && p->buf[1] == 'R' &&
929 p->buf[2] == 'M' && p->buf[3] == 'F' &&
930 p->buf[4] == 0 && p->buf[5] == 0) ||
931 (p->buf[0] == '.' && p->buf[1] == 'r' &&
932 p->buf[2] == 'a' && p->buf[3] == 0xfd))
933 return AVPROBE_SCORE_MAX;
938 static int64_t rm_read_dts(AVFormatContext *s, int stream_index,
939 int64_t *ppos, int64_t pos_limit)
941 RMDemuxContext *rm = s->priv_data;
943 int stream_index2, flags, len, h;
948 return AV_NOPTS_VALUE;
950 avio_seek(s->pb, pos, SEEK_SET);
956 len=sync(s, &dts, &flags, &stream_index2, &pos);
958 return AV_NOPTS_VALUE;
960 st = s->streams[stream_index2];
961 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
962 h= avio_r8(s->pb); len--;
964 seq = avio_r8(s->pb); len--;
968 if((flags&2) && (seq&0x7F) == 1){
969 // av_log(s, AV_LOG_DEBUG, "%d %d-%d %"PRId64" %d\n", flags, stream_index2, stream_index, dts, seq);
970 av_add_index_entry(st, pos, dts, 0, 0, AVINDEX_KEYFRAME);
971 if(stream_index2 == stream_index)
975 avio_skip(s->pb, len);
981 AVInputFormat ff_rm_demuxer = {
983 .long_name = NULL_IF_CONFIG_SMALL("RealMedia format"),
984 .priv_data_size = sizeof(RMDemuxContext),
985 .read_probe = rm_probe,
986 .read_header = rm_read_header,
987 .read_packet = rm_read_packet,
988 .read_close = rm_read_close,
989 .read_timestamp = rm_read_dts,
992 AVInputFormat ff_rdt_demuxer = {
994 .long_name = NULL_IF_CONFIG_SMALL("RDT demuxer"),
995 .priv_data_size = sizeof(RMDemuxContext),
996 .read_close = rm_read_close,
997 .flags = AVFMT_NOFILE,