flacdec.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. url_fseek(s->pb, -4, SEEK_CUR);
  51. return 0;
  52. }
  53. /* process metadata blocks */
  54. while (!url_feof(s->pb) && !metadata_last) {
  55. get_buffer(s->pb, header, 4);
  56. ff_flac_parse_block_header(header, &metadata_last, &metadata_type,
  57. &metadata_size);
  58. switch (metadata_type) {
  59. /* allocate and read metadata block for supported types */
  60. case FLAC_METADATA_TYPE_STREAMINFO:
  61. case FLAC_METADATA_TYPE_VORBIS_COMMENT:
  62. buffer = av_mallocz(metadata_size + FF_INPUT_BUFFER_PADDING_SIZE);
  63. if (!buffer) {
  64. return AVERROR_NOMEM;
  65. }
  66. if (get_buffer(s->pb, buffer, metadata_size) != metadata_size) {
  67. av_freep(&buffer);
  68. return AVERROR_IO;
  69. }
  70. break;
  71. /* skip metadata block for unsupported types */
  72. default:
  73. ret = url_fseek(s->pb, metadata_size, SEEK_CUR);
  74. if (ret < 0)
  75. return ret;
  76. }
  77. if (metadata_type == FLAC_METADATA_TYPE_STREAMINFO) {
  78. FLACStreaminfo si;
  79. /* STREAMINFO can only occur once */
  80. if (found_streaminfo) {
  81. av_freep(&buffer);
  82. return AVERROR_INVALIDDATA;
  83. }
  84. if (metadata_size != FLAC_STREAMINFO_SIZE) {
  85. av_freep(&buffer);
  86. return AVERROR_INVALIDDATA;
  87. }
  88. found_streaminfo = 1;
  89. st->codec->extradata = buffer;
  90. st->codec->extradata_size = metadata_size;
  91. buffer = NULL;
  92. /* get codec params from STREAMINFO header */
  93. ff_flac_parse_streaminfo(st->codec, &si, st->codec->extradata);
  94. /* set time base and duration */
  95. if (si.samplerate > 0) {
  96. av_set_pts_info(st, 64, 1, si.samplerate);
  97. if (si.samples > 0)
  98. st->duration = si.samples;
  99. }
  100. } else {
  101. /* STREAMINFO must be the first block */
  102. if (!found_streaminfo) {
  103. av_freep(&buffer);
  104. return AVERROR_INVALIDDATA;
  105. }
  106. /* process supported blocks other than STREAMINFO */
  107. if (metadata_type == FLAC_METADATA_TYPE_VORBIS_COMMENT) {
  108. if (vorbis_comment(s, buffer, metadata_size)) {
  109. av_log(s, AV_LOG_WARNING, "error parsing VorbisComment metadata\n");
  110. }
  111. }
  112. av_freep(&buffer);
  113. }
  114. }
  115. return 0;
  116. }
  117. static int flac_probe(AVProbeData *p)
  118. {
  119. uint8_t *bufptr = p->buf;
  120. uint8_t *end = p->buf + p->buf_size;
  121. if(ff_id3v2_match(bufptr))
  122. bufptr += ff_id3v2_tag_len(bufptr);
  123. if(bufptr > end-4 || memcmp(bufptr, "fLaC", 4)) return 0;
  124. else return AVPROBE_SCORE_MAX/2;
  125. }
  126. AVInputFormat flac_demuxer = {
  127. "flac",
  128. NULL_IF_CONFIG_SMALL("raw FLAC"),
  129. 0,
  130. flac_probe,
  131. flac_read_header,
  132. ff_raw_read_partial_packet,
  133. .flags= AVFMT_GENERIC_INDEX,
  134. .extensions = "flac",
  135. .value = CODEC_ID_FLAC,
  136. };