SDIStream.cpp 22.5 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
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/*****************************************************************************
 * SDIStream.cpp: SDI sout module for vlc
 *****************************************************************************
 * Copyright © 2018 VideoLabs, VideoLAN and 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

#include "SDIStream.hpp"
#include "sdiout.hpp"

#include <vlc_modules.h>
#include <vlc_meta.h>
#include <vlc_block.h>

#include <sstream>

using namespace sdi_sout;

AbstractStreamOutputBuffer::AbstractStreamOutputBuffer()
{
}

AbstractStreamOutputBuffer::~AbstractStreamOutputBuffer()
{
}

43
44
45
46
47
48
49
50
51
52
AbstractQueueStreamOutputBuffer::AbstractQueueStreamOutputBuffer()
{
    b_draining = false;
}

AbstractQueueStreamOutputBuffer::~AbstractQueueStreamOutputBuffer()
{

}

53
void AbstractQueueStreamOutputBuffer::Enqueue(void *p)
54
{
55
    buffer_mutex.lock();
56
    queued.push(p);
57
    buffer_mutex.unlock();
58
59
}

60
void *AbstractQueueStreamOutputBuffer::Dequeue()
61
62
{
    void *p = NULL;
63
    buffer_mutex.lock();
64
65
66
67
68
    if(!queued.empty())
    {
        p = queued.front();
        queued.pop();
    }
69
    buffer_mutex.unlock();
70
71
72
    return p;
}

73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
void AbstractQueueStreamOutputBuffer::Drain()
{
    buffer_mutex.lock();
    b_draining = true;
    buffer_mutex.unlock();
}

bool AbstractQueueStreamOutputBuffer::isEOS()
{
    buffer_mutex.lock();
    bool b = b_draining && queued.empty();
    buffer_mutex.unlock();
    return b;
}

88
BlockStreamOutputBuffer::BlockStreamOutputBuffer()
89
    : AbstractQueueStreamOutputBuffer()
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
{

}

BlockStreamOutputBuffer::~BlockStreamOutputBuffer()
{

}

void BlockStreamOutputBuffer::FlushQueued()
{
    block_t *p;
    while((p = reinterpret_cast<block_t *>(Dequeue())))
        block_Release(p);
}


PictureStreamOutputBuffer::PictureStreamOutputBuffer()
108
    : AbstractQueueStreamOutputBuffer()
109
{
110
    vlc_sem_init(&pool_semaphore, 16);
111
112
113
114
}

PictureStreamOutputBuffer::~PictureStreamOutputBuffer()
{
115
116
117
118
119
120
121
122
123
}

void * PictureStreamOutputBuffer::Dequeue()
{
    void *p = AbstractQueueStreamOutputBuffer::Dequeue();
    if(p)
        vlc_sem_post(&pool_semaphore);
    return p;
}
124

125
126
127
128
129
void PictureStreamOutputBuffer::Enqueue(void *p)
{
    if(p)
        vlc_sem_wait(&pool_semaphore);
    AbstractQueueStreamOutputBuffer::Enqueue(p);
130
131
132
133
134
135
136
137
138
}

void PictureStreamOutputBuffer::FlushQueued()
{
    picture_t *p;
    while((p = reinterpret_cast<picture_t *>(Dequeue())))
        picture_Release(p);
}

139
140
141
142
143
144
145
146
147
148
149
150
vlc_tick_t PictureStreamOutputBuffer::NextPictureTime()
{
    vlc_tick_t t;
    buffer_mutex.lock();
    if(!queued.empty())
        t = reinterpret_cast<picture_t *>(queued.front())->date;
    else
        t = VLC_TICK_INVALID;
    buffer_mutex.unlock();
    return t;
}

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
unsigned StreamID::i_next_sequence_id = 0;

StreamID::StreamID(int i_stream_id)
{
    stream_id = i_stream_id;
    sequence_id = i_next_sequence_id++;
}

StreamID::StreamID(int i_stream_id, int i_sequence)
{
    stream_id = i_stream_id;
    sequence_id = i_sequence;
}

std::string StreamID::toString() const
{
    std::stringstream ss;
    ss.imbue(std::locale("C"));
    ss << "Stream(";
    if(stream_id > -1)
        ss << "id #" << stream_id << ", ";
    ss << "seq " << sequence_id << ")";
    return ss.str();
}

StreamID& StreamID::operator=(const StreamID &other)
{
    stream_id = other.stream_id;
    sequence_id = other.sequence_id;
    return *this;
}

183
bool StreamID::operator==(const StreamID &other) const
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
{
    if(stream_id == -1 || other.stream_id == -1)
        return sequence_id == other.sequence_id;
    else
        return stream_id == other.stream_id;
}

AbstractStream::AbstractStream(vlc_object_t *p_obj,
                               const StreamID &id,
                               AbstractStreamOutputBuffer *buffer)
    : id(id)
{
    p_stream = p_obj;
    outputbuffer = buffer;
}

AbstractStream::~AbstractStream()
{

}

const StreamID & AbstractStream::getID() const
{
    return id;
}

struct decoder_owner
{
    decoder_t dec;
    AbstractDecodedStream *id;
    bool b_error;
    es_format_t last_fmt_update;
    es_format_t decoder_out;
217
    vlc_decoder_device *dec_dev;
218
219
220
221
222
223
224
225
226
};

AbstractDecodedStream::AbstractDecodedStream(vlc_object_t *p_obj,
                                             const StreamID &id,
                                             AbstractStreamOutputBuffer *buffer)
    : AbstractStream(p_obj, id, buffer)
{
    p_decoder = NULL;
    es_format_Init(&requestedoutput, 0, 0);
227
228
229
230
231
    vlc_mutex_init(&inputLock);
    vlc_cond_init(&inputWait);
    threadEnd = false;
    status = DECODING;
    pcr = VLC_TICK_INVALID;
232
233
234
235
}

AbstractDecodedStream::~AbstractDecodedStream()
{
236
237
    Flush();
    deinit();
238
    es_format_Clean(&requestedoutput);
239
}
240

241
242
243
244
245
246
247
248
249
250
void AbstractDecodedStream::deinit()
{
    if(p_decoder)
    {
        Flush();
        vlc_mutex_lock(&inputLock);
        vlc_cond_signal(&inputWait);
        threadEnd = true;
        vlc_mutex_unlock(&inputLock);
        vlc_join(thread, NULL);
251
        ReleaseDecoder();
252
    }
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
}

bool AbstractDecodedStream::init(const es_format_t *p_fmt)
{
    const char *category;
    if(p_fmt->i_cat == VIDEO_ES)
        category = "video decoder";
    else if(p_fmt->i_cat == AUDIO_ES)
        category = "audio decoder";
    else
        return false;

    /* Create decoder object */
    struct decoder_owner * p_owner =
            reinterpret_cast<struct decoder_owner *>(
                vlc_object_create(p_stream, sizeof(*p_owner)));
    if(!p_owner)
        return false;

    es_format_Init(&p_owner->decoder_out, p_fmt->i_cat, 0);
    es_format_Init(&p_owner->last_fmt_update, p_fmt->i_cat, 0);
    p_owner->b_error = false;
    p_owner->id = this;

    p_decoder = &p_owner->dec;
