demux_ts: fix crash on broken packets
[mplayer/glamo.git] / edl.c
blob677a2efb24be7f5152281db686706cf76d385ac8
1 /*
2 * This file is part of MPlayer.
4 * MPlayer is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * MPlayer is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include "config.h"
22 #include "mp_msg.h"
23 #include "edl.h"
25 char *edl_filename; // file to extract EDL entries from (-edl)
26 char *edl_output_filename; // file to put EDL entries in (-edlout)
28 /**
29 * Allocates a new EDL record and makes sure allocation was successful.
31 * \return New allocated EDL record.
32 * \brief Allocate new EDL record
35 static edl_record_ptr edl_alloc_new(edl_record_ptr next_edl_record)
37 edl_record_ptr new_record = calloc(1, sizeof(struct edl_record));
38 if (!new_record) {
39 mp_tmsg(MSGT_CPLAYER, MSGL_FATAL, "Can't allocate enough memory to hold EDL data.\n");
40 exit(1);
43 if (next_edl_record) // if this isn't the first record, tell the previous one what the new one is.
44 next_edl_record->next = new_record;
45 new_record->prev = next_edl_record;
46 new_record->next = NULL;
48 return new_record;
51 /**
52 * Goes through entire EDL records and frees all memory.
53 * Assumes next_edl_record is valid or NULL.
55 * \brief Free EDL memory
58 void free_edl(edl_record_ptr next_edl_record)
60 edl_record_ptr tmp;
61 while (next_edl_record) {
62 tmp = next_edl_record->next;
63 free(next_edl_record);
64 next_edl_record = tmp;
68 /** Parses edl_filename to fill EDL operations queue.
69 * Prints out how many EDL operations recorded total.
70 * \brief Fills EDL operations queue.
73 edl_record_ptr edl_parse_file(void)
75 FILE *fd;
76 char line[100];
77 float start, stop;
78 int action;
79 int record_count = 0;
80 int lineCount = 0;
81 edl_record_ptr edl_records = NULL;
82 edl_record_ptr next_edl_record = NULL;
84 if (edl_filename)
86 if ((fd = fopen(edl_filename, "r")) == NULL)
88 return NULL;
91 while (fgets(line, 99, fd) != NULL)
93 lineCount++;
95 if ((sscanf(line, "%f %f %d", &start, &stop, &action))
96 != 3)
98 mp_tmsg(MSGT_CPLAYER, MSGL_WARN, "Badly formatted EDL line [%d], discarding.\n",
99 lineCount);
100 continue;
103 if (next_edl_record && start <= next_edl_record->stop_sec)
105 mp_tmsg(MSGT_CPLAYER, MSGL_WARN, "Invalid EDL line: %s\n", line);
106 mp_tmsg(MSGT_CPLAYER, MSGL_WARN,
107 "Last stop position was [%f]; next start is [%f].\n"\
108 "Entries must be in chronological order, cannot overlap. Discarding.\n",
109 next_edl_record->stop_sec, start);
110 continue;
113 if (stop <= start)
115 mp_tmsg(MSGT_CPLAYER, MSGL_WARN, "Invalid EDL line: %s\n",
116 line);
117 mp_tmsg(MSGT_CPLAYER, MSGL_WARN, "Stop time has to be after start time.\n");
118 continue;
121 next_edl_record = edl_alloc_new(next_edl_record);
123 if (!edl_records) edl_records = next_edl_record;
125 next_edl_record->action = action;
127 if (action == EDL_MUTE)
129 next_edl_record->length_sec = 0;
130 next_edl_record->start_sec = start;
131 next_edl_record->stop_sec = start;
133 next_edl_record = edl_alloc_new(next_edl_record);
135 next_edl_record->action = action;
136 next_edl_record->length_sec = 0;
137 next_edl_record->start_sec = stop;
138 next_edl_record->stop_sec = stop;
139 } else
141 next_edl_record->length_sec = stop - start;
142 next_edl_record->start_sec = start;
143 next_edl_record->stop_sec = stop;
146 record_count++;
149 fclose(fd);
152 if (edl_records)
153 mp_tmsg(MSGT_CPLAYER, MSGL_INFO, "Read %d EDL actions.\n", record_count);
154 else
155 mp_tmsg(MSGT_CPLAYER, MSGL_INFO, "There are no EDL actions to take care of.\n");
157 return edl_records;