FFmpeg  4.0
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 {
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;
46  avctx->sample_rate = 16000;
47  avctx->frame_size = 120;
48  s->duration = avctx->frame_size;
49  return 57;
50  }
51 
52  if (data[0] != SBC_SYNCWORD)
53  return -2;
54 
55  sr = (data[1] >> 6) & 0x03;
56  blocks = (((data[1] >> 4) & 0x03) + 1) << 2;
57  mode = (data[1] >> 2) & 0x03;
58  subbands = (((data[1] >> 0) & 0x01) + 1) << 2;
59  bitpool = data[2];
60 
61  channels = mode == SBC_MODE_MONO ? 1 : 2;
62  joint = mode == SBC_MODE_JOINT_STEREO;
63 
64  length = 4 + (subbands * channels) / 2
65  + ((((mode == SBC_MODE_DUAL_CHANNEL) + 1) * blocks * bitpool
66  + (joint * subbands)) + 7) / 8;
67 
68  avctx->channels = channels;
70  avctx->sample_rate = sample_rates[sr];
71  avctx->frame_size = subbands * blocks;
72  s->duration = avctx->frame_size;
73  return length;
74 }
75 
77  const uint8_t **poutbuf, int *poutbuf_size,
78  const uint8_t *buf, int buf_size)
79 {
81  int next;
82 
84  next = buf_size;
85  } else {
86  if (pc->header_size) {
87  memcpy(pc->header + pc->header_size, buf,
88  sizeof(pc->header) - pc->header_size);
89  next = sbc_parse_header(s, avctx, pc->header, sizeof(pc->header))
90  - pc->buffered_size;
91  pc->header_size = 0;
92  } else {
93  next = sbc_parse_header(s, avctx, buf, buf_size);
94  if (next >= buf_size)
95  next = -1;
96  }
97 
98  if (next < 0) {
99  pc->header_size = FFMIN(sizeof(pc->header), buf_size);
100  memcpy(pc->header, buf, pc->header_size);
101  pc->buffered_size = buf_size;
102  next = END_NOT_FOUND;
103  }
104 
105  if (ff_combine_frame(&pc->pc, next, &buf, &buf_size) < 0) {
106  *poutbuf = NULL;
107  *poutbuf_size = 0;
108  return buf_size;
109  }
110  }
111 
112  *poutbuf = buf;
113  *poutbuf_size = buf_size;
114  return next;
115 }
116 
118  .codec_ids = { AV_CODEC_ID_SBC },
119  .priv_data_size = sizeof(SBCParseContext),
120  .parser_parse = sbc_parse,
121  .parser_close = ff_parse_close,
122 };
#define NULL
Definition: coverity.c:32
const char * s
Definition: avisynth_c.h:768
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:76
channels
Definition: aptx.c:30
uint8_t header[3]
Definition: sbc_parser.c:28
int codec_ids[5]
Definition: avcodec.h:5200
int duration
Duration of the current frame.
Definition: avcodec.h:5154
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:2181
uint8_t
const char data[16]
Definition: mxf.c:90
#define SBC_MODE_MONO
Definition: sbc.h:54
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:319
ParseContext pc
Definition: sbc_parser.c:27
void ff_parse_close(AVCodecParserContext *s)
Definition: parser.c:397
#define FFMIN(a, b)
Definition: common.h:96
#define SBC_MODE_JOINT_STEREO
Definition: sbc.h:57
static int sbc_parse_header(AVCodecParserContext *s, AVCodecContext *avctx, const uint8_t *data, size_t len)
Definition: sbc_parser.c:33
int frame_size
Number of samples per channel in an audio frame.
Definition: avcodec.h:2193
#define MSBC_SYNCWORD
Definition: sbc.h:69
int sample_rate
samples per second
Definition: avcodec.h:2173
#define SBC_MODE_DUAL_CHANNEL
Definition: sbc.h:55
main external API structure.
Definition: avcodec.h:1518
void * buf
Definition: avisynth_c.h:690
AVCodecParser ff_sbc_parser
Definition: sbc_parser.c:117
#define END_NOT_FOUND
Definition: parser.h:40
sample_rates
signed 16 bits
Definition: samplefmt.h:61
#define PARSER_FLAG_COMPLETE_FRAMES
Definition: avcodec.h:5066
SBC common definitions for the encoder and decoder.
int len
int channels
number of audio channels
Definition: avcodec.h:2174
subbands
Definition: aptx.c:36
const char int length
Definition: avisynth_c.h:768
mode
Use these values in ebur128_init (or&#39;ed).
Definition: ebur128.h:83
#define SBC_SYNCWORD
Definition: sbc.h:68