Commit 6c9bc13f authored by Felix Paul Kühne's avatar Felix Paul Kühne

include/vlc_bits: add a block based byte stream writer in a generic place...

include/vlc_bits: add a block based byte stream writer in a generic place based on the mp4 and avi muxers
parent b80538f7
/***************************************************************************** /*****************************************************************************
* vlc_bits.h : Bit handling helpers * vlc_bits.h : Bit handling helpers
***************************************************************************** *****************************************************************************
* Copyright (C) 2003 VLC authors and VideoLAN * Copyright (C) 2001, 2002, 2003, 2006, 2015 VLC authors and VideoLAN
* $Id$ * $Id$
* *
* Authors: Laurent Aimar <fenrir@via.ecp.fr> * Authors: Laurent Aimar <fenrir@via.ecp.fr>
* Gildas Bazin <gbazin at videolan dot org>
* Rafaël Carré <funman at videolan dot org>
* *
* This program is free software; you can redistribute it and/or modify it * 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 * under the terms of the GNU Lesser General Public License as published by
...@@ -24,11 +26,20 @@ ...@@ -24,11 +26,20 @@
#ifndef VLC_BITS_H #ifndef VLC_BITS_H
#define VLC_BITS_H 1 #define VLC_BITS_H 1
#include <vlc_block.h>
/** /**
* \file * \file
* This file defines functions, structures for handling streams of bits in vlc * This file defines functions, structures for handling streams of bits in vlc
*/ */
typedef struct bo_t
{
block_t *b;
size_t len;
size_t basesize;
} bo_t;
typedef struct bs_s typedef struct bs_s
{ {
uint8_t *p_start; uint8_t *p_start;
...@@ -194,4 +205,132 @@ static inline void bs_align_1( bs_t *s ) ...@@ -194,4 +205,132 @@ static inline void bs_align_1( bs_t *s )
} }
} }
static inline void bo_init(bo_t *p_bo, int i_size)
{
p_bo->b = block_Alloc(i_size);
p_bo->b->i_buffer = 0;
p_bo->len = p_bo->basesize = i_size;
}
static inline void bo_set_8(bo_t *p_bo, size_t i_offset, uint8_t i)
{
if (i_offset >= p_bo->len)
{
int i_growth = p_bo->basesize;
while(i_offset >= p_bo->len + i_growth)
i_growth += p_bo->basesize;
int i = p_bo->b->i_buffer; /* Realloc would set payload size == buffer size */
p_bo->b = block_Realloc(p_bo->b, 0, p_bo->len + i_growth);
if (!p_bo->b)
return;
p_bo->b->i_buffer = i;
p_bo->len += i_growth;
}
p_bo->b->p_buffer[i_offset] = i;
}
static inline void bo_add_8(bo_t *p_bo, uint8_t i)
{
bo_set_8( p_bo, p_bo->b->i_buffer, i );
p_bo->b->i_buffer++;
}
static inline void bo_add_16be(bo_t *p_bo, uint16_t i)
{
bo_add_8(p_bo, ((i >> 8) &0xff));
bo_add_8(p_bo, i &0xff);
}
static inline void bo_add_16le(bo_t *p_bo, uint16_t i)
{
bo_add_8(p_bo, i &0xff);
bo_add_8(p_bo, ((i >> 8) &0xff));
}
static inline void bo_set_16be(bo_t *p_bo, int i_offset, uint16_t i)
{
bo_set_8(p_bo, i_offset + 1, ((i >> 8) &0xff));
bo_set_8(p_bo, i_offset, i &0xff);
}
static inline void bo_set_16le(bo_t *p_bo, int i_offset, uint16_t i)
{
bo_set_8(p_bo, i_offset, i &0xff);
bo_set_8(p_bo, i_offset + 1, ((i >> 8) &0xff));
}
static inline void bo_add_24be(bo_t *p_bo, uint32_t i)
{
bo_add_8(p_bo, ((i >> 16) &0xff));
bo_add_8(p_bo, ((i >> 8) &0xff));
bo_add_8(p_bo, (i &0xff));
}
static inline void bo_add_32be(bo_t *p_bo, uint32_t i)
{
bo_add_16be(p_bo, ((i >> 16) &0xffff));
bo_add_16be(p_bo, i &0xffff);
}
static inline void bo_add_32le(bo_t *p_bo, uint32_t i)
{
bo_add_16le(p_bo, i &0xffff);
bo_add_16le(p_bo, ((i >> 16) &0xffff));
}
static inline void bo_set_32be(bo_t *p_bo, int i_offset, uint32_t i)
{
bo_set_16be(p_bo, i_offset + 2, ((i >> 16) &0xffff));
bo_set_16be(p_bo, i_offset, i &0xffff);
}
static inline void bo_set_32le(bo_t *p_bo, int i_offset, uint32_t i)
{
bo_set_16le(p_bo, i_offset, i &0xffff);
bo_set_16le(p_bo, i_offset + 2, ((i >> 16) &0xffff));
}
static inline void bo_swap_32be (bo_t *p_bo, int i_pos, uint32_t i)
{
p_bo->b->p_buffer[i_pos ] = (i >> 24)&0xff;
p_bo->b->p_buffer[i_pos + 1] = (i >> 16)&0xff;
p_bo->b->p_buffer[i_pos + 2] = (i >> 8)&0xff;
p_bo->b->p_buffer[i_pos + 3] = (i )&0xff;
}
static inline void bo_add_64be(bo_t *p_bo, uint64_t i)
{
bo_add_32be(p_bo, ((i >> 32) &0xffffffff));
bo_add_32be(p_bo, i &0xffffffff);
}
static inline void bo_add_64le(bo_t *p_bo, uint64_t i)
{
bo_add_32le(p_bo, i &0xffffffff);
bo_add_32le(p_bo, ((i >> 32) &0xffffffff));
}
static inline void bo_add_fourcc(bo_t *p_bo, const char *fcc)
{
bo_add_8(p_bo, fcc[0]);
bo_add_8(p_bo, fcc[1]);
bo_add_8(p_bo, fcc[2]);
bo_add_8(p_bo, fcc[3]);
}
static inline void bo_add_mem(bo_t *p_bo, int i_size, const uint8_t *p_mem)
{
for (int i = 0; i < i_size; i++)
bo_add_8(p_bo, p_mem[i]);
}
static inline void bo_add_mp4_tag_descr(bo_t *p_bo, uint8_t tag, uint32_t size)
{
bo_add_8(p_bo, tag);
for (int i = 3; i>0; i--)
bo_add_8(p_bo, (size>>(7*i)) | 0x80);
bo_add_8(p_bo, size & 0x7F);
}
#endif #endif
...@@ -36,6 +36,7 @@ ...@@ -36,6 +36,7 @@
#include <vlc_sout.h> #include <vlc_sout.h>
#include <vlc_block.h> #include <vlc_block.h>
#include <vlc_codecs.h> #include <vlc_codecs.h>
#include <vlc_bits.h>
/***************************************************************************** /*****************************************************************************
* Module descriptor * Module descriptor
...@@ -571,106 +572,6 @@ static int Mux ( sout_mux_t *p_mux ) ...@@ -571,106 +572,6 @@ static int Mux ( sout_mux_t *p_mux )
return( 0 ); return( 0 );
} }
/****************************************************************************/
/****************************************************************************/
/****************************************************************************/
/****************************************************************************/
typedef struct buffer_out_s
{
int i_buffer_size;
block_t *p_block;
} buffer_out_t;
static void bo_Init( buffer_out_t *p_bo, int i_size )
{
p_bo->p_block = block_Alloc( i_size );
p_bo->p_block->i_buffer = 0;
p_bo->i_buffer_size = i_size;
}
static void bo_SetByte( buffer_out_t *p_bo, int i_offset, uint8_t i )
{
if( i_offset >= p_bo->i_buffer_size )
{
int i_growth = HDR_BASE_SIZE;
while( i_offset >= p_bo->i_buffer_size + i_growth )
{
i_growth += HDR_BASE_SIZE;
}
int i = p_bo->p_block->i_buffer; /* Realloc would set payload size == buffer size */
p_bo->p_block = block_Realloc( p_bo->p_block, 0, p_bo->i_buffer_size + i_growth );
p_bo->p_block->i_buffer = i;
p_bo->i_buffer_size += i_growth;
}
p_bo->p_block->p_buffer[i_offset] = i;
}
static void bo_AddByte( buffer_out_t *p_bo, uint8_t i )
{
bo_SetByte( p_bo, p_bo->p_block->i_buffer, i );
p_bo->p_block->i_buffer++;
}
static void bo_SetWordLE( buffer_out_t *p_bo, int i_offset, uint16_t i )
{
bo_SetByte( p_bo, i_offset, i &0xff );
bo_SetByte( p_bo, i_offset + 1, ( ( i >> 8) &0xff ) );
}
static void bo_AddWordLE( buffer_out_t *p_bo, uint16_t i )
{
bo_AddByte( p_bo, i &0xff );
bo_AddByte( p_bo, ( ( i >> 8) &0xff ) );
}
static void bo_AddWordBE( buffer_out_t *p_bo, uint16_t i )
{
bo_AddByte( p_bo, ( ( i >> 8) &0xff ) );
bo_AddByte( p_bo, i &0xff );
}
static void bo_SetDWordLE( buffer_out_t *p_bo, int i_offset, uint32_t i )
{
bo_SetWordLE( p_bo, i_offset, i &0xffff );
bo_SetWordLE( p_bo, i_offset + 2, ( ( i >> 16) &0xffff ) );
}
static void bo_AddDWordLE( buffer_out_t *p_bo, uint32_t i )
{
bo_AddWordLE( p_bo, i &0xffff );
bo_AddWordLE( p_bo, ( ( i >> 16) &0xffff ) );
}
static void bo_AddDWordBE( buffer_out_t *p_bo, uint32_t i )
{
bo_AddWordBE( p_bo, ( ( i >> 16) &0xffff ) );
bo_AddWordBE( p_bo, i &0xffff );
}
#if 0
static void bo_AddLWordLE( buffer_out_t *p_bo, uint64_t i )
{
bo_AddDWordLE( p_bo, i &0xffffffff );
bo_AddDWordLE( p_bo, ( ( i >> 32) &0xffffffff ) );
}
static void bo_AddLWordBE( buffer_out_t *p_bo, uint64_t i )
{
bo_AddDWordBE( p_bo, ( ( i >> 32) &0xffffffff ) );
bo_AddDWordBE( p_bo, i &0xffffffff );
}
#endif
static void bo_AddFCC( buffer_out_t *p_bo, const char *fcc )
{
bo_AddByte( p_bo, fcc[0] );
bo_AddByte( p_bo, fcc[1] );
bo_AddByte( p_bo, fcc[2] );
bo_AddByte( p_bo, fcc[3] );
}
static void bo_AddMem( buffer_out_t *p_bo, int i_size, uint8_t *p_mem )
{
int i;
for( i = 0; i < i_size; i++ )
{
bo_AddByte( p_bo, p_mem[i] );
}
}
/**************************************************************************** /****************************************************************************
**************************************************************************** ****************************************************************************
** **
...@@ -680,21 +581,21 @@ static void bo_AddMem( buffer_out_t *p_bo, int i_size, uint8_t *p_mem ) ...@@ -680,21 +581,21 @@ static void bo_AddMem( buffer_out_t *p_bo, int i_size, uint8_t *p_mem )
****************************************************************************/ ****************************************************************************/
#define AVI_BOX_ENTER( fcc ) \ #define AVI_BOX_ENTER( fcc ) \
int i_datasize_offset; \ int i_datasize_offset; \
bo_AddFCC( p_bo, fcc ); \ bo_add_fourcc( p_bo, fcc ); \
i_datasize_offset = p_bo->p_block->i_buffer; \ i_datasize_offset = p_bo->b->i_buffer; \
bo_AddDWordLE( p_bo, 0 ) bo_add_32le( p_bo, 0 )
#define AVI_BOX_ENTER_LIST( fcc ) \ #define AVI_BOX_ENTER_LIST( fcc ) \
AVI_BOX_ENTER( "LIST" ); \ AVI_BOX_ENTER( "LIST" ); \
bo_AddFCC( p_bo, fcc ) bo_add_fourcc( p_bo, fcc )
#define AVI_BOX_EXIT( i_err ) \ #define AVI_BOX_EXIT( i_err ) \
if( p_bo->p_block->i_buffer&0x01 ) bo_AddByte( p_bo, 0 ); \ if( p_bo->b->i_buffer&0x01 ) bo_add_8( p_bo, 0 ); \
bo_SetDWordLE( p_bo, i_datasize_offset, p_bo->p_block->i_buffer - i_datasize_offset - 4 ); \ bo_set_32le( p_bo, i_datasize_offset, p_bo->b->i_buffer - i_datasize_offset - 4 ); \
return( i_err ); return( i_err );
static int avi_HeaderAdd_avih( sout_mux_t *p_mux, static int avi_HeaderAdd_avih( sout_mux_t *p_mux,
buffer_out_t *p_bo ) bo_t *p_bo )
{ {
sout_mux_sys_t *p_sys = p_mux->p_sys; sout_mux_sys_t *p_sys = p_mux->p_sys;
avi_stream_t *p_video = NULL; avi_stream_t *p_video = NULL;
...@@ -737,34 +638,34 @@ static int avi_HeaderAdd_avih( sout_mux_t *p_mux, ...@@ -737,34 +638,34 @@ static int avi_HeaderAdd_avih( sout_mux_t *p_mux,
} }
} }
bo_AddDWordLE( p_bo, i_microsecperframe ); bo_add_32le( p_bo, i_microsecperframe );
bo_AddDWordLE( p_bo, i_maxbytespersec ); bo_add_32le( p_bo, i_maxbytespersec );
bo_AddDWordLE( p_bo, 0 ); /* padding */ bo_add_32le( p_bo, 0 ); /* padding */
bo_AddDWordLE( p_bo, AVIF_TRUSTCKTYPE | bo_add_32le( p_bo, AVIF_TRUSTCKTYPE |
AVIF_HASINDEX | AVIF_HASINDEX |
AVIF_ISINTERLEAVED ); /* flags */ AVIF_ISINTERLEAVED ); /* flags */
bo_AddDWordLE( p_bo, i_totalframes ); bo_add_32le( p_bo, i_totalframes );
bo_AddDWordLE( p_bo, 0 ); /* initial frame */ bo_add_32le( p_bo, 0 ); /* initial frame */
bo_AddDWordLE( p_bo, p_sys->i_streams ); /* streams count */ bo_add_32le( p_bo, p_sys->i_streams ); /* streams count */
bo_AddDWordLE( p_bo, 1024 * 1024 ); /* suggested buffer size */ bo_add_32le( p_bo, 1024 * 1024 ); /* suggested buffer size */
if( p_video ) if( p_video )
{ {
bo_AddDWordLE( p_bo, p_video->p_bih->biWidth ); bo_add_32le( p_bo, p_video->p_bih->biWidth );
bo_AddDWordLE( p_bo, p_video->p_bih->biHeight ); bo_add_32le( p_bo, p_video->p_bih->biHeight );
} }
else else
{ {
bo_AddDWordLE( p_bo, 0 ); bo_add_32le( p_bo, 0 );
bo_AddDWordLE( p_bo, 0 ); bo_add_32le( p_bo, 0 );
} }
bo_AddDWordLE( p_bo, 0 ); /* ???? */ bo_add_32le( p_bo, 0 ); /* ???? */
bo_AddDWordLE( p_bo, 0 ); /* ???? */ bo_add_32le( p_bo, 0 ); /* ???? */
bo_AddDWordLE( p_bo, 0 ); /* ???? */ bo_add_32le( p_bo, 0 ); /* ???? */
bo_AddDWordLE( p_bo, 0 ); /* ???? */ bo_add_32le( p_bo, 0 ); /* ???? */
AVI_BOX_EXIT( 0 ); AVI_BOX_EXIT( 0 );
} }
static int avi_HeaderAdd_strh( buffer_out_t *p_bo, avi_stream_t *p_stream ) static int avi_HeaderAdd_strh( bo_t *p_bo, avi_stream_t *p_stream )
{ {
AVI_BOX_ENTER( "strh" ); AVI_BOX_ENTER( "strh" );
...@@ -772,23 +673,23 @@ static int avi_HeaderAdd_strh( buffer_out_t *p_bo, avi_stream_t *p_stream ) ...@@ -772,23 +673,23 @@ static int avi_HeaderAdd_strh( buffer_out_t *p_bo, avi_stream_t *p_stream )
{ {
case VIDEO_ES: case VIDEO_ES:
{ {
bo_AddFCC( p_bo, "vids" ); bo_add_fourcc( p_bo, "vids" );
bo_AddDWordBE( p_bo, p_stream->p_bih->biCompression ); bo_add_32be( p_bo, p_stream->p_bih->biCompression );
bo_AddDWordLE( p_bo, 0 ); /* flags */ bo_add_32le( p_bo, 0 ); /* flags */
bo_AddWordLE( p_bo, 0 ); /* priority */ bo_add_16le( p_bo, 0 ); /* priority */
bo_AddWordLE( p_bo, 0 ); /* langage */ bo_add_16le( p_bo, 0 ); /* langage */
bo_AddDWordLE( p_bo, 0 ); /* initial frame */ bo_add_32le( p_bo, 0 ); /* initial frame */
bo_AddDWordLE( p_bo, 1000 );/* scale */ bo_add_32le( p_bo, 1000 );/* scale */
bo_AddDWordLE( p_bo, (uint32_t)( 1000 * p_stream->f_fps )); bo_add_32le( p_bo, (uint32_t)( 1000 * p_stream->f_fps ));
bo_AddDWordLE( p_bo, 0 ); /* start */ bo_add_32le( p_bo, 0 ); /* start */
bo_AddDWordLE( p_bo, p_stream->i_frames ); bo_add_32le( p_bo, p_stream->i_frames );
bo_AddDWordLE( p_bo, 1024 * 1024 ); bo_add_32le( p_bo, 1024 * 1024 );
bo_AddDWordLE( p_bo, -1 ); /* quality */ bo_add_32le( p_bo, -1 ); /* quality */
bo_AddDWordLE( p_bo, 0 ); /* samplesize */ bo_add_32le( p_bo, 0 ); /* samplesize */
bo_AddWordLE( p_bo, 0 ); /* ??? */ bo_add_16le( p_bo, 0 ); /* ??? */
bo_AddWordLE( p_bo, 0 ); /* ??? */ bo_add_16le( p_bo, 0 ); /* ??? */
bo_AddWordLE( p_bo, p_stream->p_bih->biWidth ); bo_add_16le( p_bo, p_stream->p_bih->biWidth );
bo_AddWordLE( p_bo, p_stream->p_bih->biHeight ); bo_add_16le( p_bo, p_stream->p_bih->biHeight );
} }
break; break;
case AUDIO_ES: case AUDIO_ES:
...@@ -807,23 +708,23 @@ static int avi_HeaderAdd_strh( buffer_out_t *p_bo, avi_stream_t *p_stream ) ...@@ -807,23 +708,23 @@ static int avi_HeaderAdd_strh( buffer_out_t *p_bo, avi_stream_t *p_stream )
i_scale = 1000; i_scale = 1000;
i_rate = 1000 * p_stream->i_bitrate / 8; i_rate = 1000 * p_stream->i_bitrate / 8;
} }
bo_AddFCC( p_bo, "auds" ); bo_add_fourcc( p_bo, "auds" );
bo_AddDWordLE( p_bo, 0 ); /* tag */ bo_add_32le( p_bo, 0 ); /* tag */
bo_AddDWordLE( p_bo, 0 ); /* flags */ bo_add_32le( p_bo, 0 ); /* flags */
bo_AddWordLE( p_bo, 0 ); /* priority */ bo_add_16le( p_bo, 0 ); /* priority */
bo_AddWordLE( p_bo, 0 ); /* langage */ bo_add_16le( p_bo, 0 ); /* langage */
bo_AddDWordLE( p_bo, 0 ); /* initial frame */ bo_add_32le( p_bo, 0 ); /* initial frame */
bo_AddDWordLE( p_bo, i_scale );/* scale */ bo_add_32le( p_bo, i_scale );/* scale */
bo_AddDWordLE( p_bo, i_rate ); bo_add_32le( p_bo, i_rate );
bo_AddDWordLE( p_bo, 0 ); /* start */ bo_add_32le( p_bo, 0 ); /* start */
bo_AddDWordLE( p_bo, p_stream->i_frames ); bo_add_32le( p_bo, p_stream->i_frames );
bo_AddDWordLE( p_bo, 10 * 1024 ); bo_add_32le( p_bo, 10 * 1024 );
bo_AddDWordLE( p_bo, -1 ); /* quality */ bo_add_32le( p_bo, -1 ); /* quality */
bo_AddDWordLE( p_bo, i_samplesize ); bo_add_32le( p_bo, i_samplesize );
bo_AddWordLE( p_bo, 0 ); /* ??? */ bo_add_16le( p_bo, 0 ); /* ??? */
bo_AddWordLE( p_bo, 0 ); /* ??? */ bo_add_16le( p_bo, 0 ); /* ??? */
bo_AddWordLE( p_bo, 0 ); bo_add_16le( p_bo, 0 );
bo_AddWordLE( p_bo, 0 ); bo_add_16le( p_bo, 0 );
} }
break; break;
} }
...@@ -831,42 +732,42 @@ static int avi_HeaderAdd_strh( buffer_out_t *p_bo, avi_stream_t *p_stream ) ...@@ -831,42 +732,42 @@ static int avi_HeaderAdd_strh( buffer_out_t *p_bo, avi_stream_t *p_stream )
AVI_BOX_EXIT( 0 ); AVI_BOX_EXIT( 0 );
} }
static int avi_HeaderAdd_strf( buffer_out_t *p_bo, avi_stream_t *p_stream ) static int avi_HeaderAdd_strf( bo_t *p_bo, avi_stream_t *p_stream )
{ {
AVI_BOX_ENTER( "strf" ); AVI_BOX_ENTER( "strf" );
switch( p_stream->i_cat ) switch( p_stream->i_cat )
{ {
case AUDIO_ES: case AUDIO_ES:
bo_AddWordLE( p_bo, p_stream->p_wf->wFormatTag ); bo_add_16le( p_bo, p_stream->p_wf->wFormatTag );
bo_AddWordLE( p_bo, p_stream->p_wf->nChannels ); bo_add_16le( p_bo, p_stream->p_wf->nChannels );
bo_AddDWordLE( p_bo, p_stream->p_wf->nSamplesPerSec ); bo_add_32le( p_bo, p_stream->p_wf->nSamplesPerSec );
bo_AddDWordLE( p_bo, p_stream->p_wf->nAvgBytesPerSec ); bo_add_32le( p_bo, p_stream->p_wf->nAvgBytesPerSec );
bo_AddWordLE( p_bo, p_stream->p_wf->nBlockAlign ); bo_add_16le( p_bo, p_stream->p_wf->nBlockAlign );
bo_AddWordLE( p_bo, p_stream->p_wf->wBitsPerSample ); bo_add_16le( p_bo, p_stream->p_wf->wBitsPerSample );
bo_AddWordLE( p_bo, p_stream->p_wf->cbSize ); bo_add_16le( p_bo, p_stream->p_wf->cbSize );
bo_AddMem( p_bo, p_stream->p_wf->cbSize, (uint8_t*)&p_stream->p_wf[1] ); bo_add_mem( p_bo, p_stream->p_wf->cbSize, (uint8_t*)&p_stream->p_wf[1] );
break; break;
case VIDEO_ES: case VIDEO_ES:
bo_AddDWordLE( p_bo, p_stream->p_bih->biSize ); bo_add_32le( p_bo, p_stream->p_bih->biSize );
bo_AddDWordLE( p_bo, p_stream->p_bih->biWidth ); bo_add_32le( p_bo, p_stream->p_bih->biWidth );
bo_AddDWordLE( p_bo, p_stream->p_bih->biHeight ); bo_add_32le( p_bo, p_stream->p_bih->biHeight );
bo_AddWordLE( p_bo, p_stream->p_bih->biPlanes ); bo_add_16le( p_bo, p_stream->p_bih->biPlanes );
bo_AddWordLE( p_bo, p_stream->p_bih->biBitCount ); bo_add_16le( p_bo, p_stream->p_bih->biBitCount );
if( VLC_FOURCC( 0, 0, 0, 1 ) == 0x00000001 ) if( VLC_FOURCC( 0, 0, 0, 1 ) == 0x00000001 )
{ {
bo_AddDWordBE( p_bo, p_stream->p_bih->biCompression ); bo_add_32be( p_bo, p_stream->p_bih->biCompression );
} }
else else
{ {
bo_AddDWordLE( p_bo, p_stream->p_bih->biCompression ); bo_add_32le( p_bo, p_stream->p_bih->biCompression );
} }
bo_AddDWordLE( p_bo, p_stream->p_bih->biSizeImage ); bo_add_32le( p_bo, p_stream->p_bih->biSizeImage );
bo_AddDWordLE( p_bo, p_stream->p_bih->biXPelsPerMeter ); bo_add_32le( p_bo, p_stream->p_bih->biXPelsPerMeter );
bo_AddDWordLE( p_bo, p_stream->p_bih->biYPelsPerMeter ); bo_add_32le( p_bo, p_stream->p_bih->biYPelsPerMeter );
bo_AddDWordLE( p_bo, p_stream->p_bih->biClrUsed ); bo_add_32le( p_bo, p_stream->p_bih->biClrUsed );
bo_AddDWordLE( p_bo, p_stream->p_bih->biClrImportant ); bo_add_32le( p_bo, p_stream->p_bih->biClrImportant );
bo_AddMem( p_bo, bo_add_mem( p_bo,
p_stream->p_bih->biSize - sizeof( VLC_BITMAPINFOHEADER ), p_stream->p_bih->biSize - sizeof( VLC_BITMAPINFOHEADER ),
(uint8_t*)&p_stream->p_bih[1] ); (uint8_t*)&p_stream->p_bih[1] );
break; break;
...@@ -875,7 +776,7 @@ static int avi_HeaderAdd_strf( buffer_out_t *p_bo, avi_stream_t *p_stream ) ...@@ -875,7 +776,7 @@ static int avi_HeaderAdd_strf( buffer_out_t *p_bo, avi_stream_t *p_stream )
AVI_BOX_EXIT( 0 ); AVI_BOX_EXIT( 0 );
} }
static int avi_HeaderAdd_strl( buffer_out_t *p_bo, avi_stream_t *p_stream ) static int avi_HeaderAdd_strl( bo_t *p_bo, avi_stream_t *p_stream )
{ {
AVI_BOX_ENTER_LIST( "strl" ); AVI_BOX_ENTER_LIST( "strl" );
...@@ -885,18 +786,18 @@ static int avi_HeaderAdd_strl( buffer_out_t *p_bo, avi_stream_t *p_stream ) ...@@ -885,18 +786,18 @@ static int avi_HeaderAdd_strl( buffer_out_t *p_bo, avi_stream_t *p_stream )
AVI_BOX_EXIT( 0 ); AVI_BOX_EXIT( 0 );
} }
static int avi_HeaderAdd_meta( buffer_out_t *p_bo, const char psz_meta[4], static int avi_HeaderAdd_meta( bo_t *p_bo, const char psz_meta[4],
const char *psz_data ) const char *psz_data )
{ {
if ( psz_data == NULL ) return 1; if ( psz_data == NULL ) return 1;
const char *psz = psz_data; const char *psz = psz_data;
AVI_BOX_ENTER( psz_meta ); AVI_BOX_ENTER( psz_meta );
while (*psz) bo_AddByte( p_bo, *psz++ ); while (*psz) bo_add_8( p_bo, *psz++ );
bo_AddByte( p_bo, 0 ); bo_add_8( p_bo, 0 );
AVI_BOX_EXIT( 0 ); AVI_BOX_EXIT( 0 );
} }
static int avi_HeaderAdd_INFO( sout_mux_t *p_mux, buffer_out_t *p_bo ) static int avi_HeaderAdd_INFO( sout_mux_t *p_mux, bo_t *p_bo )
{ {
char *psz; char *psz;
...@@ -931,7 +832,7 @@ static block_t *avi_HeaderCreateRIFF( sout_mux_t *p_mux ) ...@@ -931,7 +832,7 @@ static block_t *avi_HeaderCreateRIFF( sout_mux_t *p_mux )
sout_mux_sys_t *p_sys = p_mux->p_sys; sout_mux_sys_t *p_sys = p_mux->p_sys;
int i_stream; int i_stream;
int i_junk; int i_junk;
buffer_out_t bo; bo_t bo;
struct struct
{ {
...@@ -940,23 +841,23 @@ static block_t *avi_HeaderCreateRIFF( sout_mux_t *p_mux ) ...@@ -940,23 +841,23 @@ static block_t *avi_HeaderCreateRIFF( sout_mux_t *p_mux )
int i_hdrldatastart; int i_hdrldatastart;
} offsets; } offsets;
bo_Init( &bo, HDR_BASE_SIZE ); bo_init( &bo, HDR_BASE_SIZE );
bo_AddFCC( &bo, "RIFF" ); bo_add_fourcc( &bo, "RIFF" );
offsets.i_riffsize = bo.p_block->i_buffer; offsets.i_riffsize = bo.b->i_buffer;
bo_AddDWordLE( &bo, 0xEFBEADDE ); bo_add_32le( &bo, 0xEFBEADDE );
bo_AddFCC( &bo, "AVI " ); bo_add_fourcc( &bo, "AVI " );
bo_AddFCC( &bo, "LIST" ); bo_add_fourcc( &bo, "LIST" );
/* HDRL List size should exclude following data in HDR buffer /* HDRL List size should exclude following data in HDR buffer
* -12 (RIFF, RIFF size, 'AVI ' tag), * -12 (RIFF, RIFF size, 'AVI ' tag),
* - 8 (hdr1 LIST tag and its size) * - 8 (hdr1 LIST tag and its size)
* - 12 (movi LIST tag, size, 'movi' listType ) * - 12 (movi LIST tag, size, 'movi' listType )
*/ */
offsets.i_hdrllistsize = bo.p_block->i_buffer; offsets.i_hdrllistsize = bo.b->i_buffer;
bo_AddDWordLE( &bo, 0xEFBEADDE ); bo_add_32le( &bo, 0xEFBEADDE );
bo_AddFCC( &bo, "hdrl" ); bo_add_fourcc( &bo, "hdrl" );
offsets.i_hdrldatastart = bo.p_block->i_buffer; offsets.i_hdrldatastart = bo.b->i_buffer;
avi_HeaderAdd_avih( p_mux, &bo ); avi_HeaderAdd_avih( p_mux, &bo );
for( i_stream = 0; i_stream < p_sys->i_streams; i_stream++ ) for( i_stream = 0; i_stream < p_sys->i_streams; i_stream++ )
...@@ -965,53 +866,53 @@ static block_t *avi_HeaderCreateRIFF( sout_mux_t *p_mux ) ...@@ -965,53 +866,53 @@ static block_t *avi_HeaderCreateRIFF( sout_mux_t *p_mux )
} }
/* align on 16 bytes */ /* align on 16 bytes */
int i_align = ( ( bo.p_block->i_buffer + 12 + 0xE ) & ~ 0xF ); int i_align = ( ( bo.b->i_buffer + 12 + 0xE ) & ~ 0xF );
i_junk = i_align - bo.p_block->i_buffer; i_junk = i_align - bo.b->i_buffer;
bo_AddFCC( &bo, "JUNK" ); bo_add_fourcc( &bo, "JUNK" );
bo_AddDWordLE( &bo, i_junk ); bo_add_32le( &bo, i_junk );
for( int i=0; i< i_junk; i++ ) for( int i=0; i< i_junk; i++ )
{ {
bo_AddByte( &bo, 0 ); bo_add_8( &bo, 0 );
} }
/* Now set hdrl size */ /* Now set hdrl size */
bo_SetDWordLE( &bo, offsets.i_hdrllistsize, bo_set_32le( &bo, offsets.i_hdrllistsize,
bo.p_block->i_buffer - offsets.i_hdrldatastart ); bo.b->i_buffer - offsets.i_hdrldatastart );
avi_HeaderAdd_INFO( p_mux, &bo ); avi_HeaderAdd_INFO( p_mux, &bo );
bo_AddFCC( &bo, "LIST" ); bo_add_fourcc( &bo, "LIST" );
bo_AddDWordLE( &bo, p_sys->i_movi_size + 4 ); bo_add_32le( &bo, p_sys->i_movi_size + 4 );
bo_AddFCC( &bo, "movi" ); bo_add_fourcc( &bo, "movi" );
/* Now set RIFF size */ /* Now set RIFF size */
bo_SetDWordLE( &bo, offsets.i_riffsize, bo.p_block->i_buffer - 8 bo_set_32le( &bo, offsets.i_riffsize, bo.b->i_buffer - 8
+ p_sys->i_movi_size + p_sys->i_idx1_size ); + p_sys->i_movi_size + p_sys->i_idx1_size );
return( bo.p_block ); return( bo.b );
} }
static block_t * avi_HeaderCreateidx1( sout_mux_t *p_mux ) static block_t * avi_HeaderCreateidx1( sout_mux_t *p_mux )
{ {
sout_mux_sys_t *p_sys = p_mux->p_sys; sout_mux_sys_t *p_sys = p_mux->p_sys;
uint32_t i_idx1_size; uint32_t i_idx1_size;
buffer_out_t bo; bo_t bo;
i_idx1_size = 16 * p_sys->idx1.i_entry_count + 8; i_idx1_size = 16 * p_sys->idx1.i_entry_count + 8;
bo_Init( &bo, i_idx1_size ); bo_init( &bo, i_idx1_size );
memset( bo.p_block->p_buffer, 0, i_idx1_size); memset( bo.b->p_buffer, 0, i_idx1_size);
bo_AddFCC( &bo, "idx1" ); bo_add_fourcc( &bo, "idx1" );
bo_AddDWordLE( &bo, i_idx1_size - 8); bo_add_32le( &bo, i_idx1_size - 8);
for( unsigned i = 0; i < p_sys->idx1.i_entry_count; i++ ) for( unsigned i = 0; i < p_sys->idx1.i_entry_count; i++ )
{ {
bo_AddFCC( &bo, p_sys->idx1.entry[i].fcc ); bo_add_fourcc( &bo, p_sys->idx1.entry[i].fcc );
bo_AddDWordLE( &bo, p_sys->idx1.entry[i].i_flags ); bo_add_32le( &bo, p_sys->idx1.entry[i].i_flags );
bo_AddDWordLE( &bo, p_sys->idx1.entry[i].i_pos ); bo_add_32le( &bo, p_sys->idx1.entry[i].i_pos );
bo_AddDWordLE( &bo, p_sys->idx1.entry[i].i_length ); bo_add_32le( &bo, p_sys->idx1.entry[i].i_length );
} }
return( bo.p_block ); return( bo.b );
} }
...@@ -35,6 +35,8 @@ ...@@ -35,6 +35,8 @@
#include <vlc_sout.h> #include <vlc_sout.h>
#include <vlc_block.h> #include <vlc_block.h>
#include <vlc_bits.h>
#include <time.h> #include <time.h>
#include <vlc_iso_lang.h> #include <vlc_iso_lang.h>
...@@ -201,24 +203,6 @@ struct sout_mux_sys_t ...@@ -201,24 +203,6 @@ struct sout_mux_sys_t
uint32_t i_mfhd_sequence; uint32_t i_mfhd_sequence;
}; };
typedef struct bo_t
{
block_t *b;
size_t len;
} bo_t;
static void bo_init (bo_t *);
static void bo_add_8 (bo_t *, uint8_t);
static void bo_add_16be (bo_t *, uint16_t);
static void bo_add_24be (bo_t *, uint32_t);
static void bo_add_32be (bo_t *, uint32_t);
static void bo_add_64be (bo_t *, uint64_t);
static void bo_add_fourcc(bo_t *, const char *);
static void bo_add_mem (bo_t *, int , const uint8_t *);
static void bo_add_descr(bo_t *, uint8_t , uint32_t);
static void bo_fix_32be (bo_t *, int , uint32_t);
static bo_t *box_new (const char *fcc); static bo_t *box_new (const char *fcc);
static bo_t *box_full_new(const char *fcc, uint8_t v, uint32_t f); static bo_t *box_full_new(const char *fcc, uint8_t v, uint32_t f);
static void box_fix (bo_t *box); static void box_fix (bo_t *box);
...@@ -309,7 +293,7 @@ static void Close(vlc_object_t *p_this) ...@@ -309,7 +293,7 @@ static void Close(vlc_object_t *p_this)
/* Update mdat size */ /* Update mdat size */
bo_t bo; bo_t bo;
bo_init(&bo); bo_init(&bo, 1024);
if (p_sys->i_pos - p_sys->i_mdat_pos >= (((uint64_t)1)<<32)) { if (p_sys->i_pos - p_sys->i_mdat_pos >= (((uint64_t)1)<<32)) {
/* Extended size */ /* Extended size */
bo_add_32be (&bo, 1); bo_add_32be (&bo, 1);
...@@ -812,12 +796,12 @@ static bo_t *GetESDS(mp4_stream_t *p_stream) ...@@ -812,12 +796,12 @@ static bo_t *GetESDS(mp4_stream_t *p_stream)
esds = box_full_new("esds", 0, 0); esds = box_full_new("esds", 0, 0);
/* ES_Descr */ /* ES_Descr */
bo_add_descr(esds, 0x03, 3 + 5 + 13 + i_decoder_specific_info_size + 5 + 1); bo_add_mp4_tag_descr(esds, 0x03, 3 + 5 + 13 + i_decoder_specific_info_size + 5 + 1);
bo_add_16be(esds, p_stream->i_track_id); bo_add_16be(esds, p_stream->i_track_id);
bo_add_8 (esds, 0x1f); // flags=0|streamPriority=0x1f bo_add_8 (esds, 0x1f); // flags=0|streamPriority=0x1f
/* DecoderConfigDescr */ /* DecoderConfigDescr */
bo_add_descr(esds, 0x04, 13 + i_decoder_specific_info_size); bo_add_mp4_tag_descr(esds, 0x04, 13 + i_decoder_specific_info_size);
int i_object_type_indication; int i_object_type_indication;
switch(p_stream->fmt.i_codec) switch(p_stream->fmt.i_codec)
...@@ -855,14 +839,14 @@ static bo_t *GetESDS(mp4_stream_t *p_stream) ...@@ -855,14 +839,14 @@ static bo_t *GetESDS(mp4_stream_t *p_stream)
if (p_stream->fmt.i_extra > 0) { if (p_stream->fmt.i_extra > 0) {
/* DecoderSpecificInfo */ /* DecoderSpecificInfo */
bo_add_descr(esds, 0x05, p_stream->fmt.i_extra); bo_add_mp4_tag_descr(esds, 0x05, p_stream->fmt.i_extra);
for (int i = 0; i < p_stream->fmt.i_extra; i++) for (int i = 0; i < p_stream->fmt.i_extra; i++)
bo_add_8(esds, ((uint8_t*)p_stream->fmt.p_extra)[i]); bo_add_8(esds, ((uint8_t*)p_stream->fmt.p_extra)[i]);
} }
/* SL_Descr mandatory */ /* SL_Descr mandatory */
bo_add_descr(esds, 0x06, 1); bo_add_mp4_tag_descr(esds, 0x06, 1);
bo_add_8 (esds, 0x02); // sl_predefined bo_add_8 (esds, 0x02); // sl_predefined
return esds; return esds;
...@@ -1722,11 +1706,11 @@ static bo_t *GetStblBox(sout_mux_t *p_mux, mp4_stream_t *p_stream) ...@@ -1722,11 +1706,11 @@ static bo_t *GetStblBox(sout_mux_t *p_mux, mp4_stream_t *p_stream)
} }
/* Fix stco entry count */ /* Fix stco entry count */
bo_fix_32be(stco, 12, i_chunk); bo_swap_32be(stco, 12, i_chunk);
msg_Dbg(p_mux, "created %d chunks (stco)", i_chunk); msg_Dbg(p_mux, "created %d chunks (stco)", i_chunk);
/* Fix stsc entry count */ /* Fix stsc entry count */
bo_fix_32be(stsc, 12, i_stsc_entries ); bo_swap_32be(stsc, 12, i_stsc_entries );
/* add stts */ /* add stts */
bo_t *stts = box_full_new("stts", 0, 0); bo_t *stts = box_full_new("stts", 0, 0);
...@@ -1744,7 +1728,7 @@ static bo_t *GetStblBox(sout_mux_t *p_mux, mp4_stream_t *p_stream) ...@@ -1744,7 +1728,7 @@ static bo_t *GetStblBox(sout_mux_t *p_mux, mp4_stream_t *p_stream)
bo_add_32be(stts, i - i_first); // sample-count bo_add_32be(stts, i - i_first); // sample-count
bo_add_32be(stts, (uint64_t)i_delta * p_stream->i_timescale / CLOCK_FREQ); // sample-delta bo_add_32be(stts, (uint64_t)i_delta * p_stream->i_timescale / CLOCK_FREQ); // sample-delta
} }
bo_fix_32be(stts, 12, i_index); bo_swap_32be(stts, 12, i_index);
/* composition time handling */ /* composition time handling */
bo_t *ctts = NULL; bo_t *ctts = NULL;
...@@ -1764,7 +1748,7 @@ static bo_t *GetStblBox(sout_mux_t *p_mux, mp4_stream_t *p_stream) ...@@ -1764,7 +1748,7 @@ static bo_t *GetStblBox(sout_mux_t *p_mux, mp4_stream_t *p_stream)
bo_add_32be(ctts, i - i_first); // sample-count bo_add_32be(ctts, i - i_first); // sample-count
bo_add_32be(ctts, i_offset * p_stream->i_timescale / CLOCK_FREQ ); // sample-offset bo_add_32be(ctts, i_offset * p_stream->i_timescale / CLOCK_FREQ ); // sample-offset
} }
bo_fix_32be(ctts, 12, i_index); bo_swap_32be(ctts, 12, i_index);
} }
bo_t *stsz = box_full_new("stsz", 0, 0); bo_t *stsz = box_full_new("stsz", 0, 0);
...@@ -1815,7 +1799,7 @@ static bo_t *GetStblBox(sout_mux_t *p_mux, mp4_stream_t *p_stream) ...@@ -1815,7 +1799,7 @@ static bo_t *GetStblBox(sout_mux_t *p_mux, mp4_stream_t *p_stream)
} }
if (stss) if (stss)
bo_fix_32be(stss, 12, i_index); bo_swap_32be(stss, 12, i_index);
/* Now gather all boxes into stbl */ /* Now gather all boxes into stbl */
bo_t *stbl = box_new("stbl"); bo_t *stbl = box_new("stbl");
...@@ -2226,81 +2210,13 @@ static bo_t *GetMoovBox(sout_mux_t *p_mux) ...@@ -2226,81 +2210,13 @@ static bo_t *GetMoovBox(sout_mux_t *p_mux)
/****************************************************************************/ /****************************************************************************/
static void bo_init(bo_t *p_bo)
{
p_bo->len = 0;
p_bo->b = block_Alloc(1024);
}
static void bo_add_8(bo_t *p_bo, uint8_t i)
{
if (p_bo->len >= p_bo->b->i_buffer)
p_bo->b = block_Realloc(p_bo->b, 0, p_bo->b->i_buffer + 1024);
p_bo->b->p_buffer[p_bo->len++] = i;
}
static void bo_add_16be(bo_t *p_bo, uint16_t i)
{
bo_add_8(p_bo, ((i >> 8) &0xff));
bo_add_8(p_bo, i &0xff);
}
static void bo_add_24be(bo_t *p_bo, uint32_t i)
{
bo_add_8(p_bo, ((i >> 16) &0xff));
bo_add_8(p_bo, ((i >> 8) &0xff));
bo_add_8(p_bo, ( i &0xff));
}
static void bo_add_32be(bo_t *p_bo, uint32_t i)
{
bo_add_16be(p_bo, ((i >> 16) &0xffff));
bo_add_16be(p_bo, i &0xffff);
}
static void bo_fix_32be (bo_t *p_bo, int i_pos, uint32_t i)
{
p_bo->b->p_buffer[i_pos ] = (i >> 24)&0xff;
p_bo->b->p_buffer[i_pos + 1] = (i >> 16)&0xff;
p_bo->b->p_buffer[i_pos + 2] = (i >> 8)&0xff;
p_bo->b->p_buffer[i_pos + 3] = (i )&0xff;
}
static void bo_add_64be(bo_t *p_bo, uint64_t i)
{
bo_add_32be(p_bo, ((i >> 32) &0xffffffff));
bo_add_32be(p_bo, i &0xffffffff);
}
static void bo_add_fourcc(bo_t *p_bo, const char *fcc)
{
bo_add_8(p_bo, fcc[0]);
bo_add_8(p_bo, fcc[1]);
bo_add_8(p_bo, fcc[2]);
bo_add_8(p_bo, fcc[3]);
}
static void bo_add_mem(bo_t *p_bo, int i_size, const uint8_t *p_mem)
{
for (int i = 0; i < i_size; i++)
bo_add_8(p_bo, p_mem[i]);
}
static void bo_add_descr(bo_t *p_bo, uint8_t tag, uint32_t size)
{
bo_add_8(p_bo, tag);
for (int i = 3; i>0; i--)
bo_add_8(p_bo, (size>>(7*i)) | 0x80);
bo_add_8(p_bo, size & 0x7F);
}
static bo_t *box_new(const char *fcc) static bo_t *box_new(const char *fcc)
{ {
bo_t *box = malloc(sizeof(*box)); bo_t *box = malloc(sizeof(*box));
if (!box) if (!box)
return NULL; return NULL;
bo_init(box); bo_init(box, 1024);
bo_add_32be (box, 0); bo_add_32be (box, 0);
bo_add_fourcc(box, fcc); bo_add_fourcc(box, fcc);
......
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