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