source: 3DVCSoftware/branches/HTM-5.0-Qualcomm-Fix/source/Lib/TLibDecoder/AnnexBread.cpp @ 222

Last change on this file since 222 was 56, checked in by hschwarz, 13 years ago

updated trunk (move to HM6.1)

File size: 6.9 KB
Line 
1/* The copyright in this software is being made available under the BSD
2 * License, included below. This software may be subject to other third party
3 * and contributor rights, including patent rights, and no such rights are
4 * granted under this license.
5 *
6 * Copyright (c) 2010-2012, ITU/ISO/IEC
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are met:
11 *
12 *  * Redistributions of source code must retain the above copyright notice,
13 *    this list of conditions and the following disclaimer.
14 *  * Redistributions in binary form must reproduce the above copyright notice,
15 *    this list of conditions and the following disclaimer in the documentation
16 *    and/or other materials provided with the distribution.
17 *  * Neither the name of the ITU/ISO/IEC nor the names of its contributors may
18 *    be used to endorse or promote products derived from this software without
19 *    specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
31 * THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34#include <stdint.h>
35#include <cassert>
36#include <vector>
37#include "AnnexBread.h"
38
39using namespace std;
40
41//! \ingroup TLibDecoder
42//! \{
43
44/**
45 * Parse an AVC AnnexB Bytestream bs to extract a single nalUnit
46 * while accumulating bytestream statistics into stats.
47 *
48 * If EOF occurs while trying to extract a NALunit, an exception
49 * of std::ios_base::failure is thrown.  The contsnts of stats will
50 * be correct at this point.
51 */
52static void
53_byteStreamNALUnit(
54  InputByteStream& bs,
55  vector<uint8_t>& nalUnit,
56  AnnexBStats& stats)
57{
58  /* At the beginning of the decoding process, the decoder initialises its
59   * current position in the byte stream to the beginning of the byte stream.
60   * It then extracts and discards each leading_zero_8bits syntax element (if
61   * present), moving the current position in the byte stream forward one
62   * byte at a time, until the current position in the byte stream is such
63   * that the next four bytes in the bitstream form the four-byte sequence
64   * 0x00000001.
65   */
66  while ((bs.eofBeforeNBytes(24/8) || bs.peekBytes(24/8) != 0x000001)
67  &&     (bs.eofBeforeNBytes(32/8) || bs.peekBytes(32/8) != 0x00000001))
68  {
69    uint8_t leading_zero_8bits = bs.readByte();
70    assert(leading_zero_8bits == 0);
71    stats.m_numLeadingZero8BitsBytes++;
72  }
73
74  /* 1. When the next four bytes in the bitstream form the four-byte sequence
75   * 0x00000001, the next byte in the byte stream (which is a zero_byte
76   * syntax element) is extracted and discarded and the current position in
77   * the byte stream is set equal to the position of the byte following this
78   * discarded byte.
79   */
80  /* NB, the previous step guarantees this will succeed -- if EOF was
81   * encountered, an exception will stop execution getting this far */
82  if (bs.peekBytes(24/8) != 0x000001)
83  {
84    uint8_t zero_byte = bs.readByte();
85    assert(zero_byte == 0);
86    stats.m_numZeroByteBytes++;
87  }
88
89  /* 2. The next three-byte sequence in the byte stream (which is a
90   * start_code_prefix_one_3bytes) is extracted and discarded and the current
91   * position in the byte stream is set equal to the position of the byte
92   * following this three-byte sequence.
93   */
94  /* NB, (1) guarantees that the next three bytes are 0x00 00 01 */
95  uint32_t start_code_prefix_one_3bytes = bs.readBytes(24/8);
96  assert(start_code_prefix_one_3bytes == 0x000001);
97  stats.m_numStartCodePrefixBytes += 3;
98
99  /* 3. NumBytesInNALunit is set equal to the number of bytes starting with
100   * the byte at the current position in the byte stream up to and including
101   * the last byte that precedes the location of any of the following
102   * conditions:
103   *   a. A subsequent byte-aligned three-byte sequence equal to 0x000000, or
104   *   b. A subsequent byte-aligned three-byte sequence equal to 0x000001, or
105   *   c. The end of the byte stream, as determined by unspecified means.
106   */
107  /* 4. NumBytesInNALunit bytes are removed from the bitstream and the
108   * current position in the byte stream is advanced by NumBytesInNALunit
109   * bytes. This sequence of bytes is nal_unit( NumBytesInNALunit ) and is
110   * decoded using the NAL unit decoding process
111   */
112  /* NB, (unsigned)x > 2 implies n!=0 && n!=1 */
113  while (bs.eofBeforeNBytes(24/8) || bs.peekBytes(24/8) > 1) // since 0x000002 tile marker may exist in the bitstream
114  {
115    nalUnit.push_back(bs.readByte());
116  }
117 
118  /* 5. When the current position in the byte stream is:
119   *  - not at the end of the byte stream (as determined by unspecified means)
120   *  - and the next bytes in the byte stream do not start with a three-byte
121   *    sequence equal to 0x000001
122   *  - and the next bytes in the byte stream do not start with a four byte
123   *    sequence equal to 0x00000001,
124   * the decoder extracts and discards each trailing_zero_8bits syntax
125   * element, moving the current position in the byte stream forward one byte
126   * at a time, until the current position in the byte stream is such that:
127   *  - the next bytes in the byte stream form the four-byte sequence
128   *    0x00000001 or
129   *  - the end of the byte stream has been encountered (as determined by
130   *    unspecified means).
131   */
132  /* NB, (3) guarantees there are at least three bytes available or none */
133  while ((bs.eofBeforeNBytes(24/8) || bs.peekBytes(24/8) != 0x000001)
134  &&     (bs.eofBeforeNBytes(32/8) || bs.peekBytes(32/8) != 0x00000001))
135  {
136    uint8_t trailing_zero_8bits = bs.readByte();
137    assert(trailing_zero_8bits == 0);
138    stats.m_numTrailingZero8BitsBytes++;
139  }
140}
141
142/**
143 * Parse an AVC AnnexB Bytestream bs to extract a single nalUnit
144 * while accumulating bytestream statistics into stats.
145 *
146 * Returns false if EOF was reached (NB, nalunit data may be valid),
147 *         otherwise true.
148 */
149bool
150byteStreamNALUnit(
151  InputByteStream& bs,
152  vector<uint8_t>& nalUnit,
153  AnnexBStats& stats)
154{
155  bool eof = false;
156  try
157  {
158    _byteStreamNALUnit(bs, nalUnit, stats);
159  }
160  catch (...)
161  {
162    eof = true;
163  }
164  stats.m_numBytesInNALUnit = unsigned(nalUnit.size());
165  return eof;
166}
167//! \}
Note: See TracBrowser for help on using the repository browser.