Merge remote-tracking branch 'libav/master'
[FFMpeg-mirror/mplayer-patches.git] / libavformat / rmdec.c
blob7746740df6df9b2663e49d3ebd2b809062757663
1 /*
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/channel_layout.h"
24 #include "libavutil/intreadwrite.h"
25 #include "libavutil/dict.h"
26 #include "avformat.h"
27 #include "internal.h"
28 #include "rmsipr.h"
29 #include "rm.h"
31 #define DEINT_ID_GENR MKTAG('g', 'e', 'n', 'r') ///< interleaving for Cooker/Atrac
32 #define DEINT_ID_INT0 MKTAG('I', 'n', 't', '0') ///< no interleaving needed
33 #define DEINT_ID_INT4 MKTAG('I', 'n', 't', '4') ///< interleaving for 28.8
34 #define DEINT_ID_SIPR MKTAG('s', 'i', 'p', 'r') ///< interleaving for Sipro
35 #define DEINT_ID_VBRF MKTAG('v', 'b', 'r', 'f') ///< VBR case for AAC
36 #define DEINT_ID_VBRS MKTAG('v', 'b', 'r', 's') ///< VBR case for AAC
38 struct RMStream {
39 AVPacket pkt; ///< place to store merged video frame / reordered audio data
40 int videobufsize; ///< current assembled frame size
41 int videobufpos; ///< position for the next slice in the video buffer
42 int curpic_num; ///< picture number of current frame
43 int cur_slice, slices;
44 int64_t pktpos; ///< first slice position in file
45 /// Audio descrambling matrix parameters
46 int64_t audiotimestamp; ///< Audio packet timestamp
47 int sub_packet_cnt; // Subpacket counter, used while reading
48 int sub_packet_size, sub_packet_h, coded_framesize; ///< Descrambling parameters from container
49 int audio_framesize; /// Audio frame size from container
50 int sub_packet_lengths[16]; /// Length of each subpacket
51 int32_t deint_id; ///< deinterleaver used in audio stream
54 typedef struct {
55 int nb_packets;
56 int old_format;
57 int current_stream;
58 int remaining_len;
59 int audio_stream_num; ///< Stream number for audio packets
60 int audio_pkt_cnt; ///< Output packet counter
61 } RMDemuxContext;
63 static inline void get_strl(AVIOContext *pb, char *buf, int buf_size, int len)
65 int i;
66 char *q, r;
68 q = buf;
69 for(i=0;i<len;i++) {
70 r = avio_r8(pb);
71 if (i < buf_size - 1)
72 *q++ = r;
74 if (buf_size > 0) *q = '\0';
77 static void get_str8(AVIOContext *pb, char *buf, int buf_size)
79 get_strl(pb, buf, buf_size, avio_r8(pb));
82 static int rm_read_extradata(AVIOContext *pb, AVCodecContext *avctx, unsigned size)
84 if (size >= 1<<24)
85 return -1;
86 avctx->extradata = av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE);
87 if (!avctx->extradata)
88 return AVERROR(ENOMEM);
89 avctx->extradata_size = avio_read(pb, avctx->extradata, size);
90 memset(avctx->extradata + avctx->extradata_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
91 if (avctx->extradata_size != size)
92 return AVERROR(EIO);
93 return 0;
96 static void rm_read_metadata(AVFormatContext *s, int wide)
98 char buf[1024];
99 int i;
100 for (i=0; i<FF_ARRAY_ELEMS(ff_rm_metadata); i++) {
101 int len = wide ? avio_rb16(s->pb) : avio_r8(s->pb);
102 get_strl(s->pb, buf, sizeof(buf), len);
103 av_dict_set(&s->metadata, ff_rm_metadata[i], buf, 0);
107 RMStream *ff_rm_alloc_rmstream (void)
109 RMStream *rms = av_mallocz(sizeof(RMStream));
110 rms->curpic_num = -1;
111 return rms;
114 void ff_rm_free_rmstream (RMStream *rms)
116 av_free_packet(&rms->pkt);
119 static int rm_read_audio_stream_info(AVFormatContext *s, AVIOContext *pb,
120 AVStream *st, RMStream *ast, int read_all)
122 char buf[256];
123 uint32_t version;
124 int ret;
126 /* ra type header */
127 version = avio_rb16(pb); /* version */
128 if (version == 3) {
129 int header_size = avio_rb16(pb);
130 int64_t startpos = avio_tell(pb);
131 avio_skip(pb, 14);
132 rm_read_metadata(s, 0);
133 if ((startpos + header_size) >= avio_tell(pb) + 2) {
134 // fourcc (should always be "lpcJ")
135 avio_r8(pb);
136 get_str8(pb, buf, sizeof(buf));
138 // Skip extra header crap (this should never happen)
139 if ((startpos + header_size) > avio_tell(pb))
140 avio_skip(pb, header_size + startpos - avio_tell(pb));
141 st->codec->sample_rate = 8000;
142 st->codec->channels = 1;
143 st->codec->channel_layout = AV_CH_LAYOUT_MONO;
144 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
145 st->codec->codec_id = AV_CODEC_ID_RA_144;
146 ast->deint_id = DEINT_ID_INT0;
147 } else {
148 int flavor, sub_packet_h, coded_framesize, sub_packet_size;
149 int codecdata_length;
150 /* old version (4) */
151 avio_skip(pb, 2); /* unused */
152 avio_rb32(pb); /* .ra4 */
153 avio_rb32(pb); /* data size */
154 avio_rb16(pb); /* version2 */
155 avio_rb32(pb); /* header size */
156 flavor= avio_rb16(pb); /* add codec info / flavor */
157 ast->coded_framesize = coded_framesize = avio_rb32(pb); /* coded frame size */
158 avio_rb32(pb); /* ??? */
159 avio_rb32(pb); /* ??? */
160 avio_rb32(pb); /* ??? */
161 ast->sub_packet_h = sub_packet_h = avio_rb16(pb); /* 1 */
162 st->codec->block_align= avio_rb16(pb); /* frame size */
163 ast->sub_packet_size = sub_packet_size = avio_rb16(pb); /* sub packet size */
164 avio_rb16(pb); /* ??? */
165 if (version == 5) {
166 avio_rb16(pb); avio_rb16(pb); avio_rb16(pb);
168 st->codec->sample_rate = avio_rb16(pb);
169 avio_rb32(pb);
170 st->codec->channels = avio_rb16(pb);
171 if (version == 5) {
172 ast->deint_id = avio_rl32(pb);
173 avio_read(pb, buf, 4);
174 buf[4] = 0;
175 } else {
176 get_str8(pb, buf, sizeof(buf)); /* desc */
177 ast->deint_id = AV_RL32(buf);
178 get_str8(pb, buf, sizeof(buf)); /* desc */
180 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
181 st->codec->codec_tag = AV_RL32(buf);
182 st->codec->codec_id = ff_codec_get_id(ff_rm_codec_tags,
183 st->codec->codec_tag);
185 switch (st->codec->codec_id) {
186 case AV_CODEC_ID_AC3:
187 st->need_parsing = AVSTREAM_PARSE_FULL;
188 break;
189 case AV_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 break;
194 case AV_CODEC_ID_COOK:
195 st->need_parsing = AVSTREAM_PARSE_HEADERS;
196 case AV_CODEC_ID_ATRAC3:
197 case AV_CODEC_ID_SIPR:
198 avio_rb16(pb); avio_r8(pb);
199 if (version == 5)
200 avio_r8(pb);
201 codecdata_length = avio_rb32(pb);
202 if(codecdata_length + FF_INPUT_BUFFER_PADDING_SIZE <= (unsigned)codecdata_length){
203 av_log(s, AV_LOG_ERROR, "codecdata_length too large\n");
204 return -1;
207 ast->audio_framesize = st->codec->block_align;
208 if (st->codec->codec_id == AV_CODEC_ID_SIPR) {
209 if (flavor > 3) {
210 av_log(s, AV_LOG_ERROR, "bad SIPR file flavor %d\n",
211 flavor);
212 return -1;
214 st->codec->block_align = ff_sipr_subpk_size[flavor];
215 } else {
216 if(sub_packet_size <= 0){
217 av_log(s, AV_LOG_ERROR, "sub_packet_size is invalid\n");
218 return -1;
220 st->codec->block_align = ast->sub_packet_size;
222 if ((ret = rm_read_extradata(pb, st->codec, codecdata_length)) < 0)
223 return ret;
224 break;
225 case AV_CODEC_ID_AAC:
226 avio_rb16(pb); avio_r8(pb);
227 if (version == 5)
228 avio_r8(pb);
229 codecdata_length = avio_rb32(pb);
230 if(codecdata_length + FF_INPUT_BUFFER_PADDING_SIZE <= (unsigned)codecdata_length){
231 av_log(s, AV_LOG_ERROR, "codecdata_length too large\n");
232 return -1;
234 if (codecdata_length >= 1) {
235 avio_r8(pb);
236 if ((ret = rm_read_extradata(pb, st->codec, codecdata_length - 1)) < 0)
237 return ret;
239 break;
240 default:
241 av_strlcpy(st->codec->codec_name, buf, sizeof(st->codec->codec_name));
243 if (ast->deint_id == DEINT_ID_INT4 ||
244 ast->deint_id == DEINT_ID_GENR ||
245 ast->deint_id == DEINT_ID_SIPR) {
246 if (st->codec->block_align <= 0 ||
247 ast->audio_framesize * sub_packet_h > (unsigned)INT_MAX ||
248 ast->audio_framesize * sub_packet_h < st->codec->block_align)
249 return AVERROR_INVALIDDATA;
250 if (av_new_packet(&ast->pkt, ast->audio_framesize * sub_packet_h) < 0)
251 return AVERROR(ENOMEM);
253 switch (ast->deint_id) {
254 case DEINT_ID_INT4:
255 if (ast->coded_framesize > ast->audio_framesize ||
256 sub_packet_h <= 1 ||
257 ast->coded_framesize * sub_packet_h > (2 + (sub_packet_h & 1)) * ast->audio_framesize)
258 return AVERROR_INVALIDDATA;
259 break;
260 case DEINT_ID_GENR:
261 if (ast->sub_packet_size <= 0 ||
262 ast->sub_packet_size > ast->audio_framesize)
263 return AVERROR_INVALIDDATA;
264 break;
265 case DEINT_ID_SIPR:
266 case DEINT_ID_INT0:
267 case DEINT_ID_VBRS:
268 case DEINT_ID_VBRF:
269 break;
270 default:
271 av_log(NULL,0,"Unknown interleaver %X\n", ast->deint_id);
272 return AVERROR_INVALIDDATA;
275 if (read_all) {
276 avio_r8(pb);
277 avio_r8(pb);
278 avio_r8(pb);
279 rm_read_metadata(s, 0);
282 return 0;
286 ff_rm_read_mdpr_codecdata (AVFormatContext *s, AVIOContext *pb,
287 AVStream *st, RMStream *rst, int codec_data_size)
289 unsigned int v;
290 int size;
291 int64_t codec_pos;
292 int ret;
294 avpriv_set_pts_info(st, 64, 1, 1000);
295 codec_pos = avio_tell(pb);
296 v = avio_rb32(pb);
297 if (v == MKTAG(0xfd, 'a', 'r', '.')) {
298 /* ra type header */
299 if (rm_read_audio_stream_info(s, pb, st, rst, 0))
300 return -1;
301 } else if (v == MKBETAG('L', 'S', 'D', ':')) {
302 avio_seek(pb, -4, SEEK_CUR);
303 if ((ret = rm_read_extradata(pb, st->codec, codec_data_size)) < 0)
304 return ret;
306 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
307 st->codec->codec_tag = AV_RL32(st->codec->extradata);
308 st->codec->codec_id = ff_codec_get_id(ff_rm_codec_tags,
309 st->codec->codec_tag);
310 } else {
311 int fps;
312 if (avio_rl32(pb) != MKTAG('V', 'I', 'D', 'O')) {
313 fail1:
314 av_log(st->codec, AV_LOG_ERROR, "Unsupported video codec\n");
315 goto skip;
317 st->codec->codec_tag = avio_rl32(pb);
318 st->codec->codec_id = ff_codec_get_id(ff_rm_codec_tags,
319 st->codec->codec_tag);
320 av_dlog(s, "%X %X\n", st->codec->codec_tag, MKTAG('R', 'V', '2', '0'));
321 if (st->codec->codec_id == AV_CODEC_ID_NONE)
322 goto fail1;
323 st->codec->width = avio_rb16(pb);
324 st->codec->height = avio_rb16(pb);
325 avio_skip(pb, 2); // looks like bits per sample
326 avio_skip(pb, 4); // always zero?
327 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
328 st->need_parsing = AVSTREAM_PARSE_TIMESTAMPS;
329 fps = avio_rb32(pb);
331 if ((ret = rm_read_extradata(pb, st->codec, codec_data_size - (avio_tell(pb) - codec_pos))) < 0)
332 return ret;
334 av_reduce(&st->avg_frame_rate.den, &st->avg_frame_rate.num,
335 0x10000, fps, (1 << 30) - 1);
336 #if FF_API_R_FRAME_RATE
337 st->r_frame_rate = st->avg_frame_rate;
338 #endif
341 skip:
342 /* skip codec info */
343 size = avio_tell(pb) - codec_pos;
344 avio_skip(pb, codec_data_size - size);
346 return 0;
349 /** this function assumes that the demuxer has already seeked to the start
350 * of the INDX chunk, and will bail out if not. */
351 static int rm_read_index(AVFormatContext *s)
353 AVIOContext *pb = s->pb;
354 unsigned int size, n_pkts, str_id, next_off, n, pos, pts;
355 AVStream *st;
357 do {
358 if (avio_rl32(pb) != MKTAG('I','N','D','X'))
359 return -1;
360 size = avio_rb32(pb);
361 if (size < 20)
362 return -1;
363 avio_skip(pb, 2);
364 n_pkts = avio_rb32(pb);
365 str_id = avio_rb16(pb);
366 next_off = avio_rb32(pb);
367 for (n = 0; n < s->nb_streams; n++)
368 if (s->streams[n]->id == str_id) {
369 st = s->streams[n];
370 break;
372 if (n == s->nb_streams) {
373 av_log(s, AV_LOG_ERROR,
374 "Invalid stream index %d for index at pos %"PRId64"\n",
375 str_id, avio_tell(pb));
376 goto skip;
377 } else if ((avio_size(pb) - avio_tell(pb)) / 14 < n_pkts) {
378 av_log(s, AV_LOG_ERROR,
379 "Nr. of packets in packet index for stream index %d "
380 "exceeds filesize (%"PRId64" at %"PRId64" = %"PRId64")\n",
381 str_id, avio_size(pb), avio_tell(pb),
382 (avio_size(pb) - avio_tell(pb)) / 14);
383 goto skip;
386 for (n = 0; n < n_pkts; n++) {
387 avio_skip(pb, 2);
388 pts = avio_rb32(pb);
389 pos = avio_rb32(pb);
390 avio_skip(pb, 4); /* packet no. */
392 av_add_index_entry(st, pos, pts, 0, 0, AVINDEX_KEYFRAME);
395 skip:
396 if (next_off && avio_tell(pb) < next_off &&
397 avio_seek(pb, next_off, SEEK_SET) < 0) {
398 av_log(s, AV_LOG_ERROR,
399 "Non-linear index detected, not supported\n");
400 return -1;
402 } while (next_off);
404 return 0;
407 static int rm_read_header_old(AVFormatContext *s)
409 RMDemuxContext *rm = s->priv_data;
410 AVStream *st;
412 rm->old_format = 1;
413 st = avformat_new_stream(s, NULL);
414 if (!st)
415 return -1;
416 st->priv_data = ff_rm_alloc_rmstream();
417 return rm_read_audio_stream_info(s, s->pb, st, st->priv_data, 1);
420 static int rm_read_header(AVFormatContext *s)
422 RMDemuxContext *rm = s->priv_data;
423 AVStream *st;
424 AVIOContext *pb = s->pb;
425 unsigned int tag;
426 int tag_size;
427 unsigned int start_time, duration;
428 unsigned int data_off = 0, indx_off = 0;
429 char buf[128];
430 int flags = 0;
432 tag = avio_rl32(pb);
433 if (tag == MKTAG('.', 'r', 'a', 0xfd)) {
434 /* very old .ra format */
435 return rm_read_header_old(s);
436 } else if (tag != MKTAG('.', 'R', 'M', 'F')) {
437 return AVERROR(EIO);
440 tag_size = avio_rb32(pb);
441 avio_skip(pb, tag_size - 8);
443 for(;;) {
444 if (pb->eof_reached)
445 return -1;
446 tag = avio_rl32(pb);
447 tag_size = avio_rb32(pb);
448 avio_rb16(pb);
449 av_dlog(s, "tag=%c%c%c%c (%08x) size=%d\n",
450 (tag ) & 0xff,
451 (tag >> 8) & 0xff,
452 (tag >> 16) & 0xff,
453 (tag >> 24) & 0xff,
454 tag,
455 tag_size);
456 if (tag_size < 10 && tag != MKTAG('D', 'A', 'T', 'A'))
457 return -1;
458 switch(tag) {
459 case MKTAG('P', 'R', 'O', 'P'):
460 /* file header */
461 avio_rb32(pb); /* max bit rate */
462 avio_rb32(pb); /* avg bit rate */
463 avio_rb32(pb); /* max packet size */
464 avio_rb32(pb); /* avg packet size */
465 avio_rb32(pb); /* nb packets */
466 avio_rb32(pb); /* duration */
467 avio_rb32(pb); /* preroll */
468 indx_off = avio_rb32(pb); /* index offset */
469 data_off = avio_rb32(pb); /* data offset */
470 avio_rb16(pb); /* nb streams */
471 flags = avio_rb16(pb); /* flags */
472 break;
473 case MKTAG('C', 'O', 'N', 'T'):
474 rm_read_metadata(s, 1);
475 break;
476 case MKTAG('M', 'D', 'P', 'R'):
477 st = avformat_new_stream(s, NULL);
478 if (!st)
479 return AVERROR(ENOMEM);
480 st->id = avio_rb16(pb);
481 avio_rb32(pb); /* max bit rate */
482 st->codec->bit_rate = avio_rb32(pb); /* bit rate */
483 avio_rb32(pb); /* max packet size */
484 avio_rb32(pb); /* avg packet size */
485 start_time = avio_rb32(pb); /* start time */
486 avio_rb32(pb); /* preroll */
487 duration = avio_rb32(pb); /* duration */
488 st->start_time = start_time;
489 st->duration = duration;
490 get_str8(pb, buf, sizeof(buf)); /* desc */
491 get_str8(pb, buf, sizeof(buf)); /* mimetype */
492 st->codec->codec_type = AVMEDIA_TYPE_DATA;
493 st->priv_data = ff_rm_alloc_rmstream();
494 if (ff_rm_read_mdpr_codecdata(s, s->pb, st, st->priv_data,
495 avio_rb32(pb)) < 0)
496 return -1;
497 break;
498 case MKTAG('D', 'A', 'T', 'A'):
499 goto header_end;
500 default:
501 /* unknown tag: skip it */
502 avio_skip(pb, tag_size - 10);
503 break;
506 header_end:
507 rm->nb_packets = avio_rb32(pb); /* number of packets */
508 if (!rm->nb_packets && (flags & 4))
509 rm->nb_packets = 3600 * 25;
510 avio_rb32(pb); /* next data header */
512 if (!data_off)
513 data_off = avio_tell(pb) - 18;
514 if (indx_off && pb->seekable && !(s->flags & AVFMT_FLAG_IGNIDX) &&
515 avio_seek(pb, indx_off, SEEK_SET) >= 0) {
516 rm_read_index(s);
517 avio_seek(pb, data_off + 18, SEEK_SET);
520 return 0;
523 static int get_num(AVIOContext *pb, int *len)
525 int n, n1;
527 n = avio_rb16(pb);
528 (*len)-=2;
529 n &= 0x7FFF;
530 if (n >= 0x4000) {
531 return n - 0x4000;
532 } else {
533 n1 = avio_rb16(pb);
534 (*len)-=2;
535 return (n << 16) | n1;
539 /* multiple of 20 bytes for ra144 (ugly) */
540 #define RAW_PACKET_SIZE 1000
542 static int sync(AVFormatContext *s, int64_t *timestamp, int *flags, int *stream_index, int64_t *pos){
543 RMDemuxContext *rm = s->priv_data;
544 AVIOContext *pb = s->pb;
545 AVStream *st;
546 uint32_t state=0xFFFFFFFF;
548 while(!pb->eof_reached){
549 int len, num, i;
550 *pos= avio_tell(pb) - 3;
551 if(rm->remaining_len > 0){
552 num= rm->current_stream;
553 len= rm->remaining_len;
554 *timestamp = AV_NOPTS_VALUE;
555 *flags= 0;
556 }else{
557 state= (state<<8) + avio_r8(pb);
559 if(state == MKBETAG('I', 'N', 'D', 'X')){
560 int n_pkts, expected_len;
561 len = avio_rb32(pb);
562 avio_skip(pb, 2);
563 n_pkts = avio_rb32(pb);
564 expected_len = 20 + n_pkts * 14;
565 if (len == 20)
566 /* some files don't add index entries to chunk size... */
567 len = expected_len;
568 else if (len != expected_len)
569 av_log(s, AV_LOG_WARNING,
570 "Index size %d (%d pkts) is wrong, should be %d.\n",
571 len, n_pkts, expected_len);
572 len -= 14; // we already read part of the index header
573 if(len<0)
574 continue;
575 goto skip;
576 } else if (state == MKBETAG('D','A','T','A')) {
577 av_log(s, AV_LOG_WARNING,
578 "DATA tag in middle of chunk, file may be broken.\n");
581 if(state > (unsigned)0xFFFF || state <= 12)
582 continue;
583 len=state - 12;
584 state= 0xFFFFFFFF;
586 num = avio_rb16(pb);
587 *timestamp = avio_rb32(pb);
588 avio_r8(pb); /* reserved */
589 *flags = avio_r8(pb); /* flags */
591 for(i=0;i<s->nb_streams;i++) {
592 st = s->streams[i];
593 if (num == st->id)
594 break;
596 if (i == s->nb_streams) {
597 skip:
598 /* skip packet if unknown number */
599 avio_skip(pb, len);
600 rm->remaining_len = 0;
601 continue;
603 *stream_index= i;
605 return len;
607 return -1;
610 static int rm_assemble_video_frame(AVFormatContext *s, AVIOContext *pb,
611 RMDemuxContext *rm, RMStream *vst,
612 AVPacket *pkt, int len, int *pseq,
613 int64_t *timestamp)
615 int hdr, seq, pic_num, len2, pos;
616 int type;
618 hdr = avio_r8(pb); len--;
619 type = hdr >> 6;
621 if(type != 3){ // not frame as a part of packet
622 seq = avio_r8(pb); len--;
624 if(type != 1){ // not whole frame
625 len2 = get_num(pb, &len);
626 pos = get_num(pb, &len);
627 pic_num = avio_r8(pb); len--;
629 if(len<0)
630 return -1;
631 rm->remaining_len = len;
632 if(type&1){ // frame, not slice
633 if(type == 3){ // frame as a part of packet
634 len= len2;
635 *timestamp = pos;
637 if(rm->remaining_len < len)
638 return -1;
639 rm->remaining_len -= len;
640 if(av_new_packet(pkt, len + 9) < 0)
641 return AVERROR(EIO);
642 pkt->data[0] = 0;
643 AV_WL32(pkt->data + 1, 1);
644 AV_WL32(pkt->data + 5, 0);
645 avio_read(pb, pkt->data + 9, len);
646 return 0;
648 //now we have to deal with single slice
650 *pseq = seq;
651 if((seq & 0x7F) == 1 || vst->curpic_num != pic_num){
652 vst->slices = ((hdr & 0x3F) << 1) + 1;
653 vst->videobufsize = len2 + 8*vst->slices + 1;
654 av_free_packet(&vst->pkt); //FIXME this should be output.
655 if(av_new_packet(&vst->pkt, vst->videobufsize) < 0)
656 return AVERROR(ENOMEM);
657 vst->videobufpos = 8*vst->slices + 1;
658 vst->cur_slice = 0;
659 vst->curpic_num = pic_num;
660 vst->pktpos = avio_tell(pb);
662 if(type == 2)
663 len = FFMIN(len, pos);
665 if(++vst->cur_slice > vst->slices)
666 return 1;
667 AV_WL32(vst->pkt.data - 7 + 8*vst->cur_slice, 1);
668 AV_WL32(vst->pkt.data - 3 + 8*vst->cur_slice, vst->videobufpos - 8*vst->slices - 1);
669 if(vst->videobufpos + len > vst->videobufsize)
670 return 1;
671 if (avio_read(pb, vst->pkt.data + vst->videobufpos, len) != len)
672 return AVERROR(EIO);
673 vst->videobufpos += len;
674 rm->remaining_len-= len;
676 if (type == 2 || vst->videobufpos == vst->videobufsize) {
677 vst->pkt.data[0] = vst->cur_slice-1;
678 *pkt= vst->pkt;
679 vst->pkt.data= NULL;
680 vst->pkt.size= 0;
681 vst->pkt.buf = NULL;
682 #if FF_API_DESTRUCT_PACKET
683 vst->pkt.destruct = NULL;
684 #endif
685 if(vst->slices != vst->cur_slice) //FIXME find out how to set slices correct from the begin
686 memmove(pkt->data + 1 + 8*vst->cur_slice, pkt->data + 1 + 8*vst->slices,
687 vst->videobufpos - 1 - 8*vst->slices);
688 pkt->size = vst->videobufpos + 8*(vst->cur_slice - vst->slices);
689 pkt->pts = AV_NOPTS_VALUE;
690 pkt->pos = vst->pktpos;
691 vst->slices = 0;
692 return 0;
695 return 1;
698 static inline void
699 rm_ac3_swap_bytes (AVStream *st, AVPacket *pkt)
701 uint8_t *ptr;
702 int j;
704 if (st->codec->codec_id == AV_CODEC_ID_AC3) {
705 ptr = pkt->data;
706 for (j=0;j<pkt->size;j+=2) {
707 FFSWAP(int, ptr[0], ptr[1]);
708 ptr += 2;
714 ff_rm_parse_packet (AVFormatContext *s, AVIOContext *pb,
715 AVStream *st, RMStream *ast, int len, AVPacket *pkt,
716 int *seq, int flags, int64_t timestamp)
718 RMDemuxContext *rm = s->priv_data;
720 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
721 rm->current_stream= st->id;
722 if(rm_assemble_video_frame(s, pb, rm, ast, pkt, len, seq, &timestamp))
723 return -1; //got partial frame
724 } else if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
725 if ((ast->deint_id == DEINT_ID_GENR) ||
726 (ast->deint_id == DEINT_ID_INT4) ||
727 (ast->deint_id == DEINT_ID_SIPR)) {
728 int x;
729 int sps = ast->sub_packet_size;
730 int cfs = ast->coded_framesize;
731 int h = ast->sub_packet_h;
732 int y = ast->sub_packet_cnt;
733 int w = ast->audio_framesize;
735 if (flags & 2)
736 y = ast->sub_packet_cnt = 0;
737 if (!y)
738 ast->audiotimestamp = timestamp;
740 switch (ast->deint_id) {
741 case DEINT_ID_INT4:
742 for (x = 0; x < h/2; x++)
743 avio_read(pb, ast->pkt.data+x*2*w+y*cfs, cfs);
744 break;
745 case DEINT_ID_GENR:
746 for (x = 0; x < w/sps; x++)
747 avio_read(pb, ast->pkt.data+sps*(h*x+((h+1)/2)*(y&1)+(y>>1)), sps);
748 break;
749 case DEINT_ID_SIPR:
750 avio_read(pb, ast->pkt.data + y * w, w);
751 break;
754 if (++(ast->sub_packet_cnt) < h)
755 return -1;
756 if (ast->deint_id == DEINT_ID_SIPR)
757 ff_rm_reorder_sipr_data(ast->pkt.data, h, w);
759 ast->sub_packet_cnt = 0;
760 rm->audio_stream_num = st->index;
761 rm->audio_pkt_cnt = h * w / st->codec->block_align;
762 } else if ((ast->deint_id == DEINT_ID_VBRF) ||
763 (ast->deint_id == DEINT_ID_VBRS)) {
764 int x;
765 rm->audio_stream_num = st->index;
766 ast->sub_packet_cnt = (avio_rb16(pb) & 0xf0) >> 4;
767 if (ast->sub_packet_cnt) {
768 for (x = 0; x < ast->sub_packet_cnt; x++)
769 ast->sub_packet_lengths[x] = avio_rb16(pb);
770 rm->audio_pkt_cnt = ast->sub_packet_cnt;
771 ast->audiotimestamp = timestamp;
772 } else
773 return -1;
774 } else {
775 av_get_packet(pb, pkt, len);
776 rm_ac3_swap_bytes(st, pkt);
778 } else
779 av_get_packet(pb, pkt, len);
781 pkt->stream_index = st->index;
783 #if 0
784 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
785 if(st->codec->codec_id == AV_CODEC_ID_RV20){
786 int seq= 128*(pkt->data[2]&0x7F) + (pkt->data[3]>>1);
787 av_log(s, AV_LOG_DEBUG, "%d %"PRId64" %d\n", *timestamp, *timestamp*512LL/25, seq);
789 seq |= (timestamp&~0x3FFF);
790 if(seq - timestamp > 0x2000) seq -= 0x4000;
791 if(seq - timestamp < -0x2000) seq += 0x4000;
794 #endif
796 pkt->pts = timestamp;
797 if (flags & 2)
798 pkt->flags |= AV_PKT_FLAG_KEY;
800 return st->codec->codec_type == AVMEDIA_TYPE_AUDIO ? rm->audio_pkt_cnt : 0;
804 ff_rm_retrieve_cache (AVFormatContext *s, AVIOContext *pb,
805 AVStream *st, RMStream *ast, AVPacket *pkt)
807 RMDemuxContext *rm = s->priv_data;
809 assert (rm->audio_pkt_cnt > 0);
811 if (ast->deint_id == DEINT_ID_VBRF ||
812 ast->deint_id == DEINT_ID_VBRS)
813 av_get_packet(pb, pkt, ast->sub_packet_lengths[ast->sub_packet_cnt - rm->audio_pkt_cnt]);
814 else {
815 av_new_packet(pkt, st->codec->block_align);
816 memcpy(pkt->data, ast->pkt.data + st->codec->block_align * //FIXME avoid this
817 (ast->sub_packet_h * ast->audio_framesize / st->codec->block_align - rm->audio_pkt_cnt),
818 st->codec->block_align);
820 rm->audio_pkt_cnt--;
821 if ((pkt->pts = ast->audiotimestamp) != AV_NOPTS_VALUE) {
822 ast->audiotimestamp = AV_NOPTS_VALUE;
823 pkt->flags = AV_PKT_FLAG_KEY;
824 } else
825 pkt->flags = 0;
826 pkt->stream_index = st->index;
828 return rm->audio_pkt_cnt;
831 static int rm_read_packet(AVFormatContext *s, AVPacket *pkt)
833 RMDemuxContext *rm = s->priv_data;
834 AVStream *st;
835 int i, len, res, seq = 1;
836 int64_t timestamp, pos;
837 int flags;
839 for (;;) {
840 if (rm->audio_pkt_cnt) {
841 // If there are queued audio packet return them first
842 st = s->streams[rm->audio_stream_num];
843 ff_rm_retrieve_cache(s, s->pb, st, st->priv_data, pkt);
844 flags = 0;
845 } else {
846 if (rm->old_format) {
847 RMStream *ast;
849 st = s->streams[0];
850 ast = st->priv_data;
851 timestamp = AV_NOPTS_VALUE;
852 len = !ast->audio_framesize ? RAW_PACKET_SIZE :
853 ast->coded_framesize * ast->sub_packet_h / 2;
854 flags = (seq++ == 1) ? 2 : 0;
855 pos = avio_tell(s->pb);
856 } else {
857 len=sync(s, &timestamp, &flags, &i, &pos);
858 if (len > 0)
859 st = s->streams[i];
862 if(len<0 || s->pb->eof_reached)
863 return AVERROR(EIO);
865 res = ff_rm_parse_packet (s, s->pb, st, st->priv_data, len, pkt,
866 &seq, flags, timestamp);
867 if((flags&2) && (seq&0x7F) == 1)
868 av_add_index_entry(st, pos, timestamp, 0, 0, AVINDEX_KEYFRAME);
869 if (res)
870 continue;
873 if( (st->discard >= AVDISCARD_NONKEY && !(flags&2))
874 || st->discard >= AVDISCARD_ALL){
875 av_free_packet(pkt);
876 } else
877 break;
880 return 0;
883 static int rm_read_close(AVFormatContext *s)
885 int i;
887 for (i=0;i<s->nb_streams;i++)
888 ff_rm_free_rmstream(s->streams[i]->priv_data);
890 return 0;
893 static int rm_probe(AVProbeData *p)
895 /* check file header */
896 if ((p->buf[0] == '.' && p->buf[1] == 'R' &&
897 p->buf[2] == 'M' && p->buf[3] == 'F' &&
898 p->buf[4] == 0 && p->buf[5] == 0) ||
899 (p->buf[0] == '.' && p->buf[1] == 'r' &&
900 p->buf[2] == 'a' && p->buf[3] == 0xfd))
901 return AVPROBE_SCORE_MAX;
902 else
903 return 0;
906 static int64_t rm_read_dts(AVFormatContext *s, int stream_index,
907 int64_t *ppos, int64_t pos_limit)
909 RMDemuxContext *rm = s->priv_data;
910 int64_t pos, dts;
911 int stream_index2, flags, len, h;
913 pos = *ppos;
915 if(rm->old_format)
916 return AV_NOPTS_VALUE;
918 avio_seek(s->pb, pos, SEEK_SET);
919 rm->remaining_len=0;
920 for(;;){
921 int seq=1;
922 AVStream *st;
924 len=sync(s, &dts, &flags, &stream_index2, &pos);
925 if(len<0)
926 return AV_NOPTS_VALUE;
928 st = s->streams[stream_index2];
929 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
930 h= avio_r8(s->pb); len--;
931 if(!(h & 0x40)){
932 seq = avio_r8(s->pb); len--;
936 if((flags&2) && (seq&0x7F) == 1){
937 av_dlog(s, "%d %d-%d %"PRId64" %d\n",
938 flags, stream_index2, stream_index, dts, seq);
939 av_add_index_entry(st, pos, dts, 0, 0, AVINDEX_KEYFRAME);
940 if(stream_index2 == stream_index)
941 break;
944 avio_skip(s->pb, len);
946 *ppos = pos;
947 return dts;
950 AVInputFormat ff_rm_demuxer = {
951 .name = "rm",
952 .long_name = NULL_IF_CONFIG_SMALL("RealMedia"),
953 .priv_data_size = sizeof(RMDemuxContext),
954 .read_probe = rm_probe,
955 .read_header = rm_read_header,
956 .read_packet = rm_read_packet,
957 .read_close = rm_read_close,
958 .read_timestamp = rm_read_dts,
961 AVInputFormat ff_rdt_demuxer = {
962 .name = "rdt",
963 .long_name = NULL_IF_CONFIG_SMALL("RDT demuxer"),
964 .priv_data_size = sizeof(RMDemuxContext),
965 .read_close = rm_read_close,
966 .flags = AVFMT_NOFILE,