/src/vlc/modules/demux/mp4/mp4.c
Line | Count | Source |
1 | | /***************************************************************************** |
2 | | * mp4.c : MP4 file input module for vlc |
3 | | ***************************************************************************** |
4 | | * Copyright (C) 2001-2004, 2010 VLC authors and VideoLAN |
5 | | * |
6 | | * Authors: Laurent Aimar <fenrir@via.ecp.fr> |
7 | | * |
8 | | * This program is free software; you can redistribute it and/or modify it |
9 | | * under the terms of the GNU Lesser General Public License as published by |
10 | | * the Free Software Foundation; either version 2.1 of the License, or |
11 | | * (at your option) any later version. |
12 | | * |
13 | | * This program is distributed in the hope that it will be useful, |
14 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | | * GNU Lesser General Public License for more details. |
17 | | * |
18 | | * You should have received a copy of the GNU Lesser General Public License |
19 | | * along with this program; if not, write to the Free Software Foundation, |
20 | | * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA. |
21 | | *****************************************************************************/ |
22 | | #ifdef HAVE_CONFIG_H |
23 | | # include "config.h" |
24 | | #endif |
25 | | |
26 | | /***************************************************************************** |
27 | | * Preamble |
28 | | *****************************************************************************/ |
29 | | #include "mp4.h" |
30 | | |
31 | | #include <vlc_arrays.h> |
32 | | #include <vlc_demux.h> |
33 | | #include <vlc_charset.h> /* EnsureUTF8 */ |
34 | | #include <vlc_input.h> |
35 | | #include <vlc_aout.h> |
36 | | #include <vlc_plugin.h> |
37 | | #include <vlc_dialog.h> |
38 | | #include <vlc_url.h> |
39 | | #include <vlc_replay_gain.h> |
40 | | #include <assert.h> |
41 | | #include <limits.h> |
42 | | #include <math.h> |
43 | | #include "meta.h" |
44 | | #include "attachments.h" |
45 | | #include "heif.h" |
46 | | #include "../../codec/cc.h" |
47 | | #include "../av1_unpack.h" |
48 | | |
49 | | #include <stdlib.h> |
50 | | |
51 | | /***************************************************************************** |
52 | | * Module descriptor |
53 | | *****************************************************************************/ |
54 | | static int Open ( vlc_object_t * ); |
55 | | static void Close( vlc_object_t * ); |
56 | | |
57 | | #define CFG_PREFIX "mp4-" |
58 | | |
59 | | #define MP4_M4A_TEXT N_("M4A audio only") |
60 | | #define MP4_M4A_LONGTEXT N_("Ignore non audio tracks from iTunes audio files") |
61 | | |
62 | | #define MP4_ELST_TEXT N_("Handle edit list") |
63 | | |
64 | | #define HEIF_DURATION_TEXT N_("Duration in seconds") |
65 | | #define HEIF_DURATION_LONGTEXT N_( \ |
66 | | "Duration in seconds before simulating an end of file. " \ |
67 | | "A negative value means an unlimited play time.") |
68 | | |
69 | 108 | vlc_module_begin () |
70 | 54 | set_subcategory( SUBCAT_INPUT_DEMUX ) |
71 | 54 | set_description( N_("MP4 stream demuxer") ) |
72 | 54 | set_shortname( N_("MP4") ) |
73 | 54 | set_capability( "demux", 240 ) |
74 | 108 | set_callbacks( Open, Close ) |
75 | 54 | add_file_extension("m4a") |
76 | 54 | add_file_extension("m4v") |
77 | 54 | add_file_extension("moov") |
78 | 54 | add_file_extension("mov") |
79 | 54 | add_file_extension("mp4") |
80 | | |
81 | 54 | set_section("Hacks", NULL) |
82 | 54 | add_bool( CFG_PREFIX"m4a-audioonly", false, MP4_M4A_TEXT, MP4_M4A_LONGTEXT ) |
83 | 54 | add_bool( CFG_PREFIX"editlist", true, MP4_ELST_TEXT, MP4_ELST_TEXT ) |
84 | | |
85 | 54 | add_submodule() |
86 | 54 | set_subcategory( SUBCAT_INPUT_DEMUX ) |
87 | 54 | set_description( N_("HEIF demuxer") ) |
88 | 54 | set_shortname( "heif" ) |
89 | 54 | set_capability( "demux", 239 ) |
90 | 108 | set_callbacks( OpenHEIF, CloseHEIF ) |
91 | 54 | set_section( N_("HEIF demuxer"), NULL ) |
92 | 54 | add_float( "heif-image-duration", HEIF_DEFAULT_DURATION, |
93 | 54 | HEIF_DURATION_TEXT, HEIF_DURATION_LONGTEXT ) |
94 | 54 | change_safe() |
95 | 54 | vlc_module_end () |
96 | | |
97 | | /***************************************************************************** |
98 | | * Local prototypes |
99 | | *****************************************************************************/ |
100 | | static int Demux ( demux_t * ); |
101 | 0 | static int DemuxRef( demux_t *p_demux ){ (void)p_demux; return 0;} |
102 | | static int DemuxFrag( demux_t * ); |
103 | | static int Control ( demux_t *, int, va_list ); |
104 | | |
105 | | typedef struct |
106 | | { |
107 | | MP4_Box_t *p_root; /* container for the whole file */ |
108 | | |
109 | | vlc_tick_t i_pcr; |
110 | | |
111 | | uint64_t i_moov_duration; |
112 | | uint64_t i_duration; /* Declared fragmented duration (movie time scale) */ |
113 | | uint64_t i_cumulated_duration; /* Same as above, but not from probing, (movie time scale) */ |
114 | | uint32_t i_timescale; /* movie time scale */ |
115 | | vlc_tick_t i_nztime; /* time position of the presentation (CLOCK_FREQ timescale) */ |
116 | | vlc_tick_t i_max_pts_offset; |
117 | | unsigned int i_tracks; /* number of tracks */ |
118 | | mp4_track_t *track; /* array of track */ |
119 | | float f_fps; /* number of frame per seconds */ |
120 | | |
121 | | bool b_quicktime; |
122 | | bool b_fragmented; /* fMP4 */ |
123 | | bool b_seekable; |
124 | | bool b_fastseekable; |
125 | | bool b_error; /* unrecoverable */ |
126 | | |
127 | | bool b_index_probed; /* mFra sync points index */ |
128 | | bool b_fragments_probed; /* moof segments index created */ |
129 | | |
130 | | MP4_Box_t *p_moov; |
131 | | |
132 | | struct |
133 | | { |
134 | | uint32_t i_current_box_type; |
135 | | MP4_Box_t *p_fragment_atom; |
136 | | uint64_t i_post_mdat_offset; |
137 | | uint32_t i_lastseqnumber; |
138 | | } context; |
139 | | |
140 | | /* */ |
141 | | bool seekpoint_changed; |
142 | | int i_seekpoint; |
143 | | input_title_t *p_title; |
144 | | vlc_meta_t *p_meta; |
145 | | struct |
146 | | { |
147 | | uint32_t i_delay_samples; |
148 | | float f_replay_gain_norm; |
149 | | float f_replay_gain_peak; |
150 | | uint32_t i_target_bitrate; |
151 | | } qt; |
152 | | |
153 | | /* ASF in MP4 */ |
154 | | asf_packet_sys_t asfpacketsys; |
155 | | vlc_tick_t i_preroll; /* foobar */ |
156 | | vlc_tick_t i_preroll_start; |
157 | | |
158 | | struct |
159 | | { |
160 | | enum es_format_category_e es_cat_filter; |
161 | | } hacks; |
162 | | |
163 | | mp4_fragments_index_t *p_fragsindex; |
164 | | |
165 | | ssize_t i_attachments; |
166 | | input_attachment_t **pp_attachments; |
167 | | } demux_sys_t; |
168 | | |
169 | 1.61G | #define DEMUX_INCREMENT VLC_TICK_FROM_MS(250) /* How far the pcr will go, each round */ |
170 | 0 | #define DEMUX_TRACK_MAX_PRELOAD VLC_TICK_FROM_SEC(15) /* maximum preloading, to deal with interleaving */ |
171 | | |
172 | 178M | #define INVALID_PRELOAD UINT_MAX |
173 | 362M | #define UNKNOWN_DELTA UINT32_MAX |
174 | 4.87M | #define INVALID_PTS VLC_TICK_MIN |
175 | | |
176 | 1.04G | #define VLC_DEMUXER_EOS (VLC_DEMUXER_EGENERIC - 1) |
177 | 228 | #define VLC_DEMUXER_FATAL (VLC_DEMUXER_EGENERIC - 2) |
178 | | |
179 | | /***************************************************************************** |
180 | | * Declaration of local function |
181 | | *****************************************************************************/ |
182 | | static void MP4_TrackSetup( demux_t *, mp4_track_t *, const MP4_Box_t *, bool, bool ); |
183 | | static void MP4_TrackInit( mp4_track_t *, const MP4_Box_t * ); |
184 | | static void MP4_TrackClean( es_out_t *, mp4_track_t * ); |
185 | | |
186 | | static void MP4_Block_Send( demux_t *, mp4_track_t *, block_t * ); |
187 | | |
188 | | static void MP4_TrackSelect ( demux_t *, mp4_track_t *, bool ); |
189 | | static int MP4_TrackSeek ( demux_t *, mp4_track_t *, vlc_tick_t ); |
190 | | |
191 | | static uint64_t MP4_TrackGetPos ( mp4_track_t * ); |
192 | | static uint32_t MP4_TrackGetReadSize( mp4_track_t *, uint32_t * ); |
193 | | static int MP4_TrackNextSample( demux_t *, mp4_track_t *, uint32_t ); |
194 | | static void MP4_TrackSetELST( demux_t *, mp4_track_t *, vlc_tick_t ); |
195 | | static void TrackUpdateSampleAndTimes( mp4_track_t *p_track ); |
196 | | |
197 | | static void MP4_UpdateSeekpoint( demux_t *, vlc_tick_t ); |
198 | | |
199 | | static MP4_Box_t * MP4_GetTrexByTrackID( MP4_Box_t *p_moov, const uint32_t i_id ); |
200 | | static void MP4_GetDefaultSizeAndDuration( MP4_Box_t *p_moov, |
201 | | const MP4_Box_data_tfhd_t *p_tfhd_data, |
202 | | uint32_t *pi_default_size, |
203 | | uint32_t *pi_default_duration ); |
204 | | |
205 | | static stime_t GetMoovTrackDuration( demux_sys_t *p_sys, unsigned i_track_ID ); |
206 | | |
207 | | static int ProbeFragments( demux_t *p_demux, bool b_force, bool *pb_fragmented ); |
208 | | static int ProbeFragmentsChecked( demux_t *p_demux ); |
209 | | static int ProbeIndex( demux_t *p_demux ); |
210 | | |
211 | | static int FragCreateTrunIndex( demux_t *, MP4_Box_t *, MP4_Box_t *, stime_t ); |
212 | | |
213 | | static int FragGetMoofBySidxIndex( demux_t *p_demux, vlc_tick_t i_target_time, |
214 | | uint64_t *pi_moof_pos, vlc_tick_t *pi_sampletime ); |
215 | | static int FragGetMoofByTfraIndex( demux_t *p_demux, const vlc_tick_t i_target_time, unsigned i_track_ID, |
216 | | uint64_t *pi_moof_pos, vlc_tick_t *pi_sampletime ); |
217 | | static void FragResetContext( demux_sys_t * ); |
218 | | |
219 | | /* ASF Handlers */ |
220 | | static asf_track_info_t * MP4ASF_GetTrackInfo( asf_packet_sys_t *p_packetsys, uint8_t i_stream_number ); |
221 | | static void MP4ASF_Send(asf_packet_sys_t *p_packetsys, uint8_t i_stream_number, block_t **pp_frame); |
222 | | static void MP4ASF_ResetFrames( demux_sys_t *p_sys ); |
223 | | |
224 | | /* RTP Hint track */ |
225 | | static block_t * MP4_RTPHint_Convert( demux_t *p_demux, block_t *p_block, vlc_fourcc_t i_codec ); |
226 | | static block_t * MP4_RTPHintToFrame( demux_t *p_demux, block_t *p_block, uint32_t packetcount ); |
227 | | |
228 | | static int MP4_LoadMeta( demux_sys_t *p_sys, vlc_meta_t *p_meta ); |
229 | | |
230 | | /* Helpers */ |
231 | | |
232 | | static int64_t MP4_rescale( int64_t i_value, uint32_t i_timescale, uint32_t i_newscale ) |
233 | 3.81G | { |
234 | 3.81G | if( i_timescale == i_newscale ) |
235 | 3.11M | return i_value; |
236 | | |
237 | 3.81G | if( i_value <= INT64_MAX / i_newscale ) |
238 | 3.78G | return i_value * i_newscale / i_timescale; |
239 | | |
240 | | /* overflow */ |
241 | 31.8M | int64_t q = i_value / i_timescale; |
242 | 31.8M | int64_t r = i_value % i_timescale; |
243 | 31.8M | return q * i_newscale + r * i_newscale / i_timescale; |
244 | 3.81G | } |
245 | | |
246 | | static vlc_tick_t MP4_rescale_mtime( int64_t i_value, uint32_t i_timescale ) |
247 | 1.75G | { |
248 | 1.75G | return MP4_rescale(i_value, i_timescale, CLOCK_FREQ); |
249 | 1.75G | } |
250 | | |
251 | | static int64_t MP4_rescale_qtime( vlc_tick_t i_value, uint32_t i_timescale ) |
252 | 381M | { |
253 | 381M | return MP4_rescale(i_value, CLOCK_FREQ, i_timescale); |
254 | 381M | } |
255 | | |
256 | | static uint32_t stream_ReadU32( stream_t *s, void *p_read, uint32_t i_toread ) |
257 | 0 | { |
258 | 0 | ssize_t i_return = 0; |
259 | 0 | if ( i_toread > INT32_MAX ) |
260 | 0 | { |
261 | 0 | i_return = vlc_stream_Read( s, p_read, (size_t) INT32_MAX ); |
262 | 0 | if ( i_return < INT32_MAX ) |
263 | 0 | return i_return; |
264 | 0 | else |
265 | 0 | i_toread -= INT32_MAX; |
266 | 0 | } |
267 | 0 | i_return += vlc_stream_Read( s, (uint8_t *)p_read + i_return, (size_t) i_toread ); |
268 | 0 | return i_return; |
269 | 0 | } |
270 | | |
271 | | static int MP4_reftypeToFlag( vlc_fourcc_t i_reftype ) |
272 | 428 | { |
273 | 428 | switch( i_reftype ) |
274 | 428 | { |
275 | 0 | case ATOM_chap: |
276 | 0 | return USEAS_CHAPTERS; |
277 | 72 | case VLC_FOURCC('t','m','c','d'): |
278 | 72 | return USEAS_TIMECODE; |
279 | 356 | default: |
280 | 356 | return USEAS_NONE; |
281 | 428 | } |
282 | 428 | } |
283 | | |
284 | | static bool MP4_isMetadata( const mp4_track_t *tk ) |
285 | 2.55G | { |
286 | 2.55G | return tk->i_use_flags & (USEAS_CHAPTERS|USEAS_TIMECODE); |
287 | 2.55G | } |
288 | | |
289 | | static MP4_Box_t * MP4_GetTrexByTrackID( MP4_Box_t *p_moov, const uint32_t i_id ) |
290 | 832 | { |
291 | 832 | if(!p_moov) |
292 | 0 | return NULL; |
293 | 832 | MP4_Box_t *p_trex = MP4_BoxGet( p_moov, "mvex/trex" ); |
294 | 977 | while( p_trex ) |
295 | 767 | { |
296 | 767 | if ( p_trex->i_type == ATOM_trex && |
297 | 752 | BOXDATA(p_trex) && BOXDATA(p_trex)->i_track_ID == i_id ) |
298 | 622 | break; |
299 | 145 | else |
300 | 145 | p_trex = p_trex->p_next; |
301 | 767 | } |
302 | 832 | return p_trex; |
303 | 832 | } |
304 | | |
305 | | /** |
306 | | * Return the track identified by tid |
307 | | */ |
308 | | static mp4_track_t *MP4_GetTrackByTrackID( demux_t *p_demux, const uint32_t tid ) |
309 | 5.27k | { |
310 | 5.27k | demux_sys_t *p_sys = p_demux->p_sys; |
311 | | |
312 | 5.27k | mp4_track_t *ret = NULL; |
313 | 14.4k | for( unsigned i = 0; i < p_sys->i_tracks; i++ ) |
314 | 9.94k | { |
315 | 9.94k | ret = &p_sys->track[i]; |
316 | 9.94k | if( ret->i_track_ID == tid ) |
317 | 791 | return ret; |
318 | 9.94k | } |
319 | 4.48k | return NULL; |
320 | 5.27k | } |
321 | | |
322 | | static MP4_Box_t * MP4_GetTrakByTrackID( MP4_Box_t *p_moov, const uint32_t i_id ) |
323 | 3.41k | { |
324 | 3.41k | MP4_Box_t *p_trak = MP4_BoxGet( p_moov, "trak" ); |
325 | 3.41k | MP4_Box_t *p_tkhd; |
326 | 6.19k | while( p_trak ) |
327 | 5.54k | { |
328 | 5.54k | if( p_trak->i_type == ATOM_trak && |
329 | 4.67k | (p_tkhd = MP4_BoxGet( p_trak, "tkhd" )) && BOXDATA(p_tkhd) && |
330 | 3.23k | BOXDATA(p_tkhd)->i_track_ID == i_id ) |
331 | 2.76k | break; |
332 | 2.78k | else |
333 | 2.78k | p_trak = p_trak->p_next; |
334 | 5.54k | } |
335 | 3.41k | return p_trak; |
336 | 3.41k | } |
337 | | |
338 | | static MP4_Box_t * MP4_GetTrafByTrackID( MP4_Box_t *p_moof, const uint32_t i_id ) |
339 | 2.89k | { |
340 | 2.89k | MP4_Box_t *p_traf = MP4_BoxGet( p_moof, "traf" ); |
341 | 2.89k | MP4_Box_t *p_tfhd; |
342 | 3.33k | while( p_traf ) |
343 | 958 | { |
344 | 958 | if( p_traf->i_type == ATOM_traf && |
345 | 831 | (p_tfhd = MP4_BoxGet( p_traf, "tfhd" )) && BOXDATA(p_tfhd) && |
346 | 811 | BOXDATA(p_tfhd)->i_track_ID == i_id ) |
347 | 517 | break; |
348 | 441 | else |
349 | 441 | p_traf = p_traf->p_next; |
350 | 958 | } |
351 | 2.89k | return p_traf; |
352 | 2.89k | } |
353 | | |
354 | | static const MP4_Box_t * MP4_GroupDescriptionByType( const MP4_Box_t *p_node, |
355 | | uint32_t i_grouping_type ) |
356 | 1 | { |
357 | 1 | for( const MP4_Box_t *p_sgpd = MP4_BoxGet( p_node, "sgpd" ); |
358 | 1 | p_sgpd; p_sgpd = p_sgpd->p_next ) |
359 | 1 | { |
360 | 1 | const MP4_Box_data_sgpd_t *p_sgpd_data = BOXDATA(p_sgpd); |
361 | 1 | if( p_sgpd->i_type != ATOM_sgpd || !p_sgpd_data || |
362 | 1 | p_sgpd_data->i_grouping_type != i_grouping_type ) |
363 | 0 | continue; |
364 | 1 | return p_sgpd; |
365 | 1 | } |
366 | 0 | return NULL; |
367 | 1 | } |
368 | | |
369 | | enum SampleGroupMatch |
370 | | { |
371 | | SAMPLE_GROUP_MATCH_EXACT = 0, |
372 | | SAMPLE_GROUP_MATCH_LAST_VALID = 1, |
373 | | }; |
374 | | |
375 | | static const MP4_Box_data_sbgp_entry_t * |
376 | | MP4_SampleToGroupInfo( const MP4_Box_t *p_node, uint32_t i_sample, |
377 | | uint32_t i_grouping_type, |
378 | | uint32_t i_grouping_type_parameter, |
379 | | uint32_t *pi_group_sample, |
380 | | enum SampleGroupMatch match, |
381 | | const MP4_Box_data_sgpd_entry_t **pp_descentry ) |
382 | 351M | { |
383 | 351M | const MP4_Box_t *p_sbgp = MP4_BoxGet( p_node, "sbgp" ); |
384 | 351M | const MP4_Box_data_sbgp_t *p_sbgp_data; |
385 | 351M | if( !p_sbgp ) |
386 | 351M | return NULL; |
387 | | |
388 | 95 | do |
389 | 164 | { |
390 | 164 | p_sbgp_data = BOXDATA(p_sbgp); |
391 | 164 | if( p_sbgp->i_type == ATOM_sbgp && p_sbgp_data && |
392 | 134 | p_sbgp_data->i_grouping_type == i_grouping_type && |
393 | 39 | (i_grouping_type_parameter == 0 || |
394 | 0 | p_sbgp_data->i_grouping_type_parameter == i_grouping_type_parameter) ) |
395 | 39 | break; |
396 | 125 | p_sbgp = p_sbgp->p_next; |
397 | 125 | } while(p_sbgp); |
398 | | |
399 | 95 | if( !p_sbgp ) |
400 | 56 | return NULL; |
401 | | |
402 | 39 | const MP4_Box_data_sbgp_entry_t *p_sampleentry = NULL; |
403 | 39 | uint32_t i_entry_start_sample = 0; |
404 | 91 | for ( uint32_t i=0; i<p_sbgp_data->i_entry_count; i++ ) |
405 | 84 | { |
406 | 84 | const uint32_t next_entry_start_sample = i_entry_start_sample + |
407 | 84 | p_sbgp_data->p_entries[i].i_sample_count; |
408 | 84 | if( i_sample >= next_entry_start_sample ) |
409 | 52 | { |
410 | 52 | if( match == SAMPLE_GROUP_MATCH_LAST_VALID && |
411 | 49 | p_sbgp_data->p_entries[i].i_group_description_index != 0 ) |
412 | 38 | { |
413 | 38 | p_sampleentry = &p_sbgp_data->p_entries[i]; |
414 | 38 | if( pi_group_sample ) |
415 | 38 | *pi_group_sample = i_entry_start_sample; |
416 | 38 | } |
417 | 52 | i_entry_start_sample = next_entry_start_sample; |
418 | 52 | continue; |
419 | 52 | } |
420 | | |
421 | | /* Sample has meaning for group */ |
422 | 32 | if( p_sbgp_data->p_entries[i].i_group_description_index != 0 ) |
423 | 3 | { |
424 | 3 | p_sampleentry = &p_sbgp_data->p_entries[i]; |
425 | 3 | if( pi_group_sample ) |
426 | 3 | *pi_group_sample = i_entry_start_sample; |
427 | 3 | } |
428 | 32 | break; |
429 | 84 | } |
430 | | |
431 | | /* No meaning found for sample */ |
432 | 39 | if( p_sampleentry == NULL ) |
433 | 5 | return NULL; |
434 | | |
435 | | /* We have a sample entry in the group and maybe a description */ |
436 | 34 | if( pp_descentry == NULL ) |
437 | 33 | return p_sampleentry; |
438 | | |
439 | | /* Lookup designated group description */ |
440 | 1 | const MP4_Box_t *p_sgpd = MP4_GroupDescriptionByType( p_node, i_grouping_type ); |
441 | 1 | if( p_sgpd ) |
442 | 1 | { |
443 | 1 | const MP4_Box_data_sgpd_t *p_descdata = BOXDATA(p_sgpd); |
444 | 1 | if( p_sampleentry && |
445 | 1 | p_sampleentry->i_group_description_index <= p_descdata->i_entry_count ) |
446 | 1 | { |
447 | 1 | *pp_descentry = &p_descdata-> |
448 | 1 | p_entries[p_sampleentry->i_group_description_index - 1]; |
449 | 1 | } |
450 | 0 | else if( p_sampleentry == NULL && |
451 | 0 | p_descdata->i_version >= 2 && |
452 | 0 | p_descdata->i_default_sample_description_index && |
453 | 0 | p_descdata->i_default_sample_description_index <= p_descdata->i_entry_count ) |
454 | 0 | { |
455 | 0 | *pp_descentry = &p_descdata-> |
456 | 0 | p_entries[p_descdata->i_default_sample_description_index - 1]; |
457 | 0 | } |
458 | 1 | } |
459 | | |
460 | 1 | return p_sampleentry; |
461 | 34 | } |
462 | | |
463 | | static es_out_id_t * MP4_CreateES( es_out_t *out, const es_format_t *p_fmt, |
464 | | bool b_forced_spu ) |
465 | 4.32k | { |
466 | 4.32k | es_out_id_t *p_es = es_out_Add( out, p_fmt ); |
467 | | /* Force SPU which isn't selected/defaulted */ |
468 | 4.32k | if( p_fmt->i_cat == SPU_ES && p_es && b_forced_spu ) |
469 | 5 | es_out_Control( out, ES_OUT_SET_ES_DEFAULT, p_es ); |
470 | | |
471 | 4.32k | return p_es; |
472 | 4.32k | } |
473 | | |
474 | | static int MP4_ChunkAllocEntries( size_t entries, uint32_t *smallbuf, |
475 | | uint32_t **dst0, uint32_t **dst1 ) |
476 | 327k | { |
477 | 327k | uint32_t *buf; |
478 | | |
479 | 327k | if( entries > MP4_CHUNK_SMALLBUF_ENTRIES ) |
480 | 975 | { |
481 | 975 | buf = calloc( entries, sizeof( buf[0] ) * 2 ); |
482 | 975 | if( unlikely(buf == NULL) ) |
483 | 0 | return VLC_ENOMEM; |
484 | 975 | } |
485 | 326k | else buf = smallbuf; |
486 | | |
487 | 327k | *dst0 = buf; |
488 | 327k | *dst1 = &buf[entries]; |
489 | | |
490 | 327k | return VLC_SUCCESS; |
491 | 327k | } |
492 | | |
493 | | static void MP4_ChunkDestroy( mp4_chunk_t *ck ) |
494 | 226k | { |
495 | 226k | if( ck->p_sample_count_dts != ck->small_dts_buf ) |
496 | 7.99k | free( ck->p_sample_count_dts ); |
497 | 226k | if( ck->p_sample_count_pts != ck->small_pts_buf ) |
498 | 119k | free( ck->p_sample_count_pts ); |
499 | 226k | } |
500 | | |
501 | | static stime_t MP4_MapTrackTimeIntoTimeline( const mp4_track_t *p_track, |
502 | | uint32_t i_movie_timescale, |
503 | | stime_t i_time ) |
504 | 1.54G | { |
505 | 1.54G | if( !p_track->p_elst || p_track->BOXDATA(p_elst)->i_entry_count == 0 ) |
506 | 240M | return i_time; |
507 | | |
508 | 1.30G | const MP4_Box_data_elst_entry_t *edit = |
509 | 1.30G | &p_track->BOXDATA(p_elst)->entries[p_track->i_elst]; |
510 | | |
511 | | /* convert to offset in our current edit */ |
512 | 1.30G | if( edit->i_media_time > 0 && p_track->i_start_dts >= edit->i_media_time ) |
513 | 517M | i_time -= edit->i_media_time; |
514 | | |
515 | | /* convert media timeline to track timescale */ |
516 | 1.30G | stime_t i_media_elst_start = MP4_rescale( p_track->i_elst_time, |
517 | 1.30G | i_movie_timescale, |
518 | 1.30G | p_track->i_timescale ); |
519 | | /* add to timeline start in current edit */ |
520 | 1.30G | i_time += i_media_elst_start; |
521 | | |
522 | 1.30G | return i_time; |
523 | 1.54G | } |
524 | | |
525 | | static stime_t MP4_MapMediaTimelineToTrackTime( const mp4_track_t *p_track, |
526 | | uint32_t i_movie_timescale, |
527 | | stime_t i_time ) |
528 | 188M | { |
529 | 188M | if( !p_track->p_elst || p_track->BOXDATA(p_elst)->i_entry_count == 0 ) |
530 | 0 | return i_time; |
531 | | |
532 | 188M | const MP4_Box_data_elst_t *elst = p_track->BOXDATA(p_elst); |
533 | | /* convert media timeline to track timescale */ |
534 | 188M | stime_t i_media_elst_start = MP4_rescale( p_track->i_elst_time, |
535 | 188M | i_movie_timescale, |
536 | 188M | p_track->i_timescale ); |
537 | | /* convert to timeline offset in current edit */ |
538 | 188M | i_time -= i_media_elst_start; |
539 | | |
540 | | /* add edit start in track samples time to get track sample time */ |
541 | 188M | if( elst->entries[p_track->i_elst].i_media_time > 0 ) |
542 | 172M | i_time += elst->entries[p_track->i_elst].i_media_time; |
543 | | |
544 | 188M | return i_time; |
545 | 188M | } |
546 | | |
547 | | static stime_t MP4_ChunkGetSampleDTS( const mp4_chunk_t *p_chunk, |
548 | | uint32_t i_sample ) |
549 | 181M | { |
550 | 181M | uint32_t i_index = 0; |
551 | 181M | stime_t sdts = p_chunk->i_first_dts; |
552 | 182M | while( i_sample > 0 && i_index < p_chunk->i_entries_dts ) |
553 | 4.69M | { |
554 | 4.69M | if( i_sample > p_chunk->p_sample_count_dts[i_index] ) |
555 | 975k | { |
556 | 975k | sdts += (stime_t)p_chunk->p_sample_count_dts[i_index] * |
557 | 975k | p_chunk->p_sample_delta_dts[i_index]; |
558 | 975k | i_sample -= p_chunk->p_sample_count_dts[i_index++]; |
559 | 975k | } |
560 | 3.72M | else |
561 | 3.72M | { |
562 | 3.72M | sdts += (stime_t)i_sample * p_chunk->p_sample_delta_dts[i_index]; |
563 | 3.72M | break; |
564 | 3.72M | } |
565 | 4.69M | } |
566 | 181M | return sdts; |
567 | 181M | } |
568 | | |
569 | | static bool MP4_ChunkGetSampleCTSDelta( const mp4_chunk_t *p_chunk, |
570 | | uint32_t i_sample, stime_t *pi_delta ) |
571 | 181M | { |
572 | 181M | if( p_chunk->p_sample_count_pts && p_chunk->p_sample_offset_pts ) |
573 | 2.09M | { |
574 | 2.93M | for( uint32_t i_index = 0; i_index < p_chunk->i_entries_pts ; i_index++ ) |
575 | 2.90M | { |
576 | 2.90M | if( i_sample < p_chunk->p_sample_count_pts[i_index] ) |
577 | 2.07M | { |
578 | 2.07M | *pi_delta = p_chunk->p_sample_offset_pts[i_index]; |
579 | 2.07M | return true; |
580 | 2.07M | } |
581 | 835k | i_sample -= p_chunk->p_sample_count_pts[i_index]; |
582 | 835k | } |
583 | 2.09M | } |
584 | 179M | return false; |
585 | 181M | } |
586 | | |
587 | | static vlc_tick_t MP4_TrackGetDTSPTS( demux_t *p_demux, const mp4_track_t *p_track, |
588 | | vlc_tick_t *pi_nzpts ) |
589 | 1.54G | { |
590 | 1.54G | demux_sys_t *p_sys = p_demux->p_sys; |
591 | | |
592 | 1.54G | stime_t sdts = p_track->i_next_dts; |
593 | 1.54G | uint32_t delta = p_track->i_next_delta; |
594 | | |
595 | | /* now handle elst */ |
596 | 1.54G | sdts = MP4_MapTrackTimeIntoTimeline( p_track, p_sys->i_timescale, sdts ); |
597 | | |
598 | 1.54G | vlc_tick_t i_dts = MP4_rescale_mtime( sdts, p_track->i_timescale); |
599 | | |
600 | 1.54G | if( pi_nzpts ) |
601 | 182M | { |
602 | 182M | if( delta != UNKNOWN_DELTA ) |
603 | 1.42M | { |
604 | 1.42M | *pi_nzpts = i_dts + MP4_rescale_mtime( delta, p_track->i_timescale ); |
605 | 1.42M | } |
606 | 181M | else if( p_track->b_ignore_implicit_pts ) |
607 | 654 | { |
608 | 654 | *pi_nzpts = INVALID_PTS; |
609 | 654 | } |
610 | 181M | else *pi_nzpts = i_dts; |
611 | 182M | } |
612 | | |
613 | 1.54G | if( p_track->i_pts_offset ) |
614 | 4.37M | { |
615 | 4.37M | if( pi_nzpts && *pi_nzpts != INVALID_PTS ) |
616 | 1.17M | *pi_nzpts += p_track->i_pts_offset; |
617 | 4.37M | i_dts += p_track->i_pts_offset; |
618 | 4.37M | } |
619 | | |
620 | 1.54G | return i_dts; |
621 | 1.54G | } |
622 | | |
623 | | static stime_t MP4_GetChunkSamplesDuration( const mp4_chunk_t *p_chunk, |
624 | | uint32_t i_start_sample, |
625 | | uint32_t i_nb_samples ) |
626 | 3.70M | { |
627 | 3.70M | stime_t i_duration = 0; |
628 | | |
629 | | /* Forward to right index, and set remaining count in that index */ |
630 | 3.70M | uint32_t i_index = 0; |
631 | 3.70M | uint32_t i_remain = 0; |
632 | 3.70M | for( uint32_t i = p_chunk->i_sample_first; |
633 | 4.62M | i<i_start_sample && i_index < p_chunk->i_entries_dts; ) |
634 | 3.06M | { |
635 | 3.06M | if( i_start_sample - i >= p_chunk->p_sample_count_dts[i_index] ) |
636 | 924k | { |
637 | 924k | i += p_chunk->p_sample_count_dts[i_index]; |
638 | 924k | i_index++; |
639 | 924k | } |
640 | 2.13M | else |
641 | 2.13M | { |
642 | 2.13M | i_remain = i_start_sample - i; |
643 | 2.13M | break; |
644 | 2.13M | } |
645 | 3.06M | } |
646 | | |
647 | | /* Compute total duration from all samples from index */ |
648 | 4.21M | while( i_nb_samples > 0 && i_index < p_chunk->i_entries_dts ) |
649 | 2.67M | { |
650 | 2.67M | if( i_nb_samples >= p_chunk->p_sample_count_dts[i_index] - i_remain ) |
651 | 517k | { |
652 | 517k | i_duration += (p_chunk->p_sample_count_dts[i_index] - i_remain) * |
653 | 517k | (int64_t) p_chunk->p_sample_delta_dts[i_index]; |
654 | 517k | i_nb_samples -= (p_chunk->p_sample_count_dts[i_index] - i_remain); |
655 | 517k | i_index++; |
656 | 517k | i_remain = 0; |
657 | 517k | } |
658 | 2.16M | else |
659 | 2.16M | { |
660 | 2.16M | i_duration += (stime_t)i_nb_samples * p_chunk->p_sample_delta_dts[i_index]; |
661 | 2.16M | break; |
662 | 2.16M | } |
663 | 2.67M | } |
664 | | |
665 | 3.70M | return i_duration; |
666 | 3.70M | } |
667 | | |
668 | | static inline vlc_tick_t MP4_GetSamplesDuration( const mp4_track_t *p_track, |
669 | | uint32_t i_nb_samples ) |
670 | 3.70M | { |
671 | 3.70M | stime_t i_duration = MP4_GetChunkSamplesDuration( &p_track->chunk[p_track->i_chunk], |
672 | 3.70M | p_track->i_sample, |
673 | 3.70M | i_nb_samples ); |
674 | 3.70M | return MP4_rescale_mtime( i_duration, p_track->i_timescale ); |
675 | 3.70M | } |
676 | | |
677 | | static inline vlc_tick_t MP4_GetMoviePTS(demux_sys_t *p_sys ) |
678 | 619M | { |
679 | 619M | return p_sys->i_nztime; |
680 | 619M | } |
681 | | |
682 | | static void LoadChapter( demux_t *p_demux ); |
683 | | |
684 | | static int LoadInitFrag( demux_t *p_demux ) |
685 | 8.26k | { |
686 | 8.26k | demux_sys_t *p_sys = p_demux->p_sys; |
687 | | |
688 | | /* Load all boxes ( except raw data ) */ |
689 | 8.26k | MP4_Box_t *p_root = MP4_BoxGetRoot( p_demux->s ); |
690 | 8.26k | if( p_root == NULL || !MP4_BoxGet( p_root, "/moov" ) ) |
691 | 2.39k | { |
692 | 2.39k | MP4_BoxFree( p_root ); |
693 | 2.39k | goto LoadInitFragError; |
694 | 2.39k | } |
695 | | |
696 | 5.86k | p_sys->p_root = p_root; |
697 | | |
698 | 5.86k | return VLC_SUCCESS; |
699 | | |
700 | 2.39k | LoadInitFragError: |
701 | 2.39k | msg_Warn( p_demux, "MP4 plugin discarded (not a valid initialization chunk)" ); |
702 | 2.39k | return VLC_EGENERIC; |
703 | 8.26k | } |
704 | | |
705 | | static int CreateTracks( demux_t *p_demux, unsigned i_tracks ) |
706 | 4.47k | { |
707 | 4.47k | demux_sys_t *p_sys = p_demux->p_sys; |
708 | | |
709 | 4.47k | if( SIZE_MAX / i_tracks < sizeof(mp4_track_t) ) |
710 | 0 | return VLC_EGENERIC; |
711 | | |
712 | 4.47k | p_sys->track = vlc_alloc( i_tracks, sizeof(mp4_track_t) ); |
713 | 4.47k | if( p_sys->track == NULL ) |
714 | 0 | return VLC_ENOMEM; |
715 | 4.47k | p_sys->i_tracks = i_tracks; |
716 | | |
717 | 10.3k | for( unsigned i=0; i<i_tracks; i++ ) |
718 | 5.91k | { |
719 | 5.91k | MP4_TrackInit( &p_sys->track[i], |
720 | 5.91k | MP4_BoxGetVa( p_sys->p_root, "/moov/trak[%u]", i ) ); |
721 | 5.91k | } |
722 | | |
723 | 4.47k | return VLC_SUCCESS; |
724 | 4.47k | } |
725 | | |
726 | | static block_t * MP4_CDP_Convert( block_t *p_block ) |
727 | 6 | { |
728 | | /* FinalCut ST334-2 CDP put inside CC violation */ |
729 | 6 | if (p_block->i_buffer < 8 + 7) |
730 | 0 | goto out; |
731 | | |
732 | 6 | if(memcmp(&p_block->p_buffer[4], "ccdp", 4)) |
733 | 0 | goto out; |
734 | | |
735 | 6 | uint_fast32_t ccdp_size = GetDWBE(p_block->p_buffer); |
736 | 6 | if (ccdp_size > p_block->i_buffer || ccdp_size < 8) |
737 | 6 | goto out; |
738 | | |
739 | 0 | p_block->p_buffer += 8; |
740 | 0 | p_block->i_buffer = ccdp_size - 8; |
741 | | /* Let cc.h handle extraction */ |
742 | 0 | cc_data_t cc; |
743 | 0 | cc_Init(&cc); |
744 | 0 | cc_Extract(&cc, CC_PAYLOAD_CDP, true, p_block->p_buffer, p_block->i_buffer); |
745 | 0 | memcpy(p_block->p_buffer, cc.p_data, cc.i_data); |
746 | 0 | p_block->i_buffer = cc.i_data; |
747 | |
|
748 | 6 | out: |
749 | 6 | return p_block; |
750 | 0 | } |
751 | | |
752 | | static block_t * MP4_EIA608_Convert( block_t * p_block ) |
753 | 2.14k | { |
754 | | /* Rebuild codec data from encap */ |
755 | 2.14k | block_t *p_newblock = NULL; |
756 | | |
757 | 2.14k | assert(p_block->i_buffer <= SSIZE_MAX); |
758 | | /* always need at least 10 bytes (atom size+header+1pair)*/ |
759 | 2.14k | if (p_block->i_buffer < 8) |
760 | 5 | goto out; |
761 | | |
762 | 2.14k | if(!memcmp(&p_block->p_buffer[4], "ccdp", 4)) |
763 | 6 | return MP4_CDP_Convert(p_block); |
764 | | |
765 | 2.13k | uint_fast32_t cdat_size = GetDWBE(p_block->p_buffer) - 8; |
766 | 2.13k | if (cdat_size > p_block->i_buffer) |
767 | 2.07k | goto out; |
768 | | |
769 | 63 | const uint8_t *cdat = p_block->p_buffer + 8; |
770 | 63 | if (memcmp(cdat - 4, "cdat", 4) != 0) |
771 | 63 | goto out; |
772 | | |
773 | 0 | p_block->p_buffer += cdat_size; |
774 | 0 | p_block->i_buffer -= cdat_size; |
775 | 0 | cdat_size &= ~1; |
776 | | |
777 | | /* cdt2 is optional */ |
778 | 0 | uint_fast32_t cdt2_size = 0; |
779 | 0 | const uint8_t *cdt2 = NULL; |
780 | |
|
781 | 0 | if (p_block->i_buffer >= 8) { |
782 | 0 | size_t size = GetDWBE(p_block->p_buffer) - 8; |
783 | |
|
784 | 0 | if (size <= p_block->i_buffer) { |
785 | 0 | cdt2 = p_block->p_buffer + 8; |
786 | |
|
787 | 0 | if (memcmp(cdt2 - 4, "cdt2", 4) == 0) |
788 | 0 | cdt2_size = size & ~1; |
789 | 0 | } |
790 | 0 | } |
791 | |
|
792 | 0 | p_newblock = block_Alloc((cdat_size + cdt2_size) / 2 * 3); |
793 | 0 | if (unlikely(p_newblock == NULL)) |
794 | 0 | goto out; |
795 | | |
796 | 0 | uint8_t *out = p_newblock->p_buffer; |
797 | |
|
798 | 0 | while (cdat_size > 0) { |
799 | 0 | *(out++) = CC_PKT_BYTE0(0); /* cc1 == field 0 */ |
800 | 0 | *(out++) = *(cdat++); |
801 | 0 | *(out++) = *(cdat++); |
802 | 0 | cdat_size -= 2; |
803 | 0 | } |
804 | |
|
805 | 0 | while (cdt2_size > 0) { |
806 | 0 | *(out++) = CC_PKT_BYTE0(1); /* cc2 == field 1 */ |
807 | 0 | *(out++) = *(cdt2++); |
808 | 0 | *(out++) = *(cdt2++); |
809 | 0 | cdt2_size -= 2; |
810 | 0 | } |
811 | |
|
812 | 0 | p_newblock->i_pts = p_block->i_dts; |
813 | 0 | p_newblock->i_flags = BLOCK_FLAG_TYPE_P; |
814 | 2.14k | out: |
815 | 2.14k | block_Release( p_block ); |
816 | 2.14k | return p_newblock; |
817 | 0 | } |
818 | | |
819 | | static uint32_t MP4_TrackGetRunSeq( mp4_track_t *p_track ) |
820 | 182M | { |
821 | 182M | if( p_track->i_chunk_count > 0 ) |
822 | 182M | return p_track->chunk[p_track->i_chunk].i_virtual_run_number; |
823 | 0 | return 0; |
824 | 182M | } |
825 | | |
826 | | /* Analyzes chunks to find max interleave length |
827 | | * sets flat flag if no interleaving is in use */ |
828 | | static void MP4_GetInterleaving( demux_t *p_demux, vlc_tick_t *pi_max_contiguous, bool *pb_flat ) |
829 | 0 | { |
830 | 0 | demux_sys_t *p_sys = p_demux->p_sys; |
831 | 0 | *pi_max_contiguous = 0; |
832 | 0 | *pb_flat = true; |
833 | | |
834 | | /* Find first recorded chunk */ |
835 | 0 | mp4_track_t *tk = NULL; |
836 | 0 | uint64_t i_duration = 0; |
837 | 0 | for( unsigned i=0; i < p_sys->i_tracks; i++ ) |
838 | 0 | { |
839 | 0 | mp4_track_t *cur = &p_sys->track[i]; |
840 | 0 | if( !cur->i_chunk_count ) |
841 | 0 | continue; |
842 | | |
843 | 0 | if( tk == NULL || cur->chunk[0].i_offset < tk->chunk[0].i_offset ) |
844 | 0 | tk = cur; |
845 | 0 | } |
846 | |
|
847 | 0 | for( ; tk != NULL; ) |
848 | 0 | { |
849 | 0 | i_duration += tk->chunk[tk->i_chunk].i_duration; |
850 | 0 | tk->i_chunk++; |
851 | | |
852 | | /* Find next chunk in data order */ |
853 | 0 | mp4_track_t *nexttk = NULL; |
854 | 0 | for( unsigned i=0; i < p_sys->i_tracks; i++ ) |
855 | 0 | { |
856 | 0 | mp4_track_t *cur = &p_sys->track[i]; |
857 | 0 | if( cur->i_chunk == cur->i_chunk_count ) |
858 | 0 | continue; |
859 | | |
860 | 0 | if( nexttk == NULL || |
861 | 0 | cur->chunk[cur->i_chunk].i_offset < nexttk->chunk[nexttk->i_chunk].i_offset ) |
862 | 0 | nexttk = cur; |
863 | 0 | } |
864 | | |
865 | | /* copy previous run */ |
866 | 0 | if( nexttk && nexttk->i_chunk > 0 ) |
867 | 0 | nexttk->chunk[nexttk->i_chunk].i_virtual_run_number = |
868 | 0 | nexttk->chunk[nexttk->i_chunk - 1].i_virtual_run_number; |
869 | |
|
870 | 0 | if( tk != nexttk ) |
871 | 0 | { |
872 | 0 | vlc_tick_t i_dur = MP4_rescale_mtime( i_duration, tk->i_timescale ); |
873 | 0 | if( i_dur > *pi_max_contiguous ) |
874 | 0 | *pi_max_contiguous = i_dur; |
875 | 0 | i_duration = 0; |
876 | |
|
877 | 0 | if( tk->i_chunk != tk->i_chunk_count ) |
878 | 0 | *pb_flat = false; |
879 | |
|
880 | 0 | if( nexttk && nexttk->i_chunk > 0 ) /* new run number */ |
881 | 0 | nexttk->chunk[nexttk->i_chunk].i_virtual_run_number++; |
882 | 0 | } |
883 | |
|
884 | 0 | tk = nexttk; |
885 | 0 | } |
886 | | |
887 | | /* reset */ |
888 | 0 | for( unsigned i=0; i < p_sys->i_tracks; i++ ) |
889 | 0 | p_sys->track[i].i_chunk = 0; |
890 | 0 | } |
891 | | |
892 | | static block_t * MP4_Block_Convert( demux_t *p_demux, const mp4_track_t *p_track, block_t *p_block ) |
893 | 3.70M | { |
894 | | /* might have some encap */ |
895 | 3.70M | if( p_track->fmt.i_cat == SPU_ES ) |
896 | 1.20M | { |
897 | 1.20M | switch( p_track->fmt.i_codec ) |
898 | 1.20M | { |
899 | 3.96k | case VLC_CODEC_WEBVTT: |
900 | 5.16k | case VLC_CODEC_TTML: |
901 | 5.70k | case VLC_CODEC_QTXT: |
902 | 57.9k | case VLC_CODEC_TX3G: |
903 | 1.18M | case VLC_CODEC_SPU: |
904 | 1.18M | case VLC_CODEC_SUBT: |
905 | | /* accept as-is */ |
906 | 1.18M | break; |
907 | 2.14k | case VLC_CODEC_CEA608: |
908 | 2.14k | case VLC_CODEC_CEA708: |
909 | 2.14k | p_block = MP4_EIA608_Convert( p_block ); |
910 | 2.14k | break; |
911 | 12.3k | default: |
912 | 12.3k | p_block->i_buffer = 0; |
913 | 12.3k | break; |
914 | 1.20M | } |
915 | 1.20M | } |
916 | 2.50M | else if( p_track->fmt.i_codec == VLC_CODEC_AV1 ) |
917 | 1.08M | { |
918 | 1.08M | p_block = AV1_Unpack_Sample( p_block ); |
919 | 1.08M | } |
920 | 1.42M | else if( p_track->fmt.i_original_fourcc == ATOM_rrtp ) |
921 | 0 | { |
922 | 0 | p_block = MP4_RTPHint_Convert( p_demux, p_block, p_track->fmt.i_codec ); |
923 | 0 | } |
924 | | |
925 | 3.70M | return p_block; |
926 | 3.70M | } |
927 | | |
928 | | static void MP4_Block_Send( demux_t *p_demux, mp4_track_t *p_track, block_t *p_block ) |
929 | 3.70M | { |
930 | 3.70M | demux_sys_t *p_sys = p_demux->p_sys; |
931 | | |
932 | 3.70M | p_block = MP4_Block_Convert( p_demux, p_track, p_block ); |
933 | 3.70M | if( p_block == NULL ) |
934 | 2.14k | return; |
935 | | |
936 | 3.70M | if ( p_track->i_chans_to_reorder ) |
937 | 0 | { |
938 | 0 | aout_ChannelReorder( p_block->p_buffer, p_block->i_buffer, |
939 | 0 | p_track->i_chans_to_reorder, |
940 | 0 | p_track->rgi_chans_reordering, |
941 | 0 | p_track->fmt.i_codec ); |
942 | 0 | } |
943 | | |
944 | 3.70M | p_block->i_flags |= p_track->i_block_flags; |
945 | 3.70M | if( p_track->i_next_block_flags ) |
946 | 1 | { |
947 | 1 | p_block->i_flags |= p_track->i_next_block_flags; |
948 | 1 | p_track->i_next_block_flags = 0; |
949 | 1 | } |
950 | | |
951 | | /* ASF packets in mov */ |
952 | 3.70M | if( p_track->p_asf ) |
953 | 0 | { |
954 | 0 | p_sys->asfpacketsys.s = vlc_stream_MemoryNew( p_demux, p_block->p_buffer, p_block->i_buffer, true ); |
955 | 0 | if ( p_sys->asfpacketsys.s ) |
956 | 0 | { |
957 | 0 | p_track->i_dts_backup = p_block->i_dts; |
958 | 0 | p_track->i_pts_backup = p_block->i_pts; |
959 | | /* And demux it as ASF packet */ |
960 | 0 | uint64_t startpos; |
961 | 0 | do |
962 | 0 | { |
963 | 0 | startpos = vlc_stream_Tell(p_sys->asfpacketsys.s); |
964 | 0 | DemuxASFPacket( &p_sys->asfpacketsys, |
965 | 0 | p_block->i_buffer - startpos, |
966 | 0 | p_block->i_buffer - startpos, |
967 | 0 | 0, p_block->i_buffer ); |
968 | 0 | } while( vlc_stream_Tell(p_sys->asfpacketsys.s) != p_block->i_buffer && |
969 | 0 | vlc_stream_Tell(p_sys->asfpacketsys.s) != startpos ); |
970 | 0 | vlc_stream_Delete(p_sys->asfpacketsys.s); |
971 | 0 | } |
972 | 0 | block_Release(p_block); |
973 | 0 | } |
974 | 3.70M | else |
975 | 3.70M | es_out_Send( p_demux->out, p_track->p_es, p_block ); |
976 | 3.70M | } |
977 | | |
978 | | int OpenHEIF ( vlc_object_t * ); |
979 | | void CloseHEIF( vlc_object_t * ); |
980 | | |
981 | | /***************************************************************************** |
982 | | * Open: check file and initializes MP4 structures |
983 | | *****************************************************************************/ |
984 | | static int Open( vlc_object_t * p_this ) |
985 | 11.1k | { |
986 | 11.1k | demux_t *p_demux = (demux_t *)p_this; |
987 | 11.1k | demux_sys_t *p_sys; |
988 | | |
989 | 11.1k | const uint8_t *p_peek; |
990 | | |
991 | 11.1k | MP4_Box_t *p_ftyp; |
992 | 11.1k | const MP4_Box_t *p_mvhd = NULL; |
993 | 11.1k | const MP4_Box_t *p_mvex = NULL; |
994 | | |
995 | 11.1k | bool b_enabled_es; |
996 | | |
997 | | /* A little test to see if it could be a mp4 */ |
998 | 11.1k | if( vlc_stream_Peek( p_demux->s, &p_peek, 12 ) < 12 ) return VLC_EGENERIC; |
999 | | |
1000 | 11.1k | switch( VLC_FOURCC( p_peek[4], p_peek[5], p_peek[6], p_peek[7] ) ) |
1001 | 11.1k | { |
1002 | 2.05k | case ATOM_moov: |
1003 | 2.17k | case ATOM_foov: |
1004 | 2.59k | case ATOM_moof: |
1005 | 2.83k | case ATOM_mdat: |
1006 | 2.97k | case ATOM_udta: |
1007 | 3.14k | case ATOM_free: |
1008 | 3.15k | case ATOM_skip: |
1009 | 3.16k | case ATOM_wide: |
1010 | 3.47k | case ATOM_uuid: |
1011 | 3.50k | case VLC_FOURCC( 'p', 'n', 'o', 't' ): |
1012 | 3.50k | break; |
1013 | 5.34k | case ATOM_ftyp: |
1014 | 5.34k | { |
1015 | | /* Early handle some brands */ |
1016 | 5.34k | switch( VLC_FOURCC(p_peek[8], p_peek[9], p_peek[10], p_peek[11]) ) |
1017 | 5.34k | { |
1018 | | /* HEIF pictures goes to heif demux */ |
1019 | 19 | case BRAND_heic: |
1020 | 42 | case BRAND_heix: |
1021 | 51 | case BRAND_mif1: |
1022 | 54 | case BRAND_jpeg: |
1023 | 55 | case BRAND_avci: |
1024 | 594 | case BRAND_avif: |
1025 | | /* We don't yet support f4v, but avformat does. */ |
1026 | 595 | case BRAND_f4v: |
1027 | 595 | return VLC_EGENERIC; |
1028 | 4.75k | default: |
1029 | 4.75k | break; |
1030 | 5.34k | } |
1031 | 4.75k | break; |
1032 | 5.34k | } |
1033 | 4.75k | default: |
1034 | 2.26k | return VLC_EGENERIC; |
1035 | 11.1k | } |
1036 | | |
1037 | | /* create our structure that will contains all data */ |
1038 | 8.26k | p_sys = calloc( 1, sizeof( demux_sys_t ) ); |
1039 | 8.26k | if ( !p_sys ) |
1040 | 0 | return VLC_EGENERIC; |
1041 | | |
1042 | | /* I need to seek */ |
1043 | 8.26k | vlc_stream_Control( p_demux->s, STREAM_CAN_SEEK, &p_sys->b_seekable ); |
1044 | 8.26k | if( p_sys->b_seekable ) |
1045 | 8.26k | vlc_stream_Control( p_demux->s, STREAM_CAN_FASTSEEK, &p_sys->b_fastseekable ); |
1046 | | |
1047 | | /*Set exported functions */ |
1048 | 8.26k | p_demux->pf_demux = Demux; |
1049 | 8.26k | p_demux->pf_control = Control; |
1050 | | |
1051 | 8.26k | p_sys->context.i_lastseqnumber = UINT32_MAX; |
1052 | 8.26k | p_sys->i_attachments = -1; |
1053 | 8.26k | p_sys->qt.f_replay_gain_norm = NAN; |
1054 | 8.26k | p_sys->qt.f_replay_gain_peak = NAN; |
1055 | | |
1056 | 8.26k | p_demux->p_sys = p_sys; |
1057 | | |
1058 | 8.26k | if( LoadInitFrag( p_demux ) != VLC_SUCCESS ) |
1059 | 2.39k | goto error; |
1060 | | |
1061 | 5.86k | MP4_BoxDumpStructure( p_demux->s, p_sys->p_root ); |
1062 | | |
1063 | 5.86k | if( ( p_ftyp = MP4_BoxGet( p_sys->p_root, "/ftyp" ) ) ) |
1064 | 4.08k | { |
1065 | 4.08k | switch( BOXDATA(p_ftyp)->i_major_brand ) |
1066 | 4.08k | { |
1067 | 1.53k | case BRAND_isom: |
1068 | 1.53k | msg_Dbg( p_demux, |
1069 | 1.53k | "ISO Media (isom) version %d.", |
1070 | 1.53k | BOXDATA(p_ftyp)->i_minor_version ); |
1071 | 1.53k | break; |
1072 | 2 | case BRAND_3gp4: |
1073 | 6 | case BRAND_3gp5: |
1074 | 7 | case BRAND_3gp6: |
1075 | 8 | case BRAND_3gp7: |
1076 | 8 | msg_Dbg( p_demux, "3GPP Media Release: %4.4s", |
1077 | 8 | (char *)&BOXDATA(p_ftyp)->i_major_brand ); |
1078 | 8 | break; |
1079 | 200 | case BRAND_qt__: |
1080 | 200 | msg_Dbg( p_demux, "Apple QuickTime media" ); |
1081 | 200 | p_sys->b_quicktime = true; |
1082 | 200 | break; |
1083 | 331 | case BRAND_isml: |
1084 | 331 | msg_Dbg( p_demux, "PIFF (= isml = fMP4) media" ); |
1085 | 331 | break; |
1086 | 1 | case BRAND_dash: |
1087 | 1 | msg_Dbg( p_demux, "DASH Stream" ); |
1088 | 1 | break; |
1089 | 414 | case BRAND_M4A: |
1090 | 414 | msg_Dbg( p_demux, "iTunes audio" ); |
1091 | 414 | if( var_InheritBool( p_demux, CFG_PREFIX"m4a-audioonly" ) ) |
1092 | 0 | p_sys->hacks.es_cat_filter = AUDIO_ES; |
1093 | 414 | break; |
1094 | 1.59k | default: |
1095 | 1.59k | msg_Dbg( p_demux, |
1096 | 1.59k | "unrecognized major media specification (%4.4s).", |
1097 | 1.59k | (char*)&BOXDATA(p_ftyp)->i_major_brand ); |
1098 | 1.59k | break; |
1099 | 4.08k | } |
1100 | | /* also lookup in compatibility list */ |
1101 | 13.8k | for(uint32_t i=0; i<BOXDATA(p_ftyp)->i_compatible_brands_count; i++) |
1102 | 9.81k | { |
1103 | 9.81k | if (BOXDATA(p_ftyp)->i_compatible_brands[i] == BRAND_dash) |
1104 | 151 | { |
1105 | 151 | msg_Dbg( p_demux, "DASH Stream" ); |
1106 | 151 | } |
1107 | 9.66k | else if (BOXDATA(p_ftyp)->i_compatible_brands[i] == BRAND_smoo) |
1108 | 35 | { |
1109 | 35 | msg_Dbg( p_demux, "Handling VLC Smooth Stream" ); |
1110 | 35 | } |
1111 | 9.81k | } |
1112 | 4.08k | } |
1113 | 1.78k | else |
1114 | 1.78k | { |
1115 | 1.78k | msg_Dbg( p_demux, "file type box missing (assuming ISO Media)" ); |
1116 | 1.78k | } |
1117 | | |
1118 | | /* the file need to have one moov box */ |
1119 | 5.86k | p_sys->p_moov = MP4_BoxGet( p_sys->p_root, "/moov" ); |
1120 | 5.86k | if( unlikely(!p_sys->p_moov) ) |
1121 | 0 | { |
1122 | 0 | p_sys->p_moov = MP4_BoxGet( p_sys->p_root, "/foov" ); |
1123 | 0 | if( !p_sys->p_moov ) |
1124 | 0 | { |
1125 | 0 | msg_Err( p_demux, "MP4 plugin discarded (no moov,foov,moof box)" ); |
1126 | 0 | goto error; |
1127 | 0 | } |
1128 | | /* we have a free box as a moov, rename it */ |
1129 | 0 | p_sys->p_moov->i_type = ATOM_moov; |
1130 | 0 | } |
1131 | | |
1132 | 5.86k | p_mvhd = MP4_BoxGet( p_sys->p_moov, "mvhd" ); |
1133 | 5.86k | if( p_mvhd && BOXDATA(p_mvhd) && BOXDATA(p_mvhd)->i_timescale && |
1134 | 4.67k | BOXDATA(p_mvhd)->i_duration < INT64_MAX ) |
1135 | 4.65k | { |
1136 | 4.65k | p_sys->i_timescale = BOXDATA(p_mvhd)->i_timescale; |
1137 | 4.65k | p_sys->i_moov_duration = |
1138 | 4.65k | p_sys->i_duration = |
1139 | 4.65k | p_sys->i_cumulated_duration = BOXDATA(p_mvhd)->i_duration; |
1140 | 4.65k | } |
1141 | 1.21k | else |
1142 | 1.21k | { |
1143 | 1.21k | msg_Warn( p_demux, "No valid mvhd found" ); |
1144 | 1.21k | goto error; |
1145 | 1.21k | } |
1146 | | |
1147 | 4.65k | MP4_Box_t *p_rmra = MP4_BoxGet( p_sys->p_root, "/moov/rmra" ); |
1148 | 4.65k | if( p_rmra != NULL && p_demux->p_input_item != NULL ) |
1149 | 0 | { |
1150 | 0 | int i_count = MP4_BoxCount( p_rmra, "rmda" ); |
1151 | 0 | int i; |
1152 | |
|
1153 | 0 | msg_Dbg( p_demux, "detected playlist mov file (%d ref)", i_count ); |
1154 | |
|
1155 | 0 | input_item_t *p_current = p_demux->p_input_item; |
1156 | |
|
1157 | 0 | input_item_node_t *p_subitems = input_item_node_Create( p_current ); |
1158 | |
|
1159 | 0 | for( i = 0; i < i_count; i++ ) |
1160 | 0 | { |
1161 | 0 | MP4_Box_t *p_rdrf = MP4_BoxGetVa( p_rmra, "rmda[%d]/rdrf", i ); |
1162 | 0 | char *psz_ref; |
1163 | 0 | uint32_t i_ref_type; |
1164 | |
|
1165 | 0 | if( !p_rdrf || !BOXDATA(p_rdrf) || !( psz_ref = strdup( BOXDATA(p_rdrf)->psz_ref ) ) ) |
1166 | 0 | { |
1167 | 0 | continue; |
1168 | 0 | } |
1169 | 0 | i_ref_type = BOXDATA(p_rdrf)->i_ref_type; |
1170 | |
|
1171 | 0 | msg_Dbg( p_demux, "new ref=`%s' type=%4.4s", |
1172 | 0 | psz_ref, (char*)&i_ref_type ); |
1173 | |
|
1174 | 0 | if( i_ref_type == VLC_FOURCC( 'u', 'r', 'l', ' ' ) ) |
1175 | 0 | { |
1176 | 0 | if( strstr( psz_ref, "qt5gateQT" ) ) |
1177 | 0 | { |
1178 | 0 | msg_Dbg( p_demux, "ignoring pseudo ref =`%s'", psz_ref ); |
1179 | 0 | free( psz_ref ); |
1180 | 0 | continue; |
1181 | 0 | } |
1182 | 0 | if( !strncmp( psz_ref, "http://", 7 ) || |
1183 | 0 | !strncmp( psz_ref, "rtsp://", 7 ) ) |
1184 | 0 | { |
1185 | 0 | ; |
1186 | 0 | } |
1187 | 0 | else |
1188 | 0 | { |
1189 | 0 | char *psz_absolute = vlc_uri_resolve( p_demux->psz_url, |
1190 | 0 | psz_ref ); |
1191 | 0 | free( psz_ref ); |
1192 | 0 | if( psz_absolute == NULL ) |
1193 | 0 | { |
1194 | 0 | input_item_node_Delete( p_subitems ); |
1195 | 0 | return VLC_ENOMEM; |
1196 | 0 | } |
1197 | 0 | psz_ref = psz_absolute; |
1198 | 0 | } |
1199 | 0 | msg_Dbg( p_demux, "adding ref = `%s'", psz_ref ); |
1200 | 0 | input_item_t *p_item = input_item_New( psz_ref, NULL ); |
1201 | 0 | input_item_node_AppendItem( p_subitems, p_item ); |
1202 | 0 | input_item_Release( p_item ); |
1203 | 0 | } |
1204 | 0 | else |
1205 | 0 | { |
1206 | 0 | msg_Err( p_demux, "unknown ref type=%4.4s FIXME (send a bug report)", |
1207 | 0 | (char*)&BOXDATA(p_rdrf)->i_ref_type ); |
1208 | 0 | } |
1209 | 0 | free( psz_ref ); |
1210 | 0 | } |
1211 | | |
1212 | | /* FIXME: create a stream_filter sub-module for this */ |
1213 | 0 | if (es_out_Control(p_demux->out, ES_OUT_POST_SUBNODE, p_subitems)) |
1214 | 0 | input_item_node_Delete(p_subitems); |
1215 | 0 | } |
1216 | | |
1217 | 4.65k | if( !(p_mvhd = MP4_BoxGet( p_sys->p_root, "/moov/mvhd" ) ) ) |
1218 | 0 | { |
1219 | 0 | if( !p_rmra ) |
1220 | 0 | { |
1221 | 0 | msg_Err( p_demux, "cannot find /moov/mvhd" ); |
1222 | 0 | goto error; |
1223 | 0 | } |
1224 | 0 | else |
1225 | 0 | { |
1226 | 0 | msg_Warn( p_demux, "cannot find /moov/mvhd (pure ref file)" ); |
1227 | 0 | p_demux->pf_demux = DemuxRef; |
1228 | 0 | return VLC_SUCCESS; |
1229 | 0 | } |
1230 | 0 | } |
1231 | 4.65k | else |
1232 | 4.65k | { |
1233 | 4.65k | p_sys->i_timescale = BOXDATA(p_mvhd)->i_timescale; |
1234 | 4.65k | if( p_sys->i_timescale == 0 || p_sys->i_timescale > 0x10000000 ) |
1235 | 157 | { |
1236 | 157 | msg_Err( p_this, "bad timescale %" PRIu32, p_sys->i_timescale ); |
1237 | 157 | goto error; |
1238 | 157 | } |
1239 | 4.65k | } |
1240 | | |
1241 | 4.49k | const unsigned i_tracks = MP4_BoxCount( p_sys->p_root, "/moov/trak" ); |
1242 | 4.49k | if( i_tracks < 1 ) |
1243 | 20 | { |
1244 | 20 | msg_Err( p_demux, "cannot find any /moov/trak" ); |
1245 | 20 | goto error; |
1246 | 20 | } |
1247 | 4.47k | msg_Dbg( p_demux, "found %u track%c", i_tracks, i_tracks ? 's':' ' ); |
1248 | | |
1249 | | /* reject old monolithically embedded mpeg */ |
1250 | 4.47k | if( i_tracks == 1 ) |
1251 | 3.45k | { |
1252 | 3.45k | const MP4_Box_t *p_hdlr = MP4_BoxGet( p_sys->p_root, "/moov/trak/mdia/hdlr" ); |
1253 | 3.45k | if( p_hdlr && BOXDATA(p_hdlr) ) |
1254 | 3.00k | { |
1255 | 3.00k | switch( BOXDATA(p_hdlr)->i_handler_type ) |
1256 | 3.00k | { |
1257 | 1 | case HANDLER_m1v: |
1258 | 2 | case HANDLER_m1s: |
1259 | 3 | case HANDLER_mpeg: |
1260 | 3 | msg_Dbg( p_demux, "Raw MPEG/PS not supported, passing to ps demux" ); |
1261 | 3 | goto error; |
1262 | 2.99k | default: |
1263 | 2.99k | break; |
1264 | 3.00k | } |
1265 | 3.00k | } |
1266 | 3.45k | } |
1267 | | |
1268 | 4.47k | if( CreateTracks( p_demux, i_tracks ) != VLC_SUCCESS ) |
1269 | 0 | goto error; |
1270 | | |
1271 | | /* set referenced tracks and |
1272 | | * check that at least 1 stream is enabled */ |
1273 | 4.47k | b_enabled_es = false; |
1274 | 10.3k | for( unsigned i = 0; i < p_sys->i_tracks; i++ ) |
1275 | 5.91k | { |
1276 | 5.91k | MP4_Box_t *p_trak = MP4_BoxGetVa( p_sys->p_root, "/moov/trak[%u]", i ); |
1277 | | |
1278 | | /* Enabled check as explained above */ |
1279 | 5.91k | MP4_Box_t *p_tkhd = MP4_BoxGet( p_trak, "tkhd" ); |
1280 | 5.91k | if( p_tkhd && BOXDATA(p_tkhd) && (BOXDATA(p_tkhd)->i_flags&MP4_TRACK_ENABLED) ) |
1281 | 4.47k | b_enabled_es = true; |
1282 | | |
1283 | | /* Referenced tracks checks */ |
1284 | 5.91k | MP4_Box_t *p_tref = MP4_BoxGet( p_trak, "tref" ); |
1285 | 5.91k | if(!p_tref) |
1286 | 5.57k | continue; |
1287 | | |
1288 | 707 | for( MP4_Box_t *p_refbox = p_tref->p_first; p_refbox; p_refbox = p_refbox->p_next ) |
1289 | 364 | { |
1290 | 364 | const MP4_Box_data_trak_reference_t *refdata = p_refbox->data.p_track_reference; |
1291 | 364 | if(unlikely(!refdata)) |
1292 | 0 | continue; |
1293 | | |
1294 | | /* Assign reference types */ |
1295 | 5.24k | for( uint32_t j = 0; j < refdata->i_entry_count; j++ ) |
1296 | 4.88k | { |
1297 | 4.88k | mp4_track_t *reftk = MP4_GetTrackByTrackID(p_demux, refdata->i_track_ID[j]); |
1298 | 4.88k | if(reftk) |
1299 | 428 | { |
1300 | 428 | msg_Dbg( p_demux, "track 0x%x refs track 0x%x for %4.4s", i, |
1301 | 428 | refdata->i_track_ID[j], (const char *) &p_refbox->i_type ); |
1302 | 428 | reftk->i_use_flags |= MP4_reftypeToFlag( p_refbox->i_type ); |
1303 | 428 | } |
1304 | 4.88k | } |
1305 | 364 | } |
1306 | 343 | } |
1307 | | |
1308 | | /* Set and store metadata */ |
1309 | 4.47k | if( (p_sys->p_meta = vlc_meta_New()) ) |
1310 | 4.47k | MP4_LoadMeta( p_sys, p_sys->p_meta ); |
1311 | | |
1312 | | /* now process each track and extract all useful information */ |
1313 | 10.3k | for( unsigned i = 0; i < p_sys->i_tracks; i++ ) |
1314 | 5.91k | { |
1315 | 5.91k | const MP4_Box_t *p_trakbox = MP4_BoxGetVa( p_sys->p_root, "/moov/trak[%u]", i ); |
1316 | 5.91k | MP4_TrackSetup( p_demux, &p_sys->track[i], p_trakbox, true, !b_enabled_es ); |
1317 | 5.91k | mp4_track_t *p_track = &p_sys->track[i]; |
1318 | | |
1319 | 5.91k | if( p_track->b_ok && ! MP4_isMetadata(p_track) ) |
1320 | 3.81k | { |
1321 | 3.81k | const char *psz_cat; |
1322 | 3.81k | switch( p_track->fmt.i_cat ) |
1323 | 3.81k | { |
1324 | 1.17k | case( VIDEO_ES ): |
1325 | 1.17k | psz_cat = "video"; |
1326 | 1.17k | break; |
1327 | 2.00k | case( AUDIO_ES ): |
1328 | 2.00k | psz_cat = "audio"; |
1329 | 2.00k | break; |
1330 | 576 | case( SPU_ES ): |
1331 | 576 | psz_cat = "subtitle"; |
1332 | 576 | break; |
1333 | | |
1334 | 60 | default: |
1335 | 60 | psz_cat = "unknown"; |
1336 | 60 | break; |
1337 | 3.81k | } |
1338 | | |
1339 | 3.81k | msg_Dbg( p_demux, "adding track[Id 0x%x] %s (%s) language %s", |
1340 | 3.81k | p_track->i_track_ID, psz_cat, |
1341 | 3.81k | p_track->b_enable ? "enable":"disable", |
1342 | 3.81k | p_track->fmt.psz_language ? |
1343 | 3.81k | p_track->fmt.psz_language : "undef" ); |
1344 | 3.81k | } |
1345 | 2.10k | else if( p_track->b_ok && (p_track->i_use_flags & USEAS_CHAPTERS) ) |
1346 | 0 | { |
1347 | 0 | msg_Dbg( p_demux, "using track[Id 0x%x] for chapter language %s", |
1348 | 0 | p_track->i_track_ID, |
1349 | 0 | p_track->fmt.psz_language ? |
1350 | 0 | p_track->fmt.psz_language : "undef" ); |
1351 | 0 | } |
1352 | 2.10k | else |
1353 | 2.10k | { |
1354 | 2.10k | msg_Dbg( p_demux, "ignoring track[Id 0x%x] %d refs %x", |
1355 | 2.10k | p_track->i_track_ID, p_track->b_ok, p_track->i_use_flags ); |
1356 | 2.10k | } |
1357 | | |
1358 | 5.91k | if( p_track->b_ok && p_track->i_cts_shift ) /* PTS shift handling pass 1 */ |
1359 | 366 | { |
1360 | 366 | p_track->i_pts_offset = MP4_rescale_mtime( p_track->i_cts_shift, |
1361 | 366 | p_track->i_timescale ); |
1362 | 366 | if( p_track->i_pts_offset > p_sys->i_max_pts_offset ) |
1363 | 320 | p_sys->i_max_pts_offset = p_track->i_pts_offset; |
1364 | 366 | } |
1365 | 5.91k | } |
1366 | | |
1367 | 10.3k | for( unsigned i = 0; i < p_sys->i_tracks; i++ ) /* PTS shift handling pass 2 */ |
1368 | 5.91k | { |
1369 | 5.91k | mp4_track_t *p_track = &p_sys->track[i]; |
1370 | 5.91k | if( !p_track->b_ok ) |
1371 | 2.08k | continue; |
1372 | 3.83k | p_track->i_pts_offset = p_sys->i_max_pts_offset - p_track->i_pts_offset; |
1373 | 3.83k | } |
1374 | | |
1375 | 4.47k | p_mvex = MP4_BoxGet( p_sys->p_moov, "mvex" ); |
1376 | 4.47k | if( p_mvex != NULL ) |
1377 | 637 | { |
1378 | 637 | const MP4_Box_t *p_mehd = MP4_BoxGet( p_mvex, "mehd"); |
1379 | 637 | if ( p_mehd && BOXDATA(p_mehd) ) |
1380 | 364 | { |
1381 | 364 | if( BOXDATA(p_mehd)->i_fragment_duration > p_sys->i_duration ) |
1382 | 145 | { |
1383 | 145 | p_sys->b_fragmented = true; |
1384 | 145 | p_sys->i_duration = BOXDATA(p_mehd)->i_fragment_duration; |
1385 | 145 | } |
1386 | 364 | } |
1387 | | |
1388 | 637 | const MP4_Box_t *p_sidx = MP4_BoxGet( p_sys->p_root, "sidx"); |
1389 | 637 | if( p_sidx ) |
1390 | 90 | p_sys->b_fragmented = true; |
1391 | | |
1392 | 637 | if ( p_sys->b_seekable ) |
1393 | 637 | { |
1394 | 637 | if( !p_sys->b_fragmented /* as unknown */ ) |
1395 | 472 | { |
1396 | | /* Probe remaining to check if there's really fragments |
1397 | | or if that file is just ready to append fragments */ |
1398 | 472 | ProbeFragments( p_demux, (p_sys->i_duration == 0), &p_sys->b_fragmented ); |
1399 | 472 | } |
1400 | | |
1401 | 637 | if( vlc_stream_Seek( p_demux->s, p_sys->p_moov->i_pos ) != VLC_SUCCESS ) |
1402 | 0 | goto error; |
1403 | 637 | } |
1404 | 0 | else /* Handle as fragmented by default as we can't see moof */ |
1405 | 0 | { |
1406 | 0 | p_sys->context.p_fragment_atom = p_sys->p_moov; |
1407 | 0 | p_sys->context.i_current_box_type = ATOM_moov; |
1408 | 0 | p_sys->b_fragmented = true; |
1409 | 0 | } |
1410 | 637 | } |
1411 | | |
1412 | 4.47k | if( p_sys->b_fragmented ) |
1413 | 574 | { |
1414 | 574 | p_demux->pf_demux = DemuxFrag; |
1415 | 574 | msg_Dbg( p_demux, "Set Fragmented demux mode" ); |
1416 | 574 | } |
1417 | | |
1418 | 4.47k | if( !p_sys->b_seekable && p_demux->pf_demux == Demux ) |
1419 | 0 | { |
1420 | 0 | msg_Warn( p_demux, "MP4 plugin discarded (not seekable)" ); |
1421 | 0 | goto error; |
1422 | 0 | } |
1423 | | |
1424 | 4.47k | if( p_sys->i_tracks > 1 && !p_sys->b_fastseekable ) |
1425 | 0 | { |
1426 | 0 | vlc_tick_t i_max_continuity; |
1427 | 0 | bool b_flat; |
1428 | 0 | MP4_GetInterleaving( p_demux, &i_max_continuity, &b_flat ); |
1429 | 0 | if( b_flat ) |
1430 | 0 | msg_Warn( p_demux, "that media doesn't look interleaved, will need to seek"); |
1431 | 0 | else if( i_max_continuity > DEMUX_TRACK_MAX_PRELOAD ) |
1432 | 0 | msg_Warn( p_demux, "that media doesn't look properly interleaved, will need to seek"); |
1433 | 0 | } |
1434 | | |
1435 | | /* */ |
1436 | 4.47k | LoadChapter( p_demux ); |
1437 | | |
1438 | 4.47k | p_sys->asfpacketsys.priv = p_demux; |
1439 | 4.47k | p_sys->asfpacketsys.s = p_demux->s; |
1440 | 4.47k | p_sys->asfpacketsys.logger = p_demux->obj.logger; |
1441 | 4.47k | p_sys->asfpacketsys.pi_preroll = &p_sys->i_preroll; |
1442 | 4.47k | p_sys->asfpacketsys.pi_preroll_start = &p_sys->i_preroll_start; |
1443 | 4.47k | p_sys->asfpacketsys.b_deduplicate = true; |
1444 | 4.47k | p_sys->asfpacketsys.b_can_hold_multiple_packets = true; |
1445 | 4.47k | p_sys->asfpacketsys.pf_doskip = NULL; |
1446 | 4.47k | p_sys->asfpacketsys.pf_send = MP4ASF_Send; |
1447 | 4.47k | p_sys->asfpacketsys.pf_gettrackinfo = MP4ASF_GetTrackInfo; |
1448 | 4.47k | p_sys->asfpacketsys.pf_updatetime = NULL; |
1449 | 4.47k | p_sys->asfpacketsys.pf_setaspectratio = NULL; |
1450 | | |
1451 | 4.47k | p_sys->hacks.es_cat_filter = UNKNOWN_ES; |
1452 | | |
1453 | 4.47k | return VLC_SUCCESS; |
1454 | | |
1455 | 3.79k | error: |
1456 | 3.79k | if( vlc_stream_Tell( p_demux->s ) > 0 ) |
1457 | 3.79k | { |
1458 | 3.79k | if( vlc_stream_Seek( p_demux->s, 0 ) != VLC_SUCCESS ) |
1459 | 3.79k | msg_Warn( p_demux, "Can't reset stream position from probing" ); |
1460 | 3.79k | } |
1461 | | |
1462 | 3.79k | Close( p_this ); |
1463 | | |
1464 | 3.79k | return VLC_EGENERIC; |
1465 | 4.47k | } |
1466 | | |
1467 | | const unsigned int SAMPLEHEADERSIZE = 4; |
1468 | | const unsigned int RTPPACKETSIZE = 12; |
1469 | | const unsigned int CONSTRUCTORSIZE = 16; |
1470 | | |
1471 | | /******************************************************************************* |
1472 | | * MP4_RTPHintToFrame: converts RTP Reception Hint Track sample to H.264 frame |
1473 | | *******************************************************************************/ |
1474 | | static block_t * MP4_RTPHintToFrame( demux_t *p_demux, block_t *p_block, uint32_t packetcount ) |
1475 | 0 | { |
1476 | 0 | uint8_t *p_slice = p_block->p_buffer + SAMPLEHEADERSIZE; |
1477 | 0 | block_t *p_newblock = NULL; |
1478 | 0 | size_t i_payload = 0; |
1479 | |
|
1480 | 0 | if( p_block->i_buffer < SAMPLEHEADERSIZE + RTPPACKETSIZE + CONSTRUCTORSIZE ) |
1481 | 0 | { |
1482 | 0 | msg_Err( p_demux, "Sample not large enough for necessary structs"); |
1483 | 0 | block_Release( p_block ); |
1484 | 0 | return NULL; |
1485 | 0 | } |
1486 | | |
1487 | 0 | for( uint32_t i = 0; i < packetcount; ++i ) |
1488 | 0 | { |
1489 | 0 | if( (size_t)(p_slice - p_block->p_buffer) + RTPPACKETSIZE + CONSTRUCTORSIZE > p_block->i_buffer ) |
1490 | 0 | goto error; |
1491 | | |
1492 | | /* skip RTP header in sample. Could be used to detect packet losses */ |
1493 | 0 | p_slice += RTPPACKETSIZE; |
1494 | |
|
1495 | 0 | mp4_rtpsampleconstructor_t sample_cons; |
1496 | |
|
1497 | 0 | sample_cons.type = p_slice[0]; |
1498 | 0 | sample_cons.trackrefindex = p_slice[1]; |
1499 | 0 | sample_cons.length = GetWBE( &p_slice[2] ); |
1500 | 0 | sample_cons.samplenumber = GetDWBE( &p_slice[4] ); |
1501 | 0 | sample_cons.sampleoffset = GetDWBE( &p_slice[8] ); |
1502 | 0 | sample_cons.bytesperblock = GetWBE( &p_slice[12] ); |
1503 | 0 | sample_cons.samplesperblock = GetWBE( &p_slice[14] ); |
1504 | | |
1505 | | /* skip packet constructor */ |
1506 | 0 | p_slice += CONSTRUCTORSIZE; |
1507 | | |
1508 | | /* check that is RTPsampleconstructor, referencing itself and no weird audio stuff */ |
1509 | 0 | if( sample_cons.type != 2||sample_cons.trackrefindex != -1 |
1510 | 0 | ||sample_cons.samplesperblock != 1||sample_cons.bytesperblock != 1 ) |
1511 | 0 | { |
1512 | 0 | msg_Err(p_demux, "Unhandled constructor in RTP Reception Hint Track. Type:%u", sample_cons.type); |
1513 | 0 | goto error; |
1514 | 0 | } |
1515 | | |
1516 | | /* slice doesn't fit in buffer */ |
1517 | 0 | if( sample_cons.sampleoffset + sample_cons.length > p_block->i_buffer) |
1518 | 0 | { |
1519 | 0 | msg_Err(p_demux, "Sample buffer is smaller than sample" ); |
1520 | 0 | goto error; |
1521 | 0 | } |
1522 | | |
1523 | 0 | block_t *p_realloc = ( p_newblock ) ? |
1524 | 0 | block_Realloc( p_newblock, 0, i_payload + sample_cons.length + 4 ): |
1525 | 0 | block_Alloc( i_payload + sample_cons.length + 4 ); |
1526 | 0 | if( !p_realloc ) |
1527 | 0 | goto error; |
1528 | | |
1529 | 0 | p_newblock = p_realloc; |
1530 | 0 | uint8_t *p_dst = &p_newblock->p_buffer[i_payload]; |
1531 | |
|
1532 | 0 | const uint8_t* p_src = p_block->p_buffer + sample_cons.sampleoffset; |
1533 | 0 | uint8_t i_type = (*p_src) & ((1<<5)-1); |
1534 | |
|
1535 | 0 | const uint8_t synccode[4] = { 0, 0, 0, 1 }; |
1536 | 0 | if( memcmp( p_src, synccode, 4 ) ) |
1537 | 0 | { |
1538 | 0 | if( i_type == 7 || i_type == 8 ) |
1539 | 0 | *p_dst++=0; |
1540 | |
|
1541 | 0 | p_dst[0] = 0; |
1542 | 0 | p_dst[1] = 0; |
1543 | 0 | p_dst[2] = 1; |
1544 | 0 | p_dst += 3; |
1545 | 0 | } |
1546 | |
|
1547 | 0 | memcpy( p_dst, p_src, sample_cons.length ); |
1548 | 0 | p_dst += sample_cons.length; |
1549 | |
|
1550 | 0 | i_payload = p_dst - p_newblock->p_buffer; |
1551 | 0 | } |
1552 | | |
1553 | 0 | block_Release( p_block ); |
1554 | 0 | if( p_newblock ) |
1555 | 0 | p_newblock->i_buffer = i_payload; |
1556 | 0 | return p_newblock; |
1557 | | |
1558 | 0 | error: |
1559 | 0 | block_Release( p_block ); |
1560 | 0 | if( p_newblock ) |
1561 | 0 | block_Release( p_newblock ); |
1562 | 0 | return NULL; |
1563 | 0 | } |
1564 | | |
1565 | | /* RTP Reception Hint Track */ |
1566 | | static block_t * MP4_RTPHint_Convert( demux_t *p_demux, block_t *p_block, vlc_fourcc_t i_codec ) |
1567 | 0 | { |
1568 | 0 | block_t *p_converted = NULL; |
1569 | 0 | if( p_block->i_buffer < 2 ) |
1570 | 0 | { |
1571 | 0 | block_Release( p_block ); |
1572 | 0 | return NULL; |
1573 | 0 | } |
1574 | | |
1575 | | /* number of RTP packets contained in this sample */ |
1576 | 0 | const uint16_t i_packets = GetWBE( p_block->p_buffer ); |
1577 | 0 | if( i_packets <= 1 || i_codec != VLC_CODEC_H264 ) |
1578 | 0 | { |
1579 | 0 | const size_t i_skip = SAMPLEHEADERSIZE + i_packets * ( RTPPACKETSIZE + CONSTRUCTORSIZE ); |
1580 | 0 | if( i_packets == 1 && i_skip < p_block->i_buffer ) |
1581 | 0 | { |
1582 | 0 | p_block->p_buffer += i_skip; |
1583 | 0 | p_converted = p_block; |
1584 | 0 | } |
1585 | 0 | else |
1586 | 0 | { |
1587 | 0 | block_Release( p_block ); |
1588 | 0 | } |
1589 | 0 | } |
1590 | 0 | else |
1591 | 0 | { |
1592 | 0 | p_converted = MP4_RTPHintToFrame( p_demux, p_block, i_packets ); |
1593 | 0 | } |
1594 | |
|
1595 | 0 | return p_converted; |
1596 | 0 | } |
1597 | | |
1598 | | static uint64_t OverflowCheck( demux_t *p_demux, mp4_track_t *tk, |
1599 | | uint64_t i_readpos, uint64_t i_samplessize ) |
1600 | 179M | { |
1601 | 179M | demux_sys_t *p_sys = p_demux->p_sys; |
1602 | 179M | if( !p_sys->b_seekable && p_sys->b_fragmented && |
1603 | 0 | p_sys->context.i_post_mdat_offset ) |
1604 | 0 | { |
1605 | | /* avoid breaking non seekable demux */ |
1606 | 0 | if( i_readpos + i_samplessize > p_sys->context.i_post_mdat_offset ) |
1607 | 0 | { |
1608 | 0 | msg_Err(p_demux, "Broken file. track[0x%x] " |
1609 | 0 | "Sample @%" PRIu64 " overflowing " |
1610 | 0 | "parent mdat by %" PRIu64, |
1611 | 0 | tk->i_track_ID, i_readpos, |
1612 | 0 | i_readpos + i_samplessize - p_sys->context.i_post_mdat_offset ); |
1613 | 0 | i_samplessize = p_sys->context.i_post_mdat_offset - i_readpos; |
1614 | 0 | } |
1615 | 0 | } |
1616 | 179M | return i_samplessize; |
1617 | 179M | } |
1618 | | |
1619 | | /***************************************************************************** |
1620 | | * Demux: read packet and send them to decoders |
1621 | | ***************************************************************************** |
1622 | | * TODO check for newly selected track (ie audio upt to now ) |
1623 | | *****************************************************************************/ |
1624 | | static int DemuxTrack( demux_t *p_demux, mp4_track_t *tk, uint64_t i_readpos, |
1625 | | vlc_tick_t i_max_preload ) |
1626 | 178M | { |
1627 | 178M | demux_sys_t *p_sys = p_demux->p_sys; |
1628 | 178M | uint32_t i_nb_samples = 0; |
1629 | 178M | uint32_t i_samplessize = 0; |
1630 | | |
1631 | 178M | if( !tk->b_ok || tk->i_sample >= tk->i_sample_count ) |
1632 | 0 | return VLC_DEMUXER_EOS; |
1633 | | |
1634 | 178M | if( tk->i_use_flags & USEAS_CHAPTERS ) |
1635 | 0 | return VLC_DEMUXER_SUCCESS; |
1636 | | |
1637 | 178M | uint32_t i_run_seq = MP4_TrackGetRunSeq( tk ); |
1638 | 178M | vlc_tick_t i_current_nzpts; |
1639 | 178M | vlc_tick_t i_current_nzdts = MP4_TrackGetDTSPTS( p_demux, tk, &i_current_nzpts ); |
1640 | 178M | const vlc_tick_t i_demux_max_nzdts =i_max_preload < INVALID_PRELOAD |
1641 | 178M | ? i_current_nzdts + i_max_preload |
1642 | 178M | : VLC_TICK_MAX; |
1643 | | |
1644 | 182M | for( ; i_demux_max_nzdts >= i_current_nzdts; ) |
1645 | 179M | { |
1646 | 179M | if( tk->i_sample >= tk->i_sample_count ) |
1647 | 0 | return VLC_DEMUXER_EOS; |
1648 | | |
1649 | | #if 0 |
1650 | | msg_Dbg( p_demux, "tk(%i)=%"PRId64" mv=%"PRId64" pos=%"PRIu64, tk->i_track_ID, |
1651 | | MP4_TrackGetDTSPTS( p_demux, tk, NULL ), |
1652 | | MP4_GetMoviePTS( p_demux->p_sys ), i_readpos ); |
1653 | | #endif |
1654 | | |
1655 | 179M | i_samplessize = MP4_TrackGetReadSize( tk, &i_nb_samples ); |
1656 | 179M | if( i_samplessize > 0 ) |
1657 | 179M | { |
1658 | 179M | block_t *p_block; |
1659 | | |
1660 | 179M | if( vlc_stream_Tell( p_demux->s ) != i_readpos ) |
1661 | 642k | { |
1662 | 642k | if( MP4_Seek( p_demux->s, i_readpos ) != VLC_SUCCESS ) |
1663 | 0 | { |
1664 | 0 | msg_Warn( p_demux, "track[0x%x] will be disabled (eof?)" |
1665 | 0 | ": Failed to seek to %"PRIu64, |
1666 | 0 | tk->i_track_ID, i_readpos ); |
1667 | 0 | MP4_TrackSelect( p_demux, tk, false ); |
1668 | 0 | goto end; |
1669 | 0 | } |
1670 | 642k | } |
1671 | | |
1672 | 179M | i_samplessize = OverflowCheck( p_demux, tk, i_readpos, i_samplessize ); |
1673 | | |
1674 | | /* now read pes */ |
1675 | 179M | if( !(p_block = vlc_stream_Block( p_demux->s, i_samplessize )) ) |
1676 | 176M | { |
1677 | 176M | msg_Warn( p_demux, "track[0x%x] will be disabled (eof?)" |
1678 | 176M | ": Failed to read %d bytes sample at %"PRIu64, |
1679 | 176M | tk->i_track_ID, i_samplessize, i_readpos ); |
1680 | 176M | MP4_TrackSelect( p_demux, tk, false ); |
1681 | 176M | goto end; |
1682 | 176M | } |
1683 | | |
1684 | | /* !important! Ensure clock is set before sending data */ |
1685 | 3.70M | if( p_sys->i_pcr == VLC_TICK_INVALID ) |
1686 | 2.15k | { |
1687 | 2.15k | es_out_SetPCR( p_demux->out, VLC_TICK_0 + i_current_nzdts ); |
1688 | 2.15k | p_sys->i_pcr = VLC_TICK_0 + i_current_nzdts; |
1689 | 2.15k | } |
1690 | | |
1691 | | /* dts */ |
1692 | 3.70M | p_block->i_dts = VLC_TICK_0 + i_current_nzdts; |
1693 | | /* pts */ |
1694 | 3.70M | p_block->i_pts = i_current_nzpts != INVALID_PTS |
1695 | 3.70M | ? VLC_TICK_0 + i_current_nzpts |
1696 | 3.70M | : VLC_TICK_INVALID; |
1697 | | |
1698 | 3.70M | p_block->i_length = MP4_GetSamplesDuration( tk, i_nb_samples ); |
1699 | | |
1700 | 3.70M | if ( tk->fmt.i_codec == VLC_CODEC_APV ) |
1701 | 0 | { |
1702 | | // the APU are preceeded by 4 bytes containing the size of the data |
1703 | | // this is not used by decoder like libavcodec or openapv. |
1704 | 0 | p_block->p_buffer += 4; |
1705 | 0 | p_block->i_buffer -= 4; |
1706 | 0 | } |
1707 | | |
1708 | 3.70M | MP4_Block_Send( p_demux, tk, p_block ); |
1709 | 3.70M | } |
1710 | | |
1711 | | /* Next sample */ |
1712 | 3.70M | if ( i_nb_samples && /* sample size could be 0, need to go fwd. see return */ |
1713 | 3.70M | MP4_TrackNextSample( p_demux, tk, i_nb_samples ) ) |
1714 | 261 | goto end; |
1715 | | |
1716 | 3.70M | uint32_t i_next_run_seq = MP4_TrackGetRunSeq( tk ); |
1717 | 3.70M | if( i_next_run_seq != i_run_seq ) |
1718 | 0 | break; |
1719 | | |
1720 | 3.70M | i_current_nzdts = MP4_TrackGetDTSPTS( p_demux, tk, &i_current_nzpts ); |
1721 | 3.70M | i_readpos = MP4_TrackGetPos( tk ); |
1722 | 3.70M | } |
1723 | | |
1724 | 2.63M | return VLC_DEMUXER_SUCCESS; |
1725 | | |
1726 | 176M | end: |
1727 | 176M | return VLC_DEMUXER_EGENERIC; |
1728 | 178M | } |
1729 | | |
1730 | | static int DemuxMoov( demux_t *p_demux ) |
1731 | 412M | { |
1732 | 412M | demux_sys_t *p_sys = p_demux->p_sys; |
1733 | 412M | unsigned int i_track; |
1734 | | |
1735 | | /* check for newly selected/unselected track */ |
1736 | 1.20G | for( i_track = 0; i_track < p_sys->i_tracks; i_track++ ) |
1737 | 795M | { |
1738 | 795M | mp4_track_t *tk = &p_sys->track[i_track]; |
1739 | 795M | bool b = true; |
1740 | | |
1741 | 795M | if( !tk->b_ok || MP4_isMetadata( tk ) || |
1742 | 605M | ( tk->b_selected && tk->i_sample >= tk->i_sample_count ) ) |
1743 | 190M | { |
1744 | 190M | continue; |
1745 | 190M | } |
1746 | | |
1747 | 605M | if( p_sys->b_seekable ) |
1748 | 605M | es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b ); |
1749 | | |
1750 | 605M | if( tk->b_selected && !b ) |
1751 | 0 | { |
1752 | 0 | MP4_TrackSelect( p_demux, tk, false ); |
1753 | 0 | } |
1754 | 605M | else if( !tk->b_selected && b) |
1755 | 193M | { |
1756 | 193M | MP4_TrackSeek( p_demux, tk, MP4_GetMoviePTS( p_sys ) ); |
1757 | 193M | } |
1758 | 605M | } |
1759 | | |
1760 | 412M | const vlc_tick_t i_nztime = MP4_GetMoviePTS( p_sys ); |
1761 | | |
1762 | | /* We demux/set pcr, even without selected tracks, (empty edits, ...) */ |
1763 | 412M | if( p_sys->i_pcr != VLC_TICK_INVALID /* not after a seek */ ) |
1764 | 400M | { |
1765 | 400M | bool b_eof = true; |
1766 | 1.18G | for( i_track = 0; i_track < p_sys->i_tracks; i_track++ ) |
1767 | 783M | { |
1768 | 783M | mp4_track_t *tk = &p_sys->track[i_track]; |
1769 | 783M | if( !tk->b_ok || MP4_isMetadata( tk ) || tk->i_sample >= tk->i_sample_count ) |
1770 | 190M | continue; |
1771 | | /* Test for EOF on each track (samples count, edit list) */ |
1772 | 593M | b_eof &= ( i_nztime > MP4_TrackGetDTSPTS( p_demux, tk, NULL ) ); |
1773 | 593M | } |
1774 | 400M | if( b_eof ) |
1775 | 0 | return VLC_DEMUXER_EOS; |
1776 | 400M | } |
1777 | | |
1778 | 412M | const vlc_tick_t i_max_preload = ( p_sys->b_fastseekable ) ? 0 : ( p_sys->b_seekable ) ? DEMUX_TRACK_MAX_PRELOAD : INVALID_PRELOAD; |
1779 | 412M | int i_status; |
1780 | | /* demux up to increment amount of data on every track, or just set pcr if empty data */ |
1781 | 412M | for( ;; ) |
1782 | 590M | { |
1783 | 590M | mp4_track_t *tk = NULL; |
1784 | 590M | i_status = VLC_DEMUXER_EOS; |
1785 | | |
1786 | | /* First pass, find any track within our target increment, ordered by position */ |
1787 | 1.91G | for( i_track = 0; i_track < p_sys->i_tracks; i_track++ ) |
1788 | 1.32G | { |
1789 | 1.32G | mp4_track_t *tk_tmp = &p_sys->track[i_track]; |
1790 | 1.32G | if( !tk_tmp->b_ok || MP4_isMetadata( tk_tmp ) || |
1791 | 961M | tk_tmp->i_sample >= tk_tmp->i_sample_count || |
1792 | 961M | (!tk_tmp->b_selected && p_sys->b_seekable) ) |
1793 | 560M | continue; |
1794 | | |
1795 | | /* At least still have data to demux on this or next turns */ |
1796 | 767M | i_status = VLC_DEMUXER_SUCCESS; |
1797 | | |
1798 | 767M | if ( MP4_TrackGetDTSPTS( p_demux, tk_tmp, NULL ) <= i_nztime + DEMUX_INCREMENT ) |
1799 | 178M | { |
1800 | 178M | if( tk == NULL || MP4_TrackGetPos( tk_tmp ) < MP4_TrackGetPos( tk ) ) |
1801 | 178M | tk = tk_tmp; |
1802 | 178M | } |
1803 | 767M | } |
1804 | | |
1805 | 590M | if( tk ) |
1806 | 178M | { |
1807 | | /* Second pass, refine and find any best candidate having a chunk pos closer than |
1808 | | * current candidate (avoids seeks when increment falls between the 2) from |
1809 | | * current position, but within extended interleave time */ |
1810 | 178M | for( i_track = 0; i_max_preload != 0 && i_track < p_sys->i_tracks; i_track++ ) |
1811 | 0 | { |
1812 | 0 | mp4_track_t *tk_tmp = &p_sys->track[i_track]; |
1813 | 0 | if( tk_tmp == tk || |
1814 | 0 | !tk_tmp->b_ok || MP4_isMetadata( tk_tmp ) || |
1815 | 0 | (!tk_tmp->b_selected && p_sys->b_seekable) || |
1816 | 0 | tk_tmp->i_sample >= tk_tmp->i_sample_count ) |
1817 | 0 | continue; |
1818 | | |
1819 | 0 | vlc_tick_t i_nzdts = MP4_TrackGetDTSPTS( p_demux, tk_tmp, NULL ); |
1820 | 0 | if ( i_nzdts <= i_nztime + DEMUX_TRACK_MAX_PRELOAD ) |
1821 | 0 | { |
1822 | | /* Found a better candidate to avoid seeking */ |
1823 | 0 | if( MP4_TrackGetPos( tk_tmp ) < MP4_TrackGetPos( tk ) ) |
1824 | 0 | tk = tk_tmp; |
1825 | | /* Note: previous candidate will be repicked on next loop */ |
1826 | 0 | } |
1827 | 0 | } |
1828 | | |
1829 | 178M | uint64_t i_pos = MP4_TrackGetPos( tk ); |
1830 | 178M | int i_ret = DemuxTrack( p_demux, tk, i_pos, i_max_preload ); |
1831 | | |
1832 | 178M | if( i_ret == VLC_DEMUXER_SUCCESS ) |
1833 | 2.63M | i_status = VLC_DEMUXER_SUCCESS; |
1834 | 178M | } |
1835 | | |
1836 | 590M | if( i_status != VLC_DEMUXER_SUCCESS || !tk ) |
1837 | 412M | break; |
1838 | 590M | } |
1839 | | |
1840 | 412M | p_sys->i_nztime += DEMUX_INCREMENT; |
1841 | 412M | if( p_sys->i_pcr != VLC_TICK_INVALID ) |
1842 | 400M | { |
1843 | 400M | p_sys->i_pcr = VLC_TICK_0 + p_sys->i_nztime; |
1844 | 400M | es_out_SetPCR( p_demux->out, p_sys->i_pcr ); |
1845 | 400M | } |
1846 | | |
1847 | | /* */ |
1848 | 412M | MP4_UpdateSeekpoint( p_demux, i_nztime + DEMUX_INCREMENT ); |
1849 | | |
1850 | 412M | return i_status; |
1851 | 412M | } |
1852 | | |
1853 | | static int Demux( demux_t *p_demux ) |
1854 | 412M | { |
1855 | 412M | demux_sys_t *p_sys = p_demux->p_sys; |
1856 | | |
1857 | 412M | assert( ! p_sys->b_fragmented ); |
1858 | | |
1859 | 412M | int i_status = DemuxMoov( p_demux ); |
1860 | | |
1861 | 412M | if( i_status == VLC_DEMUXER_EOS ) |
1862 | 3.89k | i_status = VLC_DEMUXER_EOF; |
1863 | | |
1864 | 412M | return i_status; |
1865 | 412M | } |
1866 | | |
1867 | | static void MP4_UpdateSeekpoint( demux_t *p_demux, vlc_tick_t i_time ) |
1868 | 412M | { |
1869 | 412M | demux_sys_t *p_sys = p_demux->p_sys; |
1870 | 412M | int i; |
1871 | 412M | if( !p_sys->p_title ) |
1872 | 412M | return; |
1873 | 3.90k | for( i = 0; i < p_sys->p_title->i_seekpoint; i++ ) |
1874 | 2.11k | { |
1875 | 2.11k | if( i_time < p_sys->p_title->seekpoint[i]->i_time_offset ) |
1876 | 311 | break; |
1877 | 2.11k | } |
1878 | 2.10k | i--; |
1879 | | |
1880 | 2.10k | if( i != p_sys->i_seekpoint && i >= 0 ) |
1881 | 2 | { |
1882 | 2 | p_sys->i_seekpoint = i; |
1883 | 2 | p_sys->seekpoint_changed = true; |
1884 | 2 | } |
1885 | 2.10k | } |
1886 | | /***************************************************************************** |
1887 | | * Seek: Go to i_date |
1888 | | ******************************************************************************/ |
1889 | | static int Seek( demux_t *p_demux, vlc_tick_t i_date, bool b_accurate ) |
1890 | 0 | { |
1891 | 0 | demux_sys_t *p_sys = p_demux->p_sys; |
1892 | 0 | unsigned int i_track; |
1893 | | |
1894 | | /* Now for each stream try to go to this time */ |
1895 | 0 | vlc_tick_t i_start = i_date; |
1896 | 0 | for( i_track = 0; i_track < p_sys->i_tracks; i_track++ ) |
1897 | 0 | { |
1898 | 0 | mp4_track_t *tk = &p_sys->track[i_track]; |
1899 | | /* FIXME: we should find the lowest time from tracks with indexes. |
1900 | | considering only video for now */ |
1901 | 0 | if( tk->fmt.i_cat != VIDEO_ES || MP4_isMetadata( tk ) || !tk->b_ok ) |
1902 | 0 | continue; |
1903 | 0 | if( MP4_TrackSeek( p_demux, tk, i_date ) == VLC_SUCCESS ) |
1904 | 0 | { |
1905 | 0 | vlc_tick_t i_seeked = MP4_TrackGetDTSPTS( p_demux, tk, NULL ); |
1906 | 0 | if( i_seeked < i_start ) |
1907 | 0 | i_start = i_seeked; |
1908 | 0 | } |
1909 | 0 | } |
1910 | |
|
1911 | 0 | msg_Dbg( p_demux, "seeking with %"PRId64 "ms %s to %"PRId64, |
1912 | 0 | MS_FROM_VLC_TICK(i_date - i_start), |
1913 | 0 | !b_accurate ? "alignment" : "preroll (use input-fast-seek to avoid)", i_date ); |
1914 | |
|
1915 | 0 | for( i_track = 0; i_track < p_sys->i_tracks; i_track++ ) |
1916 | 0 | { |
1917 | 0 | mp4_track_t *tk = &p_sys->track[i_track]; |
1918 | 0 | tk->i_next_block_flags |= BLOCK_FLAG_DISCONTINUITY; |
1919 | 0 | if( tk->fmt.i_cat == VIDEO_ES || !tk->b_ok ) |
1920 | 0 | continue; |
1921 | 0 | MP4_TrackSeek( p_demux, tk, i_start ); |
1922 | 0 | } |
1923 | |
|
1924 | 0 | MP4_UpdateSeekpoint( p_demux, i_date ); |
1925 | 0 | MP4ASF_ResetFrames( p_sys ); |
1926 | | /* update global time */ |
1927 | 0 | p_sys->i_nztime = i_start; |
1928 | 0 | p_sys->i_pcr = VLC_TICK_INVALID; |
1929 | |
|
1930 | 0 | if( b_accurate ) |
1931 | 0 | es_out_Control( p_demux->out, ES_OUT_SET_NEXT_DISPLAY_TIME, i_date ); |
1932 | |
|
1933 | 0 | return VLC_SUCCESS; |
1934 | 0 | } |
1935 | | |
1936 | | static int FragPrepareChunk( demux_t *p_demux, MP4_Box_t *p_moof, |
1937 | | MP4_Box_t *p_sidx, stime_t i_moof_time, bool b_discontinuity ) |
1938 | 639 | { |
1939 | 639 | demux_sys_t *p_sys = p_demux->p_sys; |
1940 | | |
1941 | 639 | if( b_discontinuity ) |
1942 | 450 | { |
1943 | 1.23k | for( unsigned i=0; i<p_sys->i_tracks; i++ ) |
1944 | 786 | p_sys->track[i].context.b_resync_time_offset = true; |
1945 | 450 | } |
1946 | | |
1947 | 639 | if( FragCreateTrunIndex( p_demux, p_moof, p_sidx, i_moof_time ) == VLC_SUCCESS ) |
1948 | 639 | { |
1949 | 1.72k | for( unsigned i=0; i<p_sys->i_tracks; i++ ) |
1950 | 1.08k | { |
1951 | 1.08k | mp4_track_t *p_track = &p_sys->track[i]; |
1952 | 1.08k | if( p_track->context.runs.i_count ) |
1953 | 358 | { |
1954 | 358 | const mp4_run_t *p_run = &p_track->context.runs.p_array[0]; |
1955 | 358 | p_track->context.i_trun_sample_pos = p_run->i_offset; |
1956 | 358 | p_track->context.i_trun_sample = 0; |
1957 | 358 | p_track->i_time = p_run->i_first_dts; |
1958 | 358 | } |
1959 | 1.08k | } |
1960 | 639 | return VLC_SUCCESS; |
1961 | 639 | } |
1962 | | |
1963 | 0 | return VLC_EGENERIC; |
1964 | 639 | } |
1965 | | |
1966 | | static vlc_tick_t FragGetDemuxTimeFromTracksTime( demux_sys_t *p_sys ) |
1967 | 450 | { |
1968 | 450 | vlc_tick_t i_time = VLC_TICK_MAX; |
1969 | 1.23k | for( unsigned int i = 0; i < p_sys->i_tracks; i++ ) |
1970 | 786 | { |
1971 | 786 | if( p_sys->track[i].context.runs.i_count == 0 ) |
1972 | 568 | continue; |
1973 | 218 | vlc_tick_t i_ttime = MP4_rescale_mtime( p_sys->track[i].i_time, |
1974 | 218 | p_sys->track[i].i_timescale ); |
1975 | 218 | i_time = __MIN( i_time, i_ttime ); |
1976 | 218 | } |
1977 | 450 | return i_time; |
1978 | 450 | } |
1979 | | |
1980 | | static uint32_t FragGetMoofSequenceNumber( MP4_Box_t *p_moof ) |
1981 | 662 | { |
1982 | 662 | const MP4_Box_t *p_mfhd = MP4_BoxGet( p_moof, "mfhd" ); |
1983 | 662 | if( p_mfhd && BOXDATA(p_mfhd) ) |
1984 | 462 | return BOXDATA(p_mfhd)->i_sequence_number; |
1985 | 200 | return 0; |
1986 | 662 | } |
1987 | | |
1988 | | static int FragSeekLoadFragment( demux_t *p_demux, uint32_t i_moox ) |
1989 | 0 | { |
1990 | 0 | demux_sys_t *p_sys = p_demux->p_sys; |
1991 | 0 | MP4_Box_t *p_moox; |
1992 | |
|
1993 | 0 | if( i_moox == ATOM_moov ) |
1994 | 0 | { |
1995 | 0 | p_moox = p_sys->p_moov; |
1996 | 0 | } |
1997 | 0 | else |
1998 | 0 | { |
1999 | 0 | const uint8_t *p_peek; |
2000 | 0 | if( vlc_stream_Peek( p_demux->s, &p_peek, 8 ) != 8 ) |
2001 | 0 | return VLC_EGENERIC; |
2002 | | |
2003 | 0 | if( ATOM_moof != VLC_FOURCC( p_peek[4], p_peek[5], p_peek[6], p_peek[7] ) ) |
2004 | 0 | return VLC_EGENERIC; |
2005 | | |
2006 | 0 | MP4_Box_t *p_vroot = MP4_BoxGetNextChunk( p_demux->s ); |
2007 | 0 | if(!p_vroot) |
2008 | 0 | return VLC_EGENERIC; |
2009 | 0 | p_moox = MP4_BoxExtract( &p_vroot->p_first, ATOM_moof ); |
2010 | 0 | MP4_BoxFree( p_vroot ); |
2011 | |
|
2012 | 0 | if(!p_moox) |
2013 | 0 | return VLC_EGENERIC; |
2014 | 0 | } |
2015 | | |
2016 | 0 | FragResetContext( p_sys ); |
2017 | | |
2018 | | /* map context */ |
2019 | 0 | p_sys->context.p_fragment_atom = p_moox; |
2020 | 0 | p_sys->context.i_current_box_type = i_moox; |
2021 | |
|
2022 | 0 | msg_Dbg( p_demux, "seeked to %4.4s at pos %" PRIu64, (char *) &i_moox, p_moox->i_pos ); |
2023 | 0 | return VLC_SUCCESS; |
2024 | 0 | } |
2025 | | |
2026 | | static unsigned GetSeekTrackIndex( demux_sys_t *p_sys ) |
2027 | 0 | { |
2028 | 0 | unsigned cand = 0; |
2029 | 0 | for( unsigned i=0; i<p_sys->i_tracks; i++ ) |
2030 | 0 | { |
2031 | 0 | if( p_sys->track[i].fmt.i_cat == VIDEO_ES || |
2032 | 0 | p_sys->track[i].fmt.i_cat == AUDIO_ES ) |
2033 | 0 | { |
2034 | 0 | if( cand != i && !p_sys->track[cand].b_selected ) |
2035 | 0 | cand = i; |
2036 | 0 | } |
2037 | 0 | } |
2038 | 0 | return cand; |
2039 | 0 | } |
2040 | | |
2041 | | static void FragTrunSeekToTime( mp4_track_t *p_track, stime_t i_target_time ) |
2042 | 0 | { |
2043 | 0 | if( !p_track->b_ok || p_track->context.runs.i_count < 1 ) |
2044 | 0 | return; |
2045 | | |
2046 | 0 | unsigned i_run = 0; |
2047 | 0 | unsigned i_sample = 0; |
2048 | 0 | uint64_t i_pos = p_track->context.runs.p_array[0].i_offset; |
2049 | 0 | stime_t i_time = p_track->context.runs.p_array[0].i_first_dts; |
2050 | |
|
2051 | 0 | for( unsigned r = 0; r < p_track->context.runs.i_count; r++ ) |
2052 | 0 | { |
2053 | 0 | const mp4_run_t *p_run = &p_track->context.runs.p_array[r]; |
2054 | 0 | const MP4_Box_data_trun_t *p_data = |
2055 | 0 | p_track->context.runs.p_array[r].p_trun->data.p_trun; |
2056 | 0 | if( i_time > i_target_time ) |
2057 | 0 | break; |
2058 | | |
2059 | 0 | i_run = r; |
2060 | 0 | i_time = p_run->i_first_dts; |
2061 | 0 | i_pos = p_run->i_offset; |
2062 | 0 | i_sample = 0; |
2063 | |
|
2064 | 0 | uint32_t dur = p_track->context.i_default_sample_duration; |
2065 | 0 | uint32_t len = p_track->context.i_default_sample_size; |
2066 | 0 | for ( unsigned i=0; i<p_data->i_sample_count; i++ ) |
2067 | 0 | { |
2068 | 0 | if( p_data->i_flags & MP4_TRUN_SAMPLE_DURATION ) |
2069 | 0 | dur = p_data->p_samples[i].i_duration; |
2070 | | |
2071 | | /* check condition */ |
2072 | 0 | if( i_time + dur > i_target_time ) |
2073 | 0 | break; |
2074 | | |
2075 | 0 | if( p_data->i_flags & MP4_TRUN_SAMPLE_SIZE ) |
2076 | 0 | len = p_data->p_samples[i].i_size; |
2077 | |
|
2078 | 0 | i_time += dur; |
2079 | 0 | i_pos += len; |
2080 | 0 | i_sample++; |
2081 | 0 | } |
2082 | 0 | } |
2083 | 0 | p_track->context.i_trun_sample = i_sample; |
2084 | 0 | p_track->context.i_trun_sample_pos = i_pos; |
2085 | 0 | p_track->context.runs.i_current = i_run; |
2086 | 0 | p_track->i_time = i_time; |
2087 | 0 | } |
2088 | | |
2089 | 642 | #define INVALID_SEGMENT_TIME VLC_TICK_MAX |
2090 | | |
2091 | | static int FragSeekToTime( demux_t *p_demux, vlc_tick_t i_nztime, bool b_accurate ) |
2092 | 0 | { |
2093 | 0 | demux_sys_t *p_sys = p_demux->p_sys; |
2094 | 0 | uint64_t i64 = UINT64_MAX; |
2095 | 0 | uint32_t i_segment_type = ATOM_moof; |
2096 | 0 | stime_t i_segment_time = INVALID_SEGMENT_TIME; |
2097 | 0 | vlc_tick_t i_sync_time = i_nztime; |
2098 | |
|
2099 | 0 | const uint64_t i_duration = __MAX(p_sys->i_duration, p_sys->i_cumulated_duration); |
2100 | 0 | if ( !p_sys->i_timescale || !i_duration || !p_sys->b_seekable ) |
2101 | 0 | return VLC_EGENERIC; |
2102 | | |
2103 | 0 | uint64_t i_backup_pos = vlc_stream_Tell( p_demux->s ); |
2104 | |
|
2105 | 0 | if ( !p_sys->b_fragments_probed && !p_sys->b_index_probed && p_sys->b_seekable ) |
2106 | 0 | { |
2107 | 0 | ProbeIndex( p_demux ); |
2108 | 0 | p_sys->b_index_probed = true; |
2109 | 0 | } |
2110 | |
|
2111 | 0 | const unsigned i_seek_track_index = GetSeekTrackIndex( p_sys ); |
2112 | 0 | const unsigned i_seek_track_ID = p_sys->track[i_seek_track_index].i_track_ID; |
2113 | |
|
2114 | 0 | if( MP4_rescale_qtime( i_nztime, p_sys->i_timescale ) |
2115 | 0 | < GetMoovTrackDuration( p_sys, i_seek_track_ID ) ) |
2116 | 0 | { |
2117 | 0 | i64 = p_sys->p_moov->i_pos; |
2118 | 0 | i_segment_type = ATOM_moov; |
2119 | 0 | } |
2120 | 0 | else if( FragGetMoofBySidxIndex( p_demux, i_nztime, &i64, &i_sync_time ) == VLC_SUCCESS ) |
2121 | 0 | { |
2122 | | /* provides base offset */ |
2123 | 0 | i_segment_time = MP4_rescale_qtime( i_sync_time, p_sys->i_timescale ); |
2124 | 0 | msg_Dbg( p_demux, "seeking to sidx moof pos %" PRId64 " %" PRId64, i64, i_sync_time ); |
2125 | 0 | } |
2126 | 0 | else |
2127 | 0 | { |
2128 | 0 | if( FragGetMoofByTfraIndex( p_demux, i_nztime, i_seek_track_ID, &i64, &i_sync_time ) == VLC_SUCCESS ) |
2129 | 0 | { |
2130 | | /* Does only provide segment position and a sync sample time */ |
2131 | 0 | msg_Dbg( p_demux, "seeking to sync point %" PRId64, i_sync_time ); |
2132 | 0 | } |
2133 | 0 | else if( !p_sys->b_fragments_probed ) |
2134 | 0 | { |
2135 | 0 | int i_ret = ProbeFragmentsChecked( p_demux ); |
2136 | 0 | if( i_ret != VLC_SUCCESS ) |
2137 | 0 | return i_ret; |
2138 | 0 | } |
2139 | | |
2140 | 0 | if( p_sys->b_fragments_probed && p_sys->p_fragsindex ) |
2141 | 0 | { |
2142 | 0 | stime_t i_basetime = MP4_rescale_qtime( i_sync_time, p_sys->i_timescale ); |
2143 | 0 | if( !MP4_Fragments_Index_Lookup( p_sys->p_fragsindex, &i_basetime, &i64, i_seek_track_index ) ) |
2144 | 0 | { |
2145 | 0 | p_sys->b_error = (vlc_stream_Seek( p_demux->s, i_backup_pos ) != VLC_SUCCESS); |
2146 | 0 | return VLC_EGENERIC; |
2147 | 0 | } |
2148 | 0 | msg_Dbg( p_demux, "seeking to fragment index pos %" PRId64 " %" PRId64, i64, |
2149 | 0 | MP4_rescale_mtime( i_basetime, p_sys->i_timescale ) ); |
2150 | 0 | } |
2151 | 0 | } |
2152 | | |
2153 | 0 | if( i64 == UINT64_MAX ) |
2154 | 0 | { |
2155 | 0 | msg_Warn( p_demux, "seek by index failed" ); |
2156 | 0 | p_sys->b_error = (vlc_stream_Seek( p_demux->s, i_backup_pos ) != VLC_SUCCESS); |
2157 | 0 | return VLC_EGENERIC; |
2158 | 0 | } |
2159 | | |
2160 | 0 | msg_Dbg( p_demux, "final seek to fragment at %"PRId64, i64 ); |
2161 | 0 | if( vlc_stream_Seek( p_demux->s, i64 ) ) |
2162 | 0 | { |
2163 | 0 | msg_Err( p_demux, "seek failed to %"PRId64, i64 ); |
2164 | 0 | p_sys->b_error = (vlc_stream_Seek( p_demux->s, i_backup_pos ) != VLC_SUCCESS); |
2165 | 0 | return VLC_EGENERIC; |
2166 | 0 | } |
2167 | | |
2168 | | /* Context is killed on success */ |
2169 | 0 | if( FragSeekLoadFragment( p_demux, i_segment_type ) != VLC_SUCCESS ) |
2170 | 0 | { |
2171 | 0 | p_sys->b_error = (vlc_stream_Seek( p_demux->s, i_backup_pos ) != VLC_SUCCESS); |
2172 | 0 | return VLC_EGENERIC; |
2173 | 0 | } |
2174 | 0 | if( i_segment_type == ATOM_moof ) |
2175 | 0 | { |
2176 | 0 | MP4_Box_t *p_moox = p_sys->context.p_fragment_atom; |
2177 | 0 | FragPrepareChunk( p_demux, p_moox, NULL, i_segment_time, true ); |
2178 | 0 | p_sys->context.i_lastseqnumber = FragGetMoofSequenceNumber( p_moox ); |
2179 | |
|
2180 | 0 | p_sys->i_nztime = FragGetDemuxTimeFromTracksTime( p_sys ); |
2181 | 0 | } |
2182 | 0 | else |
2183 | 0 | { |
2184 | 0 | p_sys->i_nztime = i_sync_time; |
2185 | 0 | } |
2186 | |
|
2187 | 0 | p_sys->i_pcr = VLC_TICK_INVALID; |
2188 | |
|
2189 | 0 | for( unsigned i=0; i<p_sys->i_tracks; i++ ) |
2190 | 0 | { |
2191 | 0 | if( i_segment_type == ATOM_moov ) |
2192 | 0 | { |
2193 | 0 | MP4_TrackSeek( p_demux, &p_sys->track[i], i_sync_time ); |
2194 | 0 | p_sys->i_pcr = VLC_TICK_INVALID; |
2195 | 0 | } |
2196 | 0 | else |
2197 | 0 | { |
2198 | 0 | stime_t i_tst = MP4_rescale_qtime( i_sync_time, p_sys->track[i].i_timescale ); |
2199 | 0 | FragTrunSeekToTime( &p_sys->track[i], i_tst ); |
2200 | 0 | p_sys->track[i].i_next_block_flags |= BLOCK_FLAG_DISCONTINUITY; |
2201 | 0 | } |
2202 | 0 | } |
2203 | |
|
2204 | 0 | MP4ASF_ResetFrames( p_sys ); |
2205 | | /* And set next display time in that trun/fragment */ |
2206 | 0 | if( b_accurate ) |
2207 | 0 | es_out_Control( p_demux->out, ES_OUT_SET_NEXT_DISPLAY_TIME, VLC_TICK_0 + i_nztime ); |
2208 | 0 | return VLC_SUCCESS; |
2209 | 0 | } |
2210 | | |
2211 | | static int FragSeekToPos( demux_t *p_demux, double f, bool b_accurate ) |
2212 | 0 | { |
2213 | 0 | demux_sys_t *p_sys = p_demux->p_sys; |
2214 | |
|
2215 | 0 | if ( !p_sys->b_seekable || !p_sys->i_timescale ) |
2216 | 0 | return VLC_EGENERIC; |
2217 | | |
2218 | 0 | uint64_t i_duration = __MAX(p_sys->i_duration, p_sys->i_cumulated_duration); |
2219 | 0 | if( !i_duration && !p_sys->b_fragments_probed ) |
2220 | 0 | { |
2221 | 0 | int i_ret = ProbeFragmentsChecked( p_demux ); |
2222 | 0 | if( i_ret != VLC_SUCCESS ) |
2223 | 0 | return i_ret; |
2224 | 0 | i_duration = __MAX(p_sys->i_duration, p_sys->i_cumulated_duration); |
2225 | 0 | } |
2226 | | |
2227 | 0 | if( !i_duration ) |
2228 | 0 | return VLC_EGENERIC; |
2229 | | |
2230 | 0 | return FragSeekToTime( p_demux, (vlc_tick_t)( f * |
2231 | 0 | MP4_rescale_mtime( i_duration, p_sys->i_timescale ) ), b_accurate ); |
2232 | 0 | } |
2233 | | |
2234 | | static void ItunMetaCallback( const struct qt_itunes_triplet_data *data, void *priv ) |
2235 | 48 | { |
2236 | 48 | demux_sys_t *p_sys = priv; |
2237 | 48 | if( data->type == iTunSMPB ) |
2238 | 21 | { |
2239 | 21 | p_sys->qt.i_delay_samples = data->SMPB.delay; |
2240 | 21 | } |
2241 | 27 | else if( data->type == iTunNORM ) |
2242 | 4 | { |
2243 | 4 | p_sys->qt.f_replay_gain_norm = data->NORM.volume_adjust; |
2244 | 4 | p_sys->qt.f_replay_gain_peak = data->NORM.peak; |
2245 | 4 | } |
2246 | 23 | else if( data->type == iTunEncodingParams ) |
2247 | 23 | { |
2248 | 23 | p_sys->qt.i_target_bitrate = data->EncodingParams.target_bitrate; |
2249 | 23 | } |
2250 | 48 | } |
2251 | | |
2252 | | static int MP4_LoadMeta( demux_sys_t *p_sys, vlc_meta_t *p_meta ) |
2253 | 4.47k | { |
2254 | 4.47k | if( !p_meta ) |
2255 | 0 | return VLC_EGENERIC; |
2256 | | |
2257 | 4.47k | const char *psz_metapath; |
2258 | 4.47k | const MP4_Box_t *p_metaroot = MP4_GetMetaRoot( p_sys->p_root, &psz_metapath ); |
2259 | | |
2260 | 4.47k | MP4_GetCoverMetaURI( p_sys->p_root, p_metaroot, psz_metapath, p_meta ); |
2261 | | |
2262 | 4.47k | if( p_metaroot ) |
2263 | 996 | SetupMeta( p_meta, p_metaroot, ItunMetaCallback, p_sys ); |
2264 | | |
2265 | 4.47k | return VLC_SUCCESS; |
2266 | 4.47k | } |
2267 | | |
2268 | | /***************************************************************************** |
2269 | | * Control: |
2270 | | *****************************************************************************/ |
2271 | | static int Control( demux_t *p_demux, int i_query, va_list args ) |
2272 | 0 | { |
2273 | 0 | demux_sys_t *p_sys = p_demux->p_sys; |
2274 | |
|
2275 | 0 | double f, *pf; |
2276 | 0 | vlc_tick_t i64; |
2277 | 0 | bool b; |
2278 | |
|
2279 | 0 | const uint64_t i_duration = __MAX(p_sys->i_duration, p_sys->i_cumulated_duration); |
2280 | |
|
2281 | 0 | switch( i_query ) |
2282 | 0 | { |
2283 | 0 | case DEMUX_CAN_SEEK: |
2284 | 0 | *va_arg( args, bool * ) = p_sys->b_seekable; |
2285 | 0 | return VLC_SUCCESS; |
2286 | | |
2287 | 0 | case DEMUX_GET_POSITION: |
2288 | 0 | pf = va_arg( args, double * ); |
2289 | 0 | if( i_duration > 0 ) |
2290 | 0 | { |
2291 | 0 | *pf = (double)p_sys->i_nztime / |
2292 | 0 | MP4_rescale_mtime( i_duration, p_sys->i_timescale ); |
2293 | 0 | } |
2294 | 0 | else |
2295 | 0 | { |
2296 | 0 | *pf = 0.0; |
2297 | 0 | } |
2298 | 0 | return VLC_SUCCESS; |
2299 | | |
2300 | 0 | case DEMUX_GET_SEEKPOINT: |
2301 | 0 | *va_arg( args, int * ) = p_sys->i_seekpoint; |
2302 | 0 | return VLC_SUCCESS; |
2303 | | |
2304 | 0 | case DEMUX_SET_POSITION: |
2305 | 0 | f = va_arg( args, double ); |
2306 | 0 | b = va_arg( args, int ); |
2307 | 0 | if ( p_demux->pf_demux == DemuxFrag ) |
2308 | 0 | return FragSeekToPos( p_demux, f, b ); |
2309 | 0 | else if( p_sys->i_timescale > 0 ) |
2310 | 0 | { |
2311 | 0 | i64 = (vlc_tick_t)( f * MP4_rescale_mtime( p_sys->i_duration, |
2312 | 0 | p_sys->i_timescale ) ); |
2313 | 0 | return Seek( p_demux, i64, b ); |
2314 | 0 | } |
2315 | 0 | else return VLC_EGENERIC; |
2316 | | |
2317 | 0 | case DEMUX_GET_TIME: |
2318 | 0 | *va_arg( args, vlc_tick_t * ) = (p_sys->i_max_pts_offset < p_sys->i_nztime) |
2319 | 0 | ? p_sys->i_nztime - p_sys->i_max_pts_offset : 0; |
2320 | 0 | return VLC_SUCCESS; |
2321 | | |
2322 | 0 | case DEMUX_SET_TIME: |
2323 | 0 | i64 = va_arg( args, vlc_tick_t ); |
2324 | 0 | b = va_arg( args, int ); |
2325 | 0 | if ( p_demux->pf_demux == DemuxFrag ) |
2326 | 0 | return FragSeekToTime( p_demux, i64, b ); |
2327 | 0 | else |
2328 | 0 | return Seek( p_demux, i64, b ); |
2329 | | |
2330 | 0 | case DEMUX_GET_LENGTH: |
2331 | 0 | if( p_sys->i_timescale > 0 ) |
2332 | 0 | { |
2333 | 0 | *va_arg( args, vlc_tick_t * ) = MP4_rescale_mtime( i_duration, |
2334 | 0 | p_sys->i_timescale ); |
2335 | 0 | } |
2336 | 0 | else *va_arg( args, vlc_tick_t * ) = 0; |
2337 | 0 | return VLC_SUCCESS; |
2338 | | |
2339 | 0 | case DEMUX_GET_FPS: |
2340 | 0 | pf = va_arg( args, double * ); |
2341 | 0 | *pf = p_sys->f_fps; |
2342 | 0 | return VLC_SUCCESS; |
2343 | | |
2344 | 0 | case DEMUX_GET_ATTACHMENTS: |
2345 | 0 | { |
2346 | 0 | input_attachment_t ***ppp_attach = va_arg( args, input_attachment_t*** ); |
2347 | 0 | int *pi_int = va_arg( args, int * ); |
2348 | |
|
2349 | 0 | if( p_sys->i_attachments == -1 ) |
2350 | 0 | p_sys->i_attachments = MP4_GetAttachments( p_sys->p_root, &p_sys->pp_attachments ); |
2351 | 0 | if( !p_sys->i_attachments ) |
2352 | 0 | return VLC_EGENERIC; |
2353 | 0 | *ppp_attach = calloc( p_sys->i_attachments, sizeof(**ppp_attach ) ); |
2354 | 0 | if( !*ppp_attach ) |
2355 | 0 | return VLC_ENOMEM; |
2356 | 0 | for ( ssize_t i = 0; i < p_sys->i_attachments; ++i ) |
2357 | 0 | { |
2358 | 0 | (*ppp_attach)[i] = vlc_input_attachment_Hold( p_sys->pp_attachments[i] ); |
2359 | 0 | msg_Dbg( p_demux, "adding attachment %s", (*ppp_attach)[i]->psz_name ); |
2360 | 0 | } |
2361 | 0 | *pi_int = p_sys->i_attachments; |
2362 | |
|
2363 | 0 | return VLC_SUCCESS; |
2364 | 0 | } |
2365 | | |
2366 | 0 | case DEMUX_GET_META: |
2367 | 0 | { |
2368 | 0 | vlc_meta_t *p_meta = va_arg( args, vlc_meta_t *); |
2369 | |
|
2370 | 0 | if( !p_sys->p_meta ) |
2371 | 0 | return VLC_EGENERIC; |
2372 | | |
2373 | 0 | vlc_meta_Merge( p_meta, p_sys->p_meta ); |
2374 | |
|
2375 | 0 | return VLC_SUCCESS; |
2376 | 0 | } |
2377 | | |
2378 | 0 | case DEMUX_GET_TITLE_INFO: |
2379 | 0 | { |
2380 | 0 | input_title_t ***ppp_title = va_arg( args, input_title_t *** ); |
2381 | 0 | int *pi_int = va_arg( args, int* ); |
2382 | 0 | int *pi_title_offset = va_arg( args, int* ); |
2383 | 0 | int *pi_seekpoint_offset = va_arg( args, int* ); |
2384 | |
|
2385 | 0 | if( !p_sys->p_title ) |
2386 | 0 | return VLC_EGENERIC; |
2387 | | |
2388 | 0 | *pi_int = 1; |
2389 | 0 | input_title_t **titles = malloc(sizeof(*titles)); |
2390 | 0 | if (titles == NULL) |
2391 | 0 | return VLC_ENOMEM; |
2392 | | |
2393 | 0 | titles[0] = vlc_input_title_Duplicate(p_sys->p_title); |
2394 | 0 | if (titles[0] == NULL) |
2395 | 0 | { |
2396 | 0 | free(titles); |
2397 | 0 | return VLC_ENOMEM; |
2398 | 0 | } |
2399 | 0 | *ppp_title = titles; |
2400 | 0 | *pi_title_offset = 0; |
2401 | 0 | *pi_seekpoint_offset = 0; |
2402 | 0 | return VLC_SUCCESS; |
2403 | 0 | } |
2404 | 0 | case DEMUX_SET_TITLE: |
2405 | 0 | { |
2406 | 0 | const int i_title = va_arg( args, int ); |
2407 | 0 | if( !p_sys->p_title || i_title != 0 ) |
2408 | 0 | return VLC_EGENERIC; |
2409 | 0 | return VLC_SUCCESS; |
2410 | 0 | } |
2411 | 0 | case DEMUX_SET_SEEKPOINT: |
2412 | 0 | { |
2413 | 0 | const int i_seekpoint = va_arg( args, int ); |
2414 | 0 | if( !p_sys->p_title ) |
2415 | 0 | return VLC_EGENERIC; |
2416 | 0 | return Seek( p_demux, p_sys->p_title->seekpoint[i_seekpoint]->i_time_offset, true ); |
2417 | 0 | } |
2418 | 0 | case DEMUX_TEST_AND_CLEAR_FLAGS: |
2419 | 0 | { |
2420 | 0 | unsigned *restrict flags = va_arg( args, unsigned * ); |
2421 | |
|
2422 | 0 | if ((*flags & INPUT_UPDATE_SEEKPOINT) && p_sys->seekpoint_changed) |
2423 | 0 | { |
2424 | 0 | *flags = INPUT_UPDATE_SEEKPOINT; |
2425 | 0 | p_sys->seekpoint_changed = false; |
2426 | 0 | } |
2427 | 0 | else |
2428 | 0 | *flags = 0; |
2429 | 0 | return VLC_SUCCESS; |
2430 | 0 | } |
2431 | | |
2432 | 0 | case DEMUX_GET_PTS_DELAY: |
2433 | 0 | { |
2434 | 0 | for( unsigned int i = 0; i < p_sys->i_tracks; i++ ) |
2435 | 0 | { |
2436 | 0 | const MP4_Box_t *p_load; |
2437 | 0 | if ( (p_load = MP4_BoxGet( p_sys->track[i].p_track, "load" )) && |
2438 | 0 | BOXDATA(p_load)->i_duration > 0 ) |
2439 | 0 | { |
2440 | 0 | *va_arg(args, vlc_tick_t *) = |
2441 | 0 | MP4_rescale_mtime( BOXDATA(p_load)->i_duration, |
2442 | 0 | p_sys->track[i].i_timescale ); |
2443 | 0 | return VLC_SUCCESS; |
2444 | 0 | } |
2445 | 0 | } |
2446 | 0 | return demux_vaControlHelper( p_demux->s, 0, -1, 0, 1, i_query, args ); |
2447 | 0 | } |
2448 | 0 | case DEMUX_SET_NEXT_DEMUX_TIME: |
2449 | 0 | case DEMUX_SET_GROUP_DEFAULT: |
2450 | 0 | case DEMUX_SET_GROUP_ALL: |
2451 | 0 | case DEMUX_SET_GROUP_LIST: |
2452 | 0 | case DEMUX_HAS_UNSUPPORTED_META: |
2453 | 0 | case DEMUX_CAN_RECORD: |
2454 | 0 | return VLC_EGENERIC; |
2455 | | |
2456 | 0 | case DEMUX_CAN_PAUSE: |
2457 | 0 | case DEMUX_SET_PAUSE_STATE: |
2458 | 0 | case DEMUX_CAN_CONTROL_PACE: |
2459 | 0 | return demux_vaControlHelper( p_demux->s, 0, -1, 0, 1, i_query, args ); |
2460 | | |
2461 | 0 | default: |
2462 | 0 | return VLC_EGENERIC; |
2463 | 0 | } |
2464 | 0 | } |
2465 | | |
2466 | | /***************************************************************************** |
2467 | | * Close: frees unused data |
2468 | | *****************************************************************************/ |
2469 | | static void Close ( vlc_object_t * p_this ) |
2470 | 8.26k | { |
2471 | 8.26k | demux_t * p_demux = (demux_t *)p_this; |
2472 | 8.26k | demux_sys_t *p_sys = p_demux->p_sys; |
2473 | 8.26k | unsigned int i_track; |
2474 | | |
2475 | 8.26k | msg_Dbg( p_demux, "freeing all memory" ); |
2476 | | |
2477 | 8.26k | FragResetContext( p_sys ); |
2478 | | |
2479 | 8.26k | MP4_BoxFree( p_sys->p_root ); |
2480 | | |
2481 | 8.26k | if( p_sys->p_title ) |
2482 | 42 | vlc_input_title_Delete( p_sys->p_title ); |
2483 | | |
2484 | 8.26k | if( p_sys->p_meta ) |
2485 | 4.47k | vlc_meta_Delete( p_sys->p_meta ); |
2486 | | |
2487 | 8.26k | MP4_Fragments_Index_Delete( p_sys->p_fragsindex ); |
2488 | | |
2489 | 14.1k | for( i_track = 0; i_track < p_sys->i_tracks; i_track++ ) |
2490 | 5.91k | MP4_TrackClean( p_demux->out, &p_sys->track[i_track] ); |
2491 | 8.26k | free( p_sys->track ); |
2492 | | |
2493 | 8.26k | for ( ssize_t i = 0; i < p_sys->i_attachments; ++i ) |
2494 | 0 | vlc_input_attachment_Release( p_sys->pp_attachments[i] ); |
2495 | 8.26k | free( p_sys->pp_attachments ); |
2496 | | |
2497 | 8.26k | free( p_sys ); |
2498 | 8.26k | } |
2499 | | |
2500 | | |
2501 | | |
2502 | | /**************************************************************************** |
2503 | | * Local functions, specific to vlc |
2504 | | ****************************************************************************/ |
2505 | | /* Chapters */ |
2506 | | static void LoadChapterGpac( demux_t *p_demux, MP4_Box_t *p_chpl ) |
2507 | 28 | { |
2508 | 28 | demux_sys_t *p_sys = p_demux->p_sys; |
2509 | | |
2510 | 28 | if( BOXDATA(p_chpl)->i_chapter == 0 ) |
2511 | 0 | return; |
2512 | | |
2513 | 28 | p_sys->p_title = vlc_input_title_New(); |
2514 | 28 | if (p_sys->p_title == NULL) |
2515 | 0 | return; |
2516 | | |
2517 | 75 | for( int i = 0; i < BOXDATA(p_chpl)->i_chapter; i++ ) |
2518 | 47 | { |
2519 | 47 | seekpoint_t *s = vlc_seekpoint_New(); |
2520 | 47 | if( s == NULL) continue; |
2521 | | |
2522 | 47 | s->psz_name = strdup( BOXDATA(p_chpl)->chapter[i].psz_name ); |
2523 | 47 | if( s->psz_name == NULL) |
2524 | 0 | { |
2525 | 0 | vlc_seekpoint_Delete( s ); |
2526 | 0 | continue; |
2527 | 0 | } |
2528 | | |
2529 | 47 | EnsureUTF8( s->psz_name ); |
2530 | 47 | msftime_t offset = BOXDATA(p_chpl)->chapter[i].i_start; |
2531 | 47 | s->i_time_offset = VLC_TICK_FROM_MSFTIME(offset); |
2532 | 47 | TAB_APPEND( p_sys->p_title->i_seekpoint, p_sys->p_title->seekpoint, s ); |
2533 | 47 | } |
2534 | 28 | } |
2535 | | static void LoadChapterGoPro( demux_t *p_demux, MP4_Box_t *p_hmmt ) |
2536 | 14 | { |
2537 | 14 | demux_sys_t *p_sys = p_demux->p_sys; |
2538 | | |
2539 | 14 | p_sys->p_title = vlc_input_title_New(); |
2540 | 14 | if( p_sys->p_title ) |
2541 | 223 | for( unsigned i = 0; i < BOXDATA(p_hmmt)->i_chapter_count; i++ ) |
2542 | 209 | { |
2543 | 209 | seekpoint_t *s = vlc_seekpoint_New(); |
2544 | 209 | if( s == NULL) |
2545 | 0 | continue; |
2546 | | |
2547 | 209 | if( asprintf( &s->psz_name, "HiLight tag #%u", i+1 ) == -1 ) |
2548 | 0 | { |
2549 | 0 | s->psz_name = NULL; |
2550 | 0 | vlc_seekpoint_Delete( s ); |
2551 | 0 | continue; |
2552 | 0 | } |
2553 | | |
2554 | 209 | EnsureUTF8( s->psz_name ); |
2555 | | /* HiLights are stored in ms so we convert them to µs */ |
2556 | 209 | s->i_time_offset = VLC_TICK_FROM_MS( BOXDATA(p_hmmt)->pi_chapter_start[i] ); |
2557 | 209 | TAB_APPEND( p_sys->p_title->i_seekpoint, p_sys->p_title->seekpoint, s ); |
2558 | 209 | } |
2559 | 14 | } |
2560 | | static void LoadChapterApple( demux_t *p_demux, mp4_track_t *tk ) |
2561 | 0 | { |
2562 | 0 | demux_sys_t *p_sys = p_demux->p_sys; |
2563 | |
|
2564 | 0 | for( tk->i_sample = 0; tk->i_sample < tk->i_sample_count; tk->i_sample++ ) |
2565 | 0 | { |
2566 | 0 | if( tk->i_sample >= tk->chunk[tk->i_chunk].i_sample_first + |
2567 | 0 | tk->chunk[tk->i_chunk].i_sample_count ) |
2568 | 0 | tk->i_chunk++; |
2569 | 0 | TrackUpdateSampleAndTimes( tk ); |
2570 | |
|
2571 | 0 | vlc_tick_t i_pts; |
2572 | 0 | MP4_TrackGetDTSPTS( p_demux, tk, &i_pts ); |
2573 | 0 | uint32_t i_nb_samples = 0; |
2574 | 0 | const uint32_t i_size = MP4_TrackGetReadSize( tk, &i_nb_samples ); |
2575 | |
|
2576 | 0 | if( i_nb_samples > 1 ) |
2577 | 0 | break; /* should not happen */ |
2578 | | |
2579 | 0 | if( i_size > 0 && !vlc_stream_Seek( p_demux->s, MP4_TrackGetPos( tk ) ) ) |
2580 | 0 | { |
2581 | 0 | char p_buffer[256]; |
2582 | 0 | const uint32_t i_read = stream_ReadU32( p_demux->s, p_buffer, |
2583 | 0 | __MIN( sizeof(p_buffer), i_size ) ); |
2584 | 0 | if( i_read > 2 ) |
2585 | 0 | { |
2586 | 0 | const uint32_t i_string = __MIN( GetWBE(p_buffer), i_read-2 ); |
2587 | 0 | const char *psnz_string = &p_buffer[2]; |
2588 | |
|
2589 | 0 | seekpoint_t *s = vlc_seekpoint_New(); |
2590 | 0 | if( s == NULL ) continue; |
2591 | | |
2592 | 0 | if( i_string > 1 && !memcmp( psnz_string, "\xFF\xFE", 2 ) ) |
2593 | 0 | s->psz_name = FromCharset( "UTF-16LE", psnz_string, i_string ); |
2594 | 0 | else |
2595 | 0 | s->psz_name = strndup( psnz_string, i_string ); |
2596 | |
|
2597 | 0 | if( s->psz_name == NULL ) |
2598 | 0 | { |
2599 | 0 | vlc_seekpoint_Delete( s ); |
2600 | 0 | continue; |
2601 | 0 | } |
2602 | | |
2603 | 0 | EnsureUTF8( s->psz_name ); |
2604 | 0 | s->i_time_offset = i_pts; |
2605 | |
|
2606 | 0 | if( !p_sys->p_title ) |
2607 | 0 | p_sys->p_title = vlc_input_title_New(); |
2608 | 0 | TAB_APPEND( p_sys->p_title->i_seekpoint, p_sys->p_title->seekpoint, s ); |
2609 | 0 | } |
2610 | 0 | } |
2611 | 0 | } |
2612 | 0 | } |
2613 | | static void LoadChapter( demux_t *p_demux ) |
2614 | 4.47k | { |
2615 | 4.47k | demux_sys_t *p_sys = p_demux->p_sys; |
2616 | 4.47k | MP4_Box_t *p_chpl; |
2617 | 4.47k | MP4_Box_t *p_hmmt; |
2618 | | |
2619 | 4.47k | if( ( p_chpl = MP4_BoxGet( p_sys->p_root, "/moov/udta/chpl" ) ) && |
2620 | 29 | BOXDATA(p_chpl) && BOXDATA(p_chpl)->i_chapter > 0 ) |
2621 | 28 | { |
2622 | 28 | LoadChapterGpac( p_demux, p_chpl ); |
2623 | 28 | } |
2624 | 4.44k | else if( ( p_hmmt = MP4_BoxGet( p_sys->p_root, "/moov/udta/HMMT" ) ) && |
2625 | 15 | BOXDATA(p_hmmt) && BOXDATA(p_hmmt)->pi_chapter_start && BOXDATA(p_hmmt)->i_chapter_count > 0 ) |
2626 | 14 | { |
2627 | 14 | LoadChapterGoPro( p_demux, p_hmmt ); |
2628 | 14 | } |
2629 | 4.42k | else |
2630 | 4.42k | { |
2631 | | /* Load the first subtitle track like quicktime */ |
2632 | 10.3k | for( unsigned i = 0; i < p_sys->i_tracks; i++ ) |
2633 | 5.87k | { |
2634 | 5.87k | mp4_track_t *tk = &p_sys->track[i]; |
2635 | 5.87k | if ( tk->b_ok && (tk->i_use_flags & USEAS_CHAPTERS) && tk->fmt.i_cat == SPU_ES ) |
2636 | 0 | { |
2637 | 0 | LoadChapterApple( p_demux, tk ); |
2638 | 0 | break; |
2639 | 0 | } |
2640 | 5.87k | } |
2641 | 4.42k | } |
2642 | | |
2643 | | /* Add duration if titles are enabled */ |
2644 | 4.47k | if( p_sys->p_title ) |
2645 | 42 | { |
2646 | 42 | const uint64_t i_duration = __MAX(p_sys->i_duration, p_sys->i_cumulated_duration); |
2647 | 42 | p_sys->p_title->i_length = |
2648 | 42 | MP4_rescale_mtime( i_duration, p_sys->i_timescale ); |
2649 | 42 | } |
2650 | 4.47k | } |
2651 | | |
2652 | | /* now create basic chunk data, the rest will be filled by MP4_CreateSamplesIndex */ |
2653 | | static int TrackCreateChunksIndex( demux_t *p_demux, |
2654 | | mp4_track_t *p_demux_track ) |
2655 | 4.31k | { |
2656 | 4.31k | MP4_Box_t *p_co64; /* give offset for each chunk, same for stco and co64 */ |
2657 | 4.31k | MP4_Box_t *p_stsc; |
2658 | | |
2659 | 4.31k | unsigned int i_chunk; |
2660 | 4.31k | unsigned int i_index, i_last; |
2661 | | |
2662 | 4.31k | if( ( !(p_co64 = MP4_BoxGet( p_demux_track->p_stbl, "stco" ) )&& |
2663 | 335 | !(p_co64 = MP4_BoxGet( p_demux_track->p_stbl, "co64" ) ) )|| |
2664 | 3.98k | ( !(p_stsc = MP4_BoxGet( p_demux_track->p_stbl, "stsc" ) ) )) |
2665 | 350 | { |
2666 | 350 | return( VLC_EGENERIC ); |
2667 | 350 | } |
2668 | | |
2669 | 3.96k | p_demux_track->i_chunk_count = BOXDATA(p_co64)->i_entry_count; |
2670 | 3.96k | if( !p_demux_track->i_chunk_count ) |
2671 | 957 | { |
2672 | 957 | msg_Warn( p_demux, "no chunk defined" ); |
2673 | 957 | } |
2674 | 3.96k | p_demux_track->chunk = calloc( p_demux_track->i_chunk_count, |
2675 | 3.96k | sizeof( mp4_chunk_t ) ); |
2676 | 3.96k | if( p_demux_track->chunk == NULL ) |
2677 | 0 | { |
2678 | 0 | return VLC_ENOMEM; |
2679 | 0 | } |
2680 | | |
2681 | | /* first we read chunk offset */ |
2682 | 230k | for( i_chunk = 0; i_chunk < p_demux_track->i_chunk_count; i_chunk++ ) |
2683 | 226k | { |
2684 | 226k | mp4_chunk_t *ck = &p_demux_track->chunk[i_chunk]; |
2685 | | |
2686 | 226k | ck->i_offset = BOXDATA(p_co64)->i_chunk_offset[i_chunk]; |
2687 | | |
2688 | 226k | ck->i_first_dts = 0; |
2689 | 226k | ck->i_entries_dts = 0; |
2690 | 226k | ck->p_sample_count_dts = NULL; |
2691 | 226k | ck->p_sample_delta_dts = NULL; |
2692 | 226k | ck->i_entries_pts = 0; |
2693 | 226k | ck->p_sample_count_pts = NULL; |
2694 | 226k | ck->p_sample_offset_pts = NULL; |
2695 | 226k | } |
2696 | | |
2697 | | /* now we read index for SampleEntry( soun vide mp4a mp4v ...) |
2698 | | to be used for the sample XXX begin to 1 |
2699 | | We construct it beginning at the end */ |
2700 | 3.96k | i_last = p_demux_track->i_chunk_count; /* last chunk proceded */ |
2701 | 3.96k | i_index = BOXDATA(p_stsc)->i_entry_count; |
2702 | | |
2703 | 13.1k | while( i_index-- > 0 ) |
2704 | 9.22k | { |
2705 | 9.22k | for( i_chunk = BOXDATA(p_stsc)->i_first_chunk[i_index] - 1; |
2706 | 230k | i_chunk < i_last; i_chunk++ ) |
2707 | 221k | { |
2708 | 221k | if( i_chunk >= p_demux_track->i_chunk_count ) |
2709 | 18 | { |
2710 | 18 | msg_Warn( p_demux, "corrupted chunk table" ); |
2711 | 18 | return VLC_EGENERIC; |
2712 | 18 | } |
2713 | | |
2714 | 221k | p_demux_track->chunk[i_chunk].i_sample_description_index = |
2715 | 221k | BOXDATA(p_stsc)->i_sample_description_index[i_index]; |
2716 | 221k | p_demux_track->chunk[i_chunk].i_sample_count = |
2717 | 221k | BOXDATA(p_stsc)->i_samples_per_chunk[i_index]; |
2718 | 221k | } |
2719 | 9.21k | i_last = BOXDATA(p_stsc)->i_first_chunk[i_index] - 1; |
2720 | 9.21k | } |
2721 | | |
2722 | 3.94k | p_demux_track->i_sample_count = 0; |
2723 | 3.94k | bool b_broken = false; |
2724 | 3.94k | if ( p_demux_track->i_chunk_count ) |
2725 | 2.99k | { |
2726 | 2.99k | p_demux_track->chunk[0].i_sample_first = 0; |
2727 | 2.99k | p_demux_track->i_sample_count += p_demux_track->chunk[0].i_sample_count; |
2728 | | |
2729 | 2.99k | const mp4_chunk_t *prev = &p_demux_track->chunk[0]; |
2730 | 222k | for( i_chunk = 1; i_chunk < p_demux_track->i_chunk_count; i_chunk++ ) |
2731 | 219k | { |
2732 | 219k | mp4_chunk_t *cur = &p_demux_track->chunk[i_chunk]; |
2733 | 219k | if( unlikely(UINT32_MAX - cur->i_sample_count < p_demux_track->i_sample_count) ) |
2734 | 3 | { |
2735 | 3 | b_broken = true; |
2736 | 3 | break; |
2737 | 3 | } |
2738 | 219k | p_demux_track->i_sample_count += cur->i_sample_count; |
2739 | 219k | cur->i_sample_first = prev->i_sample_first + prev->i_sample_count; |
2740 | 219k | prev = cur; |
2741 | 219k | } |
2742 | 2.99k | } |
2743 | | |
2744 | 3.94k | if( unlikely(b_broken) ) |
2745 | 3 | { |
2746 | 3 | msg_Err( p_demux, "Overflow in chunks total samples count" ); |
2747 | 3 | return VLC_EGENERIC; |
2748 | 3 | } |
2749 | | |
2750 | 3.94k | msg_Dbg( p_demux, "track[Id 0x%x] read %d chunk", |
2751 | 3.94k | p_demux_track->i_track_ID, p_demux_track->i_chunk_count ); |
2752 | | |
2753 | 3.94k | return VLC_SUCCESS; |
2754 | 3.94k | } |
2755 | | |
2756 | | static int xTTS_CountEntries( demux_t *p_demux, uint32_t *pi_entry /* out */, |
2757 | | const uint32_t i_index, |
2758 | | uint32_t i_index_samples_left, |
2759 | | uint32_t i_sample_count, |
2760 | | const uint32_t *pi_index_sample_count, |
2761 | | const uint32_t i_table_count ) |
2762 | 327k | { |
2763 | 327k | uint32_t i_array_offset; |
2764 | 372k | while( i_sample_count > 0 ) |
2765 | 331k | { |
2766 | 331k | if ( likely((UINT32_MAX - i_index) >= *pi_entry) ) |
2767 | 331k | i_array_offset = i_index + *pi_entry; |
2768 | 0 | else |
2769 | 0 | return VLC_EGENERIC; |
2770 | | |
2771 | 331k | if ( i_array_offset >= i_table_count ) |
2772 | 14.6k | { |
2773 | 14.6k | msg_Err( p_demux, "invalid index counting total samples %u %u", i_array_offset, i_table_count ); |
2774 | 14.6k | return VLC_EINVAL; |
2775 | 14.6k | } |
2776 | | |
2777 | 316k | if ( i_index_samples_left ) |
2778 | 272k | { |
2779 | 272k | if ( i_index_samples_left > i_sample_count ) |
2780 | 270k | { |
2781 | 270k | i_index_samples_left -= i_sample_count; |
2782 | 270k | i_sample_count = 0; |
2783 | 270k | *pi_entry +=1; /* No samples left, go copy */ |
2784 | 270k | break; |
2785 | 270k | } |
2786 | 1.46k | else |
2787 | 1.46k | { |
2788 | 1.46k | i_sample_count -= i_index_samples_left; |
2789 | 1.46k | i_index_samples_left = 0; |
2790 | 1.46k | *pi_entry += 1; |
2791 | 1.46k | continue; |
2792 | 1.46k | } |
2793 | 272k | } |
2794 | 43.9k | else |
2795 | 43.9k | { |
2796 | 43.9k | i_sample_count -= __MIN( i_sample_count, pi_index_sample_count[i_array_offset] ); |
2797 | 43.9k | *pi_entry += 1; |
2798 | 43.9k | } |
2799 | 316k | } |
2800 | | |
2801 | 312k | return VLC_SUCCESS; |
2802 | 327k | } |
2803 | | |
2804 | | static int TrackCreateSamplesIndex( demux_t *p_demux, |
2805 | | mp4_track_t *p_demux_track ) |
2806 | 3.94k | { |
2807 | 3.94k | MP4_Box_t *p_box; |
2808 | 3.94k | MP4_Box_data_stsz_t *stsz; |
2809 | | /* TODO use also stss and stsh table for seeking */ |
2810 | | /* FIXME use edit table */ |
2811 | | |
2812 | | /* Find stsz or stz2 |
2813 | | * Gives the sample size for each samples. */ |
2814 | 3.94k | p_box = MP4_BoxGet( p_demux_track->p_stbl, "stsz" ); |
2815 | 3.94k | if( !p_box ) |
2816 | 10 | p_box = MP4_BoxGet( p_demux_track->p_stbl, "stz2" ); |
2817 | 3.94k | if( !p_box ) |
2818 | 10 | { |
2819 | 10 | msg_Warn( p_demux, "cannot find STSZ or STZ2 box" ); |
2820 | 10 | return VLC_EGENERIC; |
2821 | 10 | } |
2822 | 3.93k | stsz = p_box->data.p_stsz; |
2823 | | |
2824 | | /* Use stsz table to create a sample number -> sample size table */ |
2825 | 3.93k | if( p_demux_track->i_sample_count != stsz->i_sample_count ) |
2826 | 1.83k | { |
2827 | 1.83k | msg_Warn( p_demux, "Incorrect total samples stsc %" PRIu32 " <> stsz %"PRIu32 ", " |
2828 | 1.83k | " expect truncated media playback", |
2829 | 1.83k | p_demux_track->i_sample_count, stsz->i_sample_count ); |
2830 | 1.83k | p_demux_track->i_sample_count = __MIN(p_demux_track->i_sample_count, stsz->i_sample_count); |
2831 | 1.83k | } |
2832 | | |
2833 | 3.93k | if( stsz->i_sample_size ) |
2834 | 2.38k | { |
2835 | | /* 1: all sample have the same size, so no need to construct a table */ |
2836 | 2.38k | p_demux_track->i_sample_size = stsz->i_sample_size; |
2837 | 2.38k | p_demux_track->p_sample_size = NULL; |
2838 | 2.38k | } |
2839 | 1.54k | else |
2840 | 1.54k | { |
2841 | | /* 2: each sample can have a different size */ |
2842 | 1.54k | p_demux_track->i_sample_size = 0; |
2843 | 1.54k | p_demux_track->p_sample_size = |
2844 | 1.54k | calloc( p_demux_track->i_sample_count, sizeof( uint32_t ) ); |
2845 | 1.54k | if( p_demux_track->p_sample_size == NULL ) |
2846 | 0 | return VLC_ENOMEM; |
2847 | | |
2848 | 428k | for( uint32_t i_sample = 0; i_sample < p_demux_track->i_sample_count; i_sample++ ) |
2849 | 427k | { |
2850 | 427k | p_demux_track->p_sample_size[i_sample] = |
2851 | 427k | stsz->i_entry_size[i_sample]; |
2852 | 427k | } |
2853 | 1.54k | } |
2854 | | |
2855 | 3.93k | if ( p_demux_track->i_chunk_count && p_demux_track->i_sample_size == 0 ) |
2856 | 772 | { |
2857 | 772 | const mp4_chunk_t *lastchunk = &p_demux_track->chunk[p_demux_track->i_chunk_count - 1]; |
2858 | 772 | if( (uint64_t)lastchunk->i_sample_count + p_demux_track->i_chunk_count - 1 > stsz->i_sample_count ) |
2859 | 3 | { |
2860 | 3 | msg_Err( p_demux, "invalid samples table: stsz table is too small" ); |
2861 | 3 | return VLC_EGENERIC; |
2862 | 3 | } |
2863 | 772 | } |
2864 | | |
2865 | | /* Use stts table to create a sample number -> dts table. |
2866 | | * XXX: if we don't want to waste too much memory, we can't expand |
2867 | | * the box! so each chunk will contain an "extract" of this table |
2868 | | * for fast research (problem with raw stream where a sample is sometime |
2869 | | * just channels*bits_per_sample/8 */ |
2870 | | |
2871 | | /* FIXME: refactor STTS & CTTS, STTS having now only few extra lines and |
2872 | | * differing in 2/2 fields and 1 signedness */ |
2873 | | |
2874 | 3.93k | int64_t i_next_dts = 0; |
2875 | | /* Find stts |
2876 | | * Gives mapping between sample and decoding time |
2877 | | */ |
2878 | 3.93k | p_box = MP4_BoxGet( p_demux_track->p_stbl, "stts" ); |
2879 | 3.93k | if( !p_box ) |
2880 | 33 | { |
2881 | 33 | msg_Warn( p_demux, "cannot find STTS box" ); |
2882 | 33 | return VLC_EGENERIC; |
2883 | 33 | } |
2884 | 3.89k | else |
2885 | 3.89k | { |
2886 | 3.89k | MP4_Box_data_stts_t *stts = p_box->data.p_stts; |
2887 | | |
2888 | 3.89k | msg_Warn( p_demux, "STTS table of %"PRIu32" entries", stts->i_entry_count ); |
2889 | | |
2890 | | /* Create sample -> dts table per chunk */ |
2891 | 3.89k | uint32_t i_index = 0; |
2892 | 3.89k | uint32_t i_current_index_samples_left = 0; |
2893 | | |
2894 | 223k | for( uint32_t i_chunk = 0; i_chunk < p_demux_track->i_chunk_count; i_chunk++ ) |
2895 | 219k | { |
2896 | 219k | mp4_chunk_t *ck = &p_demux_track->chunk[i_chunk]; |
2897 | 219k | uint32_t i_sample_count; |
2898 | | |
2899 | | /* save first dts */ |
2900 | 219k | ck->i_first_dts = i_next_dts; |
2901 | | |
2902 | | /* count how many entries are needed for this chunk |
2903 | | * for p_sample_delta_dts and p_sample_count_dts */ |
2904 | 219k | ck->i_entries_dts = 0; |
2905 | | |
2906 | 219k | int i_ret = xTTS_CountEntries( p_demux, &ck->i_entries_dts, i_index, |
2907 | 219k | i_current_index_samples_left, |
2908 | 219k | ck->i_sample_count, |
2909 | 219k | stts->pi_sample_count, |
2910 | 219k | stts->i_entry_count ); |
2911 | 219k | if ( i_ret == VLC_EGENERIC ) |
2912 | 0 | return i_ret; |
2913 | | |
2914 | | /* allocate them */ |
2915 | 219k | i_ret = MP4_ChunkAllocEntries( ck->i_entries_dts, |
2916 | 219k | ck->small_dts_buf, |
2917 | 219k | &ck->p_sample_count_dts, |
2918 | 219k | &ck->p_sample_delta_dts ); |
2919 | 219k | if( i_ret ) |
2920 | 0 | { |
2921 | 0 | msg_Err( p_demux, "can't allocate memory for i_entry=%"PRIu32, ck->i_entries_dts ); |
2922 | 0 | ck->i_entries_dts = 0; |
2923 | 0 | return i_ret; |
2924 | 0 | } |
2925 | | |
2926 | | /* now copy */ |
2927 | 219k | i_sample_count = ck->i_sample_count; |
2928 | | |
2929 | 226k | for( uint32_t i = 0; i < ck->i_entries_dts; i++ ) |
2930 | 204k | { |
2931 | 204k | if ( i_current_index_samples_left ) |
2932 | 197k | { |
2933 | 197k | if ( i_current_index_samples_left > i_sample_count ) |
2934 | 196k | { |
2935 | 196k | ck->p_sample_count_dts[i] = i_sample_count; |
2936 | 196k | ck->p_sample_delta_dts[i] = stts->pi_sample_delta[i_index]; |
2937 | 196k | i_next_dts += (int64_t)ck->p_sample_count_dts[i] * stts->pi_sample_delta[i_index]; |
2938 | 196k | if ( i_sample_count ) ck->i_duration = i_next_dts - ck->i_first_dts; |
2939 | 196k | i_current_index_samples_left -= i_sample_count; |
2940 | 196k | i_sample_count = 0; |
2941 | 196k | assert( i == ck->i_entries_dts - 1 ); |
2942 | 196k | break; |
2943 | 196k | } |
2944 | 1.13k | else |
2945 | 1.13k | { |
2946 | 1.13k | ck->p_sample_count_dts[i] = i_current_index_samples_left; |
2947 | 1.13k | ck->p_sample_delta_dts[i] = stts->pi_sample_delta[i_index]; |
2948 | 1.13k | i_next_dts += (int64_t)ck->p_sample_count_dts[i] * stts->pi_sample_delta[i_index]; |
2949 | 1.13k | if ( i_current_index_samples_left ) ck->i_duration = i_next_dts - ck->i_first_dts; |
2950 | 1.13k | i_sample_count -= i_current_index_samples_left; |
2951 | 1.13k | i_current_index_samples_left = 0; |
2952 | 1.13k | i_index++; |
2953 | 1.13k | } |
2954 | 197k | } |
2955 | 6.50k | else |
2956 | 6.50k | { |
2957 | 6.50k | if ( stts->pi_sample_count[i_index] > i_sample_count ) |
2958 | 1.83k | { |
2959 | 1.83k | ck->p_sample_count_dts[i] = i_sample_count; |
2960 | 1.83k | ck->p_sample_delta_dts[i] = stts->pi_sample_delta[i_index]; |
2961 | 1.83k | i_next_dts += (int64_t)ck->p_sample_count_dts[i] * stts->pi_sample_delta[i_index]; |
2962 | 1.83k | if ( i_sample_count ) ck->i_duration = i_next_dts - ck->i_first_dts; |
2963 | 1.83k | i_current_index_samples_left = stts->pi_sample_count[i_index] - i_sample_count; |
2964 | 1.83k | i_sample_count = 0; |
2965 | 1.83k | assert( i == ck->i_entries_dts - 1 ); |
2966 | | // keep building from same index |
2967 | 1.83k | } |
2968 | 4.67k | else |
2969 | 4.67k | { |
2970 | 4.67k | ck->p_sample_count_dts[i] = stts->pi_sample_count[i_index]; |
2971 | 4.67k | ck->p_sample_delta_dts[i] = stts->pi_sample_delta[i_index]; |
2972 | 4.67k | i_next_dts += (int64_t)ck->p_sample_count_dts[i] * stts->pi_sample_delta[i_index]; |
2973 | 4.67k | if ( stts->pi_sample_count[i_index] ) ck->i_duration = i_next_dts - ck->i_first_dts; |
2974 | 4.67k | i_sample_count -= stts->pi_sample_count[i_index]; |
2975 | 4.67k | i_index++; |
2976 | 4.67k | } |
2977 | 6.50k | } |
2978 | | |
2979 | 204k | } |
2980 | 219k | } |
2981 | 3.89k | } |
2982 | | |
2983 | | |
2984 | | /* Find ctts |
2985 | | * Gives the delta between decoding time (dts) and composition table (pts) |
2986 | | */ |
2987 | 3.89k | p_box = MP4_BoxGet( p_demux_track->p_stbl, "ctts" ); |
2988 | 3.89k | if( p_box && p_box->data.p_ctts ) |
2989 | 784 | { |
2990 | 784 | MP4_Box_data_ctts_t *ctts = p_box->data.p_ctts; |
2991 | | |
2992 | 784 | msg_Warn( p_demux, "CTTS table of %"PRIu32" entries", ctts->i_entry_count ); |
2993 | | |
2994 | 784 | int64_t i_cts_shift = 0; |
2995 | 784 | const MP4_Box_t *p_cslg = MP4_BoxGet( p_demux_track->p_stbl, "cslg" ); |
2996 | 784 | if( p_cslg && BOXDATA(p_cslg) ) |
2997 | 285 | { |
2998 | 285 | i_cts_shift = BOXDATA(p_cslg)->ct_to_dts_shift; |
2999 | 285 | } |
3000 | 499 | else if( ctts->i_entry_count ) /* Compute for Quicktime */ |
3001 | 141 | { |
3002 | 120k | for( uint32_t i = 0; i < ctts->i_entry_count; i++ ) |
3003 | 119k | { |
3004 | 119k | if( ctts->pi_sample_offset[i] < 0 && ctts->pi_sample_offset[i] < -i_cts_shift ) |
3005 | 312 | i_cts_shift = -ctts->pi_sample_offset[i]; |
3006 | 119k | } |
3007 | 141 | } |
3008 | 784 | p_demux_track->i_cts_shift = i_cts_shift; |
3009 | | |
3010 | | /* Create pts-dts table per chunk */ |
3011 | 784 | uint32_t i_index = 0; |
3012 | 784 | uint32_t i_current_index_samples_left = 0; |
3013 | | |
3014 | 108k | for( uint32_t i_chunk = 0; i_chunk < p_demux_track->i_chunk_count; i_chunk++ ) |
3015 | 108k | { |
3016 | 108k | mp4_chunk_t *ck = &p_demux_track->chunk[i_chunk]; |
3017 | 108k | uint32_t i_sample_count; |
3018 | | |
3019 | | /* count how many entries are needed for this chunk |
3020 | | * for p_sample_offset_pts and p_sample_count_pts */ |
3021 | 108k | ck->i_entries_pts = 0; |
3022 | 108k | int i_ret = xTTS_CountEntries( p_demux, &ck->i_entries_pts, i_index, |
3023 | 108k | i_current_index_samples_left, |
3024 | 108k | ck->i_sample_count, |
3025 | 108k | ctts->pi_sample_count, |
3026 | 108k | ctts->i_entry_count ); |
3027 | 108k | if ( i_ret == VLC_EGENERIC ) |
3028 | 0 | return i_ret; |
3029 | | |
3030 | | /* allocate them */ |
3031 | 108k | i_ret = MP4_ChunkAllocEntries( ck->i_entries_pts, |
3032 | 108k | ck->small_pts_buf, |
3033 | 108k | &ck->p_sample_count_pts, |
3034 | 108k | &ck->p_sample_offset_pts ); |
3035 | 108k | if( i_ret ) |
3036 | 0 | { |
3037 | 0 | msg_Err( p_demux, "can't allocate memory for i_entry=%"PRIu32, ck->i_entries_pts ); |
3038 | 0 | ck->i_entries_pts = 0; |
3039 | 0 | return i_ret; |
3040 | 0 | } |
3041 | | |
3042 | | /* now copy */ |
3043 | 108k | i_sample_count = ck->i_sample_count; |
3044 | | |
3045 | 145k | for( uint32_t i = 0; i < ck->i_entries_pts; i++ ) |
3046 | 112k | { |
3047 | 112k | int64_t i_ctsdelta = ctts->pi_sample_offset[i_index] + i_cts_shift; |
3048 | 112k | if( i_ctsdelta < 0 ) /* should not */ |
3049 | 623 | i_ctsdelta = 0; |
3050 | 112k | ck->p_sample_offset_pts[i] = i_ctsdelta; |
3051 | 112k | if ( i_current_index_samples_left ) |
3052 | 74.8k | { |
3053 | 74.8k | if ( i_current_index_samples_left > i_sample_count ) |
3054 | 74.5k | { |
3055 | 74.5k | ck->p_sample_count_pts[i] = i_sample_count; |
3056 | 74.5k | i_current_index_samples_left -= i_sample_count; |
3057 | 74.5k | i_sample_count = 0; |
3058 | 74.5k | assert( i == ck->i_entries_pts - 1 ); |
3059 | 74.5k | break; |
3060 | 74.5k | } |
3061 | 333 | else |
3062 | 333 | { |
3063 | 333 | ck->p_sample_count_pts[i] = i_current_index_samples_left; |
3064 | 333 | i_sample_count -= i_current_index_samples_left; |
3065 | 333 | i_current_index_samples_left = 0; |
3066 | 333 | i_index++; |
3067 | 333 | } |
3068 | 74.8k | } |
3069 | 37.4k | else |
3070 | 37.4k | { |
3071 | 37.4k | if ( ctts->pi_sample_count[i_index] > i_sample_count ) |
3072 | 492 | { |
3073 | 492 | ck->p_sample_count_pts[i] = i_sample_count; |
3074 | 492 | i_current_index_samples_left = ctts->pi_sample_count[i_index] - i_sample_count; |
3075 | 492 | i_sample_count = 0; |
3076 | 492 | assert( i == ck->i_entries_pts - 1 ); |
3077 | | // keep building from same index |
3078 | 492 | } |
3079 | 36.9k | else |
3080 | 36.9k | { |
3081 | 36.9k | ck->p_sample_count_pts[i] = ctts->pi_sample_count[i_index]; |
3082 | 36.9k | i_sample_count -= ctts->pi_sample_count[i_index]; |
3083 | 36.9k | i_index++; |
3084 | 36.9k | } |
3085 | 37.4k | } |
3086 | 112k | } |
3087 | 108k | } |
3088 | 784 | } |
3089 | | |
3090 | 3.89k | msg_Dbg( p_demux, "track[Id 0x%x] read %"PRIu32" samples length:%"PRId64"s", |
3091 | 3.89k | p_demux_track->i_track_ID, p_demux_track->i_sample_count, |
3092 | 3.89k | i_next_dts / p_demux_track->i_timescale ); |
3093 | | |
3094 | 3.89k | return VLC_SUCCESS; |
3095 | 3.89k | } |
3096 | | |
3097 | | |
3098 | | /** |
3099 | | * It computes the sample rate for a video track using the given sample |
3100 | | * description index |
3101 | | */ |
3102 | | static void TrackGetESSampleRate( demux_t *p_demux, |
3103 | | unsigned *pi_num, unsigned *pi_den, |
3104 | | const mp4_track_t *p_track, |
3105 | | unsigned i_sd_index, |
3106 | | unsigned i_chunk ) |
3107 | 1.69k | { |
3108 | 1.69k | demux_sys_t *p_sys = p_demux->p_sys; |
3109 | 1.69k | *pi_num = 0; |
3110 | 1.69k | *pi_den = 0; |
3111 | | |
3112 | 1.69k | MP4_Box_t *p_trak = MP4_GetTrakByTrackID( MP4_BoxGet( p_sys->p_root, |
3113 | 1.69k | "/moov" ), |
3114 | 1.69k | p_track->i_track_ID ); |
3115 | 1.69k | MP4_Box_t *p_mdhd = MP4_BoxGet( p_trak, "mdia/mdhd" ); |
3116 | 1.69k | if ( p_mdhd && BOXDATA(p_mdhd) ) |
3117 | 1.69k | { |
3118 | 1.69k | vlc_ureduce( pi_num, pi_den, |
3119 | 1.69k | (uint64_t) BOXDATA(p_mdhd)->i_timescale * p_track->i_sample_count, |
3120 | 1.69k | (uint64_t) BOXDATA(p_mdhd)->i_duration, |
3121 | 1.69k | UINT16_MAX ); |
3122 | 1.69k | return; |
3123 | 1.69k | } |
3124 | | |
3125 | 2 | if( p_track->i_chunk_count == 0 ) |
3126 | 1 | return; |
3127 | | |
3128 | | /* */ |
3129 | 1 | const mp4_chunk_t *p_chunk = &p_track->chunk[i_chunk]; |
3130 | 1 | while( p_chunk > &p_track->chunk[0] && |
3131 | 0 | p_chunk[-1].i_sample_description_index == i_sd_index ) |
3132 | 0 | { |
3133 | 0 | p_chunk--; |
3134 | 0 | } |
3135 | | |
3136 | 1 | uint64_t i_sample = 0; |
3137 | 1 | uint64_t i_total_duration = 0; |
3138 | 1 | do |
3139 | 1 | { |
3140 | 1 | i_sample += p_chunk->i_sample_count; |
3141 | 1 | i_total_duration += p_chunk->i_duration; |
3142 | 1 | p_chunk++; |
3143 | 1 | } |
3144 | 1 | while( p_chunk < &p_track->chunk[p_track->i_chunk_count] && |
3145 | 1 | p_chunk->i_sample_description_index == i_sd_index ); |
3146 | | |
3147 | 1 | if( i_sample > 0 && i_total_duration ) |
3148 | 1 | vlc_ureduce( pi_num, pi_den, |
3149 | 1 | i_sample * p_track->i_timescale, |
3150 | 1 | i_total_duration, |
3151 | 1 | UINT16_MAX); |
3152 | 1 | } |
3153 | | |
3154 | | static void TrackConfigInit( track_config_t *p_cfg ) |
3155 | 4.39k | { |
3156 | 4.39k | memset( p_cfg, 0, sizeof(*p_cfg) ); |
3157 | 4.39k | } |
3158 | | |
3159 | | static void TrackConfigApply( const track_config_t *p_cfg, |
3160 | | mp4_track_t *p_track ) |
3161 | 4.34k | { |
3162 | 4.34k | if( p_cfg->i_timescale_override ) |
3163 | 7 | p_track->i_timescale = p_cfg->i_timescale_override; |
3164 | 4.34k | if( p_cfg->i_sample_size_override ) |
3165 | 0 | p_track->i_sample_size = p_cfg->i_sample_size_override; |
3166 | 4.34k | p_track->p_asf = p_cfg->p_asf; |
3167 | 4.34k | memcpy( p_track->rgi_chans_reordering, p_cfg->rgi_chans_reordering, |
3168 | 4.34k | AOUT_CHAN_MAX * sizeof(p_cfg->rgi_chans_reordering[0]) ); |
3169 | 4.34k | p_track->i_chans_to_reorder = p_cfg->i_chans_to_reorder; |
3170 | 4.34k | p_track->b_forced_spu = p_cfg->b_forced_spu; |
3171 | 4.34k | p_track->i_block_flags = p_cfg->i_block_flags; |
3172 | 4.34k | p_track->b_ignore_implicit_pts = p_cfg->b_ignore_implicit_pts; |
3173 | 4.34k | } |
3174 | | |
3175 | | static int TrackFillConfig( demux_t *p_demux, const mp4_track_t *p_track, |
3176 | | const MP4_Box_t *p_sample,unsigned i_chunk, |
3177 | | es_format_t *p_fmt, track_config_t *p_cfg ) |
3178 | 4.39k | { |
3179 | | /* */ |
3180 | 4.39k | switch( p_track->fmt.i_cat ) |
3181 | 4.39k | { |
3182 | 1.69k | case VIDEO_ES: |
3183 | 1.69k | if ( p_sample->i_handler != ATOM_vide || |
3184 | 1.69k | !SetupVideoES( p_demux, p_track, p_sample, p_fmt, p_cfg ) ) |
3185 | 0 | return VLC_EGENERIC; |
3186 | | |
3187 | | /* Set frame rate */ |
3188 | 1.69k | TrackGetESSampleRate( p_demux, |
3189 | 1.69k | &p_fmt->video.i_frame_rate, |
3190 | 1.69k | &p_fmt->video.i_frame_rate_base, |
3191 | 1.69k | p_track, p_sample->i_index, i_chunk ); |
3192 | 1.69k | break; |
3193 | | |
3194 | 2.02k | case AUDIO_ES: |
3195 | 2.02k | if ( p_sample->i_handler != ATOM_soun || |
3196 | 2.02k | !SetupAudioES( p_demux, p_track, p_sample, p_fmt, p_cfg ) ) |
3197 | 0 | return VLC_EGENERIC; |
3198 | 2.02k | break; |
3199 | | |
3200 | 2.02k | case SPU_ES: |
3201 | 619 | if ( ( p_sample->i_handler != ATOM_text && |
3202 | 619 | p_sample->i_handler != ATOM_subt && |
3203 | 619 | p_sample->i_handler != ATOM_sbtl && |
3204 | 1 | p_sample->i_handler != ATOM_clcp ) || |
3205 | 618 | !SetupSpuES( p_demux, p_track, p_sample, p_fmt, p_cfg ) ) |
3206 | 1 | return VLC_EGENERIC; |
3207 | 618 | break; |
3208 | | |
3209 | 618 | default: |
3210 | 62 | break; |
3211 | 4.39k | } |
3212 | | |
3213 | 4.39k | return VLC_SUCCESS; |
3214 | 4.39k | } |
3215 | | |
3216 | | /* |
3217 | | * TrackCreateES: |
3218 | | * Create ES and PES to init decoder if needed, for a track starting at i_chunk |
3219 | | */ |
3220 | | static int TrackCreateES( demux_t *p_demux, mp4_track_t *p_track, |
3221 | | unsigned int i_chunk, es_out_id_t **pp_es ) |
3222 | 3.89k | { |
3223 | 3.89k | demux_sys_t *p_sys = p_demux->p_sys; |
3224 | 3.89k | unsigned int i_sample_description_index; |
3225 | | |
3226 | 3.89k | if( p_sys->b_fragmented || p_track->i_chunk_count == 0 ) |
3227 | 931 | i_sample_description_index = 1; /* XXX */ |
3228 | 2.96k | else |
3229 | 2.96k | i_sample_description_index = |
3230 | 2.96k | p_track->chunk[i_chunk].i_sample_description_index; |
3231 | | |
3232 | 3.89k | if( pp_es ) |
3233 | 3.88k | *pp_es = NULL; |
3234 | | |
3235 | 3.89k | if( !i_sample_description_index ) |
3236 | 28 | { |
3237 | 28 | msg_Warn( p_demux, "invalid SampleEntry index (track[Id 0x%x])", |
3238 | 28 | p_track->i_track_ID ); |
3239 | 28 | return VLC_EGENERIC; |
3240 | 28 | } |
3241 | | |
3242 | 3.87k | const MP4_Box_t *p_sample = MP4_BoxGetVa( p_track->p_stsd, "[%u]", |
3243 | 3.87k | i_sample_description_index - 1 ); |
3244 | 3.87k | if( !p_sample || |
3245 | 3.83k | ( !p_sample->data.p_payload && p_track->fmt.i_cat != SPU_ES ) ) |
3246 | 35 | { |
3247 | 35 | msg_Warn( p_demux, "cannot find SampleEntry (track[Id 0x%x])", |
3248 | 35 | p_track->i_track_ID ); |
3249 | 35 | return VLC_EGENERIC; |
3250 | 35 | } |
3251 | | |
3252 | 3.83k | track_config_t cfg; |
3253 | 3.83k | TrackConfigInit( &cfg ); |
3254 | 3.83k | es_format_t *p_fmt = &p_track->fmt; |
3255 | | |
3256 | 3.83k | p_track->fmt.i_id = p_track->i_track_ID; |
3257 | | |
3258 | 3.83k | if( TrackFillConfig( p_demux, p_track, p_sample, i_chunk, |
3259 | 3.83k | p_fmt, &cfg ) != VLC_SUCCESS ) |
3260 | 1 | { |
3261 | 1 | return VLC_EGENERIC; |
3262 | 1 | } |
3263 | | |
3264 | 3.83k | TrackConfigApply( &cfg, p_track ); |
3265 | | |
3266 | 3.83k | switch( p_fmt->i_cat ) |
3267 | 3.83k | { |
3268 | 1.17k | case VIDEO_ES: |
3269 | 1.17k | p_sys->f_fps = (float)p_fmt->video.i_frame_rate / |
3270 | 1.17k | (float)p_fmt->video.i_frame_rate_base; |
3271 | 1.17k | break; |
3272 | 2.02k | case AUDIO_ES: |
3273 | 2.02k | { |
3274 | 2.02k | int i_ret = VLC_EGENERIC; |
3275 | 2.02k | if( p_sys->p_meta ) |
3276 | 2.02k | { |
3277 | 2.02k | i_ret = vlc_replay_gain_CopyFromMeta( &p_fmt->audio_replay_gain, p_sys->p_meta ); |
3278 | 2.02k | } |
3279 | | |
3280 | | /* use replay gain if available; otherwise use sound check */ |
3281 | 2.02k | if( i_ret != VLC_SUCCESS ) |
3282 | 2.02k | { |
3283 | 2.02k | audio_replay_gain_t *p_arg = &p_fmt->audio_replay_gain; |
3284 | 2.02k | replay_gain_Reset( p_arg ); |
3285 | | |
3286 | 2.02k | if( isfinite(p_sys->qt.f_replay_gain_norm) ) |
3287 | 4 | { |
3288 | 4 | p_arg->pb_gain[AUDIO_REPLAY_GAIN_TRACK] = true; |
3289 | 4 | p_arg->pf_gain[AUDIO_REPLAY_GAIN_TRACK] = p_sys->qt.f_replay_gain_norm; |
3290 | 4 | } |
3291 | | |
3292 | 2.02k | if( isfinite(p_sys->qt.f_replay_gain_peak) ) |
3293 | 4 | { |
3294 | 4 | p_arg->pb_peak[AUDIO_REPLAY_GAIN_TRACK] = true; |
3295 | 4 | p_arg->pf_peak[AUDIO_REPLAY_GAIN_TRACK] = p_sys->qt.f_replay_gain_peak; |
3296 | 4 | } |
3297 | | |
3298 | 2.02k | if( p_arg->pb_gain[AUDIO_REPLAY_GAIN_TRACK] ) |
3299 | 4 | { |
3300 | | /* sound check uses -16 LUFS as the reference level */ |
3301 | 4 | p_arg->pb_reference_loudness = true; |
3302 | 4 | p_arg->pf_reference_loudness = -16.f; |
3303 | 4 | } |
3304 | 2.02k | } |
3305 | | |
3306 | 2.02k | if( p_sys->qt.i_target_bitrate ) |
3307 | 3 | { |
3308 | 3 | p_fmt->i_bitrate = p_sys->qt.i_target_bitrate; |
3309 | 3 | } |
3310 | | |
3311 | 2.02k | switch( p_fmt->i_codec ) |
3312 | 2.02k | { |
3313 | | /* When not specified, set max standard decoder delay */ |
3314 | 249 | case VLC_CODEC_MP4A: |
3315 | 250 | case VLC_CODEC_OPUS: |
3316 | 262 | case VLC_CODEC_MP3: |
3317 | 271 | case VLC_CODEC_MP2: |
3318 | 282 | case VLC_CODEC_MPGA: |
3319 | 282 | { |
3320 | 282 | const unsigned i_rate = p_fmt->audio.i_rate ? p_fmt->audio.i_rate : p_track->i_timescale; |
3321 | 282 | const MP4_Box_t *p_elst = p_track->p_elst; |
3322 | 282 | if( p_elst && BOXDATA(p_elst)->i_entry_count && |
3323 | 81 | BOXDATA(p_elst)->entries[0].i_media_time < 0 ) |
3324 | 15 | { |
3325 | 15 | p_track->i_decoder_delay = MP4_rescale( BOXDATA(p_elst)->entries[0].i_segment_duration, |
3326 | 15 | p_sys->i_timescale, |
3327 | 15 | p_track->i_timescale ); |
3328 | 15 | } |
3329 | 267 | else if( p_sys->qt.i_delay_samples > 0 ) |
3330 | 14 | { |
3331 | 14 | p_track->i_decoder_delay = MP4_rescale( |
3332 | 14 | p_sys->qt.i_delay_samples, i_rate, |
3333 | 14 | p_track->i_timescale ); |
3334 | 14 | } |
3335 | | /* AAC historical Apple decoder delay 2112 > 2048 */ |
3336 | 253 | else if( p_fmt->i_codec == VLC_CODEC_MP4A ) |
3337 | 220 | { |
3338 | 220 | p_track->i_decoder_delay = MP4_rescale( |
3339 | 220 | 2112, i_rate, |
3340 | 220 | p_track->i_timescale ); |
3341 | 220 | } |
3342 | | /* Opus has an expected 80ms discard on seek */ |
3343 | 33 | else if( p_fmt->i_codec == VLC_CODEC_OPUS ) |
3344 | 1 | { |
3345 | 1 | p_track->i_decoder_delay = MP4_rescale( |
3346 | 1 | 80 * 48, 48000, |
3347 | 1 | p_track->i_timescale ); |
3348 | 1 | } |
3349 | | /* MPEG have One frame delay */ |
3350 | 32 | else if( p_fmt->i_codec == VLC_CODEC_MP3 ) |
3351 | 12 | { |
3352 | | /* https://lame.sourceforge.io/tech-FAQ.txt |
3353 | | * https://www.compuphase.com/mp3/mp3loops.htm |
3354 | | https://www.iis.fraunhofer.de/content/dam/iis/de/doc/ame/conference/AES-116-Convention_guideline-to-audio-codec-delay_AES116.pdf */ |
3355 | 12 | p_track->i_decoder_delay = MP4_rescale( |
3356 | 12 | 576, i_rate, |
3357 | 12 | p_track->i_timescale ); |
3358 | 12 | } |
3359 | 20 | else if( p_fmt->i_codec == VLC_CODEC_MPGA || |
3360 | 9 | p_fmt->i_codec == VLC_CODEC_MP2 ) |
3361 | 20 | { |
3362 | 20 | p_track->i_decoder_delay = MP4_rescale( |
3363 | 20 | 240, i_rate, |
3364 | 20 | p_track->i_timescale ); |
3365 | 20 | } |
3366 | 282 | } |
3367 | 282 | break; |
3368 | 1.73k | default: |
3369 | 1.73k | break; |
3370 | 2.02k | } |
3371 | 2.02k | break; |
3372 | 2.02k | } |
3373 | 2.02k | default: |
3374 | 636 | break; |
3375 | 3.83k | } |
3376 | | |
3377 | 3.83k | p_track->p_sample = p_sample; |
3378 | | |
3379 | 3.83k | if( pp_es ) |
3380 | 3.81k | *pp_es = MP4_CreateES( p_demux->out, p_fmt, p_track->b_forced_spu ); |
3381 | | |
3382 | 3.83k | return ( !pp_es || *pp_es ) ? VLC_SUCCESS : VLC_EGENERIC; |
3383 | 3.83k | } |
3384 | | |
3385 | | /* *** Try to find nearest sync points *** */ |
3386 | | static int TrackGetNearestSeekPoint( demux_t *p_demux, mp4_track_t *p_track, |
3387 | | uint32_t i_sample, uint32_t *pi_sync_sample ) |
3388 | 176M | { |
3389 | 176M | int i_ret = VLC_EGENERIC; |
3390 | 176M | *pi_sync_sample = i_sample; |
3391 | | |
3392 | 176M | const MP4_Box_t *p_stss; |
3393 | | /* try sync points without recovery roll */ |
3394 | 176M | if( ( p_stss = MP4_BoxGet( p_track->p_stbl, "stss" ) ) ) |
3395 | 1.31M | { |
3396 | | /* XXX in libmp4 sample begin at 0 */ |
3397 | 1.31M | const uint32_t stts_sample = i_sample + 1; |
3398 | 1.31M | const MP4_Box_data_stss_t *p_stss_data = BOXDATA(p_stss); |
3399 | 1.31M | msg_Dbg( p_demux, "track[Id 0x%x] using Sync Sample Box (stss)", |
3400 | 1.31M | p_track->i_track_ID ); |
3401 | 17.1M | for( unsigned i_index = 0; i_index < p_stss_data->i_entry_count; i_index++ ) |
3402 | 16.9M | { |
3403 | 16.9M | if(( i_index == p_stss_data->i_entry_count - 1 || |
3404 | 16.8M | stts_sample < p_stss_data->i_sample_number[i_index+1] ) && |
3405 | 1.08M | p_stss_data->i_sample_number[i_index] != 0 ) |
3406 | 1.08M | { |
3407 | 1.08M | *pi_sync_sample = p_stss_data->i_sample_number[i_index] - 1; |
3408 | 1.08M | msg_Dbg( p_demux, "stss gives %d --> %" PRIu32 " (sample number)", |
3409 | 1.08M | i_sample, *pi_sync_sample ); |
3410 | 1.08M | i_ret = VLC_SUCCESS; |
3411 | 1.08M | break; |
3412 | 1.08M | } |
3413 | 16.9M | } |
3414 | 1.31M | } |
3415 | | |
3416 | | /* try or refine using RAP with recovery roll info */ |
3417 | 176M | uint32_t i_alternative_sync_sample = i_sample; |
3418 | 176M | if( MP4_SampleToGroupInfo( p_track->p_stbl, i_sample, SAMPLEGROUP_rap, |
3419 | 176M | 0, &i_alternative_sync_sample, true, NULL ) ) |
3420 | 33 | { |
3421 | 33 | i_ret = VLC_SUCCESS; |
3422 | 33 | msg_Dbg( p_demux, "tk %u sbgp gives %d --> %" PRIu32 " (sample number)", |
3423 | 33 | p_track->i_track_ID, i_sample, i_alternative_sync_sample ); |
3424 | 33 | if( i_alternative_sync_sample > *pi_sync_sample && |
3425 | 0 | i_alternative_sync_sample < i_sample ) |
3426 | 0 | *pi_sync_sample = i_alternative_sync_sample; |
3427 | 33 | } |
3428 | | |
3429 | 176M | return i_ret; |
3430 | 176M | } |
3431 | | |
3432 | | struct cmplowestchunkdts_ctx |
3433 | | { |
3434 | | uint64_t dts; |
3435 | | const mp4_chunk_t **pp_lowest; |
3436 | | }; |
3437 | | |
3438 | | static int cmplowestchunkdts( const void *key, const void *other ) |
3439 | 1.41G | { |
3440 | 1.41G | const struct cmplowestchunkdts_ctx *ctx = key; |
3441 | 1.41G | const mp4_chunk_t *ck = other; |
3442 | 1.41G | if(ctx->dts > ck->i_first_dts) |
3443 | 1.41G | { |
3444 | 1.41G | *ctx->pp_lowest = ck; |
3445 | 1.41G | return 1; |
3446 | 1.41G | } |
3447 | 1.85M | else if(ctx->dts < ck->i_first_dts) |
3448 | 1.85M | { |
3449 | 1.85M | return -1; |
3450 | 1.85M | } |
3451 | 758 | else |
3452 | 758 | { |
3453 | 758 | *ctx->pp_lowest = ck; |
3454 | 758 | return 0; |
3455 | 758 | } |
3456 | 1.41G | } |
3457 | | |
3458 | | static int STTSToSampleChunk(const mp4_track_t *p_track, uint64_t i_dts, |
3459 | | uint32_t *pi_chunk, uint32_t *pi_sample) |
3460 | 194M | { |
3461 | 194M | const mp4_chunk_t *ck = NULL; |
3462 | | |
3463 | 194M | if( p_track->i_chunk_count > 1 && /* start heuristic */ |
3464 | 177M | (uint64_t) i_dts < p_track->chunk[1].i_first_dts ) |
3465 | 140k | { |
3466 | 140k | ck = &p_track->chunk[0]; |
3467 | 140k | } |
3468 | 193M | else |
3469 | 193M | { |
3470 | 193M | const mp4_chunk_t *lowestchunk = NULL; |
3471 | 193M | struct cmplowestchunkdts_ctx bsearchkeyctx = { .dts = i_dts, |
3472 | 193M | .pp_lowest = &lowestchunk }; |
3473 | 193M | ck = bsearch( &bsearchkeyctx, p_track->chunk, p_track->i_chunk_count, |
3474 | 193M | sizeof(*p_track->chunk), cmplowestchunkdts ); |
3475 | 193M | if( ck == NULL ) |
3476 | 193M | ck = lowestchunk; |
3477 | 193M | if( unlikely(ck == NULL) ) |
3478 | | /* if we default to last chunk, it will be checked searching i_sample */ |
3479 | 0 | ck = &p_track->chunk[p_track->i_chunk_count - 1]; |
3480 | 193M | } |
3481 | | |
3482 | | /* *** find sample in the chunk *** */ |
3483 | 194M | uint32_t i_sample = ck->i_sample_first; |
3484 | 194M | uint64_t i_entrydts = ck->i_first_dts; |
3485 | | |
3486 | 194M | for( uint_fast32_t i = 0; |
3487 | 213M | i < ck->i_entries_dts && i_sample < ck->i_sample_count; |
3488 | 194M | i++ ) |
3489 | 19.6M | { |
3490 | 19.6M | uint64_t i_entry_duration = ck->p_sample_count_dts[i] * (uint64_t) |
3491 | 19.6M | ck->p_sample_delta_dts[i]; |
3492 | 19.6M | if( i_entrydts + i_entry_duration < i_dts ) |
3493 | 19.5M | { |
3494 | 19.5M | i_entrydts += i_entry_duration; |
3495 | 19.5M | i_sample += ck->p_sample_count_dts[i]; |
3496 | 19.5M | } |
3497 | 141k | else |
3498 | 141k | { |
3499 | 141k | if( ck->p_sample_delta_dts[i] > 0 ) |
3500 | 141k | i_sample += ( i_dts - i_entrydts ) / ck->p_sample_delta_dts[i]; |
3501 | 141k | break; |
3502 | 141k | } |
3503 | 19.6M | } |
3504 | | |
3505 | 194M | *pi_chunk = ck - p_track->chunk; |
3506 | 194M | *pi_sample = i_sample; |
3507 | | |
3508 | 194M | if( i_sample >= p_track->i_sample_count ) |
3509 | 17.0M | return VLC_EGENERIC; |
3510 | | |
3511 | 177M | return VLC_SUCCESS; |
3512 | 194M | } |
3513 | | |
3514 | | /* given a time it return sample/chunk |
3515 | | * it also update elst field of the track |
3516 | | */ |
3517 | | static int TrackTimeToSampleChunk( demux_t *p_demux, mp4_track_t *p_track, |
3518 | | vlc_tick_t start, uint32_t *pi_chunk, |
3519 | | uint32_t *pi_sample ) |
3520 | 193M | { |
3521 | 193M | demux_sys_t *p_sys = p_demux->p_sys; |
3522 | 193M | stime_t i_start; |
3523 | | |
3524 | | /* FIXME see if it's needed to check p_track->i_chunk_count */ |
3525 | 193M | if( p_track->i_chunk_count == 0 ) |
3526 | 4.19k | return( VLC_EGENERIC ); |
3527 | | |
3528 | | /* handle elst (find the correct one) */ |
3529 | 193M | MP4_TrackSetELST( p_demux, p_track, start ); |
3530 | 193M | if( p_track->p_elst && p_track->BOXDATA(p_elst)->i_entry_count > 0 ) |
3531 | 188M | { |
3532 | | /* now calculate i_start for this elst */ |
3533 | | /* offset */ |
3534 | 188M | if( start < MP4_rescale_mtime( p_track->i_elst_time, p_sys->i_timescale ) ) |
3535 | 42 | { |
3536 | 42 | *pi_chunk = 0; |
3537 | 42 | *pi_sample= 0; |
3538 | | |
3539 | 42 | return VLC_SUCCESS; |
3540 | 42 | } |
3541 | | /* to track time scale */ |
3542 | 188M | i_start = MP4_rescale_qtime( start, p_track->i_timescale ); |
3543 | | /* map through elst offset */ |
3544 | 188M | i_start = MP4_MapMediaTimelineToTrackTime( p_track, p_sys->i_timescale, i_start ); |
3545 | | |
3546 | 188M | msg_Dbg( p_demux, "elst (%d) gives %"PRId64"ms (movie)-> %"PRId64 |
3547 | 188M | "ms (track)", p_track->i_elst, |
3548 | 188M | MS_FROM_VLC_TICK(start), |
3549 | 188M | MP4_rescale( i_start, p_track->i_timescale, 1000 ) ); |
3550 | 188M | } |
3551 | 4.91M | else |
3552 | 4.91M | { |
3553 | | /* convert absolute time to in timescale unit */ |
3554 | 4.91M | i_start = MP4_rescale_qtime( start, p_track->i_timescale ); |
3555 | 4.91M | } |
3556 | | |
3557 | | /* *** find good chunk *** */ |
3558 | 193M | uint32_t i_sample, i_chunk; |
3559 | 193M | if( STTSToSampleChunk( p_track, i_start, &i_chunk, &i_sample ) != VLC_SUCCESS ) |
3560 | 17.0M | { |
3561 | 17.0M | msg_Warn( p_demux, "track[Id 0x%x] will be disabled " |
3562 | 17.0M | "(seeking too far) chunk=%"PRIu32" sample=%"PRIu32, |
3563 | 17.0M | p_track->i_track_ID, i_chunk, i_sample ); |
3564 | 17.0M | return( VLC_EGENERIC ); |
3565 | 17.0M | } |
3566 | | |
3567 | | |
3568 | | /* *** Try to find nearest sync points *** */ |
3569 | 176M | uint32_t i_sync_sample = i_sample; |
3570 | 176M | if( VLC_SUCCESS == |
3571 | 176M | TrackGetNearestSeekPoint( p_demux, p_track, i_sample, &i_sync_sample ) ) |
3572 | 1.08M | { |
3573 | 1.08M | msg_Dbg(p_demux,"track[Id 0x%x] sync point found sample %"PRIu32"(-%"PRIu32")", |
3574 | 1.08M | p_track->i_track_ID, i_sync_sample, i_sample - i_sync_sample); |
3575 | 1.08M | } |
3576 | 175M | else |
3577 | 175M | { |
3578 | 175M | const MP4_Box_data_sgpd_entry_t *p_entrydesc; |
3579 | 175M | if( MP4_SampleToGroupInfo( p_track->p_stbl, i_sample, |
3580 | 175M | SAMPLEGROUP_roll, 0, &i_sync_sample, |
3581 | 175M | SAMPLE_GROUP_MATCH_EXACT, &p_entrydesc ) ) |
3582 | 1 | { |
3583 | 1 | msg_Dbg(p_demux, "track[Id 0x%x] preroll offset: %"PRId16" samples", |
3584 | 1 | p_track->i_track_ID, p_entrydesc->roll.i_roll_distance ); |
3585 | 1 | if( p_entrydesc->roll.i_roll_distance < 0 ) |
3586 | 1 | { |
3587 | 1 | if( i_sync_sample > (uint32_t)-p_entrydesc->roll.i_roll_distance ) |
3588 | 0 | i_sync_sample += p_entrydesc->roll.i_roll_distance; |
3589 | 1 | else |
3590 | 1 | i_sync_sample = 0; |
3591 | 1 | } |
3592 | 1 | } |
3593 | 175M | else if( p_track->i_decoder_delay > 0 && |
3594 | 912k | p_track->i_decoder_delay <= i_start ) |
3595 | 911k | { |
3596 | 911k | uint32_t i_withdelay_sample, i_withdelay_chunk; |
3597 | 911k | if( STTSToSampleChunk( p_track, i_start - p_track->i_decoder_delay, |
3598 | 911k | &i_withdelay_chunk, |
3599 | 911k | &i_withdelay_sample ) == VLC_SUCCESS ) |
3600 | 911k | { |
3601 | 911k | msg_Warn( p_demux, "track[Id 0x%x] has %" PRId64 "µs decoder delay from %" PRId64, |
3602 | 911k | p_track->i_track_ID, |
3603 | 911k | MP4_rescale_mtime( p_track->i_decoder_delay, p_track->i_timescale), |
3604 | 911k | i_start ); |
3605 | 911k | *pi_chunk = i_withdelay_chunk; |
3606 | 911k | *pi_sample = i_withdelay_sample; |
3607 | 911k | return VLC_SUCCESS; |
3608 | 911k | } |
3609 | 911k | } |
3610 | 175M | } |
3611 | | |
3612 | | /* Go to sync point chunk */ |
3613 | 175M | if( i_sync_sample <= i_sample ) |
3614 | 175M | { |
3615 | 182M | while( i_chunk > 0 && |
3616 | 182M | i_sync_sample < p_track->chunk[i_chunk].i_sample_first ) |
3617 | 7.69M | i_chunk--; |
3618 | 175M | } |
3619 | 17 | else |
3620 | 17 | { |
3621 | 658 | while( i_chunk < p_track->i_chunk_count - 1 && |
3622 | 643 | i_sync_sample >= p_track->chunk[i_chunk].i_sample_first + |
3623 | 643 | p_track->chunk[i_chunk].i_sample_count ) |
3624 | 641 | i_chunk++; |
3625 | 17 | } |
3626 | | |
3627 | 175M | *pi_chunk = i_chunk; |
3628 | 175M | *pi_sample = i_sync_sample; |
3629 | | |
3630 | 175M | return VLC_SUCCESS; |
3631 | 176M | } |
3632 | | |
3633 | | static bool FormatIsCompatible( const es_format_t *p_fmt1, const es_format_t *p_fmt2 ) |
3634 | 559 | { |
3635 | 559 | if( p_fmt1->i_original_fourcc != p_fmt2->i_original_fourcc ) |
3636 | 0 | return false; |
3637 | 559 | if( (p_fmt1->i_extra > 0) ^ (p_fmt2->i_extra > 0) ) |
3638 | 3 | return false; |
3639 | | |
3640 | 556 | if( p_fmt1->i_codec == p_fmt2->i_codec && |
3641 | 64 | p_fmt1->i_extra && p_fmt2->i_extra && |
3642 | 38 | p_fmt1->i_extra == p_fmt2->i_extra ) |
3643 | 37 | { |
3644 | 37 | return !!memcmp( p_fmt1->p_extra, p_fmt2->p_extra, p_fmt1->i_extra ); |
3645 | 37 | } |
3646 | | |
3647 | 519 | if( p_fmt1->i_cat == AUDIO_ES ) |
3648 | 0 | { |
3649 | | /* Reject audio streams with different or unknown rates */ |
3650 | 0 | if( p_fmt1->audio.i_rate != p_fmt2->audio.i_rate || !p_fmt1->audio.i_rate ) |
3651 | 0 | return false; |
3652 | 0 | } |
3653 | | |
3654 | 519 | return es_format_IsSimilar( p_fmt1, p_fmt2 ); |
3655 | 519 | } |
3656 | | |
3657 | | static int TrackUpdateFormat( demux_t *p_demux, mp4_track_t *p_track, |
3658 | | uint32_t i_previous_chunk ) |
3659 | 179M | { |
3660 | 179M | uint32_t i_chunk = p_track->i_chunk; |
3661 | | /* now see if actual es is ok */ |
3662 | 179M | if( i_previous_chunk != i_chunk && |
3663 | 516k | p_track->chunk[i_previous_chunk].i_sample_description_index != |
3664 | 516k | p_track->chunk[i_chunk].i_sample_description_index ) |
3665 | 608 | { |
3666 | 608 | msg_Warn( p_demux, "recreate ES for track[Id 0x%x]", |
3667 | 608 | p_track->i_track_ID ); |
3668 | | |
3669 | 608 | const MP4_Box_t *p_newsample = MP4_BoxGetVa( p_track->p_stsd, "[%d]", |
3670 | 608 | p_track->chunk[i_chunk].i_sample_description_index - 1 ); |
3671 | 608 | if( p_newsample == NULL ) |
3672 | 49 | { |
3673 | 49 | msg_Err( p_demux, "Can't change track[Id 0x%x] format for sample desc %" PRIu32, |
3674 | 49 | p_track->i_track_ID, p_track->chunk[i_chunk].i_sample_description_index ); |
3675 | 49 | p_track->b_ok = false; |
3676 | 49 | p_track->b_selected = false; |
3677 | 49 | return VLC_EGENERIC; |
3678 | 49 | } |
3679 | | |
3680 | 559 | track_config_t cfg; |
3681 | 559 | TrackConfigInit( &cfg ); |
3682 | 559 | es_format_t tmpfmt; |
3683 | 559 | es_format_Init( &tmpfmt, p_track->fmt.i_cat, 0 ); |
3684 | 559 | tmpfmt.i_id = p_track->i_track_ID; |
3685 | 559 | if( TrackFillConfig( p_demux, p_track, p_newsample, i_chunk, &tmpfmt, &cfg ) == VLC_SUCCESS && |
3686 | 559 | !FormatIsCompatible( &p_track->fmt, &tmpfmt ) ) |
3687 | 506 | { |
3688 | 506 | bool b_reselect = false; |
3689 | | |
3690 | 506 | es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, |
3691 | 506 | p_track->p_es, &b_reselect ); |
3692 | | |
3693 | 506 | es_out_Del( p_demux->out, p_track->p_es ); |
3694 | | |
3695 | 506 | p_track->p_es = MP4_CreateES( p_demux->out, &tmpfmt, cfg.b_forced_spu ); |
3696 | 506 | if( !p_track->p_es ) |
3697 | 0 | { |
3698 | 0 | msg_Err( p_demux, "cannot create es for track[Id 0x%x]", |
3699 | 0 | p_track->i_track_ID ); |
3700 | |
|
3701 | 0 | p_track->b_ok = false; |
3702 | 0 | p_track->b_selected = false; |
3703 | 0 | es_format_Clean( &tmpfmt ); |
3704 | 0 | return VLC_EGENERIC; |
3705 | 0 | } |
3706 | | |
3707 | | /* select again the new decoder */ |
3708 | 506 | if( b_reselect ) |
3709 | 506 | es_out_Control( p_demux->out, ES_OUT_SET_ES, p_track->p_es ); |
3710 | | |
3711 | 506 | TrackConfigApply( &cfg, p_track ); |
3712 | 506 | es_format_Clean( &p_track->fmt ); |
3713 | 506 | es_format_Init( &p_track->fmt, tmpfmt.i_cat, tmpfmt.i_codec ); |
3714 | 506 | es_format_Copy( &p_track->fmt, &tmpfmt ); |
3715 | 506 | p_track->p_sample = p_newsample; |
3716 | 506 | } |
3717 | 559 | es_format_Clean( &tmpfmt ); |
3718 | 559 | } |
3719 | | |
3720 | 179M | return VLC_SUCCESS; |
3721 | 179M | } |
3722 | | |
3723 | | static int TrackGotoChunkSample( demux_t *p_demux, mp4_track_t *p_track, |
3724 | | uint32_t i_chunk, uint32_t i_sample ) |
3725 | 176M | { |
3726 | 176M | if( TrackUpdateFormat( p_demux, p_track, p_track->i_chunk ) != VLC_SUCCESS ) |
3727 | 0 | return VLC_EGENERIC; |
3728 | | |
3729 | 176M | p_track->i_chunk = i_chunk; |
3730 | 176M | p_track->i_sample = i_sample; |
3731 | | |
3732 | 176M | return p_track->b_selected ? VLC_SUCCESS : VLC_EGENERIC; |
3733 | 176M | } |
3734 | | |
3735 | | static void TrackUpdateStarttimes( mp4_track_t *p_track ) |
3736 | 176M | { |
3737 | 176M | assert(p_track->i_chunk < p_track->i_chunk_count); |
3738 | | |
3739 | 176M | p_track->i_start_dts = p_track->i_next_dts; |
3740 | 176M | p_track->i_start_delta = p_track->i_next_delta; |
3741 | | |
3742 | | /* Probe the 16 first B frames */ |
3743 | 176M | uint32_t i_chunk = p_track->i_chunk; |
3744 | 176M | if( !p_track->chunk[i_chunk].i_entries_pts ) |
3745 | 176M | return; |
3746 | | |
3747 | 105k | stime_t lowest = p_track->i_start_dts; |
3748 | 105k | if( p_track->i_start_delta != UNKNOWN_DELTA ) |
3749 | 104k | lowest += p_track->i_start_delta; |
3750 | | |
3751 | 907k | for( uint32_t i=1; i<16; i++ ) |
3752 | 905k | { |
3753 | 905k | uint32_t i_nextsample = p_track->i_sample + i; |
3754 | 905k | if( i_nextsample >= p_track->i_sample_count ) |
3755 | 103k | break; |
3756 | 802k | const mp4_chunk_t *ck = NULL; |
3757 | 1.57M | for( ; i_chunk < p_track->i_chunk_count; i_chunk++ ) |
3758 | 1.57M | { |
3759 | 1.57M | ck = &p_track->chunk[i_chunk]; |
3760 | 1.57M | if( i_nextsample < ck->i_sample_first + ck->i_sample_count ) |
3761 | 802k | break; |
3762 | 1.57M | } |
3763 | 802k | if( !ck ) |
3764 | 0 | break; |
3765 | 802k | assert(i_nextsample >= ck->i_sample_first); |
3766 | 802k | stime_t pts; |
3767 | 802k | stime_t dts = pts = MP4_ChunkGetSampleDTS( ck, i_nextsample - ck->i_sample_first ); |
3768 | 802k | stime_t delta = UNKNOWN_DELTA; |
3769 | 802k | if( MP4_ChunkGetSampleCTSDelta( ck, i_nextsample - ck->i_sample_first, &delta ) ) |
3770 | 788k | pts += delta; |
3771 | 802k | if( pts < lowest ) |
3772 | 165k | { |
3773 | 165k | p_track->i_start_dts = dts; |
3774 | 165k | p_track->i_start_delta = delta; |
3775 | 165k | } |
3776 | 802k | } |
3777 | 105k | } |
3778 | | |
3779 | | static void TrackUpdateSampleAndTimes( mp4_track_t *p_track ) |
3780 | 180M | { |
3781 | 180M | const mp4_chunk_t *p_chunk = &p_track->chunk[p_track->i_chunk]; |
3782 | 180M | uint32_t i_chunk_sample = p_track->i_sample - p_chunk->i_sample_first; |
3783 | 180M | if( i_chunk_sample > p_chunk->i_sample_count && p_chunk->i_sample_count ) |
3784 | 31 | i_chunk_sample = p_chunk->i_sample_count - 1; |
3785 | 180M | p_track->i_next_dts = MP4_ChunkGetSampleDTS( p_chunk, i_chunk_sample ); |
3786 | 180M | stime_t i_next_delta; |
3787 | 180M | if( !MP4_ChunkGetSampleCTSDelta( p_chunk, i_chunk_sample, &i_next_delta ) ) |
3788 | 179M | p_track->i_next_delta = UNKNOWN_DELTA; |
3789 | 1.28M | else |
3790 | 1.28M | p_track->i_next_delta = i_next_delta; |
3791 | 180M | } |
3792 | | |
3793 | | /**************************************************************************** |
3794 | | * MP4_TrackSetup: |
3795 | | **************************************************************************** |
3796 | | * Parse track information and create all needed data to run a track |
3797 | | * If it succeed b_ok is set to 1 else to 0 |
3798 | | ****************************************************************************/ |
3799 | | static void MP4_TrackSetup( demux_t *p_demux, mp4_track_t *p_track, |
3800 | | const MP4_Box_t *p_box_trak, |
3801 | | bool b_create_es, bool b_force_enable ) |
3802 | 5.91k | { |
3803 | 5.91k | demux_sys_t *p_sys = p_demux->p_sys; |
3804 | | |
3805 | 5.91k | p_track->p_track = p_box_trak; |
3806 | | |
3807 | 5.91k | char language[4] = { '\0' }; |
3808 | 5.91k | char sdp_media_type[8] = { '\0' }; |
3809 | | |
3810 | 5.91k | const MP4_Box_t *p_tkhd = MP4_BoxGet( p_box_trak, "tkhd" ); |
3811 | 5.91k | if( !p_tkhd ) |
3812 | 646 | { |
3813 | 646 | return; |
3814 | 646 | } |
3815 | | |
3816 | | /* do we launch this track by default ? */ |
3817 | 5.27k | p_track->b_enable = |
3818 | 5.27k | ( ( BOXDATA(p_tkhd)->i_flags&MP4_TRACK_ENABLED ) != 0 ); |
3819 | | |
3820 | 5.27k | p_track->i_width = BOXDATA(p_tkhd)->i_width / BLOCK16x16; |
3821 | 5.27k | p_track->i_height = BOXDATA(p_tkhd)->i_height / BLOCK16x16; |
3822 | 5.27k | p_track->f_rotation = BOXDATA(p_tkhd)->f_rotation; |
3823 | 5.27k | p_track->i_flip = BOXDATA(p_tkhd)->i_flip; |
3824 | | |
3825 | | /* FIXME: unhandled box: tref */ |
3826 | | |
3827 | 5.27k | const MP4_Box_t *p_mdhd = MP4_BoxGet( p_box_trak, "mdia/mdhd" ); |
3828 | 5.27k | const MP4_Box_t *p_hdlr = MP4_BoxGet( p_box_trak, "mdia/hdlr" ); |
3829 | | |
3830 | 5.27k | if( ( !p_mdhd )||( !p_hdlr ) ) |
3831 | 367 | { |
3832 | 367 | return; |
3833 | 367 | } |
3834 | | |
3835 | 4.90k | if( BOXDATA(p_mdhd)->i_timescale == 0 ) |
3836 | 0 | { |
3837 | 0 | msg_Warn( p_demux, "Invalid track timescale " ); |
3838 | 0 | return; |
3839 | 0 | } |
3840 | 4.90k | p_track->i_timescale = BOXDATA(p_mdhd)->i_timescale; |
3841 | | |
3842 | 4.90k | memcpy( &language, BOXDATA(p_mdhd)->rgs_language, 3 ); |
3843 | 4.90k | p_track->b_mac_encoding = BOXDATA(p_mdhd)->b_mac_encoding; |
3844 | | |
3845 | 4.90k | switch( p_hdlr->data.p_hdlr->i_handler_type ) |
3846 | 4.90k | { |
3847 | 2.42k | case( ATOM_soun ): |
3848 | 2.42k | if( !MP4_BoxGet( p_box_trak, "mdia/minf/smhd" ) ) |
3849 | 35 | { |
3850 | 35 | return; |
3851 | 35 | } |
3852 | 2.39k | es_format_Change( &p_track->fmt, AUDIO_ES, 0 ); |
3853 | 2.39k | break; |
3854 | | |
3855 | 18 | case( ATOM_pict ): /* heif */ |
3856 | 18 | es_format_Change( &p_track->fmt, VIDEO_ES, 0 ); |
3857 | 18 | break; |
3858 | | |
3859 | 1.41k | case( ATOM_vide ): |
3860 | 1.41k | if( !MP4_BoxGet( p_box_trak, "mdia/minf/vmhd") ) |
3861 | 33 | { |
3862 | 33 | return; |
3863 | 33 | } |
3864 | 1.37k | es_format_Change( &p_track->fmt, VIDEO_ES, 0 ); |
3865 | 1.37k | break; |
3866 | | |
3867 | 171 | case( ATOM_hint ): |
3868 | | /* RTP Reception Hint tracks */ |
3869 | 171 | if( !MP4_BoxGet( p_box_trak, "mdia/minf/hmhd" ) || |
3870 | 43 | !MP4_BoxGet( p_box_trak, "mdia/minf/stbl/stsd/rrtp" ) ) |
3871 | 171 | { |
3872 | 171 | break; |
3873 | 171 | } |
3874 | 0 | MP4_Box_t *p_sdp; |
3875 | | |
3876 | | /* parse the sdp message to find out whether the RTP stream contained audio or video */ |
3877 | 0 | if( !( p_sdp = MP4_BoxGet( p_box_trak, "udta/hnti/sdp " ) ) ) |
3878 | 0 | { |
3879 | 0 | msg_Warn( p_demux, "Didn't find sdp box to determine stream type" ); |
3880 | 0 | return; |
3881 | 0 | } |
3882 | | |
3883 | 0 | memcpy( sdp_media_type, BOXDATA(p_sdp)->psz_text, 7 ); |
3884 | 0 | if( !strcmp(sdp_media_type, "m=audio") ) |
3885 | 0 | { |
3886 | 0 | msg_Dbg( p_demux, "Found audio Rtp: %s", sdp_media_type ); |
3887 | 0 | es_format_Change( &p_track->fmt, AUDIO_ES, 0 ); |
3888 | 0 | } |
3889 | 0 | else if( !strcmp(sdp_media_type, "m=video") ) |
3890 | 0 | { |
3891 | 0 | msg_Dbg( p_demux, "Found video Rtp: %s", sdp_media_type ); |
3892 | 0 | es_format_Change( &p_track->fmt, VIDEO_ES, 0 ); |
3893 | 0 | } |
3894 | 0 | else |
3895 | 0 | { |
3896 | 0 | msg_Warn( p_demux, "Malformed track SDP message: %s", sdp_media_type ); |
3897 | 0 | return; |
3898 | 0 | } |
3899 | 0 | break; |
3900 | | |
3901 | 256 | case( ATOM_tx3g ): |
3902 | 644 | case( ATOM_text ): |
3903 | 652 | case( ATOM_subp ): |
3904 | 665 | case( ATOM_subt ): /* ttml */ |
3905 | 667 | case( ATOM_sbtl ): |
3906 | 671 | case( ATOM_clcp ): /* closed captions */ |
3907 | 671 | es_format_Change( &p_track->fmt, SPU_ES, 0 ); |
3908 | 671 | break; |
3909 | | |
3910 | 209 | default: |
3911 | 209 | return; |
3912 | 4.90k | } |
3913 | | |
3914 | 4.62k | p_track->asfinfo.i_cat = p_track->fmt.i_cat; |
3915 | | |
3916 | 4.62k | const MP4_Box_t *p_elst; |
3917 | 4.62k | p_track->i_elst = 0; |
3918 | 4.62k | p_track->i_elst_time = 0; |
3919 | 4.62k | p_track->p_elst = NULL; |
3920 | 4.62k | if( ( p_elst = MP4_BoxGet( p_box_trak, "edts/elst" ) ) ) |
3921 | 1.70k | { |
3922 | 1.70k | MP4_Box_data_elst_t *elst = BOXDATA(p_elst); |
3923 | 1.70k | unsigned int i; |
3924 | | |
3925 | 1.70k | msg_Warn( p_demux, "elst box found" ); |
3926 | 1.70k | bool use_editlist = var_InheritBool( p_demux, CFG_PREFIX"editlist" ); |
3927 | 5.27k | for( i = 0; i < elst->i_entry_count; i++ ) |
3928 | 3.56k | { |
3929 | 3.56k | const MP4_Box_data_elst_entry_t *edit = &elst->entries[i]; |
3930 | 3.56k | if ( edit->i_segment_duration > INT64_MAX / CLOCK_FREQ || |
3931 | 3.51k | ( edit->i_media_time >= 0 && edit->i_media_time > INT64_MAX / CLOCK_FREQ ) ) |
3932 | 66 | { |
3933 | 66 | use_editlist = false; |
3934 | 66 | msg_Dbg( p_demux, " - [%d] bogus duration=%" PRId64 " media time=%" PRId64 ")", |
3935 | 66 | i, edit->i_segment_duration, edit->i_media_time ); |
3936 | 66 | } |
3937 | 3.50k | else |
3938 | 3.56k | msg_Dbg( p_demux, " - [%d] duration=%"PRId64"ms media time=%"PRId64 |
3939 | 3.56k | "ms) rate=%d.%d", i, |
3940 | 3.56k | MP4_rescale( edit->i_segment_duration, p_sys->i_timescale, 1000 ), |
3941 | 3.56k | edit->i_media_time >= 0 ? |
3942 | 3.56k | MP4_rescale( edit->i_media_time, p_track->i_timescale, 1000 ) : |
3943 | 3.56k | INT64_C(-1), |
3944 | 3.56k | edit->i_media_rate_integer, |
3945 | 3.56k | edit->i_media_rate_fraction ); |
3946 | 3.56k | } |
3947 | | |
3948 | 1.70k | if( use_editlist ) |
3949 | 1.69k | p_track->p_elst = p_elst; |
3950 | 9 | else |
3951 | 1.70k | msg_Dbg( p_demux, "ignore editlist" ); |
3952 | 1.70k | } |
3953 | | |
3954 | | /* TODO |
3955 | | add support for: |
3956 | | p_dinf = MP4_BoxGet( p_minf, "dinf" ); |
3957 | | */ |
3958 | 4.62k | if( !( p_track->p_stbl = MP4_BoxGet( p_box_trak,"mdia/minf/stbl" ) ) || |
3959 | 4.41k | !( p_track->p_stsd = MP4_BoxGet( p_box_trak,"mdia/minf/stbl/stsd") ) ) |
3960 | 313 | { |
3961 | 313 | return; |
3962 | 313 | } |
3963 | | |
3964 | | /* Set language */ |
3965 | 4.31k | if( *language && strcmp( language, "```" ) && strcmp( language, "und" ) ) |
3966 | 2.70k | { |
3967 | 2.70k | p_track->fmt.psz_language = strdup( language ); |
3968 | 2.70k | } |
3969 | | |
3970 | 4.31k | const MP4_Box_t *p_udta = MP4_BoxGet( p_box_trak, "udta" ); |
3971 | 4.31k | if( p_udta ) |
3972 | 407 | { |
3973 | 407 | const MP4_Box_t *p_box_iter; |
3974 | 666 | for( p_box_iter = p_udta->p_first; p_box_iter != NULL; |
3975 | 407 | p_box_iter = p_box_iter->p_next ) |
3976 | 259 | { |
3977 | 259 | switch( p_box_iter->i_type ) |
3978 | 259 | { |
3979 | 0 | case ATOM_0xa9nam: |
3980 | 130 | case ATOM_name: |
3981 | 130 | p_track->fmt.psz_description = |
3982 | 130 | strndup( p_box_iter->data.p_binary->p_blob, |
3983 | 130 | p_box_iter->data.p_binary->i_blob ); |
3984 | 259 | default: |
3985 | 259 | break; |
3986 | 259 | } |
3987 | 259 | } |
3988 | 407 | } |
3989 | | |
3990 | | /* Create chunk index table and sample index table */ |
3991 | 4.31k | if( TrackCreateChunksIndex( p_demux,p_track ) || |
3992 | 3.94k | TrackCreateSamplesIndex( p_demux, p_track ) ) |
3993 | 417 | { |
3994 | 417 | msg_Err( p_demux, "cannot create chunks index" ); |
3995 | 417 | return; /* cannot create chunks index */ |
3996 | 417 | } |
3997 | | |
3998 | 3.89k | p_track->i_next_dts = 0; |
3999 | 3.89k | p_track->i_next_delta = UNKNOWN_DELTA; |
4000 | 3.89k | p_track->i_chunk = 0; |
4001 | 3.89k | p_track->i_sample = 0; |
4002 | | |
4003 | | /* Disable chapter only track */ |
4004 | 3.89k | if( (p_track->i_use_flags & USEAS_CHAPTERS) && !p_sys->b_quicktime ) |
4005 | 0 | p_track->b_enable = false; |
4006 | | |
4007 | 3.89k | const MP4_Box_t *p_tsel; |
4008 | | /* now create es */ |
4009 | 3.89k | if( b_force_enable && |
4010 | 405 | ( p_track->fmt.i_cat == VIDEO_ES || p_track->fmt.i_cat == AUDIO_ES ) ) |
4011 | 122 | { |
4012 | 122 | msg_Warn( p_demux, "Enabling track[Id 0x%x] (buggy file without enabled track)", |
4013 | 122 | p_track->i_track_ID ); |
4014 | 122 | p_track->b_enable = true; |
4015 | 122 | p_track->fmt.i_priority = ES_PRIORITY_SELECTABLE_MIN; |
4016 | 122 | } |
4017 | 3.77k | else if ( (p_tsel = MP4_BoxGet( p_box_trak, "udta/tsel" )) ) |
4018 | 10 | { |
4019 | 10 | if ( BOXDATA(p_tsel) && BOXDATA(p_tsel)->i_switch_group ) |
4020 | 4 | { |
4021 | 4 | p_track->i_switch_group = BOXDATA(p_tsel)->i_switch_group; |
4022 | 4 | int i_priority = ES_PRIORITY_SELECTABLE_MIN; |
4023 | 12 | for ( unsigned int i = 0; i < p_sys->i_tracks; i++ ) |
4024 | 8 | { |
4025 | 8 | const mp4_track_t *p_other = &p_sys->track[i]; |
4026 | 8 | if( p_other && p_other != p_track && |
4027 | 4 | p_other->fmt.i_cat == p_track->fmt.i_cat && |
4028 | 0 | p_track->i_switch_group == p_other->i_switch_group ) |
4029 | 0 | i_priority = __MAX( i_priority, p_other->fmt.i_priority + 1 ); |
4030 | 8 | } |
4031 | | /* VLC only support ES priority for AUDIO_ES and SPU_ES. |
4032 | | If there's another VIDEO_ES in the same group, we need to unselect it then */ |
4033 | 4 | if ( p_track->fmt.i_cat == VIDEO_ES && i_priority > ES_PRIORITY_SELECTABLE_MIN ) |
4034 | 0 | p_track->fmt.i_priority = ES_PRIORITY_NOT_DEFAULTABLE; |
4035 | 4 | else |
4036 | 4 | p_track->fmt.i_priority = i_priority; |
4037 | 4 | } |
4038 | 10 | } |
4039 | | /* If there's no tsel, try to enable the track coming first in edit list */ |
4040 | 3.76k | else if ( p_track->p_elst && p_track->fmt.i_priority == ES_PRIORITY_SELECTABLE_MIN ) |
4041 | 1.32k | { |
4042 | 1.32k | #define MAX_SELECTABLE (INT_MAX - ES_PRIORITY_SELECTABLE_MIN) |
4043 | 1.60k | for ( uint32_t i=0; i<p_track->BOXDATA(p_elst)->i_entry_count; i++ ) |
4044 | 1.31k | { |
4045 | 1.31k | const MP4_Box_data_elst_entry_t *edit = &p_track->BOXDATA(p_elst)->entries[i]; |
4046 | 1.31k | if ( edit->i_media_time >= 0 && edit->i_segment_duration ) |
4047 | 1.02k | { |
4048 | | /* We do selection by inverting start time into priority. |
4049 | | The track with earliest edit will have the highest prio */ |
4050 | 1.02k | const int i_time = __MIN( MAX_SELECTABLE, edit->i_media_time ); |
4051 | 1.02k | p_track->fmt.i_priority = ES_PRIORITY_SELECTABLE_MIN + MAX_SELECTABLE - i_time; |
4052 | 1.02k | break; |
4053 | 1.02k | } |
4054 | 1.31k | } |
4055 | 1.32k | } |
4056 | | |
4057 | 3.89k | if( p_sys->hacks.es_cat_filter != UNKNOWN_ES && |
4058 | 0 | p_sys->hacks.es_cat_filter != p_track->fmt.i_cat ) |
4059 | 0 | { |
4060 | 0 | p_track->fmt.i_priority = ES_PRIORITY_NOT_DEFAULTABLE; |
4061 | 0 | } |
4062 | | |
4063 | 3.89k | if( !p_track->b_enable || (p_track->i_use_flags & USEAS_CHAPTERS) ) |
4064 | 433 | p_track->fmt.i_priority = ES_PRIORITY_NOT_DEFAULTABLE; |
4065 | | |
4066 | 3.89k | if( !p_sys->b_quicktime || (p_track->i_use_flags & USEAS_CHAPTERS) == 0 ) |
4067 | 3.89k | b_create_es &= !MP4_isMetadata( p_track ); |
4068 | | |
4069 | 3.89k | if( TrackCreateES( p_demux, |
4070 | 3.89k | p_track, p_track->i_chunk, |
4071 | 3.89k | !b_create_es ? NULL : &p_track->p_es ) ) |
4072 | 64 | { |
4073 | 64 | msg_Err( p_demux, "cannot create es for track[Id 0x%x]", |
4074 | 64 | p_track->i_track_ID ); |
4075 | 64 | return; |
4076 | 64 | } |
4077 | | |
4078 | 3.83k | p_track->b_ok = true; |
4079 | 3.83k | } |
4080 | | |
4081 | | /**************************************************************************** |
4082 | | * MP4_TrackClean: |
4083 | | **************************************************************************** |
4084 | | * Cleans a track created by MP4_TrackCreate. |
4085 | | ****************************************************************************/ |
4086 | | static void MP4_TrackClean( es_out_t *out, mp4_track_t *p_track ) |
4087 | 5.91k | { |
4088 | 5.91k | es_format_Clean( &p_track->fmt ); |
4089 | | |
4090 | 5.91k | if( p_track->p_es ) |
4091 | 3.81k | es_out_Del( out, p_track->p_es ); |
4092 | | |
4093 | 5.91k | if( p_track->chunk ) |
4094 | 3.96k | { |
4095 | 230k | for( unsigned int i_chunk = 0; i_chunk < p_track->i_chunk_count; i_chunk++ ) |
4096 | 226k | MP4_ChunkDestroy( &p_track->chunk[i_chunk] ); |
4097 | 3.96k | } |
4098 | 5.91k | free( p_track->chunk ); |
4099 | | |
4100 | 5.91k | if( !p_track->i_sample_size ) |
4101 | 3.53k | free( p_track->p_sample_size ); |
4102 | | |
4103 | 5.91k | ASFPacketTrackReset( &p_track->asfinfo ); |
4104 | | |
4105 | 5.91k | free( p_track->context.runs.p_array ); |
4106 | 5.91k | } |
4107 | | |
4108 | | static void MP4_TrackInit( mp4_track_t *p_track, const MP4_Box_t *p_trackbox ) |
4109 | 5.91k | { |
4110 | 5.91k | memset( p_track, 0, sizeof(mp4_track_t) ); |
4111 | 5.91k | es_format_Init( &p_track->fmt, UNKNOWN_ES, 0 ); |
4112 | 5.91k | p_track->i_timescale = 1; |
4113 | 5.91k | p_track->p_track = p_trackbox; |
4114 | 5.91k | const MP4_Box_t *p_tkhd = MP4_BoxGet( p_trackbox, "tkhd" ); |
4115 | 5.91k | if(likely(p_tkhd) && BOXDATA(p_tkhd)) |
4116 | 5.27k | p_track->i_track_ID = BOXDATA(p_tkhd)->i_track_ID; |
4117 | 5.91k | ASFPacketTrackInit( &p_track->asfinfo ); |
4118 | 5.91k | } |
4119 | | |
4120 | | static void MP4_TrackSelect( demux_t *p_demux, mp4_track_t *p_track, bool b_select ) |
4121 | 176M | { |
4122 | 176M | if( !p_track->b_ok || MP4_isMetadata(p_track) ) |
4123 | 49 | return; |
4124 | | |
4125 | 176M | if( b_select == p_track->b_selected ) |
4126 | 0 | return; |
4127 | | |
4128 | 176M | if( !b_select && p_track->p_es ) |
4129 | 176M | { |
4130 | 176M | es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE, |
4131 | 176M | p_track->p_es, false ); |
4132 | 176M | } |
4133 | | |
4134 | 176M | p_track->b_selected = b_select; |
4135 | 176M | } |
4136 | | |
4137 | | static int MP4_TrackSeek( demux_t *p_demux, mp4_track_t *p_track, |
4138 | | vlc_tick_t i_start ) |
4139 | 193M | { |
4140 | 193M | uint32_t i_chunk; |
4141 | 193M | uint32_t i_sample; |
4142 | | |
4143 | 193M | if( !p_track->b_ok || MP4_isMetadata( p_track ) ) |
4144 | 0 | return VLC_EGENERIC; |
4145 | | |
4146 | 193M | p_track->b_selected = false; |
4147 | | |
4148 | 193M | if( TrackTimeToSampleChunk( p_demux, p_track, i_start, |
4149 | 193M | &i_chunk, &i_sample ) ) |
4150 | 17.0M | { |
4151 | 17.0M | msg_Warn( p_demux, "cannot select track[Id 0x%x]", |
4152 | 17.0M | p_track->i_track_ID ); |
4153 | 17.0M | return VLC_EGENERIC; |
4154 | 17.0M | } |
4155 | | |
4156 | 176M | p_track->b_selected = true; |
4157 | 176M | if( !TrackGotoChunkSample( p_demux, p_track, i_chunk, i_sample ) ) |
4158 | 176M | { |
4159 | 176M | p_track->b_selected = true; |
4160 | 176M | TrackUpdateSampleAndTimes( p_track ); |
4161 | 176M | TrackUpdateStarttimes( p_track ); |
4162 | 176M | } |
4163 | | |
4164 | 176M | return p_track->b_selected ? VLC_SUCCESS : VLC_EGENERIC; |
4165 | 193M | } |
4166 | | |
4167 | | |
4168 | | /* |
4169 | | * 3 types: for audio |
4170 | | * |
4171 | | */ |
4172 | | //#define MP4_DEBUG_AUDIO_SAMPLES |
4173 | | static inline uint32_t MP4_GetAudioFrameInfo( const mp4_track_t *p_track, |
4174 | | const MP4_Box_data_sample_soun_t *p_soun, |
4175 | | uint32_t *pi_size ) |
4176 | 349M | { |
4177 | 349M | uint32_t i_samples_per_frame = p_soun->i_sample_per_packet; |
4178 | 349M | uint32_t i_bytes_per_frame = p_soun->i_bytes_per_frame; |
4179 | | |
4180 | 349M | switch( p_track->fmt.i_codec ) |
4181 | 349M | { |
4182 | | /* Quicktime builtin support, _must_ ignore sample tables */ |
4183 | 344M | case VLC_CODEC_GSM: |
4184 | 344M | i_samples_per_frame = 160; |
4185 | 344M | i_bytes_per_frame = 33; |
4186 | 344M | break; |
4187 | 1.73k | case VLC_CODEC_ADPCM_IMA_QT: |
4188 | 1.73k | i_samples_per_frame = 64; |
4189 | 1.73k | i_bytes_per_frame = 34 * p_soun->i_channelcount; |
4190 | 1.73k | break; |
4191 | 4.88k | case VLC_CODEC_MACE3: |
4192 | 4.88k | i_samples_per_frame = 6; |
4193 | 4.88k | i_bytes_per_frame = 2 * p_soun->i_channelcount; |
4194 | 4.88k | break; |
4195 | 9.97k | case VLC_CODEC_MACE6: |
4196 | 9.97k | i_samples_per_frame = 6; |
4197 | 9.97k | i_bytes_per_frame = 1 * p_soun->i_channelcount; |
4198 | 9.97k | break; |
4199 | 1.76k | case VLC_FOURCC( 'u', 'l', 'a', 'w' ): |
4200 | 1.76k | i_samples_per_frame = 1; |
4201 | 1.76k | i_bytes_per_frame = p_soun->i_channelcount; |
4202 | 1.76k | break; |
4203 | 7 | case VLC_CODEC_ALAW: |
4204 | 3.22k | case VLC_CODEC_U8: |
4205 | 3.37k | case VLC_CODEC_S8: |
4206 | 337k | case VLC_CODEC_S16L: |
4207 | 344k | case VLC_CODEC_S16B: |
4208 | 346k | case VLC_CODEC_S24L: |
4209 | 346k | case VLC_CODEC_S24B: |
4210 | 347k | case VLC_CODEC_S32L: |
4211 | 349k | case VLC_CODEC_S32B: |
4212 | 355k | case VLC_CODEC_F32L: |
4213 | 356k | case VLC_CODEC_F32B: |
4214 | 356k | case VLC_CODEC_F64L: |
4215 | 356k | case VLC_CODEC_F64B: |
4216 | 356k | i_samples_per_frame = 1; |
4217 | 356k | i_bytes_per_frame = (aout_BitsPerSample( p_track->fmt.i_codec ) * |
4218 | 356k | p_soun->i_channelcount) >> 3; |
4219 | 356k | assert(i_bytes_per_frame); |
4220 | 356k | break; |
4221 | 356k | case VLC_FOURCC( 'N', 'O', 'N', 'E' ): |
4222 | 0 | case ATOM_twos: |
4223 | 0 | case ATOM_sowt: |
4224 | 0 | case ATOM_raw: |
4225 | 0 | if( p_soun->i_sample_per_packet && p_soun->i_bytes_per_frame ) |
4226 | 0 | { |
4227 | 0 | i_samples_per_frame = p_soun->i_sample_per_packet; |
4228 | 0 | i_bytes_per_frame = p_soun->i_bytes_per_frame; |
4229 | 0 | } |
4230 | 0 | else |
4231 | 0 | { |
4232 | 0 | i_samples_per_frame = 1; |
4233 | 0 | i_bytes_per_frame = (p_soun->i_samplesize+7) >> 3; |
4234 | 0 | i_bytes_per_frame *= p_soun->i_channelcount; |
4235 | 0 | } |
4236 | 0 | break; |
4237 | 5.21M | default: |
4238 | 5.21M | break; |
4239 | 349M | } |
4240 | | |
4241 | | /* Group audio packets so we don't call demux for single sample unit */ |
4242 | 349M | if( p_track->fmt.i_original_fourcc == VLC_CODEC_DVD_LPCM && |
4243 | 217 | p_soun->i_constLPCMframesperaudiopacket && |
4244 | 0 | p_soun->i_constbytesperaudiopacket ) |
4245 | 0 | { |
4246 | 0 | i_samples_per_frame = p_soun->i_constLPCMframesperaudiopacket; |
4247 | 0 | i_bytes_per_frame = p_soun->i_constbytesperaudiopacket; |
4248 | 0 | } |
4249 | | #ifdef MP4_DEBUG_AUDIO_SAMPLES |
4250 | | printf("qtv%d comp%x ss %d chan %d ba %d spp %d bpf %d / %d %d\n", |
4251 | | p_soun->i_qt_version, p_soun->i_compressionid, |
4252 | | p_soun->i_samplesize, p_soun->i_channelcount, |
4253 | | p_track->fmt.audio.i_blockalign, |
4254 | | p_soun->i_sample_per_packet, p_soun->i_bytes_per_frame, |
4255 | | i_samples_per_frame, i_bytes_per_frame); |
4256 | | #endif |
4257 | | |
4258 | 349M | *pi_size = i_bytes_per_frame; |
4259 | 349M | return i_samples_per_frame; |
4260 | 349M | } |
4261 | | |
4262 | | static uint32_t MP4_TrackGetReadSize( mp4_track_t *p_track, uint32_t *pi_nb_samples ) |
4263 | 179M | { |
4264 | 179M | uint32_t i_size = 0; |
4265 | 179M | *pi_nb_samples = 0; |
4266 | | |
4267 | 179M | if ( p_track->i_sample == p_track->i_sample_count ) |
4268 | 0 | return 0; |
4269 | | |
4270 | 179M | if ( p_track->fmt.i_cat != AUDIO_ES ) |
4271 | 4.72M | { |
4272 | 4.72M | *pi_nb_samples = 1; |
4273 | | |
4274 | 4.72M | if( p_track->i_sample_size == 0 ) /* all sizes are different */ |
4275 | 1.84M | return p_track->p_sample_size[p_track->i_sample]; |
4276 | 2.87M | else |
4277 | 2.87M | return p_track->i_sample_size; |
4278 | 4.72M | } |
4279 | 175M | else |
4280 | 175M | { |
4281 | 175M | const MP4_Box_data_sample_soun_t *p_soun = p_track->p_sample->data.p_sample_soun; |
4282 | 175M | const mp4_chunk_t *p_chunk = &p_track->chunk[p_track->i_chunk]; |
4283 | | |
4284 | | /* v0: |
4285 | | * - isobmff: codec is not using v1 fields |
4286 | | * - qt: codec is usually uncompressed and 1 stored sample == 1 byte |
4287 | | * and you need to packetize/group samples. hardcoded codecs. |
4288 | | * if compressed, samplesize == 16 and codec is also hardcoded (ex: adpcm) |
4289 | | * v1: |
4290 | | * - samplesize is meaningless, always 16 |
4291 | | * - compressed frames/packet size parameters applies, but samplesize can |
4292 | | * still be bytes. use packet size to deinterleave, but samples per frames |
4293 | | * are not the number of stored samples. The chunk can also be a packet in some |
4294 | | * (qcelp) cases, in that case we need to return chunk (return 1 stored sample). |
4295 | | * If the codec has -2 compression, this is vbr, we can't deinterleave or group |
4296 | | * samples at all. (return 1 stored sample) |
4297 | | * Again, some hardcoded codecs can exist in qt and we can't trust parameters. |
4298 | | * v2: |
4299 | | * - lpcm extensions provides additional parameters to group samples, but |
4300 | | * 1 stored sample is usually too small in length/bytes, so we need to |
4301 | | * packetize group them. Again, 1 stored sample != 1 audio sample. */ |
4302 | | |
4303 | 175M | if( p_track->fmt.i_original_fourcc == VLC_FOURCC('r','r','t','p') ) |
4304 | 0 | { |
4305 | 0 | *pi_nb_samples = 1; |
4306 | 0 | return p_track->i_sample_size; |
4307 | 0 | } |
4308 | | |
4309 | | /* all samples have a different size */ |
4310 | 175M | if( p_track->i_sample_size == 0 ) |
4311 | 17.6k | { |
4312 | 17.6k | *pi_nb_samples = 1; |
4313 | 17.6k | return p_track->p_sample_size[p_track->i_sample]; |
4314 | 17.6k | } |
4315 | | |
4316 | | /* If we are compressed but not v2 LPCM frames extensions */ |
4317 | 175M | if ( p_soun->i_compressionid == 0xFFFE && p_soun->i_qt_version < 2 ) |
4318 | 42.6k | { |
4319 | 42.6k | *pi_nb_samples = 1; /* != number of audio samples */ |
4320 | 42.6k | if ( p_track->i_sample_size ) |
4321 | 42.6k | return p_track->i_sample_size; |
4322 | 0 | else |
4323 | 0 | return p_track->p_sample_size[p_track->i_sample]; |
4324 | 42.6k | } |
4325 | | |
4326 | | /* More regular V0 cases */ |
4327 | 175M | uint32_t i_max_v0_samples; |
4328 | 175M | switch( p_track->fmt.i_codec ) |
4329 | 175M | { |
4330 | | /* Compressed samples in V0 */ |
4331 | 222 | case VLC_CODEC_AMR_NB: |
4332 | 342 | case VLC_CODEC_AMR_WB: |
4333 | 342 | i_max_v0_samples = 16; |
4334 | 342 | break; |
4335 | 903k | case VLC_CODEC_MPGA: |
4336 | 904k | case VLC_CODEC_MP2: |
4337 | 908k | case VLC_CODEC_MP3: |
4338 | 909k | case VLC_CODEC_DTS: |
4339 | 913k | case VLC_CODEC_MP4A: |
4340 | 917k | case VLC_CODEC_A52: |
4341 | 917k | case VLC_CODEC_OPUS: |
4342 | 917k | i_max_v0_samples = 1; |
4343 | 917k | break; |
4344 | | /* fixme, reverse using a list of uncompressed codecs */ |
4345 | 174M | default: |
4346 | | /* Read 25ms of samples (uncompressed) */ |
4347 | 174M | i_max_v0_samples = p_track->fmt.audio.i_rate / 40 * |
4348 | 174M | p_track->fmt.audio.i_channels; |
4349 | 174M | if( i_max_v0_samples < 1 ) |
4350 | 30.8k | i_max_v0_samples = 1; |
4351 | 174M | break; |
4352 | 175M | } |
4353 | | |
4354 | 175M | *pi_nb_samples = 0; |
4355 | | |
4356 | 175M | if( p_track->i_sample_size > 0 ) |
4357 | 175M | { |
4358 | 175M | uint32_t i_bytes_per_frame; |
4359 | 175M | uint32_t i_samples_per_frame = |
4360 | 175M | MP4_GetAudioFrameInfo( p_track, p_soun, &i_bytes_per_frame ); |
4361 | 175M | uint32_t i_chunk_remaining_samples = p_chunk->i_sample_count - |
4362 | 175M | (p_track->i_sample - p_chunk->i_sample_first); |
4363 | | |
4364 | 175M | if( i_max_v0_samples > i_chunk_remaining_samples ) |
4365 | 1.30M | i_max_v0_samples = i_chunk_remaining_samples; |
4366 | | |
4367 | 175M | if( i_samples_per_frame && i_bytes_per_frame ) |
4368 | 172M | { |
4369 | | /* GSM, ADPCM, */ |
4370 | 172M | if( i_samples_per_frame > 64 && |
4371 | 172M | i_samples_per_frame > i_bytes_per_frame ) |
4372 | 172M | { |
4373 | 172M | *pi_nb_samples = i_samples_per_frame; |
4374 | 172M | i_size = i_bytes_per_frame; |
4375 | 172M | } |
4376 | 185k | else |
4377 | 185k | { |
4378 | | /* Regular cases */ |
4379 | 185k | uint32_t i_frames = __MAX(i_max_v0_samples / i_samples_per_frame, 1); |
4380 | 185k | *pi_nb_samples = i_frames * i_samples_per_frame; |
4381 | 185k | i_size = i_frames * i_bytes_per_frame; |
4382 | 185k | } |
4383 | 172M | } |
4384 | 2.88M | else |
4385 | 2.88M | { |
4386 | 2.88M | *pi_nb_samples = 1; |
4387 | 2.88M | return p_track->i_sample_size; |
4388 | 2.88M | } |
4389 | | #ifdef MP4_DEBUG_AUDIO_SAMPLES |
4390 | | printf("demuxing qtv%d comp %x, ss%d, spf %d bpf %d samples %d maxsamples %d remain samples %d\n", |
4391 | | p_soun->i_qt_version, p_soun->i_compressionid, p_track->i_sample_size, |
4392 | | i_samples_per_frame, i_bytes_per_frame, |
4393 | | *pi_nb_samples, i_max_v0_samples, i_chunk_remaining_samples); |
4394 | | #endif |
4395 | 175M | } |
4396 | 0 | else /* Compressed */ |
4397 | 0 | { |
4398 | 0 | for( uint32_t i=p_track->i_sample; |
4399 | 0 | i<p_chunk->i_sample_first+p_chunk->i_sample_count && |
4400 | 0 | i<p_track->i_sample_count; |
4401 | 0 | i++ ) |
4402 | 0 | { |
4403 | 0 | i_size += p_track->p_sample_size[i]; |
4404 | 0 | (*pi_nb_samples)++; |
4405 | | |
4406 | | /* Try to detect compression in ISO */ |
4407 | 0 | if(p_soun->i_compressionid != 0) |
4408 | 0 | { |
4409 | | /* Return only 1 sample */ |
4410 | 0 | break; |
4411 | 0 | } |
4412 | | |
4413 | 0 | if ( *pi_nb_samples == i_max_v0_samples ) |
4414 | 0 | break; |
4415 | 0 | } |
4416 | 0 | } |
4417 | 175M | } |
4418 | | |
4419 | 172M | return i_size; |
4420 | 179M | } |
4421 | | |
4422 | | static uint64_t MP4_TrackGetPos( mp4_track_t *p_track ) |
4423 | 182M | { |
4424 | 182M | unsigned int i_sample; |
4425 | 182M | uint64_t i_pos; |
4426 | | |
4427 | 182M | i_pos = p_track->chunk[p_track->i_chunk].i_offset; |
4428 | | |
4429 | 182M | if( p_track->i_sample_size ) |
4430 | 180M | { |
4431 | | /* chunk offset in samples */ |
4432 | 180M | uint32_t i_samples = p_track->i_sample - |
4433 | 180M | p_track->chunk[p_track->i_chunk].i_sample_first; |
4434 | | |
4435 | 180M | if( p_track->fmt.i_cat == AUDIO_ES ) |
4436 | 175M | { |
4437 | 175M | MP4_Box_data_sample_soun_t *p_soun = |
4438 | 175M | p_track->p_sample->data.p_sample_soun; |
4439 | | |
4440 | 175M | if( p_soun->i_compressionid != 0xFFFE ) |
4441 | 174M | { |
4442 | 174M | uint32_t i_bytes_per_frame; |
4443 | 174M | uint32_t i_samples_per_frame = MP4_GetAudioFrameInfo( p_track, p_soun, &i_bytes_per_frame ); |
4444 | 174M | if( i_bytes_per_frame && i_samples_per_frame ) |
4445 | 172M | { |
4446 | | /* we read chunk by chunk unless a blockalign is requested */ |
4447 | 172M | return i_pos + i_samples / |
4448 | 172M | i_samples_per_frame * (uint64_t) i_bytes_per_frame; |
4449 | 172M | } |
4450 | 174M | } |
4451 | 175M | } |
4452 | | |
4453 | 7.77M | i_pos += i_samples * (uint64_t) p_track->i_sample_size; |
4454 | 7.77M | } |
4455 | 2.43M | else |
4456 | 2.43M | { |
4457 | 2.43M | for( i_sample = p_track->chunk[p_track->i_chunk].i_sample_first; |
4458 | 37.4M | i_sample < p_track->i_sample; i_sample++ ) |
4459 | 35.0M | { |
4460 | 35.0M | i_pos += p_track->p_sample_size[i_sample]; |
4461 | 35.0M | } |
4462 | 2.43M | } |
4463 | | |
4464 | 10.2M | return i_pos; |
4465 | 182M | } |
4466 | | |
4467 | | |
4468 | | static int MP4_TrackSetNextELST( mp4_track_t *tk ) |
4469 | 248k | { |
4470 | 248k | if( !tk->p_elst || |
4471 | 248k | tk->i_elst == UINT32_MAX - 1 || |
4472 | 248k | tk->i_elst + 1 >= tk->BOXDATA(p_elst)->i_entry_count ) |
4473 | 247k | return VLC_EGENERIC; |
4474 | | |
4475 | 423 | tk->i_elst_time += tk->BOXDATA(p_elst)->entries[tk->i_elst++].i_segment_duration; |
4476 | | |
4477 | 423 | return VLC_SUCCESS; |
4478 | 248k | } |
4479 | | |
4480 | | static int MP4_TrackUpdateELST( mp4_track_t *tk, uint32_t i_movie_timescale, |
4481 | | stime_t i_track_time, uint32_t i_track_sample ) |
4482 | 956k | { |
4483 | 956k | if( !tk->p_elst || tk->BOXDATA(p_elst)->i_entry_count == 0 ) |
4484 | 33.3k | return VLC_SUCCESS; |
4485 | | |
4486 | 922k | if( i_track_sample >= tk->i_sample_count ) |
4487 | 287 | return MP4_TrackSetNextELST( tk ); |
4488 | | |
4489 | 922k | vlc_tick_t i_time = MP4_rescale_mtime( i_track_time, tk->i_timescale ); |
4490 | 922k | for(;;) |
4491 | 922k | { |
4492 | 922k | const MP4_Box_data_elst_entry_t *edit = &tk->BOXDATA(p_elst)->entries[tk->i_elst]; |
4493 | 922k | vlc_tick_t i_end = MP4_rescale_mtime( edit->i_media_time, tk->i_timescale ) + |
4494 | 922k | MP4_rescale_mtime( edit->i_segment_duration, i_movie_timescale ); |
4495 | 922k | if ( i_time < i_end || MP4_TrackSetNextELST( tk ) ) |
4496 | 922k | break; |
4497 | 922k | } |
4498 | | |
4499 | 922k | return VLC_SUCCESS; |
4500 | 922k | } |
4501 | | |
4502 | | static int MP4_TrackNextSample( demux_t *p_demux, mp4_track_t *p_track, uint32_t i_samples ) |
4503 | 3.70M | { |
4504 | 3.70M | demux_sys_t *p_sys = p_demux->p_sys; |
4505 | | |
4506 | 3.70M | if ( UINT32_MAX - p_track->i_sample < i_samples ) |
4507 | 1 | i_samples = UINT32_MAX - p_track->i_sample; /* Ensure we won't overflow next add */ |
4508 | | |
4509 | 3.70M | p_track->i_sample += i_samples; |
4510 | 3.70M | const uint32_t i_previous_chunk = p_track->i_chunk; |
4511 | | |
4512 | 3.70M | if( p_track->i_sample < p_track->i_sample_count ) |
4513 | 3.70M | { |
4514 | | /* Have we changed chunk ? */ |
4515 | 4.21M | while( p_track->i_sample >= |
4516 | 4.21M | p_track->chunk[p_track->i_chunk].i_sample_first + |
4517 | 4.21M | p_track->chunk[p_track->i_chunk].i_sample_count && |
4518 | 516k | p_track->i_chunk < p_track->i_chunk_count - 1 ) |
4519 | 516k | { |
4520 | 516k | p_track->i_chunk++; |
4521 | 516k | } |
4522 | 3.70M | } |
4523 | | |
4524 | 3.70M | if( p_track->p_elst ) /* Update */ |
4525 | 956k | { |
4526 | 956k | uint32_t i_prev_elst = p_track->i_elst; |
4527 | 956k | TrackUpdateSampleAndTimes( p_track ); |
4528 | 956k | MP4_TrackUpdateELST( p_track, p_sys->i_timescale, p_track->i_next_dts, p_track->i_sample); |
4529 | 956k | if( p_track->i_elst != i_prev_elst ) |
4530 | 297 | { |
4531 | 297 | msg_Dbg( p_demux, "changed elst %u -> %u", i_prev_elst, p_track->i_elst); |
4532 | | /* *** find good chunk *** */ |
4533 | 297 | uint32_t i_sample, i_chunk; |
4534 | 297 | stime_t i_start = p_track->BOXDATA(p_elst)->entries[p_track->i_elst].i_media_time; |
4535 | 297 | if( STTSToSampleChunk( p_track, i_start, &i_chunk, &i_sample ) != VLC_SUCCESS ) |
4536 | 12 | { |
4537 | 12 | msg_Warn( p_demux, "track[Id 0x%x] will be disabled " |
4538 | 12 | "(seeking too far) chunk=%"PRIu32" sample=%"PRIu32, |
4539 | 12 | p_track->i_track_ID, i_chunk, i_sample ); |
4540 | 12 | return VLC_EGENERIC; |
4541 | 12 | } |
4542 | 285 | p_track->i_chunk = i_chunk; |
4543 | 285 | p_track->i_sample = i_sample; |
4544 | 285 | } |
4545 | 956k | } |
4546 | | |
4547 | 3.70M | TrackUpdateSampleAndTimes( p_track ); |
4548 | | |
4549 | 3.70M | if( TrackUpdateFormat( p_demux, p_track, i_previous_chunk ) != VLC_SUCCESS ) |
4550 | 49 | { |
4551 | 49 | msg_Warn( p_demux, "track[0x%x] will be disabled " |
4552 | 49 | "(cannot restart decoder)", p_track->i_track_ID ); |
4553 | 49 | MP4_TrackSelect( p_demux, p_track, false ); |
4554 | 49 | return VLC_EGENERIC; |
4555 | 49 | } |
4556 | | |
4557 | 3.70M | if( !p_track->b_selected || p_track->i_sample >= p_track->i_sample_count ) |
4558 | 200 | return VLC_EGENERIC; |
4559 | | |
4560 | 3.70M | return VLC_SUCCESS; |
4561 | 3.70M | } |
4562 | | |
4563 | | static void MP4_TrackSetELST( demux_t *p_demux, mp4_track_t *tk, |
4564 | | vlc_tick_t i_time ) |
4565 | 193M | { |
4566 | 193M | demux_sys_t *p_sys = p_demux->p_sys; |
4567 | 193M | uint32_t i_elst_last = tk->i_elst; |
4568 | | |
4569 | | /* handle elst (find the correct one) */ |
4570 | 193M | tk->i_elst = 0; |
4571 | 193M | tk->i_elst_time = 0; |
4572 | 193M | if( tk->p_elst && tk->BOXDATA(p_elst)->i_entry_count > 0 ) |
4573 | 188M | { |
4574 | 188M | MP4_Box_data_elst_t *elst = tk->BOXDATA(p_elst); |
4575 | 188M | int64_t i_mvt= MP4_rescale_qtime( i_time, p_sys->i_timescale ); |
4576 | | |
4577 | 375M | for( tk->i_elst = 0; tk->i_elst < elst->i_entry_count; tk->i_elst++ ) |
4578 | 188M | { |
4579 | 188M | uint64_t i_dur = elst->entries[tk->i_elst].i_segment_duration; |
4580 | | |
4581 | 188M | if( elst->entries[tk->i_elst].i_media_time >=0 && |
4582 | 188M | tk->i_elst_time <= i_mvt && |
4583 | 188M | i_mvt < (int64_t)(tk->i_elst_time + i_dur) ) |
4584 | 744k | { |
4585 | 744k | break; |
4586 | 744k | } |
4587 | 187M | tk->i_elst_time += i_dur; |
4588 | 187M | } |
4589 | | |
4590 | 188M | if( tk->i_elst >= elst->i_entry_count ) |
4591 | 187M | { |
4592 | | /* msg_Dbg( p_demux, "invalid number of entry in elst" ); */ |
4593 | 187M | tk->i_elst = elst->i_entry_count - 1; |
4594 | 187M | tk->i_elst_time -= elst->entries[tk->i_elst].i_segment_duration; |
4595 | 187M | } |
4596 | | |
4597 | 188M | if( i_elst_last != tk->i_elst ) |
4598 | 43 | { |
4599 | 43 | msg_Warn( p_demux, "elst old=%d new=%"PRIu32, i_elst_last, tk->i_elst ); |
4600 | 43 | if( i_elst_last < elst->i_entry_count && |
4601 | 43 | elst->entries[i_elst_last].i_media_time >= 0 ) |
4602 | 1 | tk->i_next_block_flags |= BLOCK_FLAG_DISCONTINUITY; |
4603 | 43 | } |
4604 | 188M | } |
4605 | 193M | } |
4606 | | |
4607 | | /****************************************************************************** |
4608 | | * Here are the functions used for fragmented MP4 |
4609 | | *****************************************************************************/ |
4610 | | #if 0 |
4611 | | /** |
4612 | | * Re-init decoder. |
4613 | | * \Note If we call that function too soon, |
4614 | | * before the track has been selected by MP4_TrackSelect |
4615 | | * (during the first execution of Demux), then the track gets disabled |
4616 | | */ |
4617 | | static int ReInitDecoder( demux_t *p_demux, const MP4_Box_t *p_root, |
4618 | | mp4_track_t *p_track ) |
4619 | | { |
4620 | | MP4_Box_t *p_paramsbox = MP4_BoxGet( p_root, "/moov/trak[0]" ); |
4621 | | if( !p_paramsbox ) |
4622 | | return VLC_EGENERIC; |
4623 | | |
4624 | | MP4_TrackRestart( p_demux, p_track, p_paramsbox ); |
4625 | | |
4626 | | /* Temporary hack until we support track selection */ |
4627 | | p_track->b_selected = true; |
4628 | | p_track->b_enable = true; |
4629 | | |
4630 | | return VLC_SUCCESS; |
4631 | | } |
4632 | | #endif |
4633 | | |
4634 | | static stime_t GetCumulatedDuration( demux_t *p_demux ) |
4635 | 273 | { |
4636 | 273 | demux_sys_t *p_sys = p_demux->p_sys; |
4637 | 273 | stime_t i_max_duration = 0; |
4638 | | |
4639 | 768 | for ( unsigned int i=0; i<p_sys->i_tracks; i++ ) |
4640 | 495 | { |
4641 | 495 | MP4_Box_t *p_trak = MP4_GetTrakByTrackID( p_sys->p_moov, p_sys->track[i].i_track_ID ); |
4642 | 495 | const MP4_Box_t *p_stsz; |
4643 | 495 | const MP4_Box_t *p_tkhd; |
4644 | 495 | if ( (p_tkhd = MP4_BoxGet( p_trak, "tkhd" )) && |
4645 | 156 | (p_stsz = MP4_BoxGet( p_trak, "mdia/minf/stbl/stsz" )) && |
4646 | | /* duration might be wrong an be set to whole duration :/ */ |
4647 | 87 | BOXDATA(p_stsz)->i_sample_count > 0 ) |
4648 | 27 | { |
4649 | 27 | i_max_duration = __MAX( (uint64_t)i_max_duration, BOXDATA(p_tkhd)->i_duration ); |
4650 | 27 | } |
4651 | 495 | } |
4652 | 273 | if( p_sys->p_fragsindex ) |
4653 | 221 | { |
4654 | 221 | stime_t i_tracks_duration = MP4_Fragment_Index_GetTracksDuration( p_sys->p_fragsindex ); |
4655 | 221 | i_max_duration = __MAX( i_max_duration, i_tracks_duration ); |
4656 | 221 | } |
4657 | | |
4658 | 273 | return i_max_duration; |
4659 | 273 | } |
4660 | | |
4661 | | static int ProbeIndex( demux_t *p_demux ) |
4662 | 0 | { |
4663 | 0 | demux_sys_t *p_sys = p_demux->p_sys; |
4664 | 0 | uint64_t i_stream_size; |
4665 | 0 | uint8_t mfro[MP4_MFRO_BOXSIZE]; |
4666 | 0 | assert( p_sys->b_seekable ); |
4667 | |
|
4668 | 0 | if ( MP4_BoxCount( p_sys->p_root, "/mfra" ) ) |
4669 | 0 | return VLC_EGENERIC; |
4670 | | |
4671 | 0 | if ( vlc_stream_GetSize( p_demux->s, &i_stream_size ) != VLC_SUCCESS ) |
4672 | 0 | return VLC_EGENERIC; |
4673 | | |
4674 | 0 | if ( i_stream_size < MP4_MFRO_BOXSIZE || |
4675 | 0 | vlc_stream_Seek( p_demux->s, i_stream_size - MP4_MFRO_BOXSIZE ) != VLC_SUCCESS ) |
4676 | 0 | { |
4677 | 0 | msg_Dbg( p_demux, "Probing tail for mfro has failed" ); |
4678 | 0 | return VLC_EGENERIC; |
4679 | 0 | } |
4680 | | |
4681 | 0 | if ( vlc_stream_Read( p_demux->s, &mfro, MP4_MFRO_BOXSIZE ) == MP4_MFRO_BOXSIZE && |
4682 | 0 | VLC_FOURCC(mfro[4],mfro[5],mfro[6],mfro[7]) == ATOM_mfro && |
4683 | 0 | GetDWBE( &mfro ) == MP4_MFRO_BOXSIZE ) |
4684 | 0 | { |
4685 | 0 | uint32_t i_offset = GetDWBE( &mfro[12] ); |
4686 | 0 | msg_Dbg( p_demux, "will read mfra index at %"PRIu64, i_stream_size - i_offset ); |
4687 | 0 | if ( i_stream_size > i_offset && |
4688 | 0 | vlc_stream_Seek( p_demux->s, i_stream_size - i_offset ) == VLC_SUCCESS ) |
4689 | 0 | { |
4690 | 0 | msg_Dbg( p_demux, "reading mfra index at %"PRIu64, i_stream_size - i_offset ); |
4691 | 0 | const uint32_t stoplist[] = { ATOM_mfra, 0 }; |
4692 | 0 | MP4_ReadBoxContainerChildren( p_demux->s, p_sys->p_root, stoplist ); |
4693 | 0 | } |
4694 | 0 | return VLC_SUCCESS; |
4695 | 0 | } |
4696 | 0 | return VLC_EGENERIC; |
4697 | 0 | } |
4698 | | |
4699 | | static stime_t GetMoovTrackDuration( demux_sys_t *p_sys, unsigned i_track_ID ) |
4700 | 755 | { |
4701 | 755 | MP4_Box_t *p_trak = MP4_GetTrakByTrackID( p_sys->p_moov, i_track_ID ); |
4702 | 755 | const MP4_Box_t *p_stsz; |
4703 | 755 | const MP4_Box_t *p_tkhd; |
4704 | 755 | if ( (p_tkhd = MP4_BoxGet( p_trak, "tkhd" )) && |
4705 | 450 | (p_stsz = MP4_BoxGet( p_trak, "mdia/minf/stbl/stsz" )) && |
4706 | | /* duration might be wrong an be set to whole duration :/ */ |
4707 | 345 | BOXDATA(p_stsz)->i_sample_count > 0 ) |
4708 | 23 | { |
4709 | 23 | if( BOXDATA(p_tkhd)->i_duration <= p_sys->i_moov_duration ) |
4710 | 15 | return BOXDATA(p_tkhd)->i_duration; /* In movie / mvhd scale */ |
4711 | 8 | else |
4712 | 8 | return p_sys->i_moov_duration; |
4713 | 23 | } |
4714 | 732 | return 0; |
4715 | 755 | } |
4716 | | |
4717 | | static bool GetMoofTrackDuration( MP4_Box_t *p_moov, MP4_Box_t *p_moof, |
4718 | | unsigned i_track_ID, stime_t *p_duration ) |
4719 | 2.89k | { |
4720 | 2.89k | if ( !p_moof || !p_moov ) |
4721 | 0 | return false; |
4722 | | |
4723 | 2.89k | MP4_Box_t *p_traf = MP4_BoxGet( p_moof, "traf" ); |
4724 | 3.49k | while ( p_traf ) |
4725 | 984 | { |
4726 | 984 | if ( p_traf->i_type != ATOM_traf ) |
4727 | 153 | { |
4728 | 153 | p_traf = p_traf->p_next; |
4729 | 153 | continue; |
4730 | 153 | } |
4731 | | |
4732 | 831 | const MP4_Box_t *p_tfhd = MP4_BoxGet( p_traf, "tfhd" ); |
4733 | 831 | const MP4_Box_t *p_trun = MP4_BoxGet( p_traf, "trun" ); |
4734 | 831 | if ( !p_tfhd || !p_trun || i_track_ID != BOXDATA(p_tfhd)->i_track_ID ) |
4735 | 362 | { |
4736 | 362 | p_traf = p_traf->p_next; |
4737 | 362 | continue; |
4738 | 362 | } |
4739 | | |
4740 | 469 | uint32_t i_track_timescale = 0; |
4741 | 469 | uint32_t i_track_defaultsampleduration = 0; |
4742 | | |
4743 | | /* set trex for defaults */ |
4744 | 469 | MP4_Box_t *p_trex = MP4_GetTrexByTrackID( p_moov, BOXDATA(p_tfhd)->i_track_ID ); |
4745 | 469 | if ( p_trex ) |
4746 | 377 | { |
4747 | 377 | i_track_defaultsampleduration = BOXDATA(p_trex)->i_default_sample_duration; |
4748 | 377 | } |
4749 | | |
4750 | 469 | MP4_Box_t *p_trak = MP4_GetTrakByTrackID( p_moov, BOXDATA(p_tfhd)->i_track_ID ); |
4751 | 469 | if ( p_trak ) |
4752 | 469 | { |
4753 | 469 | MP4_Box_t *p_mdhd = MP4_BoxGet( p_trak, "mdia/mdhd" ); |
4754 | 469 | if ( p_mdhd ) |
4755 | 381 | i_track_timescale = BOXDATA(p_mdhd)->i_timescale; |
4756 | 469 | } |
4757 | | |
4758 | 469 | if ( !i_track_timescale ) |
4759 | 88 | { |
4760 | 88 | p_traf = p_traf->p_next; |
4761 | 88 | continue; |
4762 | 88 | } |
4763 | | |
4764 | 381 | uint64_t i_traf_duration = 0; |
4765 | 943 | while ( p_trun && p_tfhd ) |
4766 | 562 | { |
4767 | 562 | if ( p_trun->i_type != ATOM_trun ) |
4768 | 180 | { |
4769 | 180 | p_trun = p_trun->p_next; |
4770 | 180 | continue; |
4771 | 180 | } |
4772 | 382 | const MP4_Box_data_trun_t *p_trundata = p_trun->data.p_trun; |
4773 | | |
4774 | | /* Sum total time */ |
4775 | 382 | if ( p_trundata->i_flags & MP4_TRUN_SAMPLE_DURATION ) |
4776 | 374 | { |
4777 | 4.23k | for( uint32_t i=0; i< p_trundata->i_sample_count; i++ ) |
4778 | 3.86k | i_traf_duration += p_trundata->p_samples[i].i_duration; |
4779 | 374 | } |
4780 | 8 | else if ( BOXDATA(p_tfhd)->i_flags & MP4_TFHD_DFLT_SAMPLE_DURATION ) |
4781 | 3 | { |
4782 | 3 | i_traf_duration += (uint64_t)p_trundata->i_sample_count * |
4783 | 3 | BOXDATA(p_tfhd)->i_default_sample_duration; |
4784 | 3 | } |
4785 | 5 | else |
4786 | 5 | { |
4787 | 5 | i_traf_duration += (uint64_t)p_trundata->i_sample_count * |
4788 | 5 | i_track_defaultsampleduration; |
4789 | 5 | } |
4790 | | |
4791 | 382 | p_trun = p_trun->p_next; |
4792 | 382 | } |
4793 | | |
4794 | 381 | *p_duration = i_traf_duration; |
4795 | 381 | return true; |
4796 | 469 | } |
4797 | | |
4798 | 2.51k | return false; |
4799 | 2.89k | } |
4800 | | |
4801 | | static int ProbeFragments( demux_t *p_demux, bool b_force, bool *pb_fragmented ) |
4802 | 472 | { |
4803 | 472 | demux_sys_t *p_sys = p_demux->p_sys; |
4804 | | |
4805 | 472 | msg_Dbg( p_demux, "probing fragments from %"PRId64, vlc_stream_Tell( p_demux->s ) ); |
4806 | | |
4807 | 472 | assert( p_sys->p_root ); |
4808 | | |
4809 | 472 | MP4_Box_t *p_vroot = MP4_BoxNew(ATOM_root); |
4810 | 472 | if( !p_vroot ) |
4811 | 0 | return VLC_EGENERIC; |
4812 | | |
4813 | 472 | if( p_sys->b_seekable && (p_sys->b_fastseekable || b_force) ) |
4814 | 472 | { |
4815 | 472 | MP4_ReadBoxContainerChildren( p_demux->s, p_vroot, NULL ); /* Get the rest of the file */ |
4816 | 472 | p_sys->b_fragments_probed = true; |
4817 | | |
4818 | 472 | const unsigned i_moof = MP4_BoxCount( p_vroot, "/moof" ); |
4819 | 472 | if( i_moof ) |
4820 | 409 | { |
4821 | 409 | *pb_fragmented = true; |
4822 | 409 | p_sys->p_fragsindex = MP4_Fragments_Index_New( p_sys->i_tracks, i_moof ); |
4823 | 409 | if( !p_sys->p_fragsindex ) |
4824 | 0 | { |
4825 | 0 | MP4_BoxFree( p_vroot ); |
4826 | 0 | return VLC_EGENERIC; |
4827 | 0 | } |
4828 | | |
4829 | 409 | stime_t *pi_track_times = calloc( p_sys->i_tracks, sizeof(*pi_track_times) ); |
4830 | 409 | if( !pi_track_times ) |
4831 | 0 | { |
4832 | 0 | MP4_Fragments_Index_Delete( p_sys->p_fragsindex ); |
4833 | 0 | p_sys->p_fragsindex = NULL; |
4834 | 0 | MP4_BoxFree( p_vroot ); |
4835 | 0 | return VLC_EGENERIC; |
4836 | 0 | } |
4837 | | |
4838 | 409 | unsigned index = 0; |
4839 | | |
4840 | 7.09k | for( MP4_Box_t *p_moof = p_vroot->p_first; p_moof; p_moof = p_moof->p_next ) |
4841 | 6.69k | { |
4842 | 6.69k | if( p_moof->i_type != ATOM_moof ) |
4843 | 5.16k | continue; |
4844 | | |
4845 | 4.42k | for( unsigned i=0; i<p_sys->i_tracks; i++ ) |
4846 | 2.89k | { |
4847 | 2.89k | MP4_Box_t *p_tfdt = NULL; |
4848 | 2.89k | MP4_Box_t *p_traf = MP4_GetTrafByTrackID( p_moof, p_sys->track[i].i_track_ID ); |
4849 | 2.89k | if( p_traf ) |
4850 | 517 | p_tfdt = MP4_BoxGet( p_traf, "tfdt" ); |
4851 | | |
4852 | 2.89k | if( p_tfdt && BOXDATA(p_tfdt) ) |
4853 | 4 | { |
4854 | 4 | pi_track_times[i] = p_tfdt->data.p_tfdt->i_base_media_decode_time; |
4855 | 4 | } |
4856 | 2.88k | else if( index == 0 ) /* Set first fragment time offset from moov */ |
4857 | 735 | { |
4858 | 735 | stime_t i_duration = GetMoovTrackDuration( p_sys, p_sys->track[i].i_track_ID ); |
4859 | 735 | pi_track_times[i] = MP4_rescale( i_duration, p_sys->i_timescale, p_sys->track[i].i_timescale ); |
4860 | 735 | } |
4861 | | |
4862 | 2.89k | stime_t i_movietime = MP4_rescale( pi_track_times[i], p_sys->track[i].i_timescale, p_sys->i_timescale ); |
4863 | 2.89k | p_sys->p_fragsindex->p_times[index * p_sys->i_tracks + i] = i_movietime; |
4864 | | |
4865 | 2.89k | stime_t i_duration = 0; |
4866 | 2.89k | if( GetMoofTrackDuration( p_sys->p_moov, p_moof, p_sys->track[i].i_track_ID, &i_duration ) ) |
4867 | 381 | pi_track_times[i] += i_duration; |
4868 | 2.89k | } |
4869 | | |
4870 | 1.53k | p_sys->p_fragsindex->pi_pos[index++] = p_moof->i_pos; |
4871 | 1.53k | } |
4872 | | |
4873 | 1.14k | for( unsigned i=0; i<p_sys->i_tracks; i++ ) |
4874 | 739 | { |
4875 | 739 | stime_t i_movietime = MP4_rescale( pi_track_times[i], p_sys->track[i].i_timescale, p_sys->i_timescale ); |
4876 | 739 | if( p_sys->p_fragsindex->i_last_time < i_movietime ) |
4877 | 130 | p_sys->p_fragsindex->i_last_time = i_movietime; |
4878 | 739 | } |
4879 | | |
4880 | 409 | free( pi_track_times ); |
4881 | 409 | #ifdef MP4_VERBOSE |
4882 | 409 | MP4_Fragments_Index_Dump( VLC_OBJECT(p_demux), p_sys->p_fragsindex, p_sys->i_timescale ); |
4883 | 409 | #endif |
4884 | 409 | } |
4885 | 472 | } |
4886 | 0 | else |
4887 | 0 | { |
4888 | | /* We stop at first moof, which validates our fragmentation condition |
4889 | | * and we'll find others while reading. */ |
4890 | 0 | const uint32_t excllist[] = { ATOM_moof, 0 }; |
4891 | 0 | MP4_ReadBoxContainerRestricted( p_demux->s, p_vroot, NULL, excllist ); |
4892 | | /* Peek since we stopped before restriction */ |
4893 | 0 | const uint8_t *p_peek; |
4894 | 0 | if ( vlc_stream_Peek( p_demux->s, &p_peek, 8 ) == 8 ) |
4895 | 0 | *pb_fragmented = (VLC_FOURCC( p_peek[4], p_peek[5], p_peek[6], p_peek[7] ) == ATOM_moof); |
4896 | 0 | else |
4897 | 0 | *pb_fragmented = false; |
4898 | 0 | } |
4899 | | |
4900 | 472 | MP4_BoxFree( p_vroot ); |
4901 | | |
4902 | 472 | MP4_Box_t *p_mehd = MP4_BoxGet( p_sys->p_moov, "mvex/mehd"); |
4903 | 472 | if ( !p_mehd ) |
4904 | 273 | p_sys->i_cumulated_duration = GetCumulatedDuration( p_demux ); |
4905 | | |
4906 | 472 | return VLC_SUCCESS; |
4907 | 472 | } |
4908 | | |
4909 | | static int ProbeFragmentsChecked( demux_t *p_demux ) |
4910 | 0 | { |
4911 | 0 | demux_sys_t *p_sys = p_demux->p_sys; |
4912 | |
|
4913 | 0 | if( p_sys->b_fragments_probed ) |
4914 | 0 | return VLC_SUCCESS; |
4915 | | |
4916 | 0 | if( !p_sys->b_fastseekable ) |
4917 | 0 | { |
4918 | 0 | const char *psz_msg = _( |
4919 | 0 | "Because this file index is broken or missing, " |
4920 | 0 | "seeking will not work correctly.\n" |
4921 | 0 | "VLC won't repair your file but can temporary fix this " |
4922 | 0 | "problem by building an index in memory.\n" |
4923 | 0 | "This step might take a long time on a large file.\n" |
4924 | 0 | "What do you want to do?"); |
4925 | 0 | bool b_continue = vlc_dialog_wait_question( p_demux, |
4926 | 0 | VLC_DIALOG_QUESTION_NORMAL, |
4927 | 0 | _("Do not seek"), |
4928 | 0 | _("Build index"), |
4929 | 0 | NULL, |
4930 | 0 | _("Broken or missing Index"), |
4931 | 0 | "%s", psz_msg ); |
4932 | 0 | if( !b_continue ) |
4933 | 0 | return VLC_EGENERIC; |
4934 | 0 | } |
4935 | | |
4936 | 0 | const uint64_t i_backup_pos = vlc_stream_Tell( p_demux->s ); |
4937 | 0 | int i_ret = vlc_stream_Seek( p_demux->s, p_sys->p_moov->i_pos + p_sys->p_moov->i_size ); |
4938 | 0 | if( i_ret == VLC_SUCCESS ) |
4939 | 0 | { |
4940 | 0 | bool foo; |
4941 | 0 | i_ret = ProbeFragments( p_demux, true, &foo ); |
4942 | 0 | p_sys->b_fragments_probed = true; |
4943 | 0 | } |
4944 | |
|
4945 | 0 | if( i_ret != VLC_SUCCESS ) |
4946 | 0 | p_sys->b_error = (vlc_stream_Seek( p_demux->s, i_backup_pos ) != VLC_SUCCESS); |
4947 | |
|
4948 | 0 | return i_ret; |
4949 | 0 | } |
4950 | | |
4951 | | static void FragResetContext( demux_sys_t *p_sys ) |
4952 | 9.27k | { |
4953 | 9.27k | if( p_sys->context.p_fragment_atom ) |
4954 | 1.01k | { |
4955 | 1.01k | if( p_sys->context.p_fragment_atom != p_sys->p_moov ) |
4956 | 639 | MP4_BoxFree( p_sys->context.p_fragment_atom ); |
4957 | 1.01k | p_sys->context.p_fragment_atom = NULL; |
4958 | 1.01k | } |
4959 | 9.27k | p_sys->context.i_current_box_type = 0; |
4960 | | |
4961 | 16.8k | for ( uint32_t i=0; i<p_sys->i_tracks; i++ ) |
4962 | 7.61k | { |
4963 | 7.61k | mp4_track_t *p_track = &p_sys->track[i]; |
4964 | 7.61k | p_track->context.i_default_sample_size = 0; |
4965 | 7.61k | p_track->context.i_default_sample_duration = 0; |
4966 | 7.61k | } |
4967 | 9.27k | } |
4968 | | |
4969 | | static int FragDemuxTrack( demux_t *p_demux, mp4_track_t *p_track, |
4970 | | vlc_tick_t i_max_preload ) |
4971 | 3.47k | { |
4972 | 3.47k | if( !p_track->b_ok || |
4973 | 3.47k | p_track->context.runs.i_current >= p_track->context.runs.i_count ) |
4974 | 0 | return VLC_DEMUXER_EOS; |
4975 | | |
4976 | 3.47k | const MP4_Box_data_trun_t *p_trun = |
4977 | 3.47k | p_track->context.runs.p_array[p_track->context.runs.i_current].p_trun->data.p_trun; |
4978 | | |
4979 | 3.47k | uint32_t dur = p_track->context.i_default_sample_duration, |
4980 | 3.47k | len = p_track->context.i_default_sample_size; |
4981 | | |
4982 | 3.47k | if( vlc_stream_Tell(p_demux->s) != p_track->context.i_trun_sample_pos && |
4983 | 272 | MP4_Seek( p_demux->s, p_track->context.i_trun_sample_pos ) != VLC_SUCCESS ) |
4984 | 0 | return VLC_DEMUXER_EOF; |
4985 | | |
4986 | 3.47k | const stime_t i_demux_max_dts = (i_max_preload < INVALID_PRELOAD) ? |
4987 | 3.47k | p_track->i_time + MP4_rescale_qtime( i_max_preload, p_track->i_timescale ) : |
4988 | 3.47k | VLC_TICK_MAX; |
4989 | | |
4990 | 6.89k | for( uint32_t i = p_track->context.i_trun_sample; i < p_trun->i_sample_count; i++ ) |
4991 | 6.74k | { |
4992 | 6.74k | const stime_t i_dts = p_track->i_time; |
4993 | 6.74k | stime_t i_pts = i_dts; |
4994 | | |
4995 | 6.74k | if( p_trun->i_flags & MP4_TRUN_SAMPLE_DURATION ) |
4996 | 3.27k | dur = p_trun->p_samples[i].i_duration; |
4997 | | |
4998 | 6.74k | if( i_dts > i_demux_max_dts ) |
4999 | 3.20k | return VLC_DEMUXER_SUCCESS; |
5000 | | |
5001 | 3.53k | p_track->i_time += dur; |
5002 | 3.53k | p_track->context.i_trun_sample = i + 1; |
5003 | | |
5004 | 3.53k | if( p_trun->i_flags & MP4_TRUN_SAMPLE_TIME_OFFSET ) |
5005 | 1.36k | { |
5006 | 1.36k | if ( p_trun->i_version == 1 ) |
5007 | 0 | i_pts += p_trun->p_samples[i].i_composition_time_offset.v1; |
5008 | 1.36k | else if( p_trun->p_samples[i].i_composition_time_offset.v0 < 0xFF000000 ) |
5009 | 1.36k | i_pts += p_trun->p_samples[i].i_composition_time_offset.v0; |
5010 | 3 | else /* version 0 with negative */ |
5011 | 3 | i_pts += p_trun->p_samples[i].i_composition_time_offset.v1; |
5012 | 1.36k | } |
5013 | | |
5014 | 3.53k | if( p_trun->i_flags & MP4_TRUN_SAMPLE_SIZE ) |
5015 | 3.05k | len = p_trun->p_samples[i].i_size; |
5016 | | |
5017 | 3.53k | if( !dur ) |
5018 | 3.53k | msg_Warn(p_demux, "Zero duration sample in trun."); |
5019 | | |
5020 | 3.53k | if( !len ) |
5021 | 3.53k | msg_Warn(p_demux, "Zero length sample in trun."); |
5022 | | |
5023 | 3.53k | len = OverflowCheck( p_demux, p_track, vlc_stream_Tell(p_demux->s), len ); |
5024 | | |
5025 | 3.53k | block_t *p_block = vlc_stream_Block( p_demux->s, len ); |
5026 | 3.53k | uint32_t i_read = ( p_block ) ? p_block->i_buffer : 0; |
5027 | 3.53k | p_track->context.i_trun_sample_pos += i_read; |
5028 | 3.53k | if( i_read < len || p_block == NULL ) |
5029 | 114 | { |
5030 | 114 | if( p_block ) |
5031 | 101 | block_Release( p_block ); |
5032 | 114 | return VLC_DEMUXER_FATAL; |
5033 | 114 | } |
5034 | | |
5035 | | #if 0 |
5036 | | msg_Dbg( p_demux, "tk(%i)=%"PRId64" mv=%"PRId64" pos=%"PRIu64, p_track->i_track_ID, |
5037 | | VLC_TICK_0 + MP4_rescale_mtime( i_dts, p_track->i_timescale ), |
5038 | | VLC_TICK_0 + MP4_rescale_mtime( i_pts, p_track->i_timescale ), |
5039 | | p_track->context.i_trun_sample_pos - i_read ); |
5040 | | #endif |
5041 | 3.42k | if ( p_track->p_es ) |
5042 | 3.42k | { |
5043 | 3.42k | p_block->i_dts = VLC_TICK_0 + MP4_rescale_mtime( i_dts, p_track->i_timescale ); |
5044 | 3.42k | if( p_track->fmt.i_cat == VIDEO_ES && !( p_trun->i_flags & MP4_TRUN_SAMPLE_TIME_OFFSET ) ) |
5045 | 1.66k | p_block->i_pts = VLC_TICK_INVALID; |
5046 | 1.75k | else |
5047 | 1.75k | p_block->i_pts = VLC_TICK_0 + MP4_rescale_mtime( i_pts, p_track->i_timescale ); |
5048 | 3.42k | p_block->i_length = MP4_rescale_mtime( dur, p_track->i_timescale ); |
5049 | 3.42k | MP4_Block_Send( p_demux, p_track, p_block ); |
5050 | 3.42k | } |
5051 | 0 | else block_Release( p_block ); |
5052 | 3.42k | } |
5053 | | |
5054 | 158 | if( p_track->context.i_trun_sample == p_trun->i_sample_count ) |
5055 | 158 | { |
5056 | 158 | p_track->context.i_trun_sample = 0; |
5057 | 158 | if( ++p_track->context.runs.i_current < p_track->context.runs.i_count ) |
5058 | 0 | { |
5059 | 0 | p_track->i_time = p_track->context.runs.p_array[p_track->context.runs.i_current].i_first_dts; |
5060 | 0 | p_track->context.i_trun_sample_pos = p_track->context.runs.p_array[p_track->context.runs.i_current].i_offset; |
5061 | 0 | } |
5062 | 158 | } |
5063 | | |
5064 | 158 | return VLC_DEMUXER_SUCCESS; |
5065 | 3.47k | } |
5066 | | |
5067 | | static int DemuxMoof( demux_t *p_demux ) |
5068 | 14.0M | { |
5069 | 14.0M | demux_sys_t *p_sys = p_demux->p_sys; |
5070 | 14.0M | int i_status; |
5071 | | |
5072 | 14.0M | const vlc_tick_t i_max_preload = ( p_sys->b_fastseekable ) ? 0 : ( p_sys->b_seekable ) ? DEMUX_TRACK_MAX_PRELOAD : INVALID_PRELOAD; |
5073 | | |
5074 | 14.0M | const vlc_tick_t i_nztime = MP4_GetMoviePTS( p_sys ); |
5075 | | |
5076 | | /* !important! Ensure clock is set before sending data */ |
5077 | 14.0M | if( p_sys->i_pcr == VLC_TICK_INVALID ) |
5078 | 201 | es_out_SetPCR( p_demux->out, VLC_TICK_0 + i_nztime ); |
5079 | | |
5080 | | /* Set per track read state */ |
5081 | 28.0M | for( unsigned i = 0; i < p_sys->i_tracks; i++ ) |
5082 | 14.0M | p_sys->track[i].context.i_temp = VLC_DEMUXER_SUCCESS; |
5083 | | |
5084 | | /* demux up to increment amount of data on every track, or just set pcr if empty data */ |
5085 | 14.0M | for( ;; ) |
5086 | 14.0M | { |
5087 | 14.0M | mp4_track_t *tk = NULL; |
5088 | 14.0M | i_status = VLC_DEMUXER_EOS; |
5089 | | |
5090 | | /* First pass, find any track within our target increment, ordered by position */ |
5091 | 28.0M | for( unsigned i = 0; i < p_sys->i_tracks; i++ ) |
5092 | 14.0M | { |
5093 | 14.0M | mp4_track_t *tk_tmp = &p_sys->track[i]; |
5094 | | |
5095 | 14.0M | if( !tk_tmp->b_ok || MP4_isMetadata( tk_tmp ) || |
5096 | 14.0M | (!tk_tmp->b_selected && p_sys->b_seekable) || |
5097 | 14.0M | tk_tmp->context.runs.i_current >= tk_tmp->context.runs.i_count || |
5098 | 14.0M | tk_tmp->context.i_temp != VLC_DEMUXER_SUCCESS ) |
5099 | 38.7k | continue; |
5100 | | |
5101 | | /* At least still have data to demux on this or next turns */ |
5102 | 14.0M | i_status = VLC_DEMUXER_SUCCESS; |
5103 | | |
5104 | 14.0M | if( MP4_rescale_mtime( tk_tmp->i_time, tk_tmp->i_timescale ) <= i_nztime + DEMUX_INCREMENT ) |
5105 | 3.47k | { |
5106 | 3.47k | if( tk == NULL || tk_tmp->context.i_trun_sample_pos < tk->context.i_trun_sample_pos ) |
5107 | 3.47k | tk = tk_tmp; |
5108 | 3.47k | } |
5109 | 14.0M | } |
5110 | | |
5111 | 14.0M | if( tk ) |
5112 | 3.47k | { |
5113 | | /* Second pass, refine and find any best candidate having a chunk pos closer than |
5114 | | * current candidate (avoids seeks when increment falls between the 2) from |
5115 | | * current position, but within extended interleave time */ |
5116 | 3.47k | for( unsigned i = 0; i_max_preload != 0 && i < p_sys->i_tracks; i++ ) |
5117 | 0 | { |
5118 | 0 | mp4_track_t *tk_tmp = &p_sys->track[i]; |
5119 | 0 | if( tk_tmp == tk || |
5120 | 0 | !tk_tmp->b_ok || MP4_isMetadata( tk_tmp ) || |
5121 | 0 | (!tk_tmp->b_selected && p_sys->b_seekable) || |
5122 | 0 | tk_tmp->context.runs.i_current >= tk_tmp->context.runs.i_count || |
5123 | 0 | tk_tmp->context.i_temp != VLC_DEMUXER_SUCCESS ) |
5124 | 0 | continue; |
5125 | | |
5126 | 0 | vlc_tick_t i_nzdts = MP4_rescale_mtime( tk_tmp->i_time, tk_tmp->i_timescale ); |
5127 | 0 | if ( i_nzdts <= i_nztime + DEMUX_TRACK_MAX_PRELOAD ) |
5128 | 0 | { |
5129 | | /* Found a better candidate to avoid seeking */ |
5130 | 0 | if( tk_tmp->context.i_trun_sample_pos < tk->context.i_trun_sample_pos ) |
5131 | 0 | tk = tk_tmp; |
5132 | | /* Note: previous candidate will be repicked on next loop */ |
5133 | 0 | } |
5134 | 0 | } |
5135 | | |
5136 | 3.47k | int i_ret = FragDemuxTrack( p_demux, tk, i_max_preload ); |
5137 | | |
5138 | 3.47k | tk->context.i_temp = i_ret; |
5139 | 3.47k | if( i_ret == VLC_DEMUXER_SUCCESS ) |
5140 | 3.36k | i_status = VLC_DEMUXER_SUCCESS; |
5141 | 114 | else if( i_ret == VLC_DEMUXER_FATAL ) |
5142 | 114 | i_status = VLC_DEMUXER_EOF; |
5143 | 3.47k | } |
5144 | | |
5145 | 14.0M | if( i_status != VLC_DEMUXER_SUCCESS || !tk ) |
5146 | 14.0M | break; |
5147 | 14.0M | } |
5148 | | |
5149 | 14.0M | if( i_status != VLC_DEMUXER_EOS ) |
5150 | 14.0M | { |
5151 | 14.0M | p_sys->i_nztime += DEMUX_INCREMENT; |
5152 | 14.0M | p_sys->i_pcr = VLC_TICK_0 + p_sys->i_nztime; |
5153 | 14.0M | es_out_SetPCR( p_demux->out, p_sys->i_pcr ); |
5154 | 14.0M | } |
5155 | 220 | else |
5156 | 220 | { |
5157 | 220 | vlc_tick_t i_segment_end = VLC_TICK_MAX; |
5158 | 560 | for( unsigned i = 0; i < p_sys->i_tracks; i++ ) |
5159 | 340 | { |
5160 | 340 | mp4_track_t *tk = &p_sys->track[i]; |
5161 | 340 | if( tk->b_ok || MP4_isMetadata( tk ) || |
5162 | 59 | (!tk->b_selected && !p_sys->b_seekable) ) |
5163 | 281 | continue; |
5164 | 59 | vlc_tick_t i_track_end = MP4_rescale_mtime( tk->i_time, tk->i_timescale ); |
5165 | 59 | if( i_track_end < i_segment_end ) |
5166 | 59 | i_segment_end = i_track_end; |
5167 | 59 | } |
5168 | 220 | if( i_segment_end != VLC_TICK_MAX ) |
5169 | 59 | { |
5170 | 59 | p_sys->i_nztime = i_segment_end; |
5171 | 59 | p_sys->i_pcr = VLC_TICK_0 + p_sys->i_nztime; |
5172 | 59 | es_out_SetPCR( p_demux->out, p_sys->i_pcr ); |
5173 | 59 | } |
5174 | 220 | } |
5175 | | |
5176 | 14.0M | return i_status; |
5177 | 14.0M | } |
5178 | | |
5179 | | static int FragCreateTrunIndex( demux_t *p_demux, MP4_Box_t *p_moof, |
5180 | | MP4_Box_t *p_chunksidx, stime_t i_moof_time ) |
5181 | 639 | { |
5182 | 639 | demux_sys_t *p_sys = p_demux->p_sys; |
5183 | | |
5184 | 639 | uint64_t i_traf_base_data_offset = p_moof->i_pos; |
5185 | 639 | uint32_t i_traf = 0; |
5186 | 639 | uint64_t i_prev_traf_end = 0; |
5187 | | |
5188 | 1.72k | for( unsigned i=0; i<p_sys->i_tracks; i++ ) |
5189 | 1.08k | { |
5190 | 1.08k | mp4_track_t *p_track = &p_sys->track[i]; |
5191 | 1.08k | if( p_track->context.runs.p_array ) |
5192 | 152 | free( p_track->context.runs.p_array ); |
5193 | 1.08k | p_track->context.runs.p_array = NULL; |
5194 | 1.08k | p_track->context.runs.i_count = 0; |
5195 | 1.08k | p_track->context.runs.i_current = 0; |
5196 | 1.08k | } |
5197 | | |
5198 | 639 | for( MP4_Box_t *p_traf = MP4_BoxGet( p_moof, "traf" ); |
5199 | 1.17k | p_traf ; p_traf = p_traf->p_next ) |
5200 | 539 | { |
5201 | 539 | if ( p_traf->i_type != ATOM_traf ) |
5202 | 100 | continue; |
5203 | | |
5204 | 439 | const MP4_Box_t *p_tfhd = MP4_BoxGet( p_traf, "tfhd" ); |
5205 | 439 | uint32_t i_trun_count = MP4_BoxCount( p_traf, "trun" ); |
5206 | 439 | if ( !p_tfhd || !i_trun_count ) |
5207 | 45 | continue; |
5208 | | |
5209 | 394 | mp4_track_t *p_track = MP4_GetTrackByTrackID( p_demux, BOXDATA(p_tfhd)->i_track_ID ); |
5210 | 394 | if( !p_track ) |
5211 | 31 | continue; |
5212 | | |
5213 | 363 | p_track->context.runs.p_array = realloc_or_free(p_track->context.runs.p_array, |
5214 | 363 | (i_trun_count + p_track->context.runs.i_count) * sizeof(mp4_run_t)); |
5215 | 363 | if(!p_track->context.runs.p_array) |
5216 | 0 | continue; |
5217 | 363 | memset(&p_track->context.runs.p_array[i_trun_count], 0, p_track->context.runs.i_count * sizeof(mp4_run_t)); |
5218 | 363 | i_trun_count += p_track->context.runs.i_count; |
5219 | | |
5220 | | /* Get defaults for this/these RUN */ |
5221 | 363 | uint32_t i_track_defaultsamplesize = 0; |
5222 | 363 | uint32_t i_track_defaultsampleduration = 0; |
5223 | 363 | MP4_GetDefaultSizeAndDuration( p_sys->p_moov, BOXDATA(p_tfhd), |
5224 | 363 | &i_track_defaultsamplesize, |
5225 | 363 | &i_track_defaultsampleduration ); |
5226 | 363 | p_track->context.i_default_sample_size = i_track_defaultsamplesize; |
5227 | 363 | p_track->context.i_default_sample_duration = i_track_defaultsampleduration; |
5228 | | |
5229 | 363 | stime_t i_traf_start_time = p_track->i_time; |
5230 | 363 | bool b_has_base_media_decode_time = false; |
5231 | | |
5232 | 363 | if( p_track->context.b_resync_time_offset ) /* We NEED start time offset for each track */ |
5233 | 233 | { |
5234 | 233 | p_track->context.b_resync_time_offset = false; |
5235 | | |
5236 | | /* Find start time */ |
5237 | 233 | const MP4_Box_t *p_tfdt = MP4_BoxGet( p_traf, "tfdt" ); |
5238 | 233 | if( p_tfdt ) |
5239 | 80 | { |
5240 | 80 | i_traf_start_time = BOXDATA(p_tfdt)->i_base_media_decode_time; |
5241 | 80 | b_has_base_media_decode_time = true; |
5242 | 80 | } |
5243 | | |
5244 | | /* Try using Tfxd for base offset (Smooth) */ |
5245 | 233 | if( !b_has_base_media_decode_time && p_sys->i_tracks == 1 ) |
5246 | 17 | { |
5247 | 17 | const MP4_Box_t *p_uuid = MP4_BoxGet( p_traf, "uuid" ); |
5248 | 17 | for( ; p_uuid; p_uuid = p_uuid->p_next ) |
5249 | 0 | { |
5250 | 0 | if( p_uuid->i_type == ATOM_uuid && |
5251 | 0 | !CmpUUID( &p_uuid->i_uuid, &TfxdBoxUUID ) && p_uuid->data.p_tfxd ) |
5252 | 0 | { |
5253 | 0 | i_traf_start_time = p_uuid->data.p_tfxd->i_fragment_abs_time; |
5254 | 0 | b_has_base_media_decode_time = true; |
5255 | 0 | break; |
5256 | 0 | } |
5257 | 0 | } |
5258 | 17 | } |
5259 | | |
5260 | | /* After seek we should have probed fragments */ |
5261 | 233 | if( !b_has_base_media_decode_time && p_sys->p_fragsindex ) |
5262 | 130 | { |
5263 | 130 | unsigned i_track_index = (p_track - p_sys->track); |
5264 | 130 | assert(&p_sys->track[i_track_index] == p_track); |
5265 | 130 | i_traf_start_time = MP4_Fragment_Index_GetTrackStartTime( p_sys->p_fragsindex, |
5266 | 130 | i_track_index, p_moof->i_pos ); |
5267 | 130 | i_traf_start_time = MP4_rescale( i_traf_start_time, |
5268 | 130 | p_sys->i_timescale, p_track->i_timescale ); |
5269 | 130 | b_has_base_media_decode_time = true; |
5270 | 130 | } |
5271 | | |
5272 | 233 | if( !b_has_base_media_decode_time && p_chunksidx ) |
5273 | 0 | { |
5274 | | /* Try using SIDX as base offset. |
5275 | | * This can not work for global sidx but only when sent within each fragment (dash) */ |
5276 | 0 | const MP4_Box_data_sidx_t *p_data = p_chunksidx->data.p_sidx; |
5277 | 0 | if( p_data && p_data->i_timescale && p_data->i_reference_count == 1 ) |
5278 | 0 | { |
5279 | 0 | i_traf_start_time = MP4_rescale( p_data->i_earliest_presentation_time, |
5280 | 0 | p_data->i_timescale, p_track->i_timescale ); |
5281 | 0 | b_has_base_media_decode_time = true; |
5282 | 0 | } |
5283 | 0 | } |
5284 | | |
5285 | | /* First contiguous segment (moov->moof) and there's no tfdt not probed index (yet) */ |
5286 | 233 | if( !b_has_base_media_decode_time && FragGetMoofSequenceNumber( p_moof ) == 1 ) |
5287 | 20 | { |
5288 | 20 | i_traf_start_time = MP4_rescale( GetMoovTrackDuration( p_sys, p_track->i_track_ID ), |
5289 | 20 | p_sys->i_timescale, p_track->i_timescale ); |
5290 | 20 | b_has_base_media_decode_time = true; |
5291 | 20 | } |
5292 | | |
5293 | | /* Use global sidx moof time, in case moof does not carry tfdt */ |
5294 | 233 | if( !b_has_base_media_decode_time ) |
5295 | 3 | { |
5296 | 3 | if( i_moof_time != INVALID_SEGMENT_TIME ) |
5297 | 0 | i_traf_start_time = MP4_rescale( i_moof_time, p_sys->i_timescale, p_track->i_timescale ); |
5298 | 3 | else if ( p_sys->i_nztime != VLC_TICK_MAX ) /* That should not happen */ |
5299 | 2 | i_traf_start_time = MP4_rescale_qtime( p_sys->i_nztime, p_track->i_timescale ); |
5300 | 1 | else /* That should not happen */ |
5301 | 1 | i_traf_start_time = p_track->i_time; |
5302 | 3 | } |
5303 | 233 | } |
5304 | | |
5305 | | /* Parse TRUN data */ |
5306 | | |
5307 | 363 | if ( BOXDATA(p_tfhd)->i_flags & MP4_TFHD_BASE_DATA_OFFSET ) |
5308 | 3 | { |
5309 | 3 | i_traf_base_data_offset = BOXDATA(p_tfhd)->i_base_data_offset; |
5310 | 3 | } |
5311 | | /* ignored if MP4_TFHD_BASE_DATA_OFFSET */ |
5312 | 360 | else if ( BOXDATA(p_tfhd)->i_flags & MP4_TFHD_DEFAULT_BASE_IS_MOOF ) |
5313 | 161 | { |
5314 | 161 | i_traf_base_data_offset = p_moof->i_pos /* + 8*/; |
5315 | 161 | } |
5316 | 199 | else |
5317 | 199 | { |
5318 | 199 | if ( i_traf == 0 ) |
5319 | 194 | i_traf_base_data_offset = p_moof->i_pos /*+ 8*/; |
5320 | 5 | else |
5321 | 5 | i_traf_base_data_offset = i_prev_traf_end; |
5322 | 199 | } |
5323 | | |
5324 | 363 | uint64_t i_trun_dts = i_traf_start_time; |
5325 | 363 | uint64_t i_trun_data_offset = i_traf_base_data_offset; |
5326 | 363 | uint32_t i_trun_size = 0; |
5327 | | |
5328 | 363 | for( const MP4_Box_t *p_trun = MP4_BoxGet( p_traf, "trun" ); |
5329 | 944 | p_trun && p_tfhd; p_trun = p_trun->p_next ) |
5330 | 581 | { |
5331 | 581 | if ( p_trun->i_type != ATOM_trun ) |
5332 | 217 | continue; |
5333 | | |
5334 | 364 | const MP4_Box_data_trun_t *p_trundata = p_trun->data.p_trun; |
5335 | | |
5336 | | /* Get data offset */ |
5337 | 364 | if ( p_trundata->i_flags & MP4_TRUN_DATA_OFFSET ) |
5338 | 350 | { |
5339 | | /* Fix for broken Trun data offset relative to tfhd instead of moof, as seen in smooth */ |
5340 | 350 | if( (BOXDATA(p_tfhd)->i_flags & MP4_TFHD_BASE_DATA_OFFSET) == 0 && |
5341 | 347 | i_traf == 0 && |
5342 | 342 | i_traf_base_data_offset + p_trundata->i_data_offset < p_moof->i_pos + p_moof->i_size + 8 ) |
5343 | 31 | { |
5344 | 31 | i_trun_data_offset += p_moof->i_size + 8; |
5345 | 31 | } |
5346 | 319 | else if( (BOXDATA(p_tfhd)->i_flags & MP4_TFHD_BASE_DATA_OFFSET) ) |
5347 | 3 | { |
5348 | 3 | i_trun_data_offset = BOXDATA(p_tfhd)->i_base_data_offset + p_trundata->i_data_offset; |
5349 | 3 | } |
5350 | | /* ignored if MP4_TFHD_BASE_DATA_OFFSET */ |
5351 | 316 | else if ( BOXDATA(p_tfhd)->i_flags & MP4_TFHD_DEFAULT_BASE_IS_MOOF ) |
5352 | 159 | { |
5353 | 159 | i_trun_data_offset = p_moof->i_pos + p_trundata->i_data_offset; |
5354 | 159 | } |
5355 | 157 | else |
5356 | 157 | { |
5357 | 157 | i_trun_data_offset += p_trundata->i_data_offset; |
5358 | 157 | } |
5359 | 350 | } |
5360 | 14 | else |
5361 | 14 | { |
5362 | 14 | i_trun_data_offset += i_trun_size; |
5363 | 14 | } |
5364 | | |
5365 | 364 | i_trun_size = 0; |
5366 | 364 | #ifndef NDEBUG |
5367 | 364 | msg_Dbg( p_demux, |
5368 | 364 | "tk %u run %" PRIu32 " dflt dur %"PRIu32" size %"PRIu32" firstdts %"PRId64" offset %"PRIu64, |
5369 | 364 | p_track->i_track_ID, |
5370 | 364 | p_track->context.runs.i_count, |
5371 | 364 | i_track_defaultsampleduration, |
5372 | 364 | i_track_defaultsamplesize, |
5373 | 364 | MP4_rescale_mtime( i_trun_dts, p_track->i_timescale ), i_trun_data_offset ); |
5374 | 364 | #endif |
5375 | | //************ |
5376 | 364 | assert(p_track->context.runs.i_count < i_trun_count); |
5377 | 364 | mp4_run_t *p_run = &p_track->context.runs.p_array[p_track->context.runs.i_count++]; |
5378 | 364 | p_run->i_first_dts = i_trun_dts; |
5379 | 364 | p_run->i_offset = i_trun_data_offset; |
5380 | 364 | p_run->p_trun = p_trun; |
5381 | | |
5382 | | //************ |
5383 | | /* Sum total time */ |
5384 | 364 | if ( p_trundata->i_flags & MP4_TRUN_SAMPLE_DURATION ) |
5385 | 204 | { |
5386 | 2.96k | for( uint32_t i=0; i< p_trundata->i_sample_count; i++ ) |
5387 | 2.76k | i_trun_dts += p_trundata->p_samples[i].i_duration; |
5388 | 204 | } |
5389 | 160 | else |
5390 | 160 | { |
5391 | 160 | i_trun_dts += (uint64_t)p_trundata->i_sample_count * |
5392 | 160 | i_track_defaultsampleduration; |
5393 | 160 | } |
5394 | | |
5395 | | /* Get total traf size */ |
5396 | 364 | if ( p_trundata->i_flags & MP4_TRUN_SAMPLE_SIZE ) |
5397 | 185 | { |
5398 | 4.62k | for( uint32_t i=0; i< p_trundata->i_sample_count; i++ ) |
5399 | 4.43k | i_trun_size += p_trundata->p_samples[i].i_size; |
5400 | 185 | } |
5401 | 179 | else |
5402 | 179 | { |
5403 | 179 | i_trun_size += p_trundata->i_sample_count * |
5404 | 179 | i_track_defaultsamplesize; |
5405 | 179 | } |
5406 | | |
5407 | 364 | i_prev_traf_end = i_trun_data_offset + i_trun_size; |
5408 | 364 | } |
5409 | | |
5410 | 363 | i_traf++; |
5411 | 363 | } |
5412 | | |
5413 | 639 | return VLC_SUCCESS; |
5414 | 639 | } |
5415 | | |
5416 | | static int FragGetMoofBySidxIndex( demux_t *p_demux, vlc_tick_t target_time, |
5417 | | uint64_t *pi_moof_pos, vlc_tick_t *pi_sampletime ) |
5418 | 0 | { |
5419 | 0 | demux_sys_t *p_sys = p_demux->p_sys; |
5420 | 0 | const MP4_Box_t *p_sidx = MP4_BoxGet( p_sys->p_root, "sidx" ); |
5421 | 0 | for( ; p_sidx ; p_sidx = p_sidx->p_next ) |
5422 | 0 | { |
5423 | 0 | if( p_sidx->i_type != ATOM_sidx ) |
5424 | 0 | continue; |
5425 | | |
5426 | 0 | const MP4_Box_data_sidx_t *p_data = BOXDATA(p_sidx); |
5427 | 0 | if( !p_data || !p_data->i_timescale ) |
5428 | 0 | break; |
5429 | | |
5430 | 0 | stime_t i_target_time = MP4_rescale_qtime( target_time, p_data->i_timescale ); |
5431 | | |
5432 | | /* sidx refers to offsets from end of sidx pos in the file + first offset */ |
5433 | 0 | uint64_t i_pos = p_data->i_first_offset + p_sidx->i_pos + p_sidx->i_size; |
5434 | 0 | stime_t i_time = 0; |
5435 | 0 | for( uint16_t i=0; i<p_data->i_reference_count; i++ ) |
5436 | 0 | { |
5437 | 0 | if(p_data->p_items[i].b_reference_type != 0) |
5438 | 0 | continue; |
5439 | 0 | if( i_time + p_data->p_items[i].i_subsegment_duration > i_target_time ) |
5440 | 0 | { |
5441 | 0 | *pi_sampletime = MP4_rescale_mtime( i_time, p_data->i_timescale ); |
5442 | 0 | *pi_moof_pos = i_pos; |
5443 | 0 | return VLC_SUCCESS; |
5444 | 0 | } |
5445 | 0 | i_pos += p_data->p_items[i].i_referenced_size; |
5446 | 0 | i_time += p_data->p_items[i].i_subsegment_duration; |
5447 | 0 | } |
5448 | 0 | } |
5449 | 0 | return VLC_EGENERIC; |
5450 | 0 | } |
5451 | | |
5452 | | static int FragGetMoofByTfraIndex( demux_t *p_demux, const vlc_tick_t i_target_time, unsigned i_track_ID, |
5453 | | uint64_t *pi_moof_pos, vlc_tick_t *pi_sampletime ) |
5454 | 0 | { |
5455 | 0 | demux_sys_t *p_sys = p_demux->p_sys; |
5456 | 0 | MP4_Box_t *p_tfra = MP4_BoxGet( p_sys->p_root, "mfra/tfra" ); |
5457 | 0 | for( ; p_tfra; p_tfra = p_tfra->p_next ) |
5458 | 0 | { |
5459 | 0 | if ( p_tfra->i_type == ATOM_tfra ) |
5460 | 0 | { |
5461 | 0 | const MP4_Box_data_tfra_t *p_data = BOXDATA(p_tfra); |
5462 | 0 | if( !p_data || p_data->i_track_ID != i_track_ID ) |
5463 | 0 | continue; |
5464 | | |
5465 | 0 | uint64_t i_pos = 0; |
5466 | 0 | mp4_track_t *p_track = MP4_GetTrackByTrackID( p_demux, p_data->i_track_ID ); |
5467 | 0 | if ( p_track ) |
5468 | 0 | { |
5469 | 0 | stime_t i_track_target_time = MP4_rescale_qtime( i_target_time, p_track->i_timescale ); |
5470 | 0 | for ( uint32_t i = 0; i<p_data->i_number_of_entries; i += ( p_data->i_version == 1 ) ? 2 : 1 ) |
5471 | 0 | { |
5472 | 0 | stime_t i_time = p_data->p_time[i]; |
5473 | 0 | uint64_t i_offset = p_data->p_moof_offset[i]; |
5474 | |
|
5475 | 0 | if ( i_time >= i_track_target_time ) |
5476 | 0 | { |
5477 | 0 | if ( i_pos == 0 ) /* Not in this traf */ |
5478 | 0 | break; |
5479 | | |
5480 | 0 | *pi_moof_pos = i_pos; |
5481 | 0 | *pi_sampletime = MP4_rescale_mtime( i_time, p_track->i_timescale ); |
5482 | 0 | return VLC_SUCCESS; |
5483 | 0 | } |
5484 | 0 | else |
5485 | 0 | i_pos = i_offset; |
5486 | 0 | } |
5487 | 0 | } |
5488 | 0 | } |
5489 | 0 | } |
5490 | 0 | return VLC_EGENERIC; |
5491 | 0 | } |
5492 | | |
5493 | | static void MP4_GetDefaultSizeAndDuration( MP4_Box_t *p_moov, |
5494 | | const MP4_Box_data_tfhd_t *p_tfhd_data, |
5495 | | uint32_t *pi_default_size, |
5496 | | uint32_t *pi_default_duration ) |
5497 | 363 | { |
5498 | 363 | if( p_tfhd_data->i_flags & MP4_TFHD_DFLT_SAMPLE_DURATION ) |
5499 | 43 | *pi_default_duration = p_tfhd_data->i_default_sample_duration; |
5500 | | |
5501 | 363 | if( p_tfhd_data->i_flags & MP4_TFHD_DFLT_SAMPLE_SIZE ) |
5502 | 190 | *pi_default_size = p_tfhd_data->i_default_sample_size; |
5503 | | |
5504 | 363 | if( !*pi_default_duration || !*pi_default_size ) |
5505 | 363 | { |
5506 | 363 | const MP4_Box_t *p_trex = MP4_GetTrexByTrackID( p_moov, p_tfhd_data->i_track_ID ); |
5507 | 363 | if ( p_trex ) |
5508 | 245 | { |
5509 | 245 | if ( !*pi_default_duration ) |
5510 | 223 | *pi_default_duration = BOXDATA(p_trex)->i_default_sample_duration; |
5511 | 245 | if ( !*pi_default_size ) |
5512 | 143 | *pi_default_size = BOXDATA(p_trex)->i_default_sample_size; |
5513 | 245 | } |
5514 | 363 | } |
5515 | 363 | } |
5516 | | |
5517 | | static int DemuxFrag( demux_t *p_demux ) |
5518 | 14.0M | { |
5519 | 14.0M | demux_sys_t *p_sys = p_demux->p_sys; |
5520 | 14.0M | unsigned i_track_selected = 0; |
5521 | 14.0M | int i_status = VLC_DEMUXER_SUCCESS; |
5522 | | |
5523 | 14.0M | if( unlikely(p_sys->b_error) ) |
5524 | 0 | { |
5525 | 0 | msg_Warn( p_demux, "unrecoverable error" ); |
5526 | 0 | i_status = VLC_DEMUXER_EOF; |
5527 | 0 | goto end; |
5528 | 0 | } |
5529 | | |
5530 | | /* check for newly selected/unselected track */ |
5531 | 28.0M | for( unsigned i_track = 0; i_track < p_sys->i_tracks; i_track++ ) |
5532 | 14.0M | { |
5533 | 14.0M | mp4_track_t *tk = &p_sys->track[i_track]; |
5534 | 14.0M | bool b = true; |
5535 | | |
5536 | 14.0M | if( !tk->b_ok || MP4_isMetadata( tk ) ) |
5537 | 3.93k | continue; |
5538 | | |
5539 | 14.0M | if( p_sys->b_seekable ) |
5540 | 14.0M | es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b ); |
5541 | | |
5542 | 14.0M | if(tk->b_selected != b) |
5543 | 437 | { |
5544 | 437 | msg_Dbg( p_demux, "track %u %s!", tk->i_track_ID, b ? "enabled" : "disabled" ); |
5545 | 437 | MP4_TrackSelect( p_demux, tk, b ); |
5546 | 437 | } |
5547 | | |
5548 | 14.0M | if( tk->b_selected ) |
5549 | 14.0M | i_track_selected++; |
5550 | 14.0M | } |
5551 | | |
5552 | 14.0M | if( i_track_selected <= 0 ) |
5553 | 207 | { |
5554 | 207 | msg_Warn( p_demux, "no track selected, exiting..." ); |
5555 | 207 | i_status = VLC_DEMUXER_EOF; |
5556 | 207 | goto end; |
5557 | 207 | } |
5558 | | |
5559 | 14.0M | if ( p_sys->context.i_current_box_type != ATOM_mdat ) |
5560 | 2.28k | { |
5561 | | /* Otherwise mdat is skipped. FIXME: mdat reading ! */ |
5562 | 2.28k | const uint8_t *p_peek; |
5563 | 2.28k | if( vlc_stream_Peek( p_demux->s, &p_peek, 8 ) != 8 ) |
5564 | 251 | { |
5565 | 251 | i_status = VLC_DEMUXER_EOF; |
5566 | 251 | goto end; |
5567 | 251 | } |
5568 | | |
5569 | 2.03k | p_sys->context.i_current_box_type = VLC_FOURCC( p_peek[4], p_peek[5], p_peek[6], p_peek[7] ); |
5570 | 2.03k | if( p_sys->context.i_current_box_type != ATOM_moof && |
5571 | 1.39k | p_sys->context.i_current_box_type != ATOM_moov ) |
5572 | 1.01k | { |
5573 | 1.01k | uint64_t i_pos = vlc_stream_Tell( p_demux->s ); |
5574 | 1.01k | uint64_t i_size = GetDWBE( p_peek ); |
5575 | 1.01k | if ( i_size == 1 ) |
5576 | 11 | { |
5577 | 11 | if( vlc_stream_Peek( p_demux->s, &p_peek, 16 ) != 16 ) |
5578 | 1 | { |
5579 | 1 | i_status = VLC_DEMUXER_EOF; |
5580 | 1 | goto end; |
5581 | 1 | } |
5582 | 10 | i_size = GetQWBE( p_peek + 8 ); |
5583 | 10 | } |
5584 | | |
5585 | 1.01k | if( UINT64_MAX - i_pos < i_size ) |
5586 | 0 | { |
5587 | 0 | i_status = VLC_DEMUXER_EOF; |
5588 | 0 | goto end; |
5589 | 0 | } |
5590 | | |
5591 | 1.01k | if( p_sys->context.i_current_box_type == ATOM_mdat ) |
5592 | 339 | { |
5593 | | /* We'll now read mdat using context atom, |
5594 | | * but we'll need post mdat offset, as we'll never seek backward */ |
5595 | 339 | p_sys->context.i_post_mdat_offset = i_pos + i_size; |
5596 | 339 | } |
5597 | 675 | else if( MP4_Seek( p_demux->s, i_pos + i_size ) != VLC_SUCCESS ) /* skip other atoms */ |
5598 | 0 | { |
5599 | 0 | i_status = VLC_DEMUXER_EOF; |
5600 | 0 | goto end; |
5601 | 0 | } |
5602 | 1.01k | } |
5603 | 1.01k | else |
5604 | 1.01k | { |
5605 | 1.01k | MP4_Box_t *p_vroot = MP4_BoxGetNextChunk( p_demux->s ); |
5606 | 1.01k | if(!p_vroot) |
5607 | 1 | { |
5608 | 1 | i_status = VLC_DEMUXER_EOF; |
5609 | 1 | goto end; |
5610 | 1 | } |
5611 | | |
5612 | 1.01k | MP4_Box_t *p_box = NULL; |
5613 | 1.01k | for( p_box = p_vroot->p_first; p_box; p_box = p_box->p_next ) |
5614 | 1.01k | { |
5615 | 1.01k | if( p_box->i_type == ATOM_moof || |
5616 | 376 | p_box->i_type == ATOM_moov ) |
5617 | 1.01k | break; |
5618 | 1.01k | } |
5619 | | |
5620 | 1.01k | if( p_box ) |
5621 | 1.01k | { |
5622 | 1.01k | FragResetContext( p_sys ); |
5623 | | |
5624 | 1.01k | if( p_box->i_type == ATOM_moov ) |
5625 | 376 | { |
5626 | 376 | p_sys->context.p_fragment_atom = p_sys->p_moov; |
5627 | 376 | } |
5628 | 639 | else |
5629 | 639 | { |
5630 | 639 | p_sys->context.p_fragment_atom = MP4_BoxExtract( &p_vroot->p_first, p_box->i_type ); |
5631 | | |
5632 | | /* Detect and Handle Passive Seek */ |
5633 | 639 | const uint32_t i_sequence_number = FragGetMoofSequenceNumber( p_sys->context.p_fragment_atom ); |
5634 | 639 | const bool b_discontinuity = ( i_sequence_number != p_sys->context.i_lastseqnumber + 1 ); |
5635 | 639 | if( b_discontinuity ) |
5636 | 639 | msg_Info( p_demux, "Fragment sequence discontinuity detected %"PRIu32" != %"PRIu32, |
5637 | 639 | i_sequence_number, p_sys->context.i_lastseqnumber + 1 ); |
5638 | 639 | p_sys->context.i_lastseqnumber = i_sequence_number; |
5639 | | |
5640 | | /* Prepare chunk */ |
5641 | 639 | if( FragPrepareChunk( p_demux, p_sys->context.p_fragment_atom, |
5642 | 639 | MP4_BoxGet( p_vroot, "sidx"), INVALID_SEGMENT_TIME, |
5643 | 639 | b_discontinuity ) != VLC_SUCCESS ) |
5644 | 0 | { |
5645 | 0 | MP4_BoxFree( p_vroot ); |
5646 | 0 | i_status = VLC_DEMUXER_EOF; |
5647 | 0 | goto end; |
5648 | 0 | } |
5649 | | |
5650 | 639 | if( b_discontinuity ) |
5651 | 450 | { |
5652 | 450 | p_sys->i_nztime = FragGetDemuxTimeFromTracksTime( p_sys ); |
5653 | 450 | p_sys->i_pcr = VLC_TICK_INVALID; |
5654 | 450 | } |
5655 | | /* !Prepare chunk */ |
5656 | 639 | } |
5657 | | |
5658 | 1.01k | p_sys->context.i_current_box_type = p_box->i_type; |
5659 | 1.01k | } |
5660 | | |
5661 | 1.01k | MP4_BoxFree( p_vroot ); |
5662 | | |
5663 | 1.01k | if( p_sys->context.p_fragment_atom == NULL ) |
5664 | 0 | { |
5665 | 0 | msg_Info(p_demux, "no moof or moov in current chunk"); |
5666 | 0 | return VLC_DEMUXER_SUCCESS; |
5667 | 0 | } |
5668 | 1.01k | } |
5669 | 2.03k | } |
5670 | | |
5671 | 14.0M | if ( p_sys->context.i_current_box_type == ATOM_mdat ) |
5672 | 14.0M | { |
5673 | 14.0M | assert(p_sys->context.p_fragment_atom); |
5674 | | |
5675 | 14.0M | if ( p_sys->context.p_fragment_atom ) |
5676 | 14.0M | switch( p_sys->context.p_fragment_atom->i_type ) |
5677 | 14.0M | { |
5678 | 5 | case ATOM_moov://[ftyp/moov, mdat]+ -> [moof, mdat]+ |
5679 | 5 | i_status = DemuxMoov( p_demux ); |
5680 | 5 | break; |
5681 | 14.0M | case ATOM_moof: |
5682 | 14.0M | i_status = DemuxMoof( p_demux ); |
5683 | 14.0M | break; |
5684 | 0 | default: |
5685 | 0 | msg_Err( p_demux, "fragment type %4.4s", (char*) &p_sys->context.p_fragment_atom->i_type ); |
5686 | 0 | break; |
5687 | 14.0M | } |
5688 | | |
5689 | 14.0M | if( i_status == VLC_DEMUXER_EOS ) |
5690 | 225 | { |
5691 | 225 | i_status = VLC_DEMUXER_SUCCESS; |
5692 | | /* Skip if we didn't reach the end of mdat box */ |
5693 | 225 | uint64_t i_pos = vlc_stream_Tell( p_demux->s ); |
5694 | 225 | if( i_pos != p_sys->context.i_post_mdat_offset && i_status != VLC_DEMUXER_EOF ) |
5695 | 83 | { |
5696 | 83 | if( i_pos > p_sys->context.i_post_mdat_offset ) |
5697 | 83 | msg_Err( p_demux, " Overread mdat by %" PRIu64, i_pos - p_sys->context.i_post_mdat_offset ); |
5698 | 69 | else if( !p_sys->b_seekable ) |
5699 | 0 | msg_Warn( p_demux, "mdat had still %"PRIu64" bytes unparsed as samples", |
5700 | 83 | p_sys->context.i_post_mdat_offset - i_pos ); |
5701 | 83 | if( MP4_Seek( p_demux->s, p_sys->context.i_post_mdat_offset ) != VLC_SUCCESS ) |
5702 | 0 | i_status = VLC_DEMUXER_EGENERIC; |
5703 | 83 | } |
5704 | 225 | p_sys->context.i_current_box_type = 0; |
5705 | | |
5706 | 225 | } |
5707 | 14.0M | } |
5708 | | |
5709 | 14.0M | end: |
5710 | 14.0M | if( i_status == VLC_DEMUXER_EOF ) |
5711 | 574 | { |
5712 | 574 | vlc_tick_t i_demux_end = VLC_TICK_MIN; |
5713 | 1.50k | for( unsigned i = 0; i < p_sys->i_tracks; i++ ) |
5714 | 933 | { |
5715 | 933 | const mp4_track_t *tk = &p_sys->track[i]; |
5716 | 933 | vlc_tick_t i_track_end = MP4_rescale_mtime( tk->i_time, tk->i_timescale ); |
5717 | 933 | if( i_track_end > i_demux_end ) |
5718 | 581 | i_demux_end = i_track_end; |
5719 | 933 | } |
5720 | 574 | if( i_demux_end != VLC_TICK_MIN ) |
5721 | 574 | es_out_SetPCR( p_demux->out, VLC_TICK_0 + i_demux_end ); |
5722 | 574 | } |
5723 | | |
5724 | 14.0M | return i_status; |
5725 | 14.0M | } |
5726 | | |
5727 | | /* ASF Handlers */ |
5728 | | inline static mp4_track_t *MP4ASF_GetTrack( asf_packet_sys_t *p_packetsys, |
5729 | | uint8_t i_stream_number ) |
5730 | 0 | { |
5731 | 0 | demux_t *p_demux = p_packetsys->priv; |
5732 | 0 | demux_sys_t *p_sys = p_demux->p_sys; |
5733 | 0 | for ( unsigned int i=0; i<p_sys->i_tracks; i++ ) |
5734 | 0 | { |
5735 | 0 | if ( p_sys->track[i].p_asf && |
5736 | 0 | i_stream_number == p_sys->track[i].BOXDATA(p_asf)->i_stream_number ) |
5737 | 0 | { |
5738 | 0 | return &p_sys->track[i]; |
5739 | 0 | } |
5740 | 0 | } |
5741 | 0 | return NULL; |
5742 | 0 | } |
5743 | | |
5744 | | static asf_track_info_t * MP4ASF_GetTrackInfo( asf_packet_sys_t *p_packetsys, |
5745 | | uint8_t i_stream_number ) |
5746 | 0 | { |
5747 | 0 | mp4_track_t *p_track = MP4ASF_GetTrack( p_packetsys, i_stream_number ); |
5748 | 0 | if ( p_track ) |
5749 | 0 | return &p_track->asfinfo; |
5750 | 0 | else |
5751 | 0 | return NULL; |
5752 | 0 | } |
5753 | | |
5754 | | static void MP4ASF_Send( asf_packet_sys_t *p_packetsys, uint8_t i_stream_number, |
5755 | | block_t **pp_frame ) |
5756 | 0 | { |
5757 | 0 | demux_t *p_demux = p_packetsys->priv; |
5758 | 0 | mp4_track_t *p_track = MP4ASF_GetTrack( p_packetsys, i_stream_number ); |
5759 | 0 | if ( !p_track ) |
5760 | 0 | { |
5761 | 0 | block_Release( *pp_frame ); |
5762 | 0 | } |
5763 | 0 | else |
5764 | 0 | { |
5765 | 0 | block_t *p_gather = block_ChainGather( *pp_frame ); |
5766 | 0 | p_gather->i_dts = p_track->i_dts_backup; |
5767 | 0 | p_gather->i_pts = p_track->i_pts_backup; |
5768 | 0 | es_out_Send( p_demux->out, p_track->p_es, p_gather ); |
5769 | 0 | } |
5770 | |
|
5771 | 0 | *pp_frame = NULL; |
5772 | 0 | } |
5773 | | |
5774 | | static void MP4ASF_ResetFrames( demux_sys_t *p_sys ) |
5775 | 0 | { |
5776 | 0 | for ( unsigned int i=0; i<p_sys->i_tracks; i++ ) |
5777 | 0 | { |
5778 | 0 | mp4_track_t *p_track = &p_sys->track[i]; |
5779 | 0 | ASFPacketTrackReset( &p_track->asfinfo ); |
5780 | 0 | } |
5781 | 0 | } |