-
Notifications
You must be signed in to change notification settings - Fork 0
/
stream.go
175 lines (136 loc) · 3.39 KB
/
stream.go
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
package gmf
/*
#cgo pkg-config: libavformat libavcodec
#include "libavformat/avformat.h"
#include "libavcodec/avcodec.h"
*/
import "C"
import (
"fmt"
)
type Stream struct {
avStream *C.struct_AVStream
SwsCtx *SwsCtx
SwrCtx *SwrCtx
AvFifo *AVAudioFifo
cc *CodecCtx
Pts int64
CgoMemoryManage
}
func (s *Stream) Free() {
if s.SwsCtx != nil {
s.SwsCtx.Free()
}
if s.SwrCtx != nil {
s.SwrCtx.Free()
}
if s.AvFifo != nil {
s.AvFifo.Free()
}
}
func (s *Stream) DumpContexCodec(codec *CodecCtx) {
ret := C.avcodec_parameters_from_context(s.avStream.codecpar, codec.avCodecCtx)
if ret < 0 {
panic("Failed to copy context from input to output stream codec context\n")
}
}
func (s *Stream) SetCodecFlags() {
s.avStream.codec.flags |= C.AV_CODEC_FLAG_GLOBAL_HEADER
}
func (s *Stream) CodecCtx() *CodecCtx {
// Supposed that output context is set and opened by user
if s.IsCodecCtxSet() {
return s.cc
}
// Open input codec context
c, err := FindDecoder(int(s.avStream.codec.codec_id))
if err != nil {
return nil
}
if s.cc = NewCodecCtx(c); s.cc == nil {
panic("error allocating codec context")
}
ret := int(C.avcodec_parameters_to_context(s.cc.avCodecCtx, s.avStream.codecpar))
if ret < 0 {
panic("error copying parameters to codec context")
}
if err := s.cc.Open(nil); err != nil {
panic("error opening codec context")
}
s.cc.avCodecCtx.time_base = s.avStream.codec.time_base
return s.cc
}
func (s *Stream) SetCodecCtx(cc *CodecCtx) {
s.cc = cc
}
func (s *Stream) SetCodecParameters(cp *CodecParameters) error {
if cp == nil || cp.avCodecParameters == nil {
return fmt.Errorf("codec parameters are not initialized")
}
s.avStream.codecpar = cp.avCodecParameters
return nil
}
func (s *Stream) IsCodecCtxSet() bool {
return (s.cc != nil)
}
func (s *Stream) Index() int {
return int(s.avStream.index)
}
func (s *Stream) Id() int {
return int(s.avStream.id)
}
func (s *Stream) NbFrames() int {
if int(s.avStream.nb_frames) == 0 {
return 1
}
return int(s.avStream.nb_frames)
}
func (s *Stream) TimeBase() AVRational {
return AVRational(s.avStream.time_base)
}
func (s *Stream) Type() int32 {
return s.CodecCtx().Type()
}
func (s *Stream) IsAudio() bool {
return (s.Type() == AVMEDIA_TYPE_AUDIO)
}
func (s *Stream) IsVideo() bool {
return (s.Type() == AVMEDIA_TYPE_VIDEO)
}
func (s *Stream) Duration() int64 {
return int64(s.avStream.duration)
}
func (s *Stream) SetTimeBase(val AVR) *Stream {
s.avStream.time_base.num = C.int(val.Num)
s.avStream.time_base.den = C.int(val.Den)
return s
}
func (s *Stream) GetRFrameRate() AVRational {
return AVRational(s.avStream.r_frame_rate)
}
func (s *Stream) SetRFrameRate(val AVR) {
s.avStream.r_frame_rate.num = C.int(val.Num)
s.avStream.r_frame_rate.den = C.int(val.Den)
}
func (s *Stream) SetAvgFrameRate(val AVR) {
s.avStream.avg_frame_rate.num = C.int(val.Num)
s.avStream.avg_frame_rate.den = C.int(val.Den)
}
func (s *Stream) GetAvgFrameRate() AVRational {
return AVRational(s.avStream.avg_frame_rate)
}
func (s *Stream) GetStartTime() int64 {
return int64(s.avStream.start_time)
}
func (s *Stream) GetCodecPar() *CodecParameters {
cp := NewCodecParameters()
cp.avCodecParameters = s.avStream.codecpar
return cp
}
func (s *Stream) CopyCodecPar(cp *CodecParameters) error {
ret := int(C.avcodec_parameters_copy(s.avStream.codecpar, cp.avCodecParameters))
if ret < 0 {
return AvError(ret)
}
return nil
}