ForgedInitSegment.cpp 8.91 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 * ForgedInitSegment.cpp
 *****************************************************************************
 * Copyright (C) 2015 - 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.
 *****************************************************************************/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

24 25 26 27
#include "ForgedInitSegment.hpp"
#include "MemoryChunk.hpp"
#include "../adaptive/playlist/SegmentChunk.hpp"

28 29
#include <vlc_common.h>

30 31
#include <cstdlib>

32 33 34 35 36 37
extern "C"
{
    #include "../../mux/mp4/libmp4mux.h"
    #include "../../demux/mp4/libmp4.h" /* majors */
}

38
using namespace adaptive::playlist;
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
using namespace smooth::playlist;
using namespace smooth::http;

ForgedInitSegment::ForgedInitSegment(ICanonicalUrl *parent,
                                     const std::string &type_,
                                     uint64_t timescale_,
                                     uint64_t duration_) :
    InitSegment(parent), TimescaleAble()
{
    type = type_;
    duration.Set(duration_);
    extradata = NULL;
    i_extradata = 0;
    timescale.Set(timescale_);
    formatex.cbSize = 0;
    formatex.nAvgBytesPerSec = 0;
    formatex.nBlockAlign = 0;
    formatex.nChannels = 0;
    formatex.nSamplesPerSec = 0;
    formatex.wBitsPerSample = 0;
    formatex.wFormatTag = 0;
    width = height = 0;
    fourcc = 0;
    es_type = 0;
    track_id = 1;
}

ForgedInitSegment::~ForgedInitSegment()
{
    free(extradata);
}

static uint8_t *HexDecode(const std::string &s, size_t *decoded_size)
{
    *decoded_size = s.size() / 2;
74
    uint8_t *data = (uint8_t *) malloc(*decoded_size);
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
    if(data)
    {
        for(size_t i=0; i<*decoded_size; i++)
            data[i] = std::strtoul(s.substr(i*2, 2).c_str(), NULL, 16);
    }
    return data;
}

void ForgedInitSegment::fromWaveFormatEx(const uint8_t *p_data, size_t i_data)
{
    if(i_data >= sizeof(WAVEFORMATEX))
    {
        formatex.wFormatTag = GetWLE(p_data);
        wf_tag_to_fourcc(formatex.wFormatTag, &fourcc, NULL);
        formatex.nChannels = GetWLE(&p_data[2]);
        formatex.nSamplesPerSec = GetDWLE(&p_data[4]);
        formatex.nAvgBytesPerSec = GetDWLE(&p_data[8]);
        formatex.nBlockAlign = GetWLE(&p_data[12]);
        formatex.wBitsPerSample = GetWLE(&p_data[14]);
        formatex.cbSize = GetWLE(&p_data[16]);
        if(i_data > sizeof(WAVEFORMATEX))
        {
            if(extradata)
            {
                free(extradata);
                extradata = NULL;
                i_extradata = 0;
            }
            formatex.cbSize = __MIN(i_data - sizeof(WAVEFORMATEX), formatex.cbSize);
            extradata = (uint8_t*)malloc(formatex.cbSize);
            if(extradata)
            {
                memcpy(extradata, &p_data[sizeof(WAVEFORMATEX)], formatex.cbSize);
                i_extradata = formatex.cbSize;
            }
        }
        es_type = AUDIO_ES;
    }
}

void ForgedInitSegment::fromVideoInfoHeader(const uint8_t *, size_t)
{
//    VIDEOINFOHEADER
    es_type = VIDEO_ES;
}

void ForgedInitSegment::setWaveFormatEx(const std::string &waveformat)
{
    size_t i_data;
    uint8_t *p_data = HexDecode(waveformat, &i_data);
    fromWaveFormatEx(p_data, i_data);
126
    free(p_data);
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
}

void ForgedInitSegment::setCodecPrivateData(const std::string &extra)
{
    if(extradata)
    {
        free(extradata);
        extradata = NULL;
        i_extradata = 0;
    }
    extradata = HexDecode(extra, &i_extradata);
    if(fourcc == VLC_CODEC_WMAP)
    {
        //fromWaveFormatEx(const std::string &extra);
    }
}

void ForgedInitSegment::setChannels(uint16_t i)
{
    formatex.nChannels = i;
}

void ForgedInitSegment::setPacketSize(uint16_t i)
{
    formatex.nBlockAlign = i;
}

void ForgedInitSegment::setSamplingRate(uint32_t i)
{
    formatex.nSamplesPerSec = i;
}

void ForgedInitSegment::setBitsPerSample(uint16_t i)
{
    formatex.wBitsPerSample = i;
}

void ForgedInitSegment::setVideoSize(unsigned w, unsigned h)
{
    width = w;
    height = h;
}

void ForgedInitSegment::setTrackID(unsigned i)
{
    track_id = i;
}

void ForgedInitSegment::setAudioTag(uint16_t i)
{
    wf_tag_to_fourcc(i, &fourcc, NULL);
}

