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 
26 typedef struct SBCParseContext {
28  uint8_t header[3];
32 
34  const uint8_t *data, size_t len)
35 {
36  static const int sample_rates[4] = { 16000, 32000, 44100, 48000 };
37  int sr, blocks, mode, subbands, bitpool, channels, joint;
38  int length;
39 
40  if (len < 3)
41  return -1;
42 
43  if (data[0] == MSBC_SYNCWORD && data[1] == 0 && data[2] == 0) {
44  avctx->channels = 1;
45  avctx->sample_rate = 16000;
46  avctx->frame_size = 120;
47  s->duration = avctx->frame_size;
48  return 57;
49  }
50 
51  if (data[0] != SBC_SYNCWORD)
52  return -2;
53 
54  sr = (data[1] >> 6) & 0x03;
55  blocks = (((data[1] >> 4) & 0x03) + 1) << 2;
56  mode = (data[1] >> 2) & 0x03;
57  subbands = (((data[1] >> 0) & 0x01) + 1) << 2;
58  bitpool = data[2];
59 
60  channels = mode == SBC_MODE_MONO ? 1 : 2;
61  joint = mode == SBC_MODE_JOINT_STEREO;
62 
63  length = 4 + (subbands * channels) / 2
64  + ((((mode == SBC_MODE_DUAL_CHANNEL) + 1) * blocks * bitpool
65  + (joint * subbands)) + 7) / 8;
66 
67  avctx->channels = channels;
68  avctx->sample_rate = sample_rates[sr];
69  avctx->frame_size = subbands * blocks;
70  s->duration = avctx->frame_size;
71  return length;
72 }
73 
75  const uint8_t **poutbuf, int *poutbuf_size,
76  const uint8_t *buf, int buf_size)
77 {
78  SBCParseContext *pc = s->priv_data;
79  int next;
80 
81  if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
82  next = buf_size;
83  } else {
84  if (pc->header_size) {
85  memcpy(pc->header + pc->header_size, buf,
86  sizeof(pc->header) - pc->header_size);
87  next = sbc_parse_header(s, avctx, pc->header, sizeof(pc->header))
88  - pc->buffered_size;
89  pc->header_size = 0;
90  } else {
91  next = sbc_parse_header(s, avctx, buf, buf_size);
92  if (next >= buf_size)
93  next = -1;
94  }
95 
96  if (next < 0) {
97  pc->header_size = FFMIN(sizeof(pc->header), buf_size);
98  memcpy(pc->header, buf, pc->header_size);
99  pc->buffered_size = buf_size;
100  next = END_NOT_FOUND;
101  }
102 
103  if (ff_combine_frame(&pc->pc, next, &buf, &buf_size) < 0) {
104  *poutbuf = NULL;
105  *poutbuf_size = 0;
106  return buf_size;
107  }
108  }
109 
110  *poutbuf = buf;
111  *poutbuf_size = buf_size;
112  return next;
113 }
114 
116  .codec_ids = { AV_CODEC_ID_SBC },
117  .priv_data_size = sizeof(SBCParseContext),
118  .parser_parse = sbc_parse,
119  .parser_close = ff_parse_close,
120 };
AVCodecContext::frame_size
int frame_size
Number of samples per channel in an audio frame.
Definition: avcodec.h:1012
MSBC_SYNCWORD
#define MSBC_SYNCWORD
Definition: sbc.h:71
AVCodecContext::sample_rate
int sample_rate
samples per second
Definition: avcodec.h:992
ff_parse_close
void ff_parse_close(AVCodecParserContext *s)
Definition: parser.c:286
data
const char data[16]
Definition: mxf.c:143
subbands
subbands
Definition: aptx.h:39
ff_sbc_parser
const AVCodecParser ff_sbc_parser
Definition: sbc_parser.c:115
ParseContext
Definition: parser.h:28
SBCParseContext::header_size
int header_size
Definition: sbc_parser.c:29
AV_CODEC_ID_SBC
@ AV_CODEC_ID_SBC
Definition: codec_id.h:510
sbc_parse_header
static int sbc_parse_header(AVCodecParserContext *s, AVCodecContext *avctx, const uint8_t *data, size_t len)
Definition: sbc_parser.c:33
s
#define s(width, name)
Definition: cbs_vp9.c:257
SBCParseContext
Definition: sbc_parser.c:26
channels
channels
Definition: aptx.h:33
SBC_SYNCWORD
#define SBC_SYNCWORD
Definition: sbc.h:70
NULL
#define NULL
Definition: coverity.c:32
AVCodecParser::codec_ids
int codec_ids[7]
Definition: avcodec.h:2935
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:201
sample_rates
sample_rates
Definition: ffmpeg_filter.c:153
AVCodecContext::channels
int channels
number of audio channels
Definition: avcodec.h:993
SBCParseContext::header
uint8_t header[3]
Definition: sbc_parser.c:28
PARSER_FLAG_COMPLETE_FRAMES
#define PARSER_FLAG_COMPLETE_FRAMES
Definition: avcodec.h:2809
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
parser.h
len
int len
Definition: vorbis_enc_data.h:426
AVCodecParserContext
Definition: avcodec.h:2775
AVCodecContext
main external API structure.
Definition: avcodec.h:383
mode
mode
Definition: ebur128.h:83
SBCParseContext::pc
ParseContext pc
Definition: sbc_parser.c:27
SBC_MODE_DUAL_CHANNEL
#define SBC_MODE_DUAL_CHANNEL
Definition: sbc.h:57
END_NOT_FOUND
#define END_NOT_FOUND
Definition: parser.h:40
AVCodecParser
Definition: avcodec.h:2934
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:74
SBCParseContext::buffered_size
int buffered_size
Definition: sbc_parser.c:30