2 * This file is part of MPlayer.
4 * MPlayer is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * MPlayer is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include "img_format.h"
30 #include "libavcodec/avcodec.h"
32 extern int avcodec_initialized
;
35 unsigned char* outbuf
;
37 AVCodecContext
* context
;
43 #define lavc_venc_context (*vf->priv->context)
45 //===========================================================================//
47 static int config(struct vf_instance
* vf
,
48 int width
, int height
, int d_width
, int d_height
,
49 unsigned int flags
, unsigned int outfmt
){
50 if(vf_next_query_format(vf
,IMGFMT_MPEGPES
)<=0) return 0;
52 lavc_venc_context
.width
= width
;
53 lavc_venc_context
.height
= height
;
55 if(!lavc_venc_context
.time_base
.num
|| !lavc_venc_context
.time_base
.den
){
60 lavc_venc_context
.time_base
= (AVRational
){1001,30000};
65 lavc_venc_context
.time_base
= (AVRational
){1,25};
67 // lavc_venc_context.frame_rate=vo_fps*FRAME_RATE_BASE; // same as src
71 if(vf
->priv
->outbuf
) free(vf
->priv
->outbuf
);
73 vf
->priv
->outbuf_size
=10000+width
*height
; // must be enough!
74 vf
->priv
->outbuf
= malloc(vf
->priv
->outbuf_size
);
76 if (avcodec_open(&lavc_venc_context
, vf
->priv
->codec
) != 0) {
77 mp_tmsg(MSGT_MENCODER
,MSGL_ERR
,"Could not open codec.\n");
81 if (lavc_venc_context
.codec
->encode
== NULL
) {
82 mp_msg(MSGT_MENCODER
,MSGL_ERR
,"avcodec init failed (ctx->codec->encode == NULL)!\n");
86 return vf_next_config(vf
,width
,height
,d_width
,d_height
,flags
,IMGFMT_MPEGPES
);
89 static int put_image(struct vf_instance
* vf
, mp_image_t
*mpi
, double pts
){
92 AVFrame
*pic
= vf
->priv
->pic
;
94 pic
->data
[0]=mpi
->planes
[0];
95 pic
->data
[1]=mpi
->planes
[1];
96 pic
->data
[2]=mpi
->planes
[2];
97 pic
->linesize
[0]=mpi
->stride
[0];
98 pic
->linesize
[1]=mpi
->stride
[1];
99 pic
->linesize
[2]=mpi
->stride
[2];
101 out_size
= avcodec_encode_video(&lavc_venc_context
,
102 vf
->priv
->outbuf
, vf
->priv
->outbuf_size
, pic
);
104 if(out_size
<=0) return 1;
106 dmpi
=vf_get_image(vf
->next
,IMGFMT_MPEGPES
,
107 MP_IMGTYPE_EXPORT
, 0,
110 vf
->priv
->pes
.data
=vf
->priv
->outbuf
;
111 vf
->priv
->pes
.size
=out_size
;
112 vf
->priv
->pes
.id
=0x1E0;
113 vf
->priv
->pes
.timestamp
=-1; // dunno
115 dmpi
->planes
[0]=(unsigned char*)&vf
->priv
->pes
;
117 return vf_next_put_image(vf
,dmpi
, MP_NOPTS_VALUE
);
120 //===========================================================================//
122 static int query_format(struct vf_instance
* vf
, unsigned int fmt
){
127 return vf_next_query_format(vf
, IMGFMT_MPEGPES
) & (~(VFCAP_CSP_SUPPORTED_BY_HW
| VFCAP_ACCEPT_STRIDE
));
132 static int vf_open(vf_instance_t
*vf
, char *args
){
137 vf
->put_image
=put_image
;
138 vf
->query_format
=query_format
;
139 vf
->priv
=malloc(sizeof(struct vf_priv_s
));
140 memset(vf
->priv
,0,sizeof(struct vf_priv_s
));
142 if (!avcodec_initialized
){
144 avcodec_register_all();
145 avcodec_initialized
=1;
148 vf
->priv
->codec
= (AVCodec
*)avcodec_find_encoder_by_name("mpeg1video");
149 if (!vf
->priv
->codec
) {
150 mp_tmsg(MSGT_MENCODER
,MSGL_ERR
,"Cannot find codec '%s' in libavcodec...\n", "mpeg1video");
154 vf
->priv
->context
=avcodec_alloc_context();
155 vf
->priv
->pic
= avcodec_alloc_frame();
157 // TODO: parse args ->
158 if(args
) sscanf(args
, "%d:%f", &p_quality
, &p_fps
);
162 lavc_venc_context
.flags
= CODEC_FLAG_QSCALE
;
163 lavc_venc_context
.global_quality
=
164 vf
->priv
->pic
->quality
= (int)(FF_QP2LAMBDA
* ((p_quality
<1) ? 1 : p_quality
) + 0.5);
166 // fixed bitrate (in kbits)
167 lavc_venc_context
.bit_rate
= 1000*p_quality
;
169 lavc_venc_context
.time_base
.num
= 1000*1001;
170 lavc_venc_context
.time_base
.den
= (p_fps
<1.0) ? 1000*1001*25 : (p_fps
* lavc_venc_context
.time_base
.num
);
171 lavc_venc_context
.gop_size
= 0; // I-only
172 lavc_venc_context
.pix_fmt
= PIX_FMT_YUV420P
;
177 const vf_info_t vf_info_lavc
= {
178 "realtime mpeg1 encoding with libavcodec",
186 //===========================================================================//