Subversion Repositories Kolibri OS

Rev

Rev 5592 | Rev 6106 | Go to most recent revision | Details | Compare with Previous | 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"
5021 Serge 14
4438 Serge 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
int fplay_init_context(AVCodecContext *avctx);
5592 serge 26
4438 Serge 27
 
28
AVCodecContext  *pCodecCtx;
29
AVCodecContext  *aCodecCtx;
30
AVCodec         *pCodec;
31
AVCodec         *aCodec;
32
AVFrame         *pFrame;
33
int             videoStream;
34
int             audioStream;
35
36
 
37
38
 
39
extern int resampler_size;
40
41
 
42
char *movie_file;
43
44
 
45
46
 
47
queue_t  q_audio;
48
int64_t  rewind_pos;
49
50
 
51
52
 
5603 serge 53
mutex_t driver_lock;
54
55
 
4438 Serge 56
57
 
58
{
59
  return (double)av_q2d(pFormatCtx->streams[audioStream]->time_base)*1000;
60
};
61
62
 
63
 
64
{
65
    int i;
66
    char *file_name, *dot;
67
68
 
69
    {
70
        movie_file = get_moviefile();
71
        if(movie_file == NULL)
72
        {
73
            printf("Please provide a movie file\n");
74
            return -1;
75
        }
76
    }
77
    else movie_file = argv[1];
78
79
 
80
81
 
82
83
 
84
    avdevice_register_all();
85
    av_register_all();
86
87
 
88
    {
89
        printf("Cannot open file %s\n\r", movie_file);
90
        return -1; // Couldn't open file
91
    };
92
93
 
94
95
 
96
    if(avformat_find_stream_info(pFormatCtx, NULL)<0)
97
    {
98
        printf("Cannot find streams\n\r");
99
        return -1;
100
    };
101
102
 
103
    dot = strrchr(file_name,'.');
104
    if(dot)
105
    {
106
        movie_file = malloc(dot-file_name+1);
107
        memcpy(movie_file, file_name, dot-file_name);
108
        movie_file[dot-file_name] = 0;
109
    }
110
    else movie_file = file_name;
111
112
 
113
114
 
115
116
 
117
   // Find the first video stream
118
    videoStream=-1;
119
    audioStream=-1;
120
    for(i=0; i < pFormatCtx->nb_streams; i++)
121
    {
122
        if(pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO
123
            && videoStream < 0)
124
        {
125
            videoStream=i;
126
            video_time_base = pFormatCtx->streams[i]->time_base;
127
            if(stream_duration == 0)
128
               stream_duration = pFormatCtx->streams[i]->duration;
129
130
 
131
        if(pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_AUDIO &&
132
            audioStream < 0)
133
        {
134
            audioStream=i;
135
            if(stream_duration == 0)
136
               stream_duration = pFormatCtx->streams[i]->duration;
137
        }
138
    }
139
140
 
141
    {
142
        printf("Video stream not detected\n\r");
143
        return -1; // Didn't find a video stream
144
    };
145
146
 
147
148
 
149
    pCodecCtx = pFormatCtx->streams[videoStream]->codec;
5592 serge 150
    aCodecCtx = pFormatCtx->streams[audioStream]->codec;
151
4438 Serge 152
 
153
154
 
155
 
156
157
 
158
159
 
160
        printf("Unsupported codec with id %d for input stream %d\n",
161
        pCodecCtx->codec_id, videoStream);
162
        return -1; // Codec not found
163
    }
164
165
 
166
    {
167
        printf("Error while opening codec for input stream %d\n",
168
                videoStream);
169
        return -1; // Could not open codec
170
    };
171
172
 
173
174
 
5603 serge 175
    mutex_init(&q_video.lock);
176
    mutex_init(&q_audio.lock);
177
4438 Serge 178
 
179
            aCodecCtx->request_channels = FFMIN(2, aCodecCtx->channels);
180
    else
181
            aCodecCtx->request_channels = 2;
182
183
 
184
185
 
186
    {
187
        if(avcodec_open2(aCodecCtx, aCodec, NULL) >= 0 )
188
        {
189
            WAVEHEADER       whdr;
190
            int fmt;
191
            int channels;
192
193
 
194
            aCodecCtx->sample_rate, aCodecCtx->channels, aCodecCtx->sample_fmt );
195
            whdr.riff_id = 0x46464952;
196
            whdr.riff_format = 0x45564157;
197
            whdr.wFormatTag = 0x01;
198
            whdr.nSamplesPerSec = aCodecCtx->sample_rate;
199
            whdr.nChannels = 2;
200
            whdr.wBitsPerSample = 16;
201
202
 
203
204
 
205
206
 
207
            {
208
                decoder_buffer = (uint8_t*)av_mallocz(192000*2+64);
209
                if( decoder_buffer != NULL )
210
                {
211
                    mutex_init(&astream.lock);
5603 serge 212
                    astream.count  = 0;
4438 Serge 213
                    astream.buffer = (char *)av_mallocz(192000*3);
214
                    if( astream.buffer != NULL )
215
                        have_sound = 1;
216
                    else
217
                        av_free(decoder_buffer);
218
                }
219
                if( have_sound == 0)
220
                {
221
                        printf("Not enough memory for audio buffers\n");
222
                }
223
            }
224
        }
225
        else printf("Cannot open audio codec\n\r");
226
    }
