Subversion Repositories Kolibri OS

Rev

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