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.

246 lines
7.1KB

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