X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=modules%2Fmux%2Fmpeg%2Fpes.c;h=7df2891ee8570cedfd443c49e6c47c97a79357c9;hb=fd7f8f854a13f7b65cb2b8622755aa47ee932919;hp=18da53033dd5d644c90919d57a86c07fae1c778f;hpb=59173c65552ee499e502e4961215e85526e483ff;p=vlc diff --git a/modules/mux/mpeg/pes.c b/modules/mux/mpeg/pes.c index 18da53033d..7df2891ee8 100644 --- a/modules/mux/mpeg/pes.c +++ b/modules/mux/mpeg/pes.c @@ -1,8 +1,8 @@ /***************************************************************************** * pes.c: PES packetizer used by the MPEG multiplexers ***************************************************************************** - * Copyright (C) 2001, 2002 VideoLAN - * $Id: pes.c,v 1.13 2003/12/01 23:39:11 gbazin Exp $ + * Copyright (C) 2001, 2002 the VideoLAN team + * $Id$ * * Authors: Laurent Aimar * Eric Petit @@ -19,54 +19,70 @@ * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA. + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA. *****************************************************************************/ /***************************************************************************** * Preamble *****************************************************************************/ -#include -#include -#include -#include -#include -#include - -#include -#include -#include - -#ifdef HAVE_UNISTD_H -# include + +#ifdef HAVE_CONFIG_H +# include "config.h" #endif -#include "codecs.h" +#include +#include +#include +#include + +#include #include "pes.h" #include "bits.h" -#define PES_PAYLOAD_SIZE_MAX 65500 - +/** PESHeader, write a pes header + * \param i_es_size length of payload data. (Must be < PES_PAYLOAD_SIZE_MAX + * unless the conditions for unbounded PES packets are met) + * \param i_stream_id stream id as follows: + * - 0x00 - 0xff : normal stream_id as per Table 2-18 + * - 0xfd00 - 0xfd7f : stream_id_extension = low 7 bits + * (stream_id = PES_EXTENDED_STREAM_ID) + * - 0xbd00 - 0xbdff : private_id = low 8 bits + * (stream_id = PES_PRIVATE_STREAM) + * \param i_header_size length of padding data to insert into PES packet + * header in bytes. + */ static inline int PESHeader( uint8_t *p_hdr, mtime_t i_pts, mtime_t i_dts, - int i_es_size, int i_stream_id, int i_private_id, - vlc_bool_t b_mpeg2 ) + int i_es_size, es_format_t *p_fmt, + int i_stream_id, bool b_mpeg2, + bool b_data_alignment, int i_header_size ) { bits_buffer_t bits; int i_extra = 0; + int i_private_id = -1; + int i_stream_id_extension = 0; - /* For PES_PRIVATE_STREAM_1 there is an extra header after the - pes header */ - /* i_private_id != -1 because TS use 0xbd without private_id */ - if( i_stream_id == PES_PRIVATE_STREAM_1 && i_private_id != -1 ) + /* HACK for private stream 1 in ps */ + if( ( i_stream_id >> 8 ) == PES_PRIVATE_STREAM_1 ) { + i_private_id = i_stream_id & 0xff; + i_stream_id = PES_PRIVATE_STREAM_1; + /* For PES_PRIVATE_STREAM_1 there is an extra header after the + pes header */ + /* i_private_id != -1 because TS use 0xbd without private_id */ i_extra = 1; if( ( i_private_id & 0xf0 ) == 0x80 ) - { i_extra += 3; - } + } + else if( ( i_stream_id >> 8 ) == PES_EXTENDED_STREAM_ID ) + { + /* Enable support for extended_stream_id as defined in + * ISO/IEC 13818-1:2000/Amd.2:2003 */ + /* NB, i_extended_stream_id is limited to 7 bits */ + i_stream_id_extension = i_stream_id & 0x7f; + i_stream_id = PES_EXTENDED_STREAM_ID; } - - bits_initwrite( &bits, 30, p_hdr ); + bits_initwrite( &bits, 50, p_hdr ); /* add start code */ bits_write( &bits, 24, 0x01 ); @@ -90,28 +106,49 @@ static inline int PESHeader( uint8_t *p_hdr, mtime_t i_pts, mtime_t i_dts, /* arg, a little more difficult */ if( b_mpeg2 ) { - int i_pts_dts; + int i_pts_dts; + bool b_pes_extension_flag = false; - if( i_pts >= 0 && i_dts >= 0 ) + if( i_pts > 0 && i_dts > 0 && + ( i_pts != i_dts || ( p_fmt->i_cat == VIDEO_ES && + p_fmt->i_codec != VLC_CODEC_MPGV ) ) ) { - bits_write( &bits, 16, i_es_size + i_extra+ 13 ); i_pts_dts = 0x03; + if ( !i_header_size ) i_header_size = 0xa; } - else if( i_pts >= 0 ) + else if( i_pts > 0 ) { - bits_write( &bits, 16, i_es_size + i_extra + 8 ); i_pts_dts = 0x02; + if ( !i_header_size ) i_header_size = 0x5; } else { - bits_write( &bits, 16, i_es_size + i_extra + 3 ); i_pts_dts = 0x00; + if ( !i_header_size ) i_header_size = 0x0; + } + + if( i_stream_id == PES_EXTENDED_STREAM_ID ) + { + b_pes_extension_flag = true; + i_header_size += 1 + 1; } + if( b_pes_extension_flag ) + { + i_header_size += 1; + } + + /* Unbounded streams are only allowed in TS (not PS) and only + * for some ES, eg. MPEG* Video ES or Dirac ES. */ + if( i_es_size > PES_PAYLOAD_SIZE_MAX ) + bits_write( &bits, 16, 0 ); // size unbounded + else + bits_write( &bits, 16, i_es_size + i_extra + 3 + + i_header_size ); // size bits_write( &bits, 2, 0x02 ); // mpeg2 id bits_write( &bits, 2, 0x00 ); // pes scrambling control bits_write( &bits, 1, 0x00 ); // pes priority - bits_write( &bits, 1, 0x00 ); // data alignement indicator + bits_write( &bits, 1, b_data_alignment ); // data alignement indicator bits_write( &bits, 1, 0x00 ); // copyright bits_write( &bits, 1, 0x00 ); // original or copy @@ -121,19 +158,8 @@ static inline int PESHeader( uint8_t *p_hdr, mtime_t i_pts, mtime_t i_dts, bits_write( &bits, 1, 0x00 ); // dsm trick mode flag bits_write( &bits, 1, 0x00 ); // additional copy info flag bits_write( &bits, 1, 0x00 ); // pes crc flag - bits_write( &bits, 1, 0x00 ); // pes extention flags - if( i_pts_dts == 0x03 ) - { - bits_write( &bits, 8, 0x0a ); // header size -> pts and dts - } - else if( i_pts_dts == 0x02 ) - { - bits_write( &bits, 8, 0x05 ); // header size -> pts - } - else - { - bits_write( &bits, 8, 0x00 ); // header size -> 0 - } + bits_write( &bits, 1, b_pes_extension_flag ); + bits_write( &bits, 8, i_header_size ); /* write pts */ if( i_pts_dts & 0x02 ) @@ -145,6 +171,7 @@ static inline int PESHeader( uint8_t *p_hdr, mtime_t i_pts, mtime_t i_dts, bits_write( &bits, 1, 0x01 ); // marker bits_write( &bits, 15, i_pts ); bits_write( &bits, 1, 0x01 ); // marker + i_header_size -= 0x5; } /* write i_dts */ if( i_pts_dts & 0x01 ) @@ -156,18 +183,49 @@ static inline int PESHeader( uint8_t *p_hdr, mtime_t i_pts, mtime_t i_dts, bits_write( &bits, 1, 0x01 ); // marker bits_write( &bits, 15, i_dts ); bits_write( &bits, 1, 0x01 ); // marker + i_header_size -= 0x5; + } + if( b_pes_extension_flag ) + { + bits_write( &bits, 1, 0x00 ); // PES_private_data_flag + bits_write( &bits, 1, 0x00 ); // pack_header_field_flag + bits_write( &bits, 1, 0x00 ); // program_packet_sequence_counter_flag + bits_write( &bits, 1, 0x00 ); // P-STD_buffer_flag + bits_write( &bits, 3, 0x07 ); // reserved + bits_write( &bits, 1, 0x01 ); // PES_extension_flag_2 + /* skipping unsupported parts: */ + /* PES_private_data */ + /* pack_header */ + /* program_packet_sequence_counter */ + /* P-STD_buffer_flag */ + if( i_stream_id == PES_EXTENDED_STREAM_ID ) + { + /* PES_extension_2 */ + bits_write( &bits, 1, 0x01 ); // marker + bits_write( &bits, 7, 0x01 ); // PES_extension_field_length + bits_write( &bits, 1, 0x01 ); // stream_id_extension_flag + bits_write( &bits, 7, i_stream_id_extension ); + i_header_size -= 0x2; + } + i_header_size -= 0x1; + } + while ( i_header_size ) + { + bits_write( &bits, 8, 0xff ); + i_header_size--; } } else /* MPEG1 */ { int i_pts_dts; - if( i_pts >= 0 && i_dts >= 0 ) + if( i_pts > 0 && i_dts > 0 && + ( i_pts != i_dts || p_fmt->i_cat == VIDEO_ES ) ) { bits_write( &bits, 16, i_es_size + i_extra + 10 /* + stuffing */ ); i_pts_dts = 0x03; } - else if( i_pts >= 0 ) + else if( i_pts > 0 ) { bits_write( &bits, 16, i_es_size + i_extra + 5 /* + stuffing */ ); i_pts_dts = 0x02; @@ -228,58 +286,95 @@ static inline int PESHeader( uint8_t *p_hdr, mtime_t i_pts, mtime_t i_dts, } } -int E_( EStoPES )( sout_instance_t *p_sout, - sout_buffer_t **pp_pes, - sout_buffer_t *p_es, - int i_stream_id, - int b_mpeg2 ) +/** EStoPES, encapsulate an elementary stream block into PES packet(s) + * each with a maximal payload size of @i_max_pes_size@. + * + * In some circumstances, unbounded PES packets are allowed: + * - Transport streams only (NOT programme streams) + * - Only some types of elementary streams (eg MPEG2 video) + * It is the responsibility of the caller to enforce these constraints. + * + * EStoPES will only produce an unbounded PES packet if: + * - ES is VIDEO_ES + * - i_max_pes_size > PES_PAYLOAD_SIZE_MAX + * - length of p_es > PES_PAYLOAD_SIZE_MAX + * If the last condition is not met, a single PES packet is produced + * which is not unbounded in length. + * + * \param i_stream_id stream id as follows: + * - 0x00 - 0xff : normal stream_id as per Table 2-18 + * - 0xfd00 - 0xfd7f : stream_id_extension = low 7 bits + * (stream_id = PES_EXTENDED_STREAM_ID) + * - 0xbd00 - 0xbdff : private_id = low 8 bits + * (stream_id = PES_PRIVATE_STREAM) + * \param i_header_size length of padding data to insert into PES packet + * header in bytes. + * \param i_max_pes_size maximum length of each pes packet payload. + * if zero, uses default maximum. + * To allow unbounded PES packets in transport stream + * VIDEO_ES, set to INT_MAX. + */ +int EStoPES ( sout_instance_t *p_sout, block_t **pp_pes, block_t *p_es, + es_format_t *p_fmt, int i_stream_id, + int b_mpeg2, int b_data_alignment, int i_header_size, + int i_max_pes_size ) { - sout_buffer_t *p_es_sav, *p_pes; + block_t *p_pes; mtime_t i_pts, i_dts, i_length; uint8_t *p_data; int i_size; - int i_private_id = -1; - - uint8_t header[30]; // PES header + extra < 30 (more like 17) + uint8_t header[50]; // PES header + extra < 50 (more like 17) int i_pes_payload; int i_pes_header; int i_pes_count = 1; - /* HACK for private stream 1 in ps */ - if( ( i_stream_id >> 8 ) == PES_PRIVATE_STREAM_1 ) + assert( i_max_pes_size >= 0 ); + assert( i_header_size >= 0 ); + + /* NB, Only video ES may have unbounded length */ + if( !i_max_pes_size || + ( p_fmt->i_cat != VIDEO_ES && i_max_pes_size > PES_PAYLOAD_SIZE_MAX ) ) { - i_private_id = i_stream_id & 0xff; - i_stream_id = PES_PRIVATE_STREAM_1; + i_max_pes_size = PES_PAYLOAD_SIZE_MAX; + } + + if( p_fmt->i_codec == VLC_CODEC_MP4V && + p_es->i_flags & BLOCK_FLAG_TYPE_I ) + { + /* For MPEG4 video, add VOL before I-frames */ + p_es = block_Realloc( p_es, p_fmt->i_extra, p_es->i_buffer ); + + memcpy( p_es->p_buffer, p_fmt->p_extra, p_fmt->i_extra ); } - i_pts = p_es->i_pts < 0 ? -1 : p_es->i_pts * 9 / 100; // 90000 units clock - i_dts = p_es->i_dts < 0 ? -1 : p_es->i_dts * 9 / 100; // 90000 units clock + i_pts = p_es->i_pts <= 0 ? 0 : p_es->i_pts * 9 / 100; // 90000 units clock + i_dts = p_es->i_dts <= 0 ? 0 : p_es->i_dts * 9 / 100; // 90000 units clock - i_size = p_es->i_size; + i_size = p_es->i_buffer; p_data = p_es->p_buffer; *pp_pes = p_pes = NULL; - p_es_sav = p_es; + +#ifndef NDEBUG + memset( header, 0, 50 ); +#endif do { - i_pes_payload = __MIN( i_size, PES_PAYLOAD_SIZE_MAX ); + i_pes_payload = __MIN( i_size, i_max_pes_size ); i_pes_header = PESHeader( header, i_pts, i_dts, i_pes_payload, - i_stream_id, i_private_id, b_mpeg2 ); - i_dts = -1; // only first PES has a dts/pts - i_pts = -1; + p_fmt, i_stream_id, b_mpeg2, + b_data_alignment, i_header_size ); + i_dts = 0; // only first PES has a dts/pts + i_pts = 0; - if( p_es ) + if( p_es ) { - if( sout_BufferReallocFromPreHeader( p_sout, p_es, i_pes_header ) ) - { - msg_Err( p_sout, - "cannot realloc preheader (should never happen)" ); - return( -1 ); - } + p_es = block_Realloc( p_es, i_pes_header, p_es->i_buffer ); + p_data = p_es->p_buffer+i_pes_header; /* reuse p_es for first frame */ *pp_pes = p_pes = p_es; /* don't touch i_dts, i_pts, i_length as are already set :) */ @@ -287,8 +382,7 @@ int E_( EStoPES )( sout_instance_t *p_sout, } else { - p_pes->p_next = sout_BufferNew( p_sout, - i_pes_header + i_pes_payload ); + p_pes->p_next = block_New( p_sout, i_pes_header + i_pes_payload ); p_pes = p_pes->p_next; p_pes->i_dts = 0; @@ -296,8 +390,8 @@ int E_( EStoPES )( sout_instance_t *p_sout, p_pes->i_length = 0; if( i_pes_payload > 0 ) { - p_sout->p_vlc->pf_memcpy( p_pes->p_buffer + i_pes_header, - p_data, i_pes_payload ); + vlc_memcpy( p_pes->p_buffer + i_pes_header, p_data, + i_pes_payload ); } i_pes_count++; } @@ -307,16 +401,10 @@ int E_( EStoPES )( sout_instance_t *p_sout, i_size -= i_pes_payload; p_data += i_pes_payload; - p_pes->i_size = i_pes_header + i_pes_payload; + p_pes->i_buffer = i_pes_header + i_pes_payload; } while( i_size > 0 ); - /* sav some space */ - if( p_es_sav->i_size + 10*1024 < p_es_sav->i_buffer_size ) - { - sout_BufferRealloc( p_sout, p_es_sav, p_es_sav->i_size ); - } - /* Now redate all pes */ i_dts = (*pp_pes)->i_dts; i_length = (*pp_pes)->i_length / i_pes_count; @@ -327,7 +415,6 @@ int E_( EStoPES )( sout_instance_t *p_sout, i_dts += i_length; } - return( 0 ); -} - + return 0; +}