1 /*****************************************************************************
2 * ac3_iec958.c: ac3 to spdif converter
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: ac3_iec958.c,v 1.1 2001/11/13 12:09:17 henri 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 ****************************************************************************/
33 #include <string.h> /* memset() */
44 #include "intf_msg.h" /* intf_DbgMsg(), intf_ErrMsg() */
46 #include "audio_output.h"
48 #include "modules_export.h"
50 #include "stream_control.h"
51 #include "input_ext-dec.h"
54 #include "ac3_spdif.h"
55 #include "ac3_iec958.h"
57 /****************************************************************************
58 * Local structures and tables
59 ****************************************************************************/
60 typedef struct frame_size_s
66 static const frame_size_t p_frame_size_code[64] =
68 { 32 ,{64 ,69 ,96 } },
69 { 32 ,{64 ,70 ,96 } },
70 { 40 ,{80 ,87 ,120 } },
71 { 40 ,{80 ,88 ,120 } },
72 { 48 ,{96 ,104 ,144 } },
73 { 48 ,{96 ,105 ,144 } },
74 { 56 ,{112 ,121 ,168 } },
75 { 56 ,{112 ,122 ,168 } },
76 { 64 ,{128 ,139 ,192 } },
77 { 64 ,{128 ,140 ,192 } },
78 { 80 ,{160 ,174 ,240 } },
79 { 80 ,{160 ,175 ,240 } },
80 { 96 ,{192 ,208 ,288 } },
81 { 96 ,{192 ,209 ,288 } },
82 { 112 ,{224 ,243 ,336 } },
83 { 112 ,{224 ,244 ,336 } },
84 { 128 ,{256 ,278 ,384 } },
85 { 128 ,{256 ,279 ,384 } },
86 { 160 ,{320 ,348 ,480 } },
87 { 160 ,{320 ,349 ,480 } },
88 { 192 ,{384 ,417 ,576 } },
89 { 192 ,{384 ,418 ,576 } },
90 { 224 ,{448 ,487 ,672 } },
91 { 224 ,{448 ,488 ,672 } },
92 { 256 ,{512 ,557 ,768 } },
93 { 256 ,{512 ,558 ,768 } },
94 { 320 ,{640 ,696 ,960 } },
95 { 320 ,{640 ,697 ,960 } },
96 { 384 ,{768 ,835 ,1152 } },
97 { 384 ,{768 ,836 ,1152 } },
98 { 448 ,{896 ,975 ,1344 } },
99 { 448 ,{896 ,976 ,1344 } },
100 { 512 ,{1024 ,1114 ,1536 } },
101 { 512 ,{1024 ,1115 ,1536 } },
102 { 576 ,{1152 ,1253 ,1728 } },
103 { 576 ,{1152 ,1254 ,1728 } },
104 { 640 ,{1280 ,1393 ,1920 } },
105 { 640 ,{1280 ,1394 ,1920 } }
108 /****************************************************************************
109 * ac3_iec958_build_burst: builds an iec958/spdif frame based on an ac3 frame
110 ****************************************************************************/
111 void ac3_iec958_build_burst( ac3_spdif_thread_t *p_spdif )
113 const u8 p_sync[6] = { 0x72, 0xF8, 0x1F, 0x4E, 0x01, 0x00 };
114 int i_length = p_spdif->ac3_info.i_frame_size;
116 /* Skip the first byte if i_length is odd */
117 u16 * p_in = (u16 *)( p_spdif->p_ac3 + ( i_length & 0x1 ) );
118 u16 * p_out = (u16 *)p_spdif->p_iec;
121 /* Add the spdif headers */
122 memcpy( p_spdif->p_iec, p_sync, 6 );
123 p_spdif->p_iec[6] = ( i_length * 8 ) & 0xFF;
124 p_spdif->p_iec[7] = ( ( i_length * 8 ) >> 8 ) & 0xFF;
127 swab( p_spdif->p_ac3, p_spdif->p_iec + 8, i_length );
129 /* i_length should be even */
134 *p_out = ( (*p_in & 0x00ff) << 16 ) | ( (*p_in & 0xff00) >> 16 );
141 /* Add zeroes to complete the spdif frame,
142 * they will be ignored by the decoder */
143 memset( p_spdif->p_iec + 8 + i_length, 0, SPDIF_FRAME_SIZE - 8 - i_length );
146 /****************************************************************************
147 * ac3_iec958_parse_syncinfo: parse ac3 sync info
148 ****************************************************************************/
149 int ac3_iec958_parse_syncinfo( ac3_spdif_thread_t *p_spdif )
151 int p_sample_rates[4] = { 48000, 44100, 32000, -1 };
152 int i_frame_rate_code;
153 int i_frame_size_code;
154 sync_frame_t * p_sync_frame;
156 /* Read sync frame */
157 GetChunk( &p_spdif->bit_stream, p_spdif->p_ac3 + 2,
158 sizeof(sync_frame_t) - 2 );
159 p_sync_frame = (sync_frame_t*)p_spdif->p_ac3;
161 /* Compute frame rate */
162 i_frame_rate_code = (p_sync_frame->syncinfo.code >> 6) & 0x03;
163 p_spdif->ac3_info.i_sample_rate = p_sample_rates[i_frame_rate_code];
164 if( p_spdif->ac3_info.i_sample_rate == -1 )
169 /* Compute frame size */
170 i_frame_size_code = p_sync_frame->syncinfo.code & 0x3f;
171 p_spdif->ac3_info.i_frame_size = 2 *
172 p_frame_size_code[i_frame_size_code].i_frame_size[i_frame_rate_code];
173 p_spdif->ac3_info.i_bit_rate =
174 p_frame_size_code[i_frame_size_code].i_bit_rate;
176 if( ( ( p_sync_frame->bsi.bsidmod >> 3 ) & 0x1f ) != 0x08 )
181 p_spdif->ac3_info.i_bs_mod = p_sync_frame->bsi.bsidmod & 0x7;