[Swfed-svn] swfed-svn [70] replaceMP3 対応で書き換えのコード

Back to archive index

svnno****@sourc***** svnno****@sourc*****
2008年 10月 10日 (金) 19:45:57 JST


Revision: 70
          http://svn.sourceforge.jp/cgi-bin/viewcvs.cgi?root=swfed&view=rev&rev=70
Author:   yoya
Date:     2008-10-10 19:45:57 +0900 (Fri, 10 Oct 2008)

Log Message:
-----------
replaceMP3 対応で書き換えのコード

Modified Paths:
--------------
    branches/mp3-dev/src/php_swfed.h
    branches/mp3-dev/src/swf_object.c
    branches/mp3-dev/src/swf_object.h
    branches/mp3-dev/src/swf_tag.c
    branches/mp3-dev/src/swf_tag.h
    branches/mp3-dev/src/swf_tag_sound.c


-------------- next part --------------
Modified: branches/mp3-dev/src/php_swfed.h
===================================================================
--- branches/mp3-dev/src/php_swfed.h	2008-10-10 10:45:30 UTC (rev 69)
+++ branches/mp3-dev/src/php_swfed.h	2008-10-10 10:45:57 UTC (rev 70)
@@ -59,6 +59,7 @@
 PHP_METHOD(swfed, getPNGData);
 PHP_METHOD(swfed, replacePNGData);
 PHP_METHOD(swfed, getSoundData);
+PHP_METHOD(swfed, replaceMP3Data);
 PHP_METHOD(swfed, replaceMLDData);
 PHP_METHOD(swfed, getEditString);
 PHP_METHOD(swfed, replaceEditString);

