input_file.c 32.9 KB
Newer Older
1
2
/*****************************************************************************
 * input_file.c: functions to read from a file
3
4
5
6
7
8
9
10
11
 *****************************************************************************
 * Copyright (C) 1999, 2000 VideoLAN
 *
 * Authors:
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
12
 * 
13
14
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
17
 *
18
19
20
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
21
 *****************************************************************************/
Michel Kaempf's avatar
Michel Kaempf committed
22

23
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
24
 * Preamble
25
 *****************************************************************************/
26
27
#include "defs.h"

28
#include <stdio.h>
29
#include <unistd.h>                                               /* close() */
30
31
#include <sys/types.h>                        /* on BSD, uio.h needs types.h */
#include <sys/uio.h>                                            /* "input.h" */
32
#include <sys/stat.h>                                    /* fstat, off_t ... */
33
34
#include <netinet/in.h>                                           /* ntohl() */
#include <fcntl.h>                                                 /* open() */
35
36
37
#include <malloc.h>                                      /* malloc, read ... */
#include <string.h>

Michel Kaempf's avatar
Michel Kaempf committed
38
#include "config.h"
39
40
#include "common.h"
#include "threads.h"
Michel Kaempf's avatar
Michel Kaempf committed
41
#include "mtime.h"
42
#include "intf_msg.h"
Michel Kaempf's avatar
Michel Kaempf committed
43

44
45
#include "main.h"

Michel Kaempf's avatar
Michel Kaempf committed
46
#include "input.h"
Sam Hocevar's avatar
Sam Hocevar committed
47
#include "input_ps.h"
Michel Kaempf's avatar
Michel Kaempf committed
48
49
#include "input_file.h"

50
51
52
53
54
55
56
57
#define BUF_SIZE (797*3)

#define PS_METHOD 1
#define TS_METHOD 2

#define TS_PACKET_SIZE 188
#define TS_IN_UDP 7

58
59
60
61
#define MAX_AUDIO_CHANNEL 15
#define MAX_SUBTITLES_CHANNEL 31
#define NO_SUBTITLES 255

62
63
64
65
66
67
#define PS_BUFFER_SIZE 16384
#define NO_PES 0
#define AUDIO_PES 1
#define VIDEO_PES 2
#define AC3_PES 3
#define SUBTITLE_PES 4
68
69
#define LPCM_PES 5
#define PRIVATE_PES 6
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#define UNKNOWN_PES 12

#define PCR_PID 0x20 /* 0x20 == first video stream
                      * 0x40 == first audio stream */

typedef u8 file_ts_packet[TS_PACKET_SIZE];
typedef file_ts_packet udp_packet[TS_IN_UDP];

typedef struct synchro_struct
{
    mtime_t     delta_clock;
    mtime_t     slope;
    mtime_t     last_pcr_time;
    
    file_ts_packet *last_pcr;
} synchro_t;

typedef struct in_data_s
{
    int start, end;
    vlc_mutex_t lock;
    vlc_cond_t notfull;
    vlc_cond_t notempty;
    udp_packet buf[BUF_SIZE+1];
} in_data_t;

typedef struct own_pcr_s
{
    int start, end;
    vlc_mutex_t lock;
    file_ts_packet *buf[(BUF_SIZE+1)*TS_IN_UDP+1];
} own_pcr_t;

typedef struct options_s
{
    unsigned int pcr_pid;
    u8 i_file_type;
    int in; 
108
109
    char **playlist;
    int i_list_index;
110
111
112
113
114
115
} options_t;

typedef struct s_ps
{
    unsigned int pat_counter;
    unsigned int pmt_counter;
116
117
118
119
120
121
122
123
124

    /*
     * These 3 parameters are passed 
     * as command line arguments
     */
    unsigned int audio_channel;
    unsigned int subtitles_channel;
    unsigned int audio_type;

125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
    /* 
     * 16 audio mpeg streams
     * 16 audio AV3 streams
     * 16 video mpeg streams
     * 32 subtitle streams
     */
    unsigned int media_counter[0x100];
    unsigned int association_table[0x100];
    unsigned int found_streams;

    unsigned int found_pts;

    unsigned int ts_to_write;
    unsigned int ts_written;
    unsigned int sent_ts;

    unsigned char *ps_data;
    unsigned char *ps_end;
    unsigned char *ps_buffer;

    unsigned int pes_id;
    unsigned int private_id;
    unsigned int has_pts;
    unsigned int pcr_pid;

    unsigned int pes_type;
    unsigned int pes_size;
    unsigned int to_skip;
    unsigned int offset;
} ps_t;

typedef struct input_file_s
{
    boolean_t    b_die; /* b_die flag for the disk thread */
    vlc_thread_t disk_thread;

    synchro_t    synchro;
    ps_t         ps;
    in_data_t    in_data;
    options_t    options;
    own_pcr_t    own_pcr;
} input_file_t;

