annotate libmpcodecs/ad_twin.c @ 26395:71bd93e71a97

Restore grayscale decoding support with FFmpeg. Removing support was done due to a silly misunderstanding.
author diego
date Sun, 13 Apr 2008 00:03:11 +0000
parents a033e5519802
children 63630c09e237
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
14276
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
1 #include <stdio.h>
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
2 #include <stdlib.h>
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
3 #include <unistd.h>
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
4 #include "config.h"
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
5
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
6 #include "ad_internal.h"
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
7 #include "vqf.h"
17012
6ff3379a0862 Unify include path handling, -I.. is in CFLAGS.
diego
parents: 14276
diff changeset
8 #include "loader/ldt_keeper.h"
22577
a033e5519802 Include loader/ prefix in #include path everywhere.
diego
parents: 18771
diff changeset
9 #include "loader/wine/windef.h"
17012
6ff3379a0862 Unify include path handling, -I.. is in CFLAGS.
diego
parents: 14276
diff changeset
10 #include "libaf/af_format.h"
14276
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
11
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
12 #include "help_mp.h"
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
13
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
14 static ad_info_t info =
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
15 {
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
16 "TWinVQ decoder",
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
17 "vqf",
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
18 "Roberto Togni",
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
19 "Nick Kurshev",
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
20 "Ported from MPlayerXP"
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
21 };
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
22
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
23 LIBAD_EXTERN(twin)
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
24
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
25 void* WINAPI LoadLibraryA(char* name);
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
26 void* WINAPI GetProcAddress(void* handle, char* func);
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
27 int WINAPI FreeLibrary(void* handle);
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
28
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
29 static int (*TvqInitialize)( headerInfo *setupInfo, INDEX *index, int dispErrorMessageBox );
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
30 static void (*TvqTerminate)( INDEX *index );
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
31 static void (*TvqGetVectorInfo)(int *bits0[], int *bits1[]);
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
32
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
33 static void (*TvqDecodeFrame)(INDEX *indexp, float out[]);
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
34 static int (*TvqWtypeToBtype)( int w_type, int *btype );
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
35 static void (*TvqUpdateVectorInfo)(int varbits, int *ndiv, int bits0[], int bits1[]);
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
36
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
37 static int (*TvqCheckVersion)(char *versionID);
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
38 static void (*TvqGetConfInfo)(tvqConfInfo *cf);
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
39 static int (*TvqGetFrameSize)();
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
40 static int (*TvqGetNumFixedBitsPerFrame)();
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
41
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
42 #define BYTE_BIT 8
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
43 #define BBUFSIZ 1024 /* Bit buffer size (bytes) */
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
44 #define BBUFLEN (BBUFSIZ*BYTE_BIT) /* Bit buffer length (bits) */
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
45 typedef struct vqf_priv_s
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
46 {
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
47 float pts;
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
48 WAVEFORMATEX o_wf; // out format
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
49 INDEX index;
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
50 tvqConfInfo cf;
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
51 headerInfo hi;
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
52 int *bits_0[N_INTR_TYPE], *bits_1[N_INTR_TYPE];
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
53 unsigned framesize;
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
54 /* stream related */
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
55 int readable;
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
56 int ptr; /* current point in the bit buffer */
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
57 int nbuf; /* bit buffer size */
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
58 char buf[BBUFSIZ]; /* the bit buffer */
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
59 int skip_cnt;
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
60 }vqf_priv_t;
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
61
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
62 static void* vqf_dll;
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
63
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
64 static int load_dll( char *libname )
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
65 {
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
66 #ifdef WIN32_LOADER
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
67 Setup_LDT_Keeper();
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
68 #endif
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
69 vqf_dll = LoadLibraryA(libname);
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
70 if( vqf_dll == NULL )
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
71 {
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
72 mp_msg(MSGT_DECAUDIO, MSGL_ERR, "failed loading dll\n" );
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
73 return 0;
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
74 }
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
75 TvqInitialize = GetProcAddress(vqf_dll,"TvqInitialize");
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
76 TvqTerminate = GetProcAddress(vqf_dll,"TvqTerminate");
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
77 TvqGetVectorInfo = GetProcAddress(vqf_dll,"TvqGetVectorInfo");
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
78 TvqDecodeFrame = GetProcAddress(vqf_dll,"TvqDecodeFrame");
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
79 TvqWtypeToBtype = GetProcAddress(vqf_dll,"TvqWtypeToBtype");
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
80 TvqUpdateVectorInfo = GetProcAddress(vqf_dll,"TvqUpdateVectorInfo");
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
81 TvqCheckVersion = GetProcAddress(vqf_dll,"TvqCheckVersion");
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
82 TvqGetConfInfo = GetProcAddress(vqf_dll,"TvqGetConfInfo");
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
83 TvqGetFrameSize = GetProcAddress(vqf_dll,"TvqGetFrameSize");
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
84 TvqGetNumFixedBitsPerFrame = GetProcAddress(vqf_dll,"TvqGetNumFixedBitsPerFrame");
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
85 return TvqInitialize && TvqTerminate && TvqGetVectorInfo &&
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
86 TvqDecodeFrame && TvqWtypeToBtype && TvqUpdateVectorInfo &&
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
87 TvqCheckVersion && TvqGetConfInfo && TvqGetFrameSize &&
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
88 TvqGetNumFixedBitsPerFrame;
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
89 }
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
90
17977
f70772d02eaa Convert printfs in aviprint.c to mp_msg and give the information printing
diego
parents: 17932
diff changeset
91 extern void print_wave_header(WAVEFORMATEX *h, int verbose_level);
14276
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
92 static int init_vqf_audio_codec(sh_audio_t *sh_audio){
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
93 WAVEFORMATEX *in_fmt=sh_audio->wf;
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
94 vqf_priv_t*priv=sh_audio->context;
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
95 int ver;
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
96 mp_msg(MSGT_DECAUDIO, MSGL_INFO, "======= Win32 (TWinVQ) AUDIO Codec init =======\n");
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
97
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
98 sh_audio->channels=in_fmt->nChannels;
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
99 sh_audio->samplerate=in_fmt->nSamplesPerSec;
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
100 sh_audio->sample_format=AF_FORMAT_S16_NE;
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
101 // sh_audio->sample_format=AF_FORMAT_FLOAT_NE;
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
102 sh_audio->samplesize=af_fmt2bits(sh_audio->sample_format)/8;
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
103 priv->o_wf.nChannels=in_fmt->nChannels;
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
104 priv->o_wf.nSamplesPerSec=in_fmt->nSamplesPerSec;
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
105 priv->o_wf.nBlockAlign=sh_audio->samplesize*in_fmt->nChannels;
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
106 priv->o_wf.nAvgBytesPerSec=in_fmt->nBlockAlign*in_fmt->nChannels;
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
107 priv->o_wf.wFormatTag=0x01;
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
108 priv->o_wf.wBitsPerSample=in_fmt->wBitsPerSample;
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
109 priv->o_wf.cbSize=0;
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
110
17932
3fe3b2b3a6ce Convert all if(verbose>X) to mp_msg_test calls.
diego
parents: 17012
diff changeset
111 if( mp_msg_test(MSGT_DECAUDIO,MSGL_V) )
14276
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
112 {
17932
3fe3b2b3a6ce Convert all if(verbose>X) to mp_msg_test calls.
diego
parents: 17012
diff changeset
113 mp_msg(MSGT_DECAUDIO, MSGL_V, "Input format:\n");
17977
f70772d02eaa Convert printfs in aviprint.c to mp_msg and give the information printing
diego
parents: 17932
diff changeset
114 print_wave_header(in_fmt, MSGL_V);
17932
3fe3b2b3a6ce Convert all if(verbose>X) to mp_msg_test calls.
diego
parents: 17012
diff changeset
115 mp_msg(MSGT_DECAUDIO, MSGL_V, "Output fmt:\n");
17977
f70772d02eaa Convert printfs in aviprint.c to mp_msg and give the information printing
diego
parents: 17932
diff changeset
116 print_wave_header(&priv->o_wf, MSGL_V);
14276
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
117 }
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
118 memcpy(&priv->hi,&in_fmt[1],sizeof(headerInfo));
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
119 if((ver=TvqInitialize(&priv->hi,&priv->index,0))){
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
120 const char *tvqe[]={
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
121 "No errors",
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
122 "General error",
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
123 "Wrong version",
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
124 "Channel setting error",
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
125 "Wrong coding mode",
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
126 "Inner parameter setting error",
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
127 "Wrong number of VQ pre-selection candidates, used only in encoder" };
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
128 mp_msg(MSGT_DECAUDIO, MSGL_ERR, "Tvq initialization error: %s\n",ver>=0&&ver<7?tvqe[ver]:"Unknown");
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
129 return 0;
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
130 }
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
131 ver=TvqCheckVersion(priv->hi.ID);
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
132 if(ver==TVQ_UNKNOWN_VERSION){
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
133 mp_msg(MSGT_DECAUDIO, MSGL_ERR, "Tvq unknown version of stream\n" );
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
134 return 0;
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
135 }
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
136 TvqGetConfInfo(&priv->cf);
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
137 TvqGetVectorInfo(priv->bits_0,priv->bits_1);
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
138 priv->framesize=TvqGetFrameSize();
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
139 sh_audio->audio_in_minsize=priv->framesize*in_fmt->nChannels;
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
140 sh_audio->a_in_buffer_size=4*sh_audio->audio_in_minsize;
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
141 sh_audio->a_in_buffer=malloc(sh_audio->a_in_buffer_size);
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
142 sh_audio->a_in_buffer_len=0;
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
143
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
144
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
145 return 1;
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
146 }
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
147
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
148 static int close_vqf_audio_codec(sh_audio_t *sh_audio)
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
149 {
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
150 vqf_priv_t*priv=sh_audio->context;
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
151 TvqTerminate(&priv->index);
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
152 return 1;
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
153 }
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
154
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
155 int init(sh_audio_t *sh_audio)
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
156 {
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
157 return 1;
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
158 }
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
159
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
160 int preinit(sh_audio_t *sh_audio)
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
161 {
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
162 /* Win32 VQF audio codec: */
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
163 vqf_priv_t *priv;
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
164 if(!(sh_audio->context=malloc(sizeof(vqf_priv_t)))) return 0;
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
165 priv=sh_audio->context;
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
166 if(!load_dll(sh_audio->codec->dll))
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
167 {
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
168 mp_msg(MSGT_DECAUDIO, MSGL_ERR, "win32.dll looks broken :(\n");
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
169 return 0;
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
170 }
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
171 if(!init_vqf_audio_codec(sh_audio)){
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
172 mp_msg(MSGT_DECAUDIO, MSGL_ERR, "TWinVQ initialization fail\n");
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
173 return 0;
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
174 }
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
175 mp_msg(MSGT_DECAUDIO, MSGL_INFO, "INFO: TWinVQ (%s) audio codec init OK!\n",sh_audio->codec->dll);
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
176 priv->skip_cnt = 2;
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
177 return 1;
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
178 }
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
179
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
180 void uninit(sh_audio_t *sh)
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
181 {
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
182 close_vqf_audio_codec(sh);
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
183 free(sh->context);
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
184 FreeLibrary(vqf_dll);
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
185 }
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
186
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
187 int control(sh_audio_t *sh_audio,int cmd,void* arg, ...)
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
188 {
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
189 switch(cmd) {
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
190 case ADCTRL_QUERY_FORMAT:
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
191 return CONTROL_TRUE;
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
192 default:
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
193 return CONTROL_UNKNOWN;
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
194 }
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
195 }
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
196
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
197 static int bread(char *data, /* Output: Output data array */
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
198 int size, /* Input: Length of each data */
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
199 int nbits, /* Input: Number of bits to write */
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
200 sh_audio_t *sh) /* Input: File pointer */
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
201 {
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
202 /*--- Variables ---*/
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
203 int ibits, iptr, idata, ibufadr, ibufbit, icl;
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
204 unsigned char mask, tmpdat;
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
205 int retval;
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
206 vqf_priv_t *priv=sh->context;
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
207
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
208 /*--- Main operation ---*/
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
209 retval = 0;
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
210 mask = 0x1;
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
211 for ( ibits=0; ibits<nbits; ibits++ ){
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
212 if ( priv->readable == 0 ){ /* when the file data buffer is empty */
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
213 priv->nbuf = demux_read_data(sh->ds, priv->buf, BBUFSIZ);
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
214 priv->nbuf *= 8;
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
215 priv->readable = 1;
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
216 }
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
217 iptr = priv->ptr; /* current file data buffer pointer */
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
218 if ( iptr >= priv->nbuf ) /* If data file is empty then return */
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
219 return(retval);
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
220 ibufadr = iptr/BYTE_BIT; /* current file data buffer address */
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
221 ibufbit = iptr%BYTE_BIT; /* current file data buffer bit */
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
222 /* tmpdat = stream->buf[ibufadr] >> (BYTE_BIT-ibufbit-1); */
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
223 tmpdat = (unsigned char)priv->buf[ibufadr];
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
224 tmpdat >>= (BYTE_BIT-ibufbit-1);
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
225 /* current data bit */
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
226
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
227 idata = ibits*size; /* output data address */
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
228 data[idata] = (char)(tmpdat & mask); /* set output data */
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
229 for (icl=1; icl<size; icl++)
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
230 data[idata+icl] = 0; /* clear the rest output data buffer */
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
231 priv->ptr += 1; /* update data buffer pointer */
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
232 if (priv->ptr == BBUFLEN){
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
233 priv->ptr = 0;
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
234 priv->readable = 0;
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
235 }
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
236 ++retval;
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
237 }
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
238 return(retval);
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
239 }
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
240
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
241 #define BITS_INT (sizeof(int)*8)
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
242
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
243 static int get_bstm(int *data, /* Input: input data */
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
244 unsigned nbits, /* Input: number of bits */
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
245 sh_audio_t *sh) /* Input: bit file pointer */
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
246 {
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
247 unsigned ibit;
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
248 unsigned mask;
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
249 unsigned work;
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
250 char tmpbit[BITS_INT];
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
251 int retval;
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
252
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
253 if ( nbits > BITS_INT ){
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
254 mp_msg(MSGT_DECAUDIO, MSGL_ERR, "get_bstm(): %d: %d Error.\n",
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
255 nbits, BITS_INT);
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
256 exit(1);
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
257 }
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
258 retval = bread(tmpbit, sizeof(*tmpbit), nbits, sh);
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
259 for (ibit=retval; ibit<nbits; ibit++){
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
260 tmpbit[ibit] = 0;
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
261 }
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
262 mask = 0x1<<(nbits-1);
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
263 work=0;
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
264 for ( ibit=0; ibit<nbits; ibit++ ){
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
265 work += mask*tmpbit[ibit];
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
266 mask >>= 1;
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
267 }
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
268 *data = work;
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
269 return(retval);
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
270 }
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
271
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
272 static int GetVqInfo( tvqConfInfoSubBlock *cfg,
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
273 int bits0[],
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
274 int bits1[],
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
275 int variableBits,
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
276 INDEX *index,
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
277 sh_audio_t *sh)
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
278 {
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
279 int idiv;
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
280 int bitcount = 0;
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
281
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
282 if ( index->btype == BLK_LONG ){
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
283 TvqUpdateVectorInfo( variableBits, &cfg->ndiv, bits0, bits1 ); // re-calculate VQ bits
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
284 }
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
285 for ( idiv=0; idiv<cfg->ndiv; idiv++ ){
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
286 bitcount += get_bstm(&index->wvq[idiv],bits0[idiv],sh); /* CB 0 */
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
287 bitcount += get_bstm(&index->wvq[idiv+cfg->ndiv],bits1[idiv],sh); /* CB 1 */
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
288 }
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
289 return bitcount;
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
290 }
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
291
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
292 static int GetBseInfo( tvqConfInfo *cf, tvqConfInfoSubBlock *cfg, INDEX *index, sh_audio_t *sh)
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
293 {
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
294 int i_sup, isf, itmp, idiv;
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
295 int bitcount = 0;
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
296
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
297 for ( i_sup=0; i_sup<cf->N_CH; i_sup++ ){
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
298 for ( isf=0; isf<cfg->nsf; isf++ ){
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
299 for ( idiv=0; idiv<cfg->fw_ndiv; idiv++ ){
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
300 itmp = idiv + ( isf + i_sup * cfg->nsf ) * cfg->fw_ndiv;
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
301 bitcount += get_bstm(&index->fw[itmp],cfg->fw_nbit,sh);
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
302 }
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
303 }
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
304 }
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
305 for ( i_sup=0; i_sup<cf->N_CH; i_sup++ ){
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
306 for ( isf=0; isf<cfg->nsf; isf++ ){
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
307 bitcount += get_bstm(&index->fw_alf[i_sup * cfg->nsf + isf],cf->FW_ARSW_BITS,sh);
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
308 }
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
309 }
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
310 return bitcount;
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
311 }
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
312
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
313 static int GetGainInfo(tvqConfInfo *cf, tvqConfInfoSubBlock *cfg, INDEX *index, sh_audio_t *sh )
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
314 {
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
315 int i_sup, iptop, isf;
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
316 int bitcount = 0;
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
317
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
318 for ( i_sup=0; i_sup<cf->N_CH; i_sup++ ){
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
319 iptop = ( cfg->nsubg + 1 ) * i_sup;
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
320 bitcount += get_bstm(&index->pow[iptop], cf->GAIN_BITS,sh);
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
321 for ( isf=0; isf<cfg->nsubg; isf++ ){
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
322 bitcount += get_bstm(&index->pow[iptop+isf+1], cf->SUB_GAIN_BITS,sh);
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
323 }
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
324 }
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
325 return bitcount;
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
326 }
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
327
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
328 static int GetLspInfo( tvqConfInfo *cf, INDEX *index, sh_audio_t *sh )
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
329 {
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
330 int i_sup, itmp;
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
331 int bitcount = 0;
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
332
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
333 for ( i_sup=0; i_sup<cf->N_CH; i_sup++ ){
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
334 bitcount += get_bstm(&index->lsp[i_sup][0], cf->LSP_BIT0,sh); /* pred. switch */
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
335 bitcount += get_bstm(&index->lsp[i_sup][1], cf->LSP_BIT1,sh); /* first stage */
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
336 for ( itmp=0; itmp<cf->LSP_SPLIT; itmp++ ){ /* second stage */
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
337 bitcount += get_bstm(&index->lsp[i_sup][itmp+2], cf->LSP_BIT2,sh);
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
338 }
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
339 }
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
340
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
341 return bitcount;
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
342 }
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
343
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
344 static int GetPpcInfo( tvqConfInfo *cf, INDEX *index, sh_audio_t *sh)
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
345 {
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
346 int idiv, i_sup;
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
347 int bitcount = 0;
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
348 vqf_priv_t*priv=sh->context;
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
349
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
350 for ( idiv=0; idiv<cf->N_DIV_P; idiv++ ){
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
351 bitcount += get_bstm(&(index->pls[idiv]), priv->bits_0[BLK_PPC][idiv],sh); /*CB0*/
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
352 bitcount += get_bstm(&(index->pls[idiv+cf->N_DIV_P]), priv->bits_1[BLK_PPC][idiv],sh);/*CB1*/
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
353 }
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
354 for (i_sup=0; i_sup<cf->N_CH; i_sup++){
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
355 bitcount += get_bstm(&(index->pit[i_sup]), cf->BASF_BIT,sh);
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
356 bitcount += get_bstm(&(index->pgain[i_sup]), cf->PGAIN_BIT,sh);
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
357 }
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
358
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
359 return bitcount;
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
360 }
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
361
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
362 static int GetEbcInfo( tvqConfInfo *cf, tvqConfInfoSubBlock *cfg, INDEX *index, sh_audio_t *sh)
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
363 {
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
364 int i_sup, isf, itmp;
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
365 int bitcount = 0;
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
366
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
367 for ( i_sup=0; i_sup<cf->N_CH; i_sup++ ){
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
368 for ( isf=0; isf<cfg->nsf; isf++){
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
369 int indexSfOffset = isf * ( cfg->ncrb - cfg->ebc_crb_base ) - cfg->ebc_crb_base;
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
370 for ( itmp=cfg->ebc_crb_base; itmp<cfg->ncrb; itmp++ ){
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
371 bitcount += get_bstm(&index->bc[i_sup][itmp+indexSfOffset], cfg->ebc_bits,sh);
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
372 }
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
373 }
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
374 }
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
375
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
376 return bitcount;
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
377 }
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
378
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
379 static int vqf_read_frame(sh_audio_t *sh,INDEX *index)
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
380 {
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
381 /*--- Variables ---*/
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
382 tvqConfInfoSubBlock *cfg;
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
383 int variableBits;
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
384 int bitcount;
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
385 int numFixedBitsPerFrame = TvqGetNumFixedBitsPerFrame();
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
386 int btype;
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
387 vqf_priv_t *priv=sh->context;
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
388
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
389 /*--- Initialization ---*/
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
390 variableBits = 0;
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
391 bitcount = 0;
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
392
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
393 /*--- read block independent factors ---*/
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
394 /* Window type */
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
395 bitcount += get_bstm( &index->w_type, priv->cf.BITS_WTYPE, sh );
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
396 if ( TvqWtypeToBtype( index->w_type, &index->btype ) ) {
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
397 mp_msg(MSGT_DECAUDIO, MSGL_ERR, "Error: unknown window type: %d\n", index->w_type);
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
398 return 0;
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
399 }
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
400 btype = index->btype;
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
401
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
402 /*--- read block dependent factors ---*/
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
403 cfg = &priv->cf.cfg[btype]; // set the block dependent paremeters table
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
404
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
405 bitcount += variableBits;
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
406
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
407 /* Interleaved vector quantization */
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
408 bitcount += GetVqInfo( cfg, priv->bits_0[btype], priv->bits_1[btype], variableBits, index, sh );
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
409
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
410 /* Bark-scale envelope */
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
411 bitcount += GetBseInfo( &priv->cf, cfg, index, sh );
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
412 /* Gain */
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
413 bitcount += GetGainInfo( &priv->cf, cfg, index, sh );
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
414 /* LSP */
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
415 bitcount += GetLspInfo( &priv->cf, index, sh );
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
416 /* PPC */
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
417 if ( cfg->ppc_enable ){
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
418 bitcount += GetPpcInfo( &priv->cf, index, sh );
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
419 }
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
420 /* Energy Balance Calibration */
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
421 if ( cfg->ebc_enable ){
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
422 bitcount += GetEbcInfo( &priv->cf, cfg, index, sh );
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
423 }
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
424
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
425 return bitcount == numFixedBitsPerFrame ? bitcount/8 : 0;
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
426 }
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
427
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
428 static void frtobuf_s16(float out[], /* Input --- input data frame */
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
429 short bufout[], /* Output --- output data buffer array */
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
430 unsigned frameSize, /* Input --- frame size */
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
431 unsigned numChannels) /* Input --- number of channels */
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
432 {
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
433 /*--- Variables ---*/
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
434 unsigned ismp, ich;
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
435 float *ptr;
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
436 float dtmp;
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
437
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
438 for ( ich=0; ich<numChannels; ich++ ){
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
439 ptr = out+ich*frameSize;
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
440 for ( ismp=0; ismp<frameSize; ismp++ ){
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
441 dtmp = ptr[ismp];
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
442 if ( dtmp >= 0. ) {
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
443 if ( dtmp > 32700. )
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
444 dtmp = 32700.;
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
445 bufout[ismp*numChannels+ich] = (short)(dtmp+0.5);
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
446 } else {
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
447 if ( dtmp < -32700. )
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
448 dtmp = -32700.;
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
449 bufout[ismp*numChannels+ich] = (short)(dtmp-0.5);
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
450 }
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
451 }
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
452 }
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
453 }
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
454
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
455 static void frtobuf_float(float out[], /* Input --- input data frame */
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
456 float bufout[], /* Output --- output data buffer array */
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
457 unsigned frameSize, /* Input --- frame size */
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
458 unsigned numChannels) /* Input --- number of channels */
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
459 {
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
460 /*--- Variables ---*/
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
461 unsigned ismp, ich;
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
462 float *ptr;
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
463 float dtmp;
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
464
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
465 for ( ich=0; ich<numChannels; ich++ ){
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
466 ptr = out+ich*frameSize;
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
467 for ( ismp=0; ismp<frameSize; ismp++ ){
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
468 dtmp = ptr[ismp];
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
469 if ( dtmp >= 0. ) {
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
470 if ( dtmp > 32700. )
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
471 dtmp = 32700.;
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
472 bufout[ismp*numChannels+ich] = dtmp/32767.;
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
473 } else {
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
474 if ( dtmp < -32700. )
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
475 dtmp = -32700.;
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
476 bufout[ismp*numChannels+ich] = dtmp/32767.;
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
477 }
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
478 }
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
479 }
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
480 }
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
481
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
482 int decode_audio(sh_audio_t *sh_audio,unsigned char *buf,int minlen,int maxlen)
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
483 {
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
484 int l, len=0;
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
485 vqf_priv_t *priv=sh_audio->context;
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
486 while(len<minlen)
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
487 {
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
488 float out[priv->framesize*sh_audio->channels];
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
489 l=vqf_read_frame(sh_audio,&priv->index);
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
490 if(!l) break;
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
491 TvqDecodeFrame(&priv->index, out);
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
492 if (priv->skip_cnt) {
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
493 // Ingnore first two frames, replace them with silence
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
494 priv->skip_cnt--;
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
495 memset(buf, 0, priv->framesize*sh_audio->channels*sh_audio->samplesize);
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
496 } else {
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
497 if (sh_audio->sample_format == AF_FORMAT_S16_NE)
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
498 frtobuf_s16(out, (short *)buf, priv->framesize, sh_audio->channels);
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
499 else
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
500 frtobuf_float(out, (float *)buf, priv->framesize, sh_audio->channels);
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
501 }
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
502 len += priv->framesize*sh_audio->channels*sh_audio->samplesize;
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
503 buf += priv->framesize*sh_audio->channels*sh_audio->samplesize;
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
504 }
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
505 return len;
fa80ed52127b TwinVQ decoder and demuxer
rtognimp
parents:
diff changeset
506 }