Subversion Repositories Kolibri OS

Rev

Rev 5592 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
4438 Serge 1
 
2
3
 
4
#include 
5
#include 
6
#include 
7
8
 
9
#include 
10
#include 
11
#include 
12
#include 
13
#include "../winlib/winlib.h"
14
15
 
16
#include "fplay.h"
17
18
 
19
volatile enum player_state decoder_state = PREPARE;
20
volatile enum player_state sound_state   = STOP;
21
22
 
23
24
 
25
26
 
27
AVCodecContext  *pCodecCtx;
28
AVCodecContext  *aCodecCtx;
29
AVCodec         *pCodec;
30
AVCodec         *aCodec;
31
AVFrame         *pFrame;
32
int             videoStream;
33
int             audioStream;
34
35
 
36
37
 
38
extern int resampler_size;
39
40
 
41
char *movie_file;
42
43
 
44
45
 
46
queue_t  q_audio;
47
int64_t  rewind_pos;
48
49
 
50
51
 
52
53
 
54
{
55
56
 
57
58
 
59
60
 
61
 
62
 
63
{
64
    int i;
65
    char *file_name, *dot;
66
67
 
68
    {
69
        movie_file = get_moviefile();
70
        if(movie_file == NULL)
71
        {
72
            printf("Please provide a movie file\n");
73
            return -1;
74
        }
75
    }
76
    else movie_file = argv[1];
77
78
 
79
80
 
81
    {
82
        int fd, i;
83
        char *buff;
84
        uint32_t start, stop;
85
86
 
87
88
 
89
            return 0;
90
91
 
92
        memset(buff, 0, 65536);
93
94
 
95
96
 
97
        {
98
            if(read(fd, buff, 65536) < 0)
99
                break;
100
101
 
102
        stop = get_tick_count();
103
104
 
105
    };
106
    return 0;
107
};
108
109
 
110
111
 
112
113
 
114
    avdevice_register_all();
115
    av_register_all();
116
117
 
118
119
 
120
    {
121
        printf("Cannot open file %s\n\r", movie_file);
122
        return -1; // Couldn't open file
123
    };
124
125
 
126
127
 
128
    if(avformat_find_stream_info(pFormatCtx, NULL)<0)
129
    {
130
        printf("Cannot find streams\n\r");
131
        return -1;
132
    };
133
134
 
135
    dot = strrchr(file_name,'.');
136
    if(dot)
137
    {
138
        movie_file = malloc(dot-file_name+1);
139
        memcpy(movie_file, file_name, dot-file_name);
140
        movie_file[dot-file_name] = 0;
141
    }
142
    else movie_file = file_name;
143
144
 
145
 
146
147
 
148
149
 
150
    stream_duration = pFormatCtx->duration;
151
152
 
153
   // Find the first video stream
154
    videoStream=-1;
155
    audioStream=-1;
156
    for(i=0; i < pFormatCtx->nb_streams; i++)
157
    {
158
//        pFormatCtx->streams[i]->discard = AVDISCARD_ALL;
159
160
 
161
            && videoStream < 0)
162
        {
163
            videoStream=i;
164
            video_time_base = pFormatCtx->streams[i]->time_base;
165
            if(stream_duration == 0)
166
//                stream_duration = 1000.0 *
167
//                              pFormatCtx->streams[i]->duration *
168
//                              av_q2d(pFormatCtx->streams[i]->time_base);
169
               stream_duration = pFormatCtx->streams[i]->duration;
170
171
 
172
        if(pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_AUDIO &&
173
            audioStream < 0)
174
        {
175
            audioStream=i;
176
            if(stream_duration == 0)
177
//                stream_duration = 1000.0 *
178
//                              pFormatCtx->streams[i]->duration *
179
//                              av_q2d(pFormatCtx->streams[i]->time_base);
180
               stream_duration = pFormatCtx->streams[i]->duration;
181
182
 
183
    }
184
185
 
186
    {
187
        printf("Video stream not detected\n\r");
188
        return -1; // Didn't find a video stream
189
    };
190
191
 
192
    {
193
        AVPacket  packet;
194
        int psize = 0;
195
        uint32_t start, stop;
196
197
 
198
        start = get_tick_count();
199
200
 
201
        {
202
            err = av_read_frame(pFormatCtx, &packet);
203
            if(err != 0)
204
                break;
205
            psize+= packet.size;
206
            av_free_packet(&packet);
207
        };
208
209
 
210
211
 
212
213
 
214
    };
215
};
216
#else
217
218
 
219
 
220
221
 
222
    pCodecCtx=pFormatCtx->streams[videoStream]->codec;
223
    aCodecCtx=pFormatCtx->streams[audioStream]->codec;
224
225
 
226
227
 
228
229
 
230
231
 
232
233
 
234
        printf("Unsupported codec with id %d for input stream %d\n",
235
        pCodecCtx->codec_id, videoStream);
236
        return -1; // Codec not found
237
    }
238
239
 
240
    {
241
        printf("Error while opening codec for input stream %d\n",
242
                videoStream);
243
        return -1; // Could not open codec
244
    };
245
246
 
247
248
 
249
 
250
            aCodecCtx->request_channels = FFMIN(2, aCodecCtx->channels);
251
    else
252
            aCodecCtx->request_channels = 2;
253
254
 
255
256
 
257
    {
258
        if(avcodec_open2(aCodecCtx, aCodec, NULL) >= 0 )
259
        {
260
            WAVEHEADER       whdr;
261
            int fmt;
262
            int channels;
263
264
 
265
            aCodecCtx->sample_rate, aCodecCtx->channels, aCodecCtx->sample_fmt );
266
            whdr.riff_id = 0x46464952;
267
            whdr.riff_format = 0x45564157;
268
            whdr.wFormatTag = 0x01;
269
            whdr.nSamplesPerSec = aCodecCtx->sample_rate;
270
            whdr.nChannels = 2;
271
            whdr.wBitsPerSample = 16;
272
273
 
274
275
 
276
277
 
278
            {
279
                decoder_buffer = (uint8_t*)av_mallocz(192000*2+64);
280
                if( decoder_buffer != NULL )
281
                {
282
                    astream.lock   = 0;
283
                    astream.count  = 0;
284
                    astream.buffer = (char *)av_mallocz(192000*3);
285
                    if( astream.buffer != NULL )
286
                        have_sound = 1;
287
                    else
288
                        av_free(decoder_buffer);
289
                }
290
                if( have_sound == 0)
291
                {
292
                        printf("Not enough memory for audio buffers\n");
293
                }
294
            }
295
        }
296
        else printf("Cannot open audio codec\n\r");
297
    }
