-
Notifications
You must be signed in to change notification settings - Fork 50
Audio: encode process #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
MarcAntoine-Arnaud
merged 5 commits into
avTranscoder:master
from
cchampet:dev_audioProcessing
Jun 4, 2014
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
1cdebe6
InputStreamAudio: fix compile error with libAv
423c28b
InputStreamVideo: throw if an error occured during video decoding
e8f1c25
InputStreamAudio: fix decode
72fc695
Audio: encode outputStreamAudio
3a719d0
OutputStreamAudio: overlay encodeFrame
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,163 @@ | ||
#include "OutputStreamAudio.hpp" | ||
#include "OutputStreamAudio.hpp" | ||
|
||
extern "C" { | ||
#ifndef __STDC_CONSTANT_MACROS | ||
#define __STDC_CONSTANT_MACROS | ||
#endif | ||
#include <libavcodec/avcodec.h> | ||
#include <libavformat/avformat.h> | ||
#include <libavutil/avutil.h> | ||
} | ||
|
||
namespace avtranscoder | ||
{ | ||
|
||
OutputStreamAudio::OutputStreamAudio() | ||
: m_audioDesc( "pcm_s16le" ) | ||
{ | ||
} | ||
|
||
bool OutputStreamAudio::setup() | ||
{ | ||
av_register_all(); // Warning: should be called only once | ||
|
||
AVCodecContext* codecContext( m_audioDesc.getCodecContext() ); | ||
|
||
if( codecContext == NULL ) | ||
return false; | ||
|
||
// try to open encoder with parameters. | ||
if( avcodec_open2( codecContext, m_audioDesc.getCodec(), NULL ) < 0 ) | ||
return false; | ||
|
||
return true; | ||
} | ||
|
||
bool OutputStreamAudio::encodeFrame( const AudioFrame& decodedFrame, DataStream& codedFrame ) | ||
{ | ||
#if LIBAVCODEC_VERSION_MAJOR > 54 | ||
AVFrame* frame = av_frame_alloc(); | ||
#else | ||
AVFrame* frame = avcodec_alloc_frame(); | ||
#endif | ||
|
||
AVCodecContext* codecContext = m_audioDesc.getCodecContext(); | ||
|
||
// Set default frame parameters | ||
#if LIBAVCODEC_VERSION_MAJOR > 54 | ||
av_frame_unref( frame ); | ||
#else | ||
avcodec_get_frame_defaults( frame ); | ||
#endif | ||
|
||
frame->nb_samples = decodedFrame.getNbSamples(); | ||
frame->format = codecContext->sample_fmt; | ||
frame->channel_layout = codecContext->channel_layout; | ||
|
||
// we calculate the size of the samples buffer in bytes | ||
int buffer_size = av_samples_get_buffer_size(NULL, codecContext->channels, frame->nb_samples, codecContext->sample_fmt, 0); | ||
if( buffer_size < 0 ) | ||
{ | ||
char err[250]; | ||
av_strerror( buffer_size, err, 250); | ||
|
||
throw std::runtime_error( "EncodeFrame error: buffer size < 0 - " + std::string(err) ); | ||
} | ||
|
||
int retvalue = avcodec_fill_audio_frame(frame, codecContext->channels, codecContext->sample_fmt, decodedFrame.getPtr(), buffer_size, 0); | ||
if( retvalue < 0 ) | ||
{ | ||
char err[250]; | ||
av_strerror( retvalue, err, 250); | ||
|
||
throw std::runtime_error( "EncodeFrame error: avcodec fill audio frame - " + std::string(err) ); | ||
} | ||
|
||
AVPacket packet; | ||
av_init_packet( &packet ); | ||
|
||
packet.size = 0; | ||
packet.data = NULL; | ||
packet.stream_index = 0; | ||
|
||
if( ( codecContext->coded_frame ) && | ||
( codecContext->coded_frame->pts != (int)AV_NOPTS_VALUE ) ) | ||
{ | ||
packet.pts = codecContext->coded_frame->pts; | ||
} | ||
|
||
if( codecContext->coded_frame && | ||
codecContext->coded_frame->key_frame ) | ||
{ | ||
packet.flags |= AV_PKT_FLAG_KEY; | ||
} | ||
|
||
#if LIBAVCODEC_VERSION_MAJOR > 53 | ||
int gotPacket = 0; | ||
int ret = avcodec_encode_audio2( codecContext, &packet, frame, &gotPacket ); | ||
if( ret == 0 && gotPacket == 1 ) | ||
{ | ||
codedFrame.getBuffer().resize( packet.size ); | ||
memcpy( codedFrame.getPtr(), packet.data, packet.size ); | ||
} | ||
#else | ||
int ret = avcodec_encode_audio( codecContext, packet.data, packet.size, frame ); | ||
if( ret > 0 ) | ||
{ | ||
codedFrame.getBuffer().resize( packet.size ); | ||
memcpy( codedFrame.getPtr(), packet.data, packet.size ); | ||
} | ||
#endif | ||
|
||
av_free_packet( &packet ); | ||
|
||
#if LIBAVCODEC_VERSION_MAJOR > 54 | ||
av_frame_free( &frame ); | ||
return ret == 0 && gotPacket == 1; | ||
#else | ||
#if LIBAVCODEC_VERSION_MAJOR > 53 | ||
avcodec_free_frame( &frame ); | ||
return ret == 0 && gotPacket == 1; | ||
#else | ||
av_free( frame ); | ||
#endif | ||
#endif | ||
return ret == 0; | ||
} | ||
|
||
bool OutputStreamAudio::encodeFrame( DataStream& codedFrame ) | ||
{ | ||
AVCodecContext* codecContext = m_audioDesc.getCodecContext(); | ||
|
||
AVPacket packet; | ||
av_init_packet( &packet ); | ||
|
||
packet.size = 0; | ||
packet.data = NULL; | ||
packet.stream_index = 0; | ||
|
||
#if LIBAVCODEC_VERSION_MAJOR > 53 | ||
int gotPacket = 0; | ||
int ret = avcodec_encode_audio2( codecContext, &packet, NULL, &gotPacket ); | ||
if( ret == 0 && gotPacket == 1 ) | ||
{ | ||
codedFrame.getBuffer().resize( packet.size ); | ||
memcpy( codedFrame.getPtr(), packet.data, packet.size ); | ||
} | ||
av_free_packet( &packet ); | ||
return ret == 0 && gotPacket == 1; | ||
|
||
#else | ||
int ret = avcodec_encode_audio( codecContext, packet.data, packet.size, NULL ); | ||
if( ret > 0 ) | ||
{ | ||
codedFrame.getBuffer().resize( packet.size ); | ||
memcpy( codedFrame.getPtr(), packet.data, packet.size ); | ||
} | ||
av_free_packet( &packet ); | ||
return ret == 0; | ||
|
||
#endif | ||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you explain more?
From where the input comes from?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Itr's similar to video processing.
AudioFrame is the raw sound of data, like an image in video.
And the DataStream is the coded data.
I don't have doxygen generation on API, but need to provide it for adding correct documentation when API is a little more stable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My question was about "encodeFrame( DataStream& codedFrame )" which has no decodedFrame argument.
Documentation can't be done at the end...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hoo yep !
It's for ending last frame with data buffered in coder.
Also similar to video where latency can be not null ... due to a
reoganization of video frames or packetisation of audio streams.
2014-06-04 11:20 GMT+02:00 Fabien Castan notifications@github.com:
[image: Mikros Image - Pub Evian Spider-man the amazing babyme 2]
http://www.mikrosimage.eu/realisations/pub-l-evian-spider-man-the-amazing-babyme-2/
Marc-Antoine ARNAUD
R&D Software Developer -
Image Processing & Media Technology Expert
+33 1 55 63 11 00
mrn@mikrosimage.eu
Mikros Image /
Digital Post-Production for Video and Film
www.mikrosimage.eu