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
66  int data_end;
68 
69 static int rm_read_close(AVFormatContext *s);
70 
71 static inline void get_strl(AVIOContext *pb, char *buf, int buf_size, int len)
72 {
73  int i;
74  char *q, r;
75 
76  q = buf;
77  for(i=0;i<len;i++) {
78  r = avio_r8(pb);
79  if (i < buf_size - 1)
80  *q++ = r;
81  }
82  if (buf_size > 0) *q = '\0';
83 }
84 
85 static void get_str8(AVIOContext *pb, char *buf, int buf_size)
86 {
87  get_strl(pb, buf, buf_size, avio_r8(pb));
88 }
89 
91 {
92  if (size >= 1<<24) {
93  av_log(s, AV_LOG_ERROR, "extradata size %u too large\n", size);
94  return -1;
95  }
96  if (ff_get_extradata(s, par, pb, size) < 0)
97  return AVERROR(ENOMEM);
98  return 0;
99 }
100 
101 static void rm_read_metadata(AVFormatContext *s, AVIOContext *pb, int wide)
102 {
103  char buf[1024];
104  int i;
105 
106  for (i=0; i<FF_ARRAY_ELEMS(ff_rm_metadata); i++) {
107  int len = wide ? avio_rb16(pb) : avio_r8(pb);
108  get_strl(pb, buf, sizeof(buf), len);
109  av_dict_set(&s->metadata, ff_rm_metadata[i], buf, 0);
110  }
111 }
112 
114 {
115  RMStream *rms = av_mallocz(sizeof(RMStream));
116  if (!rms)
117  return NULL;
118  rms->curpic_num = -1;
119  return rms;
120 }
121 
123 {
124  av_packet_unref(&rms->pkt);
125 }
126 
128  AVStream *st, RMStream *ast, int read_all)
129 {
130  char buf[256];
131  uint32_t version;
132  int ret;
133 
134  /* ra type header */
135  version = avio_rb16(pb); /* version */
136  if (version == 3) {
137  unsigned bytes_per_minute;
138  int header_size = avio_rb16(pb);
139  int64_t startpos = avio_tell(pb);
140  avio_skip(pb, 8);
141  bytes_per_minute = avio_rb16(pb);
142  avio_skip(pb, 4);
143  rm_read_metadata(s, pb, 0);
144  if ((startpos + header_size) >= avio_tell(pb) + 2) {
145  // fourcc (should always be "lpcJ")
146  avio_r8(pb);
147  get_str8(pb, buf, sizeof(buf));
148  }
149  // Skip extra header crap (this should never happen)
150  if ((startpos + header_size) > avio_tell(pb))
151  avio_skip(pb, header_size + startpos - avio_tell(pb));
152  if (bytes_per_minute)
153  st->codecpar->bit_rate = 8LL * bytes_per_minute / 60;
154  st->codecpar->sample_rate = 8000;
155  st->codecpar->channels = 1;
159  ast->deint_id = DEINT_ID_INT0;
160  } else {
161  int flavor, sub_packet_h, coded_framesize, sub_packet_size;
162  int codecdata_length;
163  unsigned bytes_per_minute;
164  /* old version (4) */
165  avio_skip(pb, 2); /* unused */
166  avio_rb32(pb); /* .ra4 */
167  avio_rb32(pb); /* data size */
168  avio_rb16(pb); /* version2 */
169  avio_rb32(pb); /* header size */
170  flavor= avio_rb16(pb); /* add codec info / flavor */
171  ast->coded_framesize = coded_framesize = avio_rb32(pb); /* coded frame size */
172  avio_rb32(pb); /* ??? */
173  bytes_per_minute = avio_rb32(pb);
174  if (version == 4) {
175  if (bytes_per_minute)
176  st->codecpar->bit_rate = 8LL * bytes_per_minute / 60;
177  }
178  avio_rb32(pb); /* ??? */
179  ast->sub_packet_h = sub_packet_h = avio_rb16(pb); /* 1 */
180  st->codecpar->block_align= avio_rb16(pb); /* frame size */
181  ast->sub_packet_size = sub_packet_size = avio_rb16(pb); /* sub packet size */
182  avio_rb16(pb); /* ??? */
183  if (version == 5) {
184  avio_rb16(pb); avio_rb16(pb); avio_rb16(pb);
185  }
186  st->codecpar->sample_rate = avio_rb16(pb);
187  avio_rb32(pb);
188  st->codecpar->channels = avio_rb16(pb);
189  if (version == 5) {
190  ast->deint_id = avio_rl32(pb);
191  avio_read(pb, buf, 4);
192  buf[4] = 0;
193  } else {
194  AV_WL32(buf, 0);
195  get_str8(pb, buf, sizeof(buf)); /* desc */
196  ast->deint_id = AV_RL32(buf);
197  get_str8(pb, buf, sizeof(buf)); /* desc */
198  }
200  st->codecpar->codec_tag = AV_RL32(buf);
202  st->codecpar->codec_tag);
203 
204  switch (st->codecpar->codec_id) {
205  case AV_CODEC_ID_AC3:
207  break;
208  case AV_CODEC_ID_RA_288:
209  st->codecpar->extradata_size= 0;
210  av_freep(&st->codecpar->extradata);
212  st->codecpar->block_align = coded_framesize;
213  break;
214  case AV_CODEC_ID_COOK:
216  case AV_CODEC_ID_ATRAC3:
217  case AV_CODEC_ID_SIPR:
218  if (read_all) {
219  codecdata_length = 0;
220  } else {
221  avio_rb16(pb); avio_r8(pb);
222  if (version == 5)
223  avio_r8(pb);
224  codecdata_length = avio_rb32(pb);
225  if(codecdata_length + AV_INPUT_BUFFER_PADDING_SIZE <= (unsigned)codecdata_length){
226  av_log(s, AV_LOG_ERROR, "codecdata_length too large\n");
227  return -1;
228  }
229  }
230 
232  if (st->codecpar->codec_id == AV_CODEC_ID_SIPR) {
233  if (flavor > 3) {
234  av_log(s, AV_LOG_ERROR, "bad SIPR file flavor %d\n",
235  flavor);
236  return -1;
237  }
238  st->codecpar->block_align = ff_sipr_subpk_size[flavor];
240  } else {
241  if(sub_packet_size <= 0){
242  av_log(s, AV_LOG_ERROR, "sub_packet_size is invalid\n");
243  return -1;
244  }
246  }
247  if ((ret = rm_read_extradata(s, pb, st->codecpar, codecdata_length)) < 0)
248  return ret;
249 
250  break;
251  case AV_CODEC_ID_AAC:
252  avio_rb16(pb); avio_r8(pb);
253  if (version == 5)
254  avio_r8(pb);
255  codecdata_length = avio_rb32(pb);
256  if(codecdata_length + AV_INPUT_BUFFER_PADDING_SIZE <= (unsigned)codecdata_length){
257  av_log(s, AV_LOG_ERROR, "codecdata_length too large\n");
258  return -1;
259  }
260  if (codecdata_length >= 1) {
261  avio_r8(pb);
262  if ((ret = rm_read_extradata(s, pb, st->codecpar, codecdata_length - 1)) < 0)
263  return ret;
264  }
265  break;
266  }
267  switch (ast->deint_id) {
268  case DEINT_ID_INT4:
269  if (ast->coded_framesize > ast->audio_framesize ||
270  sub_packet_h <= 1 ||
271  ast->coded_framesize * sub_packet_h > (2 + (sub_packet_h & 1)) * ast->audio_framesize)
272  return AVERROR_INVALIDDATA;
273  if (ast->coded_framesize * sub_packet_h != 2*ast->audio_framesize) {
274  avpriv_request_sample(s, "mismatching interleaver parameters");
275  return AVERROR_INVALIDDATA;
276  }
277  break;
278  case DEINT_ID_GENR:
279  if (ast->sub_packet_size <= 0 ||
280  ast->sub_packet_size > ast->audio_framesize)
281  return AVERROR_INVALIDDATA;
282  if (ast->audio_framesize % ast->sub_packet_size)
283  return AVERROR_INVALIDDATA;
284  break;
285  case DEINT_ID_SIPR:
286  case DEINT_ID_INT0:
287  case DEINT_ID_VBRS:
288  case DEINT_ID_VBRF:
289  break;
290  default:
291  av_log(s, AV_LOG_ERROR ,"Unknown interleaver %"PRIX32"\n", ast->deint_id);
292  return AVERROR_INVALIDDATA;
293  }
294  if (ast->deint_id == DEINT_ID_INT4 ||
295  ast->deint_id == DEINT_ID_GENR ||
296  ast->deint_id == DEINT_ID_SIPR) {
297  if (st->codecpar->block_align <= 0 ||
298  ast->audio_framesize * sub_packet_h > (unsigned)INT_MAX ||
299  ast->audio_framesize * sub_packet_h < st->codecpar->block_align)
300  return AVERROR_INVALIDDATA;
301  if (av_new_packet(&ast->pkt, ast->audio_framesize * sub_packet_h) < 0)
302  return AVERROR(ENOMEM);
303  }
304 
305  if (read_all) {
306  avio_r8(pb);
307  avio_r8(pb);
308  avio_r8(pb);
309  rm_read_metadata(s, pb, 0);
310  }
311  }
312  return 0;
313 }
314 
316  AVStream *st, RMStream *rst,
317  unsigned int codec_data_size, const uint8_t *mime)
318 {
319  unsigned int v;
320  int size;
321  int64_t codec_pos;
322  int ret;
323 
324  if (codec_data_size > INT_MAX)
325  return AVERROR_INVALIDDATA;
326  if (codec_data_size == 0)
327  return 0;
328 
329  avpriv_set_pts_info(st, 64, 1, 1000);
330  codec_pos = avio_tell(pb);
331  v = avio_rb32(pb);
332 
333  if (v == MKTAG(0xfd, 'a', 'r', '.')) {
334  /* ra type header */
335  if (rm_read_audio_stream_info(s, pb, st, rst, 0))
336  return -1;
337  } else if (v == MKBETAG('L', 'S', 'D', ':')) {
338  avio_seek(pb, -4, SEEK_CUR);
339  if ((ret = rm_read_extradata(s, pb, st->codecpar, codec_data_size)) < 0)
340  return ret;
341 
345  st->codecpar->codec_tag);
346  } else if(mime && !strcmp(mime, "logical-fileinfo")){
347  int stream_count, rule_count, property_count, i;
348  ff_free_stream(s, st);
349  if (avio_rb16(pb) != 0) {
350  av_log(s, AV_LOG_WARNING, "Unsupported version\n");
351  goto skip;
352  }
353  stream_count = avio_rb16(pb);
354  avio_skip(pb, 6*stream_count);
355  rule_count = avio_rb16(pb);
356  avio_skip(pb, 2*rule_count);
357  property_count = avio_rb16(pb);
358  for(i=0; i<property_count; i++){
359  uint8_t name[128], val[128];
360  avio_rb32(pb);
361  if (avio_rb16(pb) != 0) {
362  av_log(s, AV_LOG_WARNING, "Unsupported Name value property version\n");
363  goto skip; //FIXME skip just this one
364  }
365  get_str8(pb, name, sizeof(name));
366  switch(avio_rb32(pb)) {
367  case 2: get_strl(pb, val, sizeof(val), avio_rb16(pb));
368  av_dict_set(&s->metadata, name, val, 0);
369  break;
370  default: avio_skip(pb, avio_rb16(pb));
371  }
372  }
373  } else {
374  int fps;
375  if (avio_rl32(pb) != MKTAG('V', 'I', 'D', 'O')) {
376  fail1:
377  av_log(s, AV_LOG_WARNING, "Unsupported stream type %08x\n", v);
378  goto skip;
379  }
380  st->codecpar->codec_tag = avio_rl32(pb);
382  st->codecpar->codec_tag);
383  av_log(s, AV_LOG_TRACE, "%X %X\n", st->codecpar->codec_tag, MKTAG('R', 'V', '2', '0'));
384  if (st->codecpar->codec_id == AV_CODEC_ID_NONE)
385  goto fail1;
386  st->codecpar->width = avio_rb16(pb);
387  st->codecpar->height = avio_rb16(pb);
388  avio_skip(pb, 2); // looks like bits per sample
389  avio_skip(pb, 4); // always zero?
392  fps = avio_rb32(pb);
393 
394  if ((ret = rm_read_extradata(s, pb, st->codecpar, codec_data_size - (avio_tell(pb) - codec_pos))) < 0)
395  return ret;
396 
397  if (fps > 0) {
399  0x10000, fps, (1 << 30) - 1);
400 #if FF_API_R_FRAME_RATE
401  st->r_frame_rate = st->avg_frame_rate;
402 #endif
403  } else if (s->error_recognition & AV_EF_EXPLODE) {
404  av_log(s, AV_LOG_ERROR, "Invalid framerate\n");
405  return AVERROR_INVALIDDATA;
406  }
407  }
408 
409 skip:
410  /* skip codec info */
411  size = avio_tell(pb) - codec_pos;
412  if (codec_data_size >= size) {
413  avio_skip(pb, codec_data_size - size);
414  } else {
415  av_log(s, AV_LOG_WARNING, "codec_data_size %u < size %d\n", codec_data_size, size);
416  }
417 
418  return 0;
419 }
420 
421 /** this function assumes that the demuxer has already seeked to the start
422  * of the INDX chunk, and will bail out if not. */
424 {
425  AVIOContext *pb = s->pb;
426  unsigned int size, n_pkts, str_id, next_off, n, pos, pts;
427  AVStream *st;
428 
429  do {
430  if (avio_rl32(pb) != MKTAG('I','N','D','X'))
431  return -1;
432  size = avio_rb32(pb);
433  if (size < 20)
434  return -1;
435  avio_skip(pb, 2);
436  n_pkts = avio_rb32(pb);
437  str_id = avio_rb16(pb);
438  next_off = avio_rb32(pb);
439  for (n = 0; n < s->nb_streams; n++)
440  if (s->streams[n]->id == str_id) {
441  st = s->streams[n];
442  break;
443  }
444  if (n == s->nb_streams) {
445  av_log(s, AV_LOG_ERROR,
446  "Invalid stream index %d for index at pos %"PRId64"\n",
447  str_id, avio_tell(pb));
448  goto skip;
449  } else if ((avio_size(pb) - avio_tell(pb)) / 14 < n_pkts) {
450  av_log(s, AV_LOG_ERROR,
451  "Nr. of packets in packet index for stream index %d "
452  "exceeds filesize (%"PRId64" at %"PRId64" = %"PRId64")\n",
453  str_id, avio_size(pb), avio_tell(pb),
454  (avio_size(pb) - avio_tell(pb)) / 14);
455  goto skip;
456  }
457 
458  for (n = 0; n < n_pkts; n++) {
459  avio_skip(pb, 2);
460  pts = avio_rb32(pb);
461  pos = avio_rb32(pb);
462  avio_skip(pb, 4); /* packet no. */
463 
464  av_add_index_entry(st, pos, pts, 0, 0, AVINDEX_KEYFRAME);
465  }
466 
467 skip:
468  if (next_off && avio_tell(pb) < next_off &&
469  avio_seek(pb, next_off, SEEK_SET) < 0) {
470  av_log(s, AV_LOG_ERROR,
471  "Non-linear index detected, not supported\n");
472  return -1;
473  }
474  } while (next_off);
475 
476  return 0;
477 }
478 
480 {
481  RMDemuxContext *rm = s->priv_data;
482  AVStream *st;
483 
484  rm->old_format = 1;
485  st = avformat_new_stream(s, NULL);
486  if (!st)
487  return -1;
489  if (!st->priv_data)
490  return AVERROR(ENOMEM);
491  return rm_read_audio_stream_info(s, s->pb, st, st->priv_data, 1);
492 }
493 
495  AVStream *st, char *mime)
496 {
497  int number_of_streams = avio_rb16(pb);
498  int number_of_mdpr;
499  int i, ret;
500  unsigned size2;
501  for (i = 0; i<number_of_streams; i++)
502  avio_rb16(pb);
503  number_of_mdpr = avio_rb16(pb);
504  if (number_of_mdpr != 1) {
505  avpriv_request_sample(s, "MLTI with multiple (%d) MDPR", number_of_mdpr);
506  }
507  for (i = 0; i < number_of_mdpr; i++) {
508  AVStream *st2;
509  if (i > 0) {
510  st2 = avformat_new_stream(s, NULL);
511  if (!st2) {
512  ret = AVERROR(ENOMEM);
513  return ret;
514  }
515  st2->id = st->id + (i<<16);
516  st2->codecpar->bit_rate = st->codecpar->bit_rate;
517  st2->start_time = st->start_time;
518  st2->duration = st->duration;
521  if (!st2->priv_data)
522  return AVERROR(ENOMEM);
523  } else
524  st2 = st;
525 
526  size2 = avio_rb32(pb);
527  ret = ff_rm_read_mdpr_codecdata(s, s->pb, st2, st2->priv_data,
528  size2, mime);
529  if (ret < 0)
530  return ret;
531  }
532  return 0;
533 }
534 
536 {
537  RMDemuxContext *rm = s->priv_data;
538  AVStream *st;
539  AVIOContext *pb = s->pb;
540  unsigned int tag;
541  int tag_size;
542  unsigned int start_time, duration;
543  unsigned int data_off = 0, indx_off = 0;
544  char buf[128], mime[128];
545  int flags = 0;
546  int ret = -1;
547  unsigned size, v;
548  int64_t codec_pos;
549 
550  tag = avio_rl32(pb);
551  if (tag == MKTAG('.', 'r', 'a', 0xfd)) {
552  /* very old .ra format */
553  return rm_read_header_old(s);
554  } else if (tag != MKTAG('.', 'R', 'M', 'F')) {
555  return AVERROR(EIO);
556  }
557 
558  tag_size = avio_rb32(pb);
559  avio_skip(pb, tag_size - 8);
560 
561  for(;;) {
562  if (avio_feof(pb))
563  goto fail;
564  tag = avio_rl32(pb);
565  tag_size = avio_rb32(pb);
566  avio_rb16(pb);
567  av_log(s, AV_LOG_TRACE, "tag=%c%c%c%c (%08x) size=%d\n",
568  (tag ) & 0xff,
569  (tag >> 8) & 0xff,
570  (tag >> 16) & 0xff,
571  (tag >> 24) & 0xff,
572  tag,
573  tag_size);
574  if (tag_size < 10 && tag != MKTAG('D', 'A', 'T', 'A'))
575  goto fail;
576  switch(tag) {
577  case MKTAG('P', 'R', 'O', 'P'):
578  /* file header */
579  avio_rb32(pb); /* max bit rate */
580  avio_rb32(pb); /* avg bit rate */
581  avio_rb32(pb); /* max packet size */
582  avio_rb32(pb); /* avg packet size */
583  avio_rb32(pb); /* nb packets */
584  duration = avio_rb32(pb); /* duration */
585  s->duration = av_rescale(duration, AV_TIME_BASE, 1000);
586  avio_rb32(pb); /* preroll */
587  indx_off = avio_rb32(pb); /* index offset */
588  data_off = avio_rb32(pb); /* data offset */
589  avio_rb16(pb); /* nb streams */
590  flags = avio_rb16(pb); /* flags */
591  break;
592  case MKTAG('C', 'O', 'N', 'T'):
593  rm_read_metadata(s, pb, 1);
594  break;
595  case MKTAG('M', 'D', 'P', 'R'):
596  st = avformat_new_stream(s, NULL);
597  if (!st) {
598  ret = AVERROR(ENOMEM);
599  goto fail;
600  }
601  st->id = avio_rb16(pb);
602  avio_rb32(pb); /* max bit rate */
603  st->codecpar->bit_rate = avio_rb32(pb); /* bit rate */
604  avio_rb32(pb); /* max packet size */
605  avio_rb32(pb); /* avg packet size */
606  start_time = avio_rb32(pb); /* start time */
607  avio_rb32(pb); /* preroll */
608  duration = avio_rb32(pb); /* duration */
609  st->start_time = start_time;
610  st->duration = duration;
611  if(duration>0)
613  get_str8(pb, buf, sizeof(buf)); /* desc */
614  get_str8(pb, mime, sizeof(mime)); /* mimetype */
617  if (!st->priv_data)
618  return AVERROR(ENOMEM);
619 
620  size = avio_rb32(pb);
621  codec_pos = avio_tell(pb);
622 
623  ffio_ensure_seekback(pb, 4);
624  v = avio_rb32(pb);
625  if (v == MKBETAG('M', 'L', 'T', 'I')) {
626  ret = rm_read_multi(s, s->pb, st, mime);
627  if (ret < 0)
628  goto fail;
629  avio_seek(pb, codec_pos + size, SEEK_SET);
630  } else {
631  avio_skip(pb, -4);
632  if (ff_rm_read_mdpr_codecdata(s, s->pb, st, st->priv_data,
633  size, mime) < 0)
634  goto fail;
635  }
636 
637  break;
638  case MKTAG('D', 'A', 'T', 'A'):
639  goto header_end;
640  default:
641  /* unknown tag: skip it */
642  avio_skip(pb, tag_size - 10);
643  break;
644  }
645  }
646  header_end:
647  rm->nb_packets = avio_rb32(pb); /* number of packets */
648  if (!rm->nb_packets && (flags & 4))
649  rm->nb_packets = 3600 * 25;
650  avio_rb32(pb); /* next data header */
651 
652  if (!data_off)
653  data_off = avio_tell(pb) - 18;
654  if (indx_off && pb->seekable && !(s->flags & AVFMT_FLAG_IGNIDX) &&
655  avio_seek(pb, indx_off, SEEK_SET) >= 0) {
656  rm_read_index(s);
657  avio_seek(pb, data_off + 18, SEEK_SET);
658  }
659 
660  return 0;
661 
662 fail:
663  rm_read_close(s);
664  return ret;
665 }
666 
667 static int get_num(AVIOContext *pb, int *len)
668 {
669  int n, n1;
670 
671  n = avio_rb16(pb);
672  (*len)-=2;
673  n &= 0x7FFF;
674  if (n >= 0x4000) {
675  return n - 0x4000;
676  } else {
677  n1 = avio_rb16(pb);
678  (*len)-=2;
679  return (n << 16) | n1;
680  }
681 }
682 
683 /* multiple of 20 bytes for ra144 (ugly) */
684 #define RAW_PACKET_SIZE 1000
685 
686 static int rm_sync(AVFormatContext *s, int64_t *timestamp, int *flags, int *stream_index, int64_t *pos){
687  RMDemuxContext *rm = s->priv_data;
688  AVIOContext *pb = s->pb;
689  AVStream *st;
690  uint32_t state=0xFFFFFFFF;
691 
692  while(!avio_feof(pb)){
693  int len, num, i;
694  int mlti_id;
695  *pos= avio_tell(pb) - 3;
696  if(rm->remaining_len > 0){
697  num= rm->current_stream;
698  mlti_id = 0;
699  len= rm->remaining_len;
700  *timestamp = AV_NOPTS_VALUE;
701  *flags= 0;
702  }else{
703  state= (state<<8) + avio_r8(pb);
704 
705  if(state == MKBETAG('I', 'N', 'D', 'X')){
706  int n_pkts, expected_len;
707  len = avio_rb32(pb);
708  avio_skip(pb, 2);
709  n_pkts = avio_rb32(pb);
710  expected_len = 20 + n_pkts * 14;
711  if (len == 20)
712  /* some files don't add index entries to chunk size... */
713  len = expected_len;
714  else if (len != expected_len)
716  "Index size %d (%d pkts) is wrong, should be %d.\n",
717  len, n_pkts, expected_len);
718  len -= 14; // we already read part of the index header
719  if(len<0)
720  continue;
721  goto skip;
722  } else if (state == MKBETAG('D','A','T','A')) {
724  "DATA tag in middle of chunk, file may be broken.\n");
725  }
726 
727  if(state > (unsigned)0xFFFF || state <= 12)
728  continue;
729  len=state - 12;
730  state= 0xFFFFFFFF;
731 
732  num = avio_rb16(pb);
733  *timestamp = avio_rb32(pb);
734  mlti_id = (avio_r8(pb)>>1)-1<<16;
735  mlti_id = FFMAX(mlti_id, 0);
736  *flags = avio_r8(pb); /* flags */
737  }
738  for(i=0;i<s->nb_streams;i++) {
739  st = s->streams[i];
740  if (mlti_id + num == st->id)
741  break;
742  }
743  if (i == s->nb_streams) {
744 skip:
745  /* skip packet if unknown number */
746  avio_skip(pb, len);
747  rm->remaining_len = 0;
748  continue;
749  }
750  *stream_index= i;
751 
752  return len;
753  }
754  return -1;
755 }
756 
758  RMDemuxContext *rm, RMStream *vst,
759  AVPacket *pkt, int len, int *pseq,
760  int64_t *timestamp)
761 {
762  int hdr;
763  int seq = 0, pic_num = 0, len2 = 0, pos = 0; //init to silence compiler warning
764  int type;
765  int ret;
766 
767  hdr = avio_r8(pb); len--;
768  type = hdr >> 6;
769 
770  if(type != 3){ // not frame as a part of packet
771  seq = avio_r8(pb); len--;
772  }
773  if(type != 1){ // not whole frame
774  len2 = get_num(pb, &len);
775  pos = get_num(pb, &len);
776  pic_num = avio_r8(pb); len--;
777  }
778  if(len<0) {
779  av_log(s, AV_LOG_ERROR, "Insufficient data\n");
780  return -1;
781  }
782  rm->remaining_len = len;
783  if(type&1){ // frame, not slice
784  if(type == 3){ // frame as a part of packet
785  len= len2;
786  *timestamp = pos;
787  }
788  if(rm->remaining_len < len) {
789  av_log(s, AV_LOG_ERROR, "Insufficient remaining len\n");
790  return -1;
791  }
792  rm->remaining_len -= len;
793  if(av_new_packet(pkt, len + 9) < 0)
794  return AVERROR(EIO);
795  pkt->data[0] = 0;
796  AV_WL32(pkt->data + 1, 1);
797  AV_WL32(pkt->data + 5, 0);
798  if ((ret = avio_read(pb, pkt->data + 9, len)) != len) {
799  av_packet_unref(pkt);
800  av_log(s, AV_LOG_ERROR, "Failed to read %d bytes\n", len);
801  return ret < 0 ? ret : AVERROR(EIO);
802  }
803  return 0;
804  }
805  //now we have to deal with single slice
806 
807  *pseq = seq;
808  if((seq & 0x7F) == 1 || vst->curpic_num != pic_num){
809  if (len2 > ffio_limit(pb, len2)) {
810  av_log(s, AV_LOG_ERROR, "Impossibly sized packet\n");
811  return AVERROR_INVALIDDATA;
812  }
813  vst->slices = ((hdr & 0x3F) << 1) + 1;
814  vst->videobufsize = len2 + 8*vst->slices + 1;
815  av_packet_unref(&vst->pkt); //FIXME this should be output.
816  if(av_new_packet(&vst->pkt, vst->videobufsize) < 0)
817  return AVERROR(ENOMEM);
818  memset(vst->pkt.data, 0, vst->pkt.size);
819  vst->videobufpos = 8*vst->slices + 1;
820  vst->cur_slice = 0;
821  vst->curpic_num = pic_num;
822  vst->pktpos = avio_tell(pb);
823  }
824  if(type == 2)
825  len = FFMIN(len, pos);
826 
827  if(++vst->cur_slice > vst->slices) {
828  av_log(s, AV_LOG_ERROR, "cur slice %d, too large\n", vst->cur_slice);
829  return 1;
830  }
831  if(!vst->pkt.data)
832  return AVERROR(ENOMEM);
833  AV_WL32(vst->pkt.data - 7 + 8*vst->cur_slice, 1);
834  AV_WL32(vst->pkt.data - 3 + 8*vst->cur_slice, vst->videobufpos - 8*vst->slices - 1);
835  if(vst->videobufpos + len > vst->videobufsize) {
836  av_log(s, AV_LOG_ERROR, "outside videobufsize\n");
837  return 1;
838  }
839  if (avio_read(pb, vst->pkt.data + vst->videobufpos, len) != len)
840  return AVERROR(EIO);
841  vst->videobufpos += len;
842  rm->remaining_len-= len;
843 
844  if (type == 2 || vst->videobufpos == vst->videobufsize) {
845  vst->pkt.data[0] = vst->cur_slice-1;
846  *pkt= vst->pkt;
847  vst->pkt.data= NULL;
848  vst->pkt.size= 0;
849  vst->pkt.buf = NULL;
850  if(vst->slices != vst->cur_slice) //FIXME find out how to set slices correct from the begin
851  memmove(pkt->data + 1 + 8*vst->cur_slice, pkt->data + 1 + 8*vst->slices,
852  vst->videobufpos - 1 - 8*vst->slices);
853  pkt->size = vst->videobufpos + 8*(vst->cur_slice - vst->slices);
854  pkt->pts = AV_NOPTS_VALUE;
855  pkt->pos = vst->pktpos;
856  vst->slices = 0;
857  return 0;
858  }
859 
860  return 1;
861 }
862 
863 static inline void
865 {
866  uint8_t *ptr;
867  int j;
868 
869  if (st->codecpar->codec_id == AV_CODEC_ID_AC3) {
870  ptr = pkt->data;
871  for (j=0;j<pkt->size;j+=2) {
872  FFSWAP(int, ptr[0], ptr[1]);
873  ptr += 2;
874  }
875  }
876 }
877 
878 static int readfull(AVFormatContext *s, AVIOContext *pb, uint8_t *dst, int n) {
879  int ret = avio_read(pb, dst, n);
880  if (ret != n) {
881  if (ret >= 0) memset(dst + ret, 0, n - ret);
882  else memset(dst , 0, n);
883  av_log(s, AV_LOG_ERROR, "Failed to fully read block\n");
884  }
885  return ret;
886 }
887 
888 int
890  AVStream *st, RMStream *ast, int len, AVPacket *pkt,
891  int *seq, int flags, int64_t timestamp)
892 {
893  RMDemuxContext *rm = s->priv_data;
894  int ret;
895 
896  if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
897  rm->current_stream= st->id;
898  ret = rm_assemble_video_frame(s, pb, rm, ast, pkt, len, seq, &timestamp);
899  if(ret)
900  return ret < 0 ? ret : -1; //got partial frame or error
901  } else if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
902  if ((ast->deint_id == DEINT_ID_GENR) ||
903  (ast->deint_id == DEINT_ID_INT4) ||
904  (ast->deint_id == DEINT_ID_SIPR)) {
905  int x;
906  int sps = ast->sub_packet_size;
907  int cfs = ast->coded_framesize;
908  int h = ast->sub_packet_h;
909  int y = ast->sub_packet_cnt;
910  int w = ast->audio_framesize;
911 
912  if (flags & 2)
913  y = ast->sub_packet_cnt = 0;
914  if (!y)
915  ast->audiotimestamp = timestamp;
916 
917  switch (ast->deint_id) {
918  case DEINT_ID_INT4:
919  for (x = 0; x < h/2; x++)
920  readfull(s, pb, ast->pkt.data+x*2*w+y*cfs, cfs);
921  break;
922  case DEINT_ID_GENR:
923  for (x = 0; x < w/sps; x++)
924  readfull(s, pb, ast->pkt.data+sps*(h*x+((h+1)/2)*(y&1)+(y>>1)), sps);
925  break;
926  case DEINT_ID_SIPR:
927  readfull(s, pb, ast->pkt.data + y * w, w);
928  break;
929  }
930 
931  if (++(ast->sub_packet_cnt) < h)
932  return -1;
933  if (ast->deint_id == DEINT_ID_SIPR)
934  ff_rm_reorder_sipr_data(ast->pkt.data, h, w);
935 
936  ast->sub_packet_cnt = 0;
937  rm->audio_stream_num = st->index;
938  if (st->codecpar->block_align <= 0) {
939  av_log(s, AV_LOG_ERROR, "Invalid block alignment %d\n", st->codecpar->block_align);
940  return AVERROR_INVALIDDATA;
941  }
942  rm->audio_pkt_cnt = h * w / st->codecpar->block_align;
943  } else if ((ast->deint_id == DEINT_ID_VBRF) ||
944  (ast->deint_id == DEINT_ID_VBRS)) {
945  int x;
946  rm->audio_stream_num = st->index;
947  ast->sub_packet_cnt = (avio_rb16(pb) & 0xf0) >> 4;
948  if (ast->sub_packet_cnt) {
949  for (x = 0; x < ast->sub_packet_cnt; x++)
950  ast->sub_packet_lengths[x] = avio_rb16(pb);
951  rm->audio_pkt_cnt = ast->sub_packet_cnt;
952  ast->audiotimestamp = timestamp;
953  } else
954  return -1;
955  } else {
956  if ((ret = av_get_packet(pb, pkt, len)) < 0)
957  return ret;
958  rm_ac3_swap_bytes(st, pkt);
959  }
960  } else {
961  if ((ret = av_get_packet(pb, pkt, len)) < 0)
962  return ret;
963  }
964 
965  pkt->stream_index = st->index;
966 
967 #if 0
968  if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
969  if(st->codecpar->codec_id == AV_CODEC_ID_RV20){
970  int seq= 128*(pkt->data[2]&0x7F) + (pkt->data[3]>>1);
971  av_log(s, AV_LOG_DEBUG, "%d %"PRId64" %d\n", *timestamp, *timestamp*512LL/25, seq);
972 
973  seq |= (timestamp&~0x3FFF);
974  if(seq - timestamp > 0x2000) seq -= 0x4000;
975  if(seq - timestamp < -0x2000) seq += 0x4000;
976  }
977  }
978 #endif
979 
980  pkt->pts = timestamp;
981  if (flags & 2)
982  pkt->flags |= AV_PKT_FLAG_KEY;
983 
984  return st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO ? rm->audio_pkt_cnt : 0;
985 }
986 
987 int
989  AVStream *st, RMStream *ast, AVPacket *pkt)
990 {
991  RMDemuxContext *rm = s->priv_data;
992 
993  av_assert0 (rm->audio_pkt_cnt > 0);
994 
995  if (ast->deint_id == DEINT_ID_VBRF ||
996  ast->deint_id == DEINT_ID_VBRS) {
997  int ret = av_get_packet(pb, pkt, ast->sub_packet_lengths[ast->sub_packet_cnt - rm->audio_pkt_cnt]);
998  if (ret < 0)
999  return ret;
1000  } else {
1001  int ret = av_new_packet(pkt, st->codecpar->block_align);
1002  if (ret < 0)
1003  return ret;
1004  memcpy(pkt->data, ast->pkt.data + st->codecpar->block_align * //FIXME avoid this
1005  (ast->sub_packet_h * ast->audio_framesize / st->codecpar->block_align - rm->audio_pkt_cnt),
1006  st->codecpar->block_align);
1007  }
1008  rm->audio_pkt_cnt--;
1009  if ((pkt->pts = ast->audiotimestamp) != AV_NOPTS_VALUE) {
1011  pkt->flags = AV_PKT_FLAG_KEY;
1012  } else
1013  pkt->flags = 0;
1014  pkt->stream_index = st->index;
1015 
1016  return rm->audio_pkt_cnt;
1017 }
1018 
1020 {
1021  RMDemuxContext *rm = s->priv_data;
1022  AVStream *st = NULL; // init to silence compiler warning
1023  int i, len, res, seq = 1;
1024  int64_t timestamp, pos;
1025  int flags;
1026 
1027  for (;;) {
1028  if (rm->audio_pkt_cnt) {
1029  // If there are queued audio packet return them first
1030  st = s->streams[rm->audio_stream_num];
1031  res = ff_rm_retrieve_cache(s, s->pb, st, st->priv_data, pkt);
1032  if(res < 0)
1033  return res;
1034  flags = 0;
1035  } else {
1036  if (rm->old_format) {
1037  RMStream *ast;
1038 
1039  st = s->streams[0];
1040  ast = st->priv_data;
1041  timestamp = AV_NOPTS_VALUE;
1042  len = !ast->audio_framesize ? RAW_PACKET_SIZE :
1043  ast->coded_framesize * ast->sub_packet_h / 2;
1044  flags = (seq++ == 1) ? 2 : 0;
1045  pos = avio_tell(s->pb);
1046  } else {
1047  len = rm_sync(s, &timestamp, &flags, &i, &pos);
1048  if (len > 0)
1049  st = s->streams[i];
1050  }
1051 
1052  if (avio_feof(s->pb))
1053  return AVERROR_EOF;
1054  if (len <= 0)
1055  return AVERROR(EIO);
1056 
1057  res = ff_rm_parse_packet (s, s->pb, st, st->priv_data, len, pkt,
1058  &seq, flags, timestamp);
1059  if (res < -1)
1060  return res;
1061  if((flags&2) && (seq&0x7F) == 1)
1062  av_add_index_entry(st, pos, timestamp, 0, 0, AVINDEX_KEYFRAME);
1063  if (res)
1064  continue;
1065  }
1066 
1067  if( (st->discard >= AVDISCARD_NONKEY && !(flags&2))
1068  || st->discard >= AVDISCARD_ALL){
1069  av_packet_unref(pkt);
1070  } else
1071  break;
1072  }
1073 
1074  return 0;
1075 }
1076 
1078 {
1079  int i;
1080 
1081  for (i=0;i<s->nb_streams;i++)
1083 
1084  return 0;
1085 }
1086 
1087 static int rm_probe(AVProbeData *p)
1088 {
1089  /* check file header */
1090  if ((p->buf[0] == '.' && p->buf[1] == 'R' &&
1091  p->buf[2] == 'M' && p->buf[3] == 'F' &&
1092  p->buf[4] == 0 && p->buf[5] == 0) ||
1093  (p->buf[0] == '.' && p->buf[1] == 'r' &&
1094  p->buf[2] == 'a' && p->buf[3] == 0xfd))
1095  return AVPROBE_SCORE_MAX;
1096  else
1097  return 0;
1098 }
1099 
1100 static int64_t rm_read_dts(AVFormatContext *s, int stream_index,
1101  int64_t *ppos, int64_t pos_limit)
1102 {
1103  RMDemuxContext *rm = s->priv_data;
1104  int64_t pos, dts;
1105  int stream_index2, flags, len, h;
1106 
1107  pos = *ppos;
1108 
1109  if(rm->old_format)
1110  return AV_NOPTS_VALUE;
1111 
1112  if (avio_seek(s->pb, pos, SEEK_SET) < 0)
1113  return AV_NOPTS_VALUE;
1114 
1115  rm->remaining_len=0;
1116  for(;;){
1117  int seq=1;
1118  AVStream *st;
1119 
1120  len = rm_sync(s, &dts, &flags, &stream_index2, &pos);
1121  if(len<0)
1122  return AV_NOPTS_VALUE;
1123 
1124  st = s->streams[stream_index2];
1125  if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
1126  h= avio_r8(s->pb); len--;
1127  if(!(h & 0x40)){
1128  seq = avio_r8(s->pb); len--;
1129  }
1130  }
1131 
1132  if((flags&2) && (seq&0x7F) == 1){
1133  av_log(s, AV_LOG_TRACE, "%d %d-%d %"PRId64" %d\n",
1134  flags, stream_index2, stream_index, dts, seq);
1135  av_add_index_entry(st, pos, dts, 0, 0, AVINDEX_KEYFRAME);
1136  if(stream_index2 == stream_index)
1137  break;
1138  }
1139 
1140  avio_skip(s->pb, len);
1141  }
1142  *ppos = pos;
1143  return dts;
1144 }
1145 
1146 static int rm_read_seek(AVFormatContext *s, int stream_index,
1147  int64_t pts, int flags)
1148 {
1149  RMDemuxContext *rm = s->priv_data;
1150 
1151  if (ff_seek_frame_binary(s, stream_index, pts, flags) < 0)
1152  return -1;
1153  rm->audio_pkt_cnt = 0;
1154  return 0;
1155 }
1156 
1157 
1159  .name = "rm",
1160  .long_name = NULL_IF_CONFIG_SMALL("RealMedia"),
1161  .priv_data_size = sizeof(RMDemuxContext),
1162  .read_probe = rm_probe,
1166  .read_timestamp = rm_read_dts,
1168 };
1169 
1171  .name = "rdt",
1172  .long_name = NULL_IF_CONFIG_SMALL("RDT demuxer"),
1173  .priv_data_size = sizeof(RMDemuxContext),
1175  .flags = AVFMT_NOFILE,
1176 };
1177 
1178 static int ivr_probe(AVProbeData *p)
1179 {
1180  if (memcmp(p->buf, ".R1M\x0\x1\x1", 7) &&
1181  memcmp(p->buf, ".REC", 4))
1182  return 0;
1183 
1184  return AVPROBE_SCORE_MAX;
1185 }
1186 
1188 {
1189  unsigned tag, type, len, tlen, value;
1190  int i, j, n, count, nb_streams = 0, ret;
1191  uint8_t key[256], val[256];
1192  AVIOContext *pb = s->pb;
1193  AVStream *st;
1194  int64_t pos, offset, temp;
1195 
1196  pos = avio_tell(pb);
1197  tag = avio_rl32(pb);
1198  if (tag == MKTAG('.','R','1','M')) {
1199  if (avio_rb16(pb) != 1)
1200  return AVERROR_INVALIDDATA;
1201  if (avio_r8(pb) != 1)
1202  return AVERROR_INVALIDDATA;
1203  len = avio_rb32(pb);
1204  avio_skip(pb, len);
1205  avio_skip(pb, 5);
1206  temp = avio_rb64(pb);
1207  while (!avio_feof(pb) && temp) {
1208  offset = temp;
1209  temp = avio_rb64(pb);
1210  }
1211  avio_skip(pb, offset - avio_tell(pb));
1212  if (avio_r8(pb) != 1)
1213  return AVERROR_INVALIDDATA;
1214  len = avio_rb32(pb);
1215  avio_skip(pb, len);
1216  if (avio_r8(pb) != 2)
1217  return AVERROR_INVALIDDATA;
1218  avio_skip(pb, 16);
1219  pos = avio_tell(pb);
1220  tag = avio_rl32(pb);
1221  }
1222 
1223  if (tag != MKTAG('.','R','E','C'))
1224  return AVERROR_INVALIDDATA;
1225 
1226  if (avio_r8(pb) != 0)
1227  return AVERROR_INVALIDDATA;
1228  count = avio_rb32(pb);
1229  for (i = 0; i < count; i++) {
1230  if (avio_feof(pb))
1231  return AVERROR_INVALIDDATA;
1232 
1233  type = avio_r8(pb);
1234  tlen = avio_rb32(pb);
1235  avio_get_str(pb, tlen, key, sizeof(key));
1236  len = avio_rb32(pb);
1237  if (type == 5) {
1238  avio_get_str(pb, len, val, sizeof(val));
1239  av_log(s, AV_LOG_DEBUG, "%s = '%s'\n", key, val);
1240  } else if (type == 4) {
1241  av_log(s, AV_LOG_DEBUG, "%s = '0x", key);
1242  for (j = 0; j < len; j++)
1243  av_log(s, AV_LOG_DEBUG, "%X", avio_r8(pb));
1244  av_log(s, AV_LOG_DEBUG, "'\n");
1245  } else if (len == 4 && type == 3 && !strncmp(key, "StreamCount", tlen)) {
1246  nb_streams = value = avio_rb32(pb);
1247  } else if (len == 4 && type == 3) {
1248  value = avio_rb32(pb);
1249  av_log(s, AV_LOG_DEBUG, "%s = %d\n", key, value);
1250  } else {
1251  av_log(s, AV_LOG_DEBUG, "Skipping unsupported key: %s\n", key);
1252  avio_skip(pb, len);
1253  }
1254  }
1255 
1256  for (n = 0; n < nb_streams; n++) {
1257  st = avformat_new_stream(s, NULL);
1258  if (!st)
1259  return AVERROR(ENOMEM);
1261  if (!st->priv_data)
1262  return AVERROR(ENOMEM);
1263 
1264  if (avio_r8(pb) != 1)
1265  return AVERROR_INVALIDDATA;
1266 
1267  count = avio_rb32(pb);
1268  for (i = 0; i < count; i++) {
1269  if (avio_feof(pb))
1270  return AVERROR_INVALIDDATA;
1271 
1272  type = avio_r8(pb);
1273  tlen = avio_rb32(pb);
1274  avio_get_str(pb, tlen, key, sizeof(key));
1275  len = avio_rb32(pb);
1276  if (type == 5) {
1277  avio_get_str(pb, len, val, sizeof(val));
1278  av_log(s, AV_LOG_DEBUG, "%s = '%s'\n", key, val);
1279  } else if (type == 4 && !strncmp(key, "OpaqueData", tlen)) {
1280  ret = ffio_ensure_seekback(pb, 4);
1281  if (ret < 0)
1282  return ret;
1283  if (avio_rb32(pb) == MKBETAG('M', 'L', 'T', 'I')) {
1284  ret = rm_read_multi(s, pb, st, NULL);
1285  } else {
1286  avio_seek(pb, -4, SEEK_CUR);
1287  ret = ff_rm_read_mdpr_codecdata(s, pb, st, st->priv_data, len, NULL);
1288  }
1289 
1290  if (ret < 0)
1291  return ret;
1292  } else if (type == 4) {
1293  int j;
1294 
1295  av_log(s, AV_LOG_DEBUG, "%s = '0x", key);
1296  for (j = 0; j < len; j++)
1297  av_log(s, AV_LOG_DEBUG, "%X", avio_r8(pb));
1298  av_log(s, AV_LOG_DEBUG, "'\n");
1299  } else if (len == 4 && type == 3 && !strncmp(key, "Duration", tlen)) {
1300  st->duration = avio_rb32(pb);
1301  } else if (len == 4 && type == 3) {
1302  value = avio_rb32(pb);
1303  av_log(s, AV_LOG_DEBUG, "%s = %d\n", key, value);
1304  } else {
1305  av_log(s, AV_LOG_DEBUG, "Skipping unsupported key: %s\n", key);
1306  avio_skip(pb, len);
1307  }
1308  }
1309  }
1310 
1311  if (avio_r8(pb) != 6)
1312  return AVERROR_INVALIDDATA;
1313  avio_skip(pb, 12);
1314  avio_skip(pb, avio_rb64(pb) + pos - avio_tell(s->pb));
1315  if (avio_r8(pb) != 8)
1316  return AVERROR_INVALIDDATA;
1317  avio_skip(pb, 8);
1318 
1319  return 0;
1320 }
1321 
1323 {
1324  RMDemuxContext *rm = s->priv_data;
1325  int ret = AVERROR_EOF, opcode;
1326  AVIOContext *pb = s->pb;
1327  unsigned size, index;
1328  int64_t pos, pts;
1329 
1330  if (avio_feof(pb) || rm->data_end)
1331  return AVERROR_EOF;
1332 
1333  pos = avio_tell(pb);
1334 
1335  for (;;) {
1336  if (rm->audio_pkt_cnt) {
1337  // If there are queued audio packet return them first
1338  AVStream *st;
1339 
1340  st = s->streams[rm->audio_stream_num];
1341  ret = ff_rm_retrieve_cache(s, pb, st, st->priv_data, pkt);
1342  if (ret < 0) {
1343  return ret;
1344  }
1345  } else {
1346  if (rm->remaining_len) {
1347  avio_skip(pb, rm->remaining_len);
1348  rm->remaining_len = 0;
1349  }
1350 
1351  if (avio_feof(pb))
1352  return AVERROR_EOF;
1353 
1354  opcode = avio_r8(pb);
1355  if (opcode == 2) {
1356  AVStream *st;
1357  int seq = 1;
1358 
1359  pts = avio_rb32(pb);
1360  index = avio_rb16(pb);
1361  if (index >= s->nb_streams)
1362  return AVERROR_INVALIDDATA;
1363 
1364  avio_skip(pb, 4);
1365  size = avio_rb32(pb);
1366  avio_skip(pb, 4);
1367 
1368  if (size < 1 || size > INT_MAX/4) {
1369  av_log(s, AV_LOG_ERROR, "size %u is invalid\n", size);
1370  return AVERROR_INVALIDDATA;
1371  }
1372 
1373  st = s->streams[index];
1374  ret = ff_rm_parse_packet(s, pb, st, st->priv_data, size, pkt,
1375  &seq, 0, pts);
1376  if (ret < -1) {
1377  return ret;
1378  } else if (ret) {
1379  continue;
1380  }
1381 
1382  pkt->pos = pos;
1383  pkt->pts = pts;
1384  pkt->stream_index = index;
1385  } else if (opcode == 7) {
1386  pos = avio_rb64(pb);
1387  if (!pos) {
1388  rm->data_end = 1;
1389  return AVERROR_EOF;
1390  }
1391  } else {
1392  av_log(s, AV_LOG_ERROR, "Unsupported opcode=%d at %"PRIX64"\n", opcode, avio_tell(pb) - 1);
1393  return AVERROR(EIO);
1394  }
1395  }
1396 
1397  break;
1398  }
1399 
1400  return ret;
1401 }
1402 
1404  .name = "ivr",
1405  .long_name = NULL_IF_CONFIG_SMALL("IVR (Internet Video Recording)"),
1406  .priv_data_size = sizeof(RMDemuxContext),
1407  .read_probe = ivr_probe,
1411  .extensions = "ivr",
1412 };
static int ivr_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: rmdec.c:1322
static int get_num(AVIOContext *pb, int *len)
Definition: rmdec.c:667
#define NULL
Definition: coverity.c:32
full parsing and interpolation of timestamps for frames not starting on a packet boundary ...
Definition: avformat.h:812
#define RAW_PACKET_SIZE
Definition: rmdec.c:684
const char const char void * val
Definition: avisynth_c.h:771
int remaining_len
Definition: rmdec.c:63
discard all frames except keyframes
Definition: avcodec.h:791
const char * s
Definition: avisynth_c.h:768
Bytestream IO Context.
Definition: avio.h:147
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
int64_t avio_size(AVIOContext *s)
Get the filesize.
Definition: aviobuf.c:309
static int rm_read_multi(AVFormatContext *s, AVIOContext *pb, AVStream *st, char *mime)
Definition: rmdec.c:494
#define DEINT_ID_INT0
no interleaving needed
Definition: rmdec.c:37
int av_add_index_entry(AVStream *st, int64_t pos, int64_t timestamp, int size, int distance, int flags)
Add an index entry into a sorted list.
Definition: utils.c:1958
static void get_strl(AVIOContext *pb, char *buf, int buf_size, int len)
Definition: rmdec.c:71
enum AVCodecID ff_codec_get_id(const AVCodecTag *tags, unsigned int tag)
Definition: utils.c:3028
static int64_t rm_read_dts(AVFormatContext *s, int stream_index, int64_t *ppos, int64_t pos_limit)
Definition: rmdec.c:1100
const unsigned char ff_sipr_subpk_size[4]
Definition: rmsipr.c:25
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
int64_t pos
byte position in stream, -1 if unknown
Definition: avcodec.h:1632
else temp
Definition: vf_mcdeint.c:259
void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: utils.c:4585
static int read_seek(AVFormatContext *ctx, int stream_index, int64_t timestamp, int flags)
Definition: libcdio.c:153
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:4000
int num
Numerator.
Definition: rational.h:59
int index
stream index in AVFormatContext
Definition: avformat.h:890
int size
Definition: avcodec.h:1613
Definition: rmdec.c:43
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:230
int cur_slice
Definition: rmdec.c:48
void * priv_data
Definition: avformat.h:904
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:304
int version
Definition: avisynth_c.h:766
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:252
static int rm_read_header(AVFormatContext *s)
Definition: rmdec.c:535
discard all
Definition: avcodec.h:792
static AVPacket pkt
unsigned int avio_rb16(AVIOContext *s)
Definition: aviobuf.c:742
This struct describes the properties of an encoded stream.
Definition: avcodec.h:3992
#define DEINT_ID_VBRS
VBR case for AAC.
Definition: rmdec.c:41
int videobufpos
position for the next slice in the video buffer
Definition: rmdec.c:46
Format I/O context.
Definition: avformat.h:1341
int audio_stream_num
Stream number for audio packets.
Definition: rmdec.c:64
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
#define AVFMT_FLAG_IGNIDX
Ignore index.
Definition: avformat.h:1454
Public dictionary API.
static struct @249 state
int ffio_limit(AVIOContext *s, int size)
Definition: utils.c:226
void void avpriv_request_sample(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
static int64_t start_time
Definition: ffplay.c:327
uint8_t
static int nb_streams
Definition: ffprobe.c:255
Opaque data information usually continuous.
Definition: avutil.h:203
int width
Video only.
Definition: avcodec.h:4066
#define AV_LOG_TRACE
Extremely verbose debugging, useful for libav* development.
Definition: log.h:202
unsigned int avio_rb32(AVIOContext *s)
Definition: aviobuf.c:757
static int ivr_probe(AVProbeData *p)
Definition: rmdec.c:1178
int id
Format-specific stream ID.
Definition: avformat.h:896
enum AVStreamParseType need_parsing
Definition: avformat.h:1079
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4215
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1409
int64_t duration
Definition: movenc.c:63
int flags
Flags modifying the (de)muxer behaviour.
Definition: avformat.h:1452
uint8_t * data
Definition: avcodec.h:1612
static int flags
Definition: log.c:57
uint32_t tag
Definition: movenc.c:1412
#define AVERROR_EOF
End of file.
Definition: error.h:55
int curpic_num
picture number of current frame
Definition: rmdec.c:47
static av_cold int read_close(AVFormatContext *ctx)
Definition: libcdio.c:145
int av_get_packet(AVIOContext *s, AVPacket *pkt, int size)
Allocate and read the payload of a packet and initialize its fields with default values.
Definition: utils.c:289
static int rm_read_index(AVFormatContext *s)
this function assumes that the demuxer has already seeked to the start of the INDX chunk...
Definition: rmdec.c:423
ptrdiff_t size
Definition: opengl_enc.c:101
uint64_t avio_rb64(AVIOContext *s)
Definition: aviobuf.c:824
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:511
int av_reduce(int *dst_num, int *dst_den, int64_t num, int64_t den, int64_t max)
Reduce a fraction.
Definition: rational.c:35
#define DEINT_ID_GENR
interleaving for Cooker/ATRAC
Definition: rmdec.c:36
static int rm_assemble_video_frame(AVFormatContext *s, AVIOContext *pb, RMDemuxContext *rm, RMStream *vst, AVPacket *pkt, int len, int *pseq, int64_t *timestamp)
Definition: rmdec.c:757
static void get_str8(AVIOContext *pb, char *buf, int buf_size)
Definition: rmdec.c:85
full parsing and repack with timestamp and position generation by parser for raw this assumes that ea...
Definition: avformat.h:814
uint64_t channel_layout
Audio only.
Definition: avcodec.h:4102
#define av_log(a,...)
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:604
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
Definition: avcodec.h:4029
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1644
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:86
#define AVINDEX_KEYFRAME
Definition: avformat.h:827
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
AVDictionary * metadata
Metadata that applies to the whole file.
Definition: avformat.h:1557
static int rm_read_close(AVFormatContext *s)
Definition: rmdec.c:1077
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:726
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:176
const char * r
Definition: vf_curves.c:111
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
enum AVMediaType codec_type
General type of the encoded data.
Definition: avcodec.h:3996
AVBufferRef * buf
A reference to the reference-counted buffer where the packet data is stored.
Definition: avcodec.h:1595
simple assert() macros that are a bit more flexible than ISO C assert().
#define DEINT_ID_VBRF
VBR case for AAC.
Definition: rmdec.c:40
static int rm_sync(AVFormatContext *s, int64_t *timestamp, int *flags, int *stream_index, int64_t *pos)
Definition: rmdec.c:686
int64_t pktpos
first slice position in file
Definition: rmdec.c:49
static const uint8_t offset[127][2]
Definition: vf_spp.c:92
GLsizei count
Definition: opengl_enc.c:109
AVRational avg_frame_rate
Average framerate.
Definition: avformat.h:970
#define FFMAX(a, b)
Definition: common.h:94
RMStream * ff_rm_alloc_rmstream(void)
Definition: rmdec.c:113
#define fail()
Definition: checkasm.h:84
int data_end
Definition: rmdec.c:66
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1618
int extradata_size
Size of the extradata content in bytes.
Definition: avcodec.h:4018
Only parse headers, do not repack.
Definition: avformat.h:811
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:595
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:463
common internal API header
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:1397
AVPacket pkt
place to store merged video frame / reordered audio data
Definition: rmdec.c:44
int block_align
Audio only.
Definition: avcodec.h:4117
int seekable
A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable.
Definition: avio.h:243
audio channel layout utility functions
int64_t av_rescale(int64_t a, int64_t b, int64_t c)
Rescale a 64-bit integer with rounding to nearest.
Definition: mathematics.c:129
#define AV_TIME_BASE
Internal time base represented as integer.
Definition: avutil.h:254
static int readfull(AVFormatContext *s, AVIOContext *pb, uint8_t *dst, int n)
Definition: rmdec.c:878
#define FFMIN(a, b)
Definition: common.h:96
void ff_free_stream(AVFormatContext *s, AVStream *st)
Definition: utils.c:4143
static int read_probe(AVProbeData *pd)
Definition: jvdec.c:55
AVInputFormat ff_rm_demuxer
Definition: rmdec.c:1158
GLsizei GLboolean const GLfloat * value
Definition: opengl_enc.c:109
int32_t
#define DEINT_ID_INT4
interleaving for 28.8
Definition: rmdec.c:38
#define AV_EF_EXPLODE
abort decoding on minor error detection
Definition: avcodec.h:2987
int n
Definition: avisynth_c.h:684
AVInputFormat ff_ivr_demuxer
Definition: rmdec.c:1403
static int rm_read_seek(AVFormatContext *s, int stream_index, int64_t pts, int flags)
Definition: rmdec.c:1146
int sub_packet_cnt
Definition: rmdec.c:52
#define FF_ARRAY_ELEMS(a)
int coded_framesize
Descrambling parameters from container.
Definition: rmdec.c:53
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:514
int audio_framesize
Definition: rmdec.c:54
Stream structure.
Definition: avformat.h:889
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_reading.c:42
static void rm_ac3_swap_bytes(AVStream *st, AVPacket *pkt)
Definition: rmdec.c:864
const AVCodecTag ff_rm_codec_tags[]
Definition: rm.c:31
static int rm_read_extradata(AVFormatContext *s, AVIOContext *pb, AVCodecParameters *par, unsigned size)
Definition: rmdec.c:90
#define DEINT_ID_SIPR
interleaving for Sipro
Definition: rmdec.c:39
AVIOContext * pb
I/O context.
Definition: avformat.h:1383
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:569
static int rm_probe(AVProbeData *p)
Definition: rmdec.c:1087
void * buf
Definition: avisynth_c.h:690
GLint GLenum type
Definition: opengl_enc.c:105
int ff_rm_parse_packet(AVFormatContext *s, AVIOContext *pb, AVStream *st, RMStream *ast, int len, AVPacket *pkt, int *seq, int flags, int64_t timestamp)
Parse one rm-stream packet from the input bytestream.
Definition: rmdec.c:889
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
Definition: dict.c:70
int index
Definition: gxfenc.c:89
int ff_rm_read_mdpr_codecdata(AVFormatContext *s, AVIOContext *pb, AVStream *st, RMStream *rst, unsigned int codec_data_size, const uint8_t *mime)
Read the MDPR chunk, which contains stream-specific codec initialization parameters.
Definition: rmdec.c:315
int current_stream
Definition: rmdec.c:62
int old_format
Definition: rmdec.c:61
This structure contains the data a format has to probe a file.
Definition: avformat.h:461
int audio_pkt_cnt
Output packet counter.
Definition: rmdec.c:65
int nb_packets
Definition: rmdec.c:60
static int64_t pts
Global timestamp for the audio frames.
const char *const ff_rm_metadata[4]
Definition: rm.c:24
AVInputFormat ff_rdt_demuxer
Definition: rmdec.c:1170
void ff_rm_free_rmstream(RMStream *rms)
Definition: rmdec.c:122
int sub_packet_h
Definition: rmdec.c:53
int ffio_ensure_seekback(AVIOContext *s, int64_t buf_size)
Ensures that the requested seekback buffer size will be available.
Definition: aviobuf.c:930
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:946
int sample_rate
Audio only.
Definition: avcodec.h:4110
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:473
full parsing and repack
Definition: avformat.h:810
void ff_rm_reorder_sipr_data(uint8_t *buf, int sub_packet_h, int framesize)
Perform 4-bit block reordering for SIPR data.
Definition: rmsipr.c:41
Main libavformat public API header.
int32_t deint_id
Length of each subpacket.
Definition: rmdec.c:56
#define AVFMT_NOFILE
Demuxer will use avio_open, no opened file should be provided by the caller.
Definition: avformat.h:478
int64_t start_time
Decoding: pts of the first frame of the stream in presentation order, in stream time base...
Definition: avformat.h:936
int error_recognition
Error recognition; higher values will detect more errors but may misdetect some more or less valid pa...
Definition: avformat.h:1584
int ff_get_extradata(AVFormatContext *s, AVCodecParameters *par, AVIOContext *pb, int size)
Allocate extradata with additional AV_INPUT_BUFFER_PADDING_SIZE at end which is always set to 0 and f...
Definition: utils.c:3212
int ff_rm_retrieve_cache(AVFormatContext *s, AVIOContext *pb, AVStream *st, RMStream *ast, AVPacket *pkt)
Retrieve one cached packet from the rm-context.
Definition: rmdec.c:988
int den
Denominator.
Definition: rational.h:60
static int rm_read_header_old(AVFormatContext *s)
Definition: rmdec.c:479
#define MKBETAG(a, b, c, d)
Definition: common.h:343
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:739
int slices
Definition: rmdec.c:48
int len
static int rm_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: rmdec.c:1019
static int rm_read_audio_stream_info(AVFormatContext *s, AVIOContext *pb, AVStream *st, RMStream *ast, int read_all)
Definition: rmdec.c:127
void * priv_data
Format private data.
Definition: avformat.h:1369
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition: avcodec.h:4014
int channels
Audio only.
Definition: avcodec.h:4106
int64_t duration
Duration of the stream, in AV_TIME_BASE fractional seconds.
Definition: avformat.h:1436
#define av_freep(p)
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:664
int64_t audiotimestamp
Audio descrambling matrix parameters.
Definition: rmdec.c:51
AVCodecParameters * codecpar
Definition: avformat.h:1244
int avio_get_str(AVIOContext *pb, int maxlen, char *buf, int buflen)
Read a string from pb into buf.
Definition: aviobuf.c:782
int avio_feof(AVIOContext *s)
feof() equivalent for AVIOContext.
Definition: aviobuf.c:328
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition: avcodec.h:4004
#define FFSWAP(type, a, b)
Definition: common.h:99
int stream_index
Definition: avcodec.h:1614
int videobufsize
current assembled frame size
Definition: rmdec.c:45
#define AV_CH_LAYOUT_MONO
#define MKTAG(a, b, c, d)
Definition: common.h:342
enum AVDiscard discard
Selects which packets can be discarded at will and do not need to be demuxed.
Definition: avformat.h:952
AVRational r_frame_rate
Real base framerate of the stream.
Definition: avformat.h:1106
int sub_packet_lengths[16]
Audio frame size from container.
Definition: rmdec.c:55
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:87
This structure stores compressed data.
Definition: avcodec.h:1589
static void rm_read_metadata(AVFormatContext *s, AVIOContext *pb, int wide)
Definition: rmdec.c:101
static int ivr_read_header(AVFormatContext *s)
Definition: rmdec.c:1187
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1605
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
int ff_seek_frame_binary(AVFormatContext *s, int stream_index, int64_t target_ts, int flags)
Perform a binary search using av_index_search_timestamp() and AVInputFormat.read_timestamp().
Definition: utils.c:2082
int sub_packet_size
Definition: rmdec.c:53
#define AV_WL32(p, v)
Definition: intreadwrite.h:426
const char * name
Definition: opengl_enc.c:103