@ -291,9 +291,10 @@ static int cbs_h2645_read_more_rbsp_data(BitstreamContext *bc)
# define byte_alignment(rw) (bitstream_tell(rw) % 8)
# define byte_alignment(rw) (bitstream_tell(rw) % 8)
# define allocate(name, size) do { \
# define allocate(name, size) do { \
name = av_m allocz( size ) ; \
name # # _ref = av_buffer_ allocz( size ) ; \
if ( ! name ) \
if ( ! name # # _ref ) \
return AVERROR ( ENOMEM ) ; \
return AVERROR ( ENOMEM ) ; \
name = name # # _ref - > data ; \
} while ( 0 )
} while ( 0 )
# define FUNC(name) FUNC_H264(READWRITE, name)
# define FUNC(name) FUNC_H264(READWRITE, name)
@ -393,82 +394,68 @@ static int cbs_h2645_read_more_rbsp_data(BitstreamContext *bc)
# undef allocate
# undef allocate
static void cbs_h264_free_sei ( H264RawSEI * sei )
static void cbs_h264_free_sei_payload ( H264RawSEIPayload * payload )
{
{
int i ;
switch ( payload - > payload_type ) {
for ( i = 0 ; i < sei - > payload_count ; i + + ) {
case H264_SEI_TYPE_BUFFERING_PERIOD :
H264RawSEIPayload * payload = & sei - > payload [ i ] ;
case H264_SEI_TYPE_PIC_TIMING :
case H264_SEI_TYPE_RECOVERY_POINT :
switch ( payload - > payload_type ) {
case H264_SEI_TYPE_DISPLAY_ORIENTATION :
case H264_SEI_TYPE_BUFFERING_PERIOD :
break ;
case H264_SEI_TYPE_PIC_TIMING :
case H264_SEI_TYPE_USER_DATA_REGISTERED :
case H264_SEI_TYPE_RECOVERY_POINT :
av_buffer_unref ( & payload - > payload . user_data_registered . data_ref ) ;
case H264_SEI_TYPE_DISPLAY_ORIENTATION :
break ;
break ;
case H264_SEI_TYPE_USER_DATA_UNREGISTERED :
case H264_SEI_TYPE_USER_DATA_REGISTERED :
av_buffer_unref ( & payload - > payload . user_data_unregistered . data_ref ) ;
av_freep ( & payload - > payload . user_data_registered . data ) ;
break ;
break ;
default :
case H264_SEI_TYPE_USER_DATA_UNREGISTERED :
av_buffer_unref ( & payload - > payload . other . data_ref ) ;
av_freep ( & payload - > payload . user_data_unregistered . data ) ;
break ;
break ;
default :
av_freep ( & payload - > payload . other . data ) ;
break ;
}
}
}
}
}
static void cbs_h264_free_slice ( H264RawSlice * slice )
static void cbs_h264_free_sei ( void * unit , uint8_t * content )
{
{
av_freep ( & slice - > data ) ;
H264RawSEI * sei = ( H264RawSEI * ) content ;
int i ;
for ( i = 0 ; i < sei - > payload_count ; i + + )
cbs_h264_free_sei_payload ( & sei - > payload [ i ] ) ;
av_freep ( & content ) ;
}
}
static void cbs_h264_free_nal_unit ( CodedBitstreamUnit * unit )
static void cbs_h264_free_slice ( void * unit , uint8_t * conten t )
{
{
switch ( unit - > type ) {
H264RawSlice * slice = ( H264RawSlice * ) content ;
case H264_NAL_SEI :
av_buffer_unref ( & slice - > data_ref ) ;
cbs_h264_free_sei ( unit - > content ) ;
av_freep ( & content ) ;
break ;
case H264_NAL_IDR_SLICE :
case H264_NAL_SLICE :
cbs_h264_free_slice ( unit - > content ) ;
break ;
}
av_freep ( & unit - > content ) ;
}
}
static void cbs_h265_free_nal_unit ( CodedBitstreamUnit * uni t )
static void cbs_h265_free_vps ( void * unit , uint8_t * content )
{
{
switch ( unit - > type ) {
H265RawVPS * vps = ( H265RawVPS * ) content ;
case HEVC_NAL_VPS :
av_buffer_unref ( & vps - > extension_data . data_ref ) ;
av_freep ( & ( ( H265RawVPS * ) unit - > content ) - > extension_data . data ) ;
av_freep ( & content ) ;
break ;
}
case HEVC_NAL_SPS :
av_freep ( & ( ( H265RawSPS * ) unit - > content ) - > extension_data . data ) ;
static void cbs_h265_free_sps ( void * unit , uint8_t * content )
break ;
{
case HEVC_NAL_PPS :
H265RawSPS * sps = ( H265RawSPS * ) content ;
av_freep ( & ( ( H265RawPPS * ) unit - > content ) - > extension_data . data ) ;
av_buffer_unref ( & sps - > extension_data . data_ref ) ;
break ;
av_freep ( & content ) ;
case HEVC_NAL_TRAIL_N :
}
case HEVC_NAL_TRAIL_R :
case HEVC_NAL_TSA_N :
static void cbs_h265_free_pps ( void * unit , uint8_t * content )
case HEVC_NAL_TSA_R :
{
case HEVC_NAL_STSA_N :
H265RawPPS * pps = ( H265RawPPS * ) content ;
case HEVC_NAL_STSA_R :
av_buffer_unref ( & pps - > extension_data . data_ref ) ;
case HEVC_NAL_RADL_N :
av_freep ( & content ) ;
case HEVC_NAL_RADL_R :
}
case HEVC_NAL_RASL_N :
case HEVC_NAL_RASL_R :
static void cbs_h265_free_slice ( void * unit , uint8_t * content )
case HEVC_NAL_BLA_W_LP :
{
case HEVC_NAL_BLA_W_RADL :
H265RawSlice * slice = ( H265RawSlice * ) content ;
case HEVC_NAL_BLA_N_LP :
av_buffer_unref ( & slice - > data_ref ) ;
case HEVC_NAL_IDR_W_RADL :
av_freep ( & content ) ;
case HEVC_NAL_IDR_N_LP :
case HEVC_NAL_CRA_NUT :
av_freep ( & ( ( H265RawSlice * ) unit - > content ) - > data ) ;
break ;
}
av_freep ( & unit - > content ) ;
}
}
static int cbs_h2645_fragment_add_nals ( CodedBitstreamContext * ctx ,
static int cbs_h2645_fragment_add_nals ( CodedBitstreamContext * ctx ,
@ -494,7 +481,7 @@ static int cbs_h2645_fragment_add_nals(CodedBitstreamContext *ctx,
memset ( data + size , 0 , AV_INPUT_BUFFER_PADDING_SIZE ) ;
memset ( data + size , 0 , AV_INPUT_BUFFER_PADDING_SIZE ) ;
err = ff_cbs_insert_unit_data ( ctx , frag , - 1 , nal - > type ,
err = ff_cbs_insert_unit_data ( ctx , frag , - 1 , nal - > type ,
data , nal - > size ) ;
data , nal - > size , NULL ) ;
if ( err < 0 ) {
if ( err < 0 ) {
av_freep ( & data ) ;
av_freep ( & data ) ;
return err ;
return err ;
@ -705,35 +692,32 @@ static int cbs_h264_read_nal_unit(CodedBitstreamContext *ctx,
{
{
H264RawSPS * sps ;
H264RawSPS * sps ;
sps = av_mallocz ( sizeof ( * sps ) ) ;
err = ff_cbs_alloc_unit_content ( ctx , unit , sizeof ( * sps ) , NULL ) ;
if ( ! sps )
if ( err < 0 )
return AVERROR ( ENOMEM ) ;
return err ;
sps = unit - > content ;
err = cbs_h264_read_sps ( ctx , & bc , sps ) ;
err = cbs_h264_read_sps ( ctx , & bc , sps ) ;
if ( err > = 0 )
if ( err < 0 )
err = cbs_h264_replace_sps ( ctx , sps ) ;
if ( err < 0 ) {
av_free ( sps ) ;
return err ;
return err ;
}
unit - > content = sps ;
err = cbs_h264_replace_sps ( ctx , sps ) ;
if ( err < 0 )
return err ;
}
}
break ;
break ;
case H264_NAL_SPS_EXT :
case H264_NAL_SPS_EXT :
{
{
H264RawSPSExtension * sps_ext ;
err = ff_cbs_alloc_unit_content ( ctx , unit ,
sizeof ( H264RawSPSExtension ) ,
sps_ext = av_mallocz ( sizeof ( * sps_ext ) ) ;
NULL ) ;
if ( ! sps_ext )
if ( err < 0 )
return AVERROR ( ENOMEM ) ;
err = cbs_h264_read_sps_extension ( ctx , & bc , sps_ext ) ;
if ( err < 0 ) {
av_free ( sps_ext ) ;
return err ;
return err ;
}
unit - > content = sps_ext ;
err = cbs_h264_read_sps_extension ( ctx , & bc , unit - > content ) ;
if ( err < 0 )
return err ;
}
}
break ;
break ;
@ -741,18 +725,18 @@ static int cbs_h264_read_nal_unit(CodedBitstreamContext *ctx,
{
{
H264RawPPS * pps ;
H264RawPPS * pps ;
pps = av_mallocz ( sizeof ( * pps ) ) ;
err = ff_cbs_alloc_unit_content ( ctx , unit , sizeof ( * pps ) , NULL ) ;
if ( ! pps )
if ( err < 0 )
return AVERROR ( ENOMEM ) ;
return err ;
pps = unit - > content ;
err = cbs_h264_read_pps ( ctx , & bc , pps ) ;
err = cbs_h264_read_pps ( ctx , & bc , pps ) ;
if ( err > = 0 )
if ( err < 0 )
err = cbs_h264_replace_pps ( ctx , pps ) ;
if ( err < 0 ) {
av_free ( pps ) ;
return err ;
return err ;
}
unit - > content = pps ;
err = cbs_h264_replace_pps ( ctx , pps ) ;
if ( err < 0 )
return err ;
}
}
break ;
break ;
@ -763,14 +747,15 @@ static int cbs_h264_read_nal_unit(CodedBitstreamContext *ctx,
H264RawSlice * slice ;
H264RawSlice * slice ;
int pos , len ;
int pos , len ;
slice = av_mallocz ( sizeof ( * slice ) ) ;
err = ff_cbs_alloc_unit_content ( ctx , unit , sizeof ( * slice ) ,
if ( ! slice )
& cbs_h264_free_slice ) ;
return AVERROR ( ENOMEM ) ;
if ( err < 0 )
return err ;
slice = unit - > content ;
err = cbs_h264_read_slice_header ( ctx , & bc , & slice - > header ) ;
err = cbs_h264_read_slice_header ( ctx , & bc , & slice - > header ) ;
if ( err < 0 ) {
if ( err < 0 )
av_free ( slice ) ;
return err ;
return err ;
}
pos = bitstream_tell ( & bc ) ;
pos = bitstream_tell ( & bc ) ;
len = unit - > data_size ;
len = unit - > data_size ;
@ -783,54 +768,42 @@ static int cbs_h264_read_nal_unit(CodedBitstreamContext *ctx,
}
}
slice - > data_size = len - pos / 8 ;
slice - > data_size = len - pos / 8 ;
slice - > data = av_malloc ( slice - > data_size +
slice - > data_ref = av_buffer_alloc ( slice - > data_size +
AV_INPUT_BUFFER_PADDING_SIZE ) ;
AV_INPUT_BUFFER_PADDING_SIZE ) ;
if ( ! slice - > data ) {
if ( ! slice - > data_ref )
av_free ( slice ) ;
return AVERROR ( ENOMEM ) ;
return AVERROR ( ENOMEM ) ;
}
slice - > data = slice - > data_ref - > data ;
memcpy ( slice - > data ,
memcpy ( slice - > data ,
unit - > data + pos / 8 , slice - > data_size ) ;
unit - > data + pos / 8 , slice - > data_size ) ;
memset ( slice - > data + slice - > data_size , 0 ,
memset ( slice - > data + slice - > data_size , 0 ,
AV_INPUT_BUFFER_PADDING_SIZE ) ;
AV_INPUT_BUFFER_PADDING_SIZE ) ;
slice - > data_bit_start = pos % 8 ;
slice - > data_bit_start = pos % 8 ;
unit - > content = slice ;
}
}
break ;
break ;
case H264_NAL_AUD :
case H264_NAL_AUD :
{
{
H264RawAUD * aud ;
err = ff_cbs_alloc_unit_content ( ctx , unit ,
sizeof ( H264RawAUD ) , NULL ) ;
aud = av_mallocz ( sizeof ( * aud ) ) ;
if ( err < 0 )
if ( ! aud )
return AVERROR ( ENOMEM ) ;
err = cbs_h264_read_aud ( ctx , & bc , aud ) ;
if ( err < 0 ) {
av_free ( aud ) ;
return err ;
return err ;
}
unit - > content = aud ;
err = cbs_h264_read_aud ( ctx , & bc , unit - > content ) ;
if ( err < 0 )
return err ;
}
}
break ;
break ;
case H264_NAL_SEI :
case H264_NAL_SEI :
{
{
H264RawSEI * sei ;
err = ff_cbs_alloc_unit_content ( ctx , unit , sizeof ( H264RawSEI ) ,
& cbs_h264_free_sei ) ;
sei = av_mallocz ( sizeof ( * sei ) ) ;
if ( err < 0 )
if ( ! sei )
return AVERROR ( ENOMEM ) ;
err = cbs_h264_read_sei ( ctx , & bc , sei ) ;
if ( err < 0 ) {
cbs_h264_free_sei ( sei ) ;
av_free ( sei ) ;
return err ;
return err ;
}
unit - > content = sei ;
err = cbs_h264_read_sei ( ctx , & bc , unit - > content ) ;
if ( err < 0 )
return err ;
}
}
break ;
break ;
@ -856,36 +829,38 @@ static int cbs_h265_read_nal_unit(CodedBitstreamContext *ctx,
{
{
H265RawVPS * vps ;
H265RawVPS * vps ;
vps = av_mallocz ( sizeof ( * vps ) ) ;
err = ff_cbs_alloc_unit_content ( ctx , unit , sizeof ( * vps ) ,
if ( ! vps )
& cbs_h265_free_vps ) ;
return AVERROR ( ENOMEM ) ;
if ( err < 0 )
return err ;
vps = unit - > content ;
err = cbs_h265_read_vps ( ctx , & bc , vps ) ;
err = cbs_h265_read_vps ( ctx , & bc , vps ) ;
if ( err > = 0 )
if ( err < 0 )
err = cbs_h265_replace_vps ( ctx , vps ) ;
if ( err < 0 ) {
av_free ( vps ) ;
return err ;
return err ;
}
unit - > content = vps ;
err = cbs_h265_replace_vps ( ctx , vps ) ;
if ( err < 0 )
return err ;
}
}
break ;
break ;
case HEVC_NAL_SPS :
case HEVC_NAL_SPS :
{
{
H265RawSPS * sps ;
H265RawSPS * sps ;
sps = av_mallocz ( sizeof ( * sps ) ) ;
err = ff_cbs_alloc_unit_content ( ctx , unit , sizeof ( * sps ) ,
if ( ! sps )
& cbs_h265_free_sps ) ;
return AVERROR ( ENOMEM ) ;
if ( err < 0 )
return err ;
sps = unit - > content ;
err = cbs_h265_read_sps ( ctx , & bc , sps ) ;
err = cbs_h265_read_sps ( ctx , & bc , sps ) ;
if ( err > = 0 )
if ( err < 0 )
err = cbs_h265_replace_sps ( ctx , sps ) ;
if ( err < 0 ) {
av_free ( sps ) ;
return err ;
return err ;
}
unit - > content = sps ;
err = cbs_h265_replace_sps ( ctx , sps ) ;
if ( err < 0 )
return err ;
}
}
break ;
break ;
@ -893,18 +868,19 @@ static int cbs_h265_read_nal_unit(CodedBitstreamContext *ctx,
{
{
H265RawPPS * pps ;
H265RawPPS * pps ;
pps = av_mallocz ( sizeof ( * pps ) ) ;
err = ff_cbs_alloc_unit_content ( ctx , unit , sizeof ( * pps ) ,
if ( ! pps )
& cbs_h265_free_pps ) ;
return AVERROR ( ENOMEM ) ;
if ( err < 0 )
return err ;
pps = unit - > content ;
err = cbs_h265_read_pps ( ctx , & bc , pps ) ;
err = cbs_h265_read_pps ( ctx , & bc , pps ) ;
if ( err > = 0 )
if ( err < 0 )
err = cbs_h265_replace_pps ( ctx , pps ) ;
if ( err < 0 ) {
av_free ( pps ) ;
return err ;
return err ;
}
unit - > content = pps ;
err = cbs_h265_replace_pps ( ctx , pps ) ;
if ( err < 0 )
return err ;
}
}
break ;
break ;
@ -928,14 +904,15 @@ static int cbs_h265_read_nal_unit(CodedBitstreamContext *ctx,
H265RawSlice * slice ;
H265RawSlice * slice ;
int pos , len ;
int pos , len ;
slice = av_mallocz ( sizeof ( * slice ) ) ;
err = ff_cbs_alloc_unit_content ( ctx , unit , sizeof ( * slice ) ,
if ( ! slice )
& cbs_h265_free_slice ) ;
return AVERROR ( ENOMEM ) ;
if ( err < 0 )
return err ;
slice = unit - > content ;
err = cbs_h265_read_slice_segment_header ( ctx , & bc , & slice - > header ) ;
err = cbs_h265_read_slice_segment_header ( ctx , & bc , & slice - > header ) ;
if ( err < 0 ) {
if ( err < 0 )
av_free ( slice ) ;
return err ;
return err ;
}
pos = bitstream_tell ( & bc ) ;
pos = bitstream_tell ( & bc ) ;
len = unit - > data_size ;
len = unit - > data_size ;
@ -948,36 +925,29 @@ static int cbs_h265_read_nal_unit(CodedBitstreamContext *ctx,
}
}
slice - > data_size = len - pos / 8 ;
slice - > data_size = len - pos / 8 ;
slice - > data = av_malloc ( slice - > data_size +
slice - > data_ref = av_buffer_alloc ( slice - > data_size +
AV_INPUT_BUFFER_PADDING_SIZE ) ;
AV_INPUT_BUFFER_PADDING_SIZE ) ;
if ( ! slice - > data ) {
if ( ! slice - > data_ref )
av_free ( slice ) ;
return AVERROR ( ENOMEM ) ;
return AVERROR ( ENOMEM ) ;
}
slice - > data = slice - > data_ref - > data ;
memcpy ( slice - > data ,
memcpy ( slice - > data ,
unit - > data + pos / 8 , slice - > data_size ) ;
unit - > data + pos / 8 , slice - > data_size ) ;
memset ( slice - > data + slice - > data_size , 0 ,
memset ( slice - > data + slice - > data_size , 0 ,
AV_INPUT_BUFFER_PADDING_SIZE ) ;
AV_INPUT_BUFFER_PADDING_SIZE ) ;
slice - > data_bit_start = pos % 8 ;
slice - > data_bit_start = pos % 8 ;
unit - > content = slice ;
}
}
break ;
break ;
case HEVC_NAL_AUD :
case HEVC_NAL_AUD :
{
{
H265RawAUD * aud ;
err = ff_cbs_alloc_unit_content ( ctx , unit ,
sizeof ( H265RawAUD ) , NULL ) ;
aud = av_mallocz ( sizeof ( * aud ) ) ;
if ( err < 0 )
if ( ! aud )
return AVERROR ( ENOMEM ) ;
err = cbs_h265_read_aud ( ctx , & bc , aud ) ;
if ( err < 0 ) {
av_free ( aud ) ;
return err ;
return err ;
}
unit - > content = aud ;
err = cbs_h265_read_aud ( ctx , & bc , unit - > content ) ;
if ( err < 0 )
return err ;
}
}
break ;
break ;
@ -1272,7 +1242,7 @@ static int cbs_h2645_write_nal_unit(CodedBitstreamContext *ctx,
unit - > data_size = ( put_bits_count ( & pbc ) + 7 ) / 8 ;
unit - > data_size = ( put_bits_count ( & pbc ) + 7 ) / 8 ;
flush_put_bits ( & pbc ) ;
flush_put_bits ( & pbc ) ;
err = av_reallocp ( & unit - > data , unit - > data_size ) ;
err = ff_cbs_alloc_unit_data ( ctx , unit , unit - > data_size ) ;
if ( err < 0 )
if ( err < 0 )
return err ;
return err ;
@ -1354,6 +1324,12 @@ static int cbs_h2645_assemble_fragment(CodedBitstreamContext *ctx,
if ( err )
if ( err )
return err ;
return err ;
frag - > data_ref = av_buffer_create ( data , dp , NULL , NULL , 0 ) ;
if ( ! frag - > data_ref ) {
av_freep ( & data ) ;
return AVERROR ( ENOMEM ) ;
}
frag - > data = data ;
frag - > data = data ;
frag - > data_size = dp ;
frag - > data_size = dp ;
@ -1402,7 +1378,6 @@ const CodedBitstreamType ff_cbs_type_h264 = {
. write_unit = & cbs_h2645_write_nal_unit ,
. write_unit = & cbs_h2645_write_nal_unit ,
. assemble_fragment = & cbs_h2645_assemble_fragment ,
. assemble_fragment = & cbs_h2645_assemble_fragment ,
. free_unit = & cbs_h264_free_nal_unit ,
. close = & cbs_h264_close ,
. close = & cbs_h264_close ,
} ;
} ;
@ -1416,6 +1391,5 @@ const CodedBitstreamType ff_cbs_type_h265 = {
. write_unit = & cbs_h2645_write_nal_unit ,
. write_unit = & cbs_h2645_write_nal_unit ,
. assemble_fragment = & cbs_h2645_assemble_fragment ,
. assemble_fragment = & cbs_h2645_assemble_fragment ,
. free_unit = & cbs_h265_free_nal_unit ,
. close = & cbs_h265_close ,
. close = & cbs_h265_close ,
} ;
} ;