]> rtime.felk.cvut.cz Git - frescor/streamer.git/blob - codec.c
Put the SDF file name in a variable...
[frescor/streamer.git] / codec.c
1 #include "libavformat/avformat.h"
2 #include "libavcodec/avcodec.h"
3 #include "libswscale/swscale.h"
4
5 struct resample_data {
6   struct SwsContext *resample_ctx;
7   int in_height;
8   AVFrame resampled_frame;
9 };
10
11 int codec_open(AVFormatContext *ctx)
12 {
13   AVCodec *codec;
14   int i, res;
15
16   for (i = 0; i < ctx->nb_streams; i++) {
17     codec = avcodec_find_decoder(ctx->streams[i]->codec->codec_id);
18     if (!codec) {
19       fprintf(stderr, "Cannot find codec %d for input stream %d\n",
20                       ctx->streams[i]->codec->codec_id, i);
21       return -1;
22     }
23     res = avcodec_open(ctx->streams[i]->codec, codec);
24     if (res < 0) {
25       fprintf(stderr, "Cannot open codec for input stream %d\n", i);
26
27       return -1;
28     }
29   }
30
31   return 0;
32 }
33
34 int out_codec_open(AVFormatContext *ctx)
35 {
36   AVCodec *codec;
37   int i, res;
38
39   for (i = 0; i < ctx->nb_streams; i++) {
40     codec = avcodec_find_encoder(ctx->streams[i]->codec->codec_id);
41     if (!codec) {
42       fprintf(stderr, "Cannot find codec %d for output stream %d\n",
43                       ctx->streams[i]->codec->codec_id, i);
44       return -1;
45     }
46     res = avcodec_open(ctx->streams[i]->codec, codec);
47     if (res < 0) {
48       fprintf(stderr, "Cannot open codec for input stream %d\n", i);
49
50       return -1;
51     }
52   }
53
54   return 0;
55 }
56
57
58 AVFrame *pkt_decode(AVFormatContext *ctx, AVPacket *pkt)
59 {
60   static AVFrame frame;
61   int res, got;
62
63   avcodec_get_frame_defaults(&frame);
64   res = avcodec_decode_video(ctx->streams[pkt->stream_index]->codec,
65         &frame, &got, pkt->data, pkt->size);
66   if (res < 0) {
67     got = res;
68   }
69
70   if (got > 0) {
71     AVFrame *res;
72     struct resample_data *r;
73
74     r = ctx->streams[pkt->stream_index]->codec->opaque;
75     if (r) {
76       sws_scale(r->resample_ctx, frame.data, frame.linesize, 0,
77                 ctx->streams[pkt->stream_index]->codec->height,
78                 r->resampled_frame.data, r->resampled_frame.linesize);
79       res = &r->resampled_frame;
80     } else {
81       res = &frame;
82     }
83
84     return res;
85   }
86
87   return NULL;
88 }
89
90 int codec_connect(AVCodecContext *ic, AVCodecContext *oc)
91 {
92   struct resample_data *r;
93
94   if ((ic->width == oc->width) && (ic->height == oc->height) && (ic->pix_fmt == oc->pix_fmt)) {
95     return 0;
96   }
97
98   r = av_malloc(sizeof(struct resample_data));
99   r->resample_ctx = sws_getContext(ic->width, ic->height, ic->pix_fmt,
100                                    oc->width, oc->height, oc->pix_fmt,
101                                    SWS_BICUBIC, NULL, NULL, NULL);
102   if (r->resample_ctx == NULL) {
103     av_free(r);
104
105     return -1;
106   }
107   avcodec_get_frame_defaults(&r->resampled_frame);
108   avpicture_alloc((AVPicture*)&r->resampled_frame,
109                   oc->pix_fmt, oc->width, oc->height);
110   ic->opaque = r;
111
112   return 1;
113 }
114
115 AVPacket *pkt_encode(AVFormatContext *ctx, AVFrame *frame)
116 {
117   AVCodecContext *c = ctx->streams[0]->codec;
118   static AVPacket pkt;
119   int res;
120
121   pkt.size = 256 * 1024;
122   if (pkt.data == NULL) {
123     pkt.data = av_malloc(pkt.size);
124   }
125   res = avcodec_encode_video(c, pkt.data, pkt.size, frame);
126   if (c->coded_frame->pts != AV_NOPTS_VALUE) {
127     pkt.pts = av_rescale_q(c->coded_frame->pts, c->time_base, ctx->streams[0]->time_base);
128   }
129   if (res < 0) {
130     return NULL;
131   }
132
133   pkt.size = res;
134   return &pkt;
135 }
136