summaryrefslogtreecommitdiffstats
path: root/dll_init.c
blob: 87a3b493c22bfc3b9ab3f8bed05e39cde888f171 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414

#include "config.h"

#include <stdio.h>
#ifdef HAVE_MALLOC_H
#include <malloc.h>
#endif
#include <stdlib.h>
#include <unistd.h>

#include "mp_msg.h"
#include "help_mp.h"

#include "stream.h"
#include "demuxer.h"

#include "loader.h"
//#include "wine/mmreg.h"
#include "wine/vfw.h"
#include "wine/avifmt.h"

#include "codec-cfg.h"
#include "stheader.h"

#include "dll_init.h"

#ifdef USE_LIBVO2
#include "libvo2/img_format.h"
#else
#include "libvo/img_format.h"
#endif
#include "linux/shmem.h"

extern int divx_quality;

// ACM audio and VfW video codecs initialization
// based on the avifile library [http://divx.euro.ru]

int init_acm_audio_codec(sh_audio_t *sh_audio){
    HRESULT ret;
    WAVEFORMATEX *in_fmt=sh_audio->wf;
    unsigned long srcsize=0;

    mp_msg(MSGT_WIN32,MSGL_V,"======= Win32 (ACM) AUDIO Codec init =======\n");

    sh_audio->srcstream=NULL;

    sh_audio->o_wf.nChannels=in_fmt->nChannels;
    sh_audio->o_wf.nSamplesPerSec=in_fmt->nSamplesPerSec;
    sh_audio->o_wf.nAvgBytesPerSec=2*sh_audio->o_wf.nSamplesPerSec*sh_audio->o_wf.nChannels;
    sh_audio->o_wf.wFormatTag=WAVE_FORMAT_PCM;
    sh_audio->o_wf.nBlockAlign=2*in_fmt->nChannels;
    sh_audio->o_wf.wBitsPerSample=16;
//    sh_audio->o_wf.wBitsPerSample=in_fmt->wBitsPerSample;
    sh_audio->o_wf.cbSize=0;

    if(verbose) {
	printf("Input format:\n");
	    printf("  wFormatTag %d\n", in_fmt->wFormatTag);
	    printf("  nChannels %d\n", in_fmt->nChannels);
	    printf("  nSamplesPerSec %ld\n", in_fmt->nSamplesPerSec);
	    printf("  nAvgBytesPerSec %d\n", in_fmt->nAvgBytesPerSec);
	    printf("  nBlockAlign %d\n", in_fmt->nBlockAlign);
	    printf("  wBitsPerSample %d\n", in_fmt->wBitsPerSample);
	    printf("  cbSize %d\n", in_fmt->cbSize);
	printf("Output fmt:\n");
	    printf("  wFormatTag %d\n", sh_audio->o_wf.wFormatTag);
	    printf("  nChannels %d\n", sh_audio->o_wf.nChannels);
	    printf("  nSamplesPerSec %ld\n", sh_audio->o_wf.nSamplesPerSec);
	    printf("  nAvgBytesPerSec %d\n", sh_audio->o_wf.nAvgBytesPerSec);
	    printf("  nBlockAlign %d\n", sh_audio->o_wf.nBlockAlign);
	    printf("  wBitsPerSample %d\n", sh_audio->o_wf.wBitsPerSample);
	    printf("  cbSize %d\n", sh_audio->o_wf.cbSize);
    }


    win32_codec_name = sh_audio->codec->dll;
    ret=acmStreamOpen(&sh_audio->srcstream,(HACMDRIVER)NULL,
                    in_fmt,&sh_audio->o_wf,
		    NULL,0,0,0);
    if(ret){
        if(ret==ACMERR_NOTPOSSIBLE)
            mp_msg(MSGT_WIN32,MSGL_ERR,"ACM_Decoder: Unappropriate audio format\n");
        else
            mp_msg(MSGT_WIN32,MSGL_ERR,"ACM_Decoder: acmStreamOpen error: %d", (int)ret);
        sh_audio->srcstream=NULL;
        return 0;
    }
    mp_msg(MSGT_WIN32,MSGL_V,"Audio codec opened OK! ;-)\n");

    acmStreamSize(sh_audio->srcstream, in_fmt->nBlockAlign, &srcsize, ACM_STREAMSIZEF_SOURCE);
    //if(verbose) printf("Audio ACM output buffer min. size: %ld (reported by codec)\n",srcsize);
    srcsize*=2;
    //if(srcsize<MAX_OUTBURST) srcsize=MAX_OUTBURST;
    if(!srcsize){
        mp_msg(MSGT_WIN32,MSGL_WARN,"Warning! ACM codec reports srcsize=0\n");
        srcsize=16384;
    }
    // limit srcsize to 4-16kb
    //while(srcsize && srcsize<4096) srcsize*=2;
    //while(srcsize>16384) srcsize/=2;
    sh_audio->audio_out_minsize=srcsize; // audio output min. size
    mp_msg(MSGT_WIN32,MSGL_V,"Audio ACM output buffer min. size: %ld\n",srcsize);

    acmStreamSize(sh_audio->srcstream, srcsize, &srcsize, ACM_STREAMSIZEF_DESTINATION);
    sh_audio->audio_in_minsize=srcsize; // audio input min. size
    mp_msg(MSGT_WIN32,MSGL_V,"Audio ACM input buffer min. size: %ld\n",srcsize);
    
    if(srcsize<in_fmt->nBlockAlign) srcsize=in_fmt->nBlockAlign;

    sh_audio->a_in_buffer_size=2*sh_audio->audio_in_minsize;
    sh_audio->a_in_buffer=malloc(sh_audio->a_in_buffer_size);
    sh_audio->a_in_buffer_len=0;

    return 1;
}

