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.

242 lines
8.5KB

  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 (!avio_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. ret = ff_vorbis_comment(s, &s->metadata, buffer, metadata_size, 1);
  134. if (ret < 0) {
  135. av_log(s, AV_LOG_WARNING, "error parsing VorbisComment metadata\n");
  136. } else if (ret > 0) {
  137. s->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED;
  138. }
  139. /* parse the channels mask if present */
  140. chmask = av_dict_get(s->metadata, "WAVEFORMATEXTENSIBLE_CHANNEL_MASK", NULL, 0);
  141. if (chmask) {
  142. uint64_t mask = strtol(chmask->value, NULL, 0);
  143. if (!mask || mask & ~0x3ffffULL) {
  144. av_log(s, AV_LOG_WARNING,
  145. "Invalid value of WAVEFORMATEXTENSIBLE_CHANNEL_MASK\n");
  146. } else {
  147. st->codec->channel_layout = mask;
  148. av_dict_set(&s->metadata, "WAVEFORMATEXTENSIBLE_CHANNEL_MASK", NULL, 0);
  149. }
  150. }
  151. }
  152. av_freep(&buffer);
  153. }
  154. }
  155. ret = ff_replaygain_export(st, s->metadata);
  156. if (ret < 0)
  157. return ret;
  158. return 0;
  159. fail:
  160. av_free(buffer);
  161. return ret;
  162. }
  163. static int flac_probe(AVProbeData *p)
  164. {
  165. if (p->buf_size < 4 || memcmp(p->buf, "fLaC", 4))
  166. return 0;
  167. return AVPROBE_SCORE_EXTENSION;
  168. }
  169. static av_unused int64_t flac_read_timestamp(AVFormatContext *s, int stream_index,
  170. int64_t *ppos, int64_t pos_limit)
  171. {
  172. AVPacket pkt, out_pkt;
  173. AVStream *st = s->streams[stream_index];
  174. AVCodecParserContext *parser;
  175. int ret;
  176. int64_t pts = AV_NOPTS_VALUE;
  177. if (avio_seek(s->pb, *ppos, SEEK_SET) < 0)
  178. return AV_NOPTS_VALUE;
  179. av_init_packet(&pkt);
  180. parser = av_parser_init(st->codec->codec_id);
  181. if (!parser){
  182. return AV_NOPTS_VALUE;
  183. }
  184. parser->flags |= PARSER_FLAG_USE_CODEC_TS;
  185. for (;;){
  186. ret = ff_raw_read_partial_packet(s, &pkt);
  187. if (ret < 0){
  188. if (ret == AVERROR(EAGAIN))
  189. continue;
  190. else
  191. break;
  192. }
  193. av_init_packet(&out_pkt);
  194. ret = av_parser_parse2(parser, st->codec,
  195. &out_pkt.data, &out_pkt.size, pkt.data, pkt.size,
  196. pkt.pts, pkt.dts, *ppos);
  197. av_free_packet(&pkt);
  198. if (out_pkt.size){
  199. int size = out_pkt.size;
  200. if (parser->pts != AV_NOPTS_VALUE){
  201. // seeking may not have started from beginning of a frame
  202. // calculate frame start position from next frame backwards
  203. *ppos = parser->next_frame_offset - size;
  204. pts = parser->pts;
  205. break;
  206. }
  207. }
  208. }
  209. av_parser_close(parser);
  210. return pts;
  211. }
  212. AVInputFormat ff_flac_demuxer = {
  213. .name = "flac",
  214. .long_name = NULL_IF_CONFIG_SMALL("raw FLAC"),
  215. .read_probe = flac_probe,
  216. .read_header = flac_read_header,
  217. .read_packet = ff_raw_read_partial_packet,
  218. .read_timestamp = flac_read_timestamp,
  219. .flags = AVFMT_GENERIC_INDEX,
  220. .extensions = "flac",
  221. .raw_codec_id = AV_CODEC_ID_FLAC,
  222. };