GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/av1_frame_merge_bsf.c Lines: 48 73 65.8 %
Date: 2020-10-23 17:01:47 Branches: 27 42 64.3 %

Line Branch Exec Source
1
/*
2
 * Copyright (c) 2019 James Almer <jamrial@gmail.com>
3
 *
4
 * This file is part of FFmpeg.
5
 *
6
 * FFmpeg is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU Lesser General Public
8
 * License as published by the Free Software Foundation; either
9
 * version 2.1 of the License, or (at your option) any later version.
10
 *
11
 * FFmpeg is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
 * Lesser General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public
17
 * License along with FFmpeg; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
 */
20
21
#include "bsf.h"
22
#include "bsf_internal.h"
23
#include "cbs.h"
24
#include "cbs_av1.h"
25
26
typedef struct AV1FMergeContext {
27
    CodedBitstreamContext *input;
28
    CodedBitstreamContext *output;
29
    CodedBitstreamFragment frag[2];
30
    AVPacket *pkt, *in;
31
    int idx;
32
} AV1FMergeContext;
33
34
static void av1_frame_merge_flush(AVBSFContext *bsf)
35
{
36
    AV1FMergeContext *ctx = bsf->priv_data;
37
38
    ff_cbs_fragment_reset(&ctx->frag[0]);
39
    ff_cbs_fragment_reset(&ctx->frag[1]);
40
    av_packet_unref(ctx->in);
41
    av_packet_unref(ctx->pkt);
42
}
43
44
24
static int av1_frame_merge_filter(AVBSFContext *bsf, AVPacket *out)
45
{
46
24
    AV1FMergeContext *ctx = bsf->priv_data;
47
24
    CodedBitstreamFragment *frag = &ctx->frag[ctx->idx], *tu = &ctx->frag[!ctx->idx];
48
24
    AVPacket *in = ctx->in, *buffer_pkt = ctx->pkt;
49
    int err, i;
50
51
24
    err = ff_bsf_get_packet_ref(bsf, in);
52
24
    if (err < 0) {
53

2
        if (err == AVERROR_EOF && tu->nb_units > 0)
54
1
            goto eof;
55
1
        return err;
56
    }
57
58
22
    err = ff_cbs_read_packet(ctx->input, frag, in);
59
22
    if (err < 0) {
60
        av_log(bsf, AV_LOG_ERROR, "Failed to read packet.\n");
61
        goto fail;
62
    }
63
64
22
    if (frag->nb_units == 0) {
65
        av_log(bsf, AV_LOG_ERROR, "No OBU in packet.\n");
66
        err = AVERROR_INVALIDDATA;
67
        goto fail;
68
    }
69
70

22
    if (tu->nb_units == 0 && frag->units[0].type != AV1_OBU_TEMPORAL_DELIMITER) {
71
        av_log(bsf, AV_LOG_ERROR, "Missing Temporal Delimiter.\n");
72
        err = AVERROR_INVALIDDATA;
73
        goto fail;
74
    }
75
76
22
    for (i = 1; i < frag->nb_units; i++) {
77
        if (frag->units[i].type == AV1_OBU_TEMPORAL_DELIMITER) {
78
            av_log(bsf, AV_LOG_ERROR, "Temporal Delimiter in the middle of a packet.\n");
79
            err = AVERROR_INVALIDDATA;
80
            goto fail;
81
        }
82
    }
83
84

22
    if (tu->nb_units > 0 && frag->units[0].type == AV1_OBU_TEMPORAL_DELIMITER) {
85
9
eof:
86
10
        err = ff_cbs_write_packet(ctx->output, buffer_pkt, tu);
87
10
        if (err < 0) {
88
            av_log(bsf, AV_LOG_ERROR, "Failed to write packet.\n");
89
            goto fail;
90
        }
91
10
        av_packet_move_ref(out, buffer_pkt);
92
93
        // Swap fragment index, to avoid copying fragment references.
94
10
        ctx->idx = !ctx->idx;
95
    } else {
96
26
        for (i = 0; i < frag->nb_units; i++) {
97
13
            err = ff_cbs_insert_unit_content(tu, -1, frag->units[i].type,
98
13
                                             frag->units[i].content, frag->units[i].content_ref);
99
13
            if (err < 0)
100
                goto fail;
101
        }
102
103
13
        err = AVERROR(EAGAIN);
104
    }
105
106
    // Buffer packets with timestamps. There should be at most one per TU, be it split or not.
107

23
    if (!buffer_pkt->data && in->pts != AV_NOPTS_VALUE)
108
        av_packet_move_ref(buffer_pkt, in);
109
    else
110
23
        av_packet_unref(in);
111
112
23
    ff_cbs_fragment_reset(&ctx->frag[ctx->idx]);
113
114
23
fail:
115

23
    if (err < 0 && err != AVERROR(EAGAIN))
116
        av1_frame_merge_flush(bsf);
117
118
23
    return err;
119
}
120
121
1
static int av1_frame_merge_init(AVBSFContext *bsf)
122
{
123
1
    AV1FMergeContext *ctx = bsf->priv_data;
124
    int err;
125
126
1
    ctx->in  = av_packet_alloc();
127
1
    ctx->pkt = av_packet_alloc();
128

1
    if (!ctx->in || !ctx->pkt)
129
        return AVERROR(ENOMEM);
130
131
1
    err =  ff_cbs_init(&ctx->input, AV_CODEC_ID_AV1, bsf);
132
1
    if (err < 0)
133
        return err;
134
135
1
    return ff_cbs_init(&ctx->output, AV_CODEC_ID_AV1, bsf);
136
}
137
138
1
static void av1_frame_merge_close(AVBSFContext *bsf)
139
{
140
1
    AV1FMergeContext *ctx = bsf->priv_data;
141
142
1
    ff_cbs_fragment_free(&ctx->frag[0]);
143
1
    ff_cbs_fragment_free(&ctx->frag[1]);
144
1
    av_packet_free(&ctx->in);
145
1
    av_packet_free(&ctx->pkt);
146
1
    ff_cbs_close(&ctx->input);
147
1
    ff_cbs_close(&ctx->output);
148
1
}
149
150
static const enum AVCodecID av1_frame_merge_codec_ids[] = {
151
    AV_CODEC_ID_AV1, AV_CODEC_ID_NONE,
152
};
153
154
const AVBitStreamFilter ff_av1_frame_merge_bsf = {
155
    .name           = "av1_frame_merge",
156
    .priv_data_size = sizeof(AV1FMergeContext),
157
    .init           = av1_frame_merge_init,
158
    .flush          = av1_frame_merge_flush,
159
    .close          = av1_frame_merge_close,
160
    .filter         = av1_frame_merge_filter,
161
    .codec_ids      = av1_frame_merge_codec_ids,
162
};