278
    decoder_Init( p_decoder, p_fmt );
279
280
281
282
283
284
285

    setCallbacks();

    p_decoder->p_module = module_need_var(p_decoder, category, "codec");
    if(!p_decoder->p_module)
    {
        msg_Err(p_stream, "cannot find %s for %4.4s", category, (char *)&p_fmt->i_codec);
286
        ReleaseDecoder();
287
288
289
        return false;
    }

290
291
292
293
294
295
296
297
298
    if(vlc_clone(&thread, decoderThreadCallback, this, VLC_THREAD_PRIORITY_VIDEO))
    {
        es_format_Clean(&p_owner->decoder_out);
        es_format_Clean(&p_owner->last_fmt_update);
        decoder_Destroy( p_decoder );
        p_decoder = NULL;
        return false;
    }

299
300
301
    return true;
}

302
void * AbstractDecodedStream::decoderThreadCallback(void *me)
303
{
304
305
306
    reinterpret_cast<AbstractDecodedStream *>(me)->decoderThread();
    return NULL;
}
307

308
309
void AbstractDecodedStream::decoderThread()
{
310
311
312
    struct decoder_owner *p_owner =
            container_of(p_decoder, struct decoder_owner, dec);

313
314
315
    vlc_savecancel();
    vlc_mutex_lock(&inputLock);
    for(;;)
316
    {
317
318
319
        while(inputQueue.empty() && !threadEnd)
            vlc_cond_wait(&inputWait, &inputLock);
        if(threadEnd)
320
        {
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
            vlc_mutex_unlock(&inputLock);
            break;
        }

        block_t *p_block = inputQueue.front();
        inputQueue.pop();

        bool b_draincall = (status == DRAINING) && (p_block == NULL);
        vlc_mutex_unlock(&inputLock);

        if(!p_owner->b_error)
        {
            int ret = p_decoder->pf_decode(p_decoder, p_block);
            switch(ret)
            {
                case VLCDEC_SUCCESS:
                    break;
                case VLCDEC_ECRITICAL:
                    p_owner->b_error = true;
                    break;
                case VLCDEC_RELOAD:
                    p_owner->b_error = true;
                    if(p_block)
                        block_Release(p_block);
                    break;
                default:
                    vlc_assert_unreachable();
            }
        }

        vlc_mutex_lock(&inputLock);
        if(p_owner->b_error)
        {
            status = FAILED;
            outputbuffer->Drain();
        }
        else if(b_draincall)
        {
            status = DRAINED;
            outputbuffer->Drain();
361
362
        }
    }
363
}
364

