2 +----------------------------------------------------------------------+
3 | Author: yoya@awm.jp |
4 +----------------------------------------------------------------------+
10 #include "swf_tag_sound.h"
11 #include "swf_object.h"
13 swf_tag_detail_handler_t sound_detail_handler;
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;
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");
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;
45 unsigned long sound_data_len;
46 unsigned char *sound_data_ref;
48 if (swf_tag_sound == NULL) {
49 fprintf(stderr, "ERROR: swf_tag_sound_input_detail: swf_tag_sound == NULL\n");
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);
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;
76 swf_tag_sound_get_cid_detail(swf_tag_t *tag) {
78 unsigned char *data = tag->data;
80 swf_tag_sound_detail_t *swf_tag_sound = (swf_tag_sound_detail_t *) tag->detail;
81 return swf_tag_sound->sound_id;
84 fprintf(stderr, "swf_tag_sound_get_cid_detail: data==NULL\n");
87 sound_id = GetUShortLE(data);
92 swf_tag_sound_replace_cid_detail(swf_tag_t *tag, int id) {
93 unsigned char *data = tag->data;
95 swf_tag_sound_detail_t *swf_tag_sound = (swf_tag_sound_detail_t *) tag->detail;
96 swf_tag_sound->sound_id = id;
99 PutUShortLE(data, id);
101 return 0; // always 0
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;
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);
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";
133 switch(swf_tag_sound->sound_format & 0x0f) {
138 format_name = "ADPCM";
144 format_name = "Uncompressed";
147 format_name = "Nellymoser";
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);
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;
170 free(swf_tag_sound->sound_data);
171 swf_tag_sound->sound_data = NULL;
179 swf_tag_sound_get_sound_data(void *detail, unsigned long *length,
181 swf_tag_sound_detail_t *swf_tag_sound = (swf_tag_sound_detail_t *) detail;
184 if (detail == NULL) {
185 fprintf(stderr, "swf_tag_sound_get_sound_data: detail == NULL\n");
188 if (swf_tag_sound->sound_id != sound_id) {
191 *length = swf_tag_sound->sound_data_len;
192 data = malloc(*length);
193 memcpy(data, swf_tag_sound->sound_data, *length);
198 swf_tag_sound_replace_mp3_data(void *detail, int sound_id,
199 unsigned char *mp3_data,
200 unsigned long mp3_data_len) {
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;
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");
228 memcpy(swf_tag_sound->sound_data, melo_data, melo_data_len);
229 swf_tag_sound->sound_data_len = melo_data_len;