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.

166 lines
4.8KB

  1. /*
  2. * DSD Stream File (DSF) 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 "avformat.h"
  23. #include "internal.h"
  24. #include "id3v2.h"
  25. typedef struct {
  26. uint64_t data_end;
  27. } DSFContext;
  28. static int dsf_probe(AVProbeData *p)
  29. {
  30. if (p->buf_size < 12 || memcmp(p->buf, "DSD ", 4) || AV_RL64(p->buf + 4) != 28)
  31. return 0;
  32. return AVPROBE_SCORE_MAX;
  33. }
  34. static const uint64_t dsf_channel_layout[] = {
  35. 0,
  36. AV_CH_LAYOUT_MONO,
  37. AV_CH_LAYOUT_STEREO,
  38. AV_CH_LAYOUT_SURROUND,
  39. AV_CH_LAYOUT_QUAD,
  40. AV_CH_LAYOUT_4POINT0,
  41. AV_CH_LAYOUT_5POINT0_BACK,
  42. AV_CH_LAYOUT_5POINT1_BACK,
  43. };
  44. static void read_id3(AVFormatContext *s, uint64_t id3pos)
  45. {
  46. ID3v2ExtraMeta *id3v2_extra_meta = NULL;
  47. if (avio_seek(s->pb, id3pos, SEEK_SET) < 0)
  48. return;
  49. ff_id3v2_read(s, ID3v2_DEFAULT_MAGIC, &id3v2_extra_meta, 0);
  50. if (id3v2_extra_meta) {
  51. ff_id3v2_parse_apic(s, &id3v2_extra_meta);
  52. ff_id3v2_parse_chapters(s, &id3v2_extra_meta);
  53. }
  54. ff_id3v2_free_extra_meta(&id3v2_extra_meta);
  55. }
  56. static int dsf_read_header(AVFormatContext *s)
  57. {
  58. DSFContext *dsf = s->priv_data;
  59. AVIOContext *pb = s->pb;
  60. AVStream *st;
  61. uint64_t id3pos;
  62. unsigned int channel_type;
  63. avio_skip(pb, 4);
  64. if (avio_rl64(pb) != 28)
  65. return AVERROR_INVALIDDATA;
  66. /* create primary stream before any id3 coverart streams */
  67. st = avformat_new_stream(s, NULL);
  68. if (!st)
  69. return AVERROR(ENOMEM);
  70. avio_skip(pb, 8);
  71. id3pos = avio_rl64(pb);
  72. if (pb->seekable & AVIO_SEEKABLE_NORMAL) {
  73. read_id3(s, id3pos);
  74. avio_seek(pb, 28, SEEK_SET);
  75. }
  76. /* fmt chunk */
  77. if (avio_rl32(pb) != MKTAG('f', 'm', 't', ' ') || avio_rl64(pb) != 52)
  78. return AVERROR_INVALIDDATA;
  79. if (avio_rl32(pb) != 1) {
  80. avpriv_request_sample(s, "unknown format version");
  81. return AVERROR_INVALIDDATA;
  82. }
  83. if (avio_rl32(pb)) {
  84. avpriv_request_sample(s, "unknown format id");
  85. return AVERROR_INVALIDDATA;
  86. }
  87. channel_type = avio_rl32(pb);
  88. if (channel_type < FF_ARRAY_ELEMS(dsf_channel_layout))
  89. st->codecpar->channel_layout = dsf_channel_layout[channel_type];
  90. if (!st->codecpar->channel_layout)
  91. avpriv_request_sample(s, "channel type %i", channel_type);
  92. st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
  93. st->codecpar->channels = avio_rl32(pb);
  94. st->codecpar->sample_rate = avio_rl32(pb) / 8;
  95. if (st->codecpar->channels <= 0)
  96. return AVERROR_INVALIDDATA;
  97. switch(avio_rl32(pb)) {
  98. case 1: st->codecpar->codec_id = AV_CODEC_ID_DSD_LSBF_PLANAR; break;
  99. case 8: st->codecpar->codec_id = AV_CODEC_ID_DSD_MSBF_PLANAR; break;
  100. default:
  101. avpriv_request_sample(s, "unknown most significant bit");
  102. return AVERROR_INVALIDDATA;
  103. }
  104. avio_skip(pb, 8);
  105. st->codecpar->block_align = avio_rl32(pb);
  106. if (st->codecpar->block_align > INT_MAX / st->codecpar->channels) {
  107. avpriv_request_sample(s, "block_align overflow");
  108. return AVERROR_INVALIDDATA;
  109. }
  110. st->codecpar->block_align *= st->codecpar->channels;
  111. st->codecpar->bit_rate = st->codecpar->channels * st->codecpar->sample_rate * 8LL;
  112. avio_skip(pb, 4);
  113. /* data chunk */
  114. dsf->data_end = avio_tell(pb);
  115. if (avio_rl32(pb) != MKTAG('d', 'a', 't', 'a'))
  116. return AVERROR_INVALIDDATA;
  117. dsf->data_end += avio_rl64(pb);
  118. return 0;
  119. }
  120. static int dsf_read_packet(AVFormatContext *s, AVPacket *pkt)
  121. {
  122. DSFContext *dsf = s->priv_data;
  123. AVIOContext *pb = s->pb;
  124. AVStream *st = s->streams[0];
  125. int64_t pos = avio_tell(pb);
  126. if (pos >= dsf->data_end)
  127. return AVERROR_EOF;
  128. pkt->stream_index = 0;
  129. return av_get_packet(pb, pkt, FFMIN(dsf->data_end - pos, st->codecpar->block_align));
  130. }
  131. AVInputFormat ff_dsf_demuxer = {
  132. .name = "dsf",
  133. .long_name = NULL_IF_CONFIG_SMALL("DSD Stream File (DSF)"),
  134. .priv_data_size = sizeof(DSFContext),
  135. .read_probe = dsf_probe,
  136. .read_header = dsf_read_header,
  137. .read_packet = dsf_read_packet,
  138. .flags = AVFMT_GENERIC_INDEX | AVFMT_NO_BYTE_SEEK,
  139. };