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.

160 lines
4.5KB

  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_free_extra_meta(&id3v2_extra_meta);
  53. }
  54. static int dsf_read_header(AVFormatContext *s)
  55. {
  56. DSFContext *dsf = s->priv_data;
  57. AVIOContext *pb = s->pb;
  58. AVStream *st;
  59. uint64_t id3pos;
  60. unsigned int channel_type;
  61. avio_skip(pb, 4);
  62. if (avio_rl64(pb) != 28)
  63. return AVERROR_INVALIDDATA;
  64. /* create primary stream before any id3 coverart streams */
  65. st = avformat_new_stream(s, NULL);
  66. if (!st)
  67. return AVERROR(ENOMEM);
  68. avio_skip(pb, 8);
  69. id3pos = avio_rl64(pb);
  70. if (pb->seekable) {
  71. read_id3(s, id3pos);
  72. avio_seek(pb, 28, SEEK_SET);
  73. }
  74. /* fmt chunk */
  75. if (avio_rl32(pb) != MKTAG('f', 'm', 't', ' ') || avio_rl64(pb) != 52)
  76. return AVERROR_INVALIDDATA;
  77. if (avio_rl32(pb) != 1) {
  78. avpriv_request_sample(s, "unknown format version");
  79. return AVERROR_INVALIDDATA;
  80. }
  81. if (avio_rl32(pb)) {
  82. avpriv_request_sample(s, "unknown format id");
  83. return AVERROR_INVALIDDATA;
  84. }
  85. channel_type = avio_rl32(pb);
  86. if (channel_type < FF_ARRAY_ELEMS(dsf_channel_layout))
  87. st->codec->channel_layout = dsf_channel_layout[channel_type];
  88. if (!st->codec->channel_layout)
  89. avpriv_request_sample(s, "channel type %i", channel_type);
  90. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  91. st->codec->channels = avio_rl32(pb);
  92. st->codec->sample_rate = avio_rl32(pb) / 8;
  93. switch(avio_rl32(pb)) {
  94. case 1: st->codec->codec_id = AV_CODEC_ID_DSD_LSBF_PLANAR; break;
  95. case 8: st->codec->codec_id = AV_CODEC_ID_DSD_MSBF_PLANAR; break;
  96. default:
  97. avpriv_request_sample(s, "unknown most significant bit");
  98. return AVERROR_INVALIDDATA;
  99. }
  100. avio_skip(pb, 8);
  101. st->codec->block_align = avio_rl32(pb);
  102. if (st->codec->block_align > INT_MAX / st->codec->channels) {
  103. avpriv_request_sample(s, "block_align overflow");
  104. return AVERROR_INVALIDDATA;
  105. }
  106. st->codec->block_align *= st->codec->channels;
  107. avio_skip(pb, 4);
  108. /* data chunk */
  109. dsf->data_end = avio_tell(pb);
  110. if (avio_rl32(pb) != MKTAG('d', 'a', 't', 'a'))
  111. return AVERROR_INVALIDDATA;
  112. dsf->data_end += avio_rl64(pb);
  113. return 0;
  114. }
  115. static int dsf_read_packet(AVFormatContext *s, AVPacket *pkt)
  116. {
  117. DSFContext *dsf = s->priv_data;
  118. AVIOContext *pb = s->pb;
  119. AVStream *st = s->streams[0];
  120. int64_t pos = avio_tell(pb);
  121. if (pos >= dsf->data_end)
  122. return AVERROR_EOF;
  123. pkt->stream_index = 0;
  124. return av_get_packet(pb, pkt, FFMIN(dsf->data_end - pos, st->codec->block_align));
  125. }
  126. AVInputFormat ff_dsf_demuxer = {
  127. .name = "dsf",
  128. .long_name = NULL_IF_CONFIG_SMALL("DSD Stream File (DSF)"),
  129. .priv_data_size = sizeof(DSFContext),
  130. .read_probe = dsf_probe,
  131. .read_header = dsf_read_header,
  132. .read_packet = dsf_read_packet,
  133. .flags = AVFMT_GENERIC_INDEX | AVFMT_NO_BYTE_SEEK,
  134. };