Modified: branches/mp3-dev/src/swf_object.c
===================================================================
--- branches/mp3-dev/src/swf_object.c	2008-10-10 10:45:30 UTC (rev 69)
+++ branches/mp3-dev/src/swf_object.c	2008-10-10 10:45:57 UTC (rev 70)
@@ -54,12 +54,12 @@
         ; // OK
     } else if (memcmp(swf->header.magic, "CWS", 3) == 0) {
         int result;
-        unsigned char *old_buff, *new_buff;
+        unsigned char *old_buff_ref, *new_buff;
         unsigned long origsize;
-        old_buff = bitstream_buffer(bs, SWF_HEADER_SIZE);
+        old_buff_ref = bitstream_buffer(bs, SWF_HEADER_SIZE);
         origsize = swf->header.file_length - SWF_HEADER_SIZE;
         new_buff = malloc(origsize);
-        result = uncompress(new_buff, &origsize, old_buff, bs->data_len - SWF_HEADER_SIZE);
+        result = uncompress(new_buff, &origsize, old_buff_ref, bs->data_len - SWF_HEADER_SIZE);
         if (result != Z_OK) {
             if (result == Z_MEM_ERROR) {
                 fprintf(stderr, "swf_object_input: uncompress Z_MEM_ERROR: can't malloc\n");
@@ -68,8 +68,8 @@
             } else {
                 fprintf(stderr, "swf_object_input: uncompress failed by unknown reason\n");
             }
+            bitstream_close(bs);
             free(new_buff);
-            bitstream_close(bs);
             return 1; // FAILURE
         }
         bitstream_putstring(bs, new_buff, origsize);
@@ -131,11 +131,11 @@
     } else if (memcmp(swf->header.magic, "CWS", SWF_MAGIC_SIZE) == 0) {
         int result;
         unsigned long compsize;
-        unsigned char *new_buff, *old_buff;
+        unsigned char *new_buff, *old_buff_ref;
         bitstream_setpos(bs, SWF_HEADER_SIZE, 0);
-        old_buff = bitstream_buffer(bs, SWF_HEADER_SIZE);
+        old_buff_ref = bitstream_buffer(bs, SWF_HEADER_SIZE);
         new_buff = malloc(swf->header.file_length - SWF_HEADER_SIZE);
-        result = compress(new_buff, &compsize, old_buff,
+        result = compress(new_buff, &compsize, old_buff_ref,
                           bs->data_len - SWF_HEADER_SIZE);
         if (result != Z_OK) {
             if (result == Z_MEM_ERROR) {
@@ -322,6 +322,26 @@
 }
 
 int
+swf_object_replace_mp3data(swf_object_t *swf, int sound_id,
+                           unsigned char *mp3_data,
+                           unsigned long mp3_data_len) {
+    int result = 1;
+    swf_tag_t *tag;
+    if (swf == NULL) {
+        fprintf(stderr, "swf_object_replace_mp3data: swf == NULL\n");
+        return 1;
+    }
+    for (tag = swf->tag; tag; tag = tag->next) {
+        result = swf_tag_replace_mp3_data(tag, sound_id,
+                                          mp3_data, mp3_data_len);
+        if (! result) {
+            break;
+        }
+    }
+    return result;
+}
+
+int
 swf_object_replace_melodata(swf_object_t *swf, int sound_id,
                             unsigned char *melo_data,
                             unsigned long melo_data_len) {

Modified: branches/mp3-dev/src/swf_object.h
===================================================================
--- branches/mp3-dev/src/swf_object.h	2008-10-10 10:45:30 UTC (rev 69)
+++ branches/mp3-dev/src/swf_object.h	2008-10-10 10:45:57 UTC (rev 70)
@@ -37,6 +37,9 @@
                                       unsigned char *png_data,
                                       unsigned long png_data_len);
 extern unsigned char *swf_object_get_sounddata(swf_object_t *swf, unsigned long *length, int sound_id);
+extern int swf_object_replace_mp3data(swf_object_t *swf, int sound_id,
+                                      unsigned char *mp3_data,
+                                      unsigned long mp3_data_len);
 extern int swf_object_replace_melodata(swf_object_t *swf, int sound_id,
                                        unsigned char *melo_data,
                                        unsigned long melo_data_len);

Modified: branches/mp3-dev/src/swf_tag.c
===================================================================
--- branches/mp3-dev/src/swf_tag.c	2008-10-10 10:45:30 UTC (rev 69)
+++ branches/mp3-dev/src/swf_tag.c	2008-10-10 10:45:57 UTC (rev 70)
@@ -436,6 +436,45 @@
 }
 
 int
+swf_tag_replace_mp3_data(swf_tag_t *tag, int sound_id,
+                         unsigned char *mp3_data,
+                         unsigned long mp3_data_len) {
+    swf_tag_info_t *tag_info;
+    int result;
+    if (tag == NULL) {
+        fprintf(stderr, "swf_tag_replace_mp3_data: tag == NULL\n");
+        return 1;
+    }
+    if (tag->tag != 14) { // DefineSound
+        return 1;
+    }
+    tag_info = get_swf_tag_info(tag->tag);
+    if (tag_info && tag_info->detail_handler) {
+        swf_tag_detail_handler_t * detail_handler = tag_info->detail_handler();
+        if (detail_handler->identity) {
+            if (detail_handler->identity(tag->data, sound_id, tag)) {
+                return 1;
+            }
+        }
+    }
+    if (! tag->detail) {
+        swf_tag_create_detail(tag, NULL);
+    }
+    if (! tag->detail) {
+        fprintf(stderr, "swf_tag_replace_mp3g_data: Can't create tag\n");
+        return 1;
+    }
+    result= swf_tag_sound_replace_mp3_data(tag->detail, sound_id,
+                                           mp3_data, mp3_data_len);
+    if (result == 0) {
+        free(tag->data);
+        tag->data = NULL;
+        tag->length = 0;
+    }
+    return result;
+}
+
+int
 swf_tag_replace_melo_data(swf_tag_t *tag, int sound_id,
                           unsigned char *melo_data,
                           unsigned long melo_data_len) {

Modified: branches/mp3-dev/src/swf_tag.h
===================================================================
--- branches/mp3-dev/src/swf_tag.h	2008-10-10 10:45:30 UTC (rev 69)
+++ branches/mp3-dev/src/swf_tag.h	2008-10-10 10:45:57 UTC (rev 70)
@@ -68,6 +68,9 @@
 extern unsigned char *swf_tag_get_sound_data(swf_tag_t *tag,
                                              unsigned long *length,
                                              int sound_id);
+extern int swf_tag_replace_mp3_data(swf_tag_t *tag, int sound_id,
+                                    unsigned char *mp3_data,
+                                    unsigned long mp3_data_len);
 extern int swf_tag_replace_melo_data(swf_tag_t *tag, int sound_id,
                                      unsigned char *melo_data,
                                      unsigned long melo_data_len);

Modified: branches/mp3-dev/src/swf_tag_sound.c
===================================================================
--- branches/mp3-dev/src/swf_tag_sound.c	2008-10-10 10:45:30 UTC (rev 69)
+++ branches/mp3-dev/src/swf_tag_sound.c	2008-10-10 10:45:57 UTC (rev 70)
@@ -29,7 +29,7 @@
     swf_tag_sound_detail_t *swf_tag_sound;
     bitstream_t *bs;
     unsigned long sound_data_len;
-    unsigned char *sound_data;
+    unsigned char *sound_data_ref;
     (void) tag;
     (void) swf;
     swf_tag_sound = calloc(sizeof(*swf_tag_sound), 1);
@@ -53,8 +53,8 @@
         bitstream_close(bs);
         return NULL;
     }
-    sound_data = bitstream_buffer(bs, bitstream_getbytepos(bs));
-    memcpy(swf_tag_sound->sound_data, sound_data, sound_data_len);
+    sound_data_ref = bitstream_buffer(bs, bitstream_getbytepos(bs));
+    memcpy(swf_tag_sound->sound_data, sound_data_ref, sound_data_len);
     swf_tag_sound->sound_data_len = sound_data_len;
     bitstream_close(bs);
     return (void *) swf_tag_sound;
@@ -174,16 +174,93 @@
     return data;
 }
 
+typedef struct swf_mp3_header_t_ {
+    unsigned sync_word;     /* :11 */
+    unsigned version;       /* :2 */
+    unsigned layer;         /* :2 */
+    unsigned no_protection; /* :1 */
+    /* - */
+    unsigned bit_rate;      /* 4 */
+    unsigned sample_rate;   /* 2 */
+    unsigned padding;       /* 1 */
+    unsigned reserved;      /* 1 */
+    /* - */
+    unsigned channel_mode;   /* 2 */
+    unsigned mode_extension; /* 2 */
+    unsigned copyright;      /* 1 */
+    unsigned original;       /* 1 */
+    unsigned emphasis;       /* 2 */
+    /* - */
+    unsigned short check_sum; /* if no_protection == 0 */
+//    unsigned char data[];
+} swf_mp3_header_t;
+
 int
 swf_tag_sound_replace_mp3_data(void *detail, int sound_id,
                                unsigned char *mp3_data,
                                unsigned long mp3_data_len) {
-    (void) detail;
-    (void) sound_id;
-    (void) mp3_data;
-    (void) mp3_data_len;
+    swf_tag_sound_detail_t *swf_tag_sound = (swf_tag_sound_detail_t *) detail;
+    swf_mp3_header_t mp3_header;
+    bitstream_t *bs;
+    swf_tag_sound->sound_id = sound_id;
+    swf_tag_sound->sound_format = 2;
+    //
+    bs = bitstream_open();
+    bitstream_input(bs, mp3_data, mp3_data_len);
+    mp3_header.sync_word      = bitstream_getbits(bs, 11);
+    mp3_header.version        = bitstream_getbits(bs, 2);
+    mp3_header.layer          = bitstream_getbits(bs, 2);
+    mp3_header.no_protection  = bitstream_getbits(bs, 1);
+    mp3_header.bit_rate       = bitstream_getbits(bs, 4);
+    mp3_header.sample_rate    = bitstream_getbits(bs, 2);
+    mp3_header.padding        = bitstream_getbits(bs, 1);
+    mp3_header.reserved       = bitstream_getbits(bs, 1);
+    mp3_header.channel_mode   = bitstream_getbits(bs, 2);
+    mp3_header.mode_extension = bitstream_getbits(bs, 2);
+    mp3_header.copyright      = bitstream_getbits(bs, 1);
+    mp3_header.original       = bitstream_getbits(bs, 1);
+    mp3_header.emphasis       = bitstream_getbits(bs, 2);
+    if (mp3_header.no_protection == 0) {
+        mp3_header.check_sum   = bitstream_getbytesBE(bs, 2);
+    }
+    bitstream_close(bs);
+    if (mp3_header.layer != 1) { // Layer III
+        fprintf(stderr, "swf_tag_sound_replace_mp3_data: swf can't accept mp3 layer=(%d)\n", mp3_header.layer);
+        return 1;
+    }
+    if (mp3_header.original != 0) {
+        fprintf(stderr, "swf_tag_sound_replace_mp3_data: swf can't accept mp3 original != 0\n");
+        return 1;
+    }
+    if (mp3_header.reserved != 0) {
+        fprintf(stderr, "swf_tag_sound_replace_mp3_data: swf can't accept mp3 reserved != 0\n");
+        return 1;
+    }
+    switch (mp3_header.version) { /* XXX */
+      case 0: /* MPEG2.5 */
+          swf_tag_sound->sound_rate = 1; /* 11,025[HZ] */
+      case 2: /* MPEG2 */
+          swf_tag_sound->sound_rate = 2; /* 22,050[HZ] */
+      case 3: /* MPEG1 */
+          swf_tag_sound->sound_rate = 3; /* 44,100[HZ] */
+    }
+    swf_tag_sound->sound_is_16bits = 1;
+    if (mp3_header.channel_mode != 3) {
+        swf_tag_sound->sound_is_stereo = 1;
+    } else {
+        swf_tag_sound->sound_is_stereo = 0;
+    }
+    swf_tag_sound->sound_samples_count = 0;
+    free(swf_tag_sound->sound_data);
+    swf_tag_sound->sound_data = malloc(mp3_data_len);
+    if (swf_tag_sound->sound_data == NULL) {
+        fprintf(stderr, "swf_tag_sound_replace_mp3_data: swf_tag_sound->sound_data == NULL\n");
+        return 1;
+    }
+    memcpy(swf_tag_sound->sound_data, mp3_data, mp3_data_len);
+    swf_tag_sound->sound_data_len = mp3_data_len;
         // dummy
-    return -1;
+    return 0;
 }
 
 int
@@ -191,8 +268,6 @@
                                 unsigned char *melo_data,
                                 unsigned long melo_data_len) {
     swf_tag_sound_detail_t *swf_tag_sound = (swf_tag_sound_detail_t *) detail;
-    (void) melo_data;
-    (void) melo_data_len;
     swf_tag_sound->sound_id = sound_id;
     swf_tag_sound->sound_format = 15;
     swf_tag_sound->sound_rate = 0;


Swfed-svn メーリングリストの案内
Back to archive index