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.

169 lines
5.0KB

  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(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, found_ssmp = 0;
  100. AVStream *st = s->streams[0];
  101. int64_t pos;
  102. int ret = 0;
  103. while (!found_ssmp) {
  104. if (avio_feof(s->pb))
  105. return AVERROR_EOF;
  106. pos = avio_tell(s->pb);
  107. chunk = avio_rl32(s->pb);
  108. size = avio_rb32(s->pb);
  109. if (!size)
  110. continue;
  111. if (size < 8)
  112. return AVERROR_INVALIDDATA;
  113. size -= 8;
  114. switch (chunk) {
  115. case MKTAG('S','N','D','S'):
  116. if (size <= 16)
  117. return AVERROR_INVALIDDATA;
  118. avio_skip(s->pb, 8);
  119. if (avio_rl32(s->pb) != MKTAG('S','S','M','P'))
  120. return AVERROR_INVALIDDATA;
  121. avio_skip(s->pb, 4);
  122. size -= 16;
  123. ret = av_get_packet(s->pb, pkt, size);
  124. pkt->pos = pos;
  125. pkt->stream_index = 0;
  126. pkt->duration = size / st->codecpar->channels;
  127. size = 0;
  128. found_ssmp = 1;
  129. break;
  130. default:
  131. av_log(s, AV_LOG_DEBUG, "skipping unknown chunk: %X\n", chunk);
  132. break;
  133. }
  134. avio_skip(s->pb, size);
  135. }
  136. return ret;
  137. }
  138. AVInputFormat ff_threedostr_demuxer = {
  139. .name = "3dostr",
  140. .long_name = NULL_IF_CONFIG_SMALL("3DO STR"),
  141. .read_probe = threedostr_probe,
  142. .read_header = threedostr_read_header,
  143. .read_packet = threedostr_read_packet,
  144. .extensions = "str",
  145. .flags = AVFMT_GENERIC_INDEX,
  146. };