📄 input_file.c
字号:
switch( p_ps->pes_type ) { case VIDEO_PES: delta = 0x20; /* 0x20 - 0x2f */ type = "MPEG video"; tofind = p_ps->pes_id; break; case AUDIO_PES: delta = 0x40; /* 0x40 - 0x5f */ type = "MPEG audio"; tofind = p_ps->pes_id; break; /* XXX: 0x64 is for the PMT, so don't take it !!! */ case AC3_PES: delta = 0x80; /* 0x80 - 0x8f */ type = "MPEG private (AC3 audio)"; tofind = p_ps->private_id; break; 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)"; tofind = p_ps->private_id; break; default: return(-1); } 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; } return ( i );}/****************************************************************************** * 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 d閎ut 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 d閎ut 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 */ /* 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; 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; 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; /* stuffing bytes */ for (i=45 ; i < 188 ; i++) ts[i]=0xff; /* facultatif ? */ 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 ); 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 ); 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 */ vlc_thread_create( &p_if->disk_thread, "disk thread", (vlc_thread_func_t)input_DiskThread, p_if ); /* Init the synchronization XXX add error detection !!! */ init_synchro( p_if );}/****************************************************************************** * ps_read : ps reading method ******************************************************************************/ssize_t ps_read( options_t *p_options, ps_t * p_ps, void *ts ){ int pid, readbytes = 0; int datasize; p_ps->ts_written = 0; 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 */ readbytes = safe_read( p_options, p_ps->ps_buffer + datasize, PS_BUFFER_SIZE - datasize); if(readbytes == 0) { intf_ErrMsg ( "input: ps read error\n"); 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 ) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -