1939
|
1 #undef HAVE_DVB
|
1876
|
2 #define PES_MAX_SIZE 2048
|
|
3 /*
|
|
4 * Based on:
|
|
5 *
|
|
6 * test_av.c - Test program for new API
|
|
7 *
|
|
8 * Copyright (C) 2000 Ralph Metzler <ralph@convergence.de>
|
|
9 * & Marcus Metzler <marcus@convergence.de>
|
|
10 * for convergence integrated media GmbH
|
|
11 *
|
|
12 * libav - MPEG-PS multiplexer, part of ffmpeg
|
|
13 * Copyright Gerard Lantau (see http://ffmpeg.sf.net)
|
|
14 *
|
|
15 */
|
1871
|
16
|
|
17 #include <stdio.h>
|
|
18 #include <stdlib.h>
|
|
19 #include <string.h>
|
|
20
|
1876
|
21 #include <sys/types.h>
|
|
22 #include <sys/stat.h>
|
|
23 #include <fcntl.h>
|
|
24 #include <sys/poll.h>
|
|
25
|
|
26 #ifdef HAVE_DVB
|
|
27
|
|
28 #include <sys/ioctl.h>
|
|
29 #include <stdio.h>
|
|
30 #include <time.h>
|
|
31 #include <unistd.h>
|
|
32
|
|
33 #include <ost/dmx.h>
|
|
34 #include <ost/frontend.h>
|
|
35 #include <ost/sec.h>
|
|
36 #include <ost/video.h>
|
|
37 #include <ost/audio.h>
|
|
38
|
|
39 #endif
|
|
40
|
1871
|
41 #include "config.h"
|
|
42 #include "video_out.h"
|
|
43 #include "video_out_internal.h"
|
|
44
|
|
45 LIBVO_EXTERN (mpegpes)
|
|
46
|
1872
|
47 int vo_mpegpes_fd=-1;
|
1876
|
48 int vo_mpegpes_fd2=-1;
|
1872
|
49
|
1935
|
50 #ifdef USE_LIBAVCODEC
|
|
51
|
|
52 #include "../libavcodec/avcodec.h"
|
|
53
|
|
54 static unsigned char *picture_buf=NULL;
|
|
55 static unsigned char *outbuf=NULL;
|
|
56 static int outbuf_size = 100000;
|
|
57
|
|
58 static int s_pos_x,s_pos_y;
|
|
59 static int d_pos_x,d_pos_y;
|
|
60
|
1943
|
61 static int osd_w,osd_h;
|
|
62
|
1935
|
63 static AVPicture picture;
|
|
64 static AVCodec *codec=NULL;
|
|
65 static AVCodecContext codec_context;
|
|
66 extern int avcodec_inited;
|
|
67
|
|
68 #endif
|
|
69
|
1871
|
70 static vo_info_t vo_info =
|
|
71 {
|
1876
|
72 #ifdef HAVE_DVB
|
|
73 "Mpeg-PES to DVB card",
|
|
74 #else
|
1871
|
75 "Mpeg-PES file",
|
1876
|
76 #endif
|
|
77 "mpegpes",
|
1871
|
78 "A'rpi",
|
|
79 ""
|
|
80 };
|
|
81
|
|
82 static uint32_t
|
1935
|
83 init(uint32_t s_width, uint32_t s_height, uint32_t width, uint32_t height, uint32_t fullscreen, char *title, uint32_t format)
|
1871
|
84 {
|
1876
|
85 #ifdef HAVE_DVB
|
|
86 //|O_NONBLOCK
|
|
87 if((vo_mpegpes_fd = open("/dev/ost/video",O_RDWR)) < 0){
|
|
88 perror("DVB VIDEO DEVICE: ");
|
|
89 return -1;
|
|
90 }
|
|
91 if((vo_mpegpes_fd2 = open("/dev/ost/audio",O_RDWR|O_NONBLOCK)) < 0){
|
|
92 perror("DVB AUDIO DEVICE: ");
|
|
93 return -1;
|
|
94 }
|
|
95 if ( (ioctl(vo_mpegpes_fd,VIDEO_SET_BLANK, false) < 0)){
|
|
96 perror("DVB VIDEO SET BLANK: ");
|
|
97 return -1;
|
|
98 }
|
|
99 if ( (ioctl(vo_mpegpes_fd,VIDEO_SELECT_SOURCE, VIDEO_SOURCE_MEMORY) < 0)){
|
|
100 perror("DVB VIDEO SELECT SOURCE: ");
|
|
101 return -1;
|
|
102 }
|
|
103 #if 1
|
|
104 if ( (ioctl(vo_mpegpes_fd2,AUDIO_SELECT_SOURCE, AUDIO_SOURCE_MEMORY) < 0)){
|
|
105 perror("DVB AUDIO SELECT SOURCE: ");
|
|
106 return -1;
|
|
107 }
|
|
108 if ( (ioctl(vo_mpegpes_fd2,AUDIO_PLAY) < 0)){
|
|
109 perror("DVB AUDIO PLAY: ");
|
|
110 return -1;
|
|
111 }
|
|
112 #else
|
|
113 if ( (ioctl(vo_mpegpes_fd2,AUDIO_STOP,0) < 0)){
|
|
114 perror("DVB AUDIO STOP: ");
|
|
115 return -1;
|
|
116 }
|
|
117 #endif
|
|
118 if ( (ioctl(vo_mpegpes_fd,VIDEO_PLAY) < 0)){
|
|
119 perror("DVB VIDEO PLAY: ");
|
|
120 return -1;
|
|
121 }
|
|
122 if ( (ioctl(vo_mpegpes_fd2,AUDIO_SET_AV_SYNC, false) < 0)){
|
|
123 perror("DVB AUDIO SET AV SYNC: ");
|
|
124 return -1;
|
|
125 }
|
|
126 if ( (ioctl(vo_mpegpes_fd2,AUDIO_SET_MUTE, false) < 0)){
|
|
127 perror("DVB AUDIO SET MUTE: ");
|
|
128 return -1;
|
|
129 }
|
|
130
|
|
131 #else
|
|
132 vo_mpegpes_fd=open("grab.mpg",O_WRONLY|O_CREAT);
|
1872
|
133 if(vo_mpegpes_fd<0){
|
|
134 perror("vo_mpegpes");
|
|
135 return -1;
|
|
136 }
|
1876
|
137 #endif
|
1935
|
138
|
|
139 #ifdef USE_LIBAVCODEC
|
|
140 picture_buf=NULL;
|
|
141 if(format==IMGFMT_YV12){
|
|
142 int size;
|
|
143
|
|
144 if(!avcodec_inited){
|
|
145 avcodec_init();
|
|
146 avcodec_register_all();
|
|
147 avcodec_inited=1;
|
|
148 }
|
|
149
|
|
150 /* find the mpeg1 video encoder */
|
|
151 codec = avcodec_find_encoder(CODEC_ID_MPEG1VIDEO);
|
|
152 if (!codec) {
|
|
153 fprintf(stderr, "mpeg1 codec not found\n");
|
|
154 return -1;
|
|
155 }
|
|
156 memset(&codec_context,0,sizeof(codec_context));
|
|
157 codec_context.bit_rate=100000; // not used
|
|
158 codec_context.frame_rate=25*FRAME_RATE_BASE; // !!!!!
|
|
159 codec_context.gop_size=0; // I frames only
|
|
160 codec_context.flags=CODEC_FLAG_QSCALE;
|
|
161 codec_context.quality=2; // quality! 1..31 (1=best,slowest)
|
|
162
|
|
163 #if 0
|
|
164 codec_context.width=width;
|
|
165 codec_context.height=height;
|
|
166 #else
|
|
167 if(width<=352 && height<=288){
|
|
168 codec_context.width=352;
|
|
169 codec_context.height=288;
|
|
170 } else
|
|
171 if(width<=352 && height<=576){
|
|
172 codec_context.width=352;
|
|
173 codec_context.height=576;
|
|
174 } else
|
|
175 if(width<=480 && height<=576){
|
|
176 codec_context.width=480;
|
|
177 codec_context.height=576;
|
|
178 } else
|
|
179 if(width<=544 && height<=576){
|
|
180 codec_context.width=544;
|
|
181 codec_context.height=576;
|
|
182 } else {
|
|
183 codec_context.width=704;
|
|
184 codec_context.height=576;
|
|
185 }
|
|
186 #endif
|
|
187
|
1943
|
188 osd_w=s_width;
|
1935
|
189 d_pos_x=(codec_context.width-(int)s_width)/2;
|
|
190 if(d_pos_x<0){
|
|
191 s_pos_x=-d_pos_x;d_pos_x=0;
|
1943
|
192 osd_w=codec_context.width;
|
1935
|
193 } else s_pos_x=0;
|
|
194
|
1943
|
195 osd_h=s_height;
|
1935
|
196 d_pos_y=(codec_context.height-(int)s_height)/2;
|
|
197 if(d_pos_y<0){
|
|
198 s_pos_y=-d_pos_y;d_pos_y=0;
|
1943
|
199 osd_h=codec_context.height;
|
1935
|
200 } else s_pos_y=0;
|
1943
|
201
|
1935
|
202 printf("[vo] position mapping: %d;%d => %d;%d\n",s_pos_x,s_pos_y,d_pos_x,d_pos_y);
|
|
203
|
|
204 /* open it */
|
|
205 if (avcodec_open(&codec_context, codec) < 0) {
|
|
206 fprintf(stderr, "could not open codec\n");
|
|
207 return -1;
|
|
208 }
|
|
209
|
|
210 outbuf_size=10000+width*height; // must be enough!
|
|
211 outbuf = malloc(outbuf_size);
|
|
212
|
|
213 size = codec_context.width*codec_context.height;
|
|
214 picture_buf = malloc((size * 3) / 2); /* size for YUV 420 */
|
|
215
|
|
216 memset(picture_buf,0,size); // clear Y
|
|
217 memset(picture_buf+size,128,size/2); // clear UV
|
|
218
|
|
219 picture.data[0] = picture_buf;
|
|
220 picture.data[1] = picture.data[0] + size;
|
|
221 picture.data[2] = picture.data[1] + size / 4;
|
|
222 picture.linesize[0] = codec_context.width;
|
|
223 picture.linesize[1] = codec_context.width / 2;
|
|
224 picture.linesize[2] = codec_context.width / 2;
|
|
225
|
|
226 }
|
|
227 #endif
|
1871
|
228 return 0;
|
|
229 }
|
|
230
|
|
231 static const vo_info_t*
|
|
232 get_info(void)
|
|
233 {
|
|
234 return &vo_info;
|
|
235 }
|
|
236
|
1943
|
237 #ifdef USE_LIBAVCODEC
|
|
238 static void draw_alpha(int x0,int y0, int w,int h, unsigned char* src, unsigned char *srca, int stride){
|
|
239 int x,y;
|
|
240 vo_draw_alpha_yv12(w,h,src,srca,stride,
|
|
241 picture.data[0]+(x0+d_pos_x)+(y0+d_pos_y)*picture.linesize[0],picture.linesize[0]);
|
|
242 }
|
|
243 #endif
|
|
244
|
1871
|
245 static void draw_osd(void)
|
|
246 {
|
1943
|
247 #ifdef USE_LIBAVCODEC
|
|
248 if(picture_buf){ // YV12 only:
|
|
249 vo_draw_text(osd_w,osd_h,draw_alpha);
|
|
250 }
|
|
251 #endif
|
1871
|
252 }
|
|
253
|
1876
|
254 #define NFD 2
|
|
255
|
1872
|
256 static void my_write(unsigned char* data,int len){
|
1876
|
257 struct pollfd pfd[NFD];
|
|
258
|
|
259 // printf("write %d bytes \n",len);
|
|
260
|
|
261 pfd[0].fd = vo_mpegpes_fd;
|
|
262 pfd[0].events = POLLOUT;
|
|
263
|
|
264 pfd[1].fd = vo_mpegpes_fd2;
|
|
265 pfd[1].events = POLLOUT;
|
|
266
|
1872
|
267 while(len>0){
|
1876
|
268 if (poll(pfd,NFD,1)){
|
|
269 if (pfd[0].revents & POLLOUT){
|
|
270 int ret=write(vo_mpegpes_fd,data,len);
|
|
271 // printf("ret=%d \n",ret);
|
|
272 if(ret<=0){
|
|
273 perror("write");
|
|
274 usleep(0);
|
|
275 } else {
|
|
276 len-=ret; data+=ret;
|
|
277 }
|
|
278 } else usleep(1000);
|
|
279 }
|
1872
|
280 }
|
|
281 }
|
1871
|
282
|
1876
|
283 static unsigned char pes_header[PES_MAX_SIZE];
|
|
284
|
|
285 static void send_pes_packet(unsigned char* data,int len,int id,int timestamp){
|
|
286 int x;
|
|
287
|
|
288 pes_header[0]=pes_header[1]=0;
|
|
289 pes_header[2]=id>>8; pes_header[3]=id&255;
|
|
290
|
|
291 while(1){
|
|
292 int payload_size=len+5; // data + PTS
|
|
293 if(6+payload_size>PES_MAX_SIZE) payload_size=PES_MAX_SIZE-6;
|
|
294
|
|
295 // construct PES header: (code from ffmpeg's libav)
|
|
296 // startcode:
|
|
297 // packetsize:
|
|
298 pes_header[4]=(payload_size)>>8;
|
|
299 pes_header[5]=(payload_size)&255;
|
|
300 // stuffing:
|
|
301 // presentation time stamp:
|
|
302 x=(0x02 << 4) | (((timestamp >> 30) & 0x07) << 1) | 1;
|
|
303 pes_header[6]=x;
|
|
304 x=((((timestamp >> 15) & 0x7fff) << 1) | 1);
|
|
305 pes_header[7]=x>>8; pes_header[8]=x&255;
|
|
306 x=((((timestamp) & 0x7fff) << 1) | 1);
|
|
307 pes_header[9]=x>>8; pes_header[10]=x&255;
|
|
308
|
|
309 payload_size-=5;
|
|
310 memcpy(&pes_header[6+5],data,payload_size);
|
|
311 my_write(pes_header,6+5+payload_size);
|
|
312
|
|
313 len-=payload_size; data+=payload_size;
|
|
314 if(len<=0) break;
|
|
315 }
|
|
316
|
|
317 // printf("PES: draw frame! pts=%d size=%d \n",timestamp,len);
|
|
318
|
|
319 }
|
|
320
|
1871
|
321 static uint32_t draw_frame(uint8_t * src[])
|
|
322 {
|
1872
|
323 vo_mpegpes_t *p=(vo_mpegpes_t *)src[0];
|
1876
|
324 unsigned char *data=p->data;
|
|
325 // int tmp=-1;
|
|
326 send_pes_packet(p->data,p->size,p->id,p->timestamp); // video data
|
|
327 // send_pes_packet(&tmp,0,0x1C0,p->timestamp+30000); // fake audio data
|
1871
|
328
|
|
329 return 0;
|
|
330 }
|
|
331
|
1935
|
332 static void flip_page (void)
|
|
333 {
|
|
334 #ifdef USE_LIBAVCODEC
|
|
335 if(picture_buf){ // YV12 only:
|
|
336 int out_size;
|
|
337 static int fno=0;
|
|
338 /* encode the image */
|
|
339 out_size = avcodec_encode_video(&codec_context, outbuf, outbuf_size, &picture);
|
|
340 send_pes_packet(outbuf,out_size,0x1E0,fno*(90000/25));++fno;
|
|
341 // printf("frame size: %d \n",out_size);
|
|
342 }
|
|
343 #endif
|
|
344 }
|
|
345
|
|
346 static uint32_t draw_slice(uint8_t *srcimg[], int stride[], int w,int h,int x0,int y0)
|
|
347 {
|
|
348 #ifdef USE_LIBAVCODEC
|
|
349 int y;
|
|
350 unsigned char* s;
|
|
351 unsigned char* d;
|
|
352
|
|
353 x0+=d_pos_x;
|
|
354 y0+=d_pos_y;
|
|
355 if(w>picture.linesize[0]) w=picture.linesize[0]; // !!
|
|
356
|
|
357 // Y
|
|
358 s=srcimg[0]+s_pos_x+s_pos_y*stride[0];
|
|
359 d=picture.data[0]+x0+y0*picture.linesize[0];
|
|
360 for(y=0;y<h;y++){
|
|
361 memcpy(d,s,w);
|
|
362 s+=stride[0];
|
|
363 d+=picture.linesize[0];
|
|
364 }
|
|
365
|
|
366 w/=2;h/=2;x0/=2;y0/=2;
|
|
367
|
|
368 // U
|
|
369 s=srcimg[1]+(s_pos_x/2)+(s_pos_y/2)*stride[1];
|
|
370 d=picture.data[1]+x0+y0*picture.linesize[1];
|
|
371 for(y=0;y<h;y++){
|
|
372 memcpy(d,s,w);
|
|
373 s+=stride[1];
|
|
374 d+=picture.linesize[1];
|
|
375 }
|
|
376
|
|
377 // V
|
|
378 s=srcimg[2]+(s_pos_x/2)+(s_pos_y/2)*stride[2];
|
|
379 d=picture.data[2]+x0+y0*picture.linesize[2];
|
|
380 for(y=0;y<h;y++){
|
|
381 memcpy(d,s,w);
|
|
382 s+=stride[2];
|
|
383 d+=picture.linesize[2];
|
|
384 }
|
|
385 #endif
|
|
386 return 0;
|
|
387 }
|
|
388
|
|
389
|
1871
|
390 static uint32_t
|
|
391 query_format(uint32_t format)
|
|
392 {
|
|
393 if(format==IMGFMT_MPEGPES) return 1;
|
1935
|
394 #ifdef USE_LIBAVCODEC
|
|
395 if(format==IMGFMT_YV12) return 1;
|
|
396 #endif
|
1871
|
397 return 0;
|
|
398 }
|
|
399
|
|
400 static void
|
|
401 uninit(void)
|
|
402 {
|
1935
|
403 #ifdef USE_LIBAVCODEC
|
|
404 if(picture_buf){ // YV12 only:
|
|
405 free(outbuf);
|
|
406 free(picture_buf);
|
|
407 }
|
|
408 #endif
|
1876
|
409 if(vo_mpegpes_fd>=0){ close(vo_mpegpes_fd);vo_mpegpes_fd=-1;}
|
|
410 if(vo_mpegpes_fd2>=0){ close(vo_mpegpes_fd2);vo_mpegpes_fd2=-1;}
|
1871
|
411 }
|
|
412
|
|
413
|
|
414 static void check_events(void)
|
|
415 {
|
|
416 }
|
|
417
|