/* local prototypes */
void ps_fill( input_file_t * p_if, boolean_t wait );
170
ssize_t safe_read(options_t *p_options, unsigned char *buf, int count);
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
void input_DiskThread( input_file_t * p_if );
int init_synchro( input_file_t * p_if );

input_file_t input_file;

/******************************************************************************
 * ConvertPCRTime : extracts and converts the PCR time in microseconds
 ******************************************************************************/

s64 ConvertPCRTime(file_ts_packet *pcr_buff)
{
    return( (((((s64)U32_AT(((u8*)pcr_buff)+6)) << 1) | (((u8*)pcr_buff)[10] >> 7)) * 300) / 27 );
}

/******************************************************************************
 * wait_a_moment : Compute how long we must wait before sending a TS packet
 ******************************************************************************/

static void wait_a_moment( input_file_t * p_if, file_ts_packet *ts)
{
    synchro_t * p_synchro = &input_file.synchro;
    
    static int retard_count = 0;
    static s64 wait_max = 0;
    s64 sendtime; /* the date at which the TS packet should be sent */
    s64 wait;
    
    sendtime = p_synchro->last_pcr_time + p_synchro->delta_clock +
        p_synchro->slope * ((ts - p_synchro->last_pcr + (BUF_SIZE+1)*TS_IN_UDP) % ((BUF_SIZE+1)*TS_IN_UDP)); 
    wait = sendtime - mdate();
    if( wait > 0 )
    { 
        retard_count = 0;
        if(wait > 100000)
        {
206
            intf_DbgMsg( "input warning: wait time may be too long : %Ld\n", wait );
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
            return;
        }
        msleep( wait );
    }
    else
    {
        if( wait < wait_max )
        {
            wait_max = wait;
        }
        retard_count++;
        if( retard_count == 16 )
        {
            retard_count = 0;
        }
    }
}

/******************************************************************************
 * adjust : Adjust the encoder clock & remove the PCR from own_pcr
 ******************************************************************************/

static void adjust( input_file_t * p_if, file_ts_packet *ts )
{
    synchro_t * p_synchro = &p_if->synchro;
    own_pcr_t * p_own_pcr = &p_if->own_pcr;
    file_ts_packet *next_pcr;
    int no_discontinuity = 1;
    
    if( ((u8*)ts)[5] & 0x80 )
    {
        /* There is a discontinuity - I recalculate the delta */
        p_synchro->delta_clock = mdate() - ConvertPCRTime(ts);
        intf_DbgMsg( "input warning: clock discontinuity\n" );
        no_discontinuity = 0;
    }
    else
    {
        p_synchro->last_pcr = ts;
        p_synchro->last_pcr_time = ConvertPCRTime( ts );
    }
        
    vlc_mutex_lock(&p_own_pcr->lock);
    p_own_pcr->start++;
    p_own_pcr->start %= (BUF_SIZE+1)*TS_IN_UDP+1;
    
    /* If we have 2 consecutiv PCR, we can reevaluate slope */
    if( (p_own_pcr->start != p_own_pcr->end) &&
        no_discontinuity &&
        !((((u8*) next_pcr = p_own_pcr->buf[p_own_pcr->start]))[5] & 0x80))
    {
        s64 current_pcr_time = ConvertPCRTime(ts);
        s64 next_pcr_time =    ConvertPCRTime(next_pcr);
        
        if( (next_pcr_time - current_pcr_time < 0) || (next_pcr_time - current_pcr_time > 700000))
        {
263
            intf_DbgMsg( "input warning: possible discontinuity\n" );
264
265
266
267
268
269
270
271
272
273
274
275
            p_synchro->delta_clock = mdate() - next_pcr_time;
        }
        else
        {
            p_synchro->slope = (next_pcr_time - current_pcr_time) /
                ((next_pcr - ts + (BUF_SIZE+1)*TS_IN_UDP) % ((BUF_SIZE+1)*TS_IN_UDP));
        }
    }
    
    vlc_mutex_unlock(&p_own_pcr->lock);
}

276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/*****************************************************************************
 * file_next : Opens the next available file
 *****************************************************************************/

int file_next( options_t *options )
{
    /* the check for index == 0 should be done _before_ */
    options->i_list_index--;
    
    if( options->in != -1 )
    {
            close( options->in );
    }

    if( !strcmp( options->playlist[options->i_list_index], "-" ) )
    {
        /* read stdin */
        return ( options->in = 0 );
    }
    else
    {
        /* read the actual file */
        fprintf( stderr, "Playing file %s\n",
                 options->playlist[options->i_list_index] );
        return ( options->in = open( options->playlist[options->i_list_index],
                                     O_RDONLY | O_NDELAY ) );
    }
}

/*****************************************************************************
306
 * safe_read : Buffered reading method
307
 *****************************************************************************/
308

