home *** CD-ROM | disk | FTP | other *** search
/ Internet Core Protocols / Oreilly-InternetCoreProtocols.iso / RFCs / rfc2435.txt < prev    next >
Encoding:
Text File  |  1999-10-14  |  54.4 KB  |  1,516 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7. Network Working Group                                            L. Berc
  8. Request for Comments: 2435                 Digital Equipment Corporation
  9. Obsoletes: 2035                                                W. Fenner
  10. Category: Standards Track                                     Xerox PARC
  11.                                                             R. Frederick
  12.                                                               Xerox PARC
  13.                                                               S. McCanne
  14.                                             Lawrence Berkeley Laboratory
  15.                                                               P. Stewart
  16.                                                               Xerox PARC
  17.                                                             October 1998
  18.  
  19.  
  20.               RTP Payload Format for JPEG-compressed Video
  21.  
  22. Status of this Memo
  23.  
  24.    This document specifies an Internet standards track protocol for the
  25.    Internet community, and requests discussion and suggestions for
  26.    improvements.  Please refer to the current edition of the "Internet
  27.    Official Protocol Standards" (STD 1) for the standardization state
  28.    and status of this protocol.  Distribution of this memo is unlimited.
  29.  
  30. Copyright Notice
  31.  
  32.    Copyright (C) The Internet Society (1998).  All Rights Reserved.
  33.  
  34. Abstract
  35.  
  36.    This memo describes the RTP payload format for JPEG video streams.
  37.    The packet format is optimized for real-time video streams where
  38.    codec parameters change rarely from frame to frame.
  39.  
  40.    This document is a product of the Audio-Video Transport working group
  41.    within the Internet Engineering Task Force.  Comments are solicited
  42.    and should be addressed to the working group's mailing list at rem-
  43.    conf@es.net and/or the author(s).
  44.  
  45. Changes from RFC 2035
  46.  
  47.    Most of this memo is identical to RFC 2035.  The changes made to the
  48.    protocol are summarized in Appendix D.
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58. Berc, et. al.               Standards Track                     [Page 1]
  59.  
  60. RFC 2435              RTP Payload Format for JPEG           October 1998
  61.  
  62.  
  63. Key Words
  64.  
  65.    The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT",
  66.    "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this
  67.    document are to be interpreted as described in RFC 2119 [9].
  68.  
  69. 1.  Introduction
  70.  
  71.    The Joint Photographic Experts Group (JPEG) standard [1,2,3] defines
  72.    a family of compression algorithms for continuous-tone, still images.
  73.    This still image compression standard can be applied to video by
  74.    compressing each frame of video as an independent still image and
  75.    transmitting them in series.  Video coded in this fashion is often
  76.    called Motion-JPEG.
  77.  
  78.    We first give an overview of JPEG and then describe the specific
  79.    subset of JPEG that is supported in RTP and the mechanism by which
  80.    JPEG frames are carried as RTP payloads.
  81.  
  82.    The JPEG standard defines four modes of operation: the sequential DCT
  83.    mode, the progressive DCT mode, the lossless mode, and the
  84.    hierarchical mode.  Depending on the mode, the image is represented
  85.    in one or more passes.  Each pass (called a frame in the JPEG
  86.    standard) is further broken down into one or more scans.  Within each
  87.    scan, there are one to four components, which represent the three
  88.    components of a color signal (e.g., "red, green, and blue", or a
  89.    luminance signal and two chrominance signals).  These components can
  90.    be encoded as separate scans or interleaved into a single scan.
  91.  
  92.    Each frame and scan is preceded with a header containing optional
  93.    definitions for compression parameters like quantization tables and
  94.    Huffman coding tables.  The headers and optional parameters are
  95.    identified with "markers" and comprise a marker segment; each scan
  96.    appears as an entropy-coded bit stream within two marker segments.
  97.    Markers are aligned to byte boundaries and (in general) cannot appear
  98.    in the entropy-coded segment, allowing scan boundaries to be
  99.    determined without parsing the bit stream.
  100.  
  101.    Compressed data is represented in one of three formats: the
  102.    interchange format, the abbreviated format, or the table-
  103.    specification format.  The interchange format contains definitions
  104.    for all the tables used by the entropy-coded segments, while the
  105.    abbreviated format might omit some assuming they were defined out-
  106.    of-band or by a "previous" image.
  107.  
  108.    The JPEG standard does not define the meaning or format of the
  109.    components that comprise the image.  Attributes like the color space
  110.    and pixel aspect ratio must be specified out-of-band with respect to
  111.  
  112.  
  113.  
  114. Berc, et. al.               Standards Track                     [Page 2]
  115.  
  116. RFC 2435              RTP Payload Format for JPEG           October 1998
  117.  
  118.  
  119.    the JPEG bit stream.  The JPEG File Interchange Format (JFIF) [4] is
  120.    a de-facto standard that provides this extra information using an
  121.    application marker segment (APP0).  Note that a JFIF file is simply a
  122.    JPEG interchange format image along with the APP0 segment.  In the
  123.    case of video, additional parameters must be defined out-of-band
  124.    (e.g., frame rate, interlaced vs. non-interlaced, etc.).
  125.  
  126.    While the JPEG standard provides a rich set of algorithms for
  127.    flexible compression, cost-effective hardware implementations of the
  128.    full standard have not appeared.  Instead, most hardware JPEG video
  129.    codecs implement only a subset of the sequential DCT mode of
  130.    operation.  Typically, marker segments are interpreted in software
  131.    (which "re-programs" the hardware) and the hardware is presented with
  132.    a single, interleaved entropy-coded scan represented in the YUV color
  133.    space.
  134.  
  135.    The scan contains an ordered sequence of Minimum Coded Units, or
  136.    MCUs, which are the smallest group of image data coded in a JPEG bit
  137.    stream.  Each MCU defines the image data for a small rectangular
  138.    block of the output image.
  139.  
  140.    Restart markers in the JPEG data denote a point where the decoder
  141.    should reset its state.  As defined by JPEG, restart markers are the
  142.    only type of marker that may appear embedded in the entropy-coded
  143.    segment, and they may only appear on an MCU boundary.  A "restart
  144.    interval" is defined to be a block of data containing a restart
  145.    marker followed by some fixed number of MCUs.  An exception is made
  146.    for the first restart interval in each frame, which omits the initial
  147.    restart marker and just begins with the MCU data.  When these markers
  148.    are used, each frame is composed of some fixed number of back-to-back
  149.    restart intervals.
  150.  
  151. 2.  JPEG Over RTP
  152.  
  153.    To maximize interoperability among hardware-based codecs, we assume
  154.    the sequential DCT operating mode [1,Annex F] and restrict the set of
  155.    predefined RTP/JPEG "type codes" (defined below) to single-scan,
  156.    interleaved images.  While this is more restrictive than even
  157.    baseline JPEG, many hardware implementation fall short of the
  158.    baseline specification (e.g., most hardware cannot decode non-
  159.    interleaved scans).
  160.  
  161.    In practice, most of the table-specification data rarely changes from
  162.    frame to frame within a single video stream.  Therefore RTP/JPEG data
  163.    is represented in abbreviated format, with all of the tables omitted
  164.    from the bit stream where possible.  Each frame begins immediately
  165.    with the (single) entropy-coded scan.  The information that would
  166.    otherwise be in both the frame and scan headers is represented
  167.  
  168.  
  169.  
  170. Berc, et. al.               Standards Track                     [Page 3]
  171.  
  172. RFC 2435              RTP Payload Format for JPEG           October 1998
  173.  
  174.  
  175.    entirely within the RTP/JPEG header (defined below) that lies between
  176.    the RTP header and the JPEG payload.
  177.  
  178.    While parameters like Huffman tables and color space are likely to
  179.    remain fixed for the lifetime of the video stream, other parameters
  180.    should be allowed to vary, notably the quantization tables and image
  181.    size (e.g., to implement rate-adaptive transmission or allow a user
  182.    to adjust the "quality level" or resolution manually).  Thus explicit
  183.    fields in the RTP/JPEG header are allocated to represent this
  184.    information.  Since only a small set of quantization tables are
  185.    typically used, we encode the entire set of quantization tables in a
  186.    small integer field.  Customized quantization tables are accommodated
  187.    by using a special range of values in this field, and then placing
  188.    the table before the beginning of the JPEG payload.  The image width
  189.    and height are encoded explicitly.
  190.  
  191.    Because JPEG frames are typically larger than the underlying
  192.    network's maximum packet size, frames must often be fragmented into
  193.    several packets.  One approach is to allow the network layer below
  194.    RTP (e.g., IP) to perform the fragmentation.  However, this precludes
  195.    rate-controlling the resulting packet stream or partial delivery in
  196.    the presence of loss, and frames may be larger than the maximum
  197.    network layer reassembly length (see [10] for more information).  To
  198.    avoid these limitations, RTP/JPEG defines a simple fragmentation and
  199.    reassembly scheme at the RTP level.
  200.  
  201. 3.  RTP/JPEG Packet Format
  202.  
  203.    The RTP timestamp is in units of 90000Hz.  The same timestamp MUST
  204.    appear in each fragment of a given frame.  The RTP marker bit MUST be
  205.    set in the last packet of a frame.
  206.  
  207. 3.1.  JPEG header
  208.  
  209.    Each packet contains a special JPEG header which immediately follows
  210.    the RTP header.  The first 8 bytes of this header, called the "main
  211.    JPEG header", are as follows:
  212.  
  213.     0                   1                   2                   3
  214.     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
  215.    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  216.    | Type-specific |              Fragment Offset                  |
  217.    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  218.    |      Type     |       Q       |     Width     |     Height    |
  219.    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  220.  
  221.  
  222.  
  223.  
  224.  
  225.  
  226. Berc, et. al.               Standards Track                     [Page 4]
  227.  
  228. RFC 2435              RTP Payload Format for JPEG           October 1998
  229.  
  230.  
  231.    All fields in this header except for the Fragment Offset field MUST
  232.    remain the same in all packets that correspond to the same JPEG
  233.    frame.
  234.  
  235.    A Restart Marker header and/or Quantization Table header may follow
  236.    this header, depending on the values of the Type and Q fields.
  237.  
  238. 3.1.1.  Type-specific: 8 bits
  239.  
  240.    Interpretation depends on the value of the type field.  If no
  241.    interpretation is specified, this field MUST be zeroed on
  242.    transmission and ignored on reception.
  243.  
  244. 3.1.2.  Fragment Offset: 24 bits
  245.  
  246.    The Fragment Offset is the offset in bytes of the current packet in
  247.    the JPEG frame data. This value is encoded in network byte order
  248.    (most significant byte first). The Fragment Offset plus the length of
  249.    the payload data in the packet MUST NOT exceed 2^24 bytes.
  250.  
  251. 3.1.3.  Type: 8 bits
  252.  
  253.    The type field specifies the information that would otherwise be
  254.    present in a JPEG abbreviated table-specification as well as the
  255.    additional JFIF-style parameters not defined by JPEG.  Types 0-63 are
  256.    reserved as fixed, well-known mappings to be defined by this document
  257.    and future revisions of this document.  Types 64-127 are the same as
  258.    types 0-63, except that restart markers are present in the JPEG data
  259.    and a Restart Marker header appears immediately following the main
  260.    JPEG header.  Types 128-255 are free to be dynamically defined by a
  261.    session setup protocol (which is beyond the scope of this document).
  262.  
  263. 3.1.4.  Q: 8 bits
  264.  
  265.    The Q field defines the quantization tables for this frame.  Q values
  266.    0-127 indicate the quantization tables are computed using an
  267.    algorithm determined by the Type field (see below).  Q values 128-255
  268.    indicate that a Quantization Table header appears after the main JPEG
  269.    header (and the Restart Marker header, if present) in the first
  270.    packet of the frame (fragment offset 0).  This header can be used to
  271.    explicitly specify the quantization tables in-band.
  272.  
  273. 3.1.5.  Width: 8 bits
  274.  
  275.    This field encodes the width of the image in 8-pixel multiples (e.g.,
  276.    a width of 40 denotes an image 320 pixels wide).  The maximum width
  277.    is 2040 pixels.
  278.  
  279.  
  280.  
  281.  
  282. Berc, et. al.               Standards Track                     [Page 5]
  283.  
  284. RFC 2435              RTP Payload Format for JPEG           October 1998
  285.  
  286.  
  287. 3.1.6.  Height: 8 bits
  288.  
  289.    This field encodes the height of the image in 8-pixel multiples
  290.    (e.g., a height of 30 denotes an image 240 pixels tall). When
  291.    encoding interlaced video, this is the height of a video field, since
  292.    fields are individually JPEG encoded. The maximum height is 2040
  293.    pixels.
  294.  
  295. 3.1.7.  Restart Marker header
  296.  
  297.    This header MUST be present immediately after the main JPEG header
  298.    when using types 64-127.  It provides the additional information
  299.    required to properly decode a data stream containing restart markers.
  300.  
  301.     0                   1                   2                   3
  302.     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
  303.    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  304.    |       Restart Interval        |F|L|       Restart Count       |
  305.    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  306.  
  307.    The Restart Interval field specifies the number of MCUs that appear
  308.    between restart markers.  It is identical to the 16 bit value that
  309.    would appear in the DRI marker segment of a JFIF header.  This value
  310.    MUST NOT be zero.
  311.  
  312.    If the restart intervals in a frame are not guaranteed to be aligned
  313.    with packet boundaries, the F (first) and L (last) bits MUST be set
  314.    to 1 and the Restart Count MUST be set to 0x3FFF.  This indicates
  315.    that a receiver MUST reassemble the entire frame before decoding it.
  316.  
  317.    To support partial frame decoding, the frame is broken into "chunks"
  318.    each containing an integral number of restart intervals. The Restart
  319.    Count field contains the position of the first restart interval in
  320.    the current "chunk" so that receivers know which part of the frame
  321.    this data corresponds to.  A Restart Interval value SHOULD be chosen
  322.    to allow a "chunk" to completely fit within a single packet.  In this
  323.    case, both the F and L bits of the packet are set to 1.  However, if
  324.    a chunk needs to be spread across multiple packets, the F bit will be
  325.    set to 1 in the first packet of the chunk (and only that one) and the
  326.    L bit will be set to 1 in the last packet of the chunk (and only that
  327.    one).
  328.  
  329. 3.1.8.  Quantization Table header
  330.  
  331.    This header MUST be present after the main JPEG header (and after the
  332.    Restart Marker header, if present) when using Q values 128-255.  It
  333.    provides a way to specify the quantization tables associated with
  334.    this Q value in-band.
  335.  
  336.  
  337.  
  338. Berc, et. al.               Standards Track                     [Page 6]
  339.  
  340. RFC 2435              RTP Payload Format for JPEG           October 1998
  341.  
  342.  
  343.     0                   1                   2                   3
  344.     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
  345.    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  346.    |      MBZ      |   Precision   |             Length            |
  347.    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  348.    |                    Quantization Table Data                    |
  349.    |                              ...                              |
  350.    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  351.  
  352.    The Length field is set to the length in bytes of the quantization
  353.    table data to follow.  The Length field MAY be set to zero to
  354.    indicate that no quantization table data is included in this frame.
  355.    See section 4.2 for more information.  If the Length field in a
  356.    received packet is larger than the remaining number of bytes, the
  357.    packet MUST be discarded.
  358.  
  359.    When table data is included, the number of tables present depends on
  360.    the JPEG type field.  For example, type 0 uses two tables (one for
  361.    the luminance component and one shared by the chrominance
  362.    components).  Each table is an array of 64 values given in zig-zag
  363.    order, identical to the format used in a JFIF DQT marker segment.
  364.  
  365.    For each quantization table present, a bit in the Precision field
  366.    specifies the size of the coefficients in that table.  If the bit is
  367.    zero, the coefficients are 8 bits yielding a table length of 64
  368.    bytes.  If the bit is one, the coefficients are 16 bits for a table
  369.    length of 128 bytes.  For 16 bit tables, the coefficients are
  370.    presented in network byte order.  The rightmost bit in the Precision
  371.    field (bit 15 in the diagram above) corresponds to the first table
  372.    and each additional table uses the next bit to the left.  Bits beyond
  373.    those corresponding to the tables needed by the type in use MUST be
  374.    ignored.
  375.  
  376.    For Q values from 128 to 254, the Q value to quantization table data
  377.    mapping MUST be static, i.e., the receivers are guaranteed that they
  378.    only need to read the table data once in order to correctly decode
  379.    frames sent with that Q value.  A Q value of 255 denotes that the
  380.    quantization table mapping is dynamic and can change on every frame.
  381.    Decoders MUST NOT depend on any previous version of the tables, and
  382.    need to reload these tables on every frame.  Packets MUST NOT contain
  383.    Q = 255 and Length = 0.
  384.  
  385. 3.1.9.  JPEG Payload
  386.  
  387.    The data following the RTP/JPEG headers is an entropy-coded segment
  388.    consisting of a single scan.  The scan header is not present and is
  389.    inferred from the RTP/JPEG header.  The scan is terminated either
  390.    implicitly (i.e., the point at which the image is fully parsed), or
  391.  
  392.  
  393.  
  394. Berc, et. al.               Standards Track                     [Page 7]
  395.  
  396. RFC 2435              RTP Payload Format for JPEG           October 1998
  397.  
  398.  
  399.    explicitly with an EOI marker.  The scan may be padded to arbitrary
  400.    length with undefined bytes.  (Some existing hardware codecs generate
  401.    extra lines at the bottom of a video frame and removal of these lines
  402.    would require a Huffman-decoding pass over the data.)
  403.  
  404.    The type code determines whether restart markers are present.  If a
  405.    type supports restart markers, the packet MUST contain a non-zero
  406.    Restart Interval value in a Restart Marker Header and restart markers
  407.    MUST appear on byte aligned boundaries beginning with an 0xFF between
  408.    MCUs at that interval.  Additional 0xFF bytes MAY appear between
  409.    restart intervals.  This can be used in the packetization process to
  410.    align data to something like a word boundary for more efficient
  411.    copying.  Restart markers MUST NOT appear anywhere else in the JPEG
  412.    payload.  Types which do not support restart makers MUST NOT contain
  413.    restart markers anywhere in the JPEG payload. All packets MUST
  414.    contain a "stuffed" 0x00 byte following any true 0xFF byte generated
  415.    by the entropy coder [1, Sec.  B.1.1.5].
  416.  
  417. 4.  Discussion
  418.  
  419. 4.1.  The Type Field
  420.  
  421.    The Type field defines the abbreviated table-specification and
  422.    additional JFIF-style parameters not defined by JPEG, since they are
  423.    not present in the body of the transmitted JPEG data.
  424.  
  425.    Three ranges of the type field are currently defined. Types 0-63 are
  426.    reserved as fixed, well-known mappings to be defined by this document
  427.    and future revisions of this document. Types 64-127 are the same as
  428.    types 0-63, except that restart markers are present in the JPEG data
  429.    and a Restart Marker header appears immediately following the main
  430.    JPEG header. Types 128-255 are free to be dynamically defined by a
  431.    session setup protocol (which is beyond the scope of this document).
  432.  
  433.    Of the first group of fixed mappings, types 0 and 1 are currently
  434.    defined, along with the corresponding types 64 and 65 that indicate
  435.    the presence of restart markers.  They correspond to an abbreviated
  436.    table-specification indicating the "Baseline DCT sequential" mode,
  437.    8-bit samples, square pixels, three components in the YUV color
  438.    space, standard Huffman tables as defined in [1, Annex K.3], and a
  439.    single interleaved scan with a scan component selector indicating
  440.    components 1, 2, and 3 in that order.  The Y, U, and V color planes
  441.    correspond to component numbers 1, 2, and 3, respectively.  Component
  442.    1 (i.e., the luminance plane) uses Huffman table number 0 and
  443.    quantization table number 0 (defined below) and components 2 and 3
  444.    (i.e., the chrominance planes) use Huffman table number 1 and
  445.    quantization table number 1 (defined below).
  446.  
  447.  
  448.  
  449.  
  450. Berc, et. al.               Standards Track                     [Page 8]
  451.  
  452. RFC 2435              RTP Payload Format for JPEG           October 1998
  453.  
  454.  
  455.    Type numbers 2-5 are reserved and SHOULD NOT be used.  Applications
  456.    based on previous versions of this document (RFC 2035) should be
  457.    updated to indicate the presence of restart markers with type 64 or
  458.    65 and the Restart Marker header.
  459.  
  460.    The two RTP/JPEG types currently defined are described below:
  461.  
  462.                             horizontal   vertical   Quantization
  463.            types  component samp. fact. samp. fact. table number
  464.          +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  465.          |       |  1 (Y)  |     2     |     1     |     0     |
  466.          | 0, 64 |  2 (U)  |     1     |     1     |     1     |
  467.          |       |  3 (V)  |     1     |     1     |     1     |
  468.          +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  469.          |       |  1 (Y)  |     2     |     2     |     0     |
  470.          | 1, 65 |  2 (U)  |     1     |     1     |     1     |
  471.          |       |  3 (V)  |     1     |     1     |     1     |
  472.          +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  473.  
  474.    These sampling factors indicate that the chrominance components of
  475.    type 0 video is downsampled horizontally by 2 (often called 4:2:2)
  476.    while the chrominance components of type 1 video are downsampled both
  477.    horizontally and vertically by 2 (often called 4:2:0).
  478.  
  479.    Types 0 and 1 can be used to carry both progressively scanned and
  480.    interlaced image data.  This is encoded using the Type-specific field
  481.    in the main JPEG header.  The following values are defined:
  482.  
  483.       0 : Image is progressively scanned.  On a computer monitor, it can
  484.           be displayed as-is at the specified width and height.
  485.  
  486.       1 : Image is an odd field of an interlaced video signal.  The
  487.           height specified in the main JPEG header is half of the height
  488.           of the entire displayed image.  This field should be de-
  489.           interlaced with the even field following it such that lines
  490.           from each of the images alternate.  Corresponding lines from
  491.           the even field should appear just above those same lines from
  492.           the odd field.
  493.  
  494.       2 : Image is an even field of an interlaced video signal.
  495.  
  496.       3 : Image is a single field from an interlaced video signal, but
  497.           it should be displayed full frame as if it were received as
  498.           both the odd & even fields of the frame.  On a computer
  499.           monitor, each line in the image should be displayed twice,
  500.           doubling the height of the image.
  501.  
  502.  
  503.  
  504.  
  505.  
  506. Berc, et. al.               Standards Track                     [Page 9]
  507.  
  508. RFC 2435              RTP Payload Format for JPEG           October 1998
  509.  
  510.  
  511.    Appendix B contains C source code for transforming the RTP/JPEG
  512.    header parameters into the JPEG frame and scan headers that are
  513.    absent from the data payload.
  514.  
  515. 4.2.  The Q Field
  516.  
  517.    For JPEG types 0 and 1 (and their corresponding types 64 and 65), Q
  518.    values between 1 and 99 inclusive are defined as follows.  Other
  519.    values less than 128 are reserved.  Additional types are encouraged
  520.    to use this definition if applicable.
  521.  
  522.    Both type 0 and type 1 JPEG require two quantization tables.  These
  523.    tables are calculated as follows.  For 1 <= Q <= 99, the Independent
  524.    JPEG Group's formula [5] is used to produce a scale factor S as:
  525.  
  526.            S = 5000 / Q          for  1 <= Q <= 50
  527.              = 200 - 2 * Q       for 51 <= Q <= 99
  528.  
  529.    This value is then used to scale Tables K.1 and K.2 from [1]
  530.    (saturating each value to 8 bits) to give quantization table numbers
  531.    0 and 1, respectively.  C source code is provided in Appendix A to
  532.    compute these tables.
  533.  
  534.    For Q values 128-255, dynamically defined quantization tables are
  535.    used.  These tables may be specified either in-band or out of band by
  536.    something like a session setup protocol, but the Quantization Table
  537.    header MUST be present in the first packet of every frame. When the
  538.    tables are specified out of band, they may be omitted from the packet
  539.    by setting the Length field in this header to 0.
  540.  
  541.    When the quantization tables are sent in-band, they need not be sent
  542.    with every frame.  Like the out of band case, frames which do not
  543.    contain tables will have a Quantization Table header with a Length
  544.    field of 0.  While this does decrease the overhead of including the
  545.    tables, new receivers will be unable to properly decode frames from
  546.    the time they start up until they receive the tables.
  547.  
  548. 4.3.  Fragmentation and Reassembly
  549.  
  550.    Since JPEG frames can be large, they must often be fragmented.
  551.    Frames SHOULD be fragmented into packets in a manner avoiding
  552.    fragmentation at a lower level.  If support for partial frame
  553.    decoding is desired, frames SHOULD be fragmented such that each
  554.    packet contains an integral number of restart intervals (see below).
  555.  
  556.    Each packet that makes up a single frame MUST have the same
  557.    timestamp, and the RTP marker bit MUST be set on the last packet in a
  558.    frame.  The fragment offset field of each packet is set to the byte
  559.  
  560.  
  561.  
  562. Berc, et. al.               Standards Track                    [Page 10]
  563.  
  564. RFC 2435              RTP Payload Format for JPEG           October 1998
  565.  
  566.  
  567.    offset of its payload data within the original frame.  Packets making
  568.    up a frame SHOULD be sent sequentially and the fragments they contain
  569.    MUST NOT overlap one another.
  570.  
  571.    An entire frame can be identified as a sequence of packets beginning
  572.    with a packet having a zero fragment offset and ending with a packet
  573.    having the RTP marker bit set.  Missing packets can be detected
  574.    either with RTP sequence numbers or with the fragment offset and
  575.    lengths of each packet.  Reassembly could be carried out without the
  576.    offset field (i.e., using only the RTP marker bit and sequence
  577.    numbers), but an efficient single-copy implementation would not
  578.    otherwise be possible in the presence of misordered packets.
  579.    Moreover, if the last packet of the previous frame (containing the
  580.    marker bit) were dropped, then a receiver could not always detect
  581.    that the current frame is entirely intact.
  582.  
  583. 4.4.  Restart Markers
  584.  
  585.    Restart markers indicate a point in the JPEG stream at which the
  586.    Huffman decoder and DC predictors are reset, allowing partial
  587.    decoding starting at that point.  To fully take advantage of this,
  588.    however, a decoder must know which MCUs of a frame a particular
  589.    restart interval encodes.  While the original JPEG specification does
  590.    provide a small sequence number field in the restart markers for this
  591.    purpose, it is not large enough to properly cope with the loss of an
  592.    entire packet's worth of data at a typical network MTU size.  The
  593.    RTP/JPEG Restart Marker header contains the additional information
  594.    needed to accomplish this.
  595.  
  596.    The size of restart intervals SHOULD be chosen to always allow an
  597.    integral number of restart intervals to fit within a single packet.
  598.    This will guarantee that packets can be decoded independently from
  599.    one another.  If a restart interval ends up being larger than a
  600.    packet, the F and L bits in the Restart Marker header can be used to
  601.    fragment it, but the resulting set of packets must all be received by
  602.    a decoder for that restart interval to be decoded properly.
  603.  
  604.    Once a decoder has received either a single packet with both the F
  605.    and L bits set on or a contiguous sequence of packets (based on the
  606.    RTP sequence number) which begin with an F bit and end with an L bit,
  607.    it can begin decoding.  The position of the MCU at the beginning of
  608.    the data can be determined by multiplying the Restart Count value by
  609.    the Restart Interval value.  A packet (or group of packets as
  610.    identified by the F and L bits) may contain any number of consecutive
  611.    restart intervals.
  612.  
  613.    To accommodate encoders which generate frames with restart markers in
  614.    them but cannot fragment the data in this manner, the Restart Count
  615.  
  616.  
  617.  
  618. Berc, et. al.               Standards Track                    [Page 11]
  619.  
  620. RFC 2435              RTP Payload Format for JPEG           October 1998
  621.  
  622.  
  623.    field may be set to 0x3FFF with the F and L bits both set to 1.  This
  624.    indicates to decoders that the entire frame must be reassembled
  625.    before decoding it.
  626.  
  627. 5.  Security Considerations
  628.  
  629.    RTP packets using the payload format defined in this specification
  630.    are subject to the security considerations discussed in the RTP
  631.    specification [6], and any appropriate RTP profile (for example [7]).
  632.    This implies that confidentiality of the media streams is achieved by
  633.    encryption. Because the data compression used with this payload
  634.    format is applied end-to-end, encryption may be performed after
  635.    compression so there is no conflict between the two operations.
  636.  
  637.    A potential denial-of-service threat exists for data encodings using
  638.    compression techniques that have non-uniform receiver-end
  639.    computational load.  The attacker can inject pathological datagrams
  640.    into the stream which are complex to decode and cause the receiver to
  641.    be overloaded.  However, this encoding does not exhibit any
  642.    significant non-uniformity.
  643.  
  644.    Another potential denial-of-service threat exists around the
  645.    fragmentation mechanism presented here.  Receivers should be prepared
  646.    to limit the total amount of data associated with assembling received
  647.    frames so as to avoid resource exhaustion.
  648.  
  649.    As with any IP-based protocol, in some circumstances a receiver may
  650.    be overloaded simply by the receipt of too many packets, either
  651.    desired or undesired.  Network-layer authentication may be used to
  652.    discard packets from undesired sources, but the processing cost of
  653.    the authentication itself may be too high.  In a multicast
  654.    environment, pruning of specific sources will be implemented in a
  655.    future version of IGMP [8] and in multicast routing protocols to
  656.    allow a receiver to select which sources are allowed to reach it.
  657.  
  658.    A security review of this payload format found no additional
  659.    considerations beyond those in the RTP specification.
  660.  
  661.  
  662.  
  663.  
  664.  
  665.  
  666.  
  667.  
  668.  
  669.  
  670.  
  671.  
  672.  
  673.  
  674. Berc, et. al.               Standards Track                    [Page 12]
  675.  
  676. RFC 2435              RTP Payload Format for JPEG           October 1998
  677.  
  678.  
  679. 6.  Authors' Addresses
  680.  
  681.    Lance M. Berc
  682.    Systems Research Center
  683.    Digital Equipment Corporation
  684.    130 Lytton Ave
  685.    Palo Alto CA 94301
  686.  
  687.    Phone: +1 650 853 2100
  688.    EMail: berc@pa.dec.com
  689.  
  690.  
  691.    William C. Fenner
  692.    Xerox PARC
  693.    3333 Coyote Hill Road
  694.    Palo Alto, CA 94304
  695.  
  696.    Phone: +1 650 812 4816
  697.    EMail: fenner@parc.xerox.com
  698.  
  699.  
  700.    Ron Frederick
  701.    Xerox PARC
  702.    3333 Coyote Hill Road
  703.    Palo Alto, CA 94304
  704.  
  705.    Phone: +1 650 812 4459
  706.    EMail: frederick@parc.xerox.com
  707.  
  708.  
  709.    Steven McCanne
  710.    University of California at Berkeley
  711.    Electrical Engineering and Computer Science
  712.    633 Soda Hall
  713.    Berkeley, CA 94720
  714.  
  715.    Phone: +1 510 642 0865
  716.    EMail: mccanne@cs.berkeley.edu
  717.  
  718.  
  719.    Paul Stewart
  720.    Xerox PARC
  721.    3333 Coyote Hill Road
  722.    Palo Alto, CA 94304
  723.  
  724.    Phone: +1 650 812 4821
  725.    EMail: stewart@parc.xerox.com
  726.  
  727.  
  728.  
  729.  
  730. Berc, et. al.               Standards Track                    [Page 13]
  731.  
  732. RFC 2435              RTP Payload Format for JPEG           October 1998
  733.  
  734.  
  735. 7.  References
  736.  
  737.  
  738.    [1]  ISO DIS 10918-1. Digital Compression and Coding of Continuous-
  739.         tone Still Images (JPEG), CCITT Recommendation T.81.
  740.  
  741.    [2]  William B. Pennebaker, Joan L. Mitchell, JPEG: Still Image Data
  742.         Compression Standard, Van Nostrand Reinhold, 1993.
  743.  
  744.    [3]  Gregory K. Wallace, The JPEG Sill Picture Compression Standard,
  745.         Communications of the ACM, April 1991, Vol 34, No. 1, pp. 31-44.
  746.  
  747.    [4]  The JPEG File Interchange Format.  Maintained by C-Cube
  748.         Microsystems, Inc., and available in
  749.         ftp://ftp.uu.net/graphics/jpeg/jfif.ps.gz.
  750.  
  751.    [5]  Tom Lane et. al., The Independent JPEG Group software JPEG
  752.         codec.  Source code available in
  753.         ftp://ftp.uu.net/graphics/jpeg/jpegsrc.v6a.tar.gz.
  754.  
  755.    [6]  Schulzrinne, H., Casner, S., Frederick, R. and V. Jacobson,
  756.         "RTP: A Transport Protocol for Real-Time Applications", RFC
  757.         1889, January 1996.
  758.  
  759.    [7]  Schulzrinne, H., "RTP Profile for Audio and Video Conferences
  760.         with Minimal Control", RFC 1890, January 1996.
  761.  
  762.    [8]  Fenner, W., "Internet Group Management Protocol Version 2", RFC
  763.         2236, November 1997.
  764.  
  765.    [9]  Bradner, S., "Key words for use in RFCs to Indicate Requirement
  766.         Levels", BCP 14, RFC 2119, March 1997.
  767.  
  768.    [10] Kent C., and J. Mogul, "Fragmentation Considered Harmful",
  769.         Proceedings of the ACM SIGCOMM '87 Workshop on Frontiers in
  770.         Computer Communications Technology, August 1987.
  771.  
  772.  
  773.  
  774.  
  775.  
  776.  
  777.  
  778.  
  779.  
  780.  
  781.  
  782.  
  783.  
  784.  
  785.  
  786. Berc, et. al.               Standards Track                    [Page 14]
  787.  
  788. RFC 2435              RTP Payload Format for JPEG           October 1998
  789.  
  790.  
  791. Appendix A
  792.  
  793.    The following code can be used to create a quantization table from a
  794.    Q factor:
  795.  
  796. /*
  797.  * Table K.1 from JPEG spec.
  798.  */
  799. static const int jpeg_luma_quantizer[64] = {
  800.         16, 11, 10, 16, 24, 40, 51, 61,
  801.         12, 12, 14, 19, 26, 58, 60, 55,
  802.         14, 13, 16, 24, 40, 57, 69, 56,
  803.         14, 17, 22, 29, 51, 87, 80, 62,
  804.         18, 22, 37, 56, 68, 109, 103, 77,
  805.         24, 35, 55, 64, 81, 104, 113, 92,
  806.         49, 64, 78, 87, 103, 121, 120, 101,
  807.         72, 92, 95, 98, 112, 100, 103, 99
  808. };
  809.  
  810. /*
  811.  * Table K.2 from JPEG spec.
  812.  */
  813. static const int jpeg_chroma_quantizer[64] = {
  814.         17, 18, 24, 47, 99, 99, 99, 99,
  815.         18, 21, 26, 66, 99, 99, 99, 99,
  816.         24, 26, 56, 99, 99, 99, 99, 99,
  817.         47, 66, 99, 99, 99, 99, 99, 99,
  818.         99, 99, 99, 99, 99, 99, 99, 99,
  819.         99, 99, 99, 99, 99, 99, 99, 99,
  820.         99, 99, 99, 99, 99, 99, 99, 99,
  821.         99, 99, 99, 99, 99, 99, 99, 99
  822. };
  823.  
  824. /*
  825.  * Call MakeTables with the Q factor and two u_char[64] return arrays
  826.  */
  827. void
  828. MakeTables(int q, u_char *lqt, u_char *cqt)
  829. {
  830.   int i;
  831.   int factor = q;
  832.  
  833.   if (q < 1) factor = 1;
  834.   if (q > 99) factor = 99;
  835.   if (q < 50)
  836.     q = 5000 / factor;
  837.   else
  838.     q = 200 - factor*2;
  839.  
  840.  
  841.  
  842. Berc, et. al.               Standards Track                    [Page 15]
  843.  
  844. RFC 2435              RTP Payload Format for JPEG           October 1998
  845.  
  846.  
  847.   for (i=0; i < 64; i++) {
  848.     int lq = (jpeg_luma_quantizer[i] * q + 50) / 100;
  849.     int cq = (jpeg_chroma_quantizer[i] * q + 50) / 100;
  850.  
  851.     /* Limit the quantizers to 1 <= q <= 255 */
  852.     if (lq < 1) lq = 1;
  853.     else if (lq > 255) lq = 255;
  854.     lqt[i] = lq;
  855.  
  856.     if (cq < 1) cq = 1;
  857.     else if (cq > 255) cq = 255;
  858.     cqt[i] = cq;
  859.   }
  860. }
  861.  
  862.  
  863.  
  864.  
  865.  
  866.  
  867.  
  868.  
  869.  
  870.  
  871.  
  872.  
  873.  
  874.  
  875.  
  876.  
  877.  
  878.  
  879.  
  880.  
  881.  
  882.  
  883.  
  884.  
  885.  
  886.  
  887.  
  888.  
  889.  
  890.  
  891.  
  892.  
  893.  
  894.  
  895.  
  896.  
  897.  
  898. Berc, et. al.               Standards Track                    [Page 16]
  899.  
  900. RFC 2435              RTP Payload Format for JPEG           October 1998
  901.  
  902.  
  903. Appendix B
  904.  
  905.    The following routines can be used to create the JPEG marker segments
  906.    corresponding to the table-specification data that is absent from the
  907.    RTP/JPEG body.
  908.  
  909. u_char lum_dc_codelens[] = {
  910.         0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0,
  911. };
  912.  
  913. u_char lum_dc_symbols[] = {
  914.         0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
  915. };
  916.  
  917. u_char lum_ac_codelens[] = {
  918.         0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 0x7d,
  919. };
  920.  
  921. u_char lum_ac_symbols[] = {
  922.         0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12,
  923.         0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07,
  924.         0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08,
  925.         0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0,
  926.         0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16,
  927.         0x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28,
  928.         0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
  929.         0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,
  930.         0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
  931.         0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
  932.         0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79,
  933.         0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
  934.         0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
  935.         0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
  936.         0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6,
  937.         0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5,
  938.         0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4,
  939.         0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2,
  940.         0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea,
  941.         0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
  942.         0xf9, 0xfa,
  943. };
  944.  
  945. u_char chm_dc_codelens[] = {
  946.         0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,
  947. };
  948.  
  949. u_char chm_dc_symbols[] = {
  950.         0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
  951.  
  952.  
  953.  
  954. Berc, et. al.               Standards Track                    [Page 17]
  955.  
  956. RFC 2435              RTP Payload Format for JPEG           October 1998
  957.  
  958.  
  959. };
  960.  
  961. u_char chm_ac_codelens[] = {
  962.         0, 2, 1, 2, 4, 4, 3, 4, 7, 5, 4, 4, 0, 1, 2, 0x77,
  963. };
  964.  
  965. u_char chm_ac_symbols[] = {
  966.         0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21,
  967.         0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71,
  968.         0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91,
  969.         0xa1, 0xb1, 0xc1, 0x09, 0x23, 0x33, 0x52, 0xf0,
  970.         0x15, 0x62, 0x72, 0xd1, 0x0a, 0x16, 0x24, 0x34,
  971.         0xe1, 0x25, 0xf1, 0x17, 0x18, 0x19, 0x1a, 0x26,
  972.         0x27, 0x28, 0x29, 0x2a, 0x35, 0x36, 0x37, 0x38,
  973.         0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
  974.         0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
  975.         0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
  976.         0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
  977.         0x79, 0x7a, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
  978.         0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96,
  979.         0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5,
  980.         0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4,
  981.         0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3,
  982.         0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2,
  983.         0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda,
  984.         0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9,
  985.         0xea, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
  986.         0xf9, 0xfa,
  987. };
  988.  
  989. u_char *
  990. MakeQuantHeader(u_char *p, u_char *qt, int tableNo)
  991. {
  992.         *p++ = 0xff;
  993.         *p++ = 0xdb;            /* DQT */
  994.         *p++ = 0;               /* length msb */
  995.         *p++ = 67;              /* length lsb */
  996.         *p++ = tableNo;
  997.         memcpy(p, qt, 64);
  998.         return (p + 64);
  999. }
  1000.  
  1001. u_char *
  1002. MakeHuffmanHeader(u_char *p, u_char *codelens, int ncodes,
  1003.                   u_char *symbols, int nsymbols, int tableNo,
  1004.                   int tableClass)
  1005. {
  1006.         *p++ = 0xff;
  1007.  
  1008.  
  1009.  
  1010. Berc, et. al.               Standards Track                    [Page 18]
  1011.  
  1012. RFC 2435              RTP Payload Format for JPEG           October 1998
  1013.  
  1014.  
  1015.         *p++ = 0xc4;            /* DHT */
  1016.         *p++ = 0;               /* length msb */
  1017.         *p++ = 3 + ncodes + nsymbols; /* length lsb */
  1018.         *p++ = (tableClass << 4) | tableNo;
  1019.         memcpy(p, codelens, ncodes);
  1020.         p += ncodes;
  1021.         memcpy(p, symbols, nsymbols);
  1022.         p += nsymbols;
  1023.         return (p);
  1024. }
  1025.  
  1026. u_char *
  1027. MakeDRIHeader(u_char *p, u_short dri) {
  1028.         *p++ = 0xff;
  1029.         *p++ = 0xdd;            /* DRI */
  1030.         *p++ = 0x0;             /* length msb */
  1031.         *p++ = 4;               /* length lsb */
  1032.         *p++ = dri >> 8;        /* dri msb */
  1033.         *p++ = dri & 0xff;      /* dri lsb */
  1034.         return (p);
  1035. }
  1036.  
  1037. /*
  1038.  *  Arguments:
  1039.  *    type, width, height: as supplied in RTP/JPEG header
  1040.  *    lqt, cqt: quantization tables as either derived from
  1041.  *         the Q field using MakeTables() or as specified
  1042.  *         in section 4.2.
  1043.  *    dri: restart interval in MCUs, or 0 if no restarts.
  1044.  *
  1045.  *    p: pointer to return area
  1046.  *
  1047.  *  Return value:
  1048.  *    The length of the generated headers.
  1049.  *
  1050.  *    Generate a frame and scan headers that can be prepended to the
  1051.  *    RTP/JPEG data payload to produce a JPEG compressed image in
  1052.  *    interchange format (except for possible trailing garbage and
  1053.  *    absence of an EOI marker to terminate the scan).
  1054.  */
  1055. int MakeHeaders(u_char *p, int type, int w, int h, u_char *lqt,
  1056.                 u_char *cqt, u_short dri)
  1057. {
  1058.         u_char *start = p;
  1059.  
  1060.         /* convert from blocks to pixels */
  1061.         w <<= 3;
  1062.         h <<= 3;
  1063.  
  1064.  
  1065.  
  1066. Berc, et. al.               Standards Track                    [Page 19]
  1067.  
  1068. RFC 2435              RTP Payload Format for JPEG           October 1998
  1069.  
  1070.  
  1071.         *p++ = 0xff;
  1072.         *p++ = 0xd8;            /* SOI */
  1073.  
  1074.         p = MakeQuantHeader(p, lqt, 0);
  1075.         p = MakeQuantHeader(p, cqt, 1);
  1076.  
  1077.         if (dri != 0)
  1078.                 p = MakeDRIHeader(p, dri);
  1079.  
  1080.         *p++ = 0xff;
  1081.         *p++ = 0xc0;            /* SOF */
  1082.         *p++ = 0;               /* length msb */
  1083.         *p++ = 17;              /* length lsb */
  1084.         *p++ = 8;               /* 8-bit precision */
  1085.         *p++ = h >> 8;          /* height msb */
  1086.         *p++ = h;               /* height lsb */
  1087.         *p++ = w >> 8;          /* width msb */
  1088.         *p++ = w;               /* wudth lsb */
  1089.         *p++ = 3;               /* number of components */
  1090.         *p++ = 0;               /* comp 0 */
  1091.         if (type == 0)
  1092.                 *p++ = 0x21;    /* hsamp = 2, vsamp = 1 */
  1093.         else
  1094.                 *p++ = 0x22;    /* hsamp = 2, vsamp = 2 */
  1095.         *p++ = 0;               /* quant table 0 */
  1096.         *p++ = 1;               /* comp 1 */
  1097.         *p++ = 0x11;            /* hsamp = 1, vsamp = 1 */
  1098.         *p++ = 1;               /* quant table 1 */
  1099.         *p++ = 2;               /* comp 2 */
  1100.         *p++ = 0x11;            /* hsamp = 1, vsamp = 1 */
  1101.         *p++ = 1;               /* quant table 1 */
  1102.         p = MakeHuffmanHeader(p, lum_dc_codelens,
  1103.                               sizeof(lum_dc_codelens),
  1104.                               lum_dc_symbols,
  1105.                               sizeof(lum_dc_symbols), 0, 0);
  1106.         p = MakeHuffmanHeader(p, lum_ac_codelens,
  1107.                               sizeof(lum_ac_codelens),
  1108.                               lum_ac_symbols,
  1109.                               sizeof(lum_ac_symbols), 0, 1);
  1110.         p = MakeHuffmanHeader(p, chm_dc_codelens,
  1111.                               sizeof(chm_dc_codelens),
  1112.                               chm_dc_symbols,
  1113.                               sizeof(chm_dc_symbols), 1, 0);
  1114.         p = MakeHuffmanHeader(p, chm_ac_codelens,
  1115.                               sizeof(chm_ac_codelens),
  1116.                               chm_ac_symbols,
  1117.                               sizeof(chm_ac_symbols), 1, 1);
  1118.  
  1119.  
  1120.  
  1121.  
  1122. Berc, et. al.               Standards Track                    [Page 20]
  1123.  
  1124. RFC 2435              RTP Payload Format for JPEG           October 1998
  1125.  
  1126.  
  1127.         *p++ = 0xff;
  1128.         *p++ = 0xda;            /* SOS */
  1129.         *p++ = 0;               /* length msb */
  1130.         *p++ = 12;              /* length lsb */
  1131.         *p++ = 3;               /* 3 components */
  1132.         *p++ = 0;               /* comp 0 */
  1133.         *p++ = 0;               /* huffman table 0 */
  1134.         *p++ = 1;               /* comp 1 */
  1135.         *p++ = 0x11;            /* huffman table 1 */
  1136.         *p++ = 2;               /* comp 2 */
  1137.         *p++ = 0x11;            /* huffman table 1 */
  1138.         *p++ = 0;               /* first DCT coeff */
  1139.         *p++ = 63;              /* last DCT coeff */
  1140.         *p++ = 0;               /* sucessive approx. */
  1141.  
  1142.         return (p - start);
  1143. };
  1144.  
  1145.  
  1146.  
  1147.  
  1148.  
  1149.  
  1150.  
  1151.  
  1152.  
  1153.  
  1154.  
  1155.  
  1156.  
  1157.  
  1158.  
  1159.  
  1160.  
  1161.  
  1162.  
  1163.  
  1164.  
  1165.  
  1166.  
  1167.  
  1168.  
  1169.  
  1170.  
  1171.  
  1172.  
  1173.  
  1174.  
  1175.  
  1176.  
  1177.  
  1178. Berc, et. al.               Standards Track                    [Page 21]
  1179.  
  1180. RFC 2435              RTP Payload Format for JPEG           October 1998
  1181.  
  1182.  
  1183. Appendix C
  1184.  
  1185.    The following routine is used to illustrate the RTP/JPEG packet
  1186.    fragmentation and header creation.
  1187.  
  1188.    For clarity and brevity, the structure definitions are only valid for
  1189.    32-bit big-endian (most significant octet first) architectures. Bit
  1190.    fields are assumed to be packed tightly in big-endian bit order, with
  1191.    no additional padding. Modifications would be required to construct a
  1192.    portable implementation.
  1193.  
  1194. /*
  1195.  * RTP data header from RFC1889
  1196.  */
  1197. typedef struct {
  1198.         unsigned int version:2;   /* protocol version */
  1199.         unsigned int p:1;         /* padding flag */
  1200.         unsigned int x:1;         /* header extension flag */
  1201.         unsigned int cc:4;        /* CSRC count */
  1202.         unsigned int m:1;         /* marker bit */
  1203.         unsigned int pt:7;        /* payload type */
  1204.         u_int16 seq;              /* sequence number */
  1205.         u_int32 ts;               /* timestamp */
  1206.         u_int32 ssrc;             /* synchronization source */
  1207.         u_int32 csrc[1];          /* optional CSRC list */
  1208. } rtp_hdr_t;
  1209.  
  1210. #define RTP_HDR_SZ 12
  1211.  
  1212. /* The following definition is from RFC1890 */
  1213. #define RTP_PT_JPEG             26
  1214.  
  1215. struct jpeghdr {
  1216.         unsigned int tspec:8;   /* type-specific field */
  1217.         unsigned int off:24;    /* fragment byte offset */
  1218.         u_int8 type;            /* id of jpeg decoder params */
  1219.         u_int8 q;               /* quantization factor (or table id) */
  1220.         u_int8 width;           /* frame width in 8 pixel blocks */
  1221.         u_int8 height;          /* frame height in 8 pixel blocks */
  1222. };
  1223.  
  1224. struct jpeghdr_rst {
  1225.         u_int16 dri;
  1226.         unsigned int f:1;
  1227.         unsigned int l:1;
  1228.         unsigned int count:14;
  1229. };
  1230.  
  1231.  
  1232.  
  1233.  
  1234. Berc, et. al.               Standards Track                    [Page 22]
  1235.  
  1236. RFC 2435              RTP Payload Format for JPEG           October 1998
  1237.  
  1238.  
  1239. struct jpeghdr_qtable {
  1240.         u_int8  mbz;
  1241.         u_int8  precision;
  1242.         u_int16 length;
  1243. };
  1244.  
  1245. #define RTP_JPEG_RESTART           0x40
  1246.  
  1247. /* Procedure SendFrame:
  1248.  *
  1249.  *  Arguments:
  1250.  *    start_seq: The sequence number for the first packet of the current
  1251.  *               frame.
  1252.  *    ts: RTP timestamp for the current frame
  1253.  *    ssrc: RTP SSRC value
  1254.  *    jpeg_data: Huffman encoded JPEG scan data
  1255.  *    len: Length of the JPEG scan data
  1256.  *    type: The value the RTP/JPEG type field should be set to
  1257.  *    typespec: The value the RTP/JPEG type-specific field should be set
  1258.  *              to
  1259.  *    width: The width in pixels of the JPEG image
  1260.  *    height: The height in pixels of the JPEG image
  1261.  *    dri: The number of MCUs between restart markers (or 0 if there
  1262.  *         are no restart markers in the data
  1263.  *    q: The Q factor of the data, to be specified using the Independent
  1264.  *       JPEG group's algorithm if 1 <= q <= 99, specified explicitly
  1265.  *       with lqt and cqt if q >= 128, or undefined otherwise.
  1266.  *    lqt: The quantization table for the luminance channel if q >= 128
  1267.  *    cqt: The quantization table for the chrominance channels if
  1268.  *         q >= 128
  1269.  *
  1270.  *  Return value:
  1271.  *    the sequence number to be sent for the first packet of the next
  1272.  *    frame.
  1273.  *
  1274.  * The following are assumed to be defined:
  1275.  *
  1276.  * PACKET_SIZE                         - The size of the outgoing packet
  1277.  * send_packet(u_int8 *data, int len)  - Sends the packet to the network
  1278.  */
  1279.  
  1280. u_int16 SendFrame(u_int16 start_seq, u_int32 ts, u_int32 ssrc,
  1281.                    u_int8 *jpeg_data, int len, u_int8 type,
  1282.                    u_int8 typespec, int width, int height, int dri,
  1283.                    u_int8 q, u_int8 *lqt, u_int8 *cqt) {
  1284.         rtp_hdr_t rtphdr;
  1285.         struct jpeghdr jpghdr;
  1286.         struct jpeghdr_rst rsthdr;
  1287.  
  1288.  
  1289.  
  1290. Berc, et. al.               Standards Track                    [Page 23]
  1291.  
  1292. RFC 2435              RTP Payload Format for JPEG           October 1998
  1293.  
  1294.  
  1295.         struct jpeghdr_qtable qtblhdr;
  1296.         u_int8 packet_buf[PACKET_SIZE];
  1297.         u_int8 *ptr;
  1298.         int bytes_left = len;
  1299.         int seq = start_seq;
  1300.         int pkt_len, data_len;
  1301.  
  1302.         /* Initialize RTP header
  1303.          */
  1304.         rtphdr.version = 2;
  1305.         rtphdr.p = 0;
  1306.         rtphdr.x = 0;
  1307.         rtphdr.cc = 0;
  1308.         rtphdr.m = 0;
  1309.         rtphdr.pt = RTP_PT_JPEG;
  1310.         rtphdr.seq = start_seq;
  1311.         rtphdr.ts = ts;
  1312.         rtphdr.ssrc = ssrc;
  1313.  
  1314.         /* Initialize JPEG header
  1315.          */
  1316.         jpghdr.tspec = typespec;
  1317.         jpghdr.off = 0;
  1318.         jpghdr.type = type | ((dri != 0) ? RTP_JPEG_RESTART : 0);
  1319.         jpghdr.q = q;
  1320.         jpghdr.width = width / 8;
  1321.         jpghdr.height = height / 8;
  1322.  
  1323.         /* Initialize DRI header
  1324.          */
  1325.         if (dri != 0) {
  1326.                 rsthdr.dri = dri;
  1327.                 rsthdr.f = 1;        /* This code does not align RIs */
  1328.                 rsthdr.l = 1;
  1329.                 rsthdr.count = 0x3fff;
  1330.         }
  1331.  
  1332.         /* Initialize quantization table header
  1333.          */
  1334.         if (q >= 128) {
  1335.                 qtblhdr.mbz = 0;
  1336.                 qtblhdr.precision = 0; /* This code uses 8 bit tables only */
  1337.                 qtblhdr.length = 128;  /* 2 64-byte tables */
  1338.         }
  1339.  
  1340.         while (bytes_left > 0) {
  1341.                 ptr = packet_buf + RTP_HDR_SZ;
  1342.                 memcpy(ptr, &jpghdr, sizeof(jpghdr));
  1343.  
  1344.  
  1345.  
  1346. Berc, et. al.               Standards Track                    [Page 24]
  1347.  
  1348. RFC 2435              RTP Payload Format for JPEG           October 1998
  1349.  
  1350.  
  1351.                 ptr += sizeof(jpghdr);
  1352.  
  1353.                 if (dri != 0) {
  1354.                         memcpy(ptr, &rsthdr, sizeof(rsthdr));
  1355.                         ptr += sizeof(rsthdr);
  1356.                 }
  1357.  
  1358.                 if (q >= 128 && jpghdr.off == 0) {
  1359.                         memcpy(ptr, &qtblhdr, sizeof(qtblhdr));
  1360.                         ptr += sizeof(qtblhdr);
  1361.                         memcpy(ptr, lqt, 64);
  1362.                         ptr += 64;
  1363.                         memcpy(ptr, cqt, 64);
  1364.                         ptr += 64;
  1365.                 }
  1366.  
  1367.                 data_len = PACKET_SIZE - (ptr - packet_buf);
  1368.                 if (data_len >= bytes_left) {
  1369.                         data_len = bytes_left;
  1370.                         rtphdr.m = 1;
  1371.                 }
  1372.  
  1373.                 memcpy(packet_buf, &rtphdr, RTP_HDR_SZ);
  1374.                 memcpy(ptr, jpeg_data + jpghdr.off, data_len);
  1375.  
  1376.                 send_packet(packet_buf, (ptr - packet_buf) + data_len);
  1377.  
  1378.                 jpghdr.off += data_len;
  1379.                 bytes_left -= data_len;
  1380.                 rtphdr.seq++;
  1381.         }
  1382.         return rtphdr.seq;
  1383. }
  1384.  
  1385.  
  1386.  
  1387.  
  1388.  
  1389.  
  1390.  
  1391.  
  1392.  
  1393.  
  1394.  
  1395.  
  1396.  
  1397.  
  1398.  
  1399.  
  1400.  
  1401.  
  1402. Berc, et. al.               Standards Track                    [Page 25]
  1403.  
  1404. RFC 2435              RTP Payload Format for JPEG           October 1998
  1405.  
  1406.  
  1407. Appendix D
  1408.  
  1409.    This section outlines the changes between this document and its
  1410.    precdecessor, RFC 2035.  The changes to the protocol were made with
  1411.    an eye towards causing as few interoperability problems between
  1412.    implementations based on the older text and newer implementations,
  1413.    and indeed, many of the obsolete conventions can still be
  1414.    unambiguously decoded by a newer implementation.  However, use of the
  1415.    older conventions in newer implementations is strongly discouraged.
  1416.  
  1417.     o   Types 0 and 1 have been augmented to allow for the encoding of
  1418.         interlaced video images, using 2 bits of the type-specific
  1419.         field.  See section 4.1 for details.
  1420.  
  1421.     o   There has been discussion in the working group arguing for more
  1422.         flexibility in specifying the JPEG quantization tables.  This
  1423.         memo allows table coefficients to be specified explicitly
  1424.         through the use of an optional Quantization Table header,
  1425.         discussed in sections 3.1.8 and 4.2.
  1426.  
  1427.     o   In RFC 2035, the encoding of restart marker information in the
  1428.         Type field made it difficult to add new types. Additionally, the
  1429.         type- specific field was used for the restart count, making it
  1430.         unavailable for other type-specific purposes.  This memo moves
  1431.         the restart marker indication to a particular bit in the Type
  1432.         field, and adds an optional header to hold the additional
  1433.         information required, leaving the type-specific field free for
  1434.         its intended purpose.  The handling of partial frame decoding
  1435.         was also made more robust against packet loss.  See sections
  1436.         3.1.7 and 4.4 for details.
  1437.  
  1438.  
  1439.  
  1440.  
  1441.  
  1442.  
  1443.  
  1444.  
  1445.  
  1446.  
  1447.  
  1448.  
  1449.  
  1450.  
  1451.  
  1452.  
  1453.  
  1454.  
  1455.  
  1456.  
  1457.  
  1458. Berc, et. al.               Standards Track                    [Page 26]
  1459.  
  1460. RFC 2435              RTP Payload Format for JPEG           October 1998
  1461.  
  1462.  
  1463. Full Copyright Statement
  1464.  
  1465.    Copyright (C) The Internet Society (1998).  All Rights Reserved.
  1466.  
  1467.    This document and translations of it may be copied and furnished to
  1468.    others, and derivative works that comment on or otherwise explain it
  1469.    or assist in its implementation may be prepared, copied, published
  1470.    and distributed, in whole or in part, without restriction of any
  1471.    kind, provided that the above copyright notice and this paragraph are
  1472.    included on all such copies and derivative works.  However, this
  1473.    document itself may not be modified in any way, such as by removing
  1474.    the copyright notice or references to the Internet Society or other
  1475.    Internet organizations, except as needed for the purpose of
  1476.    developing Internet standards in which case the procedures for
  1477.    copyrights defined in the Internet Standards process must be
  1478.    followed, or as required to translate it into languages other than
  1479.    English.
  1480.  
  1481.    The limited permissions granted above are perpetual and will not be
  1482.    revoked by the Internet Society or its successors or assigns.
  1483.  
  1484.    This document and the information contained herein is provided on an
  1485.    "AS IS" basis and THE INTERNET SOCIETY AND THE INTERNET ENGINEERING
  1486.    TASK FORCE DISCLAIMS ALL WARRANTIES, EXPRESS OR IMPLIED, INCLUDING
  1487.    BUT NOT LIMITED TO ANY WARRANTY THAT THE USE OF THE INFORMATION
  1488.    HEREIN WILL NOT INFRINGE ANY RIGHTS OR ANY IMPLIED WARRANTIES OF
  1489.    MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  1490.  
  1491.  
  1492.  
  1493.  
  1494.  
  1495.  
  1496.  
  1497.  
  1498.  
  1499.  
  1500.  
  1501.  
  1502.  
  1503.  
  1504.  
  1505.  
  1506.  
  1507.  
  1508.  
  1509.  
  1510.  
  1511.  
  1512.  
  1513.  
  1514. Berc, et. al.               Standards Track                    [Page 27]
  1515.  
  1516.