FFmpeg
av1_frame_split_bsf.c
Go to the documentation of this file.
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 /**
22  * @file
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.
26  *
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.
31  */
32 
33 #include "libavutil/avassert.h"
34 
35 #include "bsf.h"
36 #include "bsf_internal.h"
37 #include "cbs.h"
38 #include "cbs_av1.h"
39 
40 typedef struct AV1FSplitContext {
44 
45  int nb_frames;
46  int cur_frame;
50 
52 {
54  CodedBitstreamFragment *td = &s->temporal_unit;
55  int i, ret;
56  int split = !!s->buffer_pkt->data;
57 
58  if (!s->buffer_pkt->data) {
59  int nb_frames = 0;
60 
61  ret = ff_bsf_get_packet_ref(ctx, s->buffer_pkt);
62  if (ret < 0)
63  return ret;
64 
65  ret = ff_cbs_read_packet(s->cbc, td, s->buffer_pkt);
66  if (ret < 0) {
67  av_log(ctx, AV_LOG_WARNING, "Failed to parse temporal unit.\n");
68  goto passthrough;
69  }
70 
71  for (i = 0; i < td->nb_units; i++) {
72  CodedBitstreamUnit *unit = &td->units[i];
73 
74  if (unit->type == AV1_OBU_FRAME ||
75  unit->type == AV1_OBU_FRAME_HEADER)
76  nb_frames++;
77  else if (unit->type == AV1_OBU_TILE_LIST) {
78  av_log(ctx, AV_LOG_VERBOSE, "Large scale tiles are unsupported.\n");
79  goto passthrough;
80  }
81  }
82  if (nb_frames > 1) {
83  s->cur_frame = 0;
84  s->cur_frame_idx = s->last_frame_idx = 0;
85  s->nb_frames = nb_frames;
86  split = 1;
87  }
88  }
89 
90  if (split) {
92  int cur_frame_type = -1, size = 0;
93 
94  for (i = s->cur_frame_idx; i < td->nb_units; i++) {
95  CodedBitstreamUnit *unit = &td->units[i];
96 
97  size += unit->data_size;
98  if (unit->type == AV1_OBU_FRAME) {
99  AV1RawOBU *obu = unit->content;
100 
101  if (frame) {
102  av_log(ctx, AV_LOG_WARNING, "Frame OBU found when Tile data for a "
103  "previous frame was expected.\n");
104  goto passthrough;
105  }
106 
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;
111  s->cur_frame++;
112 
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)
116  break;
117  } else if (unit->type == AV1_OBU_FRAME_HEADER) {
118  AV1RawOBU *obu = unit->content;
119 
120  if (frame) {
121  av_log(ctx, AV_LOG_WARNING, "Frame Header OBU found when Tile data for a "
122  "previous frame was expected.\n");
123  goto passthrough;
124  }
125 
126  frame = &obu->obu.frame_header;
127  cur_frame_type = obu->header.obu_type;
128  s->last_frame_idx = s->cur_frame_idx;
129  s->cur_frame++;
130 
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;
136  break;
137  }
138  } else if (unit->type == AV1_OBU_TILE_GROUP) {
139  AV1RawOBU *obu = unit->content;
140  AV1RawTileGroup *group = &obu->obu.tile_group;
141 
142  if (!frame || cur_frame_type != AV1_OBU_FRAME_HEADER) {
143  av_log(ctx, AV_LOG_WARNING, "Unexpected Tile Group OBU found before a "
144  "Frame Header.\n");
145  goto passthrough;
146  }
147 
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;
152  break;
153  }
154  }
155  }
156  av_assert0(frame && s->cur_frame <= s->nb_frames);
157 
158  ret = av_packet_ref(out, s->buffer_pkt);
159  if (ret < 0)
160  goto fail;
161 
162  out->data = (uint8_t *)td->units[s->last_frame_idx].data;
163  out->size = size;
164 
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;
169 
170  if (!frame->show_existing_frame && !frame->show_frame)
171  out->pts = AV_NOPTS_VALUE;
172 
173  if (s->cur_frame == s->nb_frames) {
174  av_packet_unref(s->buffer_pkt);
176  }
177 
178  return 0;
179  }
180 
181 passthrough:
182  av_packet_move_ref(out, s->buffer_pkt);
183 
184  ret = 0;
185 fail:
186  if (ret < 0) {
188  av_packet_unref(s->buffer_pkt);
189  }
191 
192  return ret;
193 }
194 
201 };
202 
204 {
206  CodedBitstreamFragment *td = &s->temporal_unit;
207  int ret;
208 
209  s->buffer_pkt = av_packet_alloc();
210  if (!s->buffer_pkt)
211  return AVERROR(ENOMEM);
212 
213  ret = ff_cbs_init(&s->cbc, AV_CODEC_ID_AV1, ctx);
214  if (ret < 0)
215  return ret;
216 
217  s->cbc->decompose_unit_types = decompose_unit_types;
218  s->cbc->nb_decompose_unit_types = FF_ARRAY_ELEMS(decompose_unit_types);
219 
220  if (!ctx->par_in->extradata_size)
221  return 0;
222 
223  ret = ff_cbs_read_extradata(s->cbc, td, ctx->par_in);
224  if (ret < 0)
225  av_log(ctx, AV_LOG_WARNING, "Failed to parse extradata.\n");
226 
228 
229  return 0;
230 }
231 
233 {
235 
236  av_packet_unref(s->buffer_pkt);
237  ff_cbs_fragment_reset(&s->temporal_unit);
238 }
239 
241 {
243 
244  av_packet_free(&s->buffer_pkt);
245  ff_cbs_fragment_free(&s->temporal_unit);
246  ff_cbs_close(&s->cbc);
247 }
248 
249 static const enum AVCodecID av1_frame_split_codec_ids[] = {
251 };
252 
254  .p.name = "av1_frame_split",
255  .p.codec_ids = av1_frame_split_codec_ids,
256  .priv_data_size = sizeof(AV1FSplitContext),
259  .close = av1_frame_split_close,
261 };
AV1FSplitContext::last_frame_idx
int last_frame_idx
Definition: av1_frame_split_bsf.c:48
av_packet_unref
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:426
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:186
td
#define td
Definition: regdef.h:70
AV1RawOBU::obu
union AV1RawOBU::@31 obu
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
bsf_internal.h
out
FILE * out
Definition: movenc.c:54
ff_cbs_fragment_free
av_cold void ff_cbs_fragment_free(CodedBitstreamFragment *frag)
Free the units array of a fragment in addition to what ff_cbs_fragment_reset does.
Definition: cbs.c:185
av1_frame_split_close
static void av1_frame_split_close(AVBSFContext *ctx)
Definition: av1_frame_split_bsf.c:240
ff_cbs_read_extradata
int ff_cbs_read_extradata(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, const AVCodecParameters *par)
Read the extradata bitstream found in codec parameters into a fragment, then split into units and dec...
Definition: cbs.c:285
CodedBitstreamUnit::content
void * content
Pointer to the decomposed form of this unit.
Definition: cbs.h:107
AVBitStreamFilter::name
const char * name
Definition: bsf.h:112
ff_cbs_fragment_reset
void ff_cbs_fragment_reset(CodedBitstreamFragment *frag)
Free the units contained in a fragment as well as the fragment's own data buffer, but not the units a...
Definition: cbs.c:171
CodedBitstreamContext
Context structure for coded bitstream operations.
Definition: cbs.h:219
AV1FSplitContext::buffer_pkt
AVPacket * buffer_pkt
Definition: av1_frame_split_bsf.c:41
AV1RawTileGroup::tg_end
uint16_t tg_end
Definition: cbs_av1.h:300
AV_LOG_VERBOSE
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:196
CodedBitstreamUnit::type
CodedBitstreamUnitType type
Codec-specific type of this unit.
Definition: cbs.h:74
cbs.h
filter
filter_frame For filters that do not use the this method is called when a frame is pushed to the filter s input It can be called at any time except in a reentrant way If the input frame is enough to produce then the filter should push the output frames on the output link immediately As an exception to the previous rule if the input frame is enough to produce several output frames then the filter needs output only at least one per link The additional frames can be left buffered in the filter
Definition: filter_design.txt:228
AV1_OBU_TEMPORAL_DELIMITER
@ AV1_OBU_TEMPORAL_DELIMITER
Definition: av1.h:31
AV1RawOBU::header
AV1RawOBUHeader header
Definition: cbs_av1.h:401
av_packet_free
void av_packet_free(AVPacket **pkt)
Free the packet, if the packet is reference counted, it will be unreferenced first.
Definition: avpacket.c:74
AVBSFContext
The bitstream filter state.
Definition: bsf.h:68
CodedBitstreamUnit
Coded bitstream unit structure.
Definition: cbs.h:70
ff_cbs_close
av_cold void ff_cbs_close(CodedBitstreamContext **ctx_ptr)
Close a context and free all internal state.
Definition: cbs.c:141
AV1RawFrame::header
AV1RawFrameHeader header
Definition: cbs_av1.h:306
av1_frame_split_codec_ids
static enum AVCodecID av1_frame_split_codec_ids[]
Definition: av1_frame_split_bsf.c:249
bsf.h
fail
#define fail()
Definition: checkasm.h:177
AV1_OBU_FRAME_HEADER
@ AV1_OBU_FRAME_HEADER
Definition: av1.h:32
AV1FSplitContext::cur_frame
int cur_frame
Definition: av1_frame_split_bsf.c:46
cbs_av1.h
avassert.h
FF_ARRAY_ELEMS
#define FF_ARRAY_ELEMS(a)
Definition: sinewin_tablegen.c:29
CodedBitstreamFragment
Coded bitstream fragment structure, combining one or more units.
Definition: cbs.h:122
s
#define s(width, name)
Definition: cbs_vp9.c:198
init
int(* init)(AVBSFContext *ctx)
Definition: dts2pts_bsf.c:365
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:40
ctx
AVFormatContext * ctx
Definition: movenc.c:48
frame
static AVFrame * frame
Definition: demux_decode.c:54
if
if(ret)
Definition: filter_design.txt:179
AV1RawOBU
Definition: cbs_av1.h:400
NULL
#define NULL
Definition: coverity.c:32
FFBitStreamFilter
Definition: bsf_internal.h:27
AV_CODEC_ID_AV1
@ AV_CODEC_ID_AV1
Definition: codec_id.h:283
AV1RawFrameHeader
Definition: cbs_av1.h:165
AV1FSplitContext
Definition: av1_frame_split_bsf.c:40
CodedBitstreamUnit::data_size
size_t data_size
The number of bytes in the bitstream (including any padding bits in the final byte).
Definition: cbs.h:86
AV1_OBU_TILE_GROUP
@ AV1_OBU_TILE_GROUP
Definition: av1.h:33
av_packet_ref
int av_packet_ref(AVPacket *dst, const AVPacket *src)
Setup a new reference to the data described by a given packet.
Definition: avpacket.c:434
av_packet_move_ref
void av_packet_move_ref(AVPacket *dst, AVPacket *src)
Move every field in src to dst and reset src.
Definition: avpacket.c:483
decompose_unit_types
static const CodedBitstreamUnitType decompose_unit_types[]
Definition: av1_frame_split_bsf.c:195
AV1FSplitContext::cbc
CodedBitstreamContext * cbc
Definition: av1_frame_split_bsf.c:42
AVCodecID
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: codec_id.h:49
av1_frame_split_flush
static void av1_frame_split_flush(AVBSFContext *ctx)
Definition: av1_frame_split_bsf.c:232
AV1_OBU_FRAME
@ AV1_OBU_FRAME
Definition: av1.h:35
FFBitStreamFilter::p
AVBitStreamFilter p
The public AVBitStreamFilter.
Definition: bsf_internal.h:31
size
int size
Definition: twinvq_data.h:10344
ff_av1_frame_split_bsf
const FFBitStreamFilter ff_av1_frame_split_bsf
Definition: av1_frame_split_bsf.c:253
AV_NOPTS_VALUE
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
AV1_OBU_SEQUENCE_HEADER
@ AV1_OBU_SEQUENCE_HEADER
Definition: av1.h:30
split
static char * split(char *message, char delim)
Definition: af_channelmap.c:80
av_packet_alloc
AVPacket * av_packet_alloc(void)
Allocate an AVPacket and set its fields to default values.
Definition: avpacket.c:63
AV1RawOBU::frame_header
AV1RawFrameHeader frame_header
Definition: cbs_av1.h:407
av1_frame_split_filter
static int av1_frame_split_filter(AVBSFContext *ctx, AVPacket *out)
Definition: av1_frame_split_bsf.c:51
AV_CODEC_ID_NONE
@ AV_CODEC_ID_NONE
Definition: codec_id.h:50
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:245
AV1RawOBU::tile_group
AV1RawTileGroup tile_group
Definition: cbs_av1.h:409
ret
ret
Definition: filter_design.txt:187
AV1_OBU_TILE_LIST
@ AV1_OBU_TILE_LIST
Definition: av1.h:37
av1_frame_split_init
static int av1_frame_split_init(AVBSFContext *ctx)
Definition: av1_frame_split_bsf.c:203
CodedBitstreamUnitType
uint32_t CodedBitstreamUnitType
The codec-specific type of a bitstream unit.
Definition: cbs.h:47
AV1FSplitContext::nb_frames
int nb_frames
Definition: av1_frame_split_bsf.c:45
ff_cbs_read_packet
int ff_cbs_read_packet(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, const AVPacket *pkt)
Read the data bitstream from a packet into a fragment, then split into units and decompose.
Definition: cbs.c:303
AV1RawTileGroup
Definition: cbs_av1.h:297
AV1RawOBU::frame
AV1RawFrame frame
Definition: cbs_av1.h:408
flush
void(* flush)(AVBSFContext *ctx)
Definition: dts2pts_bsf.c:367
AV1FSplitContext::cur_frame_idx
int cur_frame_idx
Definition: av1_frame_split_bsf.c:47
ff_cbs_init
av_cold int ff_cbs_init(CodedBitstreamContext **ctx_ptr, enum AVCodecID codec_id, void *log_ctx)
Create and initialise a new context for the given codec.
Definition: cbs.c:89
AVPacket
This structure stores compressed data.
Definition: packet.h:492
AV1FSplitContext::temporal_unit
CodedBitstreamFragment temporal_unit
Definition: av1_frame_split_bsf.c:43
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
ff_bsf_get_packet_ref
int ff_bsf_get_packet_ref(AVBSFContext *ctx, AVPacket *pkt)
Called by bitstream filters to get packet for filtering.
Definition: bsf.c:256
AVFormatContext::priv_data
void * priv_data
Format private data.
Definition: avformat.h:1220
AV1RawOBUHeader::obu_type
uint8_t obu_type
Definition: cbs_av1.h:31