-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecoder.go
More file actions
865 lines (752 loc) · 23.8 KB
/
Copy pathdecoder.go
File metadata and controls
865 lines (752 loc) · 23.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
//go:build amd64 || arm64
package ffmpeg
import (
"context"
"errors"
"io"
"strconv"
"strings"
"sync"
"time"
"github.com/bstkhq/go-ffmpeg-ffi/avcodec"
"github.com/bstkhq/go-ffmpeg-ffi/avformat"
"github.com/bstkhq/go-ffmpeg-ffi/avutil"
"github.com/bstkhq/go-ffmpeg-ffi/internal/bindings"
)
// Decoder decodes media files.
//
// Do not call decoder operations concurrently. Close is the exception: it may
// be called concurrently to interrupt a blocking read. Frames and packets
// returned without Copy or Clone are borrowed and must not outlive the next
// decoder operation.
type Decoder struct {
mu sync.Mutex
closeSignal sync.Once
formatCtx avformat.FormatContext
videoCodecCtx avcodec.Context
audioCodecCtx avcodec.Context
packet avcodec.Packet
frame avutil.Frame
hardwareSoftwareFrame avutil.Frame
videoStreamIdx int
audioStreamIdx int
videoInfo *StreamInfo
audioInfo *StreamInfo
streamInfoCache map[int]*StreamInfo
videoDecoderOpen bool
audioDecoderOpen bool
videoState decoderCodecState
audioState decoderCodecState
packetQueue decoderPacketQueue
packetPool decoderPacketPool
demuxEOF bool
activeMedia MediaType
prefetchedFrame avutil.Frame
prefetchedMedia MediaType
customIO *CustomIOContext
interrupt *decoderInterrupt
cleanup func()
closed bool
hardwareConfig *HWDecoderConfig
ownedHWDevice *HWDevice
hardwarePixelFormat int32
hardwareSoftwareOutput bool
videoDecoderInfo VideoDecoderInfo
}
func newDecoder(interrupt *decoderInterrupt) *Decoder {
return &Decoder{
videoStreamIdx: -1,
audioStreamIdx: -1,
interrupt: interrupt,
streamInfoCache: make(map[int]*StreamInfo),
hardwarePixelFormat: int32(avutil.PixelFormatNone),
}
}
// allocateDecodeResources installs the reusable packet and frame required by
// every Decoder entry point. Keep this allocation centralized so constructors
// for files, custom I/O, and capture devices cannot publish a partial Decoder.
func (d *Decoder) allocateDecodeResources() error {
packet := avcodec.PacketAlloc()
if packet == nil {
return errors.New("ffmpeg: failed to allocate packet")
}
frame := avutil.FrameAlloc()
if frame == nil {
avcodec.PacketFree(&packet)
return errors.New("ffmpeg: failed to allocate frame")
}
d.packet = packet
d.frame = frame
return nil
}
// DecoderOptions configures decoder behavior.
type DecoderOptions struct {
// Format hint (e.g., "mp4", "mkv") - optional
Format string
// FFmpeg options passed to avformat_open_input
AVOptions map[string]string
// Typed probing controls (mapped to avformat_open_input AVDictionary).
ProbeSizeBytes int
AnalyzeDuration time.Duration
MaxProbePackets int
FormatWhitelist []string
CodecWhitelist []string
// ProbeScore, when >0, requires the detected probe score to be at least this value.
// If TryMultipleFormats is enabled, ffmpeg will attempt additional forced demuxers when the
// auto-detected probe score is below this threshold.
ProbeScore int
// FormatBlacklist excludes demuxers from TryMultipleFormats attempts.
// Note: this is an ffmpeg-side filter; it is not passed as an avformat_open_input option.
FormatBlacklist []string
// TryMultipleFormats, when true, retries avformat_open_input with forced demuxers if
// auto-detection fails or yields a low probe score.
TryMultipleFormats bool
// Streams specifies which stream types to decode (nil = all streams).
// Packets for every selected stream are retained until consumed. Select only
// the streams you need when using DecodeVideo or DecodeAudio exclusively.
Streams []MediaType
// ProgramID selects a specific program to decode in multi-program inputs (e.g. MPEG-TS).
// When set, ffmpeg will pick the best video/audio streams within the program.
ProgramID int
// Hardware enables video hardware acceleration. Nil keeps software decoding.
// Use an empty HWDecoderConfig for automatic platform-aware selection.
Hardware *HWDecoderConfig
}
func buildDecoderAVOptions(opts *DecoderOptions) map[string]string {
if opts == nil {
return nil
}
out := make(map[string]string, len(opts.AVOptions)+8)
for k, v := range opts.AVOptions {
out[k] = v
}
// Typed fields override raw AVOptions for clarity/determinism.
if opts.ProbeSizeBytes > 0 {
out["probesize"] = strconv.Itoa(opts.ProbeSizeBytes)
}
if opts.AnalyzeDuration > 0 {
out["analyzeduration"] = strconv.FormatInt(opts.AnalyzeDuration.Microseconds(), 10)
}
if opts.MaxProbePackets > 0 {
out["max_probe_packets"] = strconv.Itoa(opts.MaxProbePackets)
}
if len(opts.FormatWhitelist) > 0 {
out["format_whitelist"] = strings.Join(opts.FormatWhitelist, ",")
}
if len(opts.CodecWhitelist) > 0 {
out["codec_whitelist"] = strings.Join(opts.CodecWhitelist, ",")
}
return out
}
func cloneDecoderOptions(opts *DecoderOptions) *DecoderOptions {
if opts == nil {
return &DecoderOptions{}
}
clone := *opts
clone.AVOptions = cloneStringMap(opts.AVOptions)
clone.FormatWhitelist = append([]string(nil), opts.FormatWhitelist...)
clone.CodecWhitelist = append([]string(nil), opts.CodecWhitelist...)
clone.FormatBlacklist = append([]string(nil), opts.FormatBlacklist...)
clone.Streams = append([]MediaType(nil), opts.Streams...)
clone.Hardware = cloneHWDecoderConfig(opts.Hardware)
return &clone
}
func cloneStringMap(values map[string]string) map[string]string {
if values == nil {
return nil
}
clone := make(map[string]string, len(values))
for key, value := range values {
clone[key] = value
}
return clone
}
// NewDecoder opens a media file for decoding.
func NewDecoder(path string, opts *DecoderOptions) (*Decoder, error) {
return NewDecoderContext(context.Background(), path, opts)
}
// NewDecoderContext opens a media file and allows FFmpeg probing and I/O to be canceled.
func NewDecoderContext(ctx context.Context, path string, opts *DecoderOptions) (*Decoder, error) {
if ctx == nil {
return nil, errors.New("ffmpeg: context cannot be nil")
}
// Ensure FFmpeg is loaded
if err := bindings.Load(); err != nil {
return nil, err
}
opts = cloneDecoderOptions(opts)
if err := validateHWDecoderConfig(opts.Hardware); err != nil {
return nil, err
}
d := newDecoder(newDecoderInterrupt())
d.hardwareConfig = opts.Hardware
if opts.Hardware != nil && opts.Hardware.Mode != HardwareAccelerationDisabled {
d.videoDecoderInfo.HardwareState = HardwareStatePending
}
if err := d.beginInterrupt(ctx); err != nil {
d.interrupt.release(nil)
return nil, err
}
defer d.clearInterrupt()
// Open input file (with optional retry logic for ambiguous probing).
var err error
d.formatCtx, err = openInputWithRetries(path, opts, d.interrupt)
if err != nil {
d.interrupt.release(d.formatCtx)
return nil, err
}
// Find stream info
if err := d.interrupt.finish(avformat.FindStreamInfo(d.formatCtx, nil)); err != nil {
d.interrupt.release(d.formatCtx)
avformat.CloseInput(&d.formatCtx)
return nil, err
}
// Stream selection.
wantVideo, wantAudio := true, true
if len(opts.Streams) > 0 {
wantVideo, wantAudio = false, false
for _, mt := range opts.Streams {
switch mt {
case MediaTypeVideo:
wantVideo = true
case MediaTypeAudio:
wantAudio = true
}
}
}
if opts != nil && opts.ProgramID > 0 {
if err := d.selectProgramStreams(opts.ProgramID, wantVideo, wantAudio); err != nil {
d.Close()
return nil, err
}
} else {
if wantVideo {
d.videoStreamIdx = int(avformat.FindBestStream(d.formatCtx, avutil.MediaTypeVideo, -1, -1, nil, 0))
if d.videoStreamIdx >= 0 {
info, err := d.getStreamInfo(d.videoStreamIdx)
if err != nil {
d.Close()
return nil, err
}
d.videoInfo = info
}
}
if wantAudio {
d.audioStreamIdx = int(avformat.FindBestStream(d.formatCtx, avutil.MediaTypeAudio, -1, -1, nil, 0))
if d.audioStreamIdx >= 0 {
info, err := d.getStreamInfo(d.audioStreamIdx)
if err != nil {
d.Close()
return nil, err
}
d.audioInfo = info
}
}
}
if err := d.allocateDecodeResources(); err != nil {
d.Close()
return nil, err
}
return d, nil
}
// getStreamInfo extracts stream information.
func (d *Decoder) getStreamInfo(streamIdx int) (*StreamInfo, error) {
if info, ok := d.streamInfoCache[streamIdx]; ok {
return info, nil
}
stream := avformat.GetStream(d.formatCtx, streamIdx)
if stream == nil {
return nil, nil
}
codecPar := avformat.GetStreamCodecPar(stream)
if codecPar == nil {
return nil, nil
}
ownedCodecPar, err := ownCodecParameters(codecPar)
if err != nil {
return nil, err
}
codecType := avformat.GetCodecParType(codecPar)
codecID := avformat.GetCodecParCodecID(codecPar)
// Get time base
tbNum, tbDen := avformat.GetStreamTimeBase(stream)
// Get codec name
var codecName string
if codec := avcodec.FindDecoder(codecID); codec != nil {
codecName = avcodec.GetCodecName(codec)
}
info := &StreamInfo{
Index: streamIdx,
Type: codecType,
CodecID: codecID,
CodecName: codecName,
TimeBase: avutil.NewRational(tbNum, tbDen),
codecPar: ownedCodecPar,
}
if codecType == avutil.MediaTypeVideo {
info.Width = int(avformat.GetCodecParWidth(codecPar))
info.Height = int(avformat.GetCodecParHeight(codecPar))
info.PixelFmt = PixelFormat(avformat.GetCodecParFormat(codecPar))
// Get frame rate
frNum, frDen := avformat.GetStreamAvgFrameRate(stream)
info.FrameRate = avutil.NewRational(frNum, frDen)
} else if codecType == avutil.MediaTypeAudio {
info.SampleRate = int(avformat.GetCodecParSampleRate(codecPar))
info.Channels = int(avformat.GetCodecParChannels(codecPar))
}
if d.streamInfoCache == nil {
d.streamInfoCache = make(map[int]*StreamInfo)
}
d.streamInfoCache[streamIdx] = info
return info, nil
}
// VideoStream returns information about the video stream.
// Returns nil if no video stream is present.
func (d *Decoder) VideoStream() *StreamInfo {
return d.videoInfo
}
// AudioStream returns information about the audio stream.
// Returns nil if no audio stream is present.
func (d *Decoder) AudioStream() *StreamInfo {
return d.audioInfo
}
// HasVideo returns true if the file has a video stream.
func (d *Decoder) HasVideo() bool {
return d.videoStreamIdx >= 0
}
// HasAudio returns true if the file has an audio stream.
func (d *Decoder) HasAudio() bool {
return d.audioStreamIdx >= 0
}
// NumStreams returns the total number of streams.
func (d *Decoder) NumStreams() int {
d.mu.Lock()
defer d.mu.Unlock()
if d.formatCtx == nil {
return 0
}
return avformat.GetNbStreams(d.formatCtx)
}
// Duration returns the duration as time.Duration.
func (d *Decoder) Duration() time.Duration {
us := d.DurationMicroseconds()
if us <= 0 {
return 0
}
return time.Duration(us) * time.Microsecond
}
// DurationMicroseconds returns the duration in microseconds (AV_TIME_BASE units).
func (d *Decoder) DurationMicroseconds() int64 {
d.mu.Lock()
defer d.mu.Unlock()
return d.durationMicrosecondsLocked()
}
func (d *Decoder) durationMicrosecondsLocked() int64 {
if d.formatCtx == nil {
return 0
}
return avformat.GetDuration(d.formatCtx)
}
// BitRate returns the bit rate.
func (d *Decoder) BitRate() int64 {
d.mu.Lock()
defer d.mu.Unlock()
if d.formatCtx == nil {
return 0
}
return avformat.GetBitRate(d.formatCtx)
}
// ReadPacket reads the next packet from the file.
// Returns (nil, nil) on EOF.
//
// The returned packet is BORROWED (decoder-owned and internally reused).
// Do not free it; if you need to keep it, call PacketClone().
func (d *Decoder) ReadPacket() (*Packet, error) {
return d.ReadPacketContext(context.Background())
}
// ReadPacketContext reads the next packet and interrupts blocking FFmpeg I/O when ctx is canceled.
func (d *Decoder) ReadPacketContext(ctx context.Context) (*Packet, error) {
d.mu.Lock()
defer d.mu.Unlock()
if d.closed {
return nil, errDecoderClosed
}
if err := d.beginInterrupt(ctx); err != nil {
return nil, err
}
defer d.clearInterrupt()
packet, err := d.readPacketLocked()
return packet, d.finishInterrupt(err)
}
// OpenVideoDecoder opens a codec context for video decoding.
// Must be called before DecodeVideoPacket.
func (d *Decoder) OpenVideoDecoder() error {
d.mu.Lock()
defer d.mu.Unlock()
return d.openVideoDecoderLocked()
}
func (d *Decoder) openVideoDecoderLocked() error {
if d.closed {
return errDecoderClosed
}
if d.videoStreamIdx < 0 {
return ErrNoVideoStream
}
if d.videoDecoderOpen {
return nil // Already opened
}
stream := avformat.GetStream(d.formatCtx, d.videoStreamIdx)
codecPar := avformat.GetStreamCodecPar(stream)
codecID := avformat.GetCodecParCodecID(codecPar)
return d.openHardwareVideoDecoderLocked(codecPar, codecID)
}
// VideoDecoderInfo returns the resolved decoder and hardware state. Hardware
// selection is lazy, so HardwareState is pending until the video decoder opens.
func (d *Decoder) VideoDecoderInfo() VideoDecoderInfo {
d.mu.Lock()
defer d.mu.Unlock()
return d.videoDecoderInfo
}
// OpenAudioDecoder opens a codec context for audio decoding.
func (d *Decoder) OpenAudioDecoder() error {
d.mu.Lock()
defer d.mu.Unlock()
return d.openAudioDecoderLocked()
}
func (d *Decoder) openAudioDecoderLocked() error {
if d.closed {
return errDecoderClosed
}
if d.audioStreamIdx < 0 {
return ErrNoAudioStream
}
if d.audioDecoderOpen {
return nil // Already opened
}
stream := avformat.GetStream(d.formatCtx, d.audioStreamIdx)
codecPar := avformat.GetStreamCodecPar(stream)
codecID := avformat.GetCodecParCodecID(codecPar)
// Find decoder
codec := avcodec.FindDecoder(codecID)
if codec == nil {
return errors.New("ffmpeg: audio decoder not found")
}
// Allocate codec context
d.audioCodecCtx = avcodec.AllocContext3(codec)
if d.audioCodecCtx == nil {
return errors.New("ffmpeg: failed to allocate audio codec context")
}
// Copy codec parameters
if err := avcodec.ParametersToContext(d.audioCodecCtx, codecPar); err != nil {
avcodec.FreeContext(&d.audioCodecCtx)
return err
}
// Open codec
if err := avcodec.Open2(d.audioCodecCtx, codec, nil); err != nil {
avcodec.FreeContext(&d.audioCodecCtx)
return err
}
d.audioDecoderOpen = true
return nil
}
// DecodeVideoPacket decodes a video packet and returns the decoded frame.
// Returns an empty frame with nil error if more data is needed (EAGAIN), and
// returns io.EOF after the decoder is fully drained.
// The returned frame is owned by the decoder; copy it if you need to keep it.
func (d *Decoder) DecodeVideoPacket(pkt *Packet) (Frame, error) {
d.mu.Lock()
defer d.mu.Unlock()
return d.decodePacketLocked(MediaTypeVideo, pkt)
}
// DecodeVideoPacketCopy decodes a video packet and returns an owned frame.
//
// Unlike DecodeVideoPacket (which returns a decoder-owned, internally reused frame),
// this method returns a cloned frame that the caller MUST free with FrameFree.
// Returns an empty frame with nil error if more data is needed (EAGAIN), and
// returns io.EOF after the decoder is fully drained.
func (d *Decoder) DecodeVideoPacketCopy(pkt *Packet) (Frame, error) {
frame, err := d.DecodeVideoPacket(pkt)
if err != nil || frame.IsNil() {
return Frame{}, err
}
return FrameClone(frame)
}
// DecodeAudioPacket decodes an audio packet and returns the decoded frame.
// Returns an empty frame with nil error if more data is needed (EAGAIN), and
// returns io.EOF after the decoder is fully drained.
// The returned frame is owned by the decoder; copy it if you need to keep it.
func (d *Decoder) DecodeAudioPacket(pkt *Packet) (Frame, error) {
d.mu.Lock()
defer d.mu.Unlock()
return d.decodePacketLocked(MediaTypeAudio, pkt)
}
func (d *Decoder) decodePacketLocked(mediaType MediaType, packet *Packet) (Frame, error) {
if d.closed {
return Frame{}, errDecoderClosed
}
state, ctx, err := d.codecStateLocked(mediaType)
if err != nil {
return Frame{}, err
}
if packet == nil || packet.ptr == nil {
state.requestFlush()
} else {
clone, err := d.clonePacketLocked(packet.ptr)
if err != nil {
return Frame{}, err
}
if err := state.enqueueOwned(clone); err != nil {
d.recyclePacketLocked(&clone)
return Frame{}, err
}
}
ready, err := state.next(ctx, d.frame)
if err != nil {
if avutil.IsEOF(err) {
return Frame{}, io.EOF
}
return Frame{}, err
}
if !ready {
if state.drained {
return Frame{}, io.EOF
}
return Frame{}, nil
}
return d.prepareDecodedFrameLocked(mediaType)
}
// DecodeAudioPacketCopy decodes an audio packet and returns an owned frame.
//
// Unlike DecodeAudioPacket (which returns a decoder-owned, internally reused frame),
// this method returns a cloned frame that the caller MUST free with FrameFree.
// Returns an empty frame with nil error if more data is needed (EAGAIN), and
// returns io.EOF after the decoder is fully drained.
func (d *Decoder) DecodeAudioPacketCopy(pkt *Packet) (Frame, error) {
frame, err := d.DecodeAudioPacket(pkt)
if err != nil || frame.IsNil() {
return Frame{}, err
}
return FrameClone(frame)
}
// DecodeVideo reads and decodes the next video frame.
// This is a convenience method that handles packet reading internally.
// Packets for other selected streams remain available to DecodeAudio or ReadFrame.
// The returned frame is owned by the decoder; do not call FrameFree on it.
// If you need to keep the frame beyond the next decode call, make a copy.
// Returns io.EOF after the decoder is fully drained.
func (d *Decoder) DecodeVideo() (Frame, error) {
return d.DecodeVideoContext(context.Background())
}
// DecodeVideoContext decodes the next video frame with cancellation.
func (d *Decoder) DecodeVideoContext(ctx context.Context) (Frame, error) {
d.mu.Lock()
defer d.mu.Unlock()
if err := d.beginInterrupt(ctx); err != nil {
return Frame{}, err
}
defer d.clearInterrupt()
if !d.videoDecoderOpen {
if err := d.openVideoDecoderLocked(); err != nil {
return Frame{}, err
}
}
frame, err := d.nextFrameLocked(MediaTypeVideo)
return frame, d.finishInterrupt(err)
}
// DecodeVideoCopy reads and decodes the next video frame and returns an owned frame.
//
// The caller MUST free the returned frame with FrameFree.
// Returns io.EOF after the decoder is fully drained.
func (d *Decoder) DecodeVideoCopy() (Frame, error) {
return d.DecodeVideoCopyContext(context.Background())
}
// DecodeVideoCopyContext decodes the next video frame as an owned frame with cancellation.
func (d *Decoder) DecodeVideoCopyContext(ctx context.Context) (Frame, error) {
frame, err := d.DecodeVideoContext(ctx)
if err != nil || frame.IsNil() {
return Frame{}, err
}
return FrameClone(frame)
}
// DecodeAudio reads and decodes the next audio frame.
// This is a convenience method that handles packet reading internally.
// Packets for other selected streams remain available to DecodeVideo or ReadFrame.
// The returned frame is owned by the decoder; do not call FrameFree on it.
// If you need to keep the frame beyond the next decode call, make a copy.
// Returns nil frame on EOF.
func (d *Decoder) DecodeAudio() (Frame, error) {
return d.DecodeAudioContext(context.Background())
}
// DecodeAudioContext decodes the next audio frame with cancellation.
func (d *Decoder) DecodeAudioContext(ctx context.Context) (Frame, error) {
d.mu.Lock()
defer d.mu.Unlock()
if err := d.beginInterrupt(ctx); err != nil {
return Frame{}, err
}
defer d.clearInterrupt()
if !d.audioDecoderOpen {
if err := d.openAudioDecoderLocked(); err != nil {
return Frame{}, err
}
}
frame, err := d.nextFrameLocked(MediaTypeAudio)
return frame, d.finishInterrupt(err)
}
// ReadFrame reads and decodes the next frame (video or audio).
// Returns a FrameWrapper with the MediaType set.
// The frame is owned by the decoder; call Copy() if you need to keep it.
// Returns io.EOF after all selected streams are fully drained.
func (d *Decoder) ReadFrame() (*FrameWrapper, error) {
return d.ReadFrameContext(context.Background())
}
// ReadFrameContext reads and decodes the next frame with cancellation.
func (d *Decoder) ReadFrameContext(ctx context.Context) (*FrameWrapper, error) {
d.mu.Lock()
defer d.mu.Unlock()
if d.closed {
return nil, errDecoderClosed
}
if err := d.beginInterrupt(ctx); err != nil {
return nil, err
}
defer d.clearInterrupt()
// Open decoders if needed
if d.HasVideo() && !d.videoDecoderOpen {
if err := d.openVideoDecoderLocked(); err != nil {
return nil, err
}
}
if d.HasAudio() && !d.audioDecoderOpen {
if err := d.openAudioDecoderLocked(); err != nil {
return nil, err
}
}
frame, err := d.readFrameLocked()
return frame, d.finishInterrupt(err)
}
// ReadFrameCopy reads and decodes the next frame (video or audio) and returns an owned frame wrapper.
//
// The returned wrapper owns its underlying frame; the caller MUST call Free() when done.
// Returns io.EOF after all selected streams are fully drained.
func (d *Decoder) ReadFrameCopy() (*FrameWrapper, error) {
return d.ReadFrameCopyContext(context.Background())
}
// ReadFrameCopyContext reads an owned frame wrapper with cancellation.
func (d *Decoder) ReadFrameCopyContext(ctx context.Context) (*FrameWrapper, error) {
fw, err := d.ReadFrameContext(ctx)
if err != nil || fw == nil {
return nil, err
}
return fw.Copy()
}
// FlushDecoder discards buffered codec output and queued packets for selected streams.
func (d *Decoder) FlushDecoder() {
d.mu.Lock()
defer d.mu.Unlock()
if d.videoCodecCtx != nil {
avcodec.FlushBuffers(d.videoCodecCtx)
}
if d.audioCodecCtx != nil {
avcodec.FlushBuffers(d.audioCodecCtx)
}
d.clearDecodeStateLocked()
}
// Seek seeks to a position in the file.
// The timestamp is specified as time.Duration from the start.
func (d *Decoder) Seek(ts time.Duration) error {
return d.SeekContext(context.Background(), ts)
}
// SeekContext seeks to a position and interrupts blocking FFmpeg I/O when ctx is canceled.
func (d *Decoder) SeekContext(ctx context.Context, ts time.Duration) error {
return d.SeekTimestampContext(ctx, ts.Microseconds())
}
// SeekTimestamp seeks to a position in the file.
// timestamp is in AV_TIME_BASE (microseconds).
func (d *Decoder) SeekTimestamp(timestamp int64) error {
return d.SeekTimestampContext(context.Background(), timestamp)
}
// SeekTimestampContext seeks to a timestamp with cancellation.
func (d *Decoder) SeekTimestampContext(ctx context.Context, timestamp int64) error {
d.mu.Lock()
defer d.mu.Unlock()
if d.closed {
return errDecoderClosed
}
if err := d.beginInterrupt(ctx); err != nil {
return err
}
defer d.clearInterrupt()
// Seek to keyframe before target
if err := d.seekInputLocked(-1, timestamp, avformat.SeekFlagBackward); err != nil {
return d.finishInterrupt(err)
}
// Flush decoder buffers
if d.videoCodecCtx != nil {
avcodec.FlushBuffers(d.videoCodecCtx)
}
if d.audioCodecCtx != nil {
avcodec.FlushBuffers(d.audioCodecCtx)
}
d.clearDecodeStateLocked()
return nil
}
// Close releases all resources.
func (d *Decoder) Close() error {
d.closeSignal.Do(func() {
if d.interrupt != nil {
d.interrupt.stop()
}
if d.customIO != nil {
d.customIO.cancelPending()
}
})
d.mu.Lock()
defer d.mu.Unlock()
if d.closed {
return nil
}
d.closed = true
d.clearDecodeStateLocked()
d.clearPacketPoolLocked()
// Free frame
if d.frame != nil {
avutil.FrameFree(&d.frame)
}
if d.hardwareSoftwareFrame != nil {
avutil.FrameFree(&d.hardwareSoftwareFrame)
}
// Free packet
if d.packet != nil {
avcodec.PacketFree(&d.packet)
}
// Free video codec context
if d.videoCodecCtx != nil {
avcodec.FreeContext(&d.videoCodecCtx)
}
if d.ownedHWDevice != nil {
_ = d.ownedHWDevice.Close()
d.ownedHWDevice = nil
}
// Free audio codec context
if d.audioCodecCtx != nil {
avcodec.FreeContext(&d.audioCodecCtx)
}
// Close input
if d.interrupt != nil {
d.interrupt.release(d.formatCtx)
}
if d.formatCtx != nil {
avformat.CloseInput(&d.formatCtx)
}
// Cleanup any extra resources (e.g. custom I/O, temp files).
if d.cleanup != nil {
d.cleanup()
d.cleanup = nil
}
if d.customIO != nil {
_ = d.customIO.Close()
d.customIO = nil
}
return nil
}