forked from 3d0c/gmf
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpacket.go
More file actions
161 lines (120 loc) · 3.34 KB
/
packet.go
File metadata and controls
161 lines (120 loc) · 3.34 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
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
}
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) 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() int {
return int(this.avPacket.pts)
}
func (this *Packet) SetPts(pts int) {
this.avPacket.pts = C.int64_t(pts)
}
func (this *Packet) Dts() int {
return int(this.avPacket.dts)
}
func (this *Packet) SetDts(val int) {
this.avPacket.dts = C.int64_t(val)
}
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() int {
return int(this.avPacket.pos)
}
func (this *Packet) Data() []byte {
return C.GoBytes(unsafe.Pointer(this.avPacket.data), C.int(this.avPacket.size))
}
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)
}