FFmpeg
Loading...
Searching...
No Matches
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 "libavutil/mem.h"
29
30#include "libavcodec/avcodec.h"
32#include "libavcodec/parser.h"
34
35#include "opus.h"
36#include "parse.h"
37
45
46static const uint8_t *parse_opus_ts_header(const uint8_t *start, int *payload_len, int buf_len)
47{
48 const uint8_t *buf = start + 1;
49 int start_trim_flag, end_trim_flag, control_extension_flag, control_extension_length;
50 uint8_t flags;
51 uint64_t payload_len_tmp;
52
54 bytestream2_init(&gb, buf, buf_len);
55
56 flags = bytestream2_get_byte(&gb);
57 start_trim_flag = (flags >> 4) & 1;
58 end_trim_flag = (flags >> 3) & 1;
59 control_extension_flag = (flags >> 2) & 1;
60
61 payload_len_tmp = *payload_len = 0;
62 while (bytestream2_peek_byte(&gb) == 0xff)
63 payload_len_tmp += bytestream2_get_byte(&gb);
64
65 payload_len_tmp += bytestream2_get_byte(&gb);
66
67 if (start_trim_flag)
68 bytestream2_skip(&gb, 2);
69 if (end_trim_flag)
70 bytestream2_skip(&gb, 2);
71 if (control_extension_flag) {
72 control_extension_length = bytestream2_get_byte(&gb);
73 bytestream2_skip(&gb, control_extension_length);
74 }
75
76 if (bytestream2_tell(&gb) + payload_len_tmp > buf_len)
77 return NULL;
78
79 *payload_len = payload_len_tmp;
80
81 return buf + bytestream2_tell(&gb);
82}
83
85 const uint8_t *buf, int buf_size)
86{
87 OpusParserContext *s = ctx->priv_data;
88
89 if (ff_opus_parse_packet(&s->pkt, buf, buf_size, s->ctx.nb_streams > 1) < 0) {
90 av_log(avctx, AV_LOG_ERROR, "Error parsing Opus packet header.\n");
92 }
93
94 ctx->duration = av_rescale(s->pkt.frame_count * s->pkt.frame_duration,
96
97 return 0;
98}
99
100/**
101 * Find the end of the current frame in the bitstream.
102 * @return the position of the first byte of the next frame, or -1
103 */
105 const uint8_t *buf, int buf_size, int *header_len)
106{
107 OpusParserContext *s = ctx->priv_data;
108 ParseContext *pc = &s->pc;
109 int ret, start_found, i = 0, payload_len = 0;
110 const uint8_t *payload;
111 uint32_t state;
112 uint16_t hdr;
113 *header_len = 0;
114
115 if (!buf_size)
116 return 0;
117
118 start_found = pc->frame_start_found;
119 state = pc->state;
120 payload = buf;
121
122 /* Check if we're using Opus in MPEG-TS framing */
123 if (!s->ts_framing && buf_size > 2) {
124 hdr = AV_RB16(buf);
125 if ((hdr & OPUS_TS_MASK) == OPUS_TS_HEADER)
126 s->ts_framing = 1;
127 }
128
129 if (s->ts_framing && !start_found) {
130 for (i = 0; i < buf_size-2; i++) {
131 state = (state << 8) | payload[i];
132 if ((state & OPUS_TS_MASK) == OPUS_TS_HEADER) {
133 payload = parse_opus_ts_header(payload, &payload_len, buf_size - i);
134 if (!payload) {
135 av_log(avctx, AV_LOG_ERROR, "Error parsing Ogg TS header.\n");
136 return AVERROR_INVALIDDATA;
137 }
138 *header_len = payload - buf;
139 start_found = 1;
140 break;
141 }
142 }
143 }
144
145 if (!s->ts_framing)
146 payload_len = buf_size;
147
148 if (payload_len <= buf_size && (!s->ts_framing || start_found)) {
149 ret = set_frame_duration(ctx, avctx, payload, payload_len);
150 if (ret < 0) {
151 pc->frame_start_found = 0;
152 return AVERROR_INVALIDDATA;
153 }
154 }
155
156 if (s->ts_framing) {
157 if (start_found) {
158 if (payload_len + *header_len <= buf_size) {
159 pc->frame_start_found = 0;
160 pc->state = -1;
161 return payload_len + *header_len;
162 }
163 }
164
165 pc->frame_start_found = start_found;
166 pc->state = state;
167 return END_NOT_FOUND;
168 }
169
170 return buf_size;
171}
172
174 const uint8_t **poutbuf, int *poutbuf_size,
175 const uint8_t *buf, int buf_size)
176{
177 OpusParserContext *s = ctx->priv_data;
178 ParseContext *pc = &s->pc;
179 int next, header_len = 0;
180
181 if (!avctx->sample_rate)
182 avctx->sample_rate = 48000;
183
184 if (avctx->extradata && !s->extradata_parsed) {
185 if (ff_opus_parse_extradata(avctx, &s->ctx, avctx->extradata,
186 avctx->extradata_size) < 0) {
187 av_log(avctx, AV_LOG_ERROR, "Error parsing Ogg extradata.\n");
188 goto fail;
189 }
190 av_freep(&s->ctx.channel_maps);
191 s->extradata_parsed = 1;
192 }
193
194 if (ctx->flags & PARSER_FLAG_COMPLETE_FRAMES) {
195 next = buf_size;
196
197 if (buf_size && set_frame_duration(ctx, avctx, buf, buf_size) < 0)
198 goto fail;
199 } else {
200 next = opus_find_frame_end(ctx, avctx, buf, buf_size, &header_len);
201
202 if (s->ts_framing && next != AVERROR_INVALIDDATA &&
203 ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
204 goto fail;
205 }
206
207 if (next == AVERROR_INVALIDDATA){
208 goto fail;
209 }
210 }
211
212 *poutbuf = buf + header_len;
213 *poutbuf_size = buf_size - header_len;
214 return next;
215
216fail:
217 *poutbuf = NULL;
218 *poutbuf_size = 0;
219 return buf_size;
220}
221
224 .priv_data_size = sizeof(OpusParserContext),
225 .parse = opus_parse,
227};
static AVFormatContext * ctx
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:92
Libavcodec external API header.
#define PARSER_FLAG_COMPLETE_FRAMES
Definition avcodec.h:2672
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition bytestream.h:137
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition bytestream.h:168
static av_always_inline int bytestream2_tell(const GetByteContext *g)
Definition bytestream.h:192
#define flags(name, subs,...)
Definition cbs_h264.c:74
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
#define NULL
Definition coverity.c:32
int ret
Definition ffplay.c:391
#define fail
Definition test.h:479
@ AV_CODEC_ID_OPUS
Definition codec_id.h:519
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
int64_t av_rescale(int64_t a, int64_t b, int64_t c)
Rescale a 64-bit integer with rounding to nearest.
#define AV_RB16(p)
av_cold void CBS_FUNC close(CodedBitstreamContext **ctx_ptr)
Close a context and free all internal state.
Definition cbs.c:118
Memory handling functions.
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 parse.c:85
av_cold int ff_opus_parse_extradata(AVCodecContext *avctx, OpusParseContext *s, const uint8_t *header, int header_size)
Definition parse.c:286
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 parser.c:104
const FFCodecParser ff_opus_parser
Definition parser.c:222
static int set_frame_duration(AVCodecParserContext *ctx, AVCodecContext *avctx, const uint8_t *buf, int buf_size)
Definition parser.c:84
static int opus_parse(AVCodecParserContext *ctx, AVCodecContext *avctx, const uint8_t **poutbuf, int *poutbuf_size, const uint8_t *buf, int buf_size)
Definition parser.c:173
static const uint8_t * parse_opus_ts_header(const uint8_t *start, int *payload_len, int buf_len)
Definition parser.c:46
#define OPUS_TS_HEADER
Definition opus.h:32
#define OPUS_TS_MASK
Definition opus.h:33
av_cold void ff_parse_close(AVCodecParserContext *s)
Definition parser.c:300
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:213
#define END_NOT_FOUND
Definition parser.h:40
#define PARSER_CODEC_LIST(...)
main external API structure.
Definition avcodec.h:443
int sample_rate
samples per second
Definition avcodec.h:1044
uint8_t * extradata
Out-of-band global headers that may be used by some codecs.
Definition avcodec.h:526
int extradata_size
Definition avcodec.h:527
OpusPacket pkt
Definition parser.c:41
ParseContext pc
Definition parser.c:39
OpusParseContext ctx
Definition parser.c:40
int extradata_parsed
Definition parser.c:42
uint32_t state
contains the last few bytes in MSB order
Definition parser.h:33
int frame_start_found
Definition parser.h:34
#define OPUS_TIMESTAMP_RATE
The clock of Opus: packet lengths, pre-skip and trimming count samples at 48 kHz whatever the sample ...
Definition tab.h:35
#define av_freep(p)
#define av_log(a,...)
static struct @232240247175332074307271266321204261331170211232 state