mirror of
https://github.com/FFmpeg/FFmpeg.git
synced 2024-11-21 10:55:51 +02:00
avformat: split off generic NAL function helpers into their own file
Signed-off-by: James Almer <jamrial@gmail.com>
This commit is contained in:
parent
0eacad6921
commit
53c8d417ed
@ -35,7 +35,7 @@ OBJS-$(HAVE_LIBC_MSVCRT) += file_open.o
|
||||
|
||||
# subsystems
|
||||
OBJS-$(CONFIG_ISO_MEDIA) += isom.o
|
||||
OBJS-$(CONFIG_ISO_WRITER) += av1.o avc.o hevc.o vvc.o vpcc.o
|
||||
OBJS-$(CONFIG_ISO_WRITER) += av1.o avc.o hevc.o nal.o vvc.o vpcc.o
|
||||
OBJS-$(CONFIG_IAMFDEC) += iamf_reader.o iamf_parse.o iamf.o
|
||||
OBJS-$(CONFIG_IAMFENC) += iamf_writer.o iamf.o
|
||||
OBJS-$(CONFIG_NETWORK) += network.o
|
||||
|
@ -26,118 +26,7 @@
|
||||
#include "avio.h"
|
||||
#include "avc.h"
|
||||
#include "avio_internal.h"
|
||||
|
||||
static const uint8_t *avc_find_startcode_internal(const uint8_t *p, const uint8_t *end)
|
||||
{
|
||||
const uint8_t *a = p + 4 - ((intptr_t)p & 3);
|
||||
|
||||
for (end -= 3; p < a && p < end; p++) {
|
||||
if (p[0] == 0 && p[1] == 0 && p[2] == 1)
|
||||
return p;
|
||||
}
|
||||
|
||||
for (end -= 3; p < end; p += 4) {
|
||||
uint32_t x = *(const uint32_t*)p;
|
||||
// if ((x - 0x01000100) & (~x) & 0x80008000) // little endian
|
||||
// if ((x - 0x00010001) & (~x) & 0x00800080) // big endian
|
||||
if ((x - 0x01010101) & (~x) & 0x80808080) { // generic
|
||||
if (p[1] == 0) {
|
||||
if (p[0] == 0 && p[2] == 1)
|
||||
return p;
|
||||
if (p[2] == 0 && p[3] == 1)
|
||||
return p+1;
|
||||
}
|
||||
if (p[3] == 0) {
|
||||
if (p[2] == 0 && p[4] == 1)
|
||||
return p+2;
|
||||
if (p[4] == 0 && p[5] == 1)
|
||||
return p+3;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (end += 3; p < end; p++) {
|
||||
if (p[0] == 0 && p[1] == 0 && p[2] == 1)
|
||||
return p;
|
||||
}
|
||||
|
||||
return end + 3;
|
||||
}
|
||||
|
||||
const uint8_t *ff_avc_find_startcode(const uint8_t *p, const uint8_t *end){
|
||||
const uint8_t *out = avc_find_startcode_internal(p, end);
|
||||
if(p<out && out<end && !out[-1]) out--;
|
||||
return out;
|
||||
}
|
||||
|
||||
static int avc_parse_nal_units(AVIOContext *pb, NALUList *list,
|
||||
const uint8_t *buf_in, int size)
|
||||
{
|
||||
const uint8_t *p = buf_in;
|
||||
const uint8_t *end = p + size;
|
||||
const uint8_t *nal_start, *nal_end;
|
||||
|
||||
size = 0;
|
||||
nal_start = ff_avc_find_startcode(p, end);
|
||||
for (;;) {
|
||||
const size_t nalu_limit = SIZE_MAX / sizeof(*list->nalus);
|
||||
while (nal_start < end && !*(nal_start++));
|
||||
if (nal_start == end)
|
||||
break;
|
||||
|
||||
nal_end = ff_avc_find_startcode(nal_start, end);
|
||||
if (pb) {
|
||||
avio_wb32(pb, nal_end - nal_start);
|
||||
avio_write(pb, nal_start, nal_end - nal_start);
|
||||
} else if (list->nb_nalus >= nalu_limit) {
|
||||
return AVERROR(ERANGE);
|
||||
} else {
|
||||
NALU *tmp = av_fast_realloc(list->nalus, &list->nalus_array_size,
|
||||
(list->nb_nalus + 1) * sizeof(*list->nalus));
|
||||
if (!tmp)
|
||||
return AVERROR(ENOMEM);
|
||||
list->nalus = tmp;
|
||||
tmp[list->nb_nalus++] = (NALU){ .offset = nal_start - p,
|
||||
.size = nal_end - nal_start };
|
||||
}
|
||||
size += 4 + nal_end - nal_start;
|
||||
nal_start = nal_end;
|
||||
}
|
||||
return size;
|
||||
}
|
||||
|
||||
int ff_avc_parse_nal_units(AVIOContext *pb, const uint8_t *buf_in, int size)
|
||||
{
|
||||
return avc_parse_nal_units(pb, NULL, buf_in, size);
|
||||
}
|
||||
|
||||
int ff_nal_units_create_list(NALUList *list, const uint8_t *buf, int size)
|
||||
{
|
||||
list->nb_nalus = 0;
|
||||
return avc_parse_nal_units(NULL, list, buf, size);
|
||||
}
|
||||
|
||||
void ff_nal_units_write_list(const NALUList *list, AVIOContext *pb,
|
||||
const uint8_t *buf)
|
||||
{
|
||||
for (unsigned i = 0; i < list->nb_nalus; i++) {
|
||||
avio_wb32(pb, list->nalus[i].size);
|
||||
avio_write(pb, buf + list->nalus[i].offset, list->nalus[i].size);
|
||||
}
|
||||
}
|
||||
|
||||
int ff_avc_parse_nal_units_buf(const uint8_t *buf_in, uint8_t **buf, int *size)
|
||||
{
|
||||
AVIOContext *pb;
|
||||
int ret = avio_open_dyn_buf(&pb);
|
||||
if(ret < 0)
|
||||
return ret;
|
||||
|
||||
ff_avc_parse_nal_units(pb, buf_in, *size);
|
||||
|
||||
*size = avio_close_dyn_buf(pb, buf);
|
||||
return 0;
|
||||
}
|
||||
#include "nal.h"
|
||||
|
||||
int ff_isom_write_avcc(AVIOContext *pb, const uint8_t *data, int len)
|
||||
{
|
||||
@ -157,7 +46,7 @@ int ff_isom_write_avcc(AVIOContext *pb, const uint8_t *data, int len)
|
||||
return 0;
|
||||
}
|
||||
|
||||
ret = ff_avc_parse_nal_units_buf(data, &buf, &len);
|
||||
ret = ff_nal_parse_units_buf(data, &buf, &len);
|
||||
if (ret < 0)
|
||||
return ret;
|
||||
start = buf;
|
||||
@ -283,55 +172,6 @@ int ff_avc_write_annexb_extradata(const uint8_t *in, uint8_t **buf, int *size)
|
||||
return 0;
|
||||
}
|
||||
|
||||
const uint8_t *ff_avc_mp4_find_startcode(const uint8_t *start,
|
||||
const uint8_t *end,
|
||||
int nal_length_size)
|
||||
{
|
||||
unsigned int res = 0;
|
||||
|
||||
if (end - start < nal_length_size)
|
||||
return NULL;
|
||||
while (nal_length_size--)
|
||||
res = (res << 8) | *start++;
|
||||
|
||||
if (res > end - start)
|
||||
return NULL;
|
||||
|
||||
return start + res;
|
||||
}
|
||||
|
||||
uint8_t *ff_nal_unit_extract_rbsp(const uint8_t *src, uint32_t src_len,
|
||||
uint32_t *dst_len, int header_len)
|
||||
{
|
||||
uint8_t *dst;
|
||||
uint32_t i, len;
|
||||
|
||||
dst = av_malloc(src_len + AV_INPUT_BUFFER_PADDING_SIZE);
|
||||
if (!dst)
|
||||
return NULL;
|
||||
|
||||
/* NAL unit header */
|
||||
i = len = 0;
|
||||
while (i < header_len && i < src_len)
|
||||
dst[len++] = src[i++];
|
||||
|
||||
while (i + 2 < src_len)
|
||||
if (!src[i] && !src[i + 1] && src[i + 2] == 3) {
|
||||
dst[len++] = src[i++];
|
||||
dst[len++] = src[i++];
|
||||
i++; // remove emulation_prevention_three_byte
|
||||
} else
|
||||
dst[len++] = src[i++];
|
||||
|
||||
while (i < src_len)
|
||||
dst[len++] = src[i++];
|
||||
|
||||
memset(dst + len, 0, AV_INPUT_BUFFER_PADDING_SIZE);
|
||||
|
||||
*dst_len = len;
|
||||
return dst;
|
||||
}
|
||||
|
||||
static const AVRational avc_sample_aspect_ratio[17] = {
|
||||
{ 0, 1 },
|
||||
{ 1, 1 },
|
||||
|
@ -26,45 +26,8 @@
|
||||
#include "libavutil/rational.h"
|
||||
#include "avio.h"
|
||||
|
||||
typedef struct NALU {
|
||||
int offset;
|
||||
uint32_t size;
|
||||
} NALU;
|
||||
|
||||
typedef struct NALUList {
|
||||
NALU *nalus;
|
||||
unsigned nalus_array_size;
|
||||
unsigned nb_nalus; ///< valid entries in nalus
|
||||
} NALUList;
|
||||
|
||||
/* This function will parse the given annex B buffer and create
|
||||
* a NALUList from it. This list can be passed to ff_nal_units_write_list()
|
||||
* to write the access unit reformatted to mp4.
|
||||
*
|
||||
* @param list A NALUList. The list->nalus and list->nalus_array_size
|
||||
* must be valid when calling this function and may be updated.
|
||||
* nb_nalus is set by this function on success.
|
||||
* @param buf buffer containing annex B H.264 or H.265. Must be padded.
|
||||
* @param size size of buf, excluding padding.
|
||||
* @return < 0 on error, the size of the mp4-style packet on success.
|
||||
*/
|
||||
int ff_nal_units_create_list(NALUList *list, const uint8_t *buf, int size);
|
||||
|
||||
/* Writes a NALUList to the specified AVIOContext. The list must originate
|
||||
* from ff_nal_units_create_list() with the same buf. */
|
||||
void ff_nal_units_write_list(const NALUList *list, AVIOContext *pb,
|
||||
const uint8_t *buf);
|
||||
|
||||
int ff_avc_parse_nal_units(AVIOContext *s, const uint8_t *buf, int size);
|
||||
int ff_avc_parse_nal_units_buf(const uint8_t *buf_in, uint8_t **buf, int *size);
|
||||
int ff_isom_write_avcc(AVIOContext *pb, const uint8_t *data, int len);
|
||||
const uint8_t *ff_avc_find_startcode(const uint8_t *p, const uint8_t *end);
|
||||
int ff_avc_write_annexb_extradata(const uint8_t *in, uint8_t **buf, int *size);
|
||||
const uint8_t *ff_avc_mp4_find_startcode(const uint8_t *start,
|
||||
const uint8_t *end,
|
||||
int nal_length_size);
|
||||
uint8_t *ff_nal_unit_extract_rbsp(const uint8_t *src, uint32_t src_len,
|
||||
uint32_t *dst_len, int header_len);
|
||||
|
||||
typedef struct {
|
||||
uint8_t id;
|
||||
|
@ -36,6 +36,7 @@
|
||||
#include "avformat.h"
|
||||
#include "flv.h"
|
||||
#include "internal.h"
|
||||
#include "nal.h"
|
||||
#include "mux.h"
|
||||
#include "libavutil/opt.h"
|
||||
#include "libavcodec/put_bits.h"
|
||||
@ -1076,7 +1077,7 @@ static int flv_write_packet(AVFormatContext *s, AVPacket *pkt)
|
||||
if (par->codec_id == AV_CODEC_ID_H264 || par->codec_id == AV_CODEC_ID_MPEG4) {
|
||||
/* check if extradata looks like mp4 formatted */
|
||||
if (par->extradata_size > 0 && *(uint8_t*)par->extradata != 1)
|
||||
if ((ret = ff_avc_parse_nal_units_buf(pkt->data, &data, &size)) < 0)
|
||||
if ((ret = ff_nal_parse_units_buf(pkt->data, &data, &size)) < 0)
|
||||
return ret;
|
||||
} else if (par->codec_id == AV_CODEC_ID_HEVC) {
|
||||
if (par->extradata_size > 0 && *(uint8_t*)par->extradata != 1)
|
||||
|
@ -27,6 +27,7 @@
|
||||
#include "avio.h"
|
||||
#include "avio_internal.h"
|
||||
#include "hevc.h"
|
||||
#include "nal.h"
|
||||
|
||||
#define MAX_SPATIAL_SEGMENTATION 4096 // max. value of u(12) field
|
||||
|
||||
@ -978,11 +979,11 @@ int ff_hevc_annexb2mp4(AVIOContext *pb, const uint8_t *buf_in,
|
||||
uint8_t *buf, *end, *start = NULL;
|
||||
|
||||
if (!filter_ps) {
|
||||
ret = ff_avc_parse_nal_units(pb, buf_in, size);
|
||||
ret = ff_nal_parse_units(pb, buf_in, size);
|
||||
goto end;
|
||||
}
|
||||
|
||||
ret = ff_avc_parse_nal_units_buf(buf_in, &start, &size);
|
||||
ret = ff_nal_parse_units_buf(buf_in, &start, &size);
|
||||
if (ret < 0)
|
||||
goto end;
|
||||
|
||||
@ -1059,7 +1060,7 @@ int ff_isom_write_hvcc(AVIOContext *pb, const uint8_t *data,
|
||||
return AVERROR_INVALIDDATA;
|
||||
}
|
||||
|
||||
ret = ff_avc_parse_nal_units_buf(data, &start, &size);
|
||||
ret = ff_nal_parse_units_buf(data, &start, &size);
|
||||
if (ret < 0)
|
||||
return ret;
|
||||
|
||||
|
@ -50,6 +50,7 @@
|
||||
#endif
|
||||
#include "hlsplaylist.h"
|
||||
#include "internal.h"
|
||||
#include "nal.h"
|
||||
#include "mux.h"
|
||||
#include "os_support.h"
|
||||
#include "url.h"
|
||||
|
@ -33,6 +33,7 @@
|
||||
#include "flacenc.h"
|
||||
#include "internal.h"
|
||||
#include "isom.h"
|
||||
#include "nal.h"
|
||||
#include "matroska.h"
|
||||
#include "mux.h"
|
||||
#include "riff.h"
|
||||
|
@ -63,6 +63,7 @@
|
||||
#include "libavutil/uuid.h"
|
||||
#include "hevc.h"
|
||||
#include "rtpenc.h"
|
||||
#include "nal.h"
|
||||
#include "mov_chan.h"
|
||||
#include "movenc_ttml.h"
|
||||
#include "mux.h"
|
||||
@ -6376,7 +6377,7 @@ int ff_mov_write_packet(AVFormatContext *s, AVPacket *pkt)
|
||||
/* from x264 or from bytestream H.264 */
|
||||
/* NAL reformatting needed */
|
||||
if (trk->hint_track >= 0 && trk->hint_track < mov->nb_tracks) {
|
||||
ret = ff_avc_parse_nal_units_buf(pkt->data, &reformatted_data,
|
||||
ret = ff_nal_parse_units_buf(pkt->data, &reformatted_data,
|
||||
&size);
|
||||
if (ret < 0)
|
||||
return ret;
|
||||
@ -6389,7 +6390,7 @@ int ff_mov_write_packet(AVFormatContext *s, AVPacket *pkt)
|
||||
goto err;
|
||||
}
|
||||
} else {
|
||||
size = ff_avc_parse_nal_units(pb, pkt->data, pkt->size);
|
||||
size = ff_nal_parse_units(pb, pkt->data, pkt->size);
|
||||
}
|
||||
}
|
||||
} else if (par->codec_id == AV_CODEC_ID_HEVC && trk->vos_len > 6 &&
|
||||
|
@ -24,6 +24,7 @@
|
||||
#include "avio_internal.h"
|
||||
#include "movenc.h"
|
||||
#include "avc.h"
|
||||
#include "nal.h"
|
||||
|
||||
static int auxiliary_info_alloc_size(MOVMuxCencContext* ctx, int size)
|
||||
{
|
||||
@ -204,13 +205,13 @@ int ff_mov_cenc_avc_parse_nal_units(MOVMuxCencContext* ctx, AVIOContext *pb,
|
||||
}
|
||||
|
||||
size = 0;
|
||||
nal_start = ff_avc_find_startcode(p, end);
|
||||
nal_start = ff_nal_find_startcode(p, end);
|
||||
for (;;) {
|
||||
while (nal_start < end && !*(nal_start++));
|
||||
if (nal_start == end)
|
||||
break;
|
||||
|
||||
nal_end = ff_avc_find_startcode(nal_start, end);
|
||||
nal_end = ff_nal_find_startcode(nal_start, end);
|
||||
|
||||
avio_wb32(pb, nal_end - nal_start);
|
||||
avio_w8(pb, *nal_start);
|
||||
|
@ -60,6 +60,7 @@
|
||||
#include "avio_internal.h"
|
||||
#include "internal.h"
|
||||
#include "avc.h"
|
||||
#include "nal.h"
|
||||
#include "mux.h"
|
||||
#include "mxf.h"
|
||||
#include "config.h"
|
||||
@ -2477,7 +2478,7 @@ static int mxf_parse_h264_frame(AVFormatContext *s, AVStream *st,
|
||||
if (mxf->header_written)
|
||||
break;
|
||||
|
||||
nal_end = ff_avc_find_startcode(buf, buf_end);
|
||||
nal_end = ff_nal_find_startcode(buf, buf_end);
|
||||
ret = ff_avc_decode_sps(sps, buf, nal_end - buf);
|
||||
if (ret < 0) {
|
||||
av_log(s, AV_LOG_ERROR, "error parsing sps\n");
|
||||
|
190
libavformat/nal.c
Normal file
190
libavformat/nal.c
Normal file
@ -0,0 +1,190 @@
|
||||
/*
|
||||
* NAL helper functions for muxers
|
||||
*
|
||||
* This file is part of FFmpeg.
|
||||
*
|
||||
* FFmpeg is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* FFmpeg is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with FFmpeg; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "libavutil/mem.h"
|
||||
#include "libavutil/error.h"
|
||||
#include "libavcodec/defs.h"
|
||||
#include "avio.h"
|
||||
#include "avio_internal.h"
|
||||
#include "nal.h"
|
||||
|
||||
static const uint8_t *nal_find_startcode_internal(const uint8_t *p, const uint8_t *end)
|
||||
{
|
||||
const uint8_t *a = p + 4 - ((intptr_t)p & 3);
|
||||
|
||||
for (end -= 3; p < a && p < end; p++) {
|
||||
if (p[0] == 0 && p[1] == 0 && p[2] == 1)
|
||||
return p;
|
||||
}
|
||||
|
||||
for (end -= 3; p < end; p += 4) {
|
||||
uint32_t x = *(const uint32_t*)p;
|
||||
// if ((x - 0x01000100) & (~x) & 0x80008000) // little endian
|
||||
// if ((x - 0x00010001) & (~x) & 0x00800080) // big endian
|
||||
if ((x - 0x01010101) & (~x) & 0x80808080) { // generic
|
||||
if (p[1] == 0) {
|
||||
if (p[0] == 0 && p[2] == 1)
|
||||
return p;
|
||||
if (p[2] == 0 && p[3] == 1)
|
||||
return p+1;
|
||||
}
|
||||
if (p[3] == 0) {
|
||||
if (p[2] == 0 && p[4] == 1)
|
||||
return p+2;
|
||||
if (p[4] == 0 && p[5] == 1)
|
||||
return p+3;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (end += 3; p < end; p++) {
|
||||
if (p[0] == 0 && p[1] == 0 && p[2] == 1)
|
||||
return p;
|
||||
}
|
||||
|
||||
return end + 3;
|
||||
}
|
||||
|
||||
const uint8_t *ff_nal_find_startcode(const uint8_t *p, const uint8_t *end){
|
||||
const uint8_t *out = nal_find_startcode_internal(p, end);
|
||||
if(p<out && out<end && !out[-1]) out--;
|
||||
return out;
|
||||
}
|
||||
|
||||
static int nal_parse_units(AVIOContext *pb, NALUList *list,
|
||||
const uint8_t *buf_in, int size)
|
||||
{
|
||||
const uint8_t *p = buf_in;
|
||||
const uint8_t *end = p + size;
|
||||
const uint8_t *nal_start, *nal_end;
|
||||
|
||||
size = 0;
|
||||
nal_start = ff_nal_find_startcode(p, end);
|
||||
for (;;) {
|
||||
const size_t nalu_limit = SIZE_MAX / sizeof(*list->nalus);
|
||||
while (nal_start < end && !*(nal_start++));
|
||||
if (nal_start == end)
|
||||
break;
|
||||
|
||||
nal_end = ff_nal_find_startcode(nal_start, end);
|
||||
if (pb) {
|
||||
avio_wb32(pb, nal_end - nal_start);
|
||||
avio_write(pb, nal_start, nal_end - nal_start);
|
||||
} else if (list->nb_nalus >= nalu_limit) {
|
||||
return AVERROR(ERANGE);
|
||||
} else {
|
||||
NALU *tmp = av_fast_realloc(list->nalus, &list->nalus_array_size,
|
||||
(list->nb_nalus + 1) * sizeof(*list->nalus));
|
||||
if (!tmp)
|
||||
return AVERROR(ENOMEM);
|
||||
list->nalus = tmp;
|
||||
tmp[list->nb_nalus++] = (NALU){ .offset = nal_start - p,
|
||||
.size = nal_end - nal_start };
|
||||
}
|
||||
size += 4 + nal_end - nal_start;
|
||||
nal_start = nal_end;
|
||||
}
|
||||
return size;
|
||||
}
|
||||
|
||||
int ff_nal_parse_units(AVIOContext *pb, const uint8_t *buf_in, int size)
|
||||
{
|
||||
return nal_parse_units(pb, NULL, buf_in, size);
|
||||
}
|
||||
|
||||
int ff_nal_units_create_list(NALUList *list, const uint8_t *buf, int size)
|
||||
{
|
||||
list->nb_nalus = 0;
|
||||
return nal_parse_units(NULL, list, buf, size);
|
||||
}
|
||||
|
||||
void ff_nal_units_write_list(const NALUList *list, AVIOContext *pb,
|
||||
const uint8_t *buf)
|
||||
{
|
||||
for (unsigned i = 0; i < list->nb_nalus; i++) {
|
||||
avio_wb32(pb, list->nalus[i].size);
|
||||
avio_write(pb, buf + list->nalus[i].offset, list->nalus[i].size);
|
||||
}
|
||||
}
|
||||
|
||||
int ff_nal_parse_units_buf(const uint8_t *buf_in, uint8_t **buf, int *size)
|
||||
{
|
||||
AVIOContext *pb;
|
||||
int ret = avio_open_dyn_buf(&pb);
|
||||
if(ret < 0)
|
||||
return ret;
|
||||
|
||||
ff_nal_parse_units(pb, buf_in, *size);
|
||||
|
||||
*size = avio_close_dyn_buf(pb, buf);
|
||||
return 0;
|
||||
}
|
||||
|
||||
const uint8_t *ff_nal_mp4_find_startcode(const uint8_t *start,
|
||||
const uint8_t *end,
|
||||
int nal_length_size)
|
||||
{
|
||||
unsigned int res = 0;
|
||||
|
||||
if (end - start < nal_length_size)
|
||||
return NULL;
|
||||
while (nal_length_size--)
|
||||
res = (res << 8) | *start++;
|
||||
|
||||
if (res > end - start)
|
||||
return NULL;
|
||||
|
||||
return start + res;
|
||||
}
|
||||
|
||||
uint8_t *ff_nal_unit_extract_rbsp(const uint8_t *src, uint32_t src_len,
|
||||
uint32_t *dst_len, int header_len)
|
||||
{
|
||||
uint8_t *dst;
|
||||
uint32_t i, len;
|
||||
|
||||
dst = av_malloc(src_len + AV_INPUT_BUFFER_PADDING_SIZE);
|
||||
if (!dst)
|
||||
return NULL;
|
||||
|
||||
/* NAL unit header */
|
||||
i = len = 0;
|
||||
while (i < header_len && i < src_len)
|
||||
dst[len++] = src[i++];
|
||||
|
||||
while (i + 2 < src_len)
|
||||
if (!src[i] && !src[i + 1] && src[i + 2] == 3) {
|
||||
dst[len++] = src[i++];
|
||||
dst[len++] = src[i++];
|
||||
i++; // remove emulation_prevention_three_byte
|
||||
} else
|
||||
dst[len++] = src[i++];
|
||||
|
||||
while (i < src_len)
|
||||
dst[len++] = src[i++];
|
||||
|
||||
memset(dst + len, 0, AV_INPUT_BUFFER_PADDING_SIZE);
|
||||
|
||||
*dst_len = len;
|
||||
return dst;
|
||||
}
|
67
libavformat/nal.h
Normal file
67
libavformat/nal.h
Normal file
@ -0,0 +1,67 @@
|
||||
/*
|
||||
* NAL helper functions for muxers
|
||||
*
|
||||
* This file is part of FFmpeg.
|
||||
*
|
||||
* FFmpeg is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* FFmpeg is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with FFmpeg; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#ifndef AVFORMAT_NAL_H
|
||||
#define AVFORMAT_NAL_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include "avio.h"
|
||||
|
||||
typedef struct NALU {
|
||||
int offset;
|
||||
uint32_t size;
|
||||
} NALU;
|
||||
|
||||
typedef struct NALUList {
|
||||
NALU *nalus;
|
||||
unsigned nalus_array_size;
|
||||
unsigned nb_nalus; ///< valid entries in nalus
|
||||
} NALUList;
|
||||
|
||||
/* This function will parse the given annex B buffer and create
|
||||
* a NALUList from it. This list can be passed to ff_nal_units_write_list()
|
||||
* to write the access unit reformatted to mp4.
|
||||
*
|
||||
* @param list A NALUList. The list->nalus and list->nalus_array_size
|
||||
* must be valid when calling this function and may be updated.
|
||||
* nb_nalus is set by this function on success.
|
||||
* @param buf buffer containing annex B H.264 or H.265. Must be padded.
|
||||
* @param size size of buf, excluding padding.
|
||||
* @return < 0 on error, the size of the mp4-style packet on success.
|
||||
*/
|
||||
int ff_nal_units_create_list(NALUList *list, const uint8_t *buf, int size);
|
||||
|
||||
/* Writes a NALUList to the specified AVIOContext. The list must originate
|
||||
* from ff_nal_units_create_list() with the same buf. */
|
||||
void ff_nal_units_write_list(const NALUList *list, AVIOContext *pb,
|
||||
const uint8_t *buf);
|
||||
|
||||
const uint8_t *ff_nal_find_startcode(const uint8_t *p, const uint8_t *end);
|
||||
const uint8_t *ff_nal_mp4_find_startcode(const uint8_t *start,
|
||||
const uint8_t *end,
|
||||
int nal_length_size);
|
||||
|
||||
int ff_nal_parse_units(AVIOContext *s, const uint8_t *buf, int size);
|
||||
int ff_nal_parse_units_buf(const uint8_t *buf_in, uint8_t **buf, int *size);
|
||||
|
||||
uint8_t *ff_nal_unit_extract_rbsp(const uint8_t *src, uint32_t src_len,
|
||||
uint32_t *dst_len, int header_len);
|
||||
|
||||
#endif /* AVFORMAT_NAL_H */
|
@ -31,6 +31,7 @@
|
||||
|
||||
#include "avformat.h"
|
||||
#include "avc.h"
|
||||
#include "nal.h"
|
||||
#include "rtpenc.h"
|
||||
|
||||
static void flush_buffered(AVFormatContext *s1, int last)
|
||||
@ -185,20 +186,20 @@ void ff_rtp_send_h264_hevc(AVFormatContext *s1, const uint8_t *buf1, int size)
|
||||
s->timestamp = s->cur_timestamp;
|
||||
s->buf_ptr = s->buf;
|
||||
if (s->nal_length_size)
|
||||
r = ff_avc_mp4_find_startcode(buf1, end, s->nal_length_size) ? buf1 : end;
|
||||
r = ff_nal_mp4_find_startcode(buf1, end, s->nal_length_size) ? buf1 : end;
|
||||
else
|
||||
r = ff_avc_find_startcode(buf1, end);
|
||||
r = ff_nal_find_startcode(buf1, end);
|
||||
while (r < end) {
|
||||
const uint8_t *r1;
|
||||
|
||||
if (s->nal_length_size) {
|
||||
r1 = ff_avc_mp4_find_startcode(r, end, s->nal_length_size);
|
||||
r1 = ff_nal_mp4_find_startcode(r, end, s->nal_length_size);
|
||||
if (!r1)
|
||||
r1 = end;
|
||||
r += s->nal_length_size;
|
||||
} else {
|
||||
while (!*(r++));
|
||||
r1 = ff_avc_find_startcode(r, end);
|
||||
r1 = ff_nal_find_startcode(r, end);
|
||||
}
|
||||
nal_send(s1, r, r1 - r, r1 == end);
|
||||
r = r1;
|
||||
|
@ -33,6 +33,7 @@
|
||||
#include "internal.h"
|
||||
#include "avc.h"
|
||||
#include "hevc.h"
|
||||
#include "nal.h"
|
||||
#include "rtp.h"
|
||||
#include "version.h"
|
||||
#if CONFIG_NETWORK
|
||||
@ -190,14 +191,14 @@ static int extradata2psets(AVFormatContext *s, const AVCodecParameters *par,
|
||||
}
|
||||
memcpy(psets, pset_string, strlen(pset_string));
|
||||
p = psets + strlen(pset_string);
|
||||
r = ff_avc_find_startcode(extradata, extradata + extradata_size);
|
||||
r = ff_nal_find_startcode(extradata, extradata + extradata_size);
|
||||
while (r < extradata + extradata_size) {
|
||||
const uint8_t *r1;
|
||||
uint8_t nal_type;
|
||||
|
||||
while (!*(r++));
|
||||
nal_type = *r & 0x1f;
|
||||
r1 = ff_avc_find_startcode(r, extradata + extradata_size);
|
||||
r1 = ff_nal_find_startcode(r, extradata + extradata_size);
|
||||
if (nal_type != 7 && nal_type != 8) { /* Only output SPS and PPS */
|
||||
r = r1;
|
||||
continue;
|
||||
|
@ -30,6 +30,7 @@
|
||||
#include "avc.h"
|
||||
#include "avio.h"
|
||||
#include "avio_internal.h"
|
||||
#include "nal.h"
|
||||
#include "vvc.h"
|
||||
|
||||
enum {
|
||||
@ -813,11 +814,11 @@ int ff_vvc_annexb2mp4(AVIOContext *pb, const uint8_t *buf_in,
|
||||
uint8_t *buf, *end, *start = NULL;
|
||||
|
||||
if (!filter_ps) {
|
||||
ret = ff_avc_parse_nal_units(pb, buf_in, size);
|
||||
ret = ff_nal_parse_units(pb, buf_in, size);
|
||||
goto end;
|
||||
}
|
||||
|
||||
ret = ff_avc_parse_nal_units_buf(buf_in, &start, &size);
|
||||
ret = ff_nal_parse_units_buf(buf_in, &start, &size);
|
||||
if (ret < 0)
|
||||
goto end;
|
||||
|
||||
@ -894,7 +895,7 @@ int ff_isom_write_vvcc(AVIOContext *pb, const uint8_t *data,
|
||||
return AVERROR_INVALIDDATA;
|
||||
}
|
||||
|
||||
ret = ff_avc_parse_nal_units_buf(data, &start, &size);
|
||||
ret = ff_nal_parse_units_buf(data, &start, &size);
|
||||
if (ret < 0)
|
||||
return ret;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user