Subversion Repositories Kolibri OS

Rev

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