365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
int AbstractDecodedStream::Send(block_t *p_block)
{
    assert(p_decoder);
    vlc_mutex_lock(&inputLock);
    inputQueue.push(p_block);
    if(p_block)
    {
        vlc_tick_t t = std::min(p_block->i_dts, p_block->i_pts);
        if(t == VLC_TICK_INVALID)
            t = std::max(p_block->i_dts, p_block->i_pts);
        pcr = std::max(pcr, t);
    }
    vlc_cond_signal(&inputWait);
    vlc_mutex_unlock(&inputLock);
    return VLC_SUCCESS;
380
381
382
383
}

void AbstractDecodedStream::Flush()
{
384
385
386
387
388
389
390
391
    vlc_mutex_lock(&inputLock);
    while(!inputQueue.empty())
    {
        if(inputQueue.front())
            block_Release(inputQueue.front());
        inputQueue.pop();
    }
    vlc_mutex_unlock(&inputLock);
392
393
394
395
396
}

void AbstractDecodedStream::Drain()
{
    Send(NULL);
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
    vlc_mutex_lock(&inputLock);
    if(status != FAILED && status != DRAINED)
        status = DRAINING;
    vlc_mutex_unlock(&inputLock);
}

bool AbstractDecodedStream::isEOS()
{
    vlc_mutex_lock(&inputLock);
    bool b = (status == FAILED || status == DRAINED);
    vlc_mutex_unlock(&inputLock);
    return b;
}

bool AbstractDecodedStream::ReachedPlaybackTime(vlc_tick_t t)
{
    vlc_mutex_lock(&inputLock);
    bool b = (pcr != VLC_TICK_INVALID) && t < pcr;
    b |= (status == DRAINED) || (status == FAILED);
    vlc_mutex_unlock(&inputLock);
    return b;
418
419
}

420
421
422
423
424
425
426
427
428
429
void AbstractDecodedStream::ReleaseDecoder()
{
    struct decoder_owner *p_owner;
    p_owner = container_of(p_decoder, struct decoder_owner, dec);
    es_format_Clean(&p_owner->decoder_out);
    es_format_Clean(&p_owner->last_fmt_update);
    decoder_Destroy( p_decoder );
    p_decoder = NULL;
}

430
431
432
433
434
435
436
437
438
439
440
441
void AbstractDecodedStream::setOutputFormat(const es_format_t *p_fmt)
{
    es_format_Clean(&requestedoutput);
    es_format_Copy(&requestedoutput, p_fmt);
}

