forked from 3d0c/gmf
-
Notifications
You must be signed in to change notification settings - Fork 1
/
stream.go
110 lines (83 loc) · 2.14 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
package gmf
/*
#cgo pkg-config: libavformat
#include "libavformat/avformat.h"
*/
import "C"
import (
"fmt"
)
type Stream struct {
avStream *C.struct_AVStream
cc *CodecCtx
Pts int64
CgoMemoryManage
}
func (this *Stream) Free() {
// nothing to do
}
func (this *Stream) DumpContexCodec(codec *CodecCtx) {
ret := C.avcodec_copy_context(this.avStream.codec, codec.avCodecCtx)
if ret < 0 {
panic("Failed to copy context from input to output stream codec context\n")
}
}
func (this *Stream) SetCodecFlags() {
this.avStream.codec.flags |= C.CODEC_FLAG_GLOBAL_HEADER
}
func (this *Stream) CodecCtx() *CodecCtx {
if this.IsCodecCtxSet() {
return this.cc
}
// @todo make explicit decoder/encoder definition
// If the codec context wasn't set, it means that it's called from InputCtx
// and it should be decoder.
c, err := FindDecoder(int(this.avStream.codec.codec_id))
if err != nil {
panic(fmt.Sprintf("unable to initialize codec for stream '%d', error:", this.Index(), err))
}
this.cc = &CodecCtx{
codec: c,
avCodecCtx: this.avStream.codec,
}
this.cc.Open(nil)
return this.cc
}
func (this *Stream) SetCodecCtx(cc *CodecCtx) {
if cc == nil {
// don't sure that it should panic...
panic("Codec context is not initialized.")
}
Retain(cc) //just Retain .not need Release,it can free memory by C.avformat_free_context() @ format.go Free().
this.avStream.codec = cc.avCodecCtx
if this.cc != nil {
this.cc.avCodecCtx = cc.avCodecCtx
}
}
func (this *Stream) IsCodecCtxSet() bool {
return (this.cc != nil)
}
func (this *Stream) Index() int {
return int(this.avStream.index)
}
func (this *Stream) Id() int {
return int(this.avStream.id)
}
func (this *Stream) NbFrames() int {
return int(this.avStream.nb_frames)
}
func (this *Stream) TimeBase() AVRational {
return AVRational(this.avStream.time_base)
}
func (this *Stream) Type() int32 {
return this.CodecCtx().Type()
}
func (this *Stream) IsAudio() bool {
return (this.Type() == AVMEDIA_TYPE_AUDIO)
}
func (this *Stream) IsVideo() bool {
return (this.Type() == AVMEDIA_TYPE_VIDEO)
}
func (this *Stream) Duration() int64 {
return int64(this.avStream.duration)
}