Updated the FFmpegWriter to use compiler #IF commands to be compatible with older and newer versions of FFmpeg. This is still a work in progress, but many of the newer commands are now being protected.

This commit is contained in:
Jonathan Thomas
2012-11-16 17:15:44 -06:00
parent b503b5a350
commit 8fb94cc123
4 changed files with 103 additions and 65 deletions

View File

@@ -18,10 +18,16 @@
extern "C" {
#include <libavcodec/avcodec.h>
#include <libavutil/opt.h>
#include <libavformat/avformat.h>
#include <libswscale/swscale.h>
#include <libavutil/mathematics.h>
#if LIBAVFORMAT_VERSION_MAJOR >= 53
#include <libavutil/opt.h>
#else
#include <libavcodec/opt.h>
#endif
}
#include <cmath>
#include <ctime>

View File

@@ -464,6 +464,7 @@ void FFmpegWriter::flush_encoders()
if (info.has_video && video_st->codec->codec_type == AVMEDIA_TYPE_VIDEO && (oc->oformat->flags & AVFMT_RAWPICTURE) && video_codec->codec->id == CODEC_ID_RAWVIDEO)
return;
int error_code = 0;
int stop_encoding = 1;
// FLUSH VIDEO ENCODER
@@ -473,7 +474,7 @@ void FFmpegWriter::flush_encoders()
cout << "Flushing VIDEO buffer!" << endl;
// Increment PTS (in frames and scaled to the codec's timebase)
write_video_count += av_rescale_q(1, AVRational{info.fps.den, info.fps.num}, video_codec->time_base);
write_video_count += av_rescale_q(1, (AVRational){info.fps.den, info.fps.num}, video_codec->time_base);
AVPacket pkt;
av_init_packet(&pkt);
@@ -482,9 +483,14 @@ void FFmpegWriter::flush_encoders()
/* encode the image */
int got_packet = 0;
int error_code = avcodec_encode_video2(video_codec, &pkt, NULL, &got_packet);
error_code = avcodec_encode_video2(video_codec, &pkt, NULL, &got_packet);
if (error_code < 0) {
string error_description = av_err2str(error_code);
string error_description = "Unknown";
#if LIBAVFORMAT_VERSION_MAJOR >= 53
error_description = av_err2str(error_code);
#endif
cout << "error: " << error_code << ": " << error_description << endl;
throw ErrorEncodingVideo("Error while flushing video frame", -1);
}
@@ -506,10 +512,15 @@ void FFmpegWriter::flush_encoders()
pkt.stream_index = video_st->index;
// Write packet
int averror = av_interleaved_write_frame(oc, &pkt);
if (averror != 0) {
string error_description = av_err2str(averror);
cout << "error: " << averror << ": " << error_description << endl;
error_code = av_interleaved_write_frame(oc, &pkt);
if (error_code != 0) {
string error_description = "Unknown";
#if LIBAVFORMAT_VERSION_MAJOR >= 53
error_description = av_err2str(error_code);
#endif
cout << "error: " << error_code << ": " << error_description << endl;
throw ErrorEncodingVideo("Error while writing video packet to flush encoder", -1);
}
}
@@ -521,7 +532,7 @@ 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 / av_get_bytes_per_sample(audio_codec->sample_fmt), AVRational{1, info.sample_rate}, audio_codec->time_base);
write_audio_count += av_rescale_q(audio_codec->frame_size / av_get_bytes_per_sample(audio_codec->sample_fmt), (AVRational){1, info.sample_rate}, audio_codec->time_base);
AVPacket pkt;
av_init_packet(&pkt);
@@ -531,9 +542,14 @@ void FFmpegWriter::flush_encoders()
/* encode the image */
int got_packet = 0;
int error_code = avcodec_encode_audio2(audio_codec, &pkt, NULL, &got_packet);
error_code = avcodec_encode_audio2(audio_codec, &pkt, NULL, &got_packet);
if (error_code < 0) {
string error_description = av_err2str(error_code);
string error_description = "Unknown";
#if LIBAVFORMAT_VERSION_MAJOR >= 53
error_description = av_err2str(error_code);
#endif
cout << "error: " << error_code << ": " << error_description << endl;
throw ErrorEncodingAudio("Error while flushing audio frame", -1);
}
@@ -559,10 +575,15 @@ void FFmpegWriter::flush_encoders()
pkt.flags |= AV_PKT_FLAG_KEY;
// Write packet
int averror = av_write_frame(oc, &pkt);
if (averror != 0) {
string error_description = av_err2str(averror);
cout << "error: " << averror << ": " << error_description << endl;
error_code = av_write_frame(oc, &pkt);
if (error_code != 0) {
string error_description = "Unknown";
#if LIBAVFORMAT_VERSION_MAJOR >= 53
error_description = av_err2str(error_code);
#endif
cout << "error: " << error_code << ": " << error_description << endl;
throw ErrorEncodingAudio("Error while writing audio packet to flush encoder", -1);
}
}
@@ -971,7 +992,7 @@ void FFmpegWriter::write_audio_packets(bool final)
break;
// Increment PTS (in samples and scaled to the codec's timebase)
write_audio_count += av_rescale_q(audio_input_position / av_get_bytes_per_sample(audio_codec->sample_fmt), AVRational{1, info.sample_rate}, audio_codec->time_base);
write_audio_count += av_rescale_q(audio_input_position / av_get_bytes_per_sample(audio_codec->sample_fmt), (AVRational){1, info.sample_rate}, audio_codec->time_base);
// Create AVFrame (and fill it with samples)
AVFrame *frame_final = avcodec_alloc_frame();
@@ -1013,11 +1034,16 @@ void FFmpegWriter::write_audio_packets(bool final)
pkt.flags |= AV_PKT_FLAG_KEY;
/* write the compressed frame in the media file */
int averror = av_interleaved_write_frame(oc, &pkt);
if (averror != 0)
int error_code = av_interleaved_write_frame(oc, &pkt);
if (error_code != 0)
{
string error_description = av_err2str(averror);
cout << "error: " << averror << ": " << error_description << endl;
string error_description = "Unknown";
#if LIBAVFORMAT_VERSION_MAJOR >= 53
error_description = av_err2str(error_code);
#endif
cout << "error: " << error_code << ": " << error_description << endl;
throw ErrorEncodingAudio("Error while writing compressed audio frame", write_audio_count);
}
}
@@ -1159,11 +1185,15 @@ void FFmpegWriter::write_video_packet(tr1::shared_ptr<Frame> frame, AVFrame* fra
pkt.pts = write_video_count;
/* write the compressed frame in the media file */
int averror = 0;
averror = av_interleaved_write_frame(oc, &pkt);
if (averror != 0)
int error_code = av_interleaved_write_frame(oc, &pkt);
if (error_code != 0)
{
//string error_description = av_err2str(averror);
string error_description = "Unknown";
#if LIBAVFORMAT_VERSION_MAJOR >= 53
error_description = av_err2str(error_code);
#endif
throw ErrorEncodingVideo("Error while writing raw video frame", frame->number);
}
@@ -1179,7 +1209,7 @@ void FFmpegWriter::write_video_packet(tr1::shared_ptr<Frame> frame, AVFrame* fra
pkt.pts = pkt.dts = AV_NOPTS_VALUE;
// Increment PTS (in frames and scaled to the codec's timebase)
write_video_count += av_rescale_q(1, AVRational{info.fps.den, info.fps.num}, video_codec->time_base);
write_video_count += av_rescale_q(1, (AVRational){info.fps.den, info.fps.num}, video_codec->time_base);
// Assign the initial AVFrame PTS from the frame counter
frame_final->pts = write_video_count;
@@ -1206,11 +1236,16 @@ void FFmpegWriter::write_video_packet(tr1::shared_ptr<Frame> frame, AVFrame* fra
/* write the compressed frame in the media file */
//int averror = av_write_frame(oc, &pkt);
int averror = av_interleaved_write_frame(oc, &pkt);
if (averror != 0)
int error_code = av_interleaved_write_frame(oc, &pkt);
if (error_code != 0)
{
string error_description = av_err2str(averror);
cout << "error: " << averror << ": " << error_description << endl;
string error_description = "Unknown";
#if LIBAVFORMAT_VERSION_MAJOR >= 53
error_description = av_err2str(error_code);
#endif
cout << "error: " << error_code << ": " << error_description << endl;
throw ErrorEncodingVideo("Error while writing compressed video frame", frame->number);
}
}

View File

@@ -27,30 +27,30 @@ int main()
// return 0;
//omp_set_num_threads(1);
//omp_set_nested(true);
//#pragma omp parallel
//{
//#pragma omp single
//{
cout << "Start reading JPEGS" << endl;
for (int x = 0; x <= 500; x++)
{
//#pragma omp task firstprivate(x)
//{
cout << x << endl;
ImageReader r("/home/jonathan/Desktop/TEMP/1.jpg");
tr1::shared_ptr<Frame> f(r.GetFrame(1));
//f->Save("/home/jonathan/Desktop/TEMP/1.jpg", 1.0);
r.Close();
//} // end omp task
} // end for loop
//} // end omp single
//} // end omp parallel
cout << "Done reading JPEGS" << endl;
return 0;
// //omp_set_num_threads(1);
// //omp_set_nested(true);
// //#pragma omp parallel
// //{
// //#pragma omp single
// //{
// cout << "Start reading JPEGS" << endl;
// for (int x = 0; x <= 500; x++)
// {
// //#pragma omp task firstprivate(x)
// //{
// cout << x << endl;
// ImageReader r("/home/jonathan/Desktop/TEMP/1.jpg");
// tr1::shared_ptr<Frame> f(r.GetFrame(1));
// //f->Save("/home/jonathan/Desktop/TEMP/1.jpg", 1.0);
// r.Close();
// //} // end omp task
//
// } // end for loop
//
// //} // end omp single
// //} // end omp parallel
// cout << "Done reading JPEGS" << endl;
// return 0;
// Create timeline
@@ -80,8 +80,8 @@ int main()
c3.Layer(3);
c3.End(20);
c3.gravity = GRAVITY_CENTER;
//c3.scale_x.AddPoint(1, 0.1);
//c3.scale_x.AddPoint(300, 2.0);
c3.scale_x.AddPoint(1, 0.1);
c3.scale_x.AddPoint(300, 2.0);
//c3.scale_y.AddPoint(1, 0.1);
//c3.scale_y.AddPoint(300, 2.0);
@@ -182,7 +182,7 @@ int main()
// Output stream info
w.OutputStreamInfo();
for (int frame = 1; frame <= 80; frame++)
for (int frame = 1; frame <= 300; frame++)
{
tr1::shared_ptr<Frame> f = t.GetFrame(frame);
if (f)

View File

@@ -134,12 +134,12 @@ void Timeline::add_layer(tr1::shared_ptr<Frame> new_frame, Clip* source_clip, in
}
/* RESIZE SOURCE CANVAS - to the same size as timeline canvas */
// if (source_width != width || source_height != height)
// {
// source_image->borderColor(Magick::Color("none"));
// source_image->border(Magick::Geometry(1, 1, 0, 0, false, false)); // prevent stretching of edge pixels (during the canvas resize)
// source_image->size(Magick::Geometry(width, height, 0, 0, false, false)); // resize the canvas (to prevent clipping)
// }
if (source_width != width || source_height != height)
{
source_image->borderColor(Magick::Color("none"));
source_image->border(Magick::Geometry(1, 1, 0, 0, false, false)); // prevent stretching of edge pixels (during the canvas resize)
source_image->size(Magick::Geometry(width, height, 0, 0, false, false)); // resize the canvas (to prevent clipping)
}
/* LOCATION, ROTATION, AND SCALE */
float r = source_clip->rotation.GetValue(clip_frame_number); // rotate in degrees
@@ -164,9 +164,6 @@ void Timeline::add_layer(tr1::shared_ptr<Frame> new_frame, Clip* source_clip, in
// origin X,Y Scale Angle NewX,NewY
double distort_args[7] = {0,0, sx,sy, r, x-1,y-1 };
source_image->distort(Magick::ScaleRotateTranslateDistortion, 7, distort_args, false);
source_image->size(Magick::Geometry(width, height, 0, 0, false, false)); // resize the canvas (to prevent clipping)
source_image->display();
}
/* COMPOSITE SOURCE IMAGE (LAYER) ONTO FINAL IMAGE */