• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

asticode / go-astiav / 8084807577

28 Feb 2024 05:05PM UTC coverage: 88.01% (-0.3%) from 88.303%
8084807577

push

github

asticode
Added Classer + now logs returns a Classer + tests are now done in astiav package

165 of 193 new or added lines in 10 files covered. (85.49%)

1 existing line in 1 file now uncovered.

1791 of 2035 relevant lines covered (88.01%)

39.86 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

76.5
/codec_context.go
1
package astiav
2

3
//#cgo pkg-config: libavcodec libavutil
4
//#include <libavcodec/avcodec.h>
5
//#include <libavutil/frame.h>
6
/*
7
extern enum AVPixelFormat goAstiavCodecContextGetFormat(AVCodecContext *ctx, enum AVPixelFormat *pix_fmts, int pix_fmts_size);
8

9
static inline enum AVPixelFormat astiavCodecContextGetFormat(AVCodecContext *ctx, const enum AVPixelFormat *pix_fmts)
10
{
11
        int pix_fmts_size = 0;
12
        while (*pix_fmts != AV_PIX_FMT_NONE) {
13
                pix_fmts_size++;
14
                pix_fmts++;
15
        }
16
        pix_fmts -= pix_fmts_size;
17
        return goAstiavCodecContextGetFormat(ctx, (enum AVPixelFormat*)(pix_fmts), pix_fmts_size);
18
}
19
static inline void astiavSetCodecContextGetFormat(AVCodecContext *ctx)
20
{
21
        ctx->get_format = astiavCodecContextGetFormat;
22
}
23
static inline void astiavResetCodecContextGetFormat(AVCodecContext *ctx)
24
{
25
        ctx->get_format = NULL;
26
}
27

28
*/
29
import "C"
30
import (
31
        "sync"
32
        "unsafe"
33
)
34

35
// https://github.com/FFmpeg/FFmpeg/blob/n5.0/libavcodec/avcodec.h#L383
36
type CodecContext struct {
37
        c *C.struct_AVCodecContext
38
        // We need to store this to unref it properly
39
        hdc *HardwareDeviceContext
40
}
41

42
func newCodecContextFromC(c *C.struct_AVCodecContext) *CodecContext {
27✔
43
        if c == nil {
27✔
NEW
44
                return nil
×
NEW
45
        }
×
46
        cc := &CodecContext{c: c}
27✔
47
        classers.set(cc)
27✔
48
        return cc
27✔
49
}
50

51
var _ Classer = (*CodecContext)(nil)
52

53
func AllocCodecContext(c *Codec) *CodecContext {
27✔
54
        var cc *C.struct_AVCodec
27✔
55
        if c != nil {
48✔
56
                cc = c.c
21✔
57
        }
21✔
58
        return newCodecContextFromC(C.avcodec_alloc_context3(cc))
27✔
59
}
60

61
func (cc *CodecContext) Free() {
21✔
62
        if cc.hdc != nil {
21✔
63
                C.av_buffer_unref(&cc.hdc.c)
×
64
                cc.hdc = nil
×
65
        }
×
66
        classers.del(cc)
21✔
67
        C.avcodec_free_context(&cc.c)
21✔
68
}
69

70
func (cc *CodecContext) String() string {
6✔
71
        s, _ := stringFromC(255, func(buf *C.char, size C.size_t) error {
12✔
72
                C.avcodec_string(buf, C.int(size), cc.c, C.int(0))
6✔
73
                return nil
6✔
74
        })
6✔
75
        return s
6✔
76
}
77

78
func (cc *CodecContext) BitRate() int64 {
9✔
79
        return int64(cc.c.bit_rate)
9✔
80
}
9✔
81

82
func (cc *CodecContext) SetBitRate(bitRate int64) {
3✔
83
        cc.c.bit_rate = C.int64_t(bitRate)
3✔
84
}
3✔
85