309
ssize_t safe_read( options_t *options, unsigned char *buf, int count )
310
311
{
    int ret, cnt=0;
312
313
    
    while( cnt < count )
314
    {
315
316
317
        ret = read( options->in, buf + cnt, count - cnt );
    
        if( ret < 0 )
318
            return ret;
319
320
321
322
323
324
325
326
327
328
329
330
331
332
    
        if( ret == 0 )
        {
            /* zero means end of file */
            if( options->i_list_index )
            {
                file_next( options );
            }
            else
            {
                break;
            }
        }
    
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
        cnt += ret;
    }

    return cnt;
}

/******************************************************************************
 * keep_pcr : Put a TS packet in the fifo if it owns a PCR
 ******************************************************************************/

int keep_pcr(int pcr_pid, file_ts_packet *ts)
{
    own_pcr_t * p_own_pcr =   &input_file.own_pcr;

#define p ((u8 *)ts)
    if ((p[3] & 0x20) && p[4] && (p[5] & 0x10)
        && ((((p[1]<<8)+p[2]) & 0x1FFF) == pcr_pid))
    {
        /* adaptation_field_control is set, adaptation_field_lenght is not 0,
         * PCR_flag is set, pid == pcr_pid */
        vlc_mutex_lock(&p_own_pcr->lock);
        p_own_pcr->buf[p_own_pcr->end++] = ts;
        p_own_pcr->end %= (BUF_SIZE+1)*TS_IN_UDP+1;
        vlc_mutex_unlock(&p_own_pcr->lock);
        return 1;
    } 
    else
        return 0;
#undef p
}

/******************************************************************************
 * get_pid : gets a pid from a PES type
 ******************************************************************************/

int get_pid (ps_t *p_ps)
{
    int i, tofind, delta;
371
    char* type;
372
373
374
375

    switch( p_ps->pes_type )
    {
        case VIDEO_PES:
376
377
            delta = 0x20; /* 0x20 - 0x2f */
            type = "MPEG video";
378
379
380
            tofind = p_ps->pes_id;
            break;
        case AUDIO_PES:
381
382
            delta = 0x40; /* 0x40 - 0x5f */
            type = "MPEG audio";
383
384
            tofind = p_ps->pes_id;
            break;
385
386
387
388
        /* XXX: 0x64 is for the PMT, so don't take it !!! */
        case AC3_PES:
            delta = 0x80; /* 0x80 - 0x8f */
            type = "MPEG private (AC3 audio)";
389
390
            tofind = p_ps->private_id;
            break;
391
392
393
394
395
396
397
398
        case LPCM_PES:
            delta = 0x90; /* 0x90 - 0x9f */
            type = "MPEG private (LPCM audio)";
            tofind = p_ps->private_id;
            break;
        case SUBTITLE_PES:
            delta = 0xa0; /* 0xa0 - 0xbf */
            type = "MPEG private (DVD subtitle)";
399
400
            tofind = p_ps->private_id;
            break;
401
        default:
402
403
404
405
            return(-1);

    }

406
407
408
409
410
411
412
413
    i = delta + (tofind & 0x1f);

    if( p_ps->association_table[i] == 0)
    {
        intf_Msg( "Found %s stream at 0x%.2x, allocating PID 0x%.2x\n",
                  type, tofind, i );
        p_ps->association_table[i] = 1;
    }
414
415

    return ( i );
416

417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
}

/******************************************************************************
 * write_media_ts : writes a ts packet from a ps stream
 ******************************************************************************/

