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.

137 lines
4.7KB

  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 "internal.h"
  24. #include "rawdec.h"
  25. #include "oggdec.h"
  26. #include "vorbiscomment.h"
  27. static int flac_read_header(AVFormatContext *s,
  28. AVFormatParameters *ap)
  29. {
  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 = avformat_new_stream(s, NULL);
  34. if (!st)
  35. return AVERROR(ENOMEM);
  36. st->codec->codec_type = AVMEDIA_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. /* if fLaC marker is not found, assume there is no header */
  41. if (avio_rl32(s->pb) != MKTAG('f','L','a','C')) {
  42. avio_seek(s->pb, -4, SEEK_CUR);
  43. return 0;
  44. }
  45. /* process metadata blocks */
  46. while (!url_feof(s->pb) && !metadata_last) {
  47. avio_read(s->pb, header, 4);
  48. avpriv_flac_parse_block_header(header, &metadata_last, &metadata_type,
  49. &metadata_size);
  50. switch (metadata_type) {
  51. /* allocate and read metadata block for supported types */
  52. case FLAC_METADATA_TYPE_STREAMINFO:
  53. case FLAC_METADATA_TYPE_VORBIS_COMMENT:
  54. buffer = av_mallocz(metadata_size + FF_INPUT_BUFFER_PADDING_SIZE);
  55. if (!buffer) {
  56. return AVERROR(ENOMEM);
  57. }
  58. if (avio_read(s->pb, buffer, metadata_size) != metadata_size) {
  59. av_freep(&buffer);
  60. return AVERROR(EIO);
  61. }
  62. break;
  63. /* skip metadata block for unsupported types */
  64. default:
  65. ret = avio_skip(s->pb, metadata_size);
  66. if (ret < 0)
  67. return ret;
  68. }
  69. if (metadata_type == FLAC_METADATA_TYPE_STREAMINFO) {
  70. FLACStreaminfo si;
  71. /* STREAMINFO can only occur once */
  72. if (found_streaminfo) {
  73. av_freep(&buffer);
  74. return AVERROR_INVALIDDATA;
  75. }
  76. if (metadata_size != FLAC_STREAMINFO_SIZE) {
  77. av_freep(&buffer);
  78. return AVERROR_INVALIDDATA;
  79. }
  80. found_streaminfo = 1;
  81. st->codec->extradata = buffer;
  82. st->codec->extradata_size = metadata_size;
  83. buffer = NULL;
  84. /* get codec params from STREAMINFO header */
  85. avpriv_flac_parse_streaminfo(st->codec, &si, st->codec->extradata);
  86. /* set time base and duration */
  87. if (si.samplerate > 0) {
  88. avpriv_set_pts_info(st, 64, 1, si.samplerate);
  89. if (si.samples > 0)
  90. st->duration = si.samples;
  91. }
  92. } else {
  93. /* STREAMINFO must be the first block */
  94. if (!found_streaminfo) {
  95. av_freep(&buffer);
  96. return AVERROR_INVALIDDATA;
  97. }
  98. /* process supported blocks other than STREAMINFO */
  99. if (metadata_type == FLAC_METADATA_TYPE_VORBIS_COMMENT) {
  100. if (ff_vorbis_comment(s, &s->metadata, buffer, metadata_size)) {
  101. av_log(s, AV_LOG_WARNING, "error parsing VorbisComment metadata\n");
  102. }
  103. }
  104. av_freep(&buffer);
  105. }
  106. }
  107. return 0;
  108. }
  109. static int flac_probe(AVProbeData *p)
  110. {
  111. uint8_t *bufptr = p->buf;
  112. uint8_t *end = p->buf + p->buf_size;
  113. if(bufptr > end-4 || memcmp(bufptr, "fLaC", 4)) return 0;
  114. else return AVPROBE_SCORE_MAX/2;
  115. }
  116. AVInputFormat ff_flac_demuxer = {
  117. .name = "flac",
  118. .long_name = NULL_IF_CONFIG_SMALL("raw FLAC"),
  119. .read_probe = flac_probe,
  120. .read_header = flac_read_header,
  121. .read_packet = ff_raw_read_partial_packet,
  122. .flags= AVFMT_GENERIC_INDEX,
  123. .extensions = "flac",
  124. .value = CODEC_ID_FLAC,
  125. };