FFmpeg
libavcodec
bsf
remove_extradata.c
Go to the documentation of this file.
1
/*
2
* copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at>
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 "
libavutil/log.h
"
22
#include "
libavutil/opt.h
"
23
24
#include "
av1_parse.h
"
25
#include "
bsf.h
"
26
#include "
bsf_internal.h
"
27
#include "
h264.h
"
28
#include "startcode.h"
29
#include "
vc1_common.h
"
30
31
#include "
hevc/hevc.h
"
32
33
enum
RemoveFreq
{
34
REMOVE_FREQ_KEYFRAME
,
35
REMOVE_FREQ_ALL
,
36
REMOVE_FREQ_NONKEYFRAME
,
37
};
38
39
#define START_CODE 0x000001
40
41
typedef
struct
RemoveExtradataContext
{
42
const
AVClass
*
class
;
43
int
freq
;
44
}
RemoveExtradataContext
;
45
46
static
int
av1_split
(
const
uint8_t *buf,
int
buf_size,
void
*logctx)
47
{
48
AV1OBU
obu;
49
const
uint8_t *ptr = buf, *end = buf + buf_size;
50
51
while
(ptr < end) {
52
int
len
=
ff_av1_extract_obu
(&obu, ptr, buf_size, logctx);
53
if
(
len
< 0)
54
break
;
55
56
if
(obu.
type
==
AV1_OBU_FRAME_HEADER
||
57
obu.
type
==
AV1_OBU_FRAME
) {
58
return
ptr - buf;
59
}
60
ptr +=
len
;
61
buf_size -=
len
;
62
}
63
64
return
0;
65
}
66
67
static
int
h264_split
(
const
uint8_t *buf,
int
buf_size)
68
{
69
const
uint8_t *ptr = buf, *end = buf + buf_size;
70
uint32_t
state
= -1;
71
int
has_sps = 0;
72
int
has_pps = 0;
73
int
nalu_type;
74
75
while
(ptr < end) {
76
ptr =
avpriv_find_start_code
(ptr, end, &
state
);
77
if
((
state
& 0xFFFFFF00) != 0x100)
78
break
;
79
nalu_type =
state
& 0x1F;
80
if
(nalu_type ==
H264_NAL_SPS
) {
81
has_sps = 1;
82
}
else
if
(nalu_type ==
H264_NAL_PPS
)
83
has_pps = 1;
84
/* else if (nalu_type == 0x01 ||
85
* nalu_type == 0x02 ||
86
* nalu_type == 0x05) {
87
* }
88
*/
89
else
if
((nalu_type !=
H264_NAL_SEI
|| has_pps) &&
90
nalu_type !=
H264_NAL_AUD
&& nalu_type !=
H264_NAL_SPS_EXT
&&
91
nalu_type != 0x0f) {
92
if
(has_sps) {
93
while
(ptr - 4 > buf && ptr[-5] == 0)
94
ptr--;
95
return
ptr - 4 - buf;
96
}
97
}
98
}
99
100
return
0;
101
}
102
103
// Split after the parameter sets at the beginning of the stream if they exist.
104
static
int
hevc_split
(
const
uint8_t *buf,
int
buf_size)
105
{
106
const
uint8_t *ptr = buf, *end = buf + buf_size;
107
uint32_t
state
= -1;
108
int
has_vps = 0;
109
int
has_sps = 0;
110
int
has_pps = 0;
111
int
nut;
112
113
while
(ptr < end) {
114
ptr =
avpriv_find_start_code
(ptr, end, &
state
);
115
if
((
state
>> 8) !=
START_CODE
)
116
break
;
117
nut = (
state
>> 1) & 0x3F;
118
if
(nut ==
HEVC_NAL_VPS
)
119
has_vps = 1;
120
else
if
(nut ==
HEVC_NAL_SPS
)
121
has_sps = 1;
122
else
if
(nut ==
HEVC_NAL_PPS
)
123
has_pps = 1;
124
else
if
((nut !=
HEVC_NAL_SEI_PREFIX
|| has_pps) &&
125
nut !=
HEVC_NAL_AUD
) {
126
if
(has_vps && has_sps) {
127
while
(ptr - 4 > buf && ptr[-5] == 0)
128
ptr--;
129
return
ptr - 4 - buf;
130
}
131
}
132
}
133
return
0;
134
}
135
136
static
int
mpegvideo_split
(
const
uint8_t *buf,
int
buf_size)
137
{
138
uint32_t
state
= -1;
139
int
found = 0;
140
141
for
(
int
i
= 0;
i
< buf_size;
i
++) {
142
state
= (
state
<< 8) | buf[
i
];
143
if
(
state
== 0x1B3) {
144
found = 1;
145
}
else
if
(found &&
state
!= 0x1B5 && state < 0x200 && state >= 0x100)
146
return
i
- 3;
147
}
148
return
0;
149
}
150
151
static
int
mpeg4video_split
(
const
uint8_t *buf,
int
buf_size)
152
{
153
const
uint8_t *ptr = buf, *end = buf + buf_size;
154
uint32_t
state
= -1;
155
156
while
(ptr < end) {
157
ptr =
avpriv_find_start_code
(ptr, end, &
state
);
158
if
(
state
== 0x1B3 ||
state
== 0x1B6)
159
return
ptr - 4 - buf;
160
}
161
162
return
0;
163
}
164
165
static
int
vc1_split
(
const
uint8_t *buf,
int
buf_size)
166
{
167
const
uint8_t *ptr = buf, *end = buf + buf_size;
168
uint32_t
state
= -1;
169
int
charged = 0;
170
171
while
(ptr < end) {
172
ptr =
avpriv_find_start_code
(ptr, end, &
state
);
173
if
(
state
==
VC1_CODE_SEQHDR
||
state
==
VC1_CODE_ENTRYPOINT
) {
174
charged = 1;
175
}
else
if
(charged &&
IS_MARKER
(
state
))
176
return
ptr - 4 - buf;
177
}
178
179
return
0;
180
}
181
182
static
int
remove_extradata
(
AVBSFContext
*
ctx
,
AVPacket
*
pkt
)
183
{
184
RemoveExtradataContext
*
s
=
ctx
->
priv_data
;
185
186
int
ret
;
187
188
ret
=
ff_bsf_get_packet_ref
(
ctx
,
pkt
);
189
if
(
ret
< 0)
190
return
ret
;
191
192
if
(
s
->freq ==
REMOVE_FREQ_ALL
||
193
(
s
->freq ==
REMOVE_FREQ_NONKEYFRAME
&& !(
pkt
->
flags
&
AV_PKT_FLAG_KEY
)) ||
194
(
s
->freq ==
REMOVE_FREQ_KEYFRAME
&&
pkt
->
flags
&
AV_PKT_FLAG_KEY
)) {
195
int
i
;
196
197
switch
(
ctx
->par_in->codec_id) {
198
case
AV_CODEC_ID_AV1
:
199
i
=
av1_split
(
pkt
->
data
,
pkt
->
size
,
ctx
);
200
break
;
201
case
AV_CODEC_ID_AVS2
:
202
case
AV_CODEC_ID_AVS3
:
203
case
AV_CODEC_ID_CAVS
:
204
case
AV_CODEC_ID_MPEG4
:
205
i
=
mpeg4video_split
(
pkt
->
data
,
pkt
->
size
);
206
break
;
207
case
AV_CODEC_ID_H264
:
208
i
=
h264_split
(
pkt
->
data
,
pkt
->
size
);
209
break
;
210
case
AV_CODEC_ID_HEVC
:
211
i
=
hevc_split
(
pkt
->
data
,
pkt
->
size
);
212
break
;
213
case
AV_CODEC_ID_MPEG1VIDEO
:
214
case
AV_CODEC_ID_MPEG2VIDEO
:
215
i
=
mpegvideo_split
(
pkt
->
data
,
pkt
->
size
);
216
break
;
217
case
AV_CODEC_ID_VC1
:
218
i
=
vc1_split
(
pkt
->
data
,
pkt
->
size
);
219
break
;
220
default
:
221
i
= 0;
222
}
223
224
pkt
->
data
+=
i
;
225
pkt
->
size
-=
i
;
226
}
227
228
return
0;
229
}
230
231
#define OFFSET(x) offsetof(RemoveExtradataContext, x)
232
#define FLAGS (AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_BSF_PARAM)
233
static
const
AVOption
options
[] = {
234
{
"freq"
,
NULL
,
OFFSET
(freq),
AV_OPT_TYPE_INT
, { .i64 =
REMOVE_FREQ_KEYFRAME
},
REMOVE_FREQ_KEYFRAME
,
REMOVE_FREQ_NONKEYFRAME
,
FLAGS
, .unit =
"freq"
},
235
{
"k"
,
NULL
, 0,
AV_OPT_TYPE_CONST
, { .i64 =
REMOVE_FREQ_NONKEYFRAME
}, .flags =
FLAGS
, .unit =
"freq"
},
236
{
"keyframe"
,
NULL
, 0,
AV_OPT_TYPE_CONST
, { .i64 =
REMOVE_FREQ_KEYFRAME
}, .flags =
FLAGS
, .unit =
"freq"
},
237
{
"e"
,
NULL
, 0,
AV_OPT_TYPE_CONST
, { .i64 =
REMOVE_FREQ_ALL
}, .flags =
FLAGS
, .unit =
"freq"
},
238
{
"all"
,
NULL
, 0,
AV_OPT_TYPE_CONST
, { .i64 =
REMOVE_FREQ_ALL
}, .flags =
FLAGS
, .unit =
"freq"
},
239
{
NULL
},
240
};
241
242
static
const
AVClass
remove_extradata_class
= {
243
.
class_name
=
"remove_extradata"
,
244
.item_name =
av_default_item_name
,
245
.option =
options
,
246
.version =
LIBAVUTIL_VERSION_INT
,
247
};
248
249
const
FFBitStreamFilter
ff_remove_extradata_bsf
= {
250
.
p
.
name
=
"remove_extra"
,
251
.p.priv_class = &
remove_extradata_class
,
252
.priv_data_size =
sizeof
(
RemoveExtradataContext
),
253
.
filter
=
remove_extradata
,
254
};
HEVC_NAL_AUD
@ HEVC_NAL_AUD
Definition:
hevc.h:64
bsf_internal.h
opt.h
AVBitStreamFilter::name
const char * name
Definition:
bsf.h:112
AV_CODEC_ID_MPEG4
@ AV_CODEC_ID_MPEG4
Definition:
codec_id.h:64
av1_split
static int av1_split(const uint8_t *buf, int buf_size, void *logctx)
Definition:
remove_extradata.c:46
H264_NAL_AUD
@ H264_NAL_AUD
Definition:
h264.h:43
AVPacket::data
uint8_t * data
Definition:
packet.h:539
AVOption
AVOption.
Definition:
opt.h:429
AV_CODEC_ID_AVS2
@ AV_CODEC_ID_AVS2
Definition:
codec_id.h:248
AV1OBU
Definition:
av1_parse.h:38
filter
void(* filter)(uint8_t *src, int stride, int qscale)
Definition:
h263dsp.c:29
RemoveExtradataContext
Definition:
remove_extradata.c:41
ff_remove_extradata_bsf
const FFBitStreamFilter ff_remove_extradata_bsf
Definition:
remove_extradata.c:249
AV_PKT_FLAG_KEY
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition:
packet.h:594
AVBSFContext
The bitstream filter state.
Definition:
bsf.h:68
remove_extradata_class
static const AVClass remove_extradata_class
Definition:
remove_extradata.c:242
av1_parse.h
bsf.h
AV1_OBU_FRAME_HEADER
@ AV1_OBU_FRAME_HEADER
Definition:
av1.h:32
OFFSET
#define OFFSET(x)
Definition:
remove_extradata.c:231
HEVC_NAL_SEI_PREFIX
@ HEVC_NAL_SEI_PREFIX
Definition:
hevc.h:68
ff_av1_extract_obu
int ff_av1_extract_obu(AV1OBU *obu, const uint8_t *buf, int length, void *logctx)
Extract an OBU from a raw bitstream.
Definition:
av1_parse.c:29
pkt
AVPacket * pkt
Definition:
movenc.c:60
s
#define s(width, name)
Definition:
cbs_vp9.c:198
vc1_split
static int vc1_split(const uint8_t *buf, int buf_size)
Definition:
remove_extradata.c:165
HEVC_NAL_VPS
@ HEVC_NAL_VPS
Definition:
hevc.h:61
ctx
AVFormatContext * ctx
Definition:
movenc.c:49
hevc.h
H264_NAL_SPS_EXT
@ H264_NAL_SPS_EXT
Definition:
h264.h:47
AV_CODEC_ID_H264
@ AV_CODEC_ID_H264
Definition:
codec_id.h:79
REMOVE_FREQ_ALL
@ REMOVE_FREQ_ALL
Definition:
remove_extradata.c:35
AV_CODEC_ID_AVS3
@ AV_CODEC_ID_AVS3
Definition:
codec_id.h:250
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition:
version.h:85
AVClass
Describe the class of an AVClass context structure.
Definition:
log.h:75
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:284
h264_split
static int h264_split(const uint8_t *buf, int buf_size)
Definition:
remove_extradata.c:67
state
static struct @466 state
VC1_CODE_SEQHDR
@ VC1_CODE_SEQHDR
Definition:
vc1_common.h:40
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition:
log.c:237
IS_MARKER
#define IS_MARKER(state)
Definition:
dca_parser.c:51
options
Definition:
swscale.c:42
VC1_CODE_ENTRYPOINT
@ VC1_CODE_ENTRYPOINT
Definition:
vc1_common.h:39
avpriv_find_start_code
const uint8_t * avpriv_find_start_code(const uint8_t *p, const uint8_t *end, uint32_t *state)
AV_CODEC_ID_MPEG1VIDEO
@ AV_CODEC_ID_MPEG1VIDEO
Definition:
codec_id.h:53
AV1_OBU_FRAME
@ AV1_OBU_FRAME
Definition:
av1.h:35
FFBitStreamFilter::p
AVBitStreamFilter p
The public AVBitStreamFilter.
Definition:
bsf_internal.h:31
REMOVE_FREQ_NONKEYFRAME
@ REMOVE_FREQ_NONKEYFRAME
Definition:
remove_extradata.c:36
AVPacket::size
int size
Definition:
packet.h:540
HEVC_NAL_SPS
@ HEVC_NAL_SPS
Definition:
hevc.h:62
FLAGS
#define FLAGS
Definition:
remove_extradata.c:232
HEVC_NAL_PPS
@ HEVC_NAL_PPS
Definition:
hevc.h:63
mpegvideo_split
static int mpegvideo_split(const uint8_t *buf, int buf_size)
Definition:
remove_extradata.c:136
AVPacket::flags
int flags
A combination of AV_PKT_FLAG values.
Definition:
packet.h:545
vc1_common.h
RemoveFreq
RemoveFreq
Definition:
remove_extradata.c:33
log.h
i
#define i(width, name, range_min, range_max)
Definition:
cbs_h2645.c:256
AV_CODEC_ID_CAVS
@ AV_CODEC_ID_CAVS
Definition:
codec_id.h:139
AV_CODEC_ID_HEVC
@ AV_CODEC_ID_HEVC
Definition:
codec_id.h:228
H264_NAL_SEI
@ H264_NAL_SEI
Definition:
h264.h:40
AV_CODEC_ID_VC1
@ AV_CODEC_ID_VC1
Definition:
codec_id.h:122
len
int len
Definition:
vorbis_enc_data.h:426
ret
ret
Definition:
filter_design.txt:187
AVClass::class_name
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition:
log.h:80
options
static const AVOption options[]
Definition:
remove_extradata.c:233
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition:
opt.h:259
mpeg4video_split
static int mpeg4video_split(const uint8_t *buf, int buf_size)
Definition:
remove_extradata.c:151
RemoveExtradataContext::freq
int freq
Definition:
remove_extradata.c:43
AV1OBU::type
int type
Definition:
av1_parse.h:53
H264_NAL_PPS
@ H264_NAL_PPS
Definition:
h264.h:42
hevc_split
static int hevc_split(const uint8_t *buf, int buf_size)
Definition:
remove_extradata.c:104
AVPacket
This structure stores compressed data.
Definition:
packet.h:516
h264.h
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
H264_NAL_SPS
@ H264_NAL_SPS
Definition:
h264.h:41
REMOVE_FREQ_KEYFRAME
@ REMOVE_FREQ_KEYFRAME
Definition:
remove_extradata.c:34
START_CODE
#define START_CODE
Definition:
remove_extradata.c:39
AV_CODEC_ID_MPEG2VIDEO
@ AV_CODEC_ID_MPEG2VIDEO
preferred ID for MPEG-1/2 video decoding
Definition:
codec_id.h:54
AV_OPT_TYPE_CONST
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
Definition:
opt.h:299
AVFormatContext::priv_data
void * priv_data
Format private data.
Definition:
avformat.h:1328
remove_extradata
static int remove_extradata(AVBSFContext *ctx, AVPacket *pkt)
Definition:
remove_extradata.c:182
Generated on Wed Dec 11 2024 19:21:27 for FFmpeg by
1.8.17