FFmpeg
av1_frame_merge.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 #include "bsf.h"
22 #include "bsf_internal.h"
23 #include "cbs.h"
24 #include "cbs_av1.h"
25 
26 typedef struct AV1FMergeContext {
31  int idx;
33 
35 {
37 
38  ff_cbs_fragment_reset(&ctx->frag[0]);
39  ff_cbs_fragment_reset(&ctx->frag[1]);
40  av_packet_unref(ctx->in);
41  av_packet_unref(ctx->pkt);
42 }
43 
45 {
47  CodedBitstreamFragment *frag = &ctx->frag[ctx->idx], *tu = &ctx->frag[!ctx->idx];
48  AVPacket *in = ctx->in, *buffer_pkt = ctx->pkt;
49  int err, i;
50 
51  err = ff_bsf_get_packet_ref(bsf, in);
52  if (err < 0) {
53  if (err == AVERROR_EOF && tu->nb_units > 0)
54  goto eof;
55  return err;
56  }
57 
58  err = ff_cbs_read_packet(ctx->input, frag, in);
59  if (err < 0) {
60  av_log(bsf, AV_LOG_ERROR, "Failed to read packet.\n");
61  goto fail;
62  }
63 
64  if (frag->nb_units == 0) {
65  av_log(bsf, AV_LOG_ERROR, "No OBU in packet.\n");
66  err = AVERROR_INVALIDDATA;
67  goto fail;
68  }
69 
70  if (tu->nb_units == 0 && frag->units[0].type != AV1_OBU_TEMPORAL_DELIMITER) {
71  av_log(bsf, AV_LOG_ERROR, "Missing Temporal Delimiter.\n");
72  err = AVERROR_INVALIDDATA;
73  goto fail;
74  }
75 
76  for (i = 1; i < frag->nb_units; i++) {
77  if (frag->units[i].type == AV1_OBU_TEMPORAL_DELIMITER) {
78  av_log(bsf, AV_LOG_ERROR, "Temporal Delimiter in the middle of a packet.\n");
79  err = AVERROR_INVALIDDATA;
80  goto fail;
81  }
82  }
83 
84  if (tu->nb_units > 0 && frag->units[0].type == AV1_OBU_TEMPORAL_DELIMITER) {
85 eof:
86  err = ff_cbs_write_packet(ctx->output, buffer_pkt, tu);
87  if (err < 0) {
88  av_log(bsf, AV_LOG_ERROR, "Failed to write packet.\n");
89  goto fail;
90  }
91  av_packet_move_ref(out, buffer_pkt);
92 
93  // Swap fragment index, to avoid copying fragment references.
94  ctx->idx = !ctx->idx;
95  } else {
96  for (i = 0; i < frag->nb_units; i++) {
97  err = ff_cbs_insert_unit_content(tu, -1, frag->units[i].type,
98  frag->units[i].content, frag->units[i].content_ref);
99  if (err < 0)
100  goto fail;
101  }
102 
103  err = AVERROR(EAGAIN);
104  }
105 
106  /* Buffer packets with timestamps (there should be at most one per TU)
107  * or any packet if buffer_pkt is empty. The latter is needed to
108  * passthrough positions in case there are no timestamps like with
109  * the raw OBU demuxer. */
110  if (!buffer_pkt->data ||
111  in->pts != AV_NOPTS_VALUE && buffer_pkt->pts == AV_NOPTS_VALUE) {
112  av_packet_unref(buffer_pkt);
113  av_packet_move_ref(buffer_pkt, in);
114  } else
115  av_packet_unref(in);
116 
117  ff_cbs_fragment_reset(&ctx->frag[ctx->idx]);
118 
119 fail:
120  if (err < 0 && err != AVERROR(EAGAIN))
122 
123  return err;
124 }
125 
127 {
129  int err;
130 
131  ctx->in = av_packet_alloc();
132  ctx->pkt = av_packet_alloc();
133  if (!ctx->in || !ctx->pkt)
134  return AVERROR(ENOMEM);
135 
136  err = ff_cbs_init(&ctx->input, AV_CODEC_ID_AV1, bsf);
137  if (err < 0)
138  return err;
139 
140  return ff_cbs_init(&ctx->output, AV_CODEC_ID_AV1, bsf);
141 }
142 
144 {
146 
147  ff_cbs_fragment_free(&ctx->frag[0]);
148  ff_cbs_fragment_free(&ctx->frag[1]);
149  av_packet_free(&ctx->in);
150  av_packet_free(&ctx->pkt);
151  ff_cbs_close(&ctx->input);
152  ff_cbs_close(&ctx->output);
153 }
154 
155 static const enum AVCodecID av1_frame_merge_codec_ids[] = {
157 };
158 
160  .p.name = "av1_frame_merge",
161  .p.codec_ids = av1_frame_merge_codec_ids,
162  .priv_data_size = sizeof(AV1FMergeContext),
165  .close = av1_frame_merge_close,
167 };
av_packet_unref
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:427
CodedBitstreamUnit::content_ref
void * content_ref
If content is reference counted, a RefStruct reference backing content.
Definition: cbs.h:112
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
av1_frame_merge_filter
static int av1_frame_merge_filter(AVBSFContext *bsf, AVPacket *out)
Definition: av1_frame_merge.c:44
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
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:57
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_insert_unit_content
int ff_cbs_insert_unit_content(CodedBitstreamFragment *frag, int position, CodedBitstreamUnitType type, void *content, void *content_ref)
Insert a new unit into a fragment with the given content.
Definition: cbs.c:782
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
ff_av1_frame_merge_bsf
const FFBitStreamFilter ff_av1_frame_merge_bsf
Definition: av1_frame_merge.c:159
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
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
ff_cbs_close
av_cold void ff_cbs_close(CodedBitstreamContext **ctx_ptr)
Close a context and free all internal state.
Definition: cbs.c:141
av1_frame_merge_flush
static void av1_frame_merge_flush(AVBSFContext *bsf)
Definition: av1_frame_merge.c:34
bsf.h
fail
#define fail()
Definition: checkasm.h:179
av1_frame_merge_close
static void av1_frame_merge_close(AVBSFContext *bsf)
Definition: av1_frame_merge.c:143
CodedBitstreamFragment::units
CodedBitstreamUnit * units
Pointer to an array of units of length nb_units_allocated.
Definition: cbs.h:168
cbs_av1.h
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
CodedBitstreamFragment
Coded bitstream fragment structure, combining one or more units.
Definition: cbs.h:122
ff_cbs_write_packet
int ff_cbs_write_packet(CodedBitstreamContext *ctx, AVPacket *pkt, CodedBitstreamFragment *frag)
Write the bitstream of a fragment to a packet.
Definition: cbs.c:473
ctx
AVFormatContext * ctx
Definition: movenc.c:48
FFBitStreamFilter
Definition: bsf_internal.h:27
AV_CODEC_ID_AV1
@ AV_CODEC_ID_AV1
Definition: codec_id.h:280
AV1FMergeContext
Definition: av1_frame_merge.c:26
flush
void(* flush)(AVBSFContext *ctx)
Definition: dts2pts.c:367
AV1FMergeContext::in
AVPacket * in
Definition: av1_frame_merge.c:30
AV1FMergeContext::input
CodedBitstreamContext * input
Definition: av1_frame_merge.c:27
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:484
AV1FMergeContext::idx
int idx
Definition: av1_frame_merge.c:31
AVCodecID
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: codec_id.h:49
av1_frame_merge_codec_ids
static enum AVCodecID av1_frame_merge_codec_ids[]
Definition: av1_frame_merge.c:155
FFBitStreamFilter::p
AVBitStreamFilter p
The public AVBitStreamFilter.
Definition: bsf_internal.h:31
init
int(* init)(AVBSFContext *ctx)
Definition: dts2pts.c:365
AV_NOPTS_VALUE
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
av_packet_alloc
AVPacket * av_packet_alloc(void)
Allocate an AVPacket and set its fields to default values.
Definition: avpacket.c:63
AV1FMergeContext::frag
CodedBitstreamFragment frag[2]
Definition: av1_frame_merge.c:29
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:255
AVPacket::pts
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:515
AV1FMergeContext::pkt
AVPacket * pkt
Definition: av1_frame_merge.c:30
AVBSFContext::priv_data
void * priv_data
Opaque filter-specific private data.
Definition: bsf.h:83
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
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:499
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
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
AV1FMergeContext::output
CodedBitstreamContext * output
Definition: av1_frame_merge.c:28
av1_frame_merge_init
static int av1_frame_merge_init(AVBSFContext *bsf)
Definition: av1_frame_merge.c:126
CodedBitstreamFragment::nb_units
int nb_units
Number of units in this fragment.
Definition: cbs.h:153