forked from 3d0c/gmf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
codec.go
124 lines (96 loc) · 2.72 KB
/
codec.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
package gmf
/*
#cgo pkg-config: libavcodec
#include <stdlib.h>
#include "libavcodec/avcodec.h"
#include "libavutil/pixfmt.h"
*/
import "C"
import (
"errors"
"fmt"
"unsafe"
)
var (
AVMEDIA_TYPE_AUDIO int32 = C.AVMEDIA_TYPE_AUDIO
AVMEDIA_TYPE_VIDEO int32 = C.AVMEDIA_TYPE_VIDEO
AV_PIX_FMT_BGR24 int32 = C.AV_PIX_FMT_BGR24
AV_PIX_FMT_GRAY8 int32 = C.AV_PIX_FMT_GRAY8
AV_PIX_FMT_RGB24 int32 = C.AV_PIX_FMT_RGB24
AV_PIX_FMT_YUV410P int32 = C.AV_PIX_FMT_YUV410P
AV_PIX_FMT_YUV411P int32 = C.AV_PIX_FMT_YUV411P
AV_PIX_FMT_YUV420P int32 = C.AV_PIX_FMT_YUV420P
AV_PIX_FMT_YUV422P int32 = C.AV_PIX_FMT_YUV422P
AV_PIX_FMT_YUV444P int32 = C.AV_PIX_FMT_YUV444P
AV_PIX_FMT_YUVJ420P int32 = C.AV_PIX_FMT_YUVJ420P
AV_PIX_FMT_YUYV422 int32 = C.AV_PIX_FMT_YUYV422
AV_PIX_FMT_NONE int32 = C.AV_PIX_FMT_NONE
FF_PROFILE_MPEG4_SIMPLE int = C.FF_PROFILE_MPEG4_SIMPLE
AV_NOPTS_VALUE int64 = C.AV_NOPTS_VALUE
)
func init() {
C.avcodec_register_all()
InitDesc()
}
type Codec struct {
avCodec *C.struct_AVCodec
CgoMemoryManage
}
func FindDecoder(i interface{}) (*Codec, error) {
var avc *C.struct_AVCodec
switch t := i.(type) {
case string:
cname := C.CString(i.(string))
defer C.free(unsafe.Pointer(cname))
avc = C.avcodec_find_decoder_by_name(cname)
break
case int:
avc = C.avcodec_find_decoder(uint32(i.(int)))
break
default:
return nil, errors.New(fmt.Sprintf("Unable to find codec, unexpected arguments type '%v'", t))
}
if avc == nil {
return nil, errors.New(fmt.Sprintf("Unable to find codec by value '%v'", i))
}
return &Codec{avCodec: avc}, nil
}
func FindEncoder(i interface{}) (*Codec, error) {
var avc *C.struct_AVCodec
switch t := i.(type) {
case string:
cname := C.CString(i.(string))
defer C.free(unsafe.Pointer(cname))
avc = C.avcodec_find_encoder_by_name(cname)
break
case int:
avc = C.avcodec_find_encoder(uint32(i.(int)))
break
default:
return nil, errors.New(fmt.Sprintf("Unable to find codec, unexpected arguments type '%v'", t))
}
if avc == nil {
return nil, errors.New(fmt.Sprintf("Unable to find codec by value '%v'", i))
}
return &Codec{avCodec: avc}, nil
}
func (this *Codec) Free() {
//nothing to do
}
func (this *Codec) Id() int {
return int(this.avCodec.id)
}
func (this *Codec) Name() string {
return C.GoString(this.avCodec.name)
}
func (this *Codec) LongName() string {
return C.GoString(this.avCodec.long_name)
}
func (this *Codec) Type() int {
// > ...field names that are keywords in Go can be
// > accessed by prefixing them with an underscore
return int(this.avCodec._type)
}
func (this *Codec) IsExperimental() bool {
return bool((this.avCodec.capabilities & C.CODEC_CAP_EXPERIMENTAL) != 0)
}