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.

152 lines
5.1KB

  1. /*
  2. * Raw FLAC demuxer
  3. * Copyright (c) 2001 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 "libavcodec/flac.h"
  22. #include "avformat.h"
  23. #include "raw.h"
  24. #include "id3v2.h"
  25. #include "oggdec.h"
  26. #include "vorbiscomment.h"
  27. static int flac_read_header(AVFormatContext *s,
  28. AVFormatParameters *ap)
  29. {
  30. uint8_t buf[ID3v2_HEADER_SIZE];
  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 = av_new_stream(s, 0);
  35. if (!st)
  36. return AVERROR(ENOMEM);
  37. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  38. st->codec->codec_id = CODEC_ID_FLAC;
  39. st->need_parsing = AVSTREAM_PARSE_FULL;
  40. /* the parameters will be extracted from the compressed bitstream */
  41. /* skip ID3v2 header if found */
  42. ret = get_buffer(s->pb, buf, ID3v2_HEADER_SIZE);
  43. if (ret == ID3v2_HEADER_SIZE && ff_id3v2_match(buf, ID3v2_DEFAULT_MAGIC)) {
  44. int len = ff_id3v2_tag_len(buf);
  45. url_fseek(s->pb, len - ID3v2_HEADER_SIZE, SEEK_CUR);
  46. } else {
  47. url_fseek(s->pb, 0, SEEK_SET);
  48. }
  49. /* if fLaC marker is not found, assume there is no header */
  50. if (get_le32(s->pb) != MKTAG('f','L','a','C')) {
  51. url_fseek(s->pb, -4, SEEK_CUR);
  52. return 0;
  53. }
  54. /* process metadata blocks */
  55. while (!url_feof(s->pb) && !metadata_last) {
  56. get_buffer(s->pb, header, 4);
  57. ff_flac_parse_block_header(header, &metadata_last, &metadata_type,
  58. &metadata_size);
  59. switch (metadata_type) {
  60. /* allocate and read metadata block for supported types */
  61. case FLAC_METADATA_TYPE_STREAMINFO:
  62. case FLAC_METADATA_TYPE_VORBIS_COMMENT:
  63. buffer = av_mallocz(metadata_size + FF_INPUT_BUFFER_PADDING_SIZE);
  64. if (!buffer) {
  65. return AVERROR(ENOMEM);
  66. }
  67. if (get_buffer(s->pb, buffer, metadata_size) != metadata_size) {
  68. av_freep(&buffer);
  69. return AVERROR(EIO);
  70. }
  71. break;
  72. /* skip metadata block for unsupported types */
  73. default:
  74. ret = url_fseek(s->pb, metadata_size, SEEK_CUR);
  75. if (ret < 0)
  76. return ret;
  77. }
  78. if (metadata_type == FLAC_METADATA_TYPE_STREAMINFO) {
  79. FLACStreaminfo si;
  80. /* STREAMINFO can only occur once */
  81. if (found_streaminfo) {
  82. av_freep(&buffer);
  83. return AVERROR_INVALIDDATA;
  84. }
  85. if (metadata_size != FLAC_STREAMINFO_SIZE) {
  86. av_freep(&buffer);
  87. return AVERROR_INVALIDDATA;
  88. }
  89. found_streaminfo = 1;
  90. st->codec->extradata = buffer;
  91. st->codec->extradata_size = metadata_size;
  92. buffer = NULL;
  93. /* get codec params from STREAMINFO header */
  94. ff_flac_parse_streaminfo(st->codec, &si, st->codec->extradata);
  95. /* set time base and duration */
  96. if (si.samplerate > 0) {
  97. av_set_pts_info(st, 64, 1, si.samplerate);
  98. if (si.samples > 0)
  99. st->duration = si.samples;
  100. }
  101. } else {
  102. /* STREAMINFO must be the first block */
  103. if (!found_streaminfo) {
  104. av_freep(&buffer);
  105. return AVERROR_INVALIDDATA;
  106. }
  107. /* process supported blocks other than STREAMINFO */
  108. if (metadata_type == FLAC_METADATA_TYPE_VORBIS_COMMENT) {
  109. if (ff_vorbis_comment(s, &s->metadata, buffer, metadata_size)) {
  110. av_log(s, AV_LOG_WARNING, "error parsing VorbisComment metadata\n");
  111. }
  112. }
  113. av_freep(&buffer);
  114. }
  115. }
  116. return 0;
  117. }
  118. static int flac_probe(AVProbeData *p)
  119. {
  120. uint8_t *bufptr = p->buf;
  121. uint8_t *end = p->buf + p->buf_size;
  122. if(ff_id3v2_match(bufptr, ID3v2_DEFAULT_MAGIC))
  123. bufptr += ff_id3v2_tag_len(bufptr);
  124. if(bufptr > end-4 || memcmp(bufptr, "fLaC", 4)) return 0;
  125. else return AVPROBE_SCORE_MAX/2;
  126. }
  127. AVInputFormat flac_demuxer = {
  128. "flac",
  129. NULL_IF_CONFIG_SMALL("raw FLAC"),
  130. 0,
  131. flac_probe,
  132. flac_read_header,
  133. ff_raw_read_partial_packet,
  134. .flags= AVFMT_GENERIC_INDEX,
  135. .extensions = "flac",
  136. .value = CODEC_ID_FLAC,
  137. .metadata_conv = ff_vorbiscomment_metadata_conv,
  138. };