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
This diff is collapsed.
...@@ -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