core: Set mpctx->chapters to NULL at uninit
[mplayer.git] / libmpcodecs / ad_libdv.c
blobfdfdf6cc6cd2e9644fae4a57cac148321d35465f
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <sys/types.h>
5 #include <unistd.h>
6 #include <math.h>
8 #include "config.h"
9 #include "mp_msg.h"
10 #include "help_mp.h"
12 #include "img_format.h"
14 #include <libdv/dv.h>
15 #include <libdv/dv_types.h>
17 #include "stream/stream.h"
18 #include "libmpdemux/demuxer.h"
19 #include "libmpdemux/stheader.h"
21 #include "ad_internal.h"
23 static const ad_info_t info =
25 "Raw DV Audio Decoder",
26 "libdv",
27 "Alexander Neundorf <neundorf@kde.org>",
28 "http://libdv.sf.net",
32 LIBAD_EXTERN(libdv)
34 // defined in vd_libdv.c:
35 dv_decoder_t* init_global_rawdv_decoder(void);
37 static int preinit(sh_audio_t *sh_audio)
39 sh_audio->audio_out_minsize=4*DV_AUDIO_MAX_SAMPLES*2;
40 return 1;
43 static int16_t *audioBuffers[4]={NULL,NULL,NULL,NULL};
45 static int init(sh_audio_t *sh)
47 int i;
48 WAVEFORMATEX *h=sh->wf;
50 if(!h) return 0;
52 sh->i_bps=h->nAvgBytesPerSec;
53 sh->channels=h->nChannels;
54 sh->samplerate=h->nSamplesPerSec;
55 sh->samplesize=(h->wBitsPerSample+7)/8;
57 sh->context=init_global_rawdv_decoder();
59 for (i=0; i < 4; i++)
60 audioBuffers[i] = malloc(2*DV_AUDIO_MAX_SAMPLES);
62 return 1;
65 static void uninit(sh_audio_t *sh_audio)
67 int i;
68 for (i=0; i < 4; i++)
69 free(audioBuffers[i]);
72 static int control(sh_audio_t *sh,int cmd,void* arg, ...)
74 // TODO!!!
75 return CONTROL_UNKNOWN;
78 static int decode_audio(sh_audio_t *audio, unsigned char *buf, int minlen, int maxlen)
80 int len=0;
81 dv_decoder_t* decoder=audio->context; //global_rawdv_decoder;
82 unsigned char* dv_audio_frame=NULL;
83 int xx=ds_get_packet(audio->ds,&dv_audio_frame);
84 if(xx<=0 || !dv_audio_frame) return 0; // EOF?
86 dv_parse_header(decoder, dv_audio_frame);
88 if(xx!=decoder->frame_size)
89 mp_msg(MSGT_GLOBAL,MSGL_WARN,MSGTR_MPCODECS_AudioFramesizeDiffers,
90 xx, decoder->frame_size);
92 if (dv_decode_full_audio(decoder, dv_audio_frame,(int16_t**) audioBuffers))
94 /* Interleave the audio into a single buffer */
95 int i=0;
96 int16_t *bufP=(int16_t*)buf;
98 // printf("samples=%d/%d chans=%d mem=%d \n",decoder->audio->samples_this_frame,DV_AUDIO_MAX_SAMPLES,
99 // decoder->audio->num_channels, decoder->audio->samples_this_frame*decoder->audio->num_channels*2);
101 // return (44100/30)*4;
103 for (i=0; i < decoder->audio->samples_this_frame; i++)
105 int ch;
106 for (ch=0; ch < decoder->audio->num_channels; ch++)
107 bufP[len++] = audioBuffers[ch][i];
110 return len*2;