FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
rmdec.c
Go to the documentation of this file.
1 /*
2  * "Real" compatible demuxer.
3  * Copyright (c) 2000, 2001 Fabrice Bellard
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 #include <inttypes.h>
23 
24 #include "libavutil/avassert.h"
25 #include "libavutil/avstring.h"
27 #include "libavutil/internal.h"
28 #include "libavutil/intreadwrite.h"
29 #include "libavutil/dict.h"
30 #include "avformat.h"
31 #include "avio_internal.h"
32 #include "internal.h"
33 #include "rmsipr.h"
34 #include "rm.h"
35 
36 #define DEINT_ID_GENR MKTAG('g', 'e', 'n', 'r') ///< interleaving for Cooker/ATRAC
37 #define DEINT_ID_INT0 MKTAG('I', 'n', 't', '0') ///< no interleaving needed
38 #define DEINT_ID_INT4 MKTAG('I', 'n', 't', '4') ///< interleaving for 28.8
39 #define DEINT_ID_SIPR MKTAG('s', 'i', 'p', 'r') ///< interleaving for Sipro
40 #define DEINT_ID_VBRF MKTAG('v', 'b', 'r', 'f') ///< VBR case for AAC
41 #define DEINT_ID_VBRS MKTAG('v', 'b', 'r', 's') ///< VBR case for AAC
42 
43 struct RMStream {
44  AVPacket pkt; ///< place to store merged video frame / reordered audio data
45  int videobufsize; ///< current assembled frame size
46  int videobufpos; ///< position for the next slice in the video buffer
47  int curpic_num; ///< picture number of current frame
49  int64_t pktpos; ///< first slice position in file
50  /// Audio descrambling matrix parameters
51  int64_t audiotimestamp; ///< Audio packet timestamp
52  int sub_packet_cnt; // Subpacket counter, used while reading
53  int sub_packet_size, sub_packet_h, coded_framesize; ///< Descrambling parameters from container
54  int audio_framesize; /// Audio frame size from container
55  int sub_packet_lengths[16]; /// Length of each subpacket
56  int32_t deint_id; ///< deinterleaver used in audio stream
57 };
58 
59 typedef struct {
64  int audio_stream_num; ///< Stream number for audio packets
65  int audio_pkt_cnt; ///< Output packet counter
67 
68 static inline void get_strl(AVIOContext *pb, char *buf, int buf_size, int len)
69 {
70  int i;
71  char *q, r;
72 
73  q = buf;
74  for(i=0;i<len;i++) {
75  r = avio_r8(pb);
76  if (i < buf_size - 1)
77  *q++ = r;
78  }
79  if (buf_size > 0) *q = '\0';
80 }
81 
82 static void get_str8(AVIOContext *pb, char *buf, int buf_size)
83 {
84  get_strl(pb, buf, buf_size, avio_r8(pb));
85 }
86 
87 static int rm_read_extradata(AVIOContext *pb, AVCodecContext *avctx, unsigned size)
88 {
89  if (size >= 1<<24) {
90  av_log(avctx, AV_LOG_ERROR, "extradata size %u too large\n", size);
91  return -1;
92  }
93  if (ff_get_extradata(avctx, pb, size) < 0)
94  return AVERROR(ENOMEM);
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 
103  for (i=0; i<FF_ARRAY_ELEMS(ff_rm_metadata); i++) {
104  int len = wide ? avio_rb16(pb) : avio_r8(pb);
105  get_strl(pb, buf, sizeof(buf), len);
106  av_dict_set(&s->metadata, ff_rm_metadata[i], buf, 0);
107  }
108 }
109 
111 {
112  RMStream *rms = av_mallocz(sizeof(RMStream));
113  rms->curpic_num = -1;
114  return rms;
115 }
116 
118 {
119  av_free_packet(&rms->pkt);
120 }
121 
123  AVStream *st, RMStream *ast, int read_all)
124 {
125  char buf[256];
126  uint32_t version;
127  int ret;
128 
129  /* ra type header */
130  version = avio_rb16(pb); /* version */
131  if (version == 3) {
132  unsigned bytes_per_minute;
133  int header_size = avio_rb16(pb);
134  int64_t startpos = avio_tell(pb);
135  avio_skip(pb, 8);
136  bytes_per_minute = avio_rb16(pb);
137  avio_skip(pb, 4);
138  rm_read_metadata(s, pb, 0);
139  if ((startpos + header_size) >= avio_tell(pb) + 2) {
140  // fourcc (should always be "lpcJ")
141  avio_r8(pb);
142  get_str8(pb, buf, sizeof(buf));
143  }
144  // Skip extra header crap (this should never happen)
145  if ((startpos + header_size) > avio_tell(pb))
146  avio_skip(pb, header_size + startpos - avio_tell(pb));
147  if (bytes_per_minute)
148  st->codec->bit_rate = 8LL * bytes_per_minute / 60;
149  st->codec->sample_rate = 8000;
150  st->codec->channels = 1;
154  ast->deint_id = DEINT_ID_INT0;
155  } else {
156  int flavor, sub_packet_h, coded_framesize, sub_packet_size;
157  int codecdata_length;
158  unsigned bytes_per_minute;
159  /* old version (4) */
160  avio_skip(pb, 2); /* unused */
161  avio_rb32(pb); /* .ra4 */
162  avio_rb32(pb); /* data size */
163  avio_rb16(pb); /* version2 */
164  avio_rb32(pb); /* header size */
165  flavor= avio_rb16(pb); /* add codec info / flavor */
166  ast->coded_framesize = coded_framesize = avio_rb32(pb); /* coded frame size */
167  avio_rb32(pb); /* ??? */
168  bytes_per_minute = avio_rb32(pb);
169  if (version == 4) {
170  if (bytes_per_minute)
171  st->codec->bit_rate = 8LL * bytes_per_minute / 60;
172  }
173  avio_rb32(pb); /* ??? */
174  ast->sub_packet_h = sub_packet_h = avio_rb16(pb); /* 1 */
175  st->codec->block_align= avio_rb16(pb); /* frame size */
176  ast->sub_packet_size = sub_packet_size = avio_rb16(pb); /* sub packet size */
177  avio_rb16(pb); /* ??? */
178  if (version == 5) {
179  avio_rb16(pb); avio_rb16(pb); avio_rb16(pb);
180  }
181  st->codec->sample_rate = avio_rb16(pb);
182  avio_rb32(pb);
183  st->codec->channels = avio_rb16(pb);
184  if (version == 5) {
185  ast->deint_id = avio_rl32(pb);
186  avio_read(pb, buf, 4);
187  buf[4] = 0;
188  } else {
189  AV_WL32(buf, 0);
190  get_str8(pb, buf, sizeof(buf)); /* desc */
191  ast->deint_id = AV_RL32(buf);
192  get_str8(pb, buf, sizeof(buf)); /* desc */
193  }
195  st->codec->codec_tag = AV_RL32(buf);
197  st->codec->codec_tag);
198 
199  switch (st->codec->codec_id) {
200  case AV_CODEC_ID_AC3:
202  break;
203  case AV_CODEC_ID_RA_288:
204  st->codec->extradata_size= 0;
205  ast->audio_framesize = st->codec->block_align;
206  st->codec->block_align = coded_framesize;
207  break;
208  case AV_CODEC_ID_COOK:
210  case AV_CODEC_ID_ATRAC3:
211  case AV_CODEC_ID_SIPR:
212  if (read_all) {
213  codecdata_length = 0;
214  } else {
215  avio_rb16(pb); avio_r8(pb);
216  if (version == 5)
217  avio_r8(pb);
218  codecdata_length = avio_rb32(pb);
219  if(codecdata_length + FF_INPUT_BUFFER_PADDING_SIZE <= (unsigned)codecdata_length){
220  av_log(s, AV_LOG_ERROR, "codecdata_length too large\n");
221  return -1;
222  }
223  }
224 
225  ast->audio_framesize = st->codec->block_align;
226  if (st->codec->codec_id == AV_CODEC_ID_SIPR) {
227  if (flavor > 3) {
228  av_log(s, AV_LOG_ERROR, "bad SIPR file flavor %d\n",
229  flavor);
230  return -1;
231  }
232  st->codec->block_align = ff_sipr_subpk_size[flavor];
233  } else {
234  if(sub_packet_size <= 0){
235  av_log(s, AV_LOG_ERROR, "sub_packet_size is invalid\n");
236  return -1;
237  }
238  st->codec->block_align = ast->sub_packet_size;
239  }
240  if ((ret = rm_read_extradata(pb, st->codec, codecdata_length)) < 0)
241  return ret;
242 
243  break;
244  case AV_CODEC_ID_AAC:
245  avio_rb16(pb); avio_r8(pb);
246  if (version == 5)
247  avio_r8(pb);
248  codecdata_length = avio_rb32(pb);
249  if(codecdata_length + FF_INPUT_BUFFER_PADDING_SIZE <= (unsigned)codecdata_length){
250  av_log(s, AV_LOG_ERROR, "codecdata_length too large\n");
251  return -1;
252  }
253  if (codecdata_length >= 1) {
254  avio_r8(pb);
255  if ((ret = rm_read_extradata(pb, st->codec, codecdata_length - 1)) < 0)
256  return ret;
257  }
258  break;
259  }
260  switch (ast->deint_id) {
261  case DEINT_ID_INT4:
262  if (ast->coded_framesize > ast->audio_framesize ||
263  sub_packet_h <= 1 ||
264  ast->coded_framesize * sub_packet_h > (2 + (sub_packet_h & 1)) * ast->audio_framesize)
265  return AVERROR_INVALIDDATA;
266  if (ast->coded_framesize * sub_packet_h != 2*ast->audio_framesize) {
267  avpriv_request_sample(s, "mismatching interleaver parameters");
268  return AVERROR_INVALIDDATA;
269  }
270  break;
271  case DEINT_ID_GENR:
272  if (ast->sub_packet_size <= 0 ||
273  ast->sub_packet_size > ast->audio_framesize)
274  return AVERROR_INVALIDDATA;
275  if (ast->audio_framesize % ast->sub_packet_size)
276  return AVERROR_INVALIDDATA;
277  break;
278  case DEINT_ID_SIPR:
279  case DEINT_ID_INT0:
280  case DEINT_ID_VBRS:
281  case DEINT_ID_VBRF:
282  break;
283  default:
284  av_log(s, AV_LOG_ERROR ,"Unknown interleaver %"PRIX32"\n", ast->deint_id);
285  return AVERROR_INVALIDDATA;
286  }
287  if (ast->deint_id == DEINT_ID_INT4 ||
288  ast->deint_id == DEINT_ID_GENR ||
289  ast->deint_id == DEINT_ID_SIPR) {
290  if (st->codec->block_align <= 0 ||
291  ast->audio_framesize * sub_packet_h > (unsigned)INT_MAX ||
292  ast->audio_framesize * sub_packet_h < st->codec->block_align)
293  return AVERROR_INVALIDDATA;
294  if (av_new_packet(&ast->pkt, ast->audio_framesize * sub_packet_h) < 0)
295  return AVERROR(ENOMEM);
296  }
297 
298  if (read_all) {
299  avio_r8(pb);
300  avio_r8(pb);
301  avio_r8(pb);
302  rm_read_metadata(s, pb, 0);
303  }
304  }
305  return 0;
306 }
307 
309  AVStream *st, RMStream *rst,
310  unsigned int codec_data_size, const uint8_t *mime)
311 {
312  unsigned int v;
313  int size;
314  int64_t codec_pos;
315  int ret;
316 
317  if (codec_data_size > INT_MAX)
318  return AVERROR_INVALIDDATA;
319 
320  avpriv_set_pts_info(st, 64, 1, 1000);
321  codec_pos = avio_tell(pb);
322  v = avio_rb32(pb);
323 
324  if (v == MKBETAG('M', 'L', 'T', 'I')) {
325  int number_of_streams = avio_rb16(pb);
326  int number_of_mdpr;
327  int i;
328  for (i = 0; i<number_of_streams; i++)
329  avio_rb16(pb);
330  number_of_mdpr = avio_rb16(pb);
331  if (number_of_mdpr != 1) {
332  avpriv_request_sample(s, "MLTI with multiple MDPR");
333  }
334  avio_rb32(pb);
335  v = avio_rb32(pb);
336  }
337 
338  if (v == MKTAG(0xfd, 'a', 'r', '.')) {
339  /* ra type header */
340  if (rm_read_audio_stream_info(s, pb, st, rst, 0))
341  return -1;
342  } else if (v == MKBETAG('L', 'S', 'D', ':')) {
343  avio_seek(pb, -4, SEEK_CUR);
344  if ((ret = rm_read_extradata(pb, st->codec, codec_data_size)) < 0)
345  return ret;
346 
348  st->codec->codec_tag = AV_RL32(st->codec->extradata);
350  st->codec->codec_tag);
351  } else if(mime && !strcmp(mime, "logical-fileinfo")){
352  int stream_count, rule_count, property_count, i;
353  ff_free_stream(s, st);
354  if (avio_rb16(pb) != 0) {
355  av_log(s, AV_LOG_WARNING, "Unsupported version\n");
356  goto skip;
357  }
358  stream_count = avio_rb16(pb);
359  avio_skip(pb, 6*stream_count);
360  rule_count = avio_rb16(pb);
361  avio_skip(pb, 2*rule_count);
362  property_count = avio_rb16(pb);
363  for(i=0; i<property_count; i++){
364  uint8_t name[128], val[128];
365  avio_rb32(pb);
366  if (avio_rb16(pb) != 0) {
367  av_log(s, AV_LOG_WARNING, "Unsupported Name value property version\n");
368  goto skip; //FIXME skip just this one
369  }
370  get_str8(pb, name, sizeof(name));
371  switch(avio_rb32(pb)) {
372  case 2: get_strl(pb, val, sizeof(val), avio_rb16(pb));
373  av_dict_set(&s->metadata, name, val, 0);
374  break;
375  default: avio_skip(pb, avio_rb16(pb));
376  }
377  }
378  } else {
379  int fps;
380  if (avio_rl32(pb) != MKTAG('V', 'I', 'D', 'O')) {
381  fail1:
382  av_log(s, AV_LOG_WARNING, "Unsupported stream type %08x\n", v);
383  goto skip;
384  }
385  st->codec->codec_tag = avio_rl32(pb);
387  st->codec->codec_tag);
388  av_dlog(s, "%X %X\n", st->codec->codec_tag, MKTAG('R', 'V', '2', '0'));
389  if (st->codec->codec_id == AV_CODEC_ID_NONE)
390  goto fail1;
391  st->codec->width = avio_rb16(pb);
392  st->codec->height = avio_rb16(pb);
393  avio_skip(pb, 2); // looks like bits per sample
394  avio_skip(pb, 4); // always zero?
397  fps = avio_rb32(pb);
398 
399  if ((ret = rm_read_extradata(pb, st->codec, codec_data_size - (avio_tell(pb) - codec_pos))) < 0)
400  return ret;
401 
402  if (fps > 0) {
404  0x10000, fps, (1 << 30) - 1);
405 #if FF_API_R_FRAME_RATE
406  st->r_frame_rate = st->avg_frame_rate;
407 #endif
408  } else if (s->error_recognition & AV_EF_EXPLODE) {
409  av_log(s, AV_LOG_ERROR, "Invalid framerate\n");
410  return AVERROR_INVALIDDATA;
411  }
412  }
413 
414 skip:
415  /* skip codec info */
416  size = avio_tell(pb) - codec_pos;
417  if (codec_data_size >= size) {
418  avio_skip(pb, codec_data_size - size);
419  } else {
420  av_log(s, AV_LOG_WARNING, "codec_data_size %u < size %d\n", codec_data_size, size);
421  }
422 
423  return 0;
424 }
425 
426 /** this function assumes that the demuxer has already seeked to the start
427  * of the INDX chunk, and will bail out if not. */
429 {
430  AVIOContext *pb = s->pb;
431  unsigned int size, n_pkts, str_id, next_off, n, pos, pts;
432  AVStream *st;
433 
434  do {
435  if (avio_rl32(pb) != MKTAG('I','N','D','X'))
436  return -1;
437  size = avio_rb32(pb);
438  if (size < 20)
439  return -1;
440  avio_skip(pb, 2);
441  n_pkts = avio_rb32(pb);
442  str_id = avio_rb16(pb);
443  next_off = avio_rb32(pb);
444  for (n = 0; n < s->nb_streams; n++)
445  if (s->streams[n]->id == str_id) {
446  st = s->streams[n];
447  break;
448  }
449  if (n == s->nb_streams) {
450  av_log(s, AV_LOG_ERROR,
451  "Invalid stream index %d for index at pos %"PRId64"\n",
452  str_id, avio_tell(pb));
453  goto skip;
454  } else if ((avio_size(pb) - avio_tell(pb)) / 14 < n_pkts) {
455  av_log(s, AV_LOG_ERROR,
456  "Nr. of packets in packet index for stream index %d "
457  "exceeds filesize (%"PRId64" at %"PRId64" = %"PRId64")\n",
458  str_id, avio_size(pb), avio_tell(pb),
459  (avio_size(pb) - avio_tell(pb)) / 14);
460  goto skip;
461  }
462 
463  for (n = 0; n < n_pkts; n++) {
464  avio_skip(pb, 2);
465  pts = avio_rb32(pb);
466  pos = avio_rb32(pb);
467  avio_skip(pb, 4); /* packet no. */
468 
469  av_add_index_entry(st, pos, pts, 0, 0, AVINDEX_KEYFRAME);
470  }
471 
472 skip:
473  if (next_off && avio_tell(pb) < next_off &&
474  avio_seek(pb, next_off, SEEK_SET) < 0) {
475  av_log(s, AV_LOG_ERROR,
476  "Non-linear index detected, not supported\n");
477  return -1;
478  }
479  } while (next_off);
480 
481  return 0;
482 }
483 
485 {
486  RMDemuxContext *rm = s->priv_data;
487  AVStream *st;
488 
489  rm->old_format = 1;
490  st = avformat_new_stream(s, NULL);
491  if (!st)
492  return -1;
494  return rm_read_audio_stream_info(s, s->pb, st, st->priv_data, 1);
495 }
496 
498 {
499  RMDemuxContext *rm = s->priv_data;
500  AVStream *st;
501  AVIOContext *pb = s->pb;
502  unsigned int tag;
503  int tag_size;
504  unsigned int start_time, duration;
505  unsigned int data_off = 0, indx_off = 0;
506  char buf[128], mime[128];
507  int flags = 0;
508 
509  tag = avio_rl32(pb);
510  if (tag == MKTAG('.', 'r', 'a', 0xfd)) {
511  /* very old .ra format */
512  return rm_read_header_old(s);
513  } else if (tag != MKTAG('.', 'R', 'M', 'F')) {
514  return AVERROR(EIO);
515  }
516 
517  tag_size = avio_rb32(pb);
518  avio_skip(pb, tag_size - 8);
519 
520  for(;;) {
521  if (avio_feof(pb))
522  return -1;
523  tag = avio_rl32(pb);
524  tag_size = avio_rb32(pb);
525  avio_rb16(pb);
526  av_dlog(s, "tag=%c%c%c%c (%08x) size=%d\n",
527  (tag ) & 0xff,
528  (tag >> 8) & 0xff,
529  (tag >> 16) & 0xff,
530  (tag >> 24) & 0xff,
531  tag,
532  tag_size);
533  if (tag_size < 10 && tag != MKTAG('D', 'A', 'T', 'A'))
534  return -1;
535  switch(tag) {
536  case MKTAG('P', 'R', 'O', 'P'):
537  /* file header */
538  avio_rb32(pb); /* max bit rate */
539  avio_rb32(pb); /* avg bit rate */
540  avio_rb32(pb); /* max packet size */
541  avio_rb32(pb); /* avg packet size */
542  avio_rb32(pb); /* nb packets */
543  duration = avio_rb32(pb); /* duration */
544  s->duration = av_rescale(duration, AV_TIME_BASE, 1000);
545  avio_rb32(pb); /* preroll */
546  indx_off = avio_rb32(pb); /* index offset */
547  data_off = avio_rb32(pb); /* data offset */
548  avio_rb16(pb); /* nb streams */
549  flags = avio_rb16(pb); /* flags */
550  break;
551  case MKTAG('C', 'O', 'N', 'T'):
552  rm_read_metadata(s, pb, 1);
553  break;
554  case MKTAG('M', 'D', 'P', 'R'):
555  st = avformat_new_stream(s, NULL);
556  if (!st)
557  return AVERROR(ENOMEM);
558  st->id = avio_rb16(pb);
559  avio_rb32(pb); /* max bit rate */
560  st->codec->bit_rate = avio_rb32(pb); /* bit rate */
561  avio_rb32(pb); /* max packet size */
562  avio_rb32(pb); /* avg packet size */
563  start_time = avio_rb32(pb); /* start time */
564  avio_rb32(pb); /* preroll */
565  duration = avio_rb32(pb); /* duration */
566  st->start_time = start_time;
567  st->duration = duration;
568  if(duration>0)
570  get_str8(pb, buf, sizeof(buf)); /* desc */
571  get_str8(pb, mime, sizeof(mime)); /* mimetype */
574  if (ff_rm_read_mdpr_codecdata(s, s->pb, st, st->priv_data,
575  avio_rb32(pb), mime) < 0)
576  return -1;
577  break;
578  case MKTAG('D', 'A', 'T', 'A'):
579  goto header_end;
580  default:
581  /* unknown tag: skip it */
582  avio_skip(pb, tag_size - 10);
583  break;
584  }
585  }
586  header_end:
587  rm->nb_packets = avio_rb32(pb); /* number of packets */
588  if (!rm->nb_packets && (flags & 4))
589  rm->nb_packets = 3600 * 25;
590  avio_rb32(pb); /* next data header */
591 
592  if (!data_off)
593  data_off = avio_tell(pb) - 18;
594  if (indx_off && pb->seekable && !(s->flags & AVFMT_FLAG_IGNIDX) &&
595  avio_seek(pb, indx_off, SEEK_SET) >= 0) {
596  rm_read_index(s);
597  avio_seek(pb, data_off + 18, SEEK_SET);
598  }
599 
600  return 0;
601 }
602 
603 static int get_num(AVIOContext *pb, int *len)
604 {
605  int n, n1;
606 
607  n = avio_rb16(pb);
608  (*len)-=2;
609  n &= 0x7FFF;
610  if (n >= 0x4000) {
611  return n - 0x4000;
612  } else {
613  n1 = avio_rb16(pb);
614  (*len)-=2;
615  return (n << 16) | n1;
616  }
617 }
618 
619 /* multiple of 20 bytes for ra144 (ugly) */
620 #define RAW_PACKET_SIZE 1000
621 
622 static int rm_sync(AVFormatContext *s, int64_t *timestamp, int *flags, int *stream_index, int64_t *pos){
623  RMDemuxContext *rm = s->priv_data;
624  AVIOContext *pb = s->pb;
625  AVStream *st;
626  uint32_t state=0xFFFFFFFF;
627 
628  while(!avio_feof(pb)){
629  int len, num, i;
630  *pos= avio_tell(pb) - 3;
631  if(rm->remaining_len > 0){
632  num= rm->current_stream;
633  len= rm->remaining_len;
634  *timestamp = AV_NOPTS_VALUE;
635  *flags= 0;
636  }else{
637  state= (state<<8) + avio_r8(pb);
638 
639  if(state == MKBETAG('I', 'N', 'D', 'X')){
640  int n_pkts, expected_len;
641  len = avio_rb32(pb);
642  avio_skip(pb, 2);
643  n_pkts = avio_rb32(pb);
644  expected_len = 20 + n_pkts * 14;
645  if (len == 20)
646  /* some files don't add index entries to chunk size... */
647  len = expected_len;
648  else if (len != expected_len)
650  "Index size %d (%d pkts) is wrong, should be %d.\n",
651  len, n_pkts, expected_len);
652  len -= 14; // we already read part of the index header
653  if(len<0)
654  continue;
655  goto skip;
656  } else if (state == MKBETAG('D','A','T','A')) {
658  "DATA tag in middle of chunk, file may be broken.\n");
659  }
660 
661  if(state > (unsigned)0xFFFF || state <= 12)
662  continue;
663  len=state - 12;
664  state= 0xFFFFFFFF;
665 
666  num = avio_rb16(pb);
667  *timestamp = avio_rb32(pb);
668  avio_r8(pb); /* reserved */
669  *flags = avio_r8(pb); /* flags */
670  }
671  for(i=0;i<s->nb_streams;i++) {
672  st = s->streams[i];
673  if (num == st->id)
674  break;
675  }
676  if (i == s->nb_streams) {
677 skip:
678  /* skip packet if unknown number */
679  avio_skip(pb, len);
680  rm->remaining_len = 0;
681  continue;
682  }
683  *stream_index= i;
684 
685  return len;
686  }
687  return -1;
688 }
689 
691  RMDemuxContext *rm, RMStream *vst,
692  AVPacket *pkt, int len, int *pseq,
693  int64_t *timestamp)
694 {
695  int hdr;
696  int seq = 0, pic_num = 0, len2 = 0, pos = 0; //init to silcense compiler warning
697  int type;
698  int ret;
699 
700  hdr = avio_r8(pb); len--;
701  type = hdr >> 6;
702 
703  if(type != 3){ // not frame as a part of packet
704  seq = avio_r8(pb); len--;
705  }
706  if(type != 1){ // not whole frame
707  len2 = get_num(pb, &len);
708  pos = get_num(pb, &len);
709  pic_num = avio_r8(pb); len--;
710  }
711  if(len<0) {
712  av_log(s, AV_LOG_ERROR, "Insufficient data\n");
713  return -1;
714  }
715  rm->remaining_len = len;
716  if(type&1){ // frame, not slice
717  if(type == 3){ // frame as a part of packet
718  len= len2;
719  *timestamp = pos;
720  }
721  if(rm->remaining_len < len) {
722  av_log(s, AV_LOG_ERROR, "Insufficient remaining len\n");
723  return -1;
724  }
725  rm->remaining_len -= len;
726  if(av_new_packet(pkt, len + 9) < 0)
727  return AVERROR(EIO);
728  pkt->data[0] = 0;
729  AV_WL32(pkt->data + 1, 1);
730  AV_WL32(pkt->data + 5, 0);
731  if ((ret = avio_read(pb, pkt->data + 9, len)) != len) {
732  av_free_packet(pkt);
733  av_log(s, AV_LOG_ERROR, "Failed to read %d bytes\n", len);
734  return ret < 0 ? ret : AVERROR(EIO);
735  }
736  return 0;
737  }
738  //now we have to deal with single slice
739 
740  *pseq = seq;
741  if((seq & 0x7F) == 1 || vst->curpic_num != pic_num){
742  if (len2 > ffio_limit(pb, len2)) {
743  av_log(s, AV_LOG_ERROR, "Impossibly sized packet\n");
744  return AVERROR_INVALIDDATA;
745  }
746  vst->slices = ((hdr & 0x3F) << 1) + 1;
747  vst->videobufsize = len2 + 8*vst->slices + 1;
748  av_free_packet(&vst->pkt); //FIXME this should be output.
749  if(av_new_packet(&vst->pkt, vst->videobufsize) < 0)
750  return AVERROR(ENOMEM);
751  memset(vst->pkt.data, 0, vst->pkt.size);
752  vst->videobufpos = 8*vst->slices + 1;
753  vst->cur_slice = 0;
754  vst->curpic_num = pic_num;
755  vst->pktpos = avio_tell(pb);
756  }
757  if(type == 2)
758  len = FFMIN(len, pos);
759 
760  if(++vst->cur_slice > vst->slices) {
761  av_log(s, AV_LOG_ERROR, "cur slice %d, too large\n", vst->cur_slice);
762  return 1;
763  }
764  if(!vst->pkt.data)
765  return AVERROR(ENOMEM);
766  AV_WL32(vst->pkt.data - 7 + 8*vst->cur_slice, 1);
767  AV_WL32(vst->pkt.data - 3 + 8*vst->cur_slice, vst->videobufpos - 8*vst->slices - 1);
768  if(vst->videobufpos + len > vst->videobufsize) {
769  av_log(s, AV_LOG_ERROR, "outside videobufsize\n");
770  return 1;
771  }
772  if (avio_read(pb, vst->pkt.data + vst->videobufpos, len) != len)
773  return AVERROR(EIO);
774  vst->videobufpos += len;
775  rm->remaining_len-= len;
776 
777  if (type == 2 || vst->videobufpos == vst->videobufsize) {
778  vst->pkt.data[0] = vst->cur_slice-1;
779  *pkt= vst->pkt;
780  vst->pkt.data= NULL;
781  vst->pkt.size= 0;
782  vst->pkt.buf = NULL;
783 #if FF_API_DESTRUCT_PACKET
785  vst->pkt.destruct = NULL;
787 #endif
788  if(vst->slices != vst->cur_slice) //FIXME find out how to set slices correct from the begin
789  memmove(pkt->data + 1 + 8*vst->cur_slice, pkt->data + 1 + 8*vst->slices,
790  vst->videobufpos - 1 - 8*vst->slices);
791  pkt->size = vst->videobufpos + 8*(vst->cur_slice - vst->slices);
792  pkt->pts = AV_NOPTS_VALUE;
793  pkt->pos = vst->pktpos;
794  vst->slices = 0;
795  return 0;
796  }
797 
798  return 1;
799 }
800 
801 static inline void
803 {
804  uint8_t *ptr;
805  int j;
806 
807  if (st->codec->codec_id == AV_CODEC_ID_AC3) {
808  ptr = pkt->data;
809  for (j=0;j<pkt->size;j+=2) {
810  FFSWAP(int, ptr[0], ptr[1]);
811  ptr += 2;
812  }
813  }
814 }
815 
816 static int readfull(AVFormatContext *s, AVIOContext *pb, uint8_t *dst, int n) {
817  int ret = avio_read(pb, dst, n);
818  if (ret != n) {
819  if (ret >= 0) memset(dst + ret, 0, n - ret);
820  else memset(dst , 0, n);
821  av_log(s, AV_LOG_ERROR, "Failed to fully read block\n");
822  }
823  return ret;
824 }
825 
826 int
828  AVStream *st, RMStream *ast, int len, AVPacket *pkt,
829  int *seq, int flags, int64_t timestamp)
830 {
831  RMDemuxContext *rm = s->priv_data;
832  int ret;
833 
834  if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
835  rm->current_stream= st->id;
836  ret = rm_assemble_video_frame(s, pb, rm, ast, pkt, len, seq, &timestamp);
837  if(ret)
838  return ret < 0 ? ret : -1; //got partial frame or error
839  } else if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
840  if ((ast->deint_id == DEINT_ID_GENR) ||
841  (ast->deint_id == DEINT_ID_INT4) ||
842  (ast->deint_id == DEINT_ID_SIPR)) {
843  int x;
844  int sps = ast->sub_packet_size;
845  int cfs = ast->coded_framesize;
846  int h = ast->sub_packet_h;
847  int y = ast->sub_packet_cnt;
848  int w = ast->audio_framesize;
849 
850  if (flags & 2)
851  y = ast->sub_packet_cnt = 0;
852  if (!y)
853  ast->audiotimestamp = timestamp;
854 
855  switch (ast->deint_id) {
856  case DEINT_ID_INT4:
857  for (x = 0; x < h/2; x++)
858  readfull(s, pb, ast->pkt.data+x*2*w+y*cfs, cfs);
859  break;
860  case DEINT_ID_GENR:
861  for (x = 0; x < w/sps; x++)
862  readfull(s, pb, ast->pkt.data+sps*(h*x+((h+1)/2)*(y&1)+(y>>1)), sps);
863  break;
864  case DEINT_ID_SIPR:
865  readfull(s, pb, ast->pkt.data + y * w, w);
866  break;
867  }
868 
869  if (++(ast->sub_packet_cnt) < h)
870  return -1;
871  if (ast->deint_id == DEINT_ID_SIPR)
872  ff_rm_reorder_sipr_data(ast->pkt.data, h, w);
873 
874  ast->sub_packet_cnt = 0;
875  rm->audio_stream_num = st->index;
876  rm->audio_pkt_cnt = h * w / st->codec->block_align;
877  } else if ((ast->deint_id == DEINT_ID_VBRF) ||
878  (ast->deint_id == DEINT_ID_VBRS)) {
879  int x;
880  rm->audio_stream_num = st->index;
881  ast->sub_packet_cnt = (avio_rb16(pb) & 0xf0) >> 4;
882  if (ast->sub_packet_cnt) {
883  for (x = 0; x < ast->sub_packet_cnt; x++)
884  ast->sub_packet_lengths[x] = avio_rb16(pb);
885  rm->audio_pkt_cnt = ast->sub_packet_cnt;
886  ast->audiotimestamp = timestamp;
887  } else
888  return -1;
889  } else {
890  av_get_packet(pb, pkt, len);
891  rm_ac3_swap_bytes(st, pkt);
892  }
893  } else
894  av_get_packet(pb, pkt, len);
895 
896  pkt->stream_index = st->index;
897 
898 #if 0
899  if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
900  if(st->codec->codec_id == AV_CODEC_ID_RV20){
901  int seq= 128*(pkt->data[2]&0x7F) + (pkt->data[3]>>1);
902  av_log(s, AV_LOG_DEBUG, "%d %"PRId64" %d\n", *timestamp, *timestamp*512LL/25, seq);
903 
904  seq |= (timestamp&~0x3FFF);
905  if(seq - timestamp > 0x2000) seq -= 0x4000;
906  if(seq - timestamp < -0x2000) seq += 0x4000;
907  }
908  }
909 #endif
910 
911  pkt->pts = timestamp;
912  if (flags & 2)
913  pkt->flags |= AV_PKT_FLAG_KEY;
914 
915  return st->codec->codec_type == AVMEDIA_TYPE_AUDIO ? rm->audio_pkt_cnt : 0;
916 }
917 
918 int
920  AVStream *st, RMStream *ast, AVPacket *pkt)
921 {
922  RMDemuxContext *rm = s->priv_data;
923 
924  av_assert0 (rm->audio_pkt_cnt > 0);
925 
926  if (ast->deint_id == DEINT_ID_VBRF ||
927  ast->deint_id == DEINT_ID_VBRS)
929  else {
930  int ret = av_new_packet(pkt, st->codec->block_align);
931  if (ret < 0)
932  return ret;
933  memcpy(pkt->data, ast->pkt.data + st->codec->block_align * //FIXME avoid this
934  (ast->sub_packet_h * ast->audio_framesize / st->codec->block_align - rm->audio_pkt_cnt),
935  st->codec->block_align);
936  }
937  rm->audio_pkt_cnt--;
938  if ((pkt->pts = ast->audiotimestamp) != AV_NOPTS_VALUE) {
940  pkt->flags = AV_PKT_FLAG_KEY;
941  } else
942  pkt->flags = 0;
943  pkt->stream_index = st->index;
944 
945  return rm->audio_pkt_cnt;
946 }
947 
949 {
950  RMDemuxContext *rm = s->priv_data;
951  AVStream *st = NULL; // init to silence compiler warning
952  int i, len, res, seq = 1;
953  int64_t timestamp, pos;
954  int flags;
955 
956  for (;;) {
957  if (rm->audio_pkt_cnt) {
958  // If there are queued audio packet return them first
959  st = s->streams[rm->audio_stream_num];
960  res = ff_rm_retrieve_cache(s, s->pb, st, st->priv_data, pkt);
961  if(res < 0)
962  return res;
963  flags = 0;
964  } else {
965  if (rm->old_format) {
966  RMStream *ast;
967 
968  st = s->streams[0];
969  ast = st->priv_data;
970  timestamp = AV_NOPTS_VALUE;
971  len = !ast->audio_framesize ? RAW_PACKET_SIZE :
972  ast->coded_framesize * ast->sub_packet_h / 2;
973  flags = (seq++ == 1) ? 2 : 0;
974  pos = avio_tell(s->pb);
975  } else {
976  len = rm_sync(s, &timestamp, &flags, &i, &pos);
977  if (len > 0)
978  st = s->streams[i];
979  }
980 
981  if (len <= 0 || avio_feof(s->pb))
982  return AVERROR(EIO);
983 
984  res = ff_rm_parse_packet (s, s->pb, st, st->priv_data, len, pkt,
985  &seq, flags, timestamp);
986  if (res < -1)
987  return res;
988  if((flags&2) && (seq&0x7F) == 1)
989  av_add_index_entry(st, pos, timestamp, 0, 0, AVINDEX_KEYFRAME);
990  if (res)
991  continue;
992  }
993 
994  if( (st->discard >= AVDISCARD_NONKEY && !(flags&2))
995  || st->discard >= AVDISCARD_ALL){
996  av_free_packet(pkt);
997  } else
998  break;
999  }
1000 
1001  return 0;
1002 }
1003 
1005 {
1006  int i;
1007 
1008  for (i=0;i<s->nb_streams;i++)
1010 
1011  return 0;
1012 }
1013 
1014 static int rm_probe(AVProbeData *p)
1015 {
1016  /* check file header */
1017  if ((p->buf[0] == '.' && p->buf[1] == 'R' &&
1018  p->buf[2] == 'M' && p->buf[3] == 'F' &&
1019  p->buf[4] == 0 && p->buf[5] == 0) ||
1020  (p->buf[0] == '.' && p->buf[1] == 'r' &&
1021  p->buf[2] == 'a' && p->buf[3] == 0xfd))
1022  return AVPROBE_SCORE_MAX;
1023  else
1024  return 0;
1025 }
1026 
1027 static int64_t rm_read_dts(AVFormatContext *s, int stream_index,
1028  int64_t *ppos, int64_t pos_limit)
1029 {
1030  RMDemuxContext *rm = s->priv_data;
1031  int64_t pos, dts;
1032  int stream_index2, flags, len, h;
1033 
1034  pos = *ppos;
1035 
1036  if(rm->old_format)
1037  return AV_NOPTS_VALUE;
1038 
1039  if (avio_seek(s->pb, pos, SEEK_SET) < 0)
1040  return AV_NOPTS_VALUE;
1041 
1042  rm->remaining_len=0;
1043  for(;;){
1044  int seq=1;
1045  AVStream *st;
1046 
1047  len = rm_sync(s, &dts, &flags, &stream_index2, &pos);
1048  if(len<0)
1049  return AV_NOPTS_VALUE;
1050 
1051  st = s->streams[stream_index2];
1052  if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
1053  h= avio_r8(s->pb); len--;
1054  if(!(h & 0x40)){
1055  seq = avio_r8(s->pb); len--;
1056  }
1057  }
1058 
1059  if((flags&2) && (seq&0x7F) == 1){
1060  av_dlog(s, "%d %d-%d %"PRId64" %d\n",
1061  flags, stream_index2, stream_index, dts, seq);
1062  av_add_index_entry(st, pos, dts, 0, 0, AVINDEX_KEYFRAME);
1063  if(stream_index2 == stream_index)
1064  break;
1065  }
1066 
1067  avio_skip(s->pb, len);
1068  }
1069  *ppos = pos;
1070  return dts;
1071 }
1072 
1073 static int rm_read_seek(AVFormatContext *s, int stream_index,
1074  int64_t pts, int flags)
1075 {
1076  RMDemuxContext *rm = s->priv_data;
1077 
1078  if (ff_seek_frame_binary(s, stream_index, pts, flags) < 0)
1079  return -1;
1080  rm->audio_pkt_cnt = 0;
1081  return 0;
1082 }
1083 
1084 
1086  .name = "rm",
1087  .long_name = NULL_IF_CONFIG_SMALL("RealMedia"),
1088  .priv_data_size = sizeof(RMDemuxContext),
1089  .read_probe = rm_probe,
1093  .read_timestamp = rm_read_dts,
1095 };
1096 
1098  .name = "rdt",
1099  .long_name = NULL_IF_CONFIG_SMALL("RDT demuxer"),
1100  .priv_data_size = sizeof(RMDemuxContext),
1102  .flags = AVFMT_NOFILE,
1103 };