Skip to content

Commit

Permalink
Fix compilation errors
Browse files Browse the repository at this point in the history
  • Loading branch information
corebreaker authored and giorgisio committed Apr 7, 2019
1 parent 3a7582f commit 821716f
Show file tree
Hide file tree
Showing 11 changed files with 609 additions and 421 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ func main() {
[FFMPEG INSTALL INSTRUCTIONS] (https://github.com/FFmpeg/FFmpeg/blob/master/INSTALL.md)

``` sh
sudo apt-get -y --force-yes install autoconf automake build-essential libass-dev libfreetype6-dev libsdl1.2-dev libtheora-dev libtool libva-dev libvdpau-dev libvorbis-dev libxcb1-dev libxcb-shm0-dev libxcb-xfixes0-dev pkg-config texi2html zlib1g-dev
sudo apt-get -y install autoconf automake build-essential libass-dev libfreetype6-dev libsdl1.2-dev libtheora-dev libtool libva-dev libvdpau-dev libvorbis-dev libxcb1-dev libxcb-shm0-dev libxcb-xfixes0-dev pkg-config texi2html zlib1g-dev

sudo apt install -y libavdevice-dev libavfilter-dev libswscale-dev libavcodec-dev libavformat-dev libswresample-dev libavutil-dev

sudo apt-get install yasm

Expand Down
1 change: 1 addition & 0 deletions _config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
theme: jekyll-theme-slate
6 changes: 6 additions & 0 deletions avcodec/avcodec.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,9 @@ func AvcodecLicense() string {

//Register all the codecs, parsers and bitstream filters which were enabled at configuration time.
func AvcodecRegisterAll() {
C.av_register_all()
C.avcodec_register_all()
// C.av_log_set_level(0xffff)
}

//Get the Class for Context.
Expand Down Expand Up @@ -242,3 +244,7 @@ func (d *Descriptor) AvcodecDescriptorNext() *Descriptor {
func AvcodecDescriptorGetByName(n string) *Descriptor {
return (*Descriptor)(C.avcodec_descriptor_get_by_name(C.CString(n)))
}

func (f *Frame) Pts() int64 {
return int64(f.pts)
}
799 changes: 391 additions & 408 deletions avcodec/codecs.go

Large diffs are not rendered by default.

39 changes: 39 additions & 0 deletions avcodec/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,24 @@ 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)))
}

// 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) AvCodecSetPktTimebase(r Rational) {
C.av_codec_set_pkt_timebase((*C.struct_AVCodecContext)(ctxt), (C.struct_AVRational)(r))
}
Expand Down Expand Up @@ -161,3 +173,30 @@ func (p *Parser) AvParserNext() *Parser {
func (p *Parser) AvRegisterCodecParser() {
C.av_register_codec_parser((*C.struct_AVCodecParser)(p))
}

func (ctxt *Context) SetTimebase(num1 int, den1 int) {
ctxt.time_base.num = C.int(num1)
ctxt.time_base.den = C.int(den1)
}

func (ctxt *Context) SetEncodeParams2(width int, height int, pxlFmt common.PixelFormat, hasBframes bool, gopSize int) {
ctxt.width = C.int(width)
ctxt.height = C.int(height)
// ctxt.bit_rate = 1000000
ctxt.gop_size = C.int(gopSize)
// ctxt.max_b_frames = 2
if hasBframes {
ctxt.has_b_frames = 1
} else {
ctxt.has_b_frames = 0
}
// ctxt.extradata = nil
// ctxt.extradata_size = 0
// ctxt.channels = 0
ctxt.pix_fmt = int32(pxlFmt)
// C.av_opt_set(ctxt.priv_data, "preset", "ultrafast", 0)
}

func (ctxt *Context) SetEncodeParams(width int, height int, pxlFmt common.PixelFormat) {
ctxt.SetEncodeParams2(width, height, pxlFmt, false /*no b frames*/, 10)
}
4 changes: 4 additions & 0 deletions avcodec/packet_struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@

package avcodec

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

func (p *Packet) Buf() *AvBufferRef {
return (*AvBufferRef)(p.buf)
}
Expand Down
30 changes: 29 additions & 1 deletion avformat/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,18 @@ package avformat
//#include <libavformat/avformat.h>
import "C"
import (
"github.com/giorgisio/goav/avcodec"
"time"
"unsafe"

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

const (
AvseekFlagBackward = 1 ///< seek backward
AvseekFlagByte = 2 ///< seeking based on position in bytes
AvseekFlagAny = 4 ///< seek to any frame, even non-keyframes
AvseekFlagFrame = 8 ///< seeking based on frame number
)

func (s *Context) AvFormatGetProbeScore() int {
Expand Down Expand Up @@ -104,6 +114,14 @@ func (s *Context) AvSeekFrame(st int, t int64, f int) int {
return int(C.av_seek_frame((*C.struct_AVFormatContext)(s), C.int(st), C.int64_t(t), C.int(f)))
}

// AvSeekFrameTime seeks to a specified time location.
// |timebase| is codec specific and can be obtained by calling AvCodecGetPktTimebase2
func (s *Context) AvSeekFrameTime(st int, at time.Duration, timebase common.AVRational) int {
t2 := C.double(C.double(at.Seconds())*C.double(timebase.Den)) / (C.double(timebase.Num))
// log.Printf("Seeking to time :%v TimebaseTime:%v ActualTimebase:%v", at, t2, timebase)
return int(C.av_seek_frame((*C.struct_AVFormatContext)(s), C.int(st), C.int64_t(t2), AvseekFlagBackward))
}

//Seek to timestamp ts.
func (s *Context) AvformatSeekFile(si int, mit, ts, mat int64, f int) int {
return int(C.avformat_seek_file((*C.struct_AVFormatContext)(s), C.int(si), C.int64_t(mit), C.int64_t(ts), C.int64_t(mat), C.int(f)))
Expand Down Expand Up @@ -192,6 +210,16 @@ func (s *Context) AvformatQueueAttachedPictures() int {
return int(C.avformat_queue_attached_pictures((*C.struct_AVFormatContext)(s)))
}

func (s *Context) AvformatNewStream2(c *AvCodec) *Stream {
stream := (*Stream)(C.avformat_new_stream((*C.struct_AVFormatContext)(s), (*C.struct_AVCodec)(c)))
stream.codec.pix_fmt = int32(common.AV_PIX_FMT_YUV)
stream.codec.width = 640
stream.codec.height = 480
stream.time_base.num = 1
stream.time_base.num = 25
return stream
}

// //av_format_control_message av_format_get_control_message_cb (const Context *s)
// func (s *Context) AvFormatControlMessage() C.av_format_get_control_message_cb {
// return C.av_format_get_control_message_cb((*C.struct_AVFormatContext)(s))
Expand Down
14 changes: 11 additions & 3 deletions avformat/context_struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (ctxt *Context) Programs() **AvProgram {
}

func (ctxt *Context) Streams() *Stream {
return (*Stream)(unsafe.Pointer(ctxt.streams))
return (*Stream)(unsafe.Pointer(*ctxt.streams))
}

func (ctxt *Context) Filename() string {
Expand Down Expand Up @@ -179,7 +179,7 @@ func (ctxt *Context) Duration() int64 {
}

func (ctxt *Context) MaxAnalyzeDuration2() int64 {
return int64(ctxt.max_analyze_duration2)
return int64(ctxt.max_analyze_duration)
}

func (ctxt *Context) MaxInterleaveDelta() int64 {
Expand All @@ -191,7 +191,7 @@ func (ctxt *Context) OutputTsOffset() int64 {
}

func (ctxt *Context) Probesize2() int64 {
return int64(ctxt.probesize2)
return int64(ctxt.probesize)
}

func (ctxt *Context) SkipInitialBytes() int64 {
Expand Down Expand Up @@ -249,3 +249,11 @@ func (ctxt *Context) PacketSize() uint {
func (ctxt *Context) Probesize() uint {
return uint(ctxt.probesize)
}

func (ctxt *Context) SetPb(pb *AvIOContext) {
ctxt.pb = (*C.struct_AVIOContext)(unsafe.Pointer(pb))
}

func (ctxt *Context) Pb2() **AvIOContext {
return (**AvIOContext)(unsafe.Pointer(&ctxt.pb))
}
105 changes: 97 additions & 8 deletions avutil/frame.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ package avutil
*/
import "C"
import (
"fmt"
"image"
"log"
"unsafe"
)

Expand All @@ -22,9 +25,9 @@ type (
AvFrameSideDataType C.enum_AVFrameSideDataType
)

func AvprivFrameGetMetadatap(f *Frame) **Dictionary {
return (**Dictionary)(unsafe.Pointer(C.avpriv_frame_get_metadatap((*C.struct_AVFrame)(unsafe.Pointer(f)))))
}
// func AvprivFrameGetMetadatap(f *Frame) **Dictionary {
// return (**Dictionary)(unsafe.Pointer(C.avpriv_frame_get_metadatap((*C.struct_AVFrame)(unsafe.Pointer(f)))))
// }

func AvFrameSetQpTable(f *Frame, b *AvBufferRef, s, q int) int {
return int(C.av_frame_set_qp_table((*C.struct_AVFrame)(unsafe.Pointer(f)), (*C.struct_AVBufferRef)(unsafe.Pointer(b)), C.int(s), C.int(q)))
Expand Down Expand Up @@ -99,11 +102,97 @@ func AvFrameGetSideData(f *Frame, t AvFrameSideDataType) *AvFrameSideData {
return (*AvFrameSideData)(C.av_frame_get_side_data((*C.struct_AVFrame)(unsafe.Pointer(f)), (C.enum_AVFrameSideDataType)(t)))
}

func Data(f *Frame) *uint8 {
return (*uint8)(unsafe.Pointer((*C.uint8_t)(unsafe.Pointer(&f.data))))
}
func Linesize(f *Frame) int {
return int(*(*C.int)(unsafe.Pointer(&f.linesize)))
func Data(f *Frame) (data [8]*uint8) {
for i := range data {
data[i] = (*uint8)(f.data[i])
}
return
}

func Linesize(f *Frame) (linesize [8]int32) {
for i := range linesize {
linesize[i] = int32(f.linesize[i])
}
return
}

//GetPicture creates a YCbCr image from the frame
func GetPicture(f *Frame) (img *image.YCbCr, err error) {
// For 4:4:4, CStride == YStride/1 && len(Cb) == len(Cr) == len(Y)/1.
// For 4:2:2, CStride == YStride/2 && len(Cb) == len(Cr) == len(Y)/2.
// For 4:2:0, CStride == YStride/2 && len(Cb) == len(Cr) == len(Y)/4.
// For 4:4:0, CStride == YStride/1 && len(Cb) == len(Cr) == len(Y)/2.
// For 4:1:1, CStride == YStride/4 && len(Cb) == len(Cr) == len(Y)/4.
// For 4:1:0, CStride == YStride/4 && len(Cb) == len(Cr) == len(Y)/8.

w := int(f.linesize[0])
h := int(f.height)
r := image.Rectangle{image.Point{0, 0}, image.Point{w, h}}
// TODO: Use the sub sample ratio from the input image 'f.format'
img = image.NewYCbCr(r, image.YCbCrSubsampleRatio420)
// convert the frame data data to a Go byte array
img.Y = C.GoBytes(unsafe.Pointer(f.data[0]), C.int(w*h))

wCb := int(f.linesize[1])
if unsafe.Pointer(f.data[1]) != nil {
img.Cb = C.GoBytes(unsafe.Pointer(f.data[1]), C.int(wCb*h/2))
}

wCr := int(f.linesize[2])
if unsafe.Pointer(f.data[2]) != nil {
img.Cr = C.GoBytes(unsafe.Pointer(f.data[2]), C.int(wCr*h/2))
}
return
}

// SetPicture sets the image pointer of |f| to the image pointers of |img|
func SetPicture(f *Frame, img *image.YCbCr) {
d := Data(f)
// l := Linesize(f)
// FIXME: Save the original pointers somewhere, this is a memory leak
d[0] = (*uint8)(unsafe.Pointer(&img.Y[0]))
// d[1] = (*uint8)(unsafe.Pointer(&img.Cb[0]))
}

func GetPictureRGB(f *Frame) (img *image.RGBA, err error) {
w := int(f.linesize[0])
h := int(f.height)
r := image.Rectangle{image.Point{0, 0}, image.Point{w, h}}
// TODO: Use the sub sample ratio from the input image 'f.format'
img = image.NewRGBA(r)
// convert the frame data data to a Go byte array
img.Pix = C.GoBytes(unsafe.Pointer(f.data[0]), C.int(w*h))
img.Stride = w
log.Println("w", w, "h", h)
return
}

func AvSetFrame(f *Frame, w int, h int, pixFmt int) (err error) {
f.width = C.int(w)
f.height = C.int(h)
f.format = C.int(pixFmt)
if ret := C.av_frame_get_buffer((*C.struct_AVFrame)(unsafe.Pointer(f)), 32 /*alignment*/); ret < 0 {
err = fmt.Errorf("Error allocating avframe buffer. Err: %v", ret)
return
}
return
}

func AvFrameGetInfo(f *Frame) (width int, height int, linesize [8]int32, data [8]*uint8) {
width = int(f.linesize[0])
height = int(f.height)
for i := range linesize {
linesize[i] = int32(f.linesize[i])
}
for i := range data {
data[i] = (*uint8)(f.data[i])
}
// log.Println("Linesize is ", f.linesize, "Data is", data)
return
}

func GetBestEffortTimestamp(f *Frame) int64 {
return int64(f.best_effort_timestamp)
}

// //static int get_video_buffer (Frame *frame, int align)
Expand Down
19 changes: 19 additions & 0 deletions common/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Use of this source code is governed by a MIT license that can be found in the LICENSE file.

package common

// Common types used across all av packages.

// AVRational exposes the numerator and denominator part of the undrelying 'Rational' structure.
type AVRational struct {
Num int
Den int
}

type PixelFormat int

const (
AV_PIX_FMT_YUV PixelFormat = 0
AV_PIX_FMT_RGB24 = 3
AV_PIX_FMT_RGBA = 28
)
9 changes: 9 additions & 0 deletions swscale/swscale.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,15 @@ func SwsScale(ctxt *Context, src *uint8, str int, y, h int, d *uint8, ds int) in
return int(C.sws_scale(cctxt, &csrc, cstr, C.int(y), C.int(h), &cd, cds))
}

func SwsScale2(ctxt *Context, srcData [8]*uint8, srcStride [8]int32, y, h int, dstData [8]*uint8, dstStride [8]int32) int {
cctxt := (*C.struct_SwsContext)(unsafe.Pointer(ctxt))
csrc := (**C.uint8_t)(unsafe.Pointer(&srcData[0]))
cstr := (*C.int)(unsafe.Pointer(&srcStride[0]))
cd := (**C.uint8_t)(unsafe.Pointer(&dstData[0]))
cds := (*C.int)(unsafe.Pointer(&dstStride))
return int(C.sws_scale(cctxt, csrc, cstr, C.int(y), C.int(h), cd, cds))
}

func SwsSetcolorspacedetails(ctxt *Context, it *int, sr int, t *int, dr, b, c, s int) int {
cit := (*C.int)(unsafe.Pointer(it))
ct := (*C.int)(unsafe.Pointer(t))
Expand Down

0 comments on commit 821716f

Please sign in to comment.