work on audio encoding

This commit is contained in:
Isaac Connor 2016-09-20 10:10:02 -04:00
parent ae80fd4d2d
commit 0614af4f51
2 changed files with 90 additions and 39 deletions

View File

@ -206,6 +206,13 @@ Debug(2, "Have audio_output_context");
av_strerror(ret, error_buffer, sizeof(error_buffer)); av_strerror(ret, error_buffer, sizeof(error_buffer));
Fatal( "could not open codec (%d) (%s)\n", ret, error_buffer ); Fatal( "could not open codec (%d) (%s)\n", ret, error_buffer );
} else { } else {
/** Create the FIFO buffer based on the specified output sample format. */
if (!(fifo = av_audio_fifo_alloc(audio_output_context->sample_fmt,
audio_output_context->channels, 1))) {
Error("Could not allocate FIFO\n");
return;
}
output_frame_size = audio_output_context->frame_size;
Debug(2, "Success opening AAC codec"); Debug(2, "Success opening AAC codec");
} }
av_dict_free(&opts); av_dict_free(&opts);
@ -514,6 +521,44 @@ if ( 0 ) {
zm_av_unref_packet(&opkt); zm_av_unref_packet(&opkt);
return 0; return 0;
} }
if ( data_present ) {
uint8_t **converted_input_samples = NULL;
/**
* Allocate as many pointers as there are audio channels.
* Each pointer will later point to the audio samples of the corresponding
* channels (although it may be NULL for interleaved formats).
*/
if (!(converted_input_samples = calloc( audio_output_context->channels, sizeof(*converted_input_samples)))) {
Error( "Could not allocate converted input sample pointers\n");
return 0;
}
/**
* Allocate memory for the samples of all channels in one consecutive
* block for convenience.
*/
if ((ret = av_samples_alloc(converted_input_samples, NULL,
audio_output_context->channels,
frame_size,
audio_output_context->sample_fmt, 0)) < 0) {
Error( "Could not allocate converted input samples (error '%s')\n",
av_make_error_string(ret).c_str() );
av_freep(&(converted_input_samples)[0]);
free(*converted_input_samples);
return 0;
}
if ((ret = av_audio_fifo_realloc(fifo, av_audio_fifo_size(fifo) + frame_size)) < 0) {
Error( "Could not reallocate FIFO\n");
return 0;
}
/** Store the new samples in the FIFO buffer. */
if (av_audio_fifo_write(fifo, (void **)converted_input_samples, frame_size) < frame_size) {
Error( "Could not write data to FIFO\n");
return 0;
}
/** Create a new frame to store the audio samples. */ /** Create a new frame to store the audio samples. */
if (!(output_frame = av_frame_alloc())) { if (!(output_frame = av_frame_alloc())) {
@ -545,7 +590,7 @@ if ( 0 ) {
Error( "Couldnt allocate output frame buffer samples (error '%s')", Error( "Couldnt allocate output frame buffer samples (error '%s')",
av_make_error_string(ret).c_str() ); av_make_error_string(ret).c_str() );
Error("Frame: samples(%d) layout (%d) format(%d) rate(%d)", output_frame->nb_samples, Error("Frame: samples(%d) layout (%d) format(%d) rate(%d)", output_frame->nb_samples,
output_frame->channel_layout, output_frame->format , output_frame->sample_rate output_frame->channel_layout, output_frame->format , output_frame->sample_rate
); );
av_frame_free(&input_frame); av_frame_free(&input_frame);
av_frame_free(&output_frame); av_frame_free(&output_frame);
@ -568,6 +613,9 @@ output_frame->channel_layout, output_frame->format , output_frame->sample_rate
zm_av_unref_packet(&opkt); zm_av_unref_packet(&opkt);
return 0; return 0;
} }
} else {
Debug(2, "Not data present" );
} // end if data_present
} }
} else { } else {
opkt.data = ipkt->data; opkt.data = ipkt->data;

View File

@ -2,6 +2,7 @@
#define ZM_VIDEOSTORE_H #define ZM_VIDEOSTORE_H
#include "zm_ffmpeg.h" #include "zm_ffmpeg.h"
#include "libavutil/audio_fifo.h"
#if HAVE_LIBAVCODEC #if HAVE_LIBAVCODEC
@ -19,6 +20,8 @@ private:
AVCodec *audio_output_codec; AVCodec *audio_output_codec;
AVCodecContext *audio_output_context; AVCodecContext *audio_output_context;
int data_present; int data_present;
AVAudioFifo *fifo;
int output_frame_size;
const char *filename; const char *filename;
const char *format; const char *format;