qml: Create MediaGroupDisplay
[vlc.git] / modules / demux / ogg_granule.c
blob87b05d5f3e001edee55c868a2687f7fbf18e5ca2
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 *****************************************************************************/
20 #ifdef HAVE_CONFIG_H
21 # include "config.h"
22 #endif
24 #ifdef HAVE_LIBVORBIS
25 #include <vorbis/codec.h>
26 #endif
28 #include <ogg/ogg.h>
30 #include <vlc_common.h>
31 #include <vlc_codecs.h>
32 #include <vlc_es.h>
34 #include "ogg.h"
35 #include "ogg_granule.h"
37 /* Theora spec 7.1 */
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 )
53 return false;
54 else
55 return !( p_packet->packet[0] & THEORA_FTYPE_INTERFRAME );
56 case VLC_CODEC_VP8:
57 return ( ( ( p_packet->granulepos >> 3 ) & 0x07FFFFFF ) == 0 );
58 case VLC_CODEC_DIRAC:
59 if( p_stream->special.dirac.b_old )
60 return (p_packet->granulepos & 0x3FFFFFFF) == 0;
61 else
62 return (p_packet->granulepos & 0xFF8000FF) == 0;
63 default:
64 return true;
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:
77 case VLC_CODEC_DAALA:
78 return ( i_granule >> p_stream->i_granule_shift ) << p_stream->i_granule_shift;
79 case VLC_CODEC_DIRAC:
80 if( p_stream->special.dirac.b_old )
81 return ( i_granule >> 30 ) << 30;
82 else
83 return ( i_granule >> 31 ) << 31;
84 default:
85 /* No change, that's keyframe */
86 return i_granule;
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;
94 else
95 return -1;
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 )
104 i_granule++;
105 /* fallthrough */
106 case VLC_CODEC_DAALA:
107 case VLC_CODEC_KATE:
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;
113 case VLC_CODEC_VP8:
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);
119 else
120 return (i_granule >> 31);
121 case VLC_CODEC_OPUS:
122 case VLC_CODEC_VORBIS:
123 case VLC_CODEC_SPEEX:
124 case VLC_CODEC_FLAC:
125 return i_granule/* - p_stream->i_pre_skip*/;
126 default:
127 return i_granule;
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);
141 else
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);
148 else
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 )
162 return -1;
165 return i_sample;
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 );
171 if( i_sample < 0 )
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 );
192 if( b_pts )
194 int64_t i_delta = Ogg_GranuleToSampleDelta( p_stream, i_granule );
195 if( i_delta != -1 )
196 i_sample += i_delta;
198 return Ogg_SampleToTime( p_stream, i_sample, b_start );