Skip to content

Commit 5a75b14

Browse files
author
Clement Champetier
committed
Audio: decode inputStreamAudio
* AudioRewrapper: update to read next frame of inputStreamAudio until read them all. * InputFile: include InputStream instead of forward declaration. Necessary if I want to use the InputStream returned by getStream function.
1 parent f703b30 commit 5a75b14

File tree

4 files changed

+44
-32
lines changed

4 files changed

+44
-32
lines changed

app/audioRewrapper/audioRewrapper.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ void transcodeAudio( const char* inputfilename, const char* outputFilename )
5353

5454
// init audio decoders
5555
InputStreamAudio inputStreamAudio( inputFile.getStream( 0 ) );
56+
inputFile.getStream( 0 ).setBufferred( true );
5657

5758
OutputFile outputFile( outputFilename );
5859

@@ -69,13 +70,18 @@ void transcodeAudio( const char* inputfilename, const char* outputFilename )
6970
AudioFrameDesc audioFrameDesc;
7071

7172
AudioFrame audioFrame( audioFrameDesc );
72-
73+
74+
DataStream codedFrame;
7375

7476
while( inputStreamAudio.readNextFrame( audioFrame ) )
7577
{
76-
std::cout << "\rprocess frame " << (int)frame - 1 << std::flush;
78+
std::cout << "\rprocess frame " << (int)frame - 1 << std::endl << std::flush;
7779

78-
// outputFile.wrap( data, 0 );
80+
// convert
81+
82+
//outputStreamAudio.encodeFrame( audioFrame, codedFrame );
83+
84+
// outputFile.wrap( codedFrame, 0 );
7985

8086
++frame;
8187
}

src/AvTranscoder/InputFile.hpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
#ifndef _AV_TRANSCODER_INPUT_FILE_HPP_
22
#define _AV_TRANSCODER_INPUT_FILE_HPP_
33

4+
#include <AvTranscoder/AvInputStream.hpp>
45
#include <AvTranscoder/DatasStructures/DataStreamDesc.hpp>
56
#include <AvTranscoder/DatasStructures/AudioDesc.hpp>
67
#include <AvTranscoder/DatasStructures/VideoDesc.hpp>
78
#include <AvTranscoder/Metadatas/MediaMetadatasStructures.hpp>
89

9-
1010
#include <string>
1111
#include <vector>
1212

