Code optimisation for video

BlitzMax Forums/BlitzMax Programming/Code optimisation for video

gameshastra(Posted 2007) [#1]
I have found a difference in the smoothness of video with my code using theora as compared to the sample code provided by you for the theora module. I will be highly obliged if you could help me out on the same. My code which creates a wrapper for theora is as below. Your help is solicited.

[Code]

'-----------------------------------------------------------------------------
' File: TheoraMovie.bmx
' Author: Ramesh D
' Description: TheoraMovie class for playing movies in Theora Format
' Usage: To create a TheoraMovie object the Create function of TheoraMovie is called with the parameters as follows: video file path, audio file path, x position,y position, width, height
'-----------------------------------------------------------------------------

Import pantson.theora
SuperStrict

Type TheoraMovie
Field _vid:Ttheora
'Field _aud:TSound
Field _frameimg:TImage
Field _xpos:Int
Field _ypos:Int
Field _width:Int
Field _height:Int
Field _state:Int
Field _soundchannel:TChannel
Const SkipVideo:Int = 1
Const EndVideo:Int = 2
Const StartVideo:Int = 3

Function Create:TheoraMovie(vidname:String,audname:String,xpos:Int,ypos:Int,width:Int,height:Int)
Local mov:TheoraMovie = New TheoraMovie
mov._vid=OpenTheora(vidname)
'mov._aud = LoadSound(audname)
mov._xpos=xpos
mov._ypos=ypos
mov._width=width
mov._height=height
mov._state=StartVideo
Return mov
End Function

Method ProcessInput(id:Int,x:Int,y:Int,data:Int)
Select(id)
Case EVENT_KEYDOWN
If data=KEY_ESCAPE
_state=SkipVideo
Close()
End If
End Select
End Method

Method Start()
_frameimg=CreateImage(TheoraWidth(_vid),TheoraHeight(_vid))
StartTheora(_vid)
'_soundchannel=PlaySound(_aud)
_vid.autoloop=False
End Method

Method Update(elapsedTime:Int)
End Method

Method Draw:Int()
If (DrawTheoraImage(_vid,_frameimg) = True)
DrawImage _frameimg,_xpos,_ypos
End If
Return _vid.eot
End Method


Method Close()
'StopChannel(_soundchannel)
CloseTheora(_vid)
End Method

Method EndOfMovie:Int()
Return _vid.eot
End Method

Method Pause()
'PauseChannel(_soundchannel)
PauseTheora(_vid)
End Method

End Type

'-------------------------------------------------------------------------------------------------------------------------------------
'Main loop

Local m:TheoraMovie

Graphics 800,600,0

' load in movie
m = TheoraMovie.Create("C:\\EAS_M.ogg","C:\\EAS_S.ogg",0,0,800,600)

m.Start()

' loop until EndOfTheora
While Not m.EndOfMovie() And Not KeyDown(key_escape)
m.Draw()
Flip

Wend

' close
m.Close()
End

[/Code]


Thanking You
Regards


PantsOn(Posted 2007) [#2]
found a quick difference...

my code
If DrawTheoraImage(movie,image) = True
  Drawimage image,x,y
  Flip
endif


Your code (expanded out)
If DrawTheoraImage(movie,image) = True
  Drawimage image,x,y
endif
Flip


It seems that you are flipping the screen every loop, even when the image hasn't been drawn or updated.

Try removing the Flip from your loop...
While Not m.EndOfMovie() And Not KeyDown(key_escape)
m.Draw()
Wend

and change your wrapper to...
Method Draw:Int()
If (DrawTheoraImage(_vid,_frameimg) = True)
DrawImage _frameimg,_xpos,_ypos
Flip
End If
Return _vid.eot
End Method


See if that makes a difference


gameshastra(Posted 2007) [#3]
That was the problem.Thank you very much for the help.

I have another problem in the same code, when the
Close() method is called for the TheoraMovie Object in the middle of the movie the application crashes with memory exception error, this happens in my application but not in the example above


PantsOn(Posted 2007) [#4]
if you turn debug on it should show you the line it crashes on.
If its the DrawTheoraImage command, then you have closed the movie and then tried drawing it again (obviously this won't work)

If its the CloseTheora command then I will need to investigate furthur.


gameshastra(Posted 2007) [#5]
It gives memory exception in in the CloseTheora function, the line is

_freedecoder(movie.decoder)

Thanks


PantsOn(Posted 2007) [#6]
i have tested the code again and it doesn't produce the error with me.
I have a feeling that movie is already NULL when closing the file.
As I don't have access to your code... can you add the following code to theora.mod

can you add the following lines to mod for testing before the _freecoder(movie.decoder) line
print "closing"
if movie = null then print "already null"
print "file = "+movie.filename


you will have to re-compile the mod after (ctrl-d)

the ammendment should display closing when you call the close commmand and test if its already closed.
It will still error though.

can you see what output the ammedment produces please?


Czar Flavius(Posted 2007) [#7]
You need to use [code] in lower case for it to work.


gameshastra(Posted 2007) [#8]
I checked the variables you mentioned at the line _freedecoder(movie.decoder).
movie is not Null.
movie.filename is valid
movie.decoder is not Null it is Byte Ptr with some value. However movie.buffer of TBank type is allready Null at the line _freedecoder(movie.decoder).
movie.file has value 000000 (all zeros)

Hope that gives some clue
Thanks


PantsOn(Posted 2007) [#9]
The only time the buffer is set to null is after the _freedecoder() command.

Sounds to me the close command is getting called twice.

how many "closing" messages do you get?


gameshastra(Posted 2007) [#10]
The theora movie plays fast in the VLC media player software
but not in our program. How do we oprimize the speed. Your help is required.

The machine specs are 1.6 GHZ with 512 MB RAM

Thanks


Dreamora(Posted 2007) [#11]
you can't

VLC uses possibilities you don't have.


PantsOn(Posted 2007) [#12]
I assume when you say

The theora movie plays fast in the VLC media player software


you mean the movie plays a a correct frame rate and is smooth.

Theora (and MPEG, and DIVx, and Xvid and TV signals) stores the data in a YUV format. This is becuase its better for compressions and transmission than RGB.

This is where the "slow" bit is.. Blitz and TheoreLib then has to convert the YUV information to an RGB environment pixel by pixel.

VLC and other media players on the other hand have a special "YUV display unit" that requires no conversion.

Unfotunately Blitz doesn't have YUV display capabilities and only RGB.

So it will always be slow.
But certainly good enough for free.

(I think)