基于qt,msvc2017-64bits,ffmpeg.opengl的播放器
zhangmeng
2021-03-03 4a6d9312cc1c9d62d66c4def71246d9faae29edb
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
/*!
 * COPYRIGHT (C) 2020 Emeric Grange - All Rights Reserved
 *
 * This file is part of MiniVideo.
 *
 * MiniVideo 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 3 of the License, or
 * (at your option) any later version.
 *
 * MiniVideo 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 MiniVideo.  If not, see <http://www.gnu.org/licenses/>.
 *
 * \author    Emeric Grange <emeric.grange@gmail.com>
 * \date      2010
 */
 
// minivideo headers
#include "import.h"
#include "minitraces.h"
#include "minivideo_typedef.h"
#include "minivideo_containers.h"
// minivideo headers
#include "minivideo_mediafile.h"
 
// C standard libraries
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
 
// C POSIX library
#ifdef _MSC_VER
    #include <direct.h>
    #define getcwd _getcwd
#else
    #include <unistd.h>
#endif
 
/* ************************************************************************** */
 
/*!
 * \brief Get various from a media filepath.
 * \param[in] *media: A pointer to a MediaFile_t structure, containing every informations available about the current media file.
 *
 * Get absolute file path, file directory, file name and extension.
 * This function will only work with Unix-style file systems.
 */
static void getInfosFromPath(MediaFile_t *media)
{
    TRACE_2(IO, "getInfosFromPath()");
 
    // Check if mediaFile->filepath is an absolute path
    char *pos_first_slash_p = strchr(media->file_path, '/');
 
    if ((pos_first_slash_p != NULL) && ((pos_first_slash_p - media->file_path) == 0))
    {
        TRACE_2(IO, "* mediaFile->file_path seems to be an absolute path already (first caracter is /)");
    }
    else
    {
        char cwd[4096];
        char absolute_filepath[4096];
        FILE *temp = NULL;
 
        // First attempt
        if (getcwd(cwd, sizeof(cwd)) != NULL)
        {
            strncpy(absolute_filepath, strncat(cwd, media->file_path, sizeof(cwd) - 1), sizeof(absolute_filepath) - 1);
            temp = fopen(absolute_filepath, "rb");
        }
 
        if (temp != NULL)
        {
            TRACE_2(IO, "* New absolute file path found, new using method 1: '%s'", absolute_filepath);
            strncpy(media->file_path, absolute_filepath, sizeof(media->file_path) - 1);
            fclose(temp);
        }
        else
        {
            // Second attempt
            if (getcwd(cwd, sizeof(cwd)) != NULL)
            {
                strncpy(absolute_filepath, strncat(cwd, "/", 1), sizeof(absolute_filepath) - 1);
                strncat(absolute_filepath, media->file_path, sizeof(absolute_filepath) - 1);
                temp = fopen(absolute_filepath, "rb");
            }
 
            if (temp != NULL)
            {
                TRACE_2(IO, "* New absolute file path found, new using method 2");
                strncpy(media->file_path, absolute_filepath, sizeof(media->file_path) - 1);
                fclose(temp);
            }
            else
            {
                TRACE_2(IO, "* mediaFile->file_path seems to be an absolute path already");
            }
        }
    }
 
    // Get directory
    char *pos_last_slash_p = strrchr(media->file_path, '/');
    if (pos_last_slash_p != NULL)
    {
        unsigned int pos_last_slash_i = pos_last_slash_p - media->file_path + 1;
        if (pos_last_slash_i > sizeof(media->file_directory) - 1)
        {
            pos_last_slash_i = sizeof(media->file_directory) - 1;
        }
 
        // Set directory
        strncpy(media->file_directory, media->file_path, pos_last_slash_i);
 
        // Get file name
        char *pos_last_dot_p = strrchr(media->file_path, '.');
        if (pos_last_dot_p != NULL)
        {
            unsigned int pos_last_dot_i = pos_last_dot_p - media->file_path - pos_last_slash_i;
            if (pos_last_dot_i > sizeof(media->file_name) - 1)
            {
                pos_last_dot_i = sizeof(media->file_name) - 1;
            }
 
            // Set file name
            strncpy(media->file_name, pos_last_slash_p + 1, pos_last_dot_i);
 
            // Set file extension (without the dot)
            strncpy(media->file_extension, pos_last_dot_p + 1, sizeof(media->file_extension) - 1);
        }
        else
        {
            TRACE_WARNING(IO, "* Cannot find file extension!");
 
            // Set file name (without the extension)
            strncpy(media->file_name, pos_last_slash_p + 1, 254);
        }
    }
    else
    {
        TRACE_WARNING(IO, "* Cannot find file directory, name and extension!");
    }
 
    // Print results
    TRACE_1(IO, "* File path      : '%s'", media->file_path);
    TRACE_1(IO, "* File directory : '%s'", media->file_directory);
    TRACE_1(IO, "* File name      : '%s'", media->file_name);
    TRACE_1(IO, "* File extension : '%s'", media->file_extension);
}
 
