Merge commit '69ab9f53f901eac6a649e22d28cf093357870627'
[ffmpeg.git] / libavcodec / hevc_parse.c
1 /*
2  * HEVC common code
3  *
4  * This file is part of FFmpeg.
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include <string.h>
22
23 #include "config.h"
24
25 #include "libavutil/intreadwrite.h"
26 #include "libavutil/mem.h"
27
28 #include "hevc.h"
29
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,
33                          HEVCNAL *nal)
34 {
35     int i, si, di;
36     uint8_t *dst;
37
38     if (s)
39         s->skipped_bytes = 0;
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 */             \
44                 length = i;                                             \
45             }                                                           \
46             break;                                                      \
47         }
48 #if HAVE_FAST_UNALIGNED
49 #define FIND_FIRST_ZERO                                                 \
50         if (i > 0 && !src[i])                                           \
51             i--;                                                        \
52         while (src[i])                                                  \
53             i++
54 #if HAVE_FAST_64BIT
55     for (i = 0; i + 1 < length; i += 9) {
56         if (!((~AV_RN64A(src + i) &
57                (AV_RN64A(src + i) - 0x0100010001000101ULL)) &
58               0x8000800080008080ULL))
59             continue;
60         FIND_FIRST_ZERO;
61         STARTCODE_TEST;
62         i -= 7;
63     }
64 #else
65     for (i = 0; i + 1 < length; i += 5) {
66         if (!((~AV_RN32A(src + i) &
67                (AV_RN32A(src + i) - 0x01000101U)) &
68               0x80008080U))
69             continue;
70         FIND_FIRST_ZERO;
71         STARTCODE_TEST;
72         i -= 3;
73     }
74 #endif /* HAVE_FAST_64BIT */
75 #else
76     for (i = 0; i + 1 < length; i += 2) {
77         if (src[i])
78             continue;
79         if (i > 0 && src[i - 1] == 0)
80             i--;
81         STARTCODE_TEST;
82     }
83 #endif /* HAVE_FAST_UNALIGNED */
84
85     if (i >= length - 1) { // no escaped 0
86         nal->data     =
87         nal->raw_data = src;
88         nal->size     =
89         nal->raw_size = length;
90         return length;
91     }
92
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);
97
98     dst = nal->rbsp_buffer;
99
100     memcpy(dst, src, i);
101     si = di = i;
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
109                 dst[di++] = 0;
110                 dst[di++] = 0;
111                 si       += 3;
112
113                 if (s) {
114                         s->skipped_bytes++;
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);
122                         }
123                         if (s->skipped_bytes_pos)
124                         s->skipped_bytes_pos[s->skipped_bytes-1] = di - 1;
125                 }
126                 continue;
127             } else // next start code
128                 goto nsc;
129         }
130
131         dst[di++] = src[si++];
132     }
133     while (si < length)
134         dst[di++] = src[si++];
135
136 nsc:
137     memset(dst + di, 0, FF_INPUT_BUFFER_PADDING_SIZE);
138
139     nal->data = dst;
140     nal->size = di;
141     nal->raw_data = src;
142     nal->raw_size = si;
143     return si;
144 }
145