]> git.sesse.net Git - ffmpeg/blob - ffserver_config.c
ffserver_config: improve AVOption handing
[ffmpeg] / ffserver_config.c
1 /*
2  * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20
21 #include <float.h>
22 #include "libavutil/opt.h"
23 #include "libavutil/parseutils.h"
24 #include "libavutil/avstring.h"
25 #include "libavutil/pixdesc.h"
26 #include "libavutil/avassert.h"
27
28 // FIXME those are internal headers, ffserver _really_ shouldn't use them
29 #include "libavformat/ffm.h"
30
31 #include "cmdutils.h"
32 #include "ffserver_config.h"
33
34 /* FIXME: make ffserver work with IPv6 */
35 /* resolve host with also IP address parsing */
36 static int resolve_host(struct in_addr *sin_addr, const char *hostname)
37 {
38
39     if (!ff_inet_aton(hostname, sin_addr)) {
40 #if HAVE_GETADDRINFO
41         struct addrinfo *ai, *cur;
42         struct addrinfo hints = { 0 };
43         hints.ai_family = AF_INET;
44         if (getaddrinfo(hostname, NULL, &hints, &ai))
45             return -1;
46         /* getaddrinfo returns a linked list of addrinfo structs.
47          * Even if we set ai_family = AF_INET above, make sure
48          * that the returned one actually is of the correct type. */
49         for (cur = ai; cur; cur = cur->ai_next) {
50             if (cur->ai_family == AF_INET) {
51                 *sin_addr = ((struct sockaddr_in *)cur->ai_addr)->sin_addr;
52                 freeaddrinfo(ai);
53                 return 0;
54             }
55         }
56         freeaddrinfo(ai);
57         return -1;
58 #else
59         struct hostent *hp;
60         hp = gethostbyname(hostname);
61         if (!hp)
62             return -1;
63         memcpy(sin_addr, hp->h_addr_list[0], sizeof(struct in_addr));
64 #endif
65     }
66     return 0;
67 }
68
69 void ffserver_get_arg(char *buf, int buf_size, const char **pp)
70 {
71     const char *p;
72     char *q;
73     int quote;
74
75     p = *pp;
76     while (av_isspace(*p)) p++;
77     q = buf;
78     quote = 0;
79     if (*p == '\"' || *p == '\'')
80         quote = *p++;
81     for(;;) {
82         if (quote) {
83             if (*p == quote)
84                 break;
85         } else {
86             if (av_isspace(*p))
87                 break;
88         }
89         if (*p == '\0')
90             break;
91         if ((q - buf) < buf_size - 1)
92             *q++ = *p;
93         p++;
94     }
95     *q = '\0';
96     if (quote && *p == quote)
97         p++;
98     *pp = p;
99 }
100
101 void ffserver_parse_acl_row(FFServerStream *stream, FFServerStream* feed,
102                             FFServerIPAddressACL *ext_acl,
103                             const char *p, const char *filename, int line_num)
104 {
105     char arg[1024];
106     FFServerIPAddressACL acl;
107     int errors = 0;
108
109     ffserver_get_arg(arg, sizeof(arg), &p);
110     if (av_strcasecmp(arg, "allow") == 0)
111         acl.action = IP_ALLOW;
112     else if (av_strcasecmp(arg, "deny") == 0)
113         acl.action = IP_DENY;
114     else {
115         fprintf(stderr, "%s:%d: ACL action '%s' is not ALLOW or DENY\n",
116                 filename, line_num, arg);
117         errors++;
118     }
119
120     ffserver_get_arg(arg, sizeof(arg), &p);
121
122     if (resolve_host(&acl.first, arg) != 0) {
123         fprintf(stderr, "%s:%d: ACL refers to invalid host or IP address '%s'\n",
124                 filename, line_num, arg);
125         errors++;
126     } else
127         acl.last = acl.first;
128
129     ffserver_get_arg(arg, sizeof(arg), &p);
130
131     if (arg[0]) {
132         if (resolve_host(&acl.last, arg) != 0) {
133             fprintf(stderr, "%s:%d: ACL refers to invalid host or IP address '%s'\n",
134                     filename, line_num, arg);
135             errors++;
136         }
137     }
138
139     if (!errors) {
140         FFServerIPAddressACL *nacl = av_mallocz(sizeof(*nacl));
141         FFServerIPAddressACL **naclp = 0;
142
143         acl.next = 0;
144         *nacl = acl;
145
146         if (stream)
147             naclp = &stream->acl;
148         else if (feed)
149             naclp = &feed->acl;
150         else if (ext_acl)
151             naclp = &ext_acl;
152         else {
153             fprintf(stderr, "%s:%d: ACL found not in <stream> or <feed>\n",
154                     filename, line_num);
155             errors++;
156         }
157
158         if (naclp) {
159             while (*naclp)
160                 naclp = &(*naclp)->next;
161
162             *naclp = nacl;
163         } else
164             av_free(nacl);
165     }
166 }
167
168 /* add a codec and set the default parameters */
169 static void add_codec(FFServerStream *stream, AVCodecContext *av)
170 {
171     AVStream *st;
172
173     if(stream->nb_streams >= FF_ARRAY_ELEMS(stream->streams))
174         return;
175
176     /* compute default parameters */
177     switch(av->codec_type) {
178     case AVMEDIA_TYPE_AUDIO:
179         if (av->bit_rate == 0)
180             av->bit_rate = 64000;
181         if (av->sample_rate == 0)
182             av->sample_rate = 22050;
183         if (av->channels == 0)
184             av->channels = 1;
185         break;
186     case AVMEDIA_TYPE_VIDEO:
187         if (av->bit_rate == 0)
188             av->bit_rate = 64000;
189         if (av->time_base.num == 0){
190             av->time_base.den = 5;
191             av->time_base.num = 1;
192         }
193         if (av->width == 0 || av->height == 0) {
194             av->width = 160;
195             av->height = 128;
196         }
197         /* Bitrate tolerance is less for streaming */
198         if (av->bit_rate_tolerance == 0)
199             av->bit_rate_tolerance = FFMAX(av->bit_rate / 4,
200                       (int64_t)av->bit_rate*av->time_base.num/av->time_base.den);
201         if (av->qmin == 0)
202             av->qmin = 3;
203         if (av->qmax == 0)
204             av->qmax = 31;
205         if (av->max_qdiff == 0)
206             av->max_qdiff = 3;
207         av->qcompress = 0.5;
208         av->qblur = 0.5;
209
210         if (!av->nsse_weight)
211             av->nsse_weight = 8;
212
213         av->frame_skip_cmp = FF_CMP_DCTMAX;
214         if (!av->me_method)
215             av->me_method = ME_EPZS;
216         av->rc_buffer_aggressivity = 1.0;
217
218         if (!av->rc_eq)
219             av->rc_eq = av_strdup("tex^qComp");
220         if (!av->i_quant_factor)
221             av->i_quant_factor = -0.8;
222         if (!av->b_quant_factor)
223             av->b_quant_factor = 1.25;
224         if (!av->b_quant_offset)
225             av->b_quant_offset = 1.25;
226         if (!av->rc_max_rate)
227             av->rc_max_rate = av->bit_rate * 2;
228
229         if (av->rc_max_rate && !av->rc_buffer_size) {
230             av->rc_buffer_size = av->rc_max_rate;
231         }
232
233
234         break;
235     default:
236         abort();
237     }
238
239     st = av_mallocz(sizeof(AVStream));
240     if (!st)
241         return;
242     st->codec = av;
243     stream->streams[stream->nb_streams++] = st;
244 }
245
246 static enum AVCodecID opt_codec(const char *name, enum AVMediaType type)
247 {
248     AVCodec *codec = avcodec_find_encoder_by_name(name);
249
250     if (!codec || codec->type != type)
251         return AV_CODEC_ID_NONE;
252     return codec->id;
253 }
254
255 static int ffserver_opt_default(const char *opt, const char *arg,
256                        AVCodecContext *avctx, int type)
257 {
258     int ret = 0;
259     const AVOption *o = av_opt_find(avctx, opt, NULL, type, 0);
260     if(o)
261         ret = av_opt_set(avctx, opt, arg, 0);
262     return ret;
263 }
264
265 static int ffserver_opt_preset(const char *arg,
266                        AVCodecContext *avctx, int type,
267                        enum AVCodecID *audio_id, enum AVCodecID *video_id)
268 {
269     FILE *f=NULL;
270     char filename[1000], tmp[1000], tmp2[1000], line[1000];
271     int ret = 0;
272     AVCodec *codec = NULL;
273
274     if (avctx)
275         codec = avcodec_find_encoder(avctx->codec_id);
276
277     if (!(f = get_preset_file(filename, sizeof(filename), arg, 0,
278                               codec ? codec->name : NULL))) {
279         fprintf(stderr, "File for preset '%s' not found\n", arg);
280         return AVERROR(EINVAL);
281     }
282
283     while(!feof(f)){
284         int e= fscanf(f, "%999[^\n]\n", line) - 1;
285         if(line[0] == '#' && !e)
286             continue;
287         e|= sscanf(line, "%999[^=]=%999[^\n]\n", tmp, tmp2) - 2;
288         if(e){
289             fprintf(stderr, "%s: Invalid syntax: '%s'\n", filename, line);
290             ret = AVERROR(EINVAL);
291             break;
292         }
293         if (audio_id && !strcmp(tmp, "acodec")) {
294             *audio_id = opt_codec(tmp2, AVMEDIA_TYPE_AUDIO);
295         } else if (video_id && !strcmp(tmp, "vcodec")){
296             *video_id = opt_codec(tmp2, AVMEDIA_TYPE_VIDEO);
297         } else if(!strcmp(tmp, "scodec")) {
298             /* opt_subtitle_codec(tmp2); */
299         } else if (avctx && (ret = ffserver_opt_default(tmp, tmp2, avctx, type)) < 0) {
300             fprintf(stderr, "%s: Invalid option or argument: '%s', parsed as '%s' = '%s'\n", filename, line, tmp, tmp2);
301             break;
302         }
303     }
304
305     fclose(f);
306
307     return ret;
308 }
309
310 static AVOutputFormat *ffserver_guess_format(const char *short_name, const char *filename, const char *mime_type)
311 {
312     AVOutputFormat *fmt = av_guess_format(short_name, filename, mime_type);
313
314     if (fmt) {
315         AVOutputFormat *stream_fmt;
316         char stream_format_name[64];
317
318         snprintf(stream_format_name, sizeof(stream_format_name), "%s_stream", fmt->name);
319         stream_fmt = av_guess_format(stream_format_name, NULL, NULL);
320
321         if (stream_fmt)
322             fmt = stream_fmt;
323     }
324
325     return fmt;
326 }
327
328 static void vreport_config_error(const char *filename, int line_num, int log_level, int *errors, const char *fmt, va_list vl)
329 {
330     av_log(NULL, log_level, "%s:%d: ", filename, line_num);
331     av_vlog(NULL, log_level, fmt, vl);
332     (*errors)++;
333 }
334
335 static void report_config_error(const char *filename, int line_num, int log_level, int *errors, const char *fmt, ...)
336 {
337     va_list vl;
338     va_start(vl, fmt);
339     vreport_config_error(filename, line_num, log_level, errors, fmt, vl);
340     va_end(vl);
341 }
342
343 static int ffserver_set_int_param(int *dest, const char *value, int factor, int min, int max,
344                                   FFServerConfig *config, int line_num, const char *error_msg, ...)
345 {
346     int tmp;
347     char *tailp;
348     if (!value || !value[0])
349         goto error;
350     errno = 0;
351     tmp = strtol(value, &tailp, 0);
352     if (tmp < min || tmp > max)
353         goto error;
354     if (factor) {
355         if (FFABS(tmp) > INT_MAX / FFABS(factor))
356             goto error;
357         tmp *= factor;
358     }
359     if (tailp[0] || errno)
360         goto error;
361     if (dest)
362         *dest = tmp;
363     return 0;
364   error:
365     if (config) {
366         va_list vl;
367         va_start(vl, error_msg);
368         vreport_config_error(config->filename, line_num, AV_LOG_ERROR, &config->errors, error_msg, vl);
369         va_end(vl);
370     }
371     return AVERROR(EINVAL);
372 }
373
374 static int ffserver_set_float_param(float *dest, const char *value, float factor, float min, float max,
375                                     FFServerConfig *config, int line_num, const char *error_msg, ...)
376 {
377     double tmp;
378     char *tailp;
379     if (!value || !value[0])
380         goto error;
381     errno = 0;
382     tmp = strtod(value, &tailp);
383     if (tmp < min || tmp > max)
384         goto error;
385     if (factor)
386         tmp *= factor;
387     if (tailp[0] || errno)
388         goto error;
389     if (dest)
390         *dest = tmp;
391     return 0;
392   error:
393     if (config) {
394         va_list vl;
395         va_start(vl, error_msg);
396         vreport_config_error(config->filename, line_num, AV_LOG_ERROR, &config->errors, error_msg, vl);
397         va_end(vl);
398     }
399     return AVERROR(EINVAL);
400 }
401
402 static int ffserver_save_avoption(const char *opt, const char *arg, AVDictionary **dict,
403                                   int type, FFServerConfig *config, int line_num)
404 {
405     int ret = 0;
406     AVDictionaryEntry *e;
407     const AVOption *o = av_opt_find(config->dummy_ctx, opt, NULL, type | AV_OPT_FLAG_ENCODING_PARAM, AV_OPT_SEARCH_CHILDREN);
408     if (!o) {
409         report_config_error(config->filename, line_num, AV_LOG_ERROR, &config->errors, "Option not found: %s\n", opt);
410     } else if ((ret = av_opt_set(config->dummy_ctx, opt, arg, AV_OPT_SEARCH_CHILDREN)) < 0) {
411         report_config_error(config->filename, line_num, AV_LOG_ERROR, &config->errors, "Invalid value for option %s (%s): %s\n", opt, arg, av_err2str(ret));
412     } else if ((e = av_dict_get(*dict, opt, NULL, 0))) {
413         if ((o->type == AV_OPT_TYPE_FLAGS) && arg && (arg[0] == '+' || arg[0] == '-'))
414             return av_dict_set(dict, opt, arg, AV_DICT_APPEND);
415         report_config_error(config->filename, line_num, AV_LOG_ERROR, &config->errors,
416                                 "Redeclaring value of the option %s, previous value: %s\n", opt, e->value);
417     } else if (av_dict_set(dict, opt, arg, 0) < 0) {
418         return AVERROR(ENOMEM);
419     }
420     return 0;
421 }
422
423 #define ERROR(...)   report_config_error(config->filename, line_num, AV_LOG_ERROR,   &config->errors,   __VA_ARGS__)
424 #define WARNING(...) report_config_error(config->filename, line_num, AV_LOG_WARNING, &config->warnings, __VA_ARGS__)
425
426 static int ffserver_parse_config_global(FFServerConfig *config, const char *cmd,
427                                         const char **p, int line_num)
428 {
429     int val;
430     char arg[1024];
431     if (!av_strcasecmp(cmd, "Port") || !av_strcasecmp(cmd, "HTTPPort")) {
432         if (!av_strcasecmp(cmd, "Port"))
433             WARNING("Port option is deprecated, use HTTPPort instead\n");
434         ffserver_get_arg(arg, sizeof(arg), p);
435         ffserver_set_int_param(&val, arg, 0, 1, 65535, config, line_num, "Invalid port: %s\n", arg);
436         if (val < 1024)
437             WARNING("Trying to use IETF assigned system port: %d\n", val);
438         config->http_addr.sin_port = htons(val);
439     } else if (!av_strcasecmp(cmd, "HTTPBindAddress") || !av_strcasecmp(cmd, "BindAddress")) {
440         if (!av_strcasecmp(cmd, "BindAddress"))
441             WARNING("BindAddress option is deprecated, use HTTPBindAddress instead\n");
442         ffserver_get_arg(arg, sizeof(arg), p);
443         if (resolve_host(&config->http_addr.sin_addr, arg) != 0)
444             ERROR("Invalid host/IP address: %s\n", arg);
445     } else if (!av_strcasecmp(cmd, "NoDaemon")) {
446         WARNING("NoDaemon option has no effect, you should remove it\n");
447     } else if (!av_strcasecmp(cmd, "RTSPPort")) {
448         ffserver_get_arg(arg, sizeof(arg), p);
449         ffserver_set_int_param(&val, arg, 0, 1, 65535, config, line_num, "Invalid port: %s\n", arg);
450         config->rtsp_addr.sin_port = htons(val);
451     } else if (!av_strcasecmp(cmd, "RTSPBindAddress")) {
452         ffserver_get_arg(arg, sizeof(arg), p);
453         if (resolve_host(&config->rtsp_addr.sin_addr, arg) != 0)
454             ERROR("Invalid host/IP address: %s\n", arg);
455     } else if (!av_strcasecmp(cmd, "MaxHTTPConnections")) {
456         ffserver_get_arg(arg, sizeof(arg), p);
457         ffserver_set_int_param(&val, arg, 0, 1, 65535, config, line_num, "Invalid MaxHTTPConnections: %s\n", arg);
458         config->nb_max_http_connections = val;
459         if (config->nb_max_connections > config->nb_max_http_connections)
460             ERROR("Inconsistent configuration: MaxClients(%d) > MaxHTTPConnections(%d)\n",
461                   config->nb_max_connections, config->nb_max_http_connections);
462     } else if (!av_strcasecmp(cmd, "MaxClients")) {
463         ffserver_get_arg(arg, sizeof(arg), p);
464         ffserver_set_int_param(&val, arg, 0, 1, 65535, config, line_num, "Invalid MaxClients: %s\n", arg);
465         config->nb_max_connections = val;
466         if (config->nb_max_connections > config->nb_max_http_connections)
467             ERROR("Inconsistent configuration: MaxClients(%d) > MaxHTTPConnections(%d)\n",
468                   config->nb_max_connections, config->nb_max_http_connections);
469     } else if (!av_strcasecmp(cmd, "MaxBandwidth")) {
470         int64_t llval;
471         char *tailp;
472         ffserver_get_arg(arg, sizeof(arg), p);
473         errno = 0;
474         llval = strtoll(arg, &tailp, 10);
475         if (llval < 10 || llval > 10000000 || tailp[0] || errno)
476             ERROR("Invalid MaxBandwidth: %s\n", arg);
477         else
478             config->max_bandwidth = llval;
479     } else if (!av_strcasecmp(cmd, "CustomLog")) {
480         if (!config->debug)
481             ffserver_get_arg(config->logfilename, sizeof(config->logfilename), p);
482     } else if (!av_strcasecmp(cmd, "LoadModule")) {
483         ERROR("Loadable modules are no longer supported\n");
484     } else
485         ERROR("Incorrect keyword: '%s'\n", cmd);
486     return 0;
487 }
488
489 static int ffserver_parse_config_feed(FFServerConfig *config, const char *cmd, const char **p,
490                                       int line_num, FFServerStream **pfeed)
491 {
492     FFServerStream *feed;
493     char arg[1024];
494     av_assert0(pfeed);
495     feed = *pfeed;
496     if (!av_strcasecmp(cmd, "<Feed")) {
497         char *q;
498         FFServerStream *s;
499         feed = av_mallocz(sizeof(FFServerStream));
500         if (!feed)
501             return AVERROR(ENOMEM);
502         ffserver_get_arg(feed->filename, sizeof(feed->filename), p);
503         q = strrchr(feed->filename, '>');
504         if (*q)
505             *q = '\0';
506
507         for (s = config->first_feed; s; s = s->next) {
508             if (!strcmp(feed->filename, s->filename))
509                 ERROR("Feed '%s' already registered\n", s->filename);
510         }
511
512         feed->fmt = av_guess_format("ffm", NULL, NULL);
513         /* default feed file */
514         snprintf(feed->feed_filename, sizeof(feed->feed_filename),
515                  "/tmp/%s.ffm", feed->filename);
516         feed->feed_max_size = 5 * 1024 * 1024;
517         feed->is_feed = 1;
518         feed->feed = feed; /* self feeding :-) */
519         *pfeed = feed;
520         return 0;
521     }
522     av_assert0(feed);
523     if (!av_strcasecmp(cmd, "Launch")) {
524         int i;
525
526         feed->child_argv = av_mallocz(64 * sizeof(char *));
527         if (!feed->child_argv)
528             return AVERROR(ENOMEM);
529         for (i = 0; i < 62; i++) {
530             ffserver_get_arg(arg, sizeof(arg), p);
531             if (!arg[0])
532                 break;
533
534             feed->child_argv[i] = av_strdup(arg);
535             if (!feed->child_argv[i])
536                 return AVERROR(ENOMEM);
537         }
538
539         feed->child_argv[i] =
540             av_asprintf("http://%s:%d/%s",
541                         (config->http_addr.sin_addr.s_addr == INADDR_ANY) ? "127.0.0.1" :
542                         inet_ntoa(config->http_addr.sin_addr), ntohs(config->http_addr.sin_port),
543                         feed->filename);
544         if (!feed->child_argv[i])
545             return AVERROR(ENOMEM);
546     } else if (!av_strcasecmp(cmd, "ACL")) {
547         ffserver_parse_acl_row(NULL, feed, NULL, *p, config->filename, line_num);
548     } else if (!av_strcasecmp(cmd, "File") || !av_strcasecmp(cmd, "ReadOnlyFile")) {
549         ffserver_get_arg(feed->feed_filename, sizeof(feed->feed_filename), p);
550         feed->readonly = !av_strcasecmp(cmd, "ReadOnlyFile");
551     } else if (!av_strcasecmp(cmd, "Truncate")) {
552         ffserver_get_arg(arg, sizeof(arg), p);
553         /* assume Truncate is true in case no argument is specified */
554         if (!arg[0]) {
555             feed->truncate = 1;
556         } else {
557             WARNING("Truncate N syntax in configuration file is deprecated, "
558                     "use Truncate alone with no arguments\n");
559             feed->truncate = strtod(arg, NULL);
560         }
561     } else if (!av_strcasecmp(cmd, "FileMaxSize")) {
562         char *p1;
563         double fsize;
564
565         ffserver_get_arg(arg, sizeof(arg), p);
566         p1 = arg;
567         fsize = strtod(p1, &p1);
568         switch(av_toupper(*p1)) {
569         case 'K':
570             fsize *= 1024;
571             break;
572         case 'M':
573             fsize *= 1024 * 1024;
574             break;
575         case 'G':
576             fsize *= 1024 * 1024 * 1024;
577             break;
578         default:
579             ERROR("Invalid file size: %s\n", arg);
580             break;
581         }
582         feed->feed_max_size = (int64_t)fsize;
583         if (feed->feed_max_size < FFM_PACKET_SIZE*4)
584             ERROR("Feed max file size is too small, must be at least %d\n", FFM_PACKET_SIZE*4);
585     } else if (!av_strcasecmp(cmd, "</Feed>")) {
586         *pfeed = NULL;
587     } else {
588         ERROR("Invalid entry '%s' inside <Feed></Feed>\n", cmd);
589     }
590     return 0;
591 }
592
593 static void ffserver_apply_stream_config(AVCodecContext *enc, const AVDictionary *conf, AVDictionary **opts)
594 {
595     AVDictionaryEntry *e;
596
597     /* Return values from ffserver_set_*_param are ignored.
598        Values are initially parsed and checked before inserting to AVDictionary. */
599
600     //video params
601     if ((e = av_dict_get(conf, "VideoBitRateRangeMin", NULL, 0)))
602         ffserver_set_int_param(&enc->rc_min_rate, e->value, 1000, INT_MIN, INT_MAX, NULL, 0, NULL);
603     if ((e = av_dict_get(conf, "VideoBitRateRangeMax", NULL, 0)))
604         ffserver_set_int_param(&enc->rc_max_rate, e->value, 1000, INT_MIN, INT_MAX, NULL, 0, NULL);
605     if ((e = av_dict_get(conf, "Debug", NULL, 0)))
606         ffserver_set_int_param(&enc->debug, e->value, 0, INT_MIN, INT_MAX, NULL, 0, NULL);
607     if ((e = av_dict_get(conf, "Strict", NULL, 0)))
608         ffserver_set_int_param(&enc->strict_std_compliance, e->value, 0, INT_MIN, INT_MAX, NULL, 0, NULL);
609     if ((e = av_dict_get(conf, "VideoBufferSize", NULL, 0)))
610         ffserver_set_int_param(&enc->rc_buffer_size, e->value, 8*1024, INT_MIN, INT_MAX, NULL, 0, NULL);
611     if ((e = av_dict_get(conf, "VideoBitRateTolerance", NULL, 0)))
612         ffserver_set_int_param(&enc->bit_rate_tolerance, e->value, 1000, INT_MIN, INT_MAX, NULL, 0, NULL);
613     if ((e = av_dict_get(conf, "VideoBitRate", NULL, 0)))
614         ffserver_set_int_param(&enc->bit_rate, e->value, 1000, INT_MIN, INT_MAX, NULL, 0, NULL);
615     if ((e = av_dict_get(conf, "VideoSizeWidth", NULL, 0)))
616         ffserver_set_int_param(&enc->width, e->value, 0, INT_MIN, INT_MAX, NULL, 0, NULL);
617     if ((e = av_dict_get(conf, "VideoSizeHeight", NULL, 0)))
618         ffserver_set_int_param(&enc->height, e->value, 0, INT_MIN, INT_MAX, NULL, 0, NULL);
619     if ((e = av_dict_get(conf, "PixelFormat", NULL, 0))) {
620         int val;
621         ffserver_set_int_param(&val, e->value, 0, INT_MIN, INT_MAX, NULL, 0, NULL);
622         enc->pix_fmt = val;
623     }
624     if ((e = av_dict_get(conf, "VideoGopSize", NULL, 0)))
625         ffserver_set_int_param(&enc->gop_size, e->value, 0, INT_MIN, INT_MAX, NULL, 0, NULL);
626     if ((e = av_dict_get(conf, "VideoFrameRateNum", NULL, 0)))
627         ffserver_set_int_param(&enc->time_base.num, e->value, 0, INT_MIN, INT_MAX, NULL, 0, NULL);
628     if ((e = av_dict_get(conf, "VideoFrameRateDen", NULL, 0)))
629         ffserver_set_int_param(&enc->time_base.den, e->value, 0, INT_MIN, INT_MAX, NULL, 0, NULL);
630     if ((e = av_dict_get(conf, "VideoQDiff", NULL, 0)))
631         ffserver_set_int_param(&enc->max_qdiff, e->value, 0, INT_MIN, INT_MAX, NULL, 0, NULL);
632     if ((e = av_dict_get(conf, "VideoQMax", NULL, 0)))
633         ffserver_set_int_param(&enc->qmax, e->value, 0, INT_MIN, INT_MAX, NULL, 0, NULL);
634     if ((e = av_dict_get(conf, "VideoQMin", NULL, 0)))
635         ffserver_set_int_param(&enc->qmin, e->value, 0, INT_MIN, INT_MAX, NULL, 0, NULL);
636     if ((e = av_dict_get(conf, "LumiMask", NULL, 0)))
637         ffserver_set_float_param(&enc->lumi_masking, e->value, 0, -FLT_MAX, FLT_MAX, NULL, 0, NULL);
638     if ((e = av_dict_get(conf, "DarkMask", NULL, 0)))
639         ffserver_set_float_param(&enc->dark_masking, e->value, 0, -FLT_MAX, FLT_MAX, NULL, 0, NULL);
640     if (av_dict_get(conf, "BitExact", NULL, 0))
641         enc->flags |= CODEC_FLAG_BITEXACT;
642     if (av_dict_get(conf, "DctFastint", NULL, 0))
643         enc->dct_algo  = FF_DCT_FASTINT;
644     if (av_dict_get(conf, "IdctSimple", NULL, 0))
645         enc->idct_algo = FF_IDCT_SIMPLE;
646     if (av_dict_get(conf, "VideoHighQuality", NULL, 0))
647         enc->mb_decision = FF_MB_DECISION_BITS;
648     if ((e = av_dict_get(conf, "VideoTag", NULL, 0)))
649         enc->codec_tag = MKTAG(e->value[0], e->value[1], e->value[2], e->value[3]);
650     if (av_dict_get(conf, "Qscale", NULL, 0)) {
651         enc->flags |= CODEC_FLAG_QSCALE;
652         ffserver_set_int_param(&enc->global_quality, e->value, FF_QP2LAMBDA, INT_MIN, INT_MAX, NULL, 0, NULL);
653     }
654     if (av_dict_get(conf, "Video4MotionVector", NULL, 0)) {
655         enc->mb_decision = FF_MB_DECISION_BITS; //FIXME remove
656         enc->flags |= CODEC_FLAG_4MV;
657     }
658     //audio params
659     if ((e = av_dict_get(conf, "AudioChannels", NULL, 0)))
660         ffserver_set_int_param(&enc->channels, e->value, 0, INT_MIN, INT_MAX, NULL, 0, NULL);
661     if ((e = av_dict_get(conf, "AudioSampleRate", NULL, 0)))
662         ffserver_set_int_param(&enc->sample_rate, e->value, 0, INT_MIN, INT_MAX, NULL, 0, NULL);
663     if ((e = av_dict_get(conf, "AudioBitRate", NULL, 0)))
664         ffserver_set_int_param(&enc->bit_rate, e->value, 0, INT_MIN, INT_MAX, NULL, 0, NULL);
665
666     av_opt_set_dict2(enc, opts, AV_OPT_SEARCH_CHILDREN);
667 }
668
669 static int ffserver_parse_config_stream(FFServerConfig *config, const char *cmd, const char **p,
670                                         int line_num, FFServerStream **pstream)
671 {
672     char arg[1024], arg2[1024];
673     FFServerStream *stream;
674     int val;
675
676     av_assert0(pstream);
677     stream = *pstream;
678
679     if (!av_strcasecmp(cmd, "<Stream")) {
680         char *q;
681         FFServerStream *s;
682         stream = av_mallocz(sizeof(FFServerStream));
683         if (!stream)
684             return AVERROR(ENOMEM);
685         config->dummy_ctx = avcodec_alloc_context3(NULL);
686         if (!config->dummy_ctx) {
687             av_free(stream);
688             return AVERROR(ENOMEM);
689         }
690         ffserver_get_arg(stream->filename, sizeof(stream->filename), p);
691         q = strrchr(stream->filename, '>');
692         if (q)
693             *q = '\0';
694
695         for (s = config->first_stream; s; s = s->next) {
696             if (!strcmp(stream->filename, s->filename))
697                 ERROR("Stream '%s' already registered\n", s->filename);
698         }
699
700         stream->fmt = ffserver_guess_format(NULL, stream->filename, NULL);
701         if (stream->fmt) {
702             config->audio_id = stream->fmt->audio_codec;
703             config->video_id = stream->fmt->video_codec;
704         } else {
705             config->audio_id = AV_CODEC_ID_NONE;
706             config->video_id = AV_CODEC_ID_NONE;
707         }
708         *pstream = stream;
709         return 0;
710     }
711     av_assert0(stream);
712     if (!av_strcasecmp(cmd, "Feed")) {
713         FFServerStream *sfeed;
714         ffserver_get_arg(arg, sizeof(arg), p);
715         sfeed = config->first_feed;
716         while (sfeed) {
717             if (!strcmp(sfeed->filename, arg))
718                 break;
719             sfeed = sfeed->next_feed;
720         }
721         if (!sfeed)
722             ERROR("Feed with name '%s' for stream '%s' is not defined\n", arg, stream->filename);
723         else
724             stream->feed = sfeed;
725     } else if (!av_strcasecmp(cmd, "Format")) {
726         ffserver_get_arg(arg, sizeof(arg), p);
727         if (!strcmp(arg, "status")) {
728             stream->stream_type = STREAM_TYPE_STATUS;
729             stream->fmt = NULL;
730         } else {
731             stream->stream_type = STREAM_TYPE_LIVE;
732             /* JPEG cannot be used here, so use single frame MJPEG */
733             if (!strcmp(arg, "jpeg"))
734                 strcpy(arg, "mjpeg");
735             stream->fmt = ffserver_guess_format(arg, NULL, NULL);
736             if (!stream->fmt)
737                 ERROR("Unknown Format: %s\n", arg);
738         }
739         if (stream->fmt) {
740             config->audio_id = stream->fmt->audio_codec;
741             config->video_id = stream->fmt->video_codec;
742         }
743     } else if (!av_strcasecmp(cmd, "InputFormat")) {
744         ffserver_get_arg(arg, sizeof(arg), p);
745         stream->ifmt = av_find_input_format(arg);
746         if (!stream->ifmt)
747             ERROR("Unknown input format: %s\n", arg);
748     } else if (!av_strcasecmp(cmd, "FaviconURL")) {
749         if (stream->stream_type == STREAM_TYPE_STATUS)
750             ffserver_get_arg(stream->feed_filename, sizeof(stream->feed_filename), p);
751         else
752             ERROR("FaviconURL only permitted for status streams\n");
753     } else if (!av_strcasecmp(cmd, "Author")    ||
754                !av_strcasecmp(cmd, "Comment")   ||
755                !av_strcasecmp(cmd, "Copyright") ||
756                !av_strcasecmp(cmd, "Title")) {
757         char key[32];
758         int i;
759         ffserver_get_arg(arg, sizeof(arg), p);
760         for (i = 0; i < strlen(cmd); i++)
761             key[i] = av_tolower(cmd[i]);
762         key[i] = 0;
763         WARNING("'%s' option in configuration file is deprecated, "
764                 "use 'Metadata %s VALUE' instead\n", cmd, key);
765         if (av_dict_set(&stream->metadata, key, arg, 0) < 0)
766             goto nomem;
767     } else if (!av_strcasecmp(cmd, "Metadata")) {
768         ffserver_get_arg(arg, sizeof(arg), p);
769         ffserver_get_arg(arg2, sizeof(arg2), p);
770         if (av_dict_set(&stream->metadata, arg, arg2, 0) < 0)
771             goto nomem;
772     } else if (!av_strcasecmp(cmd, "Preroll")) {
773         ffserver_get_arg(arg, sizeof(arg), p);
774         stream->prebuffer = atof(arg) * 1000;
775     } else if (!av_strcasecmp(cmd, "StartSendOnKey")) {
776         stream->send_on_key = 1;
777     } else if (!av_strcasecmp(cmd, "AudioCodec")) {
778         ffserver_get_arg(arg, sizeof(arg), p);
779         config->audio_id = opt_codec(arg, AVMEDIA_TYPE_AUDIO);
780         if (config->audio_id == AV_CODEC_ID_NONE)
781             ERROR("Unknown AudioCodec: %s\n", arg);
782     } else if (!av_strcasecmp(cmd, "VideoCodec")) {
783         ffserver_get_arg(arg, sizeof(arg), p);
784         config->video_id = opt_codec(arg, AVMEDIA_TYPE_VIDEO);
785         if (config->video_id == AV_CODEC_ID_NONE)
786             ERROR("Unknown VideoCodec: %s\n", arg);
787     } else if (!av_strcasecmp(cmd, "MaxTime")) {
788         ffserver_get_arg(arg, sizeof(arg), p);
789         stream->max_time = atof(arg) * 1000;
790     } else if (!av_strcasecmp(cmd, "AudioBitRate")) {
791         float f;
792         ffserver_get_arg(arg, sizeof(arg), p);
793         ffserver_set_float_param(&f, arg, 1000, 0, FLT_MAX, config, line_num, "Invalid %s: %s\n", cmd, arg);
794         if (av_dict_set_int(&config->audio_conf, cmd, lrintf(f), 0) < 0)
795             goto nomem;
796     } else if (!av_strcasecmp(cmd, "AudioChannels")) {
797         ffserver_get_arg(arg, sizeof(arg), p);
798         ffserver_set_int_param(NULL, arg, 0, 1, 8, config, line_num, "Invalid %s: %s, valid range is 1-8.", cmd, arg);
799         if (av_dict_set(&config->audio_conf, cmd, arg, 0) < 0)
800             goto nomem;
801     } else if (!av_strcasecmp(cmd, "AudioSampleRate")) {
802         ffserver_get_arg(arg, sizeof(arg), p);
803         ffserver_set_int_param(NULL, arg, 0, 0, INT_MAX, config, line_num, "Invalid %s: %s", cmd, arg);
804         if (av_dict_set(&config->audio_conf, cmd, arg, 0) < 0)
805             goto nomem;
806     } else if (!av_strcasecmp(cmd, "VideoBitRateRange")) {
807         int minrate, maxrate;
808         ffserver_get_arg(arg, sizeof(arg), p);
809         if (sscanf(arg, "%d-%d", &minrate, &maxrate) == 2) {
810             if (av_dict_set_int(&config->video_conf, "VideoBitRateRangeMin", minrate, 0) < 0 ||
811                 av_dict_set_int(&config->video_conf, "VideoBitRateRangeMax", maxrate, 0) < 0)
812                 goto nomem;
813         } else
814             ERROR("Incorrect format for VideoBitRateRange -- should be <min>-<max>: %s\n", arg);
815     } else if (!av_strcasecmp(cmd, "Debug")) {
816         ffserver_get_arg(arg, sizeof(arg), p);
817         ffserver_set_int_param(NULL, arg, 0, INT_MIN, INT_MAX, config, line_num, "Invalid %s: %s", cmd, arg);
818         if (av_dict_set(&config->video_conf, cmd, arg, 0) < 0)
819             goto nomem;
820     } else if (!av_strcasecmp(cmd, "Strict")) {
821         ffserver_get_arg(arg, sizeof(arg), p);
822         ffserver_set_int_param(NULL, arg, 0, INT_MIN, INT_MAX, config, line_num, "Invalid %s: %s", cmd, arg);
823         if (av_dict_set(&config->video_conf, cmd, arg, 0) < 0)
824             goto nomem;
825     } else if (!av_strcasecmp(cmd, "VideoBufferSize")) {
826         ffserver_get_arg(arg, sizeof(arg), p);
827         ffserver_set_int_param(NULL, arg, 8*1024, 0, INT_MAX, config, line_num, "Invalid %s: %s", cmd, arg);
828         if (av_dict_set(&config->video_conf, cmd, arg, 0) < 0)
829             goto nomem;
830     } else if (!av_strcasecmp(cmd, "VideoBitRateTolerance")) {
831         ffserver_get_arg(arg, sizeof(arg), p);
832         ffserver_set_int_param(NULL, arg, 1000, INT_MIN, INT_MAX, config, line_num, "Invalid %s: %s", cmd, arg);
833         if (av_dict_set(&config->video_conf, cmd, arg, 0) < 0)
834             goto nomem;
835     } else if (!av_strcasecmp(cmd, "VideoBitRate")) {
836         ffserver_get_arg(arg, sizeof(arg), p);
837         ffserver_set_int_param(NULL, arg, 1000, 0, INT_MAX, config, line_num, "Invalid %s: %s", cmd, arg);
838         if (av_dict_set(&config->video_conf, cmd, arg, 0) < 0)
839            goto nomem;
840     } else if (!av_strcasecmp(cmd, "VideoSize")) {
841         int ret, w, h;
842         ffserver_get_arg(arg, sizeof(arg), p);
843         ret = av_parse_video_size(&w, &h, arg);
844         if (ret < 0)
845             ERROR("Invalid video size '%s'\n", arg);
846         else if ((w % 16) || (h % 16))
847             ERROR("Image size must be a multiple of 16\n");
848         if (av_dict_set_int(&config->video_conf, "VideoSizeWidth", w, 0) < 0 ||
849             av_dict_set_int(&config->video_conf, "VideoSizeHeight", h, 0) < 0)
850             goto nomem;
851     } else if (!av_strcasecmp(cmd, "VideoFrameRate")) {
852         AVRational frame_rate;
853         ffserver_get_arg(arg, sizeof(arg), p);
854         if (av_parse_video_rate(&frame_rate, arg) < 0) {
855             ERROR("Incorrect frame rate: %s\n", arg);
856         } else {
857             if (av_dict_set_int(&config->video_conf, "VideoFrameRateNum", frame_rate.num, 0) < 0 ||
858                 av_dict_set_int(&config->video_conf, "VideoFrameRateDen", frame_rate.den, 0) < 0)
859                 goto nomem;
860         }
861     } else if (!av_strcasecmp(cmd, "PixelFormat")) {
862         enum AVPixelFormat pix_fmt;
863         ffserver_get_arg(arg, sizeof(arg), p);
864         pix_fmt = av_get_pix_fmt(arg);
865         if (pix_fmt == AV_PIX_FMT_NONE)
866             ERROR("Unknown pixel format: %s\n", arg);
867         if (av_dict_set_int(&config->video_conf, cmd, pix_fmt, 0) < 0)
868             goto nomem;
869     } else if (!av_strcasecmp(cmd, "VideoGopSize")) {
870         ffserver_get_arg(arg, sizeof(arg), p);
871         ffserver_set_int_param(NULL, arg, 0, INT_MIN, INT_MAX, config, line_num, "Invalid %s: %s", cmd, arg);
872         if (av_dict_set(&config->video_conf, cmd, arg, 0) < 0)
873             goto nomem;
874     } else if (!av_strcasecmp(cmd, "VideoIntraOnly")) {
875         if (av_dict_set(&config->video_conf, cmd, "1", 0) < 0)
876             goto nomem;
877     } else if (!av_strcasecmp(cmd, "VideoHighQuality")) {
878         if (av_dict_set(&config->video_conf, cmd, "", 0) < 0)
879             goto nomem;
880     } else if (!av_strcasecmp(cmd, "Video4MotionVector")) {
881         if (av_dict_set(&config->video_conf, cmd, "", 0) < 0)
882             goto nomem;
883     } else if (!av_strcasecmp(cmd, "AVOptionVideo") ||
884                !av_strcasecmp(cmd, "AVOptionAudio")) {
885         int ret;
886         ffserver_get_arg(arg, sizeof(arg), p);
887         ffserver_get_arg(arg2, sizeof(arg2), p);
888         if (!av_strcasecmp(cmd, "AVOptionVideo"))
889             ret = ffserver_save_avoption(arg, arg2, &config->video_opts, AV_OPT_FLAG_VIDEO_PARAM ,config, line_num);
890         else
891             ret = ffserver_save_avoption(arg, arg2, &config->audio_opts, AV_OPT_FLAG_AUDIO_PARAM ,config, line_num);
892         if (ret < 0)
893             goto nomem;
894     } else if (!av_strcasecmp(cmd, "AVPresetVideo") ||
895                !av_strcasecmp(cmd, "AVPresetAudio")) {
896         char **preset = NULL;
897         ffserver_get_arg(arg, sizeof(arg), p);
898         if (!av_strcasecmp(cmd, "AVPresetVideo")) {
899             preset = &config->video_preset;
900             ffserver_opt_preset(arg, NULL, 0, NULL, &config->video_id);
901         } else {
902             preset = &config->audio_preset;
903             ffserver_opt_preset(arg, NULL, 0, &config->audio_id, NULL);
904         }
905         *preset = av_strdup(arg);
906         if (!preset)
907             return AVERROR(ENOMEM);
908     } else if (!av_strcasecmp(cmd, "VideoTag")) {
909         ffserver_get_arg(arg, sizeof(arg), p);
910         if (strlen(arg) == 4) {
911             if (av_dict_set(&config->video_conf, "VideoTag", "arg", 0) < 0)
912                 goto nomem;
913         }
914     } else if (!av_strcasecmp(cmd, "BitExact")) {
915         if (av_dict_set(&config->video_conf, cmd, "", 0) < 0)
916             goto nomem;
917     } else if (!av_strcasecmp(cmd, "DctFastint")) {
918         if (av_dict_set(&config->video_conf, cmd, "", 0) < 0)
919             goto nomem;
920     } else if (!av_strcasecmp(cmd, "IdctSimple")) {
921         if (av_dict_set(&config->video_conf, cmd, "", 0) < 0)
922             goto nomem;
923     } else if (!av_strcasecmp(cmd, "Qscale")) {
924         ffserver_get_arg(arg, sizeof(arg), p);
925         if (av_dict_set(&config->video_conf, cmd, arg, 0) < 0)
926             goto nomem;
927     } else if (!av_strcasecmp(cmd, "VideoQDiff")) {
928         ffserver_get_arg(arg, sizeof(arg), p);
929         ffserver_set_int_param(NULL, arg, 0, 1, 31, config, line_num, "%s out of range\n", cmd);
930         if (av_dict_set(&config->video_conf, cmd, arg, 0) < 0)
931             goto nomem;
932     } else if (!av_strcasecmp(cmd, "VideoQMax")) {
933         ffserver_get_arg(arg, sizeof(arg), p);
934         ffserver_set_int_param(NULL, arg, 0, 1, 31, config, line_num, "%s out of range\n", cmd);
935         if (av_dict_set(&config->video_conf, cmd, arg, 0) < 0)
936             goto nomem;
937     } else if (!av_strcasecmp(cmd, "VideoQMin")) {
938         ffserver_get_arg(arg, sizeof(arg), p);
939         ffserver_set_int_param(NULL, arg, 0, 1, 31, config, line_num, "%s out of range\n", cmd);
940         if (av_dict_set(&config->video_conf, cmd, arg, 0) < 0)
941             goto nomem;
942     } else if (!av_strcasecmp(cmd, "LumiMask")) {
943         ffserver_get_arg(arg, sizeof(arg), p);
944         ffserver_set_float_param(NULL, arg, 0, -FLT_MAX, FLT_MAX, config, line_num, "Invalid %s: %s", cmd, arg);
945         if (av_dict_set(&config->video_conf, cmd, arg, 0) < 0)
946             goto nomem;
947     } else if (!av_strcasecmp(cmd, "DarkMask")) {
948         ffserver_get_arg(arg, sizeof(arg), p);
949         ffserver_set_float_param(NULL, arg, 0, -FLT_MAX, FLT_MAX, config, line_num, "Invalid %s: %s", cmd, arg);
950         if (av_dict_set(&config->video_conf, cmd, arg, 0) < 0)
951             goto nomem;
952     } else if (!av_strcasecmp(cmd, "NoVideo")) {
953         config->video_id = AV_CODEC_ID_NONE;
954     } else if (!av_strcasecmp(cmd, "NoAudio")) {
955         config->audio_id = AV_CODEC_ID_NONE;
956     } else if (!av_strcasecmp(cmd, "ACL")) {
957         ffserver_parse_acl_row(stream, NULL, NULL, *p, config->filename, line_num);
958     } else if (!av_strcasecmp(cmd, "DynamicACL")) {
959         ffserver_get_arg(stream->dynamic_acl, sizeof(stream->dynamic_acl), p);
960     } else if (!av_strcasecmp(cmd, "RTSPOption")) {
961         ffserver_get_arg(arg, sizeof(arg), p);
962         av_freep(&stream->rtsp_option);
963         stream->rtsp_option = av_strdup(arg);
964     } else if (!av_strcasecmp(cmd, "MulticastAddress")) {
965         ffserver_get_arg(arg, sizeof(arg), p);
966         if (resolve_host(&stream->multicast_ip, arg) != 0)
967             ERROR("Invalid host/IP address: %s\n", arg);
968         stream->is_multicast = 1;
969         stream->loop = 1; /* default is looping */
970     } else if (!av_strcasecmp(cmd, "MulticastPort")) {
971         ffserver_get_arg(arg, sizeof(arg), p);
972         ffserver_set_int_param(&val, arg, 0, 1, 65535, config, line_num, "Invalid MulticastPort: %s\n", arg);
973         stream->multicast_port = val;
974     } else if (!av_strcasecmp(cmd, "MulticastTTL")) {
975         ffserver_get_arg(arg, sizeof(arg), p);
976         ffserver_set_int_param(&val, arg, 0, INT_MIN, INT_MAX, config, line_num, "Invalid MulticastTTL: %s\n", arg);
977         stream->multicast_ttl = val;
978     } else if (!av_strcasecmp(cmd, "NoLoop")) {
979         stream->loop = 0;
980     } else if (!av_strcasecmp(cmd, "</Stream>")) {
981         if (stream->feed && stream->fmt && strcmp(stream->fmt->name, "ffm") != 0) {
982             if (config->audio_id != AV_CODEC_ID_NONE) {
983                 AVCodecContext *audio_enc = avcodec_alloc_context3(avcodec_find_encoder(config->audio_id));
984                 if (config->audio_preset &&
985                     ffserver_opt_preset(arg, audio_enc, AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_ENCODING_PARAM,
986                                         NULL, NULL) < 0)
987                     ERROR("Could not apply preset '%s'\n", arg);
988                 ffserver_apply_stream_config(audio_enc, config->audio_conf, &config->audio_opts);
989                 add_codec(stream, audio_enc);
990             }
991             if (config->video_id != AV_CODEC_ID_NONE) {
992                 AVCodecContext *video_enc = avcodec_alloc_context3(avcodec_find_encoder(config->video_id));
993                 if (config->video_preset &&
994                     ffserver_opt_preset(arg, video_enc, AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_ENCODING_PARAM,
995                                         NULL, NULL) < 0)
996                     ERROR("Could not apply preset '%s'\n", arg);
997                 ffserver_apply_stream_config(video_enc, config->video_conf, &config->video_opts);
998                 add_codec(stream, video_enc);
999             }
1000         }
1001         av_dict_free(&config->video_opts);
1002         av_dict_free(&config->video_conf);
1003         av_dict_free(&config->audio_opts);
1004         av_dict_free(&config->audio_conf);
1005         av_freep(&config->video_preset);
1006         av_freep(&config->audio_preset);
1007         avcodec_free_context(&config->dummy_ctx);
1008         *pstream = NULL;
1009     } else if (!av_strcasecmp(cmd, "File") || !av_strcasecmp(cmd, "ReadOnlyFile")) {
1010         ffserver_get_arg(stream->feed_filename, sizeof(stream->feed_filename), p);
1011     } else {
1012         ERROR("Invalid entry '%s' inside <Stream></Stream>\n", cmd);
1013     }
1014     return 0;
1015   nomem:
1016     av_log(NULL, AV_LOG_ERROR, "Out of memory. Aborting.\n");
1017     av_dict_free(&config->video_opts);
1018     av_dict_free(&config->video_conf);
1019     av_dict_free(&config->audio_opts);
1020     av_dict_free(&config->audio_conf);
1021     av_freep(&config->video_preset);
1022     av_freep(&config->audio_preset);
1023     avcodec_free_context(&config->dummy_ctx);
1024     return AVERROR(ENOMEM);
1025 }
1026
1027 static int ffserver_parse_config_redirect(FFServerConfig *config, const char *cmd, const char **p,
1028                                           int line_num, FFServerStream **predirect)
1029 {
1030     FFServerStream *redirect;
1031     av_assert0(predirect);
1032     redirect = *predirect;
1033
1034     if (!av_strcasecmp(cmd, "<Redirect")) {
1035         char *q;
1036         redirect = av_mallocz(sizeof(FFServerStream));
1037         if (!redirect)
1038             return AVERROR(ENOMEM);
1039
1040         ffserver_get_arg(redirect->filename, sizeof(redirect->filename), p);
1041         q = strrchr(redirect->filename, '>');
1042         if (*q)
1043             *q = '\0';
1044         redirect->stream_type = STREAM_TYPE_REDIRECT;
1045         *predirect = redirect;
1046         return 0;
1047     }
1048     av_assert0(redirect);
1049     if (!av_strcasecmp(cmd, "URL")) {
1050         ffserver_get_arg(redirect->feed_filename, sizeof(redirect->feed_filename), p);
1051     } else if (!av_strcasecmp(cmd, "</Redirect>")) {
1052         if (!redirect->feed_filename[0])
1053             ERROR("No URL found for <Redirect>\n");
1054         *predirect = NULL;
1055     } else {
1056         ERROR("Invalid entry '%s' inside <Redirect></Redirect>\n", cmd);
1057     }
1058     return 0;
1059 }
1060
1061 int ffserver_parse_ffconfig(const char *filename, FFServerConfig *config)
1062 {
1063     FILE *f;
1064     char line[1024];
1065     char cmd[64];
1066     const char *p;
1067     int line_num = 0;
1068     FFServerStream **last_stream, *stream = NULL, *redirect = NULL;
1069     FFServerStream **last_feed, *feed = NULL;
1070     int ret = 0;
1071
1072     av_assert0(config);
1073
1074     f = fopen(filename, "r");
1075     if (!f) {
1076         ret = AVERROR(errno);
1077         av_log(NULL, AV_LOG_ERROR, "Could not open the configuration file '%s'\n", filename);
1078         return ret;
1079     }
1080
1081     config->first_stream = NULL;
1082     last_stream = &config->first_stream;
1083     config->first_feed = NULL;
1084     last_feed = &config->first_feed;
1085     config->errors = config->warnings = 0;
1086
1087     for(;;) {
1088         if (fgets(line, sizeof(line), f) == NULL)
1089             break;
1090         line_num++;
1091         p = line;
1092         while (av_isspace(*p))
1093             p++;
1094         if (*p == '\0' || *p == '#')
1095             continue;
1096
1097         ffserver_get_arg(cmd, sizeof(cmd), &p);
1098
1099         if (feed || !av_strcasecmp(cmd, "<Feed")) {
1100             int opening = !av_strcasecmp(cmd, "<Feed");
1101             if (opening && (stream || feed || redirect)) {
1102                 ERROR("Already in a tag\n");
1103             } else {
1104                 if ((ret = ffserver_parse_config_feed(config, cmd, &p, line_num, &feed)) < 0)
1105                     break;
1106                 if (opening) {
1107                     /* add in stream list */
1108                     *last_stream = feed;
1109                     last_stream = &feed->next;
1110                     /* add in feed list */
1111                     *last_feed = feed;
1112                     last_feed = &feed->next_feed;
1113                 }
1114             }
1115         } else if (stream || !av_strcasecmp(cmd, "<Stream")) {
1116             int opening = !av_strcasecmp(cmd, "<Stream");
1117             if (opening && (stream || feed || redirect)) {
1118                 ERROR("Already in a tag\n");
1119             } else {
1120                 if ((ret = ffserver_parse_config_stream(config, cmd, &p, line_num, &stream)) < 0)
1121                     break;
1122                 if (opening) {
1123                     /* add in stream list */
1124                     *last_stream = stream;
1125                     last_stream = &stream->next;
1126                 }
1127             }
1128         } else if (redirect || !av_strcasecmp(cmd, "<Redirect")) {
1129             int opening = !av_strcasecmp(cmd, "<Redirect");
1130             if (opening && (stream || feed || redirect))
1131                 ERROR("Already in a tag\n");
1132             else {
1133                 if ((ret = ffserver_parse_config_redirect(config, cmd, &p, line_num, &redirect)) < 0)
1134                     break;
1135                 if (opening) {
1136                     /* add in stream list */
1137                     *last_stream = redirect;
1138                     last_stream = &redirect->next;
1139                 }
1140             }
1141         } else {
1142             ffserver_parse_config_global(config, cmd, &p, line_num);
1143         }
1144     }
1145
1146     fclose(f);
1147     if (ret < 0)
1148         return ret;
1149     if (config->errors)
1150         return AVERROR(EINVAL);
1151     else
1152         return 0;
1153 }
1154
1155 #undef ERROR
1156 #undef WARNING