227
    else printf("Unsupported audio codec!\n");
228
229
 
230
        return 0;
231
232
 
233
234
 
235
    av_free(pFrame);
236
237
 
238
 
239
240
 
5603 serge 241
           (AUDIO_THREAD | VIDEO_THREAD))
242
           delay(1);
243
4438 Serge 244
 
5603 serge 245
        mutex_destroy(&astream.lock);
246
4438 Serge 247
 
5603 serge 248
    mutex_destroy(&q_video.lock);
249
    mutex_destroy(&q_audio.lock);
250
4438 Serge 251
 
252
}
253
254
 
255
 
256
{
257
    AVPacket  packet;
258
    int err;
259
260
 
261
    if( err == 0)
262
    {
263
        if(packet.stream_index==videoStream)
264
            put_packet(&q_video, &packet);
265
        else if( (packet.stream_index == audioStream) &&
266
                  (have_sound != 0) )
267
        {
268
            put_packet(&q_audio, &packet);
269
            if(audio_base == -1.0)
270
            {
271
                if (packet.pts != AV_NOPTS_VALUE)
272
                    audio_base = get_audio_base() * packet.pts;
273
//                    printf("audio base %f\n", audio_base);
274
            };
275
        }
276
        else av_free_packet(&packet);
277
    }
278
    else if (err != AVERROR_EOF)
279
        printf("av_read_frame: error %x\n", err);
280
281
 
282
}
283
284
 
285
 
286
 
287
{
288
    int err = 0;
289
    AVPacket  packet;
290
291
 
292
            !err )
293
        err = load_frame();
294
295
 
296
297
 
298
299
 
300
 
301
{
302
    AVPacket  packet;
303
304
 
305
    avcodec_flush_buffers(aCodecCtx);
306
    while( get_packet(&q_video, &packet) != 0)
307
        av_free_packet(&packet);
308
309
 
310
        av_free_packet(&packet);
311
312
 
313
314
 
315
};
316
317
 
318
{
319
    int       eof;
320
    AVPacket  packet;
321
    int       ret, vret, aret;
322
323
 
324
325
 
5592 serge 326
327
 
4438 Serge 328
    {
329
        int err;
330
331
 
332
        {
333
            case PREPARE:
334
                eof = fill_queue();
335
336
 
337
                {
338
                    if( (q_video.size < 4*1024*1024) &&
339
                        (eof == 0) )
340
                    {
341
                        eof = load_frame();
342
                    }
343
                    decode_video(pCodecCtx, &q_video);
344
                    ret = decode_audio(aCodecCtx, &q_audio);
345
                }while(astream.count < resampler_size*2 &&
346
                       ret == 1);
347
348
 
349
                decoder_state = PLAY;
350
                player_state  = PLAY;
351
352
 
353
                if( (q_video.size < 4*1024*1024) &&
354
                    (eof == 0) )
355
                {
356
                    eof = load_frame();
357
                }
358
                vret = decode_video(pCodecCtx, &q_video);
359
                aret = decode_audio(aCodecCtx, &q_audio);
360
                ret = vret | aret;
361
362
 
363
                {
364
                    decoder_state = STOP;
365
                    continue;
366
                };
367
368
 
369
                {
370
                    if( (q_video.size < 4*1024*1024) &&
371
                        (eof == 0) )
372
                    {
373
                        eof = load_frame();
374
                        yield();
375
                        continue;
376
                    };
377
                    delay(1);
378
                    continue;
379
                }
380
381
 
382
                continue;
383
384
 
385
                delay(1);
386
                continue;
387
388
 
389
 
390
                while(sound_state != STOP)
391
                    delay(1);
392
393
 
394
395
 
396
                    rewind_pos = pFormatCtx->start_time;
397
                else
398
                    rewind_pos = 0;
399
400
 
401
                                         rewind_pos, INT64_MAX, 0);
402
403
 
404
                break;
405
406
 
407
                while(sound_state != STOP)
408
                    yield();
409
410
 
411
                int opts = 0;
412
                if(rewind_pos < 0)
413
                {
414
                    rewind_pos = -rewind_pos;
415
                    opts = AVSEEK_FLAG_BACKWARD;
416
                };
417
418
 
419
                    rewind_pos += pFormatCtx->start_time;
420
421
 
422
                min_pos = rewind_pos - 1000000;
423
                max_pos = rewind_pos + 1000000;
424
425
 
426
                                         rewind_pos, INT64_MAX, 0);
427
428
 
429
                {
430
                    printf("could not seek to position %f\n",
431
                            (double)rewind_pos / AV_TIME_BASE);
432
                }
433
434
 
435
                decoder_state = PREPARE;
436
                break;
437
        }
438
    };
439
440
 
441
>
5592 serge 442