libavcodec version 54 requires a different formula for nb_samples, used in audio encoding. So, I've added in conditional code, so audio works on the newest version of FFmpeg.

This commit is contained in:
Jonathan Thomas
2012-12-03 13:03:04 -06:00
parent e264d4fcab
commit a8fa5a91d2
2 changed files with 31 additions and 20 deletions

View File

@@ -561,7 +561,12 @@ void FFmpegWriter::flush_encoders()
cout << "Flushing AUDIO buffer!" << endl;
// Increment PTS (in samples and scaled to the codec's timebase)
write_audio_count += av_rescale_q(audio_codec->frame_size / audio_codec->channels, (AVRational){1, info.sample_rate}, audio_codec->time_base);
#if LIBAVFORMAT_VERSION_MAJOR >= 54
// for some reason, it requires me to multiply channels X 2
write_audio_count += av_rescale_q(audio_input_position / (audio_codec->channels * av_get_bytes_per_sample(AV_SAMPLE_FMT_S16)), (AVRational){1, info.sample_rate}, audio_codec->time_base);
#else
write_audio_count += av_rescale_q(audio_input_position / audio_codec->channels, (AVRational){1, info.sample_rate}, audio_codec->time_base);
#endif
AVPacket pkt;
av_init_packet(&pkt);
@@ -980,7 +985,8 @@ void FFmpegWriter::write_audio_packets(bool final)
audio_resample(resampleCtx, (short *) converted_audio, (short *) frame_samples, total_frame_samples);
// Update total frames & input frame size (due to bigger or smaller data types)
total_frame_samples *= (av_get_bytes_per_sample(audio_codec->sample_fmt) / av_get_bytes_per_sample(AV_SAMPLE_FMT_S16));
total_frame_samples *= (av_get_bytes_per_sample(audio_codec->sample_fmt) / av_get_bytes_per_sample(AV_SAMPLE_FMT_S16)); // adjust for different byte sizes
total_frame_samples *= (float(info.channels) / channels_in_frame); // adjust for different # of channels
audio_input_frame_size = initial_audio_input_frame_size * (av_get_bytes_per_sample(audio_codec->sample_fmt) / av_get_bytes_per_sample(AV_SAMPLE_FMT_S16));
// Set remaining samples
@@ -1022,11 +1028,22 @@ void FFmpegWriter::write_audio_packets(bool final)
break;
// Increment PTS (in samples and scaled to the codec's timebase)
#if LIBAVFORMAT_VERSION_MAJOR >= 54
// for some reason, it requires me to multiply channels X 2
write_audio_count += av_rescale_q(audio_input_position / (audio_codec->channels * av_get_bytes_per_sample(AV_SAMPLE_FMT_S16)), (AVRational){1, info.sample_rate}, audio_codec->time_base);
#else
write_audio_count += av_rescale_q(audio_input_position / audio_codec->channels, (AVRational){1, info.sample_rate}, audio_codec->time_base);
#endif
// Create AVFrame (and fill it with samples)
AVFrame *frame_final = avcodec_alloc_frame();
frame_final->nb_samples = audio_input_frame_size / audio_codec->channels; //av_get_bytes_per_sample(audio_codec->sample_fmt);
#if LIBAVFORMAT_VERSION_MAJOR >= 54
// for some reason, it requires me to multiply channels X 2
frame_final->nb_samples = audio_input_position / (audio_codec->channels * av_get_bytes_per_sample(AV_SAMPLE_FMT_S16));
#else
frame_final->nb_samples = audio_input_frame_size / audio_codec->channels;
#endif
//frame_final->nb_samples = audio_input_frame_size / audio_codec->channels; //av_get_bytes_per_sample(audio_codec->sample_fmt);
frame_final->pts = write_audio_count; // Set the AVFrame's PTS
avcodec_fill_audio_frame(frame_final, audio_codec->channels, audio_codec->sample_fmt, (uint8_t *) samples,
audio_input_position * av_get_bytes_per_sample(audio_codec->sample_fmt), 1);
@@ -1136,12 +1153,10 @@ void FFmpegWriter::process_video_packet(tr1::shared_ptr<Frame> frame)
// Determine the height & width of the source image
int source_image_width = frame->GetWidth();
int source_image_height = frame->GetHeight();
// If visualizing waveform (replace image with waveform image)
if (info.visualize)
{
source_image_width = info.width;
source_image_height = info.height;
}
// Do nothing if size is 1x1 (i.e. no image in this frame)
if (source_image_height == 1 && source_image_width == 1)
return;
// Init rescalers (if not initialized yet)
if (image_rescalers.size() == 0)
@@ -1191,10 +1206,6 @@ void FFmpegWriter::process_video_packet(tr1::shared_ptr<Frame> frame)
av_free(frame_source->data[0]);
av_free(frame_source);
if (info.visualize)
// Deallocate the waveform's image (if needed)
frame->ClearWaveform();
} // end task
}

View File

@@ -61,10 +61,10 @@ int main()
// Add some clips
//Clip c1(new FFmpegReader("/home/jonathan/Apps/videcho_site/media/user_files/videos/bd0bf442-3221-11e2-8bf6-001fd00ee3aa.webm"));
//Clip c1(new FFmpegReader("/home/jonathan/Videos/Movie Music/02 - Shattered [Turn The Car Around] (Album Version).mp3"));
FFmpegReader r1("/home/jonathan/Desktop/sintel.webm");
Clip c1(new FFmpegReader("/home/jonathan/Videos/big-buck-bunny_trailer.webm"));
Clip c2(new ImageReader("/home/jonathan/Desktop/Logo.png"));
Clip c3(new FFmpegReader("/home/jonathan/Desktop/sintel.webm"));
FFmpegReader r1("../../src/examples/piano.wav");
Clip c1(new FFmpegReader("/home/jonathan/Videos/sintel_trailer-720p.mp4"));
Clip c2(new ImageReader("/home/jonathan/Desktop/logo.png"));
Clip c3(new FFmpegReader("/home/jonathan/Videos/sintel_trailer-720p.mp4"));
//Clip c3(new FFmpegReader("/home/jonathan/Videos/Movie Music/01 Whip It.mp3"));
c1.Position(0.0);
c1.gravity = GRAVITY_CENTER;
@@ -192,7 +192,7 @@ int main()
// Set options
//w.SetAudioOptions(true, "libmp3lame", 44100, 2, 128000, false);
w.SetAudioOptions(true, "libvorbis", 44100, 2, 128000);
w.SetAudioOptions(true, "libvorbis", 48000, 2, 128000);
w.SetVideoOptions(true, "libvpx", Fraction(24,1), 624, 348, Fraction(1,1), false, false, 2000000);
// Prepare Streams
@@ -204,13 +204,13 @@ int main()
// Output stream info
w.OutputStreamInfo();
for (int frame = 200; frame <= 400; frame++)
for (int frame = 1; frame <= 100; frame++)
{
tr1::shared_ptr<Frame> f = r1.GetFrame(frame);
if (f)
{
//if (frame >= 13)
// f->DisplayWaveform();
//f->DisplayWaveform();
// Write frame
cout << "queue frame " << frame << " (" << f->number << ", " << f << ")" << endl;