@@ -19,8 +19,6 @@ class AVCodecContext;
1919
namespace avtranscoder
2020
{
2121

22-
class AvInputStream;
23-
2422
class AvExport InputFile
2523
{
2624
public:

src/AvTranscoder/InputStreamAudio.cpp

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@ extern "C" {
1111
#include <libavutil/pixdesc.h>
1212
}
1313

14+
#include "AvInputStream.hpp"
15+
1416
#include <iostream>
1517
#include <stdexcept>
1618

1719
namespace avtranscoder
1820
{
1921

20-
InputStreamAudio::InputStreamAudio( const InputStream& inputStream )
22+
InputStreamAudio::InputStreamAudio( AvInputStream& inputStream )
2123
: m_inputStream ( &inputStream )
2224
, m_codec ( NULL )
2325
, m_codecContext ( NULL )
@@ -26,14 +28,12 @@ InputStreamAudio::InputStreamAudio( const InputStream& inputStream )
2628
{
2729
avcodec_register_all();
2830

29-
std::cout << "Audio codec Id : " << m_inputStream->getAudioDesc().getAudioCodecId() << std::endl;
30-
3131
m_codec = avcodec_find_decoder( m_inputStream->getAudioDesc().getAudioCodecId() );
3232
if( m_codec == NULL )
3333
{
3434
throw std::runtime_error( "codec not supported" );
3535
}
36-
36+
3737
m_codecContext = avcodec_alloc_context3( m_codec );
3838
if( m_codecContext == NULL )
3939
{
@@ -43,6 +43,7 @@ InputStreamAudio::InputStreamAudio( const InputStream& inputStream )
4343
m_codecContext->channels = m_inputStream->getAudioDesc().getChannels();
4444

4545
std::cout << "Audio codec Id : " << m_codecContext->codec_id << std::endl;
46+
std::cout << "Audio codec Id : " << m_codec->name << std::endl;
4647
std::cout << "Audio codec Id : " << m_codec->long_name << std::endl;
4748

4849
m_codecContext->channels = m_inputStream->getAudioDesc().getCodecContext()->channels;
@@ -102,22 +103,20 @@ InputStreamAudio::~InputStreamAudio()
102103
}
103104

104105
bool InputStreamAudio::readNextFrame( AudioFrame& audioFrameBuffer )
105-
{
106-
/*int got_frame = 0;
106+
{
107+
int got_frame = 0;
107108
while( ! got_frame )
108109
{
109-
AVPacket packet;
110-
av_init_packet( &packet );
111-
112-
if( ! m_inputStream.readNextPacket( packet ) ) // error or end of file
113-
{
114-
av_free_packet( &packet );
110+
DataStream data;
111+
if( ! m_inputStream->readNextPacket( data ) ) // error or end of file
115112
return false;
116-
}
117113

114+
AVPacket packet;
115+
av_init_packet( &packet );
116+
118117
packet.stream_index = m_selectedStream;
119-
packet.data = audioFrameBuffer.getPtr();
120-
packet.size = audioFrameBuffer.getSize();
118+
packet.data = data.getPtr();
119+
packet.size = data.getSize();
121120

122121
int ret = avcodec_decode_audio4( m_codecContext, m_frame, &got_frame, &packet );
123122

@@ -128,15 +127,22 @@ bool InputStreamAudio::readNextFrame( AudioFrame& audioFrameBuffer )
128127

129128
av_free_packet( &packet );
130129
}
131-
*/
132-
//size_t unpadded_linesize = m_frame->nb_samples * av_get_bytes_per_sample( m_frame->format );
133-
134-
// size_t decodedSize = avpicture_get_size( (AVPixelFormat)m_frame->format, m_frame->width, m_frame->height );
135-
// if( frameBuffer.getBuffer().size() != decodedSize )
136-
// frameBuffer.getBuffer().resize( avpicture_get_size( (AVPixelFormat)m_frame->format, m_frame->width, m_frame->height ) );
137-
138-
// // Copy pixel data from an AVPicture into one contiguous buffer.
139-
// avpicture_layout( (AVPicture*)m_frame, (AVPixelFormat)m_frame->format, m_frame->width, m_frame->height, &frameBuffer.getBuffer()[0], frameBuffer.getBuffer().size() );
130+
131+
size_t decodedSize = av_samples_get_buffer_size(NULL, m_codecContext->channels,
132+
m_frame->nb_samples,
133+
m_codecContext->sample_fmt, 1);
134+
135+
if( decodedSize )
136+
{
137+
if( audioFrameBuffer.getSize() != decodedSize )
138+
audioFrameBuffer.getBuffer().resize( decodedSize );
139+
140+
unsigned char* dest = audioFrameBuffer.getPtr();
141+
av_samples_fill_arrays(&dest, m_frame->linesize,
142+
m_frame->data[0],
143+
m_frame->channels, m_frame->nb_samples,
144+
m_codecContext->sample_fmt, 1);
145+
}
140146

141147
return true;
142148
}

src/AvTranscoder/InputStreamAudio.hpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,18 @@
77
namespace avtranscoder
88
{
99

10+
class AvInputStream;
11+
1012
class AvExport InputStreamAudio
1113
{
1214
public:
13-
InputStreamAudio( const InputStream& inputStream );
15+
InputStreamAudio( AvInputStream& inputStream );
1416
~InputStreamAudio();
1517

1618
bool readNextFrame( AudioFrame& audioFrameBuffer );
1719

1820
private:
19-
const InputStream* m_inputStream;
21+
AvInputStream* m_inputStream;
2022
AVCodec* m_codec;
2123
AVCodecContext* m_codecContext;
2224
AVFrame* m_frame;

0 commit comments

Comments
 (0)