[xiph-commits] r14449 - in trunk/vorbis-tools: . oggenc

conrad at svn.xiph.org conrad at svn.xiph.org
Sat Feb 2 17:09:10 PST 2008


Author: conrad
Date: 2008-02-02 17:09:08 -0800 (Sat, 02 Feb 2008)
New Revision: 14449

Modified:
   trunk/vorbis-tools/configure.ac
   trunk/vorbis-tools/oggenc/skeleton.c
Log:
Fix for skeleton interpretation on big endian hosts, as reported by
ogg.k.ogg.k. This patch adds an AC_C_BIGENDIAN check to configure.ac.


Modified: trunk/vorbis-tools/configure.ac
===================================================================
--- trunk/vorbis-tools/configure.ac	2008-02-02 06:05:52 UTC (rev 14448)
+++ trunk/vorbis-tools/configure.ac	2008-02-03 01:09:08 UTC (rev 14449)
@@ -31,7 +31,12 @@
 ALL_LINGUAS="be cs da es fr hr hu nl ro ru sv uk"
 AM_GNU_GETTEXT
 
+dnl --------------------------------------------------
+dnl System checks
+dnl --------------------------------------------------
+
 AC_SYS_LARGEFILE
+AC_C_BIGENDIAN
 
 dnl --------------------------------------------------
 dnl Set build flags based on environment

Modified: trunk/vorbis-tools/oggenc/skeleton.c
===================================================================
--- trunk/vorbis-tools/oggenc/skeleton.c	2008-02-02 06:05:52 UTC (rev 14448)
+++ trunk/vorbis-tools/oggenc/skeleton.c	2008-02-03 01:09:08 UTC (rev 14449)
@@ -3,6 +3,16 @@
  * author: Tahseen Mohammad
  */
 
+/* This file depends on WORDS_BIGENDIAN being defined to 1 if the host
+ * processor stores words with the most significant byte first (like Motorola
+ * and SPARC, unlike Intel and VAX). 
+ * On little endian systems, WORDS_BIGENDIAN should be undefined.
+ *
+ * When using GNU Autotools, the correct value will be written into config.h
+ * if the autoconf macro AC_C_BIGENDIAN is called in configure.ac.
+ */
+#include "config.h"
+
 #include <stdlib.h>
 #include <string.h>
 #include <stdio.h>
@@ -11,22 +21,80 @@
 
 #include "skeleton.h"
 
