Skip to content

Commit

Permalink
It looks ffmpeg is a must for OSG? Now, compatible with ffmpeg-6.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
jiapei100 committed Dec 10, 2023
1 parent 2e4ae2e commit 759620a
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 28 deletions.
2 changes: 1 addition & 1 deletion src/osgPlugins/OpenCASCADE/ReaderWriterOpenCASCADE.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ osg::ref_ptr<osg::Geometry> ReaderWritterOpenCASCADE::OCCTKReader::_createGeomet
{
// populate vertex list
// Ref: http://www.opencascade.org/org/forum/thread_16694/?forum=3
gp_Pnt pt = (triangulation->Nodes())(j).Transformed(transformation * location.Transformation());
gp_Pnt pt = (triangulation->Node(j)).Transformed(transformation * location.Transformation());
vertexList->push_back(osg::Vec3(pt.X(), pt.Y(), pt.Z()));

// populate color list
Expand Down
8 changes: 4 additions & 4 deletions src/osgPlugins/ffmpeg/FFmpegDecoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ bool FFmpegDecoder::open(const std::string & filename, FFmpegParameters* paramet
{
// Open video file
AVFormatContext * p_format_context = 0;
AVInputFormat *iformat = 0;
const AVInputFormat *iformat = 0;

if (filename.compare(0, 5, "/dev/")==0)
{
Expand Down Expand Up @@ -304,9 +304,9 @@ bool FFmpegDecoder::readNextPacketNormal()
else
{
// Make the packet data available beyond av_read_frame() logical scope.
if ((error = av_dup_packet(&packet)) < 0) {
OSG_FATAL << "av_dup_packet() returned " << AvStrError(error) << std::endl;
throw std::runtime_error("av_dup_packet() failed");
if ((error = av_packet_make_refcounted(&packet)) < 0) {
OSG_FATAL << "av_packet_make_refcounted() returned " << AvStrError(error) << std::endl;
throw std::runtime_error("av_packet_make_refcounted() failed");
}

m_pending_packet = FFmpegPacket(packet);
Expand Down
14 changes: 8 additions & 6 deletions src/osgPlugins/ffmpeg/FFmpegDecoderAudio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,17 @@ static int decode_audio(AVCodecContext *avctx, int16_t *samples,
avpkt.size = buf_size;

AVFrame *frame = av_frame_alloc();
int ret, got_frame = 0;
int ret;

if (!frame)
return AVERROR(ENOMEM);

ret = avcodec_decode_audio4(avctx, frame, &got_frame, &avpkt);
ret = avcodec_receive_frame(avctx, frame);

#ifdef USE_AVRESAMPLE // libav's AVFrame structure does not contain a 'channels' field
if (ret >= 0 && got_frame) {
if (ret >= 0) {
#else
if (ret >= 0 && got_frame && av_frame_get_channels(frame)>0) {
if (ret >= 0 && frame->channels>0) {
#endif
int ch, plane_size;
int planar = av_sample_fmt_is_planar(avctx->sample_fmt);
Expand Down Expand Up @@ -151,7 +151,9 @@ void FFmpegDecoderAudio::open(AVStream * const stream, FFmpegParameters* paramet
return;

m_stream = stream;
m_context = stream->codec;
m_codecpar = stream->codecpar;
const AVCodec* p_codec = avcodec_find_decoder(m_codecpar->codec_id);
m_context = avcodec_alloc_context3(p_codec);

m_in_sample_rate = m_context->sample_rate;
m_in_nb_channels = m_context->channels;
Expand Down Expand Up @@ -214,7 +216,7 @@ printf("### CONVERTING from sample format %s TO %s\n\t\tFROM %d TO %d channels\n
throw std::runtime_error("invalid audio codec");;

// Find the decoder for the audio stream
AVCodec * const p_codec = avcodec_find_decoder(m_context->codec_id);
p_codec = avcodec_find_decoder(m_context->codec_id);

if (p_codec == 0)
throw std::runtime_error("avcodec_find_decoder() failed");
Expand Down
1 change: 1 addition & 0 deletions src/osgPlugins/ffmpeg/FFmpegDecoderAudio.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ class FFmpegDecoderAudio : public OpenThreads::Thread
PacketQueue & m_packets;
FFmpegClocks & m_clocks;
AVStream * m_stream;
AVCodecParameters * m_codecpar;
AVCodecContext * m_context;
FFmpegPacket m_packet;
const uint8_t * m_packet_data;
Expand Down
23 changes: 13 additions & 10 deletions src/osgPlugins/ffmpeg/FFmpegDecoderVideo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ FFmpegDecoderVideo::~FFmpegDecoderVideo()
void FFmpegDecoderVideo::open(AVStream * const stream)
{
m_stream = stream;
m_context = stream->codec;
m_codecpar = stream->codecpar;
const AVCodec* p_codec = avcodec_find_decoder(m_codecpar->codec_id);
m_context = avcodec_alloc_context3(p_codec);

// Trust the video size given at this point
// (avcodec_open seems to sometimes return a 0x0 size)
Expand Down Expand Up @@ -99,11 +101,12 @@ void FFmpegDecoderVideo::open(AVStream * const stream)

// Allocate converted RGB frame
m_frame_rgba.reset(av_frame_alloc());
m_buffer_rgba[0].resize(avpicture_get_size(AV_PIX_FMT_RGB24, width(), height()));
m_buffer_rgba[0].resize(av_image_get_buffer_size(AV_PIX_FMT_RGB24, width(), height(), 1));
m_buffer_rgba[1].resize(m_buffer_rgba[0].size());

// Assign appropriate parts of the buffer to image planes in m_frame_rgba
avpicture_fill((AVPicture *) (m_frame_rgba).get(), &(m_buffer_rgba[0])[0], AV_PIX_FMT_RGB24, width(), height());
AVFrame *avf = m_frame_rgba.get();
av_image_fill_arrays(avf->data, avf->linesize, &(m_buffer_rgba[0])[0], AV_PIX_FMT_RGB24, width(), height(), 1);

// Override get_buffer()/release_buffer() from codec context in order to retrieve the PTS of each frame.
m_context->opaque = this;
Expand Down Expand Up @@ -169,7 +172,7 @@ void FFmpegDecoderVideo::decodeLoop()
int frame_finished = 0;

// We want to use the entire packet since some codecs will require extra information for decoding
const int bytes_decoded = avcodec_decode_video2(m_context, m_frame.get(), &frame_finished, &(packet.packet));
const int bytes_decoded = avcodec_receive_frame(m_context, m_frame.get());

if (bytes_decoded < 0)
throw std::runtime_error("avcodec_decode_video failed()");
Expand Down Expand Up @@ -283,7 +286,7 @@ void FFmpegDecoderVideo::findAspectRatio()
m_pixel_aspect_ratio = ratio;
}

int FFmpegDecoderVideo::convert(AVPicture *dst, int dst_pix_fmt, AVPicture *src,
int FFmpegDecoderVideo::convert(AVFrame *dst, int dst_pix_fmt, AVFrame *src,
int src_pix_fmt, int src_width, int src_height)
{
osg::Timer_t startTick = osg::Timer::instance()->tick();
Expand Down Expand Up @@ -334,11 +337,11 @@ void FFmpegDecoderVideo::publishFrame(const double delay, bool audio_disabled)
return;
#endif

AVPicture * const src = (AVPicture *) m_frame.get();
AVPicture * const dst = (AVPicture *) m_frame_rgba.get();
AVFrame * const src = (AVFrame *) m_frame.get();
AVFrame * const dst = (AVFrame *) m_frame_rgba.get();

// Assign appropriate parts of the buffer to image planes in m_frame_rgba
avpicture_fill((AVPicture *) (m_frame_rgba).get(), &(m_buffer_rgba[m_writeBuffer])[0], AV_PIX_FMT_RGB24, width(), height());
// Assign appropriate parts of the buffer to image planes in m_frame_rgba
av_image_fill_arrays(dst->data, dst->linesize, &(m_buffer_rgba[m_writeBuffer])[0], AV_PIX_FMT_RGB24, width(), height(), 1);

// Convert YUVA420p (i.e. YUV420p plus alpha channel) using our own routine

Expand Down Expand Up @@ -370,7 +373,7 @@ void FFmpegDecoderVideo::publishFrame(const double delay, bool audio_disabled)



void FFmpegDecoderVideo::yuva420pToRgba(AVPicture * const dst, AVPicture * const src, int width, int height)
void FFmpegDecoderVideo::yuva420pToRgba(AVFrame * const dst, AVFrame * const src, int width, int height)
{
convert(dst, AV_PIX_FMT_RGB24, src, m_context->pix_fmt, width, height);

Expand Down
7 changes: 4 additions & 3 deletions src/osgPlugins/ffmpeg/FFmpegDecoderVideo.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@ class FFmpegDecoderVideo : public OpenThreads::Thread
void findAspectRatio();
void publishFrame(double delay, bool audio_disabled);
double synchronizeVideo(double pts);
void yuva420pToRgba(AVPicture *dst, AVPicture *src, int width, int height);
void yuva420pToRgba(AVFrame *dst, AVFrame *src, int width, int height);

int convert(AVPicture *dst, int dst_pix_fmt, AVPicture *src,
int convert(AVFrame *dst, int dst_pix_fmt, AVFrame *src,
int src_pix_fmt, int src_width, int src_height);


Expand All @@ -100,8 +100,9 @@ class FFmpegDecoderVideo : public OpenThreads::Thread
PacketQueue & m_packets;
FFmpegClocks & m_clocks;
AVStream * m_stream;
AVCodecParameters * m_codecpar;
AVCodecContext * m_context;
AVCodec * m_codec;
const AVCodec * m_codec;
const uint8_t * m_packet_data;
int m_bytes_remaining;
int64_t m_packet_pts;
Expand Down
1 change: 1 addition & 0 deletions src/osgPlugins/ffmpeg/FFmpegHeaders.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ extern "C"
#endif

#include <libavutil/mathematics.h>
#include <libavutil/imgutils.h>

#ifdef USE_SWSCALE
#include <libswscale/swscale.h>
Expand Down
2 changes: 1 addition & 1 deletion src/osgPlugins/ffmpeg/FFmpegPacket.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ namespace osgFFmpeg
void clear()
{
if (packet.data != 0)
av_free_packet(&packet);
av_packet_unref(&packet);

release();
}
Expand Down
4 changes: 2 additions & 2 deletions src/osgPlugins/ffmpeg/FFmpegParameters.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class FFmpegParameters : public osg::Referenced

bool isFormatAvailable() const { return m_format!=NULL; }

AVInputFormat* getFormat() { return m_format; }
const AVInputFormat* getFormat() { return m_format; }
AVDictionary** getOptions() { return &m_options; }
void setContext(AVIOContext* context) { m_context = context; }
AVIOContext* getContext() { return m_context; }
Expand All @@ -29,7 +29,7 @@ class FFmpegParameters : public osg::Referenced

protected:

AVInputFormat* m_format;
const AVInputFormat* m_format;
AVIOContext* m_context;
AVDictionary* m_options;
};
Expand Down
3 changes: 2 additions & 1 deletion src/osgPlugins/ffmpeg/ReaderWriterFFmpeg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
(LIBAVCODEC_VERSION_MAJOR==52 && LIBAVCODEC_VERSION_MINOR==20 && LIBAVCODEC_VERSION_MICRO >= 1)
#define USE_AV_LOCK_MANAGER
#endif
#undef USE_AV_LOCK_MANAGER

extern "C" {

Expand Down Expand Up @@ -118,7 +119,7 @@ class ReaderWriterFFmpeg : public osgDB::ReaderWriter
av_lockmgr_register(&lockMgr);
#endif
// Register all FFmpeg formats/codecs
av_register_all();
// av_register_all(); // Omit after ffmpeg 4.0

avformat_network_init();
}
Expand Down

0 comments on commit 759620a

Please sign in to comment.