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.identity = swf_tag_sound_identity_detail;
20 sound_detail_handler.output = swf_tag_sound_output_detail;
21 sound_detail_handler.print = swf_tag_sound_print_detail;
22 sound_detail_handler.destroy = swf_tag_sound_destroy_detail;
23 return &sound_detail_handler;
27 swf_tag_sound_create_detail(void) {
28 swf_tag_sound_detail_t *swf_tag_sound;
29 swf_tag_sound = calloc(sizeof(*swf_tag_sound), 1);
30 if (swf_tag_sound == NULL) {
31 fprintf(stderr, "ERROR: swf_tag_sound_create_detail: can't calloc\n");
38 swf_tag_sound_input_detail(swf_tag_t *tag,
39 struct swf_object_ *swf) {
40 swf_tag_sound_detail_t *swf_tag_sound = tag->detail;
41 unsigned char *data = tag->data;
42 unsigned long length = tag->length;
44 unsigned long sound_data_len;
45 unsigned char *sound_data_ref;
47 if (swf_tag_sound == NULL) {
48 fprintf(stderr, "ERROR: swf_tag_sound_input_detail: swf_tag_sound == NULL\n");
51 bs = bitstream_open();
52 bitstream_input(bs, data, length);
53 swf_tag_sound->sound_id = bitstream_getbytesLE(bs, 2);
54 swf_tag_sound->sound_format = bitstream_getbits(bs, 4);
55 swf_tag_sound->sound_rate = bitstream_getbits(bs, 2);
56 swf_tag_sound->sound_is_16bits = bitstream_getbit(bs);
57 swf_tag_sound->sound_is_stereo = bitstream_getbit(bs);
58 swf_tag_sound->sound_samples_count = bitstream_getbytesLE(bs, 4);
59 sound_data_len = bitstream_length(bs) - bitstream_getbytepos(bs);
60 swf_tag_sound->sound_data = malloc(sound_data_len);
61 if (swf_tag_sound->sound_data == NULL) {
62 fprintf(stderr, "swf_tag_sound_create_detail: swf_tag_sound->sound_data == NULL at line(%d): sound_data_len=%lu\n",
63 __LINE__, sound_data_len);
67 sound_data_ref = bitstream_buffer(bs, bitstream_getbytepos(bs));
68 memcpy(swf_tag_sound->sound_data, sound_data_ref, sound_data_len);
69 swf_tag_sound->sound_data_len = sound_data_len;
75 swf_tag_sound_identity_detail(swf_tag_t *tag, int id) {
77 unsigned char *data = tag->data;
79 swf_tag_sound_detail_t *swf_tag_sound = (swf_tag_sound_detail_t *) tag->detail;
80 if (swf_tag_sound->sound_id == id) {
86 fprintf(stderr, "swf_tag_sound_identity_detail: data==NULL\n");
89 sound_id = GetUShortLE(data);
97 swf_tag_sound_output_detail(swf_tag_t *tag, unsigned long *length,
98 struct swf_object_ *swf) {
99 swf_tag_sound_detail_t *swf_tag_sound = (swf_tag_sound_detail_t *) tag->detail;
105 bs = bitstream_open();
106 bitstream_putbytesLE(bs, swf_tag_sound->sound_id, 2);
107 bitstream_putbits(bs, swf_tag_sound->sound_format, 4);
108 bitstream_putbits(bs, swf_tag_sound->sound_rate, 2);
109 bitstream_putbit(bs, swf_tag_sound->sound_is_16bits);
110 bitstream_putbit(bs, swf_tag_sound->sound_is_stereo);
111 bitstream_putbytesLE(bs, swf_tag_sound->sound_samples_count, 4);
112 bitstream_putstring(bs, swf_tag_sound->sound_data,
113 swf_tag_sound->sound_data_len);
114 data = bitstream_steal(bs, length);
120 swf_tag_sound_print_detail(swf_tag_t *tag,
121 struct swf_object_ *swf, int indent_depth) {
122 swf_tag_sound_detail_t *swf_tag_sound = (swf_tag_sound_detail_t *) tag->detail;
123 char *format_name = "Unknown";
125 switch(swf_tag_sound->sound_format & 0x0f) {
130 format_name = "ADPCM";
136 format_name = "Uncompressed";
139 format_name = "Nellymoser";
143 print_indent(indent_depth);
144 printf("sound_id=%d\n", swf_tag_sound->sound_id);
145 print_indent(indent_depth);
146 printf("format=%u(%s) rate=%u is_16bits=%u is_stereo=%u samples_count=%lu\n",
147 swf_tag_sound->sound_format & 0x0f, format_name,
148 swf_tag_sound->sound_rate & 0x03,
149 swf_tag_sound->sound_is_16bits?1:0,
150 swf_tag_sound->sound_is_stereo?1:0,
151 swf_tag_sound->sound_samples_count);
152 print_indent(indent_depth);
153 printf("sound_data(length=%lu)\n",
154 swf_tag_sound->sound_data_len);
159 swf_tag_sound_destroy_detail(swf_tag_t *tag) {
160 swf_tag_sound_detail_t *swf_tag_sound = (swf_tag_sound_detail_t *) tag->detail;
162 free(swf_tag_sound->sound_data);
163 swf_tag_sound->sound_data = NULL;
170 swf_tag_sound_get_sound_data(void *detail, unsigned long *length,
172 swf_tag_sound_detail_t *swf_tag_sound = (swf_tag_sound_detail_t *) detail;
175 if (detail == NULL) {
176 fprintf(stderr, "swf_tag_sound_get_sound_data: detail == NULL\n");
179 if (swf_tag_sound->sound_id != sound_id) {
182 *length = swf_tag_sound->sound_data_len;
183 data = malloc(*length);
184 memcpy(data, swf_tag_sound->sound_data, *length);
189 swf_tag_sound_replace_mp3_data(void *detail, int sound_id,
190 unsigned char *mp3_data,
191 unsigned long mp3_data_len) {
201 swf_tag_sound_replace_melo_data(void *detail, int sound_id,
202 unsigned char *melo_data,
203 unsigned long melo_data_len) {
204 swf_tag_sound_detail_t *swf_tag_sound = (swf_tag_sound_detail_t *) detail;
206 (void) melo_data_len;
207 swf_tag_sound->sound_id = sound_id;
208 swf_tag_sound->sound_format = 15;
209 swf_tag_sound->sound_rate = 0;
210 swf_tag_sound->sound_is_16bits = 0;
211 swf_tag_sound->sound_is_stereo = 0;
212 swf_tag_sound->sound_samples_count = 0;
213 free(swf_tag_sound->sound_data);
214 swf_tag_sound->sound_data = malloc(melo_data_len);
215 if (swf_tag_sound->sound_data == NULL) {
216 fprintf(stderr, "swf_tag_sound_replace_melo_data: swf_tag_sound->sound_data == NULL\n");
219 memcpy(swf_tag_sound->sound_data, melo_data, melo_data_len);
220 swf_tag_sound->sound_data_len = melo_data_len;