diff --git a/modules/packetizer/flac.c b/modules/packetizer/flac.c index 1683a0ed89fbb43bd0ebf62c6e2753889169a8e8..7c7bc06b84fe36ac3783ed6b4690c7791f9af195 100644 --- a/modules/packetizer/flac.c +++ b/modules/packetizer/flac.c @@ -364,15 +364,18 @@ static block_t *Packetize(decoder_t *p_dec, block_t **pp_block) /* fallthrough */ case STATE_HEADER: + { /* Get FLAC frame header (MAX_FLAC_HEADER_SIZE bytes) */ if (block_PeekBytes(&p_sys->bytestream, p_header, FLAC_HEADER_SIZE_MAX)) return NULL; /* Need more data */ /* Check if frame is valid and get frame info */ - int i_ret = FLAC_ParseSyncInfo(p_header, FLAC_HEADER_SIZE_MAX, - p_sys->b_stream_info ? &p_sys->stream_info : NULL, - flac_crc8, &p_sys->headerinfo); - if (!i_ret) { + const struct flac_stream_info *streaminfo = + p_sys->b_stream_info ? &p_sys->stream_info : NULL; + struct flac_header_info headerinfo; + int i_ret = FLAC_ParseSyncInfo(p_header, FLAC_HEADER_SIZE_MAX, streaminfo, + flac_crc8, &headerinfo); + if (!i_ret || !FLAC_CheckFrameInfo(streaminfo, &headerinfo)) { msg_Dbg(p_dec, "emulated sync word"); block_SkipByte(&p_sys->bytestream); p_sys->i_offset = 0; @@ -380,6 +383,7 @@ static block_t *Packetize(decoder_t *p_dec, block_t **pp_block) break; } + p_sys->headerinfo = headerinfo; p_sys->i_state = STATE_NEXT_SYNC; p_sys->i_offset = FLAC_FRAME_SIZE_MIN; p_sys->crc = 0; @@ -389,6 +393,7 @@ static block_t *Packetize(decoder_t *p_dec, block_t **pp_block) * The confusing part below is that sync code needs to be verified in case * it would appear in data, so we also need to check next frame header CRC */ + } /* fallthrough */ case STATE_NEXT_SYNC: @@ -418,11 +423,12 @@ static block_t *Packetize(decoder_t *p_dec, block_t **pp_block) nextheader, FLAC_HEADER_SIZE_MAX)) return NULL; /* Need more data */ + const struct flac_stream_info *streaminfo = + p_sys->b_stream_info ? &p_sys->stream_info : NULL; struct flac_header_info dummy; /* Check if frame is valid and get frame info */ - if(FLAC_ParseSyncInfo(nextheader, FLAC_HEADER_SIZE_MAX, - p_sys->b_stream_info ? &p_sys->stream_info : NULL, - NULL, &dummy) == 0) + if(!FLAC_ParseSyncInfo(nextheader, FLAC_HEADER_SIZE_MAX, streaminfo, NULL, &dummy)|| + !FLAC_CheckFrameInfo(streaminfo, &dummy)) { /* Keep trying to find next sync point in bytestream */ p_sys->i_offset++; diff --git a/modules/packetizer/flac.h b/modules/packetizer/flac.h index 8070a5f476ac6876d3c4fdacfef4b486b1c7607f..f04d7fae9a4b47dd271f0b7e6d0a583879255032 100644 --- a/modules/packetizer/flac.h +++ b/modules/packetizer/flac.h @@ -115,6 +115,23 @@ static inline int64_t read_utf8(const uint8_t *p_buf, unsigned i_buf, int *pi_re return i_result; } +static inline int FLAC_CheckFrameInfo(const struct flac_stream_info *stream_info, + const struct flac_header_info *h) +{ + /* Sanity check using stream info header when possible */ + if (stream_info) + { + if (h->i_frame_length < stream_info->min_blocksize || + h->i_frame_length > stream_info->max_blocksize) + return 0; + if (h->i_bits_per_sample != stream_info->bits_per_sample) + return 0; + if (h->i_rate != stream_info->sample_rate) + return 0; + } + return 1; +} + /***************************************************************************** * FLAC_ParseSyncInfo: parse FLAC sync info * - stream_info can be NULL @@ -268,17 +285,6 @@ static inline int FLAC_ParseSyncInfo(const uint8_t *p_buf, unsigned i_buf, pf_crc8(p_buf, i_header) != p_buf[i_header]) return 0; - /* Sanity check using stream info header when possible */ - if (stream_info) { - if (blocksize < stream_info->min_blocksize || - blocksize > stream_info->max_blocksize) - return 0; - if ((unsigned)bits_per_sample != stream_info->bits_per_sample) - return 0; - if (samplerate != stream_info->sample_rate) - return 0; - } - /* Compute from frame absolute time */ if ( (p_buf[1] & 0x01) == 0 ) /* Fixed blocksize stream / Frames */ {