]> git.sesse.net Git - vlc/blob - src/ac3_spdif/ac3_iec958.c
e5595a08221b4f20498e3a9b152a46696effe822
[vlc] / src / ac3_spdif / ac3_iec958.c
1 /*****************************************************************************
2  * ac3_iec958.c: ac3 to spdif converter
3  *****************************************************************************
4  * Copyright (C) 2001 VideoLAN
5  * $Id: ac3_iec958.c,v 1.1 2001/04/29 02:48:51 stef Exp $
6  *
7  * Authors: Stéphane Borel <stef@via.ecp.fr>
8  *          Juha Yrjola <jyrjola@cc.hut.fi>
9  *          German Gomez Garcia <german@piraos.com>
10  *
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.
15  * 
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.
20  *
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  ****************************************************************************/
25
26 /****************************************************************************
27  * Preamble
28  ****************************************************************************/
29 #include "defs.h"
30
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <fcntl.h>
34 #include <unistd.h>
35
36 #include "config.h"
37 #include "common.h"
38 #include "threads.h"
39 #include "mtime.h"
40
41 #include "intf_msg.h"                        /* intf_DbgMsg(), intf_ErrMsg() */
42
43 #include "stream_control.h"
44 #include "input_ext-dec.h"
45
46 #include "audio_output.h"
47
48 #include "ac3_spdif.h"
49 #include "ac3_iec958.h"
50
51 /****************************************************************************
52  * Local structures and tables
53  ****************************************************************************/
54 typedef struct frame_size_s
55 {
56     u16     i_bit_rate;
57     u16     i_frame_size[3];
58 } frame_size_t;
59                 
60 static const frame_size_t p_frame_size_code[64] =
61 {
62         { 32  ,{64   ,69   ,96   } },
63         { 32  ,{64   ,70   ,96   } },
64         { 40  ,{80   ,87   ,120  } },
65         { 40  ,{80   ,88   ,120  } },
66         { 48  ,{96   ,104  ,144  } },
67         { 48  ,{96   ,105  ,144  } },
68         { 56  ,{112  ,121  ,168  } },
69         { 56  ,{112  ,122  ,168  } },
70         { 64  ,{128  ,139  ,192  } },
71         { 64  ,{128  ,140  ,192  } },
72         { 80  ,{160  ,174  ,240  } },
73         { 80  ,{160  ,175  ,240  } },
74         { 96  ,{192  ,208  ,288  } },
75         { 96  ,{192  ,209  ,288  } },
76         { 112 ,{224  ,243  ,336  } },
77         { 112 ,{224  ,244  ,336  } },
78         { 128 ,{256  ,278  ,384  } },
79         { 128 ,{256  ,279  ,384  } },
80         { 160 ,{320  ,348  ,480  } },
81         { 160 ,{320  ,349  ,480  } },
82         { 192 ,{384  ,417  ,576  } },
83         { 192 ,{384  ,418  ,576  } },
84         { 224 ,{448  ,487  ,672  } },
85         { 224 ,{448  ,488  ,672  } },
86         { 256 ,{512  ,557  ,768  } },
87         { 256 ,{512  ,558  ,768  } },
88         { 320 ,{640  ,696  ,960  } },
89         { 320 ,{640  ,697  ,960  } },
90         { 384 ,{768  ,835  ,1152 } },
91         { 384 ,{768  ,836  ,1152 } },
92         { 448 ,{896  ,975  ,1344 } },
93         { 448 ,{896  ,976  ,1344 } },
94         { 512 ,{1024 ,1114 ,1536 } },
95         { 512 ,{1024 ,1115 ,1536 } },
96         { 576 ,{1152 ,1253 ,1728 } },
97         { 576 ,{1152 ,1254 ,1728 } },
98         { 640 ,{1280 ,1393 ,1920 } },
99         { 640 ,{1280 ,1394 ,1920 } }
100 };
101
102 /****************************************************************************
103  * ac3_iec958_build_burst: builds an iec958/spdif frame based on an ac3 frame
104  ****************************************************************************/
105 void ac3_iec958_build_burst( int i_length, u8 * pi_data, u8 * pi_out )
106 {
107     const u8 pi_sync[4] = { 0x72, 0xF8, 0x1F, 0x4E };
108
109     /* add the spdif headers */
110     memcpy( pi_out, pi_sync, 4 );
111     if( i_length )
112         pi_out[4] = 0x01;
113     else
114         pi_out[4] = 0;
115     pi_out[5] = 0x00;
116     pi_out[6] = ( i_length *8 ) & 0xFF;
117     pi_out[7] = ( ( i_length *8 ) >> 8 ) & 0xFF;
118
119     swab( pi_data, pi_out + 8, i_length );
120     /* adds zero to complete the spdif frame
121      * they will be ignored by the decoder */
122     memset( pi_out + 8 + i_length, 0, SPDIF_FRAME - 8 - i_length );
123 }
124
125 /****************************************************************************
126  * ac3_iec958_parse_syncinfo: parse ac3 sync info
127  ****************************************************************************/
128 int ac3_iec958_parse_syncinfo( ac3_spdif_thread_t *p_spdif,
129                                ac3_info_t *ac3_info,
130                                u8 * pi_ac3 )
131 {
132     int             pi_sample_rates[4] = { 48000, 44100, 32000, -1 };
133     int             i_frame_rate_code;
134     int             i_frame_size_code;
135 //    u8 *            pi_tmp;
136     sync_frame_t *  p_sync_frame;
137
138     /* find sync word */
139     while( ShowBits( &p_spdif->bit_stream, 16 ) != 0xb77 )
140     {
141         RemoveBits( &p_spdif->bit_stream, 8 );
142     }
143
144     /* read sync frame */
145     pi_ac3 = malloc( sizeof(sync_frame_t) );
146     GetChunk( &p_spdif->bit_stream, pi_ac3, sizeof(sync_frame_t) );
147     p_sync_frame = (sync_frame_t*)pi_ac3;
148
149     /* compute frame rate */
150     i_frame_rate_code = (p_sync_frame->syncinfo.code >> 6) & 0x03;
151     ac3_info->i_sample_rate = pi_sample_rates[i_frame_rate_code];
152     if (ac3_info->i_sample_rate == -1)
153     {
154         return -1;
155     }
156
157     /* compute frame size */
158     i_frame_size_code = p_sync_frame->syncinfo.code & 0x3f;
159     ac3_info->i_frame_size = 2 *
160         p_frame_size_code[i_frame_size_code].i_frame_size[i_frame_rate_code];
161     ac3_info->i_bit_rate = p_frame_size_code[i_frame_size_code].i_bit_rate;
162
163     if( ( ( p_sync_frame->bsi.bsidmod >> 3 ) & 0x1f ) != 0x08 )
164     {
165         return -1;
166     }
167
168     ac3_info->i_bs_mod = p_sync_frame->bsi.bsidmod & 0x7;
169
170 //    free( pi_tmp );
171
172     return 0;
173 }