* [FFmpeg-devel] [PATCH v7 1/2] avformat: refactor ff_stream_encode_params_copy() to stream_params_copy()
@ 2022-08-06 23:35 pal
2022-08-06 23:35 ` [FFmpeg-devel] [PATCH v7 2/2] avformat/imfdec: preserve stream information pal
2022-08-08 14:48 ` [FFmpeg-devel] [PATCH v7 1/2] avformat: refactor ff_stream_encode_params_copy() to stream_params_copy() Andreas Rheinhardt
0 siblings, 2 replies; 6+ messages in thread
From: pal @ 2022-08-06 23:35 UTC (permalink / raw)
To: ffmpeg-devel; +Cc: Pierre-Anthony Lemieux
From: Pierre-Anthony Lemieux <pal@palemieux.com>
Addresses http://ffmpeg.org/pipermail/ffmpeg-devel/2022-August/299726.html
---
libavformat/avformat.c | 66 ++++++++++++++++++++++++++++++++++++++++
libavformat/fifo.c | 8 ++---
libavformat/internal.h | 11 +++++++
libavformat/mux.h | 9 ------
libavformat/mux_utils.c | 28 -----------------
libavformat/segment.c | 6 ++--
libavformat/tee.c | 7 ++---
libavformat/webm_chunk.c | 6 ++--
8 files changed, 86 insertions(+), 55 deletions(-)
diff --git a/libavformat/avformat.c b/libavformat/avformat.c
index 30d6ea6a49..19c7219471 100644
--- a/libavformat/avformat.c
+++ b/libavformat/avformat.c
@@ -235,6 +235,72 @@ int ff_stream_side_data_copy(AVStream *dst, const AVStream *src)
return 0;
}
+/**
+ * Copy all stream parameters from source to destination stream, with the
+ * exception of the index field, which is usually set by avformat_new_stream().
+ *
+ * @param dst pointer to destination AVStream
+ * @param src pointer to source AVStream
+ * @return >=0 on success, AVERROR code on error
+ */
+static int stream_params_copy(AVStream *dst, const AVStream *src)
+{
+ int ret;
+
+ dst->id = src->id;
+ dst->time_base = src->time_base;
+ dst->start_time = src->start_time;
+ dst->duration = src->duration;
+ dst->nb_frames = src->nb_frames;
+ dst->disposition = src->disposition;
+ dst->discard = src->discard;
+ dst->sample_aspect_ratio = src->sample_aspect_ratio;
+ dst->avg_frame_rate = src->avg_frame_rate;
+ dst->event_flags = src->event_flags;
+ dst->r_frame_rate = src->r_frame_rate;
+ dst->pts_wrap_bits = src->pts_wrap_bits;
+
+ av_dict_free(&dst->metadata);
+ ret = av_dict_copy(&dst->metadata, src->metadata, 0);
+ if (ret < 0)
+ return ret;
+
+ ret = avcodec_parameters_copy(dst->codecpar, src->codecpar);
+ if (ret < 0)
+ return ret;
+
+ ret = ff_stream_side_data_copy(dst, src);
+ if (ret < 0)
+ return ret;
+
+ av_packet_unref(&dst->attached_pic);
+ if (src->attached_pic.data) {
+ ret = av_packet_ref(&dst->attached_pic, &src->attached_pic);
+ if (ret < 0)
+ return ret;
+ }
+
+ return 0;
+}
+
+AVStream *ff_stream_clone(AVFormatContext *dst_ctx, const AVStream *src)
+{
+ AVStream *st;
+ int ret;
+
+ st = avformat_new_stream(dst_ctx, NULL);
+ if (!st)
+ return NULL;
+
+ ret = stream_params_copy(st, src);
+ if (ret < 0) {
+ ff_remove_stream(dst_ctx, st);
+ return NULL;
+ }
+
+ return st;
+}
+
AVProgram *av_new_program(AVFormatContext *ac, int id)
{
AVProgram *program = NULL;
diff --git a/libavformat/fifo.c b/libavformat/fifo.c
index ead2bdc5cf..55d413b952 100644
--- a/libavformat/fifo.c
+++ b/libavformat/fifo.c
@@ -505,13 +505,11 @@ static int fifo_mux_init(AVFormatContext *avf, const AVOutputFormat *oformat,
avf2->flags = avf->flags;
for (i = 0; i < avf->nb_streams; ++i) {
- AVStream *st = avformat_new_stream(avf2, NULL);
+ AVStream *st = NULL;
+
+ st = ff_stream_clone(avf2, avf->streams[i]);
if (!st)
return AVERROR(ENOMEM);
-
- ret = ff_stream_encode_params_copy(st, avf->streams[i]);
- if (ret < 0)
- return ret;
}
return 0;
diff --git a/libavformat/internal.h b/libavformat/internal.h
index b6b8fbf56f..9b07cfb271 100644
--- a/libavformat/internal.h
+++ b/libavformat/internal.h
@@ -625,6 +625,17 @@ enum AVCodecID ff_get_pcm_codec_id(int bps, int flt, int be, int sflags);
*/
int ff_stream_side_data_copy(AVStream *dst, const AVStream *src);
+/**
+ * Create a new stream and copy to it all parameters from a source stream, with
+ * the exception of the index field, which is set when the new stream is
+ * created.
+ *
+ * @param dst_ctx pointer to the context in which the new stream is created
+ * @param src pointer to source AVStream
+ * @return pointer to the new stream or NULL on error
+ */
+AVStream *ff_stream_clone(AVFormatContext *dst_ctx, const AVStream *src);
+
/**
* Wrap ffurl_move() and log if error happens.
*
diff --git a/libavformat/mux.h b/libavformat/mux.h
index c01da82194..1bfcaf795f 100644
--- a/libavformat/mux.h
+++ b/libavformat/mux.h
@@ -113,15 +113,6 @@ int ff_format_shift_data(AVFormatContext *s, int64_t read_start, int shift_size)
*/
int ff_format_output_open(AVFormatContext *s, const char *url, AVDictionary **options);
-/**
- * Copy encoding parameters from source to destination stream
- *
- * @param dst pointer to destination AVStream
- * @param src pointer to source AVStream
- * @return >=0 on success, AVERROR code on error
- */
-int ff_stream_encode_params_copy(AVStream *dst, const AVStream *src);
-
/**
* Parse creation_time in AVFormatContext metadata if exists and warn if the
* parsing fails.
diff --git a/libavformat/mux_utils.c b/libavformat/mux_utils.c
index eb8ea3d560..2fa2ab5b0f 100644
--- a/libavformat/mux_utils.c
+++ b/libavformat/mux_utils.c
@@ -121,34 +121,6 @@ int ff_format_output_open(AVFormatContext *s, const char *url, AVDictionary **op
return 0;
}
-int ff_stream_encode_params_copy(AVStream *dst, const AVStream *src)
-{
- int ret;
-
- dst->id = src->id;
- dst->time_base = src->time_base;
- dst->nb_frames = src->nb_frames;
- dst->disposition = src->disposition;
- dst->sample_aspect_ratio = src->sample_aspect_ratio;
- dst->avg_frame_rate = src->avg_frame_rate;
- dst->r_frame_rate = src->r_frame_rate;
-
- av_dict_free(&dst->metadata);
- ret = av_dict_copy(&dst->metadata, src->metadata, 0);
- if (ret < 0)
- return ret;
-
- ret = avcodec_parameters_copy(dst->codecpar, src->codecpar);
- if (ret < 0)
- return ret;
-
- ret = ff_stream_side_data_copy(dst, src);
- if (ret < 0)
- return ret;
-
- return 0;
-}
-
int ff_parse_creation_time_metadata(AVFormatContext *s, int64_t *timestamp, int return_seconds)
{
AVDictionaryEntry *entry;
diff --git a/libavformat/segment.c b/libavformat/segment.c
index fa435d9f32..c904e20708 100644
--- a/libavformat/segment.c
+++ b/libavformat/segment.c
@@ -167,11 +167,9 @@ static int segment_mux_init(AVFormatContext *s)
AVStream *st, *ist = s->streams[i];
AVCodecParameters *ipar = ist->codecpar, *opar;
- if (!(st = avformat_new_stream(oc, NULL)))
+ st = ff_stream_clone(oc, ist);
+ if (!st)
return AVERROR(ENOMEM);
- ret = ff_stream_encode_params_copy(st, ist);
- if (ret < 0)
- return ret;
opar = st->codecpar;
if (!oc->oformat->codec_tag ||
av_codec_get_id (oc->oformat->codec_tag, ipar->codec_tag) == opar->codec_id ||
diff --git a/libavformat/tee.c b/libavformat/tee.c
index f1f2a9d2a5..dd408dd096 100644
--- a/libavformat/tee.c
+++ b/libavformat/tee.c
@@ -284,14 +284,11 @@ static int open_slave(AVFormatContext *avf, char *slave, TeeSlave *tee_slave)
}
tee_slave->stream_map[i] = stream_count++;
- if (!(st2 = avformat_new_stream(avf2, NULL))) {
+ st2 = ff_stream_clone(avf2, st);
+ if (!st2) {
ret = AVERROR(ENOMEM);
goto end;
}
-
- ret = ff_stream_encode_params_copy(st2, st);
- if (ret < 0)
- goto end;
}
ret = ff_format_output_open(avf2, filename, &options);
diff --git a/libavformat/webm_chunk.c b/libavformat/webm_chunk.c
index d69db3a004..9e71a1209d 100644
--- a/libavformat/webm_chunk.c
+++ b/libavformat/webm_chunk.c
@@ -91,12 +91,10 @@ static int webm_chunk_init(AVFormatContext *s)
if ((ret = av_dict_copy(&oc->metadata, s->metadata, 0)) < 0)
return ret;
- if (!(st = avformat_new_stream(oc, NULL)))
+ st = ff_stream_clone(oc, ost);
+ if (!st)
return AVERROR(ENOMEM);
- if ((ret = ff_stream_encode_params_copy(st, ost)) < 0)
- return ret;
-
if (wc->http_method)
if ((ret = av_dict_set(&dict, "method", wc->http_method, 0)) < 0)
return ret;
--
2.25.1
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
^ permalink raw reply [flat|nested] 6+ messages in thread
* [FFmpeg-devel] [PATCH v7 2/2] avformat/imfdec: preserve stream information
2022-08-06 23:35 [FFmpeg-devel] [PATCH v7 1/2] avformat: refactor ff_stream_encode_params_copy() to stream_params_copy() pal
@ 2022-08-06 23:35 ` pal
2022-08-08 14:48 ` [FFmpeg-devel] [PATCH v7 1/2] avformat: refactor ff_stream_encode_params_copy() to stream_params_copy() Andreas Rheinhardt
1 sibling, 0 replies; 6+ messages in thread
From: pal @ 2022-08-06 23:35 UTC (permalink / raw)
To: ffmpeg-devel; +Cc: Pierre-Anthony Lemieux
From: Pierre-Anthony Lemieux <pal@palemieux.com>
---
libavformat/imfdec.c | 12 ++++--------
1 file changed, 4 insertions(+), 8 deletions(-)
diff --git a/libavformat/imfdec.c b/libavformat/imfdec.c
index 71dfb26958..5bbe7a53f8 100644
--- a/libavformat/imfdec.c
+++ b/libavformat/imfdec.c
@@ -573,18 +573,14 @@ static int set_context_streams_from_tracks(AVFormatContext *s)
first_resource_stream = c->tracks[i]->resources[0].ctx->streams[0];
av_log(s, AV_LOG_DEBUG, "Open the first resource of track %d\n", c->tracks[i]->index);
- /* Copy stream information */
- asset_stream = avformat_new_stream(s, NULL);
+ asset_stream = ff_stream_clone(s, first_resource_stream);
if (!asset_stream) {
- av_log(s, AV_LOG_ERROR, "Could not create stream\n");
+ av_log(s, AV_LOG_ERROR, "Could not clone stream\n");
return AVERROR(ENOMEM);
}
+
asset_stream->id = i;
- ret = avcodec_parameters_copy(asset_stream->codecpar, first_resource_stream->codecpar);
- if (ret < 0) {
- av_log(s, AV_LOG_ERROR, "Could not copy stream parameters\n");
- return ret;
- }
+ asset_stream->nb_frames = 0;
avpriv_set_pts_info(asset_stream,
first_resource_stream->pts_wrap_bits,
first_resource_stream->time_base.num,
--
2.25.1
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [FFmpeg-devel] [PATCH v7 1/2] avformat: refactor ff_stream_encode_params_copy() to stream_params_copy()
2022-08-06 23:35 [FFmpeg-devel] [PATCH v7 1/2] avformat: refactor ff_stream_encode_params_copy() to stream_params_copy() pal
2022-08-06 23:35 ` [FFmpeg-devel] [PATCH v7 2/2] avformat/imfdec: preserve stream information pal
@ 2022-08-08 14:48 ` Andreas Rheinhardt
2022-08-08 14:50 ` Pierre-Anthony Lemieux
1 sibling, 1 reply; 6+ messages in thread
From: Andreas Rheinhardt @ 2022-08-08 14:48 UTC (permalink / raw)
To: ffmpeg-devel
pal@sandflow.com:
> From: Pierre-Anthony Lemieux <pal@palemieux.com>
>
> Addresses http://ffmpeg.org/pipermail/ffmpeg-devel/2022-August/299726.html
>
> ---
> libavformat/avformat.c | 66 ++++++++++++++++++++++++++++++++++++++++
> libavformat/fifo.c | 8 ++---
> libavformat/internal.h | 11 +++++++
> libavformat/mux.h | 9 ------
> libavformat/mux_utils.c | 28 -----------------
> libavformat/segment.c | 6 ++--
> libavformat/tee.c | 7 ++---
> libavformat/webm_chunk.c | 6 ++--
> 8 files changed, 86 insertions(+), 55 deletions(-)
>
> diff --git a/libavformat/avformat.c b/libavformat/avformat.c
> index 30d6ea6a49..19c7219471 100644
> --- a/libavformat/avformat.c
> +++ b/libavformat/avformat.c
> @@ -235,6 +235,72 @@ int ff_stream_side_data_copy(AVStream *dst, const AVStream *src)
> return 0;
> }
>
> +/**
> + * Copy all stream parameters from source to destination stream, with the
> + * exception of the index field, which is usually set by avformat_new_stream().
> + *
> + * @param dst pointer to destination AVStream
> + * @param src pointer to source AVStream
> + * @return >=0 on success, AVERROR code on error
> + */
> +static int stream_params_copy(AVStream *dst, const AVStream *src)
> +{
> + int ret;
> +
> + dst->id = src->id;
> + dst->time_base = src->time_base;
> + dst->start_time = src->start_time;
> + dst->duration = src->duration;
> + dst->nb_frames = src->nb_frames;
> + dst->disposition = src->disposition;
> + dst->discard = src->discard;
> + dst->sample_aspect_ratio = src->sample_aspect_ratio;
> + dst->avg_frame_rate = src->avg_frame_rate;
> + dst->event_flags = src->event_flags;
> + dst->r_frame_rate = src->r_frame_rate;
> + dst->pts_wrap_bits = src->pts_wrap_bits;
> +
> + av_dict_free(&dst->metadata);
> + ret = av_dict_copy(&dst->metadata, src->metadata, 0);
> + if (ret < 0)
> + return ret;
> +
> + ret = avcodec_parameters_copy(dst->codecpar, src->codecpar);
> + if (ret < 0)
> + return ret;
> +
> + ret = ff_stream_side_data_copy(dst, src);
> + if (ret < 0)
> + return ret;
> +
> + av_packet_unref(&dst->attached_pic);
> + if (src->attached_pic.data) {
> + ret = av_packet_ref(&dst->attached_pic, &src->attached_pic);
> + if (ret < 0)
> + return ret;
> + }
> +
> + return 0;
> +}
> +
> +AVStream *ff_stream_clone(AVFormatContext *dst_ctx, const AVStream *src)
> +{
> + AVStream *st;
> + int ret;
> +
> + st = avformat_new_stream(dst_ctx, NULL);
> + if (!st)
> + return NULL;
> +
> + ret = stream_params_copy(st, src);
> + if (ret < 0) {
> + ff_remove_stream(dst_ctx, st);
> + return NULL;
> + }
> +
> + return st;
> +}
> +
> AVProgram *av_new_program(AVFormatContext *ac, int id)
> {
> AVProgram *program = NULL;
> diff --git a/libavformat/fifo.c b/libavformat/fifo.c
> index ead2bdc5cf..55d413b952 100644
> --- a/libavformat/fifo.c
> +++ b/libavformat/fifo.c
> @@ -505,13 +505,11 @@ static int fifo_mux_init(AVFormatContext *avf, const AVOutputFormat *oformat,
> avf2->flags = avf->flags;
>
> for (i = 0; i < avf->nb_streams; ++i) {
> - AVStream *st = avformat_new_stream(avf2, NULL);
> + AVStream *st = NULL;
> +
> + st = ff_stream_clone(avf2, avf->streams[i]);
I don't know why you stopped initializing st directly with its eventual
value (if I see this corrently, the line wouldn't be overlong). I can
fix this upon commit if you allow. Anyway, I will apply this in two days
unless there are any comments from anyone else.
> if (!st)
> return AVERROR(ENOMEM);
> -
> - ret = ff_stream_encode_params_copy(st, avf->streams[i]);
> - if (ret < 0)
> - return ret;
> }
>
> return 0;
> diff --git a/libavformat/internal.h b/libavformat/internal.h
> index b6b8fbf56f..9b07cfb271 100644
> --- a/libavformat/internal.h
> +++ b/libavformat/internal.h
> @@ -625,6 +625,17 @@ enum AVCodecID ff_get_pcm_codec_id(int bps, int flt, int be, int sflags);
> */
> int ff_stream_side_data_copy(AVStream *dst, const AVStream *src);
>
> +/**
> + * Create a new stream and copy to it all parameters from a source stream, with
> + * the exception of the index field, which is set when the new stream is
> + * created.
> + *
> + * @param dst_ctx pointer to the context in which the new stream is created
> + * @param src pointer to source AVStream
> + * @return pointer to the new stream or NULL on error
> + */
> +AVStream *ff_stream_clone(AVFormatContext *dst_ctx, const AVStream *src);
> +
> /**
> * Wrap ffurl_move() and log if error happens.
> *
> diff --git a/libavformat/mux.h b/libavformat/mux.h
> index c01da82194..1bfcaf795f 100644
> --- a/libavformat/mux.h
> +++ b/libavformat/mux.h
> @@ -113,15 +113,6 @@ int ff_format_shift_data(AVFormatContext *s, int64_t read_start, int shift_size)
> */
> int ff_format_output_open(AVFormatContext *s, const char *url, AVDictionary **options);
>
> -/**
> - * Copy encoding parameters from source to destination stream
> - *
> - * @param dst pointer to destination AVStream
> - * @param src pointer to source AVStream
> - * @return >=0 on success, AVERROR code on error
> - */
> -int ff_stream_encode_params_copy(AVStream *dst, const AVStream *src);
> -
> /**
> * Parse creation_time in AVFormatContext metadata if exists and warn if the
> * parsing fails.
> diff --git a/libavformat/mux_utils.c b/libavformat/mux_utils.c
> index eb8ea3d560..2fa2ab5b0f 100644
> --- a/libavformat/mux_utils.c
> +++ b/libavformat/mux_utils.c
> @@ -121,34 +121,6 @@ int ff_format_output_open(AVFormatContext *s, const char *url, AVDictionary **op
> return 0;
> }
>
> -int ff_stream_encode_params_copy(AVStream *dst, const AVStream *src)
> -{
> - int ret;
> -
> - dst->id = src->id;
> - dst->time_base = src->time_base;
> - dst->nb_frames = src->nb_frames;
> - dst->disposition = src->disposition;
> - dst->sample_aspect_ratio = src->sample_aspect_ratio;
> - dst->avg_frame_rate = src->avg_frame_rate;
> - dst->r_frame_rate = src->r_frame_rate;
> -
> - av_dict_free(&dst->metadata);
> - ret = av_dict_copy(&dst->metadata, src->metadata, 0);
> - if (ret < 0)
> - return ret;
> -
> - ret = avcodec_parameters_copy(dst->codecpar, src->codecpar);
> - if (ret < 0)
> - return ret;
> -
> - ret = ff_stream_side_data_copy(dst, src);
> - if (ret < 0)
> - return ret;
> -
> - return 0;
> -}
> -
> int ff_parse_creation_time_metadata(AVFormatContext *s, int64_t *timestamp, int return_seconds)
> {
> AVDictionaryEntry *entry;
> diff --git a/libavformat/segment.c b/libavformat/segment.c
> index fa435d9f32..c904e20708 100644
> --- a/libavformat/segment.c
> +++ b/libavformat/segment.c
> @@ -167,11 +167,9 @@ static int segment_mux_init(AVFormatContext *s)
> AVStream *st, *ist = s->streams[i];
> AVCodecParameters *ipar = ist->codecpar, *opar;
>
> - if (!(st = avformat_new_stream(oc, NULL)))
> + st = ff_stream_clone(oc, ist);
> + if (!st)
> return AVERROR(ENOMEM);
> - ret = ff_stream_encode_params_copy(st, ist);
> - if (ret < 0)
> - return ret;
> opar = st->codecpar;
> if (!oc->oformat->codec_tag ||
> av_codec_get_id (oc->oformat->codec_tag, ipar->codec_tag) == opar->codec_id ||
> diff --git a/libavformat/tee.c b/libavformat/tee.c
> index f1f2a9d2a5..dd408dd096 100644
> --- a/libavformat/tee.c
> +++ b/libavformat/tee.c
> @@ -284,14 +284,11 @@ static int open_slave(AVFormatContext *avf, char *slave, TeeSlave *tee_slave)
> }
> tee_slave->stream_map[i] = stream_count++;
>
> - if (!(st2 = avformat_new_stream(avf2, NULL))) {
> + st2 = ff_stream_clone(avf2, st);
> + if (!st2) {
> ret = AVERROR(ENOMEM);
> goto end;
> }
> -
> - ret = ff_stream_encode_params_copy(st2, st);
> - if (ret < 0)
> - goto end;
> }
>
> ret = ff_format_output_open(avf2, filename, &options);
> diff --git a/libavformat/webm_chunk.c b/libavformat/webm_chunk.c
> index d69db3a004..9e71a1209d 100644
> --- a/libavformat/webm_chunk.c
> +++ b/libavformat/webm_chunk.c
> @@ -91,12 +91,10 @@ static int webm_chunk_init(AVFormatContext *s)
> if ((ret = av_dict_copy(&oc->metadata, s->metadata, 0)) < 0)
> return ret;
>
> - if (!(st = avformat_new_stream(oc, NULL)))
> + st = ff_stream_clone(oc, ost);
> + if (!st)
> return AVERROR(ENOMEM);
>
> - if ((ret = ff_stream_encode_params_copy(st, ost)) < 0)
> - return ret;
> -
> if (wc->http_method)
> if ((ret = av_dict_set(&dict, "method", wc->http_method, 0)) < 0)
> return ret;
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [FFmpeg-devel] [PATCH v7 1/2] avformat: refactor ff_stream_encode_params_copy() to stream_params_copy()
2022-08-08 14:48 ` [FFmpeg-devel] [PATCH v7 1/2] avformat: refactor ff_stream_encode_params_copy() to stream_params_copy() Andreas Rheinhardt
@ 2022-08-08 14:50 ` Pierre-Anthony Lemieux
2022-08-10 1:05 ` Pierre-Anthony Lemieux
0 siblings, 1 reply; 6+ messages in thread
From: Pierre-Anthony Lemieux @ 2022-08-08 14:50 UTC (permalink / raw)
To: FFmpeg development discussions and patches
On Mon, Aug 8, 2022 at 7:48 AM Andreas Rheinhardt
<andreas.rheinhardt@outlook.com> wrote:
>
> pal@sandflow.com:
> > From: Pierre-Anthony Lemieux <pal@palemieux.com>
> >
> > Addresses http://ffmpeg.org/pipermail/ffmpeg-devel/2022-August/299726.html
> >
> > ---
> > libavformat/avformat.c | 66 ++++++++++++++++++++++++++++++++++++++++
> > libavformat/fifo.c | 8 ++---
> > libavformat/internal.h | 11 +++++++
> > libavformat/mux.h | 9 ------
> > libavformat/mux_utils.c | 28 -----------------
> > libavformat/segment.c | 6 ++--
> > libavformat/tee.c | 7 ++---
> > libavformat/webm_chunk.c | 6 ++--
> > 8 files changed, 86 insertions(+), 55 deletions(-)
> >
> > diff --git a/libavformat/avformat.c b/libavformat/avformat.c
> > index 30d6ea6a49..19c7219471 100644
> > --- a/libavformat/avformat.c
> > +++ b/libavformat/avformat.c
> > @@ -235,6 +235,72 @@ int ff_stream_side_data_copy(AVStream *dst, const AVStream *src)
> > return 0;
> > }
> >
> > +/**
> > + * Copy all stream parameters from source to destination stream, with the
> > + * exception of the index field, which is usually set by avformat_new_stream().
> > + *
> > + * @param dst pointer to destination AVStream
> > + * @param src pointer to source AVStream
> > + * @return >=0 on success, AVERROR code on error
> > + */
> > +static int stream_params_copy(AVStream *dst, const AVStream *src)
> > +{
> > + int ret;
> > +
> > + dst->id = src->id;
> > + dst->time_base = src->time_base;
> > + dst->start_time = src->start_time;
> > + dst->duration = src->duration;
> > + dst->nb_frames = src->nb_frames;
> > + dst->disposition = src->disposition;
> > + dst->discard = src->discard;
> > + dst->sample_aspect_ratio = src->sample_aspect_ratio;
> > + dst->avg_frame_rate = src->avg_frame_rate;
> > + dst->event_flags = src->event_flags;
> > + dst->r_frame_rate = src->r_frame_rate;
> > + dst->pts_wrap_bits = src->pts_wrap_bits;
> > +
> > + av_dict_free(&dst->metadata);
> > + ret = av_dict_copy(&dst->metadata, src->metadata, 0);
> > + if (ret < 0)
> > + return ret;
> > +
> > + ret = avcodec_parameters_copy(dst->codecpar, src->codecpar);
> > + if (ret < 0)
> > + return ret;
> > +
> > + ret = ff_stream_side_data_copy(dst, src);
> > + if (ret < 0)
> > + return ret;
> > +
> > + av_packet_unref(&dst->attached_pic);
> > + if (src->attached_pic.data) {
> > + ret = av_packet_ref(&dst->attached_pic, &src->attached_pic);
> > + if (ret < 0)
> > + return ret;
> > + }
> > +
> > + return 0;
> > +}
> > +
> > +AVStream *ff_stream_clone(AVFormatContext *dst_ctx, const AVStream *src)
> > +{
> > + AVStream *st;
> > + int ret;
> > +
> > + st = avformat_new_stream(dst_ctx, NULL);
> > + if (!st)
> > + return NULL;
> > +
> > + ret = stream_params_copy(st, src);
> > + if (ret < 0) {
> > + ff_remove_stream(dst_ctx, st);
> > + return NULL;
> > + }
> > +
> > + return st;
> > +}
> > +
> > AVProgram *av_new_program(AVFormatContext *ac, int id)
> > {
> > AVProgram *program = NULL;
> > diff --git a/libavformat/fifo.c b/libavformat/fifo.c
> > index ead2bdc5cf..55d413b952 100644
> > --- a/libavformat/fifo.c
> > +++ b/libavformat/fifo.c
> > @@ -505,13 +505,11 @@ static int fifo_mux_init(AVFormatContext *avf, const AVOutputFormat *oformat,
> > avf2->flags = avf->flags;
> >
> > for (i = 0; i < avf->nb_streams; ++i) {
> > - AVStream *st = avformat_new_stream(avf2, NULL);
> > + AVStream *st = NULL;
> > +
> > + st = ff_stream_clone(avf2, avf->streams[i]);
>
> I don't know why you stopped initializing st directly with its eventual
> value (if I see this corrently, the line wouldn't be overlong). I can
> fix this upon commit if you allow. Anyway, I will apply this in two days
> unless there are any comments from anyone else.
I thought if(<assignment>) was not favored.
Please modify as you see fit.
Thanks.
>
> > if (!st)
> > return AVERROR(ENOMEM);
> > -
> > - ret = ff_stream_encode_params_copy(st, avf->streams[i]);
> > - if (ret < 0)
> > - return ret;
> > }
> >
> > return 0;
> > diff --git a/libavformat/internal.h b/libavformat/internal.h
> > index b6b8fbf56f..9b07cfb271 100644
> > --- a/libavformat/internal.h
> > +++ b/libavformat/internal.h
> > @@ -625,6 +625,17 @@ enum AVCodecID ff_get_pcm_codec_id(int bps, int flt, int be, int sflags);
> > */
> > int ff_stream_side_data_copy(AVStream *dst, const AVStream *src);
> >
> > +/**
> > + * Create a new stream and copy to it all parameters from a source stream, with
> > + * the exception of the index field, which is set when the new stream is
> > + * created.
> > + *
> > + * @param dst_ctx pointer to the context in which the new stream is created
> > + * @param src pointer to source AVStream
> > + * @return pointer to the new stream or NULL on error
> > + */
> > +AVStream *ff_stream_clone(AVFormatContext *dst_ctx, const AVStream *src);
> > +
> > /**
> > * Wrap ffurl_move() and log if error happens.
> > *
> > diff --git a/libavformat/mux.h b/libavformat/mux.h
> > index c01da82194..1bfcaf795f 100644
> > --- a/libavformat/mux.h
> > +++ b/libavformat/mux.h
> > @@ -113,15 +113,6 @@ int ff_format_shift_data(AVFormatContext *s, int64_t read_start, int shift_size)
> > */
> > int ff_format_output_open(AVFormatContext *s, const char *url, AVDictionary **options);
> >
> > -/**
> > - * Copy encoding parameters from source to destination stream
> > - *
> > - * @param dst pointer to destination AVStream
> > - * @param src pointer to source AVStream
> > - * @return >=0 on success, AVERROR code on error
> > - */
> > -int ff_stream_encode_params_copy(AVStream *dst, const AVStream *src);
> > -
> > /**
> > * Parse creation_time in AVFormatContext metadata if exists and warn if the
> > * parsing fails.
> > diff --git a/libavformat/mux_utils.c b/libavformat/mux_utils.c
> > index eb8ea3d560..2fa2ab5b0f 100644
> > --- a/libavformat/mux_utils.c
> > +++ b/libavformat/mux_utils.c
> > @@ -121,34 +121,6 @@ int ff_format_output_open(AVFormatContext *s, const char *url, AVDictionary **op
> > return 0;
> > }
> >
> > -int ff_stream_encode_params_copy(AVStream *dst, const AVStream *src)
> > -{
> > - int ret;
> > -
> > - dst->id = src->id;
> > - dst->time_base = src->time_base;
> > - dst->nb_frames = src->nb_frames;
> > - dst->disposition = src->disposition;
> > - dst->sample_aspect_ratio = src->sample_aspect_ratio;
> > - dst->avg_frame_rate = src->avg_frame_rate;
> > - dst->r_frame_rate = src->r_frame_rate;
> > -
> > - av_dict_free(&dst->metadata);
> > - ret = av_dict_copy(&dst->metadata, src->metadata, 0);
> > - if (ret < 0)
> > - return ret;
> > -
> > - ret = avcodec_parameters_copy(dst->codecpar, src->codecpar);
> > - if (ret < 0)
> > - return ret;
> > -
> > - ret = ff_stream_side_data_copy(dst, src);
> > - if (ret < 0)
> > - return ret;
> > -
> > - return 0;
> > -}
> > -
> > int ff_parse_creation_time_metadata(AVFormatContext *s, int64_t *timestamp, int return_seconds)
> > {
> > AVDictionaryEntry *entry;
> > diff --git a/libavformat/segment.c b/libavformat/segment.c
> > index fa435d9f32..c904e20708 100644
> > --- a/libavformat/segment.c
> > +++ b/libavformat/segment.c
> > @@ -167,11 +167,9 @@ static int segment_mux_init(AVFormatContext *s)
> > AVStream *st, *ist = s->streams[i];
> > AVCodecParameters *ipar = ist->codecpar, *opar;
> >
> > - if (!(st = avformat_new_stream(oc, NULL)))
> > + st = ff_stream_clone(oc, ist);
> > + if (!st)
> > return AVERROR(ENOMEM);
> > - ret = ff_stream_encode_params_copy(st, ist);
> > - if (ret < 0)
> > - return ret;
> > opar = st->codecpar;
> > if (!oc->oformat->codec_tag ||
> > av_codec_get_id (oc->oformat->codec_tag, ipar->codec_tag) == opar->codec_id ||
> > diff --git a/libavformat/tee.c b/libavformat/tee.c
> > index f1f2a9d2a5..dd408dd096 100644
> > --- a/libavformat/tee.c
> > +++ b/libavformat/tee.c
> > @@ -284,14 +284,11 @@ static int open_slave(AVFormatContext *avf, char *slave, TeeSlave *tee_slave)
> > }
> > tee_slave->stream_map[i] = stream_count++;
> >
> > - if (!(st2 = avformat_new_stream(avf2, NULL))) {
> > + st2 = ff_stream_clone(avf2, st);
> > + if (!st2) {
> > ret = AVERROR(ENOMEM);
> > goto end;
> > }
> > -
> > - ret = ff_stream_encode_params_copy(st2, st);
> > - if (ret < 0)
> > - goto end;
> > }
> >
> > ret = ff_format_output_open(avf2, filename, &options);
> > diff --git a/libavformat/webm_chunk.c b/libavformat/webm_chunk.c
> > index d69db3a004..9e71a1209d 100644
> > --- a/libavformat/webm_chunk.c
> > +++ b/libavformat/webm_chunk.c
> > @@ -91,12 +91,10 @@ static int webm_chunk_init(AVFormatContext *s)
> > if ((ret = av_dict_copy(&oc->metadata, s->metadata, 0)) < 0)
> > return ret;
> >
> > - if (!(st = avformat_new_stream(oc, NULL)))
> > + st = ff_stream_clone(oc, ost);
> > + if (!st)
> > return AVERROR(ENOMEM);
> >
> > - if ((ret = ff_stream_encode_params_copy(st, ost)) < 0)
> > - return ret;
> > -
> > if (wc->http_method)
> > if ((ret = av_dict_set(&dict, "method", wc->http_method, 0)) < 0)
> > return ret;
>
> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
>
> To unsubscribe, visit link above, or email
> ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [FFmpeg-devel] [PATCH v7 1/2] avformat: refactor ff_stream_encode_params_copy() to stream_params_copy()
2022-08-08 14:50 ` Pierre-Anthony Lemieux
@ 2022-08-10 1:05 ` Pierre-Anthony Lemieux
2022-08-12 16:46 ` Pierre-Anthony Lemieux
0 siblings, 1 reply; 6+ messages in thread
From: Pierre-Anthony Lemieux @ 2022-08-10 1:05 UTC (permalink / raw)
To: FFmpeg development discussions and patches
On Mon, Aug 8, 2022 at 7:50 AM Pierre-Anthony Lemieux <pal@sandflow.com> wrote:
>
> On Mon, Aug 8, 2022 at 7:48 AM Andreas Rheinhardt
> <andreas.rheinhardt@outlook.com> wrote:
> >
> > pal@sandflow.com:
> > > From: Pierre-Anthony Lemieux <pal@palemieux.com>
> > >
> > > Addresses http://ffmpeg.org/pipermail/ffmpeg-devel/2022-August/299726.html
> > >
> > > ---
> > > libavformat/avformat.c | 66 ++++++++++++++++++++++++++++++++++++++++
> > > libavformat/fifo.c | 8 ++---
> > > libavformat/internal.h | 11 +++++++
> > > libavformat/mux.h | 9 ------
> > > libavformat/mux_utils.c | 28 -----------------
> > > libavformat/segment.c | 6 ++--
> > > libavformat/tee.c | 7 ++---
> > > libavformat/webm_chunk.c | 6 ++--
> > > 8 files changed, 86 insertions(+), 55 deletions(-)
> > >
> > > diff --git a/libavformat/avformat.c b/libavformat/avformat.c
> > > index 30d6ea6a49..19c7219471 100644
> > > --- a/libavformat/avformat.c
> > > +++ b/libavformat/avformat.c
> > > @@ -235,6 +235,72 @@ int ff_stream_side_data_copy(AVStream *dst, const AVStream *src)
> > > return 0;
> > > }
> > >
> > > +/**
> > > + * Copy all stream parameters from source to destination stream, with the
> > > + * exception of the index field, which is usually set by avformat_new_stream().
> > > + *
> > > + * @param dst pointer to destination AVStream
> > > + * @param src pointer to source AVStream
> > > + * @return >=0 on success, AVERROR code on error
> > > + */
> > > +static int stream_params_copy(AVStream *dst, const AVStream *src)
> > > +{
> > > + int ret;
> > > +
> > > + dst->id = src->id;
> > > + dst->time_base = src->time_base;
> > > + dst->start_time = src->start_time;
> > > + dst->duration = src->duration;
> > > + dst->nb_frames = src->nb_frames;
> > > + dst->disposition = src->disposition;
> > > + dst->discard = src->discard;
> > > + dst->sample_aspect_ratio = src->sample_aspect_ratio;
> > > + dst->avg_frame_rate = src->avg_frame_rate;
> > > + dst->event_flags = src->event_flags;
> > > + dst->r_frame_rate = src->r_frame_rate;
> > > + dst->pts_wrap_bits = src->pts_wrap_bits;
> > > +
> > > + av_dict_free(&dst->metadata);
> > > + ret = av_dict_copy(&dst->metadata, src->metadata, 0);
> > > + if (ret < 0)
> > > + return ret;
> > > +
> > > + ret = avcodec_parameters_copy(dst->codecpar, src->codecpar);
> > > + if (ret < 0)
> > > + return ret;
> > > +
> > > + ret = ff_stream_side_data_copy(dst, src);
> > > + if (ret < 0)
> > > + return ret;
> > > +
> > > + av_packet_unref(&dst->attached_pic);
> > > + if (src->attached_pic.data) {
> > > + ret = av_packet_ref(&dst->attached_pic, &src->attached_pic);
> > > + if (ret < 0)
> > > + return ret;
> > > + }
> > > +
> > > + return 0;
> > > +}
> > > +
> > > +AVStream *ff_stream_clone(AVFormatContext *dst_ctx, const AVStream *src)
> > > +{
> > > + AVStream *st;
> > > + int ret;
> > > +
> > > + st = avformat_new_stream(dst_ctx, NULL);
> > > + if (!st)
> > > + return NULL;
> > > +
> > > + ret = stream_params_copy(st, src);
> > > + if (ret < 0) {
> > > + ff_remove_stream(dst_ctx, st);
> > > + return NULL;
> > > + }
> > > +
> > > + return st;
> > > +}
> > > +
> > > AVProgram *av_new_program(AVFormatContext *ac, int id)
> > > {
> > > AVProgram *program = NULL;
> > > diff --git a/libavformat/fifo.c b/libavformat/fifo.c
> > > index ead2bdc5cf..55d413b952 100644
> > > --- a/libavformat/fifo.c
> > > +++ b/libavformat/fifo.c
> > > @@ -505,13 +505,11 @@ static int fifo_mux_init(AVFormatContext *avf, const AVOutputFormat *oformat,
> > > avf2->flags = avf->flags;
> > >
> > > for (i = 0; i < avf->nb_streams; ++i) {
> > > - AVStream *st = avformat_new_stream(avf2, NULL);
> > > + AVStream *st = NULL;
> > > +
> > > + st = ff_stream_clone(avf2, avf->streams[i]);
> >
> > I don't know why you stopped initializing st directly with its eventual
> > value (if I see this corrently, the line wouldn't be overlong). I can
> > fix this upon commit if you allow. Anyway, I will apply this in two days
> > unless there are any comments from anyone else.
>
> I thought if(<assignment>) was not favored.
>
> Please modify as you see fit.
>
> Thanks.
Couldn't this entire code block be simplified to the following?
for (i = 0; i < avf->nb_streams; ++i)
if (!ff_stream_clone(avf2, avf->streams[i]))
return AVERROR(ENOMEM);
Happy to submit a revised patch if it helps.
>
> >
> > > if (!st)
> > > return AVERROR(ENOMEM);
> > > -
> > > - ret = ff_stream_encode_params_copy(st, avf->streams[i]);
> > > - if (ret < 0)
> > > - return ret;
> > > }
> > >
> > > return 0;
> > > diff --git a/libavformat/internal.h b/libavformat/internal.h
> > > index b6b8fbf56f..9b07cfb271 100644
> > > --- a/libavformat/internal.h
> > > +++ b/libavformat/internal.h
> > > @@ -625,6 +625,17 @@ enum AVCodecID ff_get_pcm_codec_id(int bps, int flt, int be, int sflags);
> > > */
> > > int ff_stream_side_data_copy(AVStream *dst, const AVStream *src);
> > >
> > > +/**
> > > + * Create a new stream and copy to it all parameters from a source stream, with
> > > + * the exception of the index field, which is set when the new stream is
> > > + * created.
> > > + *
> > > + * @param dst_ctx pointer to the context in which the new stream is created
> > > + * @param src pointer to source AVStream
> > > + * @return pointer to the new stream or NULL on error
> > > + */
> > > +AVStream *ff_stream_clone(AVFormatContext *dst_ctx, const AVStream *src);
> > > +
> > > /**
> > > * Wrap ffurl_move() and log if error happens.
> > > *
> > > diff --git a/libavformat/mux.h b/libavformat/mux.h
> > > index c01da82194..1bfcaf795f 100644
> > > --- a/libavformat/mux.h
> > > +++ b/libavformat/mux.h
> > > @@ -113,15 +113,6 @@ int ff_format_shift_data(AVFormatContext *s, int64_t read_start, int shift_size)
> > > */
> > > int ff_format_output_open(AVFormatContext *s, const char *url, AVDictionary **options);
> > >
> > > -/**
> > > - * Copy encoding parameters from source to destination stream
> > > - *
> > > - * @param dst pointer to destination AVStream
> > > - * @param src pointer to source AVStream
> > > - * @return >=0 on success, AVERROR code on error
> > > - */
> > > -int ff_stream_encode_params_copy(AVStream *dst, const AVStream *src);
> > > -
> > > /**
> > > * Parse creation_time in AVFormatContext metadata if exists and warn if the
> > > * parsing fails.
> > > diff --git a/libavformat/mux_utils.c b/libavformat/mux_utils.c
> > > index eb8ea3d560..2fa2ab5b0f 100644
> > > --- a/libavformat/mux_utils.c
> > > +++ b/libavformat/mux_utils.c
> > > @@ -121,34 +121,6 @@ int ff_format_output_open(AVFormatContext *s, const char *url, AVDictionary **op
> > > return 0;
> > > }
> > >
> > > -int ff_stream_encode_params_copy(AVStream *dst, const AVStream *src)
> > > -{
> > > - int ret;
> > > -
> > > - dst->id = src->id;
> > > - dst->time_base = src->time_base;
> > > - dst->nb_frames = src->nb_frames;
> > > - dst->disposition = src->disposition;
> > > - dst->sample_aspect_ratio = src->sample_aspect_ratio;
> > > - dst->avg_frame_rate = src->avg_frame_rate;
> > > - dst->r_frame_rate = src->r_frame_rate;
> > > -
> > > - av_dict_free(&dst->metadata);
> > > - ret = av_dict_copy(&dst->metadata, src->metadata, 0);
> > > - if (ret < 0)
> > > - return ret;
> > > -
> > > - ret = avcodec_parameters_copy(dst->codecpar, src->codecpar);
> > > - if (ret < 0)
> > > - return ret;
> > > -
> > > - ret = ff_stream_side_data_copy(dst, src);
> > > - if (ret < 0)
> > > - return ret;
> > > -
> > > - return 0;
> > > -}
> > > -
> > > int ff_parse_creation_time_metadata(AVFormatContext *s, int64_t *timestamp, int return_seconds)
> > > {
> > > AVDictionaryEntry *entry;
> > > diff --git a/libavformat/segment.c b/libavformat/segment.c
> > > index fa435d9f32..c904e20708 100644
> > > --- a/libavformat/segment.c
> > > +++ b/libavformat/segment.c
> > > @@ -167,11 +167,9 @@ static int segment_mux_init(AVFormatContext *s)
> > > AVStream *st, *ist = s->streams[i];
> > > AVCodecParameters *ipar = ist->codecpar, *opar;
> > >
> > > - if (!(st = avformat_new_stream(oc, NULL)))
> > > + st = ff_stream_clone(oc, ist);
> > > + if (!st)
> > > return AVERROR(ENOMEM);
> > > - ret = ff_stream_encode_params_copy(st, ist);
> > > - if (ret < 0)
> > > - return ret;
> > > opar = st->codecpar;
> > > if (!oc->oformat->codec_tag ||
> > > av_codec_get_id (oc->oformat->codec_tag, ipar->codec_tag) == opar->codec_id ||
> > > diff --git a/libavformat/tee.c b/libavformat/tee.c
> > > index f1f2a9d2a5..dd408dd096 100644
> > > --- a/libavformat/tee.c
> > > +++ b/libavformat/tee.c
> > > @@ -284,14 +284,11 @@ static int open_slave(AVFormatContext *avf, char *slave, TeeSlave *tee_slave)
> > > }
> > > tee_slave->stream_map[i] = stream_count++;
> > >
> > > - if (!(st2 = avformat_new_stream(avf2, NULL))) {
> > > + st2 = ff_stream_clone(avf2, st);
> > > + if (!st2) {
> > > ret = AVERROR(ENOMEM);
> > > goto end;
> > > }
> > > -
> > > - ret = ff_stream_encode_params_copy(st2, st);
> > > - if (ret < 0)
> > > - goto end;
> > > }
> > >
> > > ret = ff_format_output_open(avf2, filename, &options);
> > > diff --git a/libavformat/webm_chunk.c b/libavformat/webm_chunk.c
> > > index d69db3a004..9e71a1209d 100644
> > > --- a/libavformat/webm_chunk.c
> > > +++ b/libavformat/webm_chunk.c
> > > @@ -91,12 +91,10 @@ static int webm_chunk_init(AVFormatContext *s)
> > > if ((ret = av_dict_copy(&oc->metadata, s->metadata, 0)) < 0)
> > > return ret;
> > >
> > > - if (!(st = avformat_new_stream(oc, NULL)))
> > > + st = ff_stream_clone(oc, ost);
> > > + if (!st)
> > > return AVERROR(ENOMEM);
> > >
> > > - if ((ret = ff_stream_encode_params_copy(st, ost)) < 0)
> > > - return ret;
> > > -
> > > if (wc->http_method)
> > > if ((ret = av_dict_set(&dict, "method", wc->http_method, 0)) < 0)
> > > return ret;
> >
> > _______________________________________________
> > ffmpeg-devel mailing list
> > ffmpeg-devel@ffmpeg.org
> > https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
> >
> > To unsubscribe, visit link above, or email
> > ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [FFmpeg-devel] [PATCH v7 1/2] avformat: refactor ff_stream_encode_params_copy() to stream_params_copy()
2022-08-10 1:05 ` Pierre-Anthony Lemieux
@ 2022-08-12 16:46 ` Pierre-Anthony Lemieux
0 siblings, 0 replies; 6+ messages in thread
From: Pierre-Anthony Lemieux @ 2022-08-12 16:46 UTC (permalink / raw)
To: FFmpeg development discussions and patches
On Tue, Aug 9, 2022 at 6:05 PM Pierre-Anthony Lemieux <pal@sandflow.com> wrote:
>
> On Mon, Aug 8, 2022 at 7:50 AM Pierre-Anthony Lemieux <pal@sandflow.com> wrote:
> >
> > On Mon, Aug 8, 2022 at 7:48 AM Andreas Rheinhardt
> > <andreas.rheinhardt@outlook.com> wrote:
> > >
> > > pal@sandflow.com:
> > > > From: Pierre-Anthony Lemieux <pal@palemieux.com>
> > > >
> > > > Addresses http://ffmpeg.org/pipermail/ffmpeg-devel/2022-August/299726.html
> > > >
> > > > ---
> > > > libavformat/avformat.c | 66 ++++++++++++++++++++++++++++++++++++++++
> > > > libavformat/fifo.c | 8 ++---
> > > > libavformat/internal.h | 11 +++++++
> > > > libavformat/mux.h | 9 ------
> > > > libavformat/mux_utils.c | 28 -----------------
> > > > libavformat/segment.c | 6 ++--
> > > > libavformat/tee.c | 7 ++---
> > > > libavformat/webm_chunk.c | 6 ++--
> > > > 8 files changed, 86 insertions(+), 55 deletions(-)
> > > >
> > > > diff --git a/libavformat/avformat.c b/libavformat/avformat.c
> > > > index 30d6ea6a49..19c7219471 100644
> > > > --- a/libavformat/avformat.c
> > > > +++ b/libavformat/avformat.c
> > > > @@ -235,6 +235,72 @@ int ff_stream_side_data_copy(AVStream *dst, const AVStream *src)
> > > > return 0;
> > > > }
> > > >
> > > > +/**
> > > > + * Copy all stream parameters from source to destination stream, with the
> > > > + * exception of the index field, which is usually set by avformat_new_stream().
> > > > + *
> > > > + * @param dst pointer to destination AVStream
> > > > + * @param src pointer to source AVStream
> > > > + * @return >=0 on success, AVERROR code on error
> > > > + */
> > > > +static int stream_params_copy(AVStream *dst, const AVStream *src)
> > > > +{
> > > > + int ret;
> > > > +
> > > > + dst->id = src->id;
> > > > + dst->time_base = src->time_base;
> > > > + dst->start_time = src->start_time;
> > > > + dst->duration = src->duration;
> > > > + dst->nb_frames = src->nb_frames;
> > > > + dst->disposition = src->disposition;
> > > > + dst->discard = src->discard;
> > > > + dst->sample_aspect_ratio = src->sample_aspect_ratio;
> > > > + dst->avg_frame_rate = src->avg_frame_rate;
> > > > + dst->event_flags = src->event_flags;
> > > > + dst->r_frame_rate = src->r_frame_rate;
> > > > + dst->pts_wrap_bits = src->pts_wrap_bits;
> > > > +
> > > > + av_dict_free(&dst->metadata);
> > > > + ret = av_dict_copy(&dst->metadata, src->metadata, 0);
> > > > + if (ret < 0)
> > > > + return ret;
> > > > +
> > > > + ret = avcodec_parameters_copy(dst->codecpar, src->codecpar);
> > > > + if (ret < 0)
> > > > + return ret;
> > > > +
> > > > + ret = ff_stream_side_data_copy(dst, src);
> > > > + if (ret < 0)
> > > > + return ret;
> > > > +
> > > > + av_packet_unref(&dst->attached_pic);
> > > > + if (src->attached_pic.data) {
> > > > + ret = av_packet_ref(&dst->attached_pic, &src->attached_pic);
> > > > + if (ret < 0)
> > > > + return ret;
> > > > + }
> > > > +
> > > > + return 0;
> > > > +}
> > > > +
> > > > +AVStream *ff_stream_clone(AVFormatContext *dst_ctx, const AVStream *src)
> > > > +{
> > > > + AVStream *st;
> > > > + int ret;
> > > > +
> > > > + st = avformat_new_stream(dst_ctx, NULL);
> > > > + if (!st)
> > > > + return NULL;
> > > > +
> > > > + ret = stream_params_copy(st, src);
> > > > + if (ret < 0) {
> > > > + ff_remove_stream(dst_ctx, st);
> > > > + return NULL;
> > > > + }
> > > > +
> > > > + return st;
> > > > +}
> > > > +
> > > > AVProgram *av_new_program(AVFormatContext *ac, int id)
> > > > {
> > > > AVProgram *program = NULL;
> > > > diff --git a/libavformat/fifo.c b/libavformat/fifo.c
> > > > index ead2bdc5cf..55d413b952 100644
> > > > --- a/libavformat/fifo.c
> > > > +++ b/libavformat/fifo.c
> > > > @@ -505,13 +505,11 @@ static int fifo_mux_init(AVFormatContext *avf, const AVOutputFormat *oformat,
> > > > avf2->flags = avf->flags;
> > > >
> > > > for (i = 0; i < avf->nb_streams; ++i) {
> > > > - AVStream *st = avformat_new_stream(avf2, NULL);
> > > > + AVStream *st = NULL;
> > > > +
> > > > + st = ff_stream_clone(avf2, avf->streams[i]);
> > >
> > > I don't know why you stopped initializing st directly with its eventual
> > > value (if I see this corrently, the line wouldn't be overlong). I can
> > > fix this upon commit if you allow. Anyway, I will apply this in two days
> > > unless there are any comments from anyone else.
> >
> > I thought if(<assignment>) was not favored.
> >
> > Please modify as you see fit.
> >
> > Thanks.
>
> Couldn't this entire code block be simplified to the following?
>
> for (i = 0; i < avf->nb_streams; ++i)
> if (!ff_stream_clone(avf2, avf->streams[i]))
> return AVERROR(ENOMEM);
>
> Happy to submit a revised patch if it helps.
See v8.
>
>
> >
> > >
> > > > if (!st)
> > > > return AVERROR(ENOMEM);
> > > > -
> > > > - ret = ff_stream_encode_params_copy(st, avf->streams[i]);
> > > > - if (ret < 0)
> > > > - return ret;
> > > > }
> > > >
> > > > return 0;
> > > > diff --git a/libavformat/internal.h b/libavformat/internal.h
> > > > index b6b8fbf56f..9b07cfb271 100644
> > > > --- a/libavformat/internal.h
> > > > +++ b/libavformat/internal.h
> > > > @@ -625,6 +625,17 @@ enum AVCodecID ff_get_pcm_codec_id(int bps, int flt, int be, int sflags);
> > > > */
> > > > int ff_stream_side_data_copy(AVStream *dst, const AVStream *src);
> > > >
> > > > +/**
> > > > + * Create a new stream and copy to it all parameters from a source stream, with
> > > > + * the exception of the index field, which is set when the new stream is
> > > > + * created.
> > > > + *
> > > > + * @param dst_ctx pointer to the context in which the new stream is created
> > > > + * @param src pointer to source AVStream
> > > > + * @return pointer to the new stream or NULL on error
> > > > + */
> > > > +AVStream *ff_stream_clone(AVFormatContext *dst_ctx, const AVStream *src);
> > > > +
> > > > /**
> > > > * Wrap ffurl_move() and log if error happens.
> > > > *
> > > > diff --git a/libavformat/mux.h b/libavformat/mux.h
> > > > index c01da82194..1bfcaf795f 100644
> > > > --- a/libavformat/mux.h
> > > > +++ b/libavformat/mux.h
> > > > @@ -113,15 +113,6 @@ int ff_format_shift_data(AVFormatContext *s, int64_t read_start, int shift_size)
> > > > */
> > > > int ff_format_output_open(AVFormatContext *s, const char *url, AVDictionary **options);
> > > >
> > > > -/**
> > > > - * Copy encoding parameters from source to destination stream
> > > > - *
> > > > - * @param dst pointer to destination AVStream
> > > > - * @param src pointer to source AVStream
> > > > - * @return >=0 on success, AVERROR code on error
> > > > - */
> > > > -int ff_stream_encode_params_copy(AVStream *dst, const AVStream *src);
> > > > -
> > > > /**
> > > > * Parse creation_time in AVFormatContext metadata if exists and warn if the
> > > > * parsing fails.
> > > > diff --git a/libavformat/mux_utils.c b/libavformat/mux_utils.c
> > > > index eb8ea3d560..2fa2ab5b0f 100644
> > > > --- a/libavformat/mux_utils.c
> > > > +++ b/libavformat/mux_utils.c
> > > > @@ -121,34 +121,6 @@ int ff_format_output_open(AVFormatContext *s, const char *url, AVDictionary **op
> > > > return 0;
> > > > }
> > > >
> > > > -int ff_stream_encode_params_copy(AVStream *dst, const AVStream *src)
> > > > -{
> > > > - int ret;
> > > > -
> > > > - dst->id = src->id;
> > > > - dst->time_base = src->time_base;
> > > > - dst->nb_frames = src->nb_frames;
> > > > - dst->disposition = src->disposition;
> > > > - dst->sample_aspect_ratio = src->sample_aspect_ratio;
> > > > - dst->avg_frame_rate = src->avg_frame_rate;
> > > > - dst->r_frame_rate = src->r_frame_rate;
> > > > -
> > > > - av_dict_free(&dst->metadata);
> > > > - ret = av_dict_copy(&dst->metadata, src->metadata, 0);
> > > > - if (ret < 0)
> > > > - return ret;
> > > > -
> > > > - ret = avcodec_parameters_copy(dst->codecpar, src->codecpar);
> > > > - if (ret < 0)
> > > > - return ret;
> > > > -
> > > > - ret = ff_stream_side_data_copy(dst, src);
> > > > - if (ret < 0)
> > > > - return ret;
> > > > -
> > > > - return 0;
> > > > -}
> > > > -
> > > > int ff_parse_creation_time_metadata(AVFormatContext *s, int64_t *timestamp, int return_seconds)
> > > > {
> > > > AVDictionaryEntry *entry;
> > > > diff --git a/libavformat/segment.c b/libavformat/segment.c
> > > > index fa435d9f32..c904e20708 100644
> > > > --- a/libavformat/segment.c
> > > > +++ b/libavformat/segment.c
> > > > @@ -167,11 +167,9 @@ static int segment_mux_init(AVFormatContext *s)
> > > > AVStream *st, *ist = s->streams[i];
> > > > AVCodecParameters *ipar = ist->codecpar, *opar;
> > > >
> > > > - if (!(st = avformat_new_stream(oc, NULL)))
> > > > + st = ff_stream_clone(oc, ist);
> > > > + if (!st)
> > > > return AVERROR(ENOMEM);
> > > > - ret = ff_stream_encode_params_copy(st, ist);
> > > > - if (ret < 0)
> > > > - return ret;
> > > > opar = st->codecpar;
> > > > if (!oc->oformat->codec_tag ||
> > > > av_codec_get_id (oc->oformat->codec_tag, ipar->codec_tag) == opar->codec_id ||
> > > > diff --git a/libavformat/tee.c b/libavformat/tee.c
> > > > index f1f2a9d2a5..dd408dd096 100644
> > > > --- a/libavformat/tee.c
> > > > +++ b/libavformat/tee.c
> > > > @@ -284,14 +284,11 @@ static int open_slave(AVFormatContext *avf, char *slave, TeeSlave *tee_slave)
> > > > }
> > > > tee_slave->stream_map[i] = stream_count++;
> > > >
> > > > - if (!(st2 = avformat_new_stream(avf2, NULL))) {
> > > > + st2 = ff_stream_clone(avf2, st);
> > > > + if (!st2) {
> > > > ret = AVERROR(ENOMEM);
> > > > goto end;
> > > > }
> > > > -
> > > > - ret = ff_stream_encode_params_copy(st2, st);
> > > > - if (ret < 0)
> > > > - goto end;
> > > > }
> > > >
> > > > ret = ff_format_output_open(avf2, filename, &options);
> > > > diff --git a/libavformat/webm_chunk.c b/libavformat/webm_chunk.c
> > > > index d69db3a004..9e71a1209d 100644
> > > > --- a/libavformat/webm_chunk.c
> > > > +++ b/libavformat/webm_chunk.c
> > > > @@ -91,12 +91,10 @@ static int webm_chunk_init(AVFormatContext *s)
> > > > if ((ret = av_dict_copy(&oc->metadata, s->metadata, 0)) < 0)
> > > > return ret;
> > > >
> > > > - if (!(st = avformat_new_stream(oc, NULL)))
> > > > + st = ff_stream_clone(oc, ost);
> > > > + if (!st)
> > > > return AVERROR(ENOMEM);
> > > >
> > > > - if ((ret = ff_stream_encode_params_copy(st, ost)) < 0)
> > > > - return ret;
> > > > -
> > > > if (wc->http_method)
> > > > if ((ret = av_dict_set(&dict, "method", wc->http_method, 0)) < 0)
> > > > return ret;
> > >
> > > _______________________________________________
> > > ffmpeg-devel mailing list
> > > ffmpeg-devel@ffmpeg.org
> > > https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
> > >
> > > To unsubscribe, visit link above, or email
> > > ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2022-08-12 16:46 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-08-06 23:35 [FFmpeg-devel] [PATCH v7 1/2] avformat: refactor ff_stream_encode_params_copy() to stream_params_copy() pal
2022-08-06 23:35 ` [FFmpeg-devel] [PATCH v7 2/2] avformat/imfdec: preserve stream information pal
2022-08-08 14:48 ` [FFmpeg-devel] [PATCH v7 1/2] avformat: refactor ff_stream_encode_params_copy() to stream_params_copy() Andreas Rheinhardt
2022-08-08 14:50 ` Pierre-Anthony Lemieux
2022-08-10 1:05 ` Pierre-Anthony Lemieux
2022-08-12 16:46 ` Pierre-Anthony Lemieux
Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
This inbox may be cloned and mirrored by anyone:
git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git
# If you have public-inbox 1.1+ installed, you may
# initialize and index your mirror using the following commands:
public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \
ffmpegdev@gitmailbox.com
public-inbox-index ffmpegdev
Example config snippet for mirrors.
AGPL code for this site: git clone https://public-inbox.org/public-inbox.git