| | 1725 | } |
|---|
| | 1726 | |
|---|
| | 1727 | static block_t *Add_ADTS( block_t *p_data, es_format_t *p_fmt ) |
|---|
| | 1728 | { |
|---|
| | 1729 | uint8_t *p_extra = p_fmt->p_extra; |
|---|
| | 1730 | |
|---|
| | 1731 | if( !p_data || p_fmt->i_extra < 2 || !p_extra ) |
|---|
| | 1732 | return p_data; /* no data to construct the headers */ |
|---|
| | 1733 | |
|---|
| | 1734 | int i_index = ( (p_extra[0] << 1) | (p_extra[1] >> 7) ) & 0x0f; |
|---|
| | 1735 | int i_profile = (p_extra[0] >> 3) - 1; /* i_profile < 4 */ |
|---|
| | 1736 | |
|---|
| | 1737 | if( i_index == 0x0f && p_fmt->i_extra < 5 ) |
|---|
| | 1738 | return p_data; /* not enough data */ |
|---|
| | 1739 | |
|---|
| | 1740 | int i_channels = (p_extra[i_index == 0x0f ? 4 : 1] >> 3) & 0x0f; |
|---|
| | 1741 | |
|---|
| | 1742 | #define ADTS_HEADER_SIZE 7 /* CRC needs 2 more bytes */ |
|---|
| | 1743 | |
|---|
| | 1744 | |
|---|
| | 1745 | /* keep a copy in case block_Realloc() fails */ |
|---|
| | 1746 | block_t *p_bak_block = block_Duplicate( p_data ); |
|---|
| | 1747 | if( !p_bak_block ) /* OOM, block_Realloc() is likely to lose our block */ |
|---|
| | 1748 | return p_data; /* the frame isn't correct but that's the best we have */ |
|---|
| | 1749 | |
|---|
| | 1750 | block_t *p_new_block = block_Realloc( p_data, ADTS_HEADER_SIZE, |
|---|
| | 1751 | p_data->i_buffer ); |
|---|
| | 1752 | if( !p_new_block ) |
|---|
| | 1753 | return p_bak_block; /* OOM, send the (incorrect) original frame */ |
|---|
| | 1754 | |
|---|
| | 1755 | block_Release( p_bak_block ); /* we don't need the copy anymore */ |
|---|
| | 1756 | |
|---|
| | 1757 | |
|---|
| | 1758 | uint8_t *p_buffer = p_new_block->p_buffer; |
|---|
| | 1759 | |
|---|
| | 1760 | /* fixed header */ |
|---|
| | 1761 | p_buffer[0] = 0xff; |
|---|
| | 1762 | p_buffer[1] = 0xf1; /* 0xf0 | 0x00 | 0x00 | 0x01 */ |
|---|
| | 1763 | p_buffer[2] = (i_profile << 6) | ((i_index & 0x0f) << 2) | ((i_channels >> 2) & 0x01) ; |
|---|
| | 1764 | p_buffer[3] = (i_channels << 6) | ((p_data->i_buffer >> 11) & 0x03); |
|---|
| | 1765 | |
|---|
| | 1766 | /* variable header (starts at last 2 bits of 4th byte) */ |
|---|
| | 1767 | |
|---|
| | 1768 | int i_fullness = 0x7ff; /* 0x7ff means VBR */ |
|---|
| | 1769 | /* XXX: We should check if it's CBR or VBR, but no known implementation |
|---|
| | 1770 | * do that, and it's a pain to calculate this field */ |
|---|
| | 1771 | |
|---|
| | 1772 | p_buffer[4] = p_data->i_buffer >> 3; |
|---|
| | 1773 | p_buffer[5] = ((p_data->i_buffer & 0x07) << 5) | ((i_fullness >> 6) & 0x1f); |
|---|
| | 1774 | p_buffer[6] = ((i_fullness & 0x3f) << 2) /* | 0xfc */; |
|---|
| | 1775 | |
|---|
| | 1776 | return p_new_block; |
|---|