Statistics
| Branch: | Revision:

chunker-player / chunker_player.c @ bd209760

History | View | Annotate | Download (34.1 KB)

1
// player.c
2
// Author 
3
// Diego Reforgiato, Dario Marchese, Carmelo Daniele, Giuseppe Tropea
4
//
5
// Use the file compile to compile the program to build (assuming libavformat and libavcodec are 
6
// correctly installed your system).
7
//
8
// Run using
9
//
10
// player <width> <height>
11

    
12
#include <libavcodec/avcodec.h>
13
#include <libavformat/avformat.h>
14
#include <libswscale/swscale.h>
15

    
16
#include <stdio.h>
17
#include <dirent.h>
18
#include <string.h>
19
#include <stdlib.h>
20
#include <sys/time.h>
21
#include <unistd.h>
22
#include <signal.h>
23
#include <SDL.h>
24
#include <SDL_thread.h>
25
#include <SDL_mutex.h>
26

    
27

    
28
#ifdef __MINGW32__
29
#undef main /* Prevents SDL from overriding main() */
30
#endif
31

    
32
#include <platform.h>
33
#include <microhttpd.h>
34

    
35
#include "chunker_player.h"
36
#include "codec_definitions.h"
37

    
38
#define SDL_AUDIO_BUFFER_SIZE 1024
39

    
40
#define QUEUE_FILLING_THRESHOLD 200
41
#define MAX_TOLLERANCE 40
42
#define AUDIO        1
43
#define VIDEO        2
44

    
45
//#define DEBUG_AUDIO
46
//#define DEBUG_VIDEO
47
//#define DEBUG_QUEUE
48
//#define DEBUG_SOURCE
49
//#define DEBUG_ENQUEUE
50
//#define DEBUG_STATS
51
//#define DEBUG_AUDIO_BUFFER
52
//#define DEBUG_ENQUEUE
53

    
54
short int QueueFillingMode=1;
55
short int QueueStopped=0;
56

    
57
typedef struct PacketQueue {
58
        AVPacketList *first_pkt;
59
        //AVPacketList *last_pkt;
60
        int nb_packets;
61
        int size;
62
        SDL_mutex *mutex;
63
        //SDL_cond *cond;
64
        short int queueType;
65
        int last_frame_extracted; //HINT THIS SHOULD BE MORE THAN 4 BYTES
66
        int total_lost_frames;
67
} PacketQueue;
68

    
69
typedef struct threadVal {
70
        int width;
71
        int height;
72
} ThreadVal;
73

    
74
int AudioQueueOffset=0;
75
PacketQueue audioq;
76
PacketQueue videoq;
77
AVPacket AudioPkt, VideoPkt;
78
int quit = 0;
79

    
80
SDL_Surface *screen;
81
SDL_Overlay *yuv_overlay;
82
SDL_Rect    rect;
83
SDL_AudioSpec spec;
84
struct SwsContext *img_convert_ctx = NULL;
85
//SDL_mutex *timing_mutex;
86

    
87
int got_sigint = 0;
88

    
89
long long DeltaTime;
90
short int FirstTimeAudio=1, FirstTime = 1;
91

    
92
int dimAudioQ;
93
float deltaAudioQ;
94
float deltaAudioQError=0;
95

    
96
void packet_queue_init(PacketQueue *q, short int Type) {
97
#ifdef DEBUG_QUEUE
98
        printf("QUEUE: INIT BEGIN: NPackets=%d Type=%d\n", q->nb_packets, q->queueType);
99
#endif
100
        memset(q,0,sizeof(PacketQueue));
101
        q->mutex = SDL_CreateMutex();
102
        //q->cond = SDL_CreateCond();
103
        QueueFillingMode=1;
104
        q->queueType=Type;
105
        q->last_frame_extracted = 3;
106
        q->total_lost_frames = 0;
107
        q->first_pkt= NULL;
108
        //q->last_pkt = NULL;
109
        q->nb_packets = 0;
110
        q->size = 0;
111
        FirstTime = 1;
112
        FirstTimeAudio = 1;
113
#ifdef DEBUG_QUEUE
114
        printf("QUEUE: INIT END: NPackets=%d Type=%d\n", q->nb_packets, q->queueType);
115
#endif
116
}
117

    
118
void packet_queue_reset(PacketQueue *q, short int Type) {
119
        AVPacketList *tmp,*tmp1;
120
#ifdef DEBUG_QUEUE
121
        printf("QUEUE: RESET BEGIN: NPackets=%d Type=%d LastExtr=%d\n", q->nb_packets, q->queueType, q->last_frame_extracted);
122
#endif
123
        SDL_LockMutex(q->mutex);
124

    
125
        tmp = q->first_pkt;
126
        while(tmp) {
127
                tmp1 = tmp;
128
                tmp = tmp->next;
129
                av_free_packet(&(tmp1->pkt));
130
                av_free(tmp1);
131
#ifdef DEBUG_QUEUE
132
        printf("F ");
133
#endif
134
        }
135
#ifdef DEBUG_QUEUE
136
        printf("\n");
137
#endif
138

    
139
        QueueFillingMode=1;
140
        q->last_frame_extracted = 3;
141
        q->total_lost_frames = 0;
142
        q->first_pkt= NULL;
143
        //q->last_pkt = NULL;
144
        q->nb_packets = 0;
145
        q->size = 0;
146
        FirstTime = 1;
147
        FirstTimeAudio = 1;
148
#ifdef DEBUG_QUEUE
149
        printf("QUEUE: RESET END: NPackets=%d Type=%d LastExtr=%d\n", q->nb_packets, q->queueType, q->last_frame_extracted);
150
#endif
151
        SDL_UnlockMutex(q->mutex);
152
}
153

    
154
int packet_queue_put(PacketQueue *q, AVPacket *pkt) {
155
        short int skip = 0;
156
        AVPacketList *pkt1, *tmp, *prevtmp;
157

    
158
        //make a copy of the incoming packet
159
        if(av_dup_packet(pkt) < 0) {
160
#ifdef DEBUG_QUEUE
161
                printf("QUEUE: PUT in Queue cannot duplicate in packet        : NPackets=%d Type=%d\n",q->nb_packets,q->queueType);
162
#endif
163
                return -1;
164
        }
165
        pkt1 = av_malloc(sizeof(AVPacketList));
166

    
167
        if(!pkt1) {
168
                av_free_packet(pkt);
169
                return -1;
170
        }
171
        pkt1->pkt = *pkt;
172
        pkt1->next = NULL;
173

    
174
        SDL_LockMutex(q->mutex);
175

    
176
/*
177
        if(QueueFillingMode) {
178
                q->last_frame_extracted = 0;
179
                FirstTime = 1;
180
                FirstTimeAudio = 1;
181
        }
182
*/
183

    
184
        // INSERTION SORT ALGORITHM
185
        // before inserting pkt, check if pkt.stream_index is <= current_extracted_frame.
186

    
187
        if(pkt->stream_index>q->last_frame_extracted) {
188
                // either checking starting from the first_pkt or needed other struct like AVPacketList with next and prev....
189
                //if (!q->last_pkt)
190
                if(!q->first_pkt)
191
                        q->first_pkt = pkt1;
192
                else if(pkt->stream_index < q->first_pkt->pkt.stream_index) {
193
                        //the packet that has arrived is earlier than the first we got some time ago!
194
                        //we need to put it at the head of the queue
195
                        pkt1->next = q->first_pkt;
196
                        q->first_pkt = pkt1;
197
                }
198
                else {
199
                        tmp = q->first_pkt;
200
                        while(tmp->pkt.stream_index<pkt->stream_index) {
201
                                prevtmp = tmp;
202
                                tmp = tmp->next;
203

    
204
                                if(!tmp) {
205
                                        break;
206
                                }
207
                        }
208
                        if(tmp && tmp->pkt.stream_index==pkt->stream_index) {
209
                                //we already have a frame with that index
210
                                skip = 1;
211
#ifdef DEBUG_QUEUE
212
                                printf("QUEUE: PUT: we already have frame with index %d, skipping\n", pkt->stream_index);
213
#endif
214
                        }
215
                        else {
216
                                prevtmp->next = pkt1;
217
                                pkt1->next = tmp;
218
                        }
219
                        //q->last_pkt->next = pkt1; // It was uncommented when not insertion sort
220
                }
221
                if(skip==0) {
222
                        //q->last_pkt = pkt1;
223
                        q->nb_packets++;
224
                        q->size += pkt1->pkt.size;
225
                        if(q->nb_packets>=QUEUE_FILLING_THRESHOLD && QueueFillingMode) // && q->queueType==AUDIO)
226
                        {
227
                                QueueFillingMode=0;
228
#ifdef DEBUG_QUEUE
229
                                printf("QUEUE: PUT: FillingMode set to zero\n");
230
#endif
231
                                //SDL_CondSignal(q->cond);
232
                        }
233
                        //WHYq->last_frame_extracted = pkt->stream_index;
234
                }
235
        }
236
        else {
237
                av_free_packet(&pkt1->pkt);
238
                av_free(pkt1);
239
#ifdef DEBUG_QUEUE
240
                                printf("QUEUE: PUT: NOT inserting because index %d > last extracted %d\n", pkt->stream_index, q->last_frame_extracted);
241
#endif
242
        }
243

    
244
        SDL_UnlockMutex(q->mutex);
245
        return 0;
246
}
247

    
248

    
249
void decode_enqueued_audio_packet(AVPacket *pkt) {
250
        uint16_t *audio_bufQ = NULL;
251
        int16_t *dataQ = NULL;
252
        int data_sizeQ = AVCODEC_MAX_AUDIO_FRAME_SIZE;
253
        int lenQ;
254

    
255
        //set the flag to decoded anyway        
256
        pkt->convergence_duration = -1;
257

    
258
        audio_bufQ = (uint16_t *)av_malloc(AVCODEC_MAX_AUDIO_FRAME_SIZE);
259
        if(audio_bufQ) {
260
#ifdef DEBUG_AUDIO_BUFFER
261
                        printf("AUDIO_BUFFER: about to decode packet %d, size %d, data %d\n", pkt->stream_index, pkt->size, pkt->data);
262
#endif
263
                //decode the packet data
264
                lenQ = avcodec_decode_audio3(aCodecCtx, (int16_t *)audio_bufQ, &data_sizeQ, pkt);
265
                if(lenQ > 0) {
266
                        dataQ = (int16_t *)av_malloc(data_sizeQ); //this will be free later at the time of playback
267
                        if(dataQ) {
268
                                memcpy(dataQ, audio_bufQ, data_sizeQ);
269
                                //discard the encoded bytes
270
                                av_free(pkt->data);
271
                                pkt->data = (int8_t *)dataQ;
272
                                pkt->size = data_sizeQ;
273
                        }
274
                        else {
275
                                //discard the encoded bytes
276
                                av_free(pkt->data);
277
                                pkt->data = NULL;
278
                                pkt->size = 0;
279
#ifdef DEBUG_AUDIO_BUFFER
280
                                printf("AUDIO_BUFFER: cannot alloc space for decoded packet %d\n", pkt->stream_index);
281
#endif
282
                        }
283
                }
284
                else {
285
                        //discard the encoded bytes
286
                        av_free(pkt->data);
287
                        pkt->data = NULL;
288
                        pkt->size = 0;
289
#ifdef DEBUG_AUDIO_BUFFER
290
                        printf("AUDIO_BUFFER: cannot decode packet %d\n", pkt->stream_index);
291
#endif
292
                }
293
                av_free(audio_bufQ);
294
        }
295
        else {
296
                //discard the encoded bytes
297
                av_free(pkt->data);
298
                pkt->data = NULL;
299
                pkt->size = 0;
300
#ifdef DEBUG_AUDIO_BUFFER
301
                printf("AUDIO_BUFFER: cannot alloc decode buffer for packet %d\n", pkt->stream_index);
302
#endif
303
        }
304
}
305

    
306

    
307

    
308

    
309
int packet_queue_get(PacketQueue *q, AVPacket *pkt, short int av) {
310
        //AVPacket tmp;
311
        AVPacketList *pkt1 = NULL;
312
        int ret=-1;
313
        int SizeToCopy=0;
314

    
315
        SDL_LockMutex(q->mutex);
316

    
317
#ifdef DEBUG_QUEUE
318
        printf("QUEUE: Get NPackets=%d Type=%d\n", q->nb_packets, q->queueType);
319
#endif
320

    
321
        if((q->queueType==AUDIO && QueueFillingMode) || QueueStopped)
322
        {
323
                SDL_UnlockMutex(q->mutex);
324
                return -1;
325
                //SDL_CondWait(q->cond, q->mutex);
326
        }
327

    
328
        pkt1 = q->first_pkt;
329
        if (pkt1) {
330
                if(av==1) {
331

    
332

    
333
                        //check if audio packet has been alread decoded
334
                        if(pkt1->pkt.convergence_duration == 0) {
335
                                //not decoded yet, decode it
336
                                decode_enqueued_audio_packet(&(pkt1->pkt));
337
                        }
338

    
339

    
340
                        if(pkt1->pkt.size-AudioQueueOffset>dimAudioQ) {
341
#ifdef DEBUG_QUEUE
342
                                printf("  AV=1 and Extract from the same packet\n");
343
#endif
344
                                //av_init_packet(&tmp);
345
                                q->size -= dimAudioQ;
346
                                pkt->size = dimAudioQ;
347
                                //tmp.data = pkt1->pkt.data+AudioQueueOffset;
348
                                memcpy(pkt->data,pkt1->pkt.data+AudioQueueOffset,dimAudioQ);
349
                                pkt->dts = pkt1->pkt.dts;
350
                                pkt->pts = pkt1->pkt.pts;
351
                                pkt->stream_index = pkt1->pkt.stream_index;//1;
352
                                pkt->flags = 1;
353
                                pkt->pos = -1;
354
                                pkt->convergence_duration = -1;
355

    
356
                                //*pkt = tmp;
357
                                //pkt1->pkt.size -= dimAudioQ;
358
#ifdef DEBUG_QUEUE
359
                                printf("   Adjust timestamps Old = %lld New = %lld\n",pkt1->pkt.dts,(int64_t)(pkt1->pkt.dts + deltaAudioQ + deltaAudioQError));
360
#endif
361
                                int64_t Olddts=pkt1->pkt.dts;
362
                                pkt1->pkt.dts += deltaAudioQ + deltaAudioQError;
363
                                pkt1->pkt.pts += deltaAudioQ + deltaAudioQError;
364
                                deltaAudioQError=(float)Olddts + deltaAudioQ + deltaAudioQError - (float)pkt1->pkt.dts;
365
                                AudioQueueOffset += dimAudioQ;
366
#ifdef DEBUG_QUEUE
367
                                printf("   deltaAudioQError = %f\n",deltaAudioQError);
368
#endif
369
                                
370
                                ret = 1;
371
                                //compute lost frame statistics
372
                                if(q->last_frame_extracted > 0 && pkt->stream_index > q->last_frame_extracted) {
373
                                        q->total_lost_frames = q->total_lost_frames + pkt->stream_index - q->last_frame_extracted - 1;
374
#ifdef DEBUG_STATS
375
                                        printf("  AUDIO stats: stidx %d last %d tot %d\n", pkt->stream_index, q->last_frame_extracted, q->total_lost_frames);
376
#endif
377
                                }
378
                                //update index of last frame extracted
379
                                q->last_frame_extracted = pkt->stream_index;
380
#ifdef DEBUG_AUDIO_BUFFER
381
                                printf("1: idx %d    \taqo %d    \tstc %d    \taqe %f    \tpsz %d\n", pkt1->pkt.stream_index, AudioQueueOffset, SizeToCopy, deltaAudioQError, pkt1->pkt.size);
382
#endif
383
                        }
384
                        else {
385
#ifdef DEBUG_QUEUE
386
                                printf("  AV = 1 and Extract from 2 packets\n");
387
#endif
388
                                // Check for loss
389
                                if(pkt1->next) {
390

    
391
                                        //check if next audio packet has been already decoded
392
                                        if(pkt1->next->pkt.convergence_duration == 0) {
393
                                                //not decoded yet, decode it
394
                                                decode_enqueued_audio_packet(&(pkt1->next->pkt));
395
                                        }
396

    
397
#ifdef DEBUG_QUEUE
398
                                        printf("   we have a next...\n");
399
#endif
400
                                        //av_init_packet(&tmp);
401
                                        pkt->size = dimAudioQ;
402
                                        pkt->dts = pkt1->pkt.dts;
403
                                        pkt->pts = pkt1->pkt.pts;
404
                                        pkt->stream_index = pkt1->pkt.stream_index;//1;
405
                                        pkt->flags = 1;
406
                                        pkt->pos = -1;
407
                                        pkt->convergence_duration = -1;
408
                                        {
409
                                                SizeToCopy=pkt1->pkt.size-AudioQueueOffset;
410
#ifdef DEBUG_QUEUE
411
                                                printf("      SizeToCopy=%d\n",SizeToCopy);
412
#endif
413
                                                memcpy(pkt->data,pkt1->pkt.data+AudioQueueOffset,SizeToCopy);
414
                                                memcpy(pkt->data+SizeToCopy,pkt1->next->pkt.data,(dimAudioQ-SizeToCopy)*sizeof(uint8_t));
415
                                        }
416
#ifdef DEBUG_AUDIO_BUFFER
417
                                        printf("2: idx %d    \taqo %d    \tstc %d    \taqe %f    \tpsz %d\n", pkt1->pkt.stream_index, AudioQueueOffset, SizeToCopy, deltaAudioQError, pkt1->pkt.size);
418
#endif
419
                                }
420
#ifdef DEBUG_AUDIO_BUFFER
421
                                else {
422
                                        printf("2: NONEXT\n");
423
                                }
424
#endif
425
                                q->first_pkt = pkt1->next;
426
//                                if (!q->first_pkt)
427
//                                        q->last_pkt = NULL;
428
                                q->nb_packets--;
429
                                q->size -= SizeToCopy;
430
                                av_free_packet(&pkt1->pkt);//free(pkt1->pkt.data);
431
                                av_free(pkt1);
432

    
433
                                // Adjust timestamps
434
                                pkt1 = q->first_pkt;
435
                                if(pkt1)
436
                                {
437
                                        int Offset=(dimAudioQ-SizeToCopy)*1000/(spec.freq*2*spec.channels);
438
                                        int64_t LastDts=pkt1->pkt.dts;
439
                                        pkt1->pkt.dts += Offset + deltaAudioQError;
440
                                        pkt1->pkt.pts += Offset + deltaAudioQError;
441
                                        deltaAudioQError = (float)LastDts + (float)Offset + deltaAudioQError - (float)pkt1->pkt.dts;
442
#ifdef DEBUG_QUEUE
443
                                        printf("   Adjust timestamps Old = %lld New = %lld\n", LastDts, pkt1->pkt.dts);
444
#endif
445
                                        AudioQueueOffset = dimAudioQ - SizeToCopy;
446
                                        q->size -= AudioQueueOffset;
447
                                        ret = 1;
448
                                }
449
                                else
450
                                {
451
                                        AudioQueueOffset=0;
452
#ifdef DEBUG_AUDIO_BUFFER
453
                                        printf("0: idx %d    \taqo %d    \tstc %d    \taqe %f    \tpsz %d\n", pkt1->pkt.stream_index, AudioQueueOffset, SizeToCopy, deltaAudioQError, pkt1->pkt.size);
454
#endif
455
                                }
456
#ifdef DEBUG_QUEUE
457
                                printf("   deltaAudioQError = %f\n",deltaAudioQError);
458
#endif
459

    
460
                                //compute lost frame statistics
461
                                if(q->last_frame_extracted > 0 && pkt->stream_index > q->last_frame_extracted) {
462
                                        q->total_lost_frames = q->total_lost_frames + pkt->stream_index - q->last_frame_extracted - 1;
463
#ifdef DEBUG_STATS
464
                                        printf("  AUDIO stats: stidx %d last %d tot %d\n", pkt->stream_index, q->last_frame_extracted, q->total_lost_frames);
465
#endif
466
                                }
467
                                //update index of last frame extracted
468
                                q->last_frame_extracted = pkt->stream_index;
469
                        }
470
                }
471
                else {
472
#ifdef DEBUG_QUEUE
473
                        printf("  AV not 1\n");
474
#endif
475
                        q->first_pkt = pkt1->next;
476
//                        if (!q->first_pkt)
477
//                                q->last_pkt = NULL;
478
                        q->nb_packets--;
479
                        q->size -= pkt1->pkt.size;
480
                        
481
                        pkt->size = pkt1->pkt.size;
482
                        pkt->dts = pkt1->pkt.dts;
483
                        pkt->pts = pkt1->pkt.pts;
484
                        pkt->stream_index = pkt1->pkt.stream_index;
485
                        pkt->flags = pkt1->pkt.flags;
486
                        pkt->pos = pkt1->pkt.pos;
487
                        pkt->convergence_duration = pkt1->pkt.convergence_duration;
488
                        //*pkt = pkt1->pkt;
489
                        memcpy(pkt->data,pkt1->pkt.data,pkt1->pkt.size);
490
                        av_free_packet(&pkt1->pkt);//free(pkt1->pkt.data);
491
                        av_free(pkt1);
492
                        ret = 1;
493
                        //compute lost frame statistics
494
                        if(q->last_frame_extracted > 0 && pkt->stream_index > q->last_frame_extracted) {
495
#ifdef DEBUG_STATS
496
                                //printf("  VIDEO stats: stidx %d last %d tot %d\n", pkt->stream_index, q->last_frame_extracted, q->total_lost_frames);
497
#endif
498
                                q->total_lost_frames = q->total_lost_frames + pkt->stream_index - q->last_frame_extracted - 1;
499
                        }
500
                        //update index of last frame extracted
501
                        q->last_frame_extracted = pkt->stream_index;
502
                }
503
        }
504
#ifdef DEBUG_QUEUE
505
        else {
506
                printf("  pk1 NULL!!!!\n");
507
        }
508
#endif
509

    
510
        if(q->nb_packets==0 && q->queueType==AUDIO) {
511
                QueueFillingMode=1;
512
#ifdef DEBUG_QUEUE
513
                printf("QUEUE: Get FillingMode ON\n");
514
#endif
515
        }
516
#ifdef DEBUG_QUEUE
517
        printf("QUEUE: Get LastFrameExtracted = %d\n",q->last_frame_extracted);
518
        printf("QUEUE: Get Tot lost frames = %d\n",q->total_lost_frames);
519
#endif
520

    
521
        SDL_UnlockMutex(q->mutex);
522
        return ret;
523
}
524

    
525

    
526
int audio_decode_frame(uint8_t *audio_buf, int buf_size) {
527
        //struct timeval now;
528
        int audio_pkt_size = 0;
529
        long long Now;
530
        short int DecodeAudio=0, SkipAudio=0;
531
        //int len1, data_size;
532

    
533
        //gettimeofday(&now,NULL);
534
        //Now = (now.tv_sec)*1000+now.tv_usec/1000;
535
        Now=(long long)SDL_GetTicks();
536

    
537
        if(QueueFillingMode || QueueStopped)
538
        {
539
                //SDL_LockMutex(timing_mutex);
540
                FirstTimeAudio=1;
541
                FirstTime = 1;
542
                //SDL_UnlockMutex(timing_mutex);
543
                return -1;
544
        }
545

    
546
        if((FirstTime==1 || FirstTimeAudio==1) && audioq.size>0) {
547
                if(audioq.first_pkt->pkt.pts>0)
548
                {
549
                        //SDL_LockMutex(timing_mutex);
550
                        DeltaTime=Now-(long long)(audioq.first_pkt->pkt.pts);
551
                        FirstTimeAudio = 0;
552
                        FirstTime = 0;
553
                        //SDL_UnlockMutex(timing_mutex);
554
#ifdef DEBUG_AUDIO 
555
                         printf("AUDIO: audio_decode_frame - DeltaTimeAudio=%lld\n",DeltaTime);
556
#endif
557
                }
558
        }
559

    
560
#ifdef DEBUG_AUDIO 
561
        if(audioq.first_pkt)
562
        {
563
                printf("AUDIO: audio_decode_frame - Syncro params: Delta:%lld Now:%lld pts=%lld pts+Delta=%lld ",(long long)DeltaTime,Now,(long long)audioq.first_pkt->pkt.pts,(long long)audioq.first_pkt->pkt.pts+DeltaTime);
564
                printf("AUDIO: QueueLen=%d ",(int)audioq.nb_packets);
565
                printf("AUDIO: QueueSize=%d\n",(int)audioq.size);
566
        }
567
        else
568
                printf("AUDIO: audio_decode_frame - Empty queue\n");
569
#endif
570

    
571

    
572
        if(audioq.nb_packets>0) {
573
                if((long long)audioq.first_pkt->pkt.pts+DeltaTime<Now-(long long)MAX_TOLLERANCE) {
574
                        SkipAudio = 1;
575
                        DecodeAudio = 0;
576
                }
577
                else if((long long)audioq.first_pkt->pkt.pts+DeltaTime>=Now-(long long)MAX_TOLLERANCE &&
578
                        (long long)audioq.first_pkt->pkt.pts+DeltaTime<=Now+(long long)MAX_TOLLERANCE) {
579
                                SkipAudio = 0;
580
                                DecodeAudio = 1;
581
                }
582
        }
583
                
584
        while(SkipAudio==1 && audioq.size>0) {
585
                SkipAudio = 0;
586
#ifdef DEBUG_AUDIO
587
                 printf("AUDIO: skipaudio: queue size=%d\n",audioq.size);
588
#endif
589
                if(packet_queue_get(&audioq,&AudioPkt,1) < 0) {
590
                        return -1;
591
                }
592
                if(audioq.first_pkt)
593
                {
594
                        if((long long)audioq.first_pkt->pkt.pts+DeltaTime<Now-(long long)MAX_TOLLERANCE) {
595
                                SkipAudio = 1;
596
                                DecodeAudio = 0;
597
                        }
598
                        else if((long long)audioq.first_pkt->pkt.pts+DeltaTime>=Now-(long long)MAX_TOLLERANCE &&
599
                                (long long)audioq.first_pkt->pkt.pts+DeltaTime<=Now+(long long)MAX_TOLLERANCE) {
600
                                        SkipAudio = 0;
601
                                        DecodeAudio = 1;
602
                        }
603
                }
604
        }
605
        if(DecodeAudio==1) {
606
                if(packet_queue_get(&audioq,&AudioPkt,1) < 0) {
607
                        return -1;
608
                }
609
                memcpy(audio_buf,AudioPkt.data,AudioPkt.size);
610
                audio_pkt_size = AudioPkt.size;
611
#ifdef DEBUG_AUDIO
612
                 printf("AUDIO: Decode audio\n");
613
#endif
614
        }
615

    
616
        return audio_pkt_size;
617
}
618

    
619

    
620
int video_callback(void *valthread) {
621
        //AVPacket pktvideo;
622
        AVCodecContext  *pCodecCtx;
623
        AVCodec         *pCodec;
624
        AVFrame         *pFrame;
625
        AVPacket        packet;
626
        int frameFinished;
627
        int countexit;
628
        AVPicture pict;
629
        //FILE *frecon;
630
        SDL_Event event;
631
        long long Now;
632
        short int SkipVideo, DecodeVideo;
633

    
634
        //double frame_rate = 0.0,time_between_frames=0.0;
635
        //struct timeval now;
636

    
637
        //int wait_for_sync = 1;
638
        ThreadVal *tval;
639
        tval = (ThreadVal *)valthread;
640

    
641
        //frame_rate = tval->framerate;
642
        //time_between_frames = 1.e6 / frame_rate;
643
        //gettimeofday(&time_now,0);
644

    
645
        //frecon = fopen("recondechunk.mpg","wb");
646

    
647
        pCodecCtx=avcodec_alloc_context();
648
        pCodecCtx->codec_type = CODEC_TYPE_VIDEO;
649
#ifdef H264_VIDEO_ENCODER
650
        pCodecCtx->codec_id  = CODEC_ID_H264;
651
        pCodecCtx->me_range = 16;
652
        pCodecCtx->max_qdiff = 4;
653
        pCodecCtx->qmin = 10;
654
        pCodecCtx->qmax = 51;
655
        pCodecCtx->qcompress = 0.6;
656
#else
657
        pCodecCtx->codec_id  = CODEC_ID_MPEG4;
658
#endif
659
        //pCodecCtx->bit_rate = 400000;
660
        // resolution must be a multiple of two
661
        pCodecCtx->width = tval->width;//176;//352;
662
        pCodecCtx->height = tval->height;//144;//288;
663
        // frames per second
664
        //pCodecCtx->time_base = (AVRational){1,25};
665
        //pCodecCtx->gop_size = 10; // emit one intra frame every ten frames
666
        //pCodecCtx->max_b_frames=1;
667
        pCodecCtx->pix_fmt = PIX_FMT_YUV420P;
668
        pCodec=avcodec_find_decoder(pCodecCtx->codec_id);
669

    
670
        if(pCodec==NULL) {
671
                fprintf(stderr, "Unsupported codec!\n");
672
                return -1; // Codec not found
673
        }
674
        if(avcodec_open(pCodecCtx, pCodec) < 0) {
675
                fprintf(stderr, "could not open codec\n");
676
                return -1; // Could not open codec
677
        }
678
        pFrame=avcodec_alloc_frame();
679
        if(pFrame==NULL) {
680
                printf("Memory error!!!\n");
681
                return -1;
682
        }
683

    
684
        while(!quit) {
685
                if(QueueFillingMode || QueueStopped)
686
                {
687
                        //SDL_LockMutex(timing_mutex);
688
                        FirstTime = 1;
689
                        //SDL_UnlockMutex(timing_mutex);
690
                        usleep(5000);
691
                        continue;
692
                }
693

    
694
                DecodeVideo = 0;
695
                SkipVideo = 0;
696
                //gettimeofday(&now,NULL);
697
                //Now = (unsigned long long)now.tv_sec*1000+(unsigned long long)now.tv_usec/1000;
698
                Now=(long long)SDL_GetTicks();
699
                if(FirstTime==1 && videoq.size>0) {
700
                        if(videoq.first_pkt->pkt.pts>0)
701
                        {
702
                                //SDL_LockMutex(timing_mutex);
703
                                DeltaTime=Now-(long long)videoq.first_pkt->pkt.pts;
704
                                FirstTime = 0;
705
                                //SDL_UnlockMutex(timing_mutex);
706
                        }
707
#ifdef DEBUG_VIDEO 
708
                         printf("VIDEO: VideoCallback - DeltaTimeAudio=%lld\n",DeltaTime);
709
#endif
710
                }
711

    
712
#ifdef DEBUG_VIDEO 
713
                if(videoq.first_pkt)
714
                {
715
                        printf("VIDEO: VideoCallback - Syncro params: Delta:%lld Now:%lld pts=%lld pts+Delta=%lld ",(long long)DeltaTime,Now,(long long)videoq.first_pkt->pkt.pts,(long long)videoq.first_pkt->pkt.pts+DeltaTime);
716
                        printf("VIDEO: Index=%d ",(int)videoq.first_pkt->pkt.stream_index);
717
                        printf("VIDEO: QueueLen=%d ",(int)videoq.nb_packets);
718
                        printf("VIDEO: QueueSize=%d\n",(int)videoq.size);
719
                }
720
                else
721
                        printf("VIDEO: VideoCallback - Empty queue\n");
722
#endif
723

    
724
                if(videoq.nb_packets>0) {
725
                        if(((long long)videoq.first_pkt->pkt.pts+DeltaTime)<Now-(long long)MAX_TOLLERANCE) {
726
                                SkipVideo = 1;
727
                                DecodeVideo = 0;
728
                        }
729
                        else 
730
                                if(((long long)videoq.first_pkt->pkt.pts+DeltaTime)>=Now-(long long)MAX_TOLLERANCE &&
731
                                   ((long long)videoq.first_pkt->pkt.pts+DeltaTime)<=Now+(long long)MAX_TOLLERANCE) {
732
                                        SkipVideo = 0;
733
                                        DecodeVideo = 1;
734
                                }
735
                }
736
#ifdef DEBUG_VIDEO
737
                printf("VIDEO: skipvideo:%d decodevideo:%d\n",SkipVideo,DecodeVideo);
738
#endif
739

    
740
                while(SkipVideo==1 && videoq.size>0) {
741
                        SkipVideo = 0;
742
#ifdef DEBUG_VIDEO 
743
                         printf("VIDEO: Skip Video\n");
744
#endif
745
                        if(packet_queue_get(&videoq,&VideoPkt,0) < 0) {
746
                                break;
747
                        }
748
                        avcodec_decode_video2(pCodecCtx, pFrame, &frameFinished, &VideoPkt);
749
                        if(videoq.first_pkt)
750
                        {
751
                                if((long long)videoq.first_pkt->pkt.pts+DeltaTime<Now-(long long)MAX_TOLLERANCE) {
752
                                        SkipVideo = 1;
753
                                        DecodeVideo = 0;
754
                                }
755
                                else if((long long)videoq.first_pkt->pkt.pts+DeltaTime>=Now-(long long)MAX_TOLLERANCE &&
756
                                                                (long long)videoq.first_pkt->pkt.pts+DeltaTime<=Now+(long long)MAX_TOLLERANCE) {
757
                                        SkipVideo = 0;
758
                                        DecodeVideo = 1;
759
                                }
760
                        }
761
                }
762
                
763
                if(DecodeVideo==1) {
764
                        if(packet_queue_get(&videoq,&VideoPkt,0) > 0) {
765

    
766
#ifdef DEBUG_VIDEO
767
                                printf("VIDEO: Decode video FrameTime=%lld Now=%lld\n",(long long)VideoPkt.pts+DeltaTime,Now);
768
#endif
769

    
770
                                avcodec_decode_video2(pCodecCtx, pFrame, &frameFinished, &VideoPkt);
771

    
772
                                if(frameFinished) { // it must be true all the time else error
773
#ifdef DEBUG_VIDEO
774
                                        printf("VIDEO: FrameFinished\n");
775
#endif
776
                                        //SaveFrame(pFrame, pCodecCtx->width, pCodecCtx->height, cont++);
777
                                        //fwrite(pktvideo.data, 1, pktvideo.size, frecon);
778

    
779
                                        // Lock SDL_yuv_overlay
780
                                        if(SDL_MUSTLOCK(screen)) {
781

    
782
                                                if(SDL_LockSurface(screen) < 0) {
783
                                                        continue;
784
                                                }
785
                                        }
786

    
787
                                        if(SDL_LockYUVOverlay(yuv_overlay) < 0) {
788
                                                if(SDL_MUSTLOCK(screen)) {
789
                                                        SDL_UnlockSurface(screen);
790
                                                }
791
                                                continue;
792
                                        }
793
                                        pict.data[0] = yuv_overlay->pixels[0];
794
                                        pict.data[1] = yuv_overlay->pixels[2];
795
                                        pict.data[2] = yuv_overlay->pixels[1];
796

    
797
                                        pict.linesize[0] = yuv_overlay->pitches[0];
798
                                        pict.linesize[1] = yuv_overlay->pitches[2];
799
                                        pict.linesize[2] = yuv_overlay->pitches[1];
800
                                        if(img_convert_ctx == NULL) {
801
                                                img_convert_ctx = sws_getContext(tval->width, tval->height, PIX_FMT_YUV420P, tval->width, tval->height, PIX_FMT_YUV420P, SWS_BICUBIC, NULL, NULL, NULL);
802
                                                if(img_convert_ctx == NULL) {
803
                                                        fprintf(stderr, "Cannot initialize the conversion context!\n");
804
                                exit(1);
805
                                                }
806
                                        }
807
                                        // let's draw the data (*yuv[3]) on a SDL screen (*screen)
808
                                        sws_scale(img_convert_ctx, pFrame->data, pFrame->linesize, 0, tval->height, pict.data, pict.linesize);
809
                                        SDL_UnlockYUVOverlay(yuv_overlay);
810
                                        // Show, baby, show!
811
                                        SDL_DisplayYUVOverlay(yuv_overlay, &rect);
812
                                        if(SDL_MUSTLOCK(screen)) {
813
                                                SDL_UnlockSurface(screen);
814
                                        }
815
                                } //if FrameFinished
816
                        } // if packet_queue_get
817
                } //if DecodeVideo=1
818

    
819
                usleep(5000);
820
        }
821
        av_free(pCodecCtx);
822
        //fclose(frecon);
823
#ifdef DEBUG_VIDEO
824
         printf("VIDEO: video callback end\n");
825
#endif
826
        return 1;
827
}
828

    
829

    
830
void audio_callback(void *userdata, Uint8 *stream, int len) {
831

    
832
        //AVCodecContext *aCodecCtx = (AVCodecContext *)userdata;
833
        int audio_size;
834

    
835
        static uint8_t audio_buf[AVCODEC_MAX_AUDIO_FRAME_SIZE];
836

    
837
        audio_size = audio_decode_frame(audio_buf, sizeof(audio_buf));
838
        if(audio_size != len) {
839
                memset(stream, 0, len);
840
        } else {
841
                memcpy(stream, (uint8_t *)audio_buf, len);
842
        }
843
}
844

    
845
void ShowBMP(char *file, SDL_Surface *screen, int x, int y) {
846
        SDL_Surface *image;
847
        SDL_Rect dest;
848

    
849
        /* Load a BMP file on a surface */
850
        image = SDL_LoadBMP(file);
851
        if ( image == NULL ) {
852
                fprintf(stderr, "Error loading %s: %s\n", file, SDL_GetError());
853
                return;
854
        }
855

    
856
        /* Copy on the screen surface 
857
        surface should be blocked now.
858
        */
859
        dest.x = x;
860
        dest.y = y;
861
        dest.w = image->w;
862
        dest.h = image->h;
863
        SDL_BlitSurface(image, NULL, screen, &dest);
864

    
865
        /* Update the screen area just changed */
866
        SDL_UpdateRects(screen, 1, &dest);
867
}
868

    
869
void SaveFrame(AVFrame *pFrame, int width, int height, int iFrame) {
870
        FILE *pFile;
871
        char szFilename[32];
872
        int  y;
873
  
874
         // Open file
875
        sprintf(szFilename, "frame%d.ppm", iFrame);
876
        pFile=fopen(szFilename, "wb");
877
        if(pFile==NULL)
878
                return;
879
  
880
        // Write header
881
        fprintf(pFile, "P5\n%d %d\n255\n", width, height);
882
  
883
        // Write pixel data
884
        for(y=0; y<height; y++)
885
                  fwrite(pFrame->data[0]+y*pFrame->linesize[0], 1, width, pFile);
886
  
887
        // Close file
888
        fclose(pFile);
889
}
890

    
891
void sigint_handler (int signal) {
892
        printf("Caught SIGINT, exiting...");
893
        got_sigint = 1;
894
}
895

    
896
void ProcessKeys() {
897
        static Uint32 LastTime=0;
898
        static int LastKey=-1;
899

    
900
        Uint32 Now=SDL_GetTicks();
901
        Uint8* keystate=SDL_GetKeyState(NULL);
902
        if(keystate[SDLK_SPACE] &&
903
          (LastKey!=SDLK_SPACE || (LastKey==SDLK_SPACE && (Now-LastTime>1000))))
904
        {
905
                LastKey=SDLK_SPACE;
906
                LastTime=Now;
907
                QueueStopped=!QueueStopped;
908
        }
909
        if(keystate[SDLK_ESCAPE] &&
910
          (LastKey!=SDLK_ESCAPE || (LastKey==SDLK_ESCAPE && (Now-LastTime>1000))))
911
        {
912
                LastKey=SDLK_ESCAPE;
913
                LastTime=Now;
914
                quit=1;
915
        }
916
        /*if(keystate[SDLK_f] &&
917
          (LastKey!=SDLK_f || (LastKey==SDLK_f && (Now-LastTime>1000))))
918
        {
919
                LastKey=SDLK_f;
920
                LastTime=Now;
921
                SDL_WM_ToggleFullScreen(NULL);
922
        }*/
923
}
924

    
925
int main(int argc, char *argv[]) {
926
        int i, j, videoStream,outbuf_size,out_size,out_size_audio,seq_current_chunk = 0,audioStream;
927
        int len1, data_size, stime,cont=0;
928
        int frameFinished, len_audio;
929
        int numBytes,outbuf_audio_size,audio_size;
930

    
931
        int y;
932
        
933
        uint8_t *outbuf,*outbuf_audio;
934
        uint8_t *outbuf_audi_audio;
935
        
936
        AVFormatContext *pFormatCtx;
937

    
938
        AVCodec         *pCodec,*aCodec;
939
        AVFrame         *pFrame; 
940

    
941
        AVPicture pict;
942
        SDL_Thread *video_thread;//exit_thread,*exit_thread2;
943
        SDL_Event event;
944
        SDL_AudioSpec wanted_spec;
945
        
946
        struct MHD_Daemon *daemon = NULL;        
947

    
948
        char buf[1024],outfile[1024], basereadfile[1024],readfile[1024];
949
        FILE *fp;        
950
        int width,height,asample_rate,achannels;
951

    
952
        ThreadVal *tval;
953
        tval = (ThreadVal *)malloc(sizeof(ThreadVal));
954
                
955
        if(argc<5) {
956
                printf("player width height sample_rate channels\n");
957
                exit(1);
958
        }
959
        sscanf(argv[1],"%d",&width);
960
        sscanf(argv[2],"%d",&height);
961
        sscanf(argv[3],"%d",&asample_rate);
962
        sscanf(argv[4],"%d",&achannels);
963
        tval->width = width;
964
        tval->height = height;
965
        
966
        // Register all formats and codecs
967

    
968
        av_register_all();
969
        if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER)) {
970
                fprintf(stderr, "Could not initialize SDL - %s\n", SDL_GetError());
971
                return -1;
972
        }
