1 /*
2 * HEVC Annex B format parser
3 *
4 * Copyright (C) 2012 - 2013 Guillaume Martres
5 *
6 * This file is part of FFmpeg.
7 *
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
25
33
34 #define START_CODE 0x000001 ///< start_code_prefix_one_3bytes
35
36 #define IS_IRAP_NAL(nal) (nal->type >= 16 && nal->type <= 23)
37 #define IS_IDR_NAL(nal) (nal->type == HEVC_NAL_IDR_W_RADL || nal->type == HEVC_NAL_IDR_N_LP)
38
41
45
49
53
56 {
64 int i, num = 0, den = 0;
65
66 unsigned int pps_id, first_slice_in_pic_flag, dependent_slice_segment_flag;
68
70 s->picture_structure =
sei->picture_timing.picture_struct;
71 s->field_order =
sei->picture_timing.picture_struct;
72
75 skip_bits1(gb);
// no_output_of_prior_pics_flag
76 }
77
82 }
85
86 ow = &
sps->output_window;
87
88 s->coded_width =
sps->width;
89 s->coded_height =
sps->height;
92 s->format =
sps->pix_fmt;
93 avctx->
profile =
sps->ptl.general_ptl.profile_idc;
94 avctx->
level =
sps->ptl.general_ptl.level_idc;
95
96 if (
sps->vps->vps_timing_info_present_flag) {
97 num =
sps->vps->vps_num_units_in_tick;
98 den =
sps->vps->vps_time_scale;
99 }
else if (
sps->vui.vui_timing_info_present_flag) {
100 num =
sps->vui.vui_num_units_in_tick;
101 den =
sps->vui.vui_time_scale;
102 }
103
104 if (num > 0 && den > 0)
106 num, den, 1 << 30);
107
108 if (!first_slice_in_pic_flag) {
109 unsigned int slice_segment_addr;
110 int slice_address_length;
111
112 if (
pps->dependent_slice_segments_enabled_flag)
113 dependent_slice_segment_flag =
get_bits1(gb);
114 else
115 dependent_slice_segment_flag = 0;
116
119 slice_segment_addr =
get_bitsz(gb, slice_address_length);
120 if (slice_segment_addr >=
sps->ctb_width *
sps->ctb_height) {
122 slice_segment_addr);
124 }
125 } else
126 dependent_slice_segment_flag = 0;
127
128 if (dependent_slice_segment_flag)
129 return 0; /* break; */
130
131 for (
i = 0;
i <
pps->num_extra_slice_header_bits;
i++)
132 skip_bits(gb, 1);
// slice_reserved_undetermined_flag[]
133
138 slice_type);
140 }
144
145 if (
pps->output_flag_present_flag)
147
148 if (
sps->separate_colour_plane)
150
152 int pic_order_cnt_lsb =
get_bits(gb,
sps->log2_max_poc_lsb);
153 s->output_picture_number =
ctx->poc =
155 } else
156 s->output_picture_number =
ctx->poc = 0;
157
167
168 return 1; /* no need to evaluate the rest */
169 }
170
171 /**
172 * Parse NAL units of found picture and decode some basic information.
173 *
174 * @param s parser context.
175 * @param avctx codec context.
176 * @param buf buffer with field/frame data.
177 * @param buf_size size of the buffer.
178 */
181 {
187
188 /* set some sane default values */
192
194
199
200 for (
i = 0;
i <
ctx->pkt.nb_nals;
i++) {
203
205 continue;
206
210 break;
213 break;
216 break;
220 break;
241 }
245 break;
246 }
247 }
248 /* didn't find a picture! */
249 av_log(avctx,
AV_LOG_ERROR,
"missing picture in access unit with size %d\n", buf_size);
250 return -1;
251 }
252
253 /**
254 * Find the end of the current frame in the bitstream.
255 * @return the position of the first byte of the next frame, or END_NOT_FOUND
256 */
258 int buf_size)
259 {
263
264 for (
i = 0;
i < buf_size;
i++) {
265 int nut, layer_id;
266
268
270 continue;
271
272 nut = (pc->
state64 >> 2 * 8 + 1) & 0x3F;
273
274 layer_id = (pc->
state64 >> 11) & 0x3F;
275 if (layer_id > 0)
276 continue;
277
278 // Beginning of access unit
280 (nut >= 41 && nut <= 44) || (nut >= 48 && nut <= 55)) {
283 if (!((pc->
state64 >> 6 * 8) & 0xFF))
286 }
289 int first_slice_segment_in_pic_flag = buf[
i] >> 7;
290 if (first_slice_segment_in_pic_flag) {
293 } else { // First slice of next frame found
295 if (!((pc->
state64 >> 6 * 8) & 0xFF))
298 }
299 }
300 }
301 }
302
304 }
305
307 const uint8_t **poutbuf, int *poutbuf_size,
308 const uint8_t *buf, int buf_size)
309 {
310 int next;
313 int is_dummy_buf = !buf_size;
314 const uint8_t *dummy_buf = buf;
315
319 1, avctx);
320 ctx->parsed_extradata = 1;
321 }
322
324 next = buf_size;
325 } else {
329 *poutbuf_size = 0;
330 return buf_size;
331 }
332 }
333
334 is_dummy_buf &= (dummy_buf == buf);
335
336 if (!is_dummy_buf)
338
339 *poutbuf = buf;
340 *poutbuf_size = buf_size;
341 return next;
342 }
343
345 {
347
351
353 }
354
360 };