Skip to content
This repository has been archived by the owner on Nov 19, 2020. It is now read-only.

Bug fix for multi channel audio Signal #1992

Open
wants to merge 4 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions Sources/Extras/Accord.Video.FFMPEG.GPL/VideoFileReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
// The Accord.NET Framework
// http://accord-framework.net
//
// Copyright AForge.NET, 2009-2011
// Copyright © AForge.NET, 2009-2011
// contacts@aforgenet.com
//
// Copyright MelvinGr, 2016-2017
// Copyright © MelvinGr, 2016-2017
// https://github.com/MelvinGr
//
// Copyright � C�sar Souza, 2009-2017
// Copyright © César Souza, 2009-2017
// cesarsouza at gmail.com
//
// This program is free software; you can redistribute it and/or modify
Expand Down Expand Up @@ -381,9 +381,9 @@ namespace Accord {
/// <exception cref="System::IO::IOException">Thrown if no video file was open.</exception>
/// <exception cref="VideoException">A error occurred while reading next video frame. See exception message.</exception>
///
void ReadVideoFrame(BitmapData^ output)
Bitmap^ ReadVideoFrame(BitmapData^ output)
{
readVideoFrame(-1, output, nullptr);
return readVideoFrame(-1, output, nullptr);
}

/// <summary>
Expand All @@ -396,9 +396,9 @@ namespace Accord {
/// <exception cref="System::IO::IOException">Thrown if no video file was open.</exception>
/// <exception cref="VideoException">A error occurred while reading next video frame. See exception message.</exception>
///
void ReadVideoFrame(int frameIndex, BitmapData^ output)
Bitmap^ ReadVideoFrame(int frameIndex, BitmapData^ output)
{
readVideoFrame(frameIndex, output, nullptr);
return readVideoFrame(frameIndex, output, nullptr);
}


Expand Down Expand Up @@ -443,9 +443,9 @@ namespace Accord {
/// <exception cref="System::IO::IOException">Thrown if no video file was open.</exception>
/// <exception cref="VideoException">A error occurred while reading next video frame. See exception message.</exception>
///
void ReadVideoFrame(BitmapData^ output, System::Collections::Generic::IList<byte>^ audio)
Bitmap^ ReadVideoFrame(BitmapData^ output, System::Collections::Generic::IList<byte>^ audio)
{
readVideoFrame(-1, output, audio);
return readVideoFrame(-1, output, audio);
}

/// <summary>
Expand All @@ -458,9 +458,9 @@ namespace Accord {
/// <exception cref="System::IO::IOException">Thrown if no video file was open.</exception>
/// <exception cref="VideoException">A error occurred while reading next video frame. See exception message.</exception>
///
void ReadVideoFrame(int frameIndex, BitmapData^ output, System::Collections::Generic::IList<byte>^ audio)
Bitmap^ ReadVideoFrame(int frameIndex, BitmapData^ output, System::Collections::Generic::IList<byte>^ audio)
{
readVideoFrame(frameIndex, output, audio);
return readVideoFrame(frameIndex, output, audio);
}


Expand Down
53 changes: 32 additions & 21 deletions Sources/Extras/Accord.Video.FFMPEG.GPL/VideoFileWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -372,16 +372,11 @@ namespace Accord {
// encode the image
CHECK(avcodec_encode_video2(c, &pkt, frame, &got_packet), "Error encoding video frame");

if (got_packet)
{
pkt.duration = ost->next_pts - frame->pts;
pkt.pts = ost->frame->pts;
pkt.dts = ost->frame->pts;
CHECK(write_frame(oc, &c->time_base, ost->st, &pkt), "Error while writing video frame");
return true;
}

return false;
pkt.duration = ost->next_pts - frame->pts;
pkt.pts = ost->frame->pts;
pkt.dts = ost->frame->pts;
CHECK(write_frame(oc, &c->time_base, ost->st, &pkt), "Error while writing video frame");
return true;
}

/// <summary>
Expand All @@ -401,16 +396,11 @@ namespace Accord {
// encode the signal
CHECK(avcodec_encode_audio2(c, &pkt, frame, &got_packet), "Error encoding audio frame");

if (got_packet)
{
pkt.duration = ost->next_pts - frame->pts;
pkt.pts = ost->frame->pts;
pkt.dts = ost->frame->pts;
CHECK(write_frame(oc, &c->time_base, ost->st, &pkt), "Error while writing audio frame");
return true;
}

return false;
pkt.duration = ost->next_pts - frame->pts;
pkt.pts = ost->frame->pts;
pkt.dts = ost->frame->pts;
CHECK(write_frame(oc, &c->time_base, ost->st, &pkt), "Error while writing audio frame");
return true;
}


Expand Down Expand Up @@ -442,6 +432,27 @@ namespace Accord {

void close()
{
if (have_video) {
int got_output = 0;
int ret = 0;

for (got_output = 1; got_output;) {
AVPacket pkt;
av_init_packet(&pkt);

ret = avcodec_encode_video2(c, &pkt, NULL, &got_output);
if (got_output) {
pkt.duration = 1;
pkt.pts = video_st.next_pts;
pkt.dts = pkt.pts;

CHECK(write_frame(oc, &c->time_base, video_st.st, &pkt), "Error while writing video frame");
av_free_packet(&pkt);

video_st.next_pts++;
}
}
}
// Write the trailer, if any. The trailer must be written before you
// close the CodecContexts open when you wrote the header; otherwise
// av_write_trailer() may try to use memory that was freed on
Expand Down Expand Up @@ -593,7 +604,7 @@ namespace Accord {

uint8_t* q = (uint8_t*)ost->tmp_frame->data[0];
int remainingNumberOfSamplesPerChannel = length;
size_t sampleSize = m_input_audio_sample_size;
size_t sampleSize = m_input_audio_sample_size * this->m_input_audio_channels;

while (remainingNumberOfSamplesPerChannel > 0)
{
Expand Down