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