1 /*****************************************************************************
2 * ogg_granule.c : ogg granule functions
3 *****************************************************************************
4 * Copyright (C) 2008 - 2018 VideoLAN Authors and VideoLabs
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation; either version 2.1 of the License, or
9 * (at your option) any later version.
11 * This program 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
14 * GNU Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
19 *****************************************************************************/
25 #include <vorbis/codec.h>
30 #include <vlc_common.h>
31 #include <vlc_codecs.h>
35 #include "ogg_granule.h"
38 #define THEORA_FTYPE_NOTDATA 0x80
39 #define THEORA_FTYPE_INTERFRAME 0x40
41 /* Checks if current packet matches codec keyframe */
42 bool Ogg_IsKeyFrame( const logical_stream_t
*p_stream
, const ogg_packet
*p_packet
)
44 if ( p_stream
->b_oggds
)
46 return ( p_packet
->bytes
> 0 && p_packet
->packet
[0] & PACKET_IS_SYNCPOINT
);
48 else switch ( p_stream
->fmt
.i_codec
)
50 case VLC_CODEC_THEORA
:
51 case VLC_CODEC_DAALA
: /* Same convention used in daala */
52 if ( p_packet
->bytes
<= 0 || p_packet
->packet
[0] & THEORA_FTYPE_NOTDATA
)
55 return !( p_packet
->packet
[0] & THEORA_FTYPE_INTERFRAME
);
57 return ( ( ( p_packet
->granulepos
>> 3 ) & 0x07FFFFFF ) == 0 );
59 if( p_stream
->special
.dirac
.b_old
)
60 return (p_packet
->granulepos
& 0x3FFFFFFF) == 0;
62 return (p_packet
->granulepos
& 0xFF8000FF) == 0;
68 int64_t Ogg_GetKeyframeGranule( const logical_stream_t
*p_stream
, int64_t i_granule
)
70 if ( p_stream
->b_oggds
)
72 return -1; /* We have no way to know */
74 else switch( p_stream
->fmt
.i_codec
)
76 case VLC_CODEC_THEORA
:
78 return ( i_granule
>> p_stream
->i_granule_shift
) << p_stream
->i_granule_shift
;
80 if( p_stream
->special
.dirac
.b_old
)
81 return ( i_granule
>> 30 ) << 30;
83 return ( i_granule
>> 31 ) << 31;
85 /* No change, that's keyframe */
90 static int64_t Ogg_GranuleToSampleDelta( const logical_stream_t
*p_stream
, int64_t i_granule
)
92 if( p_stream
->fmt
.i_codec
== VLC_CODEC_DIRAC
&& !p_stream
->special
.dirac
.b_old
)
93 return (i_granule
>> 9) & 0x1fff;
98 static int64_t Ogg_GranuleToSample( const logical_stream_t
*p_stream
, int64_t i_granule
)
100 switch( p_stream
->fmt
.i_codec
)
102 case VLC_CODEC_THEORA
:
103 if( p_stream
->i_first_frame_index
== 0 && !p_stream
->b_oggds
)
106 case VLC_CODEC_DAALA
:
109 ogg_int64_t iframe
= i_granule
>> p_stream
->i_granule_shift
;
110 ogg_int64_t pframe
= i_granule
- ( iframe
<< p_stream
->i_granule_shift
);
111 return iframe
+ pframe
;
114 case VLC_CODEC_OGGSPOTS
:
115 return i_granule
>> p_stream
->i_granule_shift
;
116 case VLC_CODEC_DIRAC
:
117 if( p_stream
->special
.dirac
.b_old
)
118 return (i_granule
>> 30) + (i_granule
& 0x3FFFFFFF);
120 return (i_granule
>> 31);
122 case VLC_CODEC_VORBIS
:
123 case VLC_CODEC_SPEEX
:
125 return i_granule
/* - p_stream->i_pre_skip*/;
131 static int64_t Ogg_ShiftPacketSample( const logical_stream_t
*p_stream
,
132 int64_t i_sample
, bool b_start
)
134 /* /!\ Packet Granule as sample value ! */
136 /* granule always point to end time of packet
137 Except with OggDS where it is reversed */
138 int64_t i_endtostartoffset
= 0; /* in interval # */
139 if( p_stream
->b_oggds
)
140 i_endtostartoffset
= (b_start
? 0 : 1);
142 i_endtostartoffset
= (b_start
? -1 : 0);
144 if( p_stream
->fmt
.i_cat
== VIDEO_ES
)
146 if( p_stream
->fmt
.i_codec
== VLC_CODEC_DIRAC
) /* points to start */
147 i_sample
+= (p_stream
->special
.dirac
.b_interlaced
? 1 : 2) * (i_endtostartoffset
+ 1);
149 i_sample
+= i_endtostartoffset
* 1;
151 else if( p_stream
->fmt
.i_cat
== AUDIO_ES
)
153 if( p_stream
->fmt
.i_codec
== VLC_CODEC_SPEEX
)
155 i_sample
+= i_endtostartoffset
*
156 p_stream
->special
.speex
.i_framesize
*
157 p_stream
->special
.speex
.i_framesperpacket
;
159 else /* we can't tell */
161 if( i_endtostartoffset
!= 0 )
168 vlc_tick_t
Ogg_SampleToTime( const logical_stream_t
*p_stream
, int64_t i_sample
, bool b_start
)
170 i_sample
= Ogg_ShiftPacketSample( p_stream
, i_sample
, b_start
);
172 return VLC_TICK_INVALID
;
174 date_t d
= p_stream
->dts
;
175 date_Set(&d
, VLC_TICK_0
);
176 return date_Increment( &d
, i_sample
);
179 bool Ogg_GranuleIsValid( const logical_stream_t
*p_stream
, int64_t i_granule
)
181 /* First frame in ogm is 0 (0[header] 0[frame] -1 2 3 -1 5 ...) */
182 return !( i_granule
< p_stream
->i_first_frame_index
- !!p_stream
->b_oggds
);
185 vlc_tick_t
Ogg_GranuleToTime( const logical_stream_t
*p_stream
, int64_t i_granule
,
186 bool b_start
, bool b_pts
)
188 if( !Ogg_GranuleIsValid( p_stream
, i_granule
) )
189 return VLC_TICK_INVALID
;
191 int64_t i_sample
= Ogg_GranuleToSample( p_stream
, i_granule
);
194 int64_t i_delta
= Ogg_GranuleToSampleDelta( p_stream
, i_granule
);
198 return Ogg_SampleToTime( p_stream
, i_sample
, b_start
);