973

    
974
        aCodecCtx = avcodec_alloc_context();
975
        //aCodecCtx->bit_rate = 64000;
976
        aCodecCtx->sample_rate = asample_rate;
977
        aCodecCtx->channels = achannels;
978
#ifdef MP3_AUDIO_ENCODER
979
        aCodec = avcodec_find_decoder(CODEC_ID_MP3); // codec audio
980
#else
981
        aCodec = avcodec_find_decoder(CODEC_ID_MP2);
982
#endif
983
        printf("MP2 codec id %d MP3 codec id %d\n",CODEC_ID_MP2,CODEC_ID_MP3);
984
        if(!aCodec) {
985
                printf("Codec not found!\n");
986
                return -1;
987
        }
988
        if(avcodec_open(aCodecCtx, aCodec)<0) {
989
                fprintf(stderr, "could not open codec\n");
990
                return -1; // Could not open codec
991
        }
992
        printf("using audio Codecid: %d ",aCodecCtx->codec_id);
993
        printf("samplerate: %d ",aCodecCtx->sample_rate);
994
        printf("channels: %d\n",aCodecCtx->channels);
995
        wanted_spec.freq = aCodecCtx->sample_rate;
996
        wanted_spec.format = AUDIO_S16SYS;
997
        wanted_spec.channels = aCodecCtx->channels;