void write_media_ts(ps_t *ps, unsigned char *ts, unsigned int pid)
{
    int i,j;
    s64 clock;
    long int extclock;

    /* if offset == 0, it means we haven't examined the PS yet */
    if (ps->offset == 0)
    {
        if (ps->pes_size < 184) {

            ts[0] = 0x47;    /* sync_byte */
            ts[1] = 0x40;    /* payload_unit_start_indicator si dbut de PES */
            ts[2] = pid;

            ts[3] = 0x30 + (ps->media_counter[pid] & 0x0f);
            ts[4] = 184 - ps->pes_size - 1;
            ts[5] = 0x00;
            for (i=6 ; i < 188 - ps->pes_size ; i++) ts[i]=0xFF; /* facultatif ? */
            memcpy(ts + 188 - ps->pes_size, ps->ps_data, ps->pes_size);
            
            /* this PS is finished, next time we'll pick a new one */
            ps->pes_type = NO_PES;
            ps->ps_data += ps->pes_size;
            ps->offset += ps->pes_size;
            return;

        }
    }

    /* now we still can have offset == 0, but size is initialized */
    
    ts[0] = 0x47;                /* sync_byte */
    ts[1] = (ps->offset == 0) ? 0x40 : 0x00;    /* payload_unit_start_indicator si dbut de PES */
    ts[2] = pid;

    if ( (ps->offset == 0) && (ps->has_pts == 0xc0) && (ps->pcr_pid == pid) )
    {

        ts[3] = 0x30 + (ps->media_counter[pid] & 0x0f);
        ts[4] = 0x07;            /* taille de l'adaptation field */
        ts[5] = 0x50;            /* rtfm */
        
        /* on va lire le PTS */
        clock = ( ((s64)(ps->ps_data[9] & 0x0E) << 29) |
            (((s64)U16_AT(ps->ps_data + 10) << 14) - (1 << 14)) |
            ((s64)U16_AT(ps->ps_data + 12) >> 1) );
        
        ps->has_pts = 0;

        extclock = 0x000;
        ts[6] = (clock & 0x1fe000000) >> 25;    /* ---111111110000000000000000000000000 */
        ts[7] = (clock & 0x001fe0000) >> 17;    /* ---000000001111111100000000000000000 */
        ts[8] = (clock & 0x00001fe00) >>  9;    /* ---000000000000000011111111000000000 */
        ts[9] = (clock & 0x0000001fe) >>  1;    /* ---000000000000000000000000111111110 */
            
        ts[10] = 0x7e + ((clock & 0x01) << 7) + ((extclock & 0x100) >> 8);
        ts[11] = extclock & 0xff;
    
        memcpy(ts + 4 + 8, ps->ps_data, 184 - 8);
            
        ts[15] = 0xe0; /* FIXME : we don't know how to choose program yet */
         
        ps->offset += 184 - 8;
        ps->ps_data += 184 - 8;
    }
    else if (ps->offset <= ps->pes_size - 184)
    {
            
        ts[3] = 0x10 + (ps->media_counter[pid] & 0x0f);
        memcpy(ts + 4, ps->ps_data, 184);

        ps->offset += 184;
        ps->ps_data += 184;
   
    }
    else
    {
            
        j = ps->pes_size - ps->offset;
        ts[3] = 0x30 + (ps->media_counter[pid] & 0x0f);
        ts[4] = 184 - j - 1;
        ts[5] = 0x00;
        for (i=6 ; i < 188 - j ; i++) ts[i]=0xFF; /* facultatif ? */
        memcpy(ts + 4 + 184 - j, ps->ps_data, j);
        ps->offset += j; /* offset = size */
        ps->ps_data += j; /* offset = size */

        /* the PES is finished */
        ps->pes_type = NO_PES;
        ps->sent_ts++;

    }

}

/******************************************************************************
 * write_pat : writes a program association table
 ******************************************************************************/

void write_pat(ps_t *ps, unsigned char *ts)
{
    int i;

    ts[0] = 0x47;        /* sync_byte */
    ts[1] = 0x40;
    ts[2] = 0x00;        /* PID = 0x0000 */
    ts[3] = 0x10 + (ps->pat_counter & 0x0f);
    ts[4] = ts[5] = 0x00;
    
    ts[6] = 0xb0; /* */
    ts[7] = 0x11; /* section_length = 0x011 */

    ts[8] = 0x00;
    ts[9] = 0xb0; /* TS id = 0x00b0 */

    ts[10] = 0xc1;
    /* section # and last section # */
    ts[11] = ts[12] = 0x00;

    /* Network PID (useless) */
    ts[13] = ts[14] = 0x00; ts[15] = 0xe0; ts[16] = 0x10;

    /* Program Map PID */
    ts[17] = 0x03; ts[18] = 0xe8; ts[19] = 0xe0; ts[20] = 0x64;

    /* CRC */
    ts[21] = 0x4d; ts[22] = 0x6a; ts[23] = 0x8b; ts[24] = 0x0f;

    for (i=25 ; i < 188 ; i++) ts[i]=0xFF; /* facultatif ? */

    ps->sent_ts++;
}

/******************************************************************************
 * write_pmt : writes a program map table
 ******************************************************************************/

