video: remove lowres support, cut "too slow" message
[mplayer.git] / playtreeparser.c
blobe23463e113c2ccd9aac80ec0328d78b736b5e965
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 /// \file
20 /// \ingroup PlaytreeParser
22 #include "config.h"
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <assert.h>
27 #include <errno.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <fcntl.h>
31 #include <unistd.h>
32 #include <ctype.h>
33 #include <limits.h>
35 #include "talloc.h"
36 #include "asxparser.h"
37 #include "playtree.h"
38 #include "playtreeparser.h"
39 #include "stream/stream.h"
40 #include "libmpdemux/demuxer.h"
41 #include "mp_msg.h"
44 #define BUF_STEP 1024
46 #define WHITES " \n\r\t"
48 static void
49 strstrip(char* str) {
50 char* i;
52 if (str==NULL)
53 return;
54 for(i = str ; i[0] != '\0' && strchr(WHITES,i[0]) != NULL; i++)
55 /* NOTHING */;
56 if(i[0] != '\0') {
57 memmove(str,i,strlen(i) + 1);
58 for(i = str + strlen(str) - 1 ; strchr(WHITES,i[0]) != NULL; i--)
59 /* NOTHING */;
60 i[1] = '\0';
61 } else
62 str[0] = '\0';
65 static char*
66 play_tree_parser_get_line(play_tree_parser_t* p) {
67 char *end,*line_end;
68 int r,resize = 0;
70 if(p->buffer == NULL) {
71 p->buffer = malloc(BUF_STEP);
72 p->buffer_size = BUF_STEP;
73 p->buffer[0] = 0;
74 p->iter = p->buffer;
77 if(p->stream->eof && (p->buffer_end == 0 || p->iter[0] == '\0'))
78 return NULL;
80 assert(p->buffer_end < p->buffer_size);
81 assert(!p->buffer[p->buffer_end]);
82 while(1) {
84 if(resize) {
85 char *tmp;
86 r = p->iter - p->buffer;
87 end = p->buffer + p->buffer_end;
88 if (p->buffer_size > INT_MAX - BUF_STEP)
89 break;
90 tmp = realloc(p->buffer, p->buffer_size + BUF_STEP);
91 if (!tmp)
92 break;
93 p->buffer = tmp;
94 p->iter = p->buffer + r;
95 p->buffer_size += BUF_STEP;
96 resize = 0;
99 if(p->buffer_size - p->buffer_end > 1 && ! p->stream->eof) {
100 r = stream_read(p->stream,p->buffer + p->buffer_end,p->buffer_size - p->buffer_end - 1);
101 if(r > 0) {
102 p->buffer_end += r;
103 assert(p->buffer_end < p->buffer_size);
104 p->buffer[p->buffer_end] = '\0';
105 while(strlen(p->buffer + p->buffer_end - r) != r)
106 p->buffer[p->buffer_end - r + strlen(p->buffer + p->buffer_end - r)] = '\n';
108 assert(!p->buffer[p->buffer_end]);
111 end = strchr(p->iter,'\n');
112 if(!end) {
113 if(p->stream->eof) {
114 end = p->buffer + p->buffer_end;
115 break;
117 resize = 1;
118 continue;
120 break;
123 line_end = (end > p->iter && *(end-1) == '\r') ? end-1 : end;
124 if(line_end - p->iter >= 0)
125 p->line = realloc(p->line, line_end - p->iter + 1);
126 else
127 return NULL;
128 if(line_end - p->iter > 0)
129 strncpy(p->line,p->iter,line_end - p->iter);
130 p->line[line_end - p->iter] = '\0';
131 if(end[0] != '\0')
132 end++;
134 if(!p->keep) {
135 if(end[0] != '\0') {
136 p->buffer_end -= end-p->iter;
137 memmove(p->buffer,end,p->buffer_end);
138 } else
139 p->buffer_end = 0;
140 p->buffer[p->buffer_end] = '\0';
141 p->iter = p->buffer;
142 } else
143 p->iter = end;
145 return p->line;
148 static void
149 play_tree_parser_reset(play_tree_parser_t* p) {
150 p->iter = p->buffer;
153 static void
154 play_tree_parser_stop_keeping(play_tree_parser_t* p) {
155 p->keep = 0;
156 if(p->iter && p->iter != p->buffer) {
157 p->buffer_end -= p->iter -p->buffer;
158 if(p->buffer_end)
159 memmove(p->buffer,p->iter,p->buffer_end);
160 p->buffer[p->buffer_end] = 0;
161 p->iter = p->buffer;
166 static play_tree_t*
167 parse_asx(play_tree_parser_t* p) {
168 int comments = 0,get_line = 1;
169 char* line = NULL;
171 mp_msg(MSGT_PLAYTREE,MSGL_V,"Trying asx...\n");
173 while(1) {
174 if(get_line) {
175 line = play_tree_parser_get_line(p);
176 if(!line)
177 return NULL;
178 strstrip(line);
179 if(line[0] == '\0')
180 continue;
182 if(!comments) {
183 if(line[0] != '<') {
184 mp_msg(MSGT_PLAYTREE,MSGL_DBG2,"First char isn't '<' but '%c'\n",line[0]);
185 mp_msg(MSGT_PLAYTREE,MSGL_DBG3,"Buffer = [%s]\n",p->buffer);
186 return NULL;
187 } else if(strncmp(line,"<!--",4) == 0) { // Comments
188 comments = 1;
189 line += 4;
190 if(line[0] != '\0' && strlen(line) > 0)
191 get_line = 0;
192 } else if(strncasecmp(line,"<ASX",4) == 0) // We got an asx element
193 break;
194 else // We don't get an asx
195 return NULL;
196 } else { // Comments
197 char* c;
198 c = strchr(line,'-');
199 if(c) {
200 if (strncmp(c,"--!>",4) == 0) { // End of comments
201 comments = 0;
202 line = c+4;
203 if(line[0] != '\0') // There is some more data on this line : keep it
204 get_line = 0;
206 } else {
207 line = c+1; // Jump the -
208 if(line[0] != '\0') // Some more data
209 get_line = 0;
210 else // End of line
211 get_line = 1;
213 } else // No - on this line (or rest of line) : get next one
214 get_line = 1;
218 mp_msg(MSGT_PLAYTREE,MSGL_V,"Detected asx format\n");
220 // We have an asx : load it in memory and parse
222 while((line = play_tree_parser_get_line(p)) != NULL)
223 /* NOTHING */;
225 mp_msg(MSGT_PLAYTREE,MSGL_DBG3,"Parsing asx file: [%s]\n",p->buffer);
226 return asx_parser_build_tree(p->opts, p->buffer,p->deep);
229 static char*
230 pls_entry_get_value(char* line) {
231 char* i;
233 i = strchr(line,'=');
234 if(!i || i[1] == '\0')
235 return NULL;
236 else
237 return i+1;
240 typedef struct pls_entry {
241 char* file;
242 char* title;
243 char* length;
244 } pls_entry_t;
246 static int
247 pls_read_entry(char* line,pls_entry_t** _e,int* _max_entry,char** val) {
248 int num,max_entry = (*_max_entry);
249 pls_entry_t* e = (*_e);
250 int limit = INT_MAX / sizeof(*e);
251 char* v;
253 v = pls_entry_get_value(line);
254 if(!v) {
255 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"No value in entry %s\n",line);
256 return -1;
259 num = atoi(line);
260 if(num <= 0 || num > limit) {
261 if (max_entry >= limit) {
262 mp_msg(MSGT_PLAYTREE, MSGL_WARN, "Too many index entries\n");
263 return -1;
265 num = max_entry+1;
266 mp_msg(MSGT_PLAYTREE,MSGL_WARN,"No or invalid entry index in entry %s\nAssuming %d\n",line,num);
268 if(num > max_entry) {
269 e = realloc(e, num * sizeof(pls_entry_t));
270 if (!e)
271 return -1;
272 memset(&e[max_entry],0,(num-max_entry)*sizeof(pls_entry_t));
273 max_entry = num;
275 (*_e) = e;
276 (*_max_entry) = max_entry;
277 (*val) = v;
279 return num;
283 static play_tree_t*
284 parse_pls(play_tree_parser_t* p) {
285 char *line,*v;
286 pls_entry_t* entries = NULL;
287 int n_entries = 0,max_entry=0,num;
288 play_tree_t *list = NULL, *entry = NULL, *last_entry = NULL;
290 mp_msg(MSGT_PLAYTREE,MSGL_V,"Trying Winamp playlist...\n");
291 while((line = play_tree_parser_get_line(p))) {
292 strstrip(line);
293 if(strlen(line))
294 break;
296 if (!line)
297 return NULL;
298 if(strcasecmp(line,"[playlist]"))
299 return NULL;
300 mp_msg(MSGT_PLAYTREE,MSGL_V,"Detected Winamp playlist format\n");
301 play_tree_parser_stop_keeping(p);
302 line = play_tree_parser_get_line(p);
303 if(!line)
304 return NULL;
305 strstrip(line);
306 if(strncasecmp(line,"NumberOfEntries",15) == 0) {
307 v = pls_entry_get_value(line);
308 n_entries = atoi(v);
309 if(n_entries < 0)
310 mp_msg(MSGT_PLAYTREE,MSGL_DBG2,"Invalid number of entries: very funny!!!\n");
311 else
312 mp_msg(MSGT_PLAYTREE,MSGL_DBG2,"Playlist claims to have %d entries. Let's see.\n",n_entries);
313 line = play_tree_parser_get_line(p);
316 while(line) {
317 strstrip(line);
318 if(line[0] == '\0') {
319 line = play_tree_parser_get_line(p);
320 continue;
322 if(strncasecmp(line,"File",4) == 0) {
323 num = pls_read_entry(line+4,&entries,&max_entry,&v);
324 if(num < 0)
325 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"No value in entry %s\n",line);
326 else
327 entries[num-1].file = strdup(v);
328 } else if(strncasecmp(line,"Title",5) == 0) {
329 num = pls_read_entry(line+5,&entries,&max_entry,&v);
330 if(num < 0)
331 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"No value in entry %s\n",line);
332 else
333 entries[num-1].title = strdup(v);
334 } else if(strncasecmp(line,"Length",6) == 0) {
335 num = pls_read_entry(line+6,&entries,&max_entry,&v);
336 if(num < 0)
337 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"No value in entry %s\n",line);
338 else {
339 char *end;
340 long val = strtol(v, &end, 10);
341 if (*end || (val <= 0 && val != -1))
342 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"Invalid length value in entry %s\n",line);
343 else if (val > 0)
344 entries[num-1].length = strdup(v);
346 } else
347 mp_msg(MSGT_PLAYTREE,MSGL_WARN,"Unknown entry type %s\n",line);
348 line = play_tree_parser_get_line(p);
351 for(num = 0; num < max_entry ; num++) {
352 if(entries[num].file == NULL)
353 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"Entry %d don't have a file !!!!\n",num+1);
354 else {
355 mp_msg(MSGT_PLAYTREE,MSGL_DBG2,"Adding entry %s\n",entries[num].file);
356 entry = play_tree_new();
357 play_tree_add_file(entry,entries[num].file);
358 if (entries[num].length)
359 play_tree_set_param(entry, bstr("endpos"), bstr(entries[num].length));
360 free(entries[num].file);
361 if(list)
362 play_tree_append_entry(last_entry,entry);
363 else
364 list = entry;
365 last_entry = entry;
367 // When we have info in playtree we add these info
368 free(entries[num].title);
369 free(entries[num].length);
372 free(entries);
374 if (!list)
375 return NULL;
377 entry = play_tree_new();
378 play_tree_set_child(entry,list);
379 return entry;
383 Reference Ini-Format: Each entry is assumed a reference
385 static play_tree_t*
386 parse_ref_ini(play_tree_parser_t* p) {
387 char *line,*v;
388 play_tree_t *list = NULL, *entry = NULL, *last_entry = NULL;
390 mp_msg(MSGT_PLAYTREE,MSGL_V,"Trying reference-ini playlist...\n");
391 if (!(line = play_tree_parser_get_line(p)))
392 return NULL;
393 strstrip(line);
394 if(strcasecmp(line,"[Reference]"))
395 return NULL;
396 mp_msg(MSGT_PLAYTREE,MSGL_V,"Detected reference-ini playlist format\n");
397 play_tree_parser_stop_keeping(p);
398 line = play_tree_parser_get_line(p);
399 if(!line)
400 return NULL;
401 while(line) {
402 strstrip(line);
403 if(strncasecmp(line,"Ref",3) == 0) {
404 v = pls_entry_get_value(line+3);
405 if(!v)
406 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"No value in entry %s\n",line);
407 else
409 mp_msg(MSGT_PLAYTREE,MSGL_DBG2,"Adding entry %s\n",v);
410 entry = play_tree_new();
411 play_tree_add_file(entry,v);
412 if(list)
413 play_tree_append_entry(last_entry,entry);
414 else
415 list = entry;
416 last_entry = entry;
419 line = play_tree_parser_get_line(p);
422 if(!list) return NULL;
423 entry = play_tree_new();
424 play_tree_set_child(entry,list);
425 return entry;
428 static play_tree_t*
429 parse_m3u(play_tree_parser_t* p) {
430 char* line;
431 play_tree_t *list = NULL, *entry = NULL, *last_entry = NULL;
433 mp_msg(MSGT_PLAYTREE,MSGL_V,"Trying extended m3u playlist...\n");
434 if (!(line = play_tree_parser_get_line(p)))
435 return NULL;
436 strstrip(line);
437 if(strcasecmp(line,"#EXTM3U"))
438 return NULL;
439 mp_msg(MSGT_PLAYTREE,MSGL_V,"Detected extended m3u playlist format\n");
440 play_tree_parser_stop_keeping(p);
442 while((line = play_tree_parser_get_line(p)) != NULL) {
443 strstrip(line);
444 if(line[0] == '\0')
445 continue;
446 /* EXTM3U files contain such lines:
447 * #EXTINF:<seconds>, <title>
448 * followed by a line with the filename
449 * for now we have no place to put that
450 * so we just skip that extra-info ::atmos
452 if(line[0] == '#') {
453 #if 0 /* code functional */
454 if(strncasecmp(line,"#EXTINF:",8) == 0) {
455 mp_msg(MSGT_PLAYTREE,MSGL_INFO,"[M3U] Duration: %dsec Title: %s\n",
456 strtol(line+8,&line,10), line+2);
458 #endif
459 continue;
461 entry = play_tree_new();
462 play_tree_add_file(entry,line);
463 if(!list)
464 list = entry;
465 else
466 play_tree_append_entry(last_entry,entry);
467 last_entry = entry;
470 if(!list) return NULL;
471 entry = play_tree_new();
472 play_tree_set_child(entry,list);
473 return entry;
476 static play_tree_t*
477 parse_smil(play_tree_parser_t* p) {
478 int entrymode=0;
479 char* line,source[512],*pos,*s_start,*s_end,*src_line;
480 play_tree_t *list = NULL, *entry = NULL, *last_entry = NULL;
481 int is_rmsmil = 0;
482 unsigned int npkt, ttlpkt;
484 mp_msg(MSGT_PLAYTREE,MSGL_V,"Trying smil playlist...\n");
486 // Check if smil
487 while((line = play_tree_parser_get_line(p)) != NULL) {
488 strstrip(line);
489 if(line[0] == '\0') // Ignore empties
490 continue;
491 if (strncasecmp(line,"<?xml",5)==0) // smil in xml
492 continue;
493 if (strncasecmp(line,"<!DOCTYPE smil",13)==0) // smil in xml
494 continue;
495 if (strncasecmp(line,"<smil",5)==0 || strncasecmp(line,"<?wpl",5)==0 ||
496 strncasecmp(line,"(smil-document",14)==0)
497 break; // smil header found
498 else
499 return NULL; //line not smil exit
502 if (!line) return NULL;
503 mp_msg(MSGT_PLAYTREE,MSGL_V,"Detected smil playlist format\n");
504 play_tree_parser_stop_keeping(p);
506 if (strncasecmp(line,"(smil-document",14)==0) {
507 mp_msg(MSGT_PLAYTREE,MSGL_V,"Special smil-over-realrtsp playlist header\n");
508 is_rmsmil = 1;
509 if (sscanf(line, "(smil-document (ver 1.0)(npkt %u)(ttlpkt %u", &npkt, &ttlpkt) != 2) {
510 mp_msg(MSGT_PLAYTREE,MSGL_WARN,"smil-over-realrtsp: header parsing failure, assuming single packet.\n");
511 npkt = ttlpkt = 1;
513 if (ttlpkt == 0 || npkt > ttlpkt) {
514 mp_msg(MSGT_PLAYTREE,MSGL_WARN,"smil-over-realrtsp: bad packet counters (npkk = %u, ttlpkt = %u), assuming single packet.\n",
515 npkt, ttlpkt);
516 npkt = ttlpkt = 1;
520 //Get entries from smil
521 src_line = line;
522 line = NULL;
523 do {
524 strstrip(src_line);
525 free(line);
526 line = NULL;
527 /* If we're parsing smil over realrtsp and this is not the last packet and
528 * this is the last line in the packet (terminating with ") ) we must get
529 * the next line, strip the header, and concatenate it to the current line.
531 if (is_rmsmil && npkt != ttlpkt && strstr(src_line,"\")")) {
532 char *payload;
534 line = strdup(src_line);
535 if(!(src_line = play_tree_parser_get_line(p))) {
536 mp_msg(MSGT_PLAYTREE,MSGL_WARN,"smil-over-realrtsp: can't get line from packet %u/%u.\n", npkt, ttlpkt);
537 break;
539 strstrip(src_line);
540 // Skip header, packet starts after "
541 if(!(payload = strchr(src_line,'\"'))) {
542 mp_msg(MSGT_PLAYTREE,MSGL_WARN,"smil-over-realrtsp: can't find start of packet, using complete line.\n");
543 payload = src_line;
544 } else
545 payload++;
546 // Skip ") at the end of the last line from the current packet
547 line[strlen(line)-2] = 0;
548 line = realloc(line, strlen(line)+strlen(payload)+1);
549 strcat (line, payload);
550 npkt++;
551 } else
552 line = strdup(src_line);
553 /* Unescape \" to " for smil-over-rtsp */
554 if (is_rmsmil && line[0] != '\0') {
555 int i, j;
557 for (i = 0; i < strlen(line); i++)
558 if (line[i] == '\\' && line[i+1] == '"')
559 for (j = i; line[j]; j++)
560 line[j] = line[j+1];
562 pos = line;
563 while (pos) {
564 if (!entrymode) { // all entries filled so far
565 while ((pos=strchr(pos, '<'))) {
566 if (strncasecmp(pos,"<video",6)==0 || strncasecmp(pos,"<audio",6)==0 || strncasecmp(pos,"<media",6)==0) {
567 entrymode=1;
568 break; // Got a valid tag, exit '<' search loop
570 pos++;
573 if (entrymode) { //Entry found but not yet filled
574 pos = strstr(pos,"src="); // Is source present on this line
575 if (pos != NULL) {
576 entrymode=0;
577 if (pos[4] != '"' && pos[4] != '\'') {
578 mp_msg(MSGT_PLAYTREE,MSGL_V,"Unknown delimiter %c in source line %s\n", pos[4], line);
579 break;
581 s_start=pos+5;
582 s_end=strchr(s_start,pos[4]);
583 if (s_end == NULL) {
584 mp_msg(MSGT_PLAYTREE,MSGL_V,"Error parsing this source line %s\n",line);
585 break;
587 if (s_end-s_start> 511) {
588 mp_msg(MSGT_PLAYTREE,MSGL_V,"Cannot store such a large source %s\n",line);
589 break;
591 strncpy(source,s_start,s_end-s_start);
592 source[(s_end-s_start)]='\0'; // Null terminate
593 entry = play_tree_new();
594 play_tree_add_file(entry,source);
595 if(!list) //Insert new entry
596 list = entry;
597 else
598 play_tree_append_entry(last_entry,entry);
599 last_entry = entry;
600 pos = s_end;
604 } while((src_line = play_tree_parser_get_line(p)) != NULL);
606 free(line);
608 if(!list) return NULL; // Nothing found
610 entry = play_tree_new();
611 play_tree_set_child(entry,list);
612 return entry;
615 static play_tree_t*
616 embedded_playlist_parse(struct MPOpts *opts, char *line) {
617 int f=DEMUXER_TYPE_PLAYLIST;
618 stream_t* stream;
619 play_tree_parser_t* ptp;
620 play_tree_t* entry;
622 // Get stream opened to link
623 stream=open_stream(line, opts, &f);
624 if(!stream) {
625 mp_msg(MSGT_PLAYTREE,MSGL_WARN,"Can't open playlist %s\n",line);
626 return NULL;
629 //add new playtree
630 mp_msg(MSGT_PLAYTREE,MSGL_V,"Adding playlist %s to element entryref\n",line);
632 ptp = play_tree_parser_new(stream, opts, 1);
633 entry = play_tree_parser_get_play_tree(ptp, 1);
634 play_tree_parser_free(ptp);
635 free_stream(stream);
637 return entry;
640 static play_tree_t*
641 parse_textplain(play_tree_parser_t* p) {
642 char* line;
643 char *c;
644 int embedded;
645 play_tree_t *list = NULL, *entry = NULL, *last_entry = NULL;
647 mp_msg(MSGT_PLAYTREE,MSGL_V,"Trying plaintext playlist...\n");
648 play_tree_parser_stop_keeping(p);
650 while((line = play_tree_parser_get_line(p)) != NULL) {
651 strstrip(line);
652 if(line[0] == '\0' || line[0] == '#' || (line[0] == '/' && line[1] == '/'))
653 continue;
655 //Special check for embedded smil or ram reference in file
656 embedded = 0;
657 if (strlen(line) > 5)
658 for(c = line; c[0]; c++ )
659 if ( ((c[0] == '.') && //start with . and next have smil with optional ? or &
660 (tolower(c[1]) == 's') && (tolower(c[2])== 'm') &&
661 (tolower(c[3]) == 'i') && (tolower(c[4]) == 'l') &&
662 (!c[5] || c[5] == '?' || c[5] == '&')) || // or
663 ((c[0] == '.') && // start with . and next have smi or ram with optional ? or &
664 ( ((tolower(c[1]) == 's') && (tolower(c[2])== 'm') && (tolower(c[3]) == 'i')) ||
665 ((tolower(c[1]) == 'r') && (tolower(c[2])== 'a') && (tolower(c[3]) == 'm')) )
666 && (!c[4] || c[4] == '?' || c[4] == '&')) ){
667 entry=embedded_playlist_parse(p->opts, line);
668 embedded = 1;
669 break;
672 if (!embedded) { //regular file link
673 entry = play_tree_new();
674 play_tree_add_file(entry,line);
677 if (entry != NULL) {
678 if(!list)
679 list = entry;
680 else
681 play_tree_append_entry(last_entry,entry);
682 last_entry = entry;
686 if(!list) return NULL;
687 entry = play_tree_new();
688 play_tree_set_child(entry,list);
689 return entry;
693 * \brief decode the base64 used in nsc files
694 * \param in input string, 0-terminated
695 * \param buf output buffer, must point to memory suitable for realloc,
696 * will be NULL on failure.
697 * \return decoded length in bytes
699 static int decode_nsc_base64(char *in, char **buf) {
700 int i, j, n;
701 if (in[0] != '0' || in[1] != '2')
702 goto err_out;
703 in += 2; // skip prefix
704 if (strlen(in) < 16) // error out if nothing to decode
705 goto err_out;
706 in += 12; // skip encoded string length
707 n = strlen(in) / 4;
708 *buf = realloc(*buf, n * 3);
709 for (i = 0; i < n; i++) {
710 uint8_t c[4];
711 for (j = 0; j < 4; j++) {
712 c[j] = in[4 * i + j];
713 if (c[j] >= '0' && c[j] <= '9') c[j] += 0 - '0';
714 else if (c[j] >= 'A' && c[j] <= 'Z') c[j] += 10 - 'A';
715 else if (c[j] >= 'a' && c[j] <= 'z') c[j] += 36 - 'a';
716 else if (c[j] == '{') c[j] = 62;
717 else if (c[j] == '}') c[j] = 63;
718 else {
719 mp_msg(MSGT_PLAYTREE, MSGL_ERR, "Invalid character %c (0x%02"PRIx8")\n", c[j], c[j]);
720 goto err_out;
723 (*buf)[3 * i] = (c[0] << 2) | (c[1] >> 4);
724 (*buf)[3 * i + 1] = (c[1] << 4) | (c[2] >> 2);
725 (*buf)[3 * i + 2] = (c[2] << 6) | c[3];
727 return 3 * n;
728 err_out:
729 free(*buf);
730 *buf = NULL;
731 return 0;
735 * \brief "converts" utf16 to ascii by just discarding every second byte
736 * \param buf buffer to convert
737 * \param len lenght of buffer, must be > 0
739 static void utf16_to_ascii(char *buf, int len) {
740 int i;
741 if (len <= 0) return;
742 for (i = 0; i < len / 2; i++)
743 buf[i] = buf[i * 2];
744 buf[i] = 0; // just in case
747 static play_tree_t *parse_nsc(play_tree_parser_t* p) {
748 char *line, *addr = NULL, *url, *unicast_url = NULL;
749 int port = 0;
750 play_tree_t *entry = NULL;
752 mp_msg(MSGT_PLAYTREE,MSGL_V,"Trying nsc playlist...\n");
753 while((line = play_tree_parser_get_line(p)) != NULL) {
754 strstrip(line);
755 if(!line[0]) // Ignore empties
756 continue;
757 if (strncasecmp(line,"[Address]", 9) == 0)
758 break; // nsc header found
759 else
760 return NULL;
762 mp_msg(MSGT_PLAYTREE,MSGL_V,"Detected nsc playlist format\n");
763 play_tree_parser_stop_keeping(p);
764 while ((line = play_tree_parser_get_line(p)) != NULL) {
765 strstrip(line);
766 if (!line[0])
767 continue;
768 if (strncasecmp(line, "Unicast URL=", 12) == 0) {
769 int len = decode_nsc_base64(&line[12], &unicast_url);
770 if (len <= 0)
771 mp_msg(MSGT_PLAYTREE, MSGL_WARN, "[nsc] Unsupported Unicast URL encoding\n");
772 else
773 utf16_to_ascii(unicast_url, len);
774 } else if (strncasecmp(line, "IP Address=", 11) == 0) {
775 int len = decode_nsc_base64(&line[11], &addr);
776 if (len <= 0)
777 mp_msg(MSGT_PLAYTREE, MSGL_WARN, "[nsc] Unsupported IP Address encoding\n");
778 else
779 utf16_to_ascii(addr, len);
780 } else if (strncasecmp(line, "IP Port=", 8) == 0) {
781 port = strtol(&line[8], NULL, 0);
785 if (unicast_url)
786 url = strdup(unicast_url);
787 else if (addr && port) {
788 url = malloc(strlen(addr) + 7 + 20 + 1);
789 sprintf(url, "http://%s:%i", addr, port);
790 } else
791 goto out;
793 entry = play_tree_new();
794 play_tree_add_file(entry, url);
795 free(url);
796 out:
797 free(addr);
798 free(unicast_url);
799 return entry;
802 play_tree_t*
803 parse_playtree(stream_t *stream, struct MPOpts *opts, int forced) {
804 play_tree_parser_t* p;
805 play_tree_t* ret;
807 assert(stream != NULL);
809 p = play_tree_parser_new(stream, opts, 0);
810 if(!p)
811 return NULL;
813 ret = play_tree_parser_get_play_tree(p, forced);
814 play_tree_parser_free(p);
816 return ret;
819 static void
820 play_tree_add_basepath(play_tree_t* pt, char* bp) {
821 int i,bl = strlen(bp),fl;
823 if(pt->child) {
824 play_tree_t* i;
825 for(i = pt->child ; i != NULL ; i = i->next)
826 play_tree_add_basepath(i,bp);
827 return;
830 if(!pt->files)
831 return;
833 for(i = 0 ; pt->files[i] != NULL ; i++) {
834 fl = strlen(pt->files[i]);
835 // if we find a full unix path, url:// or X:\ at the beginning,
836 // don't mangle it.
837 if(fl <= 0 || strstr(pt->files[i],"://") || (strstr(pt->files[i],":\\") == pt->files[i] + 1) || (pt->files[i][0] == '/') )
838 continue;
839 // if the path begins with \ then prepend drive letter to it.
840 if (pt->files[i][0] == '\\') {
841 if (pt->files[i][1] == '\\')
842 continue;
843 pt->files[i] = realloc(pt->files[i], 2 + fl + 1);
844 memmove(pt->files[i] + 2,pt->files[i],fl+1);
845 memcpy(pt->files[i],bp,2);
846 continue;
848 pt->files[i] = realloc(pt->files[i], bl + fl + 1);
849 memmove(pt->files[i] + bl,pt->files[i],fl+1);
850 memcpy(pt->files[i],bp,bl);
854 // Wrapper for play_tree_add_basepath (add base path from file)
855 void play_tree_add_bpf(play_tree_t *pt, struct bstr filename)
857 char *ls, *file;
859 if (pt)
861 file = bstrdup0(NULL, filename);
862 if (file)
864 ls = strrchr(file,'/');
865 if(!ls) ls = strrchr(file,'\\');
866 if(ls) {
867 ls[1] = '\0';
868 play_tree_add_basepath(pt,file);
870 talloc_free(file);
875 play_tree_t*
876 parse_playlist_file(struct MPOpts *opts, struct bstr file) {
877 stream_t *stream;
878 play_tree_t* ret;
879 int f=DEMUXER_TYPE_PLAYLIST;
881 char *file0 = bstrdup0(NULL, file);
882 stream = open_stream(file0, opts, &f);
883 talloc_free(file0);
885 if(!stream) {
886 mp_msg(MSGT_PLAYTREE,MSGL_ERR,
887 "Error while opening playlist file %.*s: %s\n",
888 BSTR_P(file), strerror(errno));
889 return NULL;
892 mp_msg(MSGT_PLAYTREE, MSGL_V,
893 "Parsing playlist file %.*s...\n", BSTR_P(file));
895 ret = parse_playtree(stream, opts, 1);
896 free_stream(stream);
898 play_tree_add_bpf(ret, file);
900 return ret;
905 play_tree_parser_t*
906 play_tree_parser_new(stream_t* stream, struct MPOpts *opts, int deep) {
907 play_tree_parser_t* p;
909 p = calloc(1,sizeof(play_tree_parser_t));
910 if(!p)
911 return NULL;
912 p->stream = stream;
913 p->opts = opts;
914 p->deep = deep;
915 p->keep = 1;
917 return p;
921 void
922 play_tree_parser_free(play_tree_parser_t* p) {
924 assert(p != NULL);
926 free(p->buffer);
927 free(p->line);
928 free(p);
931 play_tree_t*
932 play_tree_parser_get_play_tree(play_tree_parser_t* p, int forced) {
933 play_tree_t* tree = NULL;
935 assert(p != NULL);
937 while(play_tree_parser_get_line(p) != NULL) {
938 play_tree_parser_reset(p);
940 tree = parse_asx(p);
941 if(tree) break;
942 play_tree_parser_reset(p);
944 tree = parse_pls(p);
945 if(tree) break;
946 play_tree_parser_reset(p);
948 tree = parse_m3u(p);
949 if(tree) break;
950 play_tree_parser_reset(p);
952 tree = parse_ref_ini(p);
953 if(tree) break;
954 play_tree_parser_reset(p);
956 tree = parse_smil(p);
957 if(tree) break;
958 play_tree_parser_reset(p);
960 tree = parse_nsc(p);
961 if(tree) break;
962 play_tree_parser_reset(p);
964 // Here come the others formats ( textplain must stay the last one )
965 if (forced)
967 tree = parse_textplain(p);
968 if(tree) break;
970 break;
973 if(tree)
974 mp_msg(MSGT_PLAYTREE,MSGL_V,"Playlist successfully parsed\n");
975 else
976 mp_msg(MSGT_PLAYTREE,((forced==1)?MSGL_ERR:MSGL_V),"Error while parsing playlist\n");
978 if(tree)
979 tree = play_tree_cleanup(tree);
981 if(!tree) mp_msg(MSGT_PLAYTREE,((forced==1)?MSGL_WARN:MSGL_V),"Warning: empty playlist\n");
983 return tree;