1.1161: misc fixes [partly]
[mplayer/greg.git] / edl.c
blob0433b68bc14b7e3d8a15438bc98a9596d8f3c889
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include "config.h"
4 #include "mp_msg.h"
5 #include "edl.h"
6 #include "help_mp.h"
8 char *edl_filename; // file to extract EDL entries from (-edl)
9 char *edl_output_filename; // file to put EDL entries in (-edlout)
11 #ifdef USE_EDL
13 /**
14 * We can't do -edl and -edlout at the same time
15 * so we check that here.
17 * \return EDL_ERROR on error and 1 otherwise.
18 * \brief Makes sure EDL has been called correctly.
21 int edl_check_mode(void)
23 if (edl_filename && edl_output_filename)
25 return (EDL_ERROR);
28 return (1);
31 /**
32 * Allocates a new EDL record and makes sure allocation was successful.
34 * \return New allocated EDL record.
35 * \brief Allocate new EDL record
38 static edl_record_ptr edl_alloc_new(edl_record_ptr next_edl_record)
40 edl_record_ptr new_record = calloc(1, sizeof(struct edl_record));
41 if (!new_record) {
42 mp_msg(MSGT_CPLAYER, MSGL_FATAL, MSGTR_EdlOutOfMem);
43 exit(1);
46 if (next_edl_record) // if this isn't the first record, tell the previous one what the new one is.
47 next_edl_record->next = new_record;
48 new_record->prev = next_edl_record;
50 return new_record;
53 /**
54 * Goes through entire EDL records and frees all memory.
55 * Assumes next_edl_record is valid or NULL.
57 * \brief Free EDL memory
60 void free_edl(edl_record_ptr next_edl_record)
62 edl_record_ptr tmp;
63 while (next_edl_record) {
64 tmp = next_edl_record->next;
65 free(next_edl_record);
66 next_edl_record = tmp;
70 /** Parses edl_filename to fill EDL operations queue.
71 * Prints out how many EDL operations recorded total.
72 * \brief Fills EDL operations queue.
75 edl_record_ptr edl_parse_file()
77 FILE *fd;
78 char line[100];
79 float start, stop;
80 int action;
81 int record_count = 0;
82 int lineCount = 0;
83 edl_record_ptr edl_records = edl_alloc_new(NULL);
84 edl_record_ptr next_edl_record = edl_records;
86 if (edl_filename)
88 if ((fd = fopen(edl_filename, "r")) == NULL)
90 return NULL;
91 } else
93 while (fgets(line, 99, fd) != NULL)
95 lineCount++;
96 if ((sscanf(line, "%f %f %d", &start, &stop, &action))
97 != 3)
99 mp_msg(MSGT_CPLAYER, MSGL_WARN, MSGTR_EdlBadlyFormattedLine,
100 lineCount + 1);
101 continue;
102 } else
104 if (next_edl_record->prev && start <= next_edl_record->prev->stop_sec)
106 mp_msg(MSGT_CPLAYER, MSGL_WARN, MSGTR_EdlNOValidLine, line);
107 mp_msg(MSGT_CPLAYER, MSGL_WARN, MSGTR_EdlBadLineOverlap,
108 next_edl_record->prev->stop_sec, start);
109 continue;
111 if (stop <= start)
113 mp_msg(MSGT_CPLAYER, MSGL_WARN, MSGTR_EdlNOValidLine,
114 line);
115 mp_msg(MSGT_CPLAYER, MSGL_WARN, MSGTR_EdlBadLineBadStop);
116 continue;
118 next_edl_record->action = action;
119 if (action == EDL_MUTE)
121 next_edl_record->length_sec = 0;
122 next_edl_record->start_sec = start;
123 next_edl_record->stop_sec = start;
125 next_edl_record = edl_alloc_new(next_edl_record);
127 next_edl_record->action = action;
128 next_edl_record->length_sec = 0;
129 next_edl_record->start_sec = stop;
130 next_edl_record->stop_sec = stop;
131 } else
133 next_edl_record->length_sec = stop - start;
134 next_edl_record->start_sec = start;
135 next_edl_record->stop_sec = stop;
137 next_edl_record = edl_alloc_new(next_edl_record);
138 record_count++;
142 fclose(fd);
144 if (next_edl_record->prev) {
145 next_edl_record->prev->next = NULL; // a record was before me, i don't want them thinking i'm a real record.
146 mp_msg(MSGT_CPLAYER, MSGL_INFO, MSGTR_EdlRecordsNo, record_count);
148 else {
149 mp_msg(MSGT_CPLAYER, MSGL_INFO, MSGTR_EdlQueueEmpty);
150 edl_records = NULL; // there was no previous record, we only had one record, the empty one.
152 free(next_edl_record);
153 return edl_records;
156 #endif