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