flacdec.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. AVFormatParameters *ap)
  30. {
  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 = avformat_new_stream(s, NULL);
  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. /* if fLaC marker is not found, assume there is no header */
  42. if (avio_rl32(s->pb) != MKTAG('f','L','a','C')) {
  43. avio_seek(s->pb, -4, SEEK_CUR);
  44. return 0;
  45. }
  46. /* process metadata blocks */
  47. while (!url_feof(s->pb) && !metadata_last) {
  48. avio_read(s->pb, header, 4);
  49. avpriv_flac_parse_block_header(header, &metadata_last, &metadata_type,
  50. &metadata_size);
  51. switch (metadata_type) {
  52. /* allocate and read metadata block for supported types */
  53. case FLAC_METADATA_TYPE_STREAMINFO:
  54. case FLAC_METADATA_TYPE_CUESHEET:
  55. case FLAC_METADATA_TYPE_VORBIS_COMMENT:
  56. buffer = av_mallocz(metadata_size + FF_INPUT_BUFFER_PADDING_SIZE);
  57. if (!buffer) {
  58. return AVERROR(ENOMEM);
  59. }
  60. if (avio_read(s->pb, buffer, metadata_size) != metadata_size) {
  61. av_freep(&buffer);
  62. return AVERROR(EIO);
  63. }
  64. break;
  65. /* skip metadata block for unsupported types */
  66. default:
  67. ret = avio_skip(s->pb, metadata_size);
  68. if (ret < 0)
  69. return ret;
  70. }
  71. if (metadata_type == FLAC_METADATA_TYPE_STREAMINFO) {
  72. FLACStreaminfo si;
  73. /* STREAMINFO can only occur once */
  74. if (found_streaminfo) {
  75. av_freep(&buffer);
  76. return AVERROR_INVALIDDATA;
  77. }
  78. if (metadata_size != FLAC_STREAMINFO_SIZE) {
  79. av_freep(&buffer);
  80. return AVERROR_INVALIDDATA;
  81. }
  82. found_streaminfo = 1;
  83. st->codec->extradata = buffer;
  84. st->codec->extradata_size = metadata_size;
  85. buffer = NULL;
  86. /* get codec params from STREAMINFO header */
  87. avpriv_flac_parse_streaminfo(st->codec, &si, st->codec->extradata);
  88. /* set time base and duration */
  89. if (si.samplerate > 0) {
  90. avpriv_set_pts_info(st, 64, 1, si.samplerate);
  91. if (si.samples > 0)
  92. st->duration = si.samples;
  93. }
  94. } else if (metadata_type == FLAC_METADATA_TYPE_CUESHEET) {
  95. uint8_t isrc[13];
  96. uint64_t start;
  97. const uint8_t *offset;
  98. int i, chapters, track, ti;
  99. if (metadata_size < 431)
  100. return AVERROR_INVALIDDATA;
  101. offset = buffer + 395;
  102. chapters = bytestream_get_byte(&offset) - 1;
  103. if (chapters <= 0)
  104. return AVERROR_INVALIDDATA;
  105. for (i = 0; i < chapters; i++) {
  106. if (offset + 36 - buffer > metadata_size)
  107. return AVERROR_INVALIDDATA;
  108. start = bytestream_get_be64(&offset);
  109. track = bytestream_get_byte(&offset);
  110. bytestream_get_buffer(&offset, isrc, 12);
  111. isrc[12] = 0;
  112. offset += 14;
  113. ti = bytestream_get_byte(&offset);
  114. if (ti <= 0) return AVERROR_INVALIDDATA;
  115. offset += ti * 12;
  116. avpriv_new_chapter(s, track, st->time_base, start, AV_NOPTS_VALUE, isrc);
  117. }
  118. } else {
  119. /* STREAMINFO must be the first block */
  120. if (!found_streaminfo) {
  121. av_freep(&buffer);
  122. return AVERROR_INVALIDDATA;
  123. }
  124. /* process supported blocks other than STREAMINFO */
  125. if (metadata_type == FLAC_METADATA_TYPE_VORBIS_COMMENT) {
  126. if (ff_vorbis_comment(s, &s->metadata, buffer, metadata_size)) {
  127. av_log(s, AV_LOG_WARNING, "error parsing VorbisComment metadata\n");
  128. }
  129. }
  130. av_freep(&buffer);
  131. }
  132. }
  133. return 0;
  134. }
  135. static int flac_probe(AVProbeData *p)
  136. {
  137. if (p->buf_size < 4 || memcmp(p->buf, "fLaC", 4))
  138. return 0;
  139. 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. .value = CODEC_ID_FLAC,
  150. };