/src/ffmpeg/libavformat/oggparseopus.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Opus parser for Ogg |
3 | | * Copyright (c) 2012 Nicolas George |
4 | | * |
5 | | * This file is part of FFmpeg. |
6 | | * |
7 | | * FFmpeg is free software; you can redistribute it and/or |
8 | | * modify it under the terms of the GNU Lesser General Public |
9 | | * License as published by the Free Software Foundation; either |
10 | | * version 2.1 of the License, or (at your option) any later version. |
11 | | * |
12 | | * FFmpeg is distributed in the hope that it will be useful, |
13 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
15 | | * Lesser General Public License for more details. |
16 | | * |
17 | | * You should have received a copy of the GNU Lesser General Public |
18 | | * License along with FFmpeg; if not, write to the Free Software |
19 | | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
20 | | */ |
21 | | |
22 | | #include <string.h> |
23 | | |
24 | | #include "libavutil/intreadwrite.h" |
25 | | #include "libavutil/mem.h" |
26 | | #include "avformat.h" |
27 | | #include "internal.h" |
28 | | #include "oggdec.h" |
29 | | |
30 | | struct oggopus_private { |
31 | | int need_comments; |
32 | | unsigned pre_skip; |
33 | | int64_t cur_dts; |
34 | | }; |
35 | | |
36 | 3.62k | #define OPUS_SEEK_PREROLL_MS 80 |
37 | 7.40k | #define OPUS_HEAD_SIZE 19 |
38 | | |
39 | | static int opus_header(AVFormatContext *avf, int idx) |
40 | 7.90k | { |
41 | 7.90k | struct ogg *ogg = avf->priv_data; |
42 | 7.90k | struct ogg_stream *os = &ogg->streams[idx]; |
43 | 7.90k | AVStream *st = avf->streams[idx]; |
44 | 7.90k | struct oggopus_private *priv = os->private; |
45 | 7.90k | uint8_t *packet = os->buf + os->pstart; |
46 | 7.90k | int ret; |
47 | | |
48 | 7.90k | if (!priv) { |
49 | 1.18k | priv = os->private = av_mallocz(sizeof(*priv)); |
50 | 1.18k | if (!priv) |
51 | 0 | return AVERROR(ENOMEM); |
52 | 1.18k | } |
53 | | |
54 | 7.90k | if (os->flags & OGG_FLAG_BOS) { |
55 | 3.70k | if (os->psize < OPUS_HEAD_SIZE || (AV_RL8(packet + 8) & 0xF0) != 0) |
56 | 78 | return AVERROR_INVALIDDATA; |
57 | 3.62k | st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO; |
58 | 3.62k | st->codecpar->codec_id = AV_CODEC_ID_OPUS; |
59 | 3.62k | st->codecpar->ch_layout.nb_channels = AV_RL8(packet + 9); |
60 | | |
61 | 3.62k | priv->pre_skip = AV_RL16(packet + 10); |
62 | 3.62k | st->codecpar->initial_padding = priv->pre_skip; |
63 | 3.62k | os->start_trimming = priv->pre_skip; |
64 | | /*orig_sample_rate = AV_RL32(packet + 12);*/ |
65 | | /*gain = AV_RL16(packet + 16);*/ |
66 | | /*channel_map = AV_RL8 (packet + 18);*/ |
67 | | |
68 | 3.62k | if ((ret = ff_alloc_extradata(st->codecpar, os->psize)) < 0) |
69 | 0 | return ret; |
70 | | |
71 | 3.62k | memcpy(st->codecpar->extradata, packet, os->psize); |
72 | | |
73 | 3.62k | st->codecpar->sample_rate = 48000; |
74 | 3.62k | st->codecpar->seek_preroll = av_rescale(OPUS_SEEK_PREROLL_MS, |
75 | 3.62k | st->codecpar->sample_rate, 1000); |
76 | 3.62k | avpriv_set_pts_info(st, 64, 1, 48000); |
77 | 3.62k | priv->need_comments = 1; |
78 | 3.62k | return 1; |
79 | 3.62k | } |
80 | | |
81 | 4.20k | if (priv->need_comments) { |
82 | 2.45k | if (os->psize < 8 || memcmp(packet, "OpusTags", 8)) |
83 | 12 | return AVERROR_INVALIDDATA; |
84 | 2.44k | ff_vorbis_stream_comment(avf, st, packet + 8, os->psize - 8); |
85 | 2.44k | priv->need_comments--; |
86 | 2.44k | return 1; |
87 | 2.45k | } |
88 | | |
89 | 1.75k | return 0; |
90 | 4.20k | } |
91 | | |
92 | | static int opus_duration(uint8_t *src, int size) |
93 | 22.3k | { |
94 | 22.3k | unsigned nb_frames = 1; |
95 | 22.3k | unsigned toc = src[0]; |
96 | 22.3k | unsigned toc_config = toc >> 3; |
97 | 22.3k | unsigned toc_count = toc & 3; |
98 | 22.3k | unsigned frame_size = toc_config < 12 ? FFMAX(480, 960 * (toc_config & 3)) : |
99 | 22.3k | toc_config < 16 ? 480 << (toc_config & 1) : |
100 | 42 | 120 << (toc_config & 3); |
101 | 22.3k | if (toc_count == 3) { |
102 | 17.8k | if (size<2) |
103 | 0 | return AVERROR_INVALIDDATA; |
104 | 17.8k | nb_frames = src[1] & 0x3F; |
105 | 17.8k | } else if (toc_count) { |
106 | 654 | nb_frames = 2; |
107 | 654 | } |
108 | | |
109 | 22.3k | return frame_size * nb_frames; |
110 | 22.3k | } |
111 | | |
112 | | static int opus_packet(AVFormatContext *avf, int idx) |
113 | 10.4k | { |
114 | 10.4k | struct ogg *ogg = avf->priv_data; |
115 | 10.4k | struct ogg_stream *os = &ogg->streams[idx]; |
116 | 10.4k | AVStream *st = avf->streams[idx]; |
117 | 10.4k | struct oggopus_private *priv = os->private; |
118 | 10.4k | uint8_t *packet = os->buf + os->pstart; |
119 | 10.4k | int ret; |
120 | | |
121 | 10.4k | if (!os->psize) |
122 | 0 | return AVERROR_INVALIDDATA; |
123 | 10.4k | if (os->granule > (1LL << 62)) { |
124 | 40 | av_log(avf, AV_LOG_ERROR, "Unsupported huge granule pos %"PRId64 "\n", os->granule); |
125 | 40 | return AVERROR_INVALIDDATA; |
126 | 40 | } |
127 | | |
128 | 10.4k | if ((!os->lastpts || os->lastpts == AV_NOPTS_VALUE) && !(os->flags & OGG_FLAG_EOS)) { |
129 | 7.43k | int seg, d; |
130 | 7.43k | int duration; |
131 | 7.43k | uint8_t *last_pkt = os->buf + os->pstart; |
132 | 7.43k | uint8_t *next_pkt = last_pkt; |
133 | | |
134 | 7.43k | duration = 0; |
135 | 7.43k | seg = os->segp; |
136 | 7.43k | d = opus_duration(last_pkt, os->psize); |
137 | 7.43k | if (d < 0) { |
138 | 0 | os->pflags |= AV_PKT_FLAG_CORRUPT; |
139 | 0 | return 0; |
140 | 0 | } |
141 | 7.43k | duration += d; |
142 | 7.43k | last_pkt = next_pkt = next_pkt + os->psize; |
143 | 11.9k | for (; seg < os->nsegs; seg++) { |
144 | 4.53k | next_pkt += os->segments[seg]; |
145 | 4.53k | if (os->segments[seg] < 255 && next_pkt != last_pkt) { |
146 | 4.53k | int d = opus_duration(last_pkt, next_pkt - last_pkt); |
147 | 4.53k | if (d > 0) |
148 | 4.53k | duration += d; |
149 | 4.53k | last_pkt = next_pkt; |
150 | 4.53k | } |
151 | 4.53k | } |
152 | 7.43k | os->lastpts = |
153 | 7.43k | os->lastdts = os->granule - duration; |
154 | 7.43k | } |
155 | | |
156 | 10.4k | if ((ret = opus_duration(packet, os->psize)) < 0) |
157 | 0 | return ret; |
158 | | |
159 | 10.4k | os->pduration = ret; |
160 | 10.4k | if (os->lastpts != AV_NOPTS_VALUE) { |
161 | 10.3k | if (st->start_time == AV_NOPTS_VALUE) |
162 | 761 | st->start_time = os->lastpts; |
163 | 10.3k | priv->cur_dts = os->lastdts = os->lastpts -= priv->pre_skip; |
164 | 10.3k | } |
165 | | |
166 | 10.4k | priv->cur_dts += os->pduration; |
167 | 10.4k | if ((os->flags & OGG_FLAG_EOS)) { |
168 | 1.18k | int64_t skip = priv->cur_dts - os->granule + priv->pre_skip; |
169 | 1.18k | skip = FFMIN(skip, os->pduration); |
170 | 1.18k | if (skip > 0) { |
171 | 818 | os->pduration = skip < os->pduration ? os->pduration - skip : 1; |
172 | 818 | os->end_trimming = skip; |
173 | 818 | av_log(avf, AV_LOG_DEBUG, |
174 | 818 | "Last packet was truncated to %d due to end trimming.\n", |
175 | 818 | os->pduration); |
176 | 818 | } |
177 | 1.18k | } |
178 | | |
179 | 10.4k | return 0; |
180 | 10.4k | } |
181 | | |
182 | | const struct ogg_codec ff_opus_codec = { |
183 | | .name = "Opus", |
184 | | .magic = "OpusHead", |
185 | | .magicsize = 8, |
186 | | .header = opus_header, |
187 | | .packet = opus_packet, |
188 | | .nb_header = 1, |
189 | | }; |