       time += entry.sampleDelta * entry.sampleCount
     return time / mdhdBox.timescale
 
-  # Returns a decoding time for the given sample number
+  # Returns a decoding time for the given sample number.
+  # The sample number starts at 1.
   getDecodingTime: (sampleNumber) ->
+    mdhdBox = @findParent('mdia').find('mdhd')
+
+    sampleNumber--
     time = 0
     for entry in @entries
       if sampleNumber > entry.sampleCount


  open: (filename) ->
    startTime = process.hrtime()
    @bits = new Bits fs.readFileSync filename  # up to 1GB
    diffTime = process.hrtime startTime
    console.log "took #{(diffTime[0] * 1e9 + diffTime[1]) / 1000000} ms to read"


Bits = require './bits'
EventEmitterModule = require './event_emitter'
Sequent = require 'sequent'
fs = require 'fs'

formatDate = (date) ->
  date.toISOString()

TAG_CTOO = new Buffer([0xa9, 0x74, 0x6f, 0x6f]).toString 'utf8'
MIN_TIME_DIFF = 0.01  # seconds
READ_BUFFER_TIME = 3.0
QUEUE_BUFFER_TIME = 1.5

getCurrentTime = ->
  time = process.hrtime()
  return time[0] + time[1] / 1e9

class MP4File
  constructor: (filename) ->
