1 /*****************************************************************************
2 * mp4.c : MP4 file input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: mp4.c,v 1.52 2004/01/13 01:54:54 fenrir Exp $
6 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
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 General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
21 *****************************************************************************/
23 /*****************************************************************************
25 *****************************************************************************/
26 #include <stdlib.h> /* malloc(), free() */
29 #include <vlc/input.h>
30 #include <vlc_playlist.h>
37 /*****************************************************************************
39 *****************************************************************************/
40 static int Open ( vlc_object_t * );
41 static void Close( vlc_object_t * );
44 set_description( _("MP4 demuxer") );
45 set_capability( "demux", 242 );
46 set_callbacks( Open, Close );
49 /*****************************************************************************
51 *****************************************************************************/
52 static int Demux ( input_thread_t * );
53 static int DemuxRef( input_thread_t *p_input )
57 static int Seek ( input_thread_t *, mtime_t );
58 static int Control ( input_thread_t *, int, va_list );
60 /*****************************************************************************
61 * Declaration of local function
62 *****************************************************************************/
63 static void MP4_TrackCreate ( input_thread_t *, track_data_mp4_t *, MP4_Box_t *);
64 static void MP4_TrackDestroy( input_thread_t *, track_data_mp4_t * );
66 static int MP4_TrackSelect ( input_thread_t *, track_data_mp4_t *, mtime_t );
67 static void MP4_TrackUnselect(input_thread_t *, track_data_mp4_t * );
69 static int MP4_TrackSeek ( input_thread_t *, track_data_mp4_t *, mtime_t );
71 static uint64_t MP4_GetTrackPos ( track_data_mp4_t * );
72 static int MP4_TrackSampleSize( track_data_mp4_t * );
73 static int MP4_TrackNextSample( input_thread_t *, track_data_mp4_t * );
75 static char *LanguageGetName( const char *psz_code );
78 if( p ) { free( p ); (p) = NULL;}
80 /*****************************************************************************
81 * Open: check file and initializes MP4 structures
82 *****************************************************************************/
83 static int Open( vlc_object_t * p_this )
85 input_thread_t *p_input = (input_thread_t *)p_this;
96 vlc_bool_t b_seekable;
98 /* a little test to see if it could be a mp4 */
99 if( stream_Peek( p_input->s, &p_peek, 8 ) < 8 )
101 msg_Warn( p_input, "MP4 plugin discarded (cannot peek)" );
104 switch( VLC_FOURCC( p_peek[4], p_peek[5], p_peek[6], p_peek[7] ) )
117 msg_Warn( p_input, "MP4 plugin discarded (not a valid file)" );
122 stream_Control( p_input->s, STREAM_CAN_SEEK, &b_seekable );
125 msg_Warn( p_input, "MP4 plugin discarded (unseekable)" );
129 /*Set exported functions */
130 p_input->pf_demux = Demux;
131 p_input->pf_demux_control = Control;
133 /* create our structure that will contains all data */
134 p_input->p_demux_data = p_sys = malloc( sizeof( demux_sys_t ) );
135 memset( p_sys, 0, sizeof( demux_sys_t ) );
137 /* Now load all boxes ( except raw data ) */
138 if( ( p_sys->p_root = MP4_BoxGetRoot( p_input ) ) == NULL )
140 msg_Warn( p_input, "MP4 plugin discarded (not a valid file)" );
144 MP4_BoxDumpStructure( p_input, p_sys->p_root );
146 if( ( p_ftyp = MP4_BoxGet( p_sys->p_root, "/ftyp" ) ) )
148 switch( p_ftyp->data.p_ftyp->i_major_brand )
152 "ISO Media file (isom) version %d.",
153 p_ftyp->data.p_ftyp->i_minor_version );
157 "unrecognized major file specification (%4.4s).",
158 (char*)&p_ftyp->data.p_ftyp->i_major_brand );
164 msg_Dbg( p_input, "file type box missing (assuming ISO Media file)" );
167 /* the file need to have one moov box */
168 if( MP4_BoxCount( p_sys->p_root, "/moov" ) <= 0 )
170 MP4_Box_t *p_foov = MP4_BoxGet( p_sys->p_root, "/foov" );
174 msg_Err( p_input, "MP4 plugin discarded (no moov box)" );
177 /* we have a free box as a moov, rename it */
178 p_foov->i_type = FOURCC_moov;
181 if( ( p_rmra = MP4_BoxGet( p_sys->p_root, "/moov/rmra" ) ) )
183 playlist_t *p_playlist;
184 int i_count = MP4_BoxCount( p_rmra, "rmda" );
187 msg_Dbg( p_input, "detected playlist mov file (%d ref)", i_count );
190 (playlist_t *)vlc_object_find( p_input,
195 //p_playlist->pp_items[p_playlist->i_index]->b_autodeletion = VLC_TRUE;
196 for( i = 0; i < i_count; i++ )
198 MP4_Box_t *p_rdrf = MP4_BoxGet( p_rmra, "rmda[%d]/rdrf", i );
202 if( !p_rdrf || !( psz_ref = p_rdrf->data.p_rdrf->psz_ref ) )
206 i_ref_type = p_rdrf->data.p_rdrf->i_ref_type;
208 msg_Dbg( p_input, "new ref=`%s' type=%4.4s",
209 psz_ref, (char*)&i_ref_type );
211 if( i_ref_type == VLC_FOURCC( 'u', 'r', 'l', ' ' ) )
213 if( strstr( psz_ref, "qt5gateQT" ) )
215 msg_Dbg( p_input, "ignoring pseudo ref =`%s'", psz_ref );
218 if( !strncmp( psz_ref, "http://", 7 ) ||
219 !strncmp( psz_ref, "rtsp://", 7 ) )
221 msg_Dbg( p_input, "adding ref = `%s'", psz_ref );
222 playlist_Add( p_playlist, psz_ref, psz_ref,
223 PLAYLIST_APPEND, PLAYLIST_END );
227 /* msg dbg relative ? */
228 char *psz_absolute = alloca( strlen( p_input->psz_source ) + strlen( psz_ref ) + 1);
229 char *end = strrchr( p_input->psz_source, '/' );
233 int i_len = end + 1 - p_input->psz_source;
235 strncpy( psz_absolute, p_input->psz_source, i_len);
236 psz_absolute[i_len] = '\0';
240 strcpy( psz_absolute, "" );
242 strcat( psz_absolute, psz_ref );
243 msg_Dbg( p_input, "adding ref = `%s'", psz_absolute );
244 playlist_Add( p_playlist, psz_absolute, psz_absolute,
245 PLAYLIST_APPEND, PLAYLIST_END );
250 msg_Err( p_input, "unknown ref type=%4.4s FIXME (send a bug report)", (char*)&p_rdrf->data.p_rdrf->i_ref_type );
253 vlc_object_release( p_playlist );
257 msg_Err( p_input, "can't find playlist" );
261 if( !(p_mvhd = MP4_BoxGet( p_sys->p_root, "/moov/mvhd" ) ) )
265 msg_Err( p_input, "cannot find /moov/mvhd" );
270 msg_Warn( p_input, "cannot find /moov/mvhd (pure ref file)" );
271 p_input->pf_demux = DemuxRef;
277 p_sys->i_timescale = p_mvhd->data.p_mvhd->i_timescale;
278 p_sys->i_duration = p_mvhd->data.p_mvhd->i_duration;
281 if( !( p_sys->i_tracks = MP4_BoxCount( p_sys->p_root, "/moov/trak" ) ) )
283 msg_Err( p_input, "cannot find any /moov/trak" );
286 msg_Dbg( p_input, "find %d track%c",
288 p_sys->i_tracks ? 's':' ' );
290 /* create one program */
291 vlc_mutex_lock( &p_input->stream.stream_lock );
292 if( input_InitStream( p_input, 0 ) == -1)
294 vlc_mutex_unlock( &p_input->stream.stream_lock );
295 msg_Err( p_input, "cannot init stream" );
298 if( p_sys->i_duration/p_sys->i_timescale > 0 )
300 p_input->stream.i_mux_rate =
301 p_input->stream.p_selected_area->i_size / 50 /
302 ( p_sys->i_duration / p_sys->i_timescale );
306 p_input->stream.i_mux_rate = 0;
308 vlc_mutex_unlock( &p_input->stream.stream_lock );
311 /* allocate memory */
312 p_sys->track = calloc( p_sys->i_tracks, sizeof( track_data_mp4_t ) );
313 memset( p_sys->track, 0, p_sys->i_tracks * sizeof( track_data_mp4_t ) );
315 /* now process each track and extract all usefull informations */
316 for( i = 0; i < p_sys->i_tracks; i++ )
318 p_trak = MP4_BoxGet( p_sys->p_root, "/moov/trak[%d]", i );
319 MP4_TrackCreate( p_input, &p_sys->track[i], p_trak );
321 if( p_sys->track[i].b_ok )
324 switch( p_sys->track[i].fmt.i_cat )
337 msg_Dbg( p_input, "adding track[Id 0x%x] %s (%s) language %s",
338 p_sys->track[i].i_track_ID,
340 p_sys->track[i].b_enable ? "enable":"disable",
341 p_sys->track[i].fmt.psz_language ? p_sys->track[i].fmt.psz_language : "undef" );
345 msg_Dbg( p_input, "ignoring track[Id 0x%x]", p_sys->track[i].i_track_ID );
354 MP4_BoxFree( p_input, p_sys->p_root );
360 /*****************************************************************************
361 * Demux: read packet and send them to decoders
362 *****************************************************************************
363 * TODO check for newly selected track (ie audio upt to now )
364 *****************************************************************************/
365 static int Demux( input_thread_t *p_input )
367 demux_sys_t *p_sys = p_input->p_demux_data;
368 unsigned int i_track;
371 unsigned int i_track_selected;
372 vlc_bool_t b_play_audio;
374 /* check for newly selected/unselected track */
375 for( i_track = 0, i_track_selected = 0; i_track < p_sys->i_tracks; i_track++ )
377 #define track p_sys->track[i_track]
378 if( track.b_selected && track.i_sample >= track.i_sample_count )
380 msg_Warn( p_input, "track[0x%x] will be disabled", track.i_track_ID );
381 MP4_TrackUnselect( p_input, &track );
383 else if( track.b_ok )
386 es_out_Control( p_input->p_es_out, ES_OUT_GET_ES_STATE, track.p_es, &b );
388 if( track.b_selected && !b )
390 MP4_TrackUnselect( p_input, &track );
392 else if( !track.b_selected && b)
394 MP4_TrackSelect( p_input, &track, MP4_GetMoviePTS( p_sys ) );
397 if( track.b_selected )
405 if( i_track_selected <= 0 )
407 msg_Warn( p_input, "no track selected, exiting..." );
411 /* first wait for the good time to read a packet */
412 input_ClockManageRef( p_input,
413 p_input->stream.p_selected_program,
416 /* update pcr XXX in mpeg scale so in 90000 unit/s */
417 p_sys->i_pcr = MP4_GetMoviePTS( p_sys ) * 9 / 100;
419 /* we will read 100ms for each stream so ...*/
420 p_sys->i_time += __MAX( p_sys->i_timescale / 10 , 1 );
422 /* Check if we need to send the audio data to decoder */
423 b_play_audio = !p_input->stream.control.b_mute;
425 for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
427 #define track p_sys->track[i_track]
430 MP4_GetTrackPTS( &track ) >= MP4_GetMoviePTS( p_sys ) )
435 while( MP4_GetTrackPTS( &track ) < MP4_GetMoviePTS( p_sys ) )
438 if( ( !b_play_audio && track.fmt.i_cat == AUDIO_ES ) ||
439 MP4_TrackSampleSize( &track ) <= 0 )
441 if( MP4_TrackNextSample( p_input, &track ) )
451 if( stream_Seek( p_input->s, MP4_GetTrackPos( &track ) ) )
453 msg_Warn( p_input, "track[0x%x] will be disabled (eof?)", track.i_track_ID );
454 MP4_TrackUnselect( p_input, &track );
459 if( ( p_block = stream_Block( p_input->s,
460 MP4_TrackSampleSize( &track ) ) ) == NULL )
462 msg_Warn( p_input, "track[0x%x] will be disabled (eof?)", track.i_track_ID );
463 MP4_TrackUnselect( p_input, &track );
467 if( track.b_drms && track.p_drms )
469 drms_decrypt( track.p_drms,
470 (uint32_t *)p_block->p_buffer,
474 if( track.fmt.i_cat == VIDEO_ES )
476 /* FIXME sometime we can calculate PTS */
479 input_ClockGetTS( p_input,
480 p_input->stream.p_selected_program,
481 MP4_GetTrackPTS( &track ) * 9/100 );
487 input_ClockGetTS( p_input,
488 p_input->stream.p_selected_program,
489 MP4_GetTrackPTS( &track ) * 9/100 );
492 if( !track.b_drms || ( track.b_drms && track.p_drms ) )
494 es_out_Send( p_input->p_es_out, track.p_es, p_block );
497 if( MP4_TrackNextSample( p_input, &track ) )
508 /*****************************************************************************
509 * Seek: Got to i_date
510 ******************************************************************************/
511 static int Seek ( input_thread_t *p_input, mtime_t i_date )
513 demux_sys_t *p_sys = p_input->p_demux_data;
514 unsigned int i_track;
515 /* First update update global time */
516 p_sys->i_time = i_date * p_sys->i_timescale / 1000000;
517 p_sys->i_pcr = i_date* 9 / 100;
519 /* Now for each stream try to go to this time */
520 for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
522 #define track p_sys->track[i_track]
523 if( track.b_ok && track.b_selected )
525 MP4_TrackSeek( p_input, &track, i_date );
532 /*****************************************************************************
534 *****************************************************************************/
535 static int Control ( input_thread_t *p_input, int i_query, va_list args )
537 demux_sys_t *p_sys = p_input->p_demux_data;
544 case DEMUX_GET_POSITION:
545 pf = (double*)va_arg( args, double * );
546 if( p_sys->i_duration > 0 )
548 *pf = (double)p_sys->i_time / (double)p_sys->i_duration;
556 case DEMUX_SET_POSITION:
557 f = (double)va_arg( args, double );
560 (double)p_sys->i_duration /
561 (double)p_sys->i_timescale );
562 return Seek( p_input, i64 );
565 pi64 = (int64_t*)va_arg( args, int64_t * );
566 *pi64 = (mtime_t)1000000 *
567 (mtime_t)p_sys->i_time /
568 (mtime_t)p_sys->i_timescale;
572 i64 = (int64_t)va_arg( args, int64_t );
573 return Seek( p_input, i64 );
575 case DEMUX_GET_LENGTH:
576 pi64 = (int64_t*)va_arg( args, int64_t * );
577 *pi64 = (mtime_t)1000000 *
578 (mtime_t)p_sys->i_duration /
579 (mtime_t)p_sys->i_timescale;
582 msg_Warn( p_input, "DEMUX_GET_FPS unimplemented !!" );
585 msg_Err( p_input, "control query unimplemented !!!" );
586 return demux_vaControlDefault( p_input, i_query, args );
590 /*****************************************************************************
591 * Close: frees unused data
592 *****************************************************************************/
593 static void Close ( vlc_object_t * p_this )
595 unsigned int i_track;
596 input_thread_t * p_input = (input_thread_t *)p_this;
597 demux_sys_t *p_sys = p_input->p_demux_data;
599 msg_Dbg( p_input, "freeing all memory" );
601 MP4_BoxFree( p_input, p_sys->p_root );
602 for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
604 MP4_TrackDestroy( p_input, &p_sys->track[i_track] );
606 FREE( p_sys->track );
613 /****************************************************************************
614 * Local functions, specific to vlc
615 ****************************************************************************/
617 /* now create basic chunk data, the rest will be filled by MP4_CreateSamplesIndex */
618 static int TrackCreateChunksIndex( input_thread_t *p_input,
619 track_data_mp4_t *p_demux_track )
621 MP4_Box_t *p_co64; /* give offset for each chunk, same for stco and co64 */
624 unsigned int i_chunk;
625 unsigned int i_index, i_last;
627 if( ( !(p_co64 = MP4_BoxGet( p_demux_track->p_stbl, "stco" ) )&&
628 !(p_co64 = MP4_BoxGet( p_demux_track->p_stbl, "co64" ) ) )||
629 ( !(p_stsc = MP4_BoxGet( p_demux_track->p_stbl, "stsc" ) ) ))
631 return( VLC_EGENERIC );
634 p_demux_track->i_chunk_count = p_co64->data.p_co64->i_entry_count;
635 if( !p_demux_track->i_chunk_count )
637 msg_Warn( p_input, "no chunk defined" );
638 return( VLC_EGENERIC );
640 p_demux_track->chunk = calloc( p_demux_track->i_chunk_count,
641 sizeof( chunk_data_mp4_t ) );
643 /* first we read chunk offset */
644 for( i_chunk = 0; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
646 p_demux_track->chunk[i_chunk].i_offset =
647 p_co64->data.p_co64->i_chunk_offset[i_chunk];
650 /* now we read index for SampleEntry( soun vide mp4a mp4v ...)
651 to be used for the sample XXX begin to 1
652 We construct it begining at the end */
653 i_last = p_demux_track->i_chunk_count; /* last chunk proceded */
654 i_index = p_stsc->data.p_stsc->i_entry_count;
657 msg_Warn( p_input, "cannot read chunk table or table empty" );
658 return( VLC_EGENERIC );
664 for( i_chunk = p_stsc->data.p_stsc->i_first_chunk[i_index] - 1;
665 i_chunk < i_last; i_chunk++ )
667 p_demux_track->chunk[i_chunk].i_sample_description_index =
668 p_stsc->data.p_stsc->i_sample_description_index[i_index];
669 p_demux_track->chunk[i_chunk].i_sample_count =
670 p_stsc->data.p_stsc->i_samples_per_chunk[i_index];
672 i_last = p_stsc->data.p_stsc->i_first_chunk[i_index] - 1;
675 p_demux_track->chunk[0].i_sample_first = 0;
676 for( i_chunk = 1; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
678 p_demux_track->chunk[i_chunk].i_sample_first =
679 p_demux_track->chunk[i_chunk-1].i_sample_first +
680 p_demux_track->chunk[i_chunk-1].i_sample_count;
684 "track[Id 0x%x] read %d chunk",
685 p_demux_track->i_track_ID,
686 p_demux_track->i_chunk_count );
688 return( VLC_SUCCESS );
690 static int TrackCreateSamplesIndex( input_thread_t *p_input,
691 track_data_mp4_t *p_demux_track )
693 MP4_Box_t *p_stts; /* makes mapping between sample and decoding time,
694 ctts make same mapping but for composition time,
695 not yet used and probably not usefull */
696 MP4_Box_t *p_stsz; /* gives sample size of each samples, there is also stz2
697 that uses a compressed form FIXME make them in libmp4
699 /* TODO use also stss and stsh table for seeking */
700 /* FIXME use edit table */
705 int64_t i_index_sample_used;
709 p_stts = MP4_BoxGet( p_demux_track->p_stbl, "stts" );
710 p_stsz = MP4_BoxGet( p_demux_track->p_stbl, "stsz" ); /* FIXME and stz2 */
712 if( ( !p_stts )||( !p_stsz ) )
714 msg_Warn( p_input, "cannot read sample table" );
715 return( VLC_EGENERIC );
718 p_demux_track->i_sample_count = p_stsz->data.p_stsz->i_sample_count;
721 /* for sample size, there are 2 case */
722 if( p_stsz->data.p_stsz->i_sample_size )
724 /* 1: all sample have the same size, so no need to construct a table */
725 p_demux_track->i_sample_size = p_stsz->data.p_stsz->i_sample_size;
726 p_demux_track->p_sample_size = NULL;
730 /* 2: each sample can have a different size */
731 p_demux_track->i_sample_size = 0;
732 p_demux_track->p_sample_size =
733 calloc( p_demux_track->i_sample_count, sizeof( uint32_t ) );
735 for( i_sample = 0; i_sample < p_demux_track->i_sample_count; i_sample++ )
737 p_demux_track->p_sample_size[i_sample] =
738 p_stsz->data.p_stsz->i_entry_size[i_sample];
741 /* we have extract all information from stsz,
744 /* if we don't want to waste too much memory, we can't expand
745 the box !, so each chunk will contain an "extract" of this table
749 i_index = 0; i_index_sample_used =0;
751 /* create and init last data for each chunk */
752 for(i_chunk = 0 ; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
755 int64_t i_entry, i_sample_count, i;
757 p_demux_track->chunk[i_chunk].i_first_dts = i_last_dts;
758 /* count how many entries needed for this chunk
759 for p_sample_delta_dts and p_sample_count_dts */
761 i_sample_count = p_demux_track->chunk[i_chunk].i_sample_count;
764 while( i_sample_count > 0 )
766 i_sample_count -= p_stts->data.p_stts->i_sample_count[i_index+i_entry];
769 i_sample_count += i_index_sample_used; /* don't count already used sample
776 p_demux_track->chunk[i_chunk].p_sample_count_dts =
777 calloc( i_entry, sizeof( uint32_t ) );
778 p_demux_track->chunk[i_chunk].p_sample_delta_dts =
779 calloc( i_entry, sizeof( uint32_t ) );
782 i_sample_count = p_demux_track->chunk[i_chunk].i_sample_count;
783 for( i = 0; i < i_entry; i++ )
788 i_rest = p_stts->data.p_stts->i_sample_count[i_index] - i_index_sample_used;
790 i_used = __MIN( i_rest, i_sample_count );
792 i_index_sample_used += i_used;
793 i_sample_count -= i_used;
795 p_demux_track->chunk[i_chunk].p_sample_count_dts[i] = i_used;
797 p_demux_track->chunk[i_chunk].p_sample_delta_dts[i] =
798 p_stts->data.p_stts->i_sample_delta[i_index];
800 i_last_dts += i_used *
801 p_demux_track->chunk[i_chunk].p_sample_delta_dts[i];
803 if( i_index_sample_used >=
804 p_stts->data.p_stts->i_sample_count[i_index] )
808 i_index_sample_used = 0;
815 "track[Id 0x%x] read %d samples length:"I64Fd"s",
816 p_demux_track->i_track_ID,
817 p_demux_track->i_sample_count,
818 i_last_dts / p_demux_track->i_timescale );
820 return( VLC_SUCCESS );
825 * Create ES and PES to init decoder if needed, for a track starting at i_chunk
827 static int TrackCreateES ( input_thread_t *p_input,
828 track_data_mp4_t *p_track,
829 unsigned int i_chunk,
830 es_out_id_t **pp_es )
837 if( !p_track->chunk[i_chunk].i_sample_description_index )
840 "invalid SampleEntry index (track[Id 0x%x])",
841 p_track->i_track_ID );
845 p_sample = MP4_BoxGet( p_track->p_stsd, "[%d]",
846 p_track->chunk[i_chunk].i_sample_description_index - 1 );
848 if( !p_sample || !p_sample->data.p_data )
851 "cannot find SampleEntry (track[Id 0x%x])",
852 p_track->i_track_ID );
853 return( VLC_EGENERIC );
856 p_track->p_sample = p_sample;
858 if( p_track->i_sample_size == 1 )
860 MP4_Box_data_sample_soun_t *p_soun;
862 p_soun = p_sample->data.p_sample_soun;
864 if( p_soun->i_qt_version == 0 )
866 switch( p_sample->i_type )
868 case VLC_FOURCC( 'i', 'm', 'a', '4' ):
869 p_soun->i_qt_version = 1;
870 p_soun->i_sample_per_packet = 64;
871 p_soun->i_bytes_per_packet = 34;
872 p_soun->i_bytes_per_frame = 34 * p_soun->i_channelcount;
873 p_soun->i_bytes_per_sample = 2;
875 case VLC_FOURCC( 'M', 'A', 'C', '3' ):
876 p_soun->i_qt_version = 1;
877 p_soun->i_sample_per_packet = 6;
878 p_soun->i_bytes_per_packet = 2;
879 p_soun->i_bytes_per_frame = 2 * p_soun->i_channelcount;
880 p_soun->i_bytes_per_sample = 2;
882 case VLC_FOURCC( 'M', 'A', 'C', '6' ):
883 p_soun->i_qt_version = 1;
884 p_soun->i_sample_per_packet = 12;
885 p_soun->i_bytes_per_packet = 2;
886 p_soun->i_bytes_per_frame = 2 * p_soun->i_channelcount;
887 p_soun->i_bytes_per_sample = 2;
894 else if( p_soun->i_qt_version == 1 && p_soun->i_sample_per_packet <= 0 )
896 p_soun->i_qt_version = 0;
901 /* It's a little ugly but .. there are special cases */
902 switch( p_sample->i_type )
904 case( VLC_FOURCC( '.', 'm', 'p', '3' ) ):
905 case( VLC_FOURCC( 'm', 's', 0x00, 0x55 ) ):
906 p_track->fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'a' );
908 case( VLC_FOURCC( 'r', 'a', 'w', ' ' ) ):
909 p_track->fmt.i_codec = VLC_FOURCC( 'a', 'r', 'a', 'w' );
911 case( VLC_FOURCC( 's', '2', '6', '3' ) ):
912 p_track->fmt.i_codec = VLC_FOURCC( 'h', '2', '6', '3' );
915 p_track->fmt.i_codec = p_sample->i_type;
919 /* now see if esds is present and if so create a data packet
920 with decoder_specific_info */
921 #define p_decconfig p_esds->data.p_esds->es_descriptor.p_decConfigDescr
922 if( ( ( p_esds = MP4_BoxGet( p_sample, "esds" ) ) ||
923 ( p_esds = MP4_BoxGet( p_sample, "wave/esds" ) ) )&&
924 ( p_esds->data.p_esds )&&
927 /* First update information based on i_objectTypeIndication */
928 switch( p_decconfig->i_objectTypeIndication )
930 case( 0x20 ): /* MPEG4 VIDEO */
931 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','4','v' );
934 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','4','a' );
941 case( 0x65): /* MPEG2 video */
942 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','g','v' );
944 /* Theses are MPEG2-AAC */
945 case( 0x66): /* main profile */
946 case( 0x67): /* Low complexity profile */
947 case( 0x68): /* Scaleable Sampling rate profile */
948 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','4','a' );
950 /* true MPEG 2 audio */
952 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','g','a' );
954 case( 0x6a): /* MPEG1 video */
955 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','g','v' );
957 case( 0x6b): /* MPEG1 audio */
958 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','g','a' );
960 case( 0x6c ): /* jpeg */
961 p_track->fmt.i_codec = VLC_FOURCC( 'j','p','e','g' );
964 /* Unknown entry, but don't touch i_fourcc */
966 "unknown objectTypeIndication(0x%x) (Track[ID 0x%x])",
967 p_decconfig->i_objectTypeIndication,
968 p_track->i_track_ID );
971 p_track->fmt.i_extra = p_decconfig->i_decoder_specific_info_len;
972 if( p_track->fmt.i_extra > 0 )
974 p_track->fmt.p_extra = malloc( p_track->fmt.i_extra );
975 memcpy( p_track->fmt.p_extra, p_decconfig->p_decoder_specific_info,
976 p_track->fmt.i_extra );
981 switch( p_sample->i_type )
983 /* qt decoder, send the complete chunk */
984 case VLC_FOURCC( 'S', 'V', 'Q', '3' ):
985 case VLC_FOURCC( 'S', 'V', 'Q', '1' ):
986 case VLC_FOURCC( 'V', 'P', '3', '1' ):
987 case VLC_FOURCC( '3', 'I', 'V', '1' ):
988 case VLC_FOURCC( 'Z', 'y', 'G', 'o' ):
989 p_track->fmt.i_extra =
990 p_sample->data.p_sample_vide->i_qt_image_description;
991 if( p_track->fmt.i_extra > 0 )
993 p_track->fmt.p_extra = malloc( p_track->fmt.i_extra );
994 memcpy( p_track->fmt.p_extra,
995 p_sample->data.p_sample_vide->p_qt_image_description,
996 p_track->fmt.i_extra);
999 case VLC_FOURCC( 'Q', 'D', 'M', 'C' ):
1000 case VLC_FOURCC( 'Q', 'D', 'M', '2' ):
1001 case VLC_FOURCC( 'Q', 'c', 'l', 'p' ):
1002 case VLC_FOURCC( 's', 'a', 'm', 'r' ):
1003 p_track->fmt.i_extra =
1004 p_sample->data.p_sample_soun->i_qt_description;
1005 if( p_track->fmt.i_extra > 0 )
1007 p_track->fmt.p_extra = malloc( p_track->fmt.i_extra );
1008 memcpy( p_track->fmt.p_extra,
1009 p_sample->data.p_sample_soun->p_qt_description,
1010 p_track->fmt.i_extra);
1020 /* some last initialisation */
1021 switch( p_track->fmt.i_cat )
1024 p_track->fmt.video.i_width = p_sample->data.p_sample_vide->i_width;
1025 p_track->fmt.video.i_height = p_sample->data.p_sample_vide->i_height;
1027 /* fall on display size */
1028 if( p_track->fmt.video.i_width <= 0 )
1029 p_track->fmt.video.i_width = p_track->i_width;
1030 if( p_track->fmt.video.i_height <= 0 )
1031 p_track->fmt.video.i_height = p_track->i_height;
1033 /* Find out apect ratio from display size */
1034 if( p_track->i_width > 0 && p_track->i_height > 0 )
1035 p_track->fmt.video.i_aspect =
1036 VOUT_ASPECT_FACTOR * p_track->i_width / p_track->i_height;
1041 p_track->fmt.audio.i_channels =
1042 p_sample->data.p_sample_soun->i_channelcount;
1043 p_track->fmt.audio.i_rate =
1044 p_sample->data.p_sample_soun->i_sampleratehi;
1045 p_track->fmt.i_bitrate = p_sample->data.p_sample_soun->i_channelcount *
1046 p_sample->data.p_sample_soun->i_sampleratehi *
1047 p_sample->data.p_sample_soun->i_samplesize;
1048 p_track->fmt.audio.i_bitspersample =
1049 p_sample->data.p_sample_soun->i_samplesize;
1056 *pp_es = es_out_Add( p_input->p_es_out, &p_track->fmt );
1061 /* given a time it return sample/chunk */
1062 static int TrackTimeToSampleChunk( input_thread_t *p_input,
1063 track_data_mp4_t *p_track,
1066 uint32_t *pi_sample )
1070 unsigned int i_sample;
1071 unsigned int i_chunk;
1074 /* convert absolute time to in timescale unit */
1075 i_start = i_start * (mtime_t)p_track->i_timescale / (mtime_t)1000000;
1077 /* FIXME see if it's needed to check p_track->i_chunk_count */
1078 if( !p_track->b_ok || p_track->i_chunk_count == 0 )
1080 return( VLC_EGENERIC );
1083 /* we start from sample 0/chunk 0, hope it won't take too much time */
1084 /* *** find good chunk *** */
1085 for( i_chunk = 0; ; i_chunk++ )
1087 if( i_chunk + 1 >= p_track->i_chunk_count )
1089 /* at the end and can't check if i_start in this chunk,
1090 it will be check while searching i_sample */
1091 i_chunk = p_track->i_chunk_count - 1;
1095 if( i_start >= p_track->chunk[i_chunk].i_first_dts &&
1096 i_start < p_track->chunk[i_chunk + 1].i_first_dts )
1102 /* *** find sample in the chunk *** */
1103 i_sample = p_track->chunk[i_chunk].i_sample_first;
1104 i_dts = p_track->chunk[i_chunk].i_first_dts;
1105 for( i_index = 0; i_sample < p_track->chunk[i_chunk].i_sample_count; )
1108 p_track->chunk[i_chunk].p_sample_count_dts[i_index] *
1109 p_track->chunk[i_chunk].p_sample_delta_dts[i_index] < i_start )
1112 p_track->chunk[i_chunk].p_sample_count_dts[i_index] *
1113 p_track->chunk[i_chunk].p_sample_delta_dts[i_index];
1115 i_sample += p_track->chunk[i_chunk].p_sample_count_dts[i_index];
1120 if( p_track->chunk[i_chunk].p_sample_delta_dts[i_index] <= 0 )
1124 i_sample += ( i_start - i_dts ) / p_track->chunk[i_chunk].p_sample_delta_dts[i_index];
1129 if( i_sample >= p_track->i_sample_count )
1132 "track[Id 0x%x] will be disabled (seeking too far) chunk=%d sample=%d",
1133 p_track->i_track_ID, i_chunk, i_sample );
1134 return( VLC_EGENERIC );
1138 /* *** Try to find nearest sync points *** */
1139 if( ( p_stss = MP4_BoxGet( p_track->p_stbl, "stss" ) ) )
1141 unsigned int i_index;
1143 "track[Id 0x%x] using Sync Sample Box (stss)",
1144 p_track->i_track_ID );
1145 for( i_index = 0; i_index < p_stss->data.p_stss->i_entry_count; i_index++ )
1147 if( p_stss->data.p_stss->i_sample_number[i_index] >= i_sample )
1151 msg_Dbg( p_input, "stts gives %d --> %d (sample number)",
1153 p_stss->data.p_stss->i_sample_number[i_index-1] );
1154 i_sample = p_stss->data.p_stss->i_sample_number[i_index-1];
1155 /* new i_sample is less than old so i_chunk can only decreased */
1156 while( i_chunk > 0 &&
1157 i_sample < p_track->chunk[i_chunk].i_sample_first )
1164 msg_Dbg( p_input, "stts gives %d --> %d (sample number)",
1166 p_stss->data.p_stss->i_sample_number[i_index] );
1167 i_sample = p_stss->data.p_stss->i_sample_number[i_index];
1168 /* new i_sample is more than old so i_chunk can only increased */
1169 while( i_chunk < p_track->i_chunk_count - 1 &&
1170 i_sample >= p_track->chunk[i_chunk].i_sample_first +
1171 p_track->chunk[i_chunk].i_sample_count )
1183 "track[Id 0x%x] does not provide Sync Sample Box (stss)",
1184 p_track->i_track_ID );
1187 if( pi_chunk ) *pi_chunk = i_chunk;
1188 if( pi_sample ) *pi_sample = i_sample;
1189 return( VLC_SUCCESS );
1192 static int TrackGotoChunkSample( input_thread_t *p_input,
1193 track_data_mp4_t *p_track,
1194 unsigned int i_chunk,
1195 unsigned int i_sample )
1197 vlc_bool_t b_reselect = VLC_FALSE;
1199 /* now see if actual es is ok */
1200 if( p_track->i_chunk < 0 ||
1201 p_track->i_chunk >= p_track->i_chunk_count ||
1202 p_track->chunk[p_track->i_chunk].i_sample_description_index !=
1203 p_track->chunk[i_chunk].i_sample_description_index )
1205 msg_Warn( p_input, "Recreate ES" );
1207 es_out_Control( p_input->p_es_out, ES_OUT_GET_ES_STATE, p_track->p_es, &b_reselect );
1209 es_out_Del( p_input->p_es_out, p_track->p_es );
1211 p_track->p_es = NULL;
1213 if( TrackCreateES( p_input,
1217 msg_Err( p_input, "cannot create es for track[Id 0x%x]",
1218 p_track->i_track_ID );
1220 p_track->b_ok = VLC_FALSE;
1221 p_track->b_selected = VLC_FALSE;
1222 return VLC_EGENERIC;
1226 /* select again the new decoder */
1229 es_out_Control( p_input->p_es_out, ES_OUT_SET_ES, p_track->p_es );
1232 p_track->i_chunk = i_chunk;
1233 p_track->i_sample = i_sample;
1235 return p_track->b_selected ? VLC_SUCCESS : VLC_EGENERIC;
1238 /****************************************************************************
1240 ****************************************************************************
1241 * Parse track information and create all needed data to run a track
1242 * If it succeed b_ok is set to 1 else to 0
1243 ****************************************************************************/
1244 static void MP4_TrackCreate( input_thread_t *p_input,
1245 track_data_mp4_t *p_track,
1246 MP4_Box_t * p_box_trak )
1248 MP4_Box_t *p_tkhd = MP4_BoxGet( p_box_trak, "tkhd" );
1249 MP4_Box_t *p_tref = MP4_BoxGet( p_box_trak, "tref" );
1263 /* hint track unsuported */
1265 /* set default value (-> track unusable) */
1266 p_track->b_ok = VLC_FALSE;
1267 p_track->b_enable = VLC_FALSE;
1268 p_track->b_selected = VLC_FALSE;
1270 es_format_Init( &p_track->fmt, UNKNOWN_ES, 0 );
1277 /* do we launch this track by default ? */
1279 ( ( p_tkhd->data.p_tkhd->i_flags&MP4_TRACK_ENABLED ) != 0 );
1281 p_track->i_track_ID = p_tkhd->data.p_tkhd->i_track_ID;
1282 p_track->i_width = p_tkhd->data.p_tkhd->i_width / 65536;
1283 p_track->i_height = p_tkhd->data.p_tkhd->i_height / 65536;
1285 if( ( p_elst = MP4_BoxGet( p_box_trak, "edts/elst" ) ) )
1287 /* msg_Warn( p_input, "unhandled box: edts --> FIXME" ); */
1292 /* msg_Warn( p_input, "unhandled box: tref --> FIXME" ); */
1295 p_mdhd = MP4_BoxGet( p_box_trak, "mdia/mdhd" );
1296 p_hdlr = MP4_BoxGet( p_box_trak, "mdia/hdlr" );
1298 if( ( !p_mdhd )||( !p_hdlr ) )
1303 p_track->i_timescale = p_mdhd->data.p_mdhd->i_timescale;
1305 for( i = 0; i < 3; i++ )
1307 language[i] = p_mdhd->data.p_mdhd->i_language[i];
1311 switch( p_hdlr->data.p_hdlr->i_handler_type )
1313 case( FOURCC_soun ):
1314 if( !( p_smhd = MP4_BoxGet( p_box_trak, "mdia/minf/smhd" ) ) )
1318 p_track->fmt.i_cat = AUDIO_ES;
1321 case( FOURCC_vide ):
1322 if( !( p_vmhd = MP4_BoxGet( p_box_trak, "mdia/minf/vmhd" ) ) )
1326 p_track->fmt.i_cat = VIDEO_ES;
1334 p_dinf = MP4_BoxGet( p_minf, "dinf" );
1336 if( !( p_track->p_stbl = MP4_BoxGet( p_box_trak,"mdia/minf/stbl" ) ) ||
1337 !( p_track->p_stsd = MP4_BoxGet( p_box_trak,"mdia/minf/stbl/stsd") ) )
1342 p_drms = MP4_BoxGet( p_track->p_stsd, "drms" );
1343 p_track->b_drms = p_drms != NULL;
1344 p_track->p_drms = p_track->b_drms ?
1345 p_drms->data.p_sample_soun->p_drms : NULL;
1348 if( strcmp( language, "```" ) && strcmp( language, "und" ) )
1350 p_track->fmt.psz_language = LanguageGetName( language );
1353 /* fxi i_timescale for AUDIO_ES with i_qt_version == 0 */
1354 if( p_track->fmt.i_cat == AUDIO_ES ) //&& p_track->i_sample_size == 1 )
1356 MP4_Box_t *p_sample;
1358 p_sample = MP4_BoxGet( p_track->p_stsd, "[0]" );
1359 if( p_sample && p_sample->data.p_sample_soun)
1361 MP4_Box_data_sample_soun_t *p_soun = p_sample->data.p_sample_soun;
1362 if( p_soun->i_qt_version == 0 &&
1363 p_track->i_timescale != p_soun->i_sampleratehi )
1366 "i_timescale ("I64Fu") != i_sampleratehi (%u) with "
1368 "Making both equal. (report any problem)",
1369 p_track->i_timescale, p_soun->i_sampleratehi );
1371 if( p_soun->i_sampleratehi )
1372 p_track->i_timescale = p_soun->i_sampleratehi;
1374 p_soun->i_sampleratehi = p_track->i_timescale;
1379 /* Create chunk index table and sample index table */
1380 if( TrackCreateChunksIndex( p_input,p_track ) ||
1381 TrackCreateSamplesIndex( p_input, p_track ) )
1383 return; /* cannot create chunks index */
1386 p_track->i_chunk = 0;
1387 p_track->i_sample = 0;
1390 if( TrackCreateES( p_input,
1391 p_track, p_track->i_chunk,
1394 msg_Err( p_input, "cannot create es for track[Id 0x%x]",
1395 p_track->i_track_ID );
1402 for( i = 0; i < p_track->i_chunk_count; i++ )
1404 fprintf( stderr, "%-5d sample_count=%d pts=%lld\n", i, p_track->chunk[i].i_sample_count, p_track->chunk[i].i_first_dts );
1409 p_track->b_ok = VLC_TRUE;
1412 /****************************************************************************
1414 ****************************************************************************
1415 * Destroy a track created by MP4_TrackCreate.
1416 ****************************************************************************/
1417 static void MP4_TrackDestroy( input_thread_t *p_input,
1418 track_data_mp4_t *p_track )
1420 unsigned int i_chunk;
1422 p_track->b_ok = VLC_FALSE;
1423 p_track->b_enable = VLC_FALSE;
1424 p_track->b_selected = VLC_FALSE;
1426 es_format_Init( &p_track->fmt, UNKNOWN_ES, 0 );
1428 for( i_chunk = 0; i_chunk < p_track->i_chunk_count; i_chunk++ )
1430 if( p_track->chunk )
1432 FREE(p_track->chunk[i_chunk].p_sample_count_dts);
1433 FREE(p_track->chunk[i_chunk].p_sample_delta_dts );
1436 FREE( p_track->chunk );
1438 if( !p_track->i_sample_size )
1440 FREE( p_track->p_sample_size );
1444 static int MP4_TrackSelect ( input_thread_t *p_input,
1445 track_data_mp4_t *p_track,
1451 if( !p_track->b_ok )
1453 return( VLC_EGENERIC );
1456 if( p_track->b_selected )
1459 "track[Id 0x%x] already selected",
1460 p_track->i_track_ID );
1461 return( VLC_SUCCESS );
1464 if( TrackTimeToSampleChunk( p_input,
1466 &i_chunk, &i_sample ) )
1469 "cannot select track[Id 0x%x]",
1470 p_track->i_track_ID );
1471 return( VLC_EGENERIC );
1474 p_track->b_selected = VLC_TRUE;
1476 if( TrackGotoChunkSample( p_input, p_track, i_chunk, i_sample ) )
1478 p_track->b_selected = VLC_FALSE;
1480 return p_track->b_selected ? VLC_SUCCESS : VLC_EGENERIC;
1483 static void MP4_TrackUnselect(input_thread_t *p_input,
1484 track_data_mp4_t *p_track )
1486 if( !p_track->b_ok )
1491 if( !p_track->b_selected )
1494 "track[Id 0x%x] already unselected",
1495 p_track->i_track_ID );
1500 es_out_Control( p_input->p_es_out, ES_OUT_SET_ES_STATE, p_track->p_es, VLC_FALSE );
1503 p_track->b_selected = VLC_FALSE;
1506 static int MP4_TrackSeek ( input_thread_t *p_input,
1507 track_data_mp4_t *p_track,
1513 if( !p_track->b_ok )
1515 return( VLC_EGENERIC );
1518 if( TrackTimeToSampleChunk( p_input,
1520 &i_chunk, &i_sample ) )
1523 "cannot select track[Id 0x%x]",
1524 p_track->i_track_ID );
1525 return( VLC_EGENERIC );
1528 p_track->b_selected = VLC_TRUE;
1530 TrackGotoChunkSample( p_input, p_track, i_chunk, i_sample );
1532 return( p_track->b_selected ? VLC_SUCCESS : VLC_EGENERIC );
1540 * 3 types: for audio
1543 #define QT_V0_MAX_SAMPLES 1500
1544 static int MP4_TrackSampleSize( track_data_mp4_t *p_track )
1547 MP4_Box_data_sample_soun_t *p_soun;
1549 if( p_track->i_sample_size == 0 )
1551 /* most simple case */
1552 return( p_track->p_sample_size[p_track->i_sample] );
1554 if( p_track->fmt.i_cat != AUDIO_ES )
1556 return( p_track->i_sample_size );
1559 if( p_track->i_sample_size != 1 )
1561 //msg_Warn( p_input, "SampleSize != 1" );
1562 return( p_track->i_sample_size );
1565 p_soun = p_track->p_sample->data.p_sample_soun;
1567 if( p_soun->i_qt_version == 1 )
1569 i_size = p_track->chunk[p_track->i_chunk].i_sample_count / p_soun->i_sample_per_packet * p_soun->i_bytes_per_frame;
1574 int i_samples = p_track->chunk[p_track->i_chunk].i_sample_count -
1575 ( p_track->i_sample - p_track->chunk[p_track->i_chunk].i_sample_first );
1576 if( i_samples > QT_V0_MAX_SAMPLES )
1578 i_samples = QT_V0_MAX_SAMPLES;
1580 i_size = i_samples * p_soun->i_channelcount * p_soun->i_samplesize / 8;
1583 //fprintf( stderr, "size=%d\n", i_size );
1588 static uint64_t MP4_GetTrackPos( track_data_mp4_t *p_track )
1590 unsigned int i_sample;
1594 i_pos = p_track->chunk[p_track->i_chunk].i_offset;
1596 if( p_track->i_sample_size )
1598 MP4_Box_data_sample_soun_t *p_soun = p_track->p_sample->data.p_sample_soun;
1600 if( p_soun->i_qt_version == 0 )
1602 i_pos += ( p_track->i_sample - p_track->chunk[p_track->i_chunk].i_sample_first ) *
1603 p_soun->i_channelcount * p_soun->i_samplesize / 8;
1607 /* we read chunk by chunk */
1613 for( i_sample = p_track->chunk[p_track->i_chunk].i_sample_first;
1614 i_sample < p_track->i_sample; i_sample++ )
1616 i_pos += p_track->p_sample_size[i_sample];
1623 static int MP4_TrackNextSample( input_thread_t *p_input,
1624 track_data_mp4_t *p_track )
1627 if( p_track->fmt.i_cat == AUDIO_ES &&
1628 p_track->i_sample_size != 0 )
1630 MP4_Box_data_sample_soun_t *p_soun;
1632 p_soun = p_track->p_sample->data.p_sample_soun;
1634 if( p_soun->i_qt_version == 1 )
1636 /* chunk by chunk */
1638 p_track->chunk[p_track->i_chunk].i_sample_first +
1639 p_track->chunk[p_track->i_chunk].i_sample_count;
1644 p_track->i_sample += QT_V0_MAX_SAMPLES;
1645 if( p_track->i_sample > p_track->chunk[p_track->i_chunk].i_sample_first + p_track->chunk[p_track->i_chunk].i_sample_count )
1648 p_track->chunk[p_track->i_chunk].i_sample_first +
1649 p_track->chunk[p_track->i_chunk].i_sample_count;
1655 p_track->i_sample++;
1658 if( p_track->i_sample >= p_track->i_sample_count )
1660 /* we have reach end of the track so free decoder stuff */
1661 msg_Warn( p_input, "track[0x%x] will be disabled", p_track->i_track_ID );
1662 MP4_TrackUnselect( p_input, p_track );
1663 return( VLC_EGENERIC );
1666 /* Have we changed chunk ? */
1667 if( p_track->i_sample >=
1668 p_track->chunk[p_track->i_chunk].i_sample_first +
1669 p_track->chunk[p_track->i_chunk].i_sample_count )
1671 if( TrackGotoChunkSample( p_input,
1673 p_track->i_chunk + 1,
1674 p_track->i_sample ) )
1676 msg_Warn( p_input, "track[0x%x] will be disabled (cannot restart decoder)", p_track->i_track_ID );
1677 MP4_TrackUnselect( p_input, p_track );
1678 return( VLC_EGENERIC );
1682 return( VLC_SUCCESS );
1686 static char *LanguageGetName( const char *psz_code )
1688 const iso639_lang_t *pl;
1690 pl = GetLang_2B( psz_code );
1691 if( !strcmp( pl->psz_iso639_1, "??" ) )
1693 pl = GetLang_2T( psz_code );
1696 if( !strcmp( pl->psz_iso639_1, "??" ) )
1698 return strdup( psz_code );
1701 if( *pl->psz_native_name )
1703 return strdup( pl->psz_native_name );
1705 return strdup( pl->psz_eng_name );