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