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.
28 #include "img_format.h"
31 #include "libavcodec/avcodec.h"
33 extern int avcodec_initialized
;
36 unsigned char* outbuf
;
38 AVCodecContext
* context
;
44 #define lavc_venc_context (*vf->priv->context)
46 //===========================================================================//
48 static int config(struct vf_instance
* vf
,
49 int width
, int height
, int d_width
, int d_height
,
50 unsigned int flags
, unsigned int outfmt
){
51 if(vf_next_query_format(vf
,IMGFMT_MPEGPES
)<=0) return 0;
53 lavc_venc_context
.width
= width
;
54 lavc_venc_context
.height
= height
;
56 if(!lavc_venc_context
.time_base
.num
|| !lavc_venc_context
.time_base
.den
){
61 lavc_venc_context
.time_base
= (AVRational
){1001,30000};
66 lavc_venc_context
.time_base
= (AVRational
){1,25};
68 // lavc_venc_context.frame_rate=vo_fps*FRAME_RATE_BASE; // same as src
72 if(vf
->priv
->outbuf
) free(vf
->priv
->outbuf
);
74 vf
->priv
->outbuf_size
=10000+width
*height
; // must be enough!
75 vf
->priv
->outbuf
= malloc(vf
->priv
->outbuf_size
);
77 if (avcodec_open(&lavc_venc_context
, vf
->priv
->codec
) != 0) {
78 mp_tmsg(MSGT_MENCODER
,MSGL_ERR
,"Could not open codec.\n");
82 if (lavc_venc_context
.codec
->encode
== NULL
) {
83 mp_msg(MSGT_MENCODER
,MSGL_ERR
,"avcodec init failed (ctx->codec->encode == NULL)!\n");
87 return vf_next_config(vf
,width
,height
,d_width
,d_height
,flags
,IMGFMT_MPEGPES
);
90 static int put_image(struct vf_instance
* vf
, mp_image_t
*mpi
, double pts
){
93 AVFrame
*pic
= vf
->priv
->pic
;
95 pic
->data
[0]=mpi
->planes
[0];
96 pic
->data
[1]=mpi
->planes
[1];
97 pic
->data
[2]=mpi
->planes
[2];
98 pic
->linesize
[0]=mpi
->stride
[0];
99 pic
->linesize
[1]=mpi
->stride
[1];
100 pic
->linesize
[2]=mpi
->stride
[2];
102 out_size
= avcodec_encode_video(&lavc_venc_context
,
103 vf
->priv
->outbuf
, vf
->priv
->outbuf_size
, pic
);
105 if(out_size
<=0) return 1;
107 dmpi
=vf_get_image(vf
->next
,IMGFMT_MPEGPES
,
108 MP_IMGTYPE_EXPORT
, 0,
111 vf
->priv
->pes
.data
=vf
->priv
->outbuf
;
112 vf
->priv
->pes
.size
=out_size
;
113 vf
->priv
->pes
.id
=0x1E0;
114 vf
->priv
->pes
.timestamp
=-1; // dunno
116 dmpi
->planes
[0]=(unsigned char*)&vf
->priv
->pes
;
118 return vf_next_put_image(vf
,dmpi
, MP_NOPTS_VALUE
);
121 //===========================================================================//
123 static int query_format(struct vf_instance
* vf
, unsigned int fmt
){
128 return vf_next_query_format(vf
, IMGFMT_MPEGPES
) & (~(VFCAP_CSP_SUPPORTED_BY_HW
| VFCAP_ACCEPT_STRIDE
));
133 static int open(vf_instance_t
*vf
, char* args
){
138 vf
->put_image
=put_image
;
139 vf
->query_format
=query_format
;
140 vf
->priv
=malloc(sizeof(struct vf_priv_s
));
141 memset(vf
->priv
,0,sizeof(struct vf_priv_s
));
143 if (!avcodec_initialized
){
145 avcodec_register_all();
146 avcodec_initialized
=1;
149 vf
->priv
->codec
= (AVCodec
*)avcodec_find_encoder_by_name("mpeg1video");
150 if (!vf
->priv
->codec
) {
151 mp_tmsg(MSGT_MENCODER
,MSGL_ERR
,"Cannot find codec '%s' in libavcodec...\n", "mpeg1video");
155 vf
->priv
->context
=avcodec_alloc_context();
156 vf
->priv
->pic
= avcodec_alloc_frame();
158 // TODO: parse args ->
159 if(args
) sscanf(args
, "%d:%f", &p_quality
, &p_fps
);
163 lavc_venc_context
.flags
= CODEC_FLAG_QSCALE
;
164 lavc_venc_context
.global_quality
=
165 vf
->priv
->pic
->quality
= (int)(FF_QP2LAMBDA
* ((p_quality
<1) ? 1 : p_quality
) + 0.5);
167 // fixed bitrate (in kbits)
168 lavc_venc_context
.bit_rate
= 1000*p_quality
;
170 lavc_venc_context
.time_base
.num
= 1000*1001;
171 lavc_venc_context
.time_base
.den
= (p_fps
<1.0) ? 1000*1001*25 : (p_fps
* lavc_venc_context
.time_base
.num
);
172 lavc_venc_context
.gop_size
= 0; // I-only
173 lavc_venc_context
.pix_fmt
= PIX_FMT_YUV420P
;
178 const vf_info_t vf_info_lavc
= {
179 "realtime mpeg1 encoding with libavcodec",
187 //===========================================================================//