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.

148 lines
4.9KB

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