86
func (cc *CodecContext) Channels() int {
9✔
87
        return int(cc.c.channels)
9✔
88
}
9✔
89

90
func (cc *CodecContext) SetChannels(channels int) {
3✔
91
        cc.c.channels = C.int(channels)
3✔
92
}
3✔
93

94
func (cc *CodecContext) ChannelLayout() ChannelLayout {
6✔
95
        l, _ := newChannelLayoutFromC(&cc.c.ch_layout).clone()
6✔
96
        return l
6✔
97
}
6✔
98

99
func (cc *CodecContext) SetChannelLayout(l ChannelLayout) {
3✔
100
        l.copy(&cc.c.ch_layout) //nolint: errcheck
3✔
101
}
3✔
102

103
func (cc *CodecContext) ChromaLocation() ChromaLocation {
3✔
104
        return ChromaLocation(cc.c.chroma_sample_location)
3✔
105
}
3✔
106

107
func (cc *CodecContext) Class() *Class {
54✔
108
        return newClassFromC(unsafe.Pointer(cc.c))
54✔
109
}
54✔
110

111
func (cc *CodecContext) CodecID() CodecID {
6✔
112
        return CodecID(cc.c.codec_id)
6✔
113
}
6✔
114

115
func (cc *CodecContext) ColorPrimaries() ColorPrimaries {
3✔
116
        return ColorPrimaries(cc.c.color_primaries)
3✔
117
}
3✔
118

119
func (cc *CodecContext) ColorRange() ColorRange {
3✔
120
        return ColorRange(cc.c.color_range)
3✔
121
}
3✔
122

123
func (cc *CodecContext) ColorSpace() ColorSpace {
3✔
124
        return ColorSpace(cc.c.colorspace)
3✔
125
}
3✔
126

127
func (cc *CodecContext) ColorTransferCharacteristic() ColorTransferCharacteristic {
3✔
128
        return ColorTransferCharacteristic(cc.c.color_trc)
3✔
129
}
3✔
130

131
func (cc *CodecContext) Flags() CodecContextFlags {
3✔
132
        return CodecContextFlags(cc.c.flags)
3✔
133
}
3✔
134

135
func (cc *CodecContext) SetFlags(fs CodecContextFlags) {
3✔
136
        cc.c.flags = C.int(fs)
3✔
137
}
3✔
138

139
func (cc *CodecContext) Flags2() CodecContextFlags2 {
3✔
140
        return CodecContextFlags2(cc.c.flags2)
3✔
141
}
3✔
142

143
func (cc *CodecContext) SetFlags2(fs CodecContextFlags2) {
3✔
144
        cc.c.flags2 = C.int(fs)
3✔
145
}
3✔
146

147
func (cc *CodecContext) Framerate() Rational {
3✔
148
        return newRationalFromC(cc.c.framerate)
3✔
149
}
3✔
150

151
func (cc *CodecContext) SetFramerate(f Rational) {
3✔
152
        cc.c.framerate = f.c
3✔
153
}
3✔
154

155
func (cc *CodecContext) FrameSize() int {
3✔
156
        return int(cc.c.frame_size)
3✔
157
}
3✔
158

159
func (cc *CodecContext) GopSize() int {
6✔
160
        return int(cc.c.gop_size)
6✔
161
}
6✔
162

163
func (cc *CodecContext) SetGopSize(gopSize int) {
3✔
164
        cc.c.gop_size = C.int(gopSize)
3✔
165
}
3✔
166

167
func (cc *CodecContext) Height() int {
6✔
168
        return int(cc.c.height)
6✔
169
}
6✔
170

171
func (cc *CodecContext) SetHeight(height int) {
6✔
172
        cc.c.height = C.int(height)
6✔
173
}
6✔
174

175
func (cc *CodecContext) Level() Level {
3✔
176
        return Level(cc.c.level)
3✔
177
}
3✔
178

179
func (cc *CodecContext) MediaType() MediaType {
6✔
180
        return MediaType(cc.c.codec_type)
6✔
181
}
6✔
182

