2019-11-11 00:09:02 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2019 James Almer <jamrial@gmail.com>
|
|
|
|
*
|
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
2020-04-07 14:41:48 +00:00
|
|
|
#include "bsf.h"
|
2020-04-07 14:49:28 +00:00
|
|
|
#include "bsf_internal.h"
|
2019-11-11 00:09:02 +00:00
|
|
|
#include "cbs.h"
|
|
|
|
#include "cbs_av1.h"
|
|
|
|
|
|
|
|
typedef struct AV1FMergeContext {
|
2022-05-23 03:23:35 +00:00
|
|
|
CodedBitstreamContext *input;
|
|
|
|
CodedBitstreamContext *output;
|
2019-11-11 00:09:02 +00:00
|
|
|
CodedBitstreamFragment frag[2];
|
|
|
|
AVPacket *pkt, *in;
|
|
|
|
int idx;
|
|
|
|
} AV1FMergeContext;
|
|
|
|
|
|
|
|
static void av1_frame_merge_flush(AVBSFContext *bsf)
|
|
|
|
{
|
|
|
|
AV1FMergeContext *ctx = bsf->priv_data;
|
|
|
|
|
2022-05-23 03:23:35 +00:00
|
|
|
ff_cbs_fragment_reset(&ctx->frag[0]);
|
|
|
|
ff_cbs_fragment_reset(&ctx->frag[1]);
|
2019-11-11 00:09:02 +00:00
|
|
|
av_packet_unref(ctx->in);
|
|
|
|
av_packet_unref(ctx->pkt);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int av1_frame_merge_filter(AVBSFContext *bsf, AVPacket *out)
|
|
|
|
{
|
|
|
|
AV1FMergeContext *ctx = bsf->priv_data;
|
|
|
|
CodedBitstreamFragment *frag = &ctx->frag[ctx->idx], *tu = &ctx->frag[!ctx->idx];
|
|
|
|
AVPacket *in = ctx->in, *buffer_pkt = ctx->pkt;
|
|
|
|
int err, i;
|
|
|
|
|
|
|
|
err = ff_bsf_get_packet_ref(bsf, in);
|
|
|
|
if (err < 0) {
|
|
|
|
if (err == AVERROR_EOF && tu->nb_units > 0)
|
|
|
|
goto eof;
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2022-05-23 03:23:35 +00:00
|
|
|
err = ff_cbs_read_packet(ctx->input, frag, in);
|
2019-11-11 00:09:02 +00:00
|
|
|
if (err < 0) {
|
|
|
|
av_log(bsf, AV_LOG_ERROR, "Failed to read packet.\n");
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (frag->nb_units == 0) {
|
|
|
|
av_log(bsf, AV_LOG_ERROR, "No OBU in packet.\n");
|
|
|
|
err = AVERROR_INVALIDDATA;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tu->nb_units == 0 && frag->units[0].type != AV1_OBU_TEMPORAL_DELIMITER) {
|
|
|
|
av_log(bsf, AV_LOG_ERROR, "Missing Temporal Delimiter.\n");
|
|
|
|
err = AVERROR_INVALIDDATA;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 1; i < frag->nb_units; i++) {
|
|
|
|
if (frag->units[i].type == AV1_OBU_TEMPORAL_DELIMITER) {
|
|
|
|
av_log(bsf, AV_LOG_ERROR, "Temporal Delimiter in the middle of a packet.\n");
|
|
|
|
err = AVERROR_INVALIDDATA;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tu->nb_units > 0 && frag->units[0].type == AV1_OBU_TEMPORAL_DELIMITER) {
|
|
|
|
eof:
|
2022-05-23 03:23:35 +00:00
|
|
|
err = ff_cbs_write_packet(ctx->output, buffer_pkt, tu);
|
2019-11-11 00:09:02 +00:00
|
|
|
if (err < 0) {
|
|
|
|
av_log(bsf, AV_LOG_ERROR, "Failed to write packet.\n");
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
av_packet_move_ref(out, buffer_pkt);
|
|
|
|
|
|
|
|
// Swap fragment index, to avoid copying fragment references.
|
|
|
|
ctx->idx = !ctx->idx;
|
|
|
|
} else {
|
|
|
|
for (i = 0; i < frag->nb_units; i++) {
|
2022-05-23 03:23:35 +00:00
|
|
|
err = ff_cbs_insert_unit_content(tu, -1, frag->units[i].type,
|
2019-11-11 00:09:02 +00:00
|
|
|
frag->units[i].content, frag->units[i].content_ref);
|
|
|
|
if (err < 0)
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = AVERROR(EAGAIN);
|
|
|
|
}
|
|
|
|
|
2024-04-25 09:18:18 +00:00
|
|
|
/* Buffer packets with timestamps (there should be at most one per TU)
|
|
|
|
* or any packet if buffer_pkt is empty. The latter is needed to
|
|
|
|
* passthrough positions in case there are no timestamps like with
|
|
|
|
* the raw OBU demuxer. */
|
|
|
|
if (!buffer_pkt->data ||
|
|
|
|
in->pts != AV_NOPTS_VALUE && buffer_pkt->pts == AV_NOPTS_VALUE) {
|
|
|
|
av_packet_unref(buffer_pkt);
|
2019-11-11 00:09:02 +00:00
|
|
|
av_packet_move_ref(buffer_pkt, in);
|
2024-04-25 09:18:18 +00:00
|
|
|
} else
|
2019-11-11 00:09:02 +00:00
|
|
|
av_packet_unref(in);
|
|
|
|
|
2022-05-23 03:23:35 +00:00
|
|
|
ff_cbs_fragment_reset(&ctx->frag[ctx->idx]);
|
2019-11-11 00:09:02 +00:00
|
|
|
|
|
|
|
fail:
|
|
|
|
if (err < 0 && err != AVERROR(EAGAIN))
|
|
|
|
av1_frame_merge_flush(bsf);
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int av1_frame_merge_init(AVBSFContext *bsf)
|
|
|
|
{
|
|
|
|
AV1FMergeContext *ctx = bsf->priv_data;
|
2022-05-23 03:23:35 +00:00
|
|
|
int err;
|
2019-11-11 00:09:02 +00:00
|
|
|
|
|
|
|
ctx->in = av_packet_alloc();
|
|
|
|
ctx->pkt = av_packet_alloc();
|
|
|
|
if (!ctx->in || !ctx->pkt)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
2022-05-23 03:23:35 +00:00
|
|
|
err = ff_cbs_init(&ctx->input, AV_CODEC_ID_AV1, bsf);
|
|
|
|
if (err < 0)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
return ff_cbs_init(&ctx->output, AV_CODEC_ID_AV1, bsf);
|
2019-11-11 00:09:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void av1_frame_merge_close(AVBSFContext *bsf)
|
|
|
|
{
|
|
|
|
AV1FMergeContext *ctx = bsf->priv_data;
|
|
|
|
|
2022-05-23 03:23:35 +00:00
|
|
|
ff_cbs_fragment_free(&ctx->frag[0]);
|
|
|
|
ff_cbs_fragment_free(&ctx->frag[1]);
|
2019-11-11 00:09:02 +00:00
|
|
|
av_packet_free(&ctx->in);
|
|
|
|
av_packet_free(&ctx->pkt);
|
2022-05-23 03:23:35 +00:00
|
|
|
ff_cbs_close(&ctx->input);
|
|
|
|
ff_cbs_close(&ctx->output);
|
2019-11-11 00:09:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static const enum AVCodecID av1_frame_merge_codec_ids[] = {
|
|
|
|
AV_CODEC_ID_AV1, AV_CODEC_ID_NONE,
|
|
|
|
};
|
|
|
|
|
2024-04-25 09:18:18 +00:00
|
|
|
const FFBitStreamFilter ff_av1_frame_merge_bsf = {
|
|
|
|
.p.name = "av1_frame_merge",
|
|
|
|
.p.codec_ids = av1_frame_merge_codec_ids,
|
2019-11-11 00:09:02 +00:00
|
|
|
.priv_data_size = sizeof(AV1FMergeContext),
|
|
|
|
.init = av1_frame_merge_init,
|
|
|
|
.flush = av1_frame_merge_flush,
|
|
|
|
.close = av1_frame_merge_close,
|
|
|
|
.filter = av1_frame_merge_filter,
|
|
|
|
};
|