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.

109 lines
3.3KB

  1. /*
  2. * NSP demuxer
  3. * Copyright (c) 2017 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/avstring.h"
  22. #include "libavutil/intreadwrite.h"
  23. #include "avformat.h"
  24. #include "internal.h"
  25. #include "pcm.h"
  26. static int nsp_probe(AVProbeData *p)
  27. {
  28. if (AV_RB32(p->buf) == AV_RB32("FORM") &&
  29. AV_RB32(p->buf + 4) == AV_RB32("DS16"))
  30. return AVPROBE_SCORE_MAX;
  31. return 0;
  32. }
  33. static int nsp_read_header(AVFormatContext *s)
  34. {
  35. int channels = 0, rate = 0;
  36. uint32_t chunk, size;
  37. AVStream *st;
  38. int64_t pos;
  39. avio_skip(s->pb, 12);
  40. st = avformat_new_stream(s, NULL);
  41. if (!st)
  42. return AVERROR(ENOMEM);
  43. while (!avio_feof(s->pb)) {
  44. char value[1024];
  45. chunk = avio_rb32(s->pb);
  46. size = avio_rl32(s->pb);
  47. pos = avio_tell(s->pb);
  48. switch (chunk) {
  49. case MKBETAG('H', 'E', 'D', 'R'):
  50. case MKBETAG('H', 'D', 'R', '8'):
  51. if (size < 32)
  52. return AVERROR_INVALIDDATA;
  53. avio_skip(s->pb, 20);
  54. rate = avio_rl32(s->pb);
  55. avio_skip(s->pb, size - (avio_tell(s->pb) - pos));
  56. break;
  57. case MKBETAG('N', 'O', 'T', 'E'):
  58. avio_get_str(s->pb, size, value, sizeof(value));
  59. av_dict_set(&s->metadata, "Comment", value, 0);
  60. avio_skip(s->pb, size & 1);
  61. break;
  62. case MKBETAG('S', 'D', 'A', 'B'):
  63. channels = 2;
  64. break;
  65. case MKBETAG('S', 'D', '_', '2'):
  66. case MKBETAG('S', 'D', '_', '3'):
  67. case MKBETAG('S', 'D', '_', '4'):
  68. case MKBETAG('S', 'D', '_', '5'):
  69. case MKBETAG('S', 'D', '_', '6'):
  70. case MKBETAG('S', 'D', '_', '7'):
  71. case MKBETAG('S', 'D', '_', '8'):
  72. av_log(s, AV_LOG_WARNING, "Unsupported chunk!\n");
  73. case MKBETAG('S', 'D', 'A', '_'):
  74. case MKBETAG('S', 'D', '_', 'A'):
  75. channels = 1;
  76. break;
  77. }
  78. if (channels)
  79. break;
  80. }
  81. st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
  82. st->codecpar->channels = channels;
  83. st->codecpar->sample_rate = rate;
  84. st->codecpar->codec_id = AV_CODEC_ID_PCM_S16LE;
  85. st->codecpar->block_align = 2 * channels;
  86. return 0;
  87. }
  88. AVInputFormat ff_nsp_demuxer = {
  89. .name = "nsp",
  90. .long_name = NULL_IF_CONFIG_SMALL("Computerized Speech Lab NSP"),
  91. .read_probe = nsp_probe,
  92. .read_header = nsp_read_header,
  93. .read_packet = ff_pcm_read_packet,
  94. .read_seek = ff_pcm_read_seek,
  95. .extensions = "nsp",
  96. .flags = AVFMT_GENERIC_INDEX,
  97. };