OSDN Git Service

add bitmap_id parameter into apply factor method.
[swfed/swfed.git] / src / swf_tag_sound.c
1 /*
2   +----------------------------------------------------------------------+
3   | Author: yoya@awm.jp                                                  |
4   +----------------------------------------------------------------------+
5 */
6
7 #include <stdio.h>
8 #include <string.h>
9 #include "bitstream.h"
10 #include "swf_tag_sound.h"
11 #include "swf_object.h"
12
13 swf_tag_detail_handler_t sound_detail_handler;
14
15 swf_tag_detail_handler_t *
16 swf_tag_sound_detail_handler(void) {
17     sound_detail_handler.create   = swf_tag_sound_create_detail;
18     sound_detail_handler.input    = swf_tag_sound_input_detail;
19     sound_detail_handler.get_cid  = swf_tag_sound_get_cid_detail;
20     sound_detail_handler.replace_cid = swf_tag_sound_replace_cid_detail;
21     sound_detail_handler.output   = swf_tag_sound_output_detail;
22     sound_detail_handler.print    = swf_tag_sound_print_detail;
23     sound_detail_handler.destroy  = swf_tag_sound_destroy_detail;
24     return &sound_detail_handler;
25 }
26
27 void *
28 swf_tag_sound_create_detail(void) {
29     swf_tag_sound_detail_t *swf_tag_sound;
30     swf_tag_sound = calloc(sizeof(*swf_tag_sound), 1);
31     if (swf_tag_sound == NULL) {
32         fprintf(stderr, "ERROR: swf_tag_sound_create_detail: can't calloc\n");
33         return NULL;
34     }
35     return swf_tag_sound;
36 }
37
38 int
39 swf_tag_sound_input_detail(swf_tag_t *tag,
40                             struct swf_object_ *swf) {
41     swf_tag_sound_detail_t *swf_tag_sound = tag->detail;
42     unsigned char *data  = tag->data;
43     unsigned long length = tag->length;
44     bitstream_t *bs;
45     unsigned long sound_data_len;
46     unsigned char *sound_data_ref;
47     (void) swf;
48     if (swf_tag_sound == NULL) {
49         fprintf(stderr, "ERROR: swf_tag_sound_input_detail: swf_tag_sound == NULL\n");
50         return 1;
51     }
52     bs = bitstream_open();
53     bitstream_input(bs, data, length);
54     swf_tag_sound->sound_id = bitstream_getbytesLE(bs, 2);
55     swf_tag_sound->sound_format    = bitstream_getbits(bs, 4);
56     swf_tag_sound->sound_rate      = bitstream_getbits(bs, 2);
57     swf_tag_sound->sound_is_16bits = bitstream_getbit(bs);
58     swf_tag_sound->sound_is_stereo = bitstream_getbit(bs);
59     swf_tag_sound->sound_samples_count = bitstream_getbytesLE(bs, 4);
60     sound_data_len = bitstream_length(bs) - bitstream_getbytepos(bs);
61     swf_tag_sound->sound_data = malloc(sound_data_len);
62     if (swf_tag_sound->sound_data == NULL) {
63         fprintf(stderr, "swf_tag_sound_create_detail: swf_tag_sound->sound_data == NULL at line(%d): sound_data_len=%lu\n",
64                 __LINE__, sound_data_len);
65         bitstream_close(bs);
66         return 1;
67     }
68     sound_data_ref = bitstream_buffer(bs, bitstream_getbytepos(bs));
69     memcpy(swf_tag_sound->sound_data, sound_data_ref, sound_data_len);
70     swf_tag_sound->sound_data_len = sound_data_len;
71     bitstream_close(bs);
72     return 0;
73 }
74
75 int
76 swf_tag_sound_get_cid_detail(swf_tag_t *tag) {
77     int sound_id;
78     unsigned char *data = tag->data;
79     if (tag->detail) {
80         swf_tag_sound_detail_t *swf_tag_sound = (swf_tag_sound_detail_t *) tag->detail;
81         return swf_tag_sound->sound_id;
82     }
83     if (data == NULL) {
84         fprintf(stderr, "swf_tag_sound_get_cid_detail: data==NULL\n");
85         return -1;
86     }
87     sound_id = GetUShortLE(data);
88     return sound_id;
89 }
90
91 int
92 swf_tag_sound_replace_cid_detail(swf_tag_t *tag, int id) {
93     unsigned char *data = tag->data;
94     if (tag->detail) {
95         swf_tag_sound_detail_t *swf_tag_sound = (swf_tag_sound_detail_t *) tag->detail;
96         swf_tag_sound->sound_id = id;
97     }
98     if (data == NULL) {
99         PutUShortLE(data, id);
100     }        
101     return 0; // always 0
102 }
103
104 unsigned char *
105 swf_tag_sound_output_detail(swf_tag_t *tag, unsigned long *length,
106                             struct swf_object_ *swf) {
107     swf_tag_sound_detail_t *swf_tag_sound = (swf_tag_sound_detail_t *) tag->detail;
108     bitstream_t *bs;
109     unsigned char *data;
110     (void) tag;
111     (void) swf;
112     *length = 0;
113     bs = bitstream_open();
114     bitstream_putbytesLE(bs, swf_tag_sound->sound_id, 2);
115     bitstream_putbits(bs, swf_tag_sound->sound_format, 4);
116     bitstream_putbits(bs, swf_tag_sound->sound_rate, 2);
117     bitstream_putbit(bs, swf_tag_sound->sound_is_16bits);
118     bitstream_putbit(bs, swf_tag_sound->sound_is_stereo);
119     bitstream_putbytesLE(bs, swf_tag_sound->sound_samples_count, 4);
120     bitstream_putstring(bs, swf_tag_sound->sound_data,
121                         swf_tag_sound->sound_data_len);
122     data = bitstream_steal(bs, length);
123     bitstream_close(bs);
124     return data;
125 }
126
127 void
128 swf_tag_sound_print_detail(swf_tag_t *tag,
129                            struct swf_object_ *swf, int indent_depth) {
130     swf_tag_sound_detail_t *swf_tag_sound = (swf_tag_sound_detail_t *) tag->detail;
131     char *format_name = "Unknown";
132     (void) swf;
133     switch(swf_tag_sound->sound_format & 0x0f) {
134     case 0:
135         format_name = "Raw";
136         break;
137     case 1:
138         format_name = "ADPCM";
139         break;
140     case 2:
141         format_name = "MP3";
142         break;
143     case 3:
144         format_name = "Uncompressed";
145         break;
146     case 6:
147         format_name = "Nellymoser";
148         break;
149         
150     }
151     print_indent(indent_depth);
152     printf("sound_id=%d\n", swf_tag_sound->sound_id);
153     print_indent(indent_depth);    
154     printf("format=%u(%s) rate=%u is_16bits=%u is_stereo=%u samples_count=%lu\n",
155            swf_tag_sound->sound_format & 0x0f, format_name,
156            swf_tag_sound->sound_rate  & 0x03,
157            swf_tag_sound->sound_is_16bits?1:0,
158            swf_tag_sound->sound_is_stereo?1:0,
159            swf_tag_sound->sound_samples_count);
160     print_indent(indent_depth);    
161     printf("sound_data(length=%lu)\n",
162            swf_tag_sound->sound_data_len);
163     return ;
164 }
165
166 void
167 swf_tag_sound_destroy_detail(swf_tag_t *tag) {
168     swf_tag_sound_detail_t *swf_tag_sound = (swf_tag_sound_detail_t *) tag->detail;
169     if (swf_tag_sound) {
170         free(swf_tag_sound->sound_data);
171         swf_tag_sound->sound_data = NULL;
172         free(swf_tag_sound);
173         tag->detail = NULL;
174     }
175     return ;
176 }
177
178 unsigned char *
179 swf_tag_sound_get_sound_data(void *detail, unsigned long *length,
180                            int sound_id) {
181     swf_tag_sound_detail_t *swf_tag_sound = (swf_tag_sound_detail_t *) detail;
182     unsigned char *data;
183     *length = 0;
184     if (detail == NULL) {
185         fprintf(stderr, "swf_tag_sound_get_sound_data: detail == NULL\n");
186         return NULL;
187     }
188     if (swf_tag_sound->sound_id != sound_id) {
189         return NULL;
190     }
191     *length = swf_tag_sound->sound_data_len;
192     data = malloc(*length);
193     memcpy(data, swf_tag_sound->sound_data, *length);
194     return data;
195 }
196
197 int
198 swf_tag_sound_replace_mp3_data(void *detail, int sound_id,
199                                unsigned char *mp3_data,
200                                unsigned long mp3_data_len) {
201     (void) detail;
202     (void) sound_id;
203     (void) mp3_data;
204     (void) mp3_data_len;
205         // dummy
206     return -1;
207 }
208
209 int
210 swf_tag_sound_replace_melo_data(void *detail, int sound_id,
211                                 unsigned char *melo_data,
212                                 unsigned long melo_data_len) {
213     swf_tag_sound_detail_t *swf_tag_sound = (swf_tag_sound_detail_t *) detail;
214     (void) melo_data;
215     (void) melo_data_len;
216     swf_tag_sound->sound_id = sound_id;
217     swf_tag_sound->sound_format = 15;
218     swf_tag_sound->sound_rate          = 0;
219     swf_tag_sound->sound_is_16bits     = 0;
220     swf_tag_sound->sound_is_stereo     = 0;
221     swf_tag_sound->sound_samples_count = 0;
222     free(swf_tag_sound->sound_data);
223     swf_tag_sound->sound_data = malloc(melo_data_len);
224     if (swf_tag_sound->sound_data == NULL) {
225         fprintf(stderr, "swf_tag_sound_replace_melo_data: swf_tag_sound->sound_data == NULL\n");
226         return 1;
227     }
228     memcpy(swf_tag_sound->sound_data, melo_data, melo_data_len);
229     swf_tag_sound->sound_data_len = melo_data_len;
230     return 0;
231 }