Subversion Repositories Kolibri OS

Rev

Rev 2427 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1696 serge 1
 
2
3
 
4
#include 
5
#include 
6
#include 
7
8
 
9
#include 
10
#include 
11
#include 
2349 Serge 12
#include "../winlib/winlib.h"
2427 Serge 13
2349 Serge 14
 
1696 serge 15
#include "fplay.h"
16
17
 
2427 Serge 18
1696 serge 19
 
20
21
 
22
23
 
24
AVCodecContext  *pCodecCtx;
25
AVCodecContext  *aCodecCtx;
26
AVCodec         *pCodec;
27
AVCodec         *aCodec;
28
AVFrame         *pFrame;
29
int             videoStream;
30
int             audioStream;
31
32
 
33
34
 
35
extern int sample_rate;
36
char *movie_file;
2349 Serge 37
1696 serge 38
 
2693 Serge 39
2349 Serge 40
 
41
queue_t  q_audio;
42
int64_t  rewind_pos;
2693 Serge 43
2349 Serge 44
 
2693 Serge 45
46
 
47
48
 
49
{
50
51
 
52
53
 
54
55
 
56
 
1696 serge 57
{
58
    int i;
59
60
 
61
        printf("Please provide a movie file\n");
62
        return -1;
63
    }
64
65
 
2349 Serge 66
    /* register all codecs, demux and protocols */
1696 serge 67
68
 
2415 Serge 69
70
 
1696 serge 71
    avdevice_register_all();
72
    av_register_all();
73
74
 
2349 Serge 75
    {
1696 serge 76
        printf("Cannot open file %s\n\r", argv[1]);
77
        return -1; // Couldn't open file
78
    };
79
80
 
2693 Serge 81
1696 serge 82
 
83
    if(avformat_find_stream_info(pFormatCtx, NULL)<0)
2349 Serge 84
    {
1696 serge 85
        printf("Cannot find streams\n\r");
86
        return -1;
87
    };
88
89
 
2349 Serge 90
1696 serge 91
 
2415 Serge 92
1696 serge 93
 
2693 Serge 94
    stream_duration = pFormatCtx->duration;
95
96
 
97
   // Find the first video stream
1696 serge 98
    videoStream=-1;
99
    audioStream=-1;
100
    for(i=0; i < pFormatCtx->nb_streams; i++)
101
    {
102
//        pFormatCtx->streams[i]->discard = AVDISCARD_ALL;
2693 Serge 103
104
 
2349 Serge 105
            && videoStream < 0)
1696 serge 106
        {
107
            videoStream=i;
108
            video_time_base = pFormatCtx->streams[i]->time_base;
109
            if(stream_duration == 0)
2693 Serge 110
//                stream_duration = 1000.0 *
111
//                              pFormatCtx->streams[i]->duration *
112
//                              av_q2d(pFormatCtx->streams[i]->time_base);
113
               stream_duration = pFormatCtx->streams[i]->duration;
114
115
 
1696 serge 116
        if(pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_AUDIO &&
2349 Serge 117
            audioStream < 0)
1696 serge 118
        {
119
            audioStream=i;
120
            if(stream_duration == 0)
2693 Serge 121
//                stream_duration = 1000.0 *
122
//                              pFormatCtx->streams[i]->duration *
123
//                              av_q2d(pFormatCtx->streams[i]->time_base);
124
               stream_duration = pFormatCtx->streams[i]->duration;
125
126
 
1696 serge 127
    }
128
129
 
130
    {
131
        printf("Video stream not detected\n\r");
132
        return -1; // Didn't find a video stream
133
    }
134
135
 
2693 Serge 136
2427 Serge 137
 
2349 Serge 138
1696 serge 139
 
140
    pCodecCtx=pFormatCtx->streams[videoStream]->codec;
141
    aCodecCtx=pFormatCtx->streams[audioStream]->codec;
142
143
 
144
    pCodec=avcodec_find_decoder(pCodecCtx->codec_id);
145
    if(pCodec==NULL) {
146
        printf("Unsupported codec with id %d for input stream %d\n",
2349 Serge 147
        pCodecCtx->codec_id, videoStream);
148
        return -1; // Codec not found
1696 serge 149
    }
150
2349 Serge 151
 
152
    {
1696 serge 153
        printf("Error while opening codec for input stream %d\n",
2349 Serge 154
                videoStream);
155
        return -1; // Could not open codec
1696 serge 156
    };
157
158
 
159
            aCodecCtx->request_channels = FFMIN(2, aCodecCtx->channels);
160
    else
161
            aCodecCtx->request_channels = 2;
162
163
 
164
165
 
166
    {
167
        if(avcodec_open2(aCodecCtx, aCodec, NULL) >= 0 )
2349 Serge 168
        {
1696 serge 169
            WAVEHEADER       whdr;
170
            int fmt;
171
172
 
173
            aCodecCtx->sample_rate, aCodecCtx->channels);
174
175
 
176
            whdr.riff_format = 0x45564157;
177
            whdr.wFormatTag = 0x01;
178
            whdr.nSamplesPerSec = aCodecCtx->sample_rate;
179
            whdr.nChannels = aCodecCtx->channels;
180
            whdr.wBitsPerSample = 16;
181
182
 
183
184
 
185
186
 
187
            {
188
                decoder_buffer = (uint8_t*)av_mallocz(AVCODEC_MAX_AUDIO_FRAME_SIZE*2+64);
2349 Serge 189
                if( decoder_buffer != NULL )
1696 serge 190
                {
191
                    astream.lock   = 0;
192
                    astream.count  = 0;
193
                    astream.buffer = (char *)av_mallocz(AVCODEC_MAX_AUDIO_FRAME_SIZE*8);
194
                    if( astream.buffer != NULL )
195
                        have_sound = 1;
196
                    else
197
                        av_free(decoder_buffer);
198
                }
199
                if( have_sound == 0)
200
                {
201
                        printf("Not enough memory for audio buffers\n");
202
                }
203
            }
204
        }
205
        else printf("Cannot open audio codec\n\r");
206
    }
207
    else printf("Unsupported audio codec!\n");
208
209
 
210
        return 0;
211
212
 
2349 Serge 213
1696 serge 214
 
215
216
 
217
    av_free(pFrame);
218
219
 
2349 Serge 220
 
1696 serge 221
222
 
223
 // avcodec_close(pCodecCtx);
224
225
 
226
 // av_close_input_file(pFormatCtx);
227
228
 
229
230
 
231
}
232
233
 
