FFmpeg
Loading...
Searching...
No Matches
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 "libavcodec/bsf.h"
23#include "libavcodec/cbs.h"
24#include "libavcodec/cbs_av1.h"
25
26#define MAX_TU_OBUS (1 << 16)
27
35
37{
39
40 ff_cbs_fragment_reset(&ctx->frag[0]);
41 ff_cbs_fragment_reset(&ctx->frag[1]);
43 av_packet_unref(ctx->pkt);
44}
45
47{
49 CodedBitstreamFragment *frag = &ctx->frag[ctx->idx], *tu = &ctx->frag[!ctx->idx];
50 AVPacket *in = ctx->in, *buffer_pkt = ctx->pkt;
51 int err, i;
52
53 err = ff_bsf_get_packet_ref(bsf, in);
54 if (err < 0) {
55 if (err == AVERROR_EOF && tu->nb_units > 0)
56 goto eof;
57 return err;
58 }
59
60 err = ff_cbs_read_packet(ctx->input, frag, in);
61 if (err < 0) {
62 av_log(bsf, AV_LOG_ERROR, "Failed to read packet.\n");
63 goto fail;
64 }
65
66 if (frag->nb_units == 0) {
67 av_log(bsf, AV_LOG_ERROR, "No OBU in packet.\n");
69 goto fail;
70 }
71
72 if (frag->nb_units > MAX_TU_OBUS) {
73 av_log(bsf, AV_LOG_ERROR, "Too many OBUs in a Temporal Unit.\n");
75 goto fail;
76 }
77
78 if (tu->nb_units == 0 && frag->units[0].type != AV1_OBU_TEMPORAL_DELIMITER) {
79 av_log(bsf, AV_LOG_ERROR, "Missing Temporal Delimiter.\n");
81 goto fail;
82 }
83
84 for (i = 1; i < frag->nb_units; i++) {
85 if (frag->units[i].type == AV1_OBU_TEMPORAL_DELIMITER) {
86 av_log(bsf, AV_LOG_ERROR, "Temporal Delimiter in the middle of a packet.\n");
88 goto fail;
89 }
90 }
91
92 if (tu->nb_units > 0 && frag->units[0].type == AV1_OBU_TEMPORAL_DELIMITER) {
93eof:
94 err = ff_cbs_write_packet(ctx->output, buffer_pkt, tu);
95 if (err < 0) {
96 av_log(bsf, AV_LOG_ERROR, "Failed to write packet.\n");
97 goto fail;
98 }
99 av_packet_move_ref(out, buffer_pkt);
100
101 // Swap fragment index, to avoid copying fragment references.
102 ctx->idx = !ctx->idx;
103 } else {
104 if (tu->nb_units + frag->nb_units > MAX_TU_OBUS) {
105 av_log(bsf, AV_LOG_ERROR, "Too many OBUs in a Temporal Unit.\n");
107 goto fail;
108 }
109 for (i = 0; i < frag->nb_units; i++) {
110 err = ff_cbs_insert_unit_content(tu, -1, frag->units[i].type,
111 frag->units[i].content, frag->units[i].content_ref);
112 if (err < 0)
113 goto fail;
114 }
115
116 err = AVERROR(EAGAIN);
117 }
118
119 /* Buffer packets with timestamps (there should be at most one per TU)
120 * or any packet if buffer_pkt is empty. The latter is needed to
121 * passthrough positions in case there are no timestamps like with
122 * the raw OBU demuxer. */
123 if (!buffer_pkt->data ||
124 in->pts != AV_NOPTS_VALUE && buffer_pkt->pts == AV_NOPTS_VALUE) {
125 av_packet_unref(buffer_pkt);
126 av_packet_move_ref(buffer_pkt, in);
127 } else
128 av_packet_unref(in);
129
130 ff_cbs_fragment_reset(&ctx->frag[ctx->idx]);
131
132fail:
133 if (err < 0 && err != AVERROR(EAGAIN))
135
136 return err;
137}
138
140{
142 int err;
143
144 ctx->in = av_packet_alloc();
145 ctx->pkt = av_packet_alloc();
146 if (!ctx->in || !ctx->pkt)
147 return AVERROR(ENOMEM);
148
149 err = ff_cbs_init(&ctx->input, AV_CODEC_ID_AV1, bsf);
150 if (err < 0)
151 return err;
152
153 return ff_cbs_init(&ctx->output, AV_CODEC_ID_AV1, bsf);
154}
155
157{
159
160 ff_cbs_fragment_free(&ctx->frag[0]);
161 ff_cbs_fragment_free(&ctx->frag[1]);
162 av_packet_free(&ctx->in);
163 av_packet_free(&ctx->pkt);
164 ff_cbs_close(&ctx->input);
165 ff_cbs_close(&ctx->output);
166}
167
171
173 .p.name = "av1_frame_merge",
174 .p.codec_ids = av1_frame_merge_codec_ids,
175 .priv_data_size = sizeof(AV1FMergeContext),
180};
static FILE * out
static AVFormatContext * ctx
static int av1_frame_merge_filter(AVBSFContext *bsf, AVPacket *out)
static void av1_frame_merge_close(AVBSFContext *bsf)
static int av1_frame_merge_init(AVBSFContext *bsf)
static void av1_frame_merge_flush(AVBSFContext *bsf)
static enum AVCodecID av1_frame_merge_codec_ids[]
#define MAX_TU_OBUS
const FFBitStreamFilter ff_av1_frame_merge_bsf
int ff_bsf_get_packet_ref(AVBSFContext *ctx, AVPacket *pkt)
Called by bitstream filters to get packet for filtering.
Definition bsf.c:254
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
void(* flush)(AVBSFContext *ctx)
Definition dts2pts.c:615
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:613
#define fail
Definition test.h:479
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition codec_id.h:47
@ AV_CODEC_ID_NONE
Definition codec_id.h:48
@ AV_CODEC_ID_AV1
Definition codec_id.h:275
void av_packet_free(AVPacket **pkt)
Free the packet, if the packet is reference counted, it will be unreferenced first.
Definition packet.c:74
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition packet.c:435
void av_packet_move_ref(AVPacket *dst, AVPacket *src)
Move every field in src to dst and reset src.
Definition packet.c:492
AVPacket * av_packet_alloc(void)
Allocate an AVPacket and set its fields to default values.
Definition packet.c:63
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR_EOF
End of file.
Definition error.h:57
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition avutil.h:247
@ AV1_OBU_TEMPORAL_DELIMITER
Definition av1.h:31
av_cold void CBS_FUNC close(CodedBitstreamContext **ctx_ptr)
Close a context and free all internal state.
Definition cbs.c:118
CodedBitstreamContext * input
CodedBitstreamFragment frag[2]
CodedBitstreamContext * output
The bitstream filter state.
Definition bsf.h:68
void * priv_data
Opaque filter-specific private data.
Definition bsf.h:83
This structure stores compressed data.
Definition packet.h:587
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition packet.h:603
Context structure for coded bitstream operations.
Definition cbs.h:226
Coded bitstream fragment structure, combining one or more units.
Definition cbs.h:129
CodedBitstreamUnit * units
Pointer to an array of units of length nb_units_allocated.
Definition cbs.h:175
int nb_units
Number of units in this fragment.
Definition cbs.h:160
void * content
Pointer to the decomposed form of this unit.
Definition cbs.h:114
void * content_ref
If content is reference counted, a RefStruct reference backing content.
Definition cbs.h:119
CodedBitstreamUnitType type
Codec-specific type of this unit.
Definition cbs.h:81
#define av_log(a,...)
void(* filter)(uint8_t *src, ptrdiff_t stride, int qscale)
Definition h263dsp.c:29