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