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