VideoDecodedStream::VideoDecodedStream(vlc_object_t *p_obj,
                                       const StreamID &id,
                                       AbstractStreamOutputBuffer *buffer)
    :AbstractDecodedStream(p_obj, id, buffer)
{
    p_filters_chain = NULL;
442
    captionsOutputBuffer = NULL;
443
444
445
446
}

VideoDecodedStream::~VideoDecodedStream()
{
447
    deinit();
448
449
450
451
452
453
454
455
    if(p_filters_chain)
        filter_chain_Delete(p_filters_chain);
}

void VideoDecodedStream::setCallbacks()
{
    static struct decoder_owner_callbacks dec_cbs;
    memset(&dec_cbs, 0, sizeof(dec_cbs));
456
    dec_cbs.video.get_device = VideoDecCallback_get_device;
457
458
    dec_cbs.video.format_update = VideoDecCallback_update_format;
    dec_cbs.video.queue = VideoDecCallback_queue;
459
    dec_cbs.video.queue_cc = captionsOutputBuffer ? VideoDecCallback_queue_cc : NULL;
460
461
462
463

    p_decoder->cbs = &dec_cbs;
}

464
465
466
467
468
void VideoDecodedStream::setCaptionsOutputBuffer(AbstractStreamOutputBuffer *buf)
{
    captionsOutputBuffer = buf;
}

469
470
471
472
473
474
475
void VideoDecodedStream::VideoDecCallback_queue(decoder_t *p_dec, picture_t *p_pic)
{
    struct decoder_owner *p_owner;
    p_owner = container_of(p_dec, struct decoder_owner, dec);
    static_cast<VideoDecodedStream *>(p_owner->id)->Output(p_pic);
}

476
477
478
479
480
481
482
483
void VideoDecodedStream::VideoDecCallback_queue_cc(decoder_t *p_dec, block_t *p_block,
                                                   const decoder_cc_desc_t *)
{
    struct decoder_owner *p_owner;
    p_owner = container_of(p_dec, struct decoder_owner, dec);
    static_cast<VideoDecodedStream *>(p_owner->id)->QueueCC(p_block);
}

484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
vlc_decoder_device * VideoDecodedStream::VideoDecCallback_get_device(decoder_t *p_dec)
{
    struct decoder_owner *p_owner;
    p_owner = container_of(p_dec, struct decoder_owner, dec);
    if (p_owner->dec_dev == NULL)
    {
        p_owner->dec_dev = vlc_decoder_device_Create(&p_dec->obj, NULL);
    }
    return p_owner->dec_dev ? vlc_decoder_device_Hold(p_owner->dec_dev) : NULL;
}

void VideoDecodedStream::ReleaseDecoder()
{
    AbstractDecodedStream::ReleaseDecoder();

    struct decoder_owner *p_owner;
    p_owner = container_of(p_decoder, struct decoder_owner, dec);
    if (p_owner->dec_dev)
    {
        vlc_decoder_device_Release(p_owner->dec_dev);
        p_owner->dec_dev = NULL;
    }
}

François Cartegnie's avatar
François Cartegnie committed
508
509
int VideoDecodedStream::VideoDecCallback_update_format(decoder_t *p_dec,
                                                       vlc_video_context *)
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
{
    struct decoder_owner *p_owner;
    p_owner = container_of(p_dec, struct decoder_owner, dec);

    es_format_Clean(&p_owner->last_fmt_update);
    es_format_Copy(&p_owner->last_fmt_update, &p_dec->fmt_out);

    return VLC_SUCCESS;
}

static picture_t *transcode_video_filter_buffer_new(filter_t *p_filter)
{
    p_filter->fmt_out.video.i_chroma = p_filter->fmt_out.i_codec;
    return picture_NewFromFormat(&p_filter->fmt_out.video);
}

static const struct filter_video_callbacks transcode_filter_video_cbs =
{
528
    transcode_video_filter_buffer_new, NULL,
529
530
};

