From 639c372a227db67f2dbd6d6f04e71d7c156c0a72 Mon Sep 17 00:00:00 2001 From: George Vardakis Date: Thu, 2 Feb 2017 21:05:02 +0200 Subject: [PATCH] Implement ogg encoder block --- grc/CMakeLists.txt | 2 +- grc/satnogs_ogg_enc.xml | 38 ++++++++++ include/satnogs/CMakeLists.txt | 2 +- include/satnogs/ogg_enc.h | 54 +++++++++++++ lib/CMakeLists.txt | 7 +- lib/ogg_enc_impl.cc | 135 +++++++++++++++++++++++++++++++++ lib/ogg_enc_impl.h | 61 +++++++++++++++ swig/satnogs_swig.i | 3 + 8 files changed, 299 insertions(+), 3 deletions(-) create mode 100644 grc/satnogs_ogg_enc.xml create mode 100644 include/satnogs/ogg_enc.h create mode 100644 lib/ogg_enc_impl.cc create mode 100644 lib/ogg_enc_impl.h diff --git a/grc/CMakeLists.txt b/grc/CMakeLists.txt index 40a7195..40d66ab 100644 --- a/grc/CMakeLists.txt +++ b/grc/CMakeLists.txt @@ -48,5 +48,5 @@ if(${INCLUDE_DEBUG_BLOCKS}) endif() install(FILES ${enabled_blocks} - DESTINATION share/gnuradio/grc/blocks + satnogs_ogg_enc.xml DESTINATION share/gnuradio/grc/blocks ) diff --git a/grc/satnogs_ogg_enc.xml b/grc/satnogs_ogg_enc.xml new file mode 100644 index 0000000..d5a47f5 --- /dev/null +++ b/grc/satnogs_ogg_enc.xml @@ -0,0 +1,38 @@ + + + ogg_enc + satnogs_ogg_enc + [satnogs] + import satnogs + satnogs.ogg_enc($filename, $samp_rate, $quality) + + + ... + ... + ... + + + + + in + + + + + + out + + + diff --git a/include/satnogs/CMakeLists.txt b/include/satnogs/CMakeLists.txt index 847f492..7f65a8a 100644 --- a/include/satnogs/CMakeLists.txt +++ b/include/satnogs/CMakeLists.txt @@ -62,5 +62,5 @@ if(${INCLUDE_DEBUG_BLOCKS}) endif() install(FILES ${HEADER_FILES} - DESTINATION include/satnogs + ogg_enc.h DESTINATION include/satnogs ) \ No newline at end of file diff --git a/include/satnogs/ogg_enc.h b/include/satnogs/ogg_enc.h new file mode 100644 index 0000000..45bf948 --- /dev/null +++ b/include/satnogs/ogg_enc.h @@ -0,0 +1,54 @@ +/* -*- c++ -*- */ +/* + * gr-satnogs: SatNOGS GNU Radio Out-Of-Tree Module + * + * Copyright (C) 2017, Libre Space Foundation + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef INCLUDED_SATNOGS_OGG_ENCODER_H +#define INCLUDED_SATNOGS_OGG_ENCODER_H + +#include +#include + +namespace gr { + namespace satnogs { + + /*! + * \brief <+description of block+> + * \ingroup satnogs + * + */ + class SATNOGS_API ogg_encoder : virtual public gr::sync_block + { + public: + typedef boost::shared_ptr sptr; + + /*! + * \brief Return a shared_ptr to a new instance of satnogs::ogg_encoder. + * + * To avoid accidental use of raw pointers, satnogs::ogg_encoder's + * constructor is in a private implementation + * class. satnogs::ogg_encoder::make is the public interface for + * creating new instances. + */ + static sptr make(char* filename, double samp_rate, float quality); + }; + + } // namespace satnogs +} // namespace gr + +#endif /* INCLUDED_SATNOGS_OGG_ENCODER_H */ diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index aeb1960..8f7f8cd 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -25,6 +25,7 @@ include(GrPlatform) #define LIB_SUFFIX include_directories( ${Boost_INCLUDE_DIR} ${VOLK_INCLUDE_DIRS} + ${OGG_INCLUDE_DIRS} ) link_directories(${Boost_LIBRARY_DIRS}) @@ -56,7 +57,8 @@ list(APPEND satnogs_sources ax25_encoder_mb_impl.cc ax25_decoder_bm_impl.cc qb50_deframer_impl.cc - waterfall_sink_impl.cc ) + waterfall_sink_impl.cc + ogg_enc_impl.cc ) if(${INCLUDE_DEBUG_BLOCKS}) list(APPEND satnogs_sources ${satnogs_debug_sources}) @@ -76,6 +78,9 @@ target_link_libraries(gnuradio-satnogs ${CMAKE_THREAD_LIBS_INIT} ${NOVA_LIBRARIES} ${VOLK_LIBRARIES} + ogg + vorbis + vorbisenc ) set_target_properties(gnuradio-satnogs PROPERTIES DEFINE_SYMBOL "gnuradio_satnogs_EXPORTS") diff --git a/lib/ogg_enc_impl.cc b/lib/ogg_enc_impl.cc new file mode 100644 index 0000000..8d63b73 --- /dev/null +++ b/lib/ogg_enc_impl.cc @@ -0,0 +1,135 @@ +/* -*- c++ -*- */ +/* + * gr-satnogs: SatNOGS GNU Radio Out-Of-Tree Module + * + * Copyright (C) 2017, Libre Space Foundation + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include +#include "ogg_encoder_impl.h" + +#include +#include +#include +#include +#include + + +namespace gr { + namespace satnogs { + + ogg_encoder::sptr + ogg_encoder::make(char* filename, double samp_rate, float quality) + { + return gnuradio::get_initial_sptr + (new ogg_encoder_impl(filename, samp_rate, quality)); + } + + /* + * The private constructor + */ + ogg_encoder_impl::ogg_encoder_impl(char* filename, double samp_rate, float quality) + : gr::sync_block("ogg_encoder", + gr::io_signature::make(1, 1, sizeof(float)), + gr::io_signature::make(0, 0, 0)) + { + d_quality = quality; + d_out=fopen(filename,"wb"); + d_samp_rate = samp_rate; + vorbis_info_init(&d_vi); + int ret = vorbis_encode_init_vbr(&d_vi,1,d_samp_rate,d_quality); + if(ret)exit(1); + + vorbis_comment_init(&d_vc); + vorbis_comment_add_tag(&d_vc, "ENCODER", "satnogs ogg encoder"); + + vorbis_analysis_init(&d_vd, &d_vi); + vorbis_block_init(&d_vd, &d_vb); + + srand(time(NULL)); + ogg_stream_init(&d_os, rand()); + + ogg_packet header; + ogg_packet header_comm; + ogg_packet header_code; + + vorbis_analysis_headerout(&d_vd, &d_vc, &header, &header_comm, &header_code); + ogg_stream_packetin(&d_os, &header); + ogg_stream_packetin(&d_os, &header_comm); + ogg_stream_packetin(&d_os, &header_code); + int result = 1; + while(result){ + result=ogg_stream_flush(&d_os,&d_og); + if(result==0)break; + fwrite(d_og.header,1,d_og.header_len,d_out); + fwrite(d_og.body,1,d_og.body_len,d_out); + } + } + + ogg_encoder_impl::~ogg_encoder_impl() + { + vorbis_analysis_wrote(&d_vd,0); + ogg_stream_clear(&d_os); + vorbis_block_clear(&d_vb); + vorbis_dsp_clear(&d_vd); + vorbis_comment_clear(&d_vc); + vorbis_info_clear(&d_vi); + fclose(d_out); + } + + int + ogg_encoder_impl::work(int noutput_items, + gr_vector_const_void_star &input_items, + gr_vector_void_star &output_items) + { + size_t chunks = (noutput_items*sizeof(float))/2; + const signed char *in = (const signed char *) input_items[0]; + int i; + long bytes = 1024; + float **buffer=vorbis_analysis_buffer(&d_vd,chunks); + for(i=0;i + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + + +#ifndef INCLUDED_SATNOGS_OGG_ENCODER_IMPL_H +#define INCLUDED_SATNOGS_OGG_ENCODER_IMPL_H + +#include +#include + +namespace gr { + namespace satnogs { + + class ogg_encoder_impl : public ogg_encoder + { + private: + // Nothing to declare in this block. + ogg_stream_state d_os; + ogg_page d_og; + ogg_packet d_op; + + vorbis_info d_vi; + vorbis_comment d_vc; + + vorbis_dsp_state d_vd; + vorbis_block d_vb; + FILE* d_out; + double d_samp_rate; + float d_quality; + + public: + ogg_encoder_impl(char* filename, double samp_rate, float quality); + ~ogg_encoder_impl(); + + // Where all the action really happens + int work(int noutput_items, + gr_vector_const_void_star &input_items, + gr_vector_void_star &output_items); + }; + + } // namespace satnogs +} // namespace gr + +#endif /* INCLUDED_SATNOGS_OGG_ENCODER_IMPL_H */ diff --git a/swig/satnogs_swig.i b/swig/satnogs_swig.i index 4c6b730..95ad4a6 100644 --- a/swig/satnogs_swig.i +++ b/swig/satnogs_swig.i @@ -31,6 +31,7 @@ #include "satnogs/ax25_decoder_bm.h" #include "satnogs/qb50_deframer.h" #include "satnogs/waterfall_sink.h" +#include "satnogs/ogg_enc.h" %} @@ -97,3 +98,5 @@ GR_SWIG_BLOCK_MAGIC2(satnogs, qb50_deframer); %include "satnogs/waterfall_sink.h" GR_SWIG_BLOCK_MAGIC2(satnogs, waterfall_sink); +%include "satnogs/ogg_enc.h" +GR_SWIG_BLOCK_MAGIC2(satnogs, ogg_enc);