X-Git-Url: https://main.carlh.net/gitweb/?a=blobdiff_plain;f=src%2Flib%2Fffmpeg.cc;h=a54b4fc017def20934c831d5aca7ce548c50144b;hb=2034cf8a82be21d4d07de81ae40c8255da1ed21e;hp=31aa182937d24512d3f48c543bf7d4329f4dcc20;hpb=60450bd93af0b331d7b98c88aa199366305f0721;p=dcpomatic.git diff --git a/src/lib/ffmpeg.cc b/src/lib/ffmpeg.cc index 31aa18293..a54b4fc01 100644 --- a/src/lib/ffmpeg.cc +++ b/src/lib/ffmpeg.cc @@ -19,18 +19,19 @@ */ +#include "compose.hpp" +#include "config.h" +#include "dcpomatic_log.h" +#include "digester.h" +#include "exceptions.h" #include "ffmpeg.h" +#include "ffmpeg_audio_stream.h" #include "ffmpeg_content.h" +#include "ffmpeg_subtitle_stream.h" #include "film.h" -#include "exceptions.h" -#include "util.h" #include "log.h" -#include "dcpomatic_log.h" -#include "ffmpeg_subtitle_stream.h" -#include "ffmpeg_audio_stream.h" -#include "digester.h" -#include "compose.hpp" -#include "config.h" +#include "memory_util.h" +#include "util.h" #include extern "C" { #include @@ -72,7 +73,11 @@ FFmpeg::~FFmpeg () avcodec_free_context (&i); } - av_frame_free (&_frame); + av_frame_free (&_video_frame); + for (auto& audio_frame: _audio_frame) { + av_frame_free (&audio_frame.second); + } + avformat_close_input (&_format_context); } @@ -188,8 +193,8 @@ FFmpeg::setup_general () } } - _frame = av_frame_alloc (); - if (_frame == 0) { + _video_frame = av_frame_alloc (); + if (_video_frame == nullptr) { throw std::bad_alloc (); } } @@ -287,16 +292,18 @@ FFmpegSubtitlePeriod FFmpeg::subtitle_period (AVPacket const* packet, AVStream const* stream, AVSubtitle const & sub) { auto const packet_time = ContentTime::from_seconds (packet->pts * av_q2d(stream->time_base)); + auto const start = packet_time + ContentTime::from_seconds(sub.start_display_time / 1e3); - if (sub.end_display_time == static_cast (-1)) { - /* End time is not known */ - return FFmpegSubtitlePeriod (packet_time + ContentTime::from_seconds (sub.start_display_time / 1e3)); + if (sub.end_display_time == 0 || sub.end_display_time == static_cast(-1)) { + /* End time is not in the AVSubtitle; perhaps we can use the AVPacket's duration */ + if (packet->duration) { + return FFmpegSubtitlePeriod(start, start + ContentTime::from_seconds(packet->duration * av_q2d(stream->time_base))); + } else { + return FFmpegSubtitlePeriod(start); + } } - return FFmpegSubtitlePeriod ( - packet_time + ContentTime::from_seconds (sub.start_display_time / 1e3), - packet_time + ContentTime::from_seconds (sub.end_display_time / 1e3) - ); + return FFmpegSubtitlePeriod (start, packet_time + ContentTime::from_seconds(sub.end_display_time / 1e3)); } @@ -352,3 +359,23 @@ FFmpeg::pts_offset (vector> audio_streams, optiona return po; } + + +AVFrame * +FFmpeg::audio_frame (shared_ptr stream) +{ + auto iter = _audio_frame.find(stream); + if (iter != _audio_frame.end()) { + return iter->second; + } + + auto frame = av_frame_alloc (); + if (frame == nullptr) { + throw std::bad_alloc(); + } + + _audio_frame[stream] = frame; + return frame; + +} +