183
func (cc *CodecContext) PixelFormat() PixelFormat {
6✔
184
        return PixelFormat(cc.c.pix_fmt)
6✔
185
}
6✔
186

187
func (cc *CodecContext) SetPixelFormat(pixFmt PixelFormat) {
6✔
188
        cc.c.pix_fmt = C.enum_AVPixelFormat(pixFmt)
6✔
189
}
6✔
190

191
func (cc *CodecContext) Profile() Profile {
3✔
192
        return Profile(cc.c.profile)
3✔
193
}
3✔
194

195
func (cc *CodecContext) Qmin() int {
3✔
196
        return int(cc.c.qmin)
3✔
197
}
3✔
198

199
func (cc *CodecContext) SetQmin(qmin int) {
3✔
200
        cc.c.qmin = C.int(qmin)
3✔
201
}
3✔
202

203
func (cc *CodecContext) SampleAspectRatio() Rational {
6✔
204
        return newRationalFromC(cc.c.sample_aspect_ratio)
6✔
205
}
6✔
206

207
func (cc *CodecContext) SetSampleAspectRatio(r Rational) {
3✔
208
        cc.c.sample_aspect_ratio = r.c
3✔
209
}
3✔
210

211
func (cc *CodecContext) SampleFormat() SampleFormat {
6✔
212
        return SampleFormat(cc.c.sample_fmt)
6✔
213
}
6✔
214

215
func (cc *CodecContext) SetSampleFormat(f SampleFormat) {
3✔
216
        cc.c.sample_fmt = C.enum_AVSampleFormat(f)
3✔
217
}
3✔
218

219
func (cc *CodecContext) SampleRate() int {
6✔
220
        return int(cc.c.sample_rate)
6✔
221
}
6✔
222

223
func (cc *CodecContext) SetSampleRate(sampleRate int) {
3✔
224
        cc.c.sample_rate = C.int(sampleRate)
3✔
225
}
3✔
226

227
func (cc *CodecContext) StrictStdCompliance() StrictStdCompliance {
9✔
228
        return StrictStdCompliance(cc.c.strict_std_compliance)
9✔
229
}
9✔
230

231
func (cc *CodecContext) SetStrictStdCompliance(c StrictStdCompliance) {
3✔
232
        cc.c.strict_std_compliance = C.int(c)
3✔
233
}
3✔
234

235
func (cc *CodecContext) TimeBase() Rational {
3✔
236
        return newRationalFromC(cc.c.time_base)
3✔
237
}
3✔
238

239
func (cc *CodecContext) SetTimeBase(r Rational) {
6✔
240
        cc.c.time_base = r.c
6✔
241
}
6✔
242

243
func (cc *CodecContext) ThreadCount() int {
9✔
244
        return int(cc.c.thread_count)
9✔
245
}
9✔
246

247
func (cc *CodecContext) SetThreadCount(threadCount int) {
3✔
248
        cc.c.thread_count = C.int(threadCount)
3✔
249
}
3✔
250

251
func (cc *CodecContext) ThreadType() ThreadType {
9✔
252
        return ThreadType(cc.c.thread_type)
9✔
253
}
9✔
254

255
func (cc *CodecContext) SetThreadType(t ThreadType) {
3✔
256
        cc.c.thread_type = C.int(t)
3✔
257
}
3✔
258

259
func (cc *CodecContext) Width() int {
6✔
260
        return int(cc.c.width)
6✔
261
}
6✔
262

263
func (cc *CodecContext) SetWidth(width int) {
6✔
264
        cc.c.width = C.int(width)
6✔
265
}
6✔
266

267
func (cc *CodecContext) Open(c *Codec, d *Dictionary) error {
9✔
268
        var dc **C.struct_AVDictionary
9✔
269
        if d != nil {
9✔
270
                dc = &d.c
×
271
        }
×
272
        return newError(C.avcodec_open2(cc.c, c.c, dc))
9✔
273
}
274

