Commit 953dd004 authored by François Cartegnie's avatar François Cartegnie 🤞

packetizer: add AnnexB startcode helper

Improves startcode lookup by ~80%
(statistically/zero dependent)
parent 0aebb95c
......@@ -26,7 +26,7 @@ libpacketizer_avparser_plugin_la_CFLAGS = $(AVCODEC_CFLAGS) $(AVUTIL_CFLAGS) $(A
libpacketizer_avparser_plugin_la_LIBADD = $(AVCODEC_LIBS) $(AVUTIL_LIBS) $(LIBM)
noinst_HEADERS += packetizer/packetizer_helper.h
noinst_HEADERS += packetizer/packetizer_helper.h packetizer/startcode_helper.h
packetizer_LTLIBRARIES = \
libpacketizer_mpegvideo_plugin.la \
......
......@@ -45,6 +45,7 @@
#include "hxxx_nal.h"
#include "hxxx_common.h"
#include "packetizer_helper.h"
#include "startcode_helper.h"
/*****************************************************************************
* Module descriptor
......@@ -195,7 +196,7 @@ static int Open( vlc_object_t *p_this )
}
packetizer_Init( &p_sys->packetizer,
p_h264_startcode, sizeof(p_h264_startcode), NULL,
p_h264_startcode, sizeof(p_h264_startcode), startcode_FindAnnexB,
p_h264_startcode, 1, 5,
PacketizeReset, PacketizeParse, PacketizeValidate, p_dec );
......
......@@ -36,6 +36,7 @@
#include <vlc_block_helper.h>
#include "packetizer_helper.h"
#include "startcode_helper.h"
#include "hevc_nal.h"
#include "hxxx_nal.h"
#include "hxxx_common.h"
......@@ -100,7 +101,7 @@ static int Open(vlc_object_t *p_this)
p_sys->pp_frame_last = &p_sys->p_frame;
packetizer_Init(&p_dec->p_sys->packetizer,
p_hevc_startcode, sizeof(p_hevc_startcode), NULL,
p_hevc_startcode, sizeof(p_hevc_startcode), startcode_FindAnnexB,
p_hevc_startcode, 1, 5,
PacketizeReset, PacketizeParse, PacketizeValidate, p_dec);
......
......@@ -40,6 +40,7 @@
#include <vlc_bits.h>
#include <vlc_block_helper.h>
#include "packetizer_helper.h"
#include "startcode_helper.h"
/*****************************************************************************
* Module descriptor
......@@ -142,7 +143,7 @@ static int Open( vlc_object_t *p_this )
/* Misc init */
packetizer_Init( &p_sys->packetizer,
p_mp4v_startcode, sizeof(p_mp4v_startcode), NULL,
p_mp4v_startcode, sizeof(p_mp4v_startcode), startcode_FindAnnexB,
NULL, 0, 4,
PacketizeReset, PacketizeParse, PacketizeValidate, p_dec );
......
......@@ -53,6 +53,7 @@
#include <vlc_block_helper.h>
#include "../codec/cc.h"
#include "packetizer_helper.h"
#include "startcode_helper.h"
#define SYNC_INTRAFRAME_TEXT N_("Sync on Intra Frame")
#define SYNC_INTRAFRAME_LONGTEXT N_("Normally the packetizer would " \
......@@ -168,7 +169,7 @@ static int Open( vlc_object_t *p_this )
/* Misc init */
packetizer_Init( &p_sys->packetizer,
p_mp2v_startcode, sizeof(p_mp2v_startcode), NULL,
p_mp2v_startcode, sizeof(p_mp2v_startcode), startcode_FindAnnexB,
NULL, 0, 4,
PacketizeReset, PacketizeParse, PacketizeValidate, p_dec );
......
/*****************************************************************************
* startcode_helper.h: Startcodes helpers
*****************************************************************************
* Copyright (C) 2016 VideoLAN Authors
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
#ifndef _STARTCODE_HELPER_H
#define _STARTCODE_HELPER_H 1
/* Looks up efficiently for an AnnexB startcode 0x00 0x00 0x01
* by using a 4 times faster trick than single byte lookup.
*
* That code is adapted from libav's ff_avc_find_startcode_internal
* and i believe the trick originated from
* https://graphics.stanford.edu/~seander/bithacks.html#ZeroInWord
*/
static inline const uint8_t * startcode_FindAnnexB( const uint8_t *p, const uint8_t *end )
{
const uint8_t *a = p + 4 - ((intptr_t)p & 3);
for (end -= 3; p < a && p < end; p++) {
if (p[0] == 0 && p[1] == 0 && p[2] == 1)
return p;
}
for (end -= 3; p < end; p += 4) {
uint32_t x = *(const uint32_t*)p;
if ((x - 0x01010101) & (~x) & 0x80808080)
{
/* matching DW isn't faster */
if (p[1] == 0) {
if (p[0] == 0 && p[2] == 1)
return p;
if (p[2] == 0 && p[3] == 1)
return p+1;
}
if (p[3] == 0) {
if (p[2] == 0 && p[4] == 1)
return p+2;
if (p[4] == 0 && p[5] == 1)
return p+3;
}
}
}
for (end += 3; p < end; p++) {
if (p[0] == 0 && p[1] == 0 && p[2] == 1)
return p;
}
return NULL;
}
#endif
......@@ -39,6 +39,7 @@
#include <vlc_block_helper.h>
#include "../codec/cc.h"
#include "packetizer_helper.h"
#include "startcode_helper.h"
/*****************************************************************************
* Module descriptor
......@@ -155,7 +156,7 @@ static int Open( vlc_object_t *p_this )
return VLC_ENOMEM;
packetizer_Init( &p_sys->packetizer,
p_vc1_startcode, sizeof(p_vc1_startcode), NULL,
p_vc1_startcode, sizeof(p_vc1_startcode), startcode_FindAnnexB,
NULL, 0, 4,
PacketizeReset, PacketizeParse, PacketizeValidate, p_dec );
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment