1 /*****************************************************************************
2 * mp4.c : MP4 file input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: mp4.c,v 1.53 2004/01/18 06:33:21 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] ) )
115 case VLC_FOURCC( 'p', 'n', 'o', 't' ):
118 msg_Warn( p_input, "MP4 plugin discarded (not a valid file)" );
123 stream_Control( p_input->s, STREAM_CAN_SEEK, &b_seekable );
126 msg_Warn( p_input, "MP4 plugin discarded (unseekable)" );
130 /*Set exported functions */
131 p_input->pf_demux = Demux;
132 p_input->pf_demux_control = Control;
134 /* create our structure that will contains all data */
135 p_input->p_demux_data = p_sys = malloc( sizeof( demux_sys_t ) );
136 memset( p_sys, 0, sizeof( demux_sys_t ) );
138 /* Now load all boxes ( except raw data ) */
139 if( ( p_sys->p_root = MP4_BoxGetRoot( p_input ) ) == NULL )
141 msg_Warn( p_input, "MP4 plugin discarded (not a valid file)" );
145 MP4_BoxDumpStructure( p_input, p_sys->p_root );
147 if( ( p_ftyp = MP4_BoxGet( p_sys->p_root, "/ftyp" ) ) )
149 switch( p_ftyp->data.p_ftyp->i_major_brand )
153 "ISO Media file (isom) version %d.",
154 p_ftyp->data.p_ftyp->i_minor_version );
158 "unrecognized major file specification (%4.4s).",
159 (char*)&p_ftyp->data.p_ftyp->i_major_brand );
165 msg_Dbg( p_input, "file type box missing (assuming ISO Media file)" );
168 /* the file need to have one moov box */
169 if( MP4_BoxCount( p_sys->p_root, "/moov" ) <= 0 )
171 MP4_Box_t *p_foov = MP4_BoxGet( p_sys->p_root, "/foov" );
175 msg_Err( p_input, "MP4 plugin discarded (no moov box)" );
178 /* we have a free box as a moov, rename it */
179 p_foov->i_type = FOURCC_moov;
182 if( ( p_rmra = MP4_BoxGet( p_sys->p_root, "/moov/rmra" ) ) )
184 playlist_t *p_playlist;
185 int i_count = MP4_BoxCount( p_rmra, "rmda" );
188 msg_Dbg( p_input, "detected playlist mov file (%d ref)", i_count );
191 (playlist_t *)vlc_object_find( p_input,
196 //p_playlist->pp_items[p_playlist->i_index]->b_autodeletion = VLC_TRUE;
197 for( i = 0; i < i_count; i++ )
199 MP4_Box_t *p_rdrf = MP4_BoxGet( p_rmra, "rmda[%d]/rdrf", i );
203 if( !p_rdrf || !( psz_ref = p_rdrf->data.p_rdrf->psz_ref ) )
207 i_ref_type = p_rdrf->data.p_rdrf->i_ref_type;
209 msg_Dbg( p_input, "new ref=`%s' type=%4.4s",
210 psz_ref, (char*)&i_ref_type );
212 if( i_ref_type == VLC_FOURCC( 'u', 'r', 'l', ' ' ) )
214 if( strstr( psz_ref, "qt5gateQT" ) )
216 msg_Dbg( p_input, "ignoring pseudo ref =`%s'", psz_ref );
219 if( !strncmp( psz_ref, "http://", 7 ) ||
220 !strncmp( psz_ref, "rtsp://", 7 ) )
222 msg_Dbg( p_input, "adding ref = `%s'", psz_ref );
223 playlist_Add( p_playlist, psz_ref, psz_ref,
224 PLAYLIST_APPEND, PLAYLIST_END );
228 /* msg dbg relative ? */
229 char *psz_absolute = alloca( strlen( p_input->psz_source ) + strlen( psz_ref ) + 1);
230 char *end = strrchr( p_input->psz_source, '/' );
234 int i_len = end + 1 - p_input->psz_source;
236 strncpy( psz_absolute, p_input->psz_source, i_len);
237 psz_absolute[i_len] = '\0';
241 strcpy( psz_absolute, "" );
243 strcat( psz_absolute, psz_ref );
244 msg_Dbg( p_input, "adding ref = `%s'", psz_absolute );
245 playlist_Add( p_playlist, psz_absolute, psz_absolute,
246 PLAYLIST_APPEND, PLAYLIST_END );
251 msg_Err( p_input, "unknown ref type=%4.4s FIXME (send a bug report)", (char*)&p_rdrf->data.p_rdrf->i_ref_type );
254 vlc_object_release( p_playlist );
258 msg_Err( p_input, "can't find playlist" );
262 if( !(p_mvhd = MP4_BoxGet( p_sys->p_root, "/moov/mvhd" ) ) )
266 msg_Err( p_input, "cannot find /moov/mvhd" );
271 msg_Warn( p_input, "cannot find /moov/mvhd (pure ref file)" );
272 p_input->pf_demux = DemuxRef;
278 p_sys->i_timescale = p_mvhd->data.p_mvhd->i_timescale;
279 p_sys->i_duration = p_mvhd->data.p_mvhd->i_duration;
282 if( !( p_sys->i_tracks = MP4_BoxCount( p_sys->p_root, "/moov/trak" ) ) )
284 msg_Err( p_input, "cannot find any /moov/trak" );
287 msg_Dbg( p_input, "find %d track%c",
289 p_sys->i_tracks ? 's':' ' );
291 /* create one program */
292 vlc_mutex_lock( &p_input->stream.stream_lock );
293 if( input_InitStream( p_input, 0 ) == -1)
295 vlc_mutex_unlock( &p_input->stream.stream_lock );
296 msg_Err( p_input, "cannot init stream" );
299 if( p_sys->i_duration/p_sys->i_timescale > 0 )
301 p_input->stream.i_mux_rate =
302 p_input->stream.p_selected_area->i_size / 50 /
303 ( p_sys->i_duration / p_sys->i_timescale );
307 p_input->stream.i_mux_rate = 0;
309 vlc_mutex_unlock( &p_input->stream.stream_lock );
312 /* allocate memory */
313 p_sys->track = calloc( p_sys->i_tracks, sizeof( track_data_mp4_t ) );
314 memset( p_sys->track, 0, p_sys->i_tracks * sizeof( track_data_mp4_t ) );
316 /* now process each track and extract all usefull informations */
317 for( i = 0; i < p_sys->i_tracks; i++ )
319 p_trak = MP4_BoxGet( p_sys->p_root, "/moov/trak[%d]", i );
320 MP4_TrackCreate( p_input, &p_sys->track[i], p_trak );
322 if( p_sys->track[i].b_ok )
325 switch( p_sys->track[i].fmt.i_cat )
338 msg_Dbg( p_input, "adding track[Id 0x%x] %s (%s) language %s",
339 p_sys->track[i].i_track_ID,
341 p_sys->track[i].b_enable ? "enable":"disable",
342 p_sys->track[i].fmt.psz_language ? p_sys->track[i].fmt.psz_language : "undef" );
346 msg_Dbg( p_input, "ignoring track[Id 0x%x]", p_sys->track[i].i_track_ID );
355 MP4_BoxFree( p_input, p_sys->p_root );
361 /*****************************************************************************
362 * Demux: read packet and send them to decoders
363 *****************************************************************************
364 * TODO check for newly selected track (ie audio upt to now )
365 *****************************************************************************/
366 static int Demux( input_thread_t *p_input )
368 demux_sys_t *p_sys = p_input->p_demux_data;
369 unsigned int i_track;
372 unsigned int i_track_selected;
373 vlc_bool_t b_play_audio;
375 /* check for newly selected/unselected track */
376 for( i_track = 0, i_track_selected = 0; i_track < p_sys->i_tracks; i_track++ )
378 #define track p_sys->track[i_track]
379 if( track.b_selected && track.i_sample >= track.i_sample_count )
381 msg_Warn( p_input, "track[0x%x] will be disabled", track.i_track_ID );
382 MP4_TrackUnselect( p_input, &track );
384 else if( track.b_ok )
387 es_out_Control( p_input->p_es_out, ES_OUT_GET_ES_STATE, track.p_es, &b );
389 if( track.b_selected && !b )
391 MP4_TrackUnselect( p_input, &track );
393 else if( !track.b_selected && b)
395 MP4_TrackSelect( p_input, &track, MP4_GetMoviePTS( p_sys ) );
398 if( track.b_selected )
406 if( i_track_selected <= 0 )
408 msg_Warn( p_input, "no track selected, exiting..." );
412 /* first wait for the good time to read a packet */
413 input_ClockManageRef( p_input,
414 p_input->stream.p_selected_program,
417 /* update pcr XXX in mpeg scale so in 90000 unit/s */
418 p_sys->i_pcr = MP4_GetMoviePTS( p_sys ) * 9 / 100;
420 /* we will read 100ms for each stream so ...*/
421 p_sys->i_time += __MAX( p_sys->i_timescale / 10 , 1 );
423 /* Check if we need to send the audio data to decoder */
424 b_play_audio = !p_input->stream.control.b_mute;
426 for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
428 #define track p_sys->track[i_track]
431 MP4_GetTrackPTS( &track ) >= MP4_GetMoviePTS( p_sys ) )
436 while( MP4_GetTrackPTS( &track ) < MP4_GetMoviePTS( p_sys ) )
439 if( ( !b_play_audio && track.fmt.i_cat == AUDIO_ES ) ||
440 MP4_TrackSampleSize( &track ) <= 0 )
442 if( MP4_TrackNextSample( p_input, &track ) )
452 if( stream_Seek( p_input->s, MP4_GetTrackPos( &track ) ) )
454 msg_Warn( p_input, "track[0x%x] will be disabled (eof?)", track.i_track_ID );
455 MP4_TrackUnselect( p_input, &track );
460 if( ( p_block = stream_Block( p_input->s,
461 MP4_TrackSampleSize( &track ) ) ) == NULL )
463 msg_Warn( p_input, "track[0x%x] will be disabled (eof?)", track.i_track_ID );
464 MP4_TrackUnselect( p_input, &track );
468 if( track.b_drms && track.p_drms )
470 drms_decrypt( track.p_drms,
471 (uint32_t *)p_block->p_buffer,
475 if( track.fmt.i_cat == VIDEO_ES )
477 /* FIXME sometime we can calculate PTS */
480 input_ClockGetTS( p_input,
481 p_input->stream.p_selected_program,
482 MP4_GetTrackPTS( &track ) * 9/100 );
488 input_ClockGetTS( p_input,
489 p_input->stream.p_selected_program,
490 MP4_GetTrackPTS( &track ) * 9/100 );
493 if( !track.b_drms || ( track.b_drms && track.p_drms ) )
495 es_out_Send( p_input->p_es_out, track.p_es, p_block );
498 if( MP4_TrackNextSample( p_input, &track ) )
509 /*****************************************************************************
510 * Seek: Got to i_date
511 ******************************************************************************/
512 static int Seek ( input_thread_t *p_input, mtime_t i_date )
514 demux_sys_t *p_sys = p_input->p_demux_data;
515 unsigned int i_track;
516 /* First update update global time */
517 p_sys->i_time = i_date * p_sys->i_timescale / 1000000;
518 p_sys->i_pcr = i_date* 9 / 100;
520 /* Now for each stream try to go to this time */
521 for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
523 #define track p_sys->track[i_track]
524 if( track.b_ok && track.b_selected )
526 MP4_TrackSeek( p_input, &track, i_date );
533 /*****************************************************************************
535 *****************************************************************************/
536 static int Control ( input_thread_t *p_input, int i_query, va_list args )
538 demux_sys_t *p_sys = p_input->p_demux_data;
545 case DEMUX_GET_POSITION:
546 pf = (double*)va_arg( args, double * );
547 if( p_sys->i_duration > 0 )
549 *pf = (double)p_sys->i_time / (double)p_sys->i_duration;
557 case DEMUX_SET_POSITION:
558 f = (double)va_arg( args, double );
561 (double)p_sys->i_duration /
562 (double)p_sys->i_timescale );
563 return Seek( p_input, i64 );
566 pi64 = (int64_t*)va_arg( args, int64_t * );
567 *pi64 = (mtime_t)1000000 *
568 (mtime_t)p_sys->i_time /
569 (mtime_t)p_sys->i_timescale;
573 i64 = (int64_t)va_arg( args, int64_t );
574 return Seek( p_input, i64 );
576 case DEMUX_GET_LENGTH:
577 pi64 = (int64_t*)va_arg( args, int64_t * );
578 *pi64 = (mtime_t)1000000 *
579 (mtime_t)p_sys->i_duration /
580 (mtime_t)p_sys->i_timescale;
583 msg_Warn( p_input, "DEMUX_GET_FPS unimplemented !!" );
586 msg_Err( p_input, "control query unimplemented !!!" );
587 return demux_vaControlDefault( p_input, i_query, args );
591 /*****************************************************************************
592 * Close: frees unused data
593 *****************************************************************************/
594 static void Close ( vlc_object_t * p_this )
596 unsigned int i_track;
597 input_thread_t * p_input = (input_thread_t *)p_this;
598 demux_sys_t *p_sys = p_input->p_demux_data;
600 msg_Dbg( p_input, "freeing all memory" );
602 MP4_BoxFree( p_input, p_sys->p_root );
603 for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
605 MP4_TrackDestroy( p_input, &p_sys->track[i_track] );
607 FREE( p_sys->track );
614 /****************************************************************************
615 * Local functions, specific to vlc
616 ****************************************************************************/
618 /* now create basic chunk data, the rest will be filled by MP4_CreateSamplesIndex */
619 static int TrackCreateChunksIndex( input_thread_t *p_input,
620 track_data_mp4_t *p_demux_track )
622 MP4_Box_t *p_co64; /* give offset for each chunk, same for stco and co64 */
625 unsigned int i_chunk;
626 unsigned int i_index, i_last;
628 if( ( !(p_co64 = MP4_BoxGet( p_demux_track->p_stbl, "stco" ) )&&
629 !(p_co64 = MP4_BoxGet( p_demux_track->p_stbl, "co64" ) ) )||
630 ( !(p_stsc = MP4_BoxGet( p_demux_track->p_stbl, "stsc" ) ) ))
632 return( VLC_EGENERIC );
635 p_demux_track->i_chunk_count = p_co64->data.p_co64->i_entry_count;
636 if( !p_demux_track->i_chunk_count )
638 msg_Warn( p_input, "no chunk defined" );
639 return( VLC_EGENERIC );
641 p_demux_track->chunk = calloc( p_demux_track->i_chunk_count,
642 sizeof( chunk_data_mp4_t ) );
644 /* first we read chunk offset */
645 for( i_chunk = 0; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
647 p_demux_track->chunk[i_chunk].i_offset =
648 p_co64->data.p_co64->i_chunk_offset[i_chunk];
651 /* now we read index for SampleEntry( soun vide mp4a mp4v ...)
652 to be used for the sample XXX begin to 1
653 We construct it begining at the end */
654 i_last = p_demux_track->i_chunk_count; /* last chunk proceded */
655 i_index = p_stsc->data.p_stsc->i_entry_count;
658 msg_Warn( p_input, "cannot read chunk table or table empty" );
659 return( VLC_EGENERIC );
665 for( i_chunk = p_stsc->data.p_stsc->i_first_chunk[i_index] - 1;
666 i_chunk < i_last; i_chunk++ )
668 p_demux_track->chunk[i_chunk].i_sample_description_index =
669 p_stsc->data.p_stsc->i_sample_description_index[i_index];
670 p_demux_track->chunk[i_chunk].i_sample_count =
671 p_stsc->data.p_stsc->i_samples_per_chunk[i_index];
673 i_last = p_stsc->data.p_stsc->i_first_chunk[i_index] - 1;
676 p_demux_track->chunk[0].i_sample_first = 0;
677 for( i_chunk = 1; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
679 p_demux_track->chunk[i_chunk].i_sample_first =
680 p_demux_track->chunk[i_chunk-1].i_sample_first +
681 p_demux_track->chunk[i_chunk-1].i_sample_count;
685 "track[Id 0x%x] read %d chunk",
686 p_demux_track->i_track_ID,
687 p_demux_track->i_chunk_count );
689 return( VLC_SUCCESS );
691 static int TrackCreateSamplesIndex( input_thread_t *p_input,
692 track_data_mp4_t *p_demux_track )
694 MP4_Box_t *p_stts; /* makes mapping between sample and decoding time,
695 ctts make same mapping but for composition time,
696 not yet used and probably not usefull */
697 MP4_Box_t *p_stsz; /* gives sample size of each samples, there is also stz2
698 that uses a compressed form FIXME make them in libmp4
700 /* TODO use also stss and stsh table for seeking */
701 /* FIXME use edit table */
706 int64_t i_index_sample_used;
710 p_stts = MP4_BoxGet( p_demux_track->p_stbl, "stts" );
711 p_stsz = MP4_BoxGet( p_demux_track->p_stbl, "stsz" ); /* FIXME and stz2 */
713 if( ( !p_stts )||( !p_stsz ) )
715 msg_Warn( p_input, "cannot read sample table" );
716 return( VLC_EGENERIC );
719 p_demux_track->i_sample_count = p_stsz->data.p_stsz->i_sample_count;
722 /* for sample size, there are 2 case */
723 if( p_stsz->data.p_stsz->i_sample_size )
725 /* 1: all sample have the same size, so no need to construct a table */
726 p_demux_track->i_sample_size = p_stsz->data.p_stsz->i_sample_size;
727 p_demux_track->p_sample_size = NULL;
731 /* 2: each sample can have a different size */
732 p_demux_track->i_sample_size = 0;
733 p_demux_track->p_sample_size =
734 calloc( p_demux_track->i_sample_count, sizeof( uint32_t ) );
736 for( i_sample = 0; i_sample < p_demux_track->i_sample_count; i_sample++ )
738 p_demux_track->p_sample_size[i_sample] =
739 p_stsz->data.p_stsz->i_entry_size[i_sample];
742 /* we have extract all information from stsz,
745 /* if we don't want to waste too much memory, we can't expand
746 the box !, so each chunk will contain an "extract" of this table
750 i_index = 0; i_index_sample_used =0;
752 /* create and init last data for each chunk */
753 for(i_chunk = 0 ; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
756 int64_t i_entry, i_sample_count, i;
758 p_demux_track->chunk[i_chunk].i_first_dts = i_last_dts;
759 /* count how many entries needed for this chunk
760 for p_sample_delta_dts and p_sample_count_dts */
762 i_sample_count = p_demux_track->chunk[i_chunk].i_sample_count;
765 while( i_sample_count > 0 )
767 i_sample_count -= p_stts->data.p_stts->i_sample_count[i_index+i_entry];
770 i_sample_count += i_index_sample_used; /* don't count already used sample
777 p_demux_track->chunk[i_chunk].p_sample_count_dts =
778 calloc( i_entry, sizeof( uint32_t ) );
779 p_demux_track->chunk[i_chunk].p_sample_delta_dts =
780 calloc( i_entry, sizeof( uint32_t ) );
783 i_sample_count = p_demux_track->chunk[i_chunk].i_sample_count;
784 for( i = 0; i < i_entry; i++ )
789 i_rest = p_stts->data.p_stts->i_sample_count[i_index] - i_index_sample_used;
791 i_used = __MIN( i_rest, i_sample_count );
793 i_index_sample_used += i_used;
794 i_sample_count -= i_used;
796 p_demux_track->chunk[i_chunk].p_sample_count_dts[i] = i_used;
798 p_demux_track->chunk[i_chunk].p_sample_delta_dts[i] =
799 p_stts->data.p_stts->i_sample_delta[i_index];
801 i_last_dts += i_used *
802 p_demux_track->chunk[i_chunk].p_sample_delta_dts[i];
804 if( i_index_sample_used >=
805 p_stts->data.p_stts->i_sample_count[i_index] )
809 i_index_sample_used = 0;
816 "track[Id 0x%x] read %d samples length:"I64Fd"s",
817 p_demux_track->i_track_ID,
818 p_demux_track->i_sample_count,
819 i_last_dts / p_demux_track->i_timescale );
821 return( VLC_SUCCESS );
826 * Create ES and PES to init decoder if needed, for a track starting at i_chunk
828 static int TrackCreateES ( input_thread_t *p_input,
829 track_data_mp4_t *p_track,
830 unsigned int i_chunk,
831 es_out_id_t **pp_es )
838 if( !p_track->chunk[i_chunk].i_sample_description_index )
841 "invalid SampleEntry index (track[Id 0x%x])",
842 p_track->i_track_ID );
846 p_sample = MP4_BoxGet( p_track->p_stsd, "[%d]",
847 p_track->chunk[i_chunk].i_sample_description_index - 1 );
849 if( !p_sample || !p_sample->data.p_data )
852 "cannot find SampleEntry (track[Id 0x%x])",
853 p_track->i_track_ID );
854 return( VLC_EGENERIC );
857 p_track->p_sample = p_sample;
859 if( p_track->i_sample_size == 1 )
861 MP4_Box_data_sample_soun_t *p_soun;
863 p_soun = p_sample->data.p_sample_soun;
865 if( p_soun->i_qt_version == 0 )
867 switch( p_sample->i_type )
869 case VLC_FOURCC( 'i', 'm', 'a', '4' ):
870 p_soun->i_qt_version = 1;
871 p_soun->i_sample_per_packet = 64;
872 p_soun->i_bytes_per_packet = 34;
873 p_soun->i_bytes_per_frame = 34 * p_soun->i_channelcount;
874 p_soun->i_bytes_per_sample = 2;
876 case VLC_FOURCC( 'M', 'A', 'C', '3' ):
877 p_soun->i_qt_version = 1;
878 p_soun->i_sample_per_packet = 6;
879 p_soun->i_bytes_per_packet = 2;
880 p_soun->i_bytes_per_frame = 2 * p_soun->i_channelcount;
881 p_soun->i_bytes_per_sample = 2;
883 case VLC_FOURCC( 'M', 'A', 'C', '6' ):
884 p_soun->i_qt_version = 1;
885 p_soun->i_sample_per_packet = 12;
886 p_soun->i_bytes_per_packet = 2;
887 p_soun->i_bytes_per_frame = 2 * p_soun->i_channelcount;
888 p_soun->i_bytes_per_sample = 2;
895 else if( p_soun->i_qt_version == 1 && p_soun->i_sample_per_packet <= 0 )
897 p_soun->i_qt_version = 0;
902 /* It's a little ugly but .. there are special cases */
903 switch( p_sample->i_type )
905 case( VLC_FOURCC( '.', 'm', 'p', '3' ) ):
906 case( VLC_FOURCC( 'm', 's', 0x00, 0x55 ) ):
907 p_track->fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'a' );
909 case( VLC_FOURCC( 'r', 'a', 'w', ' ' ) ):
910 p_track->fmt.i_codec = VLC_FOURCC( 'a', 'r', 'a', 'w' );
912 case( VLC_FOURCC( 's', '2', '6', '3' ) ):
913 p_track->fmt.i_codec = VLC_FOURCC( 'h', '2', '6', '3' );
916 p_track->fmt.i_codec = p_sample->i_type;
920 /* now see if esds is present and if so create a data packet
921 with decoder_specific_info */
922 #define p_decconfig p_esds->data.p_esds->es_descriptor.p_decConfigDescr
923 if( ( ( p_esds = MP4_BoxGet( p_sample, "esds" ) ) ||
924 ( p_esds = MP4_BoxGet( p_sample, "wave/esds" ) ) )&&
925 ( p_esds->data.p_esds )&&
928 /* First update information based on i_objectTypeIndication */
929 switch( p_decconfig->i_objectTypeIndication )
931 case( 0x20 ): /* MPEG4 VIDEO */
932 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','4','v' );
935 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','4','a' );
942 case( 0x65): /* MPEG2 video */
943 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','g','v' );
945 /* Theses are MPEG2-AAC */
946 case( 0x66): /* main profile */
947 case( 0x67): /* Low complexity profile */
948 case( 0x68): /* Scaleable Sampling rate profile */
949 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','4','a' );
951 /* true MPEG 2 audio */
953 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','g','a' );
955 case( 0x6a): /* MPEG1 video */
956 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','g','v' );
958 case( 0x6b): /* MPEG1 audio */
959 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','g','a' );
961 case( 0x6c ): /* jpeg */
962 p_track->fmt.i_codec = VLC_FOURCC( 'j','p','e','g' );
965 /* Unknown entry, but don't touch i_fourcc */
967 "unknown objectTypeIndication(0x%x) (Track[ID 0x%x])",
968 p_decconfig->i_objectTypeIndication,
969 p_track->i_track_ID );
972 p_track->fmt.i_extra = p_decconfig->i_decoder_specific_info_len;
973 if( p_track->fmt.i_extra > 0 )
975 p_track->fmt.p_extra = malloc( p_track->fmt.i_extra );
976 memcpy( p_track->fmt.p_extra, p_decconfig->p_decoder_specific_info,
977 p_track->fmt.i_extra );
982 switch( p_sample->i_type )
984 /* qt decoder, send the complete chunk */
985 case VLC_FOURCC( 'S', 'V', 'Q', '3' ):
986 case VLC_FOURCC( 'S', 'V', 'Q', '1' ):
987 case VLC_FOURCC( 'V', 'P', '3', '1' ):
988 case VLC_FOURCC( '3', 'I', 'V', '1' ):
989 case VLC_FOURCC( 'Z', 'y', 'G', 'o' ):
990 p_track->fmt.i_extra =
991 p_sample->data.p_sample_vide->i_qt_image_description;
992 if( p_track->fmt.i_extra > 0 )
994 p_track->fmt.p_extra = malloc( p_track->fmt.i_extra );
995 memcpy( p_track->fmt.p_extra,
996 p_sample->data.p_sample_vide->p_qt_image_description,
997 p_track->fmt.i_extra);
1000 case VLC_FOURCC( 'Q', 'D', 'M', 'C' ):
1001 case VLC_FOURCC( 'Q', 'D', 'M', '2' ):
1002 case VLC_FOURCC( 'Q', 'c', 'l', 'p' ):
1003 case VLC_FOURCC( 's', 'a', 'm', 'r' ):
1004 p_track->fmt.i_extra =
1005 p_sample->data.p_sample_soun->i_qt_description;
1006 if( p_track->fmt.i_extra > 0 )
1008 p_track->fmt.p_extra = malloc( p_track->fmt.i_extra );
1009 memcpy( p_track->fmt.p_extra,
1010 p_sample->data.p_sample_soun->p_qt_description,
1011 p_track->fmt.i_extra);
1021 /* some last initialisation */
1022 switch( p_track->fmt.i_cat )
1025 p_track->fmt.video.i_width = p_sample->data.p_sample_vide->i_width;
1026 p_track->fmt.video.i_height = p_sample->data.p_sample_vide->i_height;
1028 /* fall on display size */
1029 if( p_track->fmt.video.i_width <= 0 )
1030 p_track->fmt.video.i_width = p_track->i_width;
1031 if( p_track->fmt.video.i_height <= 0 )
1032 p_track->fmt.video.i_height = p_track->i_height;
1034 /* Find out apect ratio from display size */
1035 if( p_track->i_width > 0 && p_track->i_height > 0 )
1036 p_track->fmt.video.i_aspect =
1037 VOUT_ASPECT_FACTOR * p_track->i_width / p_track->i_height;
1042 p_track->fmt.audio.i_channels =
1043 p_sample->data.p_sample_soun->i_channelcount;
1044 p_track->fmt.audio.i_rate =
1045 p_sample->data.p_sample_soun->i_sampleratehi;
1046 p_track->fmt.i_bitrate = p_sample->data.p_sample_soun->i_channelcount *
1047 p_sample->data.p_sample_soun->i_sampleratehi *
1048 p_sample->data.p_sample_soun->i_samplesize;
1049 p_track->fmt.audio.i_bitspersample =
1050 p_sample->data.p_sample_soun->i_samplesize;
1057 *pp_es = es_out_Add( p_input->p_es_out, &p_track->fmt );
1062 /* given a time it return sample/chunk */
1063 static int TrackTimeToSampleChunk( input_thread_t *p_input,
1064 track_data_mp4_t *p_track,
1067 uint32_t *pi_sample )
1071 unsigned int i_sample;
1072 unsigned int i_chunk;
1075 /* convert absolute time to in timescale unit */
1076 i_start = i_start * (mtime_t)p_track->i_timescale / (mtime_t)1000000;
1078 /* FIXME see if it's needed to check p_track->i_chunk_count */
1079 if( !p_track->b_ok || p_track->i_chunk_count == 0 )
1081 return( VLC_EGENERIC );
1084 /* we start from sample 0/chunk 0, hope it won't take too much time */
1085 /* *** find good chunk *** */
1086 for( i_chunk = 0; ; i_chunk++ )
1088 if( i_chunk + 1 >= p_track->i_chunk_count )
1090 /* at the end and can't check if i_start in this chunk,
1091 it will be check while searching i_sample */
1092 i_chunk = p_track->i_chunk_count - 1;
1096 if( i_start >= p_track->chunk[i_chunk].i_first_dts &&
1097 i_start < p_track->chunk[i_chunk + 1].i_first_dts )
1103 /* *** find sample in the chunk *** */
1104 i_sample = p_track->chunk[i_chunk].i_sample_first;
1105 i_dts = p_track->chunk[i_chunk].i_first_dts;
1106 for( i_index = 0; i_sample < p_track->chunk[i_chunk].i_sample_count; )
1109 p_track->chunk[i_chunk].p_sample_count_dts[i_index] *
1110 p_track->chunk[i_chunk].p_sample_delta_dts[i_index] < i_start )
1113 p_track->chunk[i_chunk].p_sample_count_dts[i_index] *
1114 p_track->chunk[i_chunk].p_sample_delta_dts[i_index];
1116 i_sample += p_track->chunk[i_chunk].p_sample_count_dts[i_index];
1121 if( p_track->chunk[i_chunk].p_sample_delta_dts[i_index] <= 0 )
1125 i_sample += ( i_start - i_dts ) / p_track->chunk[i_chunk].p_sample_delta_dts[i_index];
1130 if( i_sample >= p_track->i_sample_count )
1133 "track[Id 0x%x] will be disabled (seeking too far) chunk=%d sample=%d",
1134 p_track->i_track_ID, i_chunk, i_sample );
1135 return( VLC_EGENERIC );
1139 /* *** Try to find nearest sync points *** */
1140 if( ( p_stss = MP4_BoxGet( p_track->p_stbl, "stss" ) ) )
1142 unsigned int i_index;
1144 "track[Id 0x%x] using Sync Sample Box (stss)",
1145 p_track->i_track_ID );
1146 for( i_index = 0; i_index < p_stss->data.p_stss->i_entry_count; i_index++ )
1148 if( p_stss->data.p_stss->i_sample_number[i_index] >= i_sample )
1152 msg_Dbg( p_input, "stts gives %d --> %d (sample number)",
1154 p_stss->data.p_stss->i_sample_number[i_index-1] );
1155 i_sample = p_stss->data.p_stss->i_sample_number[i_index-1];
1156 /* new i_sample is less than old so i_chunk can only decreased */
1157 while( i_chunk > 0 &&
1158 i_sample < p_track->chunk[i_chunk].i_sample_first )
1165 msg_Dbg( p_input, "stts gives %d --> %d (sample number)",
1167 p_stss->data.p_stss->i_sample_number[i_index] );
1168 i_sample = p_stss->data.p_stss->i_sample_number[i_index];
1169 /* new i_sample is more than old so i_chunk can only increased */
1170 while( i_chunk < p_track->i_chunk_count - 1 &&
1171 i_sample >= p_track->chunk[i_chunk].i_sample_first +
1172 p_track->chunk[i_chunk].i_sample_count )
1184 "track[Id 0x%x] does not provide Sync Sample Box (stss)",
1185 p_track->i_track_ID );
1188 if( pi_chunk ) *pi_chunk = i_chunk;
1189 if( pi_sample ) *pi_sample = i_sample;
1190 return( VLC_SUCCESS );
1193 static int TrackGotoChunkSample( input_thread_t *p_input,
1194 track_data_mp4_t *p_track,
1195 unsigned int i_chunk,
1196 unsigned int i_sample )
1198 vlc_bool_t b_reselect = VLC_FALSE;
1200 /* now see if actual es is ok */
1201 if( p_track->i_chunk < 0 ||
1202 p_track->i_chunk >= p_track->i_chunk_count ||
1203 p_track->chunk[p_track->i_chunk].i_sample_description_index !=
1204 p_track->chunk[i_chunk].i_sample_description_index )
1206 msg_Warn( p_input, "Recreate ES" );
1208 es_out_Control( p_input->p_es_out, ES_OUT_GET_ES_STATE, p_track->p_es, &b_reselect );
1210 es_out_Del( p_input->p_es_out, p_track->p_es );
1212 p_track->p_es = NULL;
1214 if( TrackCreateES( p_input,
1218 msg_Err( p_input, "cannot create es for track[Id 0x%x]",
1219 p_track->i_track_ID );
1221 p_track->b_ok = VLC_FALSE;
1222 p_track->b_selected = VLC_FALSE;
1223 return VLC_EGENERIC;
1227 /* select again the new decoder */
1230 es_out_Control( p_input->p_es_out, ES_OUT_SET_ES, p_track->p_es );
1233 p_track->i_chunk = i_chunk;
1234 p_track->i_sample = i_sample;
1236 return p_track->b_selected ? VLC_SUCCESS : VLC_EGENERIC;
1239 /****************************************************************************
1241 ****************************************************************************
1242 * Parse track information and create all needed data to run a track
1243 * If it succeed b_ok is set to 1 else to 0
1244 ****************************************************************************/
1245 static void MP4_TrackCreate( input_thread_t *p_input,
1246 track_data_mp4_t *p_track,
1247 MP4_Box_t * p_box_trak )
1249 MP4_Box_t *p_tkhd = MP4_BoxGet( p_box_trak, "tkhd" );
1250 MP4_Box_t *p_tref = MP4_BoxGet( p_box_trak, "tref" );
1264 /* hint track unsuported */
1266 /* set default value (-> track unusable) */
1267 p_track->b_ok = VLC_FALSE;
1268 p_track->b_enable = VLC_FALSE;
1269 p_track->b_selected = VLC_FALSE;
1271 es_format_Init( &p_track->fmt, UNKNOWN_ES, 0 );
1278 /* do we launch this track by default ? */
1280 ( ( p_tkhd->data.p_tkhd->i_flags&MP4_TRACK_ENABLED ) != 0 );
1282 p_track->i_track_ID = p_tkhd->data.p_tkhd->i_track_ID;
1283 p_track->i_width = p_tkhd->data.p_tkhd->i_width / 65536;
1284 p_track->i_height = p_tkhd->data.p_tkhd->i_height / 65536;
1286 if( ( p_elst = MP4_BoxGet( p_box_trak, "edts/elst" ) ) )
1288 /* msg_Warn( p_input, "unhandled box: edts --> FIXME" ); */
1293 /* msg_Warn( p_input, "unhandled box: tref --> FIXME" ); */
1296 p_mdhd = MP4_BoxGet( p_box_trak, "mdia/mdhd" );
1297 p_hdlr = MP4_BoxGet( p_box_trak, "mdia/hdlr" );
1299 if( ( !p_mdhd )||( !p_hdlr ) )
1304 p_track->i_timescale = p_mdhd->data.p_mdhd->i_timescale;
1306 for( i = 0; i < 3; i++ )
1308 language[i] = p_mdhd->data.p_mdhd->i_language[i];
1312 switch( p_hdlr->data.p_hdlr->i_handler_type )
1314 case( FOURCC_soun ):
1315 if( !( p_smhd = MP4_BoxGet( p_box_trak, "mdia/minf/smhd" ) ) )
1319 p_track->fmt.i_cat = AUDIO_ES;
1322 case( FOURCC_vide ):
1323 if( !( p_vmhd = MP4_BoxGet( p_box_trak, "mdia/minf/vmhd" ) ) )
1327 p_track->fmt.i_cat = VIDEO_ES;
1335 p_dinf = MP4_BoxGet( p_minf, "dinf" );
1337 if( !( p_track->p_stbl = MP4_BoxGet( p_box_trak,"mdia/minf/stbl" ) ) ||
1338 !( p_track->p_stsd = MP4_BoxGet( p_box_trak,"mdia/minf/stbl/stsd") ) )
1343 p_drms = MP4_BoxGet( p_track->p_stsd, "drms" );
1344 p_track->b_drms = p_drms != NULL;
1345 p_track->p_drms = p_track->b_drms ?
1346 p_drms->data.p_sample_soun->p_drms : NULL;
1349 if( strcmp( language, "```" ) && strcmp( language, "und" ) )
1351 p_track->fmt.psz_language = LanguageGetName( language );
1354 /* fxi i_timescale for AUDIO_ES with i_qt_version == 0 */
1355 if( p_track->fmt.i_cat == AUDIO_ES ) //&& p_track->i_sample_size == 1 )
1357 MP4_Box_t *p_sample;
1359 p_sample = MP4_BoxGet( p_track->p_stsd, "[0]" );
1360 if( p_sample && p_sample->data.p_sample_soun)
1362 MP4_Box_data_sample_soun_t *p_soun = p_sample->data.p_sample_soun;
1363 if( p_soun->i_qt_version == 0 &&
1364 p_track->i_timescale != p_soun->i_sampleratehi )
1367 "i_timescale ("I64Fu") != i_sampleratehi (%u) with "
1369 "Making both equal. (report any problem)",
1370 p_track->i_timescale, p_soun->i_sampleratehi );
1372 if( p_soun->i_sampleratehi )
1373 p_track->i_timescale = p_soun->i_sampleratehi;
1375 p_soun->i_sampleratehi = p_track->i_timescale;
1380 /* Create chunk index table and sample index table */
1381 if( TrackCreateChunksIndex( p_input,p_track ) ||
1382 TrackCreateSamplesIndex( p_input, p_track ) )
1384 return; /* cannot create chunks index */
1387 p_track->i_chunk = 0;
1388 p_track->i_sample = 0;
1391 if( TrackCreateES( p_input,
1392 p_track, p_track->i_chunk,
1395 msg_Err( p_input, "cannot create es for track[Id 0x%x]",
1396 p_track->i_track_ID );
1403 for( i = 0; i < p_track->i_chunk_count; i++ )
1405 fprintf( stderr, "%-5d sample_count=%d pts=%lld\n", i, p_track->chunk[i].i_sample_count, p_track->chunk[i].i_first_dts );
1410 p_track->b_ok = VLC_TRUE;
1413 /****************************************************************************
1415 ****************************************************************************
1416 * Destroy a track created by MP4_TrackCreate.
1417 ****************************************************************************/
1418 static void MP4_TrackDestroy( input_thread_t *p_input,
1419 track_data_mp4_t *p_track )
1421 unsigned int i_chunk;
1423 p_track->b_ok = VLC_FALSE;
1424 p_track->b_enable = VLC_FALSE;
1425 p_track->b_selected = VLC_FALSE;
1427 es_format_Init( &p_track->fmt, UNKNOWN_ES, 0 );
1429 for( i_chunk = 0; i_chunk < p_track->i_chunk_count; i_chunk++ )
1431 if( p_track->chunk )
1433 FREE(p_track->chunk[i_chunk].p_sample_count_dts);
1434 FREE(p_track->chunk[i_chunk].p_sample_delta_dts );
1437 FREE( p_track->chunk );
1439 if( !p_track->i_sample_size )
1441 FREE( p_track->p_sample_size );
1445 static int MP4_TrackSelect ( input_thread_t *p_input,
1446 track_data_mp4_t *p_track,
1452 if( !p_track->b_ok )
1454 return( VLC_EGENERIC );
1457 if( p_track->b_selected )
1460 "track[Id 0x%x] already selected",
1461 p_track->i_track_ID );
1462 return( VLC_SUCCESS );
1465 if( TrackTimeToSampleChunk( p_input,
1467 &i_chunk, &i_sample ) )
1470 "cannot select track[Id 0x%x]",
1471 p_track->i_track_ID );
1472 return( VLC_EGENERIC );
1475 p_track->b_selected = VLC_TRUE;
1477 if( TrackGotoChunkSample( p_input, p_track, i_chunk, i_sample ) )
1479 p_track->b_selected = VLC_FALSE;
1481 return p_track->b_selected ? VLC_SUCCESS : VLC_EGENERIC;
1484 static void MP4_TrackUnselect(input_thread_t *p_input,
1485 track_data_mp4_t *p_track )
1487 if( !p_track->b_ok )
1492 if( !p_track->b_selected )
1495 "track[Id 0x%x] already unselected",
1496 p_track->i_track_ID );
1501 es_out_Control( p_input->p_es_out, ES_OUT_SET_ES_STATE, p_track->p_es, VLC_FALSE );
1504 p_track->b_selected = VLC_FALSE;
1507 static int MP4_TrackSeek ( input_thread_t *p_input,
1508 track_data_mp4_t *p_track,
1514 if( !p_track->b_ok )
1516 return( VLC_EGENERIC );
1519 if( TrackTimeToSampleChunk( p_input,
1521 &i_chunk, &i_sample ) )
1524 "cannot select track[Id 0x%x]",
1525 p_track->i_track_ID );
1526 return( VLC_EGENERIC );
1529 p_track->b_selected = VLC_TRUE;
1531 TrackGotoChunkSample( p_input, p_track, i_chunk, i_sample );
1533 return( p_track->b_selected ? VLC_SUCCESS : VLC_EGENERIC );
1541 * 3 types: for audio
1544 #define QT_V0_MAX_SAMPLES 1500
1545 static int MP4_TrackSampleSize( track_data_mp4_t *p_track )
1548 MP4_Box_data_sample_soun_t *p_soun;
1550 if( p_track->i_sample_size == 0 )
1552 /* most simple case */
1553 return( p_track->p_sample_size[p_track->i_sample] );
1555 if( p_track->fmt.i_cat != AUDIO_ES )
1557 return( p_track->i_sample_size );
1560 if( p_track->i_sample_size != 1 )
1562 //msg_Warn( p_input, "SampleSize != 1" );
1563 return( p_track->i_sample_size );
1566 p_soun = p_track->p_sample->data.p_sample_soun;
1568 if( p_soun->i_qt_version == 1 )
1570 i_size = p_track->chunk[p_track->i_chunk].i_sample_count / p_soun->i_sample_per_packet * p_soun->i_bytes_per_frame;
1575 int i_samples = p_track->chunk[p_track->i_chunk].i_sample_count -
1576 ( p_track->i_sample - p_track->chunk[p_track->i_chunk].i_sample_first );
1577 if( i_samples > QT_V0_MAX_SAMPLES )
1579 i_samples = QT_V0_MAX_SAMPLES;
1581 i_size = i_samples * p_soun->i_channelcount * p_soun->i_samplesize / 8;
1584 //fprintf( stderr, "size=%d\n", i_size );
1589 static uint64_t MP4_GetTrackPos( track_data_mp4_t *p_track )
1591 unsigned int i_sample;
1595 i_pos = p_track->chunk[p_track->i_chunk].i_offset;
1597 if( p_track->i_sample_size )
1599 MP4_Box_data_sample_soun_t *p_soun = p_track->p_sample->data.p_sample_soun;
1601 if( p_soun->i_qt_version == 0 )
1603 i_pos += ( p_track->i_sample - p_track->chunk[p_track->i_chunk].i_sample_first ) *
1604 p_soun->i_channelcount * p_soun->i_samplesize / 8;
1608 /* we read chunk by chunk */
1614 for( i_sample = p_track->chunk[p_track->i_chunk].i_sample_first;
1615 i_sample < p_track->i_sample; i_sample++ )
1617 i_pos += p_track->p_sample_size[i_sample];
1624 static int MP4_TrackNextSample( input_thread_t *p_input,
1625 track_data_mp4_t *p_track )
1628 if( p_track->fmt.i_cat == AUDIO_ES &&
1629 p_track->i_sample_size != 0 )
1631 MP4_Box_data_sample_soun_t *p_soun;
1633 p_soun = p_track->p_sample->data.p_sample_soun;
1635 if( p_soun->i_qt_version == 1 )
1637 /* chunk by chunk */
1639 p_track->chunk[p_track->i_chunk].i_sample_first +
1640 p_track->chunk[p_track->i_chunk].i_sample_count;
1645 p_track->i_sample += QT_V0_MAX_SAMPLES;
1646 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 )
1649 p_track->chunk[p_track->i_chunk].i_sample_first +
1650 p_track->chunk[p_track->i_chunk].i_sample_count;
1656 p_track->i_sample++;
1659 if( p_track->i_sample >= p_track->i_sample_count )
1661 /* we have reach end of the track so free decoder stuff */
1662 msg_Warn( p_input, "track[0x%x] will be disabled", p_track->i_track_ID );
1663 MP4_TrackUnselect( p_input, p_track );
1664 return( VLC_EGENERIC );
1667 /* Have we changed chunk ? */
1668 if( p_track->i_sample >=
1669 p_track->chunk[p_track->i_chunk].i_sample_first +
1670 p_track->chunk[p_track->i_chunk].i_sample_count )
1672 if( TrackGotoChunkSample( p_input,
1674 p_track->i_chunk + 1,
1675 p_track->i_sample ) )
1677 msg_Warn( p_input, "track[0x%x] will be disabled (cannot restart decoder)", p_track->i_track_ID );
1678 MP4_TrackUnselect( p_input, p_track );
1679 return( VLC_EGENERIC );
1683 return( VLC_SUCCESS );
1687 static char *LanguageGetName( const char *psz_code )
1689 const iso639_lang_t *pl;
1691 pl = GetLang_2B( psz_code );
1692 if( !strcmp( pl->psz_iso639_1, "??" ) )
1694 pl = GetLang_2T( psz_code );
1697 if( !strcmp( pl->psz_iso639_1, "??" ) )
1699 return strdup( psz_code );
1702 if( *pl->psz_native_name )
1704 return strdup( pl->psz_native_name );
1706 return strdup( pl->psz_eng_name );