summaryrefslogtreecommitdiff
path: root/dxr3pesframe.c
blob: da759fd3c71c0575acaa69285b0bbc7c8fc82814 (plain)
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
 * dxr3pesframe.c
 *
 * Copyright (C) 2002-2004 Kai Möller
 * Copyright (C) 2009 Christian Gmeiner
 *
 * This library 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 library 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 library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 *
 */

#include "dxr3pesframe.h"

#include <linux/em8300.h>
#include <vdr/tools.h>
#include "dxr3demuxdevice.h"

// ==================================
bool cDxr3PesFrame::parse(const uint8_t *pes, uint32_t length)
{
    // look if at the beginning there is the pes packet start indicator
    if (pes[0] != 0 || pes[1] != 0 || pes[2] != 1) {
        esyslog("[cDxr3PesFrame]: no start indicator found...");
        return false;
    }

    // handle stream id
    switch (pes[3]) {
    case 0xBD: // private stream 1
        m_pesDataType = PES_PRIVATE_DATA;
        break;

    case 0xC0 ... 0xDF: // audio stream
        m_pesDataType = PES_AUDIO_DATA;
        break;

    case 0xE0 ... 0xEF: // video stream
        m_pesDataType = PES_VIDEO_DATA;
        break;

    default:
        return false;
    }

    // store start of pes frame
    m_pesStart = pes;

    // read pes header len
    uint8_t pesHeaderDataLength = pes[8];

    // look if there is a pts
    if (pes[7] & (1 << 7)) {
        // read pts
        uint64_t pts  = (((int64_t)pes[ 9]) & 0x0e) << 29;
        pts |= ( (int64_t)pes[10])         << 22;
        pts |= (((int64_t)pes[11]) & 0xfe) << 14;
        pts |= ( (int64_t)pes[12])         <<  7;
        pts |= (((int64_t)pes[13]) & 0xfe) >>  1;

        m_pts = pts >> 1;
    }

    // set pointer to start of payload
    int payloadStart = 9 + pesHeaderDataLength;
    m_payload = &pes[payloadStart];
    m_payloadLength = length - payloadStart;

    // check for max sizes supported by syncbuffer
    if (m_pesDataType == PES_VIDEO_DATA && m_payloadLength > (uint32_t)VIDEO_MAX_FRAME_SIZE) {
        return false;
    }
    if (m_pesDataType == PES_AUDIO_DATA && m_payloadLength > (uint32_t)AUIDO_MAX_FRAME_SIZE) {
        return false;
    }

    if (m_pesDataType == PES_VIDEO_DATA) {

        // we can get some informations about the video payload
        // of this pes frame. For more informations have a look
        // at http://dvd.sourceforge.net/dvdinfo/mpeghdrs.html

        const uint8_t *video = &pes[payloadStart];

        // look if there is the start code prefix (0000 0000 0000 0000 0000 0001)
        if (video[0] != 0 || video[1] != 0 || video[2] != 1) {
            return true;
        }

        // check stream id
        if (video[3] == 0xb3) {

            // in the sequence header we get informations about horizontal
            // and vertical size of the video and the current aspect ratio.

            m_horizontalSize = (video[5] & 0xf0) >> 4 | video[4] << 4;
            m_verticalSize = (video[5] & 0x0f) << 8 | video[6];

            switch (video[7] & 0xf0) {
            case 0x20:
                m_aspectRatio = EM8300_ASPECTRATIO_4_3;
                break;

            case 0x30:
                m_aspectRatio = EM8300_ASPECTRATIO_16_9;
                break;
            }

            m_staticAspectRatio = m_aspectRatio;
        }
    }

    return true;
}


// ==================================
uint32_t cDxr3PesFrame::m_staticAspectRatio = EM8300_ASPECTRATIO_4_3;

// Local variables:
// mode: c++
// c-file-style: "stroustrup"
// c-file-offsets: ((inline-open . 0))
// c-basic-offset: 4
// indent-tabs-mode: nil
// End: