mpegtsenc: Allow 3 byte start codes for H264, too

Message ID 1328860885-37664-1-git-send-email-martin@martin.st
State Superseded
Headers show

Commit Message

Martin Storsjö Feb. 10, 2012, 8:01 a.m.
---
 libavformat/mpegtsenc.c |    3 ++-
 1 files changed, 2 insertions(+), 1 deletions(-)

Comments

Luca Barbato Feb. 10, 2012, 9:15 a.m. | #1
On 2/10/12 12:01 AM, Martin Storsjö wrote:
> ---
>   libavformat/mpegtsenc.c |    3 ++-
>   1 files changed, 2 insertions(+), 1 deletions(-)
>

Assuming it won't break anything or generate faulty ts, ok.

lu
Martin Storsjö Feb. 10, 2012, 9:20 a.m. | #2
On Fri, 10 Feb 2012, Luca Barbato wrote:

> On 2/10/12 12:01 AM, Martin Storsjö wrote:
>> ---
>>   libavformat/mpegtsenc.c |    3 ++-
>>   1 files changed, 2 insertions(+), 1 deletions(-)
>> 
>
> Assuming it won't break anything or generate faulty ts, ok.

TS files generated with 3 byte start codes (stream copy from RTP/H264) 
played back fine in both VLC and avplay, with this patch applied.

I'll push this in a while then, if there's no objections.

// Martin
Gil Pedersen Feb. 10, 2012, 1:11 p.m. | #3
On 10/02/2012, at 10.20, Martin Storsjö wrote:

> On Fri, 10 Feb 2012, Luca Barbato wrote:
> 
>> On 2/10/12 12:01 AM, Martin Storsjö wrote:
>>> ---
>>>  libavformat/mpegtsenc.c |    3 ++-
>>>  1 files changed, 2 insertions(+), 1 deletions(-)
>> 
>> Assuming it won't break anything or generate faulty ts, ok.
> 
> TS files generated with 3 byte start codes (stream copy from RTP/H264) played back fine in both VLC and avplay, with this patch applied.
> 
> I'll push this in a while then, if there's no objections.

This would generate non-compliant streams. H.264 Annex-B coded streams mandate that the first start code is at least 4 bytes long, and additionally ISO/IEC 13818-1 requires it to be exactly 4 bytes.

Regards,
Gil
Martin Storsjö Feb. 10, 2012, 1:26 p.m. | #4
On Fri, 10 Feb 2012, Gil Pedersen wrote:

> On 10/02/2012, at 10.20, Martin Storsjö wrote:
>
>> On Fri, 10 Feb 2012, Luca Barbato wrote:
>>
>>> On 2/10/12 12:01 AM, Martin Storsjö wrote:
>>>> ---
>>>>  libavformat/mpegtsenc.c |    3 ++-
>>>>  1 files changed, 2 insertions(+), 1 deletions(-)
>>>
>>> Assuming it won't break anything or generate faulty ts, ok.
>>
>> TS files generated with 3 byte start codes (stream copy from RTP/H264) played back fine in both VLC and avplay, with this patch applied.
>>
>> I'll push this in a while then, if there's no objections.
>
> This would generate non-compliant streams. H.264 Annex-B coded streams mandate that the first start code is at least 4 bytes long, and additionally ISO/IEC 13818-1 requires it to be exactly 4 bytes.

Right. So then perhaps we should apply the fix for the H264 RTP 
depacketizer that I posted the other day. Jason?

// Martin
Janne Grunau Feb. 12, 2012, 9:39 p.m. | #5
On 2012-02-10 13:11:49 +0000, Gil Pedersen wrote:
> On 10/02/2012, at 10.20, Martin Storsjö wrote:
> 
> > On Fri, 10 Feb 2012, Luca Barbato wrote:
> > 
> >> On 2/10/12 12:01 AM, Martin Storsjö wrote:
> >>> --- libavformat/mpegtsenc.c |    3 ++- 1 files changed, 2 insertions(+),
> >>> 1 deletions(-)
> >> 
> >> Assuming it won't break anything or generate faulty ts, ok.
> > 
> > TS files generated with 3 byte start codes (stream copy from RTP/H264)
> > played back fine in both VLC and avplay, with this patch applied.
> > 
> > I'll push this in a while then, if there's no objections.
> 
> This would generate non-compliant streams. H.264 Annex-B coded streams
> mandate that the first start code is at least 4 bytes long,

It mandates an extra zero byte before the fisrt start code prefix of
every access unit and the muxer expects complete access units.

> and additionally ISO/IEC 13818-1 requires it to be exactly 4 bytes.

start codes in ISO 13818-1 are always 4 bytes but this checks only the
start code prefix which is always three bytes.

Janne

Patch

diff --git a/libavformat/mpegtsenc.c b/libavformat/mpegtsenc.c
index 8232cbc..bbe3085 100644
--- a/libavformat/mpegtsenc.c
+++ b/libavformat/mpegtsenc.c
@@ -954,7 +954,8 @@  static int mpegts_write_packet(AVFormatContext *s, AVPacket *pkt)
         const uint8_t *p = buf, *buf_end = p+size;
         uint32_t state = -1;
 
-        if (pkt->size < 5 || AV_RB32(pkt->data) != 0x0000001) {
+        if (pkt->size < 5 || (AV_RB32(pkt->data) != 0x0000001 &&
+                              AV_RB24(pkt->data) != 0x0000001)) {
             av_log(s, AV_LOG_ERROR, "H.264 bitstream malformed, "
                    "no startcode found, use -bsf h264_mp4toannexb\n");
             return -1;