-
Hello! My stream source is an HDMI RX from a development board (RK3588). This interface is mapped as a var (
HdmiRxPath = "/dev/video0"
)
func main() {
astiav.RegisterAllDevices()
inputFormat := astiav.FindInputFormat("video4linux2")
formatContext := astiav.AllocFormatContext()
defer formatContext.Free()
hdmiDict := astiav.NewDictionary()
_ = hdmiDict.Set("pixel_format", "nv24", 0)
_ = hdmiDict.Set("video_size", "1920x1080", 0)
defer hdmiDict.Free()
err := formatContext.OpenInput(HdmiRxPath, inputFormat, hdmiDict)
if err != nil {
panic(err)
}
defer formatContext.CloseInput()
err = formatContext.FindStreamInfo(nil)
if err != nil {
panic(err)
}
packet := astiav.AllocPacket()
for i := 0; i < 10; i++ {
err := formatContext.ReadFrame(packet)
if err != nil {
panic(err)
}
fmt.Println(packet.Size())
packet.Unref()
}
} Now, I need to encode each frame into mjpeg, h264, etc. The problem I am facing is that the Packet obtained from the code above is already stored as raw data in NV24 form, and I can't figure out how to convert it into a Frame to feed into an encoder. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Ah... converting through a package main
import (
"errors"
"fmt"
"github.com/asticode/go-astiav"
)
type Rawvideo struct {
decoder *astiav.Codec
context *astiav.CodecContext
}
func NewRawvideo() (*Rawvideo, error) {
r := &Rawvideo{}
r.decoder = astiav.FindDecoder(astiav.CodecIDRawvideo)
if r.decoder == nil {
return nil, errors.New("could not find Rawvideo decoder")
}
r.context = astiav.AllocCodecContext(r.decoder)
if r.context == nil {
return nil, errors.New("unable to allocate context")
}
r.context.SetPixelFormat(astiav.PixelFormatNV12) // Anything
r.context.SetWidth(1920)
r.context.SetHeight(1080)
r.context.SetFlags(astiav.NewCodecContextFlags(astiav.CodecContextFlagLowDelay))
err := r.context.Open(r.decoder, nil)
if err != nil {
return nil, err
}
return r, nil
}
func (r *Rawvideo) Decode(packet *astiav.Packet, frame *astiav.Frame) error {
err := r.context.SendPacket(packet)
if err != nil {
return fmt.Errorf("unable to send packet: %w", err)
}
err = r.context.ReceiveFrame(frame)
if err != nil {
return fmt.Errorf("unable to receive frame: %w", err)
}
return nil
}
func (r *Rawvideo) Close() {
r.context.Free()
} |
Beta Was this translation helpful? Give feedback.
Ah... converting through a
rawvideo
decoder: