]> rtime.felk.cvut.cz Git - frescor/streamer.git/blob - output.c
Allow to select video4linux version 1 formant from commandline.
[frescor/streamer.git] / output.c
1 /*
2  *  Copyright (c) 2008 Luca Abeni
3  *
4  *  This is free software; see GPL.txt
5  */
6 #include <libavformat/avformat.h>
7
8
9 AVFormatContext *open_output_stream(const char *dst, int port, enum CodecType codec_type)
10 {
11     AVFormatContext *s;
12     static AVOutputFormat *rtp_format;
13     AVStream *st;
14
15     s = av_alloc_format_context();
16     rtp_format = guess_format("rtp", NULL, NULL);
17     if (!rtp_format) {
18         fprintf(stderr, "Unable for find the RTP format for output\n");
19
20         return NULL;
21     }
22
23     s->oformat = rtp_format;
24     st = av_new_stream(s, 0);
25     if (!st) {
26         fprintf(stderr, "Cannot allocate stream\n");
27
28         return NULL;
29     }
30
31     snprintf(s->filename, sizeof(s->filename), "rtp://%s:%d", dst, port);
32         
33     /* open the UDP sockets for RTP and RTCP */
34     if (url_fopen(&s->pb, s->filename, URL_WRONLY) < 0) {
35         fprintf(stderr, "Cannot open '%s'\n", s->filename);
36         /* FIXME: Free the stream! */
37
38         return NULL;
39     }
40     avcodec_get_context_defaults2(s->streams[0]->codec, codec_type);
41     s->streams[0]->codec->codec_id = av_guess_codec(s->oformat, NULL, s->filename, NULL, codec_type);
42     s->streams[0]->codec->codec_id = CODEC_ID_MPEG4;
43     s->streams[0]->codec->pix_fmt  = PIX_FMT_YUV420P;
44     s->streams[0]->codec->mb_decision = 2;
45     s->streams[0]->codec->me_cmp = 2;
46     //s->streams[0]->codec->me_sub_cmp = 6;
47     s->streams[0]->codec->trellis = 1;
48
49     return s;
50 }
51
52 int pkt_send(AVFormatContext *ctx, AVPacket *pkt)
53 {
54   static int inited;            /* FIXME: HACK! */
55
56
57   if (!inited) {
58     int res;
59
60 fprintf(stderr,"initing!\n");
61     res = av_write_header(ctx);
62     if (res < 0) {
63       fprintf(stderr, "Cannot open the out RTP stream!\n");
64
65       return res;
66     }
67     inited = 1;
68   }
69
70   return av_write_frame(ctx, pkt);
71 }
72