Merge pull request #9 from prohulaelk/cgo_compile_errors

fix for issues 2, 3, and 8
This commit is contained in:
nareix 2016-04-19 11:56:31 +08:00
commit 7e8474e9f2
5 changed files with 151 additions and 162 deletions

View File

@ -1,43 +1,42 @@
package codec package codec
import ( import (
/* /*
#include <libavcodec/avcodec.h> #include <libavcodec/avcodec.h>
#include <libavutil/avutil.h> #include <libavutil/avutil.h>
#include <string.h> #include <string.h>
#include <stdio.h> #include <stdio.h>
typedef struct { typedef struct {
AVCodec *c; AVCodec *c;
AVCodecContext *ctx; AVCodecContext *ctx;
AVFrame *f; AVFrame *f;
int got; int got;
} aacdec_t ; } aacdec_t ;
static int aacdec_new(aacdec_t *m, uint8_t *buf, int len) { static int aacdec_new(aacdec_t *m, uint8_t *buf, int len) {
m->c = avcodec_find_decoder(CODEC_ID_AAC); m->c = avcodec_find_decoder(CODEC_ID_AAC);
m->ctx = avcodec_alloc_context3(m->c); m->ctx = avcodec_alloc_context3(m->c);
m->f = avcodec_alloc_frame(); m->f = av_frame_alloc();
m->ctx->extradata = buf; m->ctx->extradata = buf;
m->ctx->extradata_size = len; m->ctx->extradata_size = len;
m->ctx->debug = 0x3; m->ctx->debug = 0x3;
av_log(m->ctx, AV_LOG_DEBUG, "m %p\n", m); av_log(m->ctx, AV_LOG_DEBUG, "m %p\n", m);
return avcodec_open2(m->ctx, m->c, 0); return avcodec_open2(m->ctx, m->c, 0);
} }
static int aacdec_decode(aacdec_t *m, uint8_t *data, int len) { static int aacdec_decode(aacdec_t *m, uint8_t *data, int len) {
AVPacket pkt; AVPacket pkt;
av_init_packet(&pkt); av_init_packet(&pkt);
pkt.data = data; pkt.data = data;
pkt.size = len; pkt.size = len;
av_log(m->ctx, AV_LOG_DEBUG, "decode %p\n", m); av_log(m->ctx, AV_LOG_DEBUG, "decode %p\n", m);
return avcodec_decode_audio4(m->ctx, m->f, &m->got, &pkt); return avcodec_decode_audio4(m->ctx, m->f, &m->got, &pkt);
} }
*/ */
"C" "C"
"unsafe"
"errors" "errors"
"unsafe"
) )
type AACDecoder struct { type AACDecoder struct {
@ -70,7 +69,7 @@ func (m *AACDecoder) Decode(data []byte) (sample []byte, err error) {
err = errors.New("no data") err = errors.New("no data")
return return
} }
size := int(m.m.f.linesize[0])*2 size := int(m.m.f.linesize[0]) * 2
sample = make([]byte, size*2) sample = make([]byte, size*2)
for i := 0; i < 2; i++ { for i := 0; i < 2; i++ {
C.memcpy( C.memcpy(
@ -81,4 +80,3 @@ func (m *AACDecoder) Decode(data []byte) (sample []byte, err error) {
} }
return return
} }

View File

@ -1,56 +1,55 @@
package codec package codec
import ( import (
/* /*
#include <libavcodec/avcodec.h> #include <libavcodec/avcodec.h>
#include <libavutil/avutil.h> #include <libavutil/avutil.h>
#include <string.h> #include <string.h>
typedef struct { typedef struct {
AVCodec *c; AVCodec *c;
AVCodecContext *ctx; AVCodecContext *ctx;
AVFrame *f; AVFrame *f;
int got; int got;
uint8_t buf[1024*10]; int size; uint8_t buf[1024*10]; int size;
int samplerate; int bitrate; int samplerate; int bitrate;
int channels; int channels;
} aacenc_t ; } aacenc_t ;
static int aacenc_new(aacenc_t *m) { static int aacenc_new(aacenc_t *m) {
m->c = avcodec_find_encoder(CODEC_ID_AAC); m->c = avcodec_find_encoder(CODEC_ID_AAC);
m->ctx = avcodec_alloc_context3(m->c); m->ctx = avcodec_alloc_context3(m->c);
m->ctx->sample_fmt = AV_SAMPLE_FMT_FLTP; m->ctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
m->ctx->sample_rate = m->samplerate; m->ctx->sample_rate = m->samplerate;
m->ctx->bit_rate = m->bitrate; m->ctx->bit_rate = m->bitrate;
m->ctx->channels = m->channels; m->ctx->channels = m->channels;
m->ctx->strict_std_compliance = FF_COMPLIANCE_EXPERIMENTAL; m->ctx->strict_std_compliance = FF_COMPLIANCE_EXPERIMENTAL;
m->f = avcodec_alloc_frame(); m->f = av_frame_alloc();
int r = avcodec_open2(m->ctx, m->c, 0); int r = avcodec_open2(m->ctx, m->c, 0);
av_log(m->ctx, AV_LOG_DEBUG, "extra %d\n", m->ctx->extradata_size); av_log(m->ctx, AV_LOG_DEBUG, "extra %d\n", m->ctx->extradata_size);
return r; return r;
} }
static void aacenc_encode(aacenc_t *m) { static void aacenc_encode(aacenc_t *m) {
AVPacket pkt; AVPacket pkt;
av_init_packet(&pkt); av_init_packet(&pkt);
pkt.data = m->buf; pkt.data = m->buf;
pkt.size = sizeof(m->buf); pkt.size = sizeof(m->buf);
m->f->nb_samples = 1024; m->f->nb_samples = 1024;
m->f->extended_data = m->f->data; m->f->extended_data = m->f->data;
m->f->linesize[0] = 4096; m->f->linesize[0] = 4096;
avcodec_encode_audio2(m->ctx, &pkt, m->f, &m->got); avcodec_encode_audio2(m->ctx, &pkt, m->f, &m->got);
av_log(m->ctx, AV_LOG_DEBUG, "got %d size %d\n", m->got, pkt.size); av_log(m->ctx, AV_LOG_DEBUG, "got %d size %d\n", m->got, pkt.size);
m->size = pkt.size; m->size = pkt.size;
} }
*/ */
"C" "C"
"unsafe"
"errors" "errors"
"unsafe"
) )
type AACEncoder struct { type AACEncoder struct {
m C.aacenc_t m C.aacenc_t
Header []byte Header []byte
} }
@ -90,4 +89,3 @@ func (m *AACEncoder) Encode(sample []byte) (ret []byte, err error) {
) )
return return
} }

