FFmpeg
sbc_parser.c
Go to the documentation of this file.
1 /*
2  * SBC parser
3  *
4  * Copyright (C) 2017 Aurelien Jacobs <aurel@gnuage.org>
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 "sbc.h"
24 #include "parser.h"
25 #include "parser_internal.h"
26 
27 typedef struct SBCParseContext {
29  uint8_t header[3];
33 
35  const uint8_t *data, size_t len)
36 {
37  static const int sample_rates[4] = { 16000, 32000, 44100, 48000 };
38  int sr, blocks, mode, subbands, bitpool, channels, joint;
39  int length;
40 
41  if (len < 3)
42  return -1;
43 
44  if (data[0] == MSBC_SYNCWORD && data[1] == 0 && data[2] == 0) {
47  avctx->ch_layout.nb_channels = 1;
48  avctx->sample_rate = 16000;
49  avctx->frame_size = 120;
50  s->duration = avctx->frame_size;
51  return 57;
52  }
53 
54  if (data[0] != SBC_SYNCWORD)
55  return -2;
56 
57  sr = (data[1] >> 6) & 0x03;
58  blocks = (((data[1] >> 4) & 0x03) + 1) << 2;
59  mode = (data[1] >> 2) & 0x03;
60  subbands = (((data[1] >> 0) & 0x01) + 1) << 2;
61  bitpool = data[2];
62 
63  channels = mode == SBC_MODE_MONO ? 1 : 2;
64  joint = mode == SBC_MODE_JOINT_STEREO;
65 
66  length = 4 + (subbands * channels) / 2
67  + ((((mode == SBC_MODE_DUAL_CHANNEL) + 1) * blocks * bitpool
68  + (joint * subbands)) + 7) / 8;
69 
73  avctx->sample_rate = sample_rates[sr];
74  avctx->frame_size = subbands * blocks;
75  s->duration = avctx->frame_size;
76  return length;
77 }
78 
80  const uint8_t **poutbuf, int *poutbuf_size,
81  const uint8_t *buf, int buf_size)
82 {
83  SBCParseContext *pc = s->priv_data;
84  int next;
85 
86  if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
87  next = buf_size;
88  } else {
89  if (pc->header_size) {
90  memcpy(pc->header + pc->header_size, buf,
91  sizeof(pc->header) - pc->header_size);
92  next = sbc_parse_header(s, avctx, pc->header, sizeof(pc->header))
93  - pc->buffered_size;
94  pc->header_size = 0;
95  } else {
96  next = sbc_parse_header(s, avctx, buf, buf_size);
97  if (next >= buf_size)
98  next = -1;
99  }
100 
101  if (next < 0) {
102  pc->header_size = FFMIN(sizeof(pc->header), buf_size);
103  memcpy(pc->header, buf, pc->header_size);
104  pc->buffered_size = buf_size;
105  next = END_NOT_FOUND;
106  }
107 
108  if (ff_combine_frame(&pc->pc, next, &buf, &buf_size) < 0) {
109  *poutbuf = NULL;
110  *poutbuf_size = 0;
111  return buf_size;
112  }
113  }
114 
115  *poutbuf = buf;
116  *poutbuf_size = buf_size;
117  return next;
118 }
119 
122  .priv_data_size = sizeof(SBCParseContext),
123  .parse = sbc_parse,
125 };
AVCodecContext::frame_size
int frame_size
Number of samples per channel in an audio frame.
Definition: avcodec.h:1051
MSBC_SYNCWORD
#define MSBC_SYNCWORD
Definition: sbc.h:71
AVCodecContext::sample_rate
int sample_rate
samples per second
Definition: avcodec.h:1024
ff_parse_close
av_cold void ff_parse_close(AVCodecParserContext *s)
Definition: parser.c:298
parser_internal.h
sample_rates
static const int sample_rates[]
Definition: dcaenc.h:34
mode
Definition: swscale.c:56
data
const char data[16]
Definition: mxf.c:149
AVChannelLayout::order
enum AVChannelOrder order
Channel order used in this layout.
Definition: channel_layout.h:324
subbands
subbands
Definition: aptx.h:37
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:329
close
static av_cold void close(AVCodecParserContext *s)
Definition: apv_parser.c:136
ParseContext
Definition: parser.h:28
AVCodecContext::ch_layout
AVChannelLayout ch_layout
Audio channel layout.
Definition: avcodec.h:1039
SBCParseContext::header_size
int header_size
Definition: sbc_parser.c:30
AV_CODEC_ID_SBC
@ AV_CODEC_ID_SBC
Definition: codec_id.h:546
sbc_parse_header
static int sbc_parse_header(AVCodecParserContext *s, AVCodecContext *avctx, const uint8_t *data, size_t len)
Definition: sbc_parser.c:34
s
#define s(width, name)
Definition: cbs_vp9.c:198
AV_CHANNEL_ORDER_UNSPEC
@ AV_CHANNEL_ORDER_UNSPEC
Only the channel count is specified, without any further information about the channel order.
Definition: channel_layout.h:119
SBCParseContext
Definition: sbc_parser.c:27
channels
channels
Definition: aptx.h:31
SBC_SYNCWORD
#define SBC_SYNCWORD
Definition: sbc.h:70
NULL
#define NULL
Definition: coverity.c:32
parse
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:46
SBC_MODE_MONO
#define SBC_MODE_MONO
Definition: sbc.h:56
sbc.h
ff_combine_frame
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:211
FFCodecParser
Definition: parser_internal.h:29
SBCParseContext::header
uint8_t header[3]
Definition: sbc_parser.c:29
PARSER_FLAG_COMPLETE_FRAMES
#define PARSER_FLAG_COMPLETE_FRAMES
Definition: avcodec.h:2609
ff_sbc_parser
const FFCodecParser ff_sbc_parser
Definition: sbc_parser.c:120
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
parser.h
len
int len
Definition: vorbis_enc_data.h:426
PARSER_CODEC_LIST
#define PARSER_CODEC_LIST(...)
Definition: parser_internal.h:76
AVCodecParserContext
Definition: avcodec.h:2575
AVCodecContext
main external API structure.
Definition: avcodec.h:431
mode
mode
Definition: ebur128.h:83
av_channel_layout_uninit
void av_channel_layout_uninit(AVChannelLayout *channel_layout)
Free any allocated data in the channel layout and reset the channel count to 0.
Definition: channel_layout.c:442
SBCParseContext::pc
ParseContext pc
Definition: sbc_parser.c:28
SBC_MODE_DUAL_CHANNEL
#define SBC_MODE_DUAL_CHANNEL
Definition: sbc.h:57
END_NOT_FOUND
#define END_NOT_FOUND
Definition: parser.h:40
SBC_MODE_JOINT_STEREO
#define SBC_MODE_JOINT_STEREO
Definition: sbc.h:59
sbc_parse
static int sbc_parse(AVCodecParserContext *s, AVCodecContext *avctx, const uint8_t **poutbuf, int *poutbuf_size, const uint8_t *buf, int buf_size)
Definition: sbc_parser.c:79
SBCParseContext::buffered_size
int buffered_size
Definition: sbc_parser.c:31