avformat/avformat: Add AVStream parameter to check_bitstream() sig

For most check_bitstream() functions this just avoids having
to dereference s->streams[pkt->stream_index] themselves; but for
meta-muxers it will allow to forward the packet to stream with
a different stream_index (belonging to a different AVFormatContext)
without using a spare packet.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
This commit is contained in:
Andreas Rheinhardt 2021-11-18 21:48:49 +01:00
parent c90b3661fa
commit a5ee166327
10 changed files with 36 additions and 27 deletions

View File

@ -322,6 +322,7 @@
#include "libavformat/version.h"
struct AVFormatContext;
struct AVStream;
struct AVDeviceInfoList;
struct AVDeviceCapabilitiesQuery;
@ -623,9 +624,13 @@ typedef struct AVOutputFormat {
/**
* Set up any necessary bitstream filtering and extract any extra data needed
* for the global header.
*
* @note pkt might have been directly forwarded by a meta-muxer; therefore
* pkt->stream_index as well as the pkt's timebase might be invalid.
* Return 0 if more packets from this stream must be checked; 1 if not.
*/
int (*check_bitstream)(struct AVFormatContext *, const AVPacket *pkt);
int (*check_bitstream)(struct AVFormatContext *s, struct AVStream *st,
const AVPacket *pkt);
} AVOutputFormat;
/**
* @}

View File

@ -2333,19 +2333,21 @@ static int dash_write_trailer(AVFormatContext *s)
return 0;
}
static int dash_check_bitstream(struct AVFormatContext *s, const AVPacket *avpkt)
static int dash_check_bitstream(AVFormatContext *s, AVStream *st,
const AVPacket *avpkt)
{
DASHContext *c = s->priv_data;
OutputStream *os = &c->streams[avpkt->stream_index];
OutputStream *os = &c->streams[st->index];
AVFormatContext *oc = os->ctx;
if (oc->oformat->check_bitstream) {
AVStream *const ost = oc->streams[0];
int ret;
AVPacket pkt = *avpkt;
pkt.stream_index = 0;
ret = oc->oformat->check_bitstream(oc, &pkt);
ret = oc->oformat->check_bitstream(oc, ost, &pkt);
if (ret == 1) {
FFStream *const sti = ffstream(s->streams[avpkt->stream_index]);
FFStream *const osti = ffstream(oc->streams[0]);
FFStream *const sti = ffstream(st);
FFStream *const osti = ffstream(ost);
sti->bsfc = osti->bsfc;
osti->bsfc = NULL;
}

View File

@ -1083,10 +1083,10 @@ fail:
return ret;
}
static int flv_check_bitstream(struct AVFormatContext *s, const AVPacket *pkt)
static int flv_check_bitstream(AVFormatContext *s, AVStream *st,
const AVPacket *pkt)
{
int ret = 1;
AVStream *st = s->streams[pkt->stream_index];
if (st->codecpar->codec_id == AV_CODEC_ID_AAC) {
if (pkt->size > 2 && (AV_RB16(pkt->data) & 0xfff0) == 0xfff0)

View File

@ -245,10 +245,10 @@ too_large:
return AVERROR_INVALIDDATA;
}
static int latm_check_bitstream(struct AVFormatContext *s, const AVPacket *pkt)
static int latm_check_bitstream(AVFormatContext *s, AVStream *st,
const AVPacket *pkt)
{
int ret = 1;
AVStream *st = s->streams[pkt->stream_index];
if (st->codecpar->codec_id == AV_CODEC_ID_AAC) {
if (pkt->size > 2 && (AV_RB16(pkt->data) & 0xfff0) == 0xfff0)

View File

@ -2731,10 +2731,10 @@ static int mkv_init(struct AVFormatContext *s)
return 0;
}
static int mkv_check_bitstream(struct AVFormatContext *s, const AVPacket *pkt)
static int mkv_check_bitstream(AVFormatContext *s, AVStream *st,
const AVPacket *pkt)
{
int ret = 1;
AVStream *st = s->streams[pkt->stream_index];
if (st->codecpar->codec_id == AV_CODEC_ID_AAC) {
if (pkt->size > 2 && (AV_RB16(pkt->data) & 0xfff0) == 0xfff0)

View File

@ -7343,10 +7343,10 @@ static int mov_write_trailer(AVFormatContext *s)
return res;
}
static int mov_check_bitstream(struct AVFormatContext *s, const AVPacket *pkt)
static int mov_check_bitstream(AVFormatContext *s, AVStream *st,
const AVPacket *pkt)
{
int ret = 1;
AVStream *st = s->streams[pkt->stream_index];
if (st->codecpar->codec_id == AV_CODEC_ID_AAC) {
if (pkt->size > 2 && (AV_RB16(pkt->data) & 0xfff0) == 0xfff0)

View File

@ -2208,10 +2208,10 @@ static void mpegts_deinit(AVFormatContext *s)
av_freep(&ts->services);
}
static int mpegts_check_bitstream(struct AVFormatContext *s, const AVPacket *pkt)
static int mpegts_check_bitstream(AVFormatContext *s, AVStream *st,
const AVPacket *pkt)
{
int ret = 1;
AVStream *st = s->streams[pkt->stream_index];
if (st->codecpar->codec_id == AV_CODEC_ID_H264) {
if (pkt->size >= 5 && AV_RB32(pkt->data) != 0x0000001 &&

View File

@ -1074,7 +1074,7 @@ static int check_bitstream(AVFormatContext *s, FFStream *sti, AVPacket *pkt)
if (s->oformat->check_bitstream) {
if (!sti->bitstream_checked) {
if ((ret = s->oformat->check_bitstream(s, pkt)) < 0)
if ((ret = s->oformat->check_bitstream(s, &sti->pub, pkt)) < 0)
return ret;
else if (ret == 1)
sti->bitstream_checked = 1;

View File

@ -341,9 +341,9 @@ const AVOutputFormat ff_h263_muxer = {
#endif
#if CONFIG_H264_MUXER
static int h264_check_bitstream(struct AVFormatContext *s, const AVPacket *pkt)
static int h264_check_bitstream(AVFormatContext *s, AVStream *st,
const AVPacket *pkt)
{
AVStream *st = s->streams[0];
if (pkt->size >= 5 && AV_RB32(pkt->data) != 0x0000001 &&
AV_RB24(pkt->data) != 0x000001)
return ff_stream_add_bitstream_filter(st, "h264_mp4toannexb", NULL);
@ -364,9 +364,9 @@ const AVOutputFormat ff_h264_muxer = {
#endif
#if CONFIG_HEVC_MUXER
static int hevc_check_bitstream(struct AVFormatContext *s, const AVPacket *pkt)
static int hevc_check_bitstream(AVFormatContext *s, AVStream *st,
const AVPacket *pkt)
{
AVStream *st = s->streams[0];
if (pkt->size >= 5 && AV_RB32(pkt->data) != 0x0000001 &&
AV_RB24(pkt->data) != 0x000001)
return ff_stream_add_bitstream_filter(st, "hevc_mp4toannexb", NULL);
@ -468,9 +468,9 @@ const AVOutputFormat ff_mpeg2video_muxer = {
#endif
#if CONFIG_OBU_MUXER
static int obu_check_bitstream(struct AVFormatContext *s, const AVPacket *pkt)
static int obu_check_bitstream(AVFormatContext *s, AVStream *st,
const AVPacket *pkt)
{
AVStream *st = s->streams[0];
return ff_stream_add_bitstream_filter(st, "av1_metadata", "td=insert");
}

View File

@ -984,15 +984,17 @@ static int seg_write_trailer(struct AVFormatContext *s)
return ret;
}
static int seg_check_bitstream(struct AVFormatContext *s, const AVPacket *pkt)
static int seg_check_bitstream(AVFormatContext *s, AVStream *st,
const AVPacket *pkt)
{
SegmentContext *seg = s->priv_data;
AVFormatContext *oc = seg->avf;
if (oc->oformat->check_bitstream) {
int ret = oc->oformat->check_bitstream(oc, pkt);
AVStream *const ost = oc->streams[st->index];
int ret = oc->oformat->check_bitstream(oc, ost, pkt);
if (ret == 1) {
FFStream *const sti = ffstream( s->streams[pkt->stream_index]);
FFStream *const osti = ffstream(oc->streams[pkt->stream_index]);
FFStream *const sti = ffstream(st);
FFStream *const osti = ffstream(ost);
sti->bsfc = osti->bsfc;
osti->bsfc = NULL;
}