]> git.sesse.net Git - ffmpeg/blob - libavformat/oggparseopus.c
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / libavformat / oggparseopus.c
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 "avformat.h"
26 #include "internal.h"
27 #include "oggdec.h"
28
29 struct oggopus_private {
30     int need_comments;
31     unsigned pre_skip;
32     int64_t cur_dts;
33 };
34
35 #define OPUS_HEAD_SIZE 19
36
37 static int opus_header(AVFormatContext *avf, int idx)
38 {
39     struct ogg *ogg              = avf->priv_data;
40     struct ogg_stream *os        = &ogg->streams[idx];
41     AVStream *st                 = avf->streams[idx];
42     struct oggopus_private *priv = os->private;
43     uint8_t *packet              = os->buf + os->pstart;
44     uint8_t *extradata;
45
46     if (!priv) {
47         priv = os->private = av_mallocz(sizeof(*priv));
48         if (!priv)
49             return AVERROR(ENOMEM);
50     }
51
52     if (os->flags & OGG_FLAG_BOS) {
53         if (os->psize < OPUS_HEAD_SIZE || (AV_RL8(packet + 8) & 0xF0) != 0)
54             return AVERROR_INVALIDDATA;
55         st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
56         st->codec->codec_id   = AV_CODEC_ID_OPUS;
57         st->codec->channels   = AV_RL8 (packet + 9);
58         priv->pre_skip        = AV_RL16(packet + 10);
59         /*orig_sample_rate    = AV_RL32(packet + 12);*/
60         /*gain                = AV_RL16(packet + 16);*/
61         /*channel_map         = AV_RL8 (packet + 18);*/
62
63         extradata = av_malloc(os->psize + FF_INPUT_BUFFER_PADDING_SIZE);
64         if (!extradata)
65             return AVERROR(ENOMEM);
66
67         memcpy(extradata, packet, os->psize);
68         st->codec->extradata      = extradata;
69         st->codec->extradata_size = os->psize;
70
71         st->codec->sample_rate = 48000;
72         avpriv_set_pts_info(st, 64, 1, 48000);
73         priv->need_comments = 1;
74         return 1;
75     }
76
77     if (priv->need_comments) {
78         if (os->psize < 8 || memcmp(packet, "OpusTags", 8))
79             return AVERROR_INVALIDDATA;
80         ff_vorbis_comment(avf, &st->metadata, packet + 8, os->psize - 8);
81         priv->need_comments--;
82         return 1;
83     }
84
85     return 0;
86 }
87
88 static int opus_packet(AVFormatContext *avf, int idx)
89 {
90     struct ogg *ogg              = avf->priv_data;
91     struct ogg_stream *os        = &ogg->streams[idx];
92     AVStream *st                 = avf->streams[idx];
93     struct oggopus_private *priv = os->private;
94     uint8_t *packet              = os->buf + os->pstart;
95     unsigned toc, toc_config, toc_count, frame_size, nb_frames = 1;
96
97     if (!os->psize)
98         return AVERROR_INVALIDDATA;
99
100     toc        = *packet;
101     toc_config = toc >> 3;
102     toc_count  = toc & 3;
103     frame_size = toc_config < 12 ? FFMAX(480, 960 * (toc_config & 3)) :
104                  toc_config < 16 ? 480 << (toc_config & 1) :
105                                    120 << (toc_config & 3);
106     if (toc_count == 3) {
107         if (os->psize < 2)
108             return AVERROR_INVALIDDATA;
109         nb_frames = packet[1] & 0x3F;
110     } else if (toc_count) {
111         nb_frames = 2;
112     }
113
114     os->pduration = frame_size * nb_frames;
115     if (os->lastpts != AV_NOPTS_VALUE) {
116         if (st->start_time == AV_NOPTS_VALUE)
117             st->start_time = os->lastpts;
118         priv->cur_dts = os->lastdts = os->lastpts -= priv->pre_skip;
119     }
120
121     priv->cur_dts += os->pduration;
122     if ((os->flags & OGG_FLAG_EOS)) {
123         int64_t skip = priv->cur_dts - os->granule + priv->pre_skip;
124         skip = FFMIN(skip, os->pduration);
125         if (skip > 0) {
126             os->pduration = skip < os->pduration ? os->pduration - skip : 1;
127             os->end_trimming = skip;
128             av_log(avf, AV_LOG_DEBUG,
129                    "Last packet was truncated to %d due to end trimming.\n",
130                    os->pduration);
131         }
132     }
133
134     return 0;
135 }
136
137 const struct ogg_codec ff_opus_codec = {
138     .name             = "Opus",
139     .magic            = "OpusHead",
140     .magicsize        = 8,
141     .header           = opus_header,
142     .packet           = opus_packet,
143     .nb_header        = 1,
144 };