vorbis-clip.txt 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. Topic:
  2. Sample granularity editing of a Vorbis file; inferred arbitrary sample
  3. length starting offsets / PCM stream lengths
  4. Overview:
  5. Vorbis, like mp3, is a frame-based* audio compression where audio is
  6. broken up into discrete short time segments. These segments are
  7. 'atomic' that is, one must recover the entire short time segment from
  8. the frame packet; there's no way to recover only a part of the PCM time
  9. segment from part of the coded packet without expanding the entire
  10. packet and then discarding a portion of the resulting PCM audio.
  11. * In mp3, the data segment representing a given time period is called
  12. a 'frame'; the roughly equivalent Vorbis construct is a 'packet'.
  13. Thus, when we edit a Vorbis stream, the finest physical editing
  14. granularity is on these packet boundaries (the mp3 case is
  15. actually somewhat more complex and mp3 editing is more complicated
  16. than just snipping on a frame boundary because time data can be spread
  17. backward or forward over frames. In Vorbis, packets are all
  18. stand-alone). Thus, at the physical packet level, Vorbis is still
  19. limited to streams that contain an integral number of packets.
  20. However, Vorbis streams may still exactly represent and be edited to a
  21. PCM stream of arbitrary length and starting offset without padding the
  22. beginning or end of the decoded stream or requiring that the desired
  23. edit points be packet aligned. Vorbis makes use of Ogg stream
  24. framing, and this framing provides time-stamping data, called a
  25. 'granule position'; our starting offset and finished stream length may
  26. be inferred from correct usage of the granule position data.
  27. Time stamping mechanism:
  28. Vorbis packets are bundled into into Ogg pages (note that pages do not
  29. necessarily contain integral numbers of packets, but that isn't
  30. inportant in this discussion. More about Ogg framing can be found in
  31. ogg/doc/framing.html). Each page that contains a packet boundary is
  32. stamped with the absolute sample-granularity offset of the data, that
  33. is, 'complete samples-to-date' up to the last completed packet of that
  34. page. (The same mechanism is used for eg, video, where the number
  35. represents complete 2-D frames, and so on).
  36. (It's possible but rare for a packet to span more than two pages such
  37. that page[s] in the middle have no packet boundary; these packets have
  38. a granule position of '-1'.)
  39. This granule position mechaism in Ogg is used by Vorbis to indicate when the
  40. PCM data intended to be represented in a Vorbis segment begins a
  41. number of samples into the data represented by the first packet[s]
  42. and/or ends before the physical PCM data represented in the last
  43. packet[s].
  44. File length a non-integral number of frames:
  45. A file to be encoded in Vorbis will probably not encode into an
  46. integral number of packets; such a file is encoded with the last
  47. packet containing 'extra'* samples. These samples are not padding; they
  48. will be discarded in decode.
  49. *(For best results, the encoder should use extra samples that preserve
  50. the character of the last frame. Simply setting them to zero will
  51. introduce a 'cliff' that's hard to encode, resulting in spread-frame
  52. noise. Libvorbis extrapolates the last frame past the end of data to
  53. produce the extra samples. Even simply duplicating the last value is
  54. better than clamping the signal to zero).
  55. The encoder indicates to the decoder that the file is actually shorter
  56. than all of the samples ('original' + 'extra') by setting the granule
  57. position in the last page to a short value, that is, the last
  58. timestamp is the original length of the file discarding extra samples.
  59. The decoder will see that the number of samples it has decoded in the
  60. last page is too many; it is 'original' + 'extra', where the
  61. granulepos says that through the last packet we only have 'original'
  62. number of samples. The decoder then ignores the 'extra' samples.
  63. This behavior is to occur only when the end-of-stream bit is set in
  64. the page (indicating last page of the logical stream).
  65. Note that it not legal for the granule position of the last page to
  66. indicate that there are more samples in the file than actually exist,
  67. however, implementations should handle such an illegal file gracefully
  68. in the interests of robust programming.
  69. Beginning point not on integral packet boundary:
  70. It is possible that we will the PCM data represented by a Vorbis
  71. stream to begin at a position later than where the decoded PCM data
  72. really begins after an integral packet boundary, a situation analagous
  73. to the above description where the PCM data does not end at an
  74. integral packet boundary. The easiest example is taking a clip out of
  75. a larger Vorbis stream, and choosing a beginning point of the clip
  76. that is not on a packet boundary; we need to ignore a few samples to
  77. get the desired beginning point.
  78. The process of marking the desired beginning point is similar to
  79. marking an arbitrary ending point. If the encoder wishes sample zero
  80. to be some location past the actual beginning of data, it associates a
  81. 'short' granule position value with the completion of the second*
  82. audio packet. The granule position is associated with the second
  83. packet simply by making sure the second packet completes its page.
  84. *(We associate the short value with the second packet for two reasons.
  85. a) The first packet only primes the overlap/add buffer. No data is
  86. returned before decoding the second packet; this places the decision
  87. information at the point of decision. b) Placing the short value on
  88. the first packet would make the value negative (as the first packet
  89. normally represents position zero); a negative value would break the
  90. requirement that granule positions increase; the headers have
  91. position values of zero)
  92. The decoder sees that on the first page that will return
  93. data from the overlap/add queue, we have more samples than the granule
  94. position accounts for, and discards the 'surplus' from the beginning
  95. of the queue.
  96. Note that short granule values (indicating less than the actually
  97. returned about of data) are not legal in the Vorbis spec outside of
  98. indicating beginning and ending sample positions. However, decoders
  99. should, at minimum, tolerate inadvertant short values elsewhere in the
  100. stream (just as they should tolerate out-of-order/non-increasing
  101. granulepos values, although this too is illegal).
  102. Beginning point at arbitrary positive timestamp (no 'zero' sample):
  103. It's also possible that the granule position of the first page of an
  104. audio stream is a 'long value', that is, a value larger than the
  105. amount of PCM audio decoded. This implies only that we are starting
  106. playback at some point into the logical stream, a potentially common
  107. occurence in streaming applications where the decoder may be
  108. connecting into a live stream. The decoder should not treat the long
  109. value specially.
  110. A long value elsewhere in the stream would normally occur only when a
  111. page is lost or out of sequence, as indicated by the page's sequence
  112. number. A long value under any other situation is not legal, however
  113. a decoder should tolerate both possibilities.