/* ************************************************************************** */
 
/*!
 * \brief Get media file size.
 * \param[in] *media: A pointer to a MediaFile_t structure containing various informations about the file.
 */
static void getSize(MediaFile_t *media)
{
    TRACE_2(IO, "getSize()");
 
    fseek(media->file_pointer, 0, SEEK_END);
    media->file_size = (int64_t)ftell(media->file_pointer);
    rewind(media->file_pointer);
 
    if (media->file_size < 1024) // < 1 KiB
    {
        TRACE_1(IO, "* File size      : %i bytes", media->file_size);
    }
    else if (media->file_size < 1048576) // < 1 MiB
    {
        TRACE_1(IO, "* File size      : %.2f KiB (%.2f KB)", (double)media->file_size / 1024.0, (double)media->file_size / 1000.0);
    }
    else // >= 1 MiB
    {
        TRACE_1(IO, "* File size      : %.2f MiB (%.2f MB)", (double)media->file_size / 1024.0 / 1024.0, (double)media->file_size / 1000.0 / 1000.0);
    }
}
 
/* ************************************************************************** */
 
/*!
 * \brief Detect the container used by a multimedia file.
 * \param[in] *media: A pointer to a MediaFile_t structure, containing every informations available about the current media file.
 */
static void getContainer(MediaFile_t *media)
{
    media->container = CONTAINER_UNKNOWN;
 
    // Detect container format using start codes, by readind the first bytes of the file
    rewind(media->file_pointer);
    uint8_t buffer[16];
    if (fread(buffer, sizeof(uint8_t), sizeof(buffer), media->file_pointer) == sizeof(buffer))
    {
        media->container =  getContainerUsingStartcodes(buffer);
    }
 
    if (media->container == CONTAINER_UNKNOWN)
    {
        TRACE_WARNING(IO, "* Unknown container format: startcodes detection failed...");
 
        // Fallback: detect container format using file extension
        std::string ext = media->file_extension;
        media->container = getContainerUsingExtension(ext);
 
        if (media->container == CONTAINER_UNKNOWN)
        {
            TRACE_ERROR(IO, "* Unknown container format: file extension detection failed...");
        }
    }
}
 
/* ************************************************************************** */
/* ************************************************************************** */
 
/*!
 * \brief Open a file and check what's inside it.
 * \param[in] *filepath: The path of the file to load.
 * \param[in,out] **media_ptr: A pointer to a MediaFile_t structure, containing every informations available about the current media file.
 *
 * Some more informations about supported files:
 * Size and offset are coded on int64_t (signed long long), so this library should
 * be able to handle file of 1073741824 GiB if Large File Support is enabled during
 * compilation, 2 GiB otherwise.
 * The filename is limited to 255 caracters (including file extension) and the
 * complete filepath is limited to 4096 caracters.
 *
 * These parameters will only work on POSIX compliant operating system.
 */
