static int write_buffer_ac3( filter_t *p_filter, block_t *p_in_buf ) { static const size_t a52_size = A52_FRAME_NB * 4; if( unlikely( p_in_buf->i_buffer < 6 || p_in_buf->i_buffer > a52_size || p_in_buf->i_nb_samples != A52_FRAME_NB ) ) { /* Input is not correctly packetizer. Try to parse the buffer in order * to get the mandatory informations to play AC3 over S/PDIF */ vlc_a52_header_t a52; if( vlc_a52_header_Parse( &a52, p_in_buf->p_buffer, p_in_buf->i_buffer ) != VLC_SUCCESS || a52.b_eac3 || a52.i_size > p_in_buf->i_buffer ) return SPDIF_ERROR; p_in_buf->i_buffer = a52.i_size; p_in_buf->i_nb_samples = a52.i_samples; } if( p_in_buf->i_buffer + SPDIF_HEADER_SIZE > a52_size || write_init( p_filter, p_in_buf, a52_size, A52_FRAME_NB ) ) return SPDIF_ERROR; write_buffer( p_filter, p_in_buf ); write_finalize( p_filter, IEC61937_AC3 | ( ( p_in_buf->p_buffer[5] & 0x7 ) << 8 ) /* bsmod */, 8 /* in bits */ ); return SPDIF_SUCCESS; }
static int write_buffer_eac3( filter_t *p_filter, block_t *p_in_buf ) { filter_sys_t *p_sys = p_filter->p_sys; vlc_a52_header_t a52 = { }; if( vlc_a52_header_Parse( &a52, p_in_buf->p_buffer, p_in_buf->i_buffer ) != VLC_SUCCESS || a52.i_size > p_in_buf->i_buffer ) return SPDIF_ERROR; p_in_buf->i_buffer = a52.i_size; p_in_buf->i_nb_samples = a52.i_samples; if( !p_sys->p_out_buf && write_init( p_filter, p_in_buf, AOUT_SPDIF_SIZE * 4, AOUT_SPDIF_SIZE ) ) return SPDIF_ERROR; if( p_in_buf->i_buffer > p_sys->p_out_buf->i_buffer - p_sys->i_out_offset ) return SPDIF_ERROR; write_buffer( p_filter, p_in_buf ); if( a52.b_eac3 ) { if( ( a52.eac3.strmtyp == EAC3_STRMTYP_INDEPENDENT || a52.eac3.strmtyp == EAC3_STRMTYP_AC3_CONVERT ) && a52.i_blocks_per_sync_frame != 6 ) { /* cf. Annex E 2.3.1.2 of AC3 spec */ if( a52.eac3.i_substreamid == 0 ) p_sys->eac3.i_nb_blocks_substream0 += a52.i_blocks_per_sync_frame; if( p_sys->eac3.i_nb_blocks_substream0 != 6 ) return SPDIF_MORE_DATA; else p_sys->eac3.i_nb_blocks_substream0 = 0; } write_finalize( p_filter, IEC61937_EAC3, 1 /* in bytes */ ); return SPDIF_SUCCESS; } else return SPDIF_MORE_DATA; }
/**************************************************************************** * DecodeBlock: the whole thing **************************************************************************** * This function is called just after the thread is launched. ****************************************************************************/ static block_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block ) { decoder_sys_t *p_sys = p_dec->p_sys; uint8_t p_header[VLC_A52_HEADER_SIZE]; uint8_t *p_buf; block_t *p_out_buffer; if( !pp_block || !*pp_block ) return NULL; if( (*pp_block)->i_flags&(BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) ) { if( (*pp_block)->i_flags&BLOCK_FLAG_CORRUPTED ) { p_sys->i_state = STATE_NOSYNC; block_BytestreamEmpty( &p_sys->bytestream ); } date_Set( &p_sys->end_date, 0 ); block_Release( *pp_block ); return NULL; } if( !date_Get( &p_sys->end_date ) && (*pp_block)->i_pts <= VLC_TS_INVALID) { /* We've just started the stream, wait for the first PTS. */ block_Release( *pp_block ); return NULL; } block_BytestreamPush( &p_sys->bytestream, *pp_block ); while( 1 ) { switch( p_sys->i_state ) { case STATE_NOSYNC: while( block_PeekBytes( &p_sys->bytestream, p_header, 2 ) == VLC_SUCCESS ) { if( p_header[0] == 0x0b && p_header[1] == 0x77 ) { p_sys->i_state = STATE_SYNC; break; } block_SkipByte( &p_sys->bytestream ); } if( p_sys->i_state != STATE_SYNC ) { block_BytestreamFlush( &p_sys->bytestream ); /* Need more data */ return NULL; } case STATE_SYNC: /* New frame, set the Presentation Time Stamp */ p_sys->i_pts = p_sys->bytestream.p_block->i_pts; if( p_sys->i_pts > VLC_TS_INVALID && p_sys->i_pts != date_Get( &p_sys->end_date ) ) { date_Set( &p_sys->end_date, p_sys->i_pts ); } p_sys->i_state = STATE_HEADER; case STATE_HEADER: /* Get A/52 frame header (VLC_A52_HEADER_SIZE bytes) */ if( block_PeekBytes( &p_sys->bytestream, p_header, VLC_A52_HEADER_SIZE ) != VLC_SUCCESS ) { /* Need more data */ return NULL; } /* Check if frame is valid and get frame info */ if( vlc_a52_header_Parse( &p_sys->frame, p_header, VLC_A52_HEADER_SIZE ) ) { msg_Dbg( p_dec, "emulated sync word" ); block_SkipByte( &p_sys->bytestream ); p_sys->i_state = STATE_NOSYNC; break; } p_sys->i_state = STATE_NEXT_SYNC; case STATE_NEXT_SYNC: /* TODO: If pp_block == NULL, flush the buffer without checking the * next sync word */ /* Check if next expected frame contains the sync word */ if( block_PeekOffsetBytes( &p_sys->bytestream, p_sys->frame.i_size, p_header, 2 ) != VLC_SUCCESS ) { /* Need more data */ return NULL; } if( p_sys->b_packetizer && p_header[0] == 0 && p_header[1] == 0 ) { /* A52 wav files and audio CD's use stuffing */ p_sys->i_state = STATE_GET_DATA; break; } if( p_header[0] != 0x0b || p_header[1] != 0x77 ) { msg_Dbg( p_dec, "emulated sync word " "(no sync on following frame)" ); p_sys->i_state = STATE_NOSYNC; block_SkipByte( &p_sys->bytestream ); break; } p_sys->i_state = STATE_SEND_DATA; break; case STATE_GET_DATA: /* Make sure we have enough data. * (Not useful if we went through NEXT_SYNC) */ if( block_WaitBytes( &p_sys->bytestream, p_sys->frame.i_size ) != VLC_SUCCESS ) { /* Need more data */ return NULL; } p_sys->i_state = STATE_SEND_DATA; case STATE_SEND_DATA: if( !(p_buf = GetOutBuffer( p_dec, &p_out_buffer )) ) { //p_dec->b_error = true; return NULL; } /* Copy the whole frame into the buffer. When we reach this point * we already know we have enough data available. */ block_GetBytes( &p_sys->bytestream, p_buf, __MIN( p_sys->frame.i_size, p_out_buffer->i_buffer ) ); /* Make sure we don't reuse the same pts twice */ if( p_sys->i_pts == p_sys->bytestream.p_block->i_pts ) p_sys->i_pts = p_sys->bytestream.p_block->i_pts = VLC_TS_INVALID; /* So p_block doesn't get re-added several times */ *pp_block = block_BytestreamPop( &p_sys->bytestream ); p_sys->i_state = STATE_NOSYNC; return p_out_buffer; } } }
static block_t *PacketizeBlock( decoder_t *p_dec, block_t **pp_block ) { decoder_sys_t *p_sys = p_dec->p_sys; uint8_t p_header[VLC_A52_HEADER_SIZE]; block_t *p_out_buffer; block_t *p_block = pp_block ? *pp_block : NULL; if( p_block ) { if( p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY | BLOCK_FLAG_CORRUPTED) ) { /* First always drain complete blocks before discontinuity */ block_t *p_drain = PacketizeBlock( p_dec, NULL ); if(p_drain) return p_drain; PacketizeFlush( p_dec ); if( p_block->i_flags & BLOCK_FLAG_CORRUPTED ) { block_Release( p_block ); return NULL; } } if( !date_Get( &p_sys->end_date ) && p_block->i_pts <= VLC_TS_INVALID) { /* We've just started the stream, wait for the first PTS. */ block_Release( p_block ); return NULL; } block_BytestreamPush( &p_sys->bytestream, p_block ); } while( 1 ) { switch( p_sys->i_state ) { case STATE_NOSYNC: while( block_PeekBytes( &p_sys->bytestream, p_header, 2 ) == VLC_SUCCESS ) { if( p_header[0] == 0x0b && p_header[1] == 0x77 ) { p_sys->i_state = STATE_SYNC; break; } block_SkipByte( &p_sys->bytestream ); } if( p_sys->i_state != STATE_SYNC ) { block_BytestreamFlush( &p_sys->bytestream ); /* Need more data */ return NULL; } case STATE_SYNC: /* New frame, set the Presentation Time Stamp */ p_sys->i_pts = p_sys->bytestream.p_block->i_pts; if( p_sys->i_pts > VLC_TS_INVALID && p_sys->i_pts != date_Get( &p_sys->end_date ) ) { date_Set( &p_sys->end_date, p_sys->i_pts ); } p_sys->i_state = STATE_HEADER; case STATE_HEADER: /* Get A/52 frame header (VLC_A52_HEADER_SIZE bytes) */ if( block_PeekBytes( &p_sys->bytestream, p_header, VLC_A52_HEADER_SIZE ) != VLC_SUCCESS ) { /* Need more data */ return NULL; } /* Check if frame is valid and get frame info */ vlc_a52_header_t a52; if( vlc_a52_header_Parse( &a52, p_header, VLC_A52_HEADER_SIZE ) ) { msg_Dbg( p_dec, "emulated sync word" ); block_SkipByte( &p_sys->bytestream ); p_sys->i_state = STATE_NOSYNC; break; } if( a52.b_eac3 && a52.eac3.strmtyp != EAC3_STRMTYP_INDEPENDENT ) { /* Use the channel configuration of the independent stream */ p_sys->frame.i_samples = a52.i_samples; p_sys->frame.i_size = a52.i_size; } else p_sys->frame = a52; p_sys->i_state = STATE_NEXT_SYNC; case STATE_NEXT_SYNC: /* Check if next expected frame contains the sync word */ if( block_PeekOffsetBytes( &p_sys->bytestream, p_sys->frame.i_size, p_header, 2 ) != VLC_SUCCESS ) { if( p_block == NULL ) /* drain */ { p_sys->i_state = STATE_GET_DATA; break; } /* Need more data */ return NULL; } if( p_header[0] == 0 && p_header[1] == 0 ) { /* A52 wav files and audio CD's use stuffing */ p_sys->i_state = STATE_GET_DATA; break; } if( p_header[0] != 0x0b || p_header[1] != 0x77 ) { msg_Dbg( p_dec, "emulated sync word " "(no sync on following frame)" ); p_sys->i_state = STATE_NOSYNC; block_SkipByte( &p_sys->bytestream ); break; } p_sys->i_state = STATE_GET_DATA; break; case STATE_GET_DATA: /* Make sure we have enough data. */ if( block_WaitBytes( &p_sys->bytestream, p_sys->frame.i_size ) != VLC_SUCCESS ) { /* Need more data */ return NULL; } p_sys->i_state = STATE_SEND_DATA; case STATE_SEND_DATA: if( !(p_out_buffer = GetOutBuffer( p_dec )) ) { return NULL; } /* Copy the whole frame into the buffer. When we reach this point * we already know we have enough data available. */ block_GetBytes( &p_sys->bytestream, p_out_buffer->p_buffer, __MIN( p_sys->frame.i_size, p_out_buffer->i_buffer ) ); /* Make sure we don't reuse the same pts twice */ if( p_sys->i_pts == p_sys->bytestream.p_block->i_pts ) p_sys->i_pts = p_sys->bytestream.p_block->i_pts = VLC_TS_INVALID; if( p_sys->b_discontuinity ) { p_out_buffer->i_flags |= BLOCK_FLAG_DISCONTINUITY; p_sys->b_discontuinity = false; } /* So p_block doesn't get re-added several times */ if( pp_block ) *pp_block = block_BytestreamPop( &p_sys->bytestream ); p_sys->i_state = STATE_NOSYNC; return p_out_buffer; } } }