Skip to content

Commit 9398795

Browse files
start adding seek in file
1 parent 6dad33b commit 9398795

File tree

9 files changed

+65
-2
lines changed

9 files changed

+65
-2
lines changed

app/avplay/AvReader.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,9 @@ class AvReader : public Reader
9090

9191
const char* readFrameAt( const size_t frame )
9292
{
93-
// TODO seek @ frame
94-
93+
// /std::cout << "seek at " << frame << std::endl;
94+
m_inputFile.seekAtFrame( frame );
95+
m_inputStreamVideo->flushDecoder();
9596
m_inputStreamVideo->readNextFrame( *m_sourceImage );
9697
m_colorTransform.convert( *m_sourceImage, *m_imageToDisplay );
9798
return (const char*)m_imageToDisplay->getPtr();

app/avplay/Window.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
#include <iostream>
1414
#include <iomanip>
15+
#include <cstring>
1516

1617
Reader* Window::m_reader = NULL;
1718

@@ -165,6 +166,7 @@ void Window::display()
165166
glVertex2f ( x2, y1 );
166167

167168
glEnd();
169+
168170
glutSwapBuffers();
169171
}
170172

@@ -242,6 +244,7 @@ void Window::keyboard( unsigned char k, int x, int y )
242244

243245
void Window::specialKeyboard( int k, int x, int y )
244246
{
247+
//std::cout << "k=" << k << " x=" << x << " y=" << y << std::endl;
245248
switch (k)
246249
{
247250
case GLUT_KEY_UP:
@@ -252,11 +255,16 @@ void Window::specialKeyboard( int k, int x, int y )
252255
break;
253256
case GLUT_KEY_LEFT:
254257
// cursor move
258+
displayPrevFrame();
255259
break;
256260
case GLUT_KEY_RIGHT:
257261
// cursor move
258262
displayNextFrame();
259263
break;
264+
case GLUT_KEY_HOME:
265+
displayFirstFrame();
266+
case GLUT_KEY_END:
267+
break;
260268
case GLUT_KEY_F1:
261269
displayHelp();
262270
break;
@@ -528,6 +536,23 @@ void Window::displayNextFrame()
528536
loadNewTexture( buffer, m_reader->getComponents(), m_reader->getWidth(), m_reader->getHeight(), GL_RGB, GL_UNSIGNED_BYTE );
529537
}
530538

539+
void Window::displayPrevFrame()
540+
{
541+
const char* buffer = m_reader->readPrevFrame();
542+
loadNewTexture( buffer, m_reader->getComponents(), m_reader->getWidth(), m_reader->getHeight(), GL_RGB, GL_UNSIGNED_BYTE );
543+
}
544+
545+
void Window::displayFirstFrame()
546+
{
547+
displayAtFrame( 0 );
548+
}
549+
550+
void Window::displayAtFrame( const size_t frame )
551+
{
552+
const char* buffer = m_reader->readFrameAt( frame );
553+
loadNewTexture( buffer, m_reader->getComponents(), m_reader->getWidth(), m_reader->getHeight(), GL_RGB, GL_UNSIGNED_BYTE );
554+
}
555+
531556
void Window::loopPlaying( int value )
532557
{
533558
if( m_play )

app/avplay/Window.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ class Window
3737
static void showAlphaChannelTexture();
3838

3939
static void displayNextFrame();
40+
static void displayPrevFrame();
41+
static void displayFirstFrame();
42+
static void displayAtFrame( const size_t frame );
4043

4144
static void loopPlaying( int value );
4245

src/AvTranscoder/InputFile.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,24 @@ bool InputFile::readNextPacket( const size_t streamIndex )
167167
}
168168
}
169169

170+
void InputFile::seekAtFrame( const size_t frame )
171+
{
172+
uint64_t pos = frame / 25 * AV_TIME_BASE;
173+
174+
if( (int)m_formatContext->start_time != AV_NOPTS_VALUE )
175+
pos += m_formatContext->start_time;
176+
177+
if( av_seek_frame( m_formatContext, -1, pos, AVSEEK_FLAG_BACKWARD ) < 0 )
178+
{
179+
std::cerr << "Error during seek in file" << std::endl;
180+
}
181+
182+
for( std::vector<InputStream>::iterator it = m_inputStreams.begin(); it != m_inputStreams.end(); ++it )
183+
{
184+
(*it).clearBuffering();
185+
}
186+
}
187+
170188
void InputFile::readStream( const size_t streamIndex, bool readStream )
171189
{
172190
m_inputStreams.at( streamIndex ).setBufferred( readStream );

src/AvTranscoder/InputFile.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ class AvExport InputFile
4040

4141
bool readNextPacket( const size_t streamIndex );
4242

43+
void seekAtFrame( const size_t frame );
44+
4345
void readStream( const size_t streamIndex, const bool readStream = true );
4446

4547
protected:

src/AvTranscoder/InputStream.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,4 +121,9 @@ double InputStream::getPacketDuration() const
121121
return m_packetDuration * av_q2d( m_inputFile->getFormatContext()->streams[m_streamIndex]->time_base );
122122
}
123123

124+
void InputStream::clearBuffering()
125+
{
126+
m_streamCache.clear();
127+
}
128+
124129
}

src/AvTranscoder/InputStream.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ class AvExport InputStream
4949

5050
void setBufferred( const bool bufferized ){ m_bufferized = bufferized; }
5151

52+
void clearBuffering();
53+
5254
private:
5355
InputFile* m_inputFile;
5456
std::vector<DataStream> m_streamCache;

src/AvTranscoder/InputStreamVideo.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,4 +121,9 @@ bool InputStreamVideo::readNextFrame( Image& frameBuffer )
121121
return true;
122122
}
123123

124+
void InputStreamVideo::flushDecoder()
125+
{
126+
avcodec_flush_buffers( m_codecContext );
127+
}
128+
124129
}

src/AvTranscoder/InputStreamVideo.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ class AvExport InputStreamVideo
2222

2323
bool readNextFrame( Image& frameBuffer );
2424

25+
void flushDecoder();
26+
2527
private:
2628
InputStream& m_inputStream;
2729
AVCodec* m_codec;

0 commit comments

Comments
 (0)