| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * MPEG Audio header decoder | ||
| 3 | * Copyright (c) 2001, 2002 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 | |||
| 22 | /** | ||
| 23 | * @file | ||
| 24 | * MPEG Audio header decoder. | ||
| 25 | */ | ||
| 26 | |||
| 27 | #include <string.h> | ||
| 28 | |||
| 29 | #include "libavutil/error.h" | ||
| 30 | #include "libavutil/macros.h" | ||
| 31 | #include "libavutil/mem.h" | ||
| 32 | |||
| 33 | #include "mpegaudio.h" | ||
| 34 | #include "mpegaudiodata.h" | ||
| 35 | #include "mpegaudiodecheader.h" | ||
| 36 | |||
| 37 | |||
| 38 | 442894340 | int ff_mpegaudio_decode_header(MPADecodeHeader2 *s, uint32_t header) | |
| 39 | { | ||
| 40 | int sample_rate, frame_size, mpeg25, padding; | ||
| 41 | int sample_rate_index, bitrate_index; | ||
| 42 | int ret; | ||
| 43 | |||
| 44 | 442894340 | ret = ff_mpa_check_header(header); | |
| 45 |
2/2✓ Branch 0 taken 442675520 times.
✓ Branch 1 taken 218820 times.
|
442894340 | if (ret < 0) |
| 46 | 442675520 | return ret; | |
| 47 | |||
| 48 | 218820 | memset(s, 0, sizeof(*s)); | |
| 49 | |||
| 50 |
2/2✓ Branch 0 taken 192657 times.
✓ Branch 1 taken 26163 times.
|
218820 | if (header & (1<<20)) { |
| 51 | 192657 | s->lsf = (header & (1<<19)) ? 0 : 1; | |
| 52 | 192657 | mpeg25 = 0; | |
| 53 | } else { | ||
| 54 | 26163 | s->lsf = 1; | |
| 55 | 26163 | mpeg25 = 1; | |
| 56 | } | ||
| 57 | |||
| 58 | 218820 | s->layer = 4 - ((header >> 17) & 3); | |
| 59 | /* extract frequency */ | ||
| 60 | 218820 | sample_rate_index = (header >> 10) & 3; | |
| 61 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 218820 times.
|
218820 | if (sample_rate_index >= FF_ARRAY_ELEMS(ff_mpa_freq_tab)) |
| 62 | ✗ | sample_rate_index = 0; | |
| 63 | 218820 | sample_rate = ff_mpa_freq_tab[sample_rate_index] >> (s->lsf + mpeg25); | |
| 64 | 218820 | sample_rate_index += 3 * (s->lsf + mpeg25); | |
| 65 | 218820 | s->sample_rate_index = sample_rate_index; | |
| 66 | 218820 | s->error_protection = ((header >> 16) & 1) ^ 1; | |
| 67 | 218820 | s->sample_rate = sample_rate; | |
| 68 | |||
| 69 | 218820 | bitrate_index = (header >> 12) & 0xf; | |
| 70 | 218820 | padding = (header >> 9) & 1; | |
| 71 | 218820 | s->bitrate_index = bitrate_index; | |
| 72 | 218820 | s->padding = padding; | |
| 73 | 218820 | s->private_bit = (header >> 8) & 1; | |
| 74 | 218820 | s->mode = (header >> 6) & 3; | |
| 75 | 218820 | s->mode_ext = (header >> 4) & 3; | |
| 76 | 218820 | s->copyright = (header >> 3) & 1; | |
| 77 | 218820 | s->original = (header >> 2) & 1; | |
| 78 | 218820 | s->emphasis = header & 3; | |
| 79 | |||
| 80 |
2/2✓ Branch 0 taken 91715 times.
✓ Branch 1 taken 127105 times.
|
218820 | if (s->mode == MPA_MONO) |
| 81 | 91715 | s->nb_channels = 1; | |
| 82 | else | ||
| 83 | 127105 | s->nb_channels = 2; | |
| 84 | |||
| 85 |
2/2✓ Branch 0 taken 114045 times.
✓ Branch 1 taken 104775 times.
|
218820 | if (bitrate_index != 0) { |
| 86 | 114045 | frame_size = ff_mpa_bitrate_tab[s->lsf][s->layer - 1][bitrate_index]; | |
| 87 | 114045 | s->bit_rate = frame_size * 1000; | |
| 88 |
3/3✓ Branch 0 taken 47436 times.
✓ Branch 1 taken 30061 times.
✓ Branch 2 taken 36548 times.
|
114045 | switch(s->layer) { |
| 89 | 47436 | case 1: | |
| 90 | 47436 | frame_size = (frame_size * 12000) / sample_rate; | |
| 91 | 47436 | frame_size = (frame_size + padding) * 4; | |
| 92 | 47436 | break; | |
| 93 | 30061 | case 2: | |
| 94 | 30061 | frame_size = (frame_size * 144000) / sample_rate; | |
| 95 | 30061 | frame_size += padding; | |
| 96 | 30061 | break; | |
| 97 | 36548 | default: | |
| 98 | case 3: | ||
| 99 | 36548 | frame_size = (frame_size * 144000) / (sample_rate << s->lsf); | |
| 100 | 36548 | frame_size += padding; | |
| 101 | 36548 | break; | |
| 102 | } | ||
| 103 | 114045 | s->frame_size = frame_size; | |
| 104 | } else { | ||
| 105 | /* if no frame size computed, signal it */ | ||
| 106 | 104775 | return 1; | |
| 107 | } | ||
| 108 | |||
| 109 | #if defined(DEBUG) | ||
| 110 | ff_dlog(NULL, "layer%d, %d Hz, %d kbits/s, ", | ||
| 111 | s->layer, s->sample_rate, s->bit_rate); | ||
| 112 | if (s->nb_channels == 2) { | ||
| 113 | if (s->layer == 3) { | ||
| 114 | if (s->mode_ext & MODE_EXT_MS_STEREO) | ||
| 115 | ff_dlog(NULL, "ms-"); | ||
| 116 | if (s->mode_ext & MODE_EXT_I_STEREO) | ||
| 117 | ff_dlog(NULL, "i-"); | ||
| 118 | } | ||
| 119 | ff_dlog(NULL, "stereo"); | ||
| 120 | } else { | ||
| 121 | ff_dlog(NULL, "mono"); | ||
| 122 | } | ||
| 123 | ff_dlog(NULL, "\n"); | ||
| 124 | #endif | ||
| 125 | 114045 | return 0; | |
| 126 | } | ||
| 127 | |||
| 128 | 463936559 | int avpriv_mpegaudio_decode_header2(MPADecodeHeader2 **phdr, uint32_t header) | |
| 129 | { | ||
| 130 | 463936559 | MPADecodeHeader2 *hdr = *phdr; | |
| 131 | int ret; | ||
| 132 | |||
| 133 |
2/2✓ Branch 0 taken 21371957 times.
✓ Branch 1 taken 442564602 times.
|
463936559 | if (!hdr) { |
| 134 | 21371957 | ret = ff_mpa_check_header(header); | |
| 135 |
2/2✓ Branch 0 taken 21365741 times.
✓ Branch 1 taken 6216 times.
|
21371957 | if (ret < 0) |
| 136 | 21365741 | return ret; | |
| 137 | 6216 | hdr = av_mallocz(sizeof(*hdr)); | |
| 138 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6216 times.
|
6216 | if (!hdr) |
| 139 | ✗ | return AVERROR(ENOMEM); | |
| 140 | } | ||
| 141 | |||
| 142 | 442570818 | ret = ff_mpegaudio_decode_header(hdr, header); | |
| 143 |
2/2✓ Branch 0 taken 442387713 times.
✓ Branch 1 taken 183105 times.
|
442570818 | if (ret < 0) |
| 144 | 442387713 | return ret; | |
| 145 | |||
| 146 | 183105 | *phdr = hdr; | |
| 147 | 183105 | return ret; | |
| 148 | } | ||
| 149 | |||
| 150 | #if FF_MPEGAUDIO_LEGACY_DECODE_HEADER | ||
| 151 | ✗ | int avpriv_mpegaudio_decode_header(MPADecodeHeader *s, uint32_t header) | |
| 152 | { | ||
| 153 | ✗ | MPADecodeHeader2 hdr = { 0 }; | |
| 154 | ✗ | int ret = ff_mpegaudio_decode_header(&hdr, header); | |
| 155 | |||
| 156 | ✗ | if (ret < 0) | |
| 157 | ✗ | return ret; | |
| 158 | |||
| 159 | /* callers in other libraries allocate the smaller structure, so only | ||
| 160 | its fields may be written */ | ||
| 161 | ✗ | s->frame_size = hdr.frame_size; | |
| 162 | ✗ | s->error_protection = hdr.error_protection; | |
| 163 | ✗ | s->layer = hdr.layer; | |
| 164 | ✗ | s->sample_rate = hdr.sample_rate; | |
| 165 | ✗ | s->sample_rate_index = hdr.sample_rate_index; | |
| 166 | ✗ | s->bit_rate = hdr.bit_rate; | |
| 167 | ✗ | s->nb_channels = hdr.nb_channels; | |
| 168 | ✗ | s->mode = hdr.mode; | |
| 169 | ✗ | s->mode_ext = hdr.mode_ext; | |
| 170 | ✗ | s->lsf = hdr.lsf; | |
| 171 | |||
| 172 | ✗ | return ret; | |
| 173 | } | ||
| 174 | #endif | ||
| 175 | |||
| 176 | 312380 | int ff_mpa_decode_header(uint32_t head, int *sample_rate, int *channels, int *frame_size, int *bit_rate, enum AVCodecID *codec_id) | |
| 177 | { | ||
| 178 | 312380 | MPADecodeHeader2 s1, *s = &s1; | |
| 179 | |||
| 180 |
2/2✓ Branch 1 taken 287808 times.
✓ Branch 2 taken 24572 times.
|
312380 | if (ff_mpegaudio_decode_header(s, head) != 0) { |
| 181 | 287808 | return -1; | |
| 182 | } | ||
| 183 | |||
| 184 |
3/3✓ Branch 0 taken 68 times.
✓ Branch 1 taken 10898 times.
✓ Branch 2 taken 13606 times.
|
24572 | switch(s->layer) { |
| 185 | 68 | case 1: | |
| 186 | 68 | *codec_id = AV_CODEC_ID_MP1; | |
| 187 | 68 | *frame_size = 384; | |
| 188 | 68 | break; | |
| 189 | 10898 | case 2: | |
| 190 | 10898 | *codec_id = AV_CODEC_ID_MP2; | |
| 191 | 10898 | *frame_size = 1152; | |
| 192 | 10898 | break; | |
| 193 | 13606 | default: | |
| 194 | case 3: | ||
| 195 |
1/2✓ Branch 0 taken 13606 times.
✗ Branch 1 not taken.
|
13606 | if (*codec_id != AV_CODEC_ID_MP3ADU) |
| 196 | 13606 | *codec_id = AV_CODEC_ID_MP3; | |
| 197 |
2/2✓ Branch 0 taken 278 times.
✓ Branch 1 taken 13328 times.
|
13606 | if (s->lsf) |
| 198 | 278 | *frame_size = 576; | |
| 199 | else | ||
| 200 | 13328 | *frame_size = 1152; | |
| 201 | 13606 | break; | |
| 202 | } | ||
| 203 | |||
| 204 | 24572 | *sample_rate = s->sample_rate; | |
| 205 | 24572 | *channels = s->nb_channels; | |
| 206 | 24572 | *bit_rate = s->bit_rate; | |
| 207 | 24572 | return s->frame_size; | |
| 208 | } | ||
| 209 |