+/*****************************************************************************
+ *
+ *****************************************************************************/
+static block_t *StreamParseAsf( demux_t *p_demux, live_track_t *tk,
+ bool b_marker,
+ const uint8_t *p_data, unsigned i_size )
+{
+ const unsigned i_packet_size = p_demux->p_sys->asfh.i_min_data_packet_size;
+ block_t *p_list = NULL;
+
+ while( i_size >= 4 )
+ {
+ unsigned i_flags = p_data[0];
+ unsigned i_length_offset = (p_data[1] << 16) |
+ (p_data[2] << 8) |
+ (p_data[3] );
+ bool b_key = i_flags & 0x80;
+ bool b_length = i_flags & 0x40;
+ bool b_relative_ts = i_flags & 0x20;
+ bool b_duration = i_flags & 0x10;
+ bool b_location_id = i_flags & 0x08;
+
+ //msg_Dbg( p_demux, "ASF: marker=%d size=%d : %c=%d id=%d",
+ // b_marker, i_size, b_length ? 'L' : 'O', i_length_offset );
+ unsigned i_header_size = 4;
+ if( b_relative_ts )
+ i_header_size += 4;
+ if( b_duration )
+ i_header_size += 4;
+ if( b_location_id )
+ i_header_size += 4;
+
+ if( i_header_size > i_size )
+ {
+ msg_Warn( p_demux, "Invalid header size" );
+ break;
+ }
+
+ /* XXX
+ * When b_length is true, the streams I found do not seems to respect
+ * the documentation.
+ * From them, I have failed to find which choice between '__MIN()' or
+ * 'i_length_offset - i_header_size' is the right one.
+ */
+ unsigned i_payload;
+ if( b_length )
+ i_payload = __MIN( i_length_offset, i_size - i_header_size);
+ else
+ i_payload = i_size - i_header_size;
+
+ if( !tk->p_asf_block )
+ {
+ tk->p_asf_block = block_New( p_demux, i_packet_size );
+ if( !tk->p_asf_block )
+ break;
+ tk->p_asf_block->i_buffer = 0;
+ }
+ unsigned i_offset = b_length ? 0 : i_length_offset;
+ if( i_offset == tk->p_asf_block->i_buffer && i_offset + i_payload <= i_packet_size )
+ {
+ memcpy( &tk->p_asf_block->p_buffer[i_offset], &p_data[i_header_size], i_payload );
+ tk->p_asf_block->i_buffer += i_payload;
+ if( b_marker )
+ {
+ /* We have a complete packet */
+ tk->p_asf_block->i_buffer = i_packet_size;
+ block_ChainAppend( &p_list, tk->p_asf_block );
+ tk->p_asf_block = NULL;
+ }
+ }
+ else
+ {
+ /* Reset on broken stream */
+ msg_Err( p_demux, "Broken packet detected (%d vs %d or %d + %d vs %d)",
+ i_offset, tk->p_asf_block->i_buffer, i_offset, i_payload, i_packet_size);
+ tk->p_asf_block->i_buffer = 0;
+ }
+
+ /* */
+ p_data += i_header_size + i_payload;
+ i_size -= i_header_size + i_payload;
+ }
+ return p_list;
+}
+