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.

210 lines
7.8KB

  1. /*
  2. * Raw FLAC demuxer
  3. * Copyright (c) 2001 Fabrice Bellard
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "libavcodec/flac.h"
  22. #include "avformat.h"
  23. #include "flac_picture.h"
  24. #include "internal.h"
  25. #include "rawdec.h"
  26. #include "oggdec.h"
  27. #include "vorbiscomment.h"
  28. #include "replaygain.h"
  29. static int flac_read_header(AVFormatContext *s)
  30. {
  31. int ret, metadata_last=0, metadata_type, metadata_size, found_streaminfo=0;
  32. uint8_t header[4];
  33. uint8_t *buffer=NULL;
  34. AVStream *st = avformat_new_stream(s, NULL);
  35. if (!st)
  36. return AVERROR(ENOMEM);
  37. st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
  38. st->codecpar->codec_id = AV_CODEC_ID_FLAC;
  39. st->need_parsing = AVSTREAM_PARSE_FULL;
  40. /* the parameters will be extracted from the compressed bitstream */
  41. /* if fLaC marker is not found, assume there is no header */
  42. if (avio_rl32(s->pb) != MKTAG('f','L','a','C')) {
  43. avio_seek(s->pb, -4, SEEK_CUR);
  44. return 0;
  45. }
  46. /* process metadata blocks */
  47. while (!s->pb->eof_reached && !metadata_last) {
  48. avio_read(s->pb, header, 4);
  49. flac_parse_block_header(header, &metadata_last, &metadata_type,
  50. &metadata_size);
  51. switch (metadata_type) {
  52. /* allocate and read metadata block for supported types */
  53. case FLAC_METADATA_TYPE_STREAMINFO:
  54. case FLAC_METADATA_TYPE_CUESHEET:
  55. case FLAC_METADATA_TYPE_PICTURE:
  56. case FLAC_METADATA_TYPE_VORBIS_COMMENT:
  57. buffer = av_mallocz(metadata_size + AV_INPUT_BUFFER_PADDING_SIZE);
  58. if (!buffer) {
  59. return AVERROR(ENOMEM);
  60. }
  61. if (avio_read(s->pb, buffer, metadata_size) != metadata_size) {
  62. av_freep(&buffer);
  63. return AVERROR(EIO);
  64. }
  65. break;
  66. /* skip metadata block for unsupported types */
  67. default:
  68. ret = avio_skip(s->pb, metadata_size);
  69. if (ret < 0)
  70. return ret;
  71. }
  72. if (metadata_type == FLAC_METADATA_TYPE_STREAMINFO) {
  73. uint32_t samplerate;
  74. uint64_t samples;
  75. /* STREAMINFO can only occur once */
  76. if (found_streaminfo) {
  77. av_freep(&buffer);
  78. return AVERROR_INVALIDDATA;
  79. }
  80. if (metadata_size != FLAC_STREAMINFO_SIZE) {
  81. av_freep(&buffer);
  82. return AVERROR_INVALIDDATA;
  83. }
  84. found_streaminfo = 1;
  85. st->codecpar->extradata = buffer;
  86. st->codecpar->extradata_size = metadata_size;
  87. buffer = NULL;
  88. /* get sample rate and sample count from STREAMINFO header;
  89. * other parameters will be extracted by the parser */
  90. samplerate = AV_RB24(st->codecpar->extradata + 10) >> 4;
  91. samples = (AV_RB64(st->codecpar->extradata + 13) >> 24) & ((1ULL << 36) - 1);
  92. /* set time base and duration */
  93. if (samplerate > 0) {
  94. avpriv_set_pts_info(st, 64, 1, samplerate);
  95. if (samples > 0)
  96. st->duration = samples;
  97. }
  98. } else if (metadata_type == FLAC_METADATA_TYPE_CUESHEET) {
  99. uint8_t isrc[13];
  100. uint64_t start;
  101. const uint8_t *offset;
  102. int i, chapters, track, ti;
  103. if (metadata_size < 431)
  104. return AVERROR_INVALIDDATA;
  105. offset = buffer + 395;
  106. chapters = bytestream_get_byte(&offset) - 1;
  107. if (chapters <= 0)
  108. return AVERROR_INVALIDDATA;
  109. for (i = 0; i < chapters; i++) {
  110. if (offset + 36 - buffer > metadata_size)
  111. return AVERROR_INVALIDDATA;
  112. start = bytestream_get_be64(&offset);
  113. track = bytestream_get_byte(&offset);
  114. bytestream_get_buffer(&offset, isrc, 12);
  115. isrc[12] = 0;
  116. offset += 14;
  117. ti = bytestream_get_byte(&offset);
  118. if (ti <= 0) return AVERROR_INVALIDDATA;
  119. offset += ti * 12;
  120. avpriv_new_chapter(s, track, st->time_base, start, AV_NOPTS_VALUE, isrc);
  121. }
  122. } else if (metadata_type == FLAC_METADATA_TYPE_PICTURE) {
  123. ret = ff_flac_parse_picture(s, buffer, metadata_size);
  124. av_freep(&buffer);
  125. if (ret < 0) {
  126. av_log(s, AV_LOG_ERROR, "Error parsing attached picture.\n");
  127. return ret;
  128. }
  129. } else {
  130. /* STREAMINFO must be the first block */
  131. if (!found_streaminfo) {
  132. av_freep(&buffer);
  133. return AVERROR_INVALIDDATA;
  134. }
  135. /* process supported blocks other than STREAMINFO */
  136. if (metadata_type == FLAC_METADATA_TYPE_VORBIS_COMMENT) {
  137. AVDictionaryEntry *chmask;
  138. ret = ff_vorbis_comment(s, &s->metadata, buffer, metadata_size, 1);
  139. if (ret < 0) {
  140. av_log(s, AV_LOG_WARNING, "error parsing VorbisComment metadata\n");
  141. } else if (ret > 0) {
  142. s->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED;
  143. }
  144. /* parse the channels mask if present */
  145. chmask = av_dict_get(s->metadata, "WAVEFORMATEXTENSIBLE_CHANNEL_MASK", NULL, 0);
  146. if (chmask) {
  147. uint64_t mask = strtol(chmask->value, NULL, 0);
  148. if (!mask || mask & ~0x3ffffULL) {
  149. av_log(s, AV_LOG_WARNING,
  150. "Invalid value of WAVEFORMATEXTENSIBLE_CHANNEL_MASK\n");
  151. } else {
  152. st->codecpar->channel_layout = mask;
  153. av_dict_set(&s->metadata, "WAVEFORMATEXTENSIBLE_CHANNEL_MASK", NULL, 0);
  154. }
  155. }
  156. }
  157. av_freep(&buffer);
  158. }
  159. }
  160. ret = ff_replaygain_export(st, s->metadata);
  161. if (ret < 0)
  162. return ret;
  163. return 0;
  164. }
  165. static int flac_probe(AVProbeData *p)
  166. {
  167. /* file header + metadata header + checked bytes of streaminfo */
  168. if (p->buf_size >= 4 + 4 + 13) {
  169. int type = p->buf[4] & 0x7f;
  170. int size = AV_RB24(p->buf + 5);
  171. int min_block_size = AV_RB16(p->buf + 8);
  172. int max_block_size = AV_RB16(p->buf + 10);
  173. int sample_rate = AV_RB24(p->buf + 18) >> 4;
  174. if (!memcmp(p->buf, "fLaC", 4) &&
  175. type == FLAC_METADATA_TYPE_STREAMINFO &&
  176. size == FLAC_STREAMINFO_SIZE &&
  177. min_block_size >= 16 &&
  178. max_block_size >= min_block_size &&
  179. sample_rate && sample_rate <= 655350)
  180. return AVPROBE_SCORE_MAX;
  181. }
  182. return 0;
  183. }
  184. AVInputFormat ff_flac_demuxer = {
  185. .name = "flac",
  186. .long_name = NULL_IF_CONFIG_SMALL("raw FLAC"),
  187. .read_probe = flac_probe,
  188. .read_header = flac_read_header,
  189. .read_packet = ff_raw_read_partial_packet,
  190. .flags = AVFMT_GENERIC_INDEX,
  191. .extensions = "flac",
  192. .raw_codec_id = AV_CODEC_ID_FLAC,
  193. };