/libavformat/flacdec.c

http://github.com/FFmpeg/FFmpeg · C · 347 lines · 276 code · 33 blank · 38 comment · 83 complexity · 8a2731e5adbc332d948d95bf98ce1d90 MD5 · raw file

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