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