Commit 1dd0def9 authored by Andriy Gelman's avatar Andriy Gelman

avformat/mpegtsenc: Don't use heap allocated array to store pids

A temporary heap array currently stores pids from all streams.  It is
used to make sure there are no duplicated pids. However, this array is
not needed because the pids from past streams are stored in the
MpegTSWriteStream structs.
Reviewed-by: 's avatarMarton Balint <cus@passwd.hu>
Signed-off-by: 's avatarAndriy Gelman <andriy.gelman@gmail.com>
parent e9668929
...@@ -931,7 +931,6 @@ static int mpegts_init(AVFormatContext *s) ...@@ -931,7 +931,6 @@ static int mpegts_init(AVFormatContext *s)
{ {
MpegTSWrite *ts = s->priv_data; MpegTSWrite *ts = s->priv_data;
int i, j; int i, j;
int *pids;
int ret; int ret;
if (ts->m2ts_mode == -1) { if (ts->m2ts_mode == -1) {
...@@ -989,12 +988,6 @@ static int mpegts_init(AVFormatContext *s) ...@@ -989,12 +988,6 @@ static int mpegts_init(AVFormatContext *s)
ts->sdt.write_packet = section_write_packet; ts->sdt.write_packet = section_write_packet;
ts->sdt.opaque = s; ts->sdt.opaque = s;
pids = av_malloc_array(s->nb_streams, sizeof(*pids));
if (!pids) {
ret = AVERROR(ENOMEM);
goto fail;
}
/* assign pids to each stream */ /* assign pids to each stream */
for (i = 0; i < s->nb_streams; i++) { for (i = 0; i < s->nb_streams; i++) {
AVStream *st = s->streams[i]; AVStream *st = s->streams[i];
...@@ -1002,8 +995,7 @@ static int mpegts_init(AVFormatContext *s) ...@@ -1002,8 +995,7 @@ static int mpegts_init(AVFormatContext *s)
ts_st = av_mallocz(sizeof(MpegTSWriteStream)); ts_st = av_mallocz(sizeof(MpegTSWriteStream));
if (!ts_st) { if (!ts_st) {
ret = AVERROR(ENOMEM); return AVERROR(ENOMEM);
goto fail;
} }
st->priv_data = ts_st; st->priv_data = ts_st;
...@@ -1011,8 +1003,7 @@ static int mpegts_init(AVFormatContext *s) ...@@ -1011,8 +1003,7 @@ static int mpegts_init(AVFormatContext *s)
ts_st->payload = av_mallocz(ts->pes_payload_size); ts_st->payload = av_mallocz(ts->pes_payload_size);
if (!ts_st->payload) { if (!ts_st->payload) {
ret = AVERROR(ENOMEM); return AVERROR(ENOMEM);
goto fail;
} }
/* MPEG pid values < 16 are reserved. Applications which set st->id in /* MPEG pid values < 16 are reserved. Applications which set st->id in
...@@ -1043,8 +1034,7 @@ static int mpegts_init(AVFormatContext *s) ...@@ -1043,8 +1034,7 @@ static int mpegts_init(AVFormatContext *s)
ts->m2ts_textsub_pid > M2TS_TEXTSUB_PID + 1 || ts->m2ts_textsub_pid > M2TS_TEXTSUB_PID + 1 ||
ts_st->pid < 16) { ts_st->pid < 16) {
av_log(s, AV_LOG_ERROR, "Cannot automatically assign PID for stream %d\n", st->index); av_log(s, AV_LOG_ERROR, "Cannot automatically assign PID for stream %d\n", st->index);
ret = AVERROR(EINVAL); return AVERROR(EINVAL);
goto fail;
} }
} else { } else {
ts_st->pid = ts->start_pid + i; ts_st->pid = ts->start_pid + i;
...@@ -1055,30 +1045,26 @@ static int mpegts_init(AVFormatContext *s) ...@@ -1055,30 +1045,26 @@ static int mpegts_init(AVFormatContext *s)
if (ts_st->pid >= 0x1FFF) { if (ts_st->pid >= 0x1FFF) {
av_log(s, AV_LOG_ERROR, av_log(s, AV_LOG_ERROR,
"Invalid stream id %d, must be less than 8191\n", st->id); "Invalid stream id %d, must be less than 8191\n", st->id);
ret = AVERROR(EINVAL); return AVERROR(EINVAL);
goto fail;
} }
for (j = 0; j < ts->nb_services; j++) { for (j = 0; j < ts->nb_services; j++) {
if (ts->services[j]->pmt.pid > LAST_OTHER_PID) { if (ts->services[j]->pmt.pid > LAST_OTHER_PID) {
av_log(s, AV_LOG_ERROR, av_log(s, AV_LOG_ERROR,
"Invalid PMT PID %d, must be less than %d\n", ts->services[j]->pmt.pid, LAST_OTHER_PID + 1); "Invalid PMT PID %d, must be less than %d\n", ts->services[j]->pmt.pid, LAST_OTHER_PID + 1);
ret = AVERROR(EINVAL); return AVERROR(EINVAL);
goto fail;
} }
if (ts_st->pid == ts->services[j]->pmt.pid) { if (ts_st->pid == ts->services[j]->pmt.pid) {
av_log(s, AV_LOG_ERROR, "PID %d cannot be both elementary and PMT PID\n", ts_st->pid); av_log(s, AV_LOG_ERROR, "PID %d cannot be both elementary and PMT PID\n", ts_st->pid);
ret = AVERROR(EINVAL); return AVERROR(EINVAL);
goto fail;
} }
} }
for (j = 0; j < i; j++) { for (j = 0; j < i; j++) {
if (pids[j] == ts_st->pid) { MpegTSWriteStream *ts_st_prev = s->streams[j]->priv_data;
if (ts_st_prev->pid == ts_st->pid) {
av_log(s, AV_LOG_ERROR, "Duplicate stream id %d\n", ts_st->pid); av_log(s, AV_LOG_ERROR, "Duplicate stream id %d\n", ts_st->pid);
ret = AVERROR(EINVAL); return AVERROR(EINVAL);
goto fail;
} }
} }
pids[i] = ts_st->pid;
ts_st->payload_pts = AV_NOPTS_VALUE; ts_st->payload_pts = AV_NOPTS_VALUE;
ts_st->payload_dts = AV_NOPTS_VALUE; ts_st->payload_dts = AV_NOPTS_VALUE;
ts_st->first_pts_check = 1; ts_st->first_pts_check = 1;
...@@ -1089,35 +1075,30 @@ static int mpegts_init(AVFormatContext *s) ...@@ -1089,35 +1075,30 @@ static int mpegts_init(AVFormatContext *s)
AVStream *ast; AVStream *ast;
ts_st->amux = avformat_alloc_context(); ts_st->amux = avformat_alloc_context();
if (!ts_st->amux) { if (!ts_st->amux) {
ret = AVERROR(ENOMEM); return AVERROR(ENOMEM);
goto fail;
} }
ts_st->amux->oformat = ts_st->amux->oformat =
av_guess_format((ts->flags & MPEGTS_FLAG_AAC_LATM) ? "latm" : "adts", av_guess_format((ts->flags & MPEGTS_FLAG_AAC_LATM) ? "latm" : "adts",
NULL, NULL); NULL, NULL);
if (!ts_st->amux->oformat) { if (!ts_st->amux->oformat) {
ret = AVERROR(EINVAL); return AVERROR(EINVAL);
goto fail;
} }
if (!(ast = avformat_new_stream(ts_st->amux, NULL))) { if (!(ast = avformat_new_stream(ts_st->amux, NULL))) {
ret = AVERROR(ENOMEM); return AVERROR(ENOMEM);
goto fail;
} }
ret = avcodec_parameters_copy(ast->codecpar, st->codecpar); ret = avcodec_parameters_copy(ast->codecpar, st->codecpar);
if (ret != 0) if (ret != 0)
goto fail; return ret;
ast->time_base = st->time_base; ast->time_base = st->time_base;
ret = avformat_write_header(ts_st->amux, NULL); ret = avformat_write_header(ts_st->amux, NULL);
if (ret < 0) if (ret < 0)
goto fail; return ret;
} }
if (st->codecpar->codec_id == AV_CODEC_ID_OPUS) { if (st->codecpar->codec_id == AV_CODEC_ID_OPUS) {
ts_st->opus_pending_trim_start = st->codecpar->initial_padding * 48000 / st->codecpar->sample_rate; ts_st->opus_pending_trim_start = st->codecpar->initial_padding * 48000 / st->codecpar->sample_rate;
} }
} }
av_freep(&pids);
if (ts->copyts < 1) if (ts->copyts < 1)
ts->first_pcr = av_rescale(s->max_delay, PCR_TIME_BASE, AV_TIME_BASE); ts->first_pcr = av_rescale(s->max_delay, PCR_TIME_BASE, AV_TIME_BASE);
...@@ -1138,10 +1119,6 @@ static int mpegts_init(AVFormatContext *s) ...@@ -1138,10 +1119,6 @@ static int mpegts_init(AVFormatContext *s)
av_rescale(ts->pat_period, 1000, PCR_TIME_BASE)); av_rescale(ts->pat_period, 1000, PCR_TIME_BASE));
return 0; return 0;
fail:
av_freep(&pids);
return ret;
} }
/* send SDT, PAT and PMT tables regularly */ /* send SDT, PAT and PMT tables regularly */
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment