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.

211 lines
6.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. uint64_t audio_size;
  28. uint64_t data_size;
  29. } DSFContext;
  30. static int dsf_probe(const AVProbeData *p)
  31. {
  32. if (p->buf_size < 12 || memcmp(p->buf, "DSD ", 4) || AV_RL64(p->buf + 4) != 28)
  33. return 0;
  34. return AVPROBE_SCORE_MAX;
  35. }
  36. static const uint64_t dsf_channel_layout[] = {
  37. 0,
  38. AV_CH_LAYOUT_MONO,
  39. AV_CH_LAYOUT_STEREO,
  40. AV_CH_LAYOUT_SURROUND,
  41. AV_CH_LAYOUT_QUAD,
  42. AV_CH_LAYOUT_4POINT0,
  43. AV_CH_LAYOUT_5POINT0_BACK,
  44. AV_CH_LAYOUT_5POINT1_BACK,
  45. };
  46. static void read_id3(AVFormatContext *s, uint64_t id3pos)
  47. {
  48. ID3v2ExtraMeta *id3v2_extra_meta = NULL;
  49. if (avio_seek(s->pb, id3pos, SEEK_SET) < 0)
  50. return;
  51. ff_id3v2_read(s, ID3v2_DEFAULT_MAGIC, &id3v2_extra_meta, 0);
  52. if (id3v2_extra_meta) {
  53. ff_id3v2_parse_apic(s, id3v2_extra_meta);
  54. ff_id3v2_parse_chapters(s, id3v2_extra_meta);
  55. }
  56. ff_id3v2_free_extra_meta(&id3v2_extra_meta);
  57. }
  58. static int dsf_read_header(AVFormatContext *s)
  59. {
  60. DSFContext *dsf = s->priv_data;
  61. AVIOContext *pb = s->pb;
  62. AVStream *st;
  63. uint64_t id3pos;
  64. unsigned int channel_type;
  65. avio_skip(pb, 4);
  66. if (avio_rl64(pb) != 28)
  67. return AVERROR_INVALIDDATA;
  68. /* create primary stream before any id3 coverart streams */
  69. st = avformat_new_stream(s, NULL);
  70. if (!st)
  71. return AVERROR(ENOMEM);
  72. avio_skip(pb, 8);
  73. id3pos = avio_rl64(pb);
  74. if (pb->seekable & AVIO_SEEKABLE_NORMAL) {
  75. read_id3(s, id3pos);
  76. avio_seek(pb, 28, SEEK_SET);
  77. }
  78. /* fmt chunk */
  79. if (avio_rl32(pb) != MKTAG('f', 'm', 't', ' ') || avio_rl64(pb) != 52)
  80. return AVERROR_INVALIDDATA;
  81. if (avio_rl32(pb) != 1) {
  82. avpriv_request_sample(s, "unknown format version");
  83. return AVERROR_INVALIDDATA;
  84. }
  85. if (avio_rl32(pb)) {
  86. avpriv_request_sample(s, "unknown format id");
  87. return AVERROR_INVALIDDATA;
  88. }
  89. channel_type = avio_rl32(pb);
  90. if (channel_type < FF_ARRAY_ELEMS(dsf_channel_layout))
  91. st->codecpar->channel_layout = dsf_channel_layout[channel_type];
  92. if (!st->codecpar->channel_layout)
  93. avpriv_request_sample(s, "channel type %i", channel_type);
  94. st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
  95. st->codecpar->channels = avio_rl32(pb);
  96. st->codecpar->sample_rate = avio_rl32(pb) / 8;
  97. if (st->codecpar->channels <= 0)
  98. return AVERROR_INVALIDDATA;
  99. switch(avio_rl32(pb)) {
  100. case 1: st->codecpar->codec_id = AV_CODEC_ID_DSD_LSBF_PLANAR; break;
  101. case 8: st->codecpar->codec_id = AV_CODEC_ID_DSD_MSBF_PLANAR; break;
  102. default:
  103. avpriv_request_sample(s, "unknown most significant bit");
  104. return AVERROR_INVALIDDATA;
  105. }
  106. dsf->audio_size = avio_rl64(pb) / 8 * st->codecpar->channels;
  107. st->codecpar->block_align = avio_rl32(pb);
  108. if (st->codecpar->block_align > INT_MAX / st->codecpar->channels || st->codecpar->block_align <= 0) {
  109. avpriv_request_sample(s, "block_align invalid");
  110. return AVERROR_INVALIDDATA;
  111. }
  112. st->codecpar->block_align *= st->codecpar->channels;
  113. st->codecpar->bit_rate = st->codecpar->channels * st->codecpar->sample_rate * 8LL;
  114. avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
  115. avio_skip(pb, 4);
  116. /* data chunk */
  117. dsf->data_end = avio_tell(pb);
  118. if (avio_rl32(pb) != MKTAG('d', 'a', 't', 'a'))
  119. return AVERROR_INVALIDDATA;
  120. dsf->data_size = avio_rl64(pb) - 12;
  121. dsf->data_end += dsf->data_size + 12;
  122. s->internal->data_offset = avio_tell(pb);
  123. return 0;
  124. }
  125. static int dsf_read_packet(AVFormatContext *s, AVPacket *pkt)
  126. {
  127. DSFContext *dsf = s->priv_data;
  128. AVIOContext *pb = s->pb;
  129. AVStream *st = s->streams[0];
  130. int64_t pos = avio_tell(pb);
  131. int ret;
  132. if (pos >= dsf->data_end)
  133. return AVERROR_EOF;
  134. if (dsf->data_size > dsf->audio_size) {
  135. int last_packet = pos == (dsf->data_end - st->codecpar->block_align);
  136. if (last_packet) {
  137. int64_t data_pos = pos - s->internal->data_offset;
  138. int64_t packet_size = dsf->audio_size - data_pos;
  139. int64_t skip_size = dsf->data_size - data_pos - packet_size;
  140. uint8_t *dst;
  141. int ch, ret;
  142. if (packet_size <= 0 || skip_size <= 0)
  143. return AVERROR_INVALIDDATA;
  144. if ((ret = av_new_packet(pkt, packet_size)) < 0)
  145. return ret;
  146. dst = pkt->data;
  147. for (ch = 0; ch < st->codecpar->channels; ch++) {
  148. ret = avio_read(pb, dst, packet_size / st->codecpar->channels);
  149. if (ret < packet_size / st->codecpar->channels)
  150. return AVERROR_EOF;
  151. dst += ret;
  152. avio_skip(pb, skip_size / st->codecpar->channels);
  153. }
  154. pkt->pos = pos;
  155. pkt->stream_index = 0;
  156. pkt->pts = (pos - s->internal->data_offset) / st->codecpar->channels;
  157. pkt->duration = packet_size / st->codecpar->channels;
  158. return 0;
  159. }
  160. }
  161. ret = av_get_packet(pb, pkt, FFMIN(dsf->data_end - pos, st->codecpar->block_align));
  162. if (ret < 0)
  163. return ret;
  164. pkt->stream_index = 0;
  165. pkt->pts = (pos - s->internal->data_offset) / st->codecpar->channels;
  166. pkt->duration = st->codecpar->block_align / st->codecpar->channels;
  167. return 0;
  168. }
  169. AVInputFormat ff_dsf_demuxer = {
  170. .name = "dsf",
  171. .long_name = NULL_IF_CONFIG_SMALL("DSD Stream File (DSF)"),
  172. .priv_data_size = sizeof(DSFContext),
  173. .read_probe = dsf_probe,
  174. .read_header = dsf_read_header,
  175. .read_packet = dsf_read_packet,
  176. .flags = AVFMT_GENERIC_INDEX | AVFMT_NO_BYTE_SEEK,
  177. };