FFmpeg
Loading...
Searching...
No Matches
lcevc_merge.c
Go to the documentation of this file.
1/*
2 * This file is part of FFmpeg.
3 *
4 * FFmpeg is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * FFmpeg is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with FFmpeg; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19#include "libavutil/opt.h"
20
21#include "libavcodec/bsf.h"
26#include "libavcodec/packet.h"
28
34
36 AVPacket *base_pkt, const AVPacket *lcevc_pkt)
37{
38 LCEVCMergeContext *lcevc = ctx->priv_data;
39
40 if (lcevc->nal_length_size && lcevc_pkt->size > lcevc->nal_length_size) {
42 size_t size = 0;
43
44 bytestream2_init(&bc, lcevc_pkt->data, lcevc_pkt->size);
45 do {
46 int i = 0;
47 int nal_size = get_nalsize(lcevc->nal_length_size,
49 if (nal_size < 0)
50 return base_pkt;
51 if (nal_size > INT_MAX - 4)
52 return base_pkt;
53 size += nal_size + 4;
54 nal_size += i;
55 if (nal_size > bytestream2_get_bytes_left(&bc))
56 return base_pkt;
57 bytestream2_skip(&bc, nal_size);
58 } while (bytestream2_get_bytes_left(&bc) > 0);
59
60 uint8_t *buf = av_packet_new_side_data(base_pkt, AV_PKT_DATA_LCEVC, size);
61 if (!buf)
62 return base_pkt;
63
66 bytestream2_init(&bc, lcevc_pkt->data, lcevc_pkt->size);
67 do {
68 int i = 0;
69 int nal_size = get_nalsize(lcevc->nal_length_size,
71 bytestream2_skipu(&bc, i);
72 bytestream2_put_be32u(&pc, 1); // start code
73 bytestream2_put_bufferu(&pc, bc.buffer, nal_size);
74 bytestream2_skipu(&bc, nal_size);
75 } while (bytestream2_get_bytes_left(&bc) > 0);
76 } else {
77 uint8_t *buf = av_packet_new_side_data(base_pkt, AV_PKT_DATA_LCEVC, lcevc_pkt->size);
78 if (buf)
79 memcpy(buf, lcevc_pkt->data, lcevc_pkt->size);
80 }
81
82 return base_pkt;
83}
84
86{
88 AVPacket *base, *enhancement, *out = NULL;
89 int ret;
90
91 ret = ff_packetsync_dualinput_get(fs, &base, &enhancement);
92 if (ret < 0)
93 return ret;
94 if (!enhancement)
95 return ff_bsf_filter_packet(ctx->outputs[0], base);
96 out = lcevc_merge_packets(ctx, base, enhancement);
97 return ff_bsf_filter_packet(ctx->outputs[0], out);
98}
99
101{
103 LCEVCMergeContext *lcevc = ctx->priv_data;
104
105 if (inlink->par->extradata_size > 4 && inlink->par->extradata[0])
106 lcevc->nal_length_size = (inlink->par->extradata[4] >> 6) + 1;
107
108 return 0;
109}
110
112{
113 AVBitStreamFilterContext *ctx = outlink->src;
114 LCEVCMergeContext *lcevc = ctx->priv_data;
115 int ret;
116
117 ret = ff_packetsync_init_dualinput(&lcevc->ps, ctx);
118 if (ret < 0)
119 return ret;
120
121 ret = ff_packetsync_configure(&lcevc->ps);
122 outlink->time_base = lcevc->ps.time_base;
123
124 return ret;
125}
126
128{
129 LCEVCMergeContext *lcevc = ctx->priv_data;
130
131 lcevc->ps.on_event = lcevc_merge;
132 return 0;
133}
134
136{
137 LCEVCMergeContext *lcevc = ctx->priv_data;
138
139 ff_packetsync_uninit(&lcevc->ps);
140}
141
143
145{
146 LCEVCMergeContext *lcevc = ctx->priv_data;
147 return ff_packetsync_activate(&lcevc->ps);
148}
149
153
155 {
156 .name = "base",
157 },
158 {
159 .name = "enhancement",
160 .codec_ids = enhancement_codec_ids,
161 .config_props = config_enhancement,
162 },
163};
164
166 {
167 .name = "default",
168 .config_props = config_output,
169 },
170};
171
173 .p.name = "lcevc_merge",
174 .p.priv_class = &lcevc_merge_class,
175 .priv_data_size = sizeof(LCEVCMergeContext),
176 .preinit = lcevc_merge_packetsync_preinit,
177 .init2 = init,
178 .uninit = uninit,
182};
static FILE * out
static AVFormatContext * ctx
const FFBitStreamFilter ff_lcevc_merge_bsf
int ff_bsf_filter_packet(AVBitStreamFilterLink *link, AVPacket *pkt)
Send a packet of data to the next filter.
static av_always_inline void bytestream2_init_writer(PutByteContext *p, uint8_t *buf, int buf_size)
Definition bytestream.h:147
static av_always_inline void bytestream2_skipu(GetByteContext *g, unsigned int size)
Definition bytestream.h:174
static av_always_inline int bytestream2_get_bytes_left(const GetByteContext *g)
Definition bytestream.h:158
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition bytestream.h:137
static av_always_inline unsigned int bytestream2_put_bufferu(PutByteContext *p, const uint8_t *src, unsigned int size)
Definition bytestream.h:301
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition bytestream.h:168
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define fs(width, name, subs,...)
Definition cbs_vp9.c:200
#define NULL
Definition coverity.c:32
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition codec_id.h:47
@ AV_CODEC_ID_LCEVC
Definition codec_id.h:609
@ AV_CODEC_ID_NONE
Definition codec_id.h:48
@ AV_PKT_DATA_LCEVC
Raw LCEVC payload data, as a uint8_t array, with NAL emulation bytes intact.
Definition packet.h:346
uint8_t * av_packet_new_side_data(AVPacket *pkt, enum AVPacketSideDataType type, size_t size)
Allocate new information of a packet.
Definition packet.c:231
static int get_nalsize(int nal_length_size, const uint8_t *buf, int buf_size, int *buf_index, void *logctx)
static const AVBitStreamFilterPad lcevc_merge_outputs[]
static enum AVCodecID enhancement_codec_ids[]
static av_cold void uninit(AVBitStreamFilterContext *ctx)
static int lcevc_merge(FFPacketSync *fs)
Definition lcevc_merge.c:85
static av_cold int config_enhancement(AVBitStreamFilterLink *inlink)
static AVPacket * lcevc_merge_packets(AVBitStreamFilterContext *ctx, AVPacket *base_pkt, const AVPacket *lcevc_pkt)
Definition lcevc_merge.c:35
static int activate(AVBitStreamFilterContext *ctx)
static const AVBitStreamFilterPad lcevc_merge_inputs[]
static int config_output(AVBitStreamFilterLink *outlink)
#define BSFILTER_INPUTS(array)
Definition filters.h:145
#define BSFILTER_OUTPUTS(array)
Definition filters.h:146
#define av_cold
Definition attributes.h:117
AVOptions.
void ff_packetsync_uninit(FFPacketSync *fs)
Free all memory currently allocated.
Definition packetsync.c:289
int ff_packetsync_init_dualinput(FFPacketSync *fs, AVBitStreamFilterContext *parent)
Initialize a packet sync structure for dualinput.
Definition packetsync.c:366
int ff_packetsync_configure(FFPacketSync *fs)
Configure a packet sync structure.
Definition packetsync.c:138
int ff_packetsync_activate(FFPacketSync *fs)
Examine the packets in the filter's input and try to produce output.
Definition packetsync.c:340
int ff_packetsync_dualinput_get(FFPacketSync *fs, AVPacket **f0, AVPacket **f1)
Definition packetsync.c:384
#define PACKETSYNC_DEFINE_CLASS_EXT(name, context, field, options)
Definition packetsync.h:338
static av_cold int preinit(AVBitStreamFilterContext *ctx)
Definition source.c:137
An instance of a filter.
Definition bsf.h:347
A filter pad used for either input or output.
Definition filters.h:35
Describe the class of an AVClass context structure.
Definition log.h:76
int extradata_size
Size of the extradata content in bytes.
Definition codec_par.h:75
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition codec_par.h:71
This structure stores compressed data.
Definition packet.h:580
int size
Definition packet.h:604
uint8_t * data
Definition packet.h:603
Packet sync structure.
Definition packetsync.h:160
AVRational time_base
Time base for the output events.
Definition packetsync.h:176
int(* on_event)(struct FFPacketSync *fs)
Callback called when a packet event is ready.
Definition packetsync.h:186
const uint8_t * buffer
Definition bytestream.h:34
FFPacketSync ps
Definition lcevc_merge.c:31
int size
uint8_t base
Definition vp3data.h:128