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.

162 lines
5.8KB

  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. #include "libavcodec/bytestream.h"
  28. static int flac_read_header(AVFormatContext *s)
  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_CUESHEET:
  54. case FLAC_METADATA_TYPE_VORBIS_COMMENT:
  55. buffer = av_mallocz(metadata_size + FF_INPUT_BUFFER_PADDING_SIZE);
  56. if (!buffer) {
  57. return AVERROR(ENOMEM);
  58. }
  59. if (avio_read(s->pb, buffer, metadata_size) != metadata_size) {
  60. av_freep(&buffer);
  61. return AVERROR(EIO);
  62. }
  63. break;
  64. /* skip metadata block for unsupported types */
  65. default:
  66. ret = avio_skip(s->pb, metadata_size);
  67. if (ret < 0)
  68. return ret;
  69. }
  70. if (metadata_type == FLAC_METADATA_TYPE_STREAMINFO) {
  71. FLACStreaminfo si;
  72. /* STREAMINFO can only occur once */
  73. if (found_streaminfo) {
  74. av_freep(&buffer);
  75. return AVERROR_INVALIDDATA;
  76. }
  77. if (metadata_size != FLAC_STREAMINFO_SIZE) {
  78. av_freep(&buffer);
  79. return AVERROR_INVALIDDATA;
  80. }
  81. found_streaminfo = 1;
  82. st->codec->extradata = buffer;
  83. st->codec->extradata_size = metadata_size;
  84. buffer = NULL;
  85. /* get codec params from STREAMINFO header */
  86. avpriv_flac_parse_streaminfo(st->codec, &si, st->codec->extradata);
  87. /* set time base and duration */
  88. if (si.samplerate > 0) {
  89. avpriv_set_pts_info(st, 64, 1, si.samplerate);
  90. if (si.samples > 0)
  91. st->duration = si.samples;
  92. }
  93. } else if (metadata_type == FLAC_METADATA_TYPE_CUESHEET) {
  94. uint8_t isrc[13];
  95. uint64_t start;
  96. const uint8_t *offset;
  97. int i, chapters, track, ti;
  98. if (metadata_size < 431)
  99. return AVERROR_INVALIDDATA;
  100. offset = buffer + 395;
  101. chapters = bytestream_get_byte(&offset) - 1;
  102. if (chapters <= 0)
  103. return AVERROR_INVALIDDATA;
  104. for (i = 0; i < chapters; i++) {
  105. if (offset + 36 - buffer > metadata_size)
  106. return AVERROR_INVALIDDATA;
  107. start = bytestream_get_be64(&offset);
  108. track = bytestream_get_byte(&offset);
  109. bytestream_get_buffer(&offset, isrc, 12);
  110. isrc[12] = 0;
  111. offset += 14;
  112. ti = bytestream_get_byte(&offset);
  113. if (ti <= 0) return AVERROR_INVALIDDATA;
  114. offset += ti * 12;
  115. avpriv_new_chapter(s, track, st->time_base, start, AV_NOPTS_VALUE, isrc);
  116. }
  117. } else {
  118. /* STREAMINFO must be the first block */
  119. if (!found_streaminfo) {
  120. av_freep(&buffer);
  121. return AVERROR_INVALIDDATA;
  122. }
  123. /* process supported blocks other than STREAMINFO */
  124. if (metadata_type == FLAC_METADATA_TYPE_VORBIS_COMMENT) {
  125. if (ff_vorbis_comment(s, &s->metadata, buffer, metadata_size)) {
  126. av_log(s, AV_LOG_WARNING, "error parsing VorbisComment metadata\n");
  127. }
  128. }
  129. av_freep(&buffer);
  130. }
  131. }
  132. return 0;
  133. }
  134. static int flac_probe(AVProbeData *p)
  135. {
  136. uint8_t *bufptr = p->buf;
  137. uint8_t *end = p->buf + p->buf_size;
  138. if(bufptr > end-4 || memcmp(bufptr, "fLaC", 4)) return 0;
  139. else return AVPROBE_SCORE_MAX/2;
  140. }
  141. AVInputFormat ff_flac_demuxer = {
  142. .name = "flac",
  143. .long_name = NULL_IF_CONFIG_SMALL("raw FLAC"),
  144. .read_probe = flac_probe,
  145. .read_header = flac_read_header,
  146. .read_packet = ff_raw_read_partial_packet,
  147. .flags = AVFMT_GENERIC_INDEX,
  148. .extensions = "flac",
  149. .raw_codec_id = CODEC_ID_FLAC,
  150. };