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

HdmvClipInfo.cpp « DSUtil « src - github.com/mpc-hc/mpc-hc.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b33e94655b60911c5030db3fda268154b1168eff (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
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
202
203
204
205
206
207
208
209
210
211
212
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
/*
 * (C) 2008-2014, 2017 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/>.
 *
 */

#include "stdafx.h"
#include "HdmvClipInfo.h"
#include "DSUtil.h"
#include "ISOLang.h"


CHdmvClipInfo::CHdmvClipInfo()
    : SequenceInfo_start_address(0)
    , ProgramInfo_start_address(0)
    , m_hFile(INVALID_HANDLE_VALUE)
    , m_bIsHdmv(false)
{
}

CHdmvClipInfo::~CHdmvClipInfo()
{
    CloseFile(S_OK);
}

HRESULT CHdmvClipInfo::CloseFile(HRESULT hr)
{
    if (m_hFile != INVALID_HANDLE_VALUE) {
        CloseHandle(m_hFile);
        m_hFile = INVALID_HANDLE_VALUE;
    }
    return hr;
}

DWORD CHdmvClipInfo::ReadDword()
{
    return ReadByte() << 24 | ReadByte() << 16 | ReadByte() << 8 | ReadByte();
}

short CHdmvClipInfo::ReadShort()
{
    return ReadByte() << 8 | ReadByte();
}

BYTE CHdmvClipInfo::ReadByte()
{
    BYTE  bVal;
    DWORD dwRead;
    VERIFY(ReadFile(m_hFile, &bVal, sizeof(bVal), &dwRead, nullptr));

    return bVal;
}

void CHdmvClipInfo::ReadBuffer(BYTE* pBuff, DWORD nLen)
{
    DWORD dwRead;
    VERIFY(ReadFile(m_hFile, pBuff, nLen, &dwRead, nullptr));
}

HRESULT CHdmvClipInfo::ReadProgramInfo()
{
    BYTE number_of_program_sequences;
    LARGE_INTEGER Pos;

    m_Streams.RemoveAll();
    Pos.QuadPart = ProgramInfo_start_address;
    SetFilePointerEx(m_hFile, Pos, nullptr, FILE_BEGIN);

    ReadDword();    //length
    ReadByte();     //reserved_for_word_align
    number_of_program_sequences = (BYTE)ReadByte();
    int iStream = 0;
    for (size_t i = 0; i < number_of_program_sequences; i++) {
        ReadDword();    //SPN_program_sequence_start
        ReadShort();    //program_map_PID
        BYTE number_of_streams_in_ps = (BYTE)ReadByte(); //number_of_streams_in_ps
        ReadByte();     //reserved_for_future_use

        for (size_t stream_index = 0; stream_index < number_of_streams_in_ps; stream_index++) {
            m_Streams.SetCount(iStream + 1);
            m_Streams[iStream].m_PID = ReadShort();  // stream_PID

            // == StreamCodingInfo
            Pos.QuadPart = 0;
            SetFilePointerEx(m_hFile, Pos, &Pos, FILE_CURRENT);
            Pos.QuadPart += ReadByte() + 1; // length
            m_Streams[iStream].m_Type = (PES_STREAM_TYPE)ReadByte();

            switch (m_Streams[iStream].m_Type) {
                case VIDEO_STREAM_MPEG1:
                case VIDEO_STREAM_MPEG2:
                case VIDEO_STREAM_H264:
                case VIDEO_STREAM_VC1:
                case VIDEO_STREAM_HEVC: {
                    UINT8 Temp = ReadByte();
                    BDVM_VideoFormat VideoFormat = (BDVM_VideoFormat)(Temp >> 4);
                    BDVM_FrameRate FrameRate = (BDVM_FrameRate)(Temp & 0xf);
                    Temp = ReadByte();
                    BDVM_AspectRatio AspectRatio = (BDVM_AspectRatio)(Temp >> 4);

                    m_Streams[iStream].m_VideoFormat = VideoFormat;
                    m_Streams[iStream].m_FrameRate = FrameRate;
                    m_Streams[iStream].m_AspectRatio = AspectRatio;
                }
                break;
                case AUDIO_STREAM_MPEG1:
                case AUDIO_STREAM_MPEG2:
                case AUDIO_STREAM_LPCM:
                case AUDIO_STREAM_AC3:
                case AUDIO_STREAM_DTS:
                case AUDIO_STREAM_AC3_TRUE_HD:
                case AUDIO_STREAM_AC3_PLUS:
                case AUDIO_STREAM_DTS_HD:
                case AUDIO_STREAM_DTS_HD_MASTER_AUDIO:
                case SECONDARY_AUDIO_AC3_PLUS:
                case SECONDARY_AUDIO_DTS_HD: {
                    UINT8 Temp = ReadByte();
                    BDVM_ChannelLayout ChannelLayout = (BDVM_ChannelLayout)(Temp >> 4);
                    BDVM_SampleRate SampleRate = (BDVM_SampleRate)(Temp & 0xF);

                    ReadBuffer((BYTE*)m_Streams[iStream].m_LanguageCode, 3);
                    m_Streams[iStream].m_LanguageCode[3] = '\0';
                    m_Streams[iStream].m_LCID = ISOLang::ISO6392ToLcid(m_Streams[iStream].m_LanguageCode);
                    m_Streams[iStream].m_ChannelLayout = ChannelLayout;
                    m_Streams[iStream].m_SampleRate = SampleRate;
                }
                break;
                case PRESENTATION_GRAPHICS_STREAM:
                case INTERACTIVE_GRAPHICS_STREAM: {
                    ReadBuffer((BYTE*)m_Streams[iStream].m_LanguageCode, 3);
                    m_Streams[iStream].m_LanguageCode[3] = '\0';
                    m_Streams[iStream].m_LCID = ISOLang::ISO6392ToLcid(m_Streams[iStream].m_LanguageCode);
                }
                break;
                case SUBTITLE_STREAM: {
                    ReadByte(); // Should this really be here?
                    ReadBuffer((BYTE*)m_Streams[iStream].m_LanguageCode, 3);
                    m_Streams[iStream].m_LanguageCode[3] = '\0';
                    m_Streams[iStream].m_LCID = ISOLang::ISO6392ToLcid(m_Streams[iStream].m_LanguageCode);
                }
                break;
                default:
                    break;
            }

            iStream++;
            SetFilePointerEx(m_hFile, Pos, nullptr, FILE_BEGIN);
        }
    }
    return S_OK;
}

HRESULT CHdmvClipInfo::ReadInfo(LPCTSTR strFile)
{
    m_bIsHdmv = false;
    m_hFile = CreateFile(strFile, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr,
                         OPEN_EXISTING, FILE_ATTRIBUTE_READONLY | FILE_FLAG_SEQUENTIAL_SCAN, nullptr);

    if (m_hFile != INVALID_HANDLE_VALUE) {
        BYTE Buff[100];
        ReadBuffer(Buff, 4);
        if (memcmp(Buff, "HDMV", 4)) {
            return CloseFile(VFW_E_INVALID_FILE_FORMAT);
        }

        ReadBuffer(Buff, 4);
        if ((memcmp(Buff, "0300", 4) != 0) && (memcmp(Buff, "0200", 4) != 0) && (memcmp(Buff, "0100", 4) != 0)) {
            return CloseFile(VFW_E_INVALID_FILE_FORMAT);
        }

        SequenceInfo_start_address = ReadDword();
        ProgramInfo_start_address  = ReadDword();

        ReadProgramInfo();

        m_bIsHdmv = true;

        return CloseFile(S_OK);
    }

    return AmHresultFromWin32(GetLastError());
}

CHdmvClipInfo::Stream* CHdmvClipInfo::FindStream(short wPID)
{
    size_t nStreams = m_Streams.GetCount();
    for (size_t i = 0; i < nStreams; i++) {
        if (m_Streams[i].m_PID == wPID) {
            return &m_Streams[i];
        }
    }

    return nullptr;
}

LPCTSTR CHdmvClipInfo::Stream::Format()
{
    switch (m_Type) {
        case VIDEO_STREAM_MPEG1:
            return _T("Mpeg1");
        case VIDEO_STREAM_MPEG2:
            return _T("Mpeg2");
        case VIDEO_STREAM_H264:
            return _T("H264");
        case VIDEO_STREAM_VC1:
            return _T("VC1");
        case VIDEO_STREAM_HEVC:
            return _T("HEVC");
        case AUDIO_STREAM_MPEG1:
            return _T("MPEG1");
        case AUDIO_STREAM_MPEG2:
            return _T("MPEG2");
        case AUDIO_STREAM_LPCM:
            return _T("LPCM");
        case AUDIO_STREAM_AC3:
            return _T("AC3");
        case AUDIO_STREAM_DTS:
            return _T("DTS");
        case AUDIO_STREAM_AC3_TRUE_HD:
            return _T("MLP");
        case AUDIO_STREAM_AC3_PLUS:
            return _T("DD+");
        case AUDIO_STREAM_DTS_HD:
            return _T("DTS-HD");
        case AUDIO_STREAM_DTS_HD_MASTER_AUDIO:
            return _T("DTS-HD XLL");
        case SECONDARY_AUDIO_AC3_PLUS:
            return _T("Sec DD+");
        case SECONDARY_AUDIO_DTS_HD:
            return _T("Sec DTS-HD");
        case PRESENTATION_GRAPHICS_STREAM:
            return _T("PG");
        case INTERACTIVE_GRAPHICS_STREAM:
            return _T("IG");
        case SUBTITLE_STREAM:
            return _T("Text");
        default:
            return _T("Unknown");
    }
}

HRESULT CHdmvClipInfo::ReadPlaylist(CString strPlaylistFile, REFERENCE_TIME& rtDuration, CAtlList<PlaylistItem>& Playlist)
{
    CPath Path(strPlaylistFile);
    rtDuration = 0;

    // Get BDMV folder
    Path.RemoveFileSpec();
    Path.RemoveFileSpec();

    m_hFile = CreateFile(strPlaylistFile, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr,
                         OPEN_EXISTING, FILE_ATTRIBUTE_READONLY | FILE_FLAG_SEQUENTIAL_SCAN, nullptr);

    if (m_hFile != INVALID_HANDLE_VALUE) {
        BYTE Buff[100];
        bool bDuplicate = false;
        ReadBuffer(Buff, 4);
        if (memcmp(Buff, "MPLS", 4)) {
            return CloseFile(VFW_E_INVALID_FILE_FORMAT);
        }

        ReadBuffer(Buff, 4);
        if ((memcmp(Buff, "0300", 4) != 0) && (memcmp(Buff, "0200", 4) != 0) && (memcmp(Buff, "0100", 4) != 0)) {
            return CloseFile(VFW_E_INVALID_FILE_FORMAT);
        }

        LARGE_INTEGER Pos;
        unsigned short nPlaylistItems;

        Pos.QuadPart = ReadDword(); // PlayList_start_address
        ReadDword();                // PlayListMark_start_address

        // PlayList()
        SetFilePointerEx(m_hFile, Pos, nullptr, FILE_BEGIN);
        ReadDword();                   // length
        ReadShort();                   // reserved_for_future_use
        nPlaylistItems = ReadShort();  // number_of_PlayItems
        ReadShort();                   // number_of_SubPaths

        Pos.QuadPart += 10;
        for (size_t i = 0; i < nPlaylistItems; i++) {
            DWORD dwTemp;
            PlaylistItem Item;
            SetFilePointerEx(m_hFile, Pos, nullptr, FILE_BEGIN);
            Pos.QuadPart += ReadShort() + 2;
            ReadBuffer(Buff, 5);
            Item.m_strFileName.Format(_T("%s\\STREAM\\%c%c%c%c%c.M2TS"), static_cast<LPCTSTR>(Path), Buff[0], Buff[1], Buff[2], Buff[3], Buff[4]);

            ReadBuffer(Buff, 4);
            if (memcmp(Buff, "M2TS", 4)) {
                return CloseFile(VFW_E_INVALID_FILE_FORMAT);
            }
            ReadBuffer(Buff, 3);

            dwTemp = ReadDword();
            Item.m_rtIn = 20000i64 * dwTemp / 90; // Carefull : 32->33 bits!

            dwTemp = ReadDword();
            Item.m_rtOut = 20000i64 * dwTemp / 90; // Carefull : 32->33 bits!

            rtDuration += (Item.m_rtOut - Item.m_rtIn);

            if (Playlist.Find(Item) != nullptr) {
                bDuplicate = true;
            }
            Playlist.AddTail(Item);

            //TRACE(_T("File : %s, Duration : %s, Total duration  : %s\n"), strTemp, ReftimeToString (rtOut - rtIn), ReftimeToString (rtDuration));
        }

        CloseFile(S_OK);
        return bDuplicate ? S_FALSE : S_OK;
    }

    return AmHresultFromWin32(GetLastError());
}

HRESULT CHdmvClipInfo::ReadChapters(CString strPlaylistFile, CAtlList<CHdmvClipInfo::PlaylistItem>& PlaylistItems, CAtlList<PlaylistChapter>& Chapters)
{
    CPath Path(strPlaylistFile);

    // Get BDMV folder
    Path.RemoveFileSpec();
    Path.RemoveFileSpec();

    m_hFile = CreateFile(strPlaylistFile, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr,
                         OPEN_EXISTING, FILE_ATTRIBUTE_READONLY | FILE_FLAG_SEQUENTIAL_SCAN, nullptr);

    if (m_hFile != INVALID_HANDLE_VALUE) {
        REFERENCE_TIME* rtOffset = DEBUG_NEW REFERENCE_TIME[PlaylistItems.GetCount()];
        REFERENCE_TIME rtSum = 0;
        int nIndex = 0;
        BYTE Buff[100];

        POSITION pos = PlaylistItems.GetHeadPosition();
        while (pos) {
            CHdmvClipInfo::PlaylistItem& PI = PlaylistItems.GetNext(pos);

            rtOffset[nIndex] = rtSum - PI.m_rtIn;
            rtSum = rtSum + PI.Duration();
            nIndex++;
        }

        ReadBuffer(Buff, 4);
        if (memcmp(Buff, "MPLS", 4)) {
            SAFE_DELETE_ARRAY(rtOffset);
            return CloseFile(VFW_E_INVALID_FILE_FORMAT);
        }

        ReadBuffer(Buff, 4);
        if ((memcmp(Buff, "0300", 4) != 0) && (memcmp(Buff, "0200", 4) != 0) && (memcmp(Buff, "0100", 4) != 0)) {
            SAFE_DELETE_ARRAY(rtOffset);
            return CloseFile(VFW_E_INVALID_FILE_FORMAT);
        }

        LARGE_INTEGER  Pos;
        unsigned short nMarkCount;

        ReadDword();                // PlayList_start_address
        Pos.QuadPart = ReadDword(); // PlayListMark_start_address

        // PlayListMark()
        SetFilePointerEx(m_hFile, Pos, nullptr, FILE_BEGIN);
        ReadDword();                // length
        nMarkCount = ReadShort();   // number_of_PlayList_marks
        for (size_t i = 0; i < nMarkCount; i++) {
            PlaylistChapter Chapter;

            ReadByte();                                           // reserved_for_future_use
            Chapter.m_nMarkType   = (PlaylistMarkType)ReadByte(); // mark_type
            Chapter.m_nPlayItemId = ReadShort();                  // ref_to_PlayItem_id
            Chapter.m_rtTimestamp = 20000i64 * ReadDword() / 90 + rtOffset[Chapter.m_nPlayItemId];    // mark_time_stamp
            Chapter.m_nEntryPID   = ReadShort();                  // entry_ES_PID
            Chapter.m_rtDuration  = 20000i64 * ReadDword() / 90;  // duration

            Chapters.AddTail(Chapter);

            //TRACE(_T("Chapter %d : %s\n"), i, ReftimeToString(Chapter.m_rtTimestamp));
        }

        CloseFile(S_OK);
        SAFE_DELETE_ARRAY(rtOffset);
        return S_OK;
    }

    return AmHresultFromWin32(GetLastError());
}

#define MIN_LIMIT 3

HRESULT CHdmvClipInfo::FindMainMovie(LPCTSTR strFolder, CString& strPlaylistFile, CAtlList<PlaylistItem>& MainPlaylist, CAtlList<PlaylistItem>& MPLSPlaylists)
{
    HRESULT hr = E_FAIL;

    CString strPath(strFolder);
    CString strFilter;

    MPLSPlaylists.RemoveAll();

    CAtlList<PlaylistItem> Playlist;
    WIN32_FIND_DATA fd;
    ZeroMemory(&fd, sizeof(WIN32_FIND_DATA));

    strPath.Replace(_T("\\PLAYLIST\\"), _T("\\"));
    strPath.Replace(_T("\\STREAM\\"), _T("\\"));
    strPath += _T("\\BDMV\\");
    strFilter.Format(_T("%sPLAYLIST\\*.mpls"), strPath.GetString());

    HANDLE hFind = FindFirstFile(strFilter, &fd);
    if (hFind != INVALID_HANDLE_VALUE) {
        REFERENCE_TIME rtMax = 0;
        REFERENCE_TIME rtCurrent;
        CString strCurrentPlaylist;
        do {
            strCurrentPlaylist.Format(_T("%sPLAYLIST\\%s"), strPath.GetString(), fd.cFileName);
            Playlist.RemoveAll();

            // Main movie shouldn't have duplicate M2TS filename...
            if (ReadPlaylist(strCurrentPlaylist, rtCurrent, Playlist) == S_OK) {
                if (rtCurrent > rtMax) {
                    rtMax = rtCurrent;
                    strPlaylistFile = strCurrentPlaylist;
                    MainPlaylist.RemoveAll();
                    POSITION pos = Playlist.GetHeadPosition();
                    while (pos) {
                        MainPlaylist.AddTail(Playlist.GetNext(pos));
                    }
                    hr = S_OK;
                }
                if (rtCurrent >= (REFERENCE_TIME)MIN_LIMIT * 600000000) {
                    PlaylistItem Item;
                    Item.m_strFileName = strCurrentPlaylist;
                    Item.m_rtIn = 0;
                    Item.m_rtOut = rtCurrent;
                    MPLSPlaylists.AddTail(Item);
                }

            }
        } while (FindNextFile(hFind, &fd));

        FindClose(hFind);
    }

    if (MPLSPlaylists.GetCount() > 1) {
        // bubble sort
        for (size_t j = 0; j < MPLSPlaylists.GetCount(); j++) {
            for (size_t i = 0; i < MPLSPlaylists.GetCount() - 1; i++) {
                if (MPLSPlaylists.GetAt(MPLSPlaylists.FindIndex(i)).Duration() < MPLSPlaylists.GetAt(MPLSPlaylists.FindIndex(i + 1)).Duration()) {
                    MPLSPlaylists.SwapElements(MPLSPlaylists.FindIndex(i), MPLSPlaylists.FindIndex(i + 1));
                }
            }
        }
    }

    return hr;
}