-
Notifications
You must be signed in to change notification settings - Fork 0
/
packet.go
232 lines (173 loc) · 4.97 KB
/
packet.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
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
package gmf
/*
#cgo pkg-config: libavcodec
#include "libavcodec/avcodec.h"
void shift_data(AVPacket *pkt, int offset) {
pkt->data += offset;
pkt->size -= offset;
return;
}
*/
import "C"
import (
"errors"
"fmt"
"unsafe"
)
// In cause of this:
// > AVFrame is typically allocated once and then reused multiple times to hold
// > different data (e.g. a single AVFrame to hold frames received from a
// > decoder).
// this stuff with map of singletons is used.
//
var frames map[int32]*Frame = make(map[int32]*Frame, 0)
type Packet struct {
avPacket C.AVPacket
CgoMemoryManage
}
func NewPacket() *Packet {
p := &Packet{}
C.av_init_packet(&p.avPacket)
p.avPacket.data = nil
p.avPacket.size = 0
return p
}
// @todo should be private
func (this *Packet) Decode(cc *CodecCtx) (*Frame, bool, int, error) {
var gotOutput int
var ret int = 0
if frames[cc.Type()] == nil {
frames[cc.Type()] = &Frame{avFrame: C.av_frame_alloc(), mediaType: cc.Type()}
}
switch cc.Type() {
case AVMEDIA_TYPE_AUDIO:
ret = int(C.avcodec_decode_audio4(cc.avCodecCtx, frames[AVMEDIA_TYPE_AUDIO].avFrame, (*C.int)(unsafe.Pointer(&gotOutput)), &this.avPacket))
if ret < 0 {
return nil, false, int(ret), errors.New(fmt.Sprintf("Unable to decode audio packet, averror: %s", AvError(int(ret))))
}
break
case AVMEDIA_TYPE_VIDEO:
ret = int(C.avcodec_decode_video2(cc.avCodecCtx, frames[AVMEDIA_TYPE_VIDEO].avFrame, (*C.int)(unsafe.Pointer(&gotOutput)), &this.avPacket))
if ret < 0 {
return nil, false, int(ret), errors.New(fmt.Sprintf("Unable to decode video packet, averror: %s", AvError(int(ret))))
}
break
default:
return nil, false, int(ret), errors.New(fmt.Sprintf("Unknown codec type: %v", cc.Type()))
}
return frames[cc.Type()], (gotOutput > 0), int(ret), nil
}
func (this *Packet) DecodeToNewFrame(cc *CodecCtx) (*Frame, bool, int, error) {
f := &Frame{avFrame: C.av_frame_alloc(), mediaType: cc.Type()}
return this.decode(cc, f)
}
func (this *Packet) decode(cc *CodecCtx, frame *Frame) (*Frame, bool, int, error) {
var gotOutput int
var ret int = 0
switch cc.Type() {
case AVMEDIA_TYPE_AUDIO:
ret = int(C.avcodec_decode_audio4(cc.avCodecCtx, frame.avFrame, (*C.int)(unsafe.Pointer(&gotOutput)), &this.avPacket))
if ret < 0 {
return nil, false, int(ret), errors.New(fmt.Sprintf("Unable to decode audio packet, averror: %s", AvError(int(ret))))
}
break
case AVMEDIA_TYPE_VIDEO:
ret = int(C.avcodec_decode_video2(cc.avCodecCtx, frame.avFrame, (*C.int)(unsafe.Pointer(&gotOutput)), &this.avPacket))
if ret < 0 {
return nil, false, int(ret), errors.New(fmt.Sprintf("Unable to decode video packet, averror: %s", AvError(int(ret))))
}
break
default:
return nil, false, int(ret), errors.New(fmt.Sprintf("Unknown codec type: %v", cc.Type()))
}
return frame, (gotOutput > 0), int(ret), nil
}
func (this *Packet) GetNextFrame(cc *CodecCtx) (*Frame, error) {
for {
if this.avPacket.size <= 0 {
break
}
frame, ready, ret, err := this.DecodeToNewFrame(cc)
if !ready {
Release(frame)
if ret < 0 || err != nil {
return nil, err
}
}
C.shift_data(&this.avPacket, C.int(ret))
if ready {
return frame, nil
}
}
return nil, nil
}
func (this *Packet) Frames(cc *CodecCtx) chan *Frame {
yield := make(chan *Frame)
go func() {
defer close(yield)
for {
frame, ready, ret, err := this.Decode(cc)
if ready {
yield <- frame
}
if ret < 0 || err != nil {
fmt.Println("Decoding error:", err)
break
}
C.shift_data(&this.avPacket, C.int(ret))
if this.avPacket.size <= 0 {
break
}
}
}()
return yield
}
func (this *Packet) Pts() int64 {
return int64(this.avPacket.pts)
}
func (this *Packet) SetPts(pts int64) {
this.avPacket.pts = C.int64_t(pts)
}
func (this *Packet) Dts() int64 {
return int64(this.avPacket.dts)
}
func (this *Packet) SetDts(val int64) {
this.avPacket.dts = _Ctype_int64_t(val)
}
func (this *Packet) Flags() int {
return int(this.avPacket.flags)
}
func (this *Packet) Duration() int {
return int(this.avPacket.duration)
}
func (this *Packet) SetDuration(duration int) {
this.avPacket.duration = C.int(duration)
}
func (this *Packet) StreamIndex() int {
return int(this.avPacket.stream_index)
}
func (this *Packet) Size() int {
return int(this.avPacket.size)
}
func (this *Packet) Pos() int64 {
return int64(this.avPacket.pos)
}
func (this *Packet) Data() []byte {
return C.GoBytes(unsafe.Pointer(this.avPacket.data), C.int(this.avPacket.size))
}
func (this *Packet) Clone() *Packet {
np := NewPacket()
C.av_copy_packet(&np.avPacket, &this.avPacket)
return np
}
func (this *Packet) Dump() {
fmt.Println(this.avPacket)
fmt.Println("pkt:{\n", "pts:", this.avPacket.pts, "\ndts:", this.avPacket.dts, "\ndata:", string(C.GoBytes(unsafe.Pointer(this.avPacket.data), 128)), "size:", this.avPacket.size, "\n}")
}
func (this *Packet) SetStreamIndex(val int) *Packet {
this.avPacket.stream_index = C.int(val)
return this
}
func (this *Packet) Free() {
C.av_free_packet(&this.avPacket)
}