mp3dec.c 5.5 KB

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