/src/ffmpeg/libavcodec/amr_parser.c
Line | Count | 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 | | #include "parser_internal.h" |
32 | | |
33 | | static const uint8_t amrnb_packed_size[16] = { |
34 | | 13, 14, 16, 18, 20, 21, 27, 32, 6, 1, 1, 1, 1, 1, 1, 1 |
35 | | }; |
36 | | static const uint8_t amrwb_packed_size[16] = { |
37 | | 18, 24, 33, 37, 41, 47, 51, 59, 61, 6, 1, 1, 1, 1, 1, 1 |
38 | | }; |
39 | | |
40 | | typedef struct AMRParseContext { |
41 | | ParseContext pc; |
42 | | uint64_t cumulated_size; |
43 | | uint64_t block_count; |
44 | | int current_channel; |
45 | | int remaining; |
46 | | } AMRParseContext; |
47 | | |
48 | | static av_cold int amr_parse_init(AVCodecParserContext *s1) |
49 | 903 | { |
50 | 903 | AMRParseContext *s = s1->priv_data; |
51 | 903 | s->remaining = -1; |
52 | 903 | return 0; |
53 | 903 | } |
54 | | |
55 | | static int amr_parse(AVCodecParserContext *s1, |
56 | | AVCodecContext *avctx, |
57 | | const uint8_t **poutbuf, int *poutbuf_size, |
58 | | const uint8_t *buf, int buf_size) |
59 | 7.09M | { |
60 | 7.09M | AMRParseContext *s = s1->priv_data; |
61 | 7.09M | ParseContext *pc = &s->pc; |
62 | 7.09M | int next = END_NOT_FOUND; |
63 | | |
64 | 7.09M | *poutbuf_size = 0; |
65 | 7.09M | *poutbuf = NULL; |
66 | | |
67 | 7.09M | if (!avctx->ch_layout.nb_channels) { |
68 | 599 | av_channel_layout_uninit(&avctx->ch_layout); |
69 | 599 | avctx->ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_MONO; |
70 | 599 | } |
71 | | |
72 | 7.09M | if (s1->flags & PARSER_FLAG_COMPLETE_FRAMES) { |
73 | 525 | next = buf_size; |
74 | 7.09M | } else { |
75 | 7.09M | int ch, offset = 0; |
76 | | |
77 | 14.2M | for (ch = s->current_channel; ch < avctx->ch_layout.nb_channels; ch++) { |
78 | 7.16M | if (s->remaining >= 0) { |
79 | 20.5k | next = s->remaining; |
80 | 7.14M | } else { |
81 | 7.14M | int mode = (buf[offset] >> 3) & 0x0F; |
82 | | |
83 | 7.14M | if (avctx->codec_id == AV_CODEC_ID_AMR_NB) { |
84 | 1.21M | next = amrnb_packed_size[mode]; |
85 | 5.92M | } else if (avctx->codec_id == AV_CODEC_ID_AMR_WB) { |
86 | 5.92M | next = amrwb_packed_size[mode]; |
87 | 5.92M | } |
88 | 7.14M | } |
89 | | |
90 | 7.16M | offset += next; |
91 | 7.16M | if (offset >= buf_size) { |
92 | 21.2k | s->remaining = offset - buf_size; |
93 | 21.2k | next = END_NOT_FOUND; |
94 | 21.2k | break; |
95 | 7.14M | } else { |
96 | 7.14M | s->remaining = -1; |
97 | 7.14M | } |
98 | 7.16M | } |
99 | | |
100 | 7.09M | s->current_channel = ch % avctx->ch_layout.nb_channels; |
101 | 7.09M | if (s->remaining < 0) |
102 | 7.06M | next = offset; |
103 | | |
104 | 7.09M | if (next != END_NOT_FOUND) { |
105 | 7.06M | if (s->cumulated_size < UINT64_MAX - next) { |
106 | 7.06M | s->cumulated_size += next; |
107 | | /* Both AMR formats have 50 frames per second */ |
108 | 7.06M | avctx->bit_rate = s->cumulated_size / ++s->block_count * 8 * 50; |
109 | 7.06M | } |
110 | 7.06M | } |
111 | | |
112 | 7.09M | if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) { |
113 | 21.0k | *poutbuf = NULL; |
114 | 21.0k | *poutbuf_size = 0; |
115 | 21.0k | return buf_size; |
116 | 21.0k | } |
117 | 7.09M | } |
118 | | |
119 | 7.06M | s1->duration = avctx->codec_id == AV_CODEC_ID_AMR_NB ? 160 : 320; |
120 | | |
121 | 7.06M | *poutbuf = buf; |
122 | 7.06M | *poutbuf_size = buf_size; |
123 | 7.06M | return next; |
124 | 7.09M | } |
125 | | |
126 | | const FFCodecParser ff_amr_parser = { |
127 | | PARSER_CODEC_LIST(AV_CODEC_ID_AMR_NB, AV_CODEC_ID_AMR_WB), |
128 | | .priv_data_size = sizeof(AMRParseContext), |
129 | | .init = amr_parse_init, |
130 | | .parse = amr_parse, |
131 | | .close = ff_parse_close, |
132 | | }; |