aboutsummaryrefslogtreecommitdiffhomepage
path: root/edl.c
blob: 35950bdfdecf61cf3b613ac06925f85e59358efd (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
#include <stdio.h>
#include <stdlib.h>
#include "config.h"
#include "mp_msg.h"
#include "edl.h"
#include "help_mp.h"

char *edl_filename; // file to extract EDL entries from (-edl)
char *edl_output_filename; // file to put EDL entries in (-edlout)

/**
 *  Allocates a new EDL record and makes sure allocation was successful.
 *
 *  \return New allocated EDL record.
 *  \brief Allocate new EDL record
 */

static edl_record_ptr edl_alloc_new(edl_record_ptr next_edl_record)
{
    edl_record_ptr new_record = calloc(1, sizeof(struct edl_record));
    if (!new_record) {
        mp_tmsg(MSGT_CPLAYER, MSGL_FATAL, "Can't allocate enough memory to hold EDL data.\n");
        exit(1);
    }

    if (next_edl_record) // if this isn't the first record, tell the previous one what the new one is.
        next_edl_record->next = new_record;
    new_record->prev = next_edl_record;
    new_record->next = NULL;

    return new_record;
}

/**
 *  Goes through entire EDL records and frees all memory.
 *  Assumes next_edl_record is valid or NULL.
 *
 *  \brief Free EDL memory
 */

void free_edl(edl_record_ptr next_edl_record)
{
    edl_record_ptr tmp;
    while (next_edl_record) {
        tmp = next_edl_record->next;
        free(next_edl_record);
        next_edl_record = tmp;
    }
}

/** Parses edl_filename to fill EDL operations queue.
 * Prints out how many EDL operations recorded total.
 *  \brief Fills EDL operations queue.
 */

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

    if (edl_filename)
    {
        if ((fd = fopen(edl_filename, "r")) == NULL)
        {
            return NULL;
        }

        while (fgets(line, 99, fd) != NULL)
        {
            lineCount++;

            if ((sscanf(line, "%f %f %d", &start, &stop, &action))
                != 3)
            {
                mp_tmsg(MSGT_CPLAYER, MSGL_WARN, "Badly formatted EDL line [%d], discarding.\n",
                       lineCount);
                continue;
            }

            if (next_edl_record && start <= next_edl_record->stop_sec)
            {
                mp_tmsg(MSGT_CPLAYER, MSGL_WARN, "Invalid EDL line: %s\n", line);
                mp_tmsg(MSGT_CPLAYER, MSGL_WARN,
					"Last stop position was [%f]; next start is [%f].\n"\
					"Entries must be in chronological order, cannot overlap. Discarding.\n",
                       next_edl_record->stop_sec, start);
                continue;
            }

            if (stop <= start)
            {
                mp_tmsg(MSGT_CPLAYER, MSGL_WARN, "Invalid EDL line: %s\n",
                       line);
                mp_tmsg(MSGT_CPLAYER, MSGL_WARN, "Stop time has to be after start time.\n");
                continue;
            }

            next_edl_record = edl_alloc_new(next_edl_record);

            if (!edl_records) edl_records = next_edl_record;

            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 = edl_alloc_new(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;
            } else
            {
                next_edl_record->length_sec = stop - start;
                next_edl_record->start_sec = start;
                next_edl_record->stop_sec = stop;
            }

            record_count++;
        }

        fclose(fd);
    }

    if (edl_records)
        mp_tmsg(MSGT_CPLAYER, MSGL_INFO, "Read %d EDL actions.\n", record_count);
    else
        mp_tmsg(MSGT_CPLAYER, MSGL_INFO, "There are no EDL actions to take care of.\n");

    return edl_records;
}