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.

164 lines
4.9KB

  1. /*
  2. * 3DO STR demuxer
  3. * Copyright (c) 2015 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 "avformat.h"
  22. #include "internal.h"
  23. static int threedostr_probe(const AVProbeData *p)
  24. {
  25. if (memcmp(p->buf, "CTRL", 4) &&
  26. memcmp(p->buf, "SHDR", 4) &&
  27. memcmp(p->buf, "SNDS", 4))
  28. return 0;
  29. return AVPROBE_SCORE_MAX / 3 * 2;
  30. }
  31. static int threedostr_read_header(AVFormatContext *s)
  32. {
  33. unsigned chunk, codec = 0, size, ctrl_size = -1, found_shdr = 0;
  34. AVStream *st;
  35. while (!avio_feof(s->pb) && !found_shdr) {
  36. chunk = avio_rl32(s->pb);
  37. size = avio_rb32(s->pb);
  38. if (size < 8)
  39. return AVERROR_INVALIDDATA;
  40. size -= 8;
  41. switch (chunk) {
  42. case MKTAG('C','T','R','L'):
  43. ctrl_size = size;
  44. break;
  45. case MKTAG('S','N','D','S'):
  46. if (size < 56)
  47. return AVERROR_INVALIDDATA;
  48. avio_skip(s->pb, 8);
  49. if (avio_rl32(s->pb) != MKTAG('S','H','D','R'))
  50. return AVERROR_INVALIDDATA;
  51. avio_skip(s->pb, 24);
  52. st = avformat_new_stream(s, NULL);
  53. if (!st)
  54. return AVERROR(ENOMEM);
  55. st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
  56. st->codecpar->sample_rate = avio_rb32(s->pb);
  57. st->codecpar->channels = avio_rb32(s->pb);
  58. if (st->codecpar->channels <= 0)
  59. return AVERROR_INVALIDDATA;
  60. codec = avio_rl32(s->pb);
  61. avio_skip(s->pb, 4);
  62. if (ctrl_size == 20 || ctrl_size == 3 || ctrl_size == -1)
  63. st->duration = (avio_rb32(s->pb) - 1) / st->codecpar->channels;
  64. else
  65. st->duration = avio_rb32(s->pb) * 16 / st->codecpar->channels;
  66. size -= 56;
  67. found_shdr = 1;
  68. break;
  69. case MKTAG('S','H','D','R'):
  70. if (size > 0x78) {
  71. avio_skip(s->pb, 0x74);
  72. size -= 0x78;
  73. if (avio_rl32(s->pb) == MKTAG('C','T','R','L') && size > 4) {
  74. ctrl_size = avio_rb32(s->pb);
  75. size -= 4;
  76. }
  77. }
  78. break;
  79. default:
  80. av_log(s, AV_LOG_DEBUG, "skipping unknown chunk: %X\n", chunk);
  81. break;
  82. }
  83. avio_skip(s->pb, size);
  84. }
  85. switch (codec) {
  86. case MKTAG('S','D','X','2'):
  87. st->codecpar->codec_id = AV_CODEC_ID_SDX2_DPCM;
  88. st->codecpar->block_align = 1 * st->codecpar->channels;
  89. break;
  90. default:
  91. avpriv_request_sample(s, "codec %X", codec);
  92. return AVERROR_PATCHWELCOME;
  93. }
  94. avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
  95. return 0;
  96. }
  97. static int threedostr_read_packet(AVFormatContext *s, AVPacket *pkt)
  98. {
  99. unsigned chunk, size;
  100. AVStream *st = s->streams[0];
  101. int64_t pos;
  102. int ret = 0;
  103. while (!avio_feof(s->pb)) {
  104. pos = avio_tell(s->pb);
  105. chunk = avio_rl32(s->pb);
  106. size = avio_rb32(s->pb);
  107. if (!size)
  108. continue;
  109. if (size < 8)
  110. return AVERROR_INVALIDDATA;
  111. size -= 8;
  112. switch (chunk) {
  113. case MKTAG('S','N','D','S'):
  114. if (size <= 16)
  115. return AVERROR_INVALIDDATA;
  116. avio_skip(s->pb, 8);
  117. if (avio_rl32(s->pb) != MKTAG('S','S','M','P'))
  118. return AVERROR_INVALIDDATA;
  119. avio_skip(s->pb, 4);
  120. size -= 16;
  121. ret = av_get_packet(s->pb, pkt, size);
  122. pkt->pos = pos;
  123. pkt->stream_index = 0;
  124. pkt->duration = size / st->codecpar->channels;
  125. return ret;
  126. default:
  127. av_log(s, AV_LOG_DEBUG, "skipping unknown chunk: %X\n", chunk);
  128. break;
  129. }
  130. avio_skip(s->pb, size);
  131. }
  132. return AVERROR_EOF;
  133. }
  134. AVInputFormat ff_threedostr_demuxer = {
  135. .name = "3dostr",
  136. .long_name = NULL_IF_CONFIG_SMALL("3DO STR"),
  137. .read_probe = threedostr_probe,
  138. .read_header = threedostr_read_header,
  139. .read_packet = threedostr_read_packet,
  140. .extensions = "str",
  141. .flags = AVFMT_GENERIC_INDEX,
  142. };