File: rtpvideo.c

package info (click to toggle)
vlc 3.0.23-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 208,020 kB
  • sloc: ansic: 443,448; cpp: 111,223; objc: 36,399; sh: 6,737; makefile: 6,627; javascript: 4,902; xml: 1,611; asm: 1,355; yacc: 644; python: 321; lex: 88; perl: 77; sed: 16
file content (108 lines) | stat: -rw-r--r-- 4,104 bytes parent folder | download | duplicates (7)
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
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
74
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
/*****************************************************************************
 * rtpvideo.c: video encoder for raw video for RTP (see RFC 4175)
 *****************************************************************************
 * Copyright (C) 2015 VLC authors and VideoLAN
 * $Id$
 *
 * Authors: Tristan Matthews <tmatth@videolan.org>
 *
 * 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.
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include <vlc_common.h>
#include <vlc_plugin.h>
#include <vlc_codec.h>

/****************************************************************************
 * Local prototypes
 ****************************************************************************/
static int  OpenEncoder( vlc_object_t * );
static block_t *Encode( encoder_t *p_enc, picture_t *p_pict );

/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
vlc_module_begin ()
    set_description( N_("Raw video encoder for RTP") )
    set_capability( "encoder", 50 )
    set_category( CAT_INPUT )
    set_subcategory( SUBCAT_INPUT_VCODEC )
    set_callbacks( OpenEncoder, NULL )
    add_shortcut( "rtpvideo" )
vlc_module_end ()

static int OpenEncoder( vlc_object_t *p_this )
{
    encoder_t *p_enc = (encoder_t *)p_this;
    if( p_enc->fmt_out.i_codec != VLC_CODEC_R420 && !p_enc->obj.force )
        return VLC_EGENERIC;

    p_enc->pf_encode_video = Encode;
    p_enc->fmt_in.i_codec = VLC_CODEC_I420;
    p_enc->fmt_out.i_codec = VLC_CODEC_R420;

    return VLC_SUCCESS;
}

static block_t *Encode( encoder_t *p_enc, picture_t *p_pict )
{
    VLC_UNUSED( p_enc );
    if( !p_pict ) return NULL;

    const int i_length = p_pict->p[0].i_visible_lines * p_pict->p[0].i_visible_pitch +
        p_pict->p[1].i_visible_lines * p_pict->p[1].i_visible_pitch +
        p_pict->p[2].i_visible_lines * p_pict->p[2].i_visible_pitch;

    block_t *p_block = block_Alloc( i_length );
    if( !p_block ) return NULL;

    p_block->i_dts = p_block->i_pts = p_pict->date;
    uint8_t *p_outdata = p_block->p_buffer;
    const int i_xdec = 2;
    const int i_ydec = 2;

    const uint8_t *p_yd1 = p_pict->p[0].p_pixels;
    const uint8_t *p_u = p_pict->p[1].p_pixels;
    const uint8_t *p_v = p_pict->p[2].p_pixels;

    /* Y00-Y01-Y10-Y11-U00-V00...luma from adjacent lines */
    for( int i_lin = 0; i_lin < p_pict->p[0].i_visible_lines; i_lin += i_ydec )
    {
        const uint8_t *p_yd2 = p_yd1 + p_pict->p[0].i_pitch;
        for ( int i_pix = 0; i_pix < p_pict->p[0].i_visible_pitch; i_pix += i_xdec )
        {
            *p_outdata++ = *p_yd1++;
            *p_outdata++ = *p_yd1++;
            *p_outdata++ = *p_yd2++;
            *p_outdata++ = *p_yd2++;
            *p_outdata++ = *p_u++;
            *p_outdata++ = *p_v++;
        }
        /* Skip a line + padding */
        p_yd1 += p_pict->p[0].i_pitch +
                 (p_pict->p[0].i_pitch - p_pict->p[0].i_visible_pitch);
        p_u += p_pict->p[1].i_pitch - p_pict->p[1].i_visible_pitch;
        p_v += p_pict->p[2].i_pitch - p_pict->p[2].i_visible_pitch;
    }

    return p_block;
}