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