You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

99 lines
3.1KB

  1. /*
  2. * MSF demuxer
  3. * Copyright (c) 2015 Paul B Mahol
  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. #include "libavutil/intreadwrite.h"
  22. #include "avformat.h"
  23. #include "internal.h"
  24. static int msf_probe(AVProbeData *p)
  25. {
  26. if (memcmp(p->buf, "MSF", 3))
  27. return 0;
  28. if (AV_RB32(p->buf+8) <= 0)
  29. return 0;
  30. if (AV_RB32(p->buf+16) <= 0)
  31. return 0;
  32. if (AV_RB32(p->buf+4) > 16)
  33. return AVPROBE_SCORE_MAX / 5; //unsupported / unknown codec
  34. return AVPROBE_SCORE_MAX / 3 * 2;
  35. }
  36. static int msf_read_header(AVFormatContext *s)
  37. {
  38. unsigned codec, align, size;
  39. AVStream *st;
  40. avio_skip(s->pb, 4);
  41. st = avformat_new_stream(s, NULL);
  42. if (!st)
  43. return AVERROR(ENOMEM);
  44. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  45. codec = avio_rb32(s->pb);
  46. st->codec->channels = avio_rb32(s->pb);
  47. if (st->codec->channels <= 0 || st->codec->channels >= INT_MAX / 1024)
  48. return AVERROR_INVALIDDATA;
  49. size = avio_rb32(s->pb);
  50. st->codec->sample_rate = avio_rb32(s->pb);
  51. if (st->codec->sample_rate <= 0)
  52. return AVERROR_INVALIDDATA;
  53. align = avio_rb32(s->pb) ;
  54. if (align > INT_MAX / st->codec->channels)
  55. return AVERROR_INVALIDDATA;
  56. st->codec->block_align = align;
  57. switch (codec) {
  58. case 0: st->codec->codec_id = AV_CODEC_ID_PCM_S16BE; break;
  59. case 3: st->codec->block_align = 16 * st->codec->channels;
  60. st->codec->codec_id = AV_CODEC_ID_ADPCM_PSX; break;
  61. case 7: st->need_parsing = AVSTREAM_PARSE_FULL_RAW;
  62. st->codec->codec_id = AV_CODEC_ID_MP3; break;
  63. default:
  64. avpriv_request_sample(s, "Codec %d", codec);
  65. return AVERROR_PATCHWELCOME;
  66. }
  67. st->duration = av_get_audio_frame_duration(st->codec, size);
  68. avio_skip(s->pb, 0x40 - avio_tell(s->pb));
  69. avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
  70. return 0;
  71. }
  72. static int msf_read_packet(AVFormatContext *s, AVPacket *pkt)
  73. {
  74. AVCodecContext *codec = s->streams[0]->codec;
  75. return av_get_packet(s->pb, pkt, codec->block_align ? codec->block_align : 1024 * codec->channels);
  76. }
  77. AVInputFormat ff_msf_demuxer = {
  78. .name = "msf",
  79. .long_name = NULL_IF_CONFIG_SMALL("Sony PS3 MSF"),
  80. .read_probe = msf_probe,
  81. .read_header = msf_read_header,
  82. .read_packet = msf_read_packet,
  83. .extensions = "msf",
  84. };