[xiph-cvs] cvs commit: vorbis-tools/oggenc audio.c audio.h encode.h oggenc.c

Michael Smith msmith at xiph.org
Wed Nov 29 22:04:46 PST 2000



msmith      00/11/29 22:04:46

  Modified:    oggenc   audio.c audio.h encode.h oggenc.c
  Log:
  Rewrite of the audio file opening/reading code so that plugging in another
  module (using something like libaudiofile or libsndfile) should actually
  work - previously, things just swallowed data, which would make things
  impossible on unseekable streams.

Revision  Changes    Path
1.5       +65 -11    vorbis-tools/oggenc/audio.c

Index: audio.c
===================================================================
RCS file: /usr/local/cvsroot/vorbis-tools/oggenc/audio.c,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- audio.c	2000/11/30 04:59:17	1.4
+++ audio.c	2000/11/30 06:04:45	1.5
@@ -25,6 +25,54 @@
 #define READ_U16(buf) \
         (((buf)[1]<<8)|((buf)[0]&0xff));
 
+/* Define the supported formats here */
+input_format formats[] = {
+	{wav_id, 12, wav_open, wav_close, "wav", "WAV file reader"},
+	{NULL, 0, NULL, NULL, NULL, NULL}
+};
+
+input_format *open_audio_file(FILE *in, oe_enc_opt *opt)
+{
+	int j=0;
+	unsigned char *buf=NULL;
+	int buf_size=0, buf_filled=0;
+	int size,ret;
+
+	while(formats[j].id_func)
+	{
+		size = formats[j].id_data_len;
+		if(size >= buf_size)
+		{
+			buf = realloc(buf, size);
+			buf_size = size;
+		}
+
+		if(buf_size > buf_filled)
+		{
+			ret = fread(buf+buf_filled, 1, buf_size-buf_filled, in);
+			buf_filled += ret;
+
+			if(buf_filled != buf_size)
+			{ /* File truncated */
+				buf_size = buf_filled;
+				j++;
+				continue;
+			}
+		}
+
+		if(formats[j].id_func(buf, size))
+		{
+			/* ok, we now have something that can handle the file */
+			if(formats[j].open_func(in, opt))
+				return &formats[j];
+		}
+		j++;
+	}
+
+	return NULL;
+}
+
+
 static int find_chunk(FILE *in, char *type, unsigned int *len)
 {
         unsigned char buf[8];
@@ -65,23 +113,29 @@
         }
 }
 
+int wav_id(unsigned char *buf, int len)
+{
+	unsigned int flen;
+	
+	if(len<12) return 0; /* Something screwed up */
+
+	if(memcmp(buf, "RIFF", 4))
+		return 0; /* Not wave */
+
+	flen = READ_U32(buf+4); /* We don't use this */
+
+	if(memcmp(buf+8, "WAVE",4))
+		return 0; /* RIFF, but not wave */
+
+	return 1;
+}
+
 int wav_open(FILE *in, oe_enc_opt *opt)
 {
         unsigned char buf[16];
         unsigned int len;
         wav_fmt format;
         wavfile *wav = malloc(sizeof(wavfile));
-
-	int ret = fread(buf, 1, 12, in);
-	if(ret < 12)
-		return 0;
-
-	if(memcmp(buf, "RIFF", 4))
-		return 0;
-
-	len = READ_U32(buf+4); /* We don't actually use this */
-	if(memcmp(buf+8, "WAVE",4))
-		return 0; /* Not wave file */
 
         /* Ok. At this point, we know we have a WAV file. Now we have to detect
          * whether we support the subtype, and we have to find the actual data

1.2       +14 -0     vorbis-tools/oggenc/audio.h

Index: audio.h
===================================================================
RCS file: /usr/local/cvsroot/vorbis-tools/oggenc/audio.h,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- audio.h	2000/09/07 00:57:46	1.1
+++ audio.h	2000/11/30 06:04:45	1.2
@@ -5,6 +5,17 @@
 #include "encode.h"
 #include <stdio.h>
 
+typedef struct
+{
+	int (*id_func)(unsigned char *buf, int len); /* Returns true if can load file */
+	int id_data_len; /* Amount of data needed to id whether this can load the file */
+	int (*open_func)(FILE *in, oe_enc_opt *opt);
+	void (*close_func)(void *);
+	char *format;
+	char *description;
+} input_format;
+
+
 typedef struct {
         short format;
         short channels;
@@ -20,8 +31,11 @@
         FILE *f;
 } wavfile;
 
