]> git.sesse.net Git - vlc/blob - src/input/es_out.c
es_out: clean up.
[vlc] / src / input / es_out.c
1 /*****************************************************************************
2  * es_out.c: Es Out handler for input.
3  *****************************************************************************
4  * Copyright (C) 2003-2004 VideoLAN
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>
28
29 #include <vlc/vlc.h>
30 #include <vlc/input.h>
31 #include <vlc/decoder.h>
32
33 #include "vlc_playlist.h"
34 #include "codecs.h"
35 #include "iso_lang.h"
36
37 /*****************************************************************************
38  * Local prototypes
39  *****************************************************************************/
40 struct es_out_id_t
41 {
42     int             i_channel;
43     es_descriptor_t *p_es;
44 };
45
46 struct es_out_sys_t
47 {
48     input_thread_t *p_input;
49
50     /* all es */
51     int         i_id;
52
53     int         i_es;
54     es_out_id_t **es;
55
56     /* mode gestion */
57     vlc_bool_t  b_active;
58     int         i_mode;
59
60     /* es count */
61     int         i_audio;
62     int         i_video;
63     int         i_sub;
64
65     /* es to select */
66     int         i_audio_last;
67     int         i_sub_last;
68
69     /* current main es */
70     es_out_id_t *p_es_audio;
71     es_out_id_t *p_es_video;
72     es_out_id_t *p_es_sub;
73 };
74
75 static es_out_id_t *EsOutAdd    ( es_out_t *, es_format_t * );
76 static int          EsOutSend   ( es_out_t *, es_out_id_t *, block_t * );
77 static void         EsOutDel    ( es_out_t *, es_out_id_t * );
78 static int          EsOutControl( es_out_t *, int i_query, va_list );
79
80 static char *LanguageGetName( const char *psz_code );
81
82 /**
83  * Create a new es_out structure
84  *
85  * \param p_input The related input thread
86  * \return the new es_out_t
87  */
88 es_out_t *input_EsOutNew( input_thread_t *p_input )
89 {
90     es_out_t     *out = malloc( sizeof( es_out_t ) );
91     es_out_sys_t *p_sys = malloc( sizeof( es_out_sys_t ) );
92     vlc_value_t  val;
93
94     out->pf_add     = EsOutAdd;
95     out->pf_send    = EsOutSend;
96     out->pf_del     = EsOutDel;
97     out->pf_control = EsOutControl;
98     out->p_sys      = p_sys;
99
100     p_sys->p_input = p_input;
101
102     p_sys->b_active = VLC_FALSE;
103     p_sys->i_mode   = ES_OUT_MODE_AUTO;
104
105     p_sys->i_id    = 1;
106
107     p_sys->i_es    = 0;
108     p_sys->es      = NULL;
109
110     p_sys->i_audio = 0;
111     p_sys->i_video = 0;
112     p_sys->i_sub   = 0;
113
114     var_Get( p_input, "audio-channel", &val );
115     p_sys->i_audio_last = val.i_int;
116
117     var_Get( p_input, "spu-channel", &val );
118     p_sys->i_sub_last = val.i_int;
119
120     p_sys->p_es_audio = NULL;
121     p_sys->p_es_video = NULL;
122     p_sys->p_es_sub   = NULL;
123
124     return out;
125 }
126
127 /**
128  * Deletes an es_out structure
129  *
130  * \param out  the es_out structure to destroy
131  * \return nothing
132  */
133 void input_EsOutDelete( es_out_t *out )
134 {
135     es_out_sys_t *p_sys = out->p_sys;
136     int i;
137
138     for( i = 0; i < p_sys->i_es; i++ )
139     {
140         free( p_sys->es[i] );
141     }
142     if( p_sys->es )
143     {
144         free( p_sys->es );
145     }
146     free( p_sys );
147     free( out );
148 }
149
150 /**
151  * Add a program
152  *
153  * \param out the es_out
154  * \param i_group ...
155  * \return a program descriptor for the new program
156  */
157 static pgrm_descriptor_t *EsOutAddProgram( es_out_t *out, int i_group )
158 {
159     input_thread_t    *p_input = out->p_sys->p_input;
160     pgrm_descriptor_t *p_pgrm;
161     es_descriptor_t   *p_pmt;
162
163     /* FIXME we should use a object variable but a lot of place in src/input
164      * have to be changed */
165     int               i_select = config_GetInt( p_input, "program" );
166
167     /* create it */
168     p_pgrm = input_AddProgram( p_input, i_group, 0 );
169
170     /* XXX welcome to kludge, add a dummy es, if you want to understand
171      * why have a look at input_SetProgram. Basicaly, it assume the first
172      * es to be the PMT, how that is stupid, nevertheless it is needed for
173      * the old ts demuxer */
174     p_pmt = input_AddES( p_input, p_pgrm, 0, UNKNOWN_ES, NULL, 0 );
175     p_pmt->i_fourcc = VLC_FOURCC( 'n', 'u', 'l', 'l' );
176
177     /* Select i_select or the first by default */
178     if( p_input->stream.p_selected_program == NULL &&
179         ( i_select <= 0 || i_select == i_group ) )
180     {
181         p_input->stream.p_selected_program = p_pgrm;
182     }
183
184     return p_pgrm;
185 }
186
187 /**
188  * Select an ES given the current mode
189  * XXX: you need to take a the lock before (stream.stream_lock)
190  *
191  * \param out The es_out structure
192  * \param es es_out_id structure
193  * \param b_force ...
194  * \return nothing
195  */
196 static void EsOutSelect( es_out_t *out, es_out_id_t *es, vlc_bool_t b_force )
197 {
198     es_out_sys_t      *p_sys = out->p_sys;
199     input_thread_t    *p_input = p_sys->p_input;
200
201     int i_cat = es->p_es->i_cat;
202
203     if( !p_sys->b_active ||
204         ( !b_force && es->p_es->fmt.i_priority < 0 ) )
205     {
206         return;
207     }
208
209     if( p_sys->i_mode == ES_OUT_MODE_ALL || b_force )
210     {
211         if( !es->p_es->p_dec )
212         {
213             input_SelectES( p_input, es->p_es );
214         }
215     }
216     else if( p_sys->i_mode == ES_OUT_MODE_AUTO )
217     {
218         int i_wanted  = -1;
219
220         if( es->p_es->p_pgrm != NULL &&
221             es->p_es->p_pgrm != p_input->stream.p_selected_program )
222         {
223             return;
224         }
225
226         if( i_cat == AUDIO_ES )
227         {
228             if( p_sys->p_es_audio &&
229                 p_sys->p_es_audio->p_es->fmt.i_priority >=
230                     es->p_es->fmt.i_priority )
231             {
232                 return;
233             }
234             i_wanted  = p_sys->i_audio_last >= 0 ?
235                             p_sys->i_audio_last : es->i_channel;
236         }
237         else if( i_cat == SPU_ES )
238         {
239             if( p_sys->p_es_sub &&
240                 p_sys->p_es_sub->p_es->fmt.i_priority >=
241                     es->p_es->fmt.i_priority )
242             {
243                 return;
244             }
245             i_wanted  = p_sys->i_sub_last;
246         }
247         else if( i_cat == VIDEO_ES )
248         {
249             i_wanted  = es->i_channel;
250         }
251
252         if( i_wanted == es->i_channel && es->p_es->p_dec == NULL )
253         {
254             input_SelectES( p_input, es->p_es );
255         }
256     }
257
258     /* FIXME TODO handle priority here */
259     if( es->p_es->p_dec )
260     {
261         if( i_cat == AUDIO_ES )
262         {
263             if( p_sys->i_mode == ES_OUT_MODE_AUTO &&
264                 p_sys->p_es_audio && p_sys->p_es_audio->p_es->p_dec )
265             {
266                 input_UnselectES( p_input, p_sys->p_es_audio->p_es );
267             }
268             p_sys->p_es_audio = es;
269         }
270         else if( i_cat == SPU_ES )
271         {
272             if( p_sys->i_mode == ES_OUT_MODE_AUTO &&
273                 p_sys->p_es_sub && p_sys->p_es_sub->p_es->p_dec )
274             {
275                 input_UnselectES( p_input, p_sys->p_es_sub->p_es );
276             }
277             p_sys->p_es_sub = es;
278         }
279         else if( i_cat == VIDEO_ES )
280         {
281             p_sys->p_es_video = es;
282         }
283     }
284 }
285
286 /**
287  * Add an es_out
288  *
289  * \param out the es_out to add
290  * \param fmt the es_format of the es_out
291  * \return an es_out id
292  */
293 static es_out_id_t *EsOutAdd( es_out_t *out, es_format_t *fmt )
294 {
295     es_out_sys_t      *p_sys = out->p_sys;
296     input_thread_t    *p_input = p_sys->p_input;
297     es_out_id_t       *es = malloc( sizeof( es_out_id_t ) );
298     pgrm_descriptor_t *p_pgrm = NULL;
299     char              psz_cat[sizeof( _("Stream ") ) + 10];
300     char              *psz_description;
301
302     vlc_mutex_lock( &p_input->stream.stream_lock );
303     if( fmt->i_group >= 0 )
304     {
305         /* search program */
306         p_pgrm = input_FindProgram( p_input, fmt->i_group );
307
308         if( p_pgrm == NULL )
309         {
310             /* Create it */
311             p_pgrm = EsOutAddProgram( out, fmt->i_group );
312         }
313     }
314     if( fmt->i_id < 0 )
315     {
316         fmt->i_id = out->p_sys->i_id - 1;
317     }
318
319     psz_description = LanguageGetName( fmt->psz_language );
320     es->p_es = input_AddES( p_input, p_pgrm, fmt->i_id + 1,
321                             fmt->i_cat, psz_description, 0 );
322     es->p_es->i_stream_id = fmt->i_id;
323     es->p_es->i_fourcc = fmt->i_codec;
324
325     switch( fmt->i_cat )
326     {
327     case AUDIO_ES:
328         es->i_channel = p_sys->i_audio;
329         break;
330
331     case VIDEO_ES:
332         es->i_channel = p_sys->i_video;
333         break;
334
335     case SPU_ES:
336         es->i_channel = p_sys->i_sub;
337         break;
338
339     default:
340         es->i_channel = 0;
341         break;
342     }
343
344     /* Add stream info */
345     vlc_mutex_unlock( &p_input->stream.stream_lock );
346     sprintf( psz_cat, _("Stream %d"), out->p_sys->i_id - 1 );
347
348     input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Codec"),
349                    "%.4s", (char*)&fmt->i_codec );
350
351     if( *psz_description )
352         input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Language"),
353                        "%s", psz_description );
354
355     if( fmt->psz_description && *fmt->psz_description )
356         input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Description"),
357                        "%s", fmt->psz_description );
358
359     /* Add information */
360     switch( fmt->i_cat )
361     {
362     case AUDIO_ES:
363         input_Control( p_input, INPUT_ADD_INFO, psz_cat,
364                        _("Type"), _("Audio") );
365
366         if( fmt->audio.i_channels > 0 )
367             input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Channels"),
368                            "%d", fmt->audio.i_channels );
369
370         if( fmt->audio.i_rate > 0 )
371             input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Sample rate"),
372                            _("%d Hz"), fmt->audio.i_rate );
373
374         if( fmt->audio.i_bitspersample > 0 )
375             input_Control( p_input, INPUT_ADD_INFO, psz_cat,
376                            _("Bits per sample"), "%d",
377                            fmt->audio.i_bitspersample );
378
379         if( fmt->i_bitrate > 0 )
380             input_Control( p_input, INPUT_ADD_INFO, psz_cat, _("Bitrate"),
381                            _("%d bps"), fmt->i_bitrate );
382         break;
383
384     case VIDEO_ES:
385         input_Control( p_input, INPUT_ADD_INFO, psz_cat,
386                        _("Type"), _("Video") );
387
388         if( fmt->video.i_width > 0 && fmt->video.i_height > 0 )
389             input_Control( p_input, INPUT_ADD_INFO, psz_cat,
390                            _("Resolution"), "%dx%d",
391                            fmt->video.i_width, fmt->video.i_height );
392
393         if( fmt->video.i_visible_width > 0 &&
394             fmt->video.i_visible_height > 0 )
395             input_Control( p_input, INPUT_ADD_INFO, psz_cat,
396                            _("Display resolution"), "%dx%d",
397                            fmt->video.i_visible_width,
398                            fmt->video.i_visible_height);
399         break;
400
401     case SPU_ES:
402         input_Control( p_input, INPUT_ADD_INFO, psz_cat,
403                        _("Type"), _("Subtitle") );
404         break;
405
406     default:
407         break;
408     }
409
410     vlc_mutex_lock( &p_input->stream.stream_lock );
411     free( psz_description );
412
413     es_format_Copy( &es->p_es->fmt, fmt );
414
415     /* Apply mode
416      * XXX change that when we do group too */
417     if( 1 )
418     {
419         EsOutSelect( out, es, VLC_FALSE );
420     }
421     vlc_mutex_unlock( &p_input->stream.stream_lock );
422
423     TAB_APPEND( out->p_sys->i_es, out->p_sys->es, es );
424     p_sys->i_id++;  /* always incremented */
425     switch( fmt->i_cat )
426     {
427         case AUDIO_ES:
428             p_sys->i_audio++;
429             break;
430         case SPU_ES:
431             p_sys->i_sub++;
432             break;
433         case VIDEO_ES:
434             p_sys->i_video++;
435             break;
436     }
437
438     return es;
439 }
440
441 /**
442  * Send a block for the given es_out
443  *
444  * \param out the es_out to send from
445  * \param es the es_out_id
446  * \param p_block the data block to send
447  */
448 static int EsOutSend( es_out_t *out, es_out_id_t *es, block_t *p_block )
449 {
450     es_out_sys_t *p_sys = out->p_sys;
451     pgrm_descriptor_t *p_pgrm = es->p_es->p_pgrm;
452     input_thread_t    *p_input = p_sys->p_input;
453
454     if( p_pgrm == NULL )
455     {
456         p_pgrm = p_sys->p_input->stream.p_selected_program;
457     }
458
459     /* +11 -> avoid null value with non null dts/pts */
460     if( p_block->i_dts > 0 && p_pgrm )
461     {
462         p_block->i_dts =
463             input_ClockGetTS( p_input, p_pgrm, ( p_block->i_dts + 11 ) * 9 / 100 );
464     }
465     if( p_block->i_pts > 0 && p_pgrm )
466     {
467         p_block->i_pts =
468             input_ClockGetTS( p_input, p_pgrm, ( p_block->i_pts + 11 )* 9 / 100 );
469     }
470
471     vlc_mutex_lock( &out->p_sys->p_input->stream.stream_lock );
472     p_block->i_rate = out->p_sys->p_input->stream.control.i_rate;
473     if( es->p_es->p_dec &&
474         (es->p_es->i_cat!=AUDIO_ES || !p_sys->p_input->stream.control.b_mute) )
475     {
476         vlc_mutex_unlock( &out->p_sys->p_input->stream.stream_lock );
477         input_DecodeBlock( es->p_es->p_dec, p_block );
478     }
479     else
480     {
481         vlc_mutex_unlock( &out->p_sys->p_input->stream.stream_lock );
482         block_Release( p_block );
483     }
484
485     return VLC_SUCCESS;
486 }
487
488 /*****************************************************************************
489  * EsOutDel:
490  *****************************************************************************/
491 static void EsOutDel( es_out_t *out, es_out_id_t *es )
492 {
493     es_out_sys_t *p_sys = out->p_sys;
494
495     TAB_REMOVE( p_sys->i_es, p_sys->es, es );
496
497     switch( es->p_es->i_cat )
498     {
499         case AUDIO_ES:
500             p_sys->i_audio--;
501             break;
502         case SPU_ES:
503             p_sys->i_sub--;
504             break;
505         case VIDEO_ES:
506             p_sys->i_video--;
507             break;
508     }
509
510     /* We don't try to reselect */
511     vlc_mutex_lock( &p_sys->p_input->stream.stream_lock );
512     if( es->p_es->p_dec )
513     {
514         input_UnselectES( p_sys->p_input, es->p_es );
515     }
516
517     if( es->p_es->p_waveformatex )
518     {
519         free( es->p_es->p_waveformatex );
520         es->p_es->p_waveformatex = NULL;
521     }
522     if( es->p_es->p_bitmapinfoheader )
523     {
524         free( es->p_es->p_bitmapinfoheader );
525         es->p_es->p_bitmapinfoheader = NULL;
526     }
527     input_DelES( p_sys->p_input, es->p_es );
528
529     if( p_sys->p_es_audio == es ) p_sys->p_es_audio = NULL;
530     if( p_sys->p_es_video == es ) p_sys->p_es_video = NULL;
531     if( p_sys->p_es_sub   == es ) p_sys->p_es_sub   = NULL;
532
533     vlc_mutex_unlock( &p_sys->p_input->stream.stream_lock );
534
535     free( es );
536 }
537
538 /**
539  * Control query handler
540  *
541  * \param out the es_out to control
542  * \param i_query A es_out query as defined in include/ninput.h
543  * \param args a variable list of arguments for the query
544  * \return VLC_SUCCESS or an error code
545  */
546 static int EsOutControl( es_out_t *out, int i_query, va_list args )
547 {
548     es_out_sys_t *p_sys = out->p_sys;
549     vlc_bool_t  b, *pb;
550     int         i, *pi;
551
552     es_out_id_t *es;
553
554     switch( i_query )
555     {
556         case ES_OUT_SET_ES_STATE:
557             vlc_mutex_lock( &p_sys->p_input->stream.stream_lock );
558             es = (es_out_id_t*) va_arg( args, es_out_id_t * );
559             b = (vlc_bool_t) va_arg( args, vlc_bool_t );
560             if( b && es->p_es->p_dec == NULL )
561             {
562                 input_SelectES( p_sys->p_input, es->p_es );
563                 vlc_mutex_unlock( &p_sys->p_input->stream.stream_lock );
564                 return es->p_es->p_dec ? VLC_SUCCESS : VLC_EGENERIC;
565             }
566             else if( !b && es->p_es->p_dec )
567             {
568                 input_UnselectES( p_sys->p_input, es->p_es );
569                 vlc_mutex_unlock( &p_sys->p_input->stream.stream_lock );
570                 return VLC_SUCCESS;
571             }
572             vlc_mutex_unlock( &p_sys->p_input->stream.stream_lock );
573             return VLC_SUCCESS;
574
575         case ES_OUT_GET_ES_STATE:
576             es = (es_out_id_t*) va_arg( args, es_out_id_t * );
577             pb = (vlc_bool_t*) va_arg( args, vlc_bool_t * );
578
579             *pb = es->p_es->p_dec ? VLC_TRUE : VLC_FALSE;
580             return VLC_SUCCESS;
581
582         case ES_OUT_SET_ACTIVE:
583         {
584             b = (vlc_bool_t) va_arg( args, vlc_bool_t );
585             p_sys->b_active = b;
586
587             if( b )
588             {
589                 vlc_value_t val;
590                 val.b_bool = VLC_TRUE;
591                 var_Set( p_sys->p_input, "intf-change", val );
592             }
593             return VLC_SUCCESS;
594         }
595
596         case ES_OUT_GET_ACTIVE:
597             pb = (vlc_bool_t*) va_arg( args, vlc_bool_t * );
598             *pb = p_sys->b_active;
599             return VLC_SUCCESS;
600
601         case ES_OUT_SET_MODE:
602             i = (int) va_arg( args, int );
603             if( i == ES_OUT_MODE_NONE || i == ES_OUT_MODE_ALL ||
604                 i == ES_OUT_MODE_AUTO )
605             {
606                 vlc_value_t val;
607
608                 p_sys->i_mode = i;
609
610                 /* Reapply policy mode */
611                 vlc_mutex_lock( &p_sys->p_input->stream.stream_lock );
612                 for( i = 0; i < p_sys->i_es; i++ )
613                 {
614                     if( p_sys->es[i]->p_es->p_dec )
615                     {
616                         input_UnselectES( p_sys->p_input, p_sys->es[i]->p_es );
617                     }
618                 }
619                 for( i = 0; i < p_sys->i_es; i++ )
620                 {
621                     EsOutSelect( out, p_sys->es[i], VLC_FALSE );
622                 }
623                 vlc_mutex_unlock( &p_sys->p_input->stream.stream_lock );
624
625                 val.b_bool = VLC_TRUE;
626                 var_Set( p_sys->p_input, "intf-change", val );
627
628                 return VLC_SUCCESS;
629             }
630             return VLC_EGENERIC;
631
632         case ES_OUT_GET_MODE:
633             pi = (int*) va_arg( args, int* );
634             *pi = p_sys->i_mode;
635             return VLC_SUCCESS;
636
637         case ES_OUT_SET_ES:
638             es = (es_out_id_t*) va_arg( args, es_out_id_t * );
639             if( es == NULL )
640             {
641                 for( i = 0; i < p_sys->i_es; i++ )
642                 {
643                     vlc_mutex_lock( &p_sys->p_input->stream.stream_lock );
644                     if( p_sys->es[i]->p_es->p_dec )
645                     {
646                         input_UnselectES( p_sys->p_input, p_sys->es[i]->p_es );
647                     }
648                     vlc_mutex_unlock( &p_sys->p_input->stream.stream_lock );
649                 }
650             }
651             else
652             {
653                 vlc_mutex_lock( &p_sys->p_input->stream.stream_lock );
654                 EsOutSelect( out, es, VLC_TRUE );
655                 vlc_mutex_unlock( &p_sys->p_input->stream.stream_lock );
656             }
657             return VLC_SUCCESS;
658
659         case ES_OUT_SET_PCR:
660         case ES_OUT_SET_GROUP_PCR:
661         {
662             pgrm_descriptor_t *p_pgrm = NULL;
663             int64_t           i_pcr;
664
665             if( i_query == ES_OUT_SET_PCR )
666             {
667                 p_pgrm = p_sys->p_input->stream.p_selected_program;
668             }
669             else
670             {
671                 int i_group = (int)va_arg( args, int );
672                 p_pgrm = input_FindProgram( p_sys->p_input, i_group );
673                 if( p_pgrm == NULL )
674                 {
675                     /* we create the requested program */
676                     p_pgrm = EsOutAddProgram( out, i_group );
677                 }
678             }
679             i_pcr = (int64_t)va_arg( args, int64_t );
680             /* search program */
681             if( p_pgrm )
682             {
683                 /* 11 is a vodoo trick to avoid non_pcr*9/100 to be null */
684                 input_ClockManageRef( p_sys->p_input, p_pgrm, (i_pcr + 11 ) * 9 / 100);
685             }
686             return VLC_SUCCESS;
687         }
688
689         case ES_OUT_RESET_PCR:
690             for( i = 0; i < p_sys->p_input->stream.i_pgrm_number; i++ )
691             {
692                 p_sys->p_input->stream.pp_programs[i]->i_synchro_state =
693                     SYNCHRO_REINIT;
694                 p_sys->p_input->stream.pp_programs[i]->last_pts = 0;
695             }
696             return VLC_SUCCESS;
697
698         default:
699             msg_Err( p_sys->p_input, "unknown query in es_out_Control" );
700             return VLC_EGENERIC;
701     }
702 }
703
704 /****************************************************************************
705  * LanguageGetName: try to expend iso639 into plain name
706  ****************************************************************************/
707 static char *LanguageGetName( const char *psz_code )
708 {
709     const iso639_lang_t *pl;
710
711     if( psz_code == NULL )
712     {
713         return strdup( "" );
714     }
715
716     if( strlen( psz_code ) == 2 )
717     {
718         pl = GetLang_1( psz_code );
719     }
720     else if( strlen( psz_code ) == 3 )
721     {
722         pl = GetLang_2B( psz_code );
723         if( !strcmp( pl->psz_iso639_1, "??" ) )
724         {
725             pl = GetLang_2T( psz_code );
726         }
727     }
728     else
729     {
730         return strdup( psz_code );
731     }
732
733     if( !strcmp( pl->psz_iso639_1, "??" ) )
734     {
735        return strdup( psz_code );
736     }
737     else
738     {
739         if( *pl->psz_native_name )
740         {
741             return strdup( pl->psz_native_name );
742         }
743         return strdup( pl->psz_eng_name );
744     }
745 }