flacdec.c 4.5 KB

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