View File

@ -1,41 +1,40 @@
package codec package codec
import ( import (
/* /*
#include <libavcodec/avcodec.h> #include <libavcodec/avcodec.h>
#include <libavformat/avformat.h> #include <libavformat/avformat.h>
#include <libavutil/avutil.h> #include <libavutil/avutil.h>
typedef struct { typedef struct {
AVCodec *c; AVCodec *c;
AVCodecContext *ctx; AVCodecContext *ctx;
AVFrame *f; AVFrame *f;
int got; int got;
} h264dec_t ; } h264dec_t ;
static int h264dec_new(h264dec_t *h, uint8_t *data, int len) { static int h264dec_new(h264dec_t *h, uint8_t *data, int len) {
h->c = avcodec_find_decoder(CODEC_ID_H264); h->c = avcodec_find_decoder(CODEC_ID_H264);
h->ctx = avcodec_alloc_context3(h->c); h->ctx = avcodec_alloc_context3(h->c);
h->f = avcodec_alloc_frame(); h->f = av_frame_alloc();
h->ctx->extradata = data; h->ctx->extradata = data;
h->ctx->extradata_size = len; h->ctx->extradata_size = len;
h->ctx->debug = 0x3; h->ctx->debug = 0x3;
return avcodec_open2(h->ctx, h->c, 0); return avcodec_open2(h->ctx, h->c, 0);
} }
static int h264dec_decode(h264dec_t *h, uint8_t *data, int len) { static int h264dec_decode(h264dec_t *h, uint8_t *data, int len) {
AVPacket pkt; AVPacket pkt;
av_init_packet(&pkt); av_init_packet(&pkt);
pkt.data = data; pkt.data = data;
pkt.size = len; pkt.size = len;
return avcodec_decode_video2(h->ctx, h->f, &h->got, &pkt); return avcodec_decode_video2(h->ctx, h->f, &h->got, &pkt);
} }
*/ */
"C" "C"
"unsafe"
"errors" "errors"
"image" "image"
"unsafe"
) )
type H264Decoder struct { type H264Decoder struct {
@ -76,15 +75,14 @@ func (m *H264Decoder) Decode(nal []byte) (f *image.YCbCr, err error) {
cs := int(m.m.f.linesize[1]) cs := int(m.m.f.linesize[1])
f = &image.YCbCr{ f = &image.YCbCr{
Y: fromCPtr(unsafe.Pointer(m.m.f.data[0]), ys*h), Y: fromCPtr(unsafe.Pointer(m.m.f.data[0]), ys*h),
Cb: fromCPtr(unsafe.Pointer(m.m.f.data[1]), cs*h/2), Cb: fromCPtr(unsafe.Pointer(m.m.f.data[1]), cs*h/2),
Cr: fromCPtr(unsafe.Pointer(m.m.f.data[2]), cs*h/2), Cr: fromCPtr(unsafe.Pointer(m.m.f.data[2]), cs*h/2),
YStride: ys, YStride: ys,
CStride: cs, CStride: cs,
SubsampleRatio: image.YCbCrSubsampleRatio420, SubsampleRatio: image.YCbCrSubsampleRatio420,
Rect: image.Rect(0, 0, w, h), Rect: image.Rect(0, 0, w, h),
} }
return return
} }

View File

@ -1,56 +1,55 @@
package codec package codec
import ( import (
/* /*
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdint.h> #include <stdint.h>
#include <string.h> #include <string.h>
#include <libavcodec/avcodec.h> #include <libavcodec/avcodec.h>
#include <libavformat/avformat.h> #include <libavformat/avformat.h>
#include <libavutil/avutil.h> #include <libavutil/avutil.h>
typedef struct { typedef struct {
int w, h; int w, h;
int pixfmt; int pixfmt;
char *preset[2]; char *preset[2];
char *profile; char *profile;
int bitrate; int bitrate;
int got; int got;
AVCodec *c; AVCodec *c;
AVCodecContext *ctx; AVCodecContext *ctx;
AVFrame *f; AVFrame *f;
AVPacket pkt; AVPacket pkt;
} h264enc_t; } h264enc_t;
static int h264enc_new(h264enc_t *m) { static int h264enc_new(h264enc_t *m) {
m->c = avcodec_find_encoder(CODEC_ID_H264); m->c = avcodec_find_encoder(CODEC_ID_H264);
m->ctx = avcodec_alloc_context3(m->c); m->ctx = avcodec_alloc_context3(m->c);
m->ctx->width = m->w; m->ctx->width = m->w;
m->ctx->height = m->h; m->ctx->height = m->h;
m->ctx->bit_rate = m->bitrate; m->ctx->bit_rate = m->bitrate;
m->ctx->pix_fmt = m->pixfmt; m->ctx->pix_fmt = m->pixfmt;
m->ctx->flags |= CODEC_FLAG_GLOBAL_HEADER; m->ctx->flags |= CODEC_FLAG_GLOBAL_HEADER;
m->f = avcodec_alloc_frame(); m->f = av_frame_alloc();
return avcodec_open2(m->ctx, m->c, NULL); return avcodec_open2(m->ctx, m->c, NULL);
} }
*/ */
"C" "C"
"unsafe"
"image"
"errors" "errors"
"image"
"strings" "strings"
"unsafe"
//"log" //"log"
) )
type H264Encoder struct { type H264Encoder struct {
m C.h264enc_t m C.h264enc_t
Header []byte Header []byte
Pixfmt image.YCbCrSubsampleRatio Pixfmt image.YCbCrSubsampleRatio
W, H int W, H int
} }
func NewH264Encoder( func NewH264Encoder(
@ -94,7 +93,7 @@ func NewH264Encoder(
type h264Out struct { type h264Out struct {
Data []byte Data []byte
Key bool Key bool
} }
func (m *H264Encoder) Encode(img *image.YCbCr) (out h264Out, err error) { func (m *H264Encoder) Encode(img *image.YCbCr) (out h264Out, err error) {
@ -111,12 +110,12 @@ func (m *H264Encoder) Encode(img *image.YCbCr) (out h264Out, err error) {
return return
} }
f = m.m.f f = m.m.f
f.data[0] = (*C.uint8_t)(unsafe.Pointer(&img.Y[0])); f.data[0] = (*C.uint8_t)(unsafe.Pointer(&img.Y[0]))
f.data[1] = (*C.uint8_t)(unsafe.Pointer(&img.Cb[0])); f.data[1] = (*C.uint8_t)(unsafe.Pointer(&img.Cb[0]))
f.data[2] = (*C.uint8_t)(unsafe.Pointer(&img.Cr[0])); f.data[2] = (*C.uint8_t)(unsafe.Pointer(&img.Cr[0]))
f.linesize[0] = (C.int)(img.YStride); f.linesize[0] = (C.int)(img.YStride)
f.linesize[1] = (C.int)(img.CStride); f.linesize[1] = (C.int)(img.CStride)
f.linesize[2] = (C.int)(img.CStride); f.linesize[2] = (C.int)(img.CStride)
} }
C.av_init_packet(&m.m.pkt) C.av_init_packet(&m.m.pkt)
@ -130,7 +129,7 @@ func (m *H264Encoder) Encode(img *image.YCbCr) (out h264Out, err error) {
err = errors.New("no picture") err = errors.New("no picture")
return return
} }
if (m.m.pkt.size == 0) { if m.m.pkt.size == 0 {
err = errors.New("packet size == 0") err = errors.New("packet size == 0")
return return
} }
@ -145,4 +144,3 @@ func (m *H264Encoder) Encode(img *image.YCbCr) (out h264Out, err error) {
return return
} }

19
util.go
View File

@ -1,4 +1,3 @@
/* /*
Golang h264,aac decoder/encoder libav wrapper Golang h264,aac decoder/encoder libav wrapper
@ -19,19 +18,19 @@ Golang h264,aac decoder/encoder libav wrapper
package codec package codec
import ( import (
"unsafe"
"reflect" "reflect"
"unsafe"
/* /*
#cgo darwin LDFLAGS: -lavformat -lavutil -lavcodec #cgo LDFLAGS: -lavformat -lavutil -lavcodec
#include <libavutil/avutil.h> #include <libavutil/avutil.h>
#include <libavformat/avformat.h> #include <libavformat/avformat.h>
static void libav_init() { static void libav_init() {
av_register_all(); av_register_all();
av_log_set_level(AV_LOG_DEBUG); av_log_set_level(AV_LOG_DEBUG);
} }
*/ */
"C" "C"
) )
@ -47,5 +46,3 @@ func fromCPtr(buf unsafe.Pointer, size int) (ret []uint8) {
hdr.Data = uintptr(buf) hdr.Data = uintptr(buf)
return return
} }