summaryrefslogtreecommitdiffstats
path: root/edl.c
blob: 78360bf4f6795f7678be92c1e0186233b0ad71de (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
#include <stdio.h>
#include <stdlib.h>
#include "config.h"
#include "mp_msg.h"
#include "edl.h"

#ifdef USE_EDL

int edl_check_mode(void)
{
    if (edl_filename && edl_output_filename)
    {
        return (EDL_ERROR);
    }

    return (1);
}

int edl_count_entries(void)
{
    FILE *fd = NULL;
    int entries = 0;
    int action = 0;
    float start = 0;
    float stop = 0;
    char line[100];

    if (edl_filename)
    {
        if ((fd = fopen(edl_filename, "r")) == NULL)
        {
            mp_msg(MSGT_CPLAYER, MSGL_WARN,
                   "Invalid EDL file, cant open '%s' for reading!\n",
                   edl_filename);
            return (EDL_ERROR);
        } else
        {
            while (fgets(line, 99, fd) != NULL)
            {
                if ((sscanf(line, "%f %f %d", &start, &stop, &action)) ==
                    3)
                {
                    if (action == EDL_SKIP)
                        entries += 1;
                    if (action == EDL_MUTE)
                        entries += 2;
                } else
                {
                    mp_msg(MSGT_CPLAYER, MSGL_WARN,
                           "Invalid EDL line: %s\n", line);
                    return (EDL_ERROR);
                }

            }
        }
    } else
    {
        return (EDL_ERROR);
    }

    return (entries);
}

int edl_parse_file(edl_record_ptr edl_records)
{
    FILE *fd;
    char line[100];
    float start, stop;
    int action;
    int record_count = 0;
    int lineCount = 0;
    struct edl_record *next_edl_record = edl_records;

    if (edl_filename)
    {
        if ((fd = fopen(edl_filename, "r")) == NULL)
        {
            return (EDL_ERROR);
        } else
        {
            while (fgets(line, 99, fd) != NULL)
            {
                lineCount++;
                if ((sscanf(line, "%f %f %d", &start, &stop, &action))
                    != 3)
                {
                    mp_msg(MSGT_CPLAYER, MSGL_WARN,
                           "Badly formated EDL line [%d]. Discarding!\n",
                           lineCount + 1, line);
                    continue;
                } else
                {
                    if (record_count > 0)
                    {
                        if (start <= (next_edl_record - 1)->stop_sec)
                        {
                            mp_msg(MSGT_CPLAYER, MSGL_WARN,
                                   "Invalid EDL line [%d]: %s",
                                   lineCount, line);
                            mp_msg(MSGT_CPLAYER, MSGL_WARN,
                                   "Last stop position was [%f]; next start is [%f]. Entries must be in chronological order and cannot overlap. Discarding!\n",
                                   (next_edl_record - 1)->stop_sec, start);
                            continue;
                        }
                    }
                    if (stop <= start)
                    {
                        mp_msg(MSGT_CPLAYER, MSGL_WARN,
                               "Invalid EDL line [%d]: %s", lineCount,
                               line);
                        mp_msg(MSGT_CPLAYER, MSGL_WARN,
                               "Stop time must follow start time. Discarding!\n");
                        continue;
                    }
                    next_edl_record->action = action;
                    if (action == EDL_MUTE)
                    {
                        next_edl_record->length_sec = 0;
                        next_edl_record->start_sec = start;
                        next_edl_record->stop_sec = start;
                        next_edl_record->mute_state = EDL_MUTE_START;
                        next_edl_record++;
                        (next_edl_record - 1)->next = next_edl_record;
                        next_edl_record->action = action;
                        next_edl_record->length_sec = 0;
                        next_edl_record->start_sec = stop;
                        next_edl_record->stop_sec = stop;
                        next_edl_record->mute_state = EDL_MUTE_END;

                    } else
                    {
                        next_edl_record->length_sec = stop - start;
                        next_edl_record->start_sec = start;
                        next_edl_record->stop_sec = stop;
                    }
                    next_edl_record++;

                    if (record_count >= 0)
                    {
                        (next_edl_record - 1)->next = next_edl_record;
                    }

                    record_count++;
                }
            }

            if (record_count > 0)
            {
                (next_edl_record - 1)->next = NULL;
            }
        }
        fclose(fd);
    } else
    {
        return (EDL_ERROR);
    }

    return (record_count);
}

#endif