X-Git-Url: https://main.carlh.net/gitweb/?p=dcpomatic.git;a=blobdiff_plain;f=src%2Flib%2Fffmpeg_file_encoder.cc;h=e9809008098536ee6591904e2047b378683d22d1;hp=2c67bb5f927be110d0cbadea7f0d110beb0a8f4d;hb=f515b8daea9d28200be803bb64ff17e9f30343c4;hpb=ad1ef39eda58b3a919ea3b7084401a0439409ec6 diff --git a/src/lib/ffmpeg_file_encoder.cc b/src/lib/ffmpeg_file_encoder.cc index 2c67bb5f9..e98090080 100644 --- a/src/lib/ffmpeg_file_encoder.cc +++ b/src/lib/ffmpeg_file_encoder.cc @@ -38,6 +38,8 @@ 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; @@ -50,6 +52,10 @@ FFmpegFileEncoder::FFmpegFileEncoder ( ExportFormat format, int x264_crf, boost::filesystem::path output +#ifdef DCPOMATIC_VARIANT_SWAROOP + , optional key + , optional id +#endif ) : _video_options (0) , _audio_channels (channels) @@ -57,6 +63,7 @@ FFmpegFileEncoder::FFmpegFileEncoder ( , _video_frame_size (video_frame_size) , _video_frame_rate (video_frame_rate) , _audio_frame_rate (audio_frame_rate) + , _audio_frames (0) { _pixel_format = pixel_format (format); @@ -68,20 +75,32 @@ FFmpegFileEncoder::FFmpegFileEncoder ( av_dict_set (&_video_options, "profile", "3", 0); av_dict_set (&_video_options, "threads", "auto", 0); break; - case EXPORT_FORMAT_H264: + case EXPORT_FORMAT_H264_AAC: _sample_format = AV_SAMPLE_FMT_FLTP; _video_codec_name = "libx264"; _audio_codec_name = "aac"; av_dict_set_int (&_video_options, "crf", x264_crf, 0); break; + case EXPORT_FORMAT_H264_PCM: + _sample_format = AV_SAMPLE_FMT_S32; + _video_codec_name = "libx264"; + _audio_codec_name = "pcm_s24le"; + av_dict_set_int (&_video_options, "crf", x264_crf, 0); + break; + default: + DCPOMATIC_ASSERT (false); } 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("mov", 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); @@ -104,7 +123,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)); @@ -115,7 +134,24 @@ 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"); } @@ -128,7 +164,8 @@ FFmpegFileEncoder::pixel_format (ExportFormat format) switch (format) { case EXPORT_FORMAT_PRORES: return AV_PIX_FMT_YUV422P10; - case EXPORT_FORMAT_H264: + case EXPORT_FORMAT_H264_AAC: + case EXPORT_FORMAT_H264_PCM: return AV_PIX_FMT_YUV420P; default: DCPOMATIC_ASSERT (false); @@ -177,7 +214,7 @@ FFmpegFileEncoder::setup_audio () avcodec_get_context_defaults3 (_audio_codec_context, _audio_codec); /* XXX: configurable */ - _audio_codec_context->bit_rate = 256 * 1024; + _audio_codec_context->bit_rate = _audio_channels * 128 * 1024; _audio_codec_context->sample_fmt = _sample_format; _audio_codec_context->sample_rate = _audio_frame_rate; _audio_codec_context->channel_layout = av_get_default_channel_layout (_audio_channels); @@ -244,7 +281,11 @@ FFmpegFileEncoder::video (shared_ptr video, DCPTime time) AVFrame* frame = av_frame_alloc (); DCPOMATIC_ASSERT (frame); - _pending_images[image->data()[0]] = image; + { + boost::mutex::scoped_lock lm (_pending_images_mutex); + _pending_images[image->data()[0]] = image; + } + for (int i = 0; i < 3; ++i) { AVBufferRef* buffer = av_buffer_create(image->data()[i], image->stride()[i] * image->size().height, &buffer_free, this, 0); frame->buf[i] = av_buffer_ref (buffer); @@ -256,7 +297,8 @@ FFmpegFileEncoder::video (shared_ptr video, DCPTime time) frame->width = image->size().width; frame->height = image->size().height; frame->format = _pixel_format; - frame->pts = time.seconds() / av_q2d (_video_stream->time_base); + DCPOMATIC_ASSERT (_video_stream->time_base.num == 1); + frame->pts = time.get() * _video_stream->time_base.den / DCPTime::HZ; AVPacket packet; av_init_packet (&packet); @@ -328,6 +370,16 @@ FFmpegFileEncoder::audio_frame (int size) } break; } + case AV_SAMPLE_FMT_S32: + { + int32_t* q = reinterpret_cast (samples); + for (int i = 0; i < size; ++i) { + for (int j = 0; j < channels; ++j) { + *q++ = p[j][i] * 2147483647; + } + } + break; + } case AV_SAMPLE_FMT_FLTP: { float* q = reinterpret_cast (samples); @@ -341,6 +393,9 @@ FFmpegFileEncoder::audio_frame (int size) DCPOMATIC_ASSERT (false); } + DCPOMATIC_ASSERT (_audio_stream->time_base.num == 1); + frame->pts = _audio_frames * _audio_stream->time_base.den / _audio_frame_rate; + AVPacket packet; av_init_packet (&packet); packet.data = 0; @@ -361,6 +416,7 @@ FFmpegFileEncoder::audio_frame (int size) av_frame_free (&frame); _pending_audio->trim_start (size); + _audio_frames += size; } void @@ -378,5 +434,8 @@ FFmpegFileEncoder::buffer_free (void* opaque, uint8_t* data) void FFmpegFileEncoder::buffer_free2 (uint8_t* data) { - _pending_images.erase (data); + boost::mutex::scoped_lock lm (_pending_images_mutex); + if (_pending_images.find(data) != _pending_images.end()) { + _pending_images.erase (data); + } }