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.

290 lines
9.2KB

  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;
  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. codec->channels = avio_rl16(pb);
  93. codec->sample_rate = avio_rl32(pb);
  94. bitrate = avio_rl32(pb) * 8LL;
  95. codec->block_align = avio_rl16(pb);
  96. } else {
  97. id = avio_rb16(pb);
  98. codec->channels = avio_rb16(pb);
  99. codec->sample_rate = avio_rb32(pb);
  100. bitrate = avio_rb32(pb) * 8LL;
  101. codec->block_align = avio_rb16(pb);
  102. }
  103. if (size == 14) { /* We're dealing with plain vanilla WAVEFORMAT */
  104. codec->bits_per_coded_sample = 8;
  105. } else {
  106. if (!big_endian) {
  107. codec->bits_per_coded_sample = avio_rl16(pb);
  108. } else {
  109. codec->bits_per_coded_sample = avio_rb16(pb);
  110. }
  111. }
  112. if (id == 0xFFFE) {
  113. codec->codec_tag = 0;
  114. } else {
  115. codec->codec_tag = id;
  116. codec->codec_id = ff_wav_codec_get_id(id,
  117. codec->bits_per_coded_sample);
  118. }
  119. if (size >= 18) { /* We're obviously dealing with WAVEFORMATEX */
  120. int cbSize = avio_rl16(pb); /* cbSize */
  121. if (big_endian) {
  122. avpriv_report_missing_feature(codec, "WAVEFORMATEX support for RIFX files\n");
  123. return AVERROR_PATCHWELCOME;
  124. }
  125. size -= 18;
  126. cbSize = FFMIN(size, cbSize);
  127. if (cbSize >= 22 && id == 0xfffe) { /* WAVEFORMATEXTENSIBLE */
  128. parse_waveformatex(pb, codec);
  129. cbSize -= 22;
  130. size -= 22;
  131. }
  132. if (cbSize > 0) {
  133. av_freep(&codec->extradata);
  134. if (ff_get_extradata(codec, pb, cbSize) < 0)
  135. return AVERROR(ENOMEM);
  136. size -= cbSize;
  137. }
  138. /* It is possible for the chunk to contain garbage at the end */
  139. if (size > 0)
  140. avio_skip(pb, size);
  141. }
  142. if (bitrate > INT_MAX) {
  143. if (s->error_recognition & AV_EF_EXPLODE) {
  144. av_log(s, AV_LOG_ERROR,
  145. "The bitrate %"PRIu64" is too large.\n",
  146. bitrate);
  147. return AVERROR_INVALIDDATA;
  148. } else {
  149. av_log(s, AV_LOG_WARNING,
  150. "The bitrate %"PRIu64" is too large, resetting to 0.",
  151. bitrate);
  152. codec->bit_rate = 0;
  153. }
  154. } else {
  155. codec->bit_rate = bitrate;
  156. }
  157. if (codec->sample_rate <= 0) {
  158. av_log(s, AV_LOG_ERROR,
  159. "Invalid sample rate: %d\n", codec->sample_rate);
  160. return AVERROR_INVALIDDATA;
  161. }
  162. if (codec->codec_id == AV_CODEC_ID_AAC_LATM) {
  163. /* Channels and sample_rate values are those prior to applying SBR
  164. * and/or PS. */
  165. codec->channels = 0;
  166. codec->sample_rate = 0;
  167. }
  168. /* override bits_per_coded_sample for G.726 */
  169. if (codec->codec_id == AV_CODEC_ID_ADPCM_G726 && codec->sample_rate)
  170. codec->bits_per_coded_sample = codec->bit_rate / codec->sample_rate;
  171. return 0;
  172. }
  173. enum AVCodecID ff_wav_codec_get_id(unsigned int tag, int bps)
  174. {
  175. enum AVCodecID id;
  176. id = ff_codec_get_id(ff_codec_wav_tags, tag);
  177. if (id <= 0)
  178. return id;
  179. if (id == AV_CODEC_ID_PCM_S16LE)
  180. id = ff_get_pcm_codec_id(bps, 0, 0, ~1);
  181. else if (id == AV_CODEC_ID_PCM_F32LE)
  182. id = ff_get_pcm_codec_id(bps, 1, 0, 0);
  183. if (id == AV_CODEC_ID_ADPCM_IMA_WAV && bps == 8)
  184. id = AV_CODEC_ID_PCM_ZORK;
  185. return id;
  186. }
  187. int ff_get_bmp_header(AVIOContext *pb, AVStream *st, unsigned *esize)
  188. {
  189. int tag1;
  190. if(esize) *esize = avio_rl32(pb);
  191. else avio_rl32(pb);
  192. st->codec->width = avio_rl32(pb);
  193. st->codec->height = (int32_t)avio_rl32(pb);
  194. avio_rl16(pb); /* planes */
  195. st->codec->bits_per_coded_sample = avio_rl16(pb); /* depth */
  196. tag1 = avio_rl32(pb);
  197. avio_rl32(pb); /* ImageSize */
  198. avio_rl32(pb); /* XPelsPerMeter */
  199. avio_rl32(pb); /* YPelsPerMeter */
  200. avio_rl32(pb); /* ClrUsed */
  201. avio_rl32(pb); /* ClrImportant */
  202. return tag1;
  203. }
  204. int ff_read_riff_info(AVFormatContext *s, int64_t size)
  205. {
  206. int64_t start, end, cur;
  207. AVIOContext *pb = s->pb;
  208. start = avio_tell(pb);
  209. end = start + size;
  210. while ((cur = avio_tell(pb)) >= 0 &&
  211. cur <= end - 8 /* = tag + size */) {
  212. uint32_t chunk_code;
  213. int64_t chunk_size;
  214. char key[5] = { 0 };
  215. char *value;
  216. chunk_code = avio_rl32(pb);
  217. chunk_size = avio_rl32(pb);
  218. if (avio_feof(pb)) {
  219. if (chunk_code || chunk_size) {
  220. av_log(s, AV_LOG_WARNING, "INFO subchunk truncated\n");
  221. return AVERROR_INVALIDDATA;
  222. }
  223. return AVERROR_EOF;
  224. }
  225. if (chunk_size > end ||
  226. end - chunk_size < cur ||
  227. chunk_size == UINT_MAX) {
  228. avio_seek(pb, -9, SEEK_CUR);
  229. chunk_code = avio_rl32(pb);
  230. chunk_size = avio_rl32(pb);
  231. if (chunk_size > end || end - chunk_size < cur || chunk_size == UINT_MAX) {
  232. av_log(s, AV_LOG_WARNING, "too big INFO subchunk\n");
  233. return AVERROR_INVALIDDATA;
  234. }
  235. }
  236. chunk_size += (chunk_size & 1);
  237. if (!chunk_code) {
  238. if (chunk_size)
  239. avio_skip(pb, chunk_size);
  240. else if (pb->eof_reached) {
  241. av_log(s, AV_LOG_WARNING, "truncated file\n");
  242. return AVERROR_EOF;
  243. }
  244. continue;
  245. }
  246. value = av_mallocz(chunk_size + 1);
  247. if (!value) {
  248. av_log(s, AV_LOG_ERROR,
  249. "out of memory, unable to read INFO tag\n");
  250. return AVERROR(ENOMEM);
  251. }
  252. AV_WL32(key, chunk_code);
  253. if (avio_read(pb, value, chunk_size) != chunk_size) {
  254. av_log(s, AV_LOG_WARNING,
  255. "premature end of file while reading INFO tag\n");
  256. }
  257. av_dict_set(&s->metadata, key, value, AV_DICT_DONT_STRDUP_VAL);
  258. }
  259. return 0;
  260. }