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