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