Skip to content

Commit

Permalink
Working tutorial; More hooks to libav API; removes debbuging print
Browse files Browse the repository at this point in the history
Missing constants, additional functions, remove errors
  • Loading branch information
corebreaker authored and giorgisio committed Apr 7, 2019
1 parent 821716f commit 001779d
Show file tree
Hide file tree
Showing 27 changed files with 849 additions and 290 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.idea/
.vscode/
1 change: 0 additions & 1 deletion _config.yml

This file was deleted.

33 changes: 33 additions & 0 deletions avcodec/avcodec.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type (
BitStreamFilterContext C.struct_AVBitStreamFilterContext
Rational C.struct_AVRational
Class C.struct_AVClass
AvCodecParameters C.struct_AVCodecParameters
AvHWAccel C.struct_AVHWAccel
AvPacketSideData C.struct_AVPacketSideData
AvPanScan C.struct_AVPanScan
Expand All @@ -56,6 +57,30 @@ type (
AvSampleFormat C.enum_AVSampleFormat
)

func (cp *AvCodecParameters) AvCodecGetId() CodecId {
return *((*CodecId)(unsafe.Pointer(&cp.codec_id)))
}

func (cp *AvCodecParameters) AvCodecGetType() MediaType {
return *((*MediaType)(unsafe.Pointer(&cp.codec_type)))
}

func (cp *AvCodecParameters) AvCodecGetWidth() int {
return *((*int)(unsafe.Pointer(&cp.width)))
}

func (cp *AvCodecParameters) AvCodecGetHeight() int {
return *((*int)(unsafe.Pointer(&cp.height)))
}

func (cp *AvCodecParameters) AvCodecGetChannels() int {
return *((*int)(unsafe.Pointer(&cp.channels)))
}

func (cp *AvCodecParameters) AvCodecGetSampleRate() int {
return *((*int)(unsafe.Pointer(&cp.sample_rate)))
}

func (c *Codec) AvCodecGetMaxLowres() int {
return int(C.av_codec_get_max_lowres((*C.struct_AVCodec)(c)))
}
Expand Down Expand Up @@ -136,6 +161,10 @@ func AvsubtitleFree(s *AvSubtitle) {
C.avsubtitle_free((*C.struct_AVSubtitle)(s))
}

func AvPacketAlloc() *Packet {
return (*Packet)(C.av_packet_alloc())
}

//Pack a dictionary for use in side_data.
func AvPacketPackDictionary(d *Dictionary, s *int) *uint8 {
return (*uint8)(C.av_packet_pack_dictionary((*C.struct_AVDictionary)(d), (*C.int)(unsafe.Pointer(s))))
Expand All @@ -151,6 +180,10 @@ func AvcodecFindDecoder(id CodecId) *Codec {
return (*Codec)(C.avcodec_find_decoder((C.enum_AVCodecID)(id)))
}

func AvCodecIterate(p *unsafe.Pointer) *Codec {
return (*Codec)(C.av_codec_iterate(p))
}

//Find a registered decoder with the specified name.
func AvcodecFindDecoderByName(n string) *Codec {
return (*Codec)(C.avcodec_find_decoder_by_name(C.CString(n)))
Expand Down
25 changes: 13 additions & 12 deletions avcodec/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,15 @@ package avcodec
import "C"
import (
"unsafe"

"github.com/selfmodify/goav/common"
)

func (ctxt *Context) AvCodecGetPktTimebase() Rational {
return (Rational)(C.av_codec_get_pkt_timebase((*C.struct_AVCodecContext)(ctxt)))
return Rational(C.av_codec_get_pkt_timebase((*C.struct_AVCodecContext)(ctxt)))
}

// AvCodecGetPktTimebase2 returns the timebase rational number as numerator and denominator
func (ctxt *Context) AvCodecGetPktTimebase2() (timebase common.AVRational) {
r := ctxt.AvCodecGetPktTimebase()
timebase = common.AVRational{
Num: int(r.num),
Den: int(r.den),
}
return
func (ctxt *Context) AvCodecGetPktTimebase2() Rational {
return ctxt.AvCodecGetPktTimebase()
}

func (ctxt *Context) AvCodecSetPktTimebase(r Rational) {
Expand Down Expand Up @@ -179,7 +172,7 @@ func (ctxt *Context) SetTimebase(num1 int, den1 int) {
ctxt.time_base.den = C.int(den1)
}

func (ctxt *Context) SetEncodeParams2(width int, height int, pxlFmt common.PixelFormat, hasBframes bool, gopSize int) {
func (ctxt *Context) SetEncodeParams2(width int, height int, pxlFmt PixelFormat, hasBframes bool, gopSize int) {
ctxt.width = C.int(width)
ctxt.height = C.int(height)
// ctxt.bit_rate = 1000000
Expand All @@ -197,6 +190,14 @@ func (ctxt *Context) SetEncodeParams2(width int, height int, pxlFmt common.Pixel
// C.av_opt_set(ctxt.priv_data, "preset", "ultrafast", 0)
}

func (ctxt *Context) SetEncodeParams(width int, height int, pxlFmt common.PixelFormat) {
func (ctxt *Context) SetEncodeParams(width int, height int, pxlFmt PixelFormat) {
ctxt.SetEncodeParams2(width, height, pxlFmt, false /*no b frames*/, 10)
}

func (ctxt *Context) AvcodecSendPacket(packet *Packet) int {
return (int)(C.avcodec_send_packet((*C.struct_AVCodecContext)(ctxt), (*C.struct_AVPacket)(packet)))
}

func (ctxt *Context) AvcodecReceiveFrame(frame *Frame) int {
return (int)(C.avcodec_receive_frame((*C.struct_AVCodecContext)(ctxt), (*C.struct_AVFrame)(frame)))
}
39 changes: 39 additions & 0 deletions avcodec/flags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Use of this source code is governed by a MIT license that can be found in the LICENSE file.
// Giorgis ([email protected])

package avcodec

//#cgo pkg-config: libavcodec
//#include <libavcodec/avcodec.h>
import "C"

const (
AV_CODEC_FLAG_UNALIGNED = int(C.AV_CODEC_FLAG_UNALIGNED)
AV_CODEC_FLAG_QSCALE = int(C.AV_CODEC_FLAG_QSCALE)
AV_CODEC_FLAG_4MV = int(C.AV_CODEC_FLAG_4MV)
AV_CODEC_FLAG_OUTPUT_CORRUPT = int(C.AV_CODEC_FLAG_OUTPUT_CORRUPT)
AV_CODEC_FLAG_QPEL = int(C.AV_CODEC_FLAG_QPEL)
AV_CODEC_FLAG_PASS1 = int(C.AV_CODEC_FLAG_PASS1)
AV_CODEC_FLAG_PASS2 = int(C.AV_CODEC_FLAG_PASS2)
AV_CODEC_FLAG_LOOP_FILTER = int(C.AV_CODEC_FLAG_LOOP_FILTER)
AV_CODEC_FLAG_GRAY = int(C.AV_CODEC_FLAG_GRAY)
AV_CODEC_FLAG_PSNR = int(C.AV_CODEC_FLAG_PSNR)
AV_CODEC_FLAG_TRUNCATED = int(C.AV_CODEC_FLAG_TRUNCATED)
AV_CODEC_FLAG_INTERLACED_DCT = int(C.AV_CODEC_FLAG_INTERLACED_DCT)
AV_CODEC_FLAG_LOW_DELAY = int(C.AV_CODEC_FLAG_LOW_DELAY)
AV_CODEC_FLAG_GLOBAL_HEADER = int(C.AV_CODEC_FLAG_GLOBAL_HEADER)
AV_CODEC_FLAG_BITEXACT = int(C.AV_CODEC_FLAG_BITEXACT)
AV_CODEC_FLAG_AC_PRED = int(C.AV_CODEC_FLAG_AC_PRED)
AV_CODEC_FLAG_INTERLACED_ME = int(C.AV_CODEC_FLAG_INTERLACED_ME)
AV_CODEC_FLAG_CLOSED_GOP = int(C.AV_CODEC_FLAG_CLOSED_GOP)
AV_CODEC_FLAG2_FAST = int(C.AV_CODEC_FLAG2_FAST)
AV_CODEC_FLAG2_NO_OUTPUT = int(C.AV_CODEC_FLAG2_NO_OUTPUT)
AV_CODEC_FLAG2_LOCAL_HEADER = int(C.AV_CODEC_FLAG2_LOCAL_HEADER)
AV_CODEC_FLAG2_DROP_FRAME_TIMECODE = int(C.AV_CODEC_FLAG2_DROP_FRAME_TIMECODE)
AV_CODEC_FLAG2_CHUNKS = int(C.AV_CODEC_FLAG2_CHUNKS)
AV_CODEC_FLAG2_IGNORE_CROP = int(C.AV_CODEC_FLAG2_IGNORE_CROP)
AV_CODEC_FLAG2_SHOW_ALL = int(C.AV_CODEC_FLAG2_SHOW_ALL)
AV_CODEC_FLAG2_EXPORT_MVS = int(C.AV_CODEC_FLAG2_EXPORT_MVS)
AV_CODEC_FLAG2_SKIP_MANUAL = int(C.AV_CODEC_FLAG2_SKIP_MANUAL)
AV_CODEC_FLAG2_RO_FLUSH_NOOP = int(C.AV_CODEC_FLAG2_RO_FLUSH_NOOP)
)
8 changes: 8 additions & 0 deletions avcodec/packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,17 @@ import (
"unsafe"
)

const (
AV_PKT_FLAG_KEY = int(C.AV_PKT_FLAG_KEY)
AV_PKT_FLAG_CORRUPT = int(C.AV_PKT_FLAG_CORRUPT)
AV_PKT_FLAG_DISCARD = int(C.AV_PKT_FLAG_DISCARD)
)

//Initialize optional fields of a packet with default values.
func (p *Packet) AvInitPacket() {
C.av_init_packet((*C.struct_AVPacket)(p))
p.size = 0
p.data = nil
}

//Allocate the payload of a packet and initialize its fields with default values.
Expand Down
12 changes: 12 additions & 0 deletions avcodec/packet_struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ func (p *Packet) Duration() int {
func (p *Packet) Flags() int {
return int(p.flags)
}
func (p *Packet) SetFlags(flags int) {
p.flags = C.int(flags)
}
func (p *Packet) SideDataElems() int {
return int(p.side_data_elems)
}
Expand All @@ -25,18 +28,27 @@ func (p *Packet) Size() int {
func (p *Packet) StreamIndex() int {
return int(p.stream_index)
}
func (p *Packet) SetStreamIndex(idx int) {
p.stream_index = C.int(idx)
}
func (p *Packet) ConvergenceDuration() int64 {
return int64(p.convergence_duration)
}
func (p *Packet) Dts() int64 {
return int64(p.dts)
}
func (p *Packet) SetDts(dts int64) {
p.dts = C.int64_t(dts)
}
func (p *Packet) Pos() int64 {
return int64(p.pos)
}
func (p *Packet) Pts() int64 {
return int64(p.pts)
}
func (p *Packet) SetPts(pts int64) {
p.dts = C.int64_t(pts)
}
func (p *Packet) Data() *uint8 {
return (*uint8)(p.data)
}
171 changes: 171 additions & 0 deletions avcodec/pixel.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
// Use of this source code is governed by a MIT license that can be found in the LICENSE file.
// Giorgis ([email protected])

//Package avcodec contains the codecs (decoders and encoders) provided by the libavcodec library
//Provides some generic global options, which can be set on all the encoders and decoders.
package avcodec

//#cgo pkg-config: libavformat libavcodec libavutil
//#include <stdio.h>
//#include <stdlib.h>
//#include <inttypes.h>
//#include <stdint.h>
//#include <string.h>
//#include <libavformat/avformat.h>
//#include <libavcodec/avcodec.h>
//#include <libavutil/avutil.h>
//#include <libswscale/swscale.h>
import "C"

const (
AV_PIX_FMT_YUV = 0
AV_PIX_FMT_YUV420P9 = C.AV_PIX_FMT_YUV420P9
AV_PIX_FMT_YUV422P9 = C.AV_PIX_FMT_YUV422P9
AV_PIX_FMT_YUV444P9 = C.AV_PIX_FMT_YUV444P9
AV_PIX_FMT_YUV420P10 = C.AV_PIX_FMT_YUV420P10
AV_PIX_FMT_YUV422P10 = C.AV_PIX_FMT_YUV422P10
AV_PIX_FMT_YUV440P10 = C.AV_PIX_FMT_YUV440P10
AV_PIX_FMT_YUV444P10 = C.AV_PIX_FMT_YUV444P10
AV_PIX_FMT_YUV420P12 = C.AV_PIX_FMT_YUV420P12
AV_PIX_FMT_YUV422P12 = C.AV_PIX_FMT_YUV422P12
AV_PIX_FMT_YUV440P12 = C.AV_PIX_FMT_YUV440P12
AV_PIX_FMT_YUV444P12 = C.AV_PIX_FMT_YUV444P12
AV_PIX_FMT_YUV420P14 = C.AV_PIX_FMT_YUV420P14
AV_PIX_FMT_YUV422P14 = C.AV_PIX_FMT_YUV422P14
AV_PIX_FMT_YUV444P14 = C.AV_PIX_FMT_YUV444P14
AV_PIX_FMT_YUV420P16 = C.AV_PIX_FMT_YUV420P16
AV_PIX_FMT_YUV422P16 = C.AV_PIX_FMT_YUV422P16
AV_PIX_FMT_YUV444P16 = C.AV_PIX_FMT_YUV444P16
AV_PIX_FMT_YUVA420P9 = C.AV_PIX_FMT_YUVA420P9
AV_PIX_FMT_YUVA422P9 = C.AV_PIX_FMT_YUVA422P9
AV_PIX_FMT_YUVA444P9 = C.AV_PIX_FMT_YUVA444P9
AV_PIX_FMT_YUVA420P10 = C.AV_PIX_FMT_YUVA420P10
AV_PIX_FMT_YUVA422P10 = C.AV_PIX_FMT_YUVA422P10
AV_PIX_FMT_YUVA444P10 = C.AV_PIX_FMT_YUVA444P10
AV_PIX_FMT_YUVA420P16 = C.AV_PIX_FMT_YUVA420P16
AV_PIX_FMT_YUVA422P16 = C.AV_PIX_FMT_YUVA422P16
AV_PIX_FMT_YUVA444P16 = C.AV_PIX_FMT_YUVA444P16
AV_PIX_FMT_RGB24 = C.AV_PIX_FMT_RGB24
AV_PIX_FMT_RGBA = C.AV_PIX_FMT_RGBA

SWS_FAST_BILINEAR = C.SWS_FAST_BILINEAR
SWS_BILINEAR = C.SWS_BILINEAR
SWS_BICUBIC = C.SWS_BICUBIC
SWS_X = C.SWS_X
SWS_POINT = C.SWS_POINT
SWS_AREA = C.SWS_AREA
SWS_BICUBLIN = C.SWS_BICUBLIN
SWS_GAUSS = C.SWS_GAUSS
SWS_SINC = C.SWS_SINC
SWS_LANCZOS = C.SWS_LANCZOS
SWS_SPLINE = C.SWS_SPLINE
SWS_SRC_V_CHR_DROP_MASK = C.SWS_SRC_V_CHR_DROP_MASK
SWS_SRC_V_CHR_DROP_SHIFT = C.SWS_SRC_V_CHR_DROP_SHIFT
SWS_PARAM_DEFAULT = C.SWS_PARAM_DEFAULT
SWS_PRINT_INFO = C.SWS_PRINT_INFO
SWS_FULL_CHR_H_INT = C.SWS_FULL_CHR_H_INT
SWS_FULL_CHR_H_INP = C.SWS_FULL_CHR_H_INP
SWS_DIRECT_BGR = C.SWS_DIRECT_BGR
SWS_ACCURATE_RND = C.SWS_ACCURATE_RND
SWS_BITEXACT = C.SWS_BITEXACT
SWS_ERROR_DIFFUSION = C.SWS_ERROR_DIFFUSION
SWS_MAX_REDUCE_CUTOFF = C.SWS_MAX_REDUCE_CUTOFF
SWS_CS_ITU709 = C.SWS_CS_ITU709
SWS_CS_FCC = C.SWS_CS_FCC
SWS_CS_ITU601 = C.SWS_CS_ITU601
SWS_CS_ITU624 = C.SWS_CS_ITU624
SWS_CS_SMPTE170M = C.SWS_CS_SMPTE170M
SWS_CS_SMPTE240M = C.SWS_CS_SMPTE240M
SWS_CS_DEFAULT = C.SWS_CS_DEFAULT
SWS_CS_BT2020 = C.SWS_CS_BT2020
)

func (pf PixelFormat) String() string {
switch int(pf) {
case AV_PIX_FMT_YUV420P9:
return "YUV420P9"

case AV_PIX_FMT_YUV422P9:
return "YUV422P9"

case AV_PIX_FMT_YUV444P9:
return "YUV444P9"

case AV_PIX_FMT_YUV420P10:
return "YUV420P10"

case AV_PIX_FMT_YUV422P10:
return "YUV422P10"

case AV_PIX_FMT_YUV440P10:
return "YUV440P10"

case AV_PIX_FMT_YUV444P10:
return "YUV444P10"

case AV_PIX_FMT_YUV420P12:
return "YUV420P12"

case AV_PIX_FMT_YUV422P12:
return "YUV422P12"

case AV_PIX_FMT_YUV440P12:
return "YUV440P12"

case AV_PIX_FMT_YUV444P12:
return "YUV444P12"

case AV_PIX_FMT_YUV420P14:
return "YUV420P14"

case AV_PIX_FMT_YUV422P14:
return "YUV422P14"

case AV_PIX_FMT_YUV444P14:
return "YUV444P14"

case AV_PIX_FMT_YUV420P16:
return "YUV420P16"

case AV_PIX_FMT_YUV422P16:
return "YUV422P16"

case AV_PIX_FMT_YUV444P16:
return "YUV444P16"

case AV_PIX_FMT_YUVA420P9:
return "YUVA420P9"

case AV_PIX_FMT_YUVA422P9:
return "YUVA422P9"

case AV_PIX_FMT_YUVA444P9:
return "YUVA444P9"

case AV_PIX_FMT_YUVA420P10:
return "YUVA420P10"

case AV_PIX_FMT_YUVA422P10:
return "YUVA422P10"

case AV_PIX_FMT_YUVA444P10:
return "YUVA444P10"

case AV_PIX_FMT_YUVA420P16:
return "YUVA420P16"

case AV_PIX_FMT_YUVA422P16:
return "YUVA422P16"

case AV_PIX_FMT_YUVA444P16:
return "YUVA444P16"

case AV_PIX_FMT_RGB24:
return "RGB24"

case AV_PIX_FMT_RGBA:
return "RGBA"
}

return "{UNKNOWN}"
}
Loading

0 comments on commit 001779d

Please sign in to comment.