2693 Serge 234
 
235
{
236
    int eof = 0;
237
    AVPacket  packet;
238
239
 
240
    {
241
        int err;
242
243
 
244
245
 
246
        {
247
            err = av_read_frame(pFormatCtx, &packet);
248
            if( err < 0)
249
            {
250
                eof = 1;
251
                if (err != AVERROR_EOF)
252
                    printf("av_read_frame: error %x\n", err);
253
                break;
254
            }
255
            if(packet.stream_index==videoStream)
256
            {
257
                put_packet(&q_video, &packet);
258
            }
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.dts != AV_NOPTS_VALUE)
266
                        audio_base = get_audio_base() * packet.dts;
267
//                    printf("audio base %f\n", audio_base);
268
                };
269
            }
270
            else
271
            {
272
                av_free_packet(&packet);
273
            };
274
        }
275
        else break;
276
    };
277
278
 
279
280
 
281
282
 
283
 
1696 serge 284
{
285
    int       eof;
2693 Serge 286
    AVPacket  packet;
2349 Serge 287
    int       ret;
288
1696 serge 289
 
2693 Serge 290
291
 
2427 Serge 292
    {
1696 serge 293
        int err;
2349 Serge 294
1696 serge 295
 
2349 Serge 296
1696 serge 297
 
2427 Serge 298
        {
299
            delay(1);
300
            continue;
301
        };
302
303
 
2693 Serge 304
        {
2349 Serge 305
 //           int64_t timestamp = 0;
2693 Serge 306
 //           int stream_index = av_find_default_stream_index(pFormatCtx);
307
308
 
309
310
 
311
                rewind_pos += pFormatCtx->start_time;
312
313
 
314
315
 
316
                                     rewind_pos, INT64_MAX, 0);
317
//            ret = avformat_seek_file(pFormatCtx, -1, 0,
318
//                                 0, INT64_MAX, 0);
319
//            __asm__ __volatile__("int3");
320
321
 
322
            {
323
                printf("could not seek to position %f\n",
324
                        (double)rewind_pos / AV_TIME_BASE);
325
            }
326
            else
327
            {
328
                avcodec_flush_buffers(pCodecCtx);
329
                avcodec_flush_buffers(aCodecCtx);
330
331
 
332
                    av_free_packet(&packet);
333
334
 
335
                    av_free_packet(&packet);
336
                audio_base = -1.0;
337
338
 
339
340
 
341
            };
342
            yield();
343
344
 
345
346
 
347
            printf("restart\n");
348
            continue;
349
        };
350
351
 
352
        {
353
            err = av_read_frame(pFormatCtx, &packet);
2349 Serge 354
            if( err < 0)
355
            {
1696 serge 356
                eof = 1;
2349 Serge 357
                if (err != AVERROR_EOF)
358
                    printf("av_read_frame: error %x\n", err);
359
                continue;
360
            }
361
            if(packet.stream_index==videoStream)
362
            {
363
                put_packet(&q_video, &packet);
364
            }
365
            else if( (packet.stream_index == audioStream) &&
366
                 (have_sound != 0) )
367
            {
368
                put_packet(&q_audio, &packet);
369
            }
370
            else
371
            {
372
                av_free_packet(&packet);
373
            };
374
            decode_video(pCodecCtx, &q_video);
375
            decode_audio(aCodecCtx, &q_audio);
376
            continue;
377
        };
378
        decode_video(pCodecCtx, &q_video);
379
        decode_audio(aCodecCtx, &q_audio);
380
        delay(1);
381
    };
382
1696 serge 383
 
2349 Serge 384
1696 serge 385
 
2427 Serge 386
    {
2349 Serge 387
        ret =  decode_video(pCodecCtx, &q_video);
388
        ret |= decode_audio(aCodecCtx, &q_audio);
389
        delay(1);
390
    };
1696 serge 391
    delay(50);
2349 Serge 392
    player_state = CLOSED;
2427 Serge 393
    delay(300);
2415 Serge 394
};
1696 serge 395
>
396