FFmpeg
av1_frame_merge_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 #include "bsf.h"
22 #include "bsf_internal.h"
23 #include "cbs.h"
24 #include "cbs_av1.h"
25 
26 typedef struct AV1FMergeContext {
30  int idx;
32 
34 {
36 
37  ff_cbs_fragment_reset(ctx->cbc, &ctx->frag[0]);
38  ff_cbs_fragment_reset(ctx->cbc, &ctx->frag[1]);
39  av_packet_unref(ctx->in);
40  av_packet_unref(ctx->pkt);
41 }
42 
44 {
46  CodedBitstreamFragment *frag = &ctx->frag[ctx->idx], *tu = &ctx->frag[!ctx->idx];
47  AVPacket *in = ctx->in, *buffer_pkt = ctx->pkt;
48  int err, i;
49 
50  err = ff_bsf_get_packet_ref(bsf, in);
51  if (err < 0) {
52  if (err == AVERROR_EOF && tu->nb_units > 0)
53  goto eof;
54  return err;
55  }
56 
57  err = ff_cbs_read_packet(ctx->cbc, frag, in);
58  if (err < 0) {
59  av_log(bsf, AV_LOG_ERROR, "Failed to read packet.\n");
60  goto fail;
61  }
62 
63  if (frag->nb_units == 0) {
64  av_log(bsf, AV_LOG_ERROR, "No OBU in packet.\n");
65  err = AVERROR_INVALIDDATA;
66  goto fail;
67  }
68 
69  if (tu->nb_units == 0 && frag->units[0].type != AV1_OBU_TEMPORAL_DELIMITER) {
70  av_log(bsf, AV_LOG_ERROR, "Missing Temporal Delimiter.\n");
71  err = AVERROR_INVALIDDATA;
72  goto fail;
73  }
74 
75  for (i = 1; i < frag->nb_units; i++) {
76  if (frag->units[i].type == AV1_OBU_TEMPORAL_DELIMITER) {
77  av_log(bsf, AV_LOG_ERROR, "Temporal Delimiter in the middle of a packet.\n");
78  err = AVERROR_INVALIDDATA;
79  goto fail;
80  }
81  }
82 
83  if (tu->nb_units > 0 && frag->units[0].type == AV1_OBU_TEMPORAL_DELIMITER) {
84 eof:
85  err = ff_cbs_write_packet(ctx->cbc, buffer_pkt, tu);
86  if (err < 0) {
87  av_log(bsf, AV_LOG_ERROR, "Failed to write packet.\n");
88  goto fail;
89  }
90  av_packet_move_ref(out, buffer_pkt);
91 
92  // Swap fragment index, to avoid copying fragment references.
93  ctx->idx = !ctx->idx;
94  } else {
95  for (i = 0; i < frag->nb_units; i++) {
96  err = ff_cbs_insert_unit_content(ctx->cbc, tu, -1, frag->units[i].type,
97  frag->units[i].content, frag->units[i].content_ref);
98  if (err < 0)
99  goto fail;
100  }
101 
102  err = AVERROR(EAGAIN);
103  }
104 
105  // Buffer packets with timestamps. There should be at most one per TU, be it split or not.
106  if (!buffer_pkt->data && in->pts != AV_NOPTS_VALUE)
107  av_packet_move_ref(buffer_pkt, in);
108  else
110 
111  ff_cbs_fragment_reset(ctx->cbc, &ctx->frag[ctx->idx]);
112 
113 fail:
114  if (err < 0 && err != AVERROR(EAGAIN))
116 
117  return err;
118 }
119 
121 {
123 
124  ctx->in = av_packet_alloc();
125  ctx->pkt = av_packet_alloc();
126  if (!ctx->in || !ctx->pkt)
127  return AVERROR(ENOMEM);
128 
129  return ff_cbs_init(&ctx->cbc, AV_CODEC_ID_AV1, bsf);
130 }
131 
133 {
135 
136  ff_cbs_fragment_free(ctx->cbc, &ctx->frag[0]);
137  ff_cbs_fragment_free(ctx->cbc, &ctx->frag[1]);
138  av_packet_free(&ctx->in);
139  av_packet_free(&ctx->pkt);
140  ff_cbs_close(&ctx->cbc);
141 }
142 
143 static const enum AVCodecID av1_frame_merge_codec_ids[] = {
145 };
146 
148  .name = "av1_frame_merge",
149  .priv_data_size = sizeof(AV1FMergeContext),
152  .close = av1_frame_merge_close,
155 };
av1_frame_merge_codec_ids
static enum AVCodecID av1_frame_merge_codec_ids[]
Definition: av1_frame_merge_bsf.c:143
av_packet_unref
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:605
ff_av1_frame_merge_bsf
const AVBitStreamFilter ff_av1_frame_merge_bsf
Definition: av1_frame_merge_bsf.c:147
init
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
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
av1_frame_merge_filter
static int av1_frame_merge_filter(AVBSFContext *bsf, AVPacket *out)
Definition: av1_frame_merge_bsf.c:43
out
FILE * out
Definition: movenc.c:54
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:55
CodedBitstreamUnit::content
void * content
Pointer to the decomposed form of this unit.
Definition: cbs.h:101
AVBitStreamFilter::name
const char * name
Definition: bsf.h:99
CodedBitstreamContext
Context structure for coded bitstream operations.
Definition: cbs.h:168
ff_cbs_close
void ff_cbs_close(CodedBitstreamContext **ctx_ptr)
Close a context and free all internal state.
Definition: cbs.c:115
ff_cbs_fragment_free
void ff_cbs_fragment_free(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag)
Free the units array of a fragment in addition to what ff_cbs_fragment_reset does.
Definition: cbs.c:157
CodedBitstreamUnit::type
CodedBitstreamUnitType type
Codec-specific type of this unit.
Definition: cbs.h:68
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:64
AVBSFContext
The bitstream filter state.
Definition: bsf.h:49
av1_frame_merge_flush
static void av1_frame_merge_flush(AVBSFContext *bsf)
Definition: av1_frame_merge_bsf.c:33
bsf.h
fail
#define fail()
Definition: checkasm.h:123
CodedBitstreamFragment::units
CodedBitstreamUnit * units
Pointer to an array of units of length nb_units_allocated.
Definition: cbs.h:162
cbs_av1.h
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
CodedBitstreamFragment
Coded bitstream fragment structure, combining one or more units.
Definition: cbs.h:116
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:401
ctx
AVFormatContext * ctx
Definition: movenc.c:48
flush
static void flush(AVCodecContext *avctx)
Definition: aacdec_template.c:500
AV_CODEC_ID_AV1
@ AV_CODEC_ID_AV1
Definition: codec_id.h:274
AV1FMergeContext
Definition: av1_frame_merge_bsf.c:26
AV1FMergeContext::in
AVPacket * in
Definition: av1_frame_merge_bsf.c:29
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:663
AV1FMergeContext::idx
int idx
Definition: av1_frame_merge_bsf.c:30
AVCodecID
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: codec_id.h:46
av1_frame_merge_close
static void av1_frame_merge_close(AVBSFContext *bsf)
Definition: av1_frame_merge_bsf.c:132
AV1FMergeContext::cbc
CodedBitstreamContext * cbc
Definition: av1_frame_merge_bsf.c:27
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:53
AV1FMergeContext::frag
CodedBitstreamFragment frag[2]
Definition: av1_frame_merge_bsf.c:28
in
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(const int16_t *) pi >> 8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(const int32_t *) pi >> 24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(const float *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(const float *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(const float *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(const double *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(const double *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(const double *) pi *(1U<< 31)))) #define SET_CONV_FUNC_GROUP(ofmt, ifmt) static void set_generic_function(AudioConvert *ac) { } void ff_audio_convert_free(AudioConvert **ac) { if(! *ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);} AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, int sample_rate, int apply_map) { AudioConvert *ac;int in_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) return NULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method !=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt) > 2) { ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc) { av_free(ac);return NULL;} return ac;} in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar) { ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar ? ac->channels :1;} else if(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;else ac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);return ac;} int ff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in) { int use_generic=1;int len=in->nb_samples;int p;if(ac->dc) { av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));return ff_convert_dither(ac-> in
Definition: audio_convert.c:326
av1_frame_merge_init
static int av1_frame_merge_init(AVBSFContext *bsf)
Definition: av1_frame_merge_bsf.c:120
AV_CODEC_ID_NONE
@ AV_CODEC_ID_NONE
Definition: codec_id.h:47
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
AV1FMergeContext::pkt
AVPacket * pkt
Definition: av1_frame_merge_bsf.c:29
AVBSFContext::priv_data
void * priv_data
Opaque filter-specific private data.
Definition: bsf.h:70
CodedBitstreamUnit::content_ref
AVBufferRef * content_ref
If content is reference counted, a reference to the buffer containing content.
Definition: cbs.h:106
ff_cbs_init
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:74
AVBitStreamFilter
Definition: bsf.h:98
ff_cbs_fragment_reset
void ff_cbs_fragment_reset(CodedBitstreamContext *ctx, 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:142
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:242
ff_cbs_insert_unit_content
int ff_cbs_insert_unit_content(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, int position, CodedBitstreamUnitType type, void *content, AVBufferRef *content_buf)
Insert a new unit into a fragment with the given content.
Definition: cbs.c:722
codec_ids
static enum AVCodecID codec_ids[]
Definition: aac_adtstoasc_bsf.c:148
AVPacket
This structure stores compressed data.
Definition: packet.h:332
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
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:249
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
CodedBitstreamFragment::nb_units
int nb_units
Number of units in this fragment.
Definition: cbs.h:147