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