1 /*****************************************************************************
2 * ac3_iec958.c: ac3 to spdif converter
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: ac3_iec958.c,v 1.5 2001/12/30 07:09:54 sam Exp $
7 * Authors: Stéphane Borel <stef@via.ecp.fr>
8 * Juha Yrjola <jyrjola@cc.hut.fi>
9 * German Gomez Garcia <german@piraos.com>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
24 ****************************************************************************/
26 /****************************************************************************
28 ****************************************************************************/
31 #include <string.h> /* memset() */
34 #include <videolan/vlc.h>
40 #include "audio_output.h"
42 #include "stream_control.h"
43 #include "input_ext-dec.h"
45 #include "ac3_spdif.h"
46 #include "ac3_iec958.h"
48 /****************************************************************************
49 * Local structures and tables
50 ****************************************************************************/
51 typedef struct frame_size_s
57 static const frame_size_t p_frame_size_code[64] =
59 { 32 ,{64 ,69 ,96 } },
60 { 32 ,{64 ,70 ,96 } },
61 { 40 ,{80 ,87 ,120 } },
62 { 40 ,{80 ,88 ,120 } },
63 { 48 ,{96 ,104 ,144 } },
64 { 48 ,{96 ,105 ,144 } },
65 { 56 ,{112 ,121 ,168 } },
66 { 56 ,{112 ,122 ,168 } },
67 { 64 ,{128 ,139 ,192 } },
68 { 64 ,{128 ,140 ,192 } },
69 { 80 ,{160 ,174 ,240 } },
70 { 80 ,{160 ,175 ,240 } },
71 { 96 ,{192 ,208 ,288 } },
72 { 96 ,{192 ,209 ,288 } },
73 { 112 ,{224 ,243 ,336 } },
74 { 112 ,{224 ,244 ,336 } },
75 { 128 ,{256 ,278 ,384 } },
76 { 128 ,{256 ,279 ,384 } },
77 { 160 ,{320 ,348 ,480 } },
78 { 160 ,{320 ,349 ,480 } },
79 { 192 ,{384 ,417 ,576 } },
80 { 192 ,{384 ,418 ,576 } },
81 { 224 ,{448 ,487 ,672 } },
82 { 224 ,{448 ,488 ,672 } },
83 { 256 ,{512 ,557 ,768 } },
84 { 256 ,{512 ,558 ,768 } },
85 { 320 ,{640 ,696 ,960 } },
86 { 320 ,{640 ,697 ,960 } },
87 { 384 ,{768 ,835 ,1152 } },
88 { 384 ,{768 ,836 ,1152 } },
89 { 448 ,{896 ,975 ,1344 } },
90 { 448 ,{896 ,976 ,1344 } },
91 { 512 ,{1024 ,1114 ,1536 } },
92 { 512 ,{1024 ,1115 ,1536 } },
93 { 576 ,{1152 ,1253 ,1728 } },
94 { 576 ,{1152 ,1254 ,1728 } },
95 { 640 ,{1280 ,1393 ,1920 } },
96 { 640 ,{1280 ,1394 ,1920 } }
99 /****************************************************************************
100 * ac3_iec958_build_burst: builds an iec958/spdif frame based on an ac3 frame
101 ****************************************************************************/
102 void ac3_iec958_build_burst( ac3_spdif_thread_t *p_spdif )
104 const u8 p_sync[6] = { 0x72, 0xF8, 0x1F, 0x4E, 0x01, 0x00 };
105 int i_length = p_spdif->ac3_info.i_frame_size;
107 /* Skip the first byte if i_length is odd */
108 u16 * p_in = (u16 *)( p_spdif->p_ac3 + ( i_length & 0x1 ) );
109 u16 * p_out = (u16 *)p_spdif->p_iec;
112 /* Add the spdif headers */
113 memcpy( p_spdif->p_iec, p_sync, 6 );
114 p_spdif->p_iec[6] = ( i_length * 8 ) & 0xFF;
115 p_spdif->p_iec[7] = ( ( i_length * 8 ) >> 8 ) & 0xFF;
118 swab( p_spdif->p_ac3, p_spdif->p_iec + 8, i_length );
120 /* i_length should be even */
125 *p_out = ( (*p_in & 0x00ff) << 16 ) | ( (*p_in & 0xff00) >> 16 );
132 /* Add zeroes to complete the spdif frame,
133 * they will be ignored by the decoder */
134 memset( p_spdif->p_iec + 8 + i_length, 0, SPDIF_FRAME_SIZE - 8 - i_length );
137 /****************************************************************************
138 * ac3_iec958_parse_syncinfo: parse ac3 sync info
139 ****************************************************************************/
140 int ac3_iec958_parse_syncinfo( ac3_spdif_thread_t *p_spdif )
142 int p_sample_rates[4] = { 48000, 44100, 32000, -1 };
143 int i_frame_rate_code;
144 int i_frame_size_code;
145 sync_frame_t * p_sync_frame;
147 /* Read sync frame */
148 GetChunk( &p_spdif->bit_stream, p_spdif->p_ac3 + 2,
149 sizeof(sync_frame_t) - 2 );
150 p_sync_frame = (sync_frame_t*)p_spdif->p_ac3;
152 /* Compute frame rate */
153 i_frame_rate_code = (p_sync_frame->syncinfo.code >> 6) & 0x03;
154 p_spdif->ac3_info.i_sample_rate = p_sample_rates[i_frame_rate_code];
155 if( p_spdif->ac3_info.i_sample_rate == -1 )
160 /* Compute frame size */
161 i_frame_size_code = p_sync_frame->syncinfo.code & 0x3f;
162 p_spdif->ac3_info.i_frame_size = 2 *
163 p_frame_size_code[i_frame_size_code].i_frame_size[i_frame_rate_code];
164 p_spdif->ac3_info.i_bit_rate =
165 p_frame_size_code[i_frame_size_code].i_bit_rate;
167 if( ( ( p_sync_frame->bsi.bsidmod >> 3 ) & 0x1f ) != 0x08 )
172 p_spdif->ac3_info.i_bs_mod = p_sync_frame->bsi.bsidmod & 0x7;