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.

220 lines
6.1KB

  1. /*
  2. * raw ADTS AAC demuxer
  3. * Copyright (c) 2008 Michael Niedermayer <michaelni@gmx.at>
  4. * Copyright (c) 2009 Robert Swain ( rob opendot cl )
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #include "libavutil/avassert.h"
  23. #include "libavutil/intreadwrite.h"
  24. #include "avformat.h"
  25. #include "avio_internal.h"
  26. #include "internal.h"
  27. #include "id3v1.h"
  28. #include "id3v2.h"
  29. #include "apetag.h"
  30. #define ADTS_HEADER_SIZE 7
  31. static int adts_aac_probe(const AVProbeData *p)
  32. {
  33. int max_frames = 0, first_frames = 0;
  34. int fsize, frames;
  35. const uint8_t *buf0 = p->buf;
  36. const uint8_t *buf2;
  37. const uint8_t *buf;
  38. const uint8_t *end = buf0 + p->buf_size - 7;
  39. buf = buf0;
  40. for (; buf < end; buf = buf2 + 1) {
  41. buf2 = buf;
  42. for (frames = 0; buf2 < end; frames++) {
  43. uint32_t header = AV_RB16(buf2);
  44. if ((header & 0xFFF6) != 0xFFF0) {
  45. if (buf != buf0) {
  46. // Found something that isn't an ADTS header, starting
  47. // from a position other than the start of the buffer.
  48. // Discard the count we've accumulated so far since it
  49. // probably was a false positive.
  50. frames = 0;
  51. }
  52. break;
  53. }
  54. fsize = (AV_RB32(buf2 + 3) >> 13) & 0x1FFF;
  55. if (fsize < 7)
  56. break;
  57. fsize = FFMIN(fsize, end - buf2);
  58. buf2 += fsize;
  59. }
  60. max_frames = FFMAX(max_frames, frames);
  61. if (buf == buf0)
  62. first_frames = frames;
  63. }
  64. if (first_frames >= 3)
  65. return AVPROBE_SCORE_EXTENSION + 1;
  66. else if (max_frames > 100)
  67. return AVPROBE_SCORE_EXTENSION;
  68. else if (max_frames >= 3)
  69. return AVPROBE_SCORE_EXTENSION / 2;
  70. else if (first_frames >= 1)
  71. return 1;
  72. else
  73. return 0;
  74. }
  75. static int adts_aac_resync(AVFormatContext *s)
  76. {
  77. uint16_t state;
  78. // skip data until an ADTS frame is found
  79. state = avio_r8(s->pb);
  80. while (!avio_feof(s->pb) && avio_tell(s->pb) < s->probesize) {
  81. state = (state << 8) | avio_r8(s->pb);
  82. if ((state >> 4) != 0xFFF)
  83. continue;
  84. avio_seek(s->pb, -2, SEEK_CUR);
  85. break;
  86. }
  87. if (s->pb->eof_reached)
  88. return AVERROR_EOF;
  89. if ((state >> 4) != 0xFFF)
  90. return AVERROR_INVALIDDATA;
  91. return 0;
  92. }
  93. static int adts_aac_read_header(AVFormatContext *s)
  94. {
  95. AVStream *st;
  96. int ret;
  97. st = avformat_new_stream(s, NULL);
  98. if (!st)
  99. return AVERROR(ENOMEM);
  100. st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
  101. st->codecpar->codec_id = s->iformat->raw_codec_id;
  102. st->need_parsing = AVSTREAM_PARSE_FULL_RAW;
  103. ff_id3v1_read(s);
  104. if ((s->pb->seekable & AVIO_SEEKABLE_NORMAL) &&
  105. !av_dict_get(s->metadata, "", NULL, AV_DICT_IGNORE_SUFFIX)) {
  106. int64_t cur = avio_tell(s->pb);
  107. ff_ape_parse_tag(s);
  108. avio_seek(s->pb, cur, SEEK_SET);
  109. }
  110. ret = adts_aac_resync(s);
  111. if (ret < 0)
  112. return ret;
  113. // LCM of all possible ADTS sample rates
  114. avpriv_set_pts_info(st, 64, 1, 28224000);
  115. return 0;
  116. }
  117. static int handle_id3(AVFormatContext *s, AVPacket *pkt)
  118. {
  119. AVDictionary *metadata = NULL;
  120. AVIOContext ioctx;
  121. ID3v2ExtraMeta *id3v2_extra_meta = NULL;
  122. int ret;
  123. ret = av_append_packet(s->pb, pkt, ff_id3v2_tag_len(pkt->data) - pkt->size);
  124. if (ret < 0) {
  125. return ret;
  126. }
  127. ffio_init_context(&ioctx, pkt->data, pkt->size, 0, NULL, NULL, NULL, NULL);
  128. ff_id3v2_read_dict(&ioctx, &metadata, ID3v2_DEFAULT_MAGIC, &id3v2_extra_meta);
  129. if ((ret = ff_id3v2_parse_priv_dict(&metadata, id3v2_extra_meta)) < 0)
  130. goto error;
  131. if (metadata) {
  132. if ((ret = av_dict_copy(&s->metadata, metadata, 0)) < 0)
  133. goto error;
  134. s->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED;
  135. }
  136. error:
  137. av_packet_unref(pkt);
  138. ff_id3v2_free_extra_meta(&id3v2_extra_meta);
  139. av_dict_free(&metadata);
  140. return ret;
  141. }
  142. static int adts_aac_read_packet(AVFormatContext *s, AVPacket *pkt)
  143. {
  144. int ret, fsize;
  145. retry:
  146. ret = av_get_packet(s->pb, pkt, ADTS_HEADER_SIZE);
  147. if (ret < 0)
  148. return ret;
  149. if (ret < ADTS_HEADER_SIZE) {
  150. return AVERROR(EIO);
  151. }
  152. if ((AV_RB16(pkt->data) >> 4) != 0xfff) {
  153. // Parse all the ID3 headers between frames
  154. int append = ID3v2_HEADER_SIZE - ADTS_HEADER_SIZE;
  155. av_assert2(append > 0);
  156. ret = av_append_packet(s->pb, pkt, append);
  157. if (ret != append) {
  158. return AVERROR(EIO);
  159. }
  160. if (!ff_id3v2_match(pkt->data, ID3v2_DEFAULT_MAGIC)) {
  161. av_packet_unref(pkt);
  162. ret = adts_aac_resync(s);
  163. } else
  164. ret = handle_id3(s, pkt);
  165. if (ret < 0)
  166. return ret;
  167. goto retry;
  168. }
  169. fsize = (AV_RB32(pkt->data + 3) >> 13) & 0x1FFF;
  170. if (fsize < ADTS_HEADER_SIZE) {
  171. return AVERROR_INVALIDDATA;
  172. }
  173. ret = av_append_packet(s->pb, pkt, fsize - pkt->size);
  174. return ret;
  175. }
  176. AVInputFormat ff_aac_demuxer = {
  177. .name = "aac",
  178. .long_name = NULL_IF_CONFIG_SMALL("raw ADTS AAC (Advanced Audio Coding)"),
  179. .read_probe = adts_aac_probe,
  180. .read_header = adts_aac_read_header,
  181. .read_packet = adts_aac_read_packet,
  182. .flags = AVFMT_GENERIC_INDEX,
  183. .extensions = "aac",
  184. .mime_type = "audio/aac,audio/aacp,audio/x-aac",
  185. .raw_codec_id = AV_CODEC_ID_AAC,
  186. };