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.

174 lines
5.1KB

  1. /*
  2. * Wideband Single-bit Data (WSD) demuxer
  3. * Copyright (c) 2014 Peter Ross
  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 "libavutil/timecode.h"
  23. #include "avformat.h"
  24. #include "internal.h"
  25. #include "rawdec.h"
  26. static int wsd_probe(const AVProbeData *p)
  27. {
  28. if (p->buf_size < 45 || memcmp(p->buf, "1bit", 4) ||
  29. !AV_RB32(p->buf + 36) || !p->buf[44] ||
  30. (p->buf[0] >= 0x10 && (AV_RB32(p->buf + 20) < 0x80 || AV_RB32(p->buf + 24) < 0x80)))
  31. return 0;
  32. return AVPROBE_SCORE_MAX;
  33. }
  34. static int empty_string(const char *buf, unsigned size)
  35. {
  36. while (size--) {
  37. if (*buf++ != ' ')
  38. return 0;
  39. }
  40. return 1;
  41. }
  42. static int wsd_to_av_channel_layoyt(AVFormatContext *s, int bit)
  43. {
  44. switch (bit) {
  45. case 2: return AV_CH_BACK_RIGHT;
  46. case 3:
  47. avpriv_request_sample(s, "Rr-middle");
  48. break;
  49. case 4: return AV_CH_BACK_CENTER;
  50. case 5:
  51. avpriv_request_sample(s, "Lr-middle");
  52. break;
  53. case 6: return AV_CH_BACK_LEFT;
  54. case 24: return AV_CH_LOW_FREQUENCY;
  55. case 26: return AV_CH_FRONT_RIGHT;
  56. case 27: return AV_CH_FRONT_RIGHT_OF_CENTER;
  57. case 28: return AV_CH_FRONT_CENTER;
  58. case 29: return AV_CH_FRONT_LEFT_OF_CENTER;
  59. case 30: return AV_CH_FRONT_LEFT;
  60. default:
  61. av_log(s, AV_LOG_WARNING, "reserved channel assignment\n");
  62. break;
  63. }
  64. return 0;
  65. }
  66. static int get_metadata(AVFormatContext *s, const char *const tag, const unsigned size)
  67. {
  68. uint8_t *buf;
  69. if (!(size + 1))
  70. return AVERROR(ENOMEM);
  71. buf = av_malloc(size + 1);
  72. if (!buf)
  73. return AVERROR(ENOMEM);
  74. if (avio_read(s->pb, buf, size) != size) {
  75. av_free(buf);
  76. return AVERROR(EIO);
  77. }
  78. if (empty_string(buf, size)) {
  79. av_free(buf);
  80. return 0;
  81. }
  82. buf[size] = 0;
  83. av_dict_set(&s->metadata, tag, buf, AV_DICT_DONT_STRDUP_VAL);
  84. return 0;
  85. }
  86. static int wsd_read_header(AVFormatContext *s)
  87. {
  88. AVIOContext *pb = s->pb;
  89. AVStream *st;
  90. int version;
  91. uint32_t text_offset, data_offset, channel_assign;
  92. char playback_time[AV_TIMECODE_STR_SIZE];
  93. st = avformat_new_stream(s, NULL);
  94. if (!st)
  95. return AVERROR(ENOMEM);
  96. avio_skip(pb, 8);
  97. version = avio_r8(pb);
  98. av_log(s, AV_LOG_DEBUG, "version: %i.%i\n", version >> 4, version & 0xF);
  99. avio_skip(pb, 11);
  100. if (version < 0x10) {
  101. text_offset = 0x80;
  102. data_offset = 0x800;
  103. avio_skip(pb, 8);
  104. } else {
  105. text_offset = avio_rb32(pb);
  106. data_offset = avio_rb32(pb);
  107. }
  108. avio_skip(pb, 4);
  109. av_timecode_make_smpte_tc_string(playback_time, avio_rb32(pb), 0);
  110. av_dict_set(&s->metadata, "playback_time", playback_time, 0);
  111. st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
  112. st->codecpar->codec_id = s->iformat->raw_codec_id;
  113. st->codecpar->sample_rate = avio_rb32(pb) / 8;
  114. avio_skip(pb, 4);
  115. st->codecpar->channels = avio_r8(pb) & 0xF;
  116. st->codecpar->bit_rate = (int64_t)st->codecpar->channels * st->codecpar->sample_rate * 8LL;
  117. if (!st->codecpar->channels)
  118. return AVERROR_INVALIDDATA;
  119. avio_skip(pb, 3);
  120. channel_assign = avio_rb32(pb);
  121. if (!(channel_assign & 1)) {
  122. int i;
  123. for (i = 1; i < 32; i++)
  124. if ((channel_assign >> i) & 1)
  125. st->codecpar->channel_layout |= wsd_to_av_channel_layoyt(s, i);
  126. }
  127. avio_skip(pb, 16);
  128. if (avio_rb32(pb))
  129. avpriv_request_sample(s, "emphasis");
  130. if (avio_seek(pb, text_offset, SEEK_SET) >= 0) {
  131. get_metadata(s, "title", 128);
  132. get_metadata(s, "composer", 128);
  133. get_metadata(s, "song_writer", 128);
  134. get_metadata(s, "artist", 128);
  135. get_metadata(s, "album", 128);
  136. get_metadata(s, "genre", 32);
  137. get_metadata(s, "date", 32);
  138. get_metadata(s, "location", 32);
  139. get_metadata(s, "comment", 512);
  140. get_metadata(s, "user", 512);
  141. }
  142. return avio_seek(pb, data_offset, SEEK_SET);
  143. }
  144. AVInputFormat ff_wsd_demuxer = {
  145. .name = "wsd",
  146. .long_name = NULL_IF_CONFIG_SMALL("Wideband Single-bit Data (WSD)"),
  147. .read_probe = wsd_probe,
  148. .read_header = wsd_read_header,
  149. .read_packet = ff_raw_read_partial_packet,
  150. .extensions = "wsd",
  151. .flags = AVFMT_GENERIC_INDEX | AVFMT_NO_BYTE_SEEK,
  152. .raw_codec_id = AV_CODEC_ID_DSD_MSBF,
  153. };