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.

343 lines
12KB

  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. #define SEEKPOINT_SIZE 18
  30. typedef struct FLACDecContext {
  31. int found_seektable;
  32. } FLACDecContext;
  33. static void reset_index_position(int64_t metadata_head_size, AVStream *st)
  34. {
  35. /* the real seek index offset should be the size of metadata blocks with the offset in the frame blocks */
  36. int i;
  37. for(i=0; i<st->nb_index_entries; i++) {
  38. st->index_entries[i].pos += metadata_head_size;
  39. }
  40. }
  41. static int flac_read_header(AVFormatContext *s)
  42. {
  43. int ret, metadata_last=0, metadata_type, metadata_size, found_streaminfo=0;
  44. uint8_t header[4];
  45. uint8_t *buffer=NULL;
  46. FLACDecContext *flac = s->priv_data;
  47. AVStream *st = avformat_new_stream(s, NULL);
  48. if (!st)
  49. return AVERROR(ENOMEM);
  50. st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
  51. st->codecpar->codec_id = AV_CODEC_ID_FLAC;
  52. st->need_parsing = AVSTREAM_PARSE_FULL_RAW;
  53. /* the parameters will be extracted from the compressed bitstream */
  54. /* if fLaC marker is not found, assume there is no header */
  55. if (avio_rl32(s->pb) != MKTAG('f','L','a','C')) {
  56. avio_seek(s->pb, -4, SEEK_CUR);
  57. return 0;
  58. }
  59. /* process metadata blocks */
  60. while (!avio_feof(s->pb) && !metadata_last) {
  61. if (avio_read(s->pb, header, 4) != 4)
  62. return AVERROR(AVERROR_INVALIDDATA);
  63. flac_parse_block_header(header, &metadata_last, &metadata_type,
  64. &metadata_size);
  65. switch (metadata_type) {
  66. /* allocate and read metadata block for supported types */
  67. case FLAC_METADATA_TYPE_STREAMINFO:
  68. case FLAC_METADATA_TYPE_CUESHEET:
  69. case FLAC_METADATA_TYPE_PICTURE:
  70. case FLAC_METADATA_TYPE_VORBIS_COMMENT:
  71. case FLAC_METADATA_TYPE_SEEKTABLE:
  72. buffer = av_mallocz(metadata_size + AV_INPUT_BUFFER_PADDING_SIZE);
  73. if (!buffer) {
  74. return AVERROR(ENOMEM);
  75. }
  76. if (avio_read(s->pb, buffer, metadata_size) != metadata_size) {
  77. RETURN_ERROR(AVERROR(EIO));
  78. }
  79. break;
  80. /* skip metadata block for unsupported types */
  81. default:
  82. ret = avio_skip(s->pb, metadata_size);
  83. if (ret < 0)
  84. return ret;
  85. }
  86. if (metadata_type == FLAC_METADATA_TYPE_STREAMINFO) {
  87. uint32_t samplerate;
  88. uint64_t samples;
  89. /* STREAMINFO can only occur once */
  90. if (found_streaminfo) {
  91. RETURN_ERROR(AVERROR_INVALIDDATA);
  92. }
  93. if (metadata_size != FLAC_STREAMINFO_SIZE) {
  94. RETURN_ERROR(AVERROR_INVALIDDATA);
  95. }
  96. found_streaminfo = 1;
  97. st->codecpar->extradata = buffer;
  98. st->codecpar->extradata_size = metadata_size;
  99. buffer = NULL;
  100. /* get sample rate and sample count from STREAMINFO header;
  101. * other parameters will be extracted by the parser */
  102. samplerate = AV_RB24(st->codecpar->extradata + 10) >> 4;
  103. samples = (AV_RB64(st->codecpar->extradata + 13) >> 24) & ((1ULL << 36) - 1);
  104. /* set time base and duration */
  105. if (samplerate > 0) {
  106. avpriv_set_pts_info(st, 64, 1, samplerate);
  107. if (samples > 0)
  108. st->duration = samples;
  109. }
  110. } else if (metadata_type == FLAC_METADATA_TYPE_CUESHEET) {
  111. uint8_t isrc[13];
  112. uint64_t start;
  113. const uint8_t *offset;
  114. int i, chapters, track, ti;
  115. if (metadata_size < 431)
  116. RETURN_ERROR(AVERROR_INVALIDDATA);
  117. offset = buffer + 395;
  118. chapters = bytestream_get_byte(&offset) - 1;
  119. if (chapters <= 0)
  120. RETURN_ERROR(AVERROR_INVALIDDATA);
  121. for (i = 0; i < chapters; i++) {
  122. if (offset + 36 - buffer > metadata_size)
  123. RETURN_ERROR(AVERROR_INVALIDDATA);
  124. start = bytestream_get_be64(&offset);
  125. track = bytestream_get_byte(&offset);
  126. bytestream_get_buffer(&offset, isrc, 12);
  127. isrc[12] = 0;
  128. offset += 14;
  129. ti = bytestream_get_byte(&offset);
  130. if (ti <= 0) RETURN_ERROR(AVERROR_INVALIDDATA);
  131. offset += ti * 12;
  132. avpriv_new_chapter(s, track, st->time_base, start, AV_NOPTS_VALUE, isrc);
  133. }
  134. av_freep(&buffer);
  135. } else if (metadata_type == FLAC_METADATA_TYPE_PICTURE) {
  136. ret = ff_flac_parse_picture(s, buffer, metadata_size);
  137. av_freep(&buffer);
  138. if (ret < 0) {
  139. av_log(s, AV_LOG_ERROR, "Error parsing attached picture.\n");
  140. return ret;
  141. }
  142. } else if (metadata_type == FLAC_METADATA_TYPE_SEEKTABLE) {
  143. const uint8_t *seekpoint = buffer;
  144. int i, seek_point_count = metadata_size/SEEKPOINT_SIZE;
  145. flac->found_seektable = 1;
  146. if ((s->flags&AVFMT_FLAG_FAST_SEEK)) {
  147. for(i=0; i<seek_point_count; i++) {
  148. int64_t timestamp = bytestream_get_be64(&seekpoint);
  149. int64_t pos = bytestream_get_be64(&seekpoint);
  150. /* skip number of samples */
  151. bytestream_get_be16(&seekpoint);
  152. av_add_index_entry(st, pos, timestamp, 0, 0, AVINDEX_KEYFRAME);
  153. }
  154. }
  155. av_freep(&buffer);
  156. }
  157. else {
  158. /* STREAMINFO must be the first block */
  159. if (!found_streaminfo) {
  160. RETURN_ERROR(AVERROR_INVALIDDATA);
  161. }
  162. /* process supported blocks other than STREAMINFO */
  163. if (metadata_type == FLAC_METADATA_TYPE_VORBIS_COMMENT) {
  164. AVDictionaryEntry *chmask;
  165. ret = ff_vorbis_comment(s, &s->metadata, buffer, metadata_size, 1);
  166. if (ret < 0) {
  167. av_log(s, AV_LOG_WARNING, "error parsing VorbisComment metadata\n");
  168. } else if (ret > 0) {
  169. s->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED;
  170. }
  171. /* parse the channels mask if present */
  172. chmask = av_dict_get(s->metadata, "WAVEFORMATEXTENSIBLE_CHANNEL_MASK", NULL, 0);
  173. if (chmask) {
  174. uint64_t mask = strtol(chmask->value, NULL, 0);
  175. if (!mask || mask & ~0x3ffffULL) {
  176. av_log(s, AV_LOG_WARNING,
  177. "Invalid value of WAVEFORMATEXTENSIBLE_CHANNEL_MASK\n");
  178. } else {
  179. st->codecpar->channel_layout = mask;
  180. av_dict_set(&s->metadata, "WAVEFORMATEXTENSIBLE_CHANNEL_MASK", NULL, 0);
  181. }
  182. }
  183. }
  184. av_freep(&buffer);
  185. }
  186. }
  187. ret = ff_replaygain_export(st, s->metadata);
  188. if (ret < 0)
  189. return ret;
  190. reset_index_position(avio_tell(s->pb), st);
  191. return 0;
  192. fail:
  193. av_free(buffer);
  194. return ret;
  195. }
  196. static int raw_flac_probe(AVProbeData *p)
  197. {
  198. if ((p->buf[2] & 0xF0) == 0) // blocksize code invalid
  199. return 0;
  200. if ((p->buf[2] & 0x0F) == 0x0F) // sample rate code invalid
  201. return 0;
  202. if ((p->buf[3] & 0xF0) >= FLAC_MAX_CHANNELS + FLAC_CHMODE_MID_SIDE << 4)
  203. // channel mode invalid
  204. return 0;
  205. if ((p->buf[3] & 0x06) == 0x06) // bits per sample code invalid
  206. return 0;
  207. if ((p->buf[3] & 0x01) == 0x01) // reserved bit set
  208. return 0;
  209. return AVPROBE_SCORE_EXTENSION / 4 + 1;
  210. }
  211. static int flac_probe(AVProbeData *p)
  212. {
  213. if ((AV_RB16(p->buf) & 0xFFFE) == 0xFFF8)
  214. return raw_flac_probe(p);
  215. /* file header + metadata header + checked bytes of streaminfo */
  216. if (p->buf_size >= 4 + 4 + 13) {
  217. int type = p->buf[4] & 0x7f;
  218. int size = AV_RB24(p->buf + 5);
  219. int min_block_size = AV_RB16(p->buf + 8);
  220. int max_block_size = AV_RB16(p->buf + 10);
  221. int sample_rate = AV_RB24(p->buf + 18) >> 4;
  222. if (memcmp(p->buf, "fLaC", 4))
  223. return 0;
  224. if (type == FLAC_METADATA_TYPE_STREAMINFO &&
  225. size == FLAC_STREAMINFO_SIZE &&
  226. min_block_size >= 16 &&
  227. max_block_size >= min_block_size &&
  228. sample_rate && sample_rate <= 655350)
  229. return AVPROBE_SCORE_MAX;
  230. return AVPROBE_SCORE_EXTENSION;
  231. }
  232. return 0;
  233. }
  234. static av_unused int64_t flac_read_timestamp(AVFormatContext *s, int stream_index,
  235. int64_t *ppos, int64_t pos_limit)
  236. {
  237. AVPacket pkt, out_pkt;
  238. AVStream *st = s->streams[stream_index];
  239. AVCodecParserContext *parser;
  240. int ret;
  241. int64_t pts = AV_NOPTS_VALUE;
  242. if (avio_seek(s->pb, *ppos, SEEK_SET) < 0)
  243. return AV_NOPTS_VALUE;
  244. av_init_packet(&pkt);
  245. parser = av_parser_init(st->codecpar->codec_id);
  246. if (!parser){
  247. return AV_NOPTS_VALUE;
  248. }
  249. parser->flags |= PARSER_FLAG_USE_CODEC_TS;
  250. for (;;){
  251. ret = ff_raw_read_partial_packet(s, &pkt);
  252. if (ret < 0){
  253. if (ret == AVERROR(EAGAIN))
  254. continue;
  255. else {
  256. av_packet_unref(&pkt);
  257. av_assert1(!pkt.size);
  258. }
  259. }
  260. av_init_packet(&out_pkt);
  261. av_parser_parse2(parser, st->internal->avctx,
  262. &out_pkt.data, &out_pkt.size, pkt.data, pkt.size,
  263. pkt.pts, pkt.dts, *ppos);
  264. av_packet_unref(&pkt);
  265. if (out_pkt.size){
  266. int size = out_pkt.size;
  267. if (parser->pts != AV_NOPTS_VALUE){
  268. // seeking may not have started from beginning of a frame
  269. // calculate frame start position from next frame backwards
  270. *ppos = parser->next_frame_offset - size;
  271. pts = parser->pts;
  272. break;
  273. }
  274. } else if (ret < 0)
  275. break;
  276. }
  277. av_parser_close(parser);
  278. return pts;
  279. }
  280. static int flac_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags) {
  281. int index;
  282. int64_t pos;
  283. AVIndexEntry e;
  284. FLACDecContext *flac = s->priv_data;
  285. if (!flac->found_seektable || !(s->flags&AVFMT_FLAG_FAST_SEEK)) {
  286. return -1;
  287. }
  288. index = av_index_search_timestamp(s->streams[0], timestamp, flags);
  289. if(index<0 || index >= s->streams[0]->nb_index_entries)
  290. return -1;
  291. e = s->streams[0]->index_entries[index];
  292. pos = avio_seek(s->pb, e.pos, SEEK_SET);
  293. if (pos >= 0) {
  294. return 0;
  295. }
  296. return -1;
  297. }
  298. AVInputFormat ff_flac_demuxer = {
  299. .name = "flac",
  300. .long_name = NULL_IF_CONFIG_SMALL("raw FLAC"),
  301. .read_probe = flac_probe,
  302. .read_header = flac_read_header,
  303. .read_packet = ff_raw_read_partial_packet,
  304. .read_seek = flac_seek,
  305. .read_timestamp = flac_read_timestamp,
  306. .flags = AVFMT_GENERIC_INDEX,
  307. .extensions = "flac",
  308. .raw_codec_id = AV_CODEC_ID_FLAC,
  309. .priv_data_size = sizeof(FLACDecContext),
  310. };