Welcome to mirror list, hosted at ThFree Co, Russian Federation.

PGSSub.h « Subtitles « src - github.com/mpc-hc/mpc-hc.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 09cf4adfafe20c363537609d2b609f4960810e48 (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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
 * (C) 2006-2014 see Authors.txt
 *
 * This file is part of MPC-HC.
 *
 * MPC-HC is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * MPC-HC 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#pragma once

#include "RLECodedSubtitle.h"
#include "CompositionObject.h"
#include <thread>

class CGolombBuffer;

class CPGSSub : public CRLECodedSubtitle
{
public:
    CPGSSub(CCritSec* pLock, const CString& name, LCID lcid);
    virtual ~CPGSSub();

    // ISubPicProvider
    STDMETHODIMP_(POSITION)       GetStartPosition(REFERENCE_TIME rt, double fps);
    STDMETHODIMP_(POSITION)       GetNext(POSITION pos);
    STDMETHODIMP_(REFERENCE_TIME) GetStart(POSITION pos, double fps);
    STDMETHODIMP_(REFERENCE_TIME) GetStop(POSITION pos, double fps);
    STDMETHODIMP_(bool)           IsAnimated(POSITION pos);
    STDMETHODIMP                  Render(SubPicDesc& spd, REFERENCE_TIME rt, double fps, RECT& bbox);
    STDMETHODIMP                  GetTextureSize(POSITION pos, SIZE& MaxTextureSize, SIZE& VirtualSize, POINT& VirtualTopLeft);

    virtual HRESULT ParseSample(REFERENCE_TIME rtStart, REFERENCE_TIME rtStop, BYTE* pData, size_t nLen);
    virtual void    EndOfStream() { /* Nothing to do */ };
    virtual void    Reset();

protected:
    HRESULT Render(SubPicDesc& spd, REFERENCE_TIME rt, RECT& bbox, bool bRemoveOldSegments);

private:
    enum HDMV_SEGMENT_TYPE {
        NO_SEGMENT       = 0xFFFF,
        PALETTE          = 0x14,
        OBJECT           = 0x15,
        PRESENTATION_SEG = 0x16,
        WINDOW_DEF       = 0x17,
        INTERACTIVE_SEG  = 0x18,
        END_OF_DISPLAY   = 0x80,
        HDMV_SUB1        = 0x81,
        HDMV_SUB2        = 0x82
    };


    struct VIDEO_DESCRIPTOR {
        int  nVideoWidth;
        int  nVideoHeight;
        BYTE bFrameRate;
    };

    struct COMPOSITION_DESCRIPTOR {
        short nNumber;
        BYTE  bState;
    };

    struct SEQUENCE_DESCRIPTOR {
        BYTE bFirstIn  : 1;
        BYTE bLastIn   : 1;
        BYTE bReserved : 6;
    };

    struct HDMV_CLUT {
        BYTE id;
        BYTE version_number;
        BYTE size;

        HDMV_PALETTE palette[256];

        HDMV_CLUT()
            : id(0)
            , version_number(0)
            , size(0) {
            ZeroMemory(palette, sizeof(palette));
        }
    };

    struct HDMV_PRESENTATION_SEGMENT {
        REFERENCE_TIME rtStart;
        REFERENCE_TIME rtStop;

        VIDEO_DESCRIPTOR video_descriptor;
        COMPOSITION_DESCRIPTOR composition_descriptor;

        byte palette_update_flag;
        HDMV_CLUT CLUT;

        int objectCount;

        CAutoPtrList<CompositionObject> objects;
    };

    HDMV_SEGMENT_TYPE m_nCurSegment;
    BYTE*             m_pSegBuffer;
    size_t            m_nTotalSegBuffer;
    size_t            m_nSegBufferPos;
    size_t            m_nSegSize;

    CAutoPtr<HDMV_PRESENTATION_SEGMENT>     m_pCurrentPresentationSegment;
    CAutoPtrList<HDMV_PRESENTATION_SEGMENT> m_pPresentationSegments;

    HDMV_CLUT m_CLUTs[256];
    CompositionObject m_compositionObjects[64];

    void AllocSegment(size_t nSize);
    int  ParsePresentationSegment(REFERENCE_TIME rt, CGolombBuffer* pGBuffer);
    void EnqueuePresentationSegment();
    void UpdateTimeStamp(REFERENCE_TIME rtStop);

    void ParsePalette(CGolombBuffer* pGBuffer, size_t nSize);
    void ParseObject(CGolombBuffer* pGBuffer, size_t nUnitSize);
    void ParseVideoDescriptor(CGolombBuffer* pGBuffer, VIDEO_DESCRIPTOR* pVideoDescriptor);
    void ParseCompositionDescriptor(CGolombBuffer* pGBuffer, COMPOSITION_DESCRIPTOR* pCompositionDescriptor);
    void ParseCompositionObject(CGolombBuffer* pGBuffer, const CAutoPtr<CompositionObject>& pCompositionObject);

    POSITION FindPresentationSegment(REFERENCE_TIME rt) const;

    void RemoveOldSegments(REFERENCE_TIME rt);
};

class CPGSSubFile : public CPGSSub
{
public:
    CPGSSubFile(CCritSec* pLock);
    virtual ~CPGSSubFile();

    // ISubPicProvider
    STDMETHODIMP Render(SubPicDesc& spd, REFERENCE_TIME rt, double fps, RECT& bbox);

    bool Open(CString fn, CString name = _T(""), CString videoName = _T(""));

private:
    static const WORD PGS_SYNC_CODE = 'PG';

    bool m_bStopParsing;
    std::thread m_parsingThread;

    void ParseFile(CString fn);
};