998
        wanted_spec.silence = 0;
999
        wanted_spec.samples = SDL_AUDIO_BUFFER_SIZE;
1000
        wanted_spec.callback = audio_callback;
1001
        wanted_spec.userdata = aCodecCtx;
1002
        if(SDL_OpenAudio(&wanted_spec,&spec)<0) {
1003
                fprintf(stderr,"SDL_OpenAudio: %s\n",SDL_GetError());
1004
                return -1;
1005
        }
1006
        dimAudioQ = spec.size;
1007
        deltaAudioQ = (float)((float)spec.samples)*1000/spec.freq;
1008

    
1009
#ifdef DEBUG_AUDIO
1010
        printf("freq:%d\n",spec.freq);
1011
        printf("format:%d\n",spec.format);
1012
        printf("channels:%d\n",spec.channels);
1013
        printf("silence:%d\n",spec.silence);
1014
        printf("samples:%d\n",spec.samples);
1015
        printf("size:%d\n",spec.size);
1016
        printf("deltaAudioQ: %f\n",deltaAudioQ);
1017
#endif
1018

    
1019
        pFrame=avcodec_alloc_frame();
1020
        if(pFrame==NULL) {
1021
                printf("Memory error!!!\n");
1022
                return -1;
1023
        }
1024
        outbuf_audio = malloc(AVCODEC_MAX_AUDIO_FRAME_SIZE);
