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.

239 lines
8.4KB

  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. #include "libavcodec/bytestream.h"
  30. static int flac_read_header(AVFormatContext *s)
  31. {
  32. int ret, metadata_last=0, metadata_type, metadata_size, found_streaminfo=0;
  33. uint8_t header[4];
  34. uint8_t *buffer=NULL;
  35. AVStream *st = avformat_new_stream(s, NULL);
  36. if (!st)
  37. return AVERROR(ENOMEM);
  38. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  39. st->codec->codec_id = AV_CODEC_ID_FLAC;
  40. st->need_parsing = AVSTREAM_PARSE_FULL_RAW;
  41. /* the parameters will be extracted from the compressed bitstream */
  42. /* if fLaC marker is not found, assume there is no header */
  43. if (avio_rl32(s->pb) != MKTAG('f','L','a','C')) {
  44. avio_seek(s->pb, -4, SEEK_CUR);
  45. return 0;
  46. }
  47. /* process metadata blocks */
  48. while (!url_feof(s->pb) && !metadata_last) {
  49. avio_read(s->pb, header, 4);
  50. flac_parse_block_header(header, &metadata_last, &metadata_type,
  51. &metadata_size);
  52. switch (metadata_type) {
  53. /* allocate and read metadata block for supported types */
  54. case FLAC_METADATA_TYPE_STREAMINFO:
  55. case FLAC_METADATA_TYPE_CUESHEET:
  56. case FLAC_METADATA_TYPE_PICTURE:
  57. case FLAC_METADATA_TYPE_VORBIS_COMMENT:
  58. buffer = av_mallocz(metadata_size + FF_INPUT_BUFFER_PADDING_SIZE);
  59. if (!buffer) {
  60. return AVERROR(ENOMEM);
  61. }
  62. if (avio_read(s->pb, buffer, metadata_size) != metadata_size) {
  63. RETURN_ERROR(AVERROR(EIO));
  64. }
  65. break;
  66. /* skip metadata block for unsupported types */
  67. default:
  68. ret = avio_skip(s->pb, metadata_size);
  69. if (ret < 0)
  70. return ret;
  71. }
  72. if (metadata_type == FLAC_METADATA_TYPE_STREAMINFO) {
  73. FLACStreaminfo si;
  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 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_ERROR(AVERROR_INVALIDDATA);
  100. offset = buffer + 395;
  101. chapters = bytestream_get_byte(&offset) - 1;
  102. if (chapters <= 0)
  103. RETURN_ERROR(AVERROR_INVALIDDATA);
  104. for (i = 0; i < chapters; i++) {
  105. if (offset + 36 - buffer > metadata_size)
  106. RETURN_ERROR(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_ERROR(AVERROR_INVALIDDATA);
  114. offset += ti * 12;
  115. avpriv_new_chapter(s, track, st->time_base, start, AV_NOPTS_VALUE, isrc);
  116. }
  117. av_freep(&buffer);
  118. } else if (metadata_type == FLAC_METADATA_TYPE_PICTURE) {
  119. ret = ff_flac_parse_picture(s, buffer, metadata_size);
  120. av_freep(&buffer);
  121. if (ret < 0) {
  122. av_log(s, AV_LOG_ERROR, "Error parsing attached picture.\n");
  123. return ret;
  124. }
  125. } else {
  126. /* STREAMINFO must be the first block */
  127. if (!found_streaminfo) {
  128. RETURN_ERROR(AVERROR_INVALIDDATA);
  129. }
  130. /* process supported blocks other than STREAMINFO */
  131. if (metadata_type == FLAC_METADATA_TYPE_VORBIS_COMMENT) {
  132. AVDictionaryEntry *chmask;
  133. if (ff_vorbis_comment(s, &s->metadata, buffer, metadata_size, 1)) {
  134. av_log(s, AV_LOG_WARNING, "error parsing VorbisComment metadata\n");
  135. }
  136. /* parse the channels mask if present */
  137. chmask = av_dict_get(s->metadata, "WAVEFORMATEXTENSIBLE_CHANNEL_MASK", NULL, 0);
  138. if (chmask) {
  139. uint64_t mask = strtol(chmask->value, NULL, 0);
  140. if (!mask || mask & ~0x3ffffULL) {
  141. av_log(s, AV_LOG_WARNING,
  142. "Invalid value of WAVEFORMATEXTENSIBLE_CHANNEL_MASK\n");
  143. } else {
  144. st->codec->channel_layout = mask;
  145. av_dict_set(&s->metadata, "WAVEFORMATEXTENSIBLE_CHANNEL_MASK", NULL, 0);
  146. }
  147. }
  148. }
  149. av_freep(&buffer);
  150. }
  151. }
  152. ret = ff_replaygain_export(st, s->metadata);
  153. if (ret < 0)
  154. return ret;
  155. return 0;
  156. fail:
  157. av_free(buffer);
  158. return ret;
  159. }
  160. static int flac_probe(AVProbeData *p)
  161. {
  162. if (p->buf_size < 4 || memcmp(p->buf, "fLaC", 4))
  163. return 0;
  164. return AVPROBE_SCORE_EXTENSION;
  165. }
  166. static av_unused int64_t flac_read_timestamp(AVFormatContext *s, int stream_index,
  167. int64_t *ppos, int64_t pos_limit)
  168. {
  169. AVPacket pkt, out_pkt;
  170. AVStream *st = s->streams[stream_index];
  171. AVCodecParserContext *parser;
  172. int ret;
  173. int64_t pts = AV_NOPTS_VALUE;
  174. if (avio_seek(s->pb, *ppos, SEEK_SET) < 0)
  175. return AV_NOPTS_VALUE;
  176. av_init_packet(&pkt);
  177. parser = av_parser_init(st->codec->codec_id);
  178. if (!parser){
  179. return AV_NOPTS_VALUE;
  180. }
  181. parser->flags |= PARSER_FLAG_USE_CODEC_TS;
  182. for (;;){
  183. ret = ff_raw_read_partial_packet(s, &pkt);
  184. if (ret < 0){
  185. if (ret == AVERROR(EAGAIN))
  186. continue;
  187. else
  188. break;
  189. }
  190. av_init_packet(&out_pkt);
  191. ret = av_parser_parse2(parser, st->codec,
  192. &out_pkt.data, &out_pkt.size, pkt.data, pkt.size,
  193. pkt.pts, pkt.dts, *ppos);
  194. av_free_packet(&pkt);
  195. if (out_pkt.size){
  196. int size = out_pkt.size;
  197. if (parser->pts != AV_NOPTS_VALUE){
  198. // seeking may not have started from beginning of a frame
  199. // calculate frame start position from next frame backwards
  200. *ppos = parser->next_frame_offset - size;
  201. pts = parser->pts;
  202. break;
  203. }
  204. }
  205. }
  206. av_parser_close(parser);
  207. return pts;
  208. }
  209. AVInputFormat ff_flac_demuxer = {
  210. .name = "flac",
  211. .long_name = NULL_IF_CONFIG_SMALL("raw FLAC"),
  212. .read_probe = flac_probe,
  213. .read_header = flac_read_header,
  214. .read_packet = ff_raw_read_partial_packet,
  215. .read_timestamp = flac_read_timestamp,
  216. .flags = AVFMT_GENERIC_INDEX,
  217. .extensions = "flac",
  218. .raw_codec_id = AV_CODEC_ID_FLAC,
  219. };