static block_t *PacketizeParse(void *p_private, bool *pb_ts_used, block_t *p_block) { decoder_t *p_dec = p_private; /* Remove trailing 0 bytes */ while (p_block->i_buffer > 5 && p_block->p_buffer[p_block->i_buffer-1] == 0x00 ) p_block->i_buffer--; return ParseNALBlock( p_dec, pb_ts_used, p_block ); }
/**************************************************************************** * PacketizeAVC1: Takes VCL blocks of data and creates annexe B type NAL stream * Will always use 4 byte 0 0 0 1 startcodes * Will prepend a SPS and PPS before each keyframe ****************************************************************************/ static block_t *PacketizeAVC1( decoder_t *p_dec, block_t **pp_block ) { decoder_sys_t *p_sys = p_dec->p_sys; block_t *p_block; block_t *p_ret = NULL; uint8_t *p; if( !pp_block || !*pp_block ) return NULL; if( (*pp_block)->i_flags&(BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) ) { block_Release( *pp_block ); return NULL; } p_block = *pp_block; *pp_block = NULL; for( p = p_block->p_buffer; p < &p_block->p_buffer[p_block->i_buffer]; ) { block_t *p_pic; bool b_dummy; int i_size = 0; int i; for( i = 0; i < p_sys->i_avcC_length_size; i++ ) { i_size = (i_size << 8) | (*p++); } if( i_size <= 0 || i_size > ( p_block->p_buffer + p_block->i_buffer - p ) ) { msg_Err( p_dec, "Broken frame : size %d is too big", i_size ); break; } block_t *p_part = CreateAnnexbNAL( p_dec, p, i_size ); if( !p_part ) break; p_part->i_dts = p_block->i_dts; p_part->i_pts = p_block->i_pts; /* Parse the NAL */ if( ( p_pic = ParseNALBlock( p_dec, &b_dummy, p_part ) ) ) { block_ChainAppend( &p_ret, p_pic ); } p += i_size; } block_Release( p_block ); return p_ret; }
static block_t *PacketizeParse(void *p_private, bool *pb_ts_used, block_t *p_block) { decoder_t *p_dec = p_private; decoder_sys_t *p_sys = p_dec->p_sys; /* Remove trailing 0 bytes */ while (p_block->i_buffer > 5 && p_block->p_buffer[p_block->i_buffer-1] == 0x00 ) p_block->i_buffer--; p_block = ParseNALBlock( p_dec, pb_ts_used, p_block ); if( p_block ) cc_storage_commit( p_sys->p_ccs, p_block ); return p_block; }
/**************************************************************************** * PacketizeAVC1: Takes VCL blocks of data and creates annexe B type NAL stream * Will always use 4 byte 0 0 0 1 startcodes * Will prepend a SPS and PPS before each keyframe ****************************************************************************/ static block_t *PacketizeAVC1( decoder_t *p_dec, block_t **pp_block ) { decoder_sys_t *p_sys = p_dec->p_sys; block_t *p_block; block_t *p_ret = NULL; uint8_t *p; if( !pp_block || !*pp_block ) return NULL; p_block = *pp_block; *pp_block = NULL; for( p = p_block->p_buffer; p < &p_block->p_buffer[p_block->i_buffer]; ) { block_t *p_pic; int i_size = 0; int i; for( i = 0; i < p_sys->i_avcC_length_size; i++ ) { i_size = (i_size << 8) | (*p++); } if( i_size > 0 ) { block_t *p_part = nal_get_annexeb( p_dec, p, i_size ); p_part->i_dts = p_block->i_dts; p_part->i_pts = p_block->i_pts; /* Parse the NAL */ if( ( p_pic = ParseNALBlock( p_dec, p_part ) ) ) { block_ChainAppend( &p_ret, p_pic ); } } p += i_size; } block_Release( p_block ); return p_ret; }
/**************************************************************************** * Packetize: the whole thing * Search for the startcodes 3 or more bytes * Feed ParseNALBlock ALWAYS with 4 byte startcode prepended NALs ****************************************************************************/ static block_t *Packetize( decoder_t *p_dec, block_t **pp_block ) { decoder_sys_t *p_sys = p_dec->p_sys; block_t *p_pic; if( !pp_block || !*pp_block ) return NULL; block_BytestreamPush( &p_sys->bytestream, *pp_block ); for( ;; ) { switch( p_sys->i_state ) { case STATE_NOSYNC: /* Skip until 3 byte startcode 0 0 1 */ if( block_FindStartcodeFromOffset( &p_sys->bytestream, &p_sys->i_offset, p_sys->startcode+1, 3 ) == VLC_SUCCESS) { p_sys->i_state = STATE_NEXT_SYNC; } if( p_sys->i_offset ) { /* skip the data */ block_SkipBytes( &p_sys->bytestream, p_sys->i_offset ); p_sys->i_offset = 0; block_BytestreamFlush( &p_sys->bytestream ); } if( p_sys->i_state != STATE_NEXT_SYNC ) { /* Need more data */ return NULL; } p_sys->i_offset = 1; /* To find next startcode */ case STATE_NEXT_SYNC: /* Find the next 3 byte startcode 0 0 1*/ if( block_FindStartcodeFromOffset( &p_sys->bytestream, &p_sys->i_offset, p_sys->startcode+1, 3 ) != VLC_SUCCESS) { /* Need more data */ return NULL; } /* Get the new fragment and set the pts/dts */ p_pic = block_New( p_dec, p_sys->i_offset +1 ); p_pic->i_pts = p_sys->bytestream.p_block->i_pts; p_pic->i_dts = p_sys->bytestream.p_block->i_dts; /* Force 4 byte startcode 0 0 0 1 */ p_pic->p_buffer[0] = 0; block_GetBytes( &p_sys->bytestream, &p_pic->p_buffer[1], p_pic->i_buffer-1 ); /* Remove trailing 0 bytes */ while( p_pic->i_buffer && (!p_pic->p_buffer[p_pic->i_buffer-1] ) ) p_pic->i_buffer--; p_sys->i_offset = 0; /* Parse the NAL */ if( !( p_pic = ParseNALBlock( p_dec, p_pic ) ) ) { p_sys->i_state = STATE_NOSYNC; break; } #if 0 msg_Dbg( p_dec, "pts="I64Fd" dts="I64Fd, p_pic->i_pts, p_pic->i_dts ); #endif /* 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_pic; } } }
/***************************************************************************** * Open: probe the packetizer and return score * When opening after demux, the packetizer is only loaded AFTER the decoder * That means that what you set in fmt_out is ignored by the decoder in this special case *****************************************************************************/ static int Open( vlc_object_t *p_this ) { decoder_t *p_dec = (decoder_t*)p_this; decoder_sys_t *p_sys; if( p_dec->fmt_in.i_codec != VLC_FOURCC( 'h', '2', '6', '4') && p_dec->fmt_in.i_codec != VLC_FOURCC( 'H', '2', '6', '4') && p_dec->fmt_in.i_codec != VLC_FOURCC( 'V', 'S', 'S', 'H') && p_dec->fmt_in.i_codec != VLC_FOURCC( 'v', 's', 's', 'h') && p_dec->fmt_in.i_codec != VLC_FOURCC( 'D', 'A', 'V', 'C') && ( p_dec->fmt_in.i_codec != VLC_FOURCC( 'a', 'v', 'c', '1') || p_dec->fmt_in.i_extra < 7 ) ) { return VLC_EGENERIC; } /* Allocate the memory needed to store the decoder's structure */ if( ( p_dec->p_sys = p_sys = malloc( sizeof(decoder_sys_t) ) ) == NULL ) { msg_Err( p_dec, "out of memory" ); return VLC_EGENERIC; } p_sys->i_state = STATE_NOSYNC; p_sys->i_offset = 0; p_sys->startcode[0] = 0; p_sys->startcode[1] = 0; p_sys->startcode[2] = 0; p_sys->startcode[3] = 1; p_sys->bytestream = block_BytestreamInit( p_dec ); p_sys->b_slice = VLC_FALSE; p_sys->p_frame = NULL; p_sys->b_sps = VLC_FALSE; p_sys->b_pps = VLC_FALSE; p_sys->p_sps = 0; p_sys->p_pps = 0; p_sys->b_header= VLC_FALSE; p_sys->slice.i_nal_type = -1; p_sys->slice.i_nal_ref_idc = -1; p_sys->slice.i_idr_pic_id = -1; p_sys->slice.i_frame_num = -1; p_sys->slice.i_frame_type = 0; p_sys->slice.i_pic_parameter_set_id = -1; p_sys->slice.i_field_pic_flag = 0; p_sys->slice.i_bottom_field_flag = -1; p_sys->slice.i_pic_order_cnt_lsb = -1; p_sys->slice.i_delta_pic_order_cnt_bottom = -1; /* Setup properties */ es_format_Copy( &p_dec->fmt_out, &p_dec->fmt_in ); p_dec->fmt_out.i_codec = VLC_FOURCC( 'h', '2', '6', '4' ); if( p_dec->fmt_in.i_codec == VLC_FOURCC( 'a', 'v', 'c', '1' ) ) { /* This type of stream is produced by mp4 and matroska * when we want to store it in another streamformat, you need to convert * The fmt_in.p_extra should ALWAYS contain the avcC * The fmt_out.p_extra should contain all the SPS and PPS with 4 byte startcodes */ uint8_t *p = &((uint8_t*)p_dec->fmt_in.p_extra)[4]; int i_sps, i_pps; int i; /* Parse avcC */ p_sys->i_avcC_length_size = 1 + ((*p++)&0x03); /* Read SPS */ i_sps = (*p++)&0x1f; for( i = 0; i < i_sps; i++ ) { int i_length = GetWBE( p ); block_t *p_sps = nal_get_annexeb( p_dec, p + 2, i_length ); p_sys->p_sps = block_Duplicate( p_sps ); p_sps->i_pts = p_sps->i_dts = mdate(); ParseNALBlock( p_dec, p_sps ); p += 2 + i_length; } /* Read PPS */ i_pps = *p++; for( i = 0; i < i_pps; i++ ) { int i_length = GetWBE( p ); block_t *p_pps = nal_get_annexeb( p_dec, p + 2, i_length ); p_sys->p_pps = block_Duplicate( p_pps ); p_pps->i_pts = p_pps->i_dts = mdate(); ParseNALBlock( p_dec, p_pps ); p += 2 + i_length; } msg_Dbg( p_dec, "avcC length size=%d, sps=%d, pps=%d", p_sys->i_avcC_length_size, i_sps, i_pps ); /* FIXME: FFMPEG isn't happy at all if you leave this */ if( p_dec->fmt_out.i_extra ) free( p_dec->fmt_out.p_extra ); p_dec->fmt_out.i_extra = 0; p_dec->fmt_out.p_extra = NULL; /* Set the new extradata */ p_dec->fmt_out.i_extra = p_sys->p_pps->i_buffer + p_sys->p_sps->i_buffer; p_dec->fmt_out.p_extra = (uint8_t*)malloc( p_dec->fmt_out.i_extra ); memcpy( (uint8_t*)p_dec->fmt_out.p_extra, p_sys->p_sps->p_buffer, p_sys->p_sps->i_buffer); memcpy( (uint8_t*)p_dec->fmt_out.p_extra+p_sys->p_sps->i_buffer, p_sys->p_pps->p_buffer, p_sys->p_pps->i_buffer); p_sys->b_header = VLC_TRUE; /* Set callback */ p_dec->pf_packetize = PacketizeAVC1; } else { /* This type of stream contains data with 3 of 4 byte startcodes * The fmt_in.p_extra MAY contain SPS/PPS with 4 byte startcodes * The fmt_out.p_extra should be the same */ /* Set callback */ p_dec->pf_packetize = Packetize; /* */ if( p_dec->fmt_in.i_extra > 0 ) { block_t *p_init = block_New( p_dec, p_dec->fmt_in.i_extra ); block_t *p_pic; memcpy( p_init->p_buffer, p_dec->fmt_in.p_extra, p_dec->fmt_in.i_extra ); while( ( p_pic = Packetize( p_dec, &p_init ) ) ) { /* Should not occur because we should only receive SPS/PPS */ block_Release( p_pic ); } } } return VLC_SUCCESS; }
/***************************************************************************** * Open: probe the packetizer and return score * When opening after demux, the packetizer is only loaded AFTER the decoder * That means that what you set in fmt_out is ignored by the decoder in this special case *****************************************************************************/ static int Open( vlc_object_t *p_this ) { decoder_t *p_dec = (decoder_t*)p_this; decoder_sys_t *p_sys; int i; if( p_dec->fmt_in.i_codec != VLC_CODEC_H264 ) return VLC_EGENERIC; if( p_dec->fmt_in.i_original_fourcc == VLC_FOURCC( 'a', 'v', 'c', '1') && p_dec->fmt_in.i_extra < 7 ) return VLC_EGENERIC; /* Allocate the memory needed to store the decoder's structure */ if( ( p_dec->p_sys = p_sys = malloc( sizeof(decoder_sys_t) ) ) == NULL ) { return VLC_ENOMEM; } packetizer_Init( &p_sys->packetizer, p_h264_startcode, sizeof(p_h264_startcode), p_h264_startcode, 1, 5, PacketizeReset, PacketizeParse, PacketizeValidate, p_dec ); p_sys->b_slice = false; p_sys->p_frame = NULL; p_sys->b_frame_sps = false; p_sys->b_frame_pps = false; p_sys->b_header= false; p_sys->b_sps = false; p_sys->b_pps = false; for( i = 0; i < SPS_MAX; i++ ) p_sys->pp_sps[i] = NULL; for( i = 0; i < PPS_MAX; i++ ) p_sys->pp_pps[i] = NULL; p_sys->i_recovery_frames = -1; p_sys->slice.i_nal_type = -1; p_sys->slice.i_nal_ref_idc = -1; p_sys->slice.i_idr_pic_id = -1; p_sys->slice.i_frame_num = -1; p_sys->slice.i_frame_type = 0; p_sys->slice.i_pic_parameter_set_id = -1; p_sys->slice.i_field_pic_flag = 0; p_sys->slice.i_bottom_field_flag = -1; p_sys->slice.i_pic_order_cnt_lsb = -1; p_sys->slice.i_delta_pic_order_cnt_bottom = -1; p_sys->i_frame_dts = VLC_TS_INVALID; p_sys->i_frame_pts = VLC_TS_INVALID; /* Setup properties */ es_format_Copy( &p_dec->fmt_out, &p_dec->fmt_in ); p_dec->fmt_out.i_codec = VLC_CODEC_H264; if( p_dec->fmt_in.i_original_fourcc == VLC_FOURCC( 'a', 'v', 'c', '1' ) ) { /* This type of stream is produced by mp4 and matroska * when we want to store it in another streamformat, you need to convert * The fmt_in.p_extra should ALWAYS contain the avcC * The fmt_out.p_extra should contain all the SPS and PPS with 4 byte startcodes */ uint8_t *p = &((uint8_t*)p_dec->fmt_in.p_extra)[4]; int i_sps, i_pps; bool b_dummy; int i; /* Parse avcC */ p_sys->i_avcC_length_size = 1 + ((*p++)&0x03); /* Read SPS */ i_sps = (*p++)&0x1f; for( i = 0; i < i_sps; i++ ) { uint16_t i_length = GetWBE( p ); p += 2; if( i_length > (uint8_t*)p_dec->fmt_in.p_extra + p_dec->fmt_in.i_extra - p ) { return VLC_EGENERIC; } block_t *p_sps = CreateAnnexbNAL( p_dec, p, i_length ); if( !p_sps ) return VLC_EGENERIC; ParseNALBlock( p_dec, &b_dummy, p_sps ); p += i_length; } /* Read PPS */ i_pps = *p++; for( i = 0; i < i_pps; i++ ) { uint16_t i_length = GetWBE( p ); p += 2; if( i_length > (uint8_t*)p_dec->fmt_in.p_extra + p_dec->fmt_in.i_extra - p ) { return VLC_EGENERIC; } block_t *p_pps = CreateAnnexbNAL( p_dec, p, i_length ); if( !p_pps ) return VLC_EGENERIC; ParseNALBlock( p_dec, &b_dummy, p_pps ); p += i_length; } msg_Dbg( p_dec, "avcC length size=%d, sps=%d, pps=%d", p_sys->i_avcC_length_size, i_sps, i_pps ); if( !p_sys->b_sps || !p_sys->b_pps ) return VLC_EGENERIC; /* FIXME: FFMPEG isn't happy at all if you leave this */ if( p_dec->fmt_out.i_extra > 0 ) free( p_dec->fmt_out.p_extra ); p_dec->fmt_out.i_extra = 0; p_dec->fmt_out.p_extra = NULL; /* Set the new extradata */ for( i = 0; i < SPS_MAX; i++ ) { if( p_sys->pp_sps[i] ) p_dec->fmt_out.i_extra += p_sys->pp_sps[i]->i_buffer; } for( i = 0; i < PPS_MAX; i++ ) { if( p_sys->pp_pps[i] ) p_dec->fmt_out.i_extra += p_sys->pp_pps[i]->i_buffer; } p_dec->fmt_out.p_extra = malloc( p_dec->fmt_out.i_extra ); if( p_dec->fmt_out.p_extra ) { uint8_t *p_dst = p_dec->fmt_out.p_extra; for( i = 0; i < SPS_MAX; i++ ) { if( p_sys->pp_sps[i] ) { memcpy( p_dst, p_sys->pp_sps[i]->p_buffer, p_sys->pp_sps[i]->i_buffer ); p_dst += p_sys->pp_sps[i]->i_buffer; } } for( i = 0; i < PPS_MAX; i++ ) { if( p_sys->pp_pps[i] ) { memcpy( p_dst, p_sys->pp_pps[i]->p_buffer, p_sys->pp_pps[i]->i_buffer ); p_dst += p_sys->pp_pps[i]->i_buffer; } } p_sys->b_header = true; } else { p_dec->fmt_out.i_extra = 0; } /* Set callback */ p_dec->pf_packetize = PacketizeAVC1; /* TODO CC ? */ } else { /* This type of stream contains data with 3 of 4 byte startcodes * The fmt_in.p_extra MAY contain SPS/PPS with 4 byte startcodes * The fmt_out.p_extra should be the same */ /* Set callback */ p_dec->pf_packetize = Packetize; p_dec->pf_get_cc = GetCc; /* */ p_sys->i_cc_pts = VLC_TS_INVALID; p_sys->i_cc_dts = VLC_TS_INVALID; p_sys->i_cc_flags = 0; cc_Init( &p_sys->cc ); cc_Init( &p_sys->cc_next ); /* */ if( p_dec->fmt_in.i_extra > 0 ) packetizer_Header( &p_sys->packetizer, p_dec->fmt_in.p_extra, p_dec->fmt_in.i_extra ); } return VLC_SUCCESS; }
/**************************************************************************** * Packetize: the whole thing ****************************************************************************/ static block_t *Packetize( decoder_t *p_dec, block_t **pp_block ) { decoder_sys_t *p_sys = p_dec->p_sys; block_t *p_pic; if( !pp_block || !*pp_block ) return NULL; block_BytestreamPush( &p_sys->bytestream, *pp_block ); for( ;; ) { switch( p_sys->i_state ) { case STATE_NOSYNC: if( block_FindStartcodeFromOffset( &p_sys->bytestream, &p_sys->i_offset, p_sys->startcode+1, 3 ) == VLC_SUCCESS) { p_sys->i_state = STATE_NEXT_SYNC; } if( p_sys->i_offset ) { block_SkipBytes( &p_sys->bytestream, p_sys->i_offset ); p_sys->i_offset = 0; block_BytestreamFlush( &p_sys->bytestream ); } if( p_sys->i_state != STATE_NEXT_SYNC ) { /* Need more data */ return NULL; } p_sys->i_offset = 1; /* To find next startcode */ case STATE_NEXT_SYNC: /* Find the next startcode */ if( block_FindStartcodeFromOffset( &p_sys->bytestream, &p_sys->i_offset, p_sys->startcode, 3 ) != VLC_SUCCESS) { if( block_FindStartcodeFromOffset( &p_sys->bytestream, &p_sys->i_offset, p_sys->startcode+1, 3 ) != VLC_SUCCESS ) { /* Need more data */ return NULL; } } /* Get the new fragment and set the pts/dts */ p_pic = block_New( p_dec, p_sys->i_offset ); p_pic->i_pts = p_sys->bytestream.p_block->i_pts; p_pic->i_dts = p_sys->bytestream.p_block->i_dts; block_GetBytes( &p_sys->bytestream, p_pic->p_buffer, p_pic->i_buffer ); p_sys->i_offset = 0; /* Parse the NAL */ if( !( p_pic = ParseNALBlock( p_dec, p_pic ) ) ) { p_sys->i_state = STATE_NOSYNC; break; } /* 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_pic; } } }
/***************************************************************************** * Open: probe the packetizer and return score *****************************************************************************/ static int Open( vlc_object_t *p_this ) { decoder_t *p_dec = (decoder_t*)p_this; decoder_sys_t *p_sys; if( p_dec->fmt_in.i_codec != VLC_FOURCC( 'h', '2', '6', '4') && p_dec->fmt_in.i_codec != VLC_FOURCC( 'H', '2', '6', '4') && p_dec->fmt_in.i_codec != VLC_FOURCC( 'V', 'S', 'S', 'H') && p_dec->fmt_in.i_codec != VLC_FOURCC( 'v', 's', 's', 'h') && ( p_dec->fmt_in.i_codec != VLC_FOURCC( 'a', 'v', 'c', '1') || p_dec->fmt_in.i_extra < 7 ) ) { return VLC_EGENERIC; } /* Allocate the memory needed to store the decoder's structure */ if( ( p_dec->p_sys = p_sys = malloc( sizeof(decoder_sys_t) ) ) == NULL ) { msg_Err( p_dec, "out of memory" ); return VLC_EGENERIC; } p_sys->i_state = STATE_NOSYNC; p_sys->i_offset = 0; p_sys->startcode[0] = 0; p_sys->startcode[1] = 0; p_sys->startcode[2] = 0; p_sys->startcode[3] = 1; p_sys->bytestream = block_BytestreamInit( p_dec ); p_sys->b_slice = VLC_FALSE; p_sys->p_frame = NULL; p_sys->i_dts = 0; p_sys->i_pts = 0; p_sys->i_flags = 0; p_sys->b_sps = VLC_FALSE; p_sys->i_nal_type = -1; p_sys->i_nal_ref_idc = -1; p_sys->i_idr_pic_id = -1; p_sys->i_frame_num = -1; /* Setup properties */ es_format_Copy( &p_dec->fmt_out, &p_dec->fmt_in ); p_dec->fmt_out.i_codec = VLC_FOURCC( 'h', '2', '6', '4' ); /* FIXME: FFMPEG isn't happy at all if you leave this */ if( p_dec->fmt_out.i_extra ) free( p_dec->fmt_out.p_extra ); p_dec->fmt_out.i_extra = 0; p_dec->fmt_out.p_extra = 0; if( p_dec->fmt_in.i_codec == VLC_FOURCC( 'a', 'v', 'c', '1' ) ) { uint8_t *p = &((uint8_t*)p_dec->fmt_in.p_extra)[4]; int i_sps, i_pps; int i; /* Parse avcC */ p_sys->i_avcC_length_size = 1 + ((*p++)&0x03); /* Read SPS */ i_sps = (*p++)&0x1f; for( i = 0; i < i_sps; i++ ) { int i_length = GetWBE( p ); block_t *p_sps = nal_get_annexeb( p_dec, p + 2, i_length ); ParseNALBlock( p_dec, p_sps ); p += 2 + i_length; } /* Read PPS */ i_pps = *p++; for( i = 0; i < i_pps; i++ ) { int i_length = GetWBE( p ); block_t *p_pps = nal_get_annexeb( p_dec, p + 2, i_length ); ParseNALBlock( p_dec, p_pps ); p += 2 + i_length; } msg_Dbg( p_dec, "avcC length size=%d sps=%d pps=%d", p_sys->i_avcC_length_size, i_sps, i_pps ); /* Set callback */ p_dec->pf_packetize = PacketizeAVC1; } else { /* Set callback */ p_dec->pf_packetize = Packetize; } return VLC_SUCCESS; }