[FFmpeg-user] [Libav-user] Please help me about transcoding to TS stream: H.264 bitstream malformed, no startcode found, use the h264_mp4toannexb bitstream filter (-bsf h264_mp4toannexb)

Harald Jordan harald.jordan at redstream.at
Fri Dec 14 00:25:22 CET 2012


Hi all!
 
I hope it is OK to reopen an old topic this way, hopefully I will get some
response anyway ;-)
 
It is about encoding x264 and putting it into a ts stream. My code always
generates raw YUV422 pictures, changes the pix_fmt to the codecs needs and
then does the encoding and muxing.

 

Basically everything works fine for x264 in mp4 container, but at ts
container, I am  facing the same problem that Li below had: libvformat is
not adding the needed header stuff to the encoded frame. Now I spent a lot
of time trying to analyze different streams using packet analyzers and hex
editors, but I don't get it. Also I tried using a bitstreamfilter
mp4toannexb, but as I learned this cannot work on my code that only
generates raw yuv422 pics instead of decoding an existing mp4 file. In fact
most descriptions of creating h264 ts streams on the net are about rewriting
an existing stream. 

 

I tried to first encode my raw frames to x264 and then create a new packet
that prepends the extradata (that was automatically activated after setting
GLOBAL_HEADER stuff)  - and then adds the ->data of the previousely encoded
packet to the new packet's data:

 

In this code, c is the video_st->codec where video_st is of type AVStream
and which was previously added to the OutputContext by using open_
av_new_stream function.

 

int ret = avcodec_encode_video2(c,&pkt,picture,&gotPacket); //pkt dts and
pts are set by encoder according to picture pts

 

       int size = c->extradata_size;

       uint8_t *extrdata = c->extradata;

 

 

                           AVPacket *tmp_pkt =
(AVPacket*)malloc(sizeof(AVPacket));

                           av_init_packet(tmp_pkt);

                           int newsize = video_st->codec->extradata_size +
pkt.size;

                        av_new_packet(tmp_pkt,newsize);

 
memcpy(tmp_pkt->data,video_st->codec->extradata,video_st->codec->extradata_s
ize);

                           uint8_t * newdataoffset = tmp_pkt->data +
video_st->codec->extradata_size;

                           memcpy((void*)newdataoffset,pkt.data,pkt.size);

 

//Muxing

                           int muxresult = -1;

                           pkt.stream_index= video_st->index;


                           muxresult = av_interleaved_write_frame(oc,&pkt);

 

 

>From Li's post below I understand he must have done similar, but pleeeease
could you tell me how or where exactly to put the extradata? - should I
append this data just like I did?


All the best,
Harry

 
 
