From: Andreas Rheinhardt <andreas.rheinhardt@outlook.com> To: ffmpeg-devel@ffmpeg.org Cc: Andreas Rheinhardt <andreas.rheinhardt@outlook.com> Subject: [FFmpeg-devel] [PATCH 07/25] avformat/matroskaenc: Factor writing TrackVideo out Date: Mon, 17 Jan 2022 00:03:47 +0100 Message-ID: <AM7PR03MB666075D1304F06C9ACEB48548F569@AM7PR03MB6660.eurprd03.prod.outlook.com> (raw) In-Reply-To: <AM7PR03MB66609FAEE5128E3BA3F57C0F8F569@AM7PR03MB6660.eurprd03.prod.outlook.com> It is already quite big. Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com> --- libavformat/matroskaenc.c | 130 +++++++++++++++++++++----------------- 1 file changed, 72 insertions(+), 58 deletions(-) diff --git a/libavformat/matroskaenc.c b/libavformat/matroskaenc.c index 4d4d270db1..2f5f1cc56d 100644 --- a/libavformat/matroskaenc.c +++ b/libavformat/matroskaenc.c @@ -1418,6 +1418,77 @@ static void mkv_write_dovi(AVFormatContext *s, AVIOContext *pb, AVStream *st) #endif } +static int mkv_write_track_video(AVFormatContext *s, MatroskaMuxContext *mkv, + AVStream *st, const AVCodecParameters *par, + AVIOContext *pb) +{ + const AVDictionaryEntry *tag; + int display_width_div = 1, display_height_div = 1; + ebml_master subinfo; + int ret; + + subinfo = start_ebml_master(pb, MATROSKA_ID_TRACKVIDEO, 0); + + put_ebml_uint (pb, MATROSKA_ID_VIDEOPIXELWIDTH , par->width); + put_ebml_uint (pb, MATROSKA_ID_VIDEOPIXELHEIGHT, par->height); + + mkv_write_field_order(pb, IS_WEBM(mkv), par->field_order); + + // check both side data and metadata for stereo information, + // write the result to the bitstream if any is found + ret = mkv_write_stereo_mode(s, pb, st, IS_WEBM(mkv), + &display_width_div, + &display_height_div); + if (ret < 0) + return ret; + + if (((tag = av_dict_get(st->metadata, "alpha_mode", NULL, 0)) && atoi(tag->value)) || + ((tag = av_dict_get( s->metadata, "alpha_mode", NULL, 0)) && atoi(tag->value)) || + (par->format == AV_PIX_FMT_YUVA420P)) { + put_ebml_uint(pb, MATROSKA_ID_VIDEOALPHAMODE, 1); + } + + // write DisplayWidth and DisplayHeight, they contain the size of + // a single source view and/or the display aspect ratio + if (st->sample_aspect_ratio.num) { + int64_t d_width = av_rescale(par->width, st->sample_aspect_ratio.num, st->sample_aspect_ratio.den); + if (d_width > INT_MAX) { + av_log(s, AV_LOG_ERROR, "Overflow in display width\n"); + return AVERROR(EINVAL); + } + if (d_width != par->width || display_width_div != 1 || display_height_div != 1) { + if (IS_WEBM(mkv) || display_width_div != 1 || display_height_div != 1) { + put_ebml_uint(pb, MATROSKA_ID_VIDEODISPLAYWIDTH , d_width / display_width_div); + put_ebml_uint(pb, MATROSKA_ID_VIDEODISPLAYHEIGHT, par->height / display_height_div); + } else { + AVRational display_aspect_ratio; + av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den, + par->width * (int64_t)st->sample_aspect_ratio.num, + par->height * (int64_t)st->sample_aspect_ratio.den, + 1024 * 1024); + put_ebml_uint(pb, MATROSKA_ID_VIDEODISPLAYWIDTH, display_aspect_ratio.num); + put_ebml_uint(pb, MATROSKA_ID_VIDEODISPLAYHEIGHT, display_aspect_ratio.den); + put_ebml_uint(pb, MATROSKA_ID_VIDEODISPLAYUNIT, MATROSKA_VIDEO_DISPLAYUNIT_DAR); + } + } + } else if (display_width_div != 1 || display_height_div != 1) { + put_ebml_uint(pb, MATROSKA_ID_VIDEODISPLAYWIDTH , par->width / display_width_div); + put_ebml_uint(pb, MATROSKA_ID_VIDEODISPLAYHEIGHT, par->height / display_height_div); + } else if (!IS_WEBM(mkv)) + put_ebml_uint(pb, MATROSKA_ID_VIDEODISPLAYUNIT, MATROSKA_VIDEO_DISPLAYUNIT_UNKNOWN); + + if (par->codec_id == AV_CODEC_ID_RAWVIDEO) { + uint32_t color_space = av_le2ne32(par->codec_tag); + put_ebml_binary(pb, MATROSKA_ID_VIDEOCOLORSPACE, &color_space, sizeof(color_space)); + } + mkv_write_video_color(pb, st, par); + mkv_write_video_projection(s, pb, st); + + end_ebml_master(pb, subinfo); + + return 0; +} + static int mkv_write_track(AVFormatContext *s, MatroskaMuxContext *mkv, AVStream *st, mkv_track *track, AVIOContext *pb, int is_default) @@ -1429,8 +1500,6 @@ static int mkv_write_track(AVFormatContext *s, MatroskaMuxContext *mkv, int bit_depth; int sample_rate = par->sample_rate; int output_sample_rate = 0; - int display_width_div = 1; - int display_height_div = 1; int j, ret; const AVDictionaryEntry *tag; @@ -1552,65 +1621,10 @@ static int mkv_write_track(AVFormatContext *s, MatroskaMuxContext *mkv, ffformatcontext(s)->avoid_negative_ts_use_pts = 0; } - subinfo = start_ebml_master(pb, MATROSKA_ID_TRACKVIDEO, 0); - - put_ebml_uint (pb, MATROSKA_ID_VIDEOPIXELWIDTH , par->width); - put_ebml_uint (pb, MATROSKA_ID_VIDEOPIXELHEIGHT, par->height); - - mkv_write_field_order(pb, IS_WEBM(mkv), par->field_order); - - // check both side data and metadata for stereo information, - // write the result to the bitstream if any is found - ret = mkv_write_stereo_mode(s, pb, st, IS_WEBM(mkv), - &display_width_div, - &display_height_div); + ret = mkv_write_track_video(s, mkv, st, par, pb); if (ret < 0) return ret; - if (((tag = av_dict_get(st->metadata, "alpha_mode", NULL, 0)) && atoi(tag->value)) || - ((tag = av_dict_get( s->metadata, "alpha_mode", NULL, 0)) && atoi(tag->value)) || - (par->format == AV_PIX_FMT_YUVA420P)) { - put_ebml_uint(pb, MATROSKA_ID_VIDEOALPHAMODE, 1); - } - - // write DisplayWidth and DisplayHeight, they contain the size of - // a single source view and/or the display aspect ratio - if (st->sample_aspect_ratio.num) { - int64_t d_width = av_rescale(par->width, st->sample_aspect_ratio.num, st->sample_aspect_ratio.den); - if (d_width > INT_MAX) { - av_log(s, AV_LOG_ERROR, "Overflow in display width\n"); - return AVERROR(EINVAL); - } - if (d_width != par->width || display_width_div != 1 || display_height_div != 1) { - if (IS_WEBM(mkv) || display_width_div != 1 || display_height_div != 1) { - put_ebml_uint(pb, MATROSKA_ID_VIDEODISPLAYWIDTH , d_width / display_width_div); - put_ebml_uint(pb, MATROSKA_ID_VIDEODISPLAYHEIGHT, par->height / display_height_div); - } else { - AVRational display_aspect_ratio; - av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den, - par->width * (int64_t)st->sample_aspect_ratio.num, - par->height * (int64_t)st->sample_aspect_ratio.den, - 1024 * 1024); - put_ebml_uint(pb, MATROSKA_ID_VIDEODISPLAYWIDTH, display_aspect_ratio.num); - put_ebml_uint(pb, MATROSKA_ID_VIDEODISPLAYHEIGHT, display_aspect_ratio.den); - put_ebml_uint(pb, MATROSKA_ID_VIDEODISPLAYUNIT, MATROSKA_VIDEO_DISPLAYUNIT_DAR); - } - } - } else if (display_width_div != 1 || display_height_div != 1) { - put_ebml_uint(pb, MATROSKA_ID_VIDEODISPLAYWIDTH , par->width / display_width_div); - put_ebml_uint(pb, MATROSKA_ID_VIDEODISPLAYHEIGHT, par->height / display_height_div); - } else if (!IS_WEBM(mkv)) - put_ebml_uint(pb, MATROSKA_ID_VIDEODISPLAYUNIT, MATROSKA_VIDEO_DISPLAYUNIT_UNKNOWN); - - if (par->codec_id == AV_CODEC_ID_RAWVIDEO) { - uint32_t color_space = av_le2ne32(par->codec_tag); - put_ebml_binary(pb, MATROSKA_ID_VIDEOCOLORSPACE, &color_space, sizeof(color_space)); - } - mkv_write_video_color(pb, st, par); - mkv_write_video_projection(s, pb, st); - - end_ebml_master(pb, subinfo); - if (!IS_WEBM(mkv)) mkv_write_dovi(s, pb, st); -- 2.32.0 _______________________________________________ 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".
next prev parent reply other threads:[~2022-01-16 23:05 UTC|newest] Thread overview: 35+ messages / expand[flat|nested] mbox.gz Atom feed top 2022-01-16 22:49 [FFmpeg-devel] [PATCH 01/25] avformat/matroskaenc: Fix potential overflow Andreas Rheinhardt 2022-01-16 22:51 ` James Almer 2022-01-16 23:05 ` Andreas Rheinhardt 2022-01-16 23:03 ` [FFmpeg-devel] [PATCH 02/25] avformat/matroskaenc: Don't open BlockGroup twice Andreas Rheinhardt 2022-01-16 23:03 ` [FFmpeg-devel] [PATCH 03/25] avformat/matroskaenc: Add API to write Masters with minimal length field Andreas Rheinhardt 2022-01-16 23:03 ` [FFmpeg-devel] [PATCH 04/25] avformat/matroskaenc: Don't waste bytes on SimpleTags length fields Andreas Rheinhardt 2022-01-16 23:03 ` [FFmpeg-devel] [PATCH 05/25] avformat/matroskaenc: Don't waste bytes when writing attachments Andreas Rheinhardt 2022-01-16 23:03 ` [FFmpeg-devel] [PATCH 06/25] avformat/matroskaenc: Avoid seeks when writing EBML header Andreas Rheinhardt 2022-01-16 23:03 ` Andreas Rheinhardt [this message] 2022-01-16 23:03 ` [FFmpeg-devel] [PATCH 08/25] avformat/matroskaenc: Don't waste bytes on Video element length fields Andreas Rheinhardt 2022-01-16 23:03 ` [FFmpeg-devel] [PATCH 09/25] avformat/matroskaenc: Don't waste bytes on ChapterAtoms " Andreas Rheinhardt 2022-01-16 23:03 ` [FFmpeg-devel] [PATCH 10/25] avformat/matroskaenc: Factor writing Info out Andreas Rheinhardt 2022-01-16 23:03 ` [FFmpeg-devel] [PATCH 11/25] avformat/matroskaenc: Allow to use custom reformatting functions Andreas Rheinhardt 2022-01-16 23:03 ` [FFmpeg-devel] [PATCH 12/25] avformat/matroskaenc: Speed up reformatting WavPack Andreas Rheinhardt 2022-01-16 23:03 ` [FFmpeg-devel] [PATCH 13/25] avformat/av1: Document actual behaviour of ff_av1_filter_obus() Andreas Rheinhardt 2022-01-16 23:03 ` [FFmpeg-devel] [PATCH 14/25] avformat/matroskaenc: Redo reformatting AV1 Andreas Rheinhardt 2022-01-16 23:03 ` [FFmpeg-devel] [PATCH 15/25] avformat/matroskaenc: Use common function for H.2645 annex B->mp4 Andreas Rheinhardt 2022-01-16 23:03 ` [FFmpeg-devel] [PATCH 16/25] avformat/avc: Add functions to split access unit into list of NALUs Andreas Rheinhardt 2022-01-16 23:03 ` [FFmpeg-devel] [PATCH 17/25] avformat/matroskaenc: Avoid temporary buffers when reformatting H.2645 Andreas Rheinhardt 2022-01-16 23:03 ` [FFmpeg-devel] [PATCH 18/25] avformat/matroskaenc: Remove special code for writing subtitles Andreas Rheinhardt 2022-01-16 23:03 ` [FFmpeg-devel] [PATCH 19/25] avformat/matroskaenc: Pass more parameters explicitly to mkv_write_block Andreas Rheinhardt 2022-01-16 23:04 ` [FFmpeg-devel] [PATCH 20/25] avformat/matroskaenc: Redo applying ProRes offset Andreas Rheinhardt 2022-01-16 23:04 ` [FFmpeg-devel] [PATCH 21/25] avformat/matroskaenc: Don't waste bytes on BlockGroup length fields Andreas Rheinhardt 2022-01-16 23:04 ` [FFmpeg-devel] [PATCH 22/25] avformat/matroskaenc: Remove duplicated code for writing WebVTT subs Andreas Rheinhardt 2022-01-16 23:04 ` [FFmpeg-devel] [PATCH 23/25] avformat/matroskaenc: Reindentation Andreas Rheinhardt 2022-01-16 23:04 ` [FFmpeg-devel] [PATCH 24/25] avformat/matroskaenc: Avoid repeated avio_tell() Andreas Rheinhardt 2022-01-16 23:04 ` [FFmpeg-devel] [PATCH 25/25] avformat/matroskaenc: Write data directly into dynamic buffers Andreas Rheinhardt 2022-01-18 11:17 ` [FFmpeg-devel] [PATCH 01/25] avformat/matroskaenc: Fix potential overflow Andreas Rheinhardt 2022-01-18 23:32 ` [FFmpeg-devel] [PATCH 26/31] avformat/mux: Remove assert based on faulty assumptions Andreas Rheinhardt 2022-01-18 23:32 ` [FFmpeg-devel] [PATCH 27/31] fate/matroska: Add test for avoiding negative timestamps Andreas Rheinhardt 2022-01-19 0:33 ` Andreas Rheinhardt 2022-01-18 23:32 ` [FFmpeg-devel] [PATCH 28/31] avformat/avformat: Add AVFMT_AVOID_NEG_TS_DISABLED Andreas Rheinhardt 2022-01-18 23:32 ` [FFmpeg-devel] [PATCH 29/31] avformat/mux: Preserve sync even if later packet has negative ts Andreas Rheinhardt 2022-01-18 23:32 ` [FFmpeg-devel] [PATCH 30/31] avformat/mux: Peek into the muxing queue for avoid_negative_ts Andreas Rheinhardt 2022-01-18 23:32 ` [FFmpeg-devel] [PATCH 31/31] avformat/hls: Remove redundant cast Andreas Rheinhardt
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=AM7PR03MB666075D1304F06C9ACEB48548F569@AM7PR03MB6660.eurprd03.prod.outlook.com \ --to=andreas.rheinhardt@outlook.com \ --cc=ffmpeg-devel@ffmpeg.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: link
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