4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 #include "libavutil/intreadwrite.h"
26 #include "libavutil/mem.h"
30 /* FIXME: This is adapted from ff_h264_decode_nal, avoiding duplication
31 * between these functions would be nice. */
32 int ff_hevc_extract_rbsp(HEVCContext *s, const uint8_t *src, int length,
40 #define STARTCODE_TEST \
41 if (i + 2 < length && src[i + 1] == 0 && src[i + 2] <= 3) { \
42 if (src[i + 2] != 3) { \
43 /* startcode, so we must be past the end */ \
48 #if HAVE_FAST_UNALIGNED
49 #define FIND_FIRST_ZERO \
50 if (i > 0 && !src[i]) \
55 for (i = 0; i + 1 < length; i += 9) {
56 if (!((~AV_RN64A(src + i) &
57 (AV_RN64A(src + i) - 0x0100010001000101ULL)) &
58 0x8000800080008080ULL))
65 for (i = 0; i + 1 < length; i += 5) {
66 if (!((~AV_RN32A(src + i) &
67 (AV_RN32A(src + i) - 0x01000101U)) &
74 #endif /* HAVE_FAST_64BIT */
76 for (i = 0; i + 1 < length; i += 2) {
79 if (i > 0 && src[i - 1] == 0)
83 #endif /* HAVE_FAST_UNALIGNED */
85 if (i >= length - 1) { // no escaped 0
89 nal->raw_size = length;
93 av_fast_malloc(&nal->rbsp_buffer, &nal->rbsp_buffer_size,
94 length + FF_INPUT_BUFFER_PADDING_SIZE);
95 if (!nal->rbsp_buffer)
96 return AVERROR(ENOMEM);
98 dst = nal->rbsp_buffer;
102 while (si + 2 < length) {
103 // remove escapes (very rare 1:2^22)
104 if (src[si + 2] > 3) {
105 dst[di++] = src[si++];
106 dst[di++] = src[si++];
107 } else if (src[si] == 0 && src[si + 1] == 0) {
108 if (src[si + 2] == 3) { // escape
115 if (s->skipped_bytes_pos_size < s->skipped_bytes) {
116 s->skipped_bytes_pos_size *= 2;
117 av_reallocp_array(&s->skipped_bytes_pos,
118 s->skipped_bytes_pos_size,
119 sizeof(*s->skipped_bytes_pos));
120 if (!s->skipped_bytes_pos)
121 return AVERROR(ENOMEM);
123 if (s->skipped_bytes_pos)
124 s->skipped_bytes_pos[s->skipped_bytes-1] = di - 1;
127 } else // next start code
131 dst[di++] = src[si++];
134 dst[di++] = src[si++];
137 memset(dst + di, 0, FF_INPUT_BUFFER_PADDING_SIZE);