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.

245 lines
8.6KB

  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 "flac_picture.h"
  24. #include "internal.h"
  25. #include "rawdec.h"
  26. #include "oggdec.h"
  27. #include "vorbiscomment.h"
  28. #include "replaygain.h"
  29. static int flac_read_header(AVFormatContext *s)
  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 = AV_CODEC_ID_FLAC;
  39. st->need_parsing = AVSTREAM_PARSE_FULL_RAW;
  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 (!avio_feof(s->pb) && !metadata_last) {
  48. avio_read(s->pb, header, 4);
  49. 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_PICTURE:
  56. case FLAC_METADATA_TYPE_VORBIS_COMMENT:
  57. buffer = av_mallocz(metadata_size + FF_INPUT_BUFFER_PADDING_SIZE);
  58. if (!buffer) {
  59. return AVERROR(ENOMEM);
  60. }
  61. if (avio_read(s->pb, buffer, metadata_size) != metadata_size) {
  62. RETURN_ERROR(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. uint32_t samplerate;
  73. uint64_t samples;
  74. /* STREAMINFO can only occur once */
  75. if (found_streaminfo) {
  76. RETURN_ERROR(AVERROR_INVALIDDATA);
  77. }
  78. if (metadata_size != FLAC_STREAMINFO_SIZE) {
  79. RETURN_ERROR(AVERROR_INVALIDDATA);
  80. }
  81. found_streaminfo = 1;
  82. st->codec->extradata = buffer;
  83. st->codec->extradata_size = metadata_size;
  84. buffer = NULL;
  85. /* get sample rate and sample count from STREAMINFO header;
  86. * other parameters will be extracted by the parser */
  87. samplerate = AV_RB24(st->codec->extradata + 10) >> 4;
  88. samples = (AV_RB64(st->codec->extradata + 13) >> 24) & ((1ULL << 36) - 1);
  89. /* set time base and duration */
  90. if (samplerate > 0) {
  91. avpriv_set_pts_info(st, 64, 1, samplerate);
  92. if (samples > 0)
  93. st->duration = samples;
  94. }
  95. } else if (metadata_type == FLAC_METADATA_TYPE_CUESHEET) {
  96. uint8_t isrc[13];
  97. uint64_t start;
  98. const uint8_t *offset;
  99. int i, chapters, track, ti;
  100. if (metadata_size < 431)
  101. RETURN_ERROR(AVERROR_INVALIDDATA);
  102. offset = buffer + 395;
  103. chapters = bytestream_get_byte(&offset) - 1;
  104. if (chapters <= 0)
  105. RETURN_ERROR(AVERROR_INVALIDDATA);
  106. for (i = 0; i < chapters; i++) {
  107. if (offset + 36 - buffer > metadata_size)
  108. RETURN_ERROR(AVERROR_INVALIDDATA);
  109. start = bytestream_get_be64(&offset);
  110. track = bytestream_get_byte(&offset);
  111. bytestream_get_buffer(&offset, isrc, 12);
  112. isrc[12] = 0;
  113. offset += 14;
  114. ti = bytestream_get_byte(&offset);
  115. if (ti <= 0) RETURN_ERROR(AVERROR_INVALIDDATA);
  116. offset += ti * 12;
  117. avpriv_new_chapter(s, track, st->time_base, start, AV_NOPTS_VALUE, isrc);
  118. }
  119. av_freep(&buffer);
  120. } else if (metadata_type == FLAC_METADATA_TYPE_PICTURE) {
  121. ret = ff_flac_parse_picture(s, buffer, metadata_size);
  122. av_freep(&buffer);
  123. if (ret < 0) {
  124. av_log(s, AV_LOG_ERROR, "Error parsing attached picture.\n");
  125. return ret;
  126. }
  127. } else {
  128. /* STREAMINFO must be the first block */
  129. if (!found_streaminfo) {
  130. RETURN_ERROR(AVERROR_INVALIDDATA);
  131. }
  132. /* process supported blocks other than STREAMINFO */
  133. if (metadata_type == FLAC_METADATA_TYPE_VORBIS_COMMENT) {
  134. AVDictionaryEntry *chmask;
  135. ret = ff_vorbis_comment(s, &s->metadata, buffer, metadata_size, 1);
  136. if (ret < 0) {
  137. av_log(s, AV_LOG_WARNING, "error parsing VorbisComment metadata\n");
  138. } else if (ret > 0) {
  139. s->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED;
  140. }
  141. /* parse the channels mask if present */
  142. chmask = av_dict_get(s->metadata, "WAVEFORMATEXTENSIBLE_CHANNEL_MASK", NULL, 0);
  143. if (chmask) {
  144. uint64_t mask = strtol(chmask->value, NULL, 0);
  145. if (!mask || mask & ~0x3ffffULL) {
  146. av_log(s, AV_LOG_WARNING,
  147. "Invalid value of WAVEFORMATEXTENSIBLE_CHANNEL_MASK\n");
  148. } else {
  149. st->codec->channel_layout = mask;
  150. av_dict_set(&s->metadata, "WAVEFORMATEXTENSIBLE_CHANNEL_MASK", NULL, 0);
  151. }
  152. }
  153. }
  154. av_freep(&buffer);
  155. }
  156. }
  157. ret = ff_replaygain_export(st, s->metadata);
  158. if (ret < 0)
  159. return ret;
  160. return 0;
  161. fail:
  162. av_free(buffer);
  163. return ret;
  164. }
  165. static int flac_probe(AVProbeData *p)
  166. {
  167. if (p->buf_size < 4 || memcmp(p->buf, "fLaC", 4))
  168. return 0;
  169. return AVPROBE_SCORE_EXTENSION;
  170. }
  171. static av_unused int64_t flac_read_timestamp(AVFormatContext *s, int stream_index,
  172. int64_t *ppos, int64_t pos_limit)
  173. {
  174. AVPacket pkt, out_pkt;
  175. AVStream *st = s->streams[stream_index];
  176. AVCodecParserContext *parser;
  177. int ret;
  178. int64_t pts = AV_NOPTS_VALUE;
  179. if (avio_seek(s->pb, *ppos, SEEK_SET) < 0)
  180. return AV_NOPTS_VALUE;
  181. av_init_packet(&pkt);
  182. parser = av_parser_init(st->codec->codec_id);
  183. if (!parser){
  184. return AV_NOPTS_VALUE;
  185. }
  186. parser->flags |= PARSER_FLAG_USE_CODEC_TS;
  187. for (;;){
  188. ret = ff_raw_read_partial_packet(s, &pkt);
  189. if (ret < 0){
  190. if (ret == AVERROR(EAGAIN))
  191. continue;
  192. else
  193. break;
  194. }
  195. av_init_packet(&out_pkt);
  196. ret = av_parser_parse2(parser, st->codec,
  197. &out_pkt.data, &out_pkt.size, pkt.data, pkt.size,
  198. pkt.pts, pkt.dts, *ppos);
  199. av_free_packet(&pkt);
  200. if (out_pkt.size){
  201. int size = out_pkt.size;
  202. if (parser->pts != AV_NOPTS_VALUE){
  203. // seeking may not have started from beginning of a frame
  204. // calculate frame start position from next frame backwards
  205. *ppos = parser->next_frame_offset - size;
  206. pts = parser->pts;
  207. break;
  208. }
  209. }
  210. }
  211. av_parser_close(parser);
  212. return pts;
  213. }
  214. AVInputFormat ff_flac_demuxer = {
  215. .name = "flac",
  216. .long_name = NULL_IF_CONFIG_SMALL("raw FLAC"),
  217. .read_probe = flac_probe,
  218. .read_header = flac_read_header,
  219. .read_packet = ff_raw_read_partial_packet,
  220. .read_timestamp = flac_read_timestamp,
  221. .flags = AVFMT_GENERIC_INDEX,
  222. .extensions = "flac",
  223. .raw_codec_id = AV_CODEC_ID_FLAC,
  224. };