FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
rmdec.c
Go to the documentation of this file.
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/avassert.h"
23 #include "libavutil/avstring.h"
25 #include "libavutil/intreadwrite.h"
26 #include "libavutil/dict.h"
27 #include "avformat.h"
28 #include "avio_internal.h"
29 #include "internal.h"
30 #include "rmsipr.h"
31 #include "rm.h"
32 
33 #define DEINT_ID_GENR MKTAG('g', 'e', 'n', 'r') ///< interleaving for Cooker/Atrac
34 #define DEINT_ID_INT0 MKTAG('I', 'n', 't', '0') ///< no interleaving needed
35 #define DEINT_ID_INT4 MKTAG('I', 'n', 't', '4') ///< interleaving for 28.8
36 #define DEINT_ID_SIPR MKTAG('s', 'i', 'p', 'r') ///< interleaving for Sipro
37 #define DEINT_ID_VBRF MKTAG('v', 'b', 'r', 'f') ///< VBR case for AAC
38 #define DEINT_ID_VBRS MKTAG('v', 'b', 'r', 's') ///< VBR case for AAC
39 
40 struct RMStream {
41  AVPacket pkt; ///< place to store merged video frame / reordered audio data
42  int videobufsize; ///< current assembled frame size
43  int videobufpos; ///< position for the next slice in the video buffer
44  int curpic_num; ///< picture number of current frame
46  int64_t pktpos; ///< first slice position in file
47  /// Audio descrambling matrix parameters
48  int64_t audiotimestamp; ///< Audio packet timestamp
49  int sub_packet_cnt; // Subpacket counter, used while reading
50  int sub_packet_size, sub_packet_h, coded_framesize; ///< Descrambling parameters from container
51  int audio_framesize; /// Audio frame size from container
52  int sub_packet_lengths[16]; /// Length of each subpacket
53  int32_t deint_id; ///< deinterleaver used in audio stream
54 };
55 
56 typedef struct {
61  int audio_stream_num; ///< Stream number for audio packets
62  int audio_pkt_cnt; ///< Output packet counter
64 
65 static inline void get_strl(AVIOContext *pb, char *buf, int buf_size, int len)
66 {
67  int i;
68  char *q, r;
69 
70  q = buf;
71  for(i=0;i<len;i++) {
72  r = avio_r8(pb);
73  if (i < buf_size - 1)
74  *q++ = r;
75  }
76  if (buf_size > 0) *q = '\0';
77 }
78 
79 static void get_str8(AVIOContext *pb, char *buf, int buf_size)
80 {
81  get_strl(pb, buf, buf_size, avio_r8(pb));
82 }
83 
84 static int rm_read_extradata(AVIOContext *pb, AVCodecContext *avctx, unsigned size)
85 {
86  if (size >= 1<<24)
87  return -1;
89  if (!avctx->extradata)
90  return AVERROR(ENOMEM);
91  avctx->extradata_size = avio_read(pb, avctx->extradata, size);
92  memset(avctx->extradata + avctx->extradata_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
93  if (avctx->extradata_size != size)
94  return AVERROR(EIO);
95  return 0;
96 }
97 
98 static void rm_read_metadata(AVFormatContext *s, AVIOContext *pb, int wide)
99 {
100  char buf[1024];
101  int i;
102 
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);
107  }
108 }
109 
111 {
112  RMStream *rms = av_mallocz(sizeof(RMStream));
113  rms->curpic_num = -1;
114  return rms;
115 }
116 
118 {
119  av_free_packet(&rms->pkt);
120 }
121 
123  AVStream *st, RMStream *ast, int read_all)
124 {
125  char buf[256];
126  uint32_t version;
127  int ret;
128 
129  /* ra type header */
130  version = avio_rb16(pb); /* version */
131  if (version == 3) {
132  unsigned bytes_per_minute;
133  int header_size = avio_rb16(pb);
134  int64_t startpos = avio_tell(pb);
135  avio_skip(pb, 8);
136  bytes_per_minute = avio_rb16(pb);
137  avio_skip(pb, 4);
138  rm_read_metadata(s, pb, 0);
139  if ((startpos + header_size) >= avio_tell(pb) + 2) {
140  // fourcc (should always be "lpcJ")
141  avio_r8(pb);
142  get_str8(pb, buf, sizeof(buf));
143  }
144  // Skip extra header crap (this should never happen)
145  if ((startpos + header_size) > avio_tell(pb))
146  avio_skip(pb, header_size + startpos - avio_tell(pb));
147  if (bytes_per_minute)
148  st->codec->bit_rate = 8LL * bytes_per_minute / 60;
149  st->codec->sample_rate = 8000;
150  st->codec->channels = 1;
154  ast->deint_id = DEINT_ID_INT0;
155  } else {
156  int flavor, sub_packet_h, coded_framesize, sub_packet_size;
157  int codecdata_length;
158  unsigned bytes_per_minute;
159  /* old version (4) */
160  avio_skip(pb, 2); /* unused */
161  avio_rb32(pb); /* .ra4 */
162  avio_rb32(pb); /* data size */
163  avio_rb16(pb); /* version2 */
164  avio_rb32(pb); /* header size */
165  flavor= avio_rb16(pb); /* add codec info / flavor */
166  ast->coded_framesize = coded_framesize = avio_rb32(pb); /* coded frame size */
167  avio_rb32(pb); /* ??? */
168  bytes_per_minute = avio_rb32(pb);
169  if (version == 4) {
170  if (bytes_per_minute)
171  st->codec->bit_rate = 8LL * bytes_per_minute / 60;
172  }
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); /* ??? */
178  if (version == 5) {
179  avio_rb16(pb); avio_rb16(pb); avio_rb16(pb);
180  }
181  st->codec->sample_rate = avio_rb16(pb);
182  avio_rb32(pb);
183  st->codec->channels = avio_rb16(pb);
184  if (version == 5) {
185  ast->deint_id = avio_rl32(pb);
186  avio_read(pb, buf, 4);
187  buf[4] = 0;
188  } else {
189  get_str8(pb, buf, sizeof(buf)); /* desc */
190  ast->deint_id = AV_RL32(buf);
191  get_str8(pb, buf, sizeof(buf)); /* desc */
192  }
194  st->codec->codec_tag = AV_RL32(buf);
196  st->codec->codec_tag);
197 
198  switch (st->codec->codec_id) {
199  case AV_CODEC_ID_AC3:
201  break;
202  case AV_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;
206  break;
207  case AV_CODEC_ID_COOK:
209  case AV_CODEC_ID_ATRAC3:
210  case AV_CODEC_ID_SIPR:
211  if (read_all) {
212  codecdata_length = 0;
213  } else {
214  avio_rb16(pb); avio_r8(pb);
215  if (version == 5)
216  avio_r8(pb);
217  codecdata_length = avio_rb32(pb);
218  if(codecdata_length + FF_INPUT_BUFFER_PADDING_SIZE <= (unsigned)codecdata_length){
219  av_log(s, AV_LOG_ERROR, "codecdata_length too large\n");
220  return -1;
221  }
222  }
223 
224  ast->audio_framesize = st->codec->block_align;
225  if (st->codec->codec_id == AV_CODEC_ID_SIPR) {
226  if (flavor > 3) {
227  av_log(s, AV_LOG_ERROR, "bad SIPR file flavor %d\n",
228  flavor);
229  return -1;
230  }
231  st->codec->block_align = ff_sipr_subpk_size[flavor];
232  } else {
233  if(sub_packet_size <= 0){
234  av_log(s, AV_LOG_ERROR, "sub_packet_size is invalid\n");
235  return -1;
236  }
237  st->codec->block_align = ast->sub_packet_size;
238  }
239  if ((ret = rm_read_extradata(pb, st->codec, codecdata_length)) < 0)
240  return ret;
241 
242  break;
243  case AV_CODEC_ID_AAC:
244  avio_rb16(pb); avio_r8(pb);
245  if (version == 5)
246  avio_r8(pb);
247  codecdata_length = avio_rb32(pb);
248  if(codecdata_length + FF_INPUT_BUFFER_PADDING_SIZE <= (unsigned)codecdata_length){
249  av_log(s, AV_LOG_ERROR, "codecdata_length too large\n");
250  return -1;
251  }
252  if (codecdata_length >= 1) {
253  avio_r8(pb);
254  if ((ret = rm_read_extradata(pb, st->codec, codecdata_length - 1)) < 0)
255  return ret;
256  }
257  break;
258  default:
259  av_strlcpy(st->codec->codec_name, buf, sizeof(st->codec->codec_name));
260  }
261  if (ast->deint_id == DEINT_ID_INT4 ||
262  ast->deint_id == DEINT_ID_GENR ||
263  ast->deint_id == DEINT_ID_SIPR) {
264  if (st->codec->block_align <= 0 ||
265  ast->audio_framesize * sub_packet_h > (unsigned)INT_MAX ||
266  ast->audio_framesize * sub_packet_h < st->codec->block_align)
267  return AVERROR_INVALIDDATA;
268  if (av_new_packet(&ast->pkt, ast->audio_framesize * sub_packet_h) < 0)
269  return AVERROR(ENOMEM);
270  }
271  switch (ast->deint_id) {
272  case DEINT_ID_INT4:
273  if (ast->coded_framesize > ast->audio_framesize ||
274  sub_packet_h <= 1 ||
275  ast->coded_framesize * sub_packet_h > (2 + (sub_packet_h & 1)) * ast->audio_framesize)
276  return AVERROR_INVALIDDATA;
277  break;
278  case DEINT_ID_GENR:
279  if (ast->sub_packet_size <= 0 ||
280  ast->sub_packet_size > ast->audio_framesize)
281  return AVERROR_INVALIDDATA;
282  break;
283  case DEINT_ID_SIPR:
284  case DEINT_ID_INT0:
285  case DEINT_ID_VBRS:
286  case DEINT_ID_VBRF:
287  break;
288  default:
289  av_log(s, AV_LOG_ERROR, "Unknown interleaver %X\n", ast->deint_id);
290  return AVERROR_INVALIDDATA;
291  }
292 
293  if (read_all) {
294  avio_r8(pb);
295  avio_r8(pb);
296  avio_r8(pb);
297  rm_read_metadata(s, pb, 0);
298  }
299  }
300  return 0;
301 }
302 
303 int
305  AVStream *st, RMStream *rst, int codec_data_size, const uint8_t *mime)
306 {
307  unsigned int v;
308  int size;
309  int64_t codec_pos;
310  int ret;
311 
312  avpriv_set_pts_info(st, 64, 1, 1000);
313  codec_pos = avio_tell(pb);
314  v = avio_rb32(pb);
315  if (v == MKTAG(0xfd, 'a', 'r', '.')) {
316  /* ra type header */
317  if (rm_read_audio_stream_info(s, pb, st, rst, 0))
318  return -1;
319  } else if (v == MKBETAG('L', 'S', 'D', ':')) {
320  avio_seek(pb, -4, SEEK_CUR);
321  if ((ret = rm_read_extradata(pb, st->codec, codec_data_size)) < 0)
322  return ret;
323 
325  st->codec->codec_tag = AV_RL32(st->codec->extradata);
327  st->codec->codec_tag);
328  } else if(mime && !strcmp(mime, "logical-fileinfo")){
329  int stream_count, rule_count, property_count, i;
330  ff_free_stream(s, st);
331  if (avio_rb16(pb) != 0) {
332  av_log(s, AV_LOG_WARNING, "Unsupported version\n");
333  goto skip;
334  }
335  stream_count = avio_rb16(pb);
336  avio_skip(pb, 6*stream_count);
337  rule_count = avio_rb16(pb);
338  avio_skip(pb, 2*rule_count);
339  property_count = avio_rb16(pb);
340  for(i=0; i<property_count; i++){
341  uint8_t name[128], val[128];
342  avio_rb32(pb);
343  if (avio_rb16(pb) != 0) {
344  av_log(s, AV_LOG_WARNING, "Unsupported Name value property version\n");
345  goto skip; //FIXME skip just this one
346  }
347  get_str8(pb, name, sizeof(name));
348  switch(avio_rb32(pb)) {
349  case 2: get_strl(pb, val, sizeof(val), avio_rb16(pb));
350  av_dict_set(&s->metadata, name, val, 0);
351  break;
352  default: avio_skip(pb, avio_rb16(pb));
353  }
354  }
355  } else {
356  int fps;
357  if (avio_rl32(pb) != MKTAG('V', 'I', 'D', 'O')) {
358  fail1:
359  av_log(s, AV_LOG_WARNING, "Unsupported stream type %08x\n", v);
360  goto skip;
361  }
362  st->codec->codec_tag = avio_rl32(pb);
364  st->codec->codec_tag);
365  av_dlog(s, "%X %X\n", st->codec->codec_tag, MKTAG('R', 'V', '2', '0'));
366  if (st->codec->codec_id == AV_CODEC_ID_NONE)
367  goto fail1;
368  st->codec->width = avio_rb16(pb);
369  st->codec->height = avio_rb16(pb);
370  avio_skip(pb, 2); // looks like bits per sample
371  avio_skip(pb, 4); // always zero?
374  fps = avio_rb32(pb);
375 
376  if ((ret = rm_read_extradata(pb, st->codec, codec_data_size - (avio_tell(pb) - codec_pos))) < 0)
377  return ret;
378 
380  0x10000, fps, (1 << 30) - 1);
381 #if FF_API_R_FRAME_RATE
382  st->r_frame_rate = st->avg_frame_rate;
383 #endif
384  }
385 
386 skip:
387  /* skip codec info */
388  size = avio_tell(pb) - codec_pos;
389  avio_skip(pb, codec_data_size - size);
390 
391  return 0;
392 }
393 
394 /** this function assumes that the demuxer has already seeked to the start
395  * of the INDX chunk, and will bail out if not. */
397 {
398  AVIOContext *pb = s->pb;
399  unsigned int size, n_pkts, str_id, next_off, n, pos, pts;
400  AVStream *st;
401 
402  do {
403  if (avio_rl32(pb) != MKTAG('I','N','D','X'))
404  return -1;
405  size = avio_rb32(pb);
406  if (size < 20)
407  return -1;
408  avio_skip(pb, 2);
409  n_pkts = avio_rb32(pb);
410  str_id = avio_rb16(pb);
411  next_off = avio_rb32(pb);
412  for (n = 0; n < s->nb_streams; n++)
413  if (s->streams[n]->id == str_id) {
414  st = s->streams[n];
415  break;
416  }
417  if (n == s->nb_streams) {
418  av_log(s, AV_LOG_ERROR,
419  "Invalid stream index %d for index at pos %"PRId64"\n",
420  str_id, avio_tell(pb));
421  goto skip;
422  } else if ((avio_size(pb) - avio_tell(pb)) / 14 < n_pkts) {
423  av_log(s, AV_LOG_ERROR,
424  "Nr. of packets in packet index for stream index %d "
425  "exceeds filesize (%"PRId64" at %"PRId64" = %"PRId64")\n",
426  str_id, avio_size(pb), avio_tell(pb),
427  (avio_size(pb) - avio_tell(pb)) / 14);
428  goto skip;
429  }
430 
431  for (n = 0; n < n_pkts; n++) {
432  avio_skip(pb, 2);
433  pts = avio_rb32(pb);
434  pos = avio_rb32(pb);
435  avio_skip(pb, 4); /* packet no. */
436 
437  av_add_index_entry(st, pos, pts, 0, 0, AVINDEX_KEYFRAME);
438  }
439 
440 skip:
441  if (next_off && avio_tell(pb) < next_off &&
442  avio_seek(pb, next_off, SEEK_SET) < 0) {
443  av_log(s, AV_LOG_ERROR,
444  "Non-linear index detected, not supported\n");
445  return -1;
446  }
447  } while (next_off);
448 
449  return 0;
450 }
451 
453 {
454  RMDemuxContext *rm = s->priv_data;
455  AVStream *st;
456 
457  rm->old_format = 1;
458  st = avformat_new_stream(s, NULL);
459  if (!st)
460  return -1;
462  return rm_read_audio_stream_info(s, s->pb, st, st->priv_data, 1);
463 }
464 
466 {
467  RMDemuxContext *rm = s->priv_data;
468  AVStream *st;
469  AVIOContext *pb = s->pb;
470  unsigned int tag;
471  int tag_size;
472  unsigned int start_time, duration;
473  unsigned int data_off = 0, indx_off = 0;
474  char buf[128], mime[128];
475  int flags = 0;
476 
477  tag = avio_rl32(pb);
478  if (tag == MKTAG('.', 'r', 'a', 0xfd)) {
479  /* very old .ra format */
480  return rm_read_header_old(s);
481  } else if (tag != MKTAG('.', 'R', 'M', 'F')) {
482  return AVERROR(EIO);
483  }
484 
485  tag_size = avio_rb32(pb);
486  avio_skip(pb, tag_size - 8);
487 
488  for(;;) {
489  if (url_feof(pb))
490  return -1;
491  tag = avio_rl32(pb);
492  tag_size = avio_rb32(pb);
493  avio_rb16(pb);
494  av_dlog(s, "tag=%c%c%c%c (%08x) size=%d\n",
495  (tag ) & 0xff,
496  (tag >> 8) & 0xff,
497  (tag >> 16) & 0xff,
498  (tag >> 24) & 0xff,
499  tag,
500  tag_size);
501  if (tag_size < 10 && tag != MKTAG('D', 'A', 'T', 'A'))
502  return -1;
503  switch(tag) {
504  case MKTAG('P', 'R', 'O', 'P'):
505  /* file header */
506  avio_rb32(pb); /* max bit rate */
507  avio_rb32(pb); /* avg bit rate */
508  avio_rb32(pb); /* max packet size */
509  avio_rb32(pb); /* avg packet size */
510  avio_rb32(pb); /* nb packets */
511  duration = avio_rb32(pb); /* duration */
512  s->duration = av_rescale(duration, AV_TIME_BASE, 1000);
513  avio_rb32(pb); /* preroll */
514  indx_off = avio_rb32(pb); /* index offset */
515  data_off = avio_rb32(pb); /* data offset */
516  avio_rb16(pb); /* nb streams */
517  flags = avio_rb16(pb); /* flags */
518  break;
519  case MKTAG('C', 'O', 'N', 'T'):
520  rm_read_metadata(s, pb, 1);
521  break;
522  case MKTAG('M', 'D', 'P', 'R'):
523  st = avformat_new_stream(s, NULL);
524  if (!st)
525  return AVERROR(ENOMEM);
526  st->id = avio_rb16(pb);
527  avio_rb32(pb); /* max bit rate */
528  st->codec->bit_rate = avio_rb32(pb); /* bit rate */
529  avio_rb32(pb); /* max packet size */
530  avio_rb32(pb); /* avg packet size */
531  start_time = avio_rb32(pb); /* start time */
532  avio_rb32(pb); /* preroll */
533  duration = avio_rb32(pb); /* duration */
534  st->start_time = start_time;
535  st->duration = duration;
536  if(duration>0)
538  get_str8(pb, buf, sizeof(buf)); /* desc */
539  get_str8(pb, mime, sizeof(mime)); /* mimetype */
542  if (ff_rm_read_mdpr_codecdata(s, s->pb, st, st->priv_data,
543  avio_rb32(pb), mime) < 0)
544  return -1;
545  break;
546  case MKTAG('D', 'A', 'T', 'A'):
547  goto header_end;
548  default:
549  /* unknown tag: skip it */
550  avio_skip(pb, tag_size - 10);
551  break;
552  }
553  }
554  header_end:
555  rm->nb_packets = avio_rb32(pb); /* number of packets */
556  if (!rm->nb_packets && (flags & 4))
557  rm->nb_packets = 3600 * 25;
558  avio_rb32(pb); /* next data header */
559 
560  if (!data_off)
561  data_off = avio_tell(pb) - 18;
562  if (indx_off && pb->seekable && !(s->flags & AVFMT_FLAG_IGNIDX) &&
563  avio_seek(pb, indx_off, SEEK_SET) >= 0) {
564  rm_read_index(s);
565  avio_seek(pb, data_off + 18, SEEK_SET);
566  }
567 
568  return 0;
569 }
570 
571 static int get_num(AVIOContext *pb, int *len)
572 {
573  int n, n1;
574 
575  n = avio_rb16(pb);
576  (*len)-=2;
577  n &= 0x7FFF;
578  if (n >= 0x4000) {
579  return n - 0x4000;
580  } else {
581  n1 = avio_rb16(pb);
582  (*len)-=2;
583  return (n << 16) | n1;
584  }
585 }
586 
587 /* multiple of 20 bytes for ra144 (ugly) */
588 #define RAW_PACKET_SIZE 1000
589 
590 static int sync(AVFormatContext *s, int64_t *timestamp, int *flags, int *stream_index, int64_t *pos){
591  RMDemuxContext *rm = s->priv_data;
592  AVIOContext *pb = s->pb;
593  AVStream *st;
594  uint32_t state=0xFFFFFFFF;
595 
596  while(!url_feof(pb)){
597  int len, num, i;
598  *pos= avio_tell(pb) - 3;
599  if(rm->remaining_len > 0){
600  num= rm->current_stream;
601  len= rm->remaining_len;
602  *timestamp = AV_NOPTS_VALUE;
603  *flags= 0;
604  }else{
605  state= (state<<8) + avio_r8(pb);
606 
607  if(state == MKBETAG('I', 'N', 'D', 'X')){
608  int n_pkts, expected_len;
609  len = avio_rb32(pb);
610  avio_skip(pb, 2);
611  n_pkts = avio_rb32(pb);
612  expected_len = 20 + n_pkts * 14;
613  if (len == 20)
614  /* some files don't add index entries to chunk size... */
615  len = expected_len;
616  else if (len != expected_len)
618  "Index size %d (%d pkts) is wrong, should be %d.\n",
619  len, n_pkts, expected_len);
620  len -= 14; // we already read part of the index header
621  if(len<0)
622  continue;
623  goto skip;
624  } else if (state == MKBETAG('D','A','T','A')) {
626  "DATA tag in middle of chunk, file may be broken.\n");
627  }
628 
629  if(state > (unsigned)0xFFFF || state <= 12)
630  continue;
631  len=state - 12;
632  state= 0xFFFFFFFF;
633 
634  num = avio_rb16(pb);
635  *timestamp = avio_rb32(pb);
636  avio_r8(pb); /* reserved */
637  *flags = avio_r8(pb); /* flags */
638  }
639  for(i=0;i<s->nb_streams;i++) {
640  st = s->streams[i];
641  if (num == st->id)
642  break;
643  }
644  if (i == s->nb_streams) {
645 skip:
646  /* skip packet if unknown number */
647  avio_skip(pb, len);
648  rm->remaining_len = 0;
649  continue;
650  }
651  *stream_index= i;
652 
653  return len;
654  }
655  return -1;
656 }
657 
659  RMDemuxContext *rm, RMStream *vst,
660  AVPacket *pkt, int len, int *pseq,
661  int64_t *timestamp)
662 {
663  int hdr;
664  int seq = 0, pic_num = 0, len2 = 0, pos = 0; //init to silcense compiler warning
665  int type;
666  int ret;
667 
668  hdr = avio_r8(pb); len--;
669  type = hdr >> 6;
670 
671  if(type != 3){ // not frame as a part of packet
672  seq = avio_r8(pb); len--;
673  }
674  if(type != 1){ // not whole frame
675  len2 = get_num(pb, &len);
676  pos = get_num(pb, &len);
677  pic_num = avio_r8(pb); len--;
678  }
679  if(len<0)
680  return -1;
681  rm->remaining_len = len;
682  if(type&1){ // frame, not slice
683  if(type == 3){ // frame as a part of packet
684  len= len2;
685  *timestamp = pos;
686  }
687  if(rm->remaining_len < len)
688  return -1;
689  rm->remaining_len -= len;
690  if(av_new_packet(pkt, len + 9) < 0)
691  return AVERROR(EIO);
692  pkt->data[0] = 0;
693  AV_WL32(pkt->data + 1, 1);
694  AV_WL32(pkt->data + 5, 0);
695  if ((ret = avio_read(pb, pkt->data + 9, len)) != len) {
696  av_free_packet(pkt);
697  return ret < 0 ? ret : AVERROR(EIO);
698  }
699  return 0;
700  }
701  //now we have to deal with single slice
702 
703  *pseq = seq;
704  if((seq & 0x7F) == 1 || vst->curpic_num != pic_num){
705  if (len2 > ffio_limit(pb, len2)) {
706  av_log(s, AV_LOG_ERROR, "Impossibly sized packet\n");
707  return AVERROR_INVALIDDATA;
708  }
709  vst->slices = ((hdr & 0x3F) << 1) + 1;
710  vst->videobufsize = len2 + 8*vst->slices + 1;
711  av_free_packet(&vst->pkt); //FIXME this should be output.
712  if(av_new_packet(&vst->pkt, vst->videobufsize) < 0)
713  return AVERROR(ENOMEM);
714  memset(vst->pkt.data, 0, vst->pkt.size);
715  vst->videobufpos = 8*vst->slices + 1;
716  vst->cur_slice = 0;
717  vst->curpic_num = pic_num;
718  vst->pktpos = avio_tell(pb);
719  }
720  if(type == 2)
721  len = FFMIN(len, pos);
722 
723  if(++vst->cur_slice > vst->slices)
724  return 1;
725  AV_WL32(vst->pkt.data - 7 + 8*vst->cur_slice, 1);
726  AV_WL32(vst->pkt.data - 3 + 8*vst->cur_slice, vst->videobufpos - 8*vst->slices - 1);
727  if(vst->videobufpos + len > vst->videobufsize)
728  return 1;
729  if (avio_read(pb, vst->pkt.data + vst->videobufpos, len) != len)
730  return AVERROR(EIO);
731  vst->videobufpos += len;
732  rm->remaining_len-= len;
733 
734  if (type == 2 || vst->videobufpos == vst->videobufsize) {
735  vst->pkt.data[0] = vst->cur_slice-1;
736  *pkt= vst->pkt;
737  vst->pkt.data= NULL;
738  vst->pkt.size= 0;
739  vst->pkt.buf = NULL;
740 #if FF_API_DESTRUCT_PACKET
741  vst->pkt.destruct = NULL;
742 #endif
743  if(vst->slices != vst->cur_slice) //FIXME find out how to set slices correct from the begin
744  memmove(pkt->data + 1 + 8*vst->cur_slice, pkt->data + 1 + 8*vst->slices,
745  vst->videobufpos - 1 - 8*vst->slices);
746  pkt->size = vst->videobufpos + 8*(vst->cur_slice - vst->slices);
747  pkt->pts = AV_NOPTS_VALUE;
748  pkt->pos = vst->pktpos;
749  vst->slices = 0;
750  return 0;
751  }
752 
753  return 1;
754 }
755 
756 static inline void
758 {
759  uint8_t *ptr;
760  int j;
761 
762  if (st->codec->codec_id == AV_CODEC_ID_AC3) {
763  ptr = pkt->data;
764  for (j=0;j<pkt->size;j+=2) {
765  FFSWAP(int, ptr[0], ptr[1]);
766  ptr += 2;
767  }
768  }
769 }
770 
771 int
773  AVStream *st, RMStream *ast, int len, AVPacket *pkt,
774  int *seq, int flags, int64_t timestamp)
775 {
776  RMDemuxContext *rm = s->priv_data;
777 
778  if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
779  rm->current_stream= st->id;
780  if(rm_assemble_video_frame(s, pb, rm, ast, pkt, len, seq, &timestamp))
781  return -1; //got partial frame
782  } else if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
783  if ((ast->deint_id == DEINT_ID_GENR) ||
784  (ast->deint_id == DEINT_ID_INT4) ||
785  (ast->deint_id == DEINT_ID_SIPR)) {
786  int x;
787  int sps = ast->sub_packet_size;
788  int cfs = ast->coded_framesize;
789  int h = ast->sub_packet_h;
790  int y = ast->sub_packet_cnt;
791  int w = ast->audio_framesize;
792 
793  if (flags & 2)
794  y = ast->sub_packet_cnt = 0;
795  if (!y)
796  ast->audiotimestamp = timestamp;
797 
798  switch (ast->deint_id) {
799  case DEINT_ID_INT4:
800  for (x = 0; x < h/2; x++)
801  avio_read(pb, ast->pkt.data+x*2*w+y*cfs, cfs);
802  break;
803  case DEINT_ID_GENR:
804  for (x = 0; x < w/sps; x++)
805  avio_read(pb, ast->pkt.data+sps*(h*x+((h+1)/2)*(y&1)+(y>>1)), sps);
806  break;
807  case DEINT_ID_SIPR:
808  avio_read(pb, ast->pkt.data + y * w, w);
809  break;
810  }
811 
812  if (++(ast->sub_packet_cnt) < h)
813  return -1;
814  if (ast->deint_id == DEINT_ID_SIPR)
815  ff_rm_reorder_sipr_data(ast->pkt.data, h, w);
816 
817  ast->sub_packet_cnt = 0;
818  rm->audio_stream_num = st->index;
819  rm->audio_pkt_cnt = h * w / st->codec->block_align;
820  } else if ((ast->deint_id == DEINT_ID_VBRF) ||
821  (ast->deint_id == DEINT_ID_VBRS)) {
822  int x;
823  rm->audio_stream_num = st->index;
824  ast->sub_packet_cnt = (avio_rb16(pb) & 0xf0) >> 4;
825  if (ast->sub_packet_cnt) {
826  for (x = 0; x < ast->sub_packet_cnt; x++)
827  ast->sub_packet_lengths[x] = avio_rb16(pb);
828  rm->audio_pkt_cnt = ast->sub_packet_cnt;
829  ast->audiotimestamp = timestamp;
830  } else
831  return -1;
832  } else {
833  av_get_packet(pb, pkt, len);
834  rm_ac3_swap_bytes(st, pkt);
835  }
836  } else
837  av_get_packet(pb, pkt, len);
838 
839  pkt->stream_index = st->index;
840 
841 #if 0
842  if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
843  if(st->codec->codec_id == AV_CODEC_ID_RV20){
844  int seq= 128*(pkt->data[2]&0x7F) + (pkt->data[3]>>1);
845  av_log(s, AV_LOG_DEBUG, "%d %"PRId64" %d\n", *timestamp, *timestamp*512LL/25, seq);
846 
847  seq |= (timestamp&~0x3FFF);
848  if(seq - timestamp > 0x2000) seq -= 0x4000;
849  if(seq - timestamp < -0x2000) seq += 0x4000;
850  }
851  }
852 #endif
853 
854  pkt->pts = timestamp;
855  if (flags & 2)
856  pkt->flags |= AV_PKT_FLAG_KEY;
857 
858  return st->codec->codec_type == AVMEDIA_TYPE_AUDIO ? rm->audio_pkt_cnt : 0;
859 }
860 
861 int
863  AVStream *st, RMStream *ast, AVPacket *pkt)
864 {
865  RMDemuxContext *rm = s->priv_data;
866 
867  av_assert0 (rm->audio_pkt_cnt > 0);
868 
869  if (ast->deint_id == DEINT_ID_VBRF ||
870  ast->deint_id == DEINT_ID_VBRS)
872  else {
873  if(av_new_packet(pkt, st->codec->block_align) < 0)
874  return AVERROR(ENOMEM);
875  memcpy(pkt->data, ast->pkt.data + st->codec->block_align * //FIXME avoid this
876  (ast->sub_packet_h * ast->audio_framesize / st->codec->block_align - rm->audio_pkt_cnt),
877  st->codec->block_align);
878  }
879  rm->audio_pkt_cnt--;
880  if ((pkt->pts = ast->audiotimestamp) != AV_NOPTS_VALUE) {
882  pkt->flags = AV_PKT_FLAG_KEY;
883  } else
884  pkt->flags = 0;
885  pkt->stream_index = st->index;
886 
887  return rm->audio_pkt_cnt;
888 }
889 
891 {
892  RMDemuxContext *rm = s->priv_data;
893  AVStream *st = NULL; // init to silence compiler warning
894  int i, len, res, seq = 1;
895  int64_t timestamp, pos;
896  int flags;
897 
898  for (;;) {
899  if (rm->audio_pkt_cnt) {
900  // If there are queued audio packet return them first
901  st = s->streams[rm->audio_stream_num];
902  res = ff_rm_retrieve_cache(s, s->pb, st, st->priv_data, pkt);
903  if(res < 0)
904  return res;
905  flags = 0;
906  } else {
907  if (rm->old_format) {
908  RMStream *ast;
909 
910  st = s->streams[0];
911  ast = st->priv_data;
912  timestamp = AV_NOPTS_VALUE;
913  len = !ast->audio_framesize ? RAW_PACKET_SIZE :
914  ast->coded_framesize * ast->sub_packet_h / 2;
915  flags = (seq++ == 1) ? 2 : 0;
916  pos = avio_tell(s->pb);
917  } else {
918  len=sync(s, &timestamp, &flags, &i, &pos);
919  if (len > 0)
920  st = s->streams[i];
921  }
922 
923  if(len<0 || url_feof(s->pb))
924  return AVERROR(EIO);
925 
926  res = ff_rm_parse_packet (s, s->pb, st, st->priv_data, len, pkt,
927  &seq, flags, timestamp);
928  if((flags&2) && (seq&0x7F) == 1)
929  av_add_index_entry(st, pos, timestamp, 0, 0, AVINDEX_KEYFRAME);
930  if (res)
931  continue;
932  }
933 
934  if( (st->discard >= AVDISCARD_NONKEY && !(flags&2))
935  || st->discard >= AVDISCARD_ALL){
936  av_free_packet(pkt);
937  } else
938  break;
939  }
940 
941  return 0;
942 }
943 
945 {
946  int i;
947 
948  for (i=0;i<s->nb_streams;i++)
950 
951  return 0;
952 }
953 
954 static int rm_probe(AVProbeData *p)
955 {
956  /* check file header */
957  if ((p->buf[0] == '.' && p->buf[1] == 'R' &&
958  p->buf[2] == 'M' && p->buf[3] == 'F' &&
959  p->buf[4] == 0 && p->buf[5] == 0) ||
960  (p->buf[0] == '.' && p->buf[1] == 'r' &&
961  p->buf[2] == 'a' && p->buf[3] == 0xfd))
962  return AVPROBE_SCORE_MAX;
963  else
964  return 0;
965 }
966 
967 static int64_t rm_read_dts(AVFormatContext *s, int stream_index,
968  int64_t *ppos, int64_t pos_limit)
969 {
970  RMDemuxContext *rm = s->priv_data;
971  int64_t pos, dts;
972  int stream_index2, flags, len, h;
973 
974  pos = *ppos;
975 
976  if(rm->old_format)
977  return AV_NOPTS_VALUE;
978 
979  if (avio_seek(s->pb, pos, SEEK_SET) < 0)
980  return AV_NOPTS_VALUE;
981 
982  rm->remaining_len=0;
983  for(;;){
984  int seq=1;
985  AVStream *st;
986 
987  len=sync(s, &dts, &flags, &stream_index2, &pos);
988  if(len<0)
989  return AV_NOPTS_VALUE;
990 
991  st = s->streams[stream_index2];
992  if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
993  h= avio_r8(s->pb); len--;
994  if(!(h & 0x40)){
995  seq = avio_r8(s->pb); len--;
996  }
997  }
998 
999  if((flags&2) && (seq&0x7F) == 1){
1000  av_dlog(s, "%d %d-%d %"PRId64" %d\n",
1001  flags, stream_index2, stream_index, dts, seq);
1002  av_add_index_entry(st, pos, dts, 0, 0, AVINDEX_KEYFRAME);
1003  if(stream_index2 == stream_index)
1004  break;
1005  }
1006 
1007  avio_skip(s->pb, len);
1008  }
1009  *ppos = pos;
1010  return dts;
1011 }
1012 
1013 static int rm_read_seek(AVFormatContext *s, int stream_index,
1014  int64_t pts, int flags)
1015 {
1016  RMDemuxContext *rm = s->priv_data;
1017 
1018  if (ff_seek_frame_binary(s, stream_index, pts, flags) < 0)
1019  return -1;
1020  rm->audio_pkt_cnt = 0;
1021  return 0;
1022 }
1023 
1024 
1026  .name = "rm",
1027  .long_name = NULL_IF_CONFIG_SMALL("RealMedia"),
1028  .priv_data_size = sizeof(RMDemuxContext),
1029  .read_probe = rm_probe,
1033  .read_timestamp = rm_read_dts,
1035 };
1036 
1038  .name = "rdt",
1039  .long_name = NULL_IF_CONFIG_SMALL("RDT demuxer"),
1040  .priv_data_size = sizeof(RMDemuxContext),
1042  .flags = AVFMT_NOFILE,
1043 };