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.

297 lines
9.5KB

  1. /*
  2. * RIFF demuxing functions and data
  3. * Copyright (c) 2000 Fabrice Bellard
  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/dict.h"
  22. #include "libavutil/error.h"
  23. #include "libavutil/log.h"
  24. #include "libavutil/mathematics.h"
  25. #include "libavcodec/avcodec.h"
  26. #include "libavcodec/bytestream.h"
  27. #include "avformat.h"
  28. #include "avio_internal.h"
  29. #include "riff.h"
  30. int ff_get_guid(AVIOContext *s, ff_asf_guid *g)
  31. {
  32. int ret;
  33. av_assert0(sizeof(*g) == 16); //compiler will optimize this out
  34. ret = avio_read(s, *g, sizeof(*g));
  35. if (ret < (int)sizeof(*g)) {
  36. memset(*g, 0, sizeof(*g));
  37. return ret < 0 ? ret : AVERROR_INVALIDDATA;
  38. }
  39. return 0;
  40. }
  41. enum AVCodecID ff_codec_guid_get_id(const AVCodecGuid *guids, ff_asf_guid guid)
  42. {
  43. int i;
  44. for (i = 0; guids[i].id != AV_CODEC_ID_NONE; i++)
  45. if (!ff_guidcmp(guids[i].guid, guid))
  46. return guids[i].id;
  47. return AV_CODEC_ID_NONE;
  48. }
  49. /* We could be given one of the three possible structures here:
  50. * WAVEFORMAT, PCMWAVEFORMAT or WAVEFORMATEX. Each structure
  51. * is an expansion of the previous one with the fields added
  52. * at the bottom. PCMWAVEFORMAT adds 'WORD wBitsPerSample' and
  53. * WAVEFORMATEX adds 'WORD cbSize' and basically makes itself
  54. * an openended structure.
  55. */
  56. static void parse_waveformatex(AVIOContext *pb, AVCodecParameters *par)
  57. {
  58. ff_asf_guid subformat;
  59. int bps = avio_rl16(pb);
  60. if (bps)
  61. par->bits_per_coded_sample = bps;
  62. par->channel_layout = avio_rl32(pb); /* dwChannelMask */
  63. ff_get_guid(pb, &subformat);
  64. if (!memcmp(subformat + 4,
  65. (const uint8_t[]){ FF_AMBISONIC_BASE_GUID }, 12) ||
  66. !memcmp(subformat + 4,
  67. (const uint8_t[]){ FF_MEDIASUBTYPE_BASE_GUID }, 12)) {
  68. par->codec_tag = AV_RL32(subformat);
  69. par->codec_id = ff_wav_codec_get_id(par->codec_tag,
  70. par->bits_per_coded_sample);
  71. } else {
  72. par->codec_id = ff_codec_guid_get_id(ff_codec_wav_guids, subformat);
  73. if (!par->codec_id)
  74. av_log(pb, AV_LOG_WARNING,
  75. "unknown subformat:"FF_PRI_GUID"\n",
  76. FF_ARG_GUID(subformat));
  77. }
  78. }
  79. /* "big_endian" values are needed for RIFX file format */
  80. int ff_get_wav_header(AVFormatContext *s, AVIOContext *pb,
  81. AVCodecParameters *par, int size, int big_endian)
  82. {
  83. int id;
  84. uint64_t bitrate = 0;
  85. if (size < 14) {
  86. avpriv_request_sample(s, "wav header size < 14");
  87. return AVERROR_INVALIDDATA;
  88. }
  89. par->codec_type = AVMEDIA_TYPE_AUDIO;
  90. if (!big_endian) {
  91. id = avio_rl16(pb);
  92. if (id != 0x0165) {
  93. par->channels = avio_rl16(pb);
  94. par->sample_rate = avio_rl32(pb);
  95. bitrate = avio_rl32(pb) * 8LL;
  96. par->block_align = avio_rl16(pb);
  97. }
  98. } else {
  99. id = avio_rb16(pb);
  100. par->channels = avio_rb16(pb);
  101. par->sample_rate = avio_rb32(pb);
  102. bitrate = avio_rb32(pb) * 8LL;
  103. par->block_align = avio_rb16(pb);
  104. }
  105. if (size == 14) { /* We're dealing with plain vanilla WAVEFORMAT */
  106. par->bits_per_coded_sample = 8;
  107. } else {
  108. if (!big_endian) {
  109. par->bits_per_coded_sample = avio_rl16(pb);
  110. } else {
  111. par->bits_per_coded_sample = avio_rb16(pb);
  112. }
  113. }
  114. if (id == 0xFFFE) {
  115. par->codec_tag = 0;
  116. } else {
  117. par->codec_tag = id;
  118. par->codec_id = ff_wav_codec_get_id(id,
  119. par->bits_per_coded_sample);
  120. }
  121. if (size >= 18 && id != 0x0165) { /* We're obviously dealing with WAVEFORMATEX */
  122. int cbSize = avio_rl16(pb); /* cbSize */
  123. if (big_endian) {
  124. avpriv_report_missing_feature(s, "WAVEFORMATEX support for RIFX files");
  125. return AVERROR_PATCHWELCOME;
  126. }
  127. size -= 18;
  128. cbSize = FFMIN(size, cbSize);
  129. if (cbSize >= 22 && id == 0xfffe) { /* WAVEFORMATEXTENSIBLE */
  130. parse_waveformatex(pb, par);
  131. cbSize -= 22;
  132. size -= 22;
  133. }
  134. if (cbSize > 0) {
  135. av_freep(&par->extradata);
  136. if (ff_get_extradata(s, par, pb, cbSize) < 0)
  137. return AVERROR(ENOMEM);
  138. size -= cbSize;
  139. }
  140. /* It is possible for the chunk to contain garbage at the end */
  141. if (size > 0)
  142. avio_skip(pb, size);
  143. } else if (id == 0x0165 && size >= 32) {
  144. int nb_streams, i;
  145. size -= 4;
  146. av_freep(&par->extradata);
  147. if (ff_get_extradata(s, par, pb, size) < 0)
  148. return AVERROR(ENOMEM);
  149. nb_streams = AV_RL16(par->extradata + 4);
  150. par->sample_rate = AV_RL32(par->extradata + 12);
  151. par->channels = 0;
  152. bitrate = 0;
  153. if (size < 8 + nb_streams * 20)
  154. return AVERROR_INVALIDDATA;
  155. for (i = 0; i < nb_streams; i++)
  156. par->channels += par->extradata[8 + i * 20 + 17];
  157. }
  158. par->bit_rate = bitrate;
  159. if (par->sample_rate <= 0) {
  160. av_log(s, AV_LOG_ERROR,
  161. "Invalid sample rate: %d\n", par->sample_rate);
  162. return AVERROR_INVALIDDATA;
  163. }
  164. if (par->codec_id == AV_CODEC_ID_AAC_LATM) {
  165. /* Channels and sample_rate values are those prior to applying SBR
  166. * and/or PS. */
  167. par->channels = 0;
  168. par->sample_rate = 0;
  169. }
  170. /* override bits_per_coded_sample for G.726 */
  171. if (par->codec_id == AV_CODEC_ID_ADPCM_G726 && par->sample_rate)
  172. par->bits_per_coded_sample = par->bit_rate / par->sample_rate;
  173. return 0;
  174. }
  175. enum AVCodecID ff_wav_codec_get_id(unsigned int tag, int bps)
  176. {
  177. enum AVCodecID id;
  178. id = ff_codec_get_id(ff_codec_wav_tags, tag);
  179. if (id <= 0)
  180. return id;
  181. if (id == AV_CODEC_ID_PCM_S16LE)
  182. id = ff_get_pcm_codec_id(bps, 0, 0, ~1);
  183. else if (id == AV_CODEC_ID_PCM_F32LE)
  184. id = ff_get_pcm_codec_id(bps, 1, 0, 0);
  185. if (id == AV_CODEC_ID_ADPCM_IMA_WAV && bps == 8)
  186. id = AV_CODEC_ID_PCM_ZORK;
  187. return id;
  188. }
  189. int ff_get_bmp_header(AVIOContext *pb, AVStream *st, uint32_t *size)
  190. {
  191. int tag1;
  192. uint32_t size_ = avio_rl32(pb);
  193. if (size)
  194. *size = size_;
  195. st->codecpar->width = avio_rl32(pb);
  196. st->codecpar->height = (int32_t)avio_rl32(pb);
  197. avio_rl16(pb); /* planes */
  198. st->codecpar->bits_per_coded_sample = avio_rl16(pb); /* depth */
  199. tag1 = avio_rl32(pb);
  200. avio_rl32(pb); /* ImageSize */
  201. avio_rl32(pb); /* XPelsPerMeter */
  202. avio_rl32(pb); /* YPelsPerMeter */
  203. avio_rl32(pb); /* ClrUsed */
  204. avio_rl32(pb); /* ClrImportant */
  205. return tag1;
  206. }
  207. int ff_read_riff_info(AVFormatContext *s, int64_t size)
  208. {
  209. int64_t start, end, cur;
  210. AVIOContext *pb = s->pb;
  211. start = avio_tell(pb);
  212. end = start + size;
  213. while ((cur = avio_tell(pb)) >= 0 &&
  214. cur <= end - 8 /* = tag + size */) {
  215. uint32_t chunk_code;
  216. int64_t chunk_size;
  217. char key[5] = { 0 };
  218. char *value;
  219. chunk_code = avio_rl32(pb);
  220. chunk_size = avio_rl32(pb);
  221. if (avio_feof(pb)) {
  222. if (chunk_code || chunk_size) {
  223. av_log(s, AV_LOG_WARNING, "INFO subchunk truncated\n");
  224. return AVERROR_INVALIDDATA;
  225. }
  226. return AVERROR_EOF;
  227. }
  228. if (chunk_size > end ||
  229. end - chunk_size < cur ||
  230. chunk_size == UINT_MAX) {
  231. avio_seek(pb, -9, SEEK_CUR);
  232. chunk_code = avio_rl32(pb);
  233. chunk_size = avio_rl32(pb);
  234. if (chunk_size > end || end - chunk_size < cur || chunk_size == UINT_MAX) {
  235. av_log(s, AV_LOG_WARNING, "too big INFO subchunk\n");
  236. return AVERROR_INVALIDDATA;
  237. }
  238. }
  239. chunk_size += (chunk_size & 1);
  240. if (!chunk_code) {
  241. if (chunk_size)
  242. avio_skip(pb, chunk_size);
  243. else if (pb->eof_reached) {
  244. av_log(s, AV_LOG_WARNING, "truncated file\n");
  245. return AVERROR_EOF;
  246. }
  247. continue;
  248. }
  249. value = av_mallocz(chunk_size + 1);
  250. if (!value) {
  251. av_log(s, AV_LOG_ERROR,
  252. "out of memory, unable to read INFO tag\n");
  253. return AVERROR(ENOMEM);
  254. }
  255. AV_WL32(key, chunk_code);
  256. // Work around VC++ 2015 Update 1 code-gen bug:
  257. // https://connect.microsoft.com/VisualStudio/feedback/details/2291638
  258. key[4] = 0;
  259. if (avio_read(pb, value, chunk_size) != chunk_size) {
  260. av_log(s, AV_LOG_WARNING,
  261. "premature end of file while reading INFO tag\n");
  262. }
  263. av_dict_set(&s->metadata, key, value, AV_DICT_DONT_STRDUP_VAL);
  264. }
  265. return 0;
  266. }