Mercurial > audlegacy-plugins
annotate src/filewriter/convert.c @ 3150:062f42c3737e
Fix possible crash in scrobbler plugin
author | Tomasz Mon <desowin@gmail.com> |
---|---|
date | Tue, 12 May 2009 08:30:43 +0200 |
parents | dcd8d93ba781 |
children |
rev | line source |
---|---|
2874
11ef2164d90b
re-written format conversion using libSAD;
Andrew O. Shadoura <bugzilla@tut.by>
parents:
diff
changeset
|
1 #include "convert.h" |
11ef2164d90b
re-written format conversion using libSAD;
Andrew O. Shadoura <bugzilla@tut.by>
parents:
diff
changeset
|
2 |
11ef2164d90b
re-written format conversion using libSAD;
Andrew O. Shadoura <bugzilla@tut.by>
parents:
diff
changeset
|
3 static SAD_dither_t *sad_state = NULL; |
11ef2164d90b
re-written format conversion using libSAD;
Andrew O. Shadoura <bugzilla@tut.by>
parents:
diff
changeset
|
4 gpointer convert_output = NULL; |
11ef2164d90b
re-written format conversion using libSAD;
Andrew O. Shadoura <bugzilla@tut.by>
parents:
diff
changeset
|
5 static gsize convert_output_length = 0; |
11ef2164d90b
re-written format conversion using libSAD;
Andrew O. Shadoura <bugzilla@tut.by>
parents:
diff
changeset
|
6 static gint nch; |
11ef2164d90b
re-written format conversion using libSAD;
Andrew O. Shadoura <bugzilla@tut.by>
parents:
diff
changeset
|
7 static AFormat in_fmt; |
11ef2164d90b
re-written format conversion using libSAD;
Andrew O. Shadoura <bugzilla@tut.by>
parents:
diff
changeset
|
8 static AFormat out_fmt; |
11ef2164d90b
re-written format conversion using libSAD;
Andrew O. Shadoura <bugzilla@tut.by>
parents:
diff
changeset
|
9 |
11ef2164d90b
re-written format conversion using libSAD;
Andrew O. Shadoura <bugzilla@tut.by>
parents:
diff
changeset
|
10 gboolean convert_init(AFormat input_fmt, AFormat output_fmt, gint channels) |
11ef2164d90b
re-written format conversion using libSAD;
Andrew O. Shadoura <bugzilla@tut.by>
parents:
diff
changeset
|
11 { |
11ef2164d90b
re-written format conversion using libSAD;
Andrew O. Shadoura <bugzilla@tut.by>
parents:
diff
changeset
|
12 gint ret; |
11ef2164d90b
re-written format conversion using libSAD;
Andrew O. Shadoura <bugzilla@tut.by>
parents:
diff
changeset
|
13 SAD_buffer_format input_sad_fmt; |
11ef2164d90b
re-written format conversion using libSAD;
Andrew O. Shadoura <bugzilla@tut.by>
parents:
diff
changeset
|
14 SAD_buffer_format output_sad_fmt; |
11ef2164d90b
re-written format conversion using libSAD;
Andrew O. Shadoura <bugzilla@tut.by>
parents:
diff
changeset
|
15 |
11ef2164d90b
re-written format conversion using libSAD;
Andrew O. Shadoura <bugzilla@tut.by>
parents:
diff
changeset
|
16 |
11ef2164d90b
re-written format conversion using libSAD;
Andrew O. Shadoura <bugzilla@tut.by>
parents:
diff
changeset
|
17 input_sad_fmt.sample_format = aud_sadfmt_from_afmt(input_fmt); |
11ef2164d90b
re-written format conversion using libSAD;
Andrew O. Shadoura <bugzilla@tut.by>
parents:
diff
changeset
|
18 if (input_sad_fmt.sample_format < 0) return FALSE; |
11ef2164d90b
re-written format conversion using libSAD;
Andrew O. Shadoura <bugzilla@tut.by>
parents:
diff
changeset
|
19 input_sad_fmt.fracbits = FMT_FRACBITS(input_fmt); |
11ef2164d90b
re-written format conversion using libSAD;
Andrew O. Shadoura <bugzilla@tut.by>
parents:
diff
changeset
|
20 input_sad_fmt.channels = channels; |
11ef2164d90b
re-written format conversion using libSAD;
Andrew O. Shadoura <bugzilla@tut.by>
parents:
diff
changeset
|
21 input_sad_fmt.channels_order = SAD_CHORDER_INTERLEAVED; |
11ef2164d90b
re-written format conversion using libSAD;
Andrew O. Shadoura <bugzilla@tut.by>
parents:
diff
changeset
|
22 input_sad_fmt.samplerate = 0; |
11ef2164d90b
re-written format conversion using libSAD;
Andrew O. Shadoura <bugzilla@tut.by>
parents:
diff
changeset
|
23 |
11ef2164d90b
re-written format conversion using libSAD;
Andrew O. Shadoura <bugzilla@tut.by>
parents:
diff
changeset
|
24 output_sad_fmt.sample_format = aud_sadfmt_from_afmt(output_fmt); |
11ef2164d90b
re-written format conversion using libSAD;
Andrew O. Shadoura <bugzilla@tut.by>
parents:
diff
changeset
|
25 if (output_sad_fmt.sample_format < 0) return FALSE; |
11ef2164d90b
re-written format conversion using libSAD;
Andrew O. Shadoura <bugzilla@tut.by>
parents:
diff
changeset
|
26 output_sad_fmt.fracbits = FMT_FRACBITS(output_fmt); |
11ef2164d90b
re-written format conversion using libSAD;
Andrew O. Shadoura <bugzilla@tut.by>
parents:
diff
changeset
|
27 output_sad_fmt.channels = channels; |
11ef2164d90b
re-written format conversion using libSAD;
Andrew O. Shadoura <bugzilla@tut.by>
parents:
diff
changeset
|
28 output_sad_fmt.channels_order = SAD_CHORDER_INTERLEAVED; |
11ef2164d90b
re-written format conversion using libSAD;
Andrew O. Shadoura <bugzilla@tut.by>
parents:
diff
changeset
|
29 output_sad_fmt.samplerate = 0; |
11ef2164d90b
re-written format conversion using libSAD;
Andrew O. Shadoura <bugzilla@tut.by>
parents:
diff
changeset
|
30 |
11ef2164d90b
re-written format conversion using libSAD;
Andrew O. Shadoura <bugzilla@tut.by>
parents:
diff
changeset
|
31 sad_state = SAD_dither_init(&input_sad_fmt, &output_sad_fmt, &ret); |
11ef2164d90b
re-written format conversion using libSAD;
Andrew O. Shadoura <bugzilla@tut.by>
parents:
diff
changeset
|
32 if (sad_state == NULL) { |
11ef2164d90b
re-written format conversion using libSAD;
Andrew O. Shadoura <bugzilla@tut.by>
parents:
diff
changeset
|
33 AUDDBG("ditherer init failed\n"); |
11ef2164d90b
re-written format conversion using libSAD;
Andrew O. Shadoura <bugzilla@tut.by>
parents:
diff
changeset
|
34 return FALSE; |
11ef2164d90b
re-written format conversion using libSAD;
Andrew O. Shadoura <bugzilla@tut.by>
parents:
diff
changeset
|
35 } |
11ef2164d90b
re-written format conversion using libSAD;
Andrew O. Shadoura <bugzilla@tut.by>
parents:
diff
changeset
|
36 |
11ef2164d90b
re-written format conversion using libSAD;
Andrew O. Shadoura <bugzilla@tut.by>
parents:
diff
changeset
|
37 in_fmt = input_fmt; |
11ef2164d90b
re-written format conversion using libSAD;
Andrew O. Shadoura <bugzilla@tut.by>
parents:
diff
changeset
|
38 out_fmt = output_fmt; |
11ef2164d90b
re-written format conversion using libSAD;
Andrew O. Shadoura <bugzilla@tut.by>
parents:
diff
changeset
|
39 nch = channels; |
11ef2164d90b
re-written format conversion using libSAD;
Andrew O. Shadoura <bugzilla@tut.by>
parents:
diff
changeset
|
40 |
11ef2164d90b
re-written format conversion using libSAD;
Andrew O. Shadoura <bugzilla@tut.by>
parents:
diff
changeset
|
41 SAD_dither_set_dither(sad_state, FALSE); |
11ef2164d90b
re-written format conversion using libSAD;
Andrew O. Shadoura <bugzilla@tut.by>
parents:
diff
changeset
|
42 return TRUE; |
11ef2164d90b
re-written format conversion using libSAD;
Andrew O. Shadoura <bugzilla@tut.by>
parents:
diff
changeset
|
43 } |
11ef2164d90b
re-written format conversion using libSAD;
Andrew O. Shadoura <bugzilla@tut.by>
parents:
diff
changeset
|
44 |
11ef2164d90b
re-written format conversion using libSAD;
Andrew O. Shadoura <bugzilla@tut.by>
parents:
diff
changeset
|
45 gint convert_process(gpointer ptr, gint length) |
11ef2164d90b
re-written format conversion using libSAD;
Andrew O. Shadoura <bugzilla@tut.by>
parents:
diff
changeset
|
46 { |
11ef2164d90b
re-written format conversion using libSAD;
Andrew O. Shadoura <bugzilla@tut.by>
parents:
diff
changeset
|
47 gint frames, len; |
11ef2164d90b
re-written format conversion using libSAD;
Andrew O. Shadoura <bugzilla@tut.by>
parents:
diff
changeset
|
48 frames = length / nch / FMT_SIZEOF(in_fmt); |
11ef2164d90b
re-written format conversion using libSAD;
Andrew O. Shadoura <bugzilla@tut.by>
parents:
diff
changeset
|
49 len = frames * nch * FMT_SIZEOF(out_fmt); |
2950
dcd8d93ba781
- mp3: adapted to lame-3.98. now filewriter writes valid TLEN.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
2874
diff
changeset
|
50 |
2874
11ef2164d90b
re-written format conversion using libSAD;
Andrew O. Shadoura <bugzilla@tut.by>
parents:
diff
changeset
|
51 if (convert_output == NULL || convert_output_length < len) |
11ef2164d90b
re-written format conversion using libSAD;
Andrew O. Shadoura <bugzilla@tut.by>
parents:
diff
changeset
|
52 { |
11ef2164d90b
re-written format conversion using libSAD;
Andrew O. Shadoura <bugzilla@tut.by>
parents:
diff
changeset
|
53 convert_output_length = len; |
11ef2164d90b
re-written format conversion using libSAD;
Andrew O. Shadoura <bugzilla@tut.by>
parents:
diff
changeset
|
54 convert_output = aud_smart_realloc(convert_output, &convert_output_length); |
11ef2164d90b
re-written format conversion using libSAD;
Andrew O. Shadoura <bugzilla@tut.by>
parents:
diff
changeset
|
55 } |
11ef2164d90b
re-written format conversion using libSAD;
Andrew O. Shadoura <bugzilla@tut.by>
parents:
diff
changeset
|
56 |
11ef2164d90b
re-written format conversion using libSAD;
Andrew O. Shadoura <bugzilla@tut.by>
parents:
diff
changeset
|
57 SAD_dither_process_buffer(sad_state, ptr, convert_output, frames); |
11ef2164d90b
re-written format conversion using libSAD;
Andrew O. Shadoura <bugzilla@tut.by>
parents:
diff
changeset
|
58 return len; |
11ef2164d90b
re-written format conversion using libSAD;
Andrew O. Shadoura <bugzilla@tut.by>
parents:
diff
changeset
|
59 } |
11ef2164d90b
re-written format conversion using libSAD;
Andrew O. Shadoura <bugzilla@tut.by>
parents:
diff
changeset
|
60 |
11ef2164d90b
re-written format conversion using libSAD;
Andrew O. Shadoura <bugzilla@tut.by>
parents:
diff
changeset
|
61 void convert_free(void) |
11ef2164d90b
re-written format conversion using libSAD;
Andrew O. Shadoura <bugzilla@tut.by>
parents:
diff
changeset
|
62 { |
11ef2164d90b
re-written format conversion using libSAD;
Andrew O. Shadoura <bugzilla@tut.by>
parents:
diff
changeset
|
63 if (sad_state != NULL) {SAD_dither_free(sad_state); sad_state = NULL;} |
11ef2164d90b
re-written format conversion using libSAD;
Andrew O. Shadoura <bugzilla@tut.by>
parents:
diff
changeset
|
64 } |