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