int acm_decode_audio(sh_audio_t *sh_audio, void* a_buffer,int minlen,int maxlen){
        ACMSTREAMHEADER ash;
        HRESULT hr;
        DWORD srcsize=0;
        DWORD len=minlen;
        acmStreamSize(sh_audio->srcstream,len , &srcsize, ACM_STREAMSIZEF_DESTINATION);
        mp_msg(MSGT_WIN32,MSGL_DBG3,"acm says: srcsize=%ld  (buffsize=%d)  out_size=%d\n",srcsize,sh_audio->a_in_buffer_size,len);

        if(srcsize<sh_audio->wf->nBlockAlign){
           srcsize=sh_audio->wf->nBlockAlign;
           acmStreamSize(sh_audio->srcstream, srcsize, &len, ACM_STREAMSIZEF_SOURCE);
           if(len>maxlen) len=maxlen;
        }

//        if(srcsize==0) srcsize=((WAVEFORMATEX *)&sh_audio->o_wf_ext)->nBlockAlign;
        if(srcsize>sh_audio->a_in_buffer_size) srcsize=sh_audio->a_in_buffer_size; // !!!!!!
        if(sh_audio->a_in_buffer_len<srcsize){
          sh_audio->a_in_buffer_len+=
            demux_read_data(sh_audio->ds,&sh_audio->a_in_buffer[sh_audio->a_in_buffer_len],
            srcsize-sh_audio->a_in_buffer_len);
        }
        mp_msg(MSGT_WIN32,MSGL_DBG3,"acm convert %d -> %d bytes\n",sh_audio->a_in_buffer_len,len);
        memset(&ash, 0, sizeof(ash));
        ash.cbStruct=sizeof(ash);
        ash.fdwStatus=0;
        ash.dwUser=0; 
        ash.pbSrc=sh_audio->a_in_buffer;
        ash.cbSrcLength=sh_audio->a_in_buffer_len;
        ash.pbDst=a_buffer;
        ash.cbDstLength=len;
        hr=acmStreamPrepareHeader(sh_audio->srcstream,&ash,0);
        if(hr){
          mp_msg(MSGT_WIN32,MSGL_V,"ACM_Decoder: acmStreamPrepareHeader error %d\n",(int)hr);
					return -1;
        }
        hr=acmStreamConvert(sh_audio->srcstream,&ash,0);
        if(hr){
          mp_msg(MSGT_WIN32,MSGL_DBG2,"ACM_Decoder: acmStreamConvert error %d\n",(int)hr);
          switch(hr)
	  {
	    case ACMERR_NOTPOSSIBLE:
	    case ACMERR_UNPREPARED:
		mp_msg(MSGT_WIN32, MSGL_DBG2, "ACM_Decoder: acmStreamConvert error: probarly not initialized!\n");
	  }  
//					return -1;
        }
        if(verbose>1)
          mp_msg(MSGT_WIN32,MSGL_DBG2,"acm converted %d -> %d\n",ash.cbSrcLengthUsed,ash.cbDstLengthUsed);
        if(ash.cbSrcLengthUsed>=sh_audio->a_in_buffer_len){
          sh_audio->a_in_buffer_len=0;
        } else {
          sh_audio->a_in_buffer_len-=ash.cbSrcLengthUsed;
          memcpy(sh_audio->a_in_buffer,&sh_audio->a_in_buffer[ash.cbSrcLengthUsed],sh_audio->a_in_buffer_len);
        }
        len=ash.cbDstLengthUsed;
        hr=acmStreamUnprepareHeader(sh_audio->srcstream,&ash,0);
        if(hr){
          mp_msg(MSGT_WIN32,MSGL_V,"ACM_Decoder: acmStreamUnprepareHeader error %d\n",(int)hr);
        }
        return len;
}

