[FFmpeg-devel] [PATCH] avformat/yuv4mpegenc: simplify writing the header

Paul B Mahol onemda at gmail.com
Thu Sep 3 22:16:35 EEST 2020


On 9/3/20, James Almer <jamrial at gmail.com> wrote:
> Actually write it in yuv4_write_header() instead of with the first
> packet.
>
> Signed-off-by: James Almer <jamrial at gmail.com>
> ---
>  libavformat/yuv4mpegenc.c | 35 ++++++++++++++---------------------
>  1 file changed, 14 insertions(+), 21 deletions(-)
>

Nice catch, LGTM

> diff --git a/libavformat/yuv4mpegenc.c b/libavformat/yuv4mpegenc.c
> index e84dbf9568..c4781042bd 100644
> --- a/libavformat/yuv4mpegenc.c
> +++ b/libavformat/yuv4mpegenc.c
> @@ -26,14 +26,16 @@
>
>  #define Y4M_LINE_MAX 256
>
> -static int yuv4_generate_header(AVFormatContext *s, char* buf)
> +static int yuv4_write_header(AVFormatContext *s)
>  {
>      AVStream *st;
> +    AVIOContext *pb = s->pb;
>      int width, height;
>      int raten, rated, aspectn, aspectd, n;
>      char inter;
>      const char *colorspace = "";
>      const char *colorrange = "";
> +    char buf[Y4M_LINE_MAX + 1];
>      int field_order;
>
>      st     = s->streams[0];
> @@ -173,7 +175,15 @@ static int yuv4_generate_header(AVFormatContext *s,
> char* buf)
>                   Y4M_MAGIC, width, height, raten, rated, inter,
>                   aspectn, aspectd, colorspace, colorrange);
>
> -    return n;
> +    if (n < 0) {
> +        av_log(s, AV_LOG_ERROR,
> +               "Error. YUV4MPEG stream header write failed.\n");
> +        return AVERROR(EIO);
> +    }
> +
> +    avio_write(pb, buf, strlen(buf));
> +
> +    return 0;
>  }
>
>
> @@ -182,26 +192,12 @@ static int yuv4_write_packet(AVFormatContext *s,
> AVPacket *pkt)
>      AVStream *st = s->streams[pkt->stream_index];
>      AVIOContext *pb = s->pb;
>      AVFrame *frame;
> -    int* first_pkt = s->priv_data;
>      int width, height, h_chroma_shift, v_chroma_shift;
>      int i;
> -    char buf2[Y4M_LINE_MAX + 1];
>      uint8_t *ptr, *ptr1, *ptr2;
>
>      frame = (AVFrame *)pkt->data;
>
> -    /* for the first packet we have to output the header as well */
> -    if (*first_pkt) {
> -        *first_pkt = 0;
> -        if (yuv4_generate_header(s, buf2) < 0) {
> -            av_log(s, AV_LOG_ERROR,
> -                   "Error. YUV4MPEG stream header write failed.\n");
> -            return AVERROR(EIO);
> -        } else {
> -            avio_write(pb, buf2, strlen(buf2));
> -        }
> -    }
> -
>      /* construct frame header */
>
>      avio_printf(s->pb, "%s\n", Y4M_FRAME_MAGIC);
> @@ -279,10 +275,8 @@ static int yuv4_write_packet(AVFormatContext *s,
> AVPacket *pkt)
>      return 0;
>  }
>
> -static int yuv4_write_header(AVFormatContext *s)
> +static int yuv4_init(AVFormatContext *s)
>  {
> -    int *first_pkt = s->priv_data;
> -
>      if (s->nb_streams != 1)
>          return AVERROR(EIO);
>
> @@ -347,7 +341,6 @@ static int yuv4_write_header(AVFormatContext *s)
>          return AVERROR(EIO);
>      }
>
> -    *first_pkt = 1;
>      return 0;
>  }
>
> @@ -355,9 +348,9 @@ AVOutputFormat ff_yuv4mpegpipe_muxer = {
>      .name              = "yuv4mpegpipe",
>      .long_name         = NULL_IF_CONFIG_SMALL("YUV4MPEG pipe"),
>      .extensions        = "y4m",
> -    .priv_data_size    = sizeof(int),
>      .audio_codec       = AV_CODEC_ID_NONE,
>      .video_codec       = AV_CODEC_ID_WRAPPED_AVFRAME,
> +    .init              = yuv4_init,
>      .write_header      = yuv4_write_header,
>      .write_packet      = yuv4_write_packet,
>  };
> --
> 2.27.0
>
> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel at ffmpeg.org
> https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
>
> To unsubscribe, visit link above, or email
> ffmpeg-devel-request at ffmpeg.org with subject "unsubscribe".


More information about the ffmpeg-devel mailing list