FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/amr_parser.c
Date: 2021-10-25 13:57:44
Exec Total Coverage
Lines: 40 45 88.9%
Branches: 17 24 70.8%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2021 Paul B Mahol
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 /**
22 * @file
23 * AMR audio parser
24 *
25 * Splits packets into individual blocks.
26 */
27
28 #include "libavutil/channel_layout.h"
29 #include "libavutil/intreadwrite.h"
30 #include "parser.h"
31
32 static const uint8_t amrnb_packed_size[16] = {
33 13, 14, 16, 18, 20, 21, 27, 32, 6, 1, 1, 1, 1, 1, 1, 1
34 };
35 static const uint8_t amrwb_packed_size[16] = {
36 18, 24, 33, 37, 41, 47, 51, 59, 61, 6, 1, 1, 1, 1, 1, 1
37 };
38
39 typedef struct AMRParseContext {
40 ParseContext pc;
41 uint64_t cumulated_size;
42 uint64_t block_count;
43 int current_channel;
44 int remaining;
45 } AMRParseContext;
46
47 1 static av_cold int amr_parse_init(AVCodecParserContext *s1)
48 {
49 1 AMRParseContext *s = s1->priv_data;
50 1 s->remaining = -1;
51 1 return 0;
52 }
53
54 289 static int amr_parse(AVCodecParserContext *s1,
55 AVCodecContext *avctx,
56 const uint8_t **poutbuf, int *poutbuf_size,
57 const uint8_t *buf, int buf_size)
58 {
59 289 AMRParseContext *s = s1->priv_data;
60 289 ParseContext *pc = &s->pc;
61 289 int next = END_NOT_FOUND;
62
63 289 *poutbuf_size = 0;
64 289 *poutbuf = NULL;
65
66
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 289 times.
289 if (!avctx->channels) {
67 avctx->channels = 1;
68 avctx->channel_layout = AV_CH_LAYOUT_MONO;
69 }
70
71
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 289 times.
289 if (s1->flags & PARSER_FLAG_COMPLETE_FRAMES) {
72 next = buf_size;
73 } else {
74 289 int ch, offset = 0;
75
76
2/2
✓ Branch 0 taken 289 times.
✓ Branch 1 taken 283 times.
572 for (ch = s->current_channel; ch < avctx->channels; ch++) {
77
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 284 times.
289 if (s->remaining >= 0) {
78 5 next = s->remaining;
79 } else {
80 284 int mode = (buf[offset] >> 3) & 0x0F;
81
82
1/2
✓ Branch 0 taken 284 times.
✗ Branch 1 not taken.
284 if (avctx->codec_id == AV_CODEC_ID_AMR_NB) {
83 284 next = amrnb_packed_size[mode];
84 } else if (avctx->codec_id == AV_CODEC_ID_AMR_WB) {
85 next = amrwb_packed_size[mode];
86 }
87 }
88
89 289 offset += next;
90
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 283 times.
289 if (offset >= buf_size) {
91 6 s->remaining = offset - buf_size;
92 6 next = END_NOT_FOUND;
93 6 break;
94 } else {
95 283 s->remaining = -1;
96 }
97 }
98
99 289 s->current_channel = ch % avctx->channels;
100
2/2
✓ Branch 0 taken 283 times.
✓ Branch 1 taken 6 times.
289 if (s->remaining < 0)
101 283 next = offset;
102
103
2/2
✓ Branch 0 taken 283 times.
✓ Branch 1 taken 6 times.
289 if (next != END_NOT_FOUND) {
104
1/2
✓ Branch 0 taken 283 times.
✗ Branch 1 not taken.
283 if (s->cumulated_size < UINT64_MAX - next) {
105 283 s->cumulated_size += next;
106 /* Both AMR formats have 50 frames per second */
107 283 avctx->bit_rate = s->cumulated_size / ++s->block_count * 8 * 50;
108 }
109 }
110
111
2/2
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 285 times.
289 if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
112 4 *poutbuf = NULL;
113 4 *poutbuf_size = 0;
114 4 return buf_size;
115 }
116 }
117
118
1/2
✓ Branch 0 taken 285 times.
✗ Branch 1 not taken.
285 s1->duration = avctx->codec_id == AV_CODEC_ID_AMR_NB ? 160 : 320;
119
120 285 *poutbuf = buf;
121 285 *poutbuf_size = buf_size;
122 285 return next;
123 }
124
125 const AVCodecParser ff_amr_parser = {
126 .codec_ids = { AV_CODEC_ID_AMR_NB, AV_CODEC_ID_AMR_WB },
127 .priv_data_size = sizeof(AMRParseContext),
128 .parser_init = amr_parse_init,
129 .parser_parse = amr_parse,
130 .parser_close = ff_parse_close,
131 };
132