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;
211  st->codecpar->block_align = coded_framesize;
212  break;
213  case AV_CODEC_ID_COOK:
215  case AV_CODEC_ID_ATRAC3:
216  case AV_CODEC_ID_SIPR:
217  if (read_all) {
218  codecdata_length = 0;
219  } else {
220  avio_rb16(pb); avio_r8(pb);
221  if (version == 5)
222  avio_r8(pb);
223  codecdata_length = avio_rb32(pb);
224  if(codecdata_length + AV_INPUT_BUFFER_PADDING_SIZE <= (unsigned)codecdata_length){
225  av_log(s, AV_LOG_ERROR, "codecdata_length too large\n");
226  return -1;
227  }
228  }
229 
231  if (st->codecpar->codec_id == AV_CODEC_ID_SIPR) {
232  if (flavor > 3) {
233  av_log(s, AV_LOG_ERROR, "bad SIPR file flavor %d\n",
234  flavor);
235  return -1;
236  }
237  st->codecpar->block_align = ff_sipr_subpk_size[flavor];
238  } else {
239  if(sub_packet_size <= 0){
240  av_log(s, AV_LOG_ERROR, "sub_packet_size is invalid\n");
241  return -1;
242  }
244  }
245  if ((ret = rm_read_extradata(s, pb, st->codecpar, codecdata_length)) < 0)
246  return ret;
247 
248  break;
249  case AV_CODEC_ID_AAC:
250  avio_rb16(pb); avio_r8(pb);
251  if (version == 5)
252  avio_r8(pb);
253  codecdata_length = avio_rb32(pb);
254  if(codecdata_length + AV_INPUT_BUFFER_PADDING_SIZE <= (unsigned)codecdata_length){
255  av_log(s, AV_LOG_ERROR, "codecdata_length too large\n");
256  return -1;
257  }
258  if (codecdata_length >= 1) {
259  avio_r8(pb);
260  if ((ret = rm_read_extradata(s, pb, st->codecpar, codecdata_length - 1)) < 0)
261  return ret;
262  }
263  break;
264  }
265  switch (ast->deint_id) {
266  case DEINT_ID_INT4:
267  if (ast->coded_framesize > ast->audio_framesize ||
268  sub_packet_h <= 1 ||
269  ast->coded_framesize * sub_packet_h > (2 + (sub_packet_h & 1)) * ast->audio_framesize)
270  return AVERROR_INVALIDDATA;
271  if (ast->coded_framesize * sub_packet_h != 2*ast->audio_framesize) {
272  avpriv_request_sample(s, "mismatching interleaver parameters");
273  return AVERROR_INVALIDDATA;
274  }
275  break;
276  case DEINT_ID_GENR:
277  if (ast->sub_packet_size <= 0 ||
278  ast->sub_packet_size > ast->audio_framesize)
279  return AVERROR_INVALIDDATA;
280  if (ast->audio_framesize % ast->sub_packet_size)
281  return AVERROR_INVALIDDATA;
282  break;
283  case DEINT_ID_SIPR:
284  case DEINT_ID_INT0:
285  case DEINT_ID_VBRS:
286  case DEINT_ID_VBRF:
287  break;
288  default:
289  av_log(s, AV_LOG_ERROR ,"Unknown interleaver %"PRIX32"\n", ast->deint_id);
290  return AVERROR_INVALIDDATA;
291  }
292  if (ast->deint_id == DEINT_ID_INT4 ||
293  ast->deint_id == DEINT_ID_GENR ||
294  ast->deint_id == DEINT_ID_SIPR) {
295  if (st->codecpar->block_align <= 0 ||
296  ast->audio_framesize * sub_packet_h > (unsigned)INT_MAX ||
297  ast->audio_framesize * sub_packet_h < st->codecpar->block_align)
298  return AVERROR_INVALIDDATA;
299  if (av_new_packet(&ast->pkt, ast->audio_framesize * sub_packet_h) < 0)
300  return AVERROR(ENOMEM);
301  }
302 
303  if (read_all) {
304  avio_r8(pb);
305  avio_r8(pb);
306  avio_r8(pb);
307  rm_read_metadata(s, pb, 0);
308  }
309  }
310  return 0;
311 }
312 
314  AVStream *st, RMStream *rst,
315  unsigned int codec_data_size, const uint8_t *mime)
316 {
317  unsigned int v;
318  int size;
319  int64_t codec_pos;
320  int ret;
321 
322  if (codec_data_size > INT_MAX)
323  return AVERROR_INVALIDDATA;
324  if (codec_data_size == 0)
325  return 0;
326 
327  avpriv_set_pts_info(st, 64, 1, 1000);
328  codec_pos = avio_tell(pb);
329  v = avio_rb32(pb);
330 
331  if (v == MKTAG(0xfd, 'a', 'r', '.')) {
332  /* ra type header */
333  if (rm_read_audio_stream_info(s, pb, st, rst, 0))
334  return -1;
335  } else if (v == MKBETAG('L', 'S', 'D', ':')) {
336  avio_seek(pb, -4, SEEK_CUR);
337  if ((ret = rm_read_extradata(s, pb, st->codecpar, codec_data_size)) < 0)
338  return ret;
339 
343  st->codecpar->codec_tag);
344  } else if(mime && !strcmp(mime, "logical-fileinfo")){
345  int stream_count, rule_count, property_count, i;
346  ff_free_stream(s, st);
347  if (avio_rb16(pb) != 0) {
348  av_log(s, AV_LOG_WARNING, "Unsupported version\n");
349  goto skip;
350  }
351  stream_count = avio_rb16(pb);
352  avio_skip(pb, 6*stream_count);
353  rule_count = avio_rb16(pb);
354  avio_skip(pb, 2*rule_count);
355  property_count = avio_rb16(pb);
356  for(i=0; i<property_count; i++){
357  uint8_t name[128], val[128];
358  avio_rb32(pb);
359  if (avio_rb16(pb) != 0) {
360  av_log(s, AV_LOG_WARNING, "Unsupported Name value property version\n");
361  goto skip; //FIXME skip just this one
362  }
363  get_str8(pb, name, sizeof(name));
364  switch(avio_rb32(pb)) {
365  case 2: get_strl(pb, val, sizeof(val), avio_rb16(pb));
366  av_dict_set(&s->metadata, name, val, 0);
367  break;
368  default: avio_skip(pb, avio_rb16(pb));
369  }
370  }
371  } else {
372  int fps;
373  if (avio_rl32(pb) != MKTAG('V', 'I', 'D', 'O')) {
374  fail1:
375  av_log(s, AV_LOG_WARNING, "Unsupported stream type %08x\n", v);
376  goto skip;
377  }
378  st->codecpar->codec_tag = avio_rl32(pb);
380  st->codecpar->codec_tag);
381  av_log(s, AV_LOG_TRACE, "%X %X\n", st->codecpar->codec_tag, MKTAG('R', 'V', '2', '0'));
382  if (st->codecpar->codec_id == AV_CODEC_ID_NONE)
383  goto fail1;
384  st->codecpar->width = avio_rb16(pb);
385  st->codecpar->height = avio_rb16(pb);
386  avio_skip(pb, 2); // looks like bits per sample
387  avio_skip(pb, 4); // always zero?
390  fps = avio_rb32(pb);
391 
392  if ((ret = rm_read_extradata(s, pb, st->codecpar, codec_data_size - (avio_tell(pb) - codec_pos))) < 0)
393  return ret;
394 
395  if (fps > 0) {
397  0x10000, fps, (1 << 30) - 1);
398 #if FF_API_R_FRAME_RATE
399  st->r_frame_rate = st->avg_frame_rate;
400 #endif
401  } else if (s->error_recognition & AV_EF_EXPLODE) {
402  av_log(s, AV_LOG_ERROR, "Invalid framerate\n");
403  return AVERROR_INVALIDDATA;
404  }
405  }
406 
407 skip:
408  /* skip codec info */
409  size = avio_tell(pb) - codec_pos;
410  if (codec_data_size >= size) {
411  avio_skip(pb, codec_data_size - size);
412  } else {
413  av_log(s, AV_LOG_WARNING, "codec_data_size %u < size %d\n", codec_data_size, size);
414  }
415 
416  return 0;
417 }
418 
419 /** this function assumes that the demuxer has already seeked to the start
420  * of the INDX chunk, and will bail out if not. */
422 {
423  AVIOContext *pb = s->pb;
424  unsigned int size, n_pkts, str_id, next_off, n, pos, pts;
425  AVStream *st;
426 
427  do {
428  if (avio_rl32(pb) != MKTAG('I','N','D','X'))
429  return -1;
430  size = avio_rb32(pb);
431  if (size < 20)
432  return -1;
433  avio_skip(pb, 2);
434  n_pkts = avio_rb32(pb);
435  str_id = avio_rb16(pb);
436  next_off = avio_rb32(pb);
437  for (n = 0; n < s->nb_streams; n++)
438  if (s->streams[n]->id == str_id) {
439  st = s->streams[n];
440  break;
441  }
442  if (n == s->nb_streams) {
443  av_log(s, AV_LOG_ERROR,
444  "Invalid stream index %d for index at pos %"PRId64"\n",
445  str_id, avio_tell(pb));
446  goto skip;
447  } else if ((avio_size(pb) - avio_tell(pb)) / 14 < n_pkts) {
448  av_log(s, AV_LOG_ERROR,
449  "Nr. of packets in packet index for stream index %d "
450  "exceeds filesize (%"PRId64" at %"PRId64" = %"PRId64")\n",
451  str_id, avio_size(pb), avio_tell(pb),
452  (avio_size(pb) - avio_tell(pb)) / 14);
453  goto skip;
454  }
455 
456  for (n = 0; n < n_pkts; n++) {
457  avio_skip(pb, 2);
458  pts = avio_rb32(pb);
459  pos = avio_rb32(pb);
460  avio_skip(pb, 4); /* packet no. */
461 
462  av_add_index_entry(st, pos, pts, 0, 0, AVINDEX_KEYFRAME);
463  }
464 
465 skip:
466  if (next_off && avio_tell(pb) < next_off &&
467  avio_seek(pb, next_off, SEEK_SET) < 0) {
468  av_log(s, AV_LOG_ERROR,
469  "Non-linear index detected, not supported\n");
470  return -1;
471  }
472  } while (next_off);
473 
474  return 0;
475 }
476 
478 {
479  RMDemuxContext *rm = s->priv_data;
480  AVStream *st;
481 
482  rm->old_format = 1;
483  st = avformat_new_stream(s, NULL);
484  if (!st)
485  return -1;
487  if (!st->priv_data)
488  return AVERROR(ENOMEM);
489  return rm_read_audio_stream_info(s, s->pb, st, st->priv_data, 1);
490 }
491 
493  AVStream *st, char *mime)
494 {
495  int number_of_streams = avio_rb16(pb);
496  int number_of_mdpr;
497  int i, ret;
498  unsigned size2;
499  for (i = 0; i<number_of_streams; i++)
500  avio_rb16(pb);
501  number_of_mdpr = avio_rb16(pb);
502  if (number_of_mdpr != 1) {
503  avpriv_request_sample(s, "MLTI with multiple (%d) MDPR", number_of_mdpr);
504  }
505  for (i = 0; i < number_of_mdpr; i++) {
506  AVStream *st2;
507  if (i > 0) {
508  st2 = avformat_new_stream(s, NULL);
509  if (!st2) {
510  ret = AVERROR(ENOMEM);
511  return ret;
512  }
513  st2->id = st->id + (i<<16);
514  st2->codecpar->bit_rate = st->codecpar->bit_rate;
515  st2->start_time = st->start_time;
516  st2->duration = st->duration;
519  if (!st2->priv_data)
520  return AVERROR(ENOMEM);
521  } else
522  st2 = st;
523 
524  size2 = avio_rb32(pb);
525  ret = ff_rm_read_mdpr_codecdata(s, s->pb, st2, st2->priv_data,
526  size2, mime);
527  if (ret < 0)
528  return ret;
529  }
530  return 0;
531 }
532 
534 {
535  RMDemuxContext *rm = s->priv_data;
536  AVStream *st;
537  AVIOContext *pb = s->pb;
538  unsigned int tag;
539  int tag_size;
540  unsigned int start_time, duration;
541  unsigned int data_off = 0, indx_off = 0;
542  char buf[128], mime[128];
543  int flags = 0;
544  int ret = -1;
545  unsigned size, v;
546  int64_t codec_pos;
547 
548  tag = avio_rl32(pb);
549  if (tag == MKTAG('.', 'r', 'a', 0xfd)) {
550  /* very old .ra format */
551  return rm_read_header_old(s);
552  } else if (tag != MKTAG('.', 'R', 'M', 'F')) {
553  return AVERROR(EIO);
554  }
555 
556  tag_size = avio_rb32(pb);
557  avio_skip(pb, tag_size - 8);
558 
559  for(;;) {
560  if (avio_feof(pb))
561  goto fail;
562  tag = avio_rl32(pb);
563  tag_size = avio_rb32(pb);
564  avio_rb16(pb);
565  av_log(s, AV_LOG_TRACE, "tag=%c%c%c%c (%08x) size=%d\n",
566  (tag ) & 0xff,
567  (tag >> 8) & 0xff,
568  (tag >> 16) & 0xff,
569  (tag >> 24) & 0xff,
570  tag,
571  tag_size);
572  if (tag_size < 10 && tag != MKTAG('D', 'A', 'T', 'A'))
573  goto fail;
574  switch(tag) {
575  case MKTAG('P', 'R', 'O', 'P'):
576  /* file header */
577  avio_rb32(pb); /* max bit rate */
578  avio_rb32(pb); /* avg bit rate */
579  avio_rb32(pb); /* max packet size */
580  avio_rb32(pb); /* avg packet size */
581  avio_rb32(pb); /* nb packets */
582  duration = avio_rb32(pb); /* duration */
583  s->duration = av_rescale(duration, AV_TIME_BASE, 1000);
584  avio_rb32(pb); /* preroll */
585  indx_off = avio_rb32(pb); /* index offset */
586  data_off = avio_rb32(pb); /* data offset */
587  avio_rb16(pb); /* nb streams */
588  flags = avio_rb16(pb); /* flags */
589  break;
590  case MKTAG('C', 'O', 'N', 'T'):
591  rm_read_metadata(s, pb, 1);
592  break;
593  case MKTAG('M', 'D', 'P', 'R'):
594  st = avformat_new_stream(s, NULL);
595  if (!st) {
596  ret = AVERROR(ENOMEM);
597  goto fail;
598  }
599  st->id = avio_rb16(pb);
600  avio_rb32(pb); /* max bit rate */
601  st->codecpar->bit_rate = avio_rb32(pb); /* bit rate */
602  avio_rb32(pb); /* max packet size */
603  avio_rb32(pb); /* avg packet size */
604  start_time = avio_rb32(pb); /* start time */
605  avio_rb32(pb); /* preroll */
606  duration = avio_rb32(pb); /* duration */
607  st->start_time = start_time;
608  st->duration = duration;
609  if(duration>0)
611  get_str8(pb, buf, sizeof(buf)); /* desc */
612  get_str8(pb, mime, sizeof(mime)); /* mimetype */
615  if (!st->priv_data)
616  return AVERROR(ENOMEM);
617 
618  size = avio_rb32(pb);
619  codec_pos = avio_tell(pb);
620 
621  ffio_ensure_seekback(pb, 4);
622  v = avio_rb32(pb);
623  if (v == MKBETAG('M', 'L', 'T', 'I')) {
624  ret = rm_read_multi(s, s->pb, st, mime);
625  if (ret < 0)
626  goto fail;
627  avio_seek(pb, codec_pos + size, SEEK_SET);
628  } else {
629  avio_skip(pb, -4);
630  if (ff_rm_read_mdpr_codecdata(s, s->pb, st, st->priv_data,
631  size, mime) < 0)
632  goto fail;
633  }
634 
635  break;
636  case MKTAG('D', 'A', 'T', 'A'):
637  goto header_end;
638  default:
639  /* unknown tag: skip it */
640  avio_skip(pb, tag_size - 10);
641  break;
642  }
643  }
644  header_end:
645  rm->nb_packets = avio_rb32(pb); /* number of packets */
646  if (!rm->nb_packets && (flags & 4))
647  rm->nb_packets = 3600 * 25;
648  avio_rb32(pb); /* next data header */
649 
650  if (!data_off)
651  data_off = avio_tell(pb) - 18;
652  if (indx_off && pb->seekable && !(s->flags & AVFMT_FLAG_IGNIDX) &&
653  avio_seek(pb, indx_off, SEEK_SET) >= 0) {
654  rm_read_index(s);
655  avio_seek(pb, data_off + 18, SEEK_SET);
656  }
657 
658  return 0;
659 
660 fail:
661  rm_read_close(s);
662  return ret;
663 }
664 
665 static int get_num(AVIOContext *pb, int *len)
666 {
667  int n, n1;
668 
669  n = avio_rb16(pb);
670  (*len)-=2;
671  n &= 0x7FFF;
672  if (n >= 0x4000) {
673  return n - 0x4000;
674  } else {
675  n1 = avio_rb16(pb);
676  (*len)-=2;
677  return (n << 16) | n1;
678  }
679 }
680 
681 /* multiple of 20 bytes for ra144 (ugly) */
682 #define RAW_PACKET_SIZE 1000
683 
684 static int rm_sync(AVFormatContext *s, int64_t *timestamp, int *flags, int *stream_index, int64_t *pos){
685  RMDemuxContext *rm = s->priv_data;
686  AVIOContext *pb = s->pb;
687  AVStream *st;
688  uint32_t state=0xFFFFFFFF;
689 
690  while(!avio_feof(pb)){
691  int len, num, i;
692  int mlti_id;
693  *pos= avio_tell(pb) - 3;
694  if(rm->remaining_len > 0){
695  num= rm->current_stream;
696  mlti_id = 0;
697  len= rm->remaining_len;
698  *timestamp = AV_NOPTS_VALUE;
699  *flags= 0;
700  }else{
701  state= (state<<8) + avio_r8(pb);
702 
703  if(state == MKBETAG('I', 'N', 'D', 'X')){
704  int n_pkts, expected_len;
705  len = avio_rb32(pb);
706  avio_skip(pb, 2);
707  n_pkts = avio_rb32(pb);
708  expected_len = 20 + n_pkts * 14;
709  if (len == 20)
710  /* some files don't add index entries to chunk size... */
711  len = expected_len;
712  else if (len != expected_len)
714  "Index size %d (%d pkts) is wrong, should be %d.\n",
715  len, n_pkts, expected_len);
716  len -= 14; // we already read part of the index header
717  if(len<0)
718  continue;
719  goto skip;
720  } else if (state == MKBETAG('D','A','T','A')) {
722  "DATA tag in middle of chunk, file may be broken.\n");
723  }
724 
725  if(state > (unsigned)0xFFFF || state <= 12)
726  continue;
727  len=state - 12;
728  state= 0xFFFFFFFF;
729 
730  num = avio_rb16(pb);
731  *timestamp = avio_rb32(pb);
732  mlti_id = (avio_r8(pb)>>1)-1<<16;
733  mlti_id = FFMAX(mlti_id, 0);
734  *flags = avio_r8(pb); /* flags */
735  }
736  for(i=0;i<s->nb_streams;i++) {
737  st = s->streams[i];
738  if (mlti_id + num == st->id)
739  break;
740  }
741  if (i == s->nb_streams) {
742 skip:
743  /* skip packet if unknown number */
744  avio_skip(pb, len);
745  rm->remaining_len = 0;
746  continue;
747  }
748  *stream_index= i;
749 
750  return len;
751  }
752  return -1;
753 }
754 
756  RMDemuxContext *rm, RMStream *vst,
757  AVPacket *pkt, int len, int *pseq,
758  int64_t *timestamp)
759 {
760  int hdr;
761  int seq = 0, pic_num = 0, len2 = 0, pos = 0; //init to silence compiler warning
762  int type;
763  int ret;
764 
765  hdr = avio_r8(pb); len--;
766  type = hdr >> 6;
767 
768  if(type != 3){ // not frame as a part of packet
769  seq = avio_r8(pb); len--;
770  }
771  if(type != 1){ // not whole frame
772  len2 = get_num(pb, &len);
773  pos = get_num(pb, &len);
774  pic_num = avio_r8(pb); len--;
775  }
776  if(len<0) {
777  av_log(s, AV_LOG_ERROR, "Insufficient data\n");
778  return -1;
779  }
780  rm->remaining_len = len;
781  if(type&1){ // frame, not slice
782  if(type == 3){ // frame as a part of packet
783  len= len2;
784  *timestamp = pos;
785  }
786  if(rm->remaining_len < len) {
787  av_log(s, AV_LOG_ERROR, "Insufficient remaining len\n");
788  return -1;
789  }
790  rm->remaining_len -= len;
791  if(av_new_packet(pkt, len + 9) < 0)
792  return AVERROR(EIO);
793  pkt->data[0] = 0;
794  AV_WL32(pkt->data + 1, 1);
795  AV_WL32(pkt->data + 5, 0);
796  if ((ret = avio_read(pb, pkt->data + 9, len)) != len) {
797  av_packet_unref(pkt);
798  av_log(s, AV_LOG_ERROR, "Failed to read %d bytes\n", len);
799  return ret < 0 ? ret : AVERROR(EIO);
800  }
801  return 0;
802  }
803  //now we have to deal with single slice
804 
805  *pseq = seq;
806  if((seq & 0x7F) == 1 || vst->curpic_num != pic_num){
807  if (len2 > ffio_limit(pb, len2)) {
808  av_log(s, AV_LOG_ERROR, "Impossibly sized packet\n");
809  return AVERROR_INVALIDDATA;
810  }
811  vst->slices = ((hdr & 0x3F) << 1) + 1;
812  vst->videobufsize = len2 + 8*vst->slices + 1;
813  av_packet_unref(&vst->pkt); //FIXME this should be output.
814  if(av_new_packet(&vst->pkt, vst->videobufsize) < 0)
815  return AVERROR(ENOMEM);
816  memset(vst->pkt.data, 0, vst->pkt.size);
817  vst->videobufpos = 8*vst->slices + 1;
818  vst->cur_slice = 0;
819  vst->curpic_num = pic_num;
820  vst->pktpos = avio_tell(pb);
821  }
822  if(type == 2)
823  len = FFMIN(len, pos);
824 
825  if(++vst->cur_slice > vst->slices) {
826  av_log(s, AV_LOG_ERROR, "cur slice %d, too large\n", vst->cur_slice);
827  return 1;
828  }
829  if(!vst->pkt.data)
830  return AVERROR(ENOMEM);
831  AV_WL32(vst->pkt.data - 7 + 8*vst->cur_slice, 1);
832  AV_WL32(vst->pkt.data - 3 + 8*vst->cur_slice, vst->videobufpos - 8*vst->slices - 1);
833  if(vst->videobufpos + len > vst->videobufsize) {
834  av_log(s, AV_LOG_ERROR, "outside videobufsize\n");
835  return 1;
836  }
837  if (avio_read(pb, vst->pkt.data + vst->videobufpos, len) != len)
838  return AVERROR(EIO);
839  vst->videobufpos += len;
840  rm->remaining_len-= len;
841 
842  if (type == 2 || vst->videobufpos == vst->videobufsize) {
843  vst->pkt.data[0] = vst->cur_slice-1;
844  *pkt= vst->pkt;
845  vst->pkt.data= NULL;
846  vst->pkt.size= 0;
847  vst->pkt.buf = NULL;
848  if(vst->slices != vst->cur_slice) //FIXME find out how to set slices correct from the begin
849  memmove(pkt->data + 1 + 8*vst->cur_slice, pkt->data + 1 + 8*vst->slices,
850  vst->videobufpos - 1 - 8*vst->slices);
851  pkt->size = vst->videobufpos + 8*(vst->cur_slice - vst->slices);
852  pkt->pts = AV_NOPTS_VALUE;
853  pkt->pos = vst->pktpos;
854  vst->slices = 0;
855  return 0;
856  }
857 
858  return 1;
859 }
860 
861 static inline void
863 {
864  uint8_t *ptr;
865  int j;
866 
867  if (st->codecpar->codec_id == AV_CODEC_ID_AC3) {
868  ptr = pkt->data;
869  for (j=0;j<pkt->size;j+=2) {
870  FFSWAP(int, ptr[0], ptr[1]);
871  ptr += 2;
872  }
873  }
874 }
875 
876 static int readfull(AVFormatContext *s, AVIOContext *pb, uint8_t *dst, int n) {
877  int ret = avio_read(pb, dst, n);
878  if (ret != n) {
879  if (ret >= 0) memset(dst + ret, 0, n - ret);
880  else memset(dst , 0, n);
881  av_log(s, AV_LOG_ERROR, "Failed to fully read block\n");
882  }
883  return ret;
884 }
885 
886 int
888  AVStream *st, RMStream *ast, int len, AVPacket *pkt,
889  int *seq, int flags, int64_t timestamp)
890 {
891  RMDemuxContext *rm = s->priv_data;
892  int ret;
893 
894  if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
895  rm->current_stream= st->id;
896  ret = rm_assemble_video_frame(s, pb, rm, ast, pkt, len, seq, &timestamp);
897  if(ret)
898  return ret < 0 ? ret : -1; //got partial frame or error
899  } else if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
900  if ((ast->deint_id == DEINT_ID_GENR) ||
901  (ast->deint_id == DEINT_ID_INT4) ||
902  (ast->deint_id == DEINT_ID_SIPR)) {
903  int x;
904  int sps = ast->sub_packet_size;
905  int cfs = ast->coded_framesize;
906  int h = ast->sub_packet_h;
907  int y = ast->sub_packet_cnt;
908  int w = ast->audio_framesize;
909 
910  if (flags & 2)
911  y = ast->sub_packet_cnt = 0;
912  if (!y)
913  ast->audiotimestamp = timestamp;
914 
915  switch (ast->deint_id) {
916  case DEINT_ID_INT4:
917  for (x = 0; x < h/2; x++)
918  readfull(s, pb, ast->pkt.data+x*2*w+y*cfs, cfs);
919  break;
920  case DEINT_ID_GENR:
921  for (x = 0; x < w/sps; x++)
922  readfull(s, pb, ast->pkt.data+sps*(h*x+((h+1)/2)*(y&1)+(y>>1)), sps);
923  break;
924  case DEINT_ID_SIPR:
925  readfull(s, pb, ast->pkt.data + y * w, w);
926  break;
927  }
928 
929  if (++(ast->sub_packet_cnt) < h)
930  return -1;
931  if (ast->deint_id == DEINT_ID_SIPR)
932  ff_rm_reorder_sipr_data(ast->pkt.data, h, w);
933 
934  ast->sub_packet_cnt = 0;
935  rm->audio_stream_num = st->index;
936  rm->audio_pkt_cnt = h * w / st->codecpar->block_align;
937  } else if ((ast->deint_id == DEINT_ID_VBRF) ||
938  (ast->deint_id == DEINT_ID_VBRS)) {
939  int x;
940  rm->audio_stream_num = st->index;
941  ast->sub_packet_cnt = (avio_rb16(pb) & 0xf0) >> 4;
942  if (ast->sub_packet_cnt) {
943  for (x = 0; x < ast->sub_packet_cnt; x++)
944  ast->sub_packet_lengths[x] = avio_rb16(pb);
945  rm->audio_pkt_cnt = ast->sub_packet_cnt;
946  ast->audiotimestamp = timestamp;
947  } else
948  return -1;
949  } else {
950  if ((ret = av_get_packet(pb, pkt, len)) < 0)
951  return ret;
952  rm_ac3_swap_bytes(st, pkt);
953  }
954  } else {
955  if ((ret = av_get_packet(pb, pkt, len)) < 0)
956  return ret;
957  }
958 
959  pkt->stream_index = st->index;
960 
961 #if 0
962  if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
963  if(st->codecpar->codec_id == AV_CODEC_ID_RV20){
964  int seq= 128*(pkt->data[2]&0x7F) + (pkt->data[3]>>1);
965  av_log(s, AV_LOG_DEBUG, "%d %"PRId64" %d\n", *timestamp, *timestamp*512LL/25, seq);
966 
967  seq |= (timestamp&~0x3FFF);
968  if(seq - timestamp > 0x2000) seq -= 0x4000;
969  if(seq - timestamp < -0x2000) seq += 0x4000;
970  }
971  }
972 #endif
973 
974  pkt->pts = timestamp;
975  if (flags & 2)
976  pkt->flags |= AV_PKT_FLAG_KEY;
977 
978  return st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO ? rm->audio_pkt_cnt : 0;
979 }
980 
981 int
983  AVStream *st, RMStream *ast, AVPacket *pkt)
984 {
985  RMDemuxContext *rm = s->priv_data;
986 
987  av_assert0 (rm->audio_pkt_cnt > 0);
988 
989  if (ast->deint_id == DEINT_ID_VBRF ||
990  ast->deint_id == DEINT_ID_VBRS) {
991  int ret = av_get_packet(pb, pkt, ast->sub_packet_lengths[ast->sub_packet_cnt - rm->audio_pkt_cnt]);
992  if (ret < 0)
993  return ret;
994  } else {
995  int ret = av_new_packet(pkt, st->codecpar->block_align);
996  if (ret < 0)
997  return ret;
998  memcpy(pkt->data, ast->pkt.data + st->codecpar->block_align * //FIXME avoid this
1000  st->codecpar->block_align);
1001  }
1002  rm->audio_pkt_cnt--;
1003  if ((pkt->pts = ast->audiotimestamp) != AV_NOPTS_VALUE) {
1005  pkt->flags = AV_PKT_FLAG_KEY;
1006  } else
1007  pkt->flags = 0;
1008  pkt->stream_index = st->index;
1009 
1010  return rm->audio_pkt_cnt;
1011 }
1012 
1014 {
1015  RMDemuxContext *rm = s->priv_data;
1016  AVStream *st = NULL; // init to silence compiler warning
1017  int i, len, res, seq = 1;
1018  int64_t timestamp, pos;
1019  int flags;
1020 
1021  for (;;) {
1022  if (rm->audio_pkt_cnt) {
1023  // If there are queued audio packet return them first
1024  st = s->streams[rm->audio_stream_num];
1025  res = ff_rm_retrieve_cache(s, s->pb, st, st->priv_data, pkt);
1026  if(res < 0)
1027  return res;
1028  flags = 0;
1029  } else {
1030  if (rm->old_format) {
1031  RMStream *ast;
1032 
1033  st = s->streams[0];
1034  ast = st->priv_data;
1035  timestamp = AV_NOPTS_VALUE;
1036  len = !ast->audio_framesize ? RAW_PACKET_SIZE :
1037  ast->coded_framesize * ast->sub_packet_h / 2;
1038  flags = (seq++ == 1) ? 2 : 0;
1039  pos = avio_tell(s->pb);
1040  } else {
1041  len = rm_sync(s, &timestamp, &flags, &i, &pos);
1042  if (len > 0)
1043  st = s->streams[i];
1044  }
1045 
1046  if (len <= 0 || avio_feof(s->pb))
1047  return AVERROR(EIO);
1048 
1049  res = ff_rm_parse_packet (s, s->pb, st, st->priv_data, len, pkt,
1050  &seq, flags, timestamp);
1051  if (res < -1)
1052  return res;
1053  if((flags&2) && (seq&0x7F) == 1)
1054  av_add_index_entry(st, pos, timestamp, 0, 0, AVINDEX_KEYFRAME);
1055  if (res)
1056  continue;
1057  }
1058 
1059  if( (st->discard >= AVDISCARD_NONKEY && !(flags&2))
1060  || st->discard >= AVDISCARD_ALL){
1061  av_packet_unref(pkt);
1062  } else
1063  break;
1064  }
1065 
1066  return 0;
1067 }
1068 
1070 {
1071  int i;
1072 
1073  for (i=0;i<s->nb_streams;i++)
1075 
1076  return 0;
1077 }
1078 
1079 static int rm_probe(AVProbeData *p)
1080 {
1081  /* check file header */
1082  if ((p->buf[0] == '.' && p->buf[1] == 'R' &&
1083  p->buf[2] == 'M' && p->buf[3] == 'F' &&
1084  p->buf[4] == 0 && p->buf[5] == 0) ||
1085  (p->buf[0] == '.' && p->buf[1] == 'r' &&
1086  p->buf[2] == 'a' && p->buf[3] == 0xfd))
1087  return AVPROBE_SCORE_MAX;
1088  else
1089  return 0;
1090 }
1091 
1092 static int64_t rm_read_dts(AVFormatContext *s, int stream_index,
1093  int64_t *ppos, int64_t pos_limit)
1094 {
1095  RMDemuxContext *rm = s->priv_data;
1096  int64_t pos, dts;
1097  int stream_index2, flags, len, h;
1098 
1099  pos = *ppos;
1100 
1101  if(rm->old_format)
1102  return AV_NOPTS_VALUE;
1103 
1104  if (avio_seek(s->pb, pos, SEEK_SET) < 0)
1105  return AV_NOPTS_VALUE;
1106 
1107  rm->remaining_len=0;
1108  for(;;){
1109  int seq=1;
1110  AVStream *st;
1111 
1112  len = rm_sync(s, &dts, &flags, &stream_index2, &pos);
1113  if(len<0)
1114  return AV_NOPTS_VALUE;
1115 
1116  st = s->streams[stream_index2];
1117  if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
1118  h= avio_r8(s->pb); len--;
1119  if(!(h & 0x40)){
1120  seq = avio_r8(s->pb); len--;
1121  }
1122  }
1123 
1124  if((flags&2) && (seq&0x7F) == 1){
1125  av_log(s, AV_LOG_TRACE, "%d %d-%d %"PRId64" %d\n",
1126  flags, stream_index2, stream_index, dts, seq);
1127  av_add_index_entry(st, pos, dts, 0, 0, AVINDEX_KEYFRAME);
1128  if(stream_index2 == stream_index)
1129  break;
1130  }
1131 
1132  avio_skip(s->pb, len);
1133  }
1134  *ppos = pos;
1135  return dts;
1136 }
1137 
1138 static int rm_read_seek(AVFormatContext *s, int stream_index,
1139  int64_t pts, int flags)
1140 {
1141  RMDemuxContext *rm = s->priv_data;
1142 
1143  if (ff_seek_frame_binary(s, stream_index, pts, flags) < 0)
1144  return -1;
1145  rm->audio_pkt_cnt = 0;
1146  return 0;
1147 }
1148 
1149 
1151  .name = "rm",
1152  .long_name = NULL_IF_CONFIG_SMALL("RealMedia"),
1153  .priv_data_size = sizeof(RMDemuxContext),
1154  .read_probe = rm_probe,
1158  .read_timestamp = rm_read_dts,
1160 };
1161 
1163  .name = "rdt",
1164  .long_name = NULL_IF_CONFIG_SMALL("RDT demuxer"),
1165  .priv_data_size = sizeof(RMDemuxContext),
1167  .flags = AVFMT_NOFILE,
1168 };
1169 
1170 static int ivr_probe(AVProbeData *p)
1171 {
1172  if (memcmp(p->buf, ".R1M\x0\x1\x1", 7) &&
1173  memcmp(p->buf, ".REC", 4))
1174  return 0;
1175 
1176  return AVPROBE_SCORE_MAX;
1177 }
1178 
1180 {
1181  unsigned tag, type, len, tlen, value;
1182  int i, j, n, count, nb_streams = 0, ret;
1183  uint8_t key[256], val[256];
1184  AVIOContext *pb = s->pb;
1185  AVStream *st;
1186  int64_t pos, offset, temp;
1187 
1188  pos = avio_tell(pb);
1189  tag = avio_rl32(pb);
1190  if (tag == MKTAG('.','R','1','M')) {
1191  if (avio_rb16(pb) != 1)
1192  return AVERROR_INVALIDDATA;
1193  if (avio_r8(pb) != 1)
1194  return AVERROR_INVALIDDATA;
1195  len = avio_rb32(pb);
1196  avio_skip(pb, len);
1197  avio_skip(pb, 5);
1198  temp = avio_rb64(pb);
1199  while (!avio_feof(pb) && temp) {
1200  offset = temp;
1201  temp = avio_rb64(pb);
1202  }
1203  avio_skip(pb, offset - avio_tell(pb));
1204  if (avio_r8(pb) != 1)
1205  return AVERROR_INVALIDDATA;
1206  len = avio_rb32(pb);
1207  avio_skip(pb, len);
1208  if (avio_r8(pb) != 2)
1209  return AVERROR_INVALIDDATA;
1210  avio_skip(pb, 16);
1211  pos = avio_tell(pb);
1212  tag = avio_rl32(pb);
1213  }
1214 
1215  if (tag != MKTAG('.','R','E','C'))
1216  return AVERROR_INVALIDDATA;
1217 
1218  if (avio_r8(pb) != 0)
1219  return AVERROR_INVALIDDATA;
1220  count = avio_rb32(pb);
1221  for (i = 0; i < count; i++) {
1222  if (avio_feof(pb))
1223  return AVERROR_INVALIDDATA;
1224 
1225  type = avio_r8(pb);
1226  tlen = avio_rb32(pb);
1227  avio_get_str(pb, tlen, key, sizeof(key));
1228  len = avio_rb32(pb);
1229  if (type == 5) {
1230  avio_get_str(pb, len, val, sizeof(val));
1231  av_log(s, AV_LOG_DEBUG, "%s = '%s'\n", key, val);
1232  } else if (type == 4) {
1233  av_log(s, AV_LOG_DEBUG, "%s = '0x", key);
1234  for (j = 0; j < len; j++)
1235  av_log(s, AV_LOG_DEBUG, "%X", avio_r8(pb));
1236  av_log(s, AV_LOG_DEBUG, "'\n");
1237  } else if (len == 4 && type == 3 && !strncmp(key, "StreamCount", tlen)) {
1238  nb_streams = value = avio_rb32(pb);
1239  } else if (len == 4 && type == 3) {
1240  value = avio_rb32(pb);
1241  av_log(s, AV_LOG_DEBUG, "%s = %d\n", key, value);
1242  } else {
1243  av_log(s, AV_LOG_DEBUG, "Skipping unsupported key: %s\n", key);
1244  avio_skip(pb, len);
1245  }
1246  }
1247 
1248  for (n = 0; n < nb_streams; n++) {
1249  st = avformat_new_stream(s, NULL);
1250  if (!st)
1251  return AVERROR(ENOMEM);
1253  if (!st->priv_data)
1254  return AVERROR(ENOMEM);
1255 
1256  if (avio_r8(pb) != 1)
1257  return AVERROR_INVALIDDATA;
1258 
1259  count = avio_rb32(pb);
1260  for (i = 0; i < count; i++) {
1261  if (avio_feof(pb))
1262  return AVERROR_INVALIDDATA;
1263 
1264  type = avio_r8(pb);
1265  tlen = avio_rb32(pb);
1266  avio_get_str(pb, tlen, key, sizeof(key));
1267  len = avio_rb32(pb);
1268  if (type == 5) {
1269  avio_get_str(pb, len, val, sizeof(val));
1270  av_log(s, AV_LOG_DEBUG, "%s = '%s'\n", key, val);
1271  } else if (type == 4 && !strncmp(key, "OpaqueData", tlen)) {
1272  ret = ffio_ensure_seekback(pb, 4);
1273  if (ret < 0)
1274  return ret;
1275  if (avio_rb32(pb) == MKBETAG('M', 'L', 'T', 'I')) {
1276  ret = rm_read_multi(s, pb, st, NULL);
1277  } else {
1278  avio_seek(pb, -4, SEEK_CUR);
1279  ret = ff_rm_read_mdpr_codecdata(s, pb, st, st->priv_data, len, NULL);
1280  }
1281 
1282  if (ret < 0)
1283  return ret;
1284  } else if (type == 4) {
1285  int j;
1286 
1287  av_log(s, AV_LOG_DEBUG, "%s = '0x", key);
1288  for (j = 0; j < len; j++)
1289  av_log(s, AV_LOG_DEBUG, "%X", avio_r8(pb));
1290  av_log(s, AV_LOG_DEBUG, "'\n");
1291  } else if (len == 4 && type == 3 && !strncmp(key, "Duration", tlen)) {
1292  st->duration = avio_rb32(pb);
1293  } else if (len == 4 && type == 3) {
1294  value = avio_rb32(pb);
1295  av_log(s, AV_LOG_DEBUG, "%s = %d\n", key, value);
1296  } else {
1297  av_log(s, AV_LOG_DEBUG, "Skipping unsupported key: %s\n", key);
1298  avio_skip(pb, len);
1299  }
1300  }
1301  }
1302 
1303  if (avio_r8(pb) != 6)
1304  return AVERROR_INVALIDDATA;
1305  avio_skip(pb, 12);
1306  avio_skip(pb, avio_rb64(pb) + pos - avio_tell(s->pb));
1307  if (avio_r8(pb) != 8)
1308  return AVERROR_INVALIDDATA;
1309  avio_skip(pb, 8);
1310 
1311  return 0;
1312 }
1313 
1315 {
1316  RMDemuxContext *rm = s->priv_data;
1317  int ret = AVERROR_EOF, opcode;
1318  AVIOContext *pb = s->pb;
1319  unsigned size, index;
1320  int64_t pos, pts;
1321 
1322  if (avio_feof(pb) || rm->data_end)
1323  return AVERROR_EOF;
1324 
1325  pos = avio_tell(pb);
1326 
1327  for (;;) {
1328  if (rm->audio_pkt_cnt) {
1329  // If there are queued audio packet return them first
1330  AVStream *st;
1331 
1332  st = s->streams[rm->audio_stream_num];
1333  ret = ff_rm_retrieve_cache(s, pb, st, st->priv_data, pkt);
1334  if (ret < 0) {
1335  return ret;
1336  }
1337  } else {
1338  if (rm->remaining_len) {
1339  avio_skip(pb, rm->remaining_len);
1340  rm->remaining_len = 0;
1341  }
1342 
1343  if (avio_feof(pb))
1344  return AVERROR_EOF;
1345 
1346  opcode = avio_r8(pb);
1347  if (opcode == 2) {
1348  AVStream *st;
1349  int seq = 1;
1350 
1351  pts = avio_rb32(pb);
1352  index = avio_rb16(pb);
1353  if (index >= s->nb_streams)
1354  return AVERROR_INVALIDDATA;
1355 
1356  avio_skip(pb, 4);
1357  size = avio_rb32(pb);
1358  avio_skip(pb, 4);
1359 
1360  if (size < 1 || size > INT_MAX/4) {
1361  av_log(s, AV_LOG_ERROR, "size %u is invalid\n", size);
1362  return AVERROR_INVALIDDATA;
1363  }
1364 
1365  st = s->streams[index];
1366  ret = ff_rm_parse_packet(s, pb, st, st->priv_data, size, pkt,
1367  &seq, 0, pts);
1368  if (ret < -1) {
1369  return ret;
1370  } else if (ret) {
1371  continue;
1372  }
1373 
1374  pkt->pos = pos;
1375  pkt->pts = pts;
1376  pkt->stream_index = index;
1377  } else if (opcode == 7) {
1378  pos = avio_rb64(pb);
1379  if (!pos) {
1380  rm->data_end = 1;
1381  return AVERROR_EOF;
1382  }
1383  } else {
1384  av_log(s, AV_LOG_ERROR, "Unsupported opcode=%d at %"PRIX64"\n", opcode, avio_tell(pb) - 1);
1385  return AVERROR(EIO);
1386  }
1387  }
1388 
1389  break;
1390  }
1391 
1392  return ret;
1393 }
1394 
1396  .name = "ivr",
1397  .long_name = NULL_IF_CONFIG_SMALL("IVR (Internet Video Recording)"),
1398  .priv_data_size = sizeof(RMDemuxContext),
1399  .read_probe = ivr_probe,
1403  .extensions = "ivr",
1404 };
static int ivr_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: rmdec.c:1314
static int get_num(AVIOContext *pb, int *len)
Definition: rmdec.c:665
#define NULL
Definition: coverity.c:32
full parsing and interpolation of timestamps for frames not starting on a packet boundary ...
Definition: avformat.h:808
#define RAW_PACKET_SIZE
Definition: rmdec.c:682
const char const char void * val
Definition: avisynth_c.h:634
int remaining_len
Definition: rmdec.c:63
discard all frames except keyframes
Definition: avcodec.h:783
const char * s
Definition: avisynth_c.h:631
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:492
#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:1909
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:2955
static int64_t rm_read_dts(AVFormatContext *s, int stream_index, int64_t *ppos, int64_t pos_limit)
Definition: rmdec.c:1092
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:1608
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:4493
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:3939
int num
numerator
Definition: rational.h:44
int index
stream index in AVFormatContext
Definition: avformat.h:877
int size
Definition: avcodec.h:1589
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:891
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:629
static int rm_read_header(AVFormatContext *s)
Definition: rmdec.c:533
discard all
Definition: avcodec.h:784
static AVPacket pkt
unsigned int avio_rb16(AVIOContext *s)
Definition: aviobuf.c:736
This struct describes the properties of an encoded stream.
Definition: avcodec.h:3931
#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:1325
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:1438
Public dictionary API.
int ffio_limit(AVIOContext *s, int size)
Definition: utils.c:197
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:330
uint8_t
static int nb_streams
Definition: ffprobe.c:254
Opaque data information usually continuous.
Definition: avutil.h:195
int width
Video only.
Definition: avcodec.h:4005
#define AV_LOG_TRACE
Extremely verbose debugging, useful for libav* development.
Definition: log.h:202
unsigned int avio_rb32(AVIOContext *s)
Definition: aviobuf.c:751
static int ivr_probe(AVProbeData *p)
Definition: rmdec.c:1170
int id
Format-specific stream ID.
Definition: avformat.h:883
enum AVStreamParseType need_parsing
Definition: avformat.h:1063
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4131
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1393
int64_t duration
Definition: movenc.c:63
int flags
Flags modifying the (de)muxer behaviour.
Definition: avformat.h:1436
uint8_t * data
Definition: avcodec.h:1588
uint32_t tag
Definition: movenc.c:1376
#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:260
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:421
ptrdiff_t size
Definition: opengl_enc.c:101
uint64_t avio_rb64(AVIOContext *s)
Definition: aviobuf.c:818
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:511
#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:755
static void get_str8(AVIOContext *pb, char *buf, int buf_size)
Definition: rmdec.c:85
uint64_t channel_layout
Audio only.
Definition: avcodec.h:4041
#define av_log(a,...)
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:598
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
Definition: avcodec.h:3968
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1620
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:823
#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:1539
static int rm_read_close(AVFormatContext *s)
Definition: rmdec.c:1069
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:720
#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:3935
AVBufferRef * buf
A reference to the reference-counted buffer where the packet data is stored.
Definition: avcodec.h:1571
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:684
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:954
#define FFMAX(a, b)
Definition: common.h:94
RMStream * ff_rm_alloc_rmstream(void)
Definition: rmdec.c:113
#define fail()
Definition: checkasm.h:82
int data_end
Definition: rmdec.c:66
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1594
int extradata_size
Size of the extradata content in bytes.
Definition: avcodec.h:3957
Only parse headers, do not repack.
Definition: avformat.h:807
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:589
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
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:461
common internal API header
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:1381
AVPacket pkt
place to store merged video frame / reordered audio data
Definition: rmdec.c:44
int block_align
Audio only.
Definition: avcodec.h:4056
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:246
static int readfull(AVFormatContext *s, AVIOContext *pb, uint8_t *dst, int n)
Definition: rmdec.c:876
#define FFMIN(a, b)
Definition: common.h:96
void ff_free_stream(AVFormatContext *s, AVStream *st)
Definition: utils.c:4059
static int read_probe(AVProbeData *pd)
Definition: jvdec.c:55
AVInputFormat ff_rm_demuxer
Definition: rmdec.c:1150
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:2955
int n
Definition: avisynth_c.h:547
AVInputFormat ff_ivr_demuxer
Definition: rmdec.c:1395
static int rm_read_seek(AVFormatContext *s, int stream_index, int64_t pts, int flags)
Definition: rmdec.c:1138
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:638
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:862
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:1367
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:563
static int rm_probe(AVProbeData *p)
Definition: rmdec.c:1079
void * buf
Definition: avisynth_c.h:553
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:887
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:69
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:313
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:459
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:1162
static int flags
Definition: cpu.c:47
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:924
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:930
int sample_rate
Audio only.
Definition: avcodec.h:4049
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:471
full parsing and repack
Definition: avformat.h:806
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:476
int64_t start_time
Decoding: pts of the first frame of the stream in presentation order, in stream time base...
Definition: avformat.h:923
int error_recognition
Error recognition; higher values will detect more errors but may misdetect some more or less valid pa...
Definition: avformat.h:1566
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:3137
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:982
int den
denominator
Definition: rational.h:45
static int rm_read_header_old(AVFormatContext *s)
Definition: rmdec.c:477
#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:731
int slices
Definition: rmdec.c:48
int len
static int rm_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: rmdec.c:1013
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:1353
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition: avcodec.h:3953
int channels
Audio only.
Definition: avcodec.h:4045
int64_t duration
Duration of the stream, in AV_TIME_BASE fractional seconds.
Definition: avformat.h:1420
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:660
int64_t audiotimestamp
Audio descrambling matrix parameters.
Definition: rmdec.c:51
AVCodecParameters * codecpar
Definition: avformat.h:1228
int avio_get_str(AVIOContext *pb, int maxlen, char *buf, int buflen)
Read a string from pb into buf.
Definition: aviobuf.c:776
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:3943
#define FFSWAP(type, a, b)
Definition: common.h:99
int stream_index
Definition: avcodec.h:1590
int videobufsize
current assembled frame size
Definition: rmdec.c:45
static struct @230 state
#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:936
AVRational r_frame_rate
Real base framerate of the stream.
Definition: avformat.h:1090
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:1565
static void rm_read_metadata(AVFormatContext *s, AVIOContext *pb, int wide)
Definition: rmdec.c:101
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:252
static int ivr_read_header(AVFormatContext *s)
Definition: rmdec.c:1179
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1581
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:240
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:2023
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