Helder AC
Helder AC

Reputation: 376

Extracting I-Frames from H264 in MPEG-TS in C

I am experimenting with video and would like to know how I can extract I-frames from H264 contained in MPEG-TS container. What I want to do is generate preview images out of a video stream. As the I-frame is supposed to be a complete picture fro which P- and B-Frames derive, is there a possibility to just extract the data of the picture without having to decode it using a codec?

I have already done some work with MPEG-TS container format but I am not that much specialized in codecs.

I am rather in search of information.

Thanks a lot.

Upvotes: 4

Views: 7074

Answers (3)

Guy Sirton
Guy Sirton

Reputation: 8401

As others have noted decoding h.264 is complicated. You could write your own decoder but it is a major effort. Why not use an existing decoder?

Intel's IPP library has the basic building blocks for a decoder and a sample decoer:

Code Samples for the Intel® Integrated Performance Primitives

There's libavcodec:

Using libavformat and libavcodec

Revised avcodec_sample.0.4.9.cPP

Upvotes: 3

alexander
alexander

Reputation: 2753

I am not expert in this domain too. But I've played with decoding. Use this gstreamer pipeline to extract preview from video.mp4:

gst-launch -v filesrc location=./video.mp4 ! qtdemux name=demux demux.video_00 ! ffdec_h264 ! videorate ! 'video/x-raw-yuv,framerate=1/1' ! jpegenc ! multifilesink location=image-%05d.jpeg

If you want to write some code, replace videorate with appsrc/appsink elements. Write control program to the pipelines (see example):

filesrc location=./video.mp4 ! qtdemux name=demux demux.video_00 ! ffdec_h264 ! appsink
appsrc ! 'video/x-raw-yuv,framerate=1/1' ! jpegenc ! multifilesink location=image-%05d.jpeg

Buffers without GST_BUFFER_FLAG_DELTA_UNIT flag set is I-frames. You can safely skip many frames and start decoding stream at any I-frame.

Upvotes: 2

puffadder
puffadder

Reputation: 1824

I am no expert in this domain but I believe the answer to your question is NO.

If you want to save the I-frame as a JPEG image, you still need to "transcode" the video frame i.e. you first need to decode the I-frame using a H264 decoder and then encode it using a JPEG encoder. This is so because the JPEG encoder does not understand a H264 frame, it only accepts uncompressed video frames as input.

As an aside, since the input to the JPEG encoder is an uncompressed frame, you can generate a JPEG image from any type of frame (I/P/B) as it would already be decoded (using reference I frame, if needed) before feeding to the encoder.

Upvotes: 3

Related Questions