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.

287 lines
8.2KB

  1. /*
  2. * MP3 demuxer
  3. * Copyright (c) 2003 Fabrice Bellard
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "libavutil/avstring.h"
  22. #include "libavutil/intreadwrite.h"
  23. #include "libavutil/dict.h"
  24. #include "libavutil/mathematics.h"
  25. #include "avformat.h"
  26. #include "internal.h"
  27. #include "id3v2.h"
  28. #include "id3v1.h"
  29. #include "libavcodec/mpegaudiodecheader.h"
  30. #define XING_FLAG_FRAMES 0x01
  31. #define XING_FLAG_SIZE 0x02
  32. #define XING_FLAG_TOC 0x04
  33. #define XING_TOC_COUNT 100
  34. typedef struct MP3DecContext {
  35. int xing_toc;
  36. } MP3DecContext;
  37. /* mp3 read */
  38. static int mp3_read_probe(AVProbeData *p)
  39. {
  40. int max_frames, first_frames = 0;
  41. int fsize, frames, sample_rate;
  42. uint32_t header;
  43. uint8_t *buf, *buf0, *buf2, *end;
  44. AVCodecContext avctx;
  45. buf0 = p->buf;
  46. end = p->buf + p->buf_size - sizeof(uint32_t);
  47. while(buf0 < end && !*buf0)
  48. buf0++;
  49. max_frames = 0;
  50. buf = buf0;
  51. for(; buf < end; buf= buf2+1) {
  52. buf2 = buf;
  53. for(frames = 0; buf2 < end; frames++) {
  54. header = AV_RB32(buf2);
  55. fsize = avpriv_mpa_decode_header(&avctx, header, &sample_rate, &sample_rate, &sample_rate, &sample_rate);
  56. if(fsize < 0)
  57. break;
  58. buf2 += fsize;
  59. }
  60. max_frames = FFMAX(max_frames, frames);
  61. if(buf == buf0)
  62. first_frames= frames;
  63. }
  64. // keep this in sync with ac3 probe, both need to avoid
  65. // issues with MPEG-files!
  66. if (first_frames >= 4) return AVPROBE_SCORE_EXTENSION + 1;
  67. if (max_frames) {
  68. int pes = 0, i;
  69. unsigned int code = -1;
  70. #define VIDEO_ID 0x000001e0
  71. #define AUDIO_ID 0x000001c0
  72. /* do a search for mpegps headers to be able to properly bias
  73. * towards mpegps if we detect this stream as both. */
  74. for (i = 0; i<p->buf_size; i++) {
  75. code = (code << 8) + p->buf[i];
  76. if ((code & 0xffffff00) == 0x100) {
  77. if ((code & 0x1f0) == VIDEO_ID) pes++;
  78. else if((code & 0x1e0) == AUDIO_ID) pes++;
  79. }
  80. }
  81. if (pes)
  82. max_frames = (max_frames + pes - 1) / pes;
  83. }
  84. if (max_frames > 500) return AVPROBE_SCORE_EXTENSION;
  85. else if (max_frames >= 4) return AVPROBE_SCORE_EXTENSION / 2;
  86. else if (max_frames >= 1) return 1;
  87. else return 0;
  88. //mpegps_mp3_unrecognized_format.mpg has max_frames=3
  89. }
  90. static void read_xing_toc(AVFormatContext *s, int64_t filesize, int64_t duration)
  91. {
  92. int i;
  93. MP3DecContext *mp3 = s->priv_data;
  94. if (!filesize &&
  95. !(filesize = avio_size(s->pb))) {
  96. av_log(s, AV_LOG_WARNING, "Cannot determine file size, skipping TOC table.\n");
  97. return;
  98. }
  99. for (i = 0; i < XING_TOC_COUNT; i++) {
  100. uint8_t b = avio_r8(s->pb);
  101. av_add_index_entry(s->streams[0],
  102. av_rescale(b, filesize, 256),
  103. av_rescale(i, duration, XING_TOC_COUNT),
  104. 0, 0, AVINDEX_KEYFRAME);
  105. }
  106. mp3->xing_toc = 1;
  107. }
  108. /**
  109. * Try to find Xing/Info/VBRI tags and compute duration from info therein
  110. */
  111. static int mp3_parse_vbr_tags(AVFormatContext *s, AVStream *st, int64_t base)
  112. {
  113. uint32_t v, spf;
  114. unsigned frames = 0; /* Total number of frames in file */
  115. unsigned size = 0; /* Total number of bytes in the stream */
  116. const int64_t xing_offtbl[2][2] = {{32, 17}, {17,9}};
  117. MPADecodeHeader c;
  118. int vbrtag_size = 0;
  119. int is_cbr;
  120. v = avio_rb32(s->pb);
  121. if(ff_mpa_check_header(v) < 0)
  122. return -1;
  123. if (avpriv_mpegaudio_decode_header(&c, v) == 0)
  124. vbrtag_size = c.frame_size;
  125. if(c.layer != 3)
  126. return -1;
  127. spf = c.lsf ? 576 : 1152; /* Samples per frame, layer 3 */
  128. /* Check for Xing / Info tag */
  129. avio_skip(s->pb, xing_offtbl[c.lsf == 1][c.nb_channels == 1]);
  130. v = avio_rb32(s->pb);
  131. is_cbr = v == MKBETAG('I', 'n', 'f', 'o');
  132. if (v == MKBETAG('X', 'i', 'n', 'g') || is_cbr) {
  133. v = avio_rb32(s->pb);
  134. if(v & XING_FLAG_FRAMES)
  135. frames = avio_rb32(s->pb);
  136. if(v & XING_FLAG_SIZE)
  137. size = avio_rb32(s->pb);
  138. if (v & XING_FLAG_TOC && frames)
  139. read_xing_toc(s, size, av_rescale_q(frames, (AVRational){spf, c.sample_rate},
  140. st->time_base));
  141. }
  142. /* Check for VBRI tag (always 32 bytes after end of mpegaudio header) */
  143. avio_seek(s->pb, base + 4 + 32, SEEK_SET);
  144. v = avio_rb32(s->pb);
  145. if(v == MKBETAG('V', 'B', 'R', 'I')) {
  146. /* Check tag version */
  147. if(avio_rb16(s->pb) == 1) {
  148. /* skip delay and quality */
  149. avio_skip(s->pb, 4);
  150. size = avio_rb32(s->pb);
  151. frames = avio_rb32(s->pb);
  152. }
  153. }
  154. if(!frames && !size)
  155. return -1;
  156. /* Skip the vbr tag frame */
  157. avio_seek(s->pb, base + vbrtag_size, SEEK_SET);
  158. if(frames)
  159. st->duration = av_rescale_q(frames, (AVRational){spf, c.sample_rate},
  160. st->time_base);
  161. if (size && frames && !is_cbr)
  162. st->codec->bit_rate = av_rescale(size, 8 * c.sample_rate, frames * (int64_t)spf);
  163. return 0;
  164. }
  165. static int mp3_read_header(AVFormatContext *s)
  166. {
  167. AVStream *st;
  168. int64_t off;
  169. st = avformat_new_stream(s, NULL);
  170. if (!st)
  171. return AVERROR(ENOMEM);
  172. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  173. st->codec->codec_id = AV_CODEC_ID_MP3;
  174. st->need_parsing = AVSTREAM_PARSE_FULL;
  175. st->start_time = 0;
  176. // lcm of all mp3 sample rates
  177. avpriv_set_pts_info(st, 64, 1, 14112000);
  178. off = avio_tell(s->pb);
  179. if (!av_dict_get(s->metadata, "", NULL, AV_DICT_IGNORE_SUFFIX))
  180. ff_id3v1_read(s);
  181. if (mp3_parse_vbr_tags(s, st, off) < 0)
  182. avio_seek(s->pb, off, SEEK_SET);
  183. /* the parameters will be extracted from the compressed bitstream */
  184. return 0;
  185. }
  186. #define MP3_PACKET_SIZE 1024
  187. static int mp3_read_packet(AVFormatContext *s, AVPacket *pkt)
  188. {
  189. int ret;
  190. ret = av_get_packet(s->pb, pkt, MP3_PACKET_SIZE);
  191. if (ret < 0)
  192. return ret;
  193. pkt->stream_index = 0;
  194. if (ret > ID3v1_TAG_SIZE &&
  195. memcmp(&pkt->data[ret - ID3v1_TAG_SIZE], "TAG", 3) == 0)
  196. ret -= ID3v1_TAG_SIZE;
  197. /* note: we need to modify the packet size here to handle the last
  198. packet */
  199. pkt->size = ret;
  200. return ret;
  201. }
  202. static int mp3_seek(AVFormatContext *s, int stream_index, int64_t timestamp,
  203. int flags)
  204. {
  205. MP3DecContext *mp3 = s->priv_data;
  206. AVIndexEntry *ie;
  207. AVStream *st = s->streams[0];
  208. int64_t ret = av_index_search_timestamp(st, timestamp, flags);
  209. uint32_t header = 0;
  210. if (!mp3->xing_toc)
  211. return AVERROR(ENOSYS);
  212. if (ret < 0)
  213. return ret;
  214. ie = &st->index_entries[ret];
  215. ret = avio_seek(s->pb, ie->pos, SEEK_SET);
  216. if (ret < 0)
  217. return ret;
  218. while (!s->pb->eof_reached) {
  219. header = (header << 8) + avio_r8(s->pb);
  220. if (ff_mpa_check_header(header) >= 0) {
  221. ff_update_cur_dts(s, st, ie->timestamp);
  222. ret = avio_seek(s->pb, -4, SEEK_CUR);
  223. return (ret >= 0) ? 0 : ret;
  224. }
  225. }
  226. return AVERROR_EOF;
  227. }
  228. AVInputFormat ff_mp3_demuxer = {
  229. .name = "mp3",
  230. .long_name = NULL_IF_CONFIG_SMALL("MP2/3 (MPEG audio layer 2/3)"),
  231. .read_probe = mp3_read_probe,
  232. .read_header = mp3_read_header,
  233. .read_packet = mp3_read_packet,
  234. .read_seek = mp3_seek,
  235. .priv_data_size = sizeof(MP3DecContext),
  236. .flags = AVFMT_GENERIC_INDEX,
  237. .extensions = "mp2,mp3,m2a,mpa", /* XXX: use probe */
  238. };