home *** CD-ROM | disk | FTP | other *** search
/ ftp.ee.lbl.gov / 2014.05.ftp.ee.lbl.gov.tar / ftp.ee.lbl.gov / draft-ietf-avt-jpeg-02.txt < prev    next >
Text File  |  1995-11-20  |  28KB  |  781 lines

  1. Internet Engineering Task Force          Audio-Video Transport Working Group
  2. INTERNET-DRAFT                                                       L. Berc
  3. draft-ietf-avt-jpeg-02.txt                     Digital Equipment Corporation
  4.                                                                    W. Fenner
  5.                                                                   Xerox PARC
  6.                                                                 R. Frederick
  7.                                                                   Xerox PARC
  8.                                                                   S. McCanne
  9.                                                 Lawrence Berkeley Laboratory
  10.                                                            November 21, 1995
  11.                                                              Expires: 5/1/96
  12.  
  13.  
  14.               RTP Payload Format for JPEG-compressed Video
  15.  
  16. Status of this Memo
  17.  
  18. This document is an Internet Draft.   Internet Drafts are working  docu-
  19. ments of the Internet Engineering  Task Force (IETF), its  Areas, and
  20. its  Working Groups.   Note that other  groups may also  distribute
  21. working documents  as Internet Drafts).
  22.  
  23. Internet Drafts  are draft  documents valid  for a  maximum of  six
  24. months.  Internet Drafts may be  updated, replaced, or  obsoleted by
  25. other  documents at any time.   It  is not appropriate  to use Internet
  26. Drafts as  reference material or  to cite  them  other than  as a
  27. "working  draft" or  "work  in progress."
  28.  
  29. Please check  the I-D  abstract  listing contained  in each  Internet
  30. Draft directory to learn the current status of this or any other Inter-
  31. net Draft.
  32.  
  33. Distribution of this document is unlimited.
  34.  
  35.                                 Abstract
  36.  
  37.      This draft describes the RTP payload format for JPEG video streams.
  38.      The packet format is optimized for real-time video streams where
  39.      codec parameters change rarely from frame to frame.
  40.  
  41. This document is a product of the Audio-Video Transport working group
  42. within the Internet Engineering Task Force.  Comments are solicited and
  43. should be addressed to the working group's mailing list at rem-
  44. conf@es.net and/or the author(s).
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.                             Expires May 1996                    [Page 1]
  53.  
  54. Internet Draft         draft-ietf-avt-jpeg-02.txt          December 1995
  55.  
  56.  
  57. 1.  Introduction
  58.  
  59. The Joint Photographic Experts Group (JPEG) standard [1,2,3] defines a
  60. family of compression algorithms for continuous-tone, still images.
  61. This still image compression standard can be applied to video by
  62. compressing each frame of video as an independent still image and
  63. transmitting them in series.  Video coded in this fashion is often
  64. called Motion-JPEG.
  65.  
  66. We first give an overview of JPEG and then describe the specific subset
  67. of JPEG that is supported in RTP and the mechanism by which JPEG frames
  68. are carried as RTP payloads.
  69.  
  70. The JPEG standard defines four modes of operation: the sequential DCT
  71. mode, the progressive DCT mode, the lossless mode, and the hierarchical
  72. mode.  Depending on the mode, the image is represented in one or more
  73. passes.  Each pass (called a frame in the JPEG standard) is further bro-
  74. ken down into one or more scans.  Within each scan, there are one to
  75. four components,which represent the three components of a color signal
  76. (e.g., ``red, green, and blue'', or a luminance signal and two chroman-
  77. ince signals).  These components can be encoded as separate scans or
  78. interleaved into a single scan.
  79.  
  80. Each frame and scan is preceded with a header containing optional defin-
  81. itions for compression parameters like quantization tables and Huffman
  82. coding tables.  The headers and optional parameters are identified with
  83. ``markers'' and comprise a marker segment; each scan appears as an
  84. entropy-coded bit stream within two marker segments.  Markers are
  85. aligned to byte boundaries and (in general) cannot appear in the
  86. entropy-coded segment, allowing scan boundaries to be determined without
  87. parsing the bit stream.
  88.  
  89. Compressed data is represented in one of three formats: the interchange
  90. format, the abbreviated format, or the table-specification format.  The
  91. interchange format contains definitions for all the table used in the by
  92. the entropy-coded segments, while the abbreviated format might omit some
  93. assuming they were defined out-of-band or by a ``previous'' image.
  94.  
  95. The JPEG standard does not define the meaning or format of the com-
  96. ponents that comprise the image.  Attributes like the color space and
  97. pixel aspect ratio must be specified out-of-band with respect to the
  98. JPEG bit stream.  The JPEG File Interchange Format (JFIF) [4] is a
  99. defacto standard that provides this extra information using an applica-
  100. tion marker segment (APP0).  Note that a JFIF file is simply a JPEG
  101. interchange format image along with the APP0 segment.  In the case of
  102. video, additional parameters must be defined out-of-band (e.g., frame
  103. rate, interlaced vs. non-interlaced, etc.).
  104.  
  105.  
  106.  
  107.  
  108.                             Expires May 1996                    [Page 2]
  109.  
  110. Internet Draft         draft-ietf-avt-jpeg-02.txt          December 1995
  111.  
  112.  
  113. While the JPEG standard provides a rich set of algorithms for flexible
  114. compression, cost-effective hardware implementations of the full stan-
  115. dard have not appeared.  Instead, most hardware JPEG video codecs imple-
  116. ment only a subset of the sequential DCT mode of operation.  Typically,
  117. marker segments are interpreted in software (which ``re-programs'' the
  118. hardware) and the hardware is presented with a single, interleaved
  119. entropy-coded scan represented in the YUV color space.
  120.  
  121. 2.  JPEG Over RTP
  122.  
  123. To maximize interoperability among hardware-based codecs, we assume the
  124. sequential DCT operating mode [1,Annex F] and restrict the set of pre-
  125. defined RTP/JPEG ``type codes'' (defined below) to single-scan, inter-
  126. leaved images.  While this is more restrictive than even baseline JPEG,
  127. many hardware implementation fall short of the baseline specification
  128. (e.g., most hardware cannot decode non-interleaved scans).
  129.  
  130. In practice, most of the table-specification data rarely changes from
  131. frame to frame within a single video stream.  Therefore, RTP/JPEG data
  132. is represented in abbreviated format, with all of the tables omitted
  133. from the bit stream.  Each image begins immediately with the (single)
  134. entropy-coded scan.  The information that would otherwise be in both the
  135. frame and scan headers is represented entirely within a 64-bit RTP/JPEG
  136. header (defined below) that lies between the RTP header and the JPEG
  137. scan and is present in every packet.
  138.  
  139. While parameters like Huffman tables and color space are likely to
  140. remain fixed for the lifetime of the video stream, other parameters
  141. should be allowed to vary, notably the quantization tables and image
  142. size (e.g., to implement rate-adaptive transmission or allow a user to
  143. adjust the ``quality level'' or resolution manually).  Thus explicit
  144. fields in the RTP/JPEG header are allocated to represent this informa-
  145. tion.  Since only a small set of quantization tables are typically used,
  146. we encode the entire set of quantization tables in a small integer
  147. field.   The image width and height are encoded explicitly.
  148.  
  149. Because JPEG frames are typically larger than the underlying network's
  150. maximum packet size, frames must often be fragmented into several pack-
  151. ets.  One approach is to allow the network layer below RTP (e.g., IP) to
  152. perform the fragmentation.  However, this precludes rate-controlling the
  153. resulting packet stream or partial delivery in the presence of loss.
  154. For example, IP will not deliver a fragmented datagram to the applica-
  155. tion if one or more fragments is lost, or IP might fragment an 8000 byte
  156. frame into a burst of 8 back-to-back packets.  Instead, RTP/JPEG defines
  157. a simple fragmentation and reassembly scheme at the RTP level.
  158.  
  159.  
  160.  
  161.  
  162.  
  163.  
  164.                             Expires May 1996                    [Page 3]
  165.  
  166. Internet Draft         draft-ietf-avt-jpeg-02.txt          December 1995
  167.  
  168.  
  169. 3.  RTP/JPEG Packet Format
  170.  
  171. The RTP timestamp is in units of 90000Hz.  The same timestamp must
  172. appear across all fragments of a single frame.  The RTP marker bit is
  173. set in the last packet of a frame.
  174.  
  175. 3.1.  JPEG header
  176.  
  177. A special header is added to each packet that immediately follows the
  178. RTP header:
  179.  
  180.  0                   1                   2                   3
  181.  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
  182. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  183. |      MBZ      |              Fragment Offset                  |
  184. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  185. |      Type     |       Q       |     Width     |     Height    |
  186. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  187.  
  188.  
  189. 3.1.1.  MBZ: 8 bits
  190.  
  191.      This field is reserved for future use and must be zero.
  192.  
  193. 3.1.2.  Fragment Offset: 24 bits
  194.  
  195.      The Fragment Offset is the data offset in bytes of the current
  196.      packet in the JPEG scan.
  197.  
  198. 3.1.3.  Type: 8 bits
  199.  
  200.      The type field specifies the information that would otherwise be
  201.      present in a JPEG abbreviated table-specification as well as the
  202.      additional JFIF-style parameters not defined by JPEG.  Types 0-127
  203.      are reserved as fixed, well-known mappings to be defined by this
  204.      document and future revisions of this document.  Types 128-255 are
  205.      free to be dynamically defined by a session setup protocol (which
  206.      is beyond the scope of this document).
  207.  
  208. 3.1.4.  Q: 8 bits
  209.  
  210.      The Q field defines the quantization tables for this frame using an
  211.      algorithm that determined by the Type field (see below).
  212.  
  213. 3.1.5.  Width: 8 bits
  214.  
  215.      This field encodes the width of the image in 8-pixel multiples
  216.      (e.g., a width of 40 denotes an image 320 pixels wide).
  217.  
  218.  
  219.  
  220.                             Expires May 1996                    [Page 4]
  221.  
  222. Internet Draft         draft-ietf-avt-jpeg-02.txt          December 1995
  223.  
  224.  
  225. 3.1.6.  Height: 8 bits
  226.  
  227.      This field encodes the height of the image in 8-pixel multiples
  228.      (e.g., a height of 30 denotes an image 240 pixels tall).
  229.  
  230. 3.1.7.  Data
  231.  
  232.      The data following the RTP/JPEG header is an entropy-coded segment
  233.      consisting of a single scan.  The scan header is not present and is
  234.      inferred from the RTP/JPEG header.  The scan is terminated either
  235.      implicitly (i.e., the point at which the image is fully parsed), or
  236.      explicitly with an EOI marker.  The scan may be padded to arbitrary
  237.      length with undefined bytes.  (Existing hardware codecs generate
  238.      extra lines at the bottom of a video frame and removal of these
  239.      lines would require a Huffman-decoding pass over the data.)
  240.  
  241.      As defined by JPEG, restart markers are the only type of marker
  242.      that may appear embedded in the entropy-coded segment.  The ``type
  243.      code'' determines whether a restart interval is defined, and there-
  244.      fore whether restart markers may be present, but none of the
  245.      current codes permit them.  Hardware JPEG implementations that can-
  246.      not tolerate such markers are known to exist.
  247.  
  248.      JPEG markers appear explicitly on byte aligned boundaries beginning
  249.      with an 0xFF.  A ``stuffed'' 0x00 byte follows any 0xFF byte gen-
  250.      erated by the entropy coder [1, Sec. B.1.1.5].
  251.  
  252. 4.  Discussion
  253.  
  254. 4.1.  The Type Field
  255.  
  256. The Type field defines the abbreviated table-specification and addi-
  257. tional JFIF-style parameters not defined by JPEG, since they are not
  258. present in the body of the transmitted JPEG data.  The Type field must
  259. remain constant for the duration of a session.
  260.  
  261. Two type codes are currently defined.  They both correspond to an abbre-
  262. viated table-specification indicating the ``Baseline DCT sequential''
  263. mode, 8-bit samples, square pixels, no restart interval, three com-
  264. ponents in the YUV color space, standard Huffman tables as defined in
  265. [1, Annex K.3], and a single interleaved scan with a scan component
  266. selector indicating components 0, 1, and 2 in that order.  The Y, U, and
  267. V color planes correspond to component numbers 0, 1, and 2, respec-
  268. tively.  Component 0 (i.e., the luminance plane) uses Huffman table
  269. number 0 and quantization table number 0 (defined below) and components
  270. 1 and 2 (i.e., the chrominance planes) use Huffman table number 1 and
  271. quantization table number 1 (defined below).
  272.  
  273.  
  274.  
  275.  
  276.                             Expires May 1996                    [Page 5]
  277.  
  278. Internet Draft         draft-ietf-avt-jpeg-02.txt          December 1995
  279.  
  280.  
  281. Additionally, video is non-interlaced and unscaled (i.e., the aspect
  282. ratio is determined by the image width and height).  The frame rate is
  283. variable and explicit via the RTP timestamp.
  284.  
  285. Two RTP/JPEG types are currently defined that assume all of the above
  286. and differ only in their JPEG sampling factors:
  287.  
  288.                         horizontal   vertical
  289.            type   comp  samp. fact. samp. fact.
  290.           +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  291.           |  0  |  0  |     2     |     1     |
  292.           |  0  |  1  |     1     |     1     |
  293.           |  0  |  2  |     1     |     1     |
  294.           +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  295.           |  1  |  0  |     2     |     2     |
  296.           |  1  |  1  |     1     |     1     |
  297.           |  1  |  2  |     1     |     1     |
  298.           +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  299.  
  300. These sampling factors indicate that the chromanince components of type
  301. 0 video is downsampled horizontally by 2 (often called 4:2:2) while the
  302. chrominance components of type 1 video are downsampled both horizontally
  303. and vertically by 2 (often called 4:2:0).
  304.  
  305. Additional types in the range 128-255 may be defined by external means,
  306. such as a session protocol.
  307.  
  308. Appendix B contains C source code for transforming the RTP/JPEG header
  309. parameters into the JPEG frame and scan headers that are absent from the
  310. data payload.
  311.  
  312. 4.2.  The Q Field
  313.  
  314. The quantization tables used in the decoding process are algorithmically
  315. derived from the Q field.  The algorithm used depends on the type field
  316. but only one algorithm is currently defined for the two types.
  317.  
  318. Both type 0 and type 1 JPEG assume two quantizations tables.  These
  319. tables are chosen as follows.  For 1 <= Q <= 99, the Independent JPEG
  320. Group's formula [5] is used to produce a scale factor S as:
  321.  
  322.         S = 5000 / Q          for  1 <= Q <= 50
  323.           = 200 - 2 * Q       for 51 <= Q <= 99
  324.  
  325. This value is then used to scale Tables K.1 and K.2 from [1] (saturating
  326. each value to 8-bits) to give quantization table numbers 0 and 1,
  327. respectively.  C source code is provided in Appendix A to compute these
  328. tables.
  329.  
  330.  
  331.  
  332.                             Expires May 1996                    [Page 6]
  333.  
  334. Internet Draft         draft-ietf-avt-jpeg-02.txt          December 1995
  335.  
  336.  
  337. For Q >= 100, a dynamically defined quantization table is used, which
  338. might be specified by a session setup protocol.  (This session protocol
  339. is beyond the scope of this document).  It is expected that the standard
  340. quantization tables will handle most cases in practice, and dynamic
  341. tables will be used rarely.  Q = 0 is reserved.
  342.  
  343. 4.3.  Fragmentation and Reassembly
  344.  
  345. Since JPEG frames are large, they must often be fragmented.  Frames
  346. should be fragmented into packets in a manner avoiding fragmentation at
  347. a lower level.  When using restart markers, frames should be fragmented
  348. such that each packet starts with a restart interval (see below).
  349.  
  350. Each packet that makes up a single frame has the same timestamp.  The
  351. fragment offset field is set to the byte offset of this packet within
  352. the original frame.  The RTP marker bit is set on the last packet in a
  353. frame.
  354.  
  355. An entire frame can be identified as a sequence of packets beginning
  356. with a packet having a zero fragment offset and ending with a packet
  357. having the RTP marker bit set.  Missing packets can be detected either
  358. with RTP sequence numbers or with the fragment offset and lengths of
  359. each packet.  Reassembly could be carried out without the offset field
  360. (i.e., using only the RTP marker bit and sequence numbers), but an effi-
  361. cient single-copy implementation would not otherwise be possible in the
  362. presence of misordered packets.  Moreover, if the last packet of the
  363. previous frame (containing the marker bit) were dropped, then a receiver
  364. could not detect that the current frame is entirely intact.
  365.  
  366. 4.4.  Restart Markers
  367.  
  368. Restart markers indicate a point in the JPEG stream at which the Huffman
  369. coder is reset, allowing partial decoding starting at that point.  The
  370. use of restart markers allows for robustness in the face of packet loss.
  371. However, not all hardware decoders support restart markers, meaning that
  372. such hardware will only be able to decode the first portion of a frame,
  373. up to a restart marker, and then fail.  Thus, for maximum interoperabil-
  374. ity, restart markers may not be present in type 0 and type 1 RTP/JPEG
  375. data.
  376.  
  377. 5.  Security Considerations
  378.  
  379. Security issues are not discussed in this memo.
  380.  
  381.  
  382.  
  383.  
  384.  
  385.  
  386.  
  387.  
  388.                             Expires May 1996                    [Page 7]
  389.  
  390. Internet Draft         draft-ietf-avt-jpeg-02.txt          December 1995
  391.  
  392.  
  393. 6.  Authors' Addresses
  394.  
  395.    Lance M. Berc
  396.    Systems Research Center
  397.    Digital Equipment Corporation
  398.    130 Lytton Ave
  399.    Palo Alto CA 94301
  400.    Phone: +1 415 853 2100
  401.    Email: berc@pa.dec.com
  402.  
  403.    William C. Fenner
  404.    Xerox PARC
  405.    3333 Coyote Hill Road
  406.    Palo Alto, CA 94304
  407.    Phone: +1 415 812 4816
  408.    Email: fenner@cmf.nrl.navy.mil
  409.  
  410.    Ron Frederick
  411.    Xerox PARC
  412.    3333 Coyote Hill Road
  413.    Palo Alto, CA 94304
  414.    Phone: +1 415 812 4459
  415.    Email: frederick@parc.xerox.com
  416.  
  417.    Steven McCanne
  418.    Lawrence Berkeley Laboratory
  419.    M/S 46A-1123
  420.    One Cyclotron Road
  421.    Berkeley, CA 94720
  422.    Phone: +1 510 486 7520
  423.    Email: mccanne@ee.lbl.gov
  424.  
  425.  
  426.  
  427.  
  428.  
  429.  
  430.  
  431.  
  432.  
  433.  
  434.  
  435.  
  436.  
  437.  
  438.  
  439.  
  440.  
  441.  
  442.  
  443.  
  444.                             Expires May 1996                    [Page 8]
  445.  
  446. Internet Draft         draft-ietf-avt-jpeg-02.txt          December 1995
  447.  
  448.  
  449. 7.  References
  450.  
  451.  
  452. [1]  ISO DIS 10918-1. Digital Compression and Coding of Continuous-tone
  453.      Still Images (JPEG), CCITT Recommendation T.81.
  454.  
  455. [2]  William B. Pennebaker, Joan L. Mitchell, JPEG: Still Image Data
  456.      Compression Standard, Van Nostrand Reinhold, 1993.
  457.  
  458. [3]  Gregory K. Wallace, The JPEG Sill Picture Compression Standard,
  459.      Communications of the ACM, April 1991, Vol 34, No. 1, pp. 31-44.
  460.  
  461. [4]  The JPEG File Interchange Format.  Maintained by C-Cube Microsys-
  462.      tems, Inc., and available in
  463.      ftp://ftp.uu.net/graphics/jpeg/jfif.ps.gz.
  464.  
  465. [5]  Tom Lane et. al., The Independent JPEG Group software JPEG codec.
  466.      Source code available in
  467.      ftp://ftp.uu.net/graphics/jpeg/jpegsrc.v5.tar.gz.
  468.  
  469. Appendix A
  470.  
  471. The following code can be used to create a quantization table from a Q
  472. factor:
  473.  
  474. /*
  475.  * Table K.1 from JPEG spec.
  476.  */
  477. static const int jpeg_luma_quantizer[64] = {
  478.         16, 11, 10, 16, 24, 40, 51, 61,
  479.         12, 12, 14, 19, 26, 58, 60, 55,
  480.         14, 13, 16, 24, 40, 57, 69, 56,
  481.         14, 17, 22, 29, 51, 87, 80, 62,
  482.         18, 22, 37, 56, 68, 109, 103, 77,
  483.         24, 35, 55, 64, 81, 104, 113, 92,
  484.         49, 64, 78, 87, 103, 121, 120, 101,
  485.         72, 92, 95, 98, 112, 100, 103, 99
  486. };
  487.  
  488. /*
  489.  * Table K.2 from JPEG spec.
  490.  */
  491. static const int jpeg_chroma_quantizer[64] = {
  492.         17, 18, 24, 47, 99, 99, 99, 99,
  493.         18, 21, 26, 66, 99, 99, 99, 99,
  494.         24, 26, 56, 99, 99, 99, 99, 99,
  495.         47, 66, 99, 99, 99, 99, 99, 99,
  496.         99, 99, 99, 99, 99, 99, 99, 99,
  497.  
  498.  
  499.  
  500.                             Expires May 1996                    [Page 9]
  501.  
  502. Internet Draft         draft-ietf-avt-jpeg-02.txt          December 1995
  503.  
  504.  
  505.         99, 99, 99, 99, 99, 99, 99, 99,
  506.         99, 99, 99, 99, 99, 99, 99, 99,
  507.         99, 99, 99, 99, 99, 99, 99, 99
  508. };
  509.  
  510. /*
  511.  * Call MakeTables with the Q factor and two int[64] return arrays
  512.  */
  513. void
  514. MakeTables(int q, u_char *lum_q, u_char *chr_q)
  515. {
  516.   int i;
  517.   int factor = q;
  518.  
  519.   if (q < 1) factor = 1;
  520.   if (q > 99) factor = 99;
  521.   if (q < 50)
  522.     q = 5000 / factor;
  523.   else
  524.     q = 200 - factor*2;
  525.  
  526.   for (i=0; i < 64; i++) {
  527.     int lq = ( jpeg_luma_quantizer[i] * q + 50) / 100;
  528.     int cq = ( jpeg_chroma_quantizer[i] * q + 50) / 100;
  529.  
  530.     /* Limit the quantizers to 1 <= q <= 255 */
  531.     if ( lq < 1) lq = 1;
  532.     else if ( lq > 255) lq = 255;
  533.     lum_q[i] = lq;
  534.  
  535.     if ( cq < 1) cq = 1;
  536.     else if ( cq > 255) cq = 255;
  537.     chr_q[i] = cq;
  538.   }
  539. }
  540.  
  541.  
  542. Appendix B
  543.  
  544. The following routines can be used to create the JPEG marker segments
  545. corresponding to the table-specification data that is absent from the
  546. RTP/JPEG body.
  547.  
  548.  
  549. u_char lum_dc_codelens[] = {
  550.         0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0,
  551. };
  552.  
  553.  
  554.  
  555.  
  556.                             Expires May 1996                   [Page 10]
  557.  
  558. Internet Draft         draft-ietf-avt-jpeg-02.txt          December 1995
  559.  
  560.  
  561. u_char lum_dc_symbols[] = {
  562.         0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
  563. };
  564.  
  565. u_char lum_ac_codelens[] = {
  566.         0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 0x7d,
  567. };
  568.  
  569. u_char lum_ac_symbols[] = {
  570.         0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12,
  571.         0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07,
  572.         0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08,
  573.         0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0,
  574.         0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16,
  575.         0x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28,
  576.         0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
  577.         0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,
  578.         0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
  579.         0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
  580.         0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79,
  581.         0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
  582.         0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
  583.         0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
  584.         0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6,
  585.         0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5,
  586.         0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4,
  587.         0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2,
  588.         0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea,
  589.         0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
  590.         0xf9, 0xfa,
  591. };
  592.  
  593. u_char chm_dc_codelens[] = {
  594.         0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,
  595. };
  596.  
  597. u_char chm_dc_symbols[] = {
  598.         0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
  599. };
  600.  
  601. u_char chm_ac_codelens[] = {
  602.         0, 2, 1, 2, 4, 4, 3, 4, 7, 5, 4, 4, 0, 1, 2, 0x77,
  603. };
  604.  
  605. u_char chm_ac_symbols[] = {
  606.         0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21,
  607.         0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71,
  608.         0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91,
  609.  
  610.  
  611.  
  612.                             Expires May 1996                   [Page 11]
  613.  
  614. Internet Draft         draft-ietf-avt-jpeg-02.txt          December 1995
  615.  
  616.  
  617.         0xa1, 0xb1, 0xc1, 0x09, 0x23, 0x33, 0x52, 0xf0,
  618.         0x15, 0x62, 0x72, 0xd1, 0x0a, 0x16, 0x24, 0x34,
  619.         0xe1, 0x25, 0xf1, 0x17, 0x18, 0x19, 0x1a, 0x26,
  620.         0x27, 0x28, 0x29, 0x2a, 0x35, 0x36, 0x37, 0x38,
  621.         0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
  622.         0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
  623.         0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
  624.         0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
  625.         0x79, 0x7a, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
  626.         0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96,
  627.         0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5,
  628.         0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4,
  629.         0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3,
  630.         0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2,
  631.         0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda,
  632.         0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9,
  633.         0xea, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
  634.         0xf9, 0xfa,
  635. };
  636.  
  637. u_char *
  638. MakeQuantHeader(u_char *p, u_char *qt, int tableNo)
  639. {
  640.         *p++ = 0xff;
  641.         *p++ = 0xdb;            /* DQT */
  642.         *p++ = 0;               /* length msb */
  643.         *p++ = 67;              /* length lsb */
  644.         *p++ = tableNo;
  645.         memcpy(p, qt, 64);
  646.         return (p + 64);
  647. }
  648.  
  649. u_char *
  650. MakeHuffmanHeader(u_char *p, u_char *codelens, int ncodes, u_char *symbols,
  651.                   int nsymbols, int tableNo, int tableClass)
  652. {
  653.         *p++ = 0xff;
  654.         *p++ = 0xc4;            /* DHT */
  655.         *p++ = 0;               /* length msb */
  656.         *p++ = 3 + ncodes + nsymbols; /* length lsb */
  657.         *p++ = tableClass << 4 | tableNo;
  658.         memcpy(p, codelens, ncodes);
  659.         p += ncodes;
  660.         memcpy(p, symbols, nsymbols);
  661.         p += nsymbols;
  662.         return (p);
  663. }
  664.  
  665.  
  666.  
  667.  
  668.                             Expires May 1996                   [Page 12]
  669.  
  670. Internet Draft         draft-ietf-avt-jpeg-02.txt          December 1995
  671.  
  672.  
  673. /*
  674.  * Given an RTP/JPEG type code, q factor, width, and height,
  675.  * generate a frame and scan headers that can be prepended
  676.  * to the RTP/JPEG data payload to produce a JPEG compressed
  677.  * image in interchange format (except for possible trailing
  678.  * garbage and absence of an EOI marker to terminate the scan).
  679.  */
  680. int MakeHeaders(u_char *p, int type, int q, int w, int h)
  681. {
  682.         u_char *start = p;
  683.         u_char lqt[64];
  684.         u_char cqt[64];
  685.  
  686.         /* convert from blocks to pixels */
  687.         w <<= 3;
  688.         h <<= 3;
  689.  
  690.         MakeTables(q, lqt, cqt);
  691.  
  692.         *p++ = 0xff;
  693.         *p++ = 0xd8;            /* SOI */
  694.  
  695.         p = MakeQuantHeader(p, lqt, 0);
  696.         p = MakeQuantHeader(p, cqt, 1);
  697.  
  698.         p = MakeHuffmanHeader(p, lum_dc_codelens, sizeof(lum_dc_codelens),
  699.                               lum_dc_symbols, sizeof(lum_dc_symbols), 0, 0);
  700.         p = MakeHuffmanHeader(p, lum_ac_codelens, sizeof(lum_ac_codelens),
  701.                               lum_ac_symbols, sizeof(lum_ac_symbols), 0, 1);
  702.         p = MakeHuffmanHeader(p, chm_dc_codelens, sizeof(chm_dc_codelens),
  703.                               chm_dc_symbols, sizeof(chm_dc_symbols), 1, 0);
  704.         p = MakeHuffmanHeader(p, chm_ac_codelens, sizeof(chm_ac_codelens),
  705.                               chm_ac_symbols, sizeof(chm_ac_symbols), 1, 1);
  706.  
  707.         *p++ = 0xff;
  708.         *p++ = 0xc0;            /* SOF */
  709.         *p++ = 0;               /* length msb */
  710.         *p++ = 17;              /* length lsb */
  711.         *p++ = 8;               /* 8-bit precision */
  712.         *p++ = h >> 8;          /* height msb */
  713.         *p++ = h;               /* height lsb */
  714.         *p++ = w >> 8;          /* width msb */
  715.         *p++ = w;               /* wudth lsb */
  716.         *p++ = 3;               /* number of components */
  717.         *p++ = 0;               /* comp 0 */
  718.         if (type == 0)
  719.                 *p++ = 0x21;    /* hsamp = 2, vsamp = 1 */
  720.         else
  721.  
  722.  
  723.  
  724.                             Expires May 1996                   [Page 13]
  725.  
  726. Internet Draft         draft-ietf-avt-jpeg-02.txt          December 1995
  727.  
  728.  
  729.                 *p++ = 0x22;    /* hsamp = 2, vsamp = 2 */
  730.         *p++ = 0;               /* quant table 0 */
  731.         *p++ = 1;               /* comp 1 */
  732.         *p++ = 0x11;            /* hsamp = 1, vsamp = 1 */
  733.         *p++ = 1;               /* quant table 1 */
  734.         *p++ = 2;               /* comp 2 */
  735.         *p++ = 0x11;            /* hsamp = 1, vsamp = 1 */
  736.         *p++ = 1;               /* quant table 1 */
  737.  
  738.         *p++ = 0xff;
  739.         *p++ = 0xda;            /* SOS */
  740.         *p++ = 0;               /* length msb */
  741.         *p++ = 12;              /* length lsb */
  742.         *p++ = 3;               /* 3 components */
  743.         *p++ = 0;               /* comp 0 */
  744.         *p++ = 0;               /* huffman table 0 */
  745.         *p++ = 1;               /* comp 1 */
  746.         *p++ = 0x11;            /* huffman table 1 */
  747.         *p++ = 2;               /* comp 2 */
  748.         *p++ = 0x11;            /* huffman table 1 */
  749.         *p++ = 0;               /* first DCT coeff */
  750.         *p++ = 63;              /* last DCT coeff */
  751.         *p++ = 0;               /* sucessive approx. */
  752.  
  753.         return (p - start);
  754. };
  755.  
  756.  
  757.  
  758.  
  759.  
  760.  
  761.  
  762.  
  763.  
  764.  
  765.  
  766.  
  767.  
  768.  
  769.  
  770.  
  771.  
  772.  
  773.  
  774.  
  775.  
  776.  
  777.  
  778.  
  779.  
  780.                             Expires May 1996                   [Page 14]
  781.