Implement ogg encoder block

This commit is contained in:
George Vardakis 2017-02-02 21:05:02 +02:00
parent 10b0356f05
commit 639c372a22
8 changed files with 299 additions and 3 deletions

View File

@ -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
)

38
grc/satnogs_ogg_enc.xml Normal file
View File

@ -0,0 +1,38 @@
<?xml version="1.0"?>
<block>
<name>ogg_enc</name>
<key>satnogs_ogg_enc</key>
<category>[satnogs]</category>
<import>import satnogs</import>
<make>satnogs.ogg_enc($filename, $samp_rate, $quality)</make>
<!-- Make one 'param' node for every Parameter you want settable from the GUI.
Sub-nodes:
* name
* key (makes the value accessible as $keyname, e.g. in the make node)
* type -->
<param>
<name>...</name>
<key>...</key>
<type>...</type>
</param>
<!-- Make one 'sink' node per input. Sub-nodes:
* name (an identifier for the GUI)
* type
* vlen
* optional (set to 1 for optional inputs) -->
<sink>
<name>in</name>
<type><!-- e.g. int, float, complex, byte, short, xxx_vector, ...--></type>
</sink>
<!-- Make one 'source' node per output. Sub-nodes:
* name (an identifier for the GUI)
* type
* vlen
* optional (set to 1 for optional inputs) -->
<source>
<name>out</name>
<type><!-- e.g. int, float, complex, byte, short, xxx_vector, ...--></type>
</source>
</block>

View File

@ -62,5 +62,5 @@ if(${INCLUDE_DEBUG_BLOCKS})
endif()
install(FILES
${HEADER_FILES}
DESTINATION include/satnogs
ogg_enc.h DESTINATION include/satnogs
)

54
include/satnogs/ogg_enc.h Normal file
View File

@ -0,0 +1,54 @@
/* -*- c++ -*- */
/*
* gr-satnogs: SatNOGS GNU Radio Out-Of-Tree Module
*
* Copyright (C) 2017, Libre Space Foundation <http://librespacefoundation.org/>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#ifndef INCLUDED_SATNOGS_OGG_ENCODER_H
#define INCLUDED_SATNOGS_OGG_ENCODER_H
#include <satnogs/api.h>
#include <gnuradio/sync_block.h>
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<ogg_encoder> 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 */

View File

@ -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")

135
lib/ogg_enc_impl.cc Normal file
View File

@ -0,0 +1,135 @@
/* -*- c++ -*- */
/*
* gr-satnogs: SatNOGS GNU Radio Out-Of-Tree Module
*
* Copyright (C) 2017, Libre Space Foundation <http://librespacefoundation.org/>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <gnuradio/io_signature.h>
#include "ogg_encoder_impl.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <math.h>
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<chunks;i++){
buffer[0][i]=((in[i*2+1]<<8)|
(0x00ff&(int)in[i*2]))/32768.f;
}
vorbis_analysis_wrote(&d_vd,i);
while(vorbis_analysis_blockout(&d_vd,&d_vb)==1){
vorbis_analysis(&d_vb,NULL);
vorbis_bitrate_addblock(&d_vb);
while(vorbis_bitrate_flushpacket(&d_vd,&d_op)){
ogg_stream_packetin(&d_os,&d_op);
int result = 1;
while(result){
int result=ogg_stream_pageout(&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);
if(ogg_page_eos(&d_og))result=1;
}
}
}
return noutput_items;
}
} /* namespace satnogs */
} /* namespace gr */

61
lib/ogg_enc_impl.h Normal file
View File

@ -0,0 +1,61 @@
/* -*- c++ -*- */
/*
* gr-satnogs: SatNOGS GNU Radio Out-Of-Tree Module
*
* Copyright (C) 2017, Libre Space Foundation <http://librespacefoundation.org/>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#ifndef INCLUDED_SATNOGS_OGG_ENCODER_IMPL_H
#define INCLUDED_SATNOGS_OGG_ENCODER_IMPL_H
#include <satnogs/ogg_encoder.h>
#include <vorbis/vorbisenc.h>
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 */

View File

@ -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);