../../../ffmpeg/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
53 / 84
63.1%
Branches:
30 / 48
62.5%
Functions:
3 / 4
75.0%

libavcodec/bsf/av1_frame_merge.c
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 "libavcodec/bsf.h"
22 #include "libavcodec/bsf_internal.h"
23 #include "libavcodec/cbs.h"
24 #include "libavcodec/cbs_av1.h"
25
26 #define MAX_TU_OBUS (1 << 16)
27
28 typedef struct AV1FMergeContext {
29 CodedBitstreamContext *input;
30 CodedBitstreamContext *output;
31 CodedBitstreamFragment frag[2];
32 AVPacket *pkt, *in;
33 int idx;
34 } AV1FMergeContext;
35
36 ✗ static void av1_frame_merge_flush(AVBSFContext *bsf)
37 {
38 ✗ AV1FMergeContext *ctx = bsf->priv_data;
39
40 ✗ ff_cbs_fragment_reset(&ctx->frag[0]);
41 ✗ ff_cbs_fragment_reset(&ctx->frag[1]);
42 ✗ av_packet_unref(ctx->in);
43 ✗ av_packet_unref(ctx->pkt);
44 ✗ }
45
46 31 static int av1_frame_merge_filter(AVBSFContext *bsf, AVPacket *out)
47 {
48 31 AV1FMergeContext *ctx = bsf->priv_data;
49 31 CodedBitstreamFragment *frag = &ctx->frag[ctx->idx], *tu = &ctx->frag[!ctx->idx];
50 31 AVPacket *in = ctx->in, *buffer_pkt = ctx->pkt;
51 int err, i;
52
53 31 err = ff_bsf_get_packet_ref(bsf, in);
54
2/2
✓ Branch 3 → 4 taken 6 times.
✓ Branch 3 → 8 taken 25 times.
31 if (err < 0) {
55
3/4
✓ Branch 4 → 5 taken 6 times.
✗ Branch 4 → 7 not taken.
✓ Branch 5 → 6 taken 2 times.
✓ Branch 5 → 7 taken 4 times.
6 if (err == AVERROR_EOF && tu->nb_units > 0)
56 2 goto eof;
57 4 return err;
58 }
59
60 25 err = ff_cbs_read_packet(ctx->input, frag, in);
61
1/2
✗ Branch 9 → 10 not taken.
✓ Branch 9 → 12 taken 25 times.
25 if (err < 0) {
62 ✗ av_log(bsf, AV_LOG_ERROR, "Failed to read packet.\n");
63 ✗ goto fail;
64 }
65
66
1/2
✗ Branch 12 → 13 not taken.
✓ Branch 12 → 15 taken 25 times.
25 if (frag->nb_units == 0) {
67 ✗ av_log(bsf, AV_LOG_ERROR, "No OBU in packet.\n");
68 ✗ err = AVERROR_INVALIDDATA;
69 ✗ goto fail;
70 }
71
72
1/2
✗ Branch 15 → 16 not taken.
✓ Branch 15 → 18 taken 25 times.
25 if (frag->nb_units > MAX_TU_OBUS) {
73 ✗ av_log(bsf, AV_LOG_ERROR, "Too many OBUs in a Temporal Unit.\n");
74 ✗ err = AVERROR_INVALIDDATA;
75 ✗ goto fail;
76 }
77
78
3/4
✓ Branch 18 → 19 taken 2 times.
✓ Branch 18 → 22 taken 23 times.
✗ Branch 19 → 20 not taken.
✓ Branch 19 → 22 taken 2 times.
25 if (tu->nb_units == 0 && frag->units[0].type != AV1_OBU_TEMPORAL_DELIMITER) {
79 ✗ av_log(bsf, AV_LOG_ERROR, "Missing Temporal Delimiter.\n");
80 ✗ err = AVERROR_INVALIDDATA;
81 ✗ goto fail;
82 }
83
84
1/2
✗ Branch 27 → 23 not taken.
✓ Branch 27 → 28 taken 25 times.
25 for (i = 1; i < frag->nb_units; i++) {
85
0/2
✗ Branch 23 → 24 not taken.
✗ Branch 23 → 26 not taken.
✗ if (frag->units[i].type == AV1_OBU_TEMPORAL_DELIMITER) {
86 ✗ av_log(bsf, AV_LOG_ERROR, "Temporal Delimiter in the middle of a packet.\n");
87 ✗ err = AVERROR_INVALIDDATA;
88 ✗ goto fail;
89 }
90 }
91
92
4/4
✓ Branch 28 → 29 taken 23 times.
✓ Branch 28 → 37 taken 2 times.
✓ Branch 29 → 30 taken 9 times.
✓ Branch 29 → 37 taken 14 times.
25 if (tu->nb_units > 0 && frag->units[0].type == AV1_OBU_TEMPORAL_DELIMITER) {
93 9 eof:
94 11 err = ff_cbs_write_packet(ctx->output, buffer_pkt, tu);
95
1/2
✗ Branch 32 → 33 not taken.
✓ Branch 32 → 35 taken 11 times.
11 if (err < 0) {
96 ✗ av_log(bsf, AV_LOG_ERROR, "Failed to write packet.\n");
97 ✗ goto fail;
98 }
99 11 av_packet_move_ref(out, buffer_pkt);
100
101 // Swap fragment index, to avoid copying fragment references.
102 11 ctx->idx = !ctx->idx;
103 } else {
104
1/2
✗ Branch 37 → 38 not taken.
✓ Branch 37 → 40 taken 16 times.
16 if (tu->nb_units + frag->nb_units > MAX_TU_OBUS) {
105 ✗ av_log(bsf, AV_LOG_ERROR, "Too many OBUs in a Temporal Unit.\n");
106 ✗ err = AVERROR_INVALIDDATA;
107 ✗ goto fail;
108 }
109
2/2
✓ Branch 45 → 41 taken 16 times.
✓ Branch 45 → 46 taken 16 times.
32 for (i = 0; i < frag->nb_units; i++) {
110 16 err = ff_cbs_insert_unit_content(tu, -1, frag->units[i].type,
111 16 frag->units[i].content, frag->units[i].content_ref);
112
1/2
✗ Branch 42 → 43 not taken.
✓ Branch 42 → 44 taken 16 times.
16 if (err < 0)
113 ✗ goto fail;
114 }
115
116 16 err = AVERROR(EAGAIN);
117 }
118
119 /* Buffer packets with timestamps (there should be at most one per TU)
120 * or any packet if buffer_pkt is empty. The latter is needed to
121 * passthrough positions in case there are no timestamps like with
122 * the raw OBU demuxer. */
123
2/2
✓ Branch 47 → 48 taken 14 times.
✓ Branch 47 → 50 taken 13 times.
27 if (!buffer_pkt->data ||
124
1/4
✗ Branch 48 → 49 not taken.
✓ Branch 48 → 52 taken 14 times.
✗ Branch 49 → 50 not taken.
✗ Branch 49 → 52 not taken.
14 in->pts != AV_NOPTS_VALUE && buffer_pkt->pts == AV_NOPTS_VALUE) {
125 13 av_packet_unref(buffer_pkt);
126 13 av_packet_move_ref(buffer_pkt, in);
127 } else
128 14 av_packet_unref(in);
129
130 27 ff_cbs_fragment_reset(&ctx->frag[ctx->idx]);
131
132 27 fail:
133
3/4
✓ Branch 54 → 55 taken 16 times.
✓ Branch 54 → 57 taken 11 times.
✗ Branch 55 → 56 not taken.
✓ Branch 55 → 57 taken 16 times.
27 if (err < 0 && err != AVERROR(EAGAIN))
134 ✗ av1_frame_merge_flush(bsf);
135
136 27 return err;
137 }
138
139 2 static int av1_frame_merge_init(AVBSFContext *bsf)
140 {
141 2 AV1FMergeContext *ctx = bsf->priv_data;
142 int err;
143
144 2 ctx->in = av_packet_alloc();
145 2 ctx->pkt = av_packet_alloc();
146
2/4
✓ Branch 4 → 5 taken 2 times.
✗ Branch 4 → 6 not taken.
✗ Branch 5 → 6 not taken.
✓ Branch 5 → 7 taken 2 times.
2 if (!ctx->in || !ctx->pkt)
147 ✗ return AVERROR(ENOMEM);
148
149 2 err = ff_cbs_init(&ctx->input, AV_CODEC_ID_AV1, bsf);
150
1/2
✗ Branch 8 → 9 not taken.
✓ Branch 8 → 10 taken 2 times.
2 if (err < 0)
151 ✗ return err;
152
153 2 return ff_cbs_init(&ctx->output, AV_CODEC_ID_AV1, bsf);
154 }
155
156 2 static void av1_frame_merge_close(AVBSFContext *bsf)
157 {
158 2 AV1FMergeContext *ctx = bsf->priv_data;
159
160 2 ff_cbs_fragment_free(&ctx->frag[0]);
161 2 ff_cbs_fragment_free(&ctx->frag[1]);
162 2 av_packet_free(&ctx->in);
163 2 av_packet_free(&ctx->pkt);
164 2 ff_cbs_close(&ctx->input);
165 2 ff_cbs_close(&ctx->output);
166 2 }
167
168 static const enum AVCodecID av1_frame_merge_codec_ids[] = {
169 AV_CODEC_ID_AV1, AV_CODEC_ID_NONE,
170 };
171
172 const FFBitStreamFilter ff_av1_frame_merge_bsf = {
173 .p.name = "av1_frame_merge",
174 .p.codec_ids = av1_frame_merge_codec_ids,
175 .priv_data_size = sizeof(AV1FMergeContext),
176 .init = av1_frame_merge_init,
177 .flush = av1_frame_merge_flush,
178 .close = av1_frame_merge_close,
179 .filter = av1_frame_merge_filter,
180 };
181