+// Sipr packet re-ordering code and index table borrowed from
+// the MPlayer Realmedia demuxer.
+static const uint8_t sipr_swap_index_table[38][2] = {
+ { 0, 63 }, { 1, 22 }, { 2, 44 }, { 3, 90 },
+ { 5, 81 }, { 7, 31 }, { 8, 86 }, { 9, 58 },
+ { 10, 36 }, { 12, 68 }, { 13, 39 }, { 14, 73 },
+ { 15, 53 }, { 16, 69 }, { 17, 57 }, { 19, 88 },
+ { 20, 34 }, { 21, 71 }, { 24, 46 }, { 25, 94 },
+ { 26, 54 }, { 28, 75 }, { 29, 50 }, { 32, 70 },
+ { 33, 92 }, { 35, 74 }, { 38, 85 }, { 40, 56 },
+ { 42, 87 }, { 43, 65 }, { 45, 59 }, { 48, 79 },
+ { 49, 93 }, { 51, 89 }, { 55, 95 }, { 61, 76 },
+ { 67, 83 }, { 77, 80 }
+};
+
+static void SiprPacketReorder(uint8_t *buf, int sub_packet_h, int framesize)
+{
+ int n, bs = sub_packet_h * framesize * 2 / 96; // nibbles per subpacket
+
+ for (n = 0; n < 38; n++) {
+ int j;
+ int i = bs * sipr_swap_index_table[n][0];
+ int o = bs * sipr_swap_index_table[n][1];
+
+ /* swap 4 bit-nibbles of block 'i' with 'o' */
+ for (j = 0; j < bs; j++, i++, o++) {
+ int x = (buf[i >> 1] >> (4 * (i & 1))) & 0xF,
+ y = (buf[o >> 1] >> (4 * (o & 1))) & 0xF;
+
+ buf[o >> 1] = (x << (4 * (o & 1))) |
+ (buf[o >> 1] & (0xF << (4 * !(o & 1))));
+ buf[i >> 1] = (y << (4 * (i & 1))) |
+ (buf[i >> 1] & (0xF << (4 * !(i & 1))));
+ }
+ }
+}
+
+static void DemuxAudioSipr( demux_t *p_demux, real_track_t *tk, mtime_t i_pts )
+{
+ demux_sys_t *p_sys = p_demux->p_sys;
+ block_t *p_block = tk->p_sipr_packet;
+
+ if( p_sys->i_buffer < tk->i_frame_size )
+ return;
+
+ if( !p_block )
+ {
+ p_block = block_New( p_demux, tk->i_frame_size * tk->i_subpacket_h );
+ if( !p_block )
+ return;
+ tk->p_sipr_packet = p_block;
+ }
+
+ memcpy( p_block->p_buffer + tk->i_sipr_subpacket_count * tk->i_frame_size,
+ p_sys->buffer, tk->i_frame_size );
+ if (!tk->i_sipr_subpacket_count)
+ {
+ p_block->i_dts =
+ p_block->i_pts = i_pts;
+ }
+
+ if( ++tk->i_sipr_subpacket_count < tk->i_subpacket_h )
+ return;
+
+ SiprPacketReorder(p_block->p_buffer, tk->i_subpacket_h, tk->i_frame_size);
+ CheckPcr( p_demux, tk, p_block->i_pts );
+ es_out_Send( p_demux->out, tk->p_es, p_block );
+ tk->i_sipr_subpacket_count = 0;
+ tk->p_sipr_packet = NULL;
+}
+