/**************************************************************************** * 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; }
/***************************************************************************** * 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 *****************************************************************************/ 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; }