| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * MPEG Audio parser | ||
| 3 | * Copyright (c) 2003 Fabrice Bellard | ||
| 4 | * Copyright (c) 2003 Michael Niedermayer | ||
| 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 "parser.h" | ||
| 24 | #include "mpegaudiodecheader.h" | ||
| 25 | #include "parser_internal.h" | ||
| 26 | #include "libavutil/common.h" | ||
| 27 | #include "libavformat/apetag.h" // for APE tag. | ||
| 28 | #include "libavformat/id3v1.h" // for ID3v1_TAG_SIZE | ||
| 29 | |||
| 30 | typedef struct MpegAudioParseContext { | ||
| 31 | ParseContext pc; | ||
| 32 | int frame_size; | ||
| 33 | uint32_t header; | ||
| 34 | int header_count; | ||
| 35 | int no_bitrate; | ||
| 36 | } MpegAudioParseContext; | ||
| 37 | |||
| 38 | #define MPA_HEADER_SIZE 4 | ||
| 39 | |||
| 40 | /* header + layer + freq + lsf/mpeg25 */ | ||
| 41 | #define SAME_HEADER_MASK \ | ||
| 42 | (0xffe00000 | (3 << 17) | (3 << 10) | (3 << 19)) | ||
| 43 | |||
| 44 | 26182 | static int mpegaudio_parse(AVCodecParserContext *s1, | |
| 45 | AVCodecContext *avctx, | ||
| 46 | const uint8_t **poutbuf, int *poutbuf_size, | ||
| 47 | const uint8_t *buf, int buf_size) | ||
| 48 | { | ||
| 49 | 26182 | MpegAudioParseContext *s = s1->priv_data; | |
| 50 | 26182 | ParseContext *pc = &s->pc; | |
| 51 | 26182 | uint32_t state= pc->state; | |
| 52 | int i; | ||
| 53 | 26182 | int next= END_NOT_FOUND; | |
| 54 | 26182 | int flush = !buf_size; | |
| 55 | |||
| 56 |
2/2✓ Branch 0 taken 47957 times.
✓ Branch 1 taken 4858 times.
|
52815 | for(i=0; i<buf_size; ){ |
| 57 |
2/2✓ Branch 0 taken 25671 times.
✓ Branch 1 taken 22286 times.
|
47957 | if(s->frame_size){ |
| 58 | 25671 | int inc= FFMIN(buf_size - i, s->frame_size); | |
| 59 | 25671 | i += inc; | |
| 60 | 25671 | s->frame_size -= inc; | |
| 61 | 25671 | state = 0; | |
| 62 | |||
| 63 |
2/2✓ Branch 0 taken 21324 times.
✓ Branch 1 taken 4347 times.
|
25671 | if(!s->frame_size){ |
| 64 | 21324 | next= i; | |
| 65 | 21324 | break; | |
| 66 | } | ||
| 67 | }else{ | ||
| 68 |
2/2✓ Branch 0 taken 305648 times.
✓ Branch 1 taken 352 times.
|
306000 | while(i<buf_size){ |
| 69 | int ret, sr, channels, bit_rate, frame_size; | ||
| 70 | 305648 | enum AVCodecID codec_id = avctx->codec_id; | |
| 71 | |||
| 72 | 305648 | state= (state<<8) + buf[i++]; | |
| 73 | |||
| 74 | 305648 | ret = ff_mpa_decode_header(state, &sr, &channels, &frame_size, &bit_rate, &codec_id); | |
| 75 |
2/2✓ Branch 0 taken 283714 times.
✓ Branch 1 taken 21934 times.
|
305648 | if (ret < 4) { |
| 76 |
2/2✓ Branch 0 taken 218248 times.
✓ Branch 1 taken 65466 times.
|
283714 | if (i > 4) |
| 77 | 218248 | s->header_count = -2; | |
| 78 | } else { | ||
| 79 |
3/4✓ Branch 0 taken 21934 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 202 times.
✓ Branch 3 taken 21732 times.
|
21934 | int header_threshold = avctx->codec_id != AV_CODEC_ID_NONE && avctx->codec_id != codec_id; |
| 80 |
4/4✓ Branch 0 taken 600 times.
✓ Branch 1 taken 21334 times.
✓ Branch 2 taken 179 times.
✓ Branch 3 taken 421 times.
|
21934 | if((state&SAME_HEADER_MASK) != (s->header&SAME_HEADER_MASK) && s->header) |
| 81 | 179 | s->header_count= -3; | |
| 82 | 21934 | s->header= state; | |
| 83 | 21934 | s->header_count++; | |
| 84 | 21934 | s->frame_size = ret-4; | |
| 85 | |||
| 86 |
2/2✓ Branch 0 taken 21420 times.
✓ Branch 1 taken 514 times.
|
21934 | if (s->header_count > header_threshold) { |
| 87 | 21420 | avctx->sample_rate= sr; | |
| 88 | 21420 | av_channel_layout_uninit(&avctx->ch_layout); | |
| 89 | 21420 | av_channel_layout_default(&avctx->ch_layout, channels); | |
| 90 | 21420 | s1->duration = frame_size; | |
| 91 | 21420 | avctx->codec_id = codec_id; | |
| 92 |
4/4✓ Branch 0 taken 6419 times.
✓ Branch 1 taken 15001 times.
✓ Branch 2 taken 115 times.
✓ Branch 3 taken 6304 times.
|
21420 | if (s->no_bitrate || !avctx->bit_rate) { |
| 93 | 15116 | s->no_bitrate = 1; | |
| 94 | 15116 | avctx->bit_rate += (bit_rate - avctx->bit_rate) / (s->header_count - header_threshold); | |
| 95 | } | ||
| 96 | } | ||
| 97 | |||
| 98 |
2/2✓ Branch 0 taken 471 times.
✓ Branch 1 taken 21463 times.
|
21934 | if (s1->flags & PARSER_FLAG_COMPLETE_FRAMES) { |
| 99 | 471 | s->frame_size = 0; | |
| 100 | 471 | next = buf_size; | |
| 101 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 21463 times.
|
21463 | } else if (codec_id == AV_CODEC_ID_MP3ADU) { |
| 102 | ✗ | avpriv_report_missing_feature(avctx, | |
| 103 | "MP3ADU full parser"); | ||
| 104 | ✗ | *poutbuf = NULL; | |
| 105 | ✗ | *poutbuf_size = 0; | |
| 106 | ✗ | return buf_size; /* parsers must not return error codes */ | |
| 107 | } | ||
| 108 | |||
| 109 | 21934 | break; | |
| 110 | } | ||
| 111 | } | ||
| 112 | } | ||
| 113 | } | ||
| 114 | |||
| 115 | 26182 | pc->state= state; | |
| 116 |
2/2✓ Branch 1 taken 4360 times.
✓ Branch 2 taken 21822 times.
|
26182 | if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) { |
| 117 | 4360 | *poutbuf = NULL; | |
| 118 | 4360 | *poutbuf_size = 0; | |
| 119 | 4360 | return buf_size; | |
| 120 | } | ||
| 121 | |||
| 122 |
6/6✓ Branch 0 taken 156 times.
✓ Branch 1 taken 21666 times.
✓ Branch 2 taken 22 times.
✓ Branch 3 taken 134 times.
✓ Branch 4 taken 8 times.
✓ Branch 5 taken 14 times.
|
21822 | if (flush && buf_size >= ID3v1_TAG_SIZE && memcmp(buf, "TAG", 3) == 0) { |
| 123 | 8 | *poutbuf = NULL; | |
| 124 | 8 | *poutbuf_size = 0; | |
| 125 | 8 | return next; | |
| 126 | } | ||
| 127 | |||
| 128 |
5/6✓ Branch 0 taken 148 times.
✓ Branch 1 taken 21666 times.
✓ Branch 2 taken 15 times.
✓ Branch 3 taken 133 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 15 times.
|
21814 | if (flush && buf_size >= APE_TAG_FOOTER_BYTES && memcmp(buf, APE_TAG_PREAMBLE, 8) == 0) { |
| 129 | ✗ | *poutbuf = NULL; | |
| 130 | ✗ | *poutbuf_size = 0; | |
| 131 | ✗ | return next; | |
| 132 | } | ||
| 133 | |||
| 134 | 21814 | *poutbuf = buf; | |
| 135 | 21814 | *poutbuf_size = buf_size; | |
| 136 | 21814 | return next; | |
| 137 | } | ||
| 138 | |||
| 139 | |||
| 140 | const FFCodecParser ff_mpegaudio_parser = { | ||
| 141 | PARSER_CODEC_LIST(AV_CODEC_ID_MP1, AV_CODEC_ID_MP2, | ||
| 142 | AV_CODEC_ID_MP3, AV_CODEC_ID_MP3ADU), | ||
| 143 | .priv_data_size = sizeof(MpegAudioParseContext), | ||
| 144 | .parse = mpegaudio_parse, | ||
| 145 | .close = ff_parse_close, | ||
| 146 | }; | ||
| 147 |