10 #include "img_format.h"
13 #include "libavcodec/avcodec.h"
15 extern int avcodec_initialized
;
18 unsigned char* outbuf
;
20 AVCodecContext
* context
;
26 #define lavc_venc_context (*vf->priv->context)
28 //===========================================================================//
30 static int config(struct vf_instance_s
* vf
,
31 int width
, int height
, int d_width
, int d_height
,
32 unsigned int flags
, unsigned int outfmt
){
33 if(vf_next_query_format(vf
,IMGFMT_MPEGPES
)<=0) return 0;
35 lavc_venc_context
.width
= width
;
36 lavc_venc_context
.height
= height
;
38 if(!lavc_venc_context
.time_base
.num
|| !lavc_venc_context
.time_base
.den
){
43 lavc_venc_context
.time_base
= (AVRational
){1001,30000};
48 lavc_venc_context
.time_base
= (AVRational
){1,25};
50 // lavc_venc_context.frame_rate=vo_fps*FRAME_RATE_BASE; // same as src
54 if(vf
->priv
->outbuf
) free(vf
->priv
->outbuf
);
56 vf
->priv
->outbuf_size
=10000+width
*height
; // must be enough!
57 vf
->priv
->outbuf
= malloc(vf
->priv
->outbuf_size
);
59 if (avcodec_open(&lavc_venc_context
, vf
->priv
->codec
) != 0) {
60 mp_msg(MSGT_MENCODER
,MSGL_ERR
,MSGTR_CantOpenCodec
);
64 if (lavc_venc_context
.codec
->encode
== NULL
) {
65 mp_msg(MSGT_MENCODER
,MSGL_ERR
,"avcodec init failed (ctx->codec->encode == NULL)!\n");
69 return vf_next_config(vf
,width
,height
,d_width
,d_height
,flags
,IMGFMT_MPEGPES
);
72 static int put_image(struct vf_instance_s
* vf
, mp_image_t
*mpi
, double pts
){
75 AVFrame
*pic
= vf
->priv
->pic
;
77 pic
->data
[0]=mpi
->planes
[0];
78 pic
->data
[1]=mpi
->planes
[1];
79 pic
->data
[2]=mpi
->planes
[2];
80 pic
->linesize
[0]=mpi
->stride
[0];
81 pic
->linesize
[1]=mpi
->stride
[1];
82 pic
->linesize
[2]=mpi
->stride
[2];
84 out_size
= avcodec_encode_video(&lavc_venc_context
,
85 vf
->priv
->outbuf
, vf
->priv
->outbuf_size
, pic
);
87 if(out_size
<=0) return 1;
89 dmpi
=vf_get_image(vf
->next
,IMGFMT_MPEGPES
,
93 vf
->priv
->pes
.data
=vf
->priv
->outbuf
;
94 vf
->priv
->pes
.size
=out_size
;
95 vf
->priv
->pes
.id
=0x1E0;
96 vf
->priv
->pes
.timestamp
=-1; // dunno
98 dmpi
->planes
[0]=(unsigned char*)&vf
->priv
->pes
;
100 return vf_next_put_image(vf
,dmpi
, MP_NOPTS_VALUE
);
103 //===========================================================================//
105 static int query_format(struct vf_instance_s
* vf
, unsigned int fmt
){
110 return vf_next_query_format(vf
, IMGFMT_MPEGPES
) & (~(VFCAP_CSP_SUPPORTED_BY_HW
| VFCAP_ACCEPT_STRIDE
));
115 static int open(vf_instance_t
*vf
, char* args
){
120 vf
->put_image
=put_image
;
121 vf
->query_format
=query_format
;
122 vf
->priv
=malloc(sizeof(struct vf_priv_s
));
123 memset(vf
->priv
,0,sizeof(struct vf_priv_s
));
125 if (!avcodec_initialized
){
127 avcodec_register_all();
128 avcodec_initialized
=1;
131 vf
->priv
->codec
= (AVCodec
*)avcodec_find_encoder_by_name("mpeg1video");
132 if (!vf
->priv
->codec
) {
133 mp_msg(MSGT_MENCODER
,MSGL_ERR
,MSGTR_MissingLAVCcodec
, "mpeg1video");
137 vf
->priv
->context
=avcodec_alloc_context();
138 vf
->priv
->pic
= avcodec_alloc_frame();
140 // TODO: parse args ->
141 if(args
) sscanf(args
, "%d:%f", &p_quality
, &p_fps
);
145 lavc_venc_context
.flags
= CODEC_FLAG_QSCALE
;
146 lavc_venc_context
.global_quality
=
147 vf
->priv
->pic
->quality
= (int)(FF_QP2LAMBDA
* ((p_quality
<1) ? 1 : p_quality
) + 0.5);
149 // fixed bitrate (in kbits)
150 lavc_venc_context
.bit_rate
= 1000*p_quality
;
152 lavc_venc_context
.time_base
.num
= 1000*1001;
153 lavc_venc_context
.time_base
.den
= (p_fps
<1.0) ? 1000*1001*25 : (p_fps
* lavc_venc_context
.time_base
.num
);
154 lavc_venc_context
.gop_size
= 0; // I-only
155 lavc_venc_context
.pix_fmt
= PIX_FMT_YUV420P
;
160 const vf_info_t vf_info_lavc
= {
161 "realtime mpeg1 encoding with libavcodec",
169 //===========================================================================//