FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 "parser.h"
32 
33 typedef struct OpusParseContext {
40 
41 static const uint8_t *parse_opus_ts_header(const uint8_t *start, int *payload_len, int buf_len)
42 {
43  const uint8_t *buf = start + 1;
44  int start_trim_flag, end_trim_flag, control_extension_flag, control_extension_length;
45  uint8_t flags;
46  uint64_t payload_len_tmp;
47 
48  GetByteContext gb;
49  bytestream2_init(&gb, buf, buf_len);
50 
51  flags = bytestream2_get_byte(&gb);
52  start_trim_flag = (flags >> 4) & 1;
53  end_trim_flag = (flags >> 3) & 1;
54  control_extension_flag = (flags >> 2) & 1;
55 
56  payload_len_tmp = *payload_len = 0;
57  while (bytestream2_peek_byte(&gb) == 0xff)
58  payload_len_tmp += bytestream2_get_byte(&gb);
59 
60  payload_len_tmp += bytestream2_get_byte(&gb);
61 
62  if (start_trim_flag)
63  bytestream2_skip(&gb, 2);
64  if (end_trim_flag)
65  bytestream2_skip(&gb, 2);
66  if (control_extension_flag) {
67  control_extension_length = bytestream2_get_byte(&gb);
68  bytestream2_skip(&gb, control_extension_length);
69  }
70 
71  if (bytestream2_tell(&gb) + payload_len_tmp > buf_len)
72  return NULL;
73 
74  *payload_len = payload_len_tmp;
75 
76  return buf + bytestream2_tell(&gb);
77 }
78 
79 /**
80  * Find the end of the current frame in the bitstream.
81  * @return the position of the first byte of the next frame, or -1
82  */
84  const uint8_t *buf, int buf_size, int *header_len)
85 {
87  ParseContext *pc = &s->pc;
88  int ret, start_found, i = 0, payload_len = 0;
89  const uint8_t *payload;
90  uint32_t state;
91  uint16_t hdr;
92  *header_len = 0;
93 
94  if (!buf_size)
95  return 0;
96 
97  start_found = pc->frame_start_found;
98  state = pc->state;
99  payload = buf;
100 
101  /* Check if we're using Opus in MPEG-TS framing */
102  if (!s->ts_framing && buf_size > 2) {
103  hdr = AV_RB16(buf);
104  if ((hdr & OPUS_TS_MASK) == OPUS_TS_HEADER)
105  s->ts_framing = 1;
106  }
107 
108  if (s->ts_framing && !start_found) {
109  for (i = 0; i < buf_size-2; i++) {
110  state = (state << 8) | payload[i];
111  if ((state & OPUS_TS_MASK) == OPUS_TS_HEADER) {
112  payload = parse_opus_ts_header(payload, &payload_len, buf_size - i);
113  if (!payload) {
114  av_log(avctx, AV_LOG_ERROR, "Error parsing Ogg TS header.\n");
115  return AVERROR_INVALIDDATA;
116  }
117  *header_len = payload - buf;
118  start_found = 1;
119  break;
120  }
121  }
122  }
123 
124  if (!s->ts_framing)
125  payload_len = buf_size;
126 
127  if (avctx->extradata && !s->extradata_parsed) {
128  ret = ff_opus_parse_extradata(avctx, &s->ctx);
129  if (ret < 0) {
130  av_log(avctx, AV_LOG_ERROR, "Error parsing Ogg extradata.\n");
131  return AVERROR_INVALIDDATA;
132  }
134  s->extradata_parsed = 1;
135  }
136 
137  if (payload_len <= buf_size && (!s->ts_framing || start_found)) {
138  ret = ff_opus_parse_packet(&s->pkt, payload, payload_len, s->ctx.nb_streams > 1);
139  if (ret < 0) {
140  av_log(avctx, AV_LOG_ERROR, "Error parsing Opus packet header.\n");
141  pc->frame_start_found = 0;
142  return AVERROR_INVALIDDATA;
143  }
144 
145  ctx->duration = s->pkt.frame_count * s->pkt.frame_duration;
146  }
147 
148  if (s->ts_framing) {
149  if (start_found) {
150  if (payload_len + *header_len <= buf_size) {
151  pc->frame_start_found = 0;
152  pc->state = -1;
153  return payload_len + *header_len;
154  }
155  }
156 
157  pc->frame_start_found = start_found;
158  pc->state = state;
159  return END_NOT_FOUND;
160  }
161 
162  return buf_size;
163 }
164 
166  const uint8_t **poutbuf, int *poutbuf_size,
167  const uint8_t *buf, int buf_size)
168 {
169  OpusParseContext *s = ctx->priv_data;
170  ParseContext *pc = &s->pc;
171  int next, header_len;
172 
173  next = opus_find_frame_end(ctx, avctx, buf, buf_size, &header_len);
174 
175  if (s->ts_framing && next != AVERROR_INVALIDDATA &&
176  ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
177  *poutbuf = NULL;
178  *poutbuf_size = 0;
179  return buf_size;
180  }
181 
182  if (next == AVERROR_INVALIDDATA){
183  *poutbuf = NULL;
184  *poutbuf_size = 0;
185  return buf_size;
186  }
187 
188  *poutbuf = buf + header_len;
189  *poutbuf_size = buf_size - header_len;
190  return next;
191 }
192 
195  .priv_data_size = sizeof(OpusParseContext),
196  .parser_parse = opus_parse,
197  .parser_close = ff_parse_close
198 };
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.c:90
#define NULL
Definition: coverity.c:32
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
int frame_count
frame count
Definition: opus.h:92
int codec_ids[5]
Definition: avcodec.h:5233
static const uint8_t * parse_opus_ts_header(const uint8_t *start, int *payload_len, int buf_len)
Definition: opus_parser.c:41
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:133
int duration
Duration of the current frame.
Definition: avcodec.h:5187
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:87
int frame_start_found
Definition: parser.h:34
uint8_t
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1641
#define OPUS_TS_HEADER
Definition: opus.h:53
#define av_log(a,...)
#define OPUS_TS_MASK
Definition: opus.h:54
ChannelMap * channel_maps
Definition: opus.h:173
int nb_streams
Definition: opus.h:166
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
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:234
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition: bytestream.h:164
ParseContext pc
Definition: opus_parser.c:34
void ff_parse_close(AVCodecParserContext *s)
Definition: parser.c:312
static struct @303 state
int frame_duration
frame duration, in samples @ 48kHz
Definition: opus.h:95
AVFormatContext * ctx
Definition: movenc.c:48
#define s(width, name)
Definition: cbs_vp9.c:257
static av_always_inline int bytestream2_tell(GetByteContext *g)
Definition: bytestream.h:188
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:165
Libavcodec external API header.
main external API structure.
Definition: avcodec.h:1540
void * buf
Definition: avisynth_c.h:690
uint32_t state
contains the last few bytes in MSB order
Definition: parser.h:33
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:83
#define END_NOT_FOUND
Definition: parser.h:40
#define flags(name, subs,...)
Definition: cbs_av1.c:584
OpusContext ctx
Definition: opus_parser.c:35
OpusPacket pkt
Definition: opus_parser.c:36
AVCodecParser ff_opus_parser
Definition: opus_parser.c:193
av_cold int ff_opus_parse_extradata(AVCodecContext *avctx, OpusContext *s)
Definition: opus.c:291
#define av_freep(p)
void INT64 start
Definition: avisynth_c.h:690