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