2 * This file is part of FFmpeg.
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.
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.
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
19 #include "bytestream.h"
20 #include "h2645_parse.h"
22 #include "hevc_parse.h"
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)
30 H2645Packet pkt = { 0 };
32 ret = ff_h2645_packet_split(&pkt, buf, buf_size, logctx, is_nalff,
33 nal_length_size, AV_CODEC_ID_HEVC, 1, 0);
38 for (i = 0; i < pkt.nb_nals; i++) {
39 H2645NAL *nal = &pkt.nals[i];
41 /* ignore everything except parameter sets and VCL NALUs */
44 ret = ff_hevc_decode_nal_vps(&nal->gb, logctx, ps);
49 ret = ff_hevc_decode_nal_sps(&nal->gb, logctx, ps, apply_defdispwin);
54 ret = ff_hevc_decode_nal_pps(&nal->gb, logctx, ps);
58 case HEVC_NAL_SEI_PREFIX:
59 case HEVC_NAL_SEI_SUFFIX:
60 ret = ff_hevc_decode_nal_sei(&nal->gb, logctx, sei, ps, nal->type);
65 av_log(logctx, AV_LOG_VERBOSE, "Ignoring NAL type %d in extradata\n", nal->type);
71 ff_h2645_packet_uninit(&pkt);
72 if (err_recognition & AV_EF_EXPLODE)
78 int ff_hevc_decode_extradata(const uint8_t *data, int size, HEVCParamSets *ps,
79 HEVCSEI *sei, int *is_nalff, int *nal_length_size,
80 int err_recognition, int apply_defdispwin, void *logctx)
85 bytestream2_init(&gb, data, size);
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;
96 bytestream2_skip(&gb, 21);
97 nal_len_size = (bytestream2_get_byte(&gb) & 3) + 1;
98 num_arrays = bytestream2_get_byte(&gb);
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;
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);
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;
118 ret = hevc_decode_nal_units(gb.buffer, nalsize, ps, sei, *is_nalff,
119 *nal_length_size, err_recognition, apply_defdispwin,
122 av_log(logctx, AV_LOG_ERROR,
123 "Decoding nal unit %d %d from hvcC failed\n",
127 bytestream2_skip(&gb, nalsize);
131 /* Now store right nal length size, that will be used to parse
133 *nal_length_size = nal_len_size;
136 ret = hevc_decode_nal_units(data, size, ps, sei, *is_nalff, *nal_length_size,
137 err_recognition, apply_defdispwin, logctx);