mp3dec.c 8.8 KB

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