int init_vfw_video_codec(sh_video_t *sh_video,int ex){
  HRESULT ret;
  int yuv=0;
  unsigned int outfmt=sh_video->codec->outfmt[sh_video->outfmtidx];
  char *temp;
  int temp_len;
  int i;

  mp_msg(MSGT_WIN32,MSGL_V,"======= Win32 (VFW) VIDEO Codec init =======\n");

  memset(&sh_video->o_bih, 0, sizeof(BITMAPINFOHEADER));
  sh_video->o_bih.biSize = sizeof(BITMAPINFOHEADER);

  win32_codec_name = sh_video->codec->dll;
//  sh_video->hic = ICOpen( 0x63646976, sh_video->bih->biCompression, ICMODE_FASTDECOMPRESS);
  sh_video->hic = ICOpen( 0x63646976, sh_video->bih->biCompression, ICMODE_DECOMPRESS);
  if(!sh_video->hic){
    mp_msg(MSGT_WIN32,MSGL_ERR,"ICOpen failed! unknown codec / wrong parameters?\n");
    return 0;
  }

//  sh_video->bih->biBitCount=32;

  temp_len = ICDecompressGetFormatSize(sh_video->hic, sh_video->bih);

  if (temp_len < sh_video->o_bih.biSize)
    temp_len = sh_video->o_bih.biSize;

  temp = malloc(temp_len);
  printf("ICDecompressGetFormatSize ret: %d\n", temp_len);

#if 0
{
  ICINFO icinfo;
  ret = ICGetInfo(sh_video->hic, &icinfo, sizeof(ICINFO));
  printf("%d - %d - %d\n", ret, icinfo.dwSize, sizeof(ICINFO));
printf("Compressor type: %.4x\n", icinfo.fccType);
printf("Compressor subtype: %.4x\n", icinfo.fccHandler);
printf("Compressor flags: %lu, version %lu, ICM version: %lu\n",
    icinfo.dwFlags, icinfo.dwVersion, icinfo.dwVersionICM);
printf("Compressor name: %s\n", icinfo.szName);
printf("Compressor description: %s\n", icinfo.szDescription);

printf("Flags:");
if (icinfo.dwFlags & VIDCF_QUALITY)
    printf(" quality");
if (icinfo.dwFlags & VIDCF_FASTTEMPORALD)
    printf(" fast-decompr");
if (icinfo.dwFlags & VIDCF_QUALITYTIME)
    printf(" temp-quality");
printf("\n");
}
#endif

  // Note: DivX.DLL overwrites 4 bytes _AFTER_ the o_bih header, so it corrupts
  // the sh_video struct content. We call it with an 1024-byte temp space and
  // then copy out the data we need:
  memset(temp,0x77,temp_len);
//  memcpy(temp,sh_video->bih,sizeof(BITMAPINFOHEADER));
//  sh_video->o_bih.biSize = temp_len;

  ret = ICDecompressGetFormat(sh_video->hic, sh_video->bih, temp);
  if(ret < 0){
    mp_msg(MSGT_WIN32,MSGL_ERR,"ICDecompressGetFormat failed: Error %d\n", (int)ret);
    for (i=0; i < temp_len; i++) mp_msg(MSGT_WIN32, MSGL_DBG2, "%02x ", temp[i]);
    return 0;
  }
  mp_msg(MSGT_WIN32,MSGL_V,"ICDecompressGetFormat OK\n");
  
  memcpy(&sh_video->o_bih,temp,sizeof(sh_video->o_bih));

  if (temp_len > sizeof(sh_video->o_bih))
  {
    mp_msg(MSGT_WIN32, MSGL_V, "Extra info in o_bih (%d bytes)!\n",
	temp_len-sizeof(sh_video->o_bih));
    for(i=sizeof(sh_video->o_bih);i<temp_len;i++) mp_msg(MSGT_WIN32, MSGL_DBG2, "%02X ",temp[i]);
  }
  free(temp);
//  printf("ICM_DECOMPRESS_QUERY=0x%X",ICM_DECOMPRESS_QUERY);

//  sh_video->o_bih.biWidth=sh_video->bih.biWidth;
//  sh_video->o_bih.biCompression = 0x32315659; //  mmioFOURCC('U','Y','V','Y');
//  ret=ICDecompressGetFormatSize(sh_video->hic,&sh_video->o_bih);
//  sh_video->o_bih.biCompression = 3; //0x32315659;
//  sh_video->o_bih.biCompression = mmioFOURCC('U','Y','V','Y');
//  sh_video->o_bih.biCompression = mmioFOURCC('U','Y','V','Y');
//  sh_video->o_bih.biCompression = mmioFOURCC('Y','U','Y','2');
//  sh_video->o_bih.biPlanes=3;
//  sh_video->o_bih.biBitCount=16;

#if 0
  // workaround for pegasus MJPEG:
  if(!sh_video->o_bih.biWidth) sh_video->o_bih.biWidth=sh_video->bih->biWidth;
  if(!sh_video->o_bih.biHeight) sh_video->o_bih.biHeight=sh_video->bih->biHeight;
  if(!sh_video->o_bih.biPlanes) sh_video->o_bih.biPlanes=sh_video->bih->biPlanes;
#endif

  switch (outfmt) {

/* planar format */
  case IMGFMT_YV12:
  case IMGFMT_I420:
  case IMGFMT_IYUV:
      sh_video->o_bih.biBitCount=12;
      yuv=1;
      break;

/* packed format */
  case IMGFMT_YUY2:
  case IMGFMT_UYVY:
  case IMGFMT_YVYU:
      sh_video->o_bih.biBitCount=16;
      yuv=1;
      break;

/* rgb/bgr format */
  case IMGFMT_RGB8:
  case IMGFMT_BGR8:
      sh_video->o_bih.biBitCount=8;
      break;

  case IMGFMT_RGB15:
  case IMGFMT_RGB16:
  case IMGFMT_BGR15:
  case IMGFMT_BGR16:
      sh_video->o_bih.biBitCount=16;
      break;

  case IMGFMT_RGB24:
  case IMGFMT_BGR24:
      sh_video->o_bih.biBitCount=24;
      break;

  case IMGFMT_RGB32:
  case IMGFMT_BGR32:
      sh_video->o_bih.biBitCount=32;
      break;

  default:
      mp_msg(MSGT_WIN32,MSGL_ERR,"unsupported image format: 0x%x\n", outfmt);
      return 0;
  }

  sh_video->o_bih.biSizeImage = sh_video->o_bih.biWidth * sh_video->o_bih.biHeight * (sh_video->o_bih.biBitCount/8);
  
  if(!(sh_video->codec->outflags[sh_video->outfmtidx]&CODECS_FLAG_FLIP)) {
      sh_video->o_bih.biHeight=-sh_video->bih->biHeight; // flip image!
  }

  if(yuv && !(sh_video->codec->outflags[sh_video->outfmtidx] & CODECS_FLAG_YUVHACK))
	 sh_video->o_bih.biCompression = outfmt;
  else
         sh_video->o_bih.biCompression = 0;

  if(verbose) {
    printf("Starting decompression, format:\n");
	printf("  biSize %ld\n", sh_video->bih->biSize);
	printf("  biWidth %ld\n", sh_video->bih->biWidth);
	printf("  biHeight %ld\n", sh_video->bih->biHeight);
	printf("  biPlanes %d\n", sh_video->bih->biPlanes);
	printf("  biBitCount %d\n", sh_video->bih->biBitCount);
	printf("  biCompression 0x%lx ('%.4s')\n", sh_video->bih->biCompression, (char *)&sh_video->bih->biCompression);
	printf("  biSizeImage %ld\n", sh_video->bih->biSizeImage);
    printf("Dest fmt:\n");
	printf("  biSize %ld\n", sh_video->o_bih.biSize);
	printf("  biWidth %ld\n", sh_video->o_bih.biWidth);
	printf("  biHeight %ld\n", sh_video->o_bih.biHeight);
	printf("  biPlanes %d\n", sh_video->o_bih.biPlanes);
	printf("  biBitCount %d\n", sh_video->o_bih.biBitCount);
	printf("  biCompression 0x%lx ('%.4s')\n", sh_video->o_bih.biCompression, (char *)&sh_video->o_bih.biCompression);
	printf("  biSizeImage %ld\n", sh_video->o_bih.biSizeImage);
  }

  ret = ex ?
      ICDecompressQueryEx(sh_video->hic, sh_video->bih, &sh_video->o_bih) :
      ICDecompressQuery(sh_video->hic, sh_video->bih, &sh_video->o_bih);
  if(ret){
    mp_msg(MSGT_WIN32,MSGL_ERR,"ICDecompressQuery failed: Error %d\n", (int)ret);
//    return 0;
  } else
  mp_msg(MSGT_WIN32,MSGL_V,"ICDecompressQuery OK\n");

  ret = ex ?
      ICDecompressBeginEx(sh_video->hic, sh_video->bih, &sh_video->o_bih) :
      ICDecompressBegin(sh_video->hic, sh_video->bih, &sh_video->o_bih);
  if(ret){
    mp_msg(MSGT_WIN32,MSGL_ERR,"ICDecompressBegin failed: Error %d\n", (int)ret);
//    return 0;
  }

  sh_video->our_out_buffer = (char*)memalign(64,sh_video->o_bih.biSizeImage);
  if(!sh_video->our_out_buffer){
    mp_msg(MSGT_WIN32,MSGL_ERR,MSGTR_NoMemForDecodedImage, sh_video->o_bih.biSizeImage);
    return 0;
  }

  if(yuv && sh_video->codec->outflags[sh_video->outfmtidx] & CODECS_FLAG_YUVHACK)
    sh_video->o_bih.biCompression = outfmt;

//  avi_header.our_in_buffer=malloc(avi_header.video.dwSuggestedBufferSize); // FIXME!!!!

  ICSendMessage(sh_video->hic, ICM_USER+80, (long)(&divx_quality) ,NULL);

  mp_msg(MSGT_WIN32,MSGL_V,"VIDEO CODEC Init OK!!! ;-)\n");
  return 1;
}

