| Line | Branch | Exec | Source | 
|---|---|---|---|
| 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 | |||
| 23 | #include "libavutil/avassert.h" | ||
| 24 | #include "libavutil/intreadwrite.h" | ||
| 25 | #include "avformat.h" | ||
| 26 | #include "avio_internal.h" | ||
| 27 | #include "demux.h" | ||
| 28 | #include "internal.h" | ||
| 29 | #include "id3v1.h" | ||
| 30 | #include "id3v2.h" | ||
| 31 | #include "apetag.h" | ||
| 32 | |||
| 33 | #define ADTS_HEADER_SIZE 7 | ||
| 34 | |||
| 35 | 7282 | static int adts_aac_probe(const AVProbeData *p) | |
| 36 | { | ||
| 37 | 7282 | int max_frames = 0, first_frames = 0; | |
| 38 | int fsize, frames; | ||
| 39 | 7282 | const uint8_t *buf0 = p->buf; | |
| 40 | const uint8_t *buf2; | ||
| 41 | const uint8_t *buf; | ||
| 42 | 7282 | const uint8_t *end = buf0 + p->buf_size - 7; | |
| 43 | |||
| 44 | 7282 | buf = buf0; | |
| 45 | |||
| 46 | 
        2/2✓ Branch 0 taken 281862936 times. 
          ✓ Branch 1 taken 7282 times. 
         | 
      281870218 | for (; buf < end; buf = buf2 + 1) { | 
| 47 | 281862936 | buf2 = buf; | |
| 48 | |||
| 49 | 
        2/2✓ Branch 0 taken 281897443 times. 
          ✓ Branch 1 taken 1758 times. 
         | 
      281899201 | for (frames = 0; buf2 < end; frames++) { | 
| 50 | 281897443 | uint32_t header = AV_RB16(buf2); | |
| 51 | 
        2/2✓ Branch 0 taken 281859830 times. 
          ✓ Branch 1 taken 37613 times. 
         | 
      281897443 | if ((header & 0xFFF6) != 0xFFF0) { | 
| 52 | 
        2/2✓ Branch 0 taken 281852575 times. 
          ✓ Branch 1 taken 7255 times. 
         | 
      281859830 | if (buf != buf0) { | 
| 53 | // Found something that isn't an ADTS header, starting | ||
| 54 | // from a position other than the start of the buffer. | ||
| 55 | // Discard the count we've accumulated so far since it | ||
| 56 | // probably was a false positive. | ||
| 57 | 281852575 | frames = 0; | |
| 58 | } | ||
| 59 | 281859830 | break; | |
| 60 | } | ||
| 61 | 37613 | fsize = (AV_RB32(buf2 + 3) >> 13) & 0x1FFF; | |
| 62 | 
        2/2✓ Branch 0 taken 1348 times. 
          ✓ Branch 1 taken 36265 times. 
         | 
      37613 | if (fsize < 7) | 
| 63 | 1348 | break; | |
| 64 | 36265 | fsize = FFMIN(fsize, end - buf2); | |
| 65 | 36265 | buf2 += fsize; | |
| 66 | } | ||
| 67 | 281862936 | max_frames = FFMAX(max_frames, frames); | |
| 68 | 
        2/2✓ Branch 0 taken 7282 times. 
          ✓ Branch 1 taken 281855654 times. 
         | 
      281862936 | if (buf == buf0) | 
| 69 | 7282 | first_frames = frames; | |
| 70 | } | ||
| 71 | |||
| 72 | 
        2/2✓ Branch 0 taken 25 times. 
          ✓ Branch 1 taken 7257 times. 
         | 
      7282 | if (first_frames >= 3) | 
| 73 | 25 | return AVPROBE_SCORE_EXTENSION + 1; | |
| 74 | 
        1/2✗ Branch 0 not taken. 
          ✓ Branch 1 taken 7257 times. 
         | 
      7257 | else if (max_frames > 100) | 
| 75 | ✗ | return AVPROBE_SCORE_EXTENSION; | |
| 76 | 
        1/2✗ Branch 0 not taken. 
          ✓ Branch 1 taken 7257 times. 
         | 
      7257 | else if (max_frames >= 3) | 
| 77 | ✗ | return AVPROBE_SCORE_EXTENSION / 2; | |
| 78 | 
        2/2✓ Branch 0 taken 2 times. 
          ✓ Branch 1 taken 7255 times. 
         | 
      7257 | else if (first_frames >= 1) | 
| 79 | 2 | return 1; | |
| 80 | else | ||
| 81 | 7255 | return 0; | |
| 82 | } | ||
| 83 | |||
| 84 | 31 | static int adts_aac_resync(AVFormatContext *s) | |
| 85 | { | ||
| 86 | uint16_t state; | ||
| 87 | 31 | int64_t start_pos = avio_tell(s->pb); | |
| 88 | |||
| 89 | // skip data until an ADTS frame is found | ||
| 90 | 31 | state = avio_r8(s->pb); | |
| 91 | 
        2/2✓ Branch 1 taken 401 times. 
          ✓ Branch 2 taken 1 times. 
         | 
      402 | while (!avio_feof(s->pb) && | 
| 92 | 
        1/2✓ Branch 1 taken 401 times. 
          ✗ Branch 2 not taken. 
         | 
      401 | (avio_tell(s->pb) - start_pos) < s->probesize) { | 
| 93 | 401 | state = (state << 8) | avio_r8(s->pb); | |
| 94 | 
        2/2✓ Branch 0 taken 371 times. 
          ✓ Branch 1 taken 30 times. 
         | 
      401 | if ((state >> 4) != 0xFFF) | 
| 95 | 371 | continue; | |
| 96 | 30 | avio_seek(s->pb, -2, SEEK_CUR); | |
| 97 | 30 | break; | |
| 98 | } | ||
| 99 | 
        2/2✓ Branch 0 taken 1 times. 
          ✓ Branch 1 taken 30 times. 
         | 
      31 | if (s->pb->eof_reached) | 
| 100 | 1 | return AVERROR_EOF; | |
| 101 | 
        1/2✗ Branch 0 not taken. 
          ✓ Branch 1 taken 30 times. 
         | 
      30 | if ((state >> 4) != 0xFFF) | 
| 102 | ✗ | return AVERROR_INVALIDDATA; | |
| 103 | |||
| 104 | 30 | return 0; | |
| 105 | } | ||
| 106 | |||
| 107 | 30 | static int adts_aac_read_header(AVFormatContext *s) | |
| 108 | { | ||
| 109 | AVStream *st; | ||
| 110 | int ret; | ||
| 111 | |||
| 112 | 30 | st = avformat_new_stream(s, NULL); | |
| 113 | 
        1/2✗ Branch 0 not taken. 
          ✓ Branch 1 taken 30 times. 
         | 
      30 | if (!st) | 
| 114 | ✗ | return AVERROR(ENOMEM); | |
| 115 | |||
| 116 | 30 | st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO; | |
| 117 | 30 | st->codecpar->codec_id = AV_CODEC_ID_AAC; | |
| 118 | 30 | ffstream(st)->need_parsing = AVSTREAM_PARSE_FULL_RAW; | |
| 119 | |||
| 120 | 30 | ff_id3v1_read(s); | |
| 121 | 
        3/4✓ Branch 0 taken 30 times. 
          ✗ Branch 1 not taken. 
          ✓ Branch 2 taken 29 times. 
          ✓ Branch 3 taken 1 times. 
         | 
      60 | if ((s->pb->seekable & AVIO_SEEKABLE_NORMAL) && | 
| 122 | 30 | !av_dict_count(s->metadata)) { | |
| 123 | 29 | int64_t cur = avio_tell(s->pb); | |
| 124 | 29 | ff_ape_parse_tag(s); | |
| 125 | 29 | avio_seek(s->pb, cur, SEEK_SET); | |
| 126 | } | ||
| 127 | |||
| 128 | 30 | ret = adts_aac_resync(s); | |
| 129 | 
        1/2✗ Branch 0 not taken. 
          ✓ Branch 1 taken 30 times. 
         | 
      30 | if (ret < 0) | 
| 130 | ✗ | return ret; | |
| 131 | |||
| 132 | // LCM of all possible ADTS sample rates | ||
| 133 | 30 | avpriv_set_pts_info(st, 64, 1, 28224000); | |
| 134 | |||
| 135 | 30 | return 0; | |
| 136 | } | ||
| 137 | |||
| 138 | 3 | static int handle_id3(AVFormatContext *s, AVPacket *pkt) | |
| 139 | { | ||
| 140 | 3 | AVDictionary *metadata = NULL; | |
| 141 | FFIOContext pb; | ||
| 142 | ID3v2ExtraMeta *id3v2_extra_meta; | ||
| 143 | int ret; | ||
| 144 | |||
| 145 | 3 | ret = av_append_packet(s->pb, pkt, ff_id3v2_tag_len(pkt->data) - pkt->size); | |
| 146 | 
        1/2✗ Branch 0 not taken. 
          ✓ Branch 1 taken 3 times. 
         | 
      3 | if (ret < 0) | 
| 147 | ✗ | return ret; | |
| 148 | |||
| 149 | 3 | ffio_init_read_context(&pb, pkt->data, pkt->size); | |
| 150 | 3 | ff_id3v2_read_dict(&pb.pub, &metadata, ID3v2_DEFAULT_MAGIC, &id3v2_extra_meta); | |
| 151 | 
        1/2✗ Branch 1 not taken. 
          ✓ Branch 2 taken 3 times. 
         | 
      3 | if ((ret = ff_id3v2_parse_priv_dict(&metadata, id3v2_extra_meta)) < 0) | 
| 152 | ✗ | goto error; | |
| 153 | |||
| 154 | 
        1/2✗ Branch 0 not taken. 
          ✓ Branch 1 taken 3 times. 
         | 
      3 | if (metadata) { | 
| 155 | 
        1/2✗ Branch 1 not taken. 
          ✓ Branch 2 taken 3 times. 
         | 
      3 | if ((ret = av_dict_copy(&s->metadata, metadata, 0)) < 0) | 
| 156 | ✗ | goto error; | |
| 157 | 3 | s->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED; | |
| 158 | } | ||
| 159 | |||
| 160 | ✗ | error: | |
| 161 | 3 | av_packet_unref(pkt); | |
| 162 | 3 | ff_id3v2_free_extra_meta(&id3v2_extra_meta); | |
| 163 | 3 | av_dict_free(&metadata); | |
| 164 | |||
| 165 | 3 | return ret; | |
| 166 | } | ||
| 167 | |||
| 168 | 5491 | static int adts_aac_read_packet(AVFormatContext *s, AVPacket *pkt) | |
| 169 | { | ||
| 170 | int ret, fsize; | ||
| 171 | |||
| 172 | 5494 | retry: | |
| 173 | 5494 | ret = av_get_packet(s->pb, pkt, ADTS_HEADER_SIZE); | |
| 174 | 
        2/2✓ Branch 0 taken 44 times. 
          ✓ Branch 1 taken 5450 times. 
         | 
      5494 | if (ret < 0) | 
| 175 | 44 | return ret; | |
| 176 | |||
| 177 | 
        1/2✗ Branch 0 not taken. 
          ✓ Branch 1 taken 5450 times. 
         | 
      5450 | if (ret < ADTS_HEADER_SIZE) | 
| 178 | ✗ | return AVERROR(EIO); | |
| 179 | |||
| 180 | 
        2/2✓ Branch 0 taken 4 times. 
          ✓ Branch 1 taken 5446 times. 
         | 
      5450 | if ((AV_RB16(pkt->data) >> 4) != 0xfff) { | 
| 181 | // Parse all the ID3 headers between frames | ||
| 182 | 4 | int append = ID3v2_HEADER_SIZE - ADTS_HEADER_SIZE; | |
| 183 | |||
| 184 | av_assert2(append > 0); | ||
| 185 | 4 | ret = av_append_packet(s->pb, pkt, append); | |
| 186 | 
        1/2✗ Branch 0 not taken. 
          ✓ Branch 1 taken 4 times. 
         | 
      4 | if (ret != append) | 
| 187 | ✗ | return AVERROR(EIO); | |
| 188 | 
        2/2✓ Branch 1 taken 1 times. 
          ✓ Branch 2 taken 3 times. 
         | 
      4 | if (!ff_id3v2_match(pkt->data, ID3v2_DEFAULT_MAGIC)) { | 
| 189 | 1 | av_packet_unref(pkt); | |
| 190 | 1 | ret = adts_aac_resync(s); | |
| 191 | } else | ||
| 192 | 3 | ret = handle_id3(s, pkt); | |
| 193 | 
        2/2✓ Branch 0 taken 1 times. 
          ✓ Branch 1 taken 3 times. 
         | 
      4 | if (ret < 0) | 
| 194 | 1 | return ret; | |
| 195 | |||
| 196 | 3 | goto retry; | |
| 197 | } | ||
| 198 | |||
| 199 | 5446 | fsize = (AV_RB32(pkt->data + 3) >> 13) & 0x1FFF; | |
| 200 | 
        1/2✗ Branch 0 not taken. 
          ✓ Branch 1 taken 5446 times. 
         | 
      5446 | if (fsize < ADTS_HEADER_SIZE) | 
| 201 | ✗ | return AVERROR_INVALIDDATA; | |
| 202 | |||
| 203 | 5446 | ret = av_append_packet(s->pb, pkt, fsize - pkt->size); | |
| 204 | |||
| 205 | 5446 | return ret; | |
| 206 | } | ||
| 207 | |||
| 208 | const FFInputFormat ff_aac_demuxer = { | ||
| 209 | .p.name = "aac", | ||
| 210 | .p.long_name = NULL_IF_CONFIG_SMALL("raw ADTS AAC (Advanced Audio Coding)"), | ||
| 211 | .p.flags = AVFMT_GENERIC_INDEX, | ||
| 212 | .p.extensions = "aac", | ||
| 213 | .p.mime_type = "audio/aac,audio/aacp,audio/x-aac", | ||
| 214 | .flags_internal = FF_INFMT_FLAG_ID3V2_AUTO, | ||
| 215 | .read_probe = adts_aac_probe, | ||
| 216 | .read_header = adts_aac_read_header, | ||
| 217 | .read_packet = adts_aac_read_packet, | ||
| 218 | .raw_codec_id = AV_CODEC_ID_AAC, | ||
| 219 | }; | ||
| 220 |