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.

321 lines
11KB

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