Libav
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 Libav.
6  *
7  * Libav 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  * Libav 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 Libav; 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/avstring.h"
26 #include "libavutil/internal.h"
27 #include "libavutil/intreadwrite.h"
28 #include "libavutil/dict.h"
29 #include "avformat.h"
30 #include "internal.h"
31 #include "rmsipr.h"
32 #include "rm.h"
33 
34 #define DEINT_ID_GENR MKTAG('g', 'e', 'n', 'r')
35 #define DEINT_ID_INT0 MKTAG('I', 'n', 't', '0')
36 #define DEINT_ID_INT4 MKTAG('I', 'n', 't', '4')
37 #define DEINT_ID_SIPR MKTAG('s', 'i', 'p', 'r')
38 #define DEINT_ID_VBRF MKTAG('v', 'b', 'r', 'f')
39 #define DEINT_ID_VBRS MKTAG('v', 'b', 'r', 's')
40 
41 struct RMStream {
45  int curpic_num;
47  int64_t pktpos;
48  int64_t audiotimestamp;
50  int sub_packet_cnt; // Subpacket counter, used while reading
55 };
56 
57 typedef struct RMDemuxContext {
65 
66 static inline void get_strl(AVIOContext *pb, char *buf, int buf_size, int len)
67 {
68  int i;
69  char *q, r;
70 
71  q = buf;
72  for(i=0;i<len;i++) {
73  r = avio_r8(pb);
74  if (i < buf_size - 1)
75  *q++ = r;
76  }
77  if (buf_size > 0) *q = '\0';
78 }
79 
80 static void get_str8(AVIOContext *pb, char *buf, int buf_size)
81 {
82  get_strl(pb, buf, buf_size, avio_r8(pb));
83 }
84 
85 static int rm_read_extradata(AVIOContext *pb, AVCodecParameters *par, unsigned size)
86 {
87  if (size >= 1<<24)
88  return -1;
90  if (!par->extradata)
91  return AVERROR(ENOMEM);
92  par->extradata_size = avio_read(pb, par->extradata, size);
93  if (par->extradata_size != size)
94  return AVERROR(EIO);
95  return 0;
96 }
97 
98 static void rm_read_metadata(AVFormatContext *s, AVIOContext *pb, int wide)
99 {
100  char buf[1024];
101  int i;
102  for (i=0; i<FF_ARRAY_ELEMS(ff_rm_metadata); i++) {
103  int len = wide ? avio_rb16(pb) : avio_r8(pb);
104  get_strl(pb, buf, sizeof(buf), len);
105  av_dict_set(&s->metadata, ff_rm_metadata[i], buf, 0);
106  }
107 }
108 
110 {
111  RMStream *rms = av_mallocz(sizeof(RMStream));
112  if (!rms)
113  return NULL;
114  rms->curpic_num = -1;
115  return rms;
116 }
117 
119 {
120  av_packet_unref(&rms->pkt);
121 }
122 
124  AVStream *st, RMStream *ast, int read_all)
125 {
126  char buf[256];
127  uint32_t version;
128  int ret;
129 
130  /* ra type header */
131  version = avio_rb16(pb); /* version */
132  if (version == 3) {
133  int header_size = avio_rb16(pb);
134  int64_t startpos = avio_tell(pb);
135  avio_skip(pb, 14);
136  rm_read_metadata(s, pb, 0);
137  if ((startpos + header_size) >= avio_tell(pb) + 2) {
138  // fourcc (should always be "lpcJ")
139  avio_r8(pb);
140  get_str8(pb, buf, sizeof(buf));
141  }
142  // Skip extra header crap (this should never happen)
143  if ((startpos + header_size) > avio_tell(pb))
144  avio_skip(pb, header_size + startpos - avio_tell(pb));
145  st->codecpar->sample_rate = 8000;
146  st->codecpar->channels = 1;
150  ast->deint_id = DEINT_ID_INT0;
151  } else {
153  int codecdata_length;
154  /* old version (4) */
155  avio_skip(pb, 2); /* unused */
156  avio_rb32(pb); /* .ra4 */
157  avio_rb32(pb); /* data size */
158  avio_rb16(pb); /* version2 */
159  avio_rb32(pb); /* header size */
160  flavor= avio_rb16(pb); /* add codec info / flavor */
161  ast->coded_framesize = coded_framesize = avio_rb32(pb); /* coded frame size */
162  avio_rb32(pb); /* ??? */
163  avio_rb32(pb); /* ??? */
164  avio_rb32(pb); /* ??? */
165  ast->sub_packet_h = sub_packet_h = avio_rb16(pb); /* 1 */
166  st->codecpar->block_align= avio_rb16(pb); /* frame size */
167  ast->sub_packet_size = sub_packet_size = avio_rb16(pb); /* sub packet size */
168  avio_rb16(pb); /* ??? */
169  if (version == 5) {
170  avio_rb16(pb); avio_rb16(pb); avio_rb16(pb);
171  }
172  st->codecpar->sample_rate = avio_rb16(pb);
173  avio_rb32(pb);
174  st->codecpar->channels = avio_rb16(pb);
175  if (version == 5) {
176  ast->deint_id = avio_rl32(pb);
177  avio_read(pb, buf, 4);
178  buf[4] = 0;
179  } else {
180  get_str8(pb, buf, sizeof(buf)); /* desc */
181  ast->deint_id = AV_RL32(buf);
182  get_str8(pb, buf, sizeof(buf)); /* desc */
183  }
185  st->codecpar->codec_tag = AV_RL32(buf);
187  st->codecpar->codec_tag);
188 
189  switch (st->codecpar->codec_id) {
190  case AV_CODEC_ID_AC3:
192  break;
193  case AV_CODEC_ID_RA_288:
194  st->codecpar->extradata_size= 0;
197  break;
198  case AV_CODEC_ID_COOK:
200  case AV_CODEC_ID_ATRAC3:
201  case AV_CODEC_ID_SIPR:
202  avio_rb16(pb); avio_r8(pb);
203  if (version == 5)
204  avio_r8(pb);
205  codecdata_length = avio_rb32(pb);
206  if(codecdata_length + AV_INPUT_BUFFER_PADDING_SIZE <= (unsigned)codecdata_length){
207  av_log(s, AV_LOG_ERROR, "codecdata_length too large\n");
208  return -1;
209  }
210 
212  if (st->codecpar->codec_id == AV_CODEC_ID_SIPR) {
213  if (flavor > 3) {
214  av_log(s, AV_LOG_ERROR, "bad SIPR file flavor %d\n",
215  flavor);
216  return -1;
217  }
218  st->codecpar->block_align = ff_sipr_subpk_size[flavor];
219  } else {
220  if(sub_packet_size <= 0){
221  av_log(s, AV_LOG_ERROR, "sub_packet_size is invalid\n");
222  return -1;
223  }
225  }
226  if ((ret = rm_read_extradata(pb, st->codecpar, codecdata_length)) < 0)
227  return ret;
228  break;
229  case AV_CODEC_ID_AAC:
230  avio_rb16(pb); avio_r8(pb);
231  if (version == 5)
232  avio_r8(pb);
233  codecdata_length = avio_rb32(pb);
234  if(codecdata_length + AV_INPUT_BUFFER_PADDING_SIZE <= (unsigned)codecdata_length){
235  av_log(s, AV_LOG_ERROR, "codecdata_length too large\n");
236  return -1;
237  }
238  if (codecdata_length >= 1) {
239  avio_r8(pb);
240  if ((ret = rm_read_extradata(pb, st->codecpar, codecdata_length - 1)) < 0)
241  return ret;
242  }
243  break;
244  }
245  if (ast->deint_id == DEINT_ID_INT4 ||
246  ast->deint_id == DEINT_ID_GENR ||
247  ast->deint_id == DEINT_ID_SIPR) {
248  if (st->codecpar->block_align <= 0 ||
249  ast->audio_framesize * sub_packet_h > (unsigned)INT_MAX ||
250  ast->audio_framesize * sub_packet_h < st->codecpar->block_align)
251  return AVERROR_INVALIDDATA;
252  if (av_new_packet(&ast->pkt, ast->audio_framesize * sub_packet_h) < 0)
253  return AVERROR(ENOMEM);
254  }
255  switch (ast->deint_id) {
256  case DEINT_ID_INT4:
257  if (ast->coded_framesize > ast->audio_framesize ||
258  sub_packet_h <= 1 ||
259  ast->coded_framesize * sub_packet_h > (2 + (sub_packet_h & 1)) * ast->audio_framesize)
260  return AVERROR_INVALIDDATA;
261  break;
262  case DEINT_ID_GENR:
263  if (ast->sub_packet_size <= 0 ||
264  ast->sub_packet_size > ast->audio_framesize)
265  return AVERROR_INVALIDDATA;
266  break;
267  case DEINT_ID_SIPR:
268  case DEINT_ID_INT0:
269  case DEINT_ID_VBRS:
270  case DEINT_ID_VBRF:
271  break;
272  default:
273  av_log(NULL, 0 ,"Unknown interleaver %"PRIX32"\n", ast->deint_id);
274  return AVERROR_INVALIDDATA;
275  }
276 
277  if (read_all) {
278  avio_r8(pb);
279  avio_r8(pb);
280  avio_r8(pb);
281  rm_read_metadata(s, pb, 0);
282  }
283  }
284  return 0;
285 }
286 
288  AVStream *st, RMStream *rst,
289  unsigned int codec_data_size)
290 {
291  unsigned int v;
292  int size;
293  int64_t codec_pos;
294  int ret;
295 
296  avpriv_set_pts_info(st, 64, 1, 1000);
297  codec_pos = avio_tell(pb);
298  v = avio_rb32(pb);
299  if (v == MKTAG(0xfd, 'a', 'r', '.')) {
300  /* ra type header */
301  if (rm_read_audio_stream_info(s, pb, st, rst, 0))
302  return -1;
303  } else if (v == MKBETAG('L', 'S', 'D', ':')) {
304  avio_seek(pb, -4, SEEK_CUR);
305  if ((ret = rm_read_extradata(pb, st->codecpar, codec_data_size)) < 0)
306  return ret;
307 
311  st->codecpar->codec_tag);
312  } else {
313  int fps;
314  if (avio_rl32(pb) != MKTAG('V', 'I', 'D', 'O')) {
315  fail1:
316  av_log(s, AV_LOG_WARNING, "Unsupported stream type %08x\n", v);
317  goto skip;
318  }
319  st->codecpar->codec_tag = avio_rl32(pb);
321  st->codecpar->codec_tag);
322  av_log(s, AV_LOG_TRACE, "%X %X\n", st->codecpar->codec_tag, MKTAG('R', 'V', '2', '0'));
323  if (st->codecpar->codec_id == AV_CODEC_ID_NONE)
324  goto fail1;
325  st->codecpar->width = avio_rb16(pb);
326  st->codecpar->height = avio_rb16(pb);
327  avio_skip(pb, 2); // looks like bits per sample
328  avio_skip(pb, 4); // always zero?
331  fps = avio_rb32(pb);
332 
333  if ((ret = rm_read_extradata(pb, st->codecpar, codec_data_size - (avio_tell(pb) - codec_pos))) < 0)
334  return ret;
335 
336  if (fps > 0) {
338  0x10000, fps, (1 << 30) - 1);
339  } else if (s->error_recognition & AV_EF_EXPLODE) {
340  av_log(s, AV_LOG_ERROR, "Invalid framerate\n");
341  return AVERROR_INVALIDDATA;
342  }
343  }
344 
345 skip:
346  /* skip codec info */
347  size = avio_tell(pb) - codec_pos;
348  avio_skip(pb, codec_data_size - size);
349 
350  return 0;
351 }
352 
356 {
357  AVIOContext *pb = s->pb;
358  unsigned int size, n_pkts, str_id, next_off, n, pos, pts;
359  AVStream *st;
360 
361  do {
362  if (avio_rl32(pb) != MKTAG('I','N','D','X'))
363  return -1;
364  size = avio_rb32(pb);
365  if (size < 20)
366  return -1;
367  avio_skip(pb, 2);
368  n_pkts = avio_rb32(pb);
369  str_id = avio_rb16(pb);
370  next_off = avio_rb32(pb);
371  for (n = 0; n < s->nb_streams; n++)
372  if (s->streams[n]->id == str_id) {
373  st = s->streams[n];
374  break;
375  }
376  if (n == s->nb_streams) {
377  av_log(s, AV_LOG_ERROR,
378  "Invalid stream index %d for index at pos %"PRId64"\n",
379  str_id, avio_tell(pb));
380  goto skip;
381  } else if ((avio_size(pb) - avio_tell(pb)) / 14 < n_pkts) {
382  av_log(s, AV_LOG_ERROR,
383  "Nr. of packets in packet index for stream index %d "
384  "exceeds filesize (%"PRId64" at %"PRId64" = %"PRId64")\n",
385  str_id, avio_size(pb), avio_tell(pb),
386  (avio_size(pb) - avio_tell(pb)) / 14);
387  goto skip;
388  }
389 
390  for (n = 0; n < n_pkts; n++) {
391  avio_skip(pb, 2);
392  pts = avio_rb32(pb);
393  pos = avio_rb32(pb);
394  avio_skip(pb, 4); /* packet no. */
395 
396  av_add_index_entry(st, pos, pts, 0, 0, AVINDEX_KEYFRAME);
397  }
398 
399 skip:
400  if (next_off && avio_tell(pb) < next_off &&
401  avio_seek(pb, next_off, SEEK_SET) < 0) {
402  av_log(s, AV_LOG_ERROR,
403  "Non-linear index detected, not supported\n");
404  return -1;
405  }
406  } while (next_off);
407 
408  return 0;
409 }
410 
412 {
413  RMDemuxContext *rm = s->priv_data;
414  AVStream *st;
415 
416  rm->old_format = 1;
417  st = avformat_new_stream(s, NULL);
418  if (!st)
419  return -1;
421  if (!st->priv_data)
422  return AVERROR(ENOMEM);
423  return rm_read_audio_stream_info(s, s->pb, st, st->priv_data, 1);
424 }
425 
427 {
428  RMDemuxContext *rm = s->priv_data;
429  AVStream *st;
430  AVIOContext *pb = s->pb;
431  unsigned int tag;
432  int tag_size;
433  unsigned int start_time, duration;
434  unsigned int data_off = 0, indx_off = 0;
435  char buf[128];
436  int flags = 0;
437 
438  tag = avio_rl32(pb);
439  if (tag == MKTAG('.', 'r', 'a', 0xfd)) {
440  /* very old .ra format */
441  return rm_read_header_old(s);
442  } else if (tag != MKTAG('.', 'R', 'M', 'F')) {
443  return AVERROR(EIO);
444  }
445 
446  tag_size = avio_rb32(pb);
447  avio_skip(pb, tag_size - 8);
448 
449  for(;;) {
450  if (pb->eof_reached)
451  return -1;
452  tag = avio_rl32(pb);
453  tag_size = avio_rb32(pb);
454  avio_rb16(pb);
455  av_log(s, AV_LOG_TRACE, "tag=%c%c%c%c (%08x) size=%d\n",
456  (tag ) & 0xff,
457  (tag >> 8) & 0xff,
458  (tag >> 16) & 0xff,
459  (tag >> 24) & 0xff,
460  tag,
461  tag_size);
462  if (tag_size < 10 && tag != MKTAG('D', 'A', 'T', 'A'))
463  return -1;
464  switch(tag) {
465  case MKTAG('P', 'R', 'O', 'P'):
466  /* file header */
467  avio_rb32(pb); /* max bit rate */
468  avio_rb32(pb); /* avg bit rate */
469  avio_rb32(pb); /* max packet size */
470  avio_rb32(pb); /* avg packet size */
471  avio_rb32(pb); /* nb packets */
472  avio_rb32(pb); /* duration */
473  avio_rb32(pb); /* preroll */
474  indx_off = avio_rb32(pb); /* index offset */
475  data_off = avio_rb32(pb); /* data offset */
476  avio_rb16(pb); /* nb streams */
477  flags = avio_rb16(pb); /* flags */
478  break;
479  case MKTAG('C', 'O', 'N', 'T'):
480  rm_read_metadata(s, pb, 1);
481  break;
482  case MKTAG('M', 'D', 'P', 'R'):
483  st = avformat_new_stream(s, NULL);
484  if (!st)
485  return AVERROR(ENOMEM);
486  st->id = avio_rb16(pb);
487  avio_rb32(pb); /* max bit rate */
488  st->codecpar->bit_rate = avio_rb32(pb); /* bit rate */
489  avio_rb32(pb); /* max packet size */
490  avio_rb32(pb); /* avg packet size */
491  start_time = avio_rb32(pb); /* start time */
492  avio_rb32(pb); /* preroll */
493  duration = avio_rb32(pb); /* duration */
494  st->start_time = start_time;
495  st->duration = duration;
496  get_str8(pb, buf, sizeof(buf)); /* desc */
497  get_str8(pb, buf, sizeof(buf)); /* mimetype */
500  if (!st->priv_data)
501  return AVERROR(ENOMEM);
502  if (ff_rm_read_mdpr_codecdata(s, s->pb, st, st->priv_data,
503  avio_rb32(pb)) < 0)
504  return -1;
505  break;
506  case MKTAG('D', 'A', 'T', 'A'):
507  goto header_end;
508  default:
509  /* unknown tag: skip it */
510  avio_skip(pb, tag_size - 10);
511  break;
512  }
513  }
514  header_end:
515  rm->nb_packets = avio_rb32(pb); /* number of packets */
516  if (!rm->nb_packets && (flags & 4))
517  rm->nb_packets = 3600 * 25;
518  avio_rb32(pb); /* next data header */
519 
520  if (!data_off)
521  data_off = avio_tell(pb) - 18;
522  if (indx_off && pb->seekable && !(s->flags & AVFMT_FLAG_IGNIDX) &&
523  avio_seek(pb, indx_off, SEEK_SET) >= 0) {
524  rm_read_index(s);
525  avio_seek(pb, data_off + 18, SEEK_SET);
526  }
527 
528  return 0;
529 }
530 
531 static int get_num(AVIOContext *pb, int *len)
532 {
533  int n, n1;
534 
535  n = avio_rb16(pb);
536  (*len)-=2;
537  n &= 0x7FFF;
538  if (n >= 0x4000) {
539  return n - 0x4000;
540  } else {
541  n1 = avio_rb16(pb);
542  (*len)-=2;
543  return (n << 16) | n1;
544  }
545 }
546 
547 /* multiple of 20 bytes for ra144 (ugly) */
548 #define RAW_PACKET_SIZE 1000
549 
550 static int sync(AVFormatContext *s, int64_t *timestamp, int *flags, int *stream_index, int64_t *pos){
551  RMDemuxContext *rm = s->priv_data;
552  AVIOContext *pb = s->pb;
553  AVStream *st;
554  uint32_t state=0xFFFFFFFF;
555 
556  while(!pb->eof_reached){
557  int len, num, i;
558  *pos= avio_tell(pb) - 3;
559  if(rm->remaining_len > 0){
560  num= rm->current_stream;
561  len= rm->remaining_len;
562  *timestamp = AV_NOPTS_VALUE;
563  *flags= 0;
564  }else{
565  state= (state<<8) + avio_r8(pb);
566 
567  if(state == MKBETAG('I', 'N', 'D', 'X')){
568  int n_pkts, expected_len;
569  len = avio_rb32(pb);
570  avio_skip(pb, 2);
571  n_pkts = avio_rb32(pb);
572  expected_len = 20 + n_pkts * 14;
573  if (len == 20)
574  /* some files don't add index entries to chunk size... */
575  len = expected_len;
576  else if (len != expected_len)
578  "Index size %d (%d pkts) is wrong, should be %d.\n",
579  len, n_pkts, expected_len);
580  len -= 14; // we already read part of the index header
581  if(len<0)
582  continue;
583  goto skip;
584  } else if (state == MKBETAG('D','A','T','A')) {
586  "DATA tag in middle of chunk, file may be broken.\n");
587  }
588 
589  if(state > (unsigned)0xFFFF || state <= 12)
590  continue;
591  len=state - 12;
592  state= 0xFFFFFFFF;
593 
594  num = avio_rb16(pb);
595  *timestamp = avio_rb32(pb);
596  avio_r8(pb); /* reserved */
597  *flags = avio_r8(pb); /* flags */
598  }
599  for(i=0;i<s->nb_streams;i++) {
600  st = s->streams[i];
601  if (num == st->id)
602  break;
603  }
604  if (i == s->nb_streams) {
605 skip:
606  /* skip packet if unknown number */
607  avio_skip(pb, len);
608  rm->remaining_len = 0;
609  continue;
610  }
611  *stream_index= i;
612 
613  return len;
614  }
615  return -1;
616 }
617 
619  RMDemuxContext *rm, RMStream *vst,
620  AVPacket *pkt, int len, int *pseq,
621  int64_t *timestamp)
622 {
623  int hdr, seq, pic_num, len2, pos;
624  int type;
625 
626  hdr = avio_r8(pb); len--;
627  type = hdr >> 6;
628 
629  if(type != 3){ // not frame as a part of packet
630  seq = avio_r8(pb); len--;
631  }
632  if(type != 1){ // not whole frame
633  len2 = get_num(pb, &len);
634  pos = get_num(pb, &len);
635  pic_num = avio_r8(pb); len--;
636  }
637  if(len<0)
638  return -1;
639  rm->remaining_len = len;
640  if(type&1){ // frame, not slice
641  if(type == 3){ // frame as a part of packet
642  len= len2;
643  *timestamp = pos;
644  }
645  if(rm->remaining_len < len)
646  return -1;
647  rm->remaining_len -= len;
648  if(av_new_packet(pkt, len + 9) < 0)
649  return AVERROR(EIO);
650  pkt->data[0] = 0;
651  AV_WL32(pkt->data + 1, 1);
652  AV_WL32(pkt->data + 5, 0);
653  avio_read(pb, pkt->data + 9, len);
654  return 0;
655  }
656  //now we have to deal with single slice
657 
658  *pseq = seq;
659  if((seq & 0x7F) == 1 || vst->curpic_num != pic_num){
660  vst->slices = ((hdr & 0x3F) << 1) + 1;
661  vst->videobufsize = len2 + 8*vst->slices + 1;
662  av_packet_unref(&vst->pkt); //FIXME this should be output.
663  if(av_new_packet(&vst->pkt, vst->videobufsize) < 0)
664  return AVERROR(ENOMEM);
665  vst->videobufpos = 8*vst->slices + 1;
666  vst->cur_slice = 0;
667  vst->curpic_num = pic_num;
668  vst->pktpos = avio_tell(pb);
669  }
670  if(type == 2)
671  len = FFMIN(len, pos);
672 
673  if(++vst->cur_slice > vst->slices)
674  return 1;
675  AV_WL32(vst->pkt.data - 7 + 8*vst->cur_slice, 1);
676  AV_WL32(vst->pkt.data - 3 + 8*vst->cur_slice, vst->videobufpos - 8*vst->slices - 1);
677  if(vst->videobufpos + len > vst->videobufsize)
678  return 1;
679  if (avio_read(pb, vst->pkt.data + vst->videobufpos, len) != len)
680  return AVERROR(EIO);
681  vst->videobufpos += len;
682  rm->remaining_len-= len;
683 
684  if (type == 2 || vst->videobufpos == vst->videobufsize) {
685  vst->pkt.data[0] = vst->cur_slice-1;
686  *pkt= vst->pkt;
687  vst->pkt.data= NULL;
688  vst->pkt.size= 0;
689  vst->pkt.buf = NULL;
690  if(vst->slices != vst->cur_slice) //FIXME find out how to set slices correct from the begin
691  memmove(pkt->data + 1 + 8*vst->cur_slice, pkt->data + 1 + 8*vst->slices,
692  vst->videobufpos - 1 - 8*vst->slices);
693  pkt->size = vst->videobufpos + 8*(vst->cur_slice - vst->slices);
694  pkt->pts = AV_NOPTS_VALUE;
695  pkt->pos = vst->pktpos;
696  vst->slices = 0;
697  return 0;
698  }
699 
700  return 1;
701 }
702 
703 static inline void
705 {
706  uint8_t *ptr;
707  int j;
708 
709  if (st->codecpar->codec_id == AV_CODEC_ID_AC3) {
710  ptr = pkt->data;
711  for (j=0;j<pkt->size;j+=2) {
712  FFSWAP(int, ptr[0], ptr[1]);
713  ptr += 2;
714  }
715  }
716 }
717 
718 int
720  AVStream *st, RMStream *ast, int len, AVPacket *pkt,
721  int *seq, int flags, int64_t timestamp)
722 {
723  RMDemuxContext *rm = s->priv_data;
724  int ret;
725 
726  if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
727  rm->current_stream= st->id;
728  if(rm_assemble_video_frame(s, pb, rm, ast, pkt, len, seq, &timestamp))
729  return -1; //got partial frame
730  } else if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
731  if ((ast->deint_id == DEINT_ID_GENR) ||
732  (ast->deint_id == DEINT_ID_INT4) ||
733  (ast->deint_id == DEINT_ID_SIPR)) {
734  int x;
735  int sps = ast->sub_packet_size;
736  int cfs = ast->coded_framesize;
737  int h = ast->sub_packet_h;
738  int y = ast->sub_packet_cnt;
739  int w = ast->audio_framesize;
740 
741  if (flags & 2)
742  y = ast->sub_packet_cnt = 0;
743  if (!y)
744  ast->audiotimestamp = timestamp;
745 
746  switch (ast->deint_id) {
747  case DEINT_ID_INT4:
748  for (x = 0; x < h/2; x++)
749  avio_read(pb, ast->pkt.data+x*2*w+y*cfs, cfs);
750  break;
751  case DEINT_ID_GENR:
752  for (x = 0; x < w/sps; x++)
753  avio_read(pb, ast->pkt.data+sps*(h*x+((h+1)/2)*(y&1)+(y>>1)), sps);
754  break;
755  case DEINT_ID_SIPR:
756  avio_read(pb, ast->pkt.data + y * w, w);
757  break;
758  }
759 
760  if (++(ast->sub_packet_cnt) < h)
761  return -1;
762  if (ast->deint_id == DEINT_ID_SIPR)
763  ff_rm_reorder_sipr_data(ast->pkt.data, h, w);
764 
765  ast->sub_packet_cnt = 0;
766  rm->audio_stream_num = st->index;
767  rm->audio_pkt_cnt = h * w / st->codecpar->block_align;
768  } else if ((ast->deint_id == DEINT_ID_VBRF) ||
769  (ast->deint_id == DEINT_ID_VBRS)) {
770  int x;
771  rm->audio_stream_num = st->index;
772  ast->sub_packet_cnt = (avio_rb16(pb) & 0xf0) >> 4;
773  if (ast->sub_packet_cnt) {
774  for (x = 0; x < ast->sub_packet_cnt; x++)
775  ast->sub_packet_lengths[x] = avio_rb16(pb);
776  rm->audio_pkt_cnt = ast->sub_packet_cnt;
777  ast->audiotimestamp = timestamp;
778  } else
779  return -1;
780  } else {
781  ret = av_get_packet(pb, pkt, len);
782  if (ret < 0)
783  return ret;
784  rm_ac3_swap_bytes(st, pkt);
785  }
786  } else {
787  ret = av_get_packet(pb, pkt, len);
788  if (ret < 0)
789  return ret;
790  }
791 
792  pkt->stream_index = st->index;
793 
794  pkt->pts = timestamp;
795  if (flags & 2)
796  pkt->flags |= AV_PKT_FLAG_KEY;
797 
798  return st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO ? rm->audio_pkt_cnt : 0;
799 }
800 
801 int
803  AVStream *st, RMStream *ast, AVPacket *pkt)
804 {
805  RMDemuxContext *rm = s->priv_data;
806  int ret;
807 
808  assert (rm->audio_pkt_cnt > 0);
809 
810  if (ast->deint_id == DEINT_ID_VBRF ||
811  ast->deint_id == DEINT_ID_VBRS) {
812  ret = av_get_packet(pb, pkt, ast->sub_packet_lengths[ast->sub_packet_cnt - rm->audio_pkt_cnt]);
813  if (ret < 0)
814  return ret;
815  }
816  else {
817  ret = av_new_packet(pkt, st->codecpar->block_align);
818  if (ret < 0)
819  return ret;
820  memcpy(pkt->data, ast->pkt.data + st->codecpar->block_align * //FIXME avoid this
822  st->codecpar->block_align);
823  }
824  rm->audio_pkt_cnt--;
825  if ((pkt->pts = ast->audiotimestamp) != AV_NOPTS_VALUE) {
827  pkt->flags = AV_PKT_FLAG_KEY;
828  } else
829  pkt->flags = 0;
830  pkt->stream_index = st->index;
831 
832  return rm->audio_pkt_cnt;
833 }
834 
836 {
837  RMDemuxContext *rm = s->priv_data;
838  AVStream *st;
839  int i, len, res, seq = 1;
840  int64_t timestamp, pos;
841  int flags;
842 
843  for (;;) {
844  if (rm->audio_pkt_cnt) {
845  // If there are queued audio packet return them first
846  st = s->streams[rm->audio_stream_num];
847  ff_rm_retrieve_cache(s, s->pb, st, st->priv_data, pkt);
848  flags = 0;
849  } else {
850  if (rm->old_format) {
851  RMStream *ast;
852 
853  st = s->streams[0];
854  ast = st->priv_data;
855  timestamp = AV_NOPTS_VALUE;
856  len = !ast->audio_framesize ? RAW_PACKET_SIZE :
857  ast->coded_framesize * ast->sub_packet_h / 2;
858  flags = (seq++ == 1) ? 2 : 0;
859  pos = avio_tell(s->pb);
860  } else {
861  len=sync(s, &timestamp, &flags, &i, &pos);
862  if (len > 0)
863  st = s->streams[i];
864  }
865 
866  if (len <= 0 || s->pb->eof_reached)
867  return AVERROR(EIO);
868 
869  res = ff_rm_parse_packet (s, s->pb, st, st->priv_data, len, pkt,
870  &seq, flags, timestamp);
871  if((flags&2) && (seq&0x7F) == 1)
872  av_add_index_entry(st, pos, timestamp, 0, 0, AVINDEX_KEYFRAME);
873  if (res)
874  continue;
875  }
876 
877  if( (st->discard >= AVDISCARD_NONKEY && !(flags&2))
878  || st->discard >= AVDISCARD_ALL){
879  av_packet_unref(pkt);
880  } else
881  break;
882  }
883 
884  return 0;
885 }
886 
888 {
889  int i;
890 
891  for (i=0;i<s->nb_streams;i++)
893 
894  return 0;
895 }
896 
897 static int rm_probe(AVProbeData *p)
898 {
899  /* check file header */
900  if ((p->buf[0] == '.' && p->buf[1] == 'R' &&
901  p->buf[2] == 'M' && p->buf[3] == 'F' &&
902  p->buf[4] == 0 && p->buf[5] == 0) ||
903  (p->buf[0] == '.' && p->buf[1] == 'r' &&
904  p->buf[2] == 'a' && p->buf[3] == 0xfd))
905  return AVPROBE_SCORE_MAX;
906  else
907  return 0;
908 }
909 
910 static int64_t rm_read_dts(AVFormatContext *s, int stream_index,
911  int64_t *ppos, int64_t pos_limit)
912 {
913  RMDemuxContext *rm = s->priv_data;
914  int64_t pos, dts;
915  int stream_index2, flags, len, h;
916 
917  pos = *ppos;
918 
919  if(rm->old_format)
920  return AV_NOPTS_VALUE;
921 
922  avio_seek(s->pb, pos, SEEK_SET);
923  rm->remaining_len=0;
924  for(;;){
925  int seq=1;
926  AVStream *st;
927 
928  len=sync(s, &dts, &flags, &stream_index2, &pos);
929  if(len<0)
930  return AV_NOPTS_VALUE;
931 
932  st = s->streams[stream_index2];
933  if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
934  h= avio_r8(s->pb); len--;
935  if(!(h & 0x40)){
936  seq = avio_r8(s->pb); len--;
937  }
938  }
939 
940  if((flags&2) && (seq&0x7F) == 1){
941  av_log(s, AV_LOG_TRACE, "%d %d-%d %"PRId64" %d\n",
942  flags, stream_index2, stream_index, dts, seq);
943  av_add_index_entry(st, pos, dts, 0, 0, AVINDEX_KEYFRAME);
944  if(stream_index2 == stream_index)
945  break;
946  }
947 
948  avio_skip(s->pb, len);
949  }
950  *ppos = pos;
951  return dts;
952 }
953 
955  .name = "rm",
956  .long_name = NULL_IF_CONFIG_SMALL("RealMedia"),
957  .priv_data_size = sizeof(RMDemuxContext),
958  .read_probe = rm_probe,
962  .read_timestamp = rm_read_dts,
963 };
964 
966  .name = "rdt",
967  .long_name = NULL_IF_CONFIG_SMALL("RDT demuxer"),
968  .priv_data_size = sizeof(RMDemuxContext),
970  .flags = AVFMT_NOFILE,
971 };
static int get_num(AVIOContext *pb, int *len)
Definition: rmdec.c:531
full parsing and interpolation of timestamps for frames not starting on a packet boundary ...
Definition: avformat.h:659
#define RAW_PACKET_SIZE
Definition: rmdec.c:548
int remaining_len
Definition: rmdec.c:61
discard all frames except keyframes
Definition: avcodec.h:688
Bytestream IO Context.
Definition: avio.h:104
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
int64_t avio_size(AVIOContext *s)
Get the filesize.
Definition: aviobuf.c:297
int size
#define DEINT_ID_INT0
no interleaving needed
Definition: rmdec.c:35
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:1214
static void get_strl(AVIOContext *pb, char *buf, int buf_size, int len)
Definition: rmdec.c:66
enum AVCodecID ff_codec_get_id(const AVCodecTag *tags, unsigned int tag)
Definition: utils.c:1983
static int64_t rm_read_dts(AVFormatContext *s, int stream_index, int64_t *ppos, int64_t pos_limit)
Definition: rmdec.c:910
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:130
int64_t pos
byte position in stream, -1 if unknown
Definition: avcodec.h:1366
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:2986
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:3483
int num
numerator
Definition: rational.h:44
int index
stream index in AVFormatContext
Definition: avformat.h:706
int size
Definition: avcodec.h:1347
Definition: rmdec.c:41
av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (%s)\, len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt), use_generic ? ac->func_descr_generic :ac->func_descr)
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:242
int cur_slice
Definition: rmdec.c:46
void * priv_data
Definition: avformat.h:720
#define FF_ARRAY_ELEMS(a)
static int rm_read_header(AVFormatContext *s)
Definition: rmdec.c:426
discard all
Definition: avcodec.h:689
unsigned int avio_rb16(AVIOContext *s)
Definition: aviobuf.c:681
This struct describes the properties of an encoded stream.
Definition: avcodec.h:3475
#define DEINT_ID_VBRS
VBR case for AAC.
Definition: rmdec.c:39
int videobufpos
position for the next slice in the video buffer
Definition: rmdec.c:44
Format I/O context.
Definition: avformat.h:940
int audio_stream_num
Stream number for audio packets.
Definition: rmdec.c:62
#define AVFMT_FLAG_IGNIDX
Ignore index.
Definition: avformat.h:1053
Public dictionary API.
uint8_t
Opaque data information usually continuous.
Definition: avutil.h:196
int width
Video only.
Definition: avcodec.h:3525
int ff_rm_read_mdpr_codecdata(AVFormatContext *s, AVIOContext *pb, AVStream *st, RMStream *rst, unsigned int codec_data_size)
Read the MDPR chunk, which contains stream-specific codec initialization parameters.
Definition: rmdec.c:287
static int rm_read_extradata(AVIOContext *pb, AVCodecParameters *par, unsigned size)
Definition: rmdec.c:85
#define AV_LOG_TRACE
Extremely verbose debugging, useful for libav* development.
Definition: log.h:150
unsigned int avio_rb32(AVIOContext *s)
Definition: aviobuf.c:696
int id
Format-specific stream ID.
Definition: avformat.h:712
enum AVStreamParseType need_parsing
Definition: avformat.h:879
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:2648
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1008
int64_t duration
Definition: movenc.c:63
int flags
Flags modifying the (de)muxer behaviour.
Definition: avformat.h:1051
uint8_t * data
Definition: avcodec.h:1346
static int flags
Definition: log.c:50
uint32_t tag
Definition: movenc.c:854
int curpic_num
picture number of current frame
Definition: rmdec.c:45
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:117
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:355
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:295
#define DEINT_ID_GENR
interleaving for Cooker/ATRAC
Definition: rmdec.c:34
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:618
static void get_str8(AVIOContext *pb, char *buf, int buf_size)
Definition: rmdec.c:80
uint64_t channel_layout
Audio only.
Definition: avcodec.h:3556
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:545
#define r
Definition: input.c:51
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1378
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:84
#define AVINDEX_KEYFRAME
Definition: avformat.h:666
static int64_t start_time
Definition: avplay.c:245
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:124
AVDictionary * metadata
Metadata that applies to the whole file.
Definition: avformat.h:1148
static int rm_read_close(AVFormatContext *s)
Definition: rmdec.c:887
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:665
#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:148
enum AVMediaType codec_type
General type of the encoded data.
Definition: avcodec.h:3479
AVBufferRef * buf
A reference to the reference-counted buffer where the packet data is stored.
Definition: avcodec.h:1329
#define DEINT_ID_VBRF
VBR case for AAC.
Definition: rmdec.c:38
int64_t pktpos
first slice position in file
Definition: rmdec.c:47
AVRational avg_frame_rate
Average framerate.
Definition: avformat.h:781
RMStream * ff_rm_alloc_rmstream(void)
Definition: rmdec.c:109
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1352
int extradata_size
Size of the extradata content in bytes.
Definition: avcodec.h:3501
Only parse headers, do not repack.
Definition: avformat.h:658
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:536
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
int bit_rate
The average bitrate of the encoded data (in bits per second).
Definition: avcodec.h:3512
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:400
common internal API header
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:996
AVPacket pkt
place to store merged video frame / reordered audio data
Definition: rmdec.c:42
int block_align
Audio only.
Definition: avcodec.h:3571
int seekable
A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable.
Definition: avio.h:153
audio channel layout utility functions
#define FFMIN(a, b)
Definition: common.h:66
static int read_probe(AVProbeData *pd)
Definition: jvdec.c:55
AVInputFormat ff_rm_demuxer
Definition: rmdec.c:954
static av_always_inline int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: avio.h:286
int32_t
#define DEINT_ID_INT4
interleaving for 28.8
Definition: rmdec.c:36
#define AV_RL32
Definition: intreadwrite.h:146
#define AV_EF_EXPLODE
Definition: avcodec.h:2681
int sub_packet_cnt
Definition: rmdec.c:50
int coded_framesize
Descrambling parameters from container.
Definition: rmdec.c:51
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:546
int audio_framesize
Definition: rmdec.c:52
Stream structure.
Definition: avformat.h:705
static void rm_ac3_swap_bytes(AVStream *st, AVPacket *pkt)
Definition: rmdec.c:704
const AVCodecTag ff_rm_codec_tags[]
Definition: rm.c:31
NULL
Definition: eval.c:55
version
Definition: ffv1enc.c:1091
#define DEINT_ID_SIPR
interleaving for Sipro
Definition: rmdec.c:37
AVIOContext * pb
I/O context.
Definition: avformat.h:982
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:347
static int rm_probe(AVProbeData *p)
Definition: rmdec.c:897
static int read_packet(AVFormatContext *ctx, AVPacket *pkt)
Definition: libcdio.c:114
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:719
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:68
int current_stream
Definition: rmdec.c:60
int old_format
Definition: rmdec.c:59
This structure contains the data a format has to probe a file.
Definition: avformat.h:398
int audio_pkt_cnt
Output packet counter.
Definition: rmdec.c:63
int nb_packets
Definition: rmdec.c:58
static int sync(AVFormatContext *s, int64_t *timestamp, int *flags, int *stream_index, int64_t *pos)
Definition: rmdec.c:550
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:965
void ff_rm_free_rmstream(RMStream *rms)
Definition: rmdec.c:118
int sub_packet_h
Definition: rmdec.c:51
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:757
int sample_rate
Audio only.
Definition: avcodec.h:3564
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:407
full parsing and repack
Definition: avformat.h:657
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.
static struct @174 state
int32_t deint_id
Length of each subpacket.
Definition: rmdec.c:54
#define AVFMT_NOFILE
Demuxer will use avio_open, no opened file should be provided by the caller.
Definition: avformat.h:412
int64_t start_time
Decoding: pts of the first frame of the stream, in stream time base.
Definition: avformat.h:750
int error_recognition
Error recognition; higher values will detect more errors but may misdetect some more or less valid pa...
Definition: avformat.h:1170
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:802
int den
denominator
Definition: rational.h:45
static int rm_read_header_old(AVFormatContext *s)
Definition: rmdec.c:411
#define MKBETAG(a, b, c, d)
Definition: common.h:257
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:638
int slices
Definition: rmdec.c:46
int eof_reached
true if eof reached
Definition: avio.h:132
int len
static int rm_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: rmdec.c:835
static int rm_read_audio_stream_info(AVFormatContext *s, AVIOContext *pb, AVStream *st, RMStream *ast, int read_all)
Definition: rmdec.c:123
void * priv_data
Format private data.
Definition: avformat.h:968
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition: avcodec.h:3497
int channels
Audio only.
Definition: avcodec.h:3560
#define AV_WL32(p, val)
Definition: intreadwrite.h:263
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:529
int64_t audiotimestamp
Audio descrambling matrix parameters.
Definition: rmdec.c:49
AVCodecParameters * codecpar
Definition: avformat.h:831
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition: avcodec.h:3487
#define FFSWAP(type, a, b)
Definition: common.h:69
int stream_index
Definition: avcodec.h:1348
int videobufsize
current assembled frame size
Definition: rmdec.c:43
#define AV_CH_LAYOUT_MONO
#define MKTAG(a, b, c, d)
Definition: common.h:256
enum AVDiscard discard
Selects which packets can be discarded at will and do not need to be demuxed.
Definition: avformat.h:763
int sub_packet_lengths[16]
Audio frame size from container.
Definition: rmdec.c:53
This structure stores compressed data.
Definition: avcodec.h:1323
static void rm_read_metadata(AVFormatContext *s, AVIOContext *pb, int wide)
Definition: rmdec.c:98
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:211
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1339
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:235
int sub_packet_size
Definition: rmdec.c:51