X-Git-Url: https://main.carlh.net/gitweb/?a=blobdiff_plain;f=src%2Flib%2Fffmpeg_file_encoder.cc;h=09845ac34bc2e03785aeb574123be489a48ce98e;hb=372555641062f5c7c6e4a1d5f67cfaf12a2c6799;hp=52977ad5bb6b12fd02cdc27eabd8c745d41267ab;hpb=e5b744922fb6aed65ec13f22a9de0c86dd1bd561;p=dcpomatic.git diff --git a/src/lib/ffmpeg_file_encoder.cc b/src/lib/ffmpeg_file_encoder.cc index 52977ad5b..09845ac34 100644 --- a/src/lib/ffmpeg_file_encoder.cc +++ b/src/lib/ffmpeg_file_encoder.cc @@ -26,7 +26,6 @@ #include "log.h" #include "image.h" #include "cross.h" -#include "butler.h" #include "compose.hpp" #include @@ -39,25 +38,24 @@ using std::pair; using boost::shared_ptr; using boost::bind; using boost::weak_ptr; +using boost::optional; +using namespace dcpomatic; int FFmpegFileEncoder::_video_stream_index = 0; int FFmpegFileEncoder::_audio_stream_index = 1; -static AVPixelFormat -force_pixel_format (AVPixelFormat, AVPixelFormat out) -{ - return out; -} - FFmpegFileEncoder::FFmpegFileEncoder ( dcp::Size video_frame_size, int video_frame_rate, int audio_frame_rate, int channels, - shared_ptr log, ExportFormat format, int x264_crf, boost::filesystem::path output +#ifdef DCPOMATIC_VARIANT_SWAROOP + , optional key + , optional id +#endif ) : _video_options (0) , _audio_channels (channels) @@ -65,11 +63,11 @@ FFmpegFileEncoder::FFmpegFileEncoder ( , _video_frame_size (video_frame_size) , _video_frame_rate (video_frame_rate) , _audio_frame_rate (audio_frame_rate) - , _log (log) { + _pixel_format = pixel_format (format); + switch (format) { case EXPORT_FORMAT_PRORES: - _pixel_format = AV_PIX_FMT_YUV422P10; _sample_format = AV_SAMPLE_FMT_S16; _video_codec_name = "prores_ks"; _audio_codec_name = "pcm_s16le"; @@ -77,7 +75,6 @@ FFmpegFileEncoder::FFmpegFileEncoder ( av_dict_set (&_video_options, "threads", "auto", 0); break; case EXPORT_FORMAT_H264: - _pixel_format = AV_PIX_FMT_YUV420P; _sample_format = AV_SAMPLE_FMT_FLTP; _video_codec_name = "libx264"; _audio_codec_name = "aac"; @@ -88,9 +85,13 @@ FFmpegFileEncoder::FFmpegFileEncoder ( setup_video (); setup_audio (); - avformat_alloc_output_context2 (&_format_context, 0, 0, _output.string().c_str()); +#ifdef DCPOMATIC_VARIANT_SWAROOP + int r = avformat_alloc_output_context2 (&_format_context, av_guess_format("mp4", 0, 0), 0, 0); +#else + int r = avformat_alloc_output_context2 (&_format_context, 0, 0, _output.string().c_str()); +#endif if (!_format_context) { - throw runtime_error ("could not allocate FFmpeg format context"); + throw runtime_error (String::compose("could not allocate FFmpeg format context (%1)", r)); } _video_stream = avformat_new_stream (_format_context, _video_codec); @@ -113,7 +114,7 @@ FFmpegFileEncoder::FFmpegFileEncoder ( throw runtime_error ("could not open FFmpeg video codec"); } - int r = avcodec_open2 (_audio_codec_context, _audio_codec, 0); + r = avcodec_open2 (_audio_codec_context, _audio_codec, 0); if (r < 0) { char buffer[256]; av_strerror (r, buffer, sizeof(buffer)); @@ -124,13 +125,45 @@ FFmpegFileEncoder::FFmpegFileEncoder ( throw runtime_error ("could not open FFmpeg output file"); } - if (avformat_write_header (_format_context, 0) < 0) { + AVDictionary* options = 0; + +#ifdef DCPOMATIC_VARIANT_SWAROOP + if (key) { + av_dict_set (&options, "encryption_key", key->hex().c_str(), 0); + /* XXX: is this OK? */ + av_dict_set (&options, "encryption_kid", "00000000000000000000000000000000", 0); + av_dict_set (&options, "encryption_scheme", "cenc-aes-ctr", 0); + } + + if (id) { + if (av_dict_set(&_format_context->metadata, SWAROOP_ID_TAG, id->c_str(), 0) < 0) { + throw runtime_error ("Could not write ID to output"); + } + } +#endif + + if (avformat_write_header (_format_context, &options) < 0) { throw runtime_error ("could not write header to FFmpeg output file"); } _pending_audio.reset (new AudioBuffers(channels, 0)); } +AVPixelFormat +FFmpegFileEncoder::pixel_format (ExportFormat format) +{ + switch (format) { + case EXPORT_FORMAT_PRORES: + return AV_PIX_FMT_YUV422P10; + case EXPORT_FORMAT_H264: + return AV_PIX_FMT_YUV420P; + default: + DCPOMATIC_ASSERT (false); + } + + return AV_PIX_FMT_YUV422P10; +} + void FFmpegFileEncoder::setup_video () { @@ -230,7 +263,7 @@ void FFmpegFileEncoder::video (shared_ptr video, DCPTime time) { shared_ptr image = video->image ( - bind (&force_pixel_format, _1, _pixel_format), + bind (&PlayerVideo::force, _1, _pixel_format), true, false );