GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/cbs_bsf.c Lines: 55 89 61.8 %
Date: 2021-04-20 15:25:36 Branches: 18 36 50.0 %

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 "bsf_internal.h"
20
#include "cbs_bsf.h"
21
22
5032
static int cbs_bsf_update_side_data(AVBSFContext *bsf, AVPacket *pkt)
23
{
24
5032
    CBSBSFContext           *ctx = bsf->priv_data;
25
5032
    CodedBitstreamFragment *frag = &ctx->fragment;
26
    uint8_t *side_data;
27
    buffer_size_t side_data_size;
28
    int err;
29
30
5032
    side_data = av_packet_get_side_data(pkt, AV_PKT_DATA_NEW_EXTRADATA,
31
                                        &side_data_size);
32
5032
    if (!side_data_size)
33
5032
        return 0;
34
35
    err = ff_cbs_read(ctx->input, frag, side_data, side_data_size);
36
    if (err < 0) {
37
        av_log(bsf, AV_LOG_ERROR,
38
               "Failed to read extradata from packet side data.\n");
39
        return err;
40
    }
41
42
    err = ctx->type->update_fragment(bsf, NULL, frag);
43
    if (err < 0)
44
        return err;
45
46
    err = ff_cbs_write_fragment_data(ctx->output, frag);
47
    if (err < 0) {
48
        av_log(bsf, AV_LOG_ERROR,
49
               "Failed to write extradata into packet side data.\n");
50
        return err;
51
    }
52
53
    side_data = av_packet_new_side_data(pkt, AV_PKT_DATA_NEW_EXTRADATA,
54
                                        frag->data_size);
55
    if (!side_data)
56
        return AVERROR(ENOMEM);
57
    memcpy(side_data, frag->data, frag->data_size);
58
59
    ff_cbs_fragment_reset(frag);
60
    return 0;
61
}
62
63
10063
int ff_cbs_bsf_generic_filter(AVBSFContext *bsf, AVPacket *pkt)
64
{
65
10063
    CBSBSFContext           *ctx = bsf->priv_data;
66
10063
    CodedBitstreamFragment *frag = &ctx->fragment;
67
    int err;
68
69
10063
    err = ff_bsf_get_packet_ref(bsf, pkt);
70
10063
    if (err < 0)
71
5031
        return err;
72
73
5032
    err = cbs_bsf_update_side_data(bsf, pkt);
74
5032
    if (err < 0)
75
        goto fail;
76
77
5032
    err = ff_cbs_read_packet(ctx->input, frag, pkt);
78
5032
    if (err < 0) {
79
1
        av_log(bsf, AV_LOG_ERROR, "Failed to read %s from packet.\n",
80
1
               ctx->type->fragment_name);
81
1
        goto fail;
82
    }
83
84
5031
    if (frag->nb_units == 0) {
85
        av_log(bsf, AV_LOG_ERROR, "No %s found in packet.\n",
86
               ctx->type->unit_name);
87
        err = AVERROR_INVALIDDATA;
88
        goto fail;
89
    }
90
91
5031
    err = ctx->type->update_fragment(bsf, pkt, frag);
92
5031
    if (err < 0)
93
        goto fail;
94
95
5031
    err = ff_cbs_write_packet(ctx->output, pkt, frag);
96
5031
    if (err < 0) {
97
        av_log(bsf, AV_LOG_ERROR, "Failed to write %s into packet.\n",
98
               ctx->type->fragment_name);
99
        goto fail;
100
    }
101
102
5031
    err = 0;
103
5032
fail:
104
5032
    ff_cbs_fragment_reset(frag);
105
106
5032
    if (err < 0)
107
1
        av_packet_unref(pkt);
108
109
5032
    return err;
110
}
111
112
69
int ff_cbs_bsf_generic_init(AVBSFContext *bsf, const CBSBSFType *type)
113
{
114
69
    CBSBSFContext           *ctx = bsf->priv_data;
115
69
    CodedBitstreamFragment *frag = &ctx->fragment;
116
    int err;
117
118
69
    ctx->type = type;
119
120
69
    err = ff_cbs_init(&ctx->input, type->codec_id, bsf);
121
69
    if (err < 0)
122
        return err;
123
124
69
    err = ff_cbs_init(&ctx->output, type->codec_id, bsf);
125
69
    if (err < 0)
126
        return err;
127
128
69
    if (bsf->par_in->extradata) {
129
55
        err = ff_cbs_read_extradata(ctx->input, frag, bsf->par_in);
130
55
        if (err < 0) {
131
            av_log(bsf, AV_LOG_ERROR, "Failed to read extradata.\n");
132
            goto fail;
133
        }
134
135
55
        err = type->update_fragment(bsf, NULL, frag);
136
55
        if (err < 0)
137
            goto fail;
138
139
55
        err = ff_cbs_write_extradata(ctx->output, bsf->par_out, frag);
140
55
        if (err < 0) {
141
            av_log(bsf, AV_LOG_ERROR, "Failed to write extradata.\n");
142
            goto fail;
143
        }
144
    }
145
146
69
    err = 0;
147
69
fail:
148
69
    ff_cbs_fragment_reset(frag);
149
69
    return err;
150
}
151
152
69
void ff_cbs_bsf_generic_close(AVBSFContext *bsf)
153
{
154
69
    CBSBSFContext *ctx = bsf->priv_data;
155
156
69
    ff_cbs_fragment_free(&ctx->fragment);
157
69
    ff_cbs_close(&ctx->input);
158
69
    ff_cbs_close(&ctx->output);
159
69
}