Merge branch 'warnings' into v2.15.x.
[dcpomatic.git] / src / lib / ffmpeg_file_encoder.cc
index cd5d473f946efb92eaf13a2840fc865b9b7ce00f..f8bd14e760280392dead846d66178e997465b5a9 100644 (file)
@@ -63,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);
 
@@ -86,6 +87,8 @@ FFmpegFileEncoder::FFmpegFileEncoder (
                _audio_codec_name = "pcm_s24le";
                av_dict_set_int (&_video_options, "crf", x264_crf, 0);
                break;
+       default:
+               DCPOMATIC_ASSERT (false);
        }
 
        setup_video ();
@@ -110,11 +113,13 @@ FFmpegFileEncoder::FFmpegFileEncoder (
                throw runtime_error ("could not create FFmpeg output audio stream");
        }
 
+DCPOMATIC_DISABLE_WARNINGS
        _video_stream->id = _video_stream_index;
        _video_stream->codec = _video_codec_context;
 
        _audio_stream->id = _audio_stream_index;
        _audio_stream->codec = _audio_codec_context;
+DCPOMATIC_ENABLE_WARNINGS
 
        if (avcodec_open2 (_video_codec_context, _video_codec, &_video_options) < 0) {
                throw runtime_error ("could not open FFmpeg video codec");
@@ -127,8 +132,9 @@ FFmpegFileEncoder::FFmpegFileEncoder (
                throw runtime_error (String::compose ("could not open FFmpeg audio codec (%1)", buffer));
        }
 
-       if (avio_open_boost (&_format_context->pb, _output, AVIO_FLAG_WRITE) < 0) {
-               throw runtime_error ("could not open FFmpeg output file");
+       r = avio_open_boost (&_format_context->pb, _output, AVIO_FLAG_WRITE);
+       if (r < 0) {
+               throw runtime_error (String::compose("could not open FFmpeg output file %1 (%2)", _output.string(), r));
        }
 
        AVDictionary* options = 0;
@@ -235,7 +241,9 @@ FFmpegFileEncoder::flush ()
                packet.size = 0;
 
                int got_packet;
+DCPOMATIC_DISABLE_WARNINGS
                avcodec_encode_video2 (_video_codec_context, &packet, 0, &got_packet);
+DCPOMATIC_ENABLE_WARNINGS
                if (got_packet) {
                        packet.stream_index = 0;
                        av_interleaved_write_frame (_format_context, &packet);
@@ -248,7 +256,9 @@ FFmpegFileEncoder::flush ()
                packet.data = 0;
                packet.size = 0;
 
+DCPOMATIC_DISABLE_WARNINGS
                avcodec_encode_audio2 (_audio_codec_context, &packet, 0, &got_packet);
+DCPOMATIC_ENABLE_WARNINGS
                if (got_packet) {
                        packet.stream_index = 0;
                        av_interleaved_write_frame (_format_context, &packet);
@@ -278,7 +288,11 @@ FFmpegFileEncoder::video (shared_ptr<PlayerVideo> 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);
@@ -290,7 +304,8 @@ FFmpegFileEncoder::video (shared_ptr<PlayerVideo> 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);
@@ -298,9 +313,11 @@ FFmpegFileEncoder::video (shared_ptr<PlayerVideo> video, DCPTime time)
        packet.size = 0;
 
        int got_packet;
+DCPOMATIC_DISABLE_WARNINGS
        if (avcodec_encode_video2 (_video_codec_context, &packet, frame, &got_packet) < 0) {
                throw EncodeError ("FFmpeg video encode failed");
        }
+DCPOMATIC_ENABLE_WARNINGS
 
        if (got_packet && packet.size) {
                packet.stream_index = _video_stream_index;
@@ -385,15 +402,20 @@ 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;
        packet.size = 0;
 
        int got_packet;
+DCPOMATIC_DISABLE_WARNINGS
        if (avcodec_encode_audio2 (_audio_codec_context, &packet, frame, &got_packet) < 0) {
                throw EncodeError ("FFmpeg audio encode failed");
        }
+DCPOMATIC_ENABLE_WARNINGS
 
        if (got_packet && packet.size) {
                packet.stream_index = _audio_stream_index;
@@ -405,6 +427,7 @@ FFmpegFileEncoder::audio_frame (int size)
        av_frame_free (&frame);
 
        _pending_audio->trim_start (size);
+       _audio_frames += size;
 }
 
 void
@@ -422,5 +445,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);
+       }
 }