275
func (cc *CodecContext) ReceivePacket(p *Packet) error {
×
276
        var pc *C.struct_AVPacket
×
277
        if p != nil {
×
278
                pc = p.c
×
279
        }
×
280
        return newError(C.avcodec_receive_packet(cc.c, pc))
×
281
}
282

283
func (cc *CodecContext) SendPacket(p *Packet) error {
363✔
284
        var pc *C.struct_AVPacket
363✔
285
        if p != nil {
726✔
286
                pc = p.c
363✔
287
        }
363✔
288
        return newError(C.avcodec_send_packet(cc.c, pc))
363✔
289
}
290

291
func (cc *CodecContext) ReceiveFrame(f *Frame) error {
726✔
292
        var fc *C.struct_AVFrame
726✔
293
        if f != nil {
1,452✔
294
                fc = f.c
726✔
295
        }
726✔
296
        return newError(C.avcodec_receive_frame(cc.c, fc))
726✔
297
}
298

299
func (cc *CodecContext) SendFrame(f *Frame) error {
×
300
        var fc *C.struct_AVFrame
×
301
        if f != nil {
×
302
                fc = f.c
×
303
        }
×
304
        return newError(C.avcodec_send_frame(cc.c, fc))
×
305
}
306

307
func (cc *CodecContext) SetHardwareDeviceContext(hdc *HardwareDeviceContext) {
×
308
        if cc.hdc != nil {
×
309
                C.av_buffer_unref(&cc.hdc.c)
×
310
        }
×
311
        cc.hdc = hdc
×
312
        if cc.hdc != nil {
×
313
                cc.c.hw_device_ctx = C.av_buffer_ref(cc.hdc.c)
×
314
        }
×
315
}
316

317
type CodecContextPixelFormatCallback func(pfs []PixelFormat) PixelFormat
318

319
var (
320
        codecContextPixelFormatCallbacks      = make(map[*C.struct_AVCodecContext]CodecContextPixelFormatCallback)
321
        codecContextPixelFormatCallbacksMutex = &sync.Mutex{}
322
)
323

324
func (cc *CodecContext) SetPixelFormatCallback(c CodecContextPixelFormatCallback) {
×
325
        // Lock
×
326
        codecContextPixelFormatCallbacksMutex.Lock()
×
327
        defer codecContextPixelFormatCallbacksMutex.Unlock()
×
328

×
329
        // Update callback
×
330
        if c == nil {
×
331
                C.astiavResetCodecContextGetFormat(cc.c)
×
332
                delete(codecContextPixelFormatCallbacks, cc.c)
×
333
        } else {
×
334
                C.astiavSetCodecContextGetFormat(cc.c)
×
335
                codecContextPixelFormatCallbacks[cc.c] = c
×
336
        }
×
337
}
338

339
//export goAstiavCodecContextGetFormat
340
func goAstiavCodecContextGetFormat(cc *C.struct_AVCodecContext, pfsCPtr *C.enum_AVPixelFormat, pfsCSize C.int) C.enum_AVPixelFormat {
×
341
        // Lock
×
342
        codecContextPixelFormatCallbacksMutex.Lock()
×
343
        defer codecContextPixelFormatCallbacksMutex.Unlock()
×
344

×
345
        // Get callback
×
346
        c, ok := codecContextPixelFormatCallbacks[cc]
×
347
        if !ok {
×
348
                return C.enum_AVPixelFormat(PixelFormatNone)
×
349
        }
×
350

351
        // Get pixel formats
352
        var pfs []PixelFormat
×
353
        for _, v := range unsafe.Slice(pfsCPtr, pfsCSize) {
×
354
                pfs = append(pfs, PixelFormat(v))
×
355
        }
×
356

357
        // Callback
358
        return C.enum_AVPixelFormat(c(pfs))
×
359

360
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc