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