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.

296 lines
8.6KB

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