annotate libmpcodecs/dec_audio.c @ 26959:bab42914ea63

fix fixes, patch by Benoit Fouet
author gpoirier
date Wed, 04 Jun 2008 20:15:04 +0000
parents afa125da85cf
children 4be2b34aa431
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
5342
c4cf81b3af71 imporetd from MPlayerXP, with small modification
arpi
parents:
diff changeset
1 #include <stdio.h>
c4cf81b3af71 imporetd from MPlayerXP, with small modification
arpi
parents:
diff changeset
2 #include <stdlib.h>
c4cf81b3af71 imporetd from MPlayerXP, with small modification
arpi
parents:
diff changeset
3 #include <unistd.h>
24892
80180dc13565 Change decode_audio() interface
uau
parents: 24886
diff changeset
4 #include <assert.h>
5342
c4cf81b3af71 imporetd from MPlayerXP, with small modification
arpi
parents:
diff changeset
5
c4cf81b3af71 imporetd from MPlayerXP, with small modification
arpi
parents:
diff changeset
6 #include "config.h"
c4cf81b3af71 imporetd from MPlayerXP, with small modification
arpi
parents:
diff changeset
7 #include "mp_msg.h"
c4cf81b3af71 imporetd from MPlayerXP, with small modification
arpi
parents:
diff changeset
8 #include "help_mp.h"
c4cf81b3af71 imporetd from MPlayerXP, with small modification
arpi
parents:
diff changeset
9
22599
4faee1254928 Add explicit location for headers from the stream/ directory.
diego
parents: 22180
diff changeset
10 #include "stream/stream.h"
22601
ed8f90096c65 Add explicit location for headers from the libmpdemux/ directory.
diego
parents: 22599
diff changeset
11 #include "libmpdemux/demuxer.h"
5342
c4cf81b3af71 imporetd from MPlayerXP, with small modification
arpi
parents:
diff changeset
12
c4cf81b3af71 imporetd from MPlayerXP, with small modification
arpi
parents:
diff changeset
13 #include "codec-cfg.h"
22601
ed8f90096c65 Add explicit location for headers from the libmpdemux/ directory.
diego
parents: 22599
diff changeset
14 #include "libmpdemux/stheader.h"
5342
c4cf81b3af71 imporetd from MPlayerXP, with small modification
arpi
parents:
diff changeset
15
c4cf81b3af71 imporetd from MPlayerXP, with small modification
arpi
parents:
diff changeset
16 #include "dec_audio.h"
c4cf81b3af71 imporetd from MPlayerXP, with small modification
arpi
parents:
diff changeset
17 #include "ad.h"
17012
6ff3379a0862 Unify include path handling, -I.. is in CFLAGS.
diego
parents: 16793
diff changeset
18 #include "libaf/af_format.h"
5342
c4cf81b3af71 imporetd from MPlayerXP, with small modification
arpi
parents:
diff changeset
19
17012
6ff3379a0862 Unify include path handling, -I.. is in CFLAGS.
diego
parents: 16793
diff changeset
20 #include "libaf/af.h"
7604
32efb806436e aufio filter layer (libaf) integration to libmpcodecs, mplayer and mencoder
arpi
parents: 7561
diff changeset
21
17635
db13b7295651 memalign segfaults when used without declaration on amd64 (default function return type is int so the pointer was trunctated to 32bits)
aurel
parents: 17625
diff changeset
22 #ifdef HAVE_MALLOC_H
db13b7295651 memalign segfaults when used without declaration on amd64 (default function return type is int so the pointer was trunctated to 32bits)
aurel
parents: 17625
diff changeset
23 #include <malloc.h>
db13b7295651 memalign segfaults when used without declaration on amd64 (default function return type is int so the pointer was trunctated to 32bits)
aurel
parents: 17625
diff changeset
24 #endif
db13b7295651 memalign segfaults when used without declaration on amd64 (default function return type is int so the pointer was trunctated to 32bits)
aurel
parents: 17625
diff changeset
25
8152
4b02f73cb4b9 dlopen() support for ad and vd
alex
parents: 8123
diff changeset
26 #ifdef DYNAMIC_PLUGINS
4b02f73cb4b9 dlopen() support for ad and vd
alex
parents: 8123
diff changeset
27 #include <dlfcn.h>
4b02f73cb4b9 dlopen() support for ad and vd
alex
parents: 8123
diff changeset
28 #endif
4b02f73cb4b9 dlopen() support for ad and vd
alex
parents: 8123
diff changeset
29
5342
c4cf81b3af71 imporetd from MPlayerXP, with small modification
arpi
parents:
diff changeset
30 #ifdef USE_FAKE_MONO
24883
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
31 int fakemono = 0;
5342
c4cf81b3af71 imporetd from MPlayerXP, with small modification
arpi
parents:
diff changeset
32 #endif
24883
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
33
5342
c4cf81b3af71 imporetd from MPlayerXP, with small modification
arpi
parents:
diff changeset
34 /* used for ac3surround decoder - set using -channels option */
c4cf81b3af71 imporetd from MPlayerXP, with small modification
arpi
parents:
diff changeset
35 int audio_output_channels = 2;
24883
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
36 af_cfg_t af_cfg = { 1, NULL }; // Configuration for audio filters
5342
c4cf81b3af71 imporetd from MPlayerXP, with small modification
arpi
parents:
diff changeset
37
24883
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
38 void afm_help(void)
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
39 {
7191
1eadce15446c -afm/-vfm help implemenetd, some cosmetics of ad/vd codec names/comments
arpi
parents: 7180
diff changeset
40 int i;
24883
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
41 mp_msg(MSGT_DECAUDIO, MSGL_INFO, MSGTR_AvailableAudioFm);
18237
4231482179b6 Get ride of the several if(identify) messy lines and rearangment of some of the output, both patches by Kiriuja mplayer-patches AT en-directo_net, his changes are barely unrelated, nevertheless Im commiting them thogeter just for the sake of my mental healt, I had both patches already applied on my local three
reynaldo
parents: 17635
diff changeset
42 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_AUDIO_DRIVERS\n");
24883
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
43 mp_msg(MSGT_DECAUDIO, MSGL_INFO, " afm: info: (comment)\n");
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
44 for (i = 0; mpcodecs_ad_drivers[i] != NULL; i++)
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
45 if (mpcodecs_ad_drivers[i]->info->comment
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
46 && mpcodecs_ad_drivers[i]->info->comment[0])
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
47 mp_msg(MSGT_DECAUDIO, MSGL_INFO, "%9s %s (%s)\n",
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
48 mpcodecs_ad_drivers[i]->info->short_name,
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
49 mpcodecs_ad_drivers[i]->info->name,
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
50 mpcodecs_ad_drivers[i]->info->comment);
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
51 else
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
52 mp_msg(MSGT_DECAUDIO, MSGL_INFO, "%9s %s\n",
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
53 mpcodecs_ad_drivers[i]->info->short_name,
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
54 mpcodecs_ad_drivers[i]->info->name);
7191
1eadce15446c -afm/-vfm help implemenetd, some cosmetics of ad/vd codec names/comments
arpi
parents: 7180
diff changeset
55 }
1eadce15446c -afm/-vfm help implemenetd, some cosmetics of ad/vd codec names/comments
arpi
parents: 7180
diff changeset
56
24885
cd55d31cecb2 dec_audio.c: Make some functions static
uau
parents: 24884
diff changeset
57 static int init_audio_codec(sh_audio_t *sh_audio)
5342
c4cf81b3af71 imporetd from MPlayerXP, with small modification
arpi
parents:
diff changeset
58 {
24883
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
59 if ((af_cfg.force & AF_INIT_FORMAT_MASK) == AF_INIT_FLOAT) {
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
60 int fmt = AF_FORMAT_FLOAT_NE;
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
61 if (sh_audio->ad_driver->control(sh_audio, ADCTRL_QUERY_FORMAT,
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
62 &fmt) == CONTROL_TRUE) {
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
63 sh_audio->sample_format = fmt;
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
64 sh_audio->samplesize = 4;
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
65 }
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
66 }
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
67 if (!sh_audio->ad_driver->preinit(sh_audio)) {
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
68 mp_msg(MSGT_DECAUDIO, MSGL_ERR, MSGTR_ADecoderPreinitFailed);
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
69 return 0;
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
70 }
5342
c4cf81b3af71 imporetd from MPlayerXP, with small modification
arpi
parents:
diff changeset
71
24883
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
72 /* allocate audio in buffer: */
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
73 if (sh_audio->audio_in_minsize > 0) {
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
74 sh_audio->a_in_buffer_size = sh_audio->audio_in_minsize;
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
75 mp_msg(MSGT_DECAUDIO, MSGL_V, MSGTR_AllocatingBytesForInputBuffer,
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
76 sh_audio->a_in_buffer_size);
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
77 sh_audio->a_in_buffer = memalign(16, sh_audio->a_in_buffer_size);
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
78 memset(sh_audio->a_in_buffer, 0, sh_audio->a_in_buffer_size);
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
79 sh_audio->a_in_buffer_len = 0;
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
80 }
5458
b3d1348b251f audio input buffer allocation/free cleanup
arpi
parents: 5342
diff changeset
81
24883
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
82 sh_audio->a_buffer_size = sh_audio->audio_out_minsize + MAX_OUTBURST;
5342
c4cf81b3af71 imporetd from MPlayerXP, with small modification
arpi
parents:
diff changeset
83
24883
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
84 mp_msg(MSGT_DECAUDIO, MSGL_V, MSGTR_AllocatingBytesForOutputBuffer,
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
85 sh_audio->audio_out_minsize, MAX_OUTBURST, sh_audio->a_buffer_size);
5342
c4cf81b3af71 imporetd from MPlayerXP, with small modification
arpi
parents:
diff changeset
86
24883
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
87 sh_audio->a_buffer = memalign(16, sh_audio->a_buffer_size);
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
88 if (!sh_audio->a_buffer) {
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
89 mp_msg(MSGT_DECAUDIO, MSGL_ERR, MSGTR_CantAllocAudioBuf);
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
90 return 0;
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
91 }
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
92 memset(sh_audio->a_buffer, 0, sh_audio->a_buffer_size);
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
93 sh_audio->a_buffer_len = 0;
5342
c4cf81b3af71 imporetd from MPlayerXP, with small modification
arpi
parents:
diff changeset
94
24883
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
95 if (!sh_audio->ad_driver->init(sh_audio)) {
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
96 mp_msg(MSGT_DECAUDIO, MSGL_WARN, MSGTR_ADecoderInitFailed);
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
97 uninit_audio(sh_audio); // free buffers
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
98 return 0;
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
99 }
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
100
25962
afa125da85cf typo fix: inited --> initialized
diego
parents: 25661
diff changeset
101 sh_audio->initialized = 1;
5458
b3d1348b251f audio input buffer allocation/free cleanup
arpi
parents: 5342
diff changeset
102
24883
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
103 if (!sh_audio->channels || !sh_audio->samplerate) {
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
104 mp_msg(MSGT_DECAUDIO, MSGL_WARN, MSGTR_UnknownAudio);
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
105 uninit_audio(sh_audio); // free buffers
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
106 return 0;
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
107 }
5342
c4cf81b3af71 imporetd from MPlayerXP, with small modification
arpi
parents:
diff changeset
108
24883
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
109 if (!sh_audio->o_bps)
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
110 sh_audio->o_bps = sh_audio->channels * sh_audio->samplerate
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
111 * sh_audio->samplesize;
7522
aa1a1249f239 -ac/-afm rewrite, now works the same way as -vc/-vfm
arpi
parents: 7502
diff changeset
112
24883
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
113 mp_msg(MSGT_DECAUDIO, MSGL_INFO,
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
114 "AUDIO: %d Hz, %d ch, %s, %3.1f kbit/%3.2f%% (ratio: %d->%d)\n",
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
115 sh_audio->samplerate, sh_audio->channels,
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
116 af_fmt2str_short(sh_audio->sample_format),
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
117 sh_audio->i_bps * 8 * 0.001,
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
118 ((float) sh_audio->i_bps / sh_audio->o_bps) * 100.0,
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
119 sh_audio->i_bps, sh_audio->o_bps);
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
120 mp_msg(MSGT_IDENTIFY, MSGL_INFO,
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
121 "ID_AUDIO_BITRATE=%d\nID_AUDIO_RATE=%d\n" "ID_AUDIO_NCH=%d\n",
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
122 sh_audio->i_bps * 8, sh_audio->samplerate, sh_audio->channels);
7604
32efb806436e aufio filter layer (libaf) integration to libmpcodecs, mplayer and mencoder
arpi
parents: 7561
diff changeset
123
24884
17c6e0a372f4 Simplify audio buffer allocation logic
uau
parents: 24883
diff changeset
124 sh_audio->a_out_buffer_size = 0;
17c6e0a372f4 Simplify audio buffer allocation logic
uau
parents: 24883
diff changeset
125 sh_audio->a_out_buffer = NULL;
17c6e0a372f4 Simplify audio buffer allocation logic
uau
parents: 24883
diff changeset
126 sh_audio->a_out_buffer_len = 0;
24883
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
127
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
128 return 1;
5342
c4cf81b3af71 imporetd from MPlayerXP, with small modification
arpi
parents:
diff changeset
129 }
c4cf81b3af71 imporetd from MPlayerXP, with small modification
arpi
parents:
diff changeset
130
24885
cd55d31cecb2 dec_audio.c: Make some functions static
uau
parents: 24884
diff changeset
131 static int init_audio(sh_audio_t *sh_audio, char *codecname, char *afm,
25661
293aeec83153 Replace the persistent CODECS_FLAG_SELECTED by a local "stringset" with
reimar
parents: 25254
diff changeset
132 int status, stringset_t *selected)
24883
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
133 {
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
134 unsigned int orig_fourcc = sh_audio->wf ? sh_audio->wf->wFormatTag : 0;
16321
efbfac98cab1 Allow forcing of demuxers and codecs by prepending '+'
reimar
parents: 16306
diff changeset
135 int force = 0;
efbfac98cab1 Allow forcing of demuxers and codecs by prepending '+'
reimar
parents: 16306
diff changeset
136 if (codecname && codecname[0] == '+') {
24883
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
137 codecname = &codecname[1];
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
138 force = 1;
16321
efbfac98cab1 Allow forcing of demuxers and codecs by prepending '+'
reimar
parents: 16306
diff changeset
139 }
24883
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
140 sh_audio->codec = NULL;
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
141 while (1) {
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
142 ad_functions_t *mpadec;
7522
aa1a1249f239 -ac/-afm rewrite, now works the same way as -vc/-vfm
arpi
parents: 7502
diff changeset
143 int i;
14819
601e2c8a2922 Remove file-global mpadec, add ad_driver member to sh_audio_t instead.
hzoli
parents: 14818
diff changeset
144 sh_audio->ad_driver = 0;
7522
aa1a1249f239 -ac/-afm rewrite, now works the same way as -vc/-vfm
arpi
parents: 7502
diff changeset
145 // restore original fourcc:
24883
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
146 if (sh_audio->wf)
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
147 sh_audio->wf->wFormatTag = i = orig_fourcc;
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
148 if (!(sh_audio->codec = find_audio_codec(sh_audio->format,
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
149 sh_audio->wf ? (&i) : NULL,
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
150 sh_audio->codec, force)))
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
151 break;
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
152 if (sh_audio->wf)
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
153 sh_audio->wf->wFormatTag = i;
7522
aa1a1249f239 -ac/-afm rewrite, now works the same way as -vc/-vfm
arpi
parents: 7502
diff changeset
154 // ok we found one codec
25661
293aeec83153 Replace the persistent CODECS_FLAG_SELECTED by a local "stringset" with
reimar
parents: 25254
diff changeset
155 if (stringset_test(selected, sh_audio->codec->name))
24883
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
156 continue; // already tried & failed
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
157 if (codecname && strcmp(sh_audio->codec->name, codecname))
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
158 continue; // -ac
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
159 if (afm && strcmp(sh_audio->codec->drv, afm))
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
160 continue; // afm doesn't match
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
161 if (!force && sh_audio->codec->status < status)
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
162 continue; // too unstable
25661
293aeec83153 Replace the persistent CODECS_FLAG_SELECTED by a local "stringset" with
reimar
parents: 25254
diff changeset
163 stringset_add(selected, sh_audio->codec->name); // tagging it
7522
aa1a1249f239 -ac/-afm rewrite, now works the same way as -vc/-vfm
arpi
parents: 7502
diff changeset
164 // ok, it matches all rules, let's find the driver!
24883
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
165 for (i = 0; mpcodecs_ad_drivers[i] != NULL; i++)
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
166 if (!strcmp(mpcodecs_ad_drivers[i]->info->short_name,
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
167 sh_audio->codec->drv))
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
168 break;
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
169 mpadec = mpcodecs_ad_drivers[i];
8152
4b02f73cb4b9 dlopen() support for ad and vd
alex
parents: 8123
diff changeset
170 #ifdef DYNAMIC_PLUGINS
24883
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
171 if (!mpadec) {
8152
4b02f73cb4b9 dlopen() support for ad and vd
alex
parents: 8123
diff changeset
172 /* try to open shared decoder plugin */
4b02f73cb4b9 dlopen() support for ad and vd
alex
parents: 8123
diff changeset
173 int buf_len;
4b02f73cb4b9 dlopen() support for ad and vd
alex
parents: 8123
diff changeset
174 char *buf;
4b02f73cb4b9 dlopen() support for ad and vd
alex
parents: 8123
diff changeset
175 ad_functions_t *funcs_sym;
4b02f73cb4b9 dlopen() support for ad and vd
alex
parents: 8123
diff changeset
176 ad_info_t *info_sym;
24883
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
177
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
178 buf_len =
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
179 strlen(MPLAYER_LIBDIR) + strlen(sh_audio->codec->drv) + 16;
8152
4b02f73cb4b9 dlopen() support for ad and vd
alex
parents: 8123
diff changeset
180 buf = malloc(buf_len);
4b02f73cb4b9 dlopen() support for ad and vd
alex
parents: 8123
diff changeset
181 if (!buf)
4b02f73cb4b9 dlopen() support for ad and vd
alex
parents: 8123
diff changeset
182 break;
24883
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
183 snprintf(buf, buf_len, "%s/mplayer/ad_%s.so", MPLAYER_LIBDIR,
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
184 sh_audio->codec->drv);
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
185 mp_msg(MSGT_DECAUDIO, MSGL_DBG2,
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
186 "Trying to open external plugin: %s\n", buf);
8152
4b02f73cb4b9 dlopen() support for ad and vd
alex
parents: 8123
diff changeset
187 sh_audio->dec_handle = dlopen(buf, RTLD_LAZY);
4b02f73cb4b9 dlopen() support for ad and vd
alex
parents: 8123
diff changeset
188 if (!sh_audio->dec_handle)
4b02f73cb4b9 dlopen() support for ad and vd
alex
parents: 8123
diff changeset
189 break;
4b02f73cb4b9 dlopen() support for ad and vd
alex
parents: 8123
diff changeset
190 snprintf(buf, buf_len, "mpcodecs_ad_%s", sh_audio->codec->drv);
4b02f73cb4b9 dlopen() support for ad and vd
alex
parents: 8123
diff changeset
191 funcs_sym = dlsym(sh_audio->dec_handle, buf);
24883
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
192 if (!funcs_sym || !funcs_sym->info || !funcs_sym->preinit
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
193 || !funcs_sym->init || !funcs_sym->uninit
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
194 || !funcs_sym->control || !funcs_sym->decode_audio)
8152
4b02f73cb4b9 dlopen() support for ad and vd
alex
parents: 8123
diff changeset
195 break;
4b02f73cb4b9 dlopen() support for ad and vd
alex
parents: 8123
diff changeset
196 info_sym = funcs_sym->info;
4b02f73cb4b9 dlopen() support for ad and vd
alex
parents: 8123
diff changeset
197 if (strcmp(info_sym->short_name, sh_audio->codec->drv))
4b02f73cb4b9 dlopen() support for ad and vd
alex
parents: 8123
diff changeset
198 break;
4b02f73cb4b9 dlopen() support for ad and vd
alex
parents: 8123
diff changeset
199 free(buf);
4b02f73cb4b9 dlopen() support for ad and vd
alex
parents: 8123
diff changeset
200 mpadec = funcs_sym;
24883
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
201 mp_msg(MSGT_DECAUDIO, MSGL_V,
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
202 "Using external decoder plugin (%s/mplayer/ad_%s.so)!\n",
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
203 MPLAYER_LIBDIR, sh_audio->codec->drv);
8152
4b02f73cb4b9 dlopen() support for ad and vd
alex
parents: 8123
diff changeset
204 }
4b02f73cb4b9 dlopen() support for ad and vd
alex
parents: 8123
diff changeset
205 #endif
24883
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
206 if (!mpadec) { // driver not available (==compiled in)
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
207 mp_msg(MSGT_DECAUDIO, MSGL_ERR,
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
208 MSGTR_AudioCodecFamilyNotAvailableStr,
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
209 sh_audio->codec->name, sh_audio->codec->drv);
7522
aa1a1249f239 -ac/-afm rewrite, now works the same way as -vc/-vfm
arpi
parents: 7502
diff changeset
210 continue;
aa1a1249f239 -ac/-afm rewrite, now works the same way as -vc/-vfm
arpi
parents: 7502
diff changeset
211 }
aa1a1249f239 -ac/-afm rewrite, now works the same way as -vc/-vfm
arpi
parents: 7502
diff changeset
212 // it's available, let's try to init!
aa1a1249f239 -ac/-afm rewrite, now works the same way as -vc/-vfm
arpi
parents: 7502
diff changeset
213 // init()
24883
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
214 mp_msg(MSGT_DECAUDIO, MSGL_INFO, MSGTR_OpeningAudioDecoder,
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
215 mpadec->info->short_name, mpadec->info->name);
14819
601e2c8a2922 Remove file-global mpadec, add ad_driver member to sh_audio_t instead.
hzoli
parents: 14818
diff changeset
216 sh_audio->ad_driver = mpadec;
24883
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
217 if (!init_audio_codec(sh_audio)) {
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
218 mp_msg(MSGT_DECAUDIO, MSGL_INFO, MSGTR_ADecoderInitFailed);
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
219 continue; // try next...
7522
aa1a1249f239 -ac/-afm rewrite, now works the same way as -vc/-vfm
arpi
parents: 7502
diff changeset
220 }
aa1a1249f239 -ac/-afm rewrite, now works the same way as -vc/-vfm
arpi
parents: 7502
diff changeset
221 // Yeah! We got it!
aa1a1249f239 -ac/-afm rewrite, now works the same way as -vc/-vfm
arpi
parents: 7502
diff changeset
222 return 1;
aa1a1249f239 -ac/-afm rewrite, now works the same way as -vc/-vfm
arpi
parents: 7502
diff changeset
223 }
aa1a1249f239 -ac/-afm rewrite, now works the same way as -vc/-vfm
arpi
parents: 7502
diff changeset
224 return 0;
aa1a1249f239 -ac/-afm rewrite, now works the same way as -vc/-vfm
arpi
parents: 7502
diff changeset
225 }
aa1a1249f239 -ac/-afm rewrite, now works the same way as -vc/-vfm
arpi
parents: 7502
diff changeset
226
24883
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
227 int init_best_audio_codec(sh_audio_t *sh_audio, char **audio_codec_list,
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
228 char **audio_fm_list)
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
229 {
25661
293aeec83153 Replace the persistent CODECS_FLAG_SELECTED by a local "stringset" with
reimar
parents: 25254
diff changeset
230 stringset_t selected;
24883
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
231 char *ac_l_default[2] = { "", (char *) NULL };
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
232 // hack:
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
233 if (!audio_codec_list)
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
234 audio_codec_list = ac_l_default;
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
235 // Go through the codec.conf and find the best codec...
25962
afa125da85cf typo fix: inited --> initialized
diego
parents: 25661
diff changeset
236 sh_audio->initialized = 0;
25661
293aeec83153 Replace the persistent CODECS_FLAG_SELECTED by a local "stringset" with
reimar
parents: 25254
diff changeset
237 stringset_init(&selected);
25962
afa125da85cf typo fix: inited --> initialized
diego
parents: 25661
diff changeset
238 while (!sh_audio->initialized && *audio_codec_list) {
24883
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
239 char *audio_codec = *(audio_codec_list++);
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
240 if (audio_codec[0]) {
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
241 if (audio_codec[0] == '-') {
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
242 // disable this codec:
25661
293aeec83153 Replace the persistent CODECS_FLAG_SELECTED by a local "stringset" with
reimar
parents: 25254
diff changeset
243 stringset_add(&selected, audio_codec + 1);
24883
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
244 } else {
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
245 // forced codec by name:
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
246 mp_msg(MSGT_DECAUDIO, MSGL_INFO, MSGTR_ForcedAudioCodec,
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
247 audio_codec);
25661
293aeec83153 Replace the persistent CODECS_FLAG_SELECTED by a local "stringset" with
reimar
parents: 25254
diff changeset
248 init_audio(sh_audio, audio_codec, NULL, -1, &selected);
24883
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
249 }
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
250 } else {
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
251 int status;
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
252 // try in stability order: UNTESTED, WORKING, BUGGY.
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
253 // never try CRASHING.
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
254 if (audio_fm_list) {
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
255 char **fmlist = audio_fm_list;
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
256 // try first the preferred codec families:
25962
afa125da85cf typo fix: inited --> initialized
diego
parents: 25661
diff changeset
257 while (!sh_audio->initialized && *fmlist) {
24883
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
258 char *audio_fm = *(fmlist++);
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
259 mp_msg(MSGT_DECAUDIO, MSGL_INFO, MSGTR_TryForceAudioFmtStr,
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
260 audio_fm);
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
261 for (status = CODECS_STATUS__MAX;
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
262 status >= CODECS_STATUS__MIN; --status)
25661
293aeec83153 Replace the persistent CODECS_FLAG_SELECTED by a local "stringset" with
reimar
parents: 25254
diff changeset
263 if (init_audio(sh_audio, NULL, audio_fm, status, &selected))
24883
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
264 break;
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
265 }
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
266 }
25962
afa125da85cf typo fix: inited --> initialized
diego
parents: 25661
diff changeset
267 if (!sh_audio->initialized)
24883
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
268 for (status = CODECS_STATUS__MAX; status >= CODECS_STATUS__MIN;
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
269 --status)
25661
293aeec83153 Replace the persistent CODECS_FLAG_SELECTED by a local "stringset" with
reimar
parents: 25254
diff changeset
270 if (init_audio(sh_audio, NULL, NULL, status, &selected))
24883
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
271 break;
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
272 }
7522
aa1a1249f239 -ac/-afm rewrite, now works the same way as -vc/-vfm
arpi
parents: 7502
diff changeset
273 }
25661
293aeec83153 Replace the persistent CODECS_FLAG_SELECTED by a local "stringset" with
reimar
parents: 25254
diff changeset
274 stringset_free(&selected);
24883
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
275
25962
afa125da85cf typo fix: inited --> initialized
diego
parents: 25661
diff changeset
276 if (!sh_audio->initialized) {
24883
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
277 mp_msg(MSGT_DECAUDIO, MSGL_ERR, MSGTR_CantFindAudioCodec,
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
278 sh_audio->format);
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
279 mp_msg(MSGT_DECAUDIO, MSGL_HINT, MSGTR_RTFMCodecs);
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
280 return 0; // failed
7522
aa1a1249f239 -ac/-afm rewrite, now works the same way as -vc/-vfm
arpi
parents: 7502
diff changeset
281 }
aa1a1249f239 -ac/-afm rewrite, now works the same way as -vc/-vfm
arpi
parents: 7502
diff changeset
282
24883
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
283 mp_msg(MSGT_DECAUDIO, MSGL_INFO, MSGTR_SelectedAudioCodec,
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
284 sh_audio->codec->name, sh_audio->codec->drv, sh_audio->codec->info);
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
285 return 1; // success
7522
aa1a1249f239 -ac/-afm rewrite, now works the same way as -vc/-vfm
arpi
parents: 7502
diff changeset
286 }
aa1a1249f239 -ac/-afm rewrite, now works the same way as -vc/-vfm
arpi
parents: 7502
diff changeset
287
5342
c4cf81b3af71 imporetd from MPlayerXP, with small modification
arpi
parents:
diff changeset
288 void uninit_audio(sh_audio_t *sh_audio)
c4cf81b3af71 imporetd from MPlayerXP, with small modification
arpi
parents:
diff changeset
289 {
24883
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
290 if (sh_audio->afilter) {
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
291 mp_msg(MSGT_DECAUDIO, MSGL_V, "Uninit audio filters...\n");
7604
32efb806436e aufio filter layer (libaf) integration to libmpcodecs, mplayer and mencoder
arpi
parents: 7561
diff changeset
292 af_uninit(sh_audio->afilter);
10803
15425a07e13f minor leak fix
alex
parents: 10683
diff changeset
293 free(sh_audio->afilter);
24883
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
294 sh_audio->afilter = NULL;
7604
32efb806436e aufio filter layer (libaf) integration to libmpcodecs, mplayer and mencoder
arpi
parents: 7561
diff changeset
295 }
25962
afa125da85cf typo fix: inited --> initialized
diego
parents: 25661
diff changeset
296 if (sh_audio->initialized) {
24883
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
297 mp_msg(MSGT_DECAUDIO, MSGL_V, MSGTR_UninitAudioStr,
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
298 sh_audio->codec->drv);
14819
601e2c8a2922 Remove file-global mpadec, add ad_driver member to sh_audio_t instead.
hzoli
parents: 14818
diff changeset
299 sh_audio->ad_driver->uninit(sh_audio);
8152
4b02f73cb4b9 dlopen() support for ad and vd
alex
parents: 8123
diff changeset
300 #ifdef DYNAMIC_PLUGINS
4b02f73cb4b9 dlopen() support for ad and vd
alex
parents: 8123
diff changeset
301 if (sh_audio->dec_handle)
4b02f73cb4b9 dlopen() support for ad and vd
alex
parents: 8123
diff changeset
302 dlclose(sh_audio->dec_handle);
4b02f73cb4b9 dlopen() support for ad and vd
alex
parents: 8123
diff changeset
303 #endif
25962
afa125da85cf typo fix: inited --> initialized
diego
parents: 25661
diff changeset
304 sh_audio->initialized = 0;
7240
a1ae7d811e35 fixed uninit order
arpi
parents: 7191
diff changeset
305 }
24884
17c6e0a372f4 Simplify audio buffer allocation logic
uau
parents: 24883
diff changeset
306 free(sh_audio->a_out_buffer);
24883
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
307 sh_audio->a_out_buffer = NULL;
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
308 sh_audio->a_out_buffer_size = 0;
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
309 if (sh_audio->a_buffer)
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
310 free(sh_audio->a_buffer);
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
311 sh_audio->a_buffer = NULL;
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
312 if (sh_audio->a_in_buffer)
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
313 free(sh_audio->a_in_buffer);
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
314 sh_audio->a_in_buffer = NULL;
5342
c4cf81b3af71 imporetd from MPlayerXP, with small modification
arpi
parents:
diff changeset
315 }
c4cf81b3af71 imporetd from MPlayerXP, with small modification
arpi
parents:
diff changeset
316
24883
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
317
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
318 int init_audio_filters(sh_audio_t *sh_audio, int in_samplerate,
24894
2bfc75674b71 Simplify init_audio_filters() arguments
uau
parents: 24893
diff changeset
319 int *out_samplerate, int *out_channels, int *out_format)
24883
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
320 {
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
321 af_stream_t *afs = sh_audio->afilter;
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
322 if (!afs) {
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
323 afs = malloc(sizeof(af_stream_t));
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
324 memset(afs, 0, sizeof(af_stream_t));
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
325 }
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
326 // input format: same as codec's output format:
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
327 afs->input.rate = in_samplerate;
24894
2bfc75674b71 Simplify init_audio_filters() arguments
uau
parents: 24893
diff changeset
328 afs->input.nch = sh_audio->channels;
2bfc75674b71 Simplify init_audio_filters() arguments
uau
parents: 24893
diff changeset
329 afs->input.format = sh_audio->sample_format;
24883
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
330 af_fix_parameters(&(afs->input));
7604
32efb806436e aufio filter layer (libaf) integration to libmpcodecs, mplayer and mencoder
arpi
parents: 7561
diff changeset
331
24883
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
332 // output format: same as ao driver's input format (if missing, fallback to input)
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
333 afs->output.rate = *out_samplerate;
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
334 afs->output.nch = *out_channels;
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
335 afs->output.format = *out_format;
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
336 af_fix_parameters(&(afs->output));
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
337
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
338 // filter config:
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
339 memcpy(&afs->cfg, &af_cfg, sizeof(af_cfg_t));
7604
32efb806436e aufio filter layer (libaf) integration to libmpcodecs, mplayer and mencoder
arpi
parents: 7561
diff changeset
340
24883
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
341 mp_msg(MSGT_DECAUDIO, MSGL_V, MSGTR_BuildingAudioFilterChain,
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
342 afs->input.rate, afs->input.nch,
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
343 af_fmt2str_short(afs->input.format), afs->output.rate,
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
344 afs->output.nch, af_fmt2str_short(afs->output.format));
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
345
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
346 // let's autoprobe it!
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
347 if (0 != af_init(afs)) {
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
348 sh_audio->afilter = NULL;
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
349 free(afs);
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
350 return 0; // failed :(
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
351 }
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
352
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
353 *out_samplerate = afs->output.rate;
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
354 *out_channels = afs->output.nch;
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
355 *out_format = afs->output.format;
15811
9b4bbb6098f6 make -srate work again, unify audio filter init and preinit.
reimar
parents: 15789
diff changeset
356
24893
bc6132465897 audio: simplify buffer allocation code
uau
parents: 24892
diff changeset
357 sh_audio->a_out_buffer_len = 0;
7604
32efb806436e aufio filter layer (libaf) integration to libmpcodecs, mplayer and mencoder
arpi
parents: 7561
diff changeset
358
24883
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
359 // ok!
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
360 sh_audio->afilter = (void *) afs;
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
361 return 1;
7604
32efb806436e aufio filter layer (libaf) integration to libmpcodecs, mplayer and mencoder
arpi
parents: 7561
diff changeset
362 }
32efb806436e aufio filter layer (libaf) integration to libmpcodecs, mplayer and mencoder
arpi
parents: 7561
diff changeset
363
24892
80180dc13565 Change decode_audio() interface
uau
parents: 24886
diff changeset
364 static int filter_n_bytes(sh_audio_t *sh, int len)
5342
c4cf81b3af71 imporetd from MPlayerXP, with small modification
arpi
parents:
diff changeset
365 {
25252
ca7e863c97bb fix declaration after statement
rfelker
parents: 24961
diff changeset
366 int error = 0;
25253
7862500323c5 fix declaration after statement, take 2
rfelker
parents: 25252
diff changeset
367 // Filter
7862500323c5 fix declaration after statement, take 2
rfelker
parents: 25252
diff changeset
368 af_data_t filter_input = {
7862500323c5 fix declaration after statement, take 2
rfelker
parents: 25252
diff changeset
369 .audio = sh->a_buffer,
7862500323c5 fix declaration after statement, take 2
rfelker
parents: 25252
diff changeset
370 .rate = sh->samplerate,
7862500323c5 fix declaration after statement, take 2
rfelker
parents: 25252
diff changeset
371 .nch = sh->channels,
7862500323c5 fix declaration after statement, take 2
rfelker
parents: 25252
diff changeset
372 .format = sh->sample_format
7862500323c5 fix declaration after statement, take 2
rfelker
parents: 25252
diff changeset
373 };
7862500323c5 fix declaration after statement, take 2
rfelker
parents: 25252
diff changeset
374 af_data_t *filter_output;
24892
80180dc13565 Change decode_audio() interface
uau
parents: 24886
diff changeset
375
25252
ca7e863c97bb fix declaration after statement
rfelker
parents: 24961
diff changeset
376 assert(len-1 + sh->audio_out_minsize <= sh->a_buffer_size);
24892
80180dc13565 Change decode_audio() interface
uau
parents: 24886
diff changeset
377
80180dc13565 Change decode_audio() interface
uau
parents: 24886
diff changeset
378 // Decode more bytes if needed
80180dc13565 Change decode_audio() interface
uau
parents: 24886
diff changeset
379 while (sh->a_buffer_len < len) {
80180dc13565 Change decode_audio() interface
uau
parents: 24886
diff changeset
380 unsigned char *buf = sh->a_buffer + sh->a_buffer_len;
80180dc13565 Change decode_audio() interface
uau
parents: 24886
diff changeset
381 int minlen = len - sh->a_buffer_len;
80180dc13565 Change decode_audio() interface
uau
parents: 24886
diff changeset
382 int maxlen = sh->a_buffer_size - sh->a_buffer_len;
80180dc13565 Change decode_audio() interface
uau
parents: 24886
diff changeset
383 int ret = sh->ad_driver->decode_audio(sh, buf, minlen, maxlen);
80180dc13565 Change decode_audio() interface
uau
parents: 24886
diff changeset
384 if (ret <= 0) {
80180dc13565 Change decode_audio() interface
uau
parents: 24886
diff changeset
385 error = -1;
80180dc13565 Change decode_audio() interface
uau
parents: 24886
diff changeset
386 len = sh->a_buffer_len;
80180dc13565 Change decode_audio() interface
uau
parents: 24886
diff changeset
387 break;
80180dc13565 Change decode_audio() interface
uau
parents: 24886
diff changeset
388 }
80180dc13565 Change decode_audio() interface
uau
parents: 24886
diff changeset
389 sh->a_buffer_len += ret;
80180dc13565 Change decode_audio() interface
uau
parents: 24886
diff changeset
390 }
24883
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
391
25254
f1da4024c3ab 10l, len may change after initialization time
rfelker
parents: 25253
diff changeset
392 filter_input.len = len;
24892
80180dc13565 Change decode_audio() interface
uau
parents: 24886
diff changeset
393 af_fix_parameters(&filter_input);
25253
7862500323c5 fix declaration after statement, take 2
rfelker
parents: 25252
diff changeset
394 filter_output = af_play(sh->afilter, &filter_input);
24892
80180dc13565 Change decode_audio() interface
uau
parents: 24886
diff changeset
395 if (!filter_output)
80180dc13565 Change decode_audio() interface
uau
parents: 24886
diff changeset
396 return -1;
80180dc13565 Change decode_audio() interface
uau
parents: 24886
diff changeset
397 if (sh->a_out_buffer_size < sh->a_out_buffer_len + filter_output->len) {
80180dc13565 Change decode_audio() interface
uau
parents: 24886
diff changeset
398 int newlen = sh->a_out_buffer_len + filter_output->len;
80180dc13565 Change decode_audio() interface
uau
parents: 24886
diff changeset
399 mp_msg(MSGT_DECAUDIO, MSGL_V, "Increasing filtered audio buffer size "
80180dc13565 Change decode_audio() interface
uau
parents: 24886
diff changeset
400 "from %d to %d\n", sh->a_out_buffer_size, newlen);
80180dc13565 Change decode_audio() interface
uau
parents: 24886
diff changeset
401 sh->a_out_buffer = realloc(sh->a_out_buffer, newlen);
80180dc13565 Change decode_audio() interface
uau
parents: 24886
diff changeset
402 sh->a_out_buffer_size = newlen;
80180dc13565 Change decode_audio() interface
uau
parents: 24886
diff changeset
403 }
80180dc13565 Change decode_audio() interface
uau
parents: 24886
diff changeset
404 memcpy(sh->a_out_buffer + sh->a_out_buffer_len, filter_output->audio,
80180dc13565 Change decode_audio() interface
uau
parents: 24886
diff changeset
405 filter_output->len);
80180dc13565 Change decode_audio() interface
uau
parents: 24886
diff changeset
406 sh->a_out_buffer_len += filter_output->len;
80180dc13565 Change decode_audio() interface
uau
parents: 24886
diff changeset
407
80180dc13565 Change decode_audio() interface
uau
parents: 24886
diff changeset
408 // remove processed data from decoder buffer:
80180dc13565 Change decode_audio() interface
uau
parents: 24886
diff changeset
409 sh->a_buffer_len -= len;
80180dc13565 Change decode_audio() interface
uau
parents: 24886
diff changeset
410 memmove(sh->a_buffer, sh->a_buffer + len, sh->a_buffer_len);
80180dc13565 Change decode_audio() interface
uau
parents: 24886
diff changeset
411
80180dc13565 Change decode_audio() interface
uau
parents: 24886
diff changeset
412 return error;
80180dc13565 Change decode_audio() interface
uau
parents: 24886
diff changeset
413 }
80180dc13565 Change decode_audio() interface
uau
parents: 24886
diff changeset
414
80180dc13565 Change decode_audio() interface
uau
parents: 24886
diff changeset
415 /* Try to get at least minlen decoded+filtered bytes in sh_audio->a_out_buffer
80180dc13565 Change decode_audio() interface
uau
parents: 24886
diff changeset
416 * (total length including possible existing data).
80180dc13565 Change decode_audio() interface
uau
parents: 24886
diff changeset
417 * Return 0 on success, -1 on error/EOF (not distinguished).
80180dc13565 Change decode_audio() interface
uau
parents: 24886
diff changeset
418 * In the former case sh_audio->a_out_buffer_len is always >= minlen
80180dc13565 Change decode_audio() interface
uau
parents: 24886
diff changeset
419 * on return. In case of EOF/error it might or might not be.
80180dc13565 Change decode_audio() interface
uau
parents: 24886
diff changeset
420 * Can reallocate sh_audio->a_out_buffer if needed to fit all filter output. */
80180dc13565 Change decode_audio() interface
uau
parents: 24886
diff changeset
421 int decode_audio(sh_audio_t *sh_audio, int minlen)
80180dc13565 Change decode_audio() interface
uau
parents: 24886
diff changeset
422 {
24961
de251a0b5228 Simplify decode_audio function a bit.
reimar
parents: 24918
diff changeset
423 // Indicates that a filter seems to be buffering large amounts of data
de251a0b5228 Simplify decode_audio function a bit.
reimar
parents: 24918
diff changeset
424 int huge_filter_buffer = 0;
24892
80180dc13565 Change decode_audio() interface
uau
parents: 24886
diff changeset
425 // Decoded audio must be cut at boundaries of this many bytes
80180dc13565 Change decode_audio() interface
uau
parents: 24886
diff changeset
426 int unitsize = sh_audio->channels * sh_audio->samplesize;
80180dc13565 Change decode_audio() interface
uau
parents: 24886
diff changeset
427
80180dc13565 Change decode_audio() interface
uau
parents: 24886
diff changeset
428 /* Filter output size will be about filter_multiplier times input size.
80180dc13565 Change decode_audio() interface
uau
parents: 24886
diff changeset
429 * If some filter buffers audio in big blocks this might only hold
80180dc13565 Change decode_audio() interface
uau
parents: 24886
diff changeset
430 * as average over time. */
80180dc13565 Change decode_audio() interface
uau
parents: 24886
diff changeset
431 double filter_multiplier = af_calc_filter_multiplier(sh_audio->afilter);
7604
32efb806436e aufio filter layer (libaf) integration to libmpcodecs, mplayer and mencoder
arpi
parents: 7561
diff changeset
432
24892
80180dc13565 Change decode_audio() interface
uau
parents: 24886
diff changeset
433 /* If the decoder set audio_out_minsize then it can do the equivalent of
80180dc13565 Change decode_audio() interface
uau
parents: 24886
diff changeset
434 * "while (output_len < target_len) output_len += audio_out_minsize;",
80180dc13565 Change decode_audio() interface
uau
parents: 24886
diff changeset
435 * so we must guarantee there is at least audio_out_minsize-1 bytes
80180dc13565 Change decode_audio() interface
uau
parents: 24886
diff changeset
436 * more space in the output buffer than the minimum length we try to
80180dc13565 Change decode_audio() interface
uau
parents: 24886
diff changeset
437 * decode. */
80180dc13565 Change decode_audio() interface
uau
parents: 24886
diff changeset
438 int max_decode_len = sh_audio->a_buffer_size - sh_audio->audio_out_minsize;
80180dc13565 Change decode_audio() interface
uau
parents: 24886
diff changeset
439 max_decode_len -= max_decode_len % unitsize;
7604
32efb806436e aufio filter layer (libaf) integration to libmpcodecs, mplayer and mencoder
arpi
parents: 7561
diff changeset
440
24892
80180dc13565 Change decode_audio() interface
uau
parents: 24886
diff changeset
441 while (sh_audio->a_out_buffer_len < minlen) {
80180dc13565 Change decode_audio() interface
uau
parents: 24886
diff changeset
442 int declen = (minlen - sh_audio->a_out_buffer_len) / filter_multiplier
80180dc13565 Change decode_audio() interface
uau
parents: 24886
diff changeset
443 + (unitsize << 5); // some extra for possible filter buffering
24961
de251a0b5228 Simplify decode_audio function a bit.
reimar
parents: 24918
diff changeset
444 if (huge_filter_buffer)
24892
80180dc13565 Change decode_audio() interface
uau
parents: 24886
diff changeset
445 /* Some filter must be doing significant buffering if the estimated
80180dc13565 Change decode_audio() interface
uau
parents: 24886
diff changeset
446 * input length didn't produce enough output from filters.
80180dc13565 Change decode_audio() interface
uau
parents: 24886
diff changeset
447 * Feed the filters 2k bytes at a time until we have enough output.
80180dc13565 Change decode_audio() interface
uau
parents: 24886
diff changeset
448 * Very small amounts could make filtering inefficient while large
80180dc13565 Change decode_audio() interface
uau
parents: 24886
diff changeset
449 * amounts can make MPlayer demux the file unnecessarily far ahead
80180dc13565 Change decode_audio() interface
uau
parents: 24886
diff changeset
450 * to get audio data and buffer video frames in memory while doing
80180dc13565 Change decode_audio() interface
uau
parents: 24886
diff changeset
451 * so. However the performance impact of either is probably not too
80180dc13565 Change decode_audio() interface
uau
parents: 24886
diff changeset
452 * significant as long as the value is not completely insane. */
24961
de251a0b5228 Simplify decode_audio function a bit.
reimar
parents: 24918
diff changeset
453 declen = 2000;
24892
80180dc13565 Change decode_audio() interface
uau
parents: 24886
diff changeset
454 declen -= declen % unitsize;
24961
de251a0b5228 Simplify decode_audio function a bit.
reimar
parents: 24918
diff changeset
455 if (declen > max_decode_len)
de251a0b5228 Simplify decode_audio function a bit.
reimar
parents: 24918
diff changeset
456 declen = max_decode_len;
de251a0b5228 Simplify decode_audio function a bit.
reimar
parents: 24918
diff changeset
457 else
de251a0b5228 Simplify decode_audio function a bit.
reimar
parents: 24918
diff changeset
458 /* if this iteration does not fill buffer, we must have lots
de251a0b5228 Simplify decode_audio function a bit.
reimar
parents: 24918
diff changeset
459 * of buffering in filters */
de251a0b5228 Simplify decode_audio function a bit.
reimar
parents: 24918
diff changeset
460 huge_filter_buffer = 1;
de251a0b5228 Simplify decode_audio function a bit.
reimar
parents: 24918
diff changeset
461 if (filter_n_bytes(sh_audio, declen) < 0)
de251a0b5228 Simplify decode_audio function a bit.
reimar
parents: 24918
diff changeset
462 return -1;
24883
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
463 }
24892
80180dc13565 Change decode_audio() interface
uau
parents: 24886
diff changeset
464 return 0;
5342
c4cf81b3af71 imporetd from MPlayerXP, with small modification
arpi
parents:
diff changeset
465 }
c4cf81b3af71 imporetd from MPlayerXP, with small modification
arpi
parents:
diff changeset
466
c4cf81b3af71 imporetd from MPlayerXP, with small modification
arpi
parents:
diff changeset
467 void resync_audio_stream(sh_audio_t *sh_audio)
c4cf81b3af71 imporetd from MPlayerXP, with small modification
arpi
parents:
diff changeset
468 {
24883
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
469 sh_audio->a_in_buffer_len = 0; // clear audio input buffer
25962
afa125da85cf typo fix: inited --> initialized
diego
parents: 25661
diff changeset
470 if (!sh_audio->initialized)
24883
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
471 return;
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
472 sh_audio->ad_driver->control(sh_audio, ADCTRL_RESYNC_STREAM, NULL);
5342
c4cf81b3af71 imporetd from MPlayerXP, with small modification
arpi
parents:
diff changeset
473 }
c4cf81b3af71 imporetd from MPlayerXP, with small modification
arpi
parents:
diff changeset
474
c4cf81b3af71 imporetd from MPlayerXP, with small modification
arpi
parents:
diff changeset
475 void skip_audio_frame(sh_audio_t *sh_audio)
c4cf81b3af71 imporetd from MPlayerXP, with small modification
arpi
parents:
diff changeset
476 {
25962
afa125da85cf typo fix: inited --> initialized
diego
parents: 25661
diff changeset
477 if (!sh_audio->initialized)
24883
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
478 return;
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
479 if (sh_audio->ad_driver->control(sh_audio, ADCTRL_SKIP_FRAME, NULL) ==
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
480 CONTROL_TRUE)
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
481 return;
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
482 // default skip code:
9d5f2d383382 Reindent dec_audio.c
uau
parents: 24242
diff changeset
483 ds_fill_buffer(sh_audio->ds); // skip block
5342
c4cf81b3af71 imporetd from MPlayerXP, with small modification
arpi
parents:
diff changeset
484 }