298
    else printf("Unsupported audio codec!\n");
299
300
 
301
        return 0;
302
303
 
304
305
 
306
307
 
308
    av_free(pFrame);
309
310
 
311
 
312
313
 
314
 // avcodec_close(pCodecCtx);
315
316
 
317
 // av_close_input_file(pFormatCtx);
318
319
 
320
321
 
322
}
323
324
 
325
 
326
{
327
    AVPacket  packet;
328
    int err;
329
330
 
331
    if( err == 0)
332
    {
333
        if(packet.stream_index==videoStream)
334
            put_packet(&q_video, &packet);
335
        else if( (packet.stream_index == audioStream) &&
336
                  (have_sound != 0) )
337
        {
338
            put_packet(&q_audio, &packet);
339
            if(audio_base == -1.0)
340
            {
341
                if (packet.pts != AV_NOPTS_VALUE)
342
                    audio_base = get_audio_base() * packet.pts;
343
//                    printf("audio base %f\n", audio_base);
344
            };
345
        }
346
        else av_free_packet(&packet);
347
    }
348
    else if (err != AVERROR_EOF)
349
        printf("av_read_frame: error %x\n", err);
350
351
 
352
}
353
354
 
355
 
356
 
357
{
358
    int err = 0;
359
    AVPacket  packet;
360
361
 
362
363
 
364
            !err )