531
filter_chain_t * VideoDecodedStream::VideoFilterCreate(const es_format_t *p_srcfmt, vlc_video_context *vctx)
532
533
534
535
536
537
538
539
540
{
    filter_chain_t *p_chain;
    filter_owner_t owner;
    memset(&owner, 0, sizeof(owner));
    owner.video = &transcode_filter_video_cbs;

    p_chain = filter_chain_NewVideo(p_stream, false, &owner);
    if(!p_chain)
        return NULL;
541
    filter_chain_Reset(p_chain, p_srcfmt, vctx, &requestedoutput);
542
543
544

    if(p_srcfmt->video.i_chroma != requestedoutput.video.i_chroma)
    {
545
        if(filter_chain_AppendConverter(p_chain, &requestedoutput) != VLC_SUCCESS)
546
547
548
549
550
        {
            filter_chain_Delete(p_chain);
            return NULL;
        }

551
552
553
554
555
556
        const es_format_t *p_fmt_out = filter_chain_GetFmtOut(p_chain);
        if(!es_format_IsSimilar(&requestedoutput, p_fmt_out))
        {
            filter_chain_Delete(p_chain);
            return NULL;
        }
557
558
559
560
561
562
563
564
565
566
    }

    return p_chain;
}

void VideoDecodedStream::Output(picture_t *p_pic)
{
    struct decoder_owner *p_owner;
    p_owner = container_of(p_decoder, struct decoder_owner, dec);

567
    if(!es_format_IsSimilar(&p_owner->decoder_out, &p_owner->last_fmt_update))
568
    {
569
570
        es_format_Clean(&p_owner->decoder_out);
        es_format_Copy(&p_owner->decoder_out, &p_owner->last_fmt_update);
571
572

        msg_Dbg(p_stream, "decoder output format now %4.4s",
573
                (char*)&p_owner->decoder_out.i_codec);
574
575
576

        if(p_filters_chain)
            filter_chain_Delete(p_filters_chain);
577
        p_filters_chain = VideoFilterCreate(&p_owner->decoder_out,
578
                                            picture_GetVideoContext(p_pic));
579
580
581
582
583
584
585
586
587
588
589
        if(!p_filters_chain)
        {
            picture_Release(p_pic);
            return;
        }
    }

    if(p_filters_chain)
        p_pic = filter_chain_VideoFilter(p_filters_chain, p_pic);
    if(p_pic)
        outputbuffer->Enqueue(p_pic);
590
591
592
593
594
595
596
597
598
    if(p_filters_chain)
    {
        p_pic = filter_chain_VideoFilter(p_filters_chain, NULL);
        while (p_pic)
        {
            outputbuffer->Enqueue(p_pic);
            p_pic = filter_chain_VideoFilter(p_filters_chain, NULL);
        }
    }
599
600
}

601
602
603
604
605
void VideoDecodedStream::QueueCC(block_t *p_block)
{
    captionsOutputBuffer->Enqueue(p_block);
}

606
607
608
609
610
611
612
613
614
615
AudioDecodedStream::AudioDecodedStream(vlc_object_t *p_obj,
                                       const StreamID &id,
                                       AbstractStreamOutputBuffer *buffer)
    :AbstractDecodedStream(p_obj, id, buffer)
{
    p_filters = NULL;
}

AudioDecodedStream::~AudioDecodedStream()
{
616
    deinit();
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
    if(p_filters)
        aout_FiltersDelete(p_stream, p_filters);
}

void AudioDecodedStream::AudioDecCallback_queue(decoder_t *p_dec, block_t *p_block)
{
    struct decoder_owner *p_owner;
    p_owner = container_of(p_dec, struct decoder_owner, dec);
    static_cast<AudioDecodedStream *>(p_owner->id)->Output(p_block);
}

