MythTV  master
bytereader.cpp
Go to the documentation of this file.
1 /*
2  * find_start_code from libavcodec/FFmpeg
3  * Copyright (c) 2022 Scott Theisen
4  *
5  * This file is part of MythTV. The functions have been adapted to use C++.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 #include "bytereader.h"
23 
24 extern "C" {
25 #include "libavutil/intreadwrite.h"
26 #ifndef AV_RB32
27 # define AV_RB32(x) \
28  (((uint32_t)((const uint8_t*)(x))[0] << 24) | \
29  (((const uint8_t*)(x))[1] << 16) | \
30  (((const uint8_t*)(x))[2] << 8) | \
31  ((const uint8_t*)(x))[3])
32 #endif
33 }
34 
35 const uint8_t* ByteReader::find_start_code(const uint8_t * p,
36  const uint8_t * const end,
37  uint32_t * const start_code)
38 {
39  // minimum length for a start code
40  if (p + 4 > end)
41  {
42  *start_code = ~0; // set to an invalid start code
43  return end;
44  }
45 
46  p += 3; // offset for negative indices in while loop
47 
48  /* with memory address increasing left to right, we are looking for (in hexadecimal):
49  * 00 00 01 XX
50  * p points at the address which should have the value of XX
51  */
52  while (p < end)
53  {
54  if (/* UU UU UU */ p[-1] < 1) // equivalently p[-1] == 0
55  {
56  p++;
57  // could be in a start code, so check next byte
58  }
59  else if (/* UU UU UN */ p[-1] > 1 ||
60  /* UU UU 01 */ p[-2] != 0 ||
61  /* UU 00 01 */ p[-3] != 0)
62  {
63  // start check over with 3 new bytes
64  p += 3;
65  }
66  else /* 00 00 01 */
67  {
68  p++;
69  // p now points at the address following the start code value XX
70  break;
71  }
72  }
73 
74  if (p > end)
75  p = end;
76  // read the previous 4 bytes, i.e. bytes {p - 4, p - 3, p - 2, p - 1}
77  *start_code = AV_RB32(p - 4);
78  return p;
79 }
80 
81 const uint8_t* ByteReader::find_start_code_truncated(const uint8_t * p,
82  const uint8_t * const end,
83  uint32_t * const start_code)
84 {
85  if (p >= end)
86  {
87  return end;
88  }
89 
90  if (*start_code == 0x100)
91  {
92  *start_code = ~0;
93  // invalidate byte 0 so overlapping start codes are not erroneously detected
94  }
95 
96  // read up to the first three bytes in p to enable reading a start code across
97  // two (to four) buffers
98  for (int i = 0; i < 3; i++)
99  {
100  *start_code <<= 8;
101  *start_code += *p;
102  p++;
103  if (start_code_is_valid(*start_code) || p == end)
104  {
105  return p;
106  }
107  }
108  // buffer length is at least 4
109  return find_start_code(p - 3, end, start_code);
110 }
bytereader.h
AV_RB32
#define AV_RB32(x)
Definition: bytereader.cpp:27
hardwareprofile.config.p
p
Definition: config.py:33
ByteReader::start_code_is_valid
bool start_code_is_valid(uint32_t start_code)
Test whether a start code found by find_start_code() is valid.
Definition: bytereader.h:54
ByteReader::find_start_code
const MTV_PUBLIC uint8_t * find_start_code(const uint8_t *p, const uint8_t *end, uint32_t *start_code)
Find the first start code in the buffer p.
Definition: bytereader.cpp:35
ByteReader::find_start_code_truncated
const MTV_PUBLIC uint8_t * find_start_code_truncated(const uint8_t *p, const uint8_t *end, uint32_t *start_code)
By preserving the start_code value between subsequent calls, the caller can detect start codes across...
Definition: bytereader.cpp:81