+ // If no mapping
+ if ( map_start != j )
+ {
+ map_channels = current_channels;
+ source_offset = j;
+ }
+
+ // Copy samples if source offset valid
+ if ( source_offset < channels )
+ {
+ // Interleave the audio buffer with the # channels for this stream/mapping.
+ for ( k = 0; k < map_channels; k++, j++, source_offset++, dest_offset++ )
+ {
+ int16_t *src = audio_buf_1 + source_offset;
+ int16_t *dest = audio_buf_2 + dest_offset;
+ int s = samples + 1;
+
+ while ( --s ) {
+ *dest = *src;
+ dest += current_channels;
+ src += channels;
+ }
+ }
+ }
+ // Otherwise silence
+ else
+ {
+ j += current_channels;
+ dest_offset += current_channels;
+ }
+ }
+ pkt.size = avcodec_encode_audio( codec, audio_outbuf, audio_outbuf_size, audio_buf_2 );
+ }
+
+ // Write the compressed frame in the media file
+ if ( codec->coded_frame && codec->coded_frame->pts != AV_NOPTS_VALUE )
+ {
+ pkt.pts = av_rescale_q( codec->coded_frame->pts, codec->time_base, stream->time_base );
+ mlt_log_debug( MLT_CONSUMER_SERVICE( consumer ), "audio stream %d pkt pts %"PRId64" frame pts %"PRId64,
+ stream->index, pkt.pts, codec->coded_frame->pts );
+ }
+ pkt.flags |= PKT_FLAG_KEY;
+ pkt.stream_index = stream->index;
+ pkt.data = audio_outbuf;
+
+ if ( pkt.size > 0 )
+ {
+ if ( av_interleaved_write_frame( oc, &pkt ) )
+ {
+ mlt_log_fatal( MLT_CONSUMER_SERVICE( consumer ), "error writing audio frame\n" );
+ mlt_events_fire( properties, "consumer-fatal-error", NULL );
+ goto on_fatal_error;
+ }
+ }
+
+ mlt_log_debug( MLT_CONSUMER_SERVICE( consumer ), " frame_size %d\n", codec->frame_size );
+ if ( i == 0 )
+ {
+ audio_pts = (double)stream->pts.val * av_q2d( stream->time_base );
+ }
+ }