1025

    
1026
        packet_queue_init(&audioq, AUDIO);
1027
        packet_queue_init(&videoq, VIDEO);
1028
        SDL_WM_SetCaption("Filling buffer...", NULL);
1029
        // Make a screen to put our video
1030
#ifndef __DARWIN__
1031
        screen = SDL_SetVideoMode(width, height, 0, 0);
1032
#else
1033
        screen = SDL_SetVideoMode(width, height, 24, 0);
1034
#endif
1035
        if(!screen) {
1036
                fprintf(stderr, "SDL: could not set video mode - exiting\n");
1037
                exit(1);
1038
        }
1039
        yuv_overlay = SDL_CreateYUVOverlay(width, height,SDL_YV12_OVERLAY, screen);
1040

    
1041
        if ( yuv_overlay == NULL ) {
1042
                fprintf(stderr,"SDL: Couldn't create SDL_yuv_overlay: %s", SDL_GetError());
1043
                exit(1);
1044
        }
1045

    
1046
        if ( yuv_overlay->hw_overlay )
1047
                fprintf(stderr,"SDL: Using hardware overlay.");
1048

    
1049
        rect.x = 0;
1050
        rect.y = 0;
1051
        rect.w = width;
1052
        rect.h = height;
1053

    
1054
        SDL_DisplayYUVOverlay(yuv_overlay, &rect);
