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