void write_pmt(ps_t *ps, unsigned char *ts)
{
    int i;
    
    ts[0] = 0x47;        /* sync_byte */
    ts[1] = 0x40;
    ts[2] = 0x0064;        /* PID = 0x0064 */
    ts[3] = 0x10 + (ps->pmt_counter & 0x0f);
    
    ts[4] = 0x00;
    ts[5] = 0x02;
    
    ts[6] = 0xb0; /* */
    ts[7] = 0x34; /* section_length = 0x034 */

    ts[8] = 0x03;
    ts[9] = 0xe8; /* prog number */

    ts[10] = 0xc1;
    /* section # and last section # */
    ts[11] = ts[12] = 0x00;

    /* PCR PID */
    ts[13] = 0xe0;
    ts[14] = 0x20;

    /* program_info_length == 0 */
    ts[15] = 0xf0; ts[16] = 0x00;

    /* Program Map / Video PID */
    ts[17] = 0x02; /* stream type = video */
    ts[18] = 0xe0; ts[19] = 0x20;
    ts[20] = 0xf0; ts[21] = 0x09; /* es info length */
594

595
596
597
598
    /* useless info */
    ts[22] = 0x07; ts[23] = 0x04; ts[24] = 0x08; ts[25] = 0x80; ts[26] = 0x24;
    ts[27] = 0x02; ts[28] = 0x11; ts[29] = 0x01; ts[30] = 0xfe;

599
600
601
602
603
604
605
606
607
    switch ( ps->audio_type )
    {
    case 12 :
    case REQUESTED_AC3 :
        /* ac3 */
        ts[31] = 0x81; /* stream type = audio */
        ts[32] = 0xe0; ts[33] = 0x80 + ps->audio_channel;
        ts[34] = 0xf0; ts[35] = 0x00; /* es info length */
        break;
608

609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
    case REQUESTED_MPEG :
        /* mpeg */
        ts[31] = 0x04; /* stream type = audio */
        ts[32] = 0xe0; ts[33] = 0x40 + ps->audio_channel;
        ts[34] = 0xf0; ts[35] = 0x00; /* es info length */
        break;

    case REQUESTED_LPCM :
        /* LPCM audio */
        ts[31] = 0x81;
        ts[32] = 0xe0; ts[33] = 0xa0 + ps->audio_channel;
        ts[34] = 0xf0; ts[35] = 0x00; /* es info  length */
        break;

    default :
        /* No audio */
        ts[31] = 0x00;
        ts[35] = 0x00; /* es info  length */
    }

    /* Subtitles */
    if( ps->subtitles_channel == NO_SUBTITLES )
    {
        ts[36] = 0x00;
        ts[37] = 0x00; ts[38] = 0x00;
        ts[39] = 0xf0; ts[40] = 0x00; /* es info length */
    }
    else
    {
        ts[36] = 0x82;
        ts[37] = 0xe0; ts[38] = 0xa0 + ( ps->subtitles_channel );
        ts[39] = 0xf0; ts[40] = 0x00; /* es info length */
    }

    /* CRC FIXME: not calculated yet*/
    ts[41] = 0x96; ts[42] = 0x70; ts[43] = 0x0b; ts[44] = 0x7c;
645

646
647
    /* stuffing bytes */
    for (i=45 ; i < 188 ; i++) ts[i]=0xff; /* facultatif ? */
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675

    ps->sent_ts++;
}


/******************************************************************************
 * ps_thread
 ******************************************************************************
 * We use threading to allow cool non-blocking read from the disk. This
 * implicit thread is the disk (producer) thread, it reads packets from
 * the PS file on the disk, and stores them in a FIFO.
 ******************************************************************************/

void ps_thread( input_file_t * p_if )
{
    int i;
    ps_t * p_ps =             &p_if->ps;
    own_pcr_t * p_own_pcr =   &p_if->own_pcr;
    in_data_t * p_in_data =   &p_if->in_data;
 
    /* Initialize the structures */
    p_own_pcr->start = p_own_pcr->end = 0; /* empty FIFO */
    vlc_mutex_init( &p_own_pcr->lock );
    p_in_data->start = p_in_data->end = 0; /* empty FIFO */
    vlc_mutex_init( &p_in_data->lock );
    vlc_cond_init( &p_in_data->notfull );
    vlc_cond_init( &p_in_data->notempty );
    
676
677
678
679
    p_ps->audio_type = main_GetIntVariable( INPUT_DVD_AUDIO_VAR, REQUESTED_AC3 );
    p_ps->audio_channel = main_GetIntVariable( INPUT_DVD_CHANNEL_VAR, 0 );
    p_ps->subtitles_channel = main_GetIntVariable( INPUT_DVD_SUBTITLE_VAR, 0 );

680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
    p_ps->pes_type = NO_PES;
    p_ps->pes_id = 0;
    p_ps->private_id = 0;
    p_ps->pes_size = 0;
    p_ps->to_skip = 0;
    p_ps->pmt_counter = 0;
    p_ps->pat_counter = 0;
    for( i=0; i<256; i++ )
        p_ps->association_table[i] = 0;
    p_ps->offset = 0;
    p_ps->found_pts = 0;
    p_ps->found_streams = 0;
    p_ps->pcr_pid = p_if->options.pcr_pid;

    p_ps->ps_buffer = malloc(PS_BUFFER_SIZE);
    /* those 2 addresses are initialized so that a new packet is read */
    p_ps->ps_data = p_ps->ps_buffer + PS_BUFFER_SIZE - 1;
    /* fix the first byte stuff */
    p_ps->ps_data[0] = 0x00;
    p_ps->ps_end = p_ps->ps_buffer + PS_BUFFER_SIZE;
    
    /* Fill the fifo until it is full */
    ps_fill( p_if, 0 );
    /* Launch the thread which fills the fifo */
704
705
    vlc_thread_create( &p_if->disk_thread, "disk thread",
                       (vlc_thread_func_t)input_DiskThread, p_if ); 
706
707
708
709
710
711
712
713
    /* Init the synchronization XXX add error detection !!! */
    init_synchro( p_if );
}