void AudioDecodedStream::Output(block_t *p_block)
{
    struct decoder_owner *p_owner;
    p_owner = container_of(p_decoder, struct decoder_owner, dec);

633
    if(!es_format_IsSimilar(&p_owner->decoder_out, &p_owner->last_fmt_update))
634
    {
635
636
637
        es_format_Clean(&p_owner->decoder_out);
        es_format_Copy(&p_owner->decoder_out, &p_owner->last_fmt_update);

638
        msg_Dbg(p_stream, "decoder output format now %4.4s %u channels",
639
640
                (char*)&p_owner->decoder_out.i_codec,
                p_owner->decoder_out.audio.i_channels);
641
642
643

        if(p_filters)
            aout_FiltersDelete(p_stream, p_filters);
644
        p_filters = AudioFiltersCreate(&p_owner->decoder_out);
645
646
647
648
649
650
651
652
653
654
655
656
657
        if(!p_filters)
        {
            msg_Err(p_stream, "filter creation failed");
            block_Release(p_block);
            return;
        }
    }

    /* Run filter chain */
    if(p_filters)
        p_block = aout_FiltersPlay(p_filters, p_block, 1.f);

    if(p_block && !p_block->i_nb_samples &&
658
       p_owner->decoder_out.audio.i_bytes_per_frame )
659
660
    {
        p_block->i_nb_samples = p_block->i_buffer /
661
                p_owner->decoder_out.audio.i_bytes_per_frame;
662
663
664
665
666
667
668
669
    }

    if(p_block)
        outputbuffer->Enqueue(p_block);
}

aout_filters_t * AudioDecodedStream::AudioFiltersCreate(const es_format_t *afmt)
{
670
    return aout_FiltersNew(p_stream, &afmt->audio, &requestedoutput.audio, NULL);
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
}

int AudioDecodedStream::AudioDecCallback_update_format(decoder_t *p_dec)
{
    struct decoder_owner *p_owner;
    p_owner = container_of(p_dec, struct decoder_owner, dec);

    if( !AOUT_FMT_LINEAR(&p_dec->fmt_out.audio) )
        return VLC_EGENERIC;

    /* fixup */
    p_dec->fmt_out.audio.i_format = p_dec->fmt_out.i_codec;
    aout_FormatPrepare(&p_dec->fmt_out.audio);

    es_format_Clean(&p_owner->last_fmt_update);
    es_format_Copy(&p_owner->last_fmt_update, &p_dec->fmt_out);

    p_owner->last_fmt_update.audio.i_format = p_owner->last_fmt_update.i_codec;

    return VLC_SUCCESS;
}

void AudioDecodedStream::setCallbacks()
{
    static struct decoder_owner_callbacks dec_cbs;
    memset(&dec_cbs, 0, sizeof(dec_cbs));
    dec_cbs.audio.format_update = AudioDecCallback_update_format;
    dec_cbs.audio.queue = AudioDecCallback_queue;
    p_decoder->cbs = &dec_cbs;
}
701

702
703

AbstractRawStream::AbstractRawStream(vlc_object_t *p_obj, const StreamID &id,
704
705
706
                               AbstractStreamOutputBuffer *buffer)
    : AbstractStream(p_obj, id, buffer)
{
707
708
    pcr = VLC_TICK_INVALID;
    b_draining = false;
709
710
}

711
AbstractRawStream::~AbstractRawStream()
712
713
714
715
{
    FlushQueued();
}

716
int AbstractRawStream::Send(block_t *p_block)
717
{
718
719
720
    vlc_tick_t t = std::min(p_block->i_dts, p_block->i_pts);
    if(t == VLC_TICK_INVALID)
        t = std::max(p_block->i_dts, p_block->i_pts);
721
722
723
724
    if(p_block->i_buffer)
        outputbuffer->Enqueue(p_block);
    else
        block_Release(p_block);
725
726
727
    buffer_mutex.lock();
    pcr = std::max(pcr, t);
    buffer_mutex.unlock();
728
729
730
    return VLC_SUCCESS;
}

731
void AbstractRawStream::Flush()
732
{
733
    FlushQueued();
734
735
}

736
void AbstractRawStream::Drain()
737
{
738
739
740
741
    buffer_mutex.lock();
    b_draining = true;
    buffer_mutex.unlock();
}
742

743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
bool AbstractRawStream::ReachedPlaybackTime(vlc_tick_t t)
{
    buffer_mutex.lock();
    bool b = (pcr != VLC_TICK_INVALID) && t < pcr;
    b |= b_draining;
    buffer_mutex.unlock();
    return b;
}

