]> git.sesse.net Git - ffmpeg/blob - libavcodec/hevc_parse.c
Merge commit '5d45fe7da972da528915fbdfe3dbf22eb2effd01'
[ffmpeg] / libavcodec / hevc_parse.c
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                                  int is_nalff, int nal_length_size, int err_recognition,
26                                  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, nal_length_size, AV_CODEC_ID_HEVC, 1);
33     if (ret < 0) {
34         goto done;
35     }
36
37     for (i = 0; i < pkt.nb_nals; i++) {
38         H2645NAL *nal = &pkt.nals[i];
39
40         /* ignore everything except parameter sets and VCL NALUs */
41         switch (nal->type) {
42         case HEVC_NAL_VPS:
43             ret = ff_hevc_decode_nal_vps(&nal->gb, logctx, ps);
44             if (ret < 0)
45                 goto done;
46             break;
47         case HEVC_NAL_SPS:
48             ret = ff_hevc_decode_nal_sps(&nal->gb, logctx, ps, apply_defdispwin);
49             if (ret < 0)
50                 goto done;
51             break;
52         case HEVC_NAL_PPS:
53             ret = ff_hevc_decode_nal_pps(&nal->gb, logctx, ps);
54             if (ret < 0)
55                 goto done;
56             break;
57         default:
58             av_log(logctx, AV_LOG_VERBOSE, "Ignoring NAL type %d in extradata\n", nal->type);
59             break;
60         }
61     }
62
63 done:
64     ff_h2645_packet_uninit(&pkt);
65     if (err_recognition & AV_EF_EXPLODE)
66         return ret;
67
68     return 0;
69 }
70
71 int ff_hevc_decode_extradata(const uint8_t *data, int size, HEVCParamSets *ps,
72                              int *is_nalff, int *nal_length_size, int err_recognition,
73                              int apply_defdispwin, void *logctx)
74 {
75     int ret = 0;
76     GetByteContext gb;
77
78     bytestream2_init(&gb, data, size);
79
80     if (size > 3 && (data[0] || data[1] || data[2] > 1)) {
81         /* It seems the extradata is encoded as hvcC format.
82          * Temporarily, we support configurationVersion==0 until 14496-15 3rd
83          * is finalized. When finalized, configurationVersion will be 1 and we
84          * can recognize hvcC by checking if avctx->extradata[0]==1 or not. */
85         int i, j, num_arrays, nal_len_size;
86
87         *is_nalff = 1;
88
89         bytestream2_skip(&gb, 21);
90         nal_len_size = (bytestream2_get_byte(&gb) & 3) + 1;
91         num_arrays   = bytestream2_get_byte(&gb);
92
93         /* nal units in the hvcC always have length coded with 2 bytes,
94          * so put a fake nal_length_size = 2 while parsing them */
95         *nal_length_size = 2;
96
97         /* Decode nal units from hvcC. */
98         for (i = 0; i < num_arrays; i++) {
99             int type = bytestream2_get_byte(&gb) & 0x3f;
100             int cnt  = bytestream2_get_be16(&gb);
101
102             for (j = 0; j < cnt; j++) {
103                 // +2 for the nal size field
104                 int nalsize = bytestream2_peek_be16(&gb) + 2;
105                 if (bytestream2_get_bytes_left(&gb) < nalsize) {
106                     av_log(logctx, AV_LOG_ERROR,
107                            "Invalid NAL unit size in extradata.\n");
108                     return AVERROR_INVALIDDATA;
109                 }
110
111                 ret = hevc_decode_nal_units(gb.buffer, nalsize, ps, *is_nalff, *nal_length_size,
112                                             err_recognition, apply_defdispwin, logctx);
113                 if (ret < 0) {
114                     av_log(logctx, AV_LOG_ERROR,
115                            "Decoding nal unit %d %d from hvcC failed\n",
116                            type, i);
117                     return ret;
118                 }
119                 bytestream2_skip(&gb, nalsize);
120             }
121         }
122
123         /* Now store right nal length size, that will be used to parse
124          * all other nals */
125         *nal_length_size = nal_len_size;
126     } else {
127         *is_nalff = 0;
128         ret = hevc_decode_nal_units(data, size, ps, *is_nalff, *nal_length_size,
129                                     err_recognition, apply_defdispwin, logctx);
130         if (ret < 0)
131             return ret;
132     }
133
134     return ret;
135 }