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.

250 lines
8.3KB

  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. const AVCodecGuid ff_codec_wav_guids[] = {
  31. { AV_CODEC_ID_AC3, { 0x2C, 0x80, 0x6D, 0xE0, 0x46, 0xDB, 0xCF, 0x11, 0xB4, 0xD1, 0x00, 0x80, 0x5F, 0x6C, 0xBB, 0xEA } },
  32. { AV_CODEC_ID_ATRAC3P, { 0xBF, 0xAA, 0x23, 0xE9, 0x58, 0xCB, 0x71, 0x44, 0xA1, 0x19, 0xFF, 0xFA, 0x01, 0xE4, 0xCE, 0x62 } },
  33. { AV_CODEC_ID_EAC3, { 0xAF, 0x87, 0xFB, 0xA7, 0x02, 0x2D, 0xFB, 0x42, 0xA4, 0xD4, 0x05, 0xCD, 0x93, 0x84, 0x3B, 0xDD } },
  34. { AV_CODEC_ID_MP2, { 0x2B, 0x80, 0x6D, 0xE0, 0x46, 0xDB, 0xCF, 0x11, 0xB4, 0xD1, 0x00, 0x80, 0x5F, 0x6C, 0xBB, 0xEA } },
  35. { AV_CODEC_ID_NONE }
  36. };
  37. void ff_get_guid(AVIOContext *s, ff_asf_guid *g)
  38. {
  39. av_assert0(sizeof(*g) == 16); //compiler will optimize this out
  40. if (avio_read(s, *g, sizeof(*g)) < (int)sizeof(*g))
  41. memset(*g, 0, sizeof(*g));
  42. }
  43. enum AVCodecID ff_codec_guid_get_id(const AVCodecGuid *guids, ff_asf_guid guid)
  44. {
  45. int i;
  46. for (i = 0; guids[i].id != AV_CODEC_ID_NONE; i++)
  47. if (!ff_guidcmp(guids[i].guid, guid))
  48. return guids[i].id;
  49. return AV_CODEC_ID_NONE;
  50. }
  51. /* We could be given one of the three possible structures here:
  52. * WAVEFORMAT, PCMWAVEFORMAT or WAVEFORMATEX. Each structure
  53. * is an expansion of the previous one with the fields added
  54. * at the bottom. PCMWAVEFORMAT adds 'WORD wBitsPerSample' and
  55. * WAVEFORMATEX adds 'WORD cbSize' and basically makes itself
  56. * an openended structure.
  57. */
  58. static void parse_waveformatex(AVIOContext *pb, AVCodecContext *c)
  59. {
  60. ff_asf_guid subformat;
  61. int bps = avio_rl16(pb);
  62. if (bps)
  63. c->bits_per_coded_sample = bps;
  64. c->channel_layout = avio_rl32(pb); /* dwChannelMask */
  65. ff_get_guid(pb, &subformat);
  66. if (!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. int ff_get_wav_header(AVIOContext *pb, AVCodecContext *codec, int size)
  80. {
  81. int id;
  82. id = avio_rl16(pb);
  83. codec->codec_type = AVMEDIA_TYPE_AUDIO;
  84. codec->channels = avio_rl16(pb);
  85. codec->sample_rate = avio_rl32(pb);
  86. codec->bit_rate = avio_rl32(pb) * 8;
  87. codec->block_align = avio_rl16(pb);
  88. if (size == 14) { /* We're dealing with plain vanilla WAVEFORMAT */
  89. codec->bits_per_coded_sample = 8;
  90. } else
  91. codec->bits_per_coded_sample = avio_rl16(pb);
  92. if (id == 0xFFFE) {
  93. codec->codec_tag = 0;
  94. } else {
  95. codec->codec_tag = id;
  96. codec->codec_id = ff_wav_codec_get_id(id,
  97. codec->bits_per_coded_sample);
  98. }
  99. if (size >= 18) { /* We're obviously dealing with WAVEFORMATEX */
  100. int cbSize = avio_rl16(pb); /* cbSize */
  101. size -= 18;
  102. cbSize = FFMIN(size, cbSize);
  103. if (cbSize >= 22 && id == 0xfffe) { /* WAVEFORMATEXTENSIBLE */
  104. parse_waveformatex(pb, codec);
  105. cbSize -= 22;
  106. size -= 22;
  107. }
  108. if (cbSize > 0) {
  109. av_free(codec->extradata);
  110. if (ff_alloc_extradata(codec, cbSize))
  111. return AVERROR(ENOMEM);
  112. avio_read(pb, codec->extradata, codec->extradata_size);
  113. size -= cbSize;
  114. }
  115. /* It is possible for the chunk to contain garbage at the end */
  116. if (size > 0)
  117. avio_skip(pb, size);
  118. }
  119. if (codec->sample_rate <= 0) {
  120. av_log(NULL, AV_LOG_ERROR,
  121. "Invalid sample rate: %d\n", codec->sample_rate);
  122. return AVERROR_INVALIDDATA;
  123. }
  124. if (codec->codec_id == AV_CODEC_ID_AAC_LATM) {
  125. /* Channels and sample_rate values are those prior to applying SBR
  126. * and/or PS. */
  127. codec->channels = 0;
  128. codec->sample_rate = 0;
  129. }
  130. /* override bits_per_coded_sample for G.726 */
  131. if (codec->codec_id == AV_CODEC_ID_ADPCM_G726 && codec->sample_rate)
  132. codec->bits_per_coded_sample = codec->bit_rate / codec->sample_rate;
  133. return 0;
  134. }
  135. enum AVCodecID ff_wav_codec_get_id(unsigned int tag, int bps)
  136. {
  137. enum AVCodecID id;
  138. id = ff_codec_get_id(ff_codec_wav_tags, tag);
  139. if (id <= 0)
  140. return id;
  141. if (id == AV_CODEC_ID_PCM_S16LE)
  142. id = ff_get_pcm_codec_id(bps, 0, 0, ~1);
  143. else if (id == AV_CODEC_ID_PCM_F32LE)
  144. id = ff_get_pcm_codec_id(bps, 1, 0, 0);
  145. if (id == AV_CODEC_ID_ADPCM_IMA_WAV && bps == 8)
  146. id = AV_CODEC_ID_PCM_ZORK;
  147. return id;
  148. }
  149. int ff_get_bmp_header(AVIOContext *pb, AVStream *st, unsigned *esize)
  150. {
  151. int tag1;
  152. if(esize) *esize = avio_rl32(pb);
  153. else avio_rl32(pb);
  154. st->codec->width = avio_rl32(pb);
  155. st->codec->height = (int32_t)avio_rl32(pb);
  156. avio_rl16(pb); /* planes */
  157. st->codec->bits_per_coded_sample = avio_rl16(pb); /* depth */
  158. tag1 = avio_rl32(pb);
  159. avio_rl32(pb); /* ImageSize */
  160. avio_rl32(pb); /* XPelsPerMeter */
  161. avio_rl32(pb); /* YPelsPerMeter */
  162. avio_rl32(pb); /* ClrUsed */
  163. avio_rl32(pb); /* ClrImportant */
  164. return tag1;
  165. }
  166. int ff_read_riff_info(AVFormatContext *s, int64_t size)
  167. {
  168. int64_t start, end, cur;
  169. AVIOContext *pb = s->pb;
  170. start = avio_tell(pb);
  171. end = start + size;
  172. while ((cur = avio_tell(pb)) >= 0 &&
  173. cur <= end - 8 /* = tag + size */) {
  174. uint32_t chunk_code;
  175. int64_t chunk_size;
  176. char key[5] = { 0 };
  177. char *value;
  178. chunk_code = avio_rl32(pb);
  179. chunk_size = avio_rl32(pb);
  180. if (url_feof(pb)) {
  181. if (chunk_code || chunk_size) {
  182. av_log(s, AV_LOG_WARNING, "INFO subchunk truncated\n");
  183. return AVERROR_INVALIDDATA;
  184. }
  185. return AVERROR_EOF;
  186. }
  187. if (chunk_size > end ||
  188. end - chunk_size < cur ||
  189. chunk_size == UINT_MAX) {
  190. avio_seek(pb, -9, SEEK_CUR);
  191. chunk_code = avio_rl32(pb);
  192. chunk_size = avio_rl32(pb);
  193. if (chunk_size > end || end - chunk_size < cur || chunk_size == UINT_MAX) {
  194. av_log(s, AV_LOG_WARNING, "too big INFO subchunk\n");
  195. return AVERROR_INVALIDDATA;
  196. }
  197. }
  198. chunk_size += (chunk_size & 1);
  199. if (!chunk_code) {
  200. if (chunk_size)
  201. avio_skip(pb, chunk_size);
  202. else if (pb->eof_reached) {
  203. av_log(s, AV_LOG_WARNING, "truncated file\n");
  204. return AVERROR_EOF;
  205. }
  206. continue;
  207. }
  208. value = av_mallocz(chunk_size + 1);
  209. if (!value) {
  210. av_log(s, AV_LOG_ERROR,
  211. "out of memory, unable to read INFO tag\n");
  212. return AVERROR(ENOMEM);
  213. }
  214. AV_WL32(key, chunk_code);
  215. if (avio_read(pb, value, chunk_size) != chunk_size) {
  216. av_log(s, AV_LOG_WARNING,
  217. "premature end of file while reading INFO tag\n");
  218. }
  219. av_dict_set(&s->metadata, key, value, AV_DICT_DONT_STRDUP_VAL);
  220. }
  221. return 0;
  222. }