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.

189 lines
6.9KB

  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. #include "libavcodec/bytestream.h"
  30. static int flac_read_header(AVFormatContext *s)
  31. {
  32. int ret, metadata_last=0, metadata_type, metadata_size, found_streaminfo=0;
  33. uint8_t header[4];
  34. uint8_t *buffer=NULL;
  35. AVStream *st = avformat_new_stream(s, NULL);
  36. if (!st)
  37. return AVERROR(ENOMEM);
  38. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  39. st->codec->codec_id = AV_CODEC_ID_FLAC;
  40. st->need_parsing = AVSTREAM_PARSE_FULL;
  41. /* the parameters will be extracted from the compressed bitstream */
  42. /* if fLaC marker is not found, assume there is no header */
  43. if (avio_rl32(s->pb) != MKTAG('f','L','a','C')) {
  44. avio_seek(s->pb, -4, SEEK_CUR);
  45. return 0;
  46. }
  47. /* process metadata blocks */
  48. while (!s->pb->eof_reached && !metadata_last) {
  49. avio_read(s->pb, header, 4);
  50. flac_parse_block_header(header, &metadata_last, &metadata_type,
  51. &metadata_size);
  52. switch (metadata_type) {
  53. /* allocate and read metadata block for supported types */
  54. case FLAC_METADATA_TYPE_STREAMINFO:
  55. case FLAC_METADATA_TYPE_CUESHEET:
  56. case FLAC_METADATA_TYPE_PICTURE:
  57. case FLAC_METADATA_TYPE_VORBIS_COMMENT:
  58. buffer = av_mallocz(metadata_size + FF_INPUT_BUFFER_PADDING_SIZE);
  59. if (!buffer) {
  60. return AVERROR(ENOMEM);
  61. }
  62. if (avio_read(s->pb, buffer, metadata_size) != metadata_size) {
  63. av_freep(&buffer);
  64. return AVERROR(EIO);
  65. }
  66. break;
  67. /* skip metadata block for unsupported types */
  68. default:
  69. ret = avio_skip(s->pb, metadata_size);
  70. if (ret < 0)
  71. return ret;
  72. }
  73. if (metadata_type == FLAC_METADATA_TYPE_STREAMINFO) {
  74. FLACStreaminfo si;
  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->codec->extradata = buffer;
  86. st->codec->extradata_size = metadata_size;
  87. buffer = NULL;
  88. /* get codec params from STREAMINFO header */
  89. avpriv_flac_parse_streaminfo(st->codec, &si, st->codec->extradata);
  90. /* set time base and duration */
  91. if (si.samplerate > 0) {
  92. avpriv_set_pts_info(st, 64, 1, si.samplerate);
  93. if (si.samples > 0)
  94. st->duration = si.samples;
  95. }
  96. } else if (metadata_type == FLAC_METADATA_TYPE_CUESHEET) {
  97. uint8_t isrc[13];
  98. uint64_t start;
  99. const uint8_t *offset;
  100. int i, chapters, track, ti;
  101. if (metadata_size < 431)
  102. return AVERROR_INVALIDDATA;
  103. offset = buffer + 395;
  104. chapters = bytestream_get_byte(&offset) - 1;
  105. if (chapters <= 0)
  106. return AVERROR_INVALIDDATA;
  107. for (i = 0; i < chapters; i++) {
  108. if (offset + 36 - buffer > metadata_size)
  109. return AVERROR_INVALIDDATA;
  110. start = bytestream_get_be64(&offset);
  111. track = bytestream_get_byte(&offset);
  112. bytestream_get_buffer(&offset, isrc, 12);
  113. isrc[12] = 0;
  114. offset += 14;
  115. ti = bytestream_get_byte(&offset);
  116. if (ti <= 0) return AVERROR_INVALIDDATA;
  117. offset += ti * 12;
  118. avpriv_new_chapter(s, track, st->time_base, start, AV_NOPTS_VALUE, isrc);
  119. }
  120. } else if (metadata_type == FLAC_METADATA_TYPE_PICTURE) {
  121. ret = ff_flac_parse_picture(s, buffer, metadata_size);
  122. av_freep(&buffer);
  123. if (ret < 0) {
  124. av_log(s, AV_LOG_ERROR, "Error parsing attached picture.\n");
  125. return ret;
  126. }
  127. } else {
  128. /* STREAMINFO must be the first block */
  129. if (!found_streaminfo) {
  130. av_freep(&buffer);
  131. return AVERROR_INVALIDDATA;
  132. }
  133. /* process supported blocks other than STREAMINFO */
  134. if (metadata_type == FLAC_METADATA_TYPE_VORBIS_COMMENT) {
  135. AVDictionaryEntry *chmask;
  136. if (ff_vorbis_comment(s, &s->metadata, buffer, metadata_size, 1)) {
  137. av_log(s, AV_LOG_WARNING, "error parsing VorbisComment metadata\n");
  138. }
  139. /* parse the channels mask if present */
  140. chmask = av_dict_get(s->metadata, "WAVEFORMATEXTENSIBLE_CHANNEL_MASK", NULL, 0);
  141. if (chmask) {
  142. uint64_t mask = strtol(chmask->value, NULL, 0);
  143. if (!mask || mask & ~0x3ffffULL) {
  144. av_log(s, AV_LOG_WARNING,
  145. "Invalid value of WAVEFORMATEXTENSIBLE_CHANNEL_MASK\n");
  146. } else {
  147. st->codec->channel_layout = mask;
  148. av_dict_set(&s->metadata, "WAVEFORMATEXTENSIBLE_CHANNEL_MASK", NULL, 0);
  149. }
  150. }
  151. }
  152. av_freep(&buffer);
  153. }
  154. }
  155. ret = ff_replaygain_export(st, s->metadata);
  156. if (ret < 0)
  157. return ret;
  158. return 0;
  159. }
  160. static int flac_probe(AVProbeData *p)
  161. {
  162. if (p->buf_size < 4 || memcmp(p->buf, "fLaC", 4))
  163. return 0;
  164. return AVPROBE_SCORE_EXTENSION;
  165. }
  166. AVInputFormat ff_flac_demuxer = {
  167. .name = "flac",
  168. .long_name = NULL_IF_CONFIG_SMALL("raw FLAC"),
  169. .read_probe = flac_probe,
  170. .read_header = flac_read_header,
  171. .read_packet = ff_raw_read_partial_packet,
  172. .flags = AVFMT_GENERIC_INDEX,
  173. .extensions = "flac",
  174. .raw_codec_id = AV_CODEC_ID_FLAC,
  175. };