]> git.sesse.net Git - ffmpeg/blob - libavformat/oggparseopus.c
avformat/electronicarts: use 64bit variable for avio_tell() result
[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
45     if (!priv) {
46         priv = os->private = av_mallocz(sizeof(*priv));
47         if (!priv)
48             return AVERROR(ENOMEM);
49     }
50
51     if (os->flags & OGG_FLAG_BOS) {
52         if (os->psize < OPUS_HEAD_SIZE || (AV_RL8(packet + 8) & 0xF0) != 0)
53             return AVERROR_INVALIDDATA;
54         st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
55         st->codec->codec_id   = AV_CODEC_ID_OPUS;
56         st->codec->channels   = AV_RL8 (packet + 9);
57         priv->pre_skip        = AV_RL16(packet + 10);
58         st->codec->delay      = priv->pre_skip;
59         /*orig_sample_rate    = AV_RL32(packet + 12);*/
60         /*gain                = AV_RL16(packet + 16);*/
61         /*channel_map         = AV_RL8 (packet + 18);*/
62
63         if (ff_alloc_extradata(st->codec, os->psize))
64             return AVERROR(ENOMEM);
65
66         memcpy(st->codec->extradata, packet, os->psize);
67
68         st->codec->sample_rate = 48000;
69         avpriv_set_pts_info(st, 64, 1, 48000);
70         priv->need_comments = 1;
71         return 1;
72     }
73
74     if (priv->need_comments) {
75         if (os->psize < 8 || memcmp(packet, "OpusTags", 8))
76             return AVERROR_INVALIDDATA;
77         ff_vorbis_comment(avf, &st->metadata, packet + 8, os->psize - 8);
78         priv->need_comments--;
79         return 1;
80     }
81
82     return 0;
83 }
84
85 static int opus_duration(uint8_t *src, int size)
86 {
87     unsigned nb_frames  = 1;
88     unsigned toc        = src[0];
89     unsigned toc_config = toc >> 3;
90     unsigned toc_count  = toc & 3;
91     unsigned frame_size = toc_config < 12 ? FFMAX(480, 960 * (toc_config & 3)) :
92                           toc_config < 16 ? 480 << (toc_config & 1) :
93                                             120 << (toc_config & 3);
94     if (toc_count == 3) {
95         if (size<2)
96             return AVERROR_INVALIDDATA;
97         nb_frames = src[1] & 0x3F;
98     } else if (toc_count) {
99         nb_frames = 2;
100     }
101
102     return frame_size * nb_frames;
103 }
104
105 static int opus_packet(AVFormatContext *avf, int idx)
106 {
107     struct ogg *ogg              = avf->priv_data;
108     struct ogg_stream *os        = &ogg->streams[idx];
109     AVStream *st                 = avf->streams[idx];
110     struct oggopus_private *priv = os->private;
111     uint8_t *packet              = os->buf + os->pstart;
112     int ret;
113
114     if (!os->psize)
115         return AVERROR_INVALIDDATA;
116
117     if ((!os->lastpts || os->lastpts == AV_NOPTS_VALUE) && !(os->flags & OGG_FLAG_EOS)) {
118         int seg, d;
119         int duration;
120         uint8_t *last_pkt  = os->buf + os->pstart;
121         uint8_t *next_pkt  = last_pkt;
122
123         duration = 0;
124         seg = os->segp;
125         d = opus_duration(last_pkt, os->psize);
126         if (d < 0) {
127             os->pflags |= AV_PKT_FLAG_CORRUPT;
128             return 0;
129         }
130         duration += d;
131         last_pkt = next_pkt =  next_pkt + os->psize;
132         for (; seg < os->nsegs; seg++) {
133             if (os->segments[seg] < 255) {
134                 int d = opus_duration(last_pkt, os->segments[seg]);
135                 if (d < 0) {
136                     duration = os->granule;
137                     break;
138                 }
139                 duration += d;
140                 last_pkt  = next_pkt + os->segments[seg];
141             }
142             next_pkt += os->segments[seg];
143         }
144         os->lastpts                 =
145         os->lastdts                 = os->granule - duration;
146     }
147
148     if ((ret = opus_duration(packet, os->psize)) < 0)
149         return ret;
150
151     os->pduration = ret;
152     if (os->lastpts != AV_NOPTS_VALUE) {
153         if (st->start_time == AV_NOPTS_VALUE)
154             st->start_time = os->lastpts;
155         priv->cur_dts = os->lastdts = os->lastpts -= priv->pre_skip;
156     }
157
158     priv->cur_dts += os->pduration;
159     if ((os->flags & OGG_FLAG_EOS)) {
160         int64_t skip = priv->cur_dts - os->granule + priv->pre_skip;
161         skip = FFMIN(skip, os->pduration);
162         if (skip > 0) {
163             os->pduration = skip < os->pduration ? os->pduration - skip : 1;
164             os->end_trimming = skip;
165             av_log(avf, AV_LOG_DEBUG,
166                    "Last packet was truncated to %d due to end trimming.\n",
167                    os->pduration);
168         }
169     }
170
171     return 0;
172 }
173
174 const struct ogg_codec ff_opus_codec = {
175     .name             = "Opus",
176     .magic            = "OpusHead",
177     .magicsize        = 8,
178     .header           = opus_header,
179     .packet           = opus_packet,
180     .nb_header        = 1,
181 };