/******************************************************************************
 * ps_read : ps reading method
 ******************************************************************************/

714
ssize_t ps_read( options_t *p_options, ps_t * p_ps, void *ts )
715
716
717
718
{
    int pid, readbytes = 0;
    int datasize;
    p_ps->ts_written = 0;
719
  
720
721
722
723
724
725
726
727
728
    while(p_ps->ts_to_write)
    {   

        /* if there's not enough data to send */
        if((datasize = p_ps->ps_end - p_ps->ps_data) <= TS_PACKET_SIZE)
        {
            /* copy the remaining bits at the beginning of the PS buffer */
            memmove ( p_ps->ps_buffer, p_ps->ps_data, datasize);
            /* read some bytes */
729
            readbytes = safe_read( p_options, p_ps->ps_buffer + datasize, PS_BUFFER_SIZE - datasize);
730
731
732

            if(readbytes == 0)
            {
733
                intf_ErrMsg ( "input: ps read error\n");
734
735
736
737
738
739
740
741
742
743
744
                return -1;
            }
            p_ps->ps_data = p_ps->ps_buffer;
            p_ps->ps_end = p_ps->ps_data + datasize + readbytes;
        }

        //printf("offset is %x, pes total size is %x, to skip is %x\n", p_ps->offset, p_ps->pes_size, p_ps->to_skip );
        if( p_ps->to_skip == 0 && p_ps->offset == p_ps->pes_size )
        {
            if( p_ps->ps_data[0] || p_ps->ps_data[1] || (p_ps->ps_data[2] != 0x01) )
            {
745
                intf_ErrMsg ( "input error: not a startcode (0x%.2x%.2x%.2x instead of 0x000001)\n", p_ps->ps_data[0], p_ps->ps_data[1], p_ps->ps_data[2] );
746
747
748
                return -1;
            }

749
750
    p_ps->pes_type = NO_PES;
    p_ps->offset = 0;
751
            p_ps->pes_size = (p_ps->ps_data[4] << 8) + p_ps->ps_data[5] + 6;
752
    p_ps->has_pts = p_ps->ps_data[7] & 0xc0;
753
754
755
756
757
758
        }

        /* if the actual data we have in pes_data is not a PES, then
         * we read the next one. */
        if( (p_ps->pes_type == NO_PES) && !p_ps->to_skip )
        {
759
    p_ps->pes_id = p_ps->ps_data[3];
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842

            if (p_ps->pes_id == 0xbd)
            {
                p_ps->private_id = p_ps->ps_data[ 9 + p_ps->ps_data[8] ];
                if ((p_ps->private_id & 0xf0) == 0x80)
                {
                    /* flux audio ac3 */
                    p_ps->pes_type = AC3_PES;
                }
                else if ((p_ps->private_id & 0xf0) == 0x20)
                {
                    /* subtitles */
                    p_ps->pes_type = SUBTITLE_PES;
                }
                else
                {
                    /* unknown private data */
                    p_ps->pes_type = PRIVATE_PES;
                }
            }
            else if ((p_ps->pes_id & 0xe0) == 0xc0)
            {
                /* flux audio */
                p_ps->pes_type = AUDIO_PES;
               //write (1, p_ps->ps_data + 9 + p_ps->ps_data[8], 2048 - (9 + p_ps->ps_data[8]));
            }
            else if ((p_ps->pes_id & 0xf0) == 0xe0)
            {
                /* flux video */
                p_ps->pes_type = VIDEO_PES;
            }
            else if (p_ps->pes_id == 0xba)
            {
                p_ps->pes_type = NO_PES;
                p_ps->pes_size = 14; /* 8 extra characters after 0x000001ba**** */
                p_ps->to_skip = 14;
            }
            else
            {
                p_ps->pes_type = UNKNOWN_PES;
                p_ps->to_skip = p_ps->pes_size;
            }
        }

        if( p_ps->to_skip )
        {
            if( p_ps->to_skip < TS_PACKET_SIZE )
            {
                p_ps->ps_data += p_ps->to_skip;
                p_ps->offset += p_ps->to_skip;
                p_ps->to_skip = 0;
            }
            else
            {
                p_ps->ps_data += TS_PACKET_SIZE;
                p_ps->offset += TS_PACKET_SIZE;
                p_ps->to_skip -= TS_PACKET_SIZE;
            }
        }

        /* now that we know what we have, we can either 
         * write this packet's data in the buffer, skip it,
         * or write a PMT or PAT table and wait for the next
         * turn before writing the packet. */
        switch (p_ps->sent_ts & 0xff)
        {
            case 0x80:
                write_pmt(p_ps,ts);
                p_ps->pmt_counter++;
                p_ps->ts_to_write--; p_ps->ts_written++; ts+=188;
                break;
            case 0x00:
                write_pat(p_ps,ts);
                p_ps->pat_counter++;
                p_ps->ts_to_write--; p_ps->ts_written++; ts+=188;
                break;
        }

        /* if there's still no found PCR_PID, and no PTS in this PES, we trash it */
        if (!p_ps->found_pts)
        {
            if (p_ps->has_pts)
            {
843
                intf_Msg( "input: found a PTS, at last ...\n" );
844
845
846
847
848
                p_ps->found_pts = 1;
            }
            else
                p_ps->pes_type = NO_PES;
        }
849

850
851
852
853
854
855
        if (p_ps->ts_to_write)
        {
            switch(p_ps->pes_type)
            {
                case VIDEO_PES:
                case AUDIO_PES:
856
case SUBTITLE_PES:
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
                case AC3_PES:
                    pid = get_pid (p_ps);
                    write_media_ts(p_ps, ts, pid);
                    p_ps->ts_to_write--; p_ps->ts_written++; ts+=188;
                    p_ps->media_counter[pid]++;
                    break;
                case UNKNOWN_PES:
                default:
                    p_ps->pes_type = NO_PES;
                    break;
            }
        }
    }

    //p_ps->ps_data += TS_PACKET_SIZE;

    return p_ps->ts_written;
}

