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.

96 lines
3.0KB

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