On 3/13/2012 10:31 AM, Li Zhang wrote:
> Hi,
> 
> 
> Now the video can be played by adding the sps and pps information stored
in the avctx->extradata to the beginning of video frame. Why the TS
container do not do that for me? Is there anyone know what the problem is?
> 
> 
> Best regards,
> 
> Li
> 
> ________________________________________
> From:  <http://ffmpeg.org/mailman/listinfo/libav-user> libav-user-bounces
at ffmpeg.org [ <http://ffmpeg.org/mailman/listinfo/libav-user>
libav-user-bounces at ffmpeg.org] On Behalf Of Li Zhang [
<http://ffmpeg.org/mailman/listinfo/libav-user> lizhang at utelisys.com]
> Sent: Tuesday, March 13, 2012 10:27 AM
> To: This list is about using libavcodec, libavformat, libavutil,
libavdevice    and libavfilter.
> Subject: Re: [Libav-user] Please help me about transcoding to   TS
stream: H.264 bitstream malformed, no startcode found,  use the
h264_mp4toannexb bitstream filter (-bsf h264_mp4toannexb)
> 
> Hi,
> 
> I have check the final .ts file I got, there is no PPS and SPS for IDR
frame. I have configure that the CODEC__FLAG_GLOBAL_HEADER. The gop_size is
250. The first video frame is I frame. But there is no PPS and SPS
information. However, when I put the video and audio into mp4 container, the
video and audio all can be played.  Is there anyone can tell me what
happened?
> 
> Best regards,
> 
> Li Zhang
> 
> ________________________________________
> From:  <http://ffmpeg.org/mailman/listinfo/libav-user> libav-user-bounces
at ffmpeg.org [ <http://ffmpeg.org/mailman/listinfo/libav-user>
libav-user-bounces at ffmpeg.org] On Behalf Of Li Zhang [
<http://ffmpeg.org/mailman/listinfo/libav-user> lizhang at utelisys.com]
> Sent: Saturday, March 10, 2012 9:37 AM
> To: This list is about using libavcodec, libavformat, libavutil,
libavdevice    and libavfilter.
> Subject: Re: [Libav-user] Please help me about transcoding to TS
stream: H.264 bitstream malformed, no startcode found,  use the
h264_mp4toannexb bitstream filter (-bsf h264_mp4toannexb)
> 
> Hi Kalileo,
> 
> Thank you very much for your quick reply.
> 
> Sorry for my unclear description last email. What I have done is start
from mpeg2 TS stream, and then decoding, encoding them to H.264+AAC without
container. Next I have put H.264+AAC data frame into AVI, MKV, MP4 container
successfully. However, my final target is putting H.264+AAC data frame into
TS container. And it failed. So what I passed to IMHO h264_mp4toannexb is
not mpegts stream but H.264+AAC frame.
> 
> So I want to make sure you understand my situation correctly.
> 
> So according to my understanding, after the H.264 encoding, I should add
the h264 Annex B header to every H264 video frame manually. Am I right?
> 
> Hope there is some experts can give us confirmation about that. Or there
is any other suggestion please tell me, thanks.
> 
> 
> 
> Best regards,
> 
> Li Zhang
> 
> ________________________________________
> From:  <http://ffmpeg.org/mailman/listinfo/libav-user> libav-user-bounces
at ffmpeg.org [ <http://ffmpeg.org/mailman/listinfo/libav-user>
libav-user-bounces at ffmpeg.org] On Behalf Of Kalileo [
<http://ffmpeg.org/mailman/listinfo/libav-user> kalileo at universalx.net]
> Sent: Friday, March 09, 2012 5:27 PM
> To: This list is about using libavcodec, libavformat, libavutil,
libavdevice and libavfilter.
> Subject: Re: [Libav-user] Please help me about transcoding to TS stream:
H.264 bitstream malformed, no startcode found,  use the h264_mp4toannexb
bitstream filter (-bsf h264_mp4toannexb)
> 
> On Mar 9, 2012, at 22:20 , Li Zhang wrote:
> 
>> 
>> Hi everyone,
>> 
>> 
>> I am a new user to ffmpeg API. I want to transcode mpeg2 video stream to
H.264+AAC and then put them into TS container again.
>> 
>> I have implemented transcoding from mpeg2 TS to AVI, MKV, MP4 local
files. Now the problem is when I used the same code put those H.264+AAC data
packet into TS container, the video can not be played and only the audio can
be played. Actually, there was video packet data in the .ts file which was
derived from trandcoding.
>> 
>> And I got the information from ffmpeg that "H.264 bitstream malformed, no
startcode found, use the h264_mp4toannexb bitstream filter (-bsf
h264_mp4toannexb)". So I initiated the h264_mp4toannexb bitstreamfilter and
use av_bitstream_filter_filter to process the packet before write them to
output file. However, av_bitstream_filter_filter returns 0 and the final
output file still the same situation and the warning information still
happen.
>> 
>> There is another warning information about DTS, it only happen every 29
frames. That is so strange.  Please help me.  I used the latest version of
ffmpeg.
>> When I used the Elecard Stream Analyzer to check my transcoded file, I
found the file missed SPS and SEI information for the video stream. There is
only SPS and SEI information for audio stream.  I do not know why the TS
container can not produce these information for me.
> 
> Hi Li,
> 
> IMHO h264_mp4toannexb does not work if you convert from mpegts stream to
mpegts stream, because as far as I have understood it h264_mp4toannexb
expects mp4 as source and not mpegts. you need to do manually what
h264_mp4toannexb would do, that is adding the h264 Annex B header in front
of every h264 frame.
> 
> I'm sure the experts here can confirm or correct me, and explain better
what to do.
> 
> 
> _______________________________________________
> Libav-user mailing list
>  <http://ffmpeg.org/mailman/listinfo/libav-user> Libav-user at ffmpeg.org
>  <http://ffmpeg.org/mailman/listinfo/libav-user>
http://ffmpeg.org/mailman/listinfo/libav-user
> _______________________________________________
> Libav-user mailing list
>  <http://ffmpeg.org/mailman/listinfo/libav-user> Libav-user at ffmpeg.org
>  <http://ffmpeg.org/mailman/listinfo/libav-user>
http://ffmpeg.org/mailman/listinfo/libav-user
> _______________________________________________
> Libav-user mailing list
>  <http://ffmpeg.org/mailman/listinfo/libav-user> Libav-user at ffmpeg.org
>  <http://ffmpeg.org/mailman/listinfo/libav-user>
http://ffmpeg.org/mailman/listinfo/libav-user
> _______________________________________________
> Libav-user mailing list
>  <http://ffmpeg.org/mailman/listinfo/libav-user> Libav-user at ffmpeg.org
>  <http://ffmpeg.org/mailman/listinfo/libav-user>
http://ffmpeg.org/mailman/listinfo/libav-user
> 
 
_______________________________________________
Libav-user mailing list
 <http://ffmpeg.org/mailman/listinfo/libav-user> Libav-user at ffmpeg.org
 <http://ffmpeg.org/mailman/listinfo/libav-user>
http://ffmpeg.org/mailman/listinfo/libav-user
  _____  


*	Previous message:
<http://ffmpeg.org/pipermail/libav-user/2012-March/001523.html> [Libav-user]
Please help me about transcoding to TS stream: H.264 bitstream malformed, no
startcode found, use the h264_mp4toannexb bitstream filter (-bsf
h264_mp4toannexb) 
*	Next message:
<http://ffmpeg.org/pipermail/libav-user/2012-March/001529.html> [Libav-user]
Please help me about transcoding to TS stream: H.264 bitstream malformed, no
startcode found, use the h264_mp4toannexb bitstream filter (-bsf
h264_mp4toannexb) 
*	Messages sorted by:
<http://ffmpeg.org/pipermail/libav-user/2012-March/date.html#1527> [ date ]
<http://ffmpeg.org/pipermail/libav-user/2012-March/thread.html#1527> [
thread ]
<http://ffmpeg.org/pipermail/libav-user/2012-March/subject.html#1527> [
subject ]
<http://ffmpeg.org/pipermail/libav-user/2012-March/author.html#1527> [
author ] 

  _____  

 <http://ffmpeg.org/mailman/listinfo/libav-user> More information about the
Libav-user mailing 



More information about the ffmpeg-user mailing list