/******************************************************************************
 * ps_fill : Fill the data buffer with TS created from a PS file
 ******************************************************************************/

void ps_fill( input_file_t * p_if, boolean_t wait )
{
    in_data_t * p_in_data = &p_if->in_data;
    ps_t * p_ps = &p_if->ps;
    int i, how_many;
    int pcr_flag;
    file_ts_packet *ts;

    /* How many TS packet for the next UDP packet */
    how_many = TS_IN_UDP;

    pcr_flag = 0;
    /* for every single TS packet */
    while( !p_if->b_die )
    {        
        /* wait until we have one free item to store the UDP packet read */
        vlc_mutex_lock(&p_in_data->lock);
        while((p_in_data->end+BUF_SIZE+1-p_in_data->start)%(BUF_SIZE+1) == BUF_SIZE )
        {
            /* The buffer is full */
            if(wait)
            {
                vlc_cond_wait(&p_in_data->notfull, &p_in_data->lock);
                if( p_if->b_die )
                    return;
            }
            else
            {
                vlc_mutex_unlock(&p_in_data->lock);
                if (!pcr_flag)
                {
                    intf_ErrMsg( "input error: bad PCR PID\n" );
                }
                return;
            }
        }
        vlc_mutex_unlock(&p_in_data->lock);
        
        /* read a whole UDP packet from the file */
        p_ps->ts_to_write = how_many;
920
        if(ps_read(&p_if->options, p_ps, ts = (file_ts_packet *)(p_in_data->buf + p_in_data->end)) != how_many)
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
        {
            msleep( 50000 ); /* XXX we need an INPUT_IDLE */
            intf_ErrMsg( "input error: read() error\n" );
        }
        
        /* Scan to mark TS packets containing a PCR */
        for(i=0; i<how_many; i++, ts++)
        {
            pcr_flag |= keep_pcr(p_ps->pcr_pid, ts);
        }
        
        vlc_mutex_lock(&p_in_data->lock);
        p_in_data->end++;
        p_in_data->end %= BUF_SIZE+1;
        vlc_cond_signal(&p_in_data->notempty);
        vlc_mutex_unlock(&p_in_data->lock);
    }
}

int init_synchro( input_file_t * p_if )
{
    int i, pcr_count;
    int howmany = TS_IN_UDP;
    file_ts_packet * ts;
    synchro_t * p_synchro = &p_if->synchro;
    in_data_t * p_in_data = &p_if->in_data;
    own_pcr_t * p_own_pcr = &p_if->own_pcr; 
    
    p_synchro->slope = 0;
    pcr_count = 0;
    
    /*
     * Initialisation of the synchro mecanism : wait for 1 PCR
     * to evaluate delta_clock
     */

    while( 1 )
    {
        vlc_mutex_lock( &p_in_data->lock );
        
        while( p_in_data->end == p_in_data->start )
        {
            vlc_cond_wait(&p_in_data->notempty, &p_in_data->lock);
        }
        /*
        if( p_in_data->end == p_in_data->start )
        {
            intf_ErrMsg( "input error: init_synchro error, not enough PCR found\n" );
            return( -1 );
        }
        */
        vlc_mutex_unlock( &p_in_data->lock );
        
        ts = (file_ts_packet*)(p_in_data->buf + p_in_data->start);
        for( i=0 ; i < howmany ; i++, ts++ )
        {
            if( ts  == p_own_pcr->buf[p_own_pcr->start] && !(((u8*)ts)[5] & 0x80) )
            {
                p_synchro->last_pcr = ts;
                p_synchro->last_pcr_time = ConvertPCRTime( ts );
                p_synchro->delta_clock = mdate() - ConvertPCRTime(ts);
                adjust( p_if, ts );
                pcr_count++;
            }
        }
        
        vlc_mutex_lock( &p_in_data->lock );
        p_in_data->start++;
        p_in_data->start %= BUF_SIZE + 1;
        vlc_cond_signal( &p_in_data->notfull );
        vlc_mutex_unlock( &p_in_data->lock );
        
        if(pcr_count)
            break; 
    }
    return( 0 );
}

