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.

219 lines
6.3KB

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