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
24
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
46
50
54
57 {
64 int i, num = 0, den = 0;
65
69
73 }
74
79 }
81
85 }
89 }
91
99
106 }
107
108 if (num != 0 && den != 0)
110 num, den, 1 << 30);
111
113 int slice_address_length;
114
117 else
119
127 }
128 } else
130
132 return 0; /* break; */
133
135 skip_bits(gb, 1);
// slice_reserved_undetermined_flag[]
136
143 }
147
150
153
157 } else
159
169
170 return 1; /* no need to evaluate the rest */
171 }
172
173 /**
174 * Parse NAL units of found picture and decode some basic information.
175 *
176 * @param s parser context.
177 * @param avctx codec context.
178 * @param buf buffer with field/frame data.
179 * @param buf_size size of the buffer.
180 */
183 {
187 int ret, i;
188
189 /* set some sane default values */
193
195
198 if (ret < 0)
199 return ret;
200
204
208 break;
211 break;
214 break;
218 break;
236 if (ret)
237 return ret;
238 break;
239 }
240 }
241 /* didn't find a picture! */
243 return -1;
244 }
245
246 /**
247 * Find the end of the current frame in the bitstream.
248 * @return the position of the first byte of the next frame, or END_NOT_FOUND
249 */
251 int buf_size)
252 {
255 int i;
256
257 for (i = 0; i < buf_size; i++) {
258 int nut;
259
261
263 continue;
264
265 nut = (pc->
state64 >> 2 * 8 + 1) & 0x3F;
266 // Beginning of access unit
268 (nut >= 41 && nut <= 44) || (nut >= 48 && nut <= 55)) {
271 return i - 5;
272 }
275 int first_slice_segment_in_pic_flag = buf[i] >> 7;
276 if (first_slice_segment_in_pic_flag) {
279 } else { // First slice of next frame found
281 return i - 5;
282 }
283 }
284 }
285 }
286
288 }
289
291 const uint8_t **poutbuf,
int *poutbuf_size,
293 {
294 int next;
297
301 1, avctx);
303 }
304
306 next = buf_size;
307 } else {
311 *poutbuf_size = 0;
312 return buf_size;
313 }
314 }
315
317
319 *poutbuf_size = buf_size;
320 return next;
321 }
322
323 // Split after the parameter sets at the beginning of the stream if they exist.
325 {
328 int has_vps = 0;
329 int has_sps = 0;
330 int has_pps = 0;
331 int nut;
332
333 while (ptr < end) {
336 break;
337 nut = (state >> 1) & 0x3F;
339 has_vps = 1;
341 has_sps = 1;
343 has_pps = 1;
346 if (has_vps && has_sps) {
347 while (ptr - 4 > buf && ptr[-5] == 0)
348 ptr--;
349 return ptr - 4 -
buf;
350 }
351 }
352 }
353 return 0;
354 }
355
357 {
359
363
365 }
366
373 };
int ff_h2645_packet_split(H2645Packet *pkt, const uint8_t *buf, int length, void *logctx, int is_nalff, int nal_length_size, enum AVCodecID codec_id, int small_padding)
Split an input packet into NAL units.
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
static int hevc_parse_slice_header(AVCodecParserContext *s, H2645NAL *nal, AVCodecContext *avctx)
AVBufferRef * vps_list[HEVC_MAX_VPS_COUNT]
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
static int hevc_split(AVCodecContext *avctx, const uint8_t *buf, int buf_size)
int width
Dimensions of the decoded video intended for presentation.
enum AVFieldOrder field_order
int coded_width
Dimensions of the coded video.
int ff_hevc_decode_extradata(const uint8_t *data, int size, HEVCParamSets *ps, HEVCSEI *sei, int *is_nalff, int *nal_length_size, int err_recognition, int apply_defdispwin, void *logctx)
AVBufferRef * sps_list[HEVC_MAX_SPS_COUNT]
enum AVPictureStructure picture_structure
Indicate whether a picture is coded as a frame, top field or bottom field.
uint8_t vps_timing_info_present_flag
static av_cold int end(AVCodecContext *avctx)
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
void ff_h2645_packet_uninit(H2645Packet *pkt)
Free all the allocated memory in the packet.
AVBufferRef * pps_list[HEVC_MAX_PPS_COUNT]
int av_reduce(int *dst_num, int *dst_den, int64_t num, int64_t den, int64_t max)
Reduce a fraction.
const uint8_t * avpriv_find_start_code(const uint8_t *p, const uint8_t *end, uint32_t *state)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
static int get_ue_golomb(GetBitContext *gb)
Read an unsigned Exp-Golomb code in the range 0 to 8190.
unsigned int log2_max_poc_lsb
int ff_hevc_compute_poc(const HEVCSPS *sps, int pocTid0, int poc_lsb, int nal_unit_type)
Compute POC of the current frame and return it.
int ff_hevc_decode_nal_vps(GetBitContext *gb, AVCodecContext *avctx, HEVCParamSets *ps)
int ff_combine_frame(ParseContext *pc, int next, const uint8_t **buf, int *buf_size)
Combine the (truncated) bitstream to a complete frame.
int vui_timing_info_present_flag
static int hevc_find_frame_end(AVCodecParserContext *s, const uint8_t *buf, int buf_size)
Find the end of the current frame in the bitstream.
static char * split(char *message, char delim)
static int parse_nal_units(AVCodecParserContext *s, const uint8_t *buf, int buf_size, AVCodecContext *avctx)
Parse NAL units of found picture and decode some basic information.
uint32_t vps_num_units_in_tick
int err_recognition
Error recognition; may misdetect some more or less valid parts as errors.
uint8_t output_flag_present_flag
void ff_hevc_ps_uninit(HEVCParamSets *ps)
enum AVPixelFormat pix_fmt
void ff_hevc_reset_sei(HEVCSEI *s)
Reset SEI values that are stored on the Context.
#define START_CODE
start_code_prefix_one_3bytes
unsigned int sps_id
seq_parameter_set_id
main external API structure.
int num_extra_slice_header_bits
uint8_t * data
The data buffer.
AVCodecParser ff_hevc_parser
uint32_t vui_num_units_in_tick
static unsigned int get_bits1(GetBitContext *s)
static void skip_bits(GetBitContext *s, int n)
uint64_t state64
contains the last 8 bytes in MSB order
int ff_hevc_decode_nal_pps(GetBitContext *gb, AVCodecContext *avctx, HEVCParamSets *ps)
int ff_hevc_decode_nal_sps(GetBitContext *gb, AVCodecContext *avctx, HEVCParamSets *ps, int apply_defdispwin)
int output_picture_number
Picture number incremented in presentation or output order.
HEVCSEIPictureTiming picture_timing
common internal api header.
common internal and external API header
#define PARSER_FLAG_COMPLETE_FRAMES
static int FUNC() sei(CodedBitstreamContext *ctx, RWContext *rw, H264RawSEI *current)
int format
The format of the coded data, corresponds to enum AVPixelFormat for video and for enum AVSampleFormat...
int temporal_id
HEVC only, nuh_temporal_id_plus_1 - 1.
unsigned int right_offset
static int hevc_parse(AVCodecParserContext *s, AVCodecContext *avctx, const uint8_t **poutbuf, int *poutbuf_size, const uint8_t *buf, int buf_size)
static void hevc_parser_close(AVCodecParserContext *s)
static av_always_inline av_const int av_ceil_log2_c(int x)
Compute ceil(log2(x)).
int key_frame
Set by parser to 1 for key frames and 0 for non-key frames.
int ff_hevc_decode_nal_sei(GetBitContext *gb, void *logctx, HEVCSEI *s, const HEVCParamSets *ps, int type)
uint8_t separate_colour_plane_flag
static av_always_inline int get_bitsz(GetBitContext *s, int n)
Read 0-25 bits.
unsigned int bottom_offset
uint8_t dependent_slice_segments_enabled_flag