1055

    
1056
        //signal (SIGINT, sigint_handler);
1057

    
1058
        // Init audio and video buffers
1059
        av_init_packet(&AudioPkt);
1060
        av_init_packet(&VideoPkt);
1061
        AudioPkt.data=(uint8_t *)malloc(AVCODEC_MAX_AUDIO_FRAME_SIZE);
1062
        if(!AudioPkt.data) return 0;
1063
        VideoPkt.data=(uint8_t *)malloc(width*height*3/2);
1064
        if(!VideoPkt.data) return 0;
1065
        
1066
        SDL_PauseAudio(0);
1067
        video_thread = SDL_CreateThread(video_callback,tval);
1068
        //timing_mutex = SDL_CreateMutex();
1069

    
1070
        //SDL_WaitThread(exit_thread2,NULL);
1071

    
1072
        daemon = initChunkPuller();
1073

    
1074
        // Wait for user input
1075
        while(!quit) {
1076
                if(QueueFillingMode) {
1077
                        SDL_WM_SetCaption("Filling buffer...", NULL);
1078

    
1079
                        if(audioq.nb_packets==0 && audioq.last_frame_extracted>0) {        // video ended therefore init queues
1080
#ifdef DEBUG_QUEUE
1081
                                printf("QUEUE: MAIN SHOULD RESET\n");
1082
#endif
1083
                                packet_queue_reset(&audioq, AUDIO);
1084
                                packet_queue_reset(&videoq, VIDEO);
1085
                        }
1086

    
1087
#ifdef DEBUG_QUEUE
1088
                        //printf("QUEUE: MAIN audio:%d video:%d audiolastframe:%d videolastframe:%d\n", audioq.nb_packets, videoq.nb_packets, audioq.last_frame_extracted, videoq.last_frame_extracted);
1089
#endif
1090
                }
1091
                else
1092
                        SDL_WM_SetCaption("NAPA-Wine Player", NULL);
1093

    
1094
                //listen for key and mouse
1095
                while(SDL_PollEvent(&event)) {
1096
                                switch(event.type) {
1097
                                        case SDL_QUIT:
1098
                                                //exit(0);
1099
                                                quit=1;
1100
                                        break;
1101
                                }
1102
                                ProcessKeys();
1103
                }
1104
                usleep(120000);
1105
        }