-int add_message_header_field(fisbone_packet *fp,
-                                        char *header_key,
+#ifdef WIN32                                                                   
+#define snprintf _snprintf
+#endif 
+
+extern int oe_write_page(ogg_page *page, FILE *fp);
+
+static  unsigned short
+_le_16 (unsigned short s)
+{
+  unsigned short ret=s;
+#ifdef WORDS_BIGENDIAN
+  ret = (s>>8) & 0x00ffU;
+  ret += (s<<8) & 0xff00U;
+#endif
+  return ret;
+}
+
+static  ogg_uint32_t
+_le_32 (ogg_uint32_t i)
+{
+   ogg_uint32_t ret=i;
+#ifdef WORDS_BIGENDIAN
+   ret =  (i>>24);
+   ret += (i>>8) & 0x0000ff00;
+   ret += (i<<8) & 0x00ff0000;
+   ret += (i<<24);
+#endif
+   return ret;
+}
+
+static  ogg_int64_t
+_le_64 (ogg_int64_t l)
+{
+  ogg_int64_t ret=l;
+  unsigned char *ucptr = (unsigned char *)&ret;
+#ifdef WORDS_BIGENDIAN
+  unsigned char temp;
+
+  temp = ucptr [0] ;
+  ucptr [0] = ucptr [7] ;
+  ucptr [7] = temp ;
+
+  temp = ucptr [1] ;
+  ucptr [1] = ucptr [6] ;
+  ucptr [6] = temp ;
+
+  temp = ucptr [2] ;
+  ucptr [2] = ucptr [5] ;
+  ucptr [5] = temp ;
+
+  temp = ucptr [3] ;
+  ucptr [3] = ucptr [4] ;
+  ucptr [4] = temp ;
+
+#endif
+  return (*(ogg_int64_t *)ucptr);
+}
+
+int add_message_header_field(fisbone_packet *fp, 
+                                        char *header_key, 
                                         char *header_value) {
 
     /* size of both key and value + ': ' + CRLF */
     int this_message_size = strlen(header_key) + strlen(header_value) + 4;
     if (fp->message_header_fields == NULL) {
-        fp->message_header_fields = _ogg_calloc(this_message_size+1, sizeof(char));
+        fp->message_header_fields = _ogg_calloc(this_message_size, sizeof(char));
     } else {
-        int new_size = (fp->current_header_size + this_message_size+1) * sizeof(char);
+        int new_size = (fp->current_header_size + this_message_size) * sizeof(char);
         fp->message_header_fields = _ogg_realloc(fp->message_header_fields, new_size);
     }
-    snprintf(fp->message_header_fields + fp->current_header_size,
-                this_message_size,
-                "%s: %s\r\n",
-                header_key,
+    snprintf(fp->message_header_fields + fp->current_header_size, 
+                this_message_size+1, 
+                "%s: %s\r\n", 
+                header_key, 
                 header_value);
     fp->current_header_size += this_message_size;
 
@@ -43,12 +111,12 @@
     memset(op.packet, 0, FISHEAD_SIZE);
 
     memcpy (op.packet, FISHEAD_IDENTIFIER, 8); /* identifier */
-    *((ogg_uint16_t*)(op.packet+8)) = SKELETON_VERSION_MAJOR; /* version major */
-    *((ogg_uint16_t*)(op.packet+10)) = SKELETON_VERSION_MINOR; /* version minor */
-    *((ogg_int64_t*)(op.packet+12)) = (ogg_int64_t)fp->ptime_n; /* presentationtime numerator */
-    *((ogg_int64_t*)(op.packet+20)) = (ogg_int64_t)fp->ptime_d; /* presentationtime denominator */
-    *((ogg_int64_t*)(op.packet+28)) = (ogg_int64_t)fp->btime_n; /* basetime numerator */
-    *((ogg_int64_t*)(op.packet+36)) = (ogg_int64_t)fp->btime_d; /* basetime denominator */
+    *((ogg_uint16_t*)(op.packet+8)) = _le_16 (SKELETON_VERSION_MAJOR); /* version major */
+    *((ogg_uint16_t*)(op.packet+10)) = _le_16 (SKELETON_VERSION_MINOR); /* version minor */
+    *((ogg_int64_t*)(op.packet+12)) = _le_64 (fp->ptime_n); /* presentationtime numerator */
+    *((ogg_int64_t*)(op.packet+20)) = _le_64 (fp->ptime_d); /* presentationtime denominator */
+    *((ogg_int64_t*)(op.packet+28)) = _le_64 (fp->btime_n); /* basetime numerator */
+    *((ogg_int64_t*)(op.packet+36)) = _le_64 (fp->btime_d); /* basetime denominator */
     /* TODO: UTC time, set to zero for now */
 
     op.b_o_s = 1;   /* its the first packet of the stream */
@@ -63,21 +131,21 @@
  * by calling add_message_header_field method.
  */
 ogg_packet ogg_from_fisbone(fisbone_packet *fp) {
-
+    
     ogg_packet op;
     int packet_size = FISBONE_SIZE + fp->current_header_size;
 
-    memset (&op, 0, sizeof (op));
+    memset (&op, 0, sizeof (op));       
     op.packet = _ogg_calloc (packet_size, sizeof(unsigned char));
     memset (op.packet, 0, packet_size);
     memcpy (op.packet, FISBONE_IDENTIFIER, 8); /* identifier */
-    *((ogg_uint32_t*)(op.packet+8)) = FISBONE_MESSAGE_HEADER_OFFSET; /* offset of the message header fields */
-    *((ogg_uint32_t*)(op.packet+12)) = fp->serial_no; /* serialno of the respective stream */
-    *((ogg_uint32_t*)(op.packet+16)) = fp->nr_header_packet; /* number of header packets */
-    *((ogg_int64_t*)(op.packet+20)) = fp->granule_rate_n; /* granulrate numerator */
-    *((ogg_int64_t*)(op.packet+28)) = fp->granule_rate_d; /* granulrate denominator */
-    *((ogg_int64_t*)(op.packet+36)) = fp->start_granule; /* start granule */
-    *((ogg_uint32_t*)(op.packet+44)) = fp->preroll; /* preroll, for theora its 0 */
+    *((ogg_uint32_t*)(op.packet+8)) = _le_32 (FISBONE_MESSAGE_HEADER_OFFSET); /* offset of the message header fields */
+    *((ogg_uint32_t*)(op.packet+12)) = _le_32 (fp->serial_no); /* serialno of the respective stream */
+    *((ogg_uint32_t*)(op.packet+16)) = _le_32 (fp->nr_header_packet); /* number of header packets */
+    *((ogg_int64_t*)(op.packet+20)) = _le_64 (fp->granule_rate_n); /* granulrate numerator */
+    *((ogg_int64_t*)(op.packet+28)) = _le_64 (fp->granule_rate_d); /* granulrate denominator */
+    *((ogg_int64_t*)(op.packet+36)) = _le_64 (fp->start_granule); /* start granule */
+    *((ogg_uint32_t*)(op.packet+44)) = _le_32 (fp->preroll); /* preroll, for theora its 0 */
     *(op.packet+48) = fp->granule_shift; /* granule shift */
     memcpy((op.packet+FISBONE_SIZE), fp->message_header_fields, fp->current_header_size);
 
@@ -116,13 +184,14 @@
 
     memset (&op, 0, sizeof(op));
     op.e_o_s = 1;
-    ogg_stream_packetin(os, &op);
+
+    return ogg_stream_packetin(os, &op);
 }
 
 int flush_ogg_stream_to_file(ogg_stream_state *os, FILE *out) {
 
     ogg_page og;
-    int result, ret;
+    int result;
 
     while((result = ogg_stream_flush(os, &og))) {
         if(!result) break;
@@ -130,4 +199,6 @@
         if(result != og.header_len + og.body_len)
             return 1;
     }
-}
\ No newline at end of file
+
+    return 0;
+}



More information about the commits mailing list