bool AbstractRawStream::isEOS()
{
    buffer_mutex.lock();
    bool b = b_draining;
    buffer_mutex.unlock();
    return b;
758
759
}

760
void AbstractRawStream::FlushQueued()
761
762
763
764
765
{
    block_t *p;
    while((p = reinterpret_cast<block_t *>(outputbuffer->Dequeue())))
        block_Release(p);
}
766

767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831

AbstractReorderedStream::AbstractReorderedStream(vlc_object_t *p_obj, const StreamID &id,
                                                 AbstractStreamOutputBuffer *buffer)
    : AbstractRawStream(p_obj, id, buffer)
{
    reorder_depth = 0;
    do_reorder = false;
}

AbstractReorderedStream::~AbstractReorderedStream()
{
}

int AbstractReorderedStream::Send(block_t *p_block)
{
    auto it = reorder.begin();
    if(do_reorder)
    {
        for(; it != reorder.end(); ++it)
        {
            if((*it)->i_pts == VLC_TICK_INVALID)
                continue;
            if(p_block->i_pts < (*it)->i_pts)
            {
                /* found insertion point */
                if(it == reorder.begin() &&
                   reorder_depth < 16 && reorder.size() < reorder_depth)
                      reorder_depth++;
                break;
            }
        }

        reorder.insert(it, p_block);

        if(reorder.size() <= reorder_depth + 1)
            return VLC_SUCCESS;

        p_block = reorder.front();
        reorder.pop_front();
    }

    return AbstractRawStream::Send(p_block);
}

void AbstractReorderedStream::Flush()
{
    Drain();
    FlushQueued();
}

void AbstractReorderedStream::Drain()
{
    while(!reorder.empty())
    {
        AbstractRawStream::Send(reorder.front());
        reorder.pop_front();
    }
}

void AbstractReorderedStream::setReorder(size_t r)
{
    reorder_depth = r;
    do_reorder = true;
}

832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
AudioCompressedStream::AudioCompressedStream(vlc_object_t *p_obj, const StreamID &id,
                                             AbstractStreamOutputBuffer *buffer)
    : AbstractRawStream(p_obj, id, buffer)
{
}

AudioCompressedStream::~AudioCompressedStream()
{
}

int AudioCompressedStream::Send(block_t *p_block)
{
    const size_t i_payload = p_block->i_buffer;
    const size_t i_pad = (p_block->i_buffer & 1) ? 1 : 0;
    p_block = block_Realloc(p_block, 12, p_block->i_buffer + i_pad);
    if(!p_block)
        return VLC_EGENERIC;
    /* Convert to AES3 Payload */
    SetWBE(&p_block->p_buffer[0], 0x0000); /* Extra 0000 */
    SetWBE(&p_block->p_buffer[2], 0x0000); /* see S337 Annex B */
    SetWBE(&p_block->p_buffer[4], 0xF872); /* Pa Start code/Preamble */
    SetWBE(&p_block->p_buffer[6], 0x4E1F); /* Pb Start code/Preamble */
    SetWBE(&p_block->p_buffer[8], 0x0001); /* A52 Burst code */
    SetWBE(&p_block->p_buffer[10], i_payload);
    if(i_pad)
        p_block->p_buffer[p_block->i_buffer - 1] = 0x00;
    return AbstractRawStream::Send(p_block);
}

bool AudioCompressedStream::init(const es_format_t *fmt)
{
    return (fmt->i_codec == VLC_CODEC_A52);
}
865
866
867

CaptionsStream::CaptionsStream(vlc_object_t *p_obj, const StreamID &id,
                               AbstractStreamOutputBuffer *buffer)
868
    : AbstractReorderedStream(p_obj, id, buffer)
869
{
870

871
872
873
874
875
876
877
878
}

CaptionsStream::~CaptionsStream()
{
}

bool CaptionsStream::init(const es_format_t *fmt)
{
879
880
    if(fmt->subs.cc.i_reorder_depth >= 0)
        setReorder(fmt->subs.cc.i_reorder_depth);
881
882
    return (fmt->i_codec == VLC_CODEC_CEA608);
}
883