From cc02d8894a6573cbd616c89c18305092cae31d96 Mon Sep 17 00:00:00 2001 From: thor Date: Sat, 12 Mar 2011 19:01:19 +0000 Subject: [PATCH] Add example code to exercise mpg123_framedata: An MPEG stream cleaner (just extracting all valid frames). Also updating the examples Makefile a bit. git-svn-id: svn://scm.orgis.org/mpg123/trunk@2911 35dc7657-300d-0410-a2e5-dc2837fedb53 --- doc/examples/Makefile | 14 ++++-- doc/examples/extract_frames.c | 92 +++++++++++++++++++++++++++++++++++ 2 files changed, 103 insertions(+), 3 deletions(-) create mode 100644 doc/examples/extract_frames.c diff --git a/doc/examples/Makefile b/doc/examples/Makefile index 93d1f0cb..52f1b07a 100644 --- a/doc/examples/Makefile +++ b/doc/examples/Makefile @@ -1,4 +1,4 @@ -targets = feedseek mpg123_to_wav mpg123_to_wav_replaced_io scan id3dump mpglib dump_seekindex +targets = feedseek mpg123_to_wav mpg123_to_wav_replaced_io scan id3dump mpglib dump_seekindex extract_frames all: $(targets) @@ -6,8 +6,13 @@ all: $(targets) #MPG123_CFLAGS := $(shell pkg-config --cflags libmpg123_64) #MPG123_LDFLAGS := $(shell pkg-config --libs libmpg123_64) # This works on sane setups where off_t is off_t, and just that. -MPG123_CFLAGS := $(shell pkg-config --cflags libmpg123) -MPG123_LDFLAGS := $(shell pkg-config --libs libmpg123) +ifeq ($(MPG123_PREFIX),) + MPG123_CFLAGS := $(shell pkg-config --cflags libmpg123) + MPG123_LDFLAGS := $(shell pkg-config --libs libmpg123) +else # Yeah, that's with GNU/Linux in mind, at least GNU ld ... + MPG123_CFLAGS := -I$(MPG123_PREFIX)/include + MPG123_LDFLAGS := -L$(MPG123_PREFIX)/lib -Wl,-rpath $(MPG123_PREFIX)/lib -lmpg123 +endif SND_CFLAGS := $(shell pkg-config --cflags sndfile) SND_LDFLAGS := $(shell pkg-config --libs sndfile) @@ -36,5 +41,8 @@ dump_seekindex: dump_seekindex.c mpglib: mpglib.c $(compile) -o mpglib mpglib.c $(linkflags) +extract_frames: extract_frames.c + $(compile) -o $@ $< $(linkflags) + clean: rm -vf $(targets) diff --git a/doc/examples/extract_frames.c b/doc/examples/extract_frames.c new file mode 100644 index 00000000..9f5ff73c --- /dev/null +++ b/doc/examples/extract_frames.c @@ -0,0 +1,92 @@ +/* + extract_frams: utlize the framebyframe API and mpg123_framedata to extract the MPEG frames out of a stream (strip off anything else). + + copyright 2011 by the mpg123 project - free software under the terms of the LGPL 2.1 + see COPYING and AUTHORS files in distribution or http://mpg123.org + initially written by Thomas Orgis +*/ + +#include + +/* unistd.h is not available under MSVC, + io.h defines the read and write functions */ +#ifndef _MSC_VER +#include +#else +#include +#endif + +#include + +int do_work(mpg123_handle *m); + +int main(int argc, char **argv) +{ + int ret = 0; + mpg123_handle *m; + + mpg123_init(); + m = mpg123_new(NULL, &ret); + + if(m == NULL) + { + fprintf(stderr, "Cannot create handle: %s", mpg123_plain_strerror(ret)); + } + else + { + fprintf(stderr, "I'll take your dirty MPEG audio from standard input and will write the extrated pure MPEG data to stadard output.\n"); + if(argc > 1 && strcmp(argv[1], "--noinfo") == 0) + { + fprintf(stderr, "Enabling parsing/consuming of the Info frame so that it will not appear in output.\n"); + ret = mpg123_param(m, MPG123_REMOVE_FLAGS, MPG123_IGNORE_INFOFRAME, 0.); + } + else + { + fprintf(stderr, "If you'd have given --noinfo as argument, I would omit a LAME/Xing info frame.\n"); + ret = mpg123_param(m, MPG123_ADD_FLAGS, MPG123_IGNORE_INFOFRAME, 0.); + } + if(ret == 0) ret = do_work(m); + + if(ret != 0) fprintf(stderr, "Some error occured: %s\n", mpg123_strerror(m)); + + + mpg123_delete(m); /* Closes, too. */ + } + mpg123_exit(); + + return ret; +} + +int do_work(mpg123_handle *m) +{ + int ret; + size_t count = 0; + ret = mpg123_open_fd(m, STDIN_FILENO); + if(ret != MPG123_OK) return ret; + + while( (ret = mpg123_framebyframe_next(m)) == MPG123_OK || ret == MPG123_NEW_FORMAT ) + { + unsigned long header; + unsigned char *bodydata; + size_t bodybytes; + if(mpg123_framedata(m, &header, &bodydata, &bodybytes) == MPG123_OK) + { + /* Need to extract the 4 header bytes from the native storage in the correct order. */ + unsigned char hbuf[4]; + int i; + for(i=0; i<4; ++i) hbuf[i] = (unsigned char) ((header >> ((3-i)*8)) & 0xff); + + /* Now write out both header and data, fire and forget. */ + write(STDOUT_FILENO, hbuf, 4); + write(STDOUT_FILENO, bodydata, bodybytes); + fprintf(stderr, "%zu: header 0x%08x, %zu body bytes\n", ++count, header, bodybytes); + } + } + + if(ret != MPG123_DONE) + fprintf(stderr, "Some error occured (non-fatal?): %s\n", mpg123_strerror(m)); + + fprintf(stderr, "Done with %zu MPEG frames.\n", count); + + return 0; +}