/*****************************************************************************
1000
 * input_DiskThread : main thread
1001
1002
1003
1004
1005
1006
1007
1008
 *****************************************************************************/

void input_DiskThread( input_file_t * p_if )
{
    ps_fill( p_if, 1 );    
    vlc_thread_exit();
}

1009
/*****************************************************************************
1010
 * input_FileOpen : open a file descriptor
1011
 *****************************************************************************/
1012
int input_FileOpen( input_thread_t *p_input )
Michel Kaempf's avatar
Michel Kaempf committed
1013
{
1014
1015
    options_t * p_options = &input_file.options;

1016
1017
1018
1019
1020
1021
    p_options->in = -1;

    p_options->playlist = (char **)p_input->p_source;
    p_options->i_list_index = p_input->i_port;

    if( file_next( p_options ) < 0 )
1022
    {
1023
        intf_ErrMsg( "input error: cannot open the file %s", p_input->p_source );
1024
1025
1026
    }

    input_file.b_die = 0;
1027
    safe_read( p_options, &p_options->i_file_type, 1 );
1028
1029
1030
1031
1032
1033
1034
1035
    
    switch( p_options->i_file_type )
    {
    case 0x00:
        p_options->pcr_pid = PCR_PID;
        ps_thread( &input_file );
        break;
    case 0x47:
1036
        intf_ErrMsg( "input error: ts files are not currently supported\n" );
1037
1038
1039
1040
1041
1042
1043
        return( 1 );
    default:
        intf_ErrMsg( "input error: cannot determine stream type\n" );
        return( 1 );
    }

    
1044
return( 0 );
Michel Kaempf's avatar
Michel Kaempf committed
1045
1046
}

1047
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
1048
 * input_FileRead : read from a file
1049
 *****************************************************************************/
Michel Kaempf's avatar
Michel Kaempf committed
1050
1051
1052
int input_FileRead( input_thread_t *p_input, const struct iovec *p_vector,
                    size_t i_count )
{
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
    in_data_t * p_in_data = &input_file.in_data;
    synchro_t * p_synchro = &input_file.synchro;
    own_pcr_t * p_own_pcr = &input_file.own_pcr;
    int i, howmany;
    file_ts_packet * ts;
    
    /* XXX XXX XXX
     * End condition not verified, should put a flag in ps_fill
     */
    howmany = TS_IN_UDP;

    vlc_mutex_lock( &p_in_data->lock );
    while( p_in_data->end == p_in_data->start )
    {
        if( !input_file.b_die )
            vlc_cond_wait( &p_in_data->notempty, &p_in_data->lock );
    }
    vlc_mutex_unlock( &p_in_data->lock );

    ts = (file_ts_packet*)(p_in_data->buf + p_in_data->start);
    for( i=0 ; i < howmany ; i++, ts++ )
    {             
        if( p_synchro->slope && (i == howmany-1) )
        {
            wait_a_moment( &input_file, ts );
        }
        if( ts  == p_own_pcr->buf[p_own_pcr->start] )
        {
            /* the TS packet contains a PCR, so we try to adjust the clock */
            adjust( &input_file, ts );
        }
    }

    for( i=0 ; i<howmany ; i++ )
    {
        memcpy( p_vector[i].iov_base, (char*)(ts - howmany + i), p_vector[i].iov_len );
    }

    vlc_mutex_lock(&p_in_data->lock);
    p_in_data->start++;
    p_in_data->start %= BUF_SIZE + 1;
    vlc_cond_signal(&p_in_data->notfull);
    vlc_mutex_unlock(&p_in_data->lock);
    
1097
    return( 188*howmany );
Michel Kaempf's avatar
Michel Kaempf committed
1098
1099
}

1100
/*****************************************************************************
1101
 * input_FileClose : close a file descriptor
1102
 *****************************************************************************/
1103
void input_FileClose( input_thread_t *p_input )
Michel Kaempf's avatar
Michel Kaempf committed
1104
{
1105
1106
1107
1108
1109
    input_file.b_die = 1;
    vlc_cond_signal( &input_file.in_data.notfull );
    vlc_thread_join( input_file.disk_thread );

    close( input_file.options.in );
Michel Kaempf's avatar
Michel Kaempf committed
1110
}