int vfw_set_postproc(sh_video_t* sh_video,int quality){
  // Works only with opendivx/divx4 based DLL
  return ICSendMessage(sh_video->hic, ICM_USER+80, (long)(&quality) ,NULL);
}

int vfw_decode_video(sh_video_t* sh_video,void* start,int in_size,int drop_frame,int ex){
    HRESULT ret;

    sh_video->bih->biSizeImage = in_size;

    if(ex)
      ret = ICDecompressEx(sh_video->hic, 
	  ( (sh_video->ds->flags&1) ? 0 : ICDECOMPRESS_NOTKEYFRAME ) |
	  ( (drop_frame==2 && !(sh_video->ds->flags&1))?(ICDECOMPRESS_HURRYUP|ICDECOMPRESS_PREROL):0 ) , 
                         sh_video->bih,   start,
                        &sh_video->o_bih,
                        drop_frame ? 0 : sh_video->our_out_buffer);
    else
      ret = ICDecompress(sh_video->hic, 
	  ( (sh_video->ds->flags&1) ? 0 : ICDECOMPRESS_NOTKEYFRAME ) |
	  ( (drop_frame==2 && !(sh_video->ds->flags&1))?(ICDECOMPRESS_HURRYUP|ICDECOMPRESS_PREROL):0 ) , 
                         sh_video->bih,   start,
                        &sh_video->o_bih,
                        drop_frame ? 0 : sh_video->our_out_buffer);

    return (int)ret;
}