2121
2222#include "avcodec.h"
2323#include "aacadtsdec.h"
24+ #include "bsf.h"
2425#include "put_bits.h"
2526#include "get_bits.h"
2627#include "mpeg4audio.h"
@@ -34,68 +35,76 @@ typedef struct AACBSFContext {
3435 * This filter creates an MPEG-4 AudioSpecificConfig from an MPEG-2/4
3536 * ADTS header and removes the ADTS header.
3637 */
37- static int aac_adtstoasc_filter (AVBitStreamFilterContext * bsfc ,
38- AVCodecContext * avctx , const char * args ,
39- uint8_t * * poutbuf , int * poutbuf_size ,
40- const uint8_t * buf , int buf_size ,
41- int keyframe )
38+ static int aac_adtstoasc_filter (AVBSFContext * bsfc , AVPacket * out )
4239{
40+ AACBSFContext * ctx = bsfc -> priv_data ;
41+
4342 GetBitContext gb ;
4443 PutBitContext pb ;
4544 AACADTSHeaderInfo hdr ;
45+ AVPacket * in ;
46+ int ret ;
4647
47- AACBSFContext * ctx = bsfc -> priv_data ;
48+ ret = ff_bsf_get_packet (bsfc , & in );
49+ if (ret < 0 )
50+ return ret ;
4851
49- init_get_bits (& gb , buf , AAC_ADTS_HEADER_SIZE * 8 );
52+ if (in -> size < AAC_ADTS_HEADER_SIZE )
53+ goto packet_too_small ;
5054
51- * poutbuf = (uint8_t * ) buf ;
52- * poutbuf_size = buf_size ;
55+ init_get_bits (& gb , in -> data , AAC_ADTS_HEADER_SIZE * 8 );
5356
54- if (avctx -> extradata )
55- if (show_bits (& gb , 12 ) != 0xfff )
56- return 0 ;
57+ if (bsfc -> par_in -> extradata && show_bits (& gb , 12 ) != 0xfff )
58+ goto finish ;
5759
5860 if (avpriv_aac_parse_header (& gb , & hdr ) < 0 ) {
59- av_log (avctx , AV_LOG_ERROR , "Error parsing ADTS frame header!\n" );
60- return AVERROR_INVALIDDATA ;
61+ av_log (bsfc , AV_LOG_ERROR , "Error parsing ADTS frame header!\n" );
62+ ret = AVERROR_INVALIDDATA ;
63+ goto fail ;
6164 }
6265
6366 if (!hdr .crc_absent && hdr .num_aac_frames > 1 ) {
64- avpriv_report_missing_feature (avctx ,
67+ avpriv_report_missing_feature (bsfc ,
6568 "Multiple RDBs per frame with CRC" );
66- return AVERROR_PATCHWELCOME ;
69+ ret = AVERROR_PATCHWELCOME ;
70+ goto fail ;
6771 }
6872
69- buf += AAC_ADTS_HEADER_SIZE + 2 * !hdr .crc_absent ;
70- buf_size -= AAC_ADTS_HEADER_SIZE + 2 * !hdr .crc_absent ;
73+ in -> size -= AAC_ADTS_HEADER_SIZE + 2 * !hdr .crc_absent ;
74+ if (in -> size <= 0 )
75+ goto packet_too_small ;
76+ in -> data += AAC_ADTS_HEADER_SIZE + 2 * !hdr .crc_absent ;
7177
7278 if (!ctx -> first_frame_done ) {
7379 int pce_size = 0 ;
7480 uint8_t pce_data [MAX_PCE_SIZE ];
81+ uint8_t * extradata ;
82+
7583 if (!hdr .chan_config ) {
76- init_get_bits (& gb , buf , buf_size * 8 );
84+ init_get_bits (& gb , in -> data , in -> size * 8 );
7785 if (get_bits (& gb , 3 ) != 5 ) {
78- avpriv_report_missing_feature (avctx ,
86+ avpriv_report_missing_feature (bsfc ,
7987 "PCE-based channel configuration "
8088 "without PCE as first syntax "
8189 "element" );
82- return AVERROR_PATCHWELCOME ;
90+ ret = AVERROR_PATCHWELCOME ;
91+ goto fail ;
8392 }
8493 init_put_bits (& pb , pce_data , MAX_PCE_SIZE );
8594 pce_size = avpriv_copy_pce_data (& pb , & gb )/8 ;
8695 flush_put_bits (& pb );
87- buf_size -= get_bits_count (& gb )/8 ;
88- buf += get_bits_count (& gb )/8 ;
96+ in -> size -= get_bits_count (& gb )/8 ;
97+ in -> data += get_bits_count (& gb )/8 ;
8998 }
90- av_free ( avctx -> extradata );
91- avctx -> extradata_size = 2 + pce_size ;
92- avctx -> extradata = av_mallocz ( avctx -> extradata_size + AV_INPUT_BUFFER_PADDING_SIZE );
93- if (!avctx -> extradata ) {
94- avctx -> extradata_size = 0 ;
95- return AVERROR ( ENOMEM ) ;
99+
100+ extradata = av_packet_new_side_data ( in , AV_PKT_DATA_NEW_EXTRADATA ,
101+ 2 + pce_size );
102+ if (!extradata ) {
103+ ret = AVERROR ( ENOMEM ) ;
104+ goto fail ;
96105 }
97106
98- init_put_bits (& pb , avctx -> extradata , avctx -> extradata_size );
107+ init_put_bits (& pb , extradata , 2 + pce_size );
99108 put_bits (& pb , 5 , hdr .object_type );
100109 put_bits (& pb , 4 , hdr .sampling_index );
101110 put_bits (& pb , 4 , hdr .chan_config );
@@ -104,20 +113,42 @@ static int aac_adtstoasc_filter(AVBitStreamFilterContext *bsfc,
104113 put_bits (& pb , 1 , 0 ); //is not extension
105114 flush_put_bits (& pb );
106115 if (pce_size ) {
107- memcpy (avctx -> extradata + 2 , pce_data , pce_size );
116+ memcpy (extradata + 2 , pce_data , pce_size );
108117 }
109118
110119 ctx -> first_frame_done = 1 ;
111120 }
112121
113- * poutbuf = (uint8_t * ) buf ;
114- * poutbuf_size = buf_size ;
122+ finish :
123+ av_packet_move_ref (out , in );
124+ av_packet_free (& in );
115125
116126 return 0 ;
127+
128+ packet_too_small :
129+ av_log (bsfc , AV_LOG_ERROR , "Input packet too small\n" );
130+ ret = AVERROR_INVALIDDATA ;
131+ fail :
132+ av_packet_free (& in );
133+ return ret ;
117134}
118135
119- AVBitStreamFilter ff_aac_adtstoasc_bsf = {
136+ static int aac_adtstoasc_init (AVBSFContext * ctx )
137+ {
138+ av_freep (& ctx -> par_out -> extradata );
139+ ctx -> par_out -> extradata_size = 0 ;
140+
141+ return 0 ;
142+ }
143+
144+ static const enum AVCodecID codec_ids [] = {
145+ AV_CODEC_ID_AAC , AV_CODEC_ID_NONE ,
146+ };
147+
148+ const AVBitStreamFilter ff_aac_adtstoasc_bsf = {
120149 .name = "aac_adtstoasc" ,
121150 .priv_data_size = sizeof (AACBSFContext ),
151+ .init = aac_adtstoasc_init ,
122152 .filter = aac_adtstoasc_filter ,
153+ .codec_ids = codec_ids ,
123154};
0 commit comments