]> git.sesse.net Git - vlc/blob - plugins/ac3_spdif/ac3_iec958.c
All decoders (audio, video, subtitles) are now modules.
[vlc] / plugins / 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/11/13 12:09:17 henri 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 <string.h>                                              /* memset() */
34 #include <fcntl.h>
35
36 #ifdef HAVE_UNISTD_H
37 #include <unistd.h>
38 #endif
39
40 #include "config.h"
41 #include "common.h"
42 #include "threads.h"
43 #include "mtime.h"
44 #include "intf_msg.h"                        /* intf_DbgMsg(), intf_ErrMsg() */
45
46 #include "audio_output.h"
47
48 #include "modules_export.h"
49
50 #include "stream_control.h"
51 #include "input_ext-dec.h"
52
53
54 #include "ac3_spdif.h"
55 #include "ac3_iec958.h"
56
57 /****************************************************************************
58  * Local structures and tables
59  ****************************************************************************/
60 typedef struct frame_size_s
61 {
62     u16     i_bit_rate;
63     u16     i_frame_size[3];
64 } frame_size_t;
65                 
66 static const frame_size_t p_frame_size_code[64] =
67 {
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 } }
106 };
107
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 )
112 {
113     const u8 p_sync[6] = { 0x72, 0xF8, 0x1F, 0x4E, 0x01, 0x00 };
114     int      i_length  = p_spdif->ac3_info.i_frame_size;
115 #ifndef HAVE_SWAB
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;
119 #endif
120
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;
125
126 #ifdef HAVE_SWAB
127     swab( p_spdif->p_ac3, p_spdif->p_iec + 8, i_length );
128 #else
129     /* i_length should be even */
130     i_length &= ~0x1;
131
132     while( i_length )
133     {
134         *p_out = ( (*p_in & 0x00ff) << 16 ) | ( (*p_in & 0xff00) >> 16 );
135         p_in++;
136         p_out++;
137         i_length -= 2;
138     }
139 #endif
140
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 );
144 }
145
146 /****************************************************************************
147  * ac3_iec958_parse_syncinfo: parse ac3 sync info
148  ****************************************************************************/
149 int ac3_iec958_parse_syncinfo( ac3_spdif_thread_t *p_spdif )
150 {
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;
155
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;
160
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 )
165     {
166         return -1;
167     }
168
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;
175
176     if( ( ( p_sync_frame->bsi.bsidmod >> 3 ) & 0x1f ) != 0x08 )
177     {
178         return -1;
179     }
180
181     p_spdif->ac3_info.i_bs_mod = p_sync_frame->bsi.bsidmod & 0x7;
182
183     return 0;
184 }
185