forked from FFmpeg/FFmpeg
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.c
More file actions
472 lines (366 loc) · 12.6 KB
/
Copy pathtest.c
File metadata and controls
472 lines (366 loc) · 12.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
/*
test.c
simple test program based on avcodec_sample.0.4.9.cpp
*/
#define INCL_DOS
#define INCL_WIN
#define INCL_GPI
#include <os2.h>
#include <mmioos2.h>
#include <fourcc.h>
#undef __STRICT_ANSI__
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "libavformat/avformat.h"
#include "libswscale/swscale.h"
#include "libswscale/rgb2rgb.h"
#include "libavcodec/dsputil.h"
#include "kva.h"
#include "libsub/subreader.h"
AVFormatContext *pFormatCtx;
AVCodecContext *pCodecCtxV;
AVFrame *pFrame;
AVFrame *pFrameOS2;
int16_t *pAudioBuf;
int videoStream;
int audioStream;
CHAR szSubName[ 255 ];
BOOL fQuit = FALSE;
void MorphToPM()
{
PPIB pib;
PTIB tib;
DosGetInfoBlocks(&tib, &pib);
// Change flag from VIO to PM:
if( pib->pib_ultype == 2 )
pib->pib_ultype = 3;
}
MRESULT EXPENTRY WndProc( HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2 )
{
return WinDefWindowProc( hwnd, msg, mp1, mp2 );
}
VOID Delay( ULONG ms )
{
PTIB ptib;
ULONG ulClass;
ULONG ulDelta;
DosGetInfoBlocks( &ptib, NULL );
ulClass = HIBYTE( ptib->tib_ptib2->tib2_ulpri );
ulDelta = LOBYTE( ptib->tib_ptib2->tib2_ulpri );
DosSetPriority( PRTYS_THREAD, PRTYC_TIMECRITICAL, PRTYD_MAXIMUM, 0 );
DosSleep( ms );
DosSetPriority( PRTYS_THREAD, ulClass, ulDelta, 0 );
}
VOID DisplaySub( HPS hps, PRECTL prcl, subtitle *sub, ULONG ulColor )
{
FONTMETRICS fm;
ULONG ulHeight;
POINTL aptl[ TXTBOX_COUNT ];
POINTL ptl;
int len, extent;
int i;
GpiQueryFontMetrics( hps, sizeof( FONTMETRICS ), &fm );
ulHeight = fm.lMaxBaselineExt + fm.lExternalLeading;
ptl.y = ulHeight * ( sub->lines - 1 ) + fm.lMaxDescender;
GpiCreateLogColorTable( hps, 0, LCOLF_RGB, 0, 0, NULL );
GpiSetColor( hps, ulColor );
for( i = 0; i < sub->lines; i++ )
{
len = strlen( sub->text[ i ]);
GpiQueryTextBox( hps, len, sub->text[ i ], TXTBOX_COUNT, aptl );
extent = aptl[ TXTBOX_CONCAT ].x - aptl[ TXTBOX_BOTTOMLEFT ].x;
ptl.x = (( prcl->xRight - prcl->xLeft ) - extent ) / 2 + prcl->xLeft;
GpiCharStringAt( hps, &ptl, strlen( sub->text[ i ] ), sub->text[ i ]);
ptl.y -= ulHeight;
}
}
void VideoThread( void *arg )
{
HWND hwnd = ( HWND )arg;
AVPacket packet;
int frameFinished;
ULONG frame_delay = av_q2d( pFormatCtx->streams[ videoStream ]->time_base ) * 1000;
sub_data * sd;
PSZ pszExt;
subtitle * cur_sub = NULL;
subtitle * prev_sub = NULL;
ULONG ulCurTime = 0;
PVOID pBuffer, pBuffer1;
ULONG ulBPL;
HPS hps;
RECTL rcl;
struct SwsContext * sws;
int sws_flags = SWS_FAST_BILINEAR | SWS_PRINT_INFO;
mm_flags = mm_support();
if( mm_flags & MM_MMX )
sws_flags |= SWS_CPU_CAPS_MMX;
if( mm_flags & MM_MMXEXT )
sws_flags |= SWS_CPU_CAPS_MMX2;
if( mm_flags & MM_3DNOW )
sws_flags |= SWS_CPU_CAPS_3DNOW;
sws_rgb2rgb_init( sws_flags );
sws = sws_getContext( pCodecCtxV->width, pCodecCtxV->height, pCodecCtxV->pix_fmt,
pCodecCtxV->width, pCodecCtxV->height, PIX_FMT_YUYV422,
sws_flags, NULL, NULL, NULL );
WinSetPresParam( hwnd, PP_FONTNAMESIZE, 8, "32.±¼¸²" );
hps = WinGetPS( hwnd );
WinQueryWindowRect( hwnd, &rcl );
pszExt = strrchr( szSubName, '.' );
strcpy( pszExt, ".smi" );
sd = sub_read_file( strdup( szSubName ), 0 );
if( sd )
{
#if 0
int i, j;
printf("subtitle info \n");
printf("filename = %s, uses_time = %d, num = %d, errs = %d\n",
sd->filename, sd->sub_uses_time, sd->sub_num, sd->sub_errs );
for( i = 0; i < sd->sub_num; i++ )
{
printf("%dth subtitle info\n", i );
printf("lines = %d, start = %lu, end = %lu, alignment = %d\n",
sd->subtitles[ i ].lines, sd->subtitles[ i ].start, sd->subtitles[ i ].end, sd->subtitles[ i ].alignment );
for( j = 0; j < sd->subtitles[ i ].lines; j++ )
printf(" %dth line = %s\n", j, sd->subtitles[ i ].text[ j ]);
}
#endif
prev_sub = cur_sub = sd->subtitles;
}
printf("video: stream time_base = %f, codec time_base = %f\n",
av_q2d( pFormatCtx->streams[ videoStream ]->time_base),
av_q2d( pCodecCtxV->time_base ));
printf("audio: stream time_base = %f, codec time_base = %f\n",
av_q2d( pFormatCtx->streams[ audioStream ]->time_base),
av_q2d( pFormatCtx->streams[ audioStream ]->codec->time_base ));
while( !fQuit )
{
if( av_read_frame( pFormatCtx, &packet ) < 0 )
{
printf("Maybe reached at end\n");
continue;
}
#if 0
if( packet.stream_index == audioStream )
{
printf("audio : pts = %Ld, dts = %Ld, duration = %d\n",
packet.pts, packet.dts, packet.duration );
}
#endif
// Is this a packet from the video stream?
if( packet.stream_index == videoStream )
{
// Decode video frame
avcodec_decode_video( pCodecCtxV, pFrame, &frameFinished,
packet.data, packet.size );
// Did we get a video frame?
if(frameFinished)
{
#if 0
if( packet.pts == AV_NOPTS_VALUE )
printf("video : pts = AV_NOPTS_VALUE, dts = %Ld, duration = %d \n",
packet.dts, packet.duration );
else
printf("video : pts = %Ld, dts = %Ld, duration = %d \n",
packet.pts, packet.dts, packet.duration );
#endif
kvaLockBuffer( &pBuffer, &ulBPL );
//pBuffer1 = av_malloc( pCodecCtxV->height * ulBPL );
if( sws )
{
sws_scale( sws, pFrame->data, pFrame->linesize, 0, pCodecCtxV->height,
&pBuffer, &ulBPL );
emms_c();
}
//memcpy( pBuffer, pBuffer1, pCodecCtxV->height * ulBPL );
//av_free( pBuffer1 );
if( sd )
{
if( prev_sub != cur_sub )
{
DisplaySub( hps, &rcl, prev_sub, 8 );
prev_sub = cur_sub;
}
}
kvaUnlockBuffer();
if( sd )
{
ulCurTime = frame_delay * packet.dts / 10; // in 1/100 s
#if 0
printf("ulCurTime = %lu, start = %lu, end = %lu\n",
ulCurTime, cur_sub->start, cur_sub->end );
#endif
if( ulCurTime >= cur_sub->start && ulCurTime <= cur_sub->end )
{
DisplaySub( hps, &rcl, cur_sub, 0xFFFFFF );
}
else
{
prev_sub = cur_sub;
while( cur_sub != sd->subtitles && ulCurTime < cur_sub->start )
cur_sub--;
while( cur_sub->lines && ulCurTime > cur_sub->end )
cur_sub++;
}
}
Delay( frame_delay );
}
}
// Free the packet that was allocated by av_read_frame
av_free_packet(&packet);
}
sub_free( sd );
WinReleasePS( hps );
if( sws )
sws_freeContext( sws );
_endthread();
}
int main(int argc, char *argv[])
{
HAB hab;
HMQ hmq;
ULONG flFrameFlags;
HWND hwndFrame;
HWND hwndClient;
QMSG qm;
PSZ szWndClass = "KMP_CLASS";
PSZ szAppTitle = "K Movie Player";
TID tid_video;
KVASETUP KvaSetup = { 0 };
RECTL rcl;
int i;
AVCodec *pCodecV;
int ret = 1;
if( argc < 2 )
{
fprintf( stderr, "Specify input file\n");
return ret;
}
// Morph the VIO application to a PM one to be able to use Win* functions
MorphToPM();
// Make stdout and stderr unbuffered
setbuf( stdout, NULL );
setbuf( stderr, NULL );
hab = WinInitialize( 0 );
hmq = WinCreateMsgQueue( hab, 0);
WinRegisterClass(
hab,
szWndClass,
WndProc,
CS_SIZEREDRAW,
sizeof( PVOID )
);
flFrameFlags = FCF_SYSMENU | FCF_TITLEBAR | FCF_MINMAX | FCF_SIZEBORDER |
FCF_SHELLPOSITION | FCF_TASKLIST;
hwndFrame = WinCreateStdWindow (
HWND_DESKTOP,
WS_VISIBLE ,
&flFrameFlags,
szWndClass,
szAppTitle,
0,
0,
1,
&hwndClient);
if( kvaInit( KVAM_WO, hwndClient, 0x000008 ))
{
WinMessageBox( HWND_DESKTOP, HWND_DESKTOP,
"Error",
"Can't init overlay!", 0, MB_ICONHAND | MB_OK);
goto exit_frame;
}
// Register all formats and codecs
av_register_all();
// Open video file
if( av_open_input_file( &pFormatCtx, argv[1], NULL, 0, NULL ) != 0 )
goto exit_kva; // Couldn't open file
strcpy( szSubName, argv[ 1 ]);
// Retrieve stream information
if( av_find_stream_info( pFormatCtx ) < 0 )
goto exit_close_file; // Couldn't find stream information
// Dump information about file onto standard error
dump_format( pFormatCtx, 0, argv[ 1 ], FALSE );
// Find the first video stream
videoStream = -1;
audioStream = -1;
for( i = 0; i < pFormatCtx->nb_streams; i++ )
{
switch( pFormatCtx->streams[ i ]->codec->codec_type )
{
case CODEC_TYPE_VIDEO :
if( videoStream < 0 )
videoStream = i;
break;
case CODEC_TYPE_AUDIO :
if( audioStream < 0 )
audioStream = i;
break;
}
}
if( videoStream < 0 )
goto exit_close_file; // Didn't find a video stream
// Get a pointer to the codec context for the video stream
pCodecCtxV = pFormatCtx->streams[ videoStream ]->codec;
// Find the decoder for the video stream
pCodecV = avcodec_find_decoder( pCodecCtxV->codec_id );
if( pCodecV == NULL )
goto exit_close_file; // Codec not found
// Inform the codec that we can handle truncated bitstreams -- i.e.,
// bitstreams where frame boundaries can fall in the middle of packets
//if( pCodecV->capabilities & CODEC_CAP_TRUNCATED )
//pCodecCtxV->flags |= CODEC_FLAG_TRUNCATED;
// Open codec
if( avcodec_open( pCodecCtxV, pCodecV ) < 0 )
goto exit_close_file; // Could not open codec
// Allocate video frame
pFrame = avcodec_alloc_frame();
// Allocate an AVFrame structure
pFrameOS2 = avcodec_alloc_frame();
KvaSetup.ulLength = sizeof( KVASETUP );
KvaSetup.szlSrcSize.cx = pCodecCtxV->width;
KvaSetup.szlSrcSize.cy = pCodecCtxV->height;
KvaSetup.fccSrcColor = FOURCC_Y422;
KvaSetup.rclSrcRect.xLeft = 0;
KvaSetup.rclSrcRect.yTop = 0;
KvaSetup.rclSrcRect.xRight = pCodecCtxV->width;
KvaSetup.rclSrcRect.yBottom = pCodecCtxV->height;
KvaSetup.ulRatio = KVAR_ORIGINAL;
if( kvaSetup( &KvaSetup ))
{
WinMessageBox (HWND_DESKTOP, HWND_DESKTOP,
"Error",
"Can't setup overlay!", 0, MB_ICONHAND | MB_OK);
goto exit_close_codec;
}
WinSetWindowPos( hwndFrame, HWND_TOP, 100, 100,
pCodecCtxV->width, pCodecCtxV->height,
SWP_SIZE | SWP_MOVE | SWP_SHOW | SWP_ZORDER | SWP_ACTIVATE );
WinQueryWindowRect( hwndClient, &rcl );
WinSetWindowPos( hwndFrame, HWND_TOP, 100, 100,
2 * pCodecCtxV->width - rcl.xRight + rcl.xLeft,
2 * pCodecCtxV->height -rcl.yTop + rcl.yBottom,
SWP_SIZE | SWP_MOVE | SWP_SHOW | SWP_ZORDER | SWP_ACTIVATE );
tid_video = _beginthread( VideoThread, NULL, 32768, ( void * )hwndClient );
while( WinGetMsg( hab, &qm, NULLHANDLE, 0, 0 ))
WinDispatchMsg( hab, &qm );
fQuit = TRUE;
DosWaitThread( &tid_video, DCWW_WAIT );
// Free the RGB image
av_free(pFrameOS2);
// Free the YUV frame
av_free(pFrame);
ret = 0;
exit_close_codec:
// Close the codec
avcodec_close(pCodecCtxV);
exit_close_file:
// Close the video file
av_close_input_file(pFormatCtx);
exit_kva:
kvaDone();
exit_frame:
WinDestroyWindow( hwndFrame );
WinDestroyMsgQueue( hmq );
WinTerminate( hab );
return ret;
}