-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinput.go
163 lines (129 loc) · 3.51 KB
/
input.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
package input
import (
"fmt"
"github.com/asticode/goav/avcodec"
"github.com/asticode/goav/avformat"
"github.com/asticode/goav/avutil"
"gocoder/encode"
)
type Stream struct {
DecodeContext *avcodec.Context
Params *avcodec.CodecParameters
}
type Context struct {
FormatCtx *avformat.Context
Filename string
Streams []Stream
}
func NewContext() Context {
return Context{
FormatCtx: avformat.AvformatAllocContext(),
Filename: "",
Streams: nil,
}
}
func (c *Context) ReadInput() chan *avcodec.Packet {
outBuffer := make(chan *avcodec.Packet, 50)
go func() {
for {
// FIXME: deref this?
packet := avcodec.AvPacketAlloc()
err := c.FormatCtx.AvReadFrame(packet)
// FIXME: find a good way to communicate this out through the channel?
if err == avutil.AVERROR_EOF {
// Send the EOF packet to signal EOF to decoder thread
outBuffer <- packet
close(outBuffer)
return
} else if err < 0 {
close(outBuffer)
return
}
outBuffer <- packet
}
}()
return outBuffer
}
func (c *Context) DecodeStream(stream <-chan *avcodec.Packet) chan encode.Frame {
outBuffer := make(chan encode.Frame, 50)
go func() {
defer close(outBuffer)
for packet := range stream {
_ = c.sendToDecoder(packet)
index := packet.StreamIndex()
for err := 0; err >= 0; {
frame := avutil.AvFrameAlloc()
err = c.getFromDecoder(index, frame)
if err == avutil.AVERROR_EAGAIN {
break
} else if err == avutil.AVERROR_EOF {
// Send EOF frame to signal EOF to encoders?
//outBuffer <- frame
return
} else if err < 0 {
fmt.Printf("Error getting frame from decoder: %s\n",
avutil.AvStrerr(err))
return
}
if index == 0 {
outBuffer <- encode.NewFrame(frame, c.mediaType(index))
}
}
}
}()
return outBuffer
}
func (c *Context) OpenInput(path string) error {
c.Filename = path
err := avformat.AvformatOpenInput(&c.FormatCtx, c.Filename, nil, nil)
if err != 0 {
return fmt.Errorf("OpenInput: %v", avutil.AvStrerr(err))
}
for _, stream := range c.FormatCtx.Streams() {
params := stream.CodecParameters()
codec := avcodec.AvcodecFindDecoder(params.CodecId())
if codec == nil {
return fmt.Errorf("could not find decoder for %v", params.CodecId())
}
decodeContext := codec.AvcodecAllocContext3()
err = avcodec.AvcodecParametersToContext(decodeContext, params)
if err != 0 {
return fmt.Errorf("OpenInput: %v", avutil.AvStrerr(err))
}
err = decodeContext.AvcodecOpen2(codec, nil)
if err < 0 {
return fmt.Errorf("OpenInput: %v", avutil.AvStrerr(err))
}
c.Streams = append(c.Streams, Stream{
DecodeContext: decodeContext,
Params: params,
})
}
return nil
}
func (c *Context) mediaType(index int) avformat.MediaType {
return avformat.MediaType(c.Streams[index].Params.CodecType())
}
func (c *Context) getFromDecoder(index int, frame *avutil.Frame) int {
stream := c.Streams[index]
return avcodec.AvcodecReceiveFrame(stream.DecodeContext, frame)
}
func (c *Context) sendToDecoder(packet *avcodec.Packet) int {
streamIndex := packet.StreamIndex()
stream := c.Streams[streamIndex]
err := avcodec.AvcodecSendPacket(stream.DecodeContext, packet)
if err < 0 {
fmt.Printf("Error sending frame to decoder: %s\n",
avutil.AvStrerr(err))
}
return err
}
func (c *Context) CloseInput() {
avformat.AvformatCloseInput(c.FormatCtx)
}
func (c *Context) FindStreamInfo() {
c.FormatCtx.AvformatFindStreamInfo(nil)
}
func (c *Context) DumpStreamInfo() {
c.FormatCtx.AvDumpFormat(0, c.Filename, 0)
}