FFmpeg
Loading...
Searching...
No Matches
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
25#include "libavcodec/bsf.h"
27#include "libavcodec/h264.h"
30
32
38
39#define START_CODE 0x000001
40
41typedef struct RemoveExtradataContext {
42 const AVClass *class;
43 int freq;
45
46static 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
67static 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.
104static 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
136static 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
151static 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
165static 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);
174 charged = 1;
175 } else if (charged && IS_MARKER(state))
176 return ptr - 4 - buf;
177 }
178
179 return 0;
180}
181
183{
184 RemoveExtradataContext *s = ctx->priv_data;
185
186 int ret;
187
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:
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;
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)
233static 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
243 .class_name = "remove_extradata",
244 .item_name = av_default_item_name,
245 .option = options,
246 .version = LIBAVUTIL_VERSION_INT,
247};
248
250 .p.name = "remove_extra",
251 .p.priv_class = &remove_extradata_class,
252 .priv_data_size = sizeof(RemoveExtradataContext),
254};
static AVFormatContext * ctx
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
const FFBitStreamFilter ff_remove_extradata_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
#define s(width, name)
Definition cbs_vp9.c:198
#define FLAGS
Definition cmdutils.c:598
#define NULL
Definition coverity.c:32
#define IS_MARKER(state)
Definition dca_parser.c:52
static AVPacket * pkt
static struct @346255127015250356166251341105367306144006377143 state
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
Definition opt.h:298
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
@ AV_CODEC_ID_AVS3
Definition codec_id.h:245
@ AV_CODEC_ID_H264
Definition codec_id.h:77
@ AV_CODEC_ID_AV1
Definition codec_id.h:275
@ AV_CODEC_ID_VC1
Definition codec_id.h:120
@ AV_CODEC_ID_CAVS
Definition codec_id.h:137
@ AV_CODEC_ID_HEVC
Definition codec_id.h:223
@ AV_CODEC_ID_MPEG4
Definition codec_id.h:62
@ AV_CODEC_ID_AVS2
Definition codec_id.h:243
@ AV_CODEC_ID_MPEG1VIDEO
Definition codec_id.h:51
@ AV_CODEC_ID_MPEG2VIDEO
preferred ID for MPEG-1/2 video decoding
Definition codec_id.h:52
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition packet.h:650
const char * av_default_item_name(void *ptr)
Return the context name.
Definition log.c:241
#define LIBAVUTIL_VERSION_INT
Definition version.h:85
H.264 common definitions.
@ H264_NAL_PPS
Definition h264.h:42
@ H264_NAL_SPS
Definition h264.h:41
@ H264_NAL_SPS_EXT
Definition h264.h:47
@ H264_NAL_SEI
Definition h264.h:40
@ H264_NAL_AUD
Definition h264.h:43
@ AV1_OBU_FRAME_HEADER
Definition av1.h:32
@ AV1_OBU_FRAME
Definition av1.h:35
@ HEVC_NAL_AUD
Definition hevc.h:64
@ HEVC_NAL_SEI_PREFIX
Definition hevc.h:68
@ HEVC_NAL_PPS
Definition hevc.h:63
@ HEVC_NAL_VPS
Definition hevc.h:61
@ HEVC_NAL_SPS
Definition hevc.h:62
AVOptions.
static int mpegvideo_split(const uint8_t *buf, int buf_size)
static const AVClass remove_extradata_class
#define START_CODE
static int vc1_split(const uint8_t *buf, int buf_size)
static int av1_split(const uint8_t *buf, int buf_size, void *logctx)
static int remove_extradata(AVBSFContext *ctx, AVPacket *pkt)
static int hevc_split(const uint8_t *buf, int buf_size)
static int mpeg4video_split(const uint8_t *buf, int buf_size)
#define OFFSET(x)
RemoveFreq
@ REMOVE_FREQ_ALL
@ REMOVE_FREQ_NONKEYFRAME
@ REMOVE_FREQ_KEYFRAME
static int h264_split(const uint8_t *buf, int buf_size)
Accelerated start code search function for start codes common to MPEG-1/2/4 video,...
const uint8_t * avpriv_find_start_code(const uint8_t *p, const uint8_t *end, uint32_t *state)
int type
Definition av1_parse.h:53
The bitstream filter state.
Definition bsf.h:68
Describe the class of an AVClass context structure.
Definition log.h:76
AVOption.
Definition opt.h:428
This structure stores compressed data.
Definition packet.h:580
void(* filter)(uint8_t *src, ptrdiff_t stride, int qscale)
Definition h263dsp.c:29
@ VC1_CODE_SEQHDR
Definition vc1_common.h:40
@ VC1_CODE_ENTRYPOINT
Definition vc1_common.h:39
int len