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