blob: 6c1138e015f1152e2c4339a773ac6575c1bacebb
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, void *logctx) |
26 | { |
27 | int i; |
28 | int ret = 0; |
29 | H2645Packet pkt = { 0 }; |
30 | |
31 | ret = ff_h2645_packet_split(&pkt, buf, buf_size, logctx, is_nalff, nal_length_size, AV_CODEC_ID_HEVC, 1); |
32 | if (ret < 0) { |
33 | goto done; |
34 | } |
35 | |
36 | for (i = 0; i < pkt.nb_nals; i++) { |
37 | H2645NAL *nal = &pkt.nals[i]; |
38 | |
39 | /* ignore everything except parameter sets and VCL NALUs */ |
40 | switch (nal->type) { |
41 | case HEVC_NAL_VPS: ff_hevc_decode_nal_vps(&nal->gb, logctx, ps); break; |
42 | case HEVC_NAL_SPS: ff_hevc_decode_nal_sps(&nal->gb, logctx, ps, 1); break; |
43 | case HEVC_NAL_PPS: ff_hevc_decode_nal_pps(&nal->gb, logctx, ps); break; |
44 | case HEVC_NAL_TRAIL_R: |
45 | case HEVC_NAL_TRAIL_N: |
46 | case HEVC_NAL_TSA_N: |
47 | case HEVC_NAL_TSA_R: |
48 | case HEVC_NAL_STSA_N: |
49 | case HEVC_NAL_STSA_R: |
50 | case HEVC_NAL_BLA_W_LP: |
51 | case HEVC_NAL_BLA_W_RADL: |
52 | case HEVC_NAL_BLA_N_LP: |
53 | case HEVC_NAL_IDR_W_RADL: |
54 | case HEVC_NAL_IDR_N_LP: |
55 | case HEVC_NAL_CRA_NUT: |
56 | case HEVC_NAL_RADL_N: |
57 | case HEVC_NAL_RADL_R: |
58 | case HEVC_NAL_RASL_N: |
59 | case HEVC_NAL_RASL_R: |
60 | av_log(logctx, AV_LOG_ERROR, "Invalid NAL unit: %d\n", nal->type); |
61 | ret = AVERROR_INVALIDDATA; |
62 | goto done; |
63 | break; |
64 | } |
65 | } |
66 | |
67 | done: |
68 | ff_h2645_packet_uninit(&pkt); |
69 | return ret; |
70 | } |
71 | |
72 | int ff_hevc_decode_extradata(const uint8_t *data, int size, HEVCParamSets *ps, |
73 | int *is_nalff, int *nal_length_size, |
74 | int err_recognition, void *logctx) |
75 | { |
76 | int ret = 0; |
77 | GetByteContext gb; |
78 | |
79 | bytestream2_init(&gb, data, size); |
80 | |
81 | if (size > 3 && (data[0] || data[1] || data[2] > 1)) { |
82 | /* It seems the extradata is encoded as hvcC format. |
83 | * Temporarily, we support configurationVersion==0 until 14496-15 3rd |
84 | * is finalized. When finalized, configurationVersion will be 1 and we |
85 | * can recognize hvcC by checking if avctx->extradata[0]==1 or not. */ |
86 | int i, j, num_arrays, nal_len_size; |
87 | |
88 | *is_nalff = 1; |
89 | |
90 | bytestream2_skip(&gb, 21); |
91 | nal_len_size = (bytestream2_get_byte(&gb) & 3) + 1; |
92 | num_arrays = bytestream2_get_byte(&gb); |
93 | |
94 | /* nal units in the hvcC always have length coded with 2 bytes, |
95 | * so put a fake nal_length_size = 2 while parsing them */ |
96 | *nal_length_size = 2; |
97 | |
98 | /* Decode nal units from hvcC. */ |
99 | for (i = 0; i < num_arrays; i++) { |
100 | int type = bytestream2_get_byte(&gb) & 0x3f; |
101 | int cnt = bytestream2_get_be16(&gb); |
102 | |
103 | for (j = 0; j < cnt; j++) { |
104 | // +2 for the nal size field |
105 | int nalsize = bytestream2_peek_be16(&gb) + 2; |
106 | if (bytestream2_get_bytes_left(&gb) < nalsize) { |
107 | av_log(logctx, AV_LOG_ERROR, |
108 | "Invalid NAL unit size in extradata.\n"); |
109 | return AVERROR_INVALIDDATA; |
110 | } |
111 | |
112 | ret = hevc_decode_nal_units(gb.buffer, nalsize, ps, *is_nalff, *nal_length_size, 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, logctx); |
129 | if (ret < 0) |
130 | return ret; |
131 | } |
132 | |
133 | return ret; |
134 | } |
135 |