Generating specific tones from user input with portaudio - c++

I am struggling with outputting user selected audio tones with portaudio. I have based a function on the paex_sine.cpp and modified it somewhat based on this post multi-audio-tones-to-sound-card-using-portaudio
I am able to generate a sine wave on the fly by placing the sin(n * FREQ * 2 * PI / SAMPLE_RATE) calculation in paCallBackMethod() but the frequency isn't correct, and doesn't change based on user input.
Here is my function code.
void CDeepWaveDlg::generateSound(float freq)
{
pitch = tone;
offset = freq;
class Sine
{
public:
Sine() : stream(0), left_phase(0), right_phase(0)
{
}
bool open(PaDeviceIndex index)
{
PaStreamParameters outputParameters;
outputParameters.device = index;
if (outputParameters.device == paNoDevice) {
return false;
}
outputParameters.channelCount = 2; /* stereo output */
outputParameters.sampleFormat = paFloat32; /* 32 bit floating point output */
outputParameters.suggestedLatency = Pa_GetDeviceInfo(outputParameters.device)->defaultLowOutputLatency;
outputParameters.hostApiSpecificStreamInfo = NULL;
PaError err = Pa_OpenStream(
&stream,
NULL, /* no input */
&outputParameters,
SAMPLE_RATE,
FRAMES_PER_BUFFER,
paClipOff, /* we won't output out of range samples so don't bother clipping them */
&Sine::paCallback,
this /* Using 'this' for userData so we can cast to Sine* in paCallback method */
);
if (err != paNoError)
{
/* Failed to open stream to device !!! */
return false;
}
err = Pa_SetStreamFinishedCallback(stream, &Sine::paStreamFinished);
if (err != paNoError)
{
Pa_CloseStream(stream);
stream = 0;
return false;
}
return true;
}
bool close()
{
if (stream == 0)
return false;
PaError err = Pa_CloseStream(stream);
stream = 0;
return (err == paNoError);
}
bool start()
{
if (stream == 0)
return false;
PaError err = Pa_StartStream(stream);
return (err == paNoError);
}
bool stop()
{
if (stream == 0)
return false;
PaError err = Pa_StopStream(stream);
return (err == paNoError);
}
private:
/* The instance callback, where we have access to every method/variable in object of class Sine */
int paCallbackMethod(const void *inputBuffer, void *outputBuffer,
unsigned long framesPerBuffer,
const PaStreamCallbackTimeInfo* timeInfo,
PaStreamCallbackFlags statusFlags)
{
float *out = (float*)outputBuffer;
unsigned long i;
(void)timeInfo; /* Prevent unused variable warnings. */
(void)statusFlags;
(void)inputBuffer;
for (i = 0; i<framesPerBuffer; i++)
{
float v = sin(i * pitch * 2.0 * M_PI /(float)SAMPLE_RATE);
float v2 = sin(i * (pitch + (float)offset) * 2.0 * M_PI /(float)SAMPLE_RATE);
*out++ = v;
*out++ = v2;
}
return paContinue;
}
/* This routine will be called by the PortAudio engine when audio is needed.
** It may called at interrupt level on some machines so don't do anything
** that could mess up the system like calling malloc() or free().
*/
static int paCallback(const void *inputBuffer, void *outputBuffer,
unsigned long framesPerBuffer,
const PaStreamCallbackTimeInfo* timeInfo,
PaStreamCallbackFlags statusFlags,
void *userData)
{
/* Here we cast userData to Sine* type so we can call the instance method paCallbackMethod, we can do that since
we called Pa_OpenStream with 'this' for userData */
return ((Sine*)userData)->paCallbackMethod(inputBuffer, outputBuffer,
framesPerBuffer,
timeInfo,
statusFlags);
}
void paStreamFinishedMethod()
{
printf("Stream Completed: %s\n", message);
}
/*
* This routine is called by portaudio when playback is done.
*/
static void paStreamFinished(void* userData)
{
return ((Sine*)userData)->paStreamFinishedMethod();
}
PaStream *stream;
float sine[TABLE_SIZE];
int left_phase;
int right_phase;
char message[20];
};
PaError err;
Sine sine;
err = Pa_Initialize();
if (err != paNoError) goto error;
if (sine.open(Pa_GetDefaultOutputDevice()))
{
if (sine.start())
{
Pa_Sleep(NUM_SECONDS * 1000);
sine.stop();
}
sine.close();
}
Pa_Terminate();
The only way I am able to get close to the desired pitch is to increase frames per buffer, which is currently 64, but I still run into the problem of not changing frequency based on user selection.
This problem is definitely beyond my skill level, but I am hoping someone can help me understand what's going on here.Thanks in advance for any help.

Related

Why does copying to portaudio input buffer with memcpy is creating noise?

I'm just trying to copy the input buffer of portaudio in a callback function. Thats my callback:
static int recordCallback( const void *inputBuffer, void *outputBuffer,
unsigned long framesPerBuffer,
const PaStreamCallbackTimeInfo* timeInfo,
PaStreamCallbackFlags statusFlags,
void *userData )
{
paTestData *data = (paTestData*)userData;
const SAMPLE *rptr = (const SAMPLE*)inputBuffer;
SAMPLE *wptr = &data->recordedSamples[data->frameIndex * NUM_CHANNELS];
long framesToCalc;
long i;
int finished;
unsigned long framesLeft = data->maxFrameIndex - data->frameIndex;
memcpy(&circularBuffer[data->write], inputBuffer, 1024); // Assuming samplesPerFrame = FRAME_SIZE
data->write = data->write + 1024;
(void) outputBuffer; /* Prevent unused variable warnings. */
(void) timeInfo;
(void) statusFlags;
(void) userData;
if( framesLeft < framesPerBuffer )
{
framesToCalc = framesLeft;
finished = paComplete;
}
else
{
framesToCalc = framesPerBuffer;
finished = paContinue;
}
data->frameIndex += framesToCalc;
return finished;
}
And this is the original callback from portaudio examples:
static int recordCallback( const void *inputBuffer, void *outputBuffer,
unsigned long framesPerBuffer,
const PaStreamCallbackTimeInfo* timeInfo,
PaStreamCallbackFlags statusFlags,
void *userData )
{
paTestData *data = (paTestData*)userData;
const SAMPLE *rptr = (const SAMPLE*)inputBuffer;
SAMPLE *wptr = &data->recordedSamples[data->frameIndex * NUM_CHANNELS];
long framesToCalc;
long i;
int finished;
unsigned long framesLeft = data->maxFrameIndex - data->frameIndex;
(void) outputBuffer; /* Prevent unused variable warnings. */
(void) timeInfo;
(void) statusFlags;
(void) userData;
if( framesLeft < framesPerBuffer )
{
framesToCalc = framesLeft;
finished = paComplete;
}
else
{
framesToCalc = framesPerBuffer;
finished = paContinue;
}
if( inputBuffer == NULL )
{
for( i=0; i<framesToCalc; i++ )
{
*wptr++ = SAMPLE_SILENCE; /* left */
if( NUM_CHANNELS == 2 ) *wptr++ = SAMPLE_SILENCE; /* right */
}
}
else
{
for( i=0; i<framesToCalc; i++ )
{
*wptr++ = *rptr++; /* left */
if( NUM_CHANNELS == 2 ) *wptr++ = *rptr++; /* right */
}
}
data->frameIndex += framesToCalc;
return finished;
}
And I'm just using memcpy instead of to iterate pointer. But the sound I record has so much noise. I can't figure out the problem. Maybe, I'm missing the channels(2 channels) but I'm not sure. Do you have any idea?
You can access the full code from here
The approach is true but I couldn`t specify the frame size correctly
Instead of this:
memcpy(&circularBuffer[data->write], inputBuffer, 1024); // 1024 = FRAME_SIZE
This will be better:
memcpy(&circularBuffer[data->write], inputBuffer, 1024*sizeof(float)); // Assuming 1024 = FRAME_SIZE
because of:
typedef float SAMPLE; is 4 bytes.

PortAudio: short noise at start and end of sound

I have piano-like application, and when I'm pressing piano key, it plays a sound. In my app I wrote my own sine wave generator. App is wrote in Qt. I think that problem is with portAudio, but I can't find any solution for this.
I've recorded for you how my problem sounds: https://vocaroo.com/i/s1yiWjaJffTU
And here is my generator class:
soundEngine.h
#ifndef SOUNDENGINE_H
#define SOUNDENGINE_H
#include <QThread>
#include <math.h>
#include "portaudio.h"
#define SAMPLE_RATE (44100)
#define FRAMES_PER_BUFFER (64)
#define FREQUENCY 220
#ifndef M_PI
#define M_PI (3.14159265)
#endif
#define TABLE_SIZE (200)
typedef struct
{
float sine[TABLE_SIZE];
int phase;
}
paTestData;
class SoundEngine : public QThread
{
Q_OBJECT
public:
bool turnOFF;
void run();
static int patestCallback( const void *inputBuffer, void *outputBuffer,unsigned long framesPerBuffer,const PaStreamCallbackTimeInfo* timeInfo,PaStreamCallbackFlags statusFlags,void *userData );
void generateSine();
void removeSine();
private:
paTestData data;
PaStream *stream;
PaError err;
bool isPressed;
};
#endif // SOUNDENGINE_H
soundEngine.cpp
#include "soundengine.h"
#include <QDebug>
void SoundEngine::run()
{
PaStreamParameters outputParameters;
int i;
double t;
turnOFF = false;
isPressed = false;
static unsigned long n=0;
for( i=0; i<TABLE_SIZE; i++, n++ )
{
t = (double)i/(double)SAMPLE_RATE;
data.sine[i] = 0;
//data.sine[i] = 0.3*sin(2 * M_PI * FREQUENCY * t);
/*data.sine[i] *= 1.0/2;
data.sine[i] += 0.5*sin(2 * M_PI * (FREQUENCY+110) * t);
data.sine[i] *= 2.0/3;
data.sine[i] += (1.0/3)*sin(2 * M_PI * (FREQUENCY+60) * t);
data.sine[i] *= 3.0/4;
data.sine[i] += (1.0/4)*sin(2 * M_PI * (FREQUENCY+160) * t);*/
}
data.phase = 0;
err = Pa_Initialize();
if(err != paNoError) qDebug()<<"Błąd przy inicjalizacji strumienia:"<<Pa_GetErrorText(err);
outputParameters.device = Pa_GetDefaultOutputDevice(); /* default output device */
if (outputParameters.device == paNoDevice) qDebug()<<"Błąd: Brak domyślnego urządzenia wyjścia!";
outputParameters.channelCount = 2; /* stereo output */
outputParameters.sampleFormat = paFloat32; /* 32 bit floating point output */
outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency;
outputParameters.hostApiSpecificStreamInfo = NULL;
err = Pa_OpenStream(
&stream,
NULL, /* no input */
&outputParameters,
SAMPLE_RATE,
FRAMES_PER_BUFFER,
paClipOff, /*paNoFlag we won't output out of range samples so don't bother clipping them */
patestCallback,
&data );
if(err != paNoError) qDebug()<<"Błąd przy otwieraniu strumienia:"<<Pa_GetErrorText(err);
//err = Pa_StartStream( stream );
if(err != paNoError) qDebug()<<"Błąd przy starcie strumienia:"<<Pa_GetErrorText(err);
while (turnOFF == false) {
Pa_Sleep(500);
}
//err = Pa_StopStream( stream );
if(err != paNoError) qDebug()<<"Błąd przy zatrzymywaniu strumienia:"<<Pa_GetErrorText(err);
err = Pa_CloseStream( stream );
if(err != paNoError) qDebug()<<"Błąd przy zamykaniu strumienia:"<<Pa_GetErrorText(err);
Pa_Terminate();
}
int SoundEngine::patestCallback(const void *inputBuffer, void *outputBuffer, unsigned long framesPerBuffer, const PaStreamCallbackTimeInfo *timeInfo, PaStreamCallbackFlags statusFlags, void *userData)
{
paTestData *callData = (paTestData*)userData;
float *out = (float*)outputBuffer;
float sample;
unsigned long i;
(void) timeInfo; /* Prevent unused variable warnings. */
(void) statusFlags;
(void) inputBuffer;
for( i=0; i<framesPerBuffer; i++ )
{
sample = callData->sine[callData->phase++];
*out++ = sample; /* left */
*out++ = sample; /* right */
if( callData->phase >= TABLE_SIZE ) callData->phase -= TABLE_SIZE;
}
return paContinue;
}
void SoundEngine::generateSine()
{
if(isPressed == false)
{
for(int i=0; i<TABLE_SIZE; i++)
{
data.sine[i] += 0.3*sin(2 * M_PI * 440 * ((double)i/(double)SAMPLE_RATE));
}
isPressed = true;
err = Pa_StartStream( stream );
}
}
void SoundEngine::removeSine()
{
err = Pa_StopStream( stream );
for(int i=0; i<TABLE_SIZE; i++)
{
data.sine[i] -= 0.3*sin(2 * M_PI * 440 * ((double)i/(double)SAMPLE_RATE));
}
isPressed = false;
}
When I'm pressing button, function
void SoundEngine::generateSine()
is running - it generates sound. When I release the button, method
void SoundEngine::removeSine()
removes the sound.
MODERATOR ATTENTION: This question seems to belong more to dsp.stackexchange than this forum.
There's nothing wrong with either your sound or PortAudio. The sound you're hearing at the end is just the result of the audio being abruptly stopped. Take a look at the following image of a sound that has a constant amplitude through the entire duration. This sound will have an audible pop at the end.
Conversely, if we attenuate the amplitude by modifying the waveform's envelope (the same sound as in image #1) so that it resembles the sound in image #2, we won't hear any abrupt change(s) in the sound at the end.
In conclusion, if your goal is to completely eliminate the pops that you're hearing, fade out (or fade in) your sound(s).

PortAudio iterate through audio data

As I am new to PortAudio, I tried an example program from the internet. The program is able to record the input of a microphone through a callback function.
I want to get every single sample of the recorded audio represented as a numeric value (e.g. float). I cannot figure out where the recorded data of the mic is stored.
This is the callback function:
static int recordCallback(const void *inputBuffer, void *outputBuffer,
unsigned long framesPerBuffer,
const PaStreamCallbackTimeInfo* timeInfo,
PaStreamCallbackFlags statusFlags,
void *userData)
{
paTestData *data = (paTestData*)userData;
const SAMPLE *rptr = (const SAMPLE*)inputBuffer;
SAMPLE *wptr = &data->recordedSamples[data->frameIndex * NUM_CHANNELS];
long framesToCalc;
long i;
int finished;
unsigned long framesLeft = data->maxFrameIndex - data->frameIndex;
(void)outputBuffer; /* Prevent unused variable warnings. */
(void)timeInfo;
(void)statusFlags;
(void)userData;
if (framesLeft < framesPerBuffer)
{
framesToCalc = framesLeft;
finished = paComplete;
}
else
{
framesToCalc = framesPerBuffer;
finished = paContinue;
}
if (inputBuffer == NULL)
{
for (i = 0; i<framesToCalc; i++)
{
*wptr++ = SAMPLE_SILENCE; /* left */
if (NUM_CHANNELS == 2) *wptr++ = SAMPLE_SILENCE; /* right */
}
}
else
{
cout << endl << "SAMPLE" << endl;
for (i = 0; i<framesToCalc; i++)
{
*wptr++ = *rptr++; /* left */
//cout << rptr<<endl;
if (NUM_CHANNELS == 2) *wptr++ = *rptr++; /* right */
}
}
data->frameIndex += framesToCalc;
return finished;
}
The audio input stream is initialized here:
err = Pa_OpenStream(
&stream,
&inputParameters,
NULL, /* &outputParameters, */
SAMPLE_RATE,
FRAMES_PER_BUFFER,
paClipOff, /* we won't output out of range samples so don't bother clipping them */
recordCallback,
&data);
The incoming data is stored in the inputBuffer pointer of the callback. Depending on the inputParameters used when calling Pa_OpenStream one should be able to cast the input buffer into an array of corresponding data type (e.g. if paFloat32 is used as sample format, then the buffer can be interpreted as const float*).
It is recommended that in the callback function you copy the incoming data into another buffer for further processing (outside of the callback).
Multiple channels samples are interleaved in the buffer. For example for stereo input inputBuffer[0] is the first sample for the left channer, inputBuffr[1] is the first sample to the right channel, inputBuffer[2] is the 2nd sample for the left channel, etc. Total number of samples is provided via framesPerBuffer argument of the callback.

PortAudio example code issue

I want to make a guitar effects program so I'm trying to run the example code pa_fuzz.c from PORTAUDIO API.
It works on paWDMKS host api. But when I connect the guitar using Behringer ucg 102 (for no lantency) the host api changes to paMME and I get errorCode 1 and message: "undefined external error".
Also in the visual studio output console I get tons of messages like:
'ConsoleApplication4.exe' (Win32): Unloaded 'C:\Windows\SysWOW64\SHCore.dll'
'ConsoleApplication4.exe' (Win32): Unloaded 'C:\Windows\SysWOW64\shlwapi.dll'
'ConsoleApplication4.exe' (Win32): Unloaded 'C:\Windows\SysWOW64\shell32.dll'
'ConsoleApplication4.exe' (Win32): Unloaded 'C:\Windows\SysWOW64\dbgcore.dll'
'ConsoleApplication4.exe' (Win32): Unloaded 'C:\Program Files\Native Instruments\Rig Kontrol 3 Driver\asio\rig3asio32.dll'
Here is the code snippet for the "PaHostErrorInfo" object;
PaHostErrorInfo *err2 = Pa_GetLastHostErrorInfo();
printf(err2->errorText);
Here is the source code:
#include <stdio.h>
#include <math.h>
#include "portaudio.h"
/*
** Note that many of the older ISA sound cards on PCs do NOT support
** full duplex audio (simultaneous record and playback).
** And some only support full duplex at lower sample rates.
*/
#define SAMPLE_RATE (8000)
#define PA_SAMPLE_TYPE paFloat32
#define FRAMES_PER_BUFFER (64)
typedef float SAMPLE;
float CubicAmplifier( float input );
static int fuzzCallback( const void *inputBuffer, void *outputBuffer,
unsigned long framesPerBuffer,
const PaStreamCallbackTimeInfo* timeInfo,
PaStreamCallbackFlags statusFlags,
void *userData );
/* Non-linear amplifier with soft distortion curve. */
float CubicAmplifier( float input )
{
float output, temp;
if( input < 0.0 )
{
temp = input + 1.0f;
output = (temp * temp * temp) - 1.0f;
}
else
{
temp = input - 1.0f;
output = (temp * temp * temp) + 1.0f;
}
return output;
}
#define FUZZ(x) CubicAmplifier(CubicAmplifier(CubicAmplifier(CubicAmplifier(x))))
static int gNumNoInputs = 0;
/* This routine will be called by the PortAudio engine when audio is needed.
** It may be called at interrupt level on some machines so don't do anything
** that could mess up the system like calling malloc() or free().
*/
static int fuzzCallback( const void *inputBuffer, void *outputBuffer,
unsigned long framesPerBuffer,
const PaStreamCallbackTimeInfo* timeInfo,
PaStreamCallbackFlags statusFlags,
void *userData )
{
SAMPLE *out = (SAMPLE*)outputBuffer;
const SAMPLE *in = (const SAMPLE*)inputBuffer;
unsigned int i;
(void) timeInfo; /* Prevent unused variable warnings. */
(void) statusFlags;
(void) userData;
if( inputBuffer == NULL )
{
for( i=0; i<framesPerBuffer; i++ )
{
*out++ = 0; /* left - silent */
*out++ = 0; /* right - silent */
}
gNumNoInputs += 1;
}
else
{
for( i=0; i<framesPerBuffer; i++ )
{
*out++ = FUZZ(*in++); /* left - distorted */
*out++ = *in++; /* right - clean */
}
}
return paContinue;
}
/*******************************************************************/
int main(void);
int main(void)
{
PaStreamParameters inputParameters, outputParameters;
PaStream *stream;
PaError err;
PaDeviceInfo *info;
err = Pa_Initialize();
if( err != paNoError ) goto error;
info = Pa_GetDeviceInfo(1);
inputParameters.device = Pa_GetDefaultInputDevice(); /* default input device */
if (inputParameters.device == paNoDevice) {
fprintf(stderr,"Error: No default input device.\n");
goto error;
}
inputParameters.channelCount = 2; /* stereo input */
inputParameters.sampleFormat = PA_SAMPLE_TYPE;
inputParameters.suggestedLatency = Pa_GetDeviceInfo( inputParameters.device )->defaultLowInputLatency;
inputParameters.hostApiSpecificStreamInfo = NULL;
info = Pa_GetDeviceInfo(4);
outputParameters.device = Pa_GetDefaultOutputDevice(); /* default output device */
if (outputParameters.device == paNoDevice) {
fprintf(stderr,"Error: No default output device.\n");
goto error;
}
outputParameters.channelCount = 2; /* stereo output */
outputParameters.sampleFormat = PA_SAMPLE_TYPE;
outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency;
outputParameters.hostApiSpecificStreamInfo = NULL;
err = Pa_OpenStream(
&stream,
&inputParameters,
&outputParameters,
SAMPLE_RATE,
FRAMES_PER_BUFFER,
0, /* paClipOff, */ /* we won't output out of range samples so don't bother clipping them */
fuzzCallback,
NULL );
PaHostErrorInfo *err2 = Pa_GetLastHostErrorInfo();
printf(err2->errorText);
if( err != paNoError ) goto error;
err = Pa_StartStream( stream );
if( err != paNoError ) goto error;
printf("Hit ENTER to stop program.\n");
getchar();
err = Pa_CloseStream( stream );
if( err != paNoError ) goto error;
printf("Finished. gNumNoInputs = %d\n", gNumNoInputs );
Pa_Terminate();
return 0;
error:
Pa_Terminate();
fprintf( stderr, "An error occured while using the portaudio stream\n" );
fprintf( stderr, "Error number: %d\n", err );
fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );
return -1;
}
Can you guys please have a look on what can be the problem? I have installed BEHRINGER USB WDM AUDIO 2.8.40 drivers. (I use this usb interface with guitar rig)
I switched to another usb port and it worked. I think it's a bug from portaudio. I will leave this answer here. Now I have another problem. LATENCY :( is so frustrating to play guitar with latency.
What can be the cause of this latency? I'm connecting my guitar with behringer ucg102 (famous for no latency). And again, I use it with Guitar Rig where I have close to 0 latency. Is it a problem from portaudio library? This code is from their example "pa_fuzz.c"
New Edit: latency problem fixed by selecting the required input and output deivice.

Decoding by libjpeg -> Encoding by x264, strange artefacts on frames

I have a collection of jpeg, which must be decoded by lib jpeg, and after it, encoded by x264 (after it encoded packets are streamed via rtmp).
Code I used for decoding:
struct my_error_mgr
{
struct jpeg_error_mgr pub;
jmp_buf setjmp_buffer;
};
typedef my_error_mgr *my_error_ptr;
METHODDEF(void) my_error_exit (j_common_ptr cinfo)
{
my_error_ptr myerr = (my_error_ptr) cinfo->err;
(*cinfo->err->output_message) (cinfo);
longjmp(myerr->setjmp_buffer, 1);
}
void init_source(j_decompress_ptr ptr)
{
Q_UNUSED(ptr)
}
boolean fill_input_buffer(j_decompress_ptr ptr)
{
Q_UNUSED(ptr)
return TRUE;
}
void term_source(j_decompress_ptr ptr)
{
Q_UNUSED(ptr)
}
void skip_input_data(j_decompress_ptr ptr, long num_bytes)
{
if(num_bytes>0)
{
ptr->src->next_input_byte+=(size_t)num_bytes;
ptr->src->bytes_in_buffer-=(size_t)num_bytes;
}
}
EtherDecoder::EtherDecoder(QObject *parent):
QObject(parent)
{
}
void EtherDecoder::dataBlockReady(QByteArray data)
{
jpeg_decompress_struct decompressInfo;
jpeg_create_decompress(&decompressInfo);
my_error_mgr err;
decompressInfo.do_fancy_upsampling = FALSE;
decompressInfo.src = (jpeg_source_mgr *) (*decompressInfo.mem->alloc_small) ((j_common_ptr) &decompressInfo, JPOOL_PERMANENT, sizeof(jpeg_source_mgr));
decompressInfo.err = jpeg_std_error(&err.pub);
err.pub.error_exit = my_error_exit;
if (setjmp(err.setjmp_buffer))
{
jpeg_destroy_decompress(&decompressInfo);
return;
}
decompressInfo.src->init_source = init_source;
decompressInfo.src->resync_to_restart = jpeg_resync_to_restart;
decompressInfo.src->fill_input_buffer = fill_input_buffer;
decompressInfo.src->skip_input_data = skip_input_data;
decompressInfo.src->term_source = term_source;
decompressInfo.src->next_input_byte = reinterpret_cast<const JOCTET*>(data.data());
decompressInfo.src->bytes_in_buffer = data.size();
jpeg_read_header(&decompressInfo, TRUE);
jpeg_start_decompress(&decompressInfo);
int size = 0;
int n_samples = 0;
char *samples = new char[5242880];
char *reserv = samples;
while (decompressInfo.output_scanline < decompressInfo.output_height)
{
n_samples = jpeg_read_scanlines(&decompressInfo, (JSAMPARRAY) &samples, 1);
samples += n_samples * decompressInfo.image_width * decompressInfo.num_components;
size += n_samples * decompressInfo.image_width * decompressInfo.num_components;
}
jpeg_finish_decompress(&decompressInfo);
QByteArray output(reserv, size);
emit frameReady(output, decompressInfo.output_width, decompressInfo.output_height);
jpeg_destroy_decompress(&decompressInfo);
delete[] reserv;
}
When I emit frameReady signal, I send data to Encoder, method, where I init Encedor looks like:
bool EtherEncoder::initEncoder(unsigned int width, unsigned int height)
{
x264_param_t param;
x264_param_default_preset(&param, "veryfast", "zerolatency");
param.i_width=width;
param.i_height=height;
param.i_frame_total=0;
param.i_csp=X264_CSP_I420;
param.i_timebase_num=1;
param.i_timebase_den=96000;
param.b_annexb=true;
param.b_repeat_headers=false;
x264_param_apply_fastfirstpass(&param);
x264_param_apply_profile(&param, "baseline");
_context=x264_encoder_open(&param);
if(!_context)
return false;
int nal_count;
x264_nal_t *nals;
if(x264_encoder_headers(_context, &nals, &nal_count)<0)
{
x264_encoder_close(_context);
_context=0;
return false;
}
_extradata=QByteArray();
_width=width;
_height=height;
if(nal_count>0)
{
_extradata=QByteArray(
(const char *)nals[0].p_payload,
nals[nal_count-1].p_payload+nals[nal_count-1].i_payload-nals[0].p_payload);
}
return true;
}
And encoding method:
void EtherEncoder::onFrameReady(QByteArray data, int width, int height)
{
while(data.size()>0)
{
if(!_context && initEncoder(width, height))
{
_timestampDelta=realTimestamp();
}
if(_context)
{
x264_picture_t pic;
x264_picture_init(&pic);
pic.i_type=X264_TYPE_AUTO;
pic.i_pts=_timestampDelta*96000;
pic.img.i_csp=X264_CSP_I420;
pic.img.i_plane=3;
int planeSize = width*height;
uint8_t *p = (uint8_t*)data.data();
pic.img.plane[0]=p;
p+=planeSize;
pic.img.plane[1]=p;
p+=planeSize/4;
pic.img.plane[2]=p;
pic.img.i_stride[0]=width;
pic.img.i_stride[1]=width/2;
pic.img.i_stride[2]=width/2;
if(_forceKeyFrame)
{
pic.i_type=X264_TYPE_I;
_forceKeyFrame=false;
}
int nal_count;
x264_nal_t *nals;
int rc=x264_encoder_encode(_context, &nals, &nal_count, &pic, &pic);
if(rc>0)
{
_mutex.lock();
_packets.push_back(
Packet(
QByteArray(
(const char *)nals[0].p_payload, nals[nal_count- 1].p_payload+nals[nal_count-1].i_payload-nals[0].p_payload),
_timestampDelta/96.0,
_timestampDelta/96.0,
pic.b_keyframe));
_timestampDelta+=40;
data.clear();
_mutex.unlock();
emit onPacketReady();
}
}
}
}
Decoding and encoding proceeds without errors, at the end I get valid video stream, but, it seems that in one of this steps I set Invalid data for decoder/encoder. I get only 1/4 part of image (top-left, as I understood) and it has invalid color and come color stripes. Maybe I set invalid strides and planes when encode frame, or maybe my setting data for libjpeg decoder is incorrect.. Please ask questions about my code, I'll try to make some explanations for you. I explodes my brain.. Thank you.