4 * Copyright (C) 2017 Aurelien Jacobs <aurel@gnuage.org>
6 * This file is part of FFmpeg.
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.
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.
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
26 typedef struct SBCParseContext
{
33 static int sbc_parse_header(AVCodecParserContext
*s
, AVCodecContext
*avctx
,
34 const uint8_t *data
, size_t len
)
36 static const int sample_rates
[4] = { 16000, 32000, 44100, 48000 };
37 int sr
, blocks
, mode
, subbands
, bitpool
, channels
, joint
;
43 if (data
[0] == MSBC_SYNCWORD
&& data
[1] == 0 && data
[2] == 0) {
44 av_channel_layout_uninit(&avctx
->ch_layout
);
45 avctx
->ch_layout
.order
= AV_CHANNEL_ORDER_UNSPEC
;
46 avctx
->ch_layout
.nb_channels
= 1;
47 avctx
->sample_rate
= 16000;
48 avctx
->frame_size
= 120;
49 s
->duration
= avctx
->frame_size
;
53 if (data
[0] != SBC_SYNCWORD
)
56 sr
= (data
[1] >> 6) & 0x03;
57 blocks
= (((data
[1] >> 4) & 0x03) + 1) << 2;
58 mode
= (data
[1] >> 2) & 0x03;
59 subbands
= (((data
[1] >> 0) & 0x01) + 1) << 2;
62 channels
= mode
== SBC_MODE_MONO
? 1 : 2;
63 joint
= mode
== SBC_MODE_JOINT_STEREO
;
65 length
= 4 + (subbands
* channels
) / 2
66 + ((((mode
== SBC_MODE_DUAL_CHANNEL
) + 1) * blocks
* bitpool
67 + (joint
* subbands
)) + 7) / 8;
69 av_channel_layout_uninit(&avctx
->ch_layout
);
70 avctx
->ch_layout
.order
= AV_CHANNEL_ORDER_UNSPEC
;
71 avctx
->ch_layout
.nb_channels
= channels
;
72 avctx
->sample_rate
= sample_rates
[sr
];
73 avctx
->frame_size
= subbands
* blocks
;
74 s
->duration
= avctx
->frame_size
;
78 static int sbc_parse(AVCodecParserContext
*s
, AVCodecContext
*avctx
,
79 const uint8_t **poutbuf
, int *poutbuf_size
,
80 const uint8_t *buf
, int buf_size
)
82 SBCParseContext
*pc
= s
->priv_data
;
85 if (s
->flags
& PARSER_FLAG_COMPLETE_FRAMES
) {
88 if (pc
->header_size
) {
89 memcpy(pc
->header
+ pc
->header_size
, buf
,
90 sizeof(pc
->header
) - pc
->header_size
);
91 next
= sbc_parse_header(s
, avctx
, pc
->header
, sizeof(pc
->header
))
95 next
= sbc_parse_header(s
, avctx
, buf
, buf_size
);
101 pc
->header_size
= FFMIN(sizeof(pc
->header
), buf_size
);
102 memcpy(pc
->header
, buf
, pc
->header_size
);
103 pc
->buffered_size
= buf_size
;
104 next
= END_NOT_FOUND
;
107 if (ff_combine_frame(&pc
->pc
, next
, &buf
, &buf_size
) < 0) {
115 *poutbuf_size
= buf_size
;
119 const AVCodecParser ff_sbc_parser
= {
120 .codec_ids
= { AV_CODEC_ID_SBC
},
121 .priv_data_size
= sizeof(SBCParseContext
),
122 .parser_parse
= sbc_parse
,
123 .parser_close
= ff_parse_close
,