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