int import_fileOpen(const char *filepath, MediaFile_t **media_ptr)
{
    TRACE_INFO(IO, BLD_GREEN "import_fileOpen()" CLR_RESET);
 
    int retcode = FAILURE;
 
    if (filepath == NULL)
    {
        TRACE_ERROR(IO, "* File path is invalid");
    }
    else
    {
        // Allocate media structure and create a shortcut
        *media_ptr = (MediaFile_t*)calloc(1, sizeof(MediaFile_t));
 
        if (*media_ptr == NULL)
        {
            TRACE_ERROR(IO, "* Unable to allocate a MediaFile_t structure!");
        }
        else
        {
            MediaFile_t *media = (*media_ptr);
 
            // Set filepath in MediaFile_t
            strncpy(media->file_path, filepath, sizeof(media->file_path) - 1);
            TRACE_INFO(IO, "* File path (raw): '%s'", filepath);
 
            // Open file, read only
            media->file_pointer = fopen(filepath, "rb");
 
            if (media->file_pointer == NULL)
            {
                TRACE_ERROR(IO, "Unable to open the media file: '%s'!", filepath);
                free(*media_ptr);
                *media_ptr = NULL;
            }
            else
            {
                TRACE_1(IO, "* File successfully opened");
 
                // Extract some informations from the media file
                getInfosFromPath(media);
                getSize(media);
                getContainer(media);
 
                retcode = SUCCESS;
            }
        }
    }
 
    return retcode;
}
 
/* ************************************************************************** */
 
/*!
 * \brief Close a file.
 * \param[in,out] **media_ptr: A pointer of pointer to a MediaFile_t structure.
 * \return 1 if success, 0 otherwise.
 */
int import_fileClose(MediaFile_t **media_ptr)
{
    TRACE_INFO(IO, BLD_GREEN "import_fileClose()" CLR_RESET);
    int retcode = SUCCESS;
    int i = 0;
 
    if ((*media_ptr) != NULL)
    {
        if ((*media_ptr)->file_pointer != NULL)
        {
            if (fclose((*media_ptr)->file_pointer) == 0)
            {
                TRACE_1(IO, "* File successfully closed");
                retcode = SUCCESS;
            }
            else
            {
                TRACE_ERROR(IO, "Unable to close that file!");
                retcode = FAILURE;
            }
        }
 
        {
            free(*media_ptr);
            *media_ptr = NULL;
 
            TRACE_1(IO, ">> MediaFile freed");
        }
    }
 
    return retcode;
}
 
/* ************************************************************************** */
 
/*!
 * \brief Print various informations about a file.
 * \param[in] *media: A pointer to a MediaFile_t structure, containing every informations available about the current media file.
 */
void import_fileStatus(MediaFile_t *media)
{
    TRACE_INFO(IO, BLD_GREEN "import_fileStatus()" CLR_RESET);
 
    unsigned i = 0;
 
    // File
    if (media->file_pointer)
    {
        TRACE_1(IO, "file_pointer is " BLD_GREEN "open" CLR_RESET);
    }
    else
    {
        TRACE_1(IO, "file_pointer is " BLD_RED "closed" CLR_RESET);
    }
 
    // File info
    TRACE_1(IO, "* File path      : '%s'", media->file_path);
    TRACE_1(IO, "* File directory : '%s'", media->file_directory);
    TRACE_1(IO, "* File name      : '%s'", media->file_name);
    TRACE_1(IO, "* File extension : '%s'", media->file_extension);
    TRACE_1(IO, "* File size      : %i MiB  /  %i MB",
            media->file_size / 1024 / 1024,
            media->file_size / 1000 / 1000);
 
    // File format
    TRACE_1(IO, "* File container :  '%s'", getContainerString(media->container, 1));
 
}
 
/* ************************************************************************** */