FFmpeg
opus_parser.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013-2014 Mozilla Corporation
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  * Opus parser
24  *
25  * Determines the duration for each packet.
26  */
27 
28 #include "avcodec.h"
29 #include "bytestream.h"
30 #include "opus.h"
31 #include "opus_parse.h"
32 #include "parser.h"
33 
34 typedef struct OpusParserContext {
41 
42 static const uint8_t *parse_opus_ts_header(const uint8_t *start, int *payload_len, int buf_len)
43 {
44  const uint8_t *buf = start + 1;
45  int start_trim_flag, end_trim_flag, control_extension_flag, control_extension_length;
46  uint8_t flags;
47  uint64_t payload_len_tmp;
48 
49  GetByteContext gb;
50  bytestream2_init(&gb, buf, buf_len);
51 
52  flags = bytestream2_get_byte(&gb);
53  start_trim_flag = (flags >> 4) & 1;
54  end_trim_flag = (flags >> 3) & 1;
55  control_extension_flag = (flags >> 2) & 1;
56 
57  payload_len_tmp = *payload_len = 0;
58  while (bytestream2_peek_byte(&gb) == 0xff)
59  payload_len_tmp += bytestream2_get_byte(&gb);
60 
61  payload_len_tmp += bytestream2_get_byte(&gb);
62 
63  if (start_trim_flag)
64  bytestream2_skip(&gb, 2);
65  if (end_trim_flag)
66  bytestream2_skip(&gb, 2);
67  if (control_extension_flag) {
68  control_extension_length = bytestream2_get_byte(&gb);
69  bytestream2_skip(&gb, control_extension_length);
70  }
71 
72  if (bytestream2_tell(&gb) + payload_len_tmp > buf_len)
73  return NULL;
74 
75  *payload_len = payload_len_tmp;
76 
77  return buf + bytestream2_tell(&gb);
78 }
79 
80 /**
81  * Find the end of the current frame in the bitstream.
82  * @return the position of the first byte of the next frame, or -1
83  */
85  const uint8_t *buf, int buf_size, int *header_len)
86 {
88  ParseContext *pc = &s->pc;
89  int ret, start_found, i = 0, payload_len = 0;
90  const uint8_t *payload;
91  uint32_t state;
92  uint16_t hdr;
93  *header_len = 0;
94 
95  if (!buf_size)
96  return 0;
97 
98  start_found = pc->frame_start_found;
99  state = pc->state;
100  payload = buf;
101 
102  /* Check if we're using Opus in MPEG-TS framing */
103  if (!s->ts_framing && buf_size > 2) {
104  hdr = AV_RB16(buf);
105  if ((hdr & OPUS_TS_MASK) == OPUS_TS_HEADER)
106  s->ts_framing = 1;
107  }
108 
109  if (s->ts_framing && !start_found) {
110  for (i = 0; i < buf_size-2; i++) {
111  state = (state << 8) | payload[i];
112  if ((state & OPUS_TS_MASK) == OPUS_TS_HEADER) {
113  payload = parse_opus_ts_header(payload, &payload_len, buf_size - i);
114  if (!payload) {
115  av_log(avctx, AV_LOG_ERROR, "Error parsing Ogg TS header.\n");
116  return AVERROR_INVALIDDATA;
117  }
118  *header_len = payload - buf;
119  start_found = 1;
120  break;
121  }
122  }
123  }
124 
125  if (!s->ts_framing)
126  payload_len = buf_size;
127 
128  if (avctx->extradata && !s->extradata_parsed) {
129  ret = ff_opus_parse_extradata(avctx, &s->ctx);
130  if (ret < 0) {
131  av_log(avctx, AV_LOG_ERROR, "Error parsing Ogg extradata.\n");
132  return AVERROR_INVALIDDATA;
133  }
134  av_freep(&s->ctx.channel_maps);
135  s->extradata_parsed = 1;
136  }
137 
138  if (payload_len <= buf_size && (!s->ts_framing || start_found)) {
139  ret = ff_opus_parse_packet(&s->pkt, payload, payload_len, s->ctx.nb_streams > 1);
140  if (ret < 0) {
141  av_log(avctx, AV_LOG_ERROR, "Error parsing Opus packet header.\n");
142  pc->frame_start_found = 0;
143  return AVERROR_INVALIDDATA;
144  }
145 
146  ctx->duration = s->pkt.frame_count * s->pkt.frame_duration;
147  }
148 
149  if (s->ts_framing) {
150  if (start_found) {
151  if (payload_len + *header_len <= buf_size) {
152  pc->frame_start_found = 0;
153  pc->state = -1;
154  return payload_len + *header_len;
155  }
156  }
157 
158  pc->frame_start_found = start_found;
159  pc->state = state;
160  return END_NOT_FOUND;
161  }
162 
163  return buf_size;
164 }
165 
167  const uint8_t **poutbuf, int *poutbuf_size,
168  const uint8_t *buf, int buf_size)
169 {
171  ParseContext *pc = &s->pc;
172  int next, header_len;
173 
174  next = opus_find_frame_end(ctx, avctx, buf, buf_size, &header_len);
175 
176  if (s->ts_framing && next != AVERROR_INVALIDDATA &&
177  ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
178  *poutbuf = NULL;
179  *poutbuf_size = 0;
180  return buf_size;
181  }
182 
183  if (next == AVERROR_INVALIDDATA){
184  *poutbuf = NULL;
185  *poutbuf_size = 0;
186  return buf_size;
187  }
188 
189  *poutbuf = buf + header_len;
190  *poutbuf_size = buf_size - header_len;
191  return next;
192 }
193 
196  .priv_data_size = sizeof(OpusParserContext),
197  .parser_parse = opus_parse,
198  .parser_close = ff_parse_close
199 };
OpusParserContext::extradata_parsed
int extradata_parsed
Definition: opus_parser.c:38
OPUS_TS_MASK
#define OPUS_TS_MASK
Definition: opus.h:33
GetByteContext
Definition: bytestream.h:33
ff_parse_close
void ff_parse_close(AVCodecParserContext *s)
Definition: parser.c:288
opus_find_frame_end
static int opus_find_frame_end(AVCodecParserContext *ctx, AVCodecContext *avctx, const uint8_t *buf, int buf_size, int *header_len)
Find the end of the current frame in the bitstream.
Definition: opus_parser.c:84
opus.h
OpusParserContext
Definition: opus_parser.c:34
OpusParserContext::ts_framing
int ts_framing
Definition: opus_parser.c:39
ParseContext::state
uint32_t state
contains the last few bytes in MSB order
Definition: parser.h:33
opus_parse.h
bytestream2_skip
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition: bytestream.h:168
ParseContext
Definition: parser.h:28
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
s
#define s(width, name)
Definition: cbs_vp9.c:198
ctx
AVFormatContext * ctx
Definition: movenc.c:48
parse_opus_ts_header
static const uint8_t * parse_opus_ts_header(const uint8_t *start, int *payload_len, int buf_len)
Definition: opus_parser.c:42
OpusParserContext::ctx
OpusParseContext ctx
Definition: opus_parser.c:36
OpusParserContext::pc
ParseContext pc
Definition: opus_parser.c:35
NULL
#define NULL
Definition: coverity.c:32
OPUS_TS_HEADER
#define OPUS_TS_HEADER
Definition: opus.h:32
opus_parse
static int opus_parse(AVCodecParserContext *ctx, AVCodecContext *avctx, const uint8_t **poutbuf, int *poutbuf_size, const uint8_t *buf, int buf_size)
Definition: opus_parser.c:166
ParseContext::frame_start_found
int frame_start_found
Definition: parser.h:34
OpusParseContext
Definition: opus_parse.h:63
OpusParserContext::pkt
OpusPacket pkt
Definition: opus_parser.c:37
bytestream2_tell
static av_always_inline int bytestream2_tell(GetByteContext *g)
Definition: bytestream.h:192
AVCodecParser::codec_ids
int codec_ids[7]
Definition: avcodec.h:2934
AV_CODEC_ID_OPUS
@ AV_CODEC_ID_OPUS
Definition: codec_id.h:502
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:203
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:255
AVCodecContext::extradata
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:542
parser.h
avcodec.h
AVCodecParserContext
Definition: avcodec.h:2774
ret
ret
Definition: filter_design.txt:187
AVCodecContext
main external API structure.
Definition: avcodec.h:441
OpusPacket
Definition: opus_parse.h:31
AVFormatContext::duration
int64_t duration
Duration of the stream, in AV_TIME_BASE fractional seconds.
Definition: avformat.h:1217
state
static struct @362 state
END_NOT_FOUND
#define END_NOT_FOUND
Definition: parser.h:40
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
ff_opus_parse_extradata
av_cold int ff_opus_parse_extradata(AVCodecContext *avctx, OpusParseContext *s)
Definition: opus_parse.c:296
AVCodecParser
Definition: avcodec.h:2933
bytestream.h
ff_opus_parser
const AVCodecParser ff_opus_parser
Definition: opus_parser.c:194
bytestream2_init
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:137
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:474
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
AVFormatContext::priv_data
void * priv_data
Format private data.
Definition: avformat.h:1143
ff_opus_parse_packet
int ff_opus_parse_packet(OpusPacket *pkt, const uint8_t *buf, int buf_size, int self_delimiting)
Parse Opus packet info from raw packet data.
Definition: opus_parse.c:95
AV_RB16
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_WB24 unsigned int_TMPL AV_RB16
Definition: bytestream.h:98