convert h264 avcc stream to SampleBuffer in iOS 8 - ios8

How do I convert h264 avcc stream to SampleBuffer in iOS 8
Need the sample buffer to perform decompression session

It has to be in Mpeg-4 format. You will need an algorithm to read the raw byte stream looking for NAL separator codes (usually 00000001) and then look for the NAL unit code in the next byte.
Typical NAL Unit codes are 67 (PPS), 68 (SPS), 41 (P Frame) and 65 (I Frame)
Generally the first will be the SPS, capture these bytes in an NSMutable Data, then capture the PPS in another. use CMVideoFormatDescriptionCreateFromH264ParameterSets with the SPS and PPS data to create a CMFormatDescription.
Use the CMFormatDescription to create a VTDecompressionSession using VTDecompressionSessionCreate.
When you have these, capture the NALUnit payload into a CMBlockBuffer making sure to replace the separator code with a 4 byte length code (the length of the NalUnit including the unit code) and then using this and the Format Description you created before, create a CMSampleBuffer.
You now have everything you need to use VTDecompressionSessionDecodeFrame.

Related

I am using ffmpeg to stream a h264 encoded avi file to a player and the player supports only packetization mode 0

I am using ffmpeg to stream a h264 encoded avi file to a player and
the player supports only packetization mode 0 ( single NAL unit mode
). But ffmpeg always uses packetization mode 1 and sends FU-A nal unit
type, the player does not play the video on receiving a fu-a nal type
payload. It just displays a blank screen. I understand non-interleaved
mode supports both single NAL unit types (1-23) and fua, but how to
can I force ffmpeg to use only single nal unit type mode? Can some one
help me?
I'm assuming you mean H264 over RTP here. FFmpeg's RTP muxer can be forced to use mode 0 by using flag -rtpflags h264_mode0; though if you are seeing FU-A type (28) then chances are some NAL units can't fit single RTP packet and mode0 won't work.

NAL Type STAP-A and retrieve the sps and pps

I wrote a RTP server to receive the RTP packets which are sent by command ffmpeg -i test.mp4 rtp rtp://ip:port (client) and the server could get the nal type 24 (STAP-A).
And I want to use the server to retrieve the spa and pps from the first nal(type 24) instead of info from ffmpeg command.
Is it possible SPS and PPS would be aggregated in one nal ?
for example
[RTP header][nal header(type 24)][nal1 header][nal1 size][nal1 payload][nal2 header][nal2 size][nal2 payload]...
thanks
It's highly likely that the STAP-A consists of the SPS and PPS: these NAL units are usually at the beginning of the stream, small and can be aggregated into a STAP A. If the IDR is small enough, it might also be part of the STAP, but usually this is to big and will be sent separately.
The best thing to verify this is to split the STAP-A into the original NAL units (See RFC6184) and check for types 7 (SPS) and 8 (PPS).

Stream H264 using live555 and FFmpeg

I am trying to stream data encoded using FFMPEg using live555. I have a custom framesource that sends the data to sink but I am unable to figure out how to set SPS and PPS in the framer. I understand that extradata contains this information but I saw only SPS in that. Does extradata changes while encoding by FFMPeg? If yes how and when we need to update this information in live555 framer.
Does anyone have a working sample using FFMpeg and live555 to stream H264
Live555 is simply a streaming tool, it does not do any encoding.
The SPS and PPS are NAL units within the encoded H264 stream (or the output from your FFMPEG implementation) (see some info here: http://www.cardinalpeak.com/blog/the-h-264-sequence-parameter-set/).
If you want to change the SPS or PPS information you need to do it in FFMPEG.
Examples of FFMPEG and Live555 working together to stream MPG2 and H264 streams are here:
https://github.com/alm865/FFMPEG-Live555-H264-H265-Streamer/
As for streaming a H264 stream, you need to break the output from FFMPEG into NAL units before you send if off to the discrete framer for it to work correctly. You must also strip the leading and trailing NAL bits from the packet (i.e. remove the NAL identifier 0x00 0x00 0x00 0x01).
Live555 will automatically read these in and update as necessary.

Parsing NAL units using FFMPEG

I am new to MPEG-4 and taking baby steps to learn it. I am using FFMPEG as reference.
I understand that all mpeg-4 are encoded into NAL units and wrt to FFMPEG av_read_frame() function returns one NAL unit, Am I right? Is frame a NAL unit? (though it can be a combination of multiple NALs)
I also saw that h264_parser.c implements a function called h264_parse which is calling parse_nal_units() inside, If i need to get NAL units how can I use this parse_nal_units from my main function?
What is av_parse_Parse2() function do? does it return decoded NAL units?
OR FFMPEG has -vbsf h264_mp4toannexb switch to dump raw NAL units, Can somebody help me understand how I can use the same from my main function?
Please help me out here...
-ash5
For question 1:
The following article has links that will help you understand what NALs are.
In h264 NAL units means frame.?
NALs are divided into several types, and depending on the type can contain decoding parameters (SPS, PPS), enhancement information (SEI) and video samples (slice header and data). A common sequence from a broadcast transport stream would be SPS, PPS, SEI, slice_header(), slice_data(), SEI, slice_header(), slice_data() *
You probably don't need to understand ISO 14496-10 section 7.3 "Syntax in tabular form" for your application.

H264: decode series of nal units with ffmpeg

I tried to decode a series of nal units with ffmpeg (libavcodec) but I get a "no frame" error. I produced the nal units with the guideline at How does one encode a series of images into H264 using the x264 C API?. I tried the following strategy for decoding:
avcodec_init();
avcodec_register_all();
AVCodec* pCodec;
pCodec=lpavcodec_find_decoder(CODEC_ID_H264);
AVCodecContext* pCodecContext;
pCodecContext=lpavcodec_alloc_context();
avcodec_open(pCodecContext,pCodec);
AVFrame *pFrame;
pFrame=avcodec_alloc_frame();
//for every nal unit:
int frameFinished=0;
//nalData2 is nalData without the first 4 bytes
avcodec_decode_video(pCodecContext,pFrame,&frameFinished,(uint8_t*) nalData2,nalLength);
I passed all units I got to this code but frameFinished stays 0. I guess there must be something wrong with the pCodecContext setup. Can someone send me a working example?
Thank you
Check out this tutorial. It should be able to decode any video type including H.264:
http://dranger.com/ffmpeg/
I don't know exactly what is causing the problem, but I suspect it has something to do with the fact that you are not using the av_read_frame from libavformat to parse out a frames worth of data at a time. H.264 has the ability to split a frame into multiple slices and therefore multiple NAL units.
I am pretty sure the x264 encoder does not do this by default and produces one NAL unit per frame. However, there are NAL units with other stream information that need to be fed to the decoder. I have experimented with this in the past and when av_read_frame parses out a frames worth of data, it sometimes contains multiple NAL units. I would suggest following the tutorial closely and seeing if that works.
Another thing is that I think you do need to pass the first 4 bytes of the NAL unit into avcodec_decode_video if that is the start code you are talking about (0x00000001). Having investigated the output from av_read_frame, the start codes are still in the data when passed to the decoder.
Try this after the codec context instantiation code:
if(pCodec->capabilities & CODEC_CAP_TRUNCATED)
pCodecContext->flags |= CODEC_FLAG_TRUNCATED; /* We may send incomplete frames */
if(pCodec->capabilities & CODEC_FLAG2_CHUNKS)
pCodecContext->flags2 |= CODEC_FLAG2_CHUNKS;

Resources