gmf icon indicating copy to clipboard operation
gmf copied to clipboard

frame to image.Image interface conversion

Open davidklassen opened this issue 8 years ago • 5 comments

Hello, I am using gmf to decode video stream and read qr-codes from this stream using this library github.com/kdar/goquirc. It expects the image.Image interface as an input for Decode() function, so right now I am doing something like this:

for frame := range packet.Frames(ist.CodecCtx()) {
    swsCtx.Scale(frame, dstFrame)

    if p, ready, _ := dstFrame.EncodeNewPacket(cc); ready {
        // Decode image
        m, _, err := image.Decode(bytes.NewReader(p.Data()))
        if err != nil {
            log.Fatal(err)
        }

        // Decode QR Code
        text, err := decoder.Decode(m)
        if err != nil {
            log.Fatal(err)
        }

        chunk := text[0]

        fmt.Printf("%s,%d,%s\n", strings.ToLower(srcFileName)[:len(srcFileName)-5], frame.TimeStamp(), B2S(chunk.Payload[:chunk.PayloadLen]))

        defer gmf.Release(p)
    }
}

This is from the example here (https://github.com/3d0c/gmf/blob/master/examples/video-to-jpeg.go) It works but does not look good to me, since I am encoding and decoding a frame just to get image.Image interface. Is there a way to get access to frame data so I could implement Image interface and would not do unnecessary encoding step?

I am new to golang, so if there is an easier way and I am doing something wrong, please tell me.

davidklassen avatar May 09 '16 13:05 davidklassen

Better late than never, and since this is still open: Have a look at https://github.com/fabian-z/gmf/blob/example/examples/video-to-image.go

This needs two new constants, AV_PIX_FMT_BGR32 and AV_CODEC_ID_RAWVIDEO in order to use swscale to convert from the video source pixel format to the format used by Go for image.RGBA.

If you need grayscale and would like to convert in libav, you can use AV_PIX_FMT_GRAY8 and image.Gray with img.Stride = width instead.

fabian-z avatar Sep 25 '17 01:09 fabian-z

@fabian-z Great! Thanks. Could you make a PR?

3d0c avatar Sep 25 '17 14:09 3d0c

@3d0c You're welcome, glad I could help! I created #63 as a first step. Regarding the constants and example, should I do two separate PRs for that? Also, would you consider replacing video-to-jpeg.go with my example (since video-to-mjpeg.go seems to be quite similar) or should this live on its own?

fabian-z avatar Sep 25 '17 21:09 fabian-z

+1

zacharynevin avatar Oct 21 '17 22:10 zacharynevin

Done. @fabian-z thanks!

3d0c avatar Oct 25 '17 14:10 3d0c