func (m *Muxer) writeAudioFrame(frame *C.AVFrame) bool { for C.av_audio_fifo_size(m.fifo) < 1024 { // generate & store in fifo C.fill_audio_frame(frame, m.audioStream.stream.codec) frame_size := frame.nb_samples if C.av_audio_fifo_realloc(m.fifo, C.av_audio_fifo_size(m.fifo)+frame_size) < 0 { return false } if C.av_audio_fifo_write(m.fifo, (*unsafe.Pointer)(unsafe.Pointer(&frame.data[0])), frame_size) < frame_size { return false } } got_packet := C.int(0) for C.av_audio_fifo_size(m.fifo) >= 1024 { // read & encode & write frame_size := C.min(C.av_audio_fifo_size(m.fifo), m.audioStream.stream.codec.frame_size) output_frame := C.alloc_audio_frame(m.audioStream.stream.codec) if C.av_audio_fifo_read(m.fifo, (*unsafe.Pointer)(unsafe.Pointer(&output_frame.data[0])), frame_size) < frame_size { C.av_frame_free(&output_frame) return false } pkt := C.AVPacket{} C.av_init_packet(&pkt) output_frame.pts = C.int64_t(m.audioStream.ts) m.audioStream.ts += int(m.audioStream.stream.codec.frame_size) if C.avcodec_encode_audio2(m.audioStream.stream.codec, &pkt, frame, &got_packet) < 0 { C.av_free_packet(&pkt) return false } if got_packet == 0 { continue } C.av_packet_rescale_ts(&pkt, m.audioStream.stream.codec.time_base, m.audioStream.stream.time_base) pkt.stream_index = m.audioStream.stream.index if C.av_interleaved_write_frame(m.context, &pkt) < 0 { return false } } return true }
func (m *Muxer) writeVideoFrame(frame *C.AVFrame) bool { if m.capture.Read(frame) != nil { return false } if m.display != nil { m.display.Render(frame) } pkt := C.AVPacket{} C.av_init_packet(&pkt) frame.pts = C.int64_t(m.videoStream.ts) m.videoStream.ts++ got_packet := C.int(0) if C.avcodec_encode_video2(m.videoStream.stream.codec, &pkt, frame, &got_packet) < 0 { C.av_free_packet(&pkt) return false } if got_packet == 0 { return false } C.av_packet_rescale_ts(&pkt, m.videoStream.stream.codec.time_base, m.videoStream.stream.time_base) pkt.stream_index = m.videoStream.stream.index return C.av_interleaved_write_frame(m.context, &pkt) == 0 }
func (c *Decoder) decodeAudio(p Packet) *Frame { packet := new(C.AVPacket) C.av_init_packet(packet) defer C.av_free_packet(packet) packet.pts = C.int64_t(p.Pts) packet.dts = C.int64_t(p.Dts) packet.size = C.int(p.Size) packet.data = (*C.uint8_t)(unsafe.Pointer(&p.Data[0])) packet.stream_index = C.int(p.Stream) packet.flags = C.int(p.Flags) packet.duration = C.int(p.Duration) packet.pos = C.int64_t(p.Pos) //size:=packet.size; samples_size := C.int(C.AVCODEC_MAX_AUDIO_FRAME_SIZE) //bps := C.av_get_bits_per_sample_fmt(c.Ctx.sample_fmt) >> 3; outbuf := (*C.uint8_t)(C.av_malloc(C.uint(samples_size))) defer C.av_free(unsafe.Pointer(outbuf)) C.avcodec_decode_audio3(c.Ctx, (*C.int16_t)(unsafe.Pointer(outbuf)), &samples_size, packet) //println(data_len) return nil }
func (c *Decoder) decodeVideo(p Packet) *Frame { if !c.Valid { //println("try to decode with an invalid Decoder") return nil } //println("try to decode with a valid Decoder") packet := new(C.AVPacket) C.av_init_packet(packet) defer C.av_free_packet(packet) packet.pts = C.int64_t(p.Pts) packet.dts = C.int64_t(p.Dts) packet.size = C.int(p.Size) //packet.data=&C.uint8_t(make([]byte, p.Size)[0]) //myBytes:=make([]byte,p.Size); /* var pb *byte if(len(p.Data)>0){ //println("Data > 0") pb=&p.Data[0] }*/ packet.data = (*C.uint8_t)(unsafe.Pointer(&p.Data[0])) // println(p.Data) // println(pb) // println(packet.data) // packet.data=unsafe.Pointer(p.Data) // for i:= 0; i < re.Size; i++ { // re.Data[i] = data[i]; // } packet.stream_index = C.int(p.Stream) packet.flags = C.int(p.Flags) packet.duration = C.int(p.Duration) packet.pos = C.int64_t(p.Pos) height := c.Ctx.height width := c.Ctx.width var frame *Frame = new(Frame) numBytes := C.avpicture_get_size(0, width, height) //_size = numBytes; //_buffer := (*C.uint8_t) (C.av_malloc(C.uint(numBytes))); if numBytes > 0 { var buffer []byte = make([]byte, numBytes) var pbuffer *byte = &buffer[0] // C.memset((*C.uint8_t)(_buffer), 0, C.size_t(numBytes)); // Assign appropriate parts of buffer to image planes //var picture * C.AVPicture=(*C.AVPicture)(unsafe.Pointer(frame)) //println(frame) //println(picture) //:=&frame //854x480 C.avpicture_fill((*C.AVPicture)(unsafe.Pointer(frame)), (*C.uint8_t)(unsafe.Pointer(pbuffer)), 0, width, height) } //frameFinished:=C.int(0) var frameFinished *C.int = new(C.int) //bytes_decoded:= C.avcodec_decode_video2(c.Ctx, (*C.AVFrame)(unsafe.Pointer(frame)), frameFinished, packet) frame.pts = packet.pts //frame.dts=packet.dts /* //C.avcodec_decode_video2(nil,nil,nil,nil) header:=fmt.Sprintf("P5\n%d %d\n255\n",width,height) file, err := os.Open("test.ppm", os.O_CREATE|os.O_TRUNC|os.O_RDWR, 0666) if(err!=nil){ println("error while openning file") } file.WriteString(header) linesize:=int(frame.linesize[0]) //println(linesize) data:=make([]byte, width) tmpdata:=(*(*[1<<30]byte)(unsafe.Pointer(frame.data[0])))[0:numBytes] // for i:= 0; i < 1; i++ { // data[i] = tmpdata[i]; // } for i := 0; i < int(height); i++{ for a:= 0; a < int(width); a++ { data[a] = tmpdata[(i*linesize)+a]; } file.Write(data); } file.Close()*/ //ioutil.WriteFile("test.data", header,0755) //println("bla fasel") //println(bytes_decoded) //println(*frameFinished) return frame }