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
23 * This bitstream filter splits AV1 Temporal Units into packets containing
24 * just one frame, plus any leading and trailing OBUs that may be present at
25 * the beginning or end, respectively.
27 * Temporal Units already containing only one frame will be passed through
28 * unchanged. When splitting can't be performed, the Temporal Unit will be
29 * passed through containing only the remaining OBUs starting from the first
30 * one after the last successfully split frame.
33 #include "libavutil/avassert.h"
36 #include "bsf_internal.h"
40 typedef struct AV1FSplitContext {
42 CodedBitstreamContext *cbc;
43 CodedBitstreamFragment temporal_unit;
51 static int av1_frame_split_filter(AVBSFContext *ctx, AVPacket *out)
53 AV1FSplitContext *s = ctx->priv_data;
54 CodedBitstreamFragment *td = &s->temporal_unit;
56 int split = !!s->buffer_pkt->data;
58 if (!s->buffer_pkt->data) {
61 ret = ff_bsf_get_packet_ref(ctx, s->buffer_pkt);
65 ret = ff_cbs_read_packet(s->cbc, td, s->buffer_pkt);
67 av_log(ctx, AV_LOG_WARNING, "Failed to parse temporal unit.\n");
71 for (i = 0; i < td->nb_units; i++) {
72 CodedBitstreamUnit *unit = &td->units[i];
74 if (unit->type == AV1_OBU_FRAME ||
75 unit->type == AV1_OBU_FRAME_HEADER)
77 else if (unit->type == AV1_OBU_TILE_LIST) {
78 av_log(ctx, AV_LOG_VERBOSE, "Large scale tiles are unsupported.\n");
84 s->cur_frame_idx = s->last_frame_idx = 0;
85 s->nb_frames = nb_frames;
91 AV1RawFrameHeader *frame = NULL;
92 int cur_frame_type = -1, size = 0;
94 for (i = s->cur_frame_idx; i < td->nb_units; i++) {
95 CodedBitstreamUnit *unit = &td->units[i];
97 size += unit->data_size;
98 if (unit->type == AV1_OBU_FRAME) {
99 AV1RawOBU *obu = unit->content;
102 av_log(ctx, AV_LOG_WARNING, "Frame OBU found when Tile data for a "
103 "previous frame was expected.\n");
107 frame = &obu->obu.frame.header;
108 cur_frame_type = obu->header.obu_type;
109 s->last_frame_idx = s->cur_frame_idx;
110 s->cur_frame_idx = i + 1;
113 // split here unless it's the last frame, in which case
114 // include every trailing OBU
115 if (s->cur_frame < s->nb_frames)
117 } else if (unit->type == AV1_OBU_FRAME_HEADER) {
118 AV1RawOBU *obu = unit->content;
121 av_log(ctx, AV_LOG_WARNING, "Frame Header OBU found when Tile data for a "
122 "previous frame was expected.\n");
126 frame = &obu->obu.frame_header;
127 cur_frame_type = obu->header.obu_type;
128 s->last_frame_idx = s->cur_frame_idx;
131 // split here if show_existing_frame unless it's the last
132 // frame, in which case include every trailing OBU
133 if (frame->show_existing_frame &&
134 s->cur_frame < s->nb_frames) {
135 s->cur_frame_idx = i + 1;
138 } else if (unit->type == AV1_OBU_TILE_GROUP) {
139 AV1RawOBU *obu = unit->content;
140 AV1RawTileGroup *group = &obu->obu.tile_group;
142 if (!frame || cur_frame_type != AV1_OBU_FRAME_HEADER) {
143 av_log(ctx, AV_LOG_WARNING, "Unexpected Tile Group OBU found before a "
148 if ((group->tg_end == (frame->tile_cols * frame->tile_rows) - 1) &&
149 // include every trailing OBU with the last frame
150 s->cur_frame < s->nb_frames) {
151 s->cur_frame_idx = i + 1;
156 av_assert0(frame && s->cur_frame <= s->nb_frames);
158 ret = av_packet_ref(out, s->buffer_pkt);
162 out->data = (uint8_t *)td->units[s->last_frame_idx].data;
165 // skip the frame in the buffer packet if it's split successfully, so it's not present
166 // if the packet is passed through in case of failure when splitting another frame.
167 s->buffer_pkt->data += size;
168 s->buffer_pkt->size -= size;
170 if (!frame->show_existing_frame && !frame->show_frame)
171 out->pts = AV_NOPTS_VALUE;
173 if (s->cur_frame == s->nb_frames) {
174 av_packet_unref(s->buffer_pkt);
175 ff_cbs_fragment_reset(td);
182 av_packet_move_ref(out, s->buffer_pkt);
187 av_packet_unref(out);
188 av_packet_unref(s->buffer_pkt);
190 ff_cbs_fragment_reset(td);
195 static const CodedBitstreamUnitType decompose_unit_types[] = {
196 AV1_OBU_TEMPORAL_DELIMITER,
197 AV1_OBU_SEQUENCE_HEADER,
198 AV1_OBU_FRAME_HEADER,
203 static int av1_frame_split_init(AVBSFContext *ctx)
205 AV1FSplitContext *s = ctx->priv_data;
206 CodedBitstreamFragment *td = &s->temporal_unit;
209 s->buffer_pkt = av_packet_alloc();
211 return AVERROR(ENOMEM);
213 ret = ff_cbs_init(&s->cbc, AV_CODEC_ID_AV1, ctx);
217 s->cbc->decompose_unit_types = (CodedBitstreamUnitType*)decompose_unit_types;
218 s->cbc->nb_decompose_unit_types = FF_ARRAY_ELEMS(decompose_unit_types);
220 if (!ctx->par_in->extradata_size)
223 ret = ff_cbs_read_extradata(s->cbc, td, ctx->par_in);
225 av_log(ctx, AV_LOG_WARNING, "Failed to parse extradata.\n");
227 ff_cbs_fragment_reset(td);
232 static void av1_frame_split_flush(AVBSFContext *ctx)
234 AV1FSplitContext *s = ctx->priv_data;
236 av_packet_unref(s->buffer_pkt);
237 ff_cbs_fragment_reset(&s->temporal_unit);
240 static void av1_frame_split_close(AVBSFContext *ctx)
242 AV1FSplitContext *s = ctx->priv_data;
244 av_packet_free(&s->buffer_pkt);
245 ff_cbs_fragment_free(&s->temporal_unit);
246 ff_cbs_close(&s->cbc);
249 static const enum AVCodecID av1_frame_split_codec_ids[] = {
250 AV_CODEC_ID_AV1, AV_CODEC_ID_NONE,
253 const AVBitStreamFilter ff_av1_frame_split_bsf = {
254 .name = "av1_frame_split",
255 .priv_data_size = sizeof(AV1FSplitContext),
256 .init = av1_frame_split_init,
257 .flush = av1_frame_split_flush,
258 .close = av1_frame_split_close,
259 .filter = av1_frame_split_filter,
260 .codec_ids = av1_frame_split_codec_ids,