56 const uint8_t *in,
int in_size,
enum PsSource ps,
int copy)
58 uint8_t start_code_size;
71 memcpy(*
out + start_code_size, in, in_size);
72 if (start_code_size == 4) {
74 }
else if (start_code_size) {
79 *
out += start_code_size + in_size;
81 *
out_size += start_code_size + in_size;
85 const uint8_t *extradata,
int extradata_size,
86 uint8_t **out_extradata,
int *out_extradata_size)
91 uint32_t total_size = 0;
92 uint8_t *
out =
NULL, unit_nb, sps_done = 0;
93 static const uint8_t nalu_header[4] = { 0, 0, 0, 1 };
95 int length_size, pps_offset = 0;
97 if (extradata_size < 7) {
107 length_size = (bytestream2_get_byteu(gb) & 0x3) + 1;
110 unit_nb = bytestream2_get_byteu(gb) & 0x1f;
119 unit_size = bytestream2_get_be16u(gb);
120 total_size += unit_size + 4;
124 "corrupted stream or invalid MP4/AVCC bitstream\n");
130 memcpy(
out + total_size - unit_size - 4, nalu_header, 4);
133 if (!unit_nb && !sps_done++) {
134 unit_nb = bytestream2_get_byteu(gb);
135 pps_offset = total_size;
140 memset(
out + total_size, 0, padding);
145 s->sps_size = pps_offset;
152 memcpy(
s->sps,
out,
s->sps_size);
155 "Warning: SPS NALU missing or invalid. "
156 "The resulting stream may not play.\n");
158 if (pps_offset < total_size) {
161 s->pps_size = total_size - pps_offset;
169 memcpy(
s->pps,
out + pps_offset,
s->pps_size);
172 "Warning: PPS NALU missing or invalid. "
173 "The resulting stream may not play.\n");
176 if (out_extradata && out_extradata_size) {
178 *out_extradata =
out;
179 *out_extradata_size = total_size;
183 s->length_size = length_size;
187 s->extradata_parsed = 1;
193 unsigned *dst_buf_size,
194 const uint8_t *
nal, uint32_t nal_size,
197 static const uint8_t nalu_header[4] = { 0, 0, 0, 1 };
198 const int start_code_size =
sizeof(nalu_header);
211 memcpy(ptr +
size, nalu_header, start_code_size);
212 size += start_code_size;
213 memcpy(ptr +
size,
nal, nal_size);
223 const uint8_t *buf_end)
230 uint32_t nal_size = 0;
233 for (
int i = 0;
i <
s->length_size;
i++)
234 nal_size = (nal_size << 8) | buf[
i];
236 buf +=
s->length_size;
240 if ((
int64_t)nal_size > buf_end - buf)
246 unit_type = *buf & 0x1f;
250 nal_size, !sps_count);
254 nal_size, !pps_count);
259 }
while (buf < buf_end);
266 int extra_size =
ctx->par_in->extradata_size;
270 (extra_size >= 3 &&
AV_RB24(
ctx->par_in->extradata) == 1) ||
271 (extra_size >= 4 &&
AV_RB32(
ctx->par_in->extradata) == 1)) {
273 "The input looks like it is Annex B already\n");
277 ctx->par_in->extradata,
278 ctx->par_in->extradata_size,
279 &
ctx->par_out->extradata,
280 &
ctx->par_out->extradata_size);
287 uint8_t unit_type, new_idr, sps_seen, pps_seen;
289 const uint8_t *buf_end;
293 size_t extradata_size;
302 if (extradata && extradata[0] == 1) {
311 if (!
s->extradata_parsed) {
322#define LOG_ONCE(...) \
325 for (
int j = 0; j < 2; j++) {
327 new_idr =
s->new_idr;
328 sps_seen =
s->idr_sps_seen;
329 pps_seen =
s->idr_pps_seen;
333 uint32_t nal_size = 0;
337 for (
int i = 0;
i <
s->length_size;
i++)
338 nal_size = (nal_size << 8) | buf[
i];
340 buf +=
s->length_size;
344 if ((
int64_t)nal_size > buf_end - buf) {
352 unit_type = *buf & 0x1f;
355 sps_seen = new_idr = 1;
357 pps_seen = new_idr = 1;
378 !sps_seen && !pps_seen) {
417 }
while (buf < buf_end);
434 s->new_idr = new_idr;
435 s->idr_sps_seen = sps_seen;
436 s->idr_pps_seen = pps_seen;
464 s->new_idr =
s->extradata_parsed;
472 .p.name =
"h264_mp4toannexb",
static enum AVCodecID codec_ids[]
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
static AVFormatContext * ctx
static av_cold void close(AVCodecParserContext *s)
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
const FFBitStreamFilter ff_h264_mp4toannexb_bsf
int ff_bsf_get_packet(AVBSFContext *ctx, AVPacket **pkt)
Called by the bitstream filters to get the next packet for filtering.
static av_always_inline void bytestream2_skipu(GetByteContext *g, unsigned int size)
static av_always_inline unsigned int bytestream2_get_bufferu(GetByteContext *g, uint8_t *dst, unsigned int size)
static av_always_inline int bytestream2_get_bytes_left(const GetByteContext *g)
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
#define i(width, name, range_min, range_max)
static int FUNC sps(CodedBitstreamContext *ctx, RWContext *rw, H264RawSPS *current)
static int FUNC nal(CodedBitstreamContext *ctx, RWContext *rw, LCEVCRawNAL *current, int nal_unit_type)
Misc types and constants that do not belong anywhere else.
void(* flush)(AVBSFContext *ctx)
int(* init)(AVBSFContext *ctx)
AVCodecID
Identify the syntax and semantics of the bitstream.
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding.
void av_packet_side_data_remove(AVPacketSideData *sd, int *pnb_sd, enum AVPacketSideDataType type)
Remove side data of the given type from a side data array.
@ AV_PKT_DATA_NEW_EXTRADATA
The AV_PKT_DATA_NEW_EXTRADATA is used to notify the codec or the format that the extradata buffer was...
void av_packet_free(AVPacket **pkt)
Free the packet, if the packet is reference counted, it will be unreferenced first.
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
void av_packet_move_ref(AVPacket *dst, AVPacket *src)
Move every field in src to dst and reset src.
uint8_t * av_packet_get_side_data(const AVPacket *pkt, enum AVPacketSideDataType type, size_t *size)
Get side information from packet.
int av_packet_copy_props(AVPacket *dst, const AVPacket *src)
Copy only "properties" fields from src to dst.
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
#define AV_LOG_WARNING
Something somehow does not look correct.
#define AV_LOG_VERBOSE
Detailed information.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
void * av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
Reallocate the given buffer if it is not large enough, otherwise do nothing.
int av_reallocp(void *ptr, size_t size)
Allocate, reallocate, or free a block of memory through a pointer to a pointer.
H.264 common definitions.
static void h264_mp4toannexb_flush(AVBSFContext *ctx)
static int h264_mp4toannexb_filter_ps(H264BSFContext *s, const uint8_t *buf, const uint8_t *buf_end)
static int h264_extradata_to_annexb(AVBSFContext *ctx, const uint8_t *extradata, int extradata_size, uint8_t **out_extradata, int *out_extradata_size)
static int h264_mp4toannexb_save_ps(uint8_t **dst, int *dst_size, unsigned *dst_buf_size, const uint8_t *nal, uint32_t nal_size, int first)
static int h264_mp4toannexb_filter(AVBSFContext *ctx, AVPacket *opkt)
static void h264_mp4toannexb_close(AVBSFContext *ctx)
static int h264_mp4toannexb_init(AVBSFContext *ctx)
static void count_or_copy(uint8_t **out, uint64_t *out_size, const uint8_t *in, int in_size, enum PsSource ps, int copy)
Memory handling functions.
@ SEI_TYPE_BUFFERING_PERIOD
The bitstream filter state.
This structure stores compressed data.
AVPacketSideData * side_data
Additional packet data that can be provided by the container.
void(* filter)(uint8_t *src, ptrdiff_t stride, int qscale)
static void copy(const float *p1, float *p2, const int length)