2 * Copyright (c) 2019 James Almer <jamrial@gmail.com>
4 * This file is part of FFmpeg.
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.
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.
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
22 #include "bsf_internal.h"
26 typedef struct AV1FMergeContext {
27 CodedBitstreamContext *cbc;
28 CodedBitstreamFragment frag[2];
33 static void av1_frame_merge_flush(AVBSFContext *bsf)
35 AV1FMergeContext *ctx = bsf->priv_data;
37 ff_cbs_fragment_reset(&ctx->frag[0]);
38 ff_cbs_fragment_reset(&ctx->frag[1]);
39 av_packet_unref(ctx->in);
40 av_packet_unref(ctx->pkt);
43 static int av1_frame_merge_filter(AVBSFContext *bsf, AVPacket *out)
45 AV1FMergeContext *ctx = bsf->priv_data;
46 CodedBitstreamFragment *frag = &ctx->frag[ctx->idx], *tu = &ctx->frag[!ctx->idx];
47 AVPacket *in = ctx->in, *buffer_pkt = ctx->pkt;
50 err = ff_bsf_get_packet_ref(bsf, in);
52 if (err == AVERROR_EOF && tu->nb_units > 0)
57 err = ff_cbs_read_packet(ctx->cbc, frag, in);
59 av_log(bsf, AV_LOG_ERROR, "Failed to read packet.\n");
63 if (frag->nb_units == 0) {
64 av_log(bsf, AV_LOG_ERROR, "No OBU in packet.\n");
65 err = AVERROR_INVALIDDATA;
69 if (tu->nb_units == 0 && frag->units[0].type != AV1_OBU_TEMPORAL_DELIMITER) {
70 av_log(bsf, AV_LOG_ERROR, "Missing Temporal Delimiter.\n");
71 err = AVERROR_INVALIDDATA;
75 for (i = 1; i < frag->nb_units; i++) {
76 if (frag->units[i].type == AV1_OBU_TEMPORAL_DELIMITER) {
77 av_log(bsf, AV_LOG_ERROR, "Temporal Delimiter in the middle of a packet.\n");
78 err = AVERROR_INVALIDDATA;
83 if (tu->nb_units > 0 && frag->units[0].type == AV1_OBU_TEMPORAL_DELIMITER) {
85 err = ff_cbs_write_packet(ctx->cbc, buffer_pkt, tu);
87 av_log(bsf, AV_LOG_ERROR, "Failed to write packet.\n");
90 av_packet_move_ref(out, buffer_pkt);
92 // Swap fragment index, to avoid copying fragment references.
95 for (i = 0; i < frag->nb_units; i++) {
96 err = ff_cbs_insert_unit_content(tu, -1, frag->units[i].type,
97 frag->units[i].content, frag->units[i].content_ref);
102 err = AVERROR(EAGAIN);
105 // Buffer packets with timestamps. There should be at most one per TU, be it split or not.
106 if (!buffer_pkt->data && in->pts != AV_NOPTS_VALUE)
107 av_packet_move_ref(buffer_pkt, in);
111 ff_cbs_fragment_reset(&ctx->frag[ctx->idx]);
114 if (err < 0 && err != AVERROR(EAGAIN))
115 av1_frame_merge_flush(bsf);
120 static int av1_frame_merge_init(AVBSFContext *bsf)
122 AV1FMergeContext *ctx = bsf->priv_data;
124 ctx->in = av_packet_alloc();
125 ctx->pkt = av_packet_alloc();
126 if (!ctx->in || !ctx->pkt)
127 return AVERROR(ENOMEM);
129 return ff_cbs_init(&ctx->cbc, AV_CODEC_ID_AV1, bsf);
132 static void av1_frame_merge_close(AVBSFContext *bsf)
134 AV1FMergeContext *ctx = bsf->priv_data;
136 ff_cbs_fragment_free(&ctx->frag[0]);
137 ff_cbs_fragment_free(&ctx->frag[1]);
138 av_packet_free(&ctx->in);
139 av_packet_free(&ctx->pkt);
140 ff_cbs_close(&ctx->cbc);
143 static const enum AVCodecID av1_frame_merge_codec_ids[] = {
144 AV_CODEC_ID_AV1, AV_CODEC_ID_NONE,
147 const AVBitStreamFilter ff_av1_frame_merge_bsf = {
148 .name = "av1_frame_merge",
149 .priv_data_size = sizeof(AV1FMergeContext),
150 .init = av1_frame_merge_init,
151 .flush = av1_frame_merge_flush,
152 .close = av1_frame_merge_close,
153 .filter = av1_frame_merge_filter,
154 .codec_ids = av1_frame_merge_codec_ids,