FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/mpegaudio_parser.c
Date: 2026-01-16 07:34:38
Exec Total Coverage
Lines: 55 62 88.7%
Functions: 1 1 100.0%
Branches: 41 44 93.2%

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 21450 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 21450 MpegAudioParseContext *s = s1->priv_data;
50 21450 ParseContext *pc = &s->pc;
51 21450 uint32_t state= pc->state;
52 int i;
53 21450 int next= END_NOT_FOUND;
54 21450 int flush = !buf_size;
55
56
2/2
✓ Branch 0 taken 39667 times.
✓ Branch 1 taken 3697 times.
43364 for(i=0; i<buf_size; ){
57
2/2
✓ Branch 0 taken 20978 times.
✓ Branch 1 taken 18689 times.
39667 if(s->frame_size){
58 20978 int inc= FFMIN(buf_size - i, s->frame_size);
59 20978 i += inc;
60 20978 s->frame_size -= inc;
61 20978 state = 0;
62
63
2/2
✓ Branch 0 taken 17753 times.
✓ Branch 1 taken 3225 times.
20978 if(!s->frame_size){
64 17753 next= i;
65 17753 break;
66 }
67 }else{
68
2/2
✓ Branch 0 taken 285692 times.
✓ Branch 1 taken 343 times.
286035 while(i<buf_size){
69 int ret, sr, channels, bit_rate, frame_size;
70 285692 enum AVCodecID codec_id = avctx->codec_id;
71
72 285692 state= (state<<8) + buf[i++];
73
74 285692 ret = ff_mpa_decode_header(state, &sr, &channels, &frame_size, &bit_rate, &codec_id);
75
2/2
✓ Branch 0 taken 267346 times.
✓ Branch 1 taken 18346 times.
285692 if (ret < 4) {
76
2/2
✓ Branch 0 taken 212652 times.
✓ Branch 1 taken 54694 times.
267346 if (i > 4)
77 212652 s->header_count = -2;
78 } else {
79
3/4
✓ Branch 0 taken 18346 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 198 times.
✓ Branch 3 taken 18148 times.
18346 int header_threshold = avctx->codec_id != AV_CODEC_ID_NONE && avctx->codec_id != codec_id;
80
4/4
✓ Branch 0 taken 531 times.
✓ Branch 1 taken 17815 times.
✓ Branch 2 taken 179 times.
✓ Branch 3 taken 352 times.
18346 if((state&SAME_HEADER_MASK) != (s->header&SAME_HEADER_MASK) && s->header)
81 179 s->header_count= -3;
82 18346 s->header= state;
83 18346 s->header_count++;
84 18346 s->frame_size = ret-4;
85
86
2/2
✓ Branch 0 taken 17841 times.
✓ Branch 1 taken 505 times.
18346 if (s->header_count > header_threshold) {
87 17841 avctx->sample_rate= sr;
88 17841 av_channel_layout_uninit(&avctx->ch_layout);
89 17841 av_channel_layout_default(&avctx->ch_layout, channels);
90 17841 s1->duration = frame_size;
91 17841 avctx->codec_id = codec_id;
92
4/4
✓ Branch 0 taken 5783 times.
✓ Branch 1 taken 12058 times.
✓ Branch 2 taken 73 times.
✓ Branch 3 taken 5710 times.
17841 if (s->no_bitrate || !avctx->bit_rate) {
93 12131 s->no_bitrate = 1;
94 12131 avctx->bit_rate += (bit_rate - avctx->bit_rate) / (s->header_count - header_threshold);
95 }
96 }
97
98
2/2
✓ Branch 0 taken 464 times.
✓ Branch 1 taken 17882 times.
18346 if (s1->flags & PARSER_FLAG_COMPLETE_FRAMES) {
99 464 s->frame_size = 0;
100 464 next = buf_size;
101
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17882 times.
17882 } 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 18346 break;
110 }
111 }
112 }
113 }
114
115 21450 pc->state= state;
116
2/2
✓ Branch 1 taken 3236 times.
✓ Branch 2 taken 18214 times.
21450 if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
117 3236 *poutbuf = NULL;
118 3236 *poutbuf_size = 0;
119 3236 return buf_size;
120 }
121
122
6/6
✓ Branch 0 taken 126 times.
✓ Branch 1 taken 18088 times.
✓ Branch 2 taken 20 times.
✓ Branch 3 taken 106 times.
✓ Branch 4 taken 6 times.
✓ Branch 5 taken 14 times.
18214 if (flush && buf_size >= ID3v1_TAG_SIZE && memcmp(buf, "TAG", 3) == 0) {
123 6 *poutbuf = NULL;
124 6 *poutbuf_size = 0;
125 6 return next;
126 }
127
128
5/6
✓ Branch 0 taken 120 times.
✓ Branch 1 taken 18088 times.
✓ Branch 2 taken 15 times.
✓ Branch 3 taken 105 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 15 times.
18208 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 18208 *poutbuf = buf;
135 18208 *poutbuf_size = buf_size;
136 18208 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