GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/hevc_parse.c Lines: 53 67 79.1 %
Date: 2020-01-28 04:56:05 Branches: 26 39 66.7 %

Line Branch Exec Source
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
380
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
380
    int ret = 0;
30
380
    H2645Packet pkt = { 0 };
31
32
380
    ret = ff_h2645_packet_split(&pkt, buf, buf_size, logctx, is_nalff,
33
                                nal_length_size, AV_CODEC_ID_HEVC, 1, 0);
34
380
    if (ret < 0) {
35
        goto done;
36
    }
37
38
1620
    for (i = 0; i < pkt.nb_nals; i++) {
39
1241
        H2645NAL *nal = &pkt.nals[i];
40
1241
        if (nal->nuh_layer_id > 0)
41
            continue;
42
43
        /* ignore everything except parameter sets and VCL NALUs */
44

1241
        switch (nal->type) {
45
372
        case HEVC_NAL_VPS:
46
372
            ret = ff_hevc_decode_nal_vps(&nal->gb, logctx, ps);
47
372
            if (ret < 0)
48
                goto done;
49
372
            break;
50
369
        case HEVC_NAL_SPS:
51
369
            ret = ff_hevc_decode_nal_sps(&nal->gb, logctx, ps, apply_defdispwin);
52
369
            if (ret < 0)
53
                goto done;
54
369
            break;
55
496
        case HEVC_NAL_PPS:
56
496
            ret = ff_hevc_decode_nal_pps(&nal->gb, logctx, ps);
57
496
            if (ret < 0)
58
1
                goto done;
59
495
            break;
60
4
        case HEVC_NAL_SEI_PREFIX:
61
        case HEVC_NAL_SEI_SUFFIX:
62
4
            ret = ff_hevc_decode_nal_sei(&nal->gb, logctx, sei, ps, nal->type);
63
4
            if (ret < 0)
64
                goto done;
65
4
            break;
66
        default:
67
            av_log(logctx, AV_LOG_VERBOSE, "Ignoring NAL type %d in extradata\n", nal->type);
68
            break;
69
        }
70
    }
71
72
379
done:
73
380
    ff_h2645_packet_uninit(&pkt);
74
380
    if (err_recognition & AV_EF_EXPLODE)
75
        return ret;
76
77
380
    return 0;
78
}
79
80
356
int ff_hevc_decode_extradata(const uint8_t *data, int size, HEVCParamSets *ps,
81
                             HEVCSEI *sei, int *is_nalff, int *nal_length_size,
82
                             int err_recognition, int apply_defdispwin, void *logctx)
83
{
84
356
    int ret = 0;
85
    GetByteContext gb;
86
87
356
    bytestream2_init(&gb, data, size);
88
89


356
    if (size > 3 && (data[0] || data[1] || data[2] > 1)) {
90
        /* It seems the extradata is encoded as hvcC format.
91
         * Temporarily, we support configurationVersion==0 until 14496-15 3rd
92
         * is finalized. When finalized, configurationVersion will be 1 and we
93
         * can recognize hvcC by checking if avctx->extradata[0]==1 or not. */
94
        int i, j, num_arrays, nal_len_size;
95
96
10
        *is_nalff = 1;
97
98
10
        bytestream2_skip(&gb, 21);
99
10
        nal_len_size = (bytestream2_get_byte(&gb) & 3) + 1;
100
10
        num_arrays   = bytestream2_get_byte(&gb);
101
102
        /* nal units in the hvcC always have length coded with 2 bytes,
103
         * so put a fake nal_length_size = 2 while parsing them */
104
10
        *nal_length_size = 2;
105
106
        /* Decode nal units from hvcC. */
107
44
        for (i = 0; i < num_arrays; i++) {
108
34
            int type = bytestream2_get_byte(&gb) & 0x3f;
109
34
            int cnt  = bytestream2_get_be16(&gb);
110
111
68
            for (j = 0; j < cnt; j++) {
112
                // +2 for the nal size field
113
34
                int nalsize = bytestream2_peek_be16(&gb) + 2;
114
34
                if (bytestream2_get_bytes_left(&gb) < nalsize) {
115
                    av_log(logctx, AV_LOG_ERROR,
116
                           "Invalid NAL unit size in extradata.\n");
117
                    return AVERROR_INVALIDDATA;
118
                }
119
120
34
                ret = hevc_decode_nal_units(gb.buffer, nalsize, ps, sei, *is_nalff,
121
                                            *nal_length_size, err_recognition, apply_defdispwin,
122
                                            logctx);
123
34
                if (ret < 0) {
124
                    av_log(logctx, AV_LOG_ERROR,
125
                           "Decoding nal unit %d %d from hvcC failed\n",
126
                           type, i);
127
                    return ret;
128
                }
129
34
                bytestream2_skip(&gb, nalsize);
130
            }
131
        }
132
133
        /* Now store right nal length size, that will be used to parse
134
         * all other nals */
135
10
        *nal_length_size = nal_len_size;
136
    } else {
137
346
        *is_nalff = 0;
138
346
        ret = hevc_decode_nal_units(data, size, ps, sei, *is_nalff, *nal_length_size,
139
                                    err_recognition, apply_defdispwin, logctx);
140
346
        if (ret < 0)
141
            return ret;
142
    }
143
144
356
    return ret;
145
}