void ForgedInitSegment::setFourCC(const std::string &fcc)
{
    if(fcc.size() == 4)
    {
        fourcc = VLC_FOURCC(fcc[0], fcc[1], fcc[2], fcc[3]);
        switch(fourcc)
        {
            case VLC_FOURCC( 'A', 'V', 'C', '1' ):
            case VLC_FOURCC( 'A', 'V', 'C', 'B' ):
            case VLC_FOURCC( 'H', '2', '6', '4' ):
            case VLC_FOURCC( 'W', 'V', 'C', '1' ):
                es_type = VIDEO_ES;
                break;
            case VLC_FOURCC( 'A', 'A', 'C', 'L' ):
            case VLC_FOURCC( 'W', 'M', 'A', 'P' ):
            default:
                es_type = AUDIO_ES;
                break;
        }
    }
}

202 203 204 205 206
void ForgedInitSegment::setLanguage(const std::string &lang)
{
    language = lang;
}

207 208 209 210 211
block_t * ForgedInitSegment::buildMoovBox()
{
    mp4mux_trackinfo_t trackinfo;
    mp4mux_trackinfo_Init(&trackinfo);

212
    trackinfo.i_track_id = 0x01; /* Will always be 1st and unique track; tfhd patched on block read */
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
    trackinfo.i_timescale = timescale.Get();
    trackinfo.i_read_duration = duration.Get();
    trackinfo.i_trex_default_length = 1;
    trackinfo.i_trex_default_size = 1;

    trackinfo.fmt.i_cat = es_type;
    trackinfo.fmt.i_original_fourcc = fourcc;
    trackinfo.fmt.i_codec = vlc_fourcc_GetCodec(es_type, fourcc);
    switch(es_type)
    {
        case VIDEO_ES:
            if( fourcc == VLC_FOURCC( 'A', 'V', 'C', '1' ) ||
                fourcc == VLC_FOURCC( 'A', 'V', 'C', 'B' ) ||
                fourcc == VLC_FOURCC( 'H', '2', '6', '4' ) )
            {
                trackinfo.fmt.i_codec = VLC_CODEC_H264;
            }
            else if( fourcc == VLC_FOURCC( 'W', 'V', 'C', '1' ) )
            {
                trackinfo.fmt.i_codec = VLC_CODEC_VC1;
//                trackinfo.fmt.video.i_bits_per_pixel = 0x18; // No clue why this was set in smooth streamfilter
            }

            trackinfo.fmt.video.i_width = width;
            trackinfo.fmt.video.i_height = height;
            trackinfo.fmt.video.i_visible_width = width;
            trackinfo.fmt.video.i_visible_height = height;

            if(i_extradata && extradata)
            {
                trackinfo.fmt.p_extra = malloc(i_extradata);
                if(trackinfo.fmt.p_extra)
                {
                    memcpy(trackinfo.fmt.p_extra, extradata, i_extradata);
                    trackinfo.fmt.i_extra = i_extradata;
                }
            }
            break;

        case AUDIO_ES:
            trackinfo.fmt.audio.i_channels = formatex.nChannels;
            trackinfo.fmt.audio.i_rate = formatex.nSamplesPerSec;
            trackinfo.fmt.audio.i_bitspersample = formatex.wBitsPerSample;
            trackinfo.fmt.audio.i_blockalign = formatex.nBlockAlign;
            trackinfo.fmt.i_bitrate = formatex.nAvgBytesPerSec * 8; // FIXME (use bitrate) ?

            if(i_extradata && extradata)
            {
                trackinfo.fmt.p_extra = malloc(i_extradata);
                if(trackinfo.fmt.p_extra)
                {
                    memcpy(trackinfo.fmt.p_extra, extradata, i_extradata);
                    trackinfo.fmt.i_extra = i_extradata;
                }
            }
        default:
            break;
    }

272 273 274
    if(!language.empty())
        trackinfo.fmt.psz_language = strdup(language.c_str());

275 276 277 278 279 280 281 282 283 284 285
    mp4mux_trackinfo_t *p_tracks = &trackinfo;
    bo_t *box = GetMoovBox(NULL, &p_tracks, 1, true, false, false, false);
    mp4mux_trackinfo_Clear(&trackinfo);

    block_t *moov = NULL;
    if(box)
    {
        moov = box->b;
        free(box);
    }

286 287 288
    if(!moov)
        return NULL;

289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
    vlc_fourcc_t extra[] = {MAJOR_isom, VLC_FOURCC('p','i','f','f'), VLC_FOURCC('i','s','o','2'), VLC_FOURCC('s','m','o','o')};
    box = GetFtyp(VLC_FOURCC('i','s','m','l'), 1, extra, ARRAY_SIZE(extra));

    if(box)
    {
        block_ChainAppend(&box->b, moov);
        moov = block_ChainGather(box->b);
        free(box);
    }

    return moov;
}

SegmentChunk * ForgedInitSegment::getChunk(const std::string &, HTTPConnectionManager *)
{
    block_t *moov = buildMoovBox();
    if(moov)
    {
        MemoryChunkSource *source = new (std::nothrow) MemoryChunkSource(moov);
        return new (std::nothrow) SegmentChunk(this, source);
    }

    return NULL;
}