6 #include "ad_internal.h"
8 #include "loader/ldt_keeper.h"
9 #include "loader/wine/windef.h"
10 #include "libaf/af_format.h"
14 static ad_info_t info
=
20 "Ported from MPlayerXP"
25 void* WINAPI
LoadLibraryA(char* name
);
26 void* WINAPI
GetProcAddress(void* handle
, char* func
);
27 int WINAPI
FreeLibrary(void* handle
);
29 static int (*TvqInitialize
)( headerInfo
*setupInfo
, INDEX
*index
, int dispErrorMessageBox
);
30 static void (*TvqTerminate
)( INDEX
*index
);
31 static void (*TvqGetVectorInfo
)(int *bits0
[], int *bits1
[]);
33 static void (*TvqDecodeFrame
)(INDEX
*indexp
, float out
[]);
34 static int (*TvqWtypeToBtype
)( int w_type
, int *btype
);
35 static void (*TvqUpdateVectorInfo
)(int varbits
, int *ndiv
, int bits0
[], int bits1
[]);
37 static int (*TvqCheckVersion
)(char *versionID
);
38 static void (*TvqGetConfInfo
)(tvqConfInfo
*cf
);
39 static int (*TvqGetFrameSize
)();
40 static int (*TvqGetNumFixedBitsPerFrame
)();
43 #define BBUFSIZ 1024 /* Bit buffer size (bytes) */
44 #define BBUFLEN (BBUFSIZ*BYTE_BIT) /* Bit buffer length (bits) */
45 typedef struct vqf_priv_s
48 WAVEFORMATEX o_wf
; // out format
52 int *bits_0
[N_INTR_TYPE
], *bits_1
[N_INTR_TYPE
];
56 int ptr
; /* current point in the bit buffer */
57 int nbuf
; /* bit buffer size */
58 char buf
[BBUFSIZ
]; /* the bit buffer */
64 static int load_dll( char *libname
)
69 vqf_dll
= LoadLibraryA(libname
);
72 mp_msg(MSGT_DECAUDIO
, MSGL_ERR
, "failed loading dll\n" );
75 TvqInitialize
= GetProcAddress(vqf_dll
,"TvqInitialize");
76 TvqTerminate
= GetProcAddress(vqf_dll
,"TvqTerminate");
77 TvqGetVectorInfo
= GetProcAddress(vqf_dll
,"TvqGetVectorInfo");
78 TvqDecodeFrame
= GetProcAddress(vqf_dll
,"TvqDecodeFrame");
79 TvqWtypeToBtype
= GetProcAddress(vqf_dll
,"TvqWtypeToBtype");
80 TvqUpdateVectorInfo
= GetProcAddress(vqf_dll
,"TvqUpdateVectorInfo");
81 TvqCheckVersion
= GetProcAddress(vqf_dll
,"TvqCheckVersion");
82 TvqGetConfInfo
= GetProcAddress(vqf_dll
,"TvqGetConfInfo");
83 TvqGetFrameSize
= GetProcAddress(vqf_dll
,"TvqGetFrameSize");
84 TvqGetNumFixedBitsPerFrame
= GetProcAddress(vqf_dll
,"TvqGetNumFixedBitsPerFrame");
85 return TvqInitialize
&& TvqTerminate
&& TvqGetVectorInfo
&&
86 TvqDecodeFrame
&& TvqWtypeToBtype
&& TvqUpdateVectorInfo
&&
87 TvqCheckVersion
&& TvqGetConfInfo
&& TvqGetFrameSize
&&
88 TvqGetNumFixedBitsPerFrame
;
91 void print_wave_header(WAVEFORMATEX
*h
, int verbose_level
);
92 static int init_vqf_audio_codec(sh_audio_t
*sh_audio
){
93 WAVEFORMATEX
*in_fmt
=sh_audio
->wf
;
94 vqf_priv_t
*priv
=sh_audio
->context
;
96 mp_msg(MSGT_DECAUDIO
, MSGL_INFO
, "======= Win32 (TWinVQ) AUDIO Codec init =======\n");
98 sh_audio
->channels
=in_fmt
->nChannels
;
99 sh_audio
->samplerate
=in_fmt
->nSamplesPerSec
;
100 sh_audio
->sample_format
=AF_FORMAT_S16_NE
;
101 // sh_audio->sample_format=AF_FORMAT_FLOAT_NE;
102 sh_audio
->samplesize
=af_fmt2bits(sh_audio
->sample_format
)/8;
103 priv
->o_wf
.nChannels
=in_fmt
->nChannels
;
104 priv
->o_wf
.nSamplesPerSec
=in_fmt
->nSamplesPerSec
;
105 priv
->o_wf
.nBlockAlign
=sh_audio
->samplesize
*in_fmt
->nChannels
;
106 priv
->o_wf
.nAvgBytesPerSec
=in_fmt
->nBlockAlign
*in_fmt
->nChannels
;
107 priv
->o_wf
.wFormatTag
=0x01;
108 priv
->o_wf
.wBitsPerSample
=in_fmt
->wBitsPerSample
;
111 if( mp_msg_test(MSGT_DECAUDIO
,MSGL_V
) )
113 mp_msg(MSGT_DECAUDIO
, MSGL_V
, "Input format:\n");
114 print_wave_header(in_fmt
, MSGL_V
);
115 mp_msg(MSGT_DECAUDIO
, MSGL_V
, "Output fmt:\n");
116 print_wave_header(&priv
->o_wf
, MSGL_V
);
118 memcpy(&priv
->hi
,&in_fmt
[1],sizeof(headerInfo
));
119 if((ver
=TvqInitialize(&priv
->hi
,&priv
->index
,0))){
124 "Channel setting error",
126 "Inner parameter setting error",
127 "Wrong number of VQ pre-selection candidates, used only in encoder" };
128 mp_msg(MSGT_DECAUDIO
, MSGL_ERR
, "Tvq initialization error: %s\n",ver
>=0&&ver
<7?tvqe
[ver
]:"Unknown");
131 ver
=TvqCheckVersion(priv
->hi
.ID
);
132 if(ver
==TVQ_UNKNOWN_VERSION
){
133 mp_msg(MSGT_DECAUDIO
, MSGL_ERR
, "Tvq unknown version of stream\n" );
136 TvqGetConfInfo(&priv
->cf
);
137 TvqGetVectorInfo(priv
->bits_0
,priv
->bits_1
);
138 priv
->framesize
=TvqGetFrameSize();
139 sh_audio
->audio_in_minsize
=priv
->framesize
*in_fmt
->nChannels
;
140 sh_audio
->a_in_buffer_size
=4*sh_audio
->audio_in_minsize
;
141 sh_audio
->a_in_buffer
=av_malloc(sh_audio
->a_in_buffer_size
);
142 sh_audio
->a_in_buffer_len
=0;
148 static int close_vqf_audio_codec(sh_audio_t
*sh_audio
)
150 vqf_priv_t
*priv
=sh_audio
->context
;
151 TvqTerminate(&priv
->index
);
155 int init(sh_audio_t
*sh_audio
)
160 int preinit(sh_audio_t
*sh_audio
)
162 /* Win32 VQF audio codec: */
164 if(!(sh_audio
->context
=malloc(sizeof(vqf_priv_t
)))) return 0;
165 priv
=sh_audio
->context
;
166 if(!load_dll(sh_audio
->codec
->dll
))
168 mp_msg(MSGT_DECAUDIO
, MSGL_ERR
, "win32.dll looks broken :(\n");
171 if(!init_vqf_audio_codec(sh_audio
)){
172 mp_msg(MSGT_DECAUDIO
, MSGL_ERR
, "TWinVQ initialization fail\n");
175 mp_msg(MSGT_DECAUDIO
, MSGL_INFO
, "INFO: TWinVQ (%s) audio codec init OK!\n",sh_audio
->codec
->dll
);
180 void uninit(sh_audio_t
*sh
)
182 close_vqf_audio_codec(sh
);
184 FreeLibrary(vqf_dll
);
187 int control(sh_audio_t
*sh_audio
,int cmd
,void* arg
, ...)
190 case ADCTRL_QUERY_FORMAT
:
193 return CONTROL_UNKNOWN
;
197 static int bread(char *data
, /* Output: Output data array */
198 int size
, /* Input: Length of each data */
199 int nbits
, /* Input: Number of bits to write */
200 sh_audio_t
*sh
) /* Input: File pointer */
202 /*--- Variables ---*/
203 int ibits
, iptr
, idata
, ibufadr
, ibufbit
, icl
;
204 unsigned char mask
, tmpdat
;
206 vqf_priv_t
*priv
=sh
->context
;
208 /*--- Main operation ---*/
211 for ( ibits
=0; ibits
<nbits
; ibits
++ ){
212 if ( priv
->readable
== 0 ){ /* when the file data buffer is empty */
213 priv
->nbuf
= demux_read_data(sh
->ds
, priv
->buf
, BBUFSIZ
);
217 iptr
= priv
->ptr
; /* current file data buffer pointer */
218 if ( iptr
>= priv
->nbuf
) /* If data file is empty then return */
220 ibufadr
= iptr
/BYTE_BIT
; /* current file data buffer address */
221 ibufbit
= iptr
%BYTE_BIT
; /* current file data buffer bit */
222 /* tmpdat = stream->buf[ibufadr] >> (BYTE_BIT-ibufbit-1); */
223 tmpdat
= (unsigned char)priv
->buf
[ibufadr
];
224 tmpdat
>>= (BYTE_BIT
-ibufbit
-1);
225 /* current data bit */
227 idata
= ibits
*size
; /* output data address */
228 data
[idata
] = (char)(tmpdat
& mask
); /* set output data */
229 for (icl
=1; icl
<size
; icl
++)
230 data
[idata
+icl
] = 0; /* clear the rest output data buffer */
231 priv
->ptr
+= 1; /* update data buffer pointer */
232 if (priv
->ptr
== BBUFLEN
){
241 #define BITS_INT (sizeof(int)*8)
243 static int get_bstm(int *data
, /* Input: input data */
244 unsigned nbits
, /* Input: number of bits */
245 sh_audio_t
*sh
) /* Input: bit file pointer */
250 char tmpbit
[BITS_INT
];
253 if ( nbits
> BITS_INT
){
254 mp_msg(MSGT_DECAUDIO
, MSGL_ERR
, "get_bstm(): %d: %d Error.\n",
258 retval
= bread(tmpbit
, sizeof(*tmpbit
), nbits
, sh
);
259 for (ibit
=retval
; ibit
<nbits
; ibit
++){
262 mask
= 0x1<<(nbits
-1);
264 for ( ibit
=0; ibit
<nbits
; ibit
++ ){
265 work
+= mask
*tmpbit
[ibit
];
272 static int GetVqInfo( tvqConfInfoSubBlock
*cfg
,
282 if ( index
->btype
== BLK_LONG
){
283 TvqUpdateVectorInfo( variableBits
, &cfg
->ndiv
, bits0
, bits1
); // re-calculate VQ bits
285 for ( idiv
=0; idiv
<cfg
->ndiv
; idiv
++ ){
286 bitcount
+= get_bstm(&index
->wvq
[idiv
],bits0
[idiv
],sh
); /* CB 0 */
287 bitcount
+= get_bstm(&index
->wvq
[idiv
+cfg
->ndiv
],bits1
[idiv
],sh
); /* CB 1 */
292 static int GetBseInfo( tvqConfInfo
*cf
, tvqConfInfoSubBlock
*cfg
, INDEX
*index
, sh_audio_t
*sh
)
294 int i_sup
, isf
, itmp
, idiv
;
297 for ( i_sup
=0; i_sup
<cf
->N_CH
; i_sup
++ ){
298 for ( isf
=0; isf
<cfg
->nsf
; isf
++ ){
299 for ( idiv
=0; idiv
<cfg
->fw_ndiv
; idiv
++ ){
300 itmp
= idiv
+ ( isf
+ i_sup
* cfg
->nsf
) * cfg
->fw_ndiv
;
301 bitcount
+= get_bstm(&index
->fw
[itmp
],cfg
->fw_nbit
,sh
);
305 for ( i_sup
=0; i_sup
<cf
->N_CH
; i_sup
++ ){
306 for ( isf
=0; isf
<cfg
->nsf
; isf
++ ){
307 bitcount
+= get_bstm(&index
->fw_alf
[i_sup
* cfg
->nsf
+ isf
],cf
->FW_ARSW_BITS
,sh
);
313 static int GetGainInfo(tvqConfInfo
*cf
, tvqConfInfoSubBlock
*cfg
, INDEX
*index
, sh_audio_t
*sh
)
315 int i_sup
, iptop
, isf
;
318 for ( i_sup
=0; i_sup
<cf
->N_CH
; i_sup
++ ){
319 iptop
= ( cfg
->nsubg
+ 1 ) * i_sup
;
320 bitcount
+= get_bstm(&index
->pow
[iptop
], cf
->GAIN_BITS
,sh
);
321 for ( isf
=0; isf
<cfg
->nsubg
; isf
++ ){
322 bitcount
+= get_bstm(&index
->pow
[iptop
+isf
+1], cf
->SUB_GAIN_BITS
,sh
);
328 static int GetLspInfo( tvqConfInfo
*cf
, INDEX
*index
, sh_audio_t
*sh
)
333 for ( i_sup
=0; i_sup
<cf
->N_CH
; i_sup
++ ){
334 bitcount
+= get_bstm(&index
->lsp
[i_sup
][0], cf
->LSP_BIT0
,sh
); /* pred. switch */
335 bitcount
+= get_bstm(&index
->lsp
[i_sup
][1], cf
->LSP_BIT1
,sh
); /* first stage */
336 for ( itmp
=0; itmp
<cf
->LSP_SPLIT
; itmp
++ ){ /* second stage */
337 bitcount
+= get_bstm(&index
->lsp
[i_sup
][itmp
+2], cf
->LSP_BIT2
,sh
);
344 static int GetPpcInfo( tvqConfInfo
*cf
, INDEX
*index
, sh_audio_t
*sh
)
348 vqf_priv_t
*priv
=sh
->context
;
350 for ( idiv
=0; idiv
<cf
->N_DIV_P
; idiv
++ ){
351 bitcount
+= get_bstm(&(index
->pls
[idiv
]), priv
->bits_0
[BLK_PPC
][idiv
],sh
); /*CB0*/
352 bitcount
+= get_bstm(&(index
->pls
[idiv
+cf
->N_DIV_P
]), priv
->bits_1
[BLK_PPC
][idiv
],sh
);/*CB1*/
354 for (i_sup
=0; i_sup
<cf
->N_CH
; i_sup
++){
355 bitcount
+= get_bstm(&(index
->pit
[i_sup
]), cf
->BASF_BIT
,sh
);
356 bitcount
+= get_bstm(&(index
->pgain
[i_sup
]), cf
->PGAIN_BIT
,sh
);
362 static int GetEbcInfo( tvqConfInfo
*cf
, tvqConfInfoSubBlock
*cfg
, INDEX
*index
, sh_audio_t
*sh
)
364 int i_sup
, isf
, itmp
;
367 for ( i_sup
=0; i_sup
<cf
->N_CH
; i_sup
++ ){
368 for ( isf
=0; isf
<cfg
->nsf
; isf
++){
369 int indexSfOffset
= isf
* ( cfg
->ncrb
- cfg
->ebc_crb_base
) - cfg
->ebc_crb_base
;
370 for ( itmp
=cfg
->ebc_crb_base
; itmp
<cfg
->ncrb
; itmp
++ ){
371 bitcount
+= get_bstm(&index
->bc
[i_sup
][itmp
+indexSfOffset
], cfg
->ebc_bits
,sh
);
379 static int vqf_read_frame(sh_audio_t
*sh
,INDEX
*index
)
381 /*--- Variables ---*/
382 tvqConfInfoSubBlock
*cfg
;
385 int numFixedBitsPerFrame
= TvqGetNumFixedBitsPerFrame();
387 vqf_priv_t
*priv
=sh
->context
;
389 /*--- Initialization ---*/
393 /*--- read block independent factors ---*/
395 bitcount
+= get_bstm( &index
->w_type
, priv
->cf
.BITS_WTYPE
, sh
);
396 if ( TvqWtypeToBtype( index
->w_type
, &index
->btype
) ) {
397 mp_msg(MSGT_DECAUDIO
, MSGL_ERR
, "Error: unknown window type: %d\n", index
->w_type
);
400 btype
= index
->btype
;
402 /*--- read block dependent factors ---*/
403 cfg
= &priv
->cf
.cfg
[btype
]; // set the block dependent paremeters table
405 bitcount
+= variableBits
;
407 /* Interleaved vector quantization */
408 bitcount
+= GetVqInfo( cfg
, priv
->bits_0
[btype
], priv
->bits_1
[btype
], variableBits
, index
, sh
);
410 /* Bark-scale envelope */
411 bitcount
+= GetBseInfo( &priv
->cf
, cfg
, index
, sh
);
413 bitcount
+= GetGainInfo( &priv
->cf
, cfg
, index
, sh
);
415 bitcount
+= GetLspInfo( &priv
->cf
, index
, sh
);
417 if ( cfg
->ppc_enable
){
418 bitcount
+= GetPpcInfo( &priv
->cf
, index
, sh
);
420 /* Energy Balance Calibration */
421 if ( cfg
->ebc_enable
){
422 bitcount
+= GetEbcInfo( &priv
->cf
, cfg
, index
, sh
);
425 return bitcount
== numFixedBitsPerFrame
? bitcount
/8 : 0;
428 static void frtobuf_s16(float out
[], /* Input --- input data frame */
429 short bufout
[], /* Output --- output data buffer array */
430 unsigned frameSize
, /* Input --- frame size */
431 unsigned numChannels
) /* Input --- number of channels */
433 /*--- Variables ---*/
438 for ( ich
=0; ich
<numChannels
; ich
++ ){
439 ptr
= out
+ich
*frameSize
;
440 for ( ismp
=0; ismp
<frameSize
; ismp
++ ){
445 bufout
[ismp
*numChannels
+ich
] = (short)(dtmp
+0.5);
447 if ( dtmp
< -32700. )
449 bufout
[ismp
*numChannels
+ich
] = (short)(dtmp
-0.5);
455 static void frtobuf_float(float out
[], /* Input --- input data frame */
456 float bufout
[], /* Output --- output data buffer array */
457 unsigned frameSize
, /* Input --- frame size */
458 unsigned numChannels
) /* Input --- number of channels */
460 /*--- Variables ---*/
465 for ( ich
=0; ich
<numChannels
; ich
++ ){
466 ptr
= out
+ich
*frameSize
;
467 for ( ismp
=0; ismp
<frameSize
; ismp
++ ){
472 bufout
[ismp
*numChannels
+ich
] = dtmp
/32767.;
474 if ( dtmp
< -32700. )
476 bufout
[ismp
*numChannels
+ich
] = dtmp
/32767.;
482 int decode_audio(sh_audio_t
*sh_audio
,unsigned char *buf
,int minlen
,int maxlen
)
485 vqf_priv_t
*priv
=sh_audio
->context
;
488 float out
[priv
->framesize
*sh_audio
->channels
];
489 l
=vqf_read_frame(sh_audio
,&priv
->index
);
491 TvqDecodeFrame(&priv
->index
, out
);
492 if (priv
->skip_cnt
) {
493 // Ingnore first two frames, replace them with silence
495 memset(buf
, 0, priv
->framesize
*sh_audio
->channels
*sh_audio
->samplesize
);
497 if (sh_audio
->sample_format
== AF_FORMAT_S16_NE
)
498 frtobuf_s16(out
, (short *)buf
, priv
->framesize
, sh_audio
->channels
);
500 frtobuf_float(out
, (float *)buf
, priv
->framesize
, sh_audio
->channels
);
502 len
+= priv
->framesize
*sh_audio
->channels
*sh_audio
->samplesize
;
503 buf
+= priv
->framesize
*sh_audio
->channels
*sh_audio
->samplesize
;