Mercurial > mplayer.hg
annotate libmpcodecs/vf_lavc.c @ 16265:f0bd9bc9647b
gtk2 is supported, next step is pure gtk without X.
author | reimar |
---|---|
date | Thu, 18 Aug 2005 13:46:01 +0000 |
parents | 77a5b8ef3314 |
children | 6ff3379a0862 |
rev | line source |
---|---|
5873 | 1 #include <stdio.h> |
2 #include <stdlib.h> | |
3 #include <string.h> | |
4 #include <inttypes.h> | |
5 | |
6 #include "../config.h" | |
7 #include "../mp_msg.h" | |
8 #include "../help_mp.h" | |
9 | |
10 #ifdef USE_LIBAVCODEC | |
11 | |
12 #include "img_format.h" | |
13 #include "mp_image.h" | |
14 #include "vf.h" | |
15 | |
16 //#include "../libvo/fastmemcpy.h" | |
17 | |
18 #ifdef USE_LIBAVCODEC_SO | |
7004 | 19 #include <ffmpeg/avcodec.h> |
5873 | 20 #else |
21 #include "libavcodec/avcodec.h" | |
22 #endif | |
23 | |
8339 | 24 #if LIBAVCODEC_BUILD < 4641 |
11000 | 25 #error we do not support libavcodec prior to build 4641, get the latest libavcodec CVS |
8339 | 26 #endif |
27 | |
8413 | 28 #if LIBAVCODEC_BUILD < 4645 |
29 #warning your version of libavcodec is old, u might want to get a newer one | |
30 #endif | |
31 | |
32 #if LIBAVCODEC_BUILD < 4645 | |
33 #define AVFrame AVVideoFrame | |
34 #define coded_frame coded_picture | |
35 #endif | |
36 | |
11419 | 37 #if LIBAVCODEC_BUILD < 4684 |
38 #define FF_QP2LAMBDA 1 | |
39 #endif | |
40 | |
5873 | 41 extern int avcodec_inited; |
42 | |
43 struct vf_priv_s { | |
44 unsigned char* outbuf; | |
45 int outbuf_size; | |
7852 | 46 AVCodecContext* context; |
8413 | 47 AVFrame* pic; |
7852 | 48 AVCodec* codec; |
5873 | 49 vo_mpegpes_t pes; |
50 }; | |
51 | |
7852 | 52 #define lavc_venc_context (*vf->priv->context) |
5873 | 53 |
54 //===========================================================================// | |
55 | |
56 static int config(struct vf_instance_s* vf, | |
57 int width, int height, int d_width, int d_height, | |
58 unsigned int flags, unsigned int outfmt){ | |
59 if(vf_next_query_format(vf,IMGFMT_MPEGPES)<=0) return 0; | |
60 | |
61 lavc_venc_context.width = width; | |
62 lavc_venc_context.height = height; | |
6019 | 63 |
15307 | 64 #if LIBAVCODEC_BUILD >= 4754 |
15843 | 65 if(!lavc_venc_context.time_base.num || !lavc_venc_context.time_base.den){ |
15307 | 66 #else |
6019 | 67 if(!lavc_venc_context.frame_rate){ |
15307 | 68 #endif |
6019 | 69 // guess FPS: |
70 switch(height){ | |
71 case 240: | |
72 case 480: | |
15307 | 73 #if LIBAVCODEC_BUILD >= 4754 |
74 lavc_venc_context.time_base= (AVRational){1001,30000}; | |
75 #else | |
9577 | 76 #if LIBAVCODEC_BUILD >= 4662 |
77 lavc_venc_context.frame_rate = 30000; | |
78 lavc_venc_context.frame_rate_base= 1001; | |
79 #else | |
6019 | 80 lavc_venc_context.frame_rate=29.97*FRAME_RATE_BASE; // NTSC |
9577 | 81 #endif |
15307 | 82 #endif |
6019 | 83 break; |
84 case 576: | |
85 case 288: | |
86 default: | |
15307 | 87 #if LIBAVCODEC_BUILD >= 4754 |
88 lavc_venc_context.time_base= (AVRational){1,25}; | |
89 #else | |
9577 | 90 #if LIBAVCODEC_BUILD >= 4662 |
91 lavc_venc_context.frame_rate = 25; | |
92 lavc_venc_context.frame_rate_base= 1; | |
93 #else | |
6019 | 94 lavc_venc_context.frame_rate=25*FRAME_RATE_BASE; // PAL |
9577 | 95 #endif |
15307 | 96 #endif |
6019 | 97 break; |
98 // lavc_venc_context.frame_rate=vo_fps*FRAME_RATE_BASE; // same as src | |
99 } | |
100 } | |
5873 | 101 |
102 if(vf->priv->outbuf) free(vf->priv->outbuf); | |
103 | |
104 vf->priv->outbuf_size=10000+width*height; // must be enough! | |
105 vf->priv->outbuf = malloc(vf->priv->outbuf_size); | |
106 | |
107 if (avcodec_open(&lavc_venc_context, vf->priv->codec) != 0) { | |
108 mp_msg(MSGT_MENCODER,MSGL_ERR,MSGTR_CantOpenCodec); | |
109 return 0; | |
110 } | |
111 | |
112 if (lavc_venc_context.codec->encode == NULL) { | |
113 mp_msg(MSGT_MENCODER,MSGL_ERR,"avcodec init failed (ctx->codec->encode == NULL)!\n"); | |
114 return 0; | |
115 } | |
116 | |
117 return vf_next_config(vf,width,height,d_width,d_height,flags,IMGFMT_MPEGPES); | |
118 } | |
119 | |
7368 | 120 static int put_image(struct vf_instance_s* vf, mp_image_t *mpi){ |
5873 | 121 mp_image_t* dmpi; |
122 int out_size; | |
8413 | 123 AVFrame *pic= vf->priv->pic; |
5873 | 124 |
8339 | 125 pic->data[0]=mpi->planes[0]; |
126 pic->data[1]=mpi->planes[1]; | |
127 pic->data[2]=mpi->planes[2]; | |
128 pic->linesize[0]=mpi->stride[0]; | |
129 pic->linesize[1]=mpi->stride[1]; | |
130 pic->linesize[2]=mpi->stride[2]; | |
5873 | 131 |
132 out_size = avcodec_encode_video(&lavc_venc_context, | |
8339 | 133 vf->priv->outbuf, vf->priv->outbuf_size, pic); |
5873 | 134 |
7368 | 135 if(out_size<=0) return 1; |
5873 | 136 |
137 dmpi=vf_get_image(vf->next,IMGFMT_MPEGPES, | |
138 MP_IMGTYPE_EXPORT, 0, | |
139 mpi->w, mpi->h); | |
140 | |
141 vf->priv->pes.data=vf->priv->outbuf; | |
142 vf->priv->pes.size=out_size; | |
143 vf->priv->pes.id=0x1E0; | |
144 vf->priv->pes.timestamp=-1; // dunno | |
145 | |
7127 | 146 dmpi->planes[0]=(unsigned char*)&vf->priv->pes; |
5873 | 147 |
7368 | 148 return vf_next_put_image(vf,dmpi); |
5873 | 149 } |
150 | |
151 //===========================================================================// | |
152 | |
153 static int query_format(struct vf_instance_s* vf, unsigned int fmt){ | |
154 switch(fmt){ | |
155 case IMGFMT_YV12: | |
156 case IMGFMT_I420: | |
157 case IMGFMT_IYUV: | |
5876 | 158 return (vf_next_query_format(vf,IMGFMT_MPEGPES) & (~(VFCAP_CSP_SUPPORTED_BY_HW|VFCAP_ACCEPT_STRIDE))); |
5873 | 159 } |
160 return 0; | |
161 } | |
162 | |
163 static int open(vf_instance_t *vf, char* args){ | |
6019 | 164 int p_quality=0; |
165 float p_fps=0; | |
166 | |
5873 | 167 vf->config=config; |
168 vf->put_image=put_image; | |
169 vf->query_format=query_format; | |
170 vf->priv=malloc(sizeof(struct vf_priv_s)); | |
171 memset(vf->priv,0,sizeof(struct vf_priv_s)); | |
172 | |
173 if (!avcodec_inited){ | |
174 avcodec_init(); | |
175 avcodec_register_all(); | |
176 avcodec_inited=1; | |
177 } | |
178 | |
179 vf->priv->codec = (AVCodec *)avcodec_find_encoder_by_name("mpeg1video"); | |
180 if (!vf->priv->codec) { | |
181 mp_msg(MSGT_MENCODER,MSGL_ERR,MSGTR_MissingLAVCcodec, "mpeg1video"); | |
182 return 0; | |
183 } | |
7852 | 184 |
185 vf->priv->context=avcodec_alloc_context(); | |
8413 | 186 #if LIBAVCODEC_BUILD >= 4645 |
187 vf->priv->pic = avcodec_alloc_frame(); | |
188 #else | |
189 vf->priv->pic = avcodec_alloc_picture(); | |
190 #endif | |
5873 | 191 |
192 // TODO: parse args -> | |
6019 | 193 if(args) sscanf(args, "%d:%f", &p_quality, &p_fps); |
194 | |
195 if(p_quality<32){ | |
196 // fixed qscale | |
197 lavc_venc_context.flags = CODEC_FLAG_QSCALE; | |
11257
837bca3ae69f
constant qscale was broken due to libavcodec changes, fix taken from ve_lavc.c
ranma
parents:
11000
diff
changeset
|
198 #if LIBAVCODEC_BUILD >= 4668 |
837bca3ae69f
constant qscale was broken due to libavcodec changes, fix taken from ve_lavc.c
ranma
parents:
11000
diff
changeset
|
199 lavc_venc_context.global_quality = |
837bca3ae69f
constant qscale was broken due to libavcodec changes, fix taken from ve_lavc.c
ranma
parents:
11000
diff
changeset
|
200 #endif |
837bca3ae69f
constant qscale was broken due to libavcodec changes, fix taken from ve_lavc.c
ranma
parents:
11000
diff
changeset
|
201 vf->priv->pic->quality = (int)(FF_QP2LAMBDA * ((p_quality<1) ? 1 : p_quality) + 0.5); |
6019 | 202 } else { |
203 // fixed bitrate (in kbits) | |
204 lavc_venc_context.bit_rate = 1000*p_quality; | |
205 } | |
15307 | 206 #if LIBAVCODEC_BUILD >= 4754 |
207 lavc_venc_context.time_base.num = 1000*1001; | |
208 lavc_venc_context.time_base.den = (p_fps<1.0) ? 0 : (p_fps * lavc_venc_context.time_base.num); | |
209 #else | |
9577 | 210 #if LIBAVCODEC_BUILD >= 4662 |
211 lavc_venc_context.frame_rate_base = 1000*1001; | |
212 lavc_venc_context.frame_rate = (p_fps<1.0) ? 0 : (p_fps * lavc_venc_context.frame_rate_base); | |
213 #else | |
214 lavc_venc_context.frame_rate = (p_fps<1.0) ? 0 : (p_fps * FRAME_RATE_BASE); | |
215 #endif | |
15307 | 216 #endif |
5873 | 217 lavc_venc_context.gop_size = 0; // I-only |
15349 | 218 lavc_venc_context.pix_fmt= PIX_FMT_YUV420P; |
5873 | 219 |
220 return 1; | |
221 } | |
222 | |
223 vf_info_t vf_info_lavc = { | |
224 "realtime mpeg1 encoding with libavcodec", | |
225 "lavc", | |
226 "A'rpi", | |
227 "", | |
9593
e9a2af584986
Add the new -vf option wich is the same as vop in reverse order.
albeu
parents:
9577
diff
changeset
|
228 open, |
e9a2af584986
Add the new -vf option wich is the same as vop in reverse order.
albeu
parents:
9577
diff
changeset
|
229 NULL |
5873 | 230 }; |
231 | |
232 //===========================================================================// | |
233 #endif |