+input_format *open_audio_file(FILE *in, oe_enc_opt *opt);
+
 int wav_open(FILE *in, oe_enc_opt *opt);
 int raw_open(FILE *in, oe_enc_opt *opt);
+int wav_id(unsigned char *buf, int len);
 void wav_close(void *);
 void raw_close(void *);
 

1.5       +0 -8      vorbis-tools/oggenc/encode.h

Index: encode.h
===================================================================
RCS file: /usr/local/cvsroot/vorbis-tools/oggenc/encode.h,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- encode.h	2000/11/05 04:52:21	1.4
+++ encode.h	2000/11/30 06:04:45	1.5
@@ -74,12 +74,4 @@
 
 int oe_encode(oe_enc_opt *opt);
 
-typedef struct
-{
-	int (*open_func)(FILE *in, oe_enc_opt *opt);
-	void (*close_func)(void *);
-	char *format;
-	char *description;
-} input_format;
-
 #endif /* __ENCODE_H */

1.8       +9 -16     vorbis-tools/oggenc/oggenc.c

Index: oggenc.c
===================================================================
RCS file: /usr/local/cvsroot/vorbis-tools/oggenc/oggenc.c,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -r1.7 -r1.8
--- oggenc.c	2000/11/23 01:09:44	1.7
+++ oggenc.c	2000/11/30 06:04:45	1.8
@@ -24,12 +24,6 @@
 #define COPYRIGHT "(c) 2000 Michael Smith <msmith at labyrinth.net.au)\n"
 #define CHUNK 4096 /* We do reads, etc. in multiples of this */
 
-/* Define supported formats here */
-input_format formats[] = {
-	{wav_open, wav_close, "wav", "WAV file reader"},
-	{NULL, NULL, NULL, NULL}
-};
-
 struct option long_options[] = {
         {"quiet",0,0,'q'},
         {"help",0,0,'h'},
@@ -107,8 +101,8 @@
                 FILE *in, *out = NULL;
                 int foundformat = 0;
                 int closeout = 0, closein = 0;
-		int j=0;
                 char *artist=NULL, *album=NULL, *title=NULL, *track=NULL, *date=NULL;
+		input_format *format;
 
 
 
@@ -157,15 +151,14 @@
                 }
                 else
                 {
-			while(formats[j].open_func)
+			format = open_audio_file(in, &enc_opts);
+			if(format)
                         {
-				if(formats[j].open_func(in, &enc_opts))
-				{
-					foundformat = 1;
-					break;
-				}
-				j++;
+				fprintf(stderr, "Opening with %s module: %s\n", 
+						format->format, format->description);
+				foundformat=1;
                         }
+
                 }
 
                 if(!foundformat)
@@ -244,8 +237,8 @@
 
                 if(out_fn) free(out_fn);
                 vorbis_comment_clear(&vc);
-		if(!opt.rawmode)
-			formats[j].close_func(enc_opts.readdata);
+		if(!opt.rawmode) 
+			format->close_func(enc_opts.readdata);
 
                 if(closein)
                         fclose(in);

--- >8 ----
List archives:  http://www.xiph.org/archives/
Ogg project homepage: http://www.xiph.org/ogg/
To unsubscribe from this list, send a message to 'cvs-request at xiph.org'
containing only the word 'unsubscribe' in the body.  No subject is needed.
Unsubscribe messages sent to the list will be ignored/filtered.



More information about the commits mailing list