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.

203 lines
5.8KB

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