From fb115b0baf8a278a6c4bf28c7caf9b3c5f88cf86 Mon Sep 17 00:00:00 2001 From: oldma3095 <43288861+oldma3095@users.noreply.github.com> Date: Mon, 25 Nov 2024 15:59:53 +0800 Subject: [PATCH 1/7] Update codec_context.go HardwareFrameContext --- codec_context.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/codec_context.go b/codec_context.go index f11c8bb..ec1f728 100644 --- a/codec_context.go +++ b/codec_context.go @@ -390,6 +390,10 @@ func (cc *CodecContext) SetHardwareFrameContext(hfc *HardwareFrameContext) { } } +func (cc *CodecContext) HardwareFrameContext() *HardwareFrameContext{ + return newHardwareFrameContextFromC(cc.c.hw_frames_ctx) +} + // https://ffmpeg.org/doxygen/7.0/structAVCodecContext.html#ad2f772bd948d8f3be4d674a3a52ee00e func (cc *CodecContext) ExtraHardwareFrames() int { return int(cc.c.extra_hw_frames) From 565ca47d52f1d22e84f630dec482ea0d7d4cd6a4 Mon Sep 17 00:00:00 2001 From: oldma3095 <43288861+oldma3095@users.noreply.github.com> Date: Mon, 25 Nov 2024 16:36:56 +0800 Subject: [PATCH 2/7] Update format_context.go DumpFormat and HardwareFrameContext --- format_context.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/format_context.go b/format_context.go index 3beb1d0..13cf550 100644 --- a/format_context.go +++ b/format_context.go @@ -364,3 +364,15 @@ func (fc *FormatContext) FindBestStream(mt MediaType, wantedStreamIndex, related } return nil, nil, fmt.Errorf("astiav: no stream with index %d", ret) } + +// https://ffmpeg.org/doxygen/7.0/group__lavf__misc.html#gae2645941f2dc779c307eb6314fd39f10 +// prints detailed information about the input or output format, such as +// duration, bitrate, streams, container, programs, metadata, side data, codec and time base. +func (fc *FormatContext) DumpFormat(index int32, url string, isOutput int32) { + urlc := (*C.char)(nil) + if len(url) > 0 { + urlc = C.CString(url) + defer C.free(unsafe.Pointer(urlc)) + } + C.av_dump_format(fc.c, C.int(index), urlc, C.int(isOutput)) +} From 0980e2281b5c0b68ed81397c3420948245c069ae Mon Sep 17 00:00:00 2001 From: ma3315865 Date: Tue, 26 Nov 2024 12:04:41 +0800 Subject: [PATCH 3/7] fix with same convention --- codec_context.go | 9 +++++---- format_context.go | 10 ++++------ 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/codec_context.go b/codec_context.go index ec1f728..61fa3f5 100644 --- a/codec_context.go +++ b/codec_context.go @@ -379,6 +379,11 @@ func (cc *CodecContext) SetHardwareDeviceContext(hdc *HardwareDeviceContext) { } } +// https://ffmpeg.org/doxygen/7.0/structAVCodecContext.html#a3bac44bb0b016ab838780cc19ac277d6 +func (cc *CodecContext) HardwareFrameContext() *HardwareFrameContext { + return newHardwareFrameContextFromC(cc.c.hw_frames_ctx) +} + // https://ffmpeg.org/doxygen/7.0/structAVCodecContext.html#a3bac44bb0b016ab838780cc19ac277d6 func (cc *CodecContext) SetHardwareFrameContext(hfc *HardwareFrameContext) { if cc.hfc != nil { @@ -390,10 +395,6 @@ func (cc *CodecContext) SetHardwareFrameContext(hfc *HardwareFrameContext) { } } -func (cc *CodecContext) HardwareFrameContext() *HardwareFrameContext{ - return newHardwareFrameContextFromC(cc.c.hw_frames_ctx) -} - // https://ffmpeg.org/doxygen/7.0/structAVCodecContext.html#ad2f772bd948d8f3be4d674a3a52ee00e func (cc *CodecContext) ExtraHardwareFrames() int { return int(cc.c.extra_hw_frames) diff --git a/format_context.go b/format_context.go index 13cf550..0972afa 100644 --- a/format_context.go +++ b/format_context.go @@ -366,13 +366,11 @@ func (fc *FormatContext) FindBestStream(mt MediaType, wantedStreamIndex, related } // https://ffmpeg.org/doxygen/7.0/group__lavf__misc.html#gae2645941f2dc779c307eb6314fd39f10 -// prints detailed information about the input or output format, such as -// duration, bitrate, streams, container, programs, metadata, side data, codec and time base. func (fc *FormatContext) DumpFormat(index int32, url string, isOutput int32) { - urlc := (*C.char)(nil) + curl := (*C.char)(nil) if len(url) > 0 { - urlc = C.CString(url) - defer C.free(unsafe.Pointer(urlc)) + curl = C.CString(url) + defer C.free(unsafe.Pointer(curl)) } - C.av_dump_format(fc.c, C.int(index), urlc, C.int(isOutput)) + C.av_dump_format(fc.c, C.int(index), curl, C.int(isOutput)) } From 91e0bb16a957b6e2536bba32398e2acc8b1b2c66 Mon Sep 17 00:00:00 2001 From: ma3315865 Date: Tue, 26 Nov 2024 12:05:31 +0800 Subject: [PATCH 4/7] fix with same convention --- format_context.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/format_context.go b/format_context.go index 0972afa..827e483 100644 --- a/format_context.go +++ b/format_context.go @@ -366,11 +366,11 @@ func (fc *FormatContext) FindBestStream(mt MediaType, wantedStreamIndex, related } // https://ffmpeg.org/doxygen/7.0/group__lavf__misc.html#gae2645941f2dc779c307eb6314fd39f10 -func (fc *FormatContext) DumpFormat(index int32, url string, isOutput int32) { +func (fc *FormatContext) Dump(streamIndex int, url string, isOutput bool) { curl := (*C.char)(nil) if len(url) > 0 { curl = C.CString(url) defer C.free(unsafe.Pointer(curl)) } - C.av_dump_format(fc.c, C.int(index), curl, C.int(isOutput)) + C.av_dump_format(fc.c, C.int(streamIndex), curl, C.int(isOutput)) } From afc0efb5b23adeb485881d7f6405e5615094a8f3 Mon Sep 17 00:00:00 2001 From: ma3315865 Date: Tue, 26 Nov 2024 12:28:32 +0800 Subject: [PATCH 5/7] fix with same convention --- format_context.go | 6 +++++- format_context_test.go | 16 +++++++++++++--- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/format_context.go b/format_context.go index 827e483..385f94a 100644 --- a/format_context.go +++ b/format_context.go @@ -372,5 +372,9 @@ func (fc *FormatContext) Dump(streamIndex int, url string, isOutput bool) { curl = C.CString(url) defer C.free(unsafe.Pointer(curl)) } - C.av_dump_format(fc.c, C.int(streamIndex), curl, C.int(isOutput)) + cisOutput := 0 + if isOutput { + cisOutput = 1 + } + C.av_dump_format(fc.c, C.int(streamIndex), curl, C.int(cisOutput)) } diff --git a/format_context_test.go b/format_context_test.go index 494108e..a91638a 100644 --- a/format_context_test.go +++ b/format_context_test.go @@ -1,6 +1,7 @@ package astiav import ( + "strings" "testing" "github.com/stretchr/testify/require" @@ -31,9 +32,18 @@ func TestFormatContext(t *testing.T) { require.NotNil(t, cl) require.Equal(t, "AVFormatContext", cl.Name()) - sdp, err := fc1.SDPCreate() - require.NoError(t, err) - require.Equal(t, "v=0\r\no=- 0 0 IN IP4 127.0.0.1\r\ns=Big Buck Bunny\r\nt=0 0\r\na=tool:libavformat 61.1.100\r\nm=video 0 RTP/AVP 96\r\nb=AS:441\r\na=rtpmap:96 H264/90000\r\na=fmtp:96 packetization-mode=1; sprop-parameter-sets=Z0LADasgKDPz4CIAAAMAAgAAAwBhHihUkA==,aM48gA==; profile-level-id=42C00D\r\na=control:streamid=0\r\nm=audio 0 RTP/AVP 97\r\nb=AS:161\r\na=rtpmap:97 MPEG4-GENERIC/48000/2\r\na=fmtp:97 profile-level-id=1;mode=AAC-hbr;sizelength=13;indexlength=3;indexdeltalength=3; config=1190\r\na=control:streamid=1\r\n", sdp) + //sdp, err := fc1.SDPCreate() + //require.NoError(t, err) + //require.Equal(t, "v=0\r\no=- 0 0 IN IP4 127.0.0.1\r\ns=Big Buck Bunny\r\nt=0 0\r\na=tool:libavformat 61.1.100\r\nm=video 0 RTP/AVP 96\r\nb=AS:441\r\na=rtpmap:96 H264/90000\r\na=fmtp:96 packetization-mode=1; sprop-parameter-sets=Z0LADasgKDPz4CIAAAMAAgAAAwBhHihUkA==,aM48gA==; profile-level-id=42C00D\r\na=control:streamid=0\r\nm=audio 0 RTP/AVP 97\r\nb=AS:161\r\na=rtpmap:97 MPEG4-GENERIC/48000/2\r\na=fmtp:97 profile-level-id=1;mode=AAC-hbr;sizelength=13;indexlength=3;indexdeltalength=3; config=1190\r\na=control:streamid=1\r\n", sdp) + + SetLogLevel(LogLevelInfo) + SetLogCallback(func(c Classer, l LogLevel, f, msg string) { + msg = strings.TrimSpace(msg) + if strings.HasPrefix(msg, "Stream") && strings.Contains(msg, "Video") { + require.Equal(t, msg, `Stream #0:0[0x1](und): Video: h264 (Constrained Baseline) (avc1 / 0x31637661), yuv420p(progressive), 320x180 [SAR 1:1 DAR 16:9], 441 kb/s, 24 fps, 24 tbr, 12288 tbn (default)`) + } + }) + fc1.Dump(0, "video.mp4", false) _, _, err = fc1.FindBestStream(MediaTypeUnknown, -1, -1) require.Error(t, err) From 4e63dbec1fae9cf4b67bf21f6ab39139e80f76af Mon Sep 17 00:00:00 2001 From: ma3315865 Date: Tue, 26 Nov 2024 12:28:46 +0800 Subject: [PATCH 6/7] fix with same convention --- format_context_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/format_context_test.go b/format_context_test.go index a91638a..22eecd9 100644 --- a/format_context_test.go +++ b/format_context_test.go @@ -32,9 +32,9 @@ func TestFormatContext(t *testing.T) { require.NotNil(t, cl) require.Equal(t, "AVFormatContext", cl.Name()) - //sdp, err := fc1.SDPCreate() - //require.NoError(t, err) - //require.Equal(t, "v=0\r\no=- 0 0 IN IP4 127.0.0.1\r\ns=Big Buck Bunny\r\nt=0 0\r\na=tool:libavformat 61.1.100\r\nm=video 0 RTP/AVP 96\r\nb=AS:441\r\na=rtpmap:96 H264/90000\r\na=fmtp:96 packetization-mode=1; sprop-parameter-sets=Z0LADasgKDPz4CIAAAMAAgAAAwBhHihUkA==,aM48gA==; profile-level-id=42C00D\r\na=control:streamid=0\r\nm=audio 0 RTP/AVP 97\r\nb=AS:161\r\na=rtpmap:97 MPEG4-GENERIC/48000/2\r\na=fmtp:97 profile-level-id=1;mode=AAC-hbr;sizelength=13;indexlength=3;indexdeltalength=3; config=1190\r\na=control:streamid=1\r\n", sdp) + sdp, err := fc1.SDPCreate() + require.NoError(t, err) + require.Equal(t, "v=0\r\no=- 0 0 IN IP4 127.0.0.1\r\ns=Big Buck Bunny\r\nt=0 0\r\na=tool:libavformat 61.1.100\r\nm=video 0 RTP/AVP 96\r\nb=AS:441\r\na=rtpmap:96 H264/90000\r\na=fmtp:96 packetization-mode=1; sprop-parameter-sets=Z0LADasgKDPz4CIAAAMAAgAAAwBhHihUkA==,aM48gA==; profile-level-id=42C00D\r\na=control:streamid=0\r\nm=audio 0 RTP/AVP 97\r\nb=AS:161\r\na=rtpmap:97 MPEG4-GENERIC/48000/2\r\na=fmtp:97 profile-level-id=1;mode=AAC-hbr;sizelength=13;indexlength=3;indexdeltalength=3; config=1190\r\na=control:streamid=1\r\n", sdp) SetLogLevel(LogLevelInfo) SetLogCallback(func(c Classer, l LogLevel, f, msg string) { From bff991fd9b09307bf28a090f8e21c14cecb765c0 Mon Sep 17 00:00:00 2001 From: ma3315865 Date: Wed, 27 Nov 2024 10:49:10 +0800 Subject: [PATCH 7/7] test --- .gitignore | 3 ++- mathematics.go | 5 +++++ mathematics_test.go | 3 +++ 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index ee79665..f5a73b8 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ .DS_STORE -coverage.out \ No newline at end of file +coverage.out +/.idea \ No newline at end of file diff --git a/mathematics.go b/mathematics.go index c9cfd31..0047fde 100644 --- a/mathematics.go +++ b/mathematics.go @@ -12,3 +12,8 @@ func RescaleQ(a int64, b Rational, c Rational) int64 { func RescaleQRnd(a int64, b Rational, c Rational, r Rounding) int64 { return int64(C.av_rescale_q_rnd(C.int64_t(a), b.c, c.c, C.enum_AVRounding(r))) } + +// https://ffmpeg.org/doxygen/7.0/group__lavu__math__rational.html#ga935dbbf6bde8dfe5fa7ddb1da582eb07 +func Q2D(a Rational) float64 { + return float64(C.av_q2d(a.c)) +} diff --git a/mathematics_test.go b/mathematics_test.go index 5e909f8..44b2b25 100644 --- a/mathematics_test.go +++ b/mathematics_test.go @@ -10,4 +10,7 @@ func TestMathematics(t *testing.T) { require.Equal(t, int64(1000), RescaleQ(100, NewRational(1, 100), NewRational(1, 1000))) require.Equal(t, int64(0), RescaleQRnd(1, NewRational(1, 100), NewRational(1, 10), RoundingDown)) require.Equal(t, int64(1), RescaleQRnd(1, NewRational(1, 100), NewRational(1, 10), RoundingUp)) + require.Equal(t, 0.04, Q2D(NewRational(1, 25))) + require.Equal(t, 0.3, Q2D(NewRational(3, 10))) + require.Equal(t, float64(30), Q2D(NewRational(30, 1))) }