1106

    
1107
        //TERMINATE
1108
        // Stop audio&video playback
1109
        SDL_WaitThread(video_thread,NULL);
1110
        SDL_PauseAudio(1);
1111
        SDL_CloseAudio();
1112
        //SDL_DestroyMutex(timing_mutex);
1113
        SDL_Quit();
1114
        av_free(aCodecCtx);
1115
        free(AudioPkt.data);
1116
        free(VideoPkt.data);
1117
        free(outbuf_audio);
1118
        finalizeChunkPuller(daemon);
1119
        return 0;
1120
}
1121

    
1122

    
1123

    
1124
int enqueueBlock(const uint8_t *block, const int block_size) {
1125
        Chunk *gchunk=NULL;
1126
        ExternalChunk *echunk=NULL;
1127
        uint8_t *tempdata, *buffer;
1128
        int i, j;
1129
        Frame *frame=NULL;
1130
        AVPacket packet, packetaudio;
1131

    
1132
        //uint8_t *video_bufQ = NULL;
1133
        uint16_t *audio_bufQ = NULL;
1134
        //uint8_t audio_bufQ[(AVCODEC_MAX_AUDIO_FRAME_SIZE * 3) / 2];
1135
        int16_t *dataQ = NULL;
1136
        int data_sizeQ;
1137
        int lenQ;
1138
        int sizeFrame = 0;
1139
        sizeFrame = 3*sizeof(int32_t)+sizeof(struct timeval);
1140
#ifdef DEBUG_ENQUEUE
1141
printf("O\n");
1142
#endif
1143
        audio_bufQ = (uint16_t *)av_malloc(AVCODEC_MAX_AUDIO_FRAME_SIZE);
1144
#ifdef DEBUG_ENQUEUE
1145
printf("O\n");
1146
#endif
1147
        gchunk = (Chunk *)malloc(sizeof(Chunk));
1148
#ifdef DEBUG_ENQUEUE
1149
printf("O\n");
1150
#endif
1151
        if(!gchunk) {
1152
                printf("Memory error in gchunk!\n");
1153
                return PLAYER_FAIL_RETURN;
1154
        }
1155
#ifdef DEBUG_ENQUEUE
1156
printf("O\n");
1157
#endif
1158

    
1159
        decodeChunk(gchunk, block, block_size);
1160
#ifdef DEBUG_ENQUEUE
1161
printf("O\n");
1162
#endif
1163

    
1164
        echunk = grapesChunkToExternalChunk(gchunk);
1165
#ifdef DEBUG_ENQUEUE
1166
printf("O\n");
1167
#endif
1168
        if(echunk == NULL) {
1169
                printf("Memory error in echunk!\n");
1170
                free(gchunk->attributes);
1171
                free(gchunk->data);
1172
                free(gchunk);
1173
                return PLAYER_FAIL_RETURN;
1174
        }
1175
#ifdef DEBUG_ENQUEUE
1176
printf("O\n");
1177
#endif
1178
        free(gchunk->attributes);
1179
#ifdef DEBUG_ENQUEUE
1180
printf("O\n");
1181
#endif
1182
        free(gchunk);
1183
#ifdef DEBUG_ENQUEUE
1184
printf("O\n");
1185
#endif
1186

    
1187
        frame = (Frame *)malloc(sizeof(Frame));
1188
#ifdef DEBUG_ENQUEUE
1189
printf("O\n");
1190
#endif
1191
        if(!frame) {
1192
                printf("Memory error!\n");
1193
                return -1;
1194
        }
1195
#ifdef DEBUG_ENQUEUE
1196
printf("O\n");
1197
#endif
1198

    
1199
        tempdata = echunk->data;
1200
        j=echunk->payload_len;
1201
        while(j>0 && !quit) {
1202
                //usleep(30000);
1203
                frame->number = *((int32_t *)tempdata);
1204
                tempdata+=sizeof(int32_t);
1205
                frame->timestamp = *((struct timeval *)tempdata);
1206
                tempdata += sizeof(struct timeval);
1207
                frame->size = *((int32_t *)tempdata);
1208
                tempdata+=sizeof(int32_t);
1209
                frame->type = *((int32_t *)tempdata);
1210
                tempdata+=sizeof(int32_t);
1211

    
1212
                buffer = tempdata; // here coded frame information
1213
                tempdata+=frame->size;
1214
                //printf("%d %d %d %d\n",frame->number,frame->timestamp.tv_usec,frame->size,frame->type);
1215

    
1216
                if(frame->type!=5) { // video frame
1217
#ifdef DEBUG_ENQUEUE
1218
printf("VVVVVO\n");
1219
#endif
1220
                        av_init_packet(&packet);
1221
#ifdef DEBUG_ENQUEUE
1222
printf("O\n");
1223
#endif
1224

    
1225
                        //video_bufQ = (uint8_t *)malloc(frame->size); //this gets freed at the time of packet_queue_get
1226
                        //if(video_bufQ) {
1227
                                //memcpy(video_bufQ, buffer, frame->size);
1228
                                packet.data = buffer;//video_bufQ;
1229
                                packet.size = frame->size;
1230
                                packet.pts = frame->timestamp.tv_sec*(unsigned long long)1000+frame->timestamp.tv_usec;
1231
                                packet.dts = frame->timestamp.tv_sec*(unsigned long long)1000+frame->timestamp.tv_usec;
1232
                                packet.stream_index = frame->number; // use of stream_index for number frame
1233
                                //packet.duration = frame->timestamp.tv_sec;
1234
#ifdef DEBUG_ENQUEUE
1235
printf("BBBBBBBO\n");
1236
#endif
1237
                                packet_queue_put(&videoq,&packet);//makes a copy of the packet
1238
#ifdef DEBUG_ENQUEUE
1239
printf("AAAAAAAAAAO\n");
1240
#endif
1241
#ifdef DEBUG_SOURCE
1242
                                printf("SOURCE: Insert video in queue pts=%lld %d %d sindex:%d\n",packet.pts,(int)frame->timestamp.tv_sec,(int)frame->timestamp.tv_usec,packet.stream_index);
1243
#endif
1244
                        //}
1245
                }
1246
                else { // audio frame
1247
#ifdef DEBUG_ENQUEUE
1248
printf("AUDIOO\n");
1249
#endif
1250

    
1251
                        av_init_packet(&packetaudio);
1252
#ifdef DEBUG_ENQUEUE
1253
printf("O\n");
1254
#endif
1255

    
1256
                        packetaudio.data = buffer;
1257
                        packetaudio.size = frame->size;
1258

    
1259
                        packetaudio.pts = frame->timestamp.tv_sec*(unsigned long long)1000+frame->timestamp.tv_usec;
1260
                        packetaudio.dts = frame->timestamp.tv_sec*(unsigned long long)1000+frame->timestamp.tv_usec;
1261
                        //packetaudio.duration = frame->timestamp.tv_sec;
1262
                        packetaudio.stream_index = frame->number; // use of stream_index for number frame
1263
                        packetaudio.flags = 1;
1264
                        packetaudio.pos = -1;
1265

    
1266
                        //instead of -1, in order to signal it is not decoded yet
1267
                        packetaudio.convergence_duration = 0;
1268

    
1269
                        // insert the audio frame into the queue
1270
/*
1271
                        data_sizeQ = AVCODEC_MAX_AUDIO_FRAME_SIZE;
1272

1273
                        lenQ = avcodec_decode_audio3(aCodecCtx, (int16_t *)audio_bufQ, &data_sizeQ, &packetaudio);
1274

1275
                        if(lenQ>0)
1276
                        {
1277

1278
                                // for freeing there is some memory still in tempdata to be freed
1279
                                dataQ = (int16_t *)av_malloc(data_sizeQ);
1280
                                if(dataQ)
1281
                                {
1282
                                        memcpy(dataQ,audio_bufQ,data_sizeQ);
1283
                                        packetaudio.data = (int8_t *)dataQ;
1284
                                        packetaudio.size = data_sizeQ;
1285
                                        packetaudio.stream_index = frame->number; // use of stream_index for number frame
1286
*/
1287
#ifdef DEBUG_ENQUEUE
1288
printf("BEFORAO\n");
1289
#endif
1290
                                        packet_queue_put(&audioq, &packetaudio);//makes a copy of the packet so i can free here
1291
#ifdef DEBUG_ENQUEUE
1292
printf("AFTAO\n");
1293
#endif
1294
/*
1295
                                        av_free(dataQ);
1296
*/
1297
#ifdef DEBUG_SOURCE
1298
                                        printf("SOURCE: Insert audio in queue pts=%lld sindex:%d\n", packetaudio.pts, packetaudio.stream_index);
1299
#endif
1300
/*
1301
                                }
1302
                        }
1303
*/
1304
                }
1305
                j = j - sizeFrame - frame->size;
1306
        }
1307
        if(echunk->data)
1308
                free(echunk->data);
1309
        if(echunk)
1310
                free(echunk);
1311
        if(frame)
1312
                free(frame);
1313
        if(audio_bufQ)
1314
                av_free(audio_bufQ);
1315
}