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