FFmpeg
Loading...
Searching...
No Matches
amr_parser.c
Go to the documentation of this file.
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
30#include "parser.h"
31#include "parser_internal.h"
32
33static 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};
36static 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
47
49{
51 s->remaining = -1;
52 return 0;
53}
54
56 AVCodecContext *avctx,
57 const uint8_t **poutbuf, int *poutbuf_size,
58 const uint8_t *buf, int buf_size)
59{
61 ParseContext *pc = &s->pc;
62 int next = END_NOT_FOUND;
63
64 *poutbuf_size = 0;
65 *poutbuf = NULL;
66
67 if (!avctx->ch_layout.nb_channels) {
70 }
71
73 next = buf_size;
74 } else {
75 int ch, offset = 0;
76
77 for (ch = s->current_channel; ch < avctx->ch_layout.nb_channels; ch++) {
78 if (s->remaining >= 0) {
79 next = s->remaining;
80 } else {
81 int mode = (buf[offset] >> 3) & 0x0F;
82
83 if (avctx->codec_id == AV_CODEC_ID_AMR_NB) {
84 next = amrnb_packed_size[mode];
85 } else if (avctx->codec_id == AV_CODEC_ID_AMR_WB) {
86 next = amrwb_packed_size[mode];
87 }
88 }
89
90 offset += next;
91 if (offset >= buf_size) {
92 s->remaining = offset - buf_size;
93 next = END_NOT_FOUND;
94 break;
95 } else {
96 s->remaining = -1;
97 }
98 }
99
100 s->current_channel = ch % avctx->ch_layout.nb_channels;
101 if (s->remaining < 0)
102 next = offset;
103
104 if (next != END_NOT_FOUND) {
105 if (s->cumulated_size < UINT64_MAX - next) {
106 s->cumulated_size += next;
107 /* Both AMR formats have 50 frames per second */
108 avctx->bit_rate = s->cumulated_size / ++s->block_count * 8 * 50;
109 }
110 }
111
112 if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
113 *poutbuf = NULL;
114 *poutbuf_size = 0;
115 return buf_size;
116 }
117 }
118
119 s1->duration = avctx->codec_id == AV_CODEC_ID_AMR_NB ? 160 : 320;
120
121 *poutbuf = buf;
122 *poutbuf_size = buf_size;
123 return next;
124}
125
static int amr_parse(AVCodecParserContext *s1, AVCodecContext *avctx, const uint8_t **poutbuf, int *poutbuf_size, const uint8_t *buf, int buf_size)
Definition amr_parser.c:55
const FFCodecParser ff_amr_parser
Definition amr_parser.c:126
static av_cold int amr_parse_init(AVCodecParserContext *s1)
Definition amr_parser.c:48
static const uint8_t amrnb_packed_size[16]
Definition amr_parser.c:33
static const uint8_t amrwb_packed_size[16]
Definition amr_parser.c:36
static int parse(AVCodecParserContext *s, AVCodecContext *avctx, const uint8_t **poutbuf, int *poutbuf_size, const uint8_t *buf, int buf_size)
Definition apv_parser.c:92
static av_cold void close(AVCodecParserContext *s)
Definition apv_parser.c:197
#define PARSER_FLAG_COMPLETE_FRAMES
Definition avcodec.h:2651
#define s(width, name)
Definition cbs_vp9.c:198
Public libavutil channel layout APIs header.
#define NULL
Definition coverity.c:32
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
@ AV_CODEC_ID_AMR_NB
Definition codec_id.h:434
@ AV_CODEC_ID_AMR_WB
Definition codec_id.h:435
#define AV_CHANNEL_LAYOUT_MONO
void av_channel_layout_uninit(AVChannelLayout *channel_layout)
Free any allocated data in the channel layout and reset the channel count to 0.
unsigned offset
Definition libaomenc.c:763
#define av_cold
Definition attributes.h:117
av_cold void ff_parse_close(AVCodecParserContext *s)
Definition parser.c:300
int ff_combine_frame(ParseContext *pc, int next, const uint8_t **buf, int *buf_size)
Combine the (truncated) bitstream to a complete frame.
Definition parser.c:213
#define END_NOT_FOUND
Definition parser.h:40
#define PARSER_CODEC_LIST(...)
uint64_t block_count
Definition amr_parser.c:43
uint64_t cumulated_size
Definition amr_parser.c:42
ParseContext pc
Definition amr_parser.c:41
An AVChannelLayout holds information about the channel layout of audio data.
int nb_channels
Number of channels in this layout.
main external API structure.
Definition avcodec.h:443
AVChannelLayout ch_layout
Audio channel layout.
Definition avcodec.h:1055
int64_t bit_rate
the average bitrate
Definition avcodec.h:493
enum AVCodecID codec_id
Definition avcodec.h:453
int duration
Duration of the current frame.
Definition avcodec.h:2731
Definition swscale.c:71