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.

307 lines
9.8KB

  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, AVCodecContext *c)
  57. {
  58. ff_asf_guid subformat;
  59. int bps = avio_rl16(pb);
  60. if (bps)
  61. c->bits_per_coded_sample = bps;
  62. c->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. c->codec_tag = AV_RL32(subformat);
  69. c->codec_id = ff_wav_codec_get_id(c->codec_tag,
  70. c->bits_per_coded_sample);
  71. } else {
  72. c->codec_id = ff_codec_guid_get_id(ff_codec_wav_guids, subformat);
  73. if (!c->codec_id)
  74. av_log(c, 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. AVCodecContext *codec, int size, int big_endian)
  82. {
  83. int id;
  84. uint64_t bitrate = 0;
  85. if (size < 14) {
  86. avpriv_request_sample(codec, "wav header size < 14");
  87. return AVERROR_INVALIDDATA;
  88. }
  89. codec->codec_type = AVMEDIA_TYPE_AUDIO;
  90. if (!big_endian) {
  91. id = avio_rl16(pb);
  92. if (id != 0x0165) {
  93. codec->channels = avio_rl16(pb);
  94. codec->sample_rate = avio_rl32(pb);
  95. bitrate = avio_rl32(pb) * 8LL;
  96. codec->block_align = avio_rl16(pb);
  97. }
  98. } else {
  99. id = avio_rb16(pb);
  100. codec->channels = avio_rb16(pb);
  101. codec->sample_rate = avio_rb32(pb);
  102. bitrate = avio_rb32(pb) * 8LL;
  103. codec->block_align = avio_rb16(pb);
  104. }
  105. if (size == 14) { /* We're dealing with plain vanilla WAVEFORMAT */
  106. codec->bits_per_coded_sample = 8;
  107. } else {
  108. if (!big_endian) {
  109. codec->bits_per_coded_sample = avio_rl16(pb);
  110. } else {
  111. codec->bits_per_coded_sample = avio_rb16(pb);
  112. }
  113. }
  114. if (id == 0xFFFE) {
  115. codec->codec_tag = 0;
  116. } else {
  117. codec->codec_tag = id;
  118. codec->codec_id = ff_wav_codec_get_id(id,
  119. codec->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(codec, "WAVEFORMATEX support for RIFX files\n");
  125. return AVERROR_PATCHWELCOME;
  126. }
  127. size -= 18;
  128. cbSize = FFMIN(size, cbSize);
  129. if (cbSize >= 22 && id == 0xfffe) { /* WAVEFORMATEXTENSIBLE */
  130. parse_waveformatex(pb, codec);
  131. cbSize -= 22;
  132. size -= 22;
  133. }
  134. if (cbSize > 0) {
  135. av_freep(&codec->extradata);
  136. if (ff_get_extradata(codec, 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(&codec->extradata);
  147. if (ff_get_extradata(codec, pb, size) < 0)
  148. return AVERROR(ENOMEM);
  149. nb_streams = AV_RL16(codec->extradata + 4);
  150. codec->sample_rate = AV_RL32(codec->extradata + 12);
  151. codec->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. codec->channels += codec->extradata[8 + i * 20 + 17];
  157. }
  158. if (bitrate > INT_MAX) {
  159. if (s->error_recognition & AV_EF_EXPLODE) {
  160. av_log(s, AV_LOG_ERROR,
  161. "The bitrate %"PRIu64" is too large.\n",
  162. bitrate);
  163. return AVERROR_INVALIDDATA;
  164. } else {
  165. av_log(s, AV_LOG_WARNING,
  166. "The bitrate %"PRIu64" is too large, resetting to 0.",
  167. bitrate);
  168. codec->bit_rate = 0;
  169. }
  170. } else {
  171. codec->bit_rate = bitrate;
  172. }
  173. if (codec->sample_rate <= 0) {
  174. av_log(s, AV_LOG_ERROR,
  175. "Invalid sample rate: %d\n", codec->sample_rate);
  176. return AVERROR_INVALIDDATA;
  177. }
  178. if (codec->codec_id == AV_CODEC_ID_AAC_LATM) {
  179. /* Channels and sample_rate values are those prior to applying SBR
  180. * and/or PS. */
  181. codec->channels = 0;
  182. codec->sample_rate = 0;
  183. }
  184. /* override bits_per_coded_sample for G.726 */
  185. if (codec->codec_id == AV_CODEC_ID_ADPCM_G726 && codec->sample_rate)
  186. codec->bits_per_coded_sample = codec->bit_rate / codec->sample_rate;
  187. return 0;
  188. }
  189. enum AVCodecID ff_wav_codec_get_id(unsigned int tag, int bps)
  190. {
  191. enum AVCodecID id;
  192. id = ff_codec_get_id(ff_codec_wav_tags, tag);
  193. if (id <= 0)
  194. return id;
  195. if (id == AV_CODEC_ID_PCM_S16LE)
  196. id = ff_get_pcm_codec_id(bps, 0, 0, ~1);
  197. else if (id == AV_CODEC_ID_PCM_F32LE)
  198. id = ff_get_pcm_codec_id(bps, 1, 0, 0);
  199. if (id == AV_CODEC_ID_ADPCM_IMA_WAV && bps == 8)
  200. id = AV_CODEC_ID_PCM_ZORK;
  201. return id;
  202. }
  203. int ff_get_bmp_header(AVIOContext *pb, AVStream *st, unsigned *esize)
  204. {
  205. int tag1;
  206. if(esize) *esize = avio_rl32(pb);
  207. else avio_rl32(pb);
  208. st->codec->width = avio_rl32(pb);
  209. st->codec->height = (int32_t)avio_rl32(pb);
  210. avio_rl16(pb); /* planes */
  211. st->codec->bits_per_coded_sample = avio_rl16(pb); /* depth */
  212. tag1 = avio_rl32(pb);
  213. avio_rl32(pb); /* ImageSize */
  214. avio_rl32(pb); /* XPelsPerMeter */
  215. avio_rl32(pb); /* YPelsPerMeter */
  216. avio_rl32(pb); /* ClrUsed */
  217. avio_rl32(pb); /* ClrImportant */
  218. return tag1;
  219. }
  220. int ff_read_riff_info(AVFormatContext *s, int64_t size)
  221. {
  222. int64_t start, end, cur;
  223. AVIOContext *pb = s->pb;
  224. start = avio_tell(pb);
  225. end = start + size;
  226. while ((cur = avio_tell(pb)) >= 0 &&
  227. cur <= end - 8 /* = tag + size */) {
  228. uint32_t chunk_code;
  229. int64_t chunk_size;
  230. char key[5] = { 0 };
  231. char *value;
  232. chunk_code = avio_rl32(pb);
  233. chunk_size = avio_rl32(pb);
  234. if (avio_feof(pb)) {
  235. if (chunk_code || chunk_size) {
  236. av_log(s, AV_LOG_WARNING, "INFO subchunk truncated\n");
  237. return AVERROR_INVALIDDATA;
  238. }
  239. return AVERROR_EOF;
  240. }
  241. if (chunk_size > end ||
  242. end - chunk_size < cur ||
  243. chunk_size == UINT_MAX) {
  244. avio_seek(pb, -9, SEEK_CUR);
  245. chunk_code = avio_rl32(pb);
  246. chunk_size = avio_rl32(pb);
  247. if (chunk_size > end || end - chunk_size < cur || chunk_size == UINT_MAX) {
  248. av_log(s, AV_LOG_WARNING, "too big INFO subchunk\n");
  249. return AVERROR_INVALIDDATA;
  250. }
  251. }
  252. chunk_size += (chunk_size & 1);
  253. if (!chunk_code) {
  254. if (chunk_size)
  255. avio_skip(pb, chunk_size);
  256. else if (pb->eof_reached) {
  257. av_log(s, AV_LOG_WARNING, "truncated file\n");
  258. return AVERROR_EOF;
  259. }
  260. continue;
  261. }
  262. value = av_mallocz(chunk_size + 1);
  263. if (!value) {
  264. av_log(s, AV_LOG_ERROR,
  265. "out of memory, unable to read INFO tag\n");
  266. return AVERROR(ENOMEM);
  267. }
  268. AV_WL32(key, chunk_code);
  269. if (avio_read(pb, value, chunk_size) != chunk_size) {
  270. av_log(s, AV_LOG_WARNING,
  271. "premature end of file while reading INFO tag\n");
  272. }
  273. av_dict_set(&s->metadata, key, value, AV_DICT_DONT_STRDUP_VAL);
  274. }
  275. return 0;
  276. }