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.

132 lines
4.7KB

  1. /*
  2. * NIST Sphere demuxer
  3. * Copyright (c) 2012 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 nist_probe(AVProbeData *p)
  27. {
  28. if (AV_RL64(p->buf) == AV_RL64("NIST_1A\x0a"))
  29. return AVPROBE_SCORE_MAX;
  30. return 0;
  31. }
  32. static int nist_read_header(AVFormatContext *s)
  33. {
  34. char buffer[32], coding[32] = "pcm", format[32] = "01";
  35. int bps = 0, be = 0;
  36. int32_t header_size = -1;
  37. AVStream *st;
  38. st = avformat_new_stream(s, NULL);
  39. if (!st)
  40. return AVERROR(ENOMEM);
  41. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  42. ff_get_line(s->pb, buffer, sizeof(buffer));
  43. ff_get_line(s->pb, buffer, sizeof(buffer));
  44. sscanf(buffer, "%"SCNd32, &header_size);
  45. if (header_size <= 0)
  46. return AVERROR_INVALIDDATA;
  47. while (!url_feof(s->pb)) {
  48. ff_get_line(s->pb, buffer, sizeof(buffer));
  49. if (avio_tell(s->pb) >= header_size)
  50. return AVERROR_INVALIDDATA;
  51. if (!memcmp(buffer, "end_head", 8)) {
  52. if (!st->codec->bits_per_coded_sample)
  53. st->codec->bits_per_coded_sample = bps << 3;
  54. if (!av_strcasecmp(coding, "pcm")) {
  55. st->codec->codec_id = ff_get_pcm_codec_id(st->codec->bits_per_coded_sample,
  56. 0, be, 0xFFFF);
  57. } else if (!av_strcasecmp(coding, "alaw")) {
  58. st->codec->codec_id = AV_CODEC_ID_PCM_ALAW;
  59. } else if (!av_strcasecmp(coding, "ulaw") ||
  60. !av_strcasecmp(coding, "mu-law")) {
  61. st->codec->codec_id = AV_CODEC_ID_PCM_MULAW;
  62. } else {
  63. avpriv_request_sample(s, "coding %s", coding);
  64. }
  65. avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
  66. st->codec->block_align = st->codec->bits_per_coded_sample * st->codec->channels / 8;
  67. if (avio_tell(s->pb) > header_size)
  68. return AVERROR_INVALIDDATA;
  69. avio_skip(s->pb, header_size - avio_tell(s->pb));
  70. return 0;
  71. } else if (!memcmp(buffer, "channel_count", 13)) {
  72. sscanf(buffer, "%*s %*s %"SCNd32, &st->codec->channels);
  73. } else if (!memcmp(buffer, "sample_byte_format", 18)) {
  74. sscanf(buffer, "%*s %*s %31s", format);
  75. if (!av_strcasecmp(format, "01")) {
  76. be = 0;
  77. } else if (!av_strcasecmp(format, "10")) {
  78. be = 1;
  79. } else if (av_strcasecmp(format, "1")) {
  80. avpriv_request_sample(s, "sample byte format %s", format);
  81. return AVERROR_PATCHWELCOME;
  82. }
  83. } else if (!memcmp(buffer, "sample_coding", 13)) {
  84. sscanf(buffer, "%*s %*s %31s", coding);
  85. } else if (!memcmp(buffer, "sample_count", 12)) {
  86. sscanf(buffer, "%*s %*s %"SCNd64, &st->duration);
  87. } else if (!memcmp(buffer, "sample_n_bytes", 14)) {
  88. sscanf(buffer, "%*s %*s %"SCNd32, &bps);
  89. } else if (!memcmp(buffer, "sample_rate", 11)) {
  90. sscanf(buffer, "%*s %*s %"SCNd32, &st->codec->sample_rate);
  91. } else if (!memcmp(buffer, "sample_sig_bits", 15)) {
  92. sscanf(buffer, "%*s %*s %"SCNd32, &st->codec->bits_per_coded_sample);
  93. } else {
  94. char key[32], value[32];
  95. if (sscanf(buffer, "%31s %*s %31s", key, value) == 3) {
  96. av_dict_set(&s->metadata, key, value, AV_DICT_APPEND);
  97. } else {
  98. av_log(s, AV_LOG_ERROR, "Failed to parse '%s' as metadata\n", buffer);
  99. }
  100. }
  101. }
  102. return AVERROR_EOF;
  103. }
  104. AVInputFormat ff_nistsphere_demuxer = {
  105. .name = "nistsphere",
  106. .long_name = NULL_IF_CONFIG_SMALL("NIST SPeech HEader REsources"),
  107. .read_probe = nist_probe,
  108. .read_header = nist_read_header,
  109. .read_packet = ff_pcm_read_packet,
  110. .read_seek = ff_pcm_read_seek,
  111. .extensions = "nist,sph",
  112. .flags = AVFMT_GENERIC_INDEX,
  113. };