-
Notifications
You must be signed in to change notification settings - Fork 170
/
packet_go112.go
155 lines (118 loc) · 3.05 KB
/
packet_go112.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
// +build go1.12
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 (
"fmt"
"unsafe"
)
const (
AV_PKT_FLAG_KEY = C.AV_PKT_FLAG_KEY // The packet contains a keyframe
)
type Packet struct {
avPacket C.struct_AVPacket
}
func NewPacket() *Packet {
p := &Packet{}
C.av_new_packet(&p.avPacket, 0)
return p
}
// Init same to NewPacket and av_new_packet
// Initialize optional fields of a packet with default values.
// Note, this does not touch the data and size members, which have to be
// initialized separately.
func Init() *Packet {
p := &Packet{}
C.av_new_packet(&p.avPacket, 0)
return p
}
func (p *Packet) Pts() int64 {
return int64(p.avPacket.pts)
}
func (p *Packet) SetPts(pts int64) *Packet {
p.avPacket.pts = C.int64_t(pts)
return p
}
func (p *Packet) Dts() int64 {
return int64(p.avPacket.dts)
}
func (p *Packet) SetDts(val int64) *Packet {
p.avPacket.dts = C.int64_t(val)
return p
}
func (p *Packet) Flags() int {
return int(p.avPacket.flags)
}
func (p *Packet) SetFlags(flags int) *Packet {
p.avPacket.flags = C.int(flags)
return p
}
func (p *Packet) Duration() int64 {
return int64(p.avPacket.duration)
}
func (p *Packet) SetDuration(duration int64) *Packet {
p.avPacket.duration = C.int64_t(duration)
return p
}
func (p *Packet) StreamIndex() int {
return int(p.avPacket.stream_index)
}
func (p *Packet) Size() int {
return int(p.avPacket.size)
}
func (p *Packet) Pos() int64 {
return int64(p.avPacket.pos)
}
func (p *Packet) Data() []byte {
return C.GoBytes(unsafe.Pointer(p.avPacket.data), C.int(p.avPacket.size))
}
// SetData [NOT SUGGESTED] should free data later
// p := gmf.NewPacket()
// defer p.Free()
// p.SetData([]byte{0x00, 0x00, 0x00, 0x01, 0x67})
// defer p.FreeData()
func (p *Packet) SetData(data []byte) *Packet {
p.avPacket.size = C.int(len(data))
p.avPacket.data = (*C.uint8_t)(C.CBytes(data))
return p
}
// FreeData free data when use SetData
// p := gmf.NewPacket()
// defer p.Free()
// p.SetData([]byte{0x00, 0x00, 0x00, 0x01, 0x67})
// defer p.FreeData()
func (p *Packet) FreeData() *Packet {
if p.avPacket.data != nil {
C.free(unsafe.Pointer(p.avPacket.data))
p.avPacket.data = nil
p.avPacket.size = 0
}
return p
}
func (p *Packet) Clone() *Packet {
np := NewPacket()
C.av_packet_ref(&np.avPacket, &p.avPacket)
return np
}
func (p *Packet) Dump() {
fmt.Printf("idx: %d\npts: %d\ndts: %d\nsize: %d\nduration:%d\npos:%d\ndata: % x\n", p.StreamIndex(), p.avPacket.pts, p.avPacket.dts, p.avPacket.size, p.avPacket.duration, p.avPacket.pos, C.GoBytes(unsafe.Pointer(p.avPacket.data), 128))
fmt.Println("------------------------------")
}
func (p *Packet) SetStreamIndex(val int) *Packet {
p.avPacket.stream_index = C.int(val)
return p
}
func (p *Packet) Free() {
C.av_packet_unref(&p.avPacket)
}
func (p *Packet) Time(timebase AVRational) int {
return int(float64(timebase.AVR().Num) / float64(timebase.AVR().Den) * float64(p.Pts()))
}