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