365
        err = load_frame();
366
367
 
368
369
 
370
371
 
372
 
373
{
374
    AVPacket  packet;
375
376
 
377
    avcodec_flush_buffers(aCodecCtx);
378
    while( get_packet(&q_video, &packet) != 0)
379
        av_free_packet(&packet);
380
381
 
382
        av_free_packet(&packet);
383
384
 
385
386
 
387
};
388
389
 
390
{
391
    int       eof;
392
    AVPacket  packet;
393
    int       ret, vret, aret;
394
395
 
396
397
 
398
    {
399
        int err;
400
401
 
402
403
 
404
        {
405
            case PREPARE:
406
                eof = fill_queue();
407
408
 
409
                {
410
                    if( (q_video.size < 4*1024*1024) &&
411
                        (eof == 0) )
412
                    {
413
                        eof = load_frame();
414
                    }
415
                    decode_video(pCodecCtx, &q_video);
416
                    ret = decode_audio(aCodecCtx, &q_audio);
417
                }while(astream.count < resampler_size*2 &&
418
                       ret == 1);
419
420
 
421
                decoder_state = PLAY;
422
                player_state  = PLAY;
423
424
 
425
                if( (q_video.size < 4*1024*1024) &&
426
                    (eof == 0) )
427
                {
428
                    eof = load_frame();
429
                }
430
                vret = decode_video(pCodecCtx, &q_video);
431
                aret = decode_audio(aCodecCtx, &q_audio);
432
                ret = vret | aret;
433
434
 
435
                {
436
                    decoder_state = STOP;
437
                    continue;
438
                };
439
440
 
441
                {
442
                    if( (q_video.size < 4*1024*1024) &&
443
                        (eof == 0) )
444
                    {
445
                        eof = load_frame();
446
                        yield();
447
                        continue;
448
                    };
449
                    delay(1);
450
                    continue;
451
                }
452
453
 
454
                continue;
455
456
 
457
                delay(1);
458
                continue;
459
460
 
461
 
462
                while(sound_state != STOP)
463
                    delay(1);
464
465
 
466
467
 
468
                    rewind_pos = pFormatCtx->start_time;
469
                else
470
                    rewind_pos = 0;
471
472
 
473
                                         rewind_pos, INT64_MAX, 0);
474
475
 
476
                break;
477
478
 
479
                while(sound_state != STOP)
480
                    yield();
481
482
 
483
                int opts = 0;
484
                if(rewind_pos < 0)
485
                {
486
                    rewind_pos = -rewind_pos;
487
                    opts = AVSEEK_FLAG_BACKWARD;
488
                };
489
490
 
491
                    rewind_pos += pFormatCtx->start_time;
492
493
 
494
                min_pos = rewind_pos - 1000000;
495
                max_pos = rewind_pos + 1000000;
496
497
 
498
                                         rewind_pos, INT64_MAX, 0);
499
500
 
501
//                                         rewind_pos, max_pos, opts);
502
//            __asm__ __volatile__("int3");
503
504
 
505
                {
506
                    printf("could not seek to position %f\n",
507
                            (double)rewind_pos / AV_TIME_BASE);
508
                }
509
510
 
511
                decoder_state = PREPARE;
512
                break;
513
        }
514
    };
515
516
 
517
518
 
519
    {
520
        ret =  decode_video(pCodecCtx, &q_video);
521
        ret |= decode_audio(aCodecCtx, &q_audio);
522
        delay(1);
523
    };
524
    delay(50);
525
    player_state = CLOSED;
526
    delay(300);
527
};
528
#endif
529
#endif
530
>