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