FFmpeg
parse.c
Go to the documentation of this file.
1 /*
2  * This file is part of FFmpeg.
3  *
4  * FFmpeg is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * FFmpeg is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with FFmpeg; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 #include "bytestream.h"
20 #include "h2645_parse.h"
21 #include "hevc.h"
22 #include "parse.h"
23 
24 static int hevc_decode_nal_units(const uint8_t *buf, int buf_size, HEVCParamSets *ps,
25  HEVCSEI *sei, int is_nalff, int nal_length_size,
26  int err_recognition, int apply_defdispwin, void *logctx)
27 {
28  int i;
29  int ret = 0;
30  int flags = (H2645_FLAG_IS_NALFF * !!is_nalff) | H2645_FLAG_SMALL_PADDING;
31  H2645Packet pkt = { 0 };
32 
33  ret = ff_h2645_packet_split(&pkt, buf, buf_size, logctx,
34  nal_length_size, AV_CODEC_ID_HEVC, flags);
35  if (ret < 0) {
36  goto done;
37  }
38 
39  for (i = 0; i < pkt.nb_nals; i++) {
40  H2645NAL *nal = &pkt.nals[i];
41  if (nal->nuh_layer_id > 0)
42  continue;
43 
44  /* ignore everything except parameter sets and VCL NALUs */
45  switch (nal->type) {
46  case HEVC_NAL_VPS:
47  ret = ff_hevc_decode_nal_vps(&nal->gb, logctx, ps);
48  if (ret < 0)
49  goto done;
50  break;
51  case HEVC_NAL_SPS:
52  ret = ff_hevc_decode_nal_sps(&nal->gb, logctx, ps, apply_defdispwin);
53  if (ret < 0)
54  goto done;
55  break;
56  case HEVC_NAL_PPS:
57  ret = ff_hevc_decode_nal_pps(&nal->gb, logctx, ps);
58  if (ret < 0)
59  goto done;
60  break;
63  ret = ff_hevc_decode_nal_sei(&nal->gb, logctx, sei, ps, nal->type);
64  if (ret < 0)
65  goto done;
66  break;
67  default:
68  av_log(logctx, AV_LOG_VERBOSE, "Ignoring NAL type %d in extradata\n", nal->type);
69  break;
70  }
71  }
72 
73 done:
75  if (err_recognition & AV_EF_EXPLODE)
76  return ret;
77 
78  return 0;
79 }
80 
81 int ff_hevc_decode_extradata(const uint8_t *data, int size, HEVCParamSets *ps,
82  HEVCSEI *sei, int *is_nalff, int *nal_length_size,
83  int err_recognition, int apply_defdispwin, void *logctx)
84 {
85  int ret = 0;
86  GetByteContext gb;
87 
88  bytestream2_init(&gb, data, size);
89 
90  /* data[0] == 1 is configurationVersion from 14496-15.
91  * data[0] == 0 is for backward compatibility predates the standard.
92  *
93  * Minimum number of bytes of hvcC with 0 numOfArrays is 23.
94  */
95  if (size >= 23 && ((data[0] == 1) || (data[0] == 0 && (data[1] || data[2] > 1)))) {
96  /* It seems the extradata is encoded as hvcC format. */
97  int i, j, num_arrays, nal_len_size;
98 
99  *is_nalff = 1;
100 
101  bytestream2_skip(&gb, 21);
102  nal_len_size = (bytestream2_get_byte(&gb) & 3) + 1;
103  num_arrays = bytestream2_get_byte(&gb);
104 
105  /* nal units in the hvcC always have length coded with 2 bytes,
106  * so put a fake nal_length_size = 2 while parsing them */
107  *nal_length_size = 2;
108 
109  /* Decode nal units from hvcC. */
110  for (i = 0; i < num_arrays; i++) {
111  int type = bytestream2_get_byte(&gb) & 0x3f;
112  int cnt = bytestream2_get_be16(&gb);
113 
114  for (j = 0; j < cnt; j++) {
115  // +2 for the nal size field
116  int nalsize = bytestream2_peek_be16(&gb) + 2;
117  if (bytestream2_get_bytes_left(&gb) < nalsize) {
118  av_log(logctx, AV_LOG_ERROR,
119  "Invalid NAL unit size in extradata.\n");
120  return AVERROR_INVALIDDATA;
121  }
122 
123  ret = hevc_decode_nal_units(gb.buffer, nalsize, ps, sei, *is_nalff,
124  *nal_length_size, err_recognition, apply_defdispwin,
125  logctx);
126  if (ret < 0) {
127  av_log(logctx, AV_LOG_ERROR,
128  "Decoding nal unit %d %d from hvcC failed\n",
129  type, i);
130  return ret;
131  }
132  bytestream2_skip(&gb, nalsize);
133  }
134  }
135 
136  /* Now store right nal length size, that will be used to parse
137  * all other nals */
138  *nal_length_size = nal_len_size;
139  } else {
140  *is_nalff = 0;
141  ret = hevc_decode_nal_units(data, size, ps, sei, *is_nalff, *nal_length_size,
142  err_recognition, apply_defdispwin, logctx);
143  if (ret < 0)
144  return ret;
145  }
146 
147  return ret;
148 }
ff_hevc_decode_extradata
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:81
AV_EF_EXPLODE
#define AV_EF_EXPLODE
abort decoding on minor error detection
Definition: defs.h:51
h2645_parse.h
GetByteContext
Definition: bytestream.h:33
H2645NAL::nuh_layer_id
int nuh_layer_id
Definition: h2645_parse.h:67
ff_h2645_packet_uninit
void ff_h2645_packet_uninit(H2645Packet *pkt)
Free all the allocated memory in the packet.
Definition: h2645_parse.c:600
data
const char data[16]
Definition: mxf.c:148
hevc_decode_nal_units
static int hevc_decode_nal_units(const uint8_t *buf, int buf_size, HEVCParamSets *ps, HEVCSEI *sei, int is_nalff, int nal_length_size, int err_recognition, int apply_defdispwin, void *logctx)
Definition: parse.c:24
AV_LOG_VERBOSE
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:196
ff_hevc_decode_nal_sps
int ff_hevc_decode_nal_sps(GetBitContext *gb, AVCodecContext *avctx, HEVCParamSets *ps, int apply_defdispwin)
Definition: ps.c:1301
bytestream2_skip
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition: bytestream.h:168
HEVC_NAL_SEI_SUFFIX
@ HEVC_NAL_SEI_SUFFIX
Definition: hevc.h:69
type
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf type
Definition: writing_filters.txt:86
HEVC_NAL_SEI_PREFIX
@ HEVC_NAL_SEI_PREFIX
Definition: hevc.h:68
pkt
AVPacket * pkt
Definition: movenc.c:60
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
GetByteContext::buffer
const uint8_t * buffer
Definition: bytestream.h:34
HEVCSEI
Definition: sei.h:82
HEVC_NAL_VPS
@ HEVC_NAL_VPS
Definition: hevc.h:61
hevc.h
H2645NAL::type
int type
NAL unit type.
Definition: h2645_parse.h:52
ff_hevc_decode_nal_vps
int ff_hevc_decode_nal_vps(GetBitContext *gb, AVCodecContext *avctx, HEVCParamSets *ps)
Definition: ps.c:447
parse.h
ff_hevc_decode_nal_pps
int ff_hevc_decode_nal_pps(GetBitContext *gb, AVCodecContext *avctx, HEVCParamSets *ps)
Definition: ps.c:1762
sei
static int FUNC() sei(CodedBitstreamContext *ctx, RWContext *rw, H264RawSEI *current)
Definition: cbs_h264_syntax_template.c:858
bytestream2_get_bytes_left
static av_always_inline int bytestream2_get_bytes_left(GetByteContext *g)
Definition: bytestream.h:158
H2645_FLAG_SMALL_PADDING
@ H2645_FLAG_SMALL_PADDING
Definition: h2645_parse.h:98
HEVC_NAL_SPS
@ HEVC_NAL_SPS
Definition: hevc.h:62
size
int size
Definition: twinvq_data.h:10344
H2645NAL::gb
GetBitContext gb
Definition: h2645_parse.h:47
H2645NAL
Definition: h2645_parse.h:34
HEVC_NAL_PPS
@ HEVC_NAL_PPS
Definition: hevc.h:63
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
AV_CODEC_ID_HEVC
@ AV_CODEC_ID_HEVC
Definition: codec_id.h:226
ret
ret
Definition: filter_design.txt:187
H2645_FLAG_IS_NALFF
@ H2645_FLAG_IS_NALFF
Definition: h2645_parse.h:97
bytestream.h
bytestream2_init
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:137
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:482
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
ff_h2645_packet_split
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.
Definition: h2645_parse.c:465
H2645Packet
Definition: h2645_parse.h:82
ff_hevc_decode_nal_sei
int ff_hevc_decode_nal_sei(GetBitContext *gb, void *logctx, HEVCSEI *s, const HEVCParamSets *ps, enum HEVCNALUnitType type)
Definition: sei.c:227
HEVCParamSets
Definition: ps.h:446