FFmpeg
hevc_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 "hevc_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  H2645Packet pkt = { 0 };
31 
32  ret = ff_h2645_packet_split(&pkt, buf, buf_size, logctx, is_nalff,
33  nal_length_size, AV_CODEC_ID_HEVC, 1, 0);
34  if (ret < 0) {
35  goto done;
36  }
37 
38  for (i = 0; i < pkt.nb_nals; i++) {
39  H2645NAL *nal = &pkt.nals[i];
40 
41  /* ignore everything except parameter sets and VCL NALUs */
42  switch (nal->type) {
43  case HEVC_NAL_VPS:
44  ret = ff_hevc_decode_nal_vps(&nal->gb, logctx, ps);
45  if (ret < 0)
46  goto done;
47  break;
48  case HEVC_NAL_SPS:
49  ret = ff_hevc_decode_nal_sps(&nal->gb, logctx, ps, apply_defdispwin);
50  if (ret < 0)
51  goto done;
52  break;
53  case HEVC_NAL_PPS:
54  ret = ff_hevc_decode_nal_pps(&nal->gb, logctx, ps);
55  if (ret < 0)
56  goto done;
57  break;
60  ret = ff_hevc_decode_nal_sei(&nal->gb, logctx, sei, ps, nal->type);
61  if (ret < 0)
62  goto done;
63  break;
64  default:
65  av_log(logctx, AV_LOG_VERBOSE, "Ignoring NAL type %d in extradata\n", nal->type);
66  break;
67  }
68  }
69 
70 done:
72  if (err_recognition & AV_EF_EXPLODE)
73  return ret;
74 
75  return 0;
76 }
77 
79  HEVCSEI *sei, int *is_nalff, int *nal_length_size,
80  int err_recognition, int apply_defdispwin, void *logctx)
81 {
82  int ret = 0;
83  GetByteContext gb;
84 
85  bytestream2_init(&gb, data, size);
86 
87  if (size > 3 && (data[0] || data[1] || data[2] > 1)) {
88  /* It seems the extradata is encoded as hvcC format.
89  * Temporarily, we support configurationVersion==0 until 14496-15 3rd
90  * is finalized. When finalized, configurationVersion will be 1 and we
91  * can recognize hvcC by checking if avctx->extradata[0]==1 or not. */
92  int i, j, num_arrays, nal_len_size;
93 
94  *is_nalff = 1;
95 
96  bytestream2_skip(&gb, 21);
97  nal_len_size = (bytestream2_get_byte(&gb) & 3) + 1;
98  num_arrays = bytestream2_get_byte(&gb);
99 
100  /* nal units in the hvcC always have length coded with 2 bytes,
101  * so put a fake nal_length_size = 2 while parsing them */
102  *nal_length_size = 2;
103 
104  /* Decode nal units from hvcC. */
105  for (i = 0; i < num_arrays; i++) {
106  int type = bytestream2_get_byte(&gb) & 0x3f;
107  int cnt = bytestream2_get_be16(&gb);
108 
109  for (j = 0; j < cnt; j++) {
110  // +2 for the nal size field
111  int nalsize = bytestream2_peek_be16(&gb) + 2;
112  if (bytestream2_get_bytes_left(&gb) < nalsize) {
113  av_log(logctx, AV_LOG_ERROR,
114  "Invalid NAL unit size in extradata.\n");
115  return AVERROR_INVALIDDATA;
116  }
117 
118  ret = hevc_decode_nal_units(gb.buffer, nalsize, ps, sei, *is_nalff,
119  *nal_length_size, err_recognition, apply_defdispwin,
120  logctx);
121  if (ret < 0) {
122  av_log(logctx, AV_LOG_ERROR,
123  "Decoding nal unit %d %d from hvcC failed\n",
124  type, i);
125  return ret;
126  }
127  bytestream2_skip(&gb, nalsize);
128  }
129  }
130 
131  /* Now store right nal length size, that will be used to parse
132  * all other nals */
133  *nal_length_size = nal_len_size;
134  } else {
135  *is_nalff = 0;
136  ret = hevc_decode_nal_units(data, size, ps, sei, *is_nalff, *nal_length_size,
137  err_recognition, apply_defdispwin, logctx);
138  if (ret < 0)
139  return ret;
140  }
141 
142  return ret;
143 }
h2645_parse.h
GetByteContext
Definition: bytestream.h:33
ff_h2645_packet_uninit
void ff_h2645_packet_uninit(H2645Packet *pkt)
Free all the allocated memory in the packet.
Definition: h2645_parse.c:517
data
const char data[16]
Definition: mxf.c:91
AV_LOG_VERBOSE
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:192
bytestream2_get_bytes_left
static av_always_inline unsigned int bytestream2_get_bytes_left(GetByteContext *g)
Definition: bytestream.h:154
bytestream2_skip
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition: bytestream.h:164
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_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: hevc_parse.c:24
hevc_parse.h
ff_hevc_decode_nal_sei
int ff_hevc_decode_nal_sei(GetBitContext *gb, void *logctx, HEVCSEI *s, const HEVCParamSets *ps, int type)
Definition: hevc_sei.c:353
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
buf
void * buf
Definition: avisynth_c.h:766
GetByteContext::buffer
const uint8_t * buffer
Definition: bytestream.h:34
HEVCSEI
Definition: hevc_sei.h:109
H2645NAL::type
int type
NAL unit type.
Definition: h2645_parse.h:52
HEVC_NAL_PPS
@ HEVC_NAL_PPS
Definition: hevc.h:63
HEVC_NAL_SEI_SUFFIX
@ HEVC_NAL_SEI_SUFFIX
Definition: hevc.h:69
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: hevc_parse.c:78
AV_EF_EXPLODE
#define AV_EF_EXPLODE
abort decoding on minor error detection
Definition: avcodec.h:2705
sei
static int FUNC() sei(CodedBitstreamContext *ctx, RWContext *rw, H264RawSEI *current)
Definition: cbs_h264_syntax_template.c:924
ff_hevc_decode_nal_sps
int ff_hevc_decode_nal_sps(GetBitContext *gb, AVCodecContext *avctx, HEVCParamSets *ps, int apply_defdispwin)
Definition: hevc_ps.c:1224
size
int size
Definition: twinvq_data.h:11134
H2645NAL::gb
GetBitContext gb
Definition: h2645_parse.h:47
H2645NAL
Definition: h2645_parse.h:32
ff_hevc_decode_nal_vps
int ff_hevc_decode_nal_vps(GetBitContext *gb, AVCodecContext *avctx, HEVCParamSets *ps)
Definition: hevc_ps.c:424
ff_h2645_packet_split
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, int use_ref)
Split an input packet into NAL units.
Definition: h2645_parse.c:393
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:259
AV_CODEC_ID_HEVC
@ AV_CODEC_ID_HEVC
Definition: avcodec.h:392
uint8_t
uint8_t
Definition: audio_convert.c:194
hevc.h
HEVC_NAL_VPS
@ HEVC_NAL_VPS
Definition: hevc.h:61
ret
ret
Definition: filter_design.txt:187
pkt
static AVPacket pkt
Definition: demuxing_decoding.c:54
HEVC_NAL_SPS
@ HEVC_NAL_SPS
Definition: hevc.h:62
bytestream.h
bytestream2_init
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:133
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
ff_hevc_decode_nal_pps
int ff_hevc_decode_nal_pps(GetBitContext *gb, AVCodecContext *avctx, HEVCParamSets *ps)
Definition: hevc_ps.c:1473
HEVC_NAL_SEI_PREFIX
@ HEVC_NAL_SEI_PREFIX
Definition: hevc.h:68
H2645Packet
Definition: h2645_parse.h:76
HEVCParamSets
Definition: hevc_ps.h:399