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
|
8340
|
25 #error we dont 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
|
5873
|
37 extern int avcodec_inited;
|
|
38
|
|
39 struct vf_priv_s {
|
|
40 unsigned char* outbuf;
|
|
41 int outbuf_size;
|
7852
|
42 AVCodecContext* context;
|
8413
|
43 AVFrame* pic;
|
7852
|
44 AVCodec* codec;
|
5873
|
45 vo_mpegpes_t pes;
|
|
46 };
|
|
47
|
7852
|
48 #define lavc_venc_context (*vf->priv->context)
|
5873
|
49
|
|
50 //===========================================================================//
|
|
51
|
|
52 static int config(struct vf_instance_s* vf,
|
|
53 int width, int height, int d_width, int d_height,
|
|
54 unsigned int flags, unsigned int outfmt){
|
|
55 if(vf_next_query_format(vf,IMGFMT_MPEGPES)<=0) return 0;
|
|
56
|
|
57 lavc_venc_context.width = width;
|
|
58 lavc_venc_context.height = height;
|
6019
|
59
|
|
60 if(!lavc_venc_context.frame_rate){
|
|
61 // guess FPS:
|
|
62 switch(height){
|
|
63 case 240:
|
|
64 case 480:
|
|
65 lavc_venc_context.frame_rate=29.97*FRAME_RATE_BASE; // NTSC
|
|
66 break;
|
|
67 case 576:
|
|
68 case 288:
|
|
69 default:
|
|
70 lavc_venc_context.frame_rate=25*FRAME_RATE_BASE; // PAL
|
|
71 break;
|
|
72 // lavc_venc_context.frame_rate=vo_fps*FRAME_RATE_BASE; // same as src
|
|
73 }
|
|
74 }
|
5873
|
75
|
|
76 if(vf->priv->outbuf) free(vf->priv->outbuf);
|
|
77
|
|
78 vf->priv->outbuf_size=10000+width*height; // must be enough!
|
|
79 vf->priv->outbuf = malloc(vf->priv->outbuf_size);
|
|
80
|
|
81 if (avcodec_open(&lavc_venc_context, vf->priv->codec) != 0) {
|
|
82 mp_msg(MSGT_MENCODER,MSGL_ERR,MSGTR_CantOpenCodec);
|
|
83 return 0;
|
|
84 }
|
|
85
|
|
86 if (lavc_venc_context.codec->encode == NULL) {
|
|
87 mp_msg(MSGT_MENCODER,MSGL_ERR,"avcodec init failed (ctx->codec->encode == NULL)!\n");
|
|
88 return 0;
|
|
89 }
|
|
90
|
|
91 return vf_next_config(vf,width,height,d_width,d_height,flags,IMGFMT_MPEGPES);
|
|
92 }
|
|
93
|
7368
|
94 static int put_image(struct vf_instance_s* vf, mp_image_t *mpi){
|
5873
|
95 mp_image_t* dmpi;
|
|
96 int out_size;
|
8413
|
97 AVFrame *pic= vf->priv->pic;
|
5873
|
98
|
8339
|
99 pic->data[0]=mpi->planes[0];
|
|
100 pic->data[1]=mpi->planes[1];
|
|
101 pic->data[2]=mpi->planes[2];
|
|
102 pic->linesize[0]=mpi->stride[0];
|
|
103 pic->linesize[1]=mpi->stride[1];
|
|
104 pic->linesize[2]=mpi->stride[2];
|
5873
|
105
|
|
106 out_size = avcodec_encode_video(&lavc_venc_context,
|
8339
|
107 vf->priv->outbuf, vf->priv->outbuf_size, pic);
|
5873
|
108
|
7368
|
109 if(out_size<=0) return 1;
|
5873
|
110
|
|
111 dmpi=vf_get_image(vf->next,IMGFMT_MPEGPES,
|
|
112 MP_IMGTYPE_EXPORT, 0,
|
|
113 mpi->w, mpi->h);
|
|
114
|
|
115 vf->priv->pes.data=vf->priv->outbuf;
|
|
116 vf->priv->pes.size=out_size;
|
|
117 vf->priv->pes.id=0x1E0;
|
|
118 vf->priv->pes.timestamp=-1; // dunno
|
|
119
|
7127
|
120 dmpi->planes[0]=(unsigned char*)&vf->priv->pes;
|
5873
|
121
|
7368
|
122 return vf_next_put_image(vf,dmpi);
|
5873
|
123 }
|
|
124
|
|
125 //===========================================================================//
|
|
126
|
|
127 static int query_format(struct vf_instance_s* vf, unsigned int fmt){
|
|
128 switch(fmt){
|
|
129 case IMGFMT_YV12:
|
|
130 case IMGFMT_I420:
|
|
131 case IMGFMT_IYUV:
|
5876
|
132 return (vf_next_query_format(vf,IMGFMT_MPEGPES) & (~(VFCAP_CSP_SUPPORTED_BY_HW|VFCAP_ACCEPT_STRIDE)));
|
5873
|
133 }
|
|
134 return 0;
|
|
135 }
|
|
136
|
|
137 static int open(vf_instance_t *vf, char* args){
|
6019
|
138 int p_quality=0;
|
|
139 float p_fps=0;
|
|
140
|
5873
|
141 vf->config=config;
|
|
142 vf->put_image=put_image;
|
|
143 vf->query_format=query_format;
|
|
144 vf->priv=malloc(sizeof(struct vf_priv_s));
|
|
145 memset(vf->priv,0,sizeof(struct vf_priv_s));
|
|
146
|
|
147 if (!avcodec_inited){
|
|
148 avcodec_init();
|
|
149 avcodec_register_all();
|
|
150 avcodec_inited=1;
|
|
151 }
|
|
152
|
|
153 vf->priv->codec = (AVCodec *)avcodec_find_encoder_by_name("mpeg1video");
|
|
154 if (!vf->priv->codec) {
|
|
155 mp_msg(MSGT_MENCODER,MSGL_ERR,MSGTR_MissingLAVCcodec, "mpeg1video");
|
|
156 return 0;
|
|
157 }
|
7852
|
158
|
|
159 vf->priv->context=avcodec_alloc_context();
|
8413
|
160 #if LIBAVCODEC_BUILD >= 4645
|
|
161 vf->priv->pic = avcodec_alloc_frame();
|
|
162 #else
|
|
163 vf->priv->pic = avcodec_alloc_picture();
|
|
164 #endif
|
5873
|
165
|
|
166 // TODO: parse args ->
|
6019
|
167 if(args) sscanf(args, "%d:%f", &p_quality, &p_fps);
|
|
168
|
|
169 if(p_quality<32){
|
|
170 // fixed qscale
|
|
171 lavc_venc_context.flags = CODEC_FLAG_QSCALE;
|
8339
|
172 vf->priv->pic->quality = (p_quality<1) ? 1 : p_quality;
|
6019
|
173 } else {
|
|
174 // fixed bitrate (in kbits)
|
|
175 lavc_venc_context.bit_rate = 1000*p_quality;
|
|
176 }
|
|
177 lavc_venc_context.frame_rate = (p_fps<1.0) ? 0 : (p_fps * FRAME_RATE_BASE);
|
5873
|
178 lavc_venc_context.qmin= 1;
|
|
179 lavc_venc_context.gop_size = 0; // I-only
|
|
180
|
|
181 return 1;
|
|
182 }
|
|
183
|
|
184 vf_info_t vf_info_lavc = {
|
|
185 "realtime mpeg1 encoding with libavcodec",
|
|
186 "lavc",
|
|
187 "A'rpi",
|
|
188 "",
|
|
189 open
|
|
190 };
|
|
191
|
|
192 //===========================================================================//
|
|
193 #endif
|