]> git.sesse.net Git - ffmpeg/blob - libavcodec/hevc_parse.c
qtrle: Properly use AVFrame API
[ffmpeg] / libavcodec / hevc_parse.c
1 /*
2  * HEVC common code
3  *
4  * This file is part of Libav.
5  *
6  * Libav 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  * Libav 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 Libav; 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(const uint8_t *src, int length,
33                          HEVCNAL *nal)
34 {
35     int i, si, di;
36     uint8_t *dst;
37
38 #define STARTCODE_TEST                                                  \
39         if (i + 2 < length && src[i + 1] == 0 && src[i + 2] <= 3) {     \
40             if (src[i + 2] != 3) {                                      \
41                 /* startcode, so we must be past the end */             \
42                 length = i;                                             \
43             }                                                           \
44             break;                                                      \
45         }
46 #if HAVE_FAST_UNALIGNED
47 #define FIND_FIRST_ZERO                                                 \
48         if (i > 0 && !src[i])                                           \
49             i--;                                                        \
50         while (src[i])                                                  \
51             i++
52 #if HAVE_FAST_64BIT
53     for (i = 0; i + 1 < length; i += 9) {
54         if (!((~AV_RN64A(src + i) &
55                (AV_RN64A(src + i) - 0x0100010001000101ULL)) &
56               0x8000800080008080ULL))
57             continue;
58         FIND_FIRST_ZERO;
59         STARTCODE_TEST;
60         i -= 7;
61     }
62 #else
63     for (i = 0; i + 1 < length; i += 5) {
64         if (!((~AV_RN32A(src + i) &
65                (AV_RN32A(src + i) - 0x01000101U)) &
66               0x80008080U))
67             continue;
68         FIND_FIRST_ZERO;
69         STARTCODE_TEST;
70         i -= 3;
71     }
72 #endif /* HAVE_FAST_64BIT */
73 #else
74     for (i = 0; i + 1 < length; i += 2) {
75         if (src[i])
76             continue;
77         if (i > 0 && src[i - 1] == 0)
78             i--;
79         STARTCODE_TEST;
80     }
81 #endif /* HAVE_FAST_UNALIGNED */
82
83     if (i >= length - 1) { // no escaped 0
84         nal->data     =
85         nal->raw_data = src;
86         nal->size     =
87         nal->raw_size = length;
88         return length;
89     }
90
91     av_fast_malloc(&nal->rbsp_buffer, &nal->rbsp_buffer_size,
92                    length + AV_INPUT_BUFFER_PADDING_SIZE);
93     if (!nal->rbsp_buffer)
94         return AVERROR(ENOMEM);
95
96     dst = nal->rbsp_buffer;
97
98     memcpy(dst, src, i);
99     si = di = i;
100     while (si + 2 < length) {
101         // remove escapes (very rare 1:2^22)
102         if (src[si + 2] > 3) {
103             dst[di++] = src[si++];
104             dst[di++] = src[si++];
105         } else if (src[si] == 0 && src[si + 1] == 0) {
106             if (src[si + 2] == 3) { // escape
107                 dst[di++] = 0;
108                 dst[di++] = 0;
109                 si       += 3;
110
111                 continue;
112             } else // next start code
113                 goto nsc;
114         }
115
116         dst[di++] = src[si++];
117     }
118     while (si < length)
119         dst[di++] = src[si++];
120
121 nsc:
122     memset(dst + di, 0, AV_INPUT_BUFFER_PADDING_SIZE);
123
124     nal->data = dst;
125     nal->size = di;
126     nal->raw_data = src;
127     nal->raw_size = si;
128     return si;
129 }
130
131 /**
132  * @return AVERROR_INVALIDDATA if the packet is not a valid NAL unit,
133  * 0 if the unit should be skipped, 1 otherwise
134  */
135 static int hls_nal_unit(HEVCNAL *nal, AVCodecContext *avctx)
136 {
137     GetBitContext *gb = &nal->gb;
138     int nuh_layer_id;
139
140     if (get_bits1(gb) != 0)
141         return AVERROR_INVALIDDATA;
142
143     nal->type = get_bits(gb, 6);
144
145     nuh_layer_id   = get_bits(gb, 6);
146     nal->temporal_id = get_bits(gb, 3) - 1;
147     if (nal->temporal_id < 0)
148         return AVERROR_INVALIDDATA;
149
150     av_log(avctx, AV_LOG_DEBUG,
151            "nal_unit_type: %d, nuh_layer_id: %dtemporal_id: %d\n",
152            nal->type, nuh_layer_id, nal->temporal_id);
153
154     return nuh_layer_id == 0;
155 }
156
157
158 int ff_hevc_split_packet(HEVCPacket *pkt, const uint8_t *buf, int length,
159                          AVCodecContext *avctx, int is_nalff, int nal_length_size)
160 {
161     int consumed, ret = 0;
162
163     pkt->nb_nals = 0;
164     while (length >= 4) {
165         HEVCNAL *nal;
166         int extract_length = 0;
167
168         if (is_nalff) {
169             int i;
170             for (i = 0; i < nal_length_size; i++)
171                 extract_length = (extract_length << 8) | buf[i];
172             buf    += nal_length_size;
173             length -= nal_length_size;
174
175             if (extract_length > length) {
176                 av_log(avctx, AV_LOG_ERROR, "Invalid NAL unit size.\n");
177                 return AVERROR_INVALIDDATA;
178             }
179         } else {
180             if (buf[2] == 0) {
181                 length--;
182                 buf++;
183                 continue;
184             }
185             if (buf[0] != 0 || buf[1] != 0 || buf[2] != 1)
186                 return AVERROR_INVALIDDATA;
187
188             buf           += 3;
189             length        -= 3;
190             extract_length = length;
191         }
192
193         if (pkt->nals_allocated < pkt->nb_nals + 1) {
194             int new_size = pkt->nals_allocated + 1;
195             HEVCNAL *tmp = av_realloc_array(pkt->nals, new_size, sizeof(*tmp));
196             if (!tmp)
197                 return AVERROR(ENOMEM);
198
199             pkt->nals = tmp;
200             memset(pkt->nals + pkt->nals_allocated, 0,
201                    (new_size - pkt->nals_allocated) * sizeof(*tmp));
202             pkt->nals_allocated = new_size;
203         }
204         nal = &pkt->nals[pkt->nb_nals++];
205
206         consumed = ff_hevc_extract_rbsp(buf, extract_length, nal);
207         if (consumed < 0)
208             return consumed;
209
210         ret = init_get_bits8(&nal->gb, nal->data, nal->size);
211         if (ret < 0)
212             return ret;
213
214         ret = hls_nal_unit(nal, avctx);
215         if (ret <= 0) {
216             if (ret < 0) {
217                 av_log(avctx, AV_LOG_ERROR, "Invalid NAL unit %d, skipping.\n",
218                        nal->type);
219             }
220             pkt->nb_nals--;
221         }
222
223         buf    += consumed;
224         length -= consumed;
225     }
226
227     return 0;
228 }