Skip to content

Commit c3b80a5

Browse files
committed
Added SetFloat32 and SetTs to frame
1 parent 3a3c3a8 commit c3b80a5

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

pkg/ffmpeg/frame.go

+34
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,29 @@ func (frame *Frame) Float32(plane int) []float32 {
168168
return ctx.Float32(plane)
169169
}
170170

171+
// Set plane data from float32 slice
172+
func (frame *Frame) SetFloat32(plane int, data []float32) error {
173+
if frame.Type() != media.AUDIO {
174+
return errors.New("frame is not an audio frame")
175+
}
176+
177+
// If the number of samples is not the same, the re-allocate the frame
178+
ctx := (*ff.AVFrame)(frame)
179+
if len(data) != frame.NumSamples() {
180+
ctx.SetNumSamples(len(data))
181+
if err := ff.AVUtil_frame_get_buffer(ctx, false); err != nil {
182+
ff.AVUtil_frame_unref(ctx)
183+
return err
184+
}
185+
}
186+
187+
// Copy data
188+
copy(ctx.Float32(plane), data)
189+
190+
// Return success
191+
return nil
192+
}
193+
171194
// Return plane data as a byte slice
172195
func (frame *Frame) Bytes(plane int) []byte {
173196
return (*ff.AVFrame)(frame).Bytes(plane)
@@ -261,6 +284,17 @@ func (frame *Frame) Ts() float64 {
261284
return ff.AVUtil_rational_q2d(tb) * float64(pts)
262285
}
263286

287+
// Set timestamp in seconds
288+
func (frame *Frame) SetTs(secs float64) {
289+
ctx := (*ff.AVFrame)(frame)
290+
tb := ctx.TimeBase()
291+
if secs == TS_UNDEFINED || tb.Num() == 0 || tb.Den() == 0 {
292+
frame.SetPts(ff.AV_NOPTS_VALUE)
293+
return
294+
}
295+
ctx.SetPts(int64(secs / ff.AVUtil_rational_q2d(tb)))
296+
}
297+
264298
///////////////////////////////////////////////////////////////////////////////
265299
// PUBLIC METHODS
266300

pkg/ffmpeg/frame_test.go

+41
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,44 @@ func Test_frame_004(t *testing.T) {
6969
assert.Equal(copy.Width(), frame.Width())
7070
assert.Equal(copy.Height(), frame.Height())
7171
}
72+
73+
func Test_frame_005(t *testing.T) {
74+
assert := assert.New(t)
75+
76+
frame, err := ffmpeg.NewFrame(ffmpeg.AudioPar("fltp", "stereo", 16000))
77+
if !assert.NoError(err) {
78+
t.FailNow()
79+
}
80+
defer frame.Close()
81+
82+
t.Log(frame)
83+
data := make([]float32, frame.SampleRate())
84+
frame.SetFloat32(0, data)
85+
frame.SetFloat32(1, data)
86+
t.Log(frame)
87+
88+
frame.SetFloat32(0, data[0:100])
89+
frame.SetFloat32(1, data[0:100])
90+
t.Log(frame)
91+
92+
}
93+
94+
func Test_frame_006(t *testing.T) {
95+
assert := assert.New(t)
96+
97+
frame, err := ffmpeg.NewFrame(ffmpeg.AudioPar("fltp", "stereo", 16000))
98+
if !assert.NoError(err) {
99+
t.FailNow()
100+
}
101+
defer frame.Close()
102+
103+
for i := 0; i < 10; i++ {
104+
frame.SetPts(int64(i))
105+
assert.Equal(int64(i), frame.Pts())
106+
}
107+
108+
for i := 0; i < 10; i++ {
109+
frame.SetTs(float64(i))
110+
assert.Equal(float64(i), frame.Ts())
111+
}
112+
}

0 commit comments

Comments
 (0)