FFmpeg
Loading...
Searching...
No Matches
parser.c
Go to the documentation of this file.
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
23#include "libavutil/common.h"
24#include "libavutil/mem.h"
25
26#include "libavcodec/golomb.h"
29#include "libavcodec/parser.h"
30
31#include "hevc.h"
32#include "parse.h"
33#include "ps.h"
34#include "sei.h"
35
36#define START_CODE 0x000001 ///< start_code_prefix_one_3bytes
37
38#define IS_IRAP_NAL(nal) (nal->type >= 16 && nal->type <= 23)
39#define IS_IDR_NAL(nal) (nal->type == HEVC_NAL_IDR_W_RADL || nal->type == HEVC_NAL_IDR_N_LP)
40
55
57 AVCodecContext *avctx)
58{
59 HEVCParserContext *ctx = s->priv_data;
60 HEVCParamSets *ps = &ctx->ps;
61 HEVCSEI *sei = &ctx->sei;
62 GetBitContext *gb = &nal->gb;
63 const HEVCPPS *pps;
64 const HEVCSPS *sps;
65 const HEVCWindow *ow;
66 int i, num = 0, den = 0;
67
68 unsigned int pps_id, first_slice_in_pic_flag, dependent_slice_segment_flag;
69 enum HEVCSliceType slice_type;
70
71 first_slice_in_pic_flag = get_bits1(gb);
72 s->picture_structure = sei->picture_timing.picture_struct;
73 s->field_order = sei->picture_timing.picture_struct;
74
75 if (IS_IRAP_NAL(nal)) {
76 s->key_frame = 1;
77 skip_bits1(gb); // no_output_of_prior_pics_flag
78 }
79
80 pps_id = get_ue_golomb(gb);
81 if (pps_id >= HEVC_MAX_PPS_COUNT || !ps->pps_list[pps_id]) {
82 av_log(avctx, AV_LOG_ERROR, "PPS id out of range: %d\n", pps_id);
84 }
85 pps = ps->pps_list[pps_id];
86 sps = pps->sps;
87
88 ow = &sps->output_window;
89
90 s->coded_width = sps->width;
91 s->coded_height = sps->height;
92 s->width = sps->width - ow->left_offset - ow->right_offset;
93 s->height = sps->height - ow->top_offset - ow->bottom_offset;
94 s->format = sps->pix_fmt;
95 avctx->profile = sps->ptl.general_ptl.profile_idc;
96 avctx->level = sps->ptl.general_ptl.level_idc;
97
98 if (sps->vps->vps_timing_info_present_flag) {
99 num = sps->vps->vps_num_units_in_tick;
100 den = sps->vps->vps_time_scale;
101 } else if (sps->vui.vui_timing_info_present_flag) {
102 num = sps->vui.vui_num_units_in_tick;
103 den = sps->vui.vui_time_scale;
104 }
105
106 if (num > 0 && den > 0)
107 av_reduce(&avctx->framerate.den, &avctx->framerate.num,
108 num, den, 1 << 30);
109
110 if (!first_slice_in_pic_flag) {
111 unsigned int slice_segment_addr;
112 int slice_address_length;
113
114 if (pps->dependent_slice_segments_enabled_flag)
115 dependent_slice_segment_flag = get_bits1(gb);
116 else
117 dependent_slice_segment_flag = 0;
118
119 slice_address_length = av_ceil_log2_c(sps->ctb_width *
120 sps->ctb_height);
121 slice_segment_addr = get_bitsz(gb, slice_address_length);
122 if (slice_segment_addr >= sps->ctb_width * sps->ctb_height) {
123 av_log(avctx, AV_LOG_ERROR, "Invalid slice segment address: %u.\n",
124 slice_segment_addr);
125 return AVERROR_INVALIDDATA;
126 }
127 } else
128 dependent_slice_segment_flag = 0;
129
130 if (dependent_slice_segment_flag)
131 return 0; /* break; */
132
133 for (i = 0; i < pps->num_extra_slice_header_bits; i++)
134 skip_bits(gb, 1); // slice_reserved_undetermined_flag[]
135
136 slice_type = get_ue_golomb_31(gb);
137 if (!(slice_type == HEVC_SLICE_I || slice_type == HEVC_SLICE_P ||
138 slice_type == HEVC_SLICE_B)) {
139 av_log(avctx, AV_LOG_ERROR, "Unknown slice type: %d.\n",
140 slice_type);
141 return AVERROR_INVALIDDATA;
142 }
143 s->pict_type = slice_type == HEVC_SLICE_B ? AV_PICTURE_TYPE_B :
144 slice_type == HEVC_SLICE_P ? AV_PICTURE_TYPE_P :
146
147 if (pps->output_flag_present_flag)
148 skip_bits1(gb); // pic_output_flag
149
150 if (sps->separate_colour_plane)
151 skip_bits(gb, 2); // colour_plane_id
152
153 if (!IS_IDR_NAL(nal)) {
154 int pic_order_cnt_lsb = get_bits(gb, sps->log2_max_poc_lsb);
155 s->output_picture_number = ctx->poc =
156 ff_hevc_compute_poc(sps, ctx->pocTid0, pic_order_cnt_lsb, nal->type);
157 } else
158 s->output_picture_number = ctx->poc = 0;
159
160 if (nal->temporal_id == 0 &&
161 nal->type != HEVC_NAL_TRAIL_N &&
162 nal->type != HEVC_NAL_TSA_N &&
163 nal->type != HEVC_NAL_STSA_N &&
164 nal->type != HEVC_NAL_RADL_N &&
165 nal->type != HEVC_NAL_RASL_N &&
166 nal->type != HEVC_NAL_RADL_R &&
167 nal->type != HEVC_NAL_RASL_R)
168 ctx->pocTid0 = ctx->poc;
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 */
181static int parse_nal_units(AVCodecParserContext *s, const uint8_t *buf,
182 int buf_size, AVCodecContext *avctx)
183{
184 HEVCParserContext *ctx = s->priv_data;
185 HEVCParamSets *ps = &ctx->ps;
186 HEVCSEI *sei = &ctx->sei;
188 int ret, i;
189
190 /* set some sane default values */
191 s->pict_type = AV_PICTURE_TYPE_I;
192 s->key_frame = 0;
193 s->picture_structure = AV_PICTURE_STRUCTURE_UNKNOWN;
194
196
197 ret = ff_h2645_packet_split(&ctx->pkt, buf, buf_size, avctx,
198 ctx->nal_length_size, AV_CODEC_ID_HEVC, flags);
199 if (ret < 0)
200 return ret;
201
202 for (i = 0; i < ctx->pkt.nb_nals; i++) {
203 H2645NAL *nal = &ctx->pkt.nals[i];
204 GetBitContext *gb = &nal->gb;
205
206 if (nal->nuh_layer_id > 0)
207 continue;
208
209 switch (nal->type) {
210 case HEVC_NAL_VPS:
211 ff_hevc_decode_nal_vps(gb, avctx, ps);
212 break;
213 case HEVC_NAL_SPS:
214 ff_hevc_decode_nal_sps(gb, avctx, ps, nal->nuh_layer_id, 1);
215 break;
216 case HEVC_NAL_PPS:
217 ff_hevc_decode_nal_pps(gb, avctx, ps);
218 break;
221 ff_hevc_decode_nal_sei(gb, avctx, sei, ps, nal->type);
222 break;
223 case HEVC_NAL_TRAIL_N:
224 case HEVC_NAL_TRAIL_R:
225 case HEVC_NAL_TSA_N:
226 case HEVC_NAL_TSA_R:
227 case HEVC_NAL_STSA_N:
228 case HEVC_NAL_STSA_R:
234 case HEVC_NAL_CRA_NUT:
235 case HEVC_NAL_RADL_N:
236 case HEVC_NAL_RADL_R:
237 case HEVC_NAL_RASL_N:
238 case HEVC_NAL_RASL_R:
239 if (ctx->sei.picture_timing.picture_struct == HEVC_SEI_PIC_STRUCT_FRAME_DOUBLING) {
240 s->repeat_pict = 1;
241 } else if (ctx->sei.picture_timing.picture_struct == HEVC_SEI_PIC_STRUCT_FRAME_TRIPLING) {
242 s->repeat_pict = 2;
243 }
244 ret = hevc_parse_slice_header(s, nal, avctx);
245 if (ret)
246 return ret;
247 break;
248 }
249 }
250 /* didn't find a picture! */
251 av_log(avctx, AV_LOG_ERROR, "missing picture in access unit with size %d\n", buf_size);
252 return -1;
253}
254
255/**
256 * Find the end of the current frame in the bitstream.
257 * @return the position of the first byte of the next frame, or END_NOT_FOUND
258 */
259static int hevc_find_frame_end(AVCodecParserContext *s, const uint8_t *buf,
260 int buf_size)
261{
262 HEVCParserContext *ctx = s->priv_data;
263 ParseContext *pc = &ctx->pc;
264 int i;
265
266 for (i = 0; i < buf_size; i++) {
267 int nut, layer_id;
268
269 pc->state64 = (pc->state64 << 8) | buf[i];
270
271 if (((pc->state64 >> 3 * 8) & 0xFFFFFF) != START_CODE)
272 continue;
273
274 nut = (pc->state64 >> 2 * 8 + 1) & 0x3F;
275
276 layer_id = (pc->state64 >> 11) & 0x3F;
277 if (layer_id > 0)
278 continue;
279
280 // Beginning of access unit
281 if ((nut >= HEVC_NAL_VPS && nut <= HEVC_NAL_EOB_NUT) || nut == HEVC_NAL_SEI_PREFIX ||
282 (nut >= 41 && nut <= 44) || (nut >= 48 && nut <= 55)) {
283 if (pc->frame_start_found) {
284 pc->frame_start_found = 0;
285 if (!((pc->state64 >> 6 * 8) & 0xFF))
286 return i - 6;
287 return i - 5;
288 }
289 } else if (nut <= HEVC_NAL_RASL_R ||
290 (nut >= HEVC_NAL_BLA_W_LP && nut <= HEVC_NAL_CRA_NUT)) {
291 int first_slice_segment_in_pic_flag = buf[i] >> 7;
292 if (first_slice_segment_in_pic_flag) {
293 if (!pc->frame_start_found) {
294 pc->frame_start_found = 1;
295 } else { // First slice of next frame found
296 pc->frame_start_found = 0;
297 if (!((pc->state64 >> 6 * 8) & 0xFF))
298 return i - 6;
299 return i - 5;
300 }
301 }
302 }
303 }
304
305 return END_NOT_FOUND;
306}
307
309 const uint8_t **poutbuf, int *poutbuf_size,
310 const uint8_t *buf, int buf_size)
311{
312 int next;
313 HEVCParserContext *ctx = s->priv_data;
314 ParseContext *pc = &ctx->pc;
315 int is_dummy_buf = !buf_size;
316 const uint8_t *dummy_buf = buf;
317
318 if (avctx->extradata && !ctx->parsed_extradata) {
319 ff_hevc_decode_extradata(avctx->extradata, avctx->extradata_size, &ctx->ps, &ctx->sei,
320 &ctx->is_avc, &ctx->nal_length_size, avctx->err_recognition,
321 1, avctx);
322 ctx->parsed_extradata = 1;
323 }
324
325 if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
326 next = buf_size;
327 } else {
328 next = hevc_find_frame_end(s, buf, buf_size);
329 if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
330 *poutbuf = NULL;
331 *poutbuf_size = 0;
332 return buf_size;
333 }
334 }
335
336 is_dummy_buf &= (dummy_buf == buf);
337
338 if (!is_dummy_buf)
339 parse_nal_units(s, buf, buf_size, avctx);
340
341 *poutbuf = buf;
342 *poutbuf_size = buf_size;
343 return next;
344}
345
347{
348 HEVCParserContext *ctx = s->priv_data;
349
352 ff_hevc_reset_sei(&ctx->sei);
353
354 av_freep(&ctx->pc.buffer);
355}
356
static AVFormatContext * ctx
static int parse(AVCodecParserContext *s, AVCodecContext *avctx, const uint8_t **poutbuf, int *poutbuf_size, const uint8_t *buf, int buf_size)
Definition apv_parser.c:92
static av_cold void close(AVCodecParserContext *s)
Definition apv_parser.c:197
#define PARSER_FLAG_COMPLETE_FRAMES
Definition avcodec.h:2651
#define flags(name, subs,...)
Definition cbs_h264.c:74
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
static int FUNC sei(CodedBitstreamContext *ctx, RWContext *rw, H264RawSEI *current)
static int FUNC sps(CodedBitstreamContext *ctx, RWContext *rw, H264RawSPS *current)
static int FUNC nal(CodedBitstreamContext *ctx, RWContext *rw, LCEVCRawNAL *current, int nal_unit_type)
#define s(width, name)
Definition cbs_vp9.c:198
common internal and external API header
static av_always_inline av_const int av_ceil_log2_c(int x)
Compute ceil(log2(x)).
Definition common.h:425
#define NULL
Definition coverity.c:32
uint64_t pps
Definition dovi_rpuenc.c:36
static unsigned int get_bits1(GetBitContext *s)
Definition get_bits.h:391
static void skip_bits(GetBitContext *s, int n)
Definition get_bits.h:383
static void skip_bits1(GetBitContext *s)
Definition get_bits.h:416
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition get_bits.h:337
static av_always_inline int get_bitsz(GetBitContext *s, int n)
Read 0-25 bits.
Definition get_bits.h:353
exp golomb vlc stuff
static int get_ue_golomb_31(GetBitContext *gb)
read unsigned exp golomb code, constraint to a max of 31.
Definition golomb.h:120
static int get_ue_golomb(GetBitContext *gb)
Read an unsigned Exp-Golomb code in the range 0 to 8190.
Definition golomb.h:53
@ AV_CODEC_ID_HEVC
Definition codec_id.h:223
@ AV_PICTURE_STRUCTURE_UNKNOWN
unknown
Definition avcodec.h:2611
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
int av_reduce(int *dst_num, int *dst_den, int64_t num, int64_t den, int64_t max)
Reduce a fraction.
Definition rational.c:35
@ AV_PICTURE_TYPE_I
Intra.
Definition avutil.h:278
@ AV_PICTURE_TYPE_P
Predicted.
Definition avutil.h:279
@ AV_PICTURE_TYPE_B
Bi-dir predicted.
Definition avutil.h:280
@ H2645_FLAG_SMALL_PADDING
Definition h2645_parse.h:98
@ H2645_FLAG_IS_NALFF
Definition h2645_parse.h:97
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)
Definition parse.c:79
H.265 parser code.
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.
Definition parser.c:259
static int hevc_parse_slice_header(AVCodecParserContext *s, H2645NAL *nal, AVCodecContext *avctx)
Definition parser.c:56
const FFCodecParser ff_hevc_parser
Definition parser.c:357
#define IS_IDR_NAL(nal)
Definition parser.c:39
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.
Definition parser.c:181
#define IS_IRAP_NAL(nal)
Definition parser.c:38
static int hevc_parse(AVCodecParserContext *s, AVCodecContext *avctx, const uint8_t **poutbuf, int *poutbuf_size, const uint8_t *buf, int buf_size)
Definition parser.c:308
static void hevc_parser_close(AVCodecParserContext *s)
Definition parser.c:346
int ff_hevc_decode_nal_pps(GetBitContext *gb, AVCodecContext *avctx, HEVCParamSets *ps)
Definition ps.c:2201
void ff_hevc_ps_uninit(HEVCParamSets *ps)
Definition ps.c:2473
int ff_hevc_decode_nal_vps(GetBitContext *gb, AVCodecContext *avctx, HEVCParamSets *ps)
Definition ps.c:786
int ff_hevc_decode_nal_sps(GetBitContext *gb, AVCodecContext *avctx, HEVCParamSets *ps, unsigned nuh_layer_id, int apply_defdispwin)
Definition ps.c:1735
static int ff_hevc_compute_poc(const HEVCSPS *sps, int pocTid0, int poc_lsb, int nal_unit_type)
Definition ps.h:548
int ff_hevc_decode_nal_sei(GetBitContext *gb, void *logctx, HEVCSEI *s, const HEVCParamSets *ps, enum HEVCNALUnitType type)
Definition sei.c:303
static void ff_hevc_reset_sei(HEVCSEI *sei)
Reset SEI values that are stored on the Context.
Definition sei.h:128
@ HEVC_SEI_PIC_STRUCT_FRAME_DOUBLING
Definition sei.h:36
@ HEVC_SEI_PIC_STRUCT_FRAME_TRIPLING
Definition sei.h:37
void ff_h2645_packet_uninit(H2645Packet *pkt)
Free all the allocated memory in the packet.
int ff_h2645_packet_split(H2645Packet *pkt, const uint8_t *buf, int length, void *logctx, int nal_length_size, enum AVCodecID codec_id, int flags)
Split an input packet into NAL units.
@ HEVC_NAL_RASL_N
Definition hevc.h:37
@ HEVC_NAL_CRA_NUT
Definition hevc.h:50
@ HEVC_NAL_TRAIL_N
Definition hevc.h:29
@ HEVC_NAL_BLA_W_LP
Definition hevc.h:45
@ HEVC_NAL_BLA_N_LP
Definition hevc.h:47
@ HEVC_NAL_BLA_W_RADL
Definition hevc.h:46
@ HEVC_NAL_TSA_R
Definition hevc.h:32
@ HEVC_NAL_IDR_W_RADL
Definition hevc.h:48
@ HEVC_NAL_IDR_N_LP
Definition hevc.h:49
@ HEVC_NAL_SEI_SUFFIX
Definition hevc.h:69
@ HEVC_NAL_TSA_N
Definition hevc.h:31
@ HEVC_NAL_SEI_PREFIX
Definition hevc.h:68
@ HEVC_NAL_PPS
Definition hevc.h:63
@ HEVC_NAL_VPS
Definition hevc.h:61
@ HEVC_NAL_TRAIL_R
Definition hevc.h:30
@ HEVC_NAL_STSA_N
Definition hevc.h:33
@ HEVC_NAL_STSA_R
Definition hevc.h:34
@ HEVC_NAL_EOB_NUT
Definition hevc.h:66
@ HEVC_NAL_RASL_R
Definition hevc.h:38
@ HEVC_NAL_RADL_R
Definition hevc.h:36
@ HEVC_NAL_SPS
Definition hevc.h:62
@ HEVC_NAL_RADL_N
Definition hevc.h:35
HEVCSliceType
Definition hevc.h:95
@ HEVC_SLICE_P
Definition hevc.h:97
@ HEVC_SLICE_I
Definition hevc.h:98
@ HEVC_SLICE_B
Definition hevc.h:96
@ HEVC_MAX_PPS_COUNT
Definition hevc.h:117
Memory handling functions.
int ff_combine_frame(ParseContext *pc, int next, const uint8_t **buf, int *buf_size)
Combine the (truncated) bitstream to a complete frame.
Definition parser.c:213
#define END_NOT_FOUND
Definition parser.h:40
#define PARSER_CODEC_LIST(...)
#define START_CODE
main external API structure.
Definition avcodec.h:443
AVRational framerate
Definition avcodec.h:563
int level
Encoding level descriptor.
Definition avcodec.h:1646
int profile
profile
Definition avcodec.h:1636
uint8_t * extradata
Out-of-band global headers that may be used by some codecs.
Definition avcodec.h:526
int extradata_size
Definition avcodec.h:527
int err_recognition
Error recognition; may misdetect some more or less valid parts as errors.
Definition avcodec.h:1416
int num
Numerator.
Definition rational.h:59
int den
Denominator.
Definition rational.h:60
Definition ps.h:371
const HEVCPPS * pps_list[HEVC_MAX_PPS_COUNT]
RefStruct references.
Definition ps.h:511
H2645Packet pkt
Definition parser.c:44
int parsed_extradata
Definition parser.c:50
ParseContext pc
Definition parser.c:42
HEVCParamSets ps
Definition parser.c:45
int nal_length_size
Definition parser.c:49
HEVCSEI sei
Definition parser.c:46
Definition sei.h:106
Definition ps.h:252
unsigned int top_offset
Definition ps.h:94
unsigned int right_offset
Definition ps.h:93
unsigned int bottom_offset
Definition ps.h:95
unsigned int left_offset
Definition ps.h:92
uint64_t state64
contains the last 8 bytes in MSB order
Definition parser.h:37
int frame_start_found
Definition parser.h:34
#define av_freep(p)
#define av_log(a,...)