Take the 2-minute tour ×

As far as I know, when we run some program, the process or files stored in secondary memory (Hard Disk) comes to primary memory (RAM) and then only program runs. My question is if, a movie file is around 10 GB and I have a RAM of 1 GB then, we can conclude that all the data from movie file is not getting loaded into RAM,

so how is it (movie file) getting loaded into RAM? I want to understand the complete internals.

What algorithms they follow to manage the same? Please help.

share|improve this question

1 Answer 1

up vote 2 down vote accepted

A movie player typically reads one chunk at a time from the movie file. I would use 2 threads to handle the video, with pseudocode like this:

Decode thread:

  read next chunk  (a chunk contains one or more frames)
  decode chunk
  add decoded frames to frame queue
  while #frames in queue > safety margin
    sleep((#frames in queue - safety margin)/framerate)
until EOF
End of movie

Display thread:

  get next frame from frame queue
  got frame?
  if yes
    display frame 
  if no
    End of movie?
       display the previous frame
  sleep until it is time to display the next frame

Edit (in response to OP:s questions in comment):

how to set the safety margin?

The purpose of the safety margin is to avoid the picture from freezing if the computer has a load spike (decoding uses a lot of processing power). If the movie has 25 frames per second, setting the safety margin at 25 frames would keep a 1 second buffer of decoded frames. I would set it at some value, perhaps 1/3 of a second and then change it based on user feedback. It has to be larger than the time it takes to decode a chunk, otherwise the movie will start to freeze up, so the chunk size is a factor when deciding on the safety margin.

And when we pause video and open another video, of say 5 GB how memory swapping happening?

When we pause the video, the decoder thread will stop decoding (note that I changed the if to a while around the sleep).

When we start a new video the decoder thread closes the old movie file, empties the frame buffer and then starts decoding the new file. Programmatically I would probably let the old decoder thread die and start a new decoder thread for the new movie file.

share|improve this answer
Thanks for reply, I have one question here as, how to set the safety margin? And when we pause video and open another video, of say 5 GB how memory swapping happening? I am curious to know (Not in programming way but in theoritically). –  Rasmi Ranjan Nayak Aug 8 '14 at 11:44
See edit in my answer. –  Klas Lindbäck Aug 8 '14 at 12:18

Your Answer


