FFmpegEncoder fixes: handle AV_CODEC_CAP_VARIABLE_FRAME_SIZE, fix typo in interleavin...
authorCarl Hetherington <cth@carlh.net>
Tue, 16 May 2017 13:16:36 +0000 (14:16 +0100)
committerCarl Hetherington <cth@carlh.net>
Tue, 16 May 2017 13:16:36 +0000 (14:16 +0100)
src/lib/ffmpeg_encoder.cc

index eb7d476a834788605aecad224a384567367b5602..8e95c687f840bffe1cf496cc74420b99d6be3b1a 100644 (file)
@@ -205,7 +205,7 @@ FFmpegEncoder::go ()
                packet.data = 0;
                packet.size = 0;
 
-               avcodec_encode_audio2 (_video_codec_context, &packet, 0, &got_packet);
+               avcodec_encode_audio2 (_audio_codec_context, &packet, 0, &got_packet);
                if (got_packet) {
                        packet.stream_index = 0;
                        av_interleaved_write_frame (_format_context, &packet);
@@ -284,24 +284,31 @@ FFmpegEncoder::video (shared_ptr<PlayerVideo> video, DCPTime time)
        }
 }
 
+/** Called when the player gives us some audio */
 void
 FFmpegEncoder::audio (shared_ptr<AudioBuffers> audio, DCPTime)
 {
        _pending_audio->append (audio);
 
-       while (_pending_audio->frames() >= _audio_codec_context->frame_size) {
-               audio_frame (_audio_codec_context->frame_size);
+       int frame_size = _audio_codec_context->frame_size;
+       if (frame_size == 0) {
+               /* codec has AV_CODEC_CAP_VARIABLE_FRAME_SIZE */
+               frame_size = 2000;
+       }
+
+       while (_pending_audio->frames() >= frame_size) {
+               audio_frame (frame_size);
        }
 }
 
 void
 FFmpegEncoder::audio_frame (int size)
 {
+       DCPOMATIC_ASSERT (size);
+
        AVFrame* frame = av_frame_alloc ();
        DCPOMATIC_ASSERT (frame);
 
-       DCPOMATIC_ASSERT (size);
-
        int const channels = _audio_codec_context->channels;
        DCPOMATIC_ASSERT (channels);
 
@@ -321,7 +328,7 @@ FFmpegEncoder::audio_frame (int size)
        {
                int16_t* q = reinterpret_cast<int16_t*> (samples);
                for (int i = 0; i < size; ++i) {
-                       for (int j = 0; j < channels; ++i) {
+                       for (int j = 0; j < channels; ++j) {
                                *q++ = p[j][i] * 32767;
                        }
                }