Revision e810bf7b
chunker_player.c | ||
---|---|---|
28 | 28 |
#undef main /* Prevents SDL from overriding main() */ |
29 | 29 |
#endif |
30 | 30 |
|
31 |
#include <platform.h> |
|
32 |
#include <microhttpd.h> |
|
33 |
//#include <AntTweakBar.h> |
|
34 |
|
|
31 | 35 |
#include "chunker_player.h" |
36 |
#include "codec_definitions.h" |
|
32 | 37 |
|
33 | 38 |
#define SDL_AUDIO_BUFFER_SIZE 1024 |
34 | 39 |
|
... | ... | |
36 | 41 |
#define AUDIO 1 |
37 | 42 |
#define VIDEO 2 |
38 | 43 |
|
39 |
//#define DEBUG_AUDIO
|
|
40 |
//#define DEBUG_VIDEO
|
|
41 |
//#define DEBUG_QUEUE
|
|
42 |
//#define DEBUG_SOURCE
|
|
44 |
#define DEBUG_AUDIO |
|
45 |
#define DEBUG_VIDEO |
|
46 |
#define DEBUG_QUEUE |
|
47 |
#define DEBUG_SOURCE |
|
43 | 48 |
|
44 | 49 |
short int QueueFillingMode=1; |
45 | 50 |
short int QueueStopped=0; |
... | ... | |
52 | 57 |
SDL_cond *cond; |
53 | 58 |
short int queueType; |
54 | 59 |
int last_frame_extracted; |
60 |
int total_lost_frames; |
|
55 | 61 |
} PacketQueue; |
56 | 62 |
|
57 | 63 |
typedef struct threadVal { |
... | ... | |
85 | 91 |
q->cond = SDL_CreateCond(); |
86 | 92 |
QueueFillingMode=1; |
87 | 93 |
q->queueType=Type; |
88 |
q->last_frame_extracted = -1; |
|
94 |
q->last_frame_extracted = 0; |
|
95 |
q->total_lost_frames = 0; |
|
89 | 96 |
} |
90 | 97 |
|
91 | 98 |
int packet_queue_put(PacketQueue *q, AVPacket *pkt) { |
... | ... | |
138 | 145 |
if(q->nb_packets>=QUEUE_FILLING_THRESHOLD && QueueFillingMode) // && q->queueType==AUDIO) |
139 | 146 |
{ |
140 | 147 |
QueueFillingMode=0; |
148 |
#ifdef DEBUG_QUEUE |
|
149 |
printf("PUT in Queue: FillingMode set to zero\n"); |
|
150 |
#endif |
|
141 | 151 |
//SDL_CondSignal(q->cond); |
142 | 152 |
} |
143 |
q->last_frame_extracted = pkt->stream_index; |
|
153 |
//WHYYYYYYYYY q->last_frame_extracted = pkt->stream_index; |
|
154 |
//#ifdef DEBUG_QUEUE |
|
155 |
// printf("PUT LastFrameExtracted set to %d\n",q->last_frame_extracted); |
|
156 |
//#endif |
|
144 | 157 |
} |
145 | 158 |
} |
146 | 159 |
|
... | ... | |
170 | 183 |
if(av==1) { |
171 | 184 |
if(pkt1->pkt.size-AudioQueueOffset>dimAudioQ) { |
172 | 185 |
#ifdef DEBUG_QUEUE |
173 |
printf("Extract from the same packet ");
|
|
186 |
printf(" AV=1 and Extract from the same packet\n");
|
|
174 | 187 |
#endif |
175 | 188 |
//av_init_packet(&tmp); |
176 | 189 |
q->size -= dimAudioQ; |
... | ... | |
179 | 192 |
memcpy(pkt->data,pkt1->pkt.data+AudioQueueOffset,dimAudioQ); |
180 | 193 |
pkt->dts = pkt1->pkt.dts; |
181 | 194 |
pkt->pts = pkt1->pkt.pts; |
182 |
pkt->stream_index = 1; |
|
195 |
pkt->stream_index = pkt1->pkt.stream_index;//1;
|
|
183 | 196 |
pkt->flags = 1; |
184 | 197 |
pkt->pos = -1; |
185 | 198 |
pkt->convergence_duration = -1; |
... | ... | |
190 | 203 |
pkt1->pkt.pts += deltaAudioQ; |
191 | 204 |
AudioQueueOffset += dimAudioQ; |
192 | 205 |
#ifdef DEBUG_QUEUE |
193 |
printf("AudioQueueOffset = %d\n",AudioQueueOffset); |
|
206 |
printf(" AudioQueueOffset = %d\n",AudioQueueOffset);
|
|
194 | 207 |
#endif |
195 | 208 |
|
196 | 209 |
ret = 1; |
210 |
//compute lost frame statistics |
|
211 |
if(q->last_frame_extracted > 0 && pkt->stream_index > q->last_frame_extracted) { |
|
212 |
#ifdef DEBUG_QUEUE |
|
213 |
printf(" stats: stidx %d last %d tot %d\n", pkt->stream_index, q->last_frame_extracted, q->total_lost_frames); |
|
214 |
#endif |
|
215 |
q->total_lost_frames = q->total_lost_frames + pkt->stream_index - q->last_frame_extracted - 1; |
|
216 |
} |
|
217 |
//update index of last frame extracted |
|
197 | 218 |
q->last_frame_extracted = pkt->stream_index; |
198 | 219 |
} |
199 | 220 |
else { |
200 | 221 |
#ifdef DEBUG_QUEUE |
201 |
printf("Extract from 2 packets ");
|
|
222 |
printf(" AV = 1 and Extract from 2 packets\n");
|
|
202 | 223 |
#endif |
203 | 224 |
// Check for loss |
204 | 225 |
if(pkt1->next) |
205 | 226 |
{ |
227 |
#ifdef DEBUG_QUEUE |
|
228 |
printf(" we have a next...\n"); |
|
229 |
#endif |
|
206 | 230 |
//av_init_packet(&tmp); |
207 | 231 |
pkt->size = dimAudioQ; |
208 | 232 |
pkt->dts = pkt1->pkt.dts; |
209 | 233 |
pkt->pts = pkt1->pkt.pts; |
210 |
pkt->stream_index = 1; |
|
234 |
pkt->stream_index = pkt1->pkt.stream_index;//1;
|
|
211 | 235 |
pkt->flags = 1; |
212 | 236 |
pkt->pos = -1; |
213 | 237 |
pkt->convergence_duration = -1; |
... | ... | |
216 | 240 |
{ |
217 | 241 |
SizeToCopy=pkt1->pkt.size-AudioQueueOffset; |
218 | 242 |
#ifdef DEBUG_QUEUE |
219 |
printf("SizeToCopy=%d ",SizeToCopy);
|
|
243 |
printf(" SizeToCopy=%d\n",SizeToCopy);
|
|
220 | 244 |
#endif |
221 | 245 |
memcpy(pkt->data,pkt1->pkt.data+AudioQueueOffset,SizeToCopy); |
222 | 246 |
memcpy(pkt->data+SizeToCopy,pkt1->next->pkt.data,(dimAudioQ-SizeToCopy)*sizeof(uint8_t)); |
... | ... | |
228 | 252 |
q->last_pkt = NULL; |
229 | 253 |
q->nb_packets--; |
230 | 254 |
q->size -= SizeToCopy; |
255 |
#ifdef DEBUG_QUEUE |
|
256 |
printf(" about to free... "); |
|
257 |
#endif |
|
231 | 258 |
free(pkt1->pkt.data); |
232 | 259 |
av_free(pkt1); |
260 |
#ifdef DEBUG_QUEUE |
|
261 |
printf("freeed\n"); |
|
262 |
#endif |
|
233 | 263 |
// Adjust timestamps |
234 | 264 |
pkt1 = q->first_pkt; |
235 | 265 |
if(pkt1) |
... | ... | |
245 | 275 |
AudioQueueOffset=0; |
246 | 276 |
} |
247 | 277 |
#ifdef DEBUG_QUEUE |
248 |
printf("AudioQueueOffset = %d\n",AudioQueueOffset); |
|
278 |
printf(" AudioQueueOffset = %d\n",AudioQueueOffset);
|
|
249 | 279 |
#endif |
280 |
|
|
281 |
//compute lost frame statistics |
|
282 |
if(q->last_frame_extracted > 0 && pkt->stream_index > q->last_frame_extracted) { |
|
283 |
#ifdef DEBUG_QUEUE |
|
284 |
printf(" stats: stidx %d last %d tot %d\n", pkt->stream_index, q->last_frame_extracted, q->total_lost_frames); |
|
285 |
#endif |
|
286 |
q->total_lost_frames = q->total_lost_frames + pkt->stream_index - q->last_frame_extracted - 1; |
|
287 |
} |
|
288 |
//update index of last frame extracted |
|
250 | 289 |
q->last_frame_extracted = pkt->stream_index; |
251 | 290 |
} |
252 | 291 |
} |
253 | 292 |
else { |
293 |
#ifdef DEBUG_QUEUE |
|
294 |
printf(" AV not 1\n"); |
|
295 |
#endif |
|
254 | 296 |
q->first_pkt = pkt1->next; |
255 | 297 |
if (!q->first_pkt) |
256 | 298 |
q->last_pkt = NULL; |
... | ... | |
265 | 307 |
pkt->pos = pkt1->pkt.pos; |
266 | 308 |
pkt->convergence_duration = pkt1->pkt.convergence_duration; |
267 | 309 |
//*pkt = pkt1->pkt; |
310 |
#ifdef DEBUG_QUEUE |
|
311 |
printf(" about to free... "); |
|
312 |
#endif |
|
268 | 313 |
memcpy(pkt->data,pkt1->pkt.data,pkt1->pkt.size); |
269 | 314 |
free(pkt1->pkt.data); |
270 | 315 |
av_free(pkt1); |
316 |
#ifdef DEBUG_QUEUE |
|
317 |
printf("freeed\n"); |
|
318 |
#endif |
|
271 | 319 |
ret = 1; |
320 |
//compute lost frame statistics |
|
321 |
if(q->last_frame_extracted > 0 && pkt->stream_index > q->last_frame_extracted) { |
|
322 |
#ifdef DEBUG_QUEUE |
|
323 |
printf(" stats: stidx %d last %d tot %d\n", pkt->stream_index, q->last_frame_extracted, q->total_lost_frames); |
|
324 |
#endif |
|
325 |
q->total_lost_frames = q->total_lost_frames + pkt->stream_index - q->last_frame_extracted - 1; |
|
326 |
} |
|
327 |
//update index of last frame extracted |
|
272 | 328 |
q->last_frame_extracted = pkt->stream_index; |
273 | 329 |
} |
274 | 330 |
} |
275 |
if(q->nb_packets==0 && q->queueType==AUDIO) |
|
331 |
#ifdef DEBUG_QUEUE |
|
332 |
else { |
|
333 |
printf(" pk1 NULLLLLLLLLL!!!!\n"); |
|
334 |
} |
|
335 |
#endif |
|
336 |
|
|
337 |
if(q->nb_packets==0 && q->queueType==AUDIO) { |
|
276 | 338 |
QueueFillingMode=1; |
339 |
#ifdef DEBUG_QUEUE |
|
340 |
printf("QUEUE Get FillingMode ON\n"); |
|
341 |
#endif |
|
342 |
} |
|
343 |
#ifdef DEBUG_QUEUE |
|
344 |
printf("QUEUE Get LastFrameExtracted = %d\n",q->last_frame_extracted); |
|
345 |
printf("QUEUE Get Tot lost frames = %d\n",q->total_lost_frames); |
|
346 |
#endif |
|
277 | 347 |
|
278 | 348 |
SDL_UnlockMutex(q->mutex); |
279 | 349 |
return ret; |
... | ... | |
305 | 375 |
FirstTimeAudio = 0; |
306 | 376 |
FirstTime = 0; |
307 | 377 |
#ifdef DEBUG_AUDIO |
308 |
printf("audio_decode_frame - DeltaTimeAudio=%lld\n",DeltaTimeAudio);
|
|
378 |
printf("audio_decode_frame - DeltaTimeAudio=%lld\n",DeltaTime); |
|
309 | 379 |
#endif |
310 | 380 |
} |
311 | 381 |
} |
... | ... | |
408 | 478 |
|
409 | 479 |
pCodecCtx=avcodec_alloc_context(); |
410 | 480 |
pCodecCtx->codec_type = CODEC_TYPE_VIDEO; |
481 |
#ifdef H264_VIDEO_ENCODER |
|
411 | 482 |
pCodecCtx->codec_id = CODEC_ID_H264; |
412 |
pCodecCtx->me_range = 16;
|
|
483 |
pCodecCtx->me_range = 16;
|
|
413 | 484 |
pCodecCtx->max_qdiff = 4; |
414 | 485 |
pCodecCtx->qmin = 10; |
415 | 486 |
pCodecCtx->qmax = 51; |
416 | 487 |
pCodecCtx->qcompress = 0.6; |
417 |
// pCodecCtx->bit_rate = 400000; |
|
488 |
#else |
|
489 |
pCodecCtx->codec_id = CODEC_ID_MPEG4; |
|
490 |
#endif |
|
491 |
//pCodecCtx->bit_rate = 400000; |
|
418 | 492 |
// resolution must be a multiple of two |
419 | 493 |
pCodecCtx->width = tval->width;//176;//352; |
420 | 494 |
pCodecCtx->height = tval->height;//144;//288; |
421 | 495 |
// frames per second |
422 |
pCodecCtx->time_base = (AVRational){1,25};
|
|
423 |
pCodecCtx->gop_size = 10; // emit one intra frame every ten frames
|
|
424 |
pCodecCtx->max_b_frames=1;
|
|
496 |
//pCodecCtx->time_base = (AVRational){1,25};
|
|
497 |
//pCodecCtx->gop_size = 10; // emit one intra frame every ten frames
|
|
498 |
//pCodecCtx->max_b_frames=1;
|
|
425 | 499 |
pCodecCtx->pix_fmt = PIX_FMT_YUV420P; |
426 | 500 |
pCodec=avcodec_find_decoder(pCodecCtx->codec_id); |
427 | 501 |
|
... | ... | |
524 | 598 |
|
525 | 599 |
avcodec_decode_video2(pCodecCtx, pFrame, &frameFinished, &VideoPkt); |
526 | 600 |
|
527 |
if(frameFinished) { // it must be true all the time else error |
|
528 |
// printf("FrameFinished\n"); |
|
601 |
if(frameFinished) { // it must be true all the time else error |
|
602 |
#ifdef DEBUG_VIDEO |
|
603 |
printf("FrameFinished\n"); |
|
604 |
#endif |
|
529 | 605 |
//SaveFrame(pFrame, pCodecCtx->width, pCodecCtx->height, cont++); |
530 | 606 |
//fwrite(pktvideo.data, 1, pktvideo.size, frecon); |
531 | 607 |
|
532 | 608 |
// Lock SDL_yuv_overlay |
533 | 609 |
if ( SDL_MUSTLOCK(screen) ) { |
534 |
if ( SDL_LockSurface(screen) < 0 ) break; |
|
610 |
#ifdef DEBUG_VIDEO |
|
611 |
printf("-1 "); |
|
612 |
#endif |
|
613 |
|
|
614 |
if ( SDL_LockSurface(screen) < 0 ) { |
|
615 |
#ifdef DEBUG_VIDEO |
|
616 |
printf("0 "); |
|
617 |
#endif |
|
618 |
break; |
|
619 |
} |
|
535 | 620 |
} |
621 |
#ifdef DEBUG_VIDEO |
|
622 |
printf("1 "); |
|
623 |
#endif |
|
536 | 624 |
if (SDL_LockYUVOverlay(yuv_overlay) < 0) break; |
625 |
#ifdef DEBUG_VIDEO |
|
626 |
printf("2 "); |
|
627 |
#endif |
|
537 | 628 |
|
538 | 629 |
pict.data[0] = yuv_overlay->pixels[0]; |
539 | 630 |
pict.data[1] = yuv_overlay->pixels[2]; |
... | ... | |
542 | 633 |
pict.linesize[0] = yuv_overlay->pitches[0]; |
543 | 634 |
pict.linesize[1] = yuv_overlay->pitches[2]; |
544 | 635 |
pict.linesize[2] = yuv_overlay->pitches[1]; |
636 |
#ifdef DEBUG_VIDEO |
|
637 |
printf("3 "); |
|
638 |
#endif |
|
639 |
|
|
545 | 640 |
img_convert_ctx = sws_getContext(tval->width,tval->height,PIX_FMT_YUV420P,tval->width,tval->height,PIX_FMT_YUV420P,SWS_BICUBIC,NULL,NULL,NULL); |
641 |
#ifdef DEBUG_VIDEO |
|
642 |
printf("4 "); |
|
643 |
#endif |
|
546 | 644 |
|
547 | 645 |
if(img_convert_ctx==NULL) { |
548 | 646 |
fprintf(stderr,"Cannot initialize the conversion context!\n"); |
549 | 647 |
exit(1); |
550 | 648 |
} |
551 | 649 |
sws_scale(img_convert_ctx,pFrame->data,pFrame->linesize,0,tval->height,pict.data,pict.linesize); |
650 |
#ifdef DEBUG_VIDEO |
|
651 |
printf("5 "); |
|
652 |
#endif |
|
552 | 653 |
|
553 | 654 |
// let's draw the data (*yuv[3]) on a SDL screen (*screen) |
554 | 655 |
if ( SDL_MUSTLOCK(screen) ) { |
555 | 656 |
SDL_UnlockSurface(screen); |
556 | 657 |
} |
658 |
#ifdef DEBUG_VIDEO |
|
659 |
printf("6 "); |
|
660 |
#endif |
|
661 |
|
|
557 | 662 |
SDL_UnlockYUVOverlay(yuv_overlay); |
558 | 663 |
|
559 | 664 |
// Show, baby, show! |
560 | 665 |
SDL_DisplayYUVOverlay(yuv_overlay, &rect); |
666 |
#ifdef DEBUG_VIDEO |
|
667 |
printf("7\n"); |
|
668 |
#endif |
|
561 | 669 |
|
562 | 670 |
} |
563 | 671 |
} |
... | ... | |
700 | 808 |
int frameFinished, len_audio; |
701 | 809 |
int numBytes,outbuf_audio_size,audio_size; |
702 | 810 |
|
703 |
int dir_entries,y;
|
|
811 |
int y; |
|
704 | 812 |
|
705 | 813 |
uint8_t *outbuf,*outbuf_audio; |
706 | 814 |
uint8_t *outbuf_audi_audio; |
... | ... | |
717 | 825 |
//SDL_mutex *lock; |
718 | 826 |
SDL_AudioSpec wanted_spec, spec; |
719 | 827 |
|
720 |
|
|
828 |
struct MHD_Daemon *daemon = NULL; |
|
829 |
|
|
721 | 830 |
char buf[1024],outfile[1024], basereadfile[1024],readfile[1024]; |
722 | 831 |
FILE *fp; |
723 |
struct dirent **namelist; |
|
832 |
// struct dirent **namelist;
|
|
724 | 833 |
int width,height,asample_rate,achannels; |
725 | 834 |
//double framerate; |
726 | 835 |
|
727 |
|
|
836 |
// TwBar *bar; |
|
728 | 837 |
|
729 | 838 |
ThreadVal *tval; |
730 | 839 |
tval = (ThreadVal *)malloc(sizeof(ThreadVal)); |
... | ... | |
757 | 866 |
//aCodecCtx->bit_rate = 64000; |
758 | 867 |
aCodecCtx->sample_rate = asample_rate; |
759 | 868 |
aCodecCtx->channels = achannels; |
869 |
#ifdef MP3_AUDIO_ENCODER |
|
760 | 870 |
aCodec = avcodec_find_decoder(CODEC_ID_MP3); // codec audio |
761 |
printf("%d %d\n",CODEC_ID_MP2,CODEC_ID_MP3); |
|
871 |
#else |
|
872 |
aCodec = avcodec_find_decoder(CODEC_ID_MP2); |
|
873 |
#endif |
|
874 |
printf("MP2 codec id %d MP3 codec id %d\n",CODEC_ID_MP2,CODEC_ID_MP3); |
|
762 | 875 |
if(!aCodec) { |
763 | 876 |
printf("Codec not found!\n"); |
764 | 877 |
return -1; |
... | ... | |
767 | 880 |
fprintf(stderr, "could not open codec\n"); |
768 | 881 |
return -1; // Could not open codec |
769 | 882 |
} |
770 |
printf("Codecid: %d %d",aCodecCtx->codec_id,aCodecCtx->sample_rate);
|
|
771 |
printf("samplerate: %d",aCodecCtx->sample_rate); |
|
772 |
printf("channels: %d",aCodecCtx->channels); |
|
883 |
printf("using audio Codecid: %d ",aCodecCtx->codec_id);
|
|
884 |
printf("samplerate: %d ",aCodecCtx->sample_rate);
|
|
885 |
printf("channels: %d\n",aCodecCtx->channels);
|
|
773 | 886 |
wanted_spec.freq = aCodecCtx->sample_rate; |
774 | 887 |
wanted_spec.format = AUDIO_S16SYS; |
775 | 888 |
wanted_spec.channels = aCodecCtx->channels; |
... | ... | |
817 | 930 |
exit(1); |
818 | 931 |
} |
819 | 932 |
|
933 |
/* |
|
934 |
// Initialize AntTweakBar |
|
935 |
TwInit(TW_OPENGL, NULL); |
|
936 |
// Tell the window size to AntTweakBar |
|
937 |
TwWindowSize(width, height); |
|
938 |
// Create a tweak bar |
|
939 |
bar = TwNewBar("TweakBar"); |
|
940 |
// Add 'width' and 'height' to 'bar': they are read-only (RO) variables of type TW_TYPE_INT32. |
|
941 |
TwAddVarRO(bar, "Width", TW_TYPE_INT32, &width, " label='Wnd width' help='Width of the graphics window (in pixels)' "); |
|
942 |
*/ |
|
943 |
|
|
820 | 944 |
yuv_overlay = SDL_CreateYUVOverlay(width, height,SDL_YV12_OVERLAY, screen); |
821 | 945 |
|
822 | 946 |
if ( yuv_overlay == NULL ) { |
... | ... | |
852 | 976 |
//lock = SDL_CreateMutex(); |
853 | 977 |
//SDL_WaitThread(exit_thread2,NULL); |
854 | 978 |
|
855 |
#ifdef DEBUG_SOURCE |
|
856 |
printf("SOURCE: Num entries=%d\n",dir_entries); |
|
857 |
#endif |
|
858 | 979 |
|
859 | 980 |
|
860 |
initChunkPuller(); |
|
981 |
daemon = initChunkPuller();
|
|
861 | 982 |
|
862 | 983 |
|
863 | 984 |
|
... | ... | |
880 | 1001 |
} |
881 | 1002 |
// Stop audio&video playback |
882 | 1003 |
|
883 |
|
|
1004 |
printf("1\n"); |
|
884 | 1005 |
SDL_WaitThread(video_thread,NULL); |
1006 |
printf("2\n"); |
|
885 | 1007 |
SDL_PauseAudio(1); |
1008 |
printf("3\n"); |
|
886 | 1009 |
SDL_CloseAudio(); |
1010 |
printf("4\n"); |
|
887 | 1011 |
SDL_Quit(); |
888 | 1012 |
|
889 | 1013 |
//SDL_DestroyMutex(lock); |
1014 |
printf("5\n"); |
|
890 | 1015 |
av_free(aCodecCtx); |
1016 |
printf("6\n"); |
|
891 | 1017 |
free(AudioPkt.data); |
1018 |
printf("7\n"); |
|
892 | 1019 |
free(VideoPkt.data); |
893 |
free(namelist); |
|
1020 |
// free(namelist); |
|
1021 |
printf("8\n"); |
|
894 | 1022 |
free(outbuf_audio); |
895 | 1023 |
|
896 |
finalizeChunkPuller(); |
|
1024 |
printf("9\n"); |
|
1025 |
finalizeChunkPuller(daemon); |
|
1026 |
printf("10\n"); |
|
897 | 1027 |
|
898 | 1028 |
return 0; |
899 | 1029 |
} |
... | ... | |
908 | 1038 |
Frame *frame=NULL; |
909 | 1039 |
AVPacket packet, packetaudio; |
910 | 1040 |
|
911 |
uint8_t audio_bufQ[(AVCODEC_MAX_AUDIO_FRAME_SIZE * 3) / 2]; |
|
1041 |
uint8_t *video_bufQ = NULL; |
|
1042 |
uint16_t *audio_bufQ = NULL; |
|
1043 |
//uint8_t audio_bufQ[(AVCODEC_MAX_AUDIO_FRAME_SIZE * 3) / 2]; |
|
912 | 1044 |
int16_t *dataQ; |
913 | 1045 |
int data_sizeQ; |
914 | 1046 |
int lenQ; |
915 | 1047 |
int sizeFrame = 0; |
916 | 1048 |
sizeFrame = 3*sizeof(int)+sizeof(struct timeval); |
917 | 1049 |
|
1050 |
audio_bufQ = (uint16_t *)av_malloc(AVCODEC_MAX_AUDIO_FRAME_SIZE); |
|
918 | 1051 |
gchunk = (Chunk *)malloc(sizeof(Chunk)); |
919 | 1052 |
if(!gchunk) { |
920 |
printf("Memory error!\n"); |
|
1053 |
printf("Memory error in gchunk!\n");
|
|
921 | 1054 |
return PLAYER_FAIL_RETURN; |
922 | 1055 |
} |
923 | 1056 |
|
924 | 1057 |
decodeChunk(gchunk, block, block_size); |
925 | 1058 |
|
926 | 1059 |
echunk = grapesChunkToExternalChunk(gchunk); |
1060 |
if(echunk == NULL) { |
|
1061 |
printf("Memory error in echunk!\n"); |
|
1062 |
free(gchunk->attributes); |
|
1063 |
free(gchunk->data); |
|
1064 |
free(gchunk); |
|
1065 |
return PLAYER_FAIL_RETURN; |
|
1066 |
} |
|
1067 |
free(gchunk->attributes); |
|
927 | 1068 |
free(gchunk); |
928 | 1069 |
|
929 | 1070 |
frame = (Frame *)malloc(sizeof(Frame)); |
... | ... | |
951 | 1092 |
|
952 | 1093 |
if(frame->type!=5) { // video frame |
953 | 1094 |
av_init_packet(&packet); |
954 |
packet.data = buffer; |
|
955 |
packet.size = frame->size; |
|
956 |
packet.pts = frame->timestamp.tv_sec*(unsigned long long)1000+frame->timestamp.tv_usec; |
|
957 |
packet.dts = frame->timestamp.tv_sec*(unsigned long long)1000+frame->timestamp.tv_usec; |
|
958 |
packet.stream_index = frame->number; // use of stream_index for number frame |
|
959 |
//packet.duration = frame->timestamp.tv_sec; |
|
960 |
packet_queue_put(&videoq,&packet); |
|
1095 |
video_bufQ = (uint8_t *)malloc(frame->size); //this gets freed at the time of packet_queue_get |
|
1096 |
if(video_bufQ) { |
|
1097 |
memcpy(video_bufQ, buffer, frame->size); |
|
1098 |
packet.data = video_bufQ; |
|
1099 |
packet.size = frame->size; |
|
1100 |
packet.pts = frame->timestamp.tv_sec*(unsigned long long)1000+frame->timestamp.tv_usec; |
|
1101 |
packet.dts = frame->timestamp.tv_sec*(unsigned long long)1000+frame->timestamp.tv_usec; |
|
1102 |
packet.stream_index = frame->number; // use of stream_index for number frame |
|
1103 |
//packet.duration = frame->timestamp.tv_sec; |
|
1104 |
packet_queue_put(&videoq,&packet); |
|
961 | 1105 |
#ifdef DEBUG_SOURCE |
962 |
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); |
|
1106 |
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);
|
|
963 | 1107 |
#endif |
1108 |
} |
|
964 | 1109 |
} |
965 | 1110 |
else { // audio frame |
966 | 1111 |
av_init_packet(&packetaudio); |
... | ... | |
977 | 1122 |
|
978 | 1123 |
// insert the audio frame into the queue |
979 | 1124 |
data_sizeQ = AVCODEC_MAX_AUDIO_FRAME_SIZE; |
980 |
lenQ = avcodec_decode_audio3(aCodecCtx, (int16_t *)audio_bufQ, &data_sizeQ, &packetaudio);
|
|
1125 |
lenQ = avcodec_decode_audio3(aCodecCtx, (int16_t *)audio_bufQ, &data_sizeQ, &packetaudio); |
|
981 | 1126 |
if(lenQ>0) |
982 | 1127 |
{ |
983 | 1128 |
// for freeing there is some memory still in tempdata to be freed |
984 |
dataQ = (int16_t *)malloc(data_sizeQ); |
|
1129 |
dataQ = (int16_t *)malloc(data_sizeQ); //this gets freed at the time of packet_queue_get
|
|
985 | 1130 |
if(dataQ) |
986 | 1131 |
{ |
987 | 1132 |
memcpy(dataQ,audio_bufQ,data_sizeQ); |
... | ... | |
1015 | 1160 |
} |
1016 | 1161 |
ProcessKeys(); |
1017 | 1162 |
*/ |
1163 |
free(echunk->data); |
|
1018 | 1164 |
free(echunk); |
1019 | 1165 |
free(frame); |
1166 |
av_free(audio_bufQ); |
|
1020 | 1167 |
} |
chunker_streamer.c | ||
---|---|---|
19 | 19 |
#include <SDL_thread.h> |
20 | 20 |
|
21 | 21 |
#include "chunker_streamer.h" |
22 |
#include "codec_definitions.h" |
|
22 | 23 |
|
23 | 24 |
#ifdef __MINGW32__ |
24 | 25 |
#undef main /* Prevents SDL from overriding main() */ |
... | ... | |
142 | 143 |
AVFrame *pFrame; |
143 | 144 |
AVFrame *pFrameRGB; |
144 | 145 |
AVPacket packet; |
146 |
int64_t last_pkt_dts=0,delta=0,delta_audio=0,last_pkt_dts_audio=0; |
|
145 | 147 |
|
146 | 148 |
Frame *frame=NULL; |
147 | 149 |
|
... | ... | |
155 | 157 |
long long Now; |
156 | 158 |
short int FirstTimeAudio=1, FirstTimeVideo=1; |
157 | 159 |
long long DeltaTimeAudio=0, DeltaTimeVideo=0, newTime; |
160 |
|
|
161 |
double ptsvideo1,ptsaudio1; |
|
158 | 162 |
|
159 | 163 |
struct dirent **namelist; |
160 | 164 |
|
... | ... | |
172 | 176 |
|
173 | 177 |
cmeta = chunkerInit("configChunker.txt"); |
174 | 178 |
|
179 |
/* |
|
175 | 180 |
dir_entries = scandir("chunks",&namelist,NULL,alphasortNew); |
176 | 181 |
if(dir_entries>0) { |
177 | 182 |
strcpy(basedelfile,"chunks/"); |
... | ... | |
187 | 192 |
rmdir("chunks"); |
188 | 193 |
} |
189 | 194 |
mkdir("chunks",0777); |
195 |
*/ |
|
190 | 196 |
|
191 | 197 |
fprintf(stderr, "Chunkbuffer Strategy:%d Value Strategy:%d\n", cmeta->strategy, cmeta->val_strategy); |
192 | 198 |
f1 = fopen("original.mpg","wb"); |
... | ... | |
208 | 214 |
// Find the first video stream |
209 | 215 |
videoStream=-1; |
210 | 216 |
audioStream=-1; |
211 |
fprintf(stderr, "Num streams : %d\n",pFormatCtx->nb_streams); |
|
217 |
|
|
212 | 218 |
for(i=0; i<pFormatCtx->nb_streams; i++) { |
213 | 219 |
if(pFormatCtx->streams[i]->codec->codec_type==CODEC_TYPE_VIDEO && videoStream<0) { |
214 | 220 |
videoStream=i; |
... | ... | |
217 | 223 |
audioStream=i; |
218 | 224 |
} |
219 | 225 |
} |
226 |
fprintf(stderr, "Num streams : %d TBR: %d %d RFRAMERATE:%d %d Duration:%d\n",pFormatCtx->nb_streams,pFormatCtx->streams[videoStream]->time_base.num,pFormatCtx->streams[videoStream]->time_base.den,pFormatCtx->streams[videoStream]->r_frame_rate.num,pFormatCtx->streams[videoStream]->r_frame_rate.den,pFormatCtx->streams[videoStream]->duration); |
|
227 |
|
|
220 | 228 |
fprintf(stderr, "Video stream has id : %d\n",videoStream); |
221 | 229 |
fprintf(stderr, "Audio stream has id : %d\n",audioStream); |
222 | 230 |
|
... | ... | |
238 | 246 |
|
239 | 247 |
|
240 | 248 |
pCodecCtxEnc=avcodec_alloc_context(); |
249 |
#ifdef H264_VIDEO_ENCODER |
|
241 | 250 |
pCodecCtxEnc->me_range=16; |
242 | 251 |
pCodecCtxEnc->max_qdiff=4; |
243 | 252 |
pCodecCtxEnc->qmin=10; |
... | ... | |
251 | 260 |
pCodecCtxEnc->height = pCodecCtx->height; |
252 | 261 |
// frames per second |
253 | 262 |
pCodecCtxEnc->time_base= pCodecCtx->time_base;//(AVRational){1,25}; |
254 |
fprintf(stderr, "CIC:%d %d %d %d\n",pCodecCtxEnc->time_base.num,pCodecCtxEnc->time_base.den,pCodecCtx->time_base.num,pCodecCtx->time_base.den); |
|
255 | 263 |
pCodecCtxEnc->gop_size = 10; // emit one intra frame every ten frames |
256 | 264 |
//pCodecCtxEnc->max_b_frames=1; |
257 | 265 |
pCodecCtxEnc->pix_fmt = PIX_FMT_YUV420P; |
266 |
#else |
|
267 |
pCodecCtxEnc->codec_type = CODEC_TYPE_VIDEO; |
|
268 |
pCodecCtxEnc->codec_id = CODEC_ID_MPEG4; |
|
269 |
pCodecCtxEnc->bit_rate = video_bitrate; |
|
270 |
pCodecCtxEnc->width = pCodecCtx->width; |
|
271 |
pCodecCtxEnc->height = pCodecCtx->height; |
|
272 |
// frames per second |
|
273 |
pCodecCtxEnc->time_base= pCodecCtx->time_base;//(AVRational){1,25}; |
|
274 |
pCodecCtxEnc->gop_size = 10; // emit one intra frame every ten frames |
|
275 |
//pCodecCtxEnc->max_b_frames=1; |
|
276 |
pCodecCtxEnc->pix_fmt = PIX_FMT_YUV420P; |
|
277 |
#endif |
|
278 |
fprintf(stderr, "CIC:%d %d %d %d\n", pCodecCtxEnc->time_base.num, pCodecCtxEnc->time_base.den, pCodecCtx->time_base.num, pCodecCtx->time_base.den); |
|
279 |
|
|
258 | 280 |
|
259 | 281 |
aCodecCtxEnc = avcodec_alloc_context(); |
260 | 282 |
aCodecCtxEnc->bit_rate = audio_bitrate; //256000 |
... | ... | |
268 | 290 |
|
269 | 291 |
if(audioStream!=-1) { |
270 | 292 |
aCodec = avcodec_find_decoder(aCodecCtx->codec_id); |
293 |
#ifdef MP3_AUDIO_ENCODER |
|
271 | 294 |
aCodecEnc = avcodec_find_encoder(CODEC_ID_MP3); |
295 |
#else |
|
296 |
aCodecEnc = avcodec_find_encoder(CODEC_ID_MP2); |
|
297 |
#endif |
|
272 | 298 |
if(aCodec==NULL) { |
273 | 299 |
fprintf(stderr,"Unsupported acodec!\n"); |
274 | 300 |
return -1; |
... | ... | |
288 | 314 |
} |
289 | 315 |
|
290 | 316 |
} |
291 |
|
|
292 |
fprintf(stderr, "Setting codecID to H264: %d %d",pCodecCtx->codec_id,CODEC_ID_H264); |
|
293 |
pCodecEnc =avcodec_find_encoder(CODEC_ID_H264);//pCodecCtx->codec_id); |
|
294 |
|
|
317 |
#ifdef H264_VIDEO_ENCODER |
|
318 |
fprintf(stderr, "Setting codecID to H264: %d %d\n",pCodecCtx->codec_id, CODEC_ID_H264); |
|
319 |
pCodecEnc = avcodec_find_encoder(CODEC_ID_H264);//pCodecCtx->codec_id); |
|
320 |
#else |
|
321 |
fprintf(stderr, "Setting codecID to mpeg4: %d %d\n",pCodecCtx->codec_id, CODEC_ID_MPEG4); |
|
322 |
pCodecEnc = avcodec_find_encoder(CODEC_ID_MPEG4); |
|
323 |
#endif |
|
295 | 324 |
if(pCodec==NULL) { |
296 | 325 |
fprintf(stderr, "Unsupported pcodec!\n"); |
297 | 326 |
return -1; // Codec not found |
... | ... | |
335 | 364 |
return -1; |
336 | 365 |
} |
337 | 366 |
sizeChunk = 6*sizeof(int)+2*sizeof(struct timeval)+sizeof(double); |
338 |
chunk->data=NULL; |
|
367 |
chunk->data=NULL;
|
|
339 | 368 |
initChunk(chunk,&seq_current_chunk); |
340 | 369 |
chunkaudio = (ExternalChunk *)malloc(sizeof(ExternalChunk)); |
341 | 370 |
if(!chunkaudio) { |
342 | 371 |
fprintf(stderr, "Memory error!!!\n"); |
343 | 372 |
return -1; |
344 | 373 |
} |
345 |
chunkaudio->data=NULL; |
|
374 |
chunkaudio->data=NULL;
|
|
346 | 375 |
initChunk(chunkaudio,&seq_current_chunk); |
347 | 376 |
stime = -1; |
348 | 377 |
|
... | ... | |
362 | 391 |
// Is this a packet from the video stream? |
363 | 392 |
if(packet.stream_index==videoStream) { |
364 | 393 |
// Decode video frame |
394 |
double PTS,glob_PTS; |
|
395 |
|
|
396 |
PTS = 0; |
|
397 |
glob_PTS = packet.pts; |
|
398 |
if(avcodec_decode_video2(pCodecCtx, pFrame, &frameFinished, &packet)>0) { |
|
365 | 399 |
#ifdef DEBUG_VIDEO_FRAMES |
366 |
//fprintf(stderr,"readingQUIDARIO\n");
|
|
400 |
fprintf(stderr, "Tipo Frame: %d %lld\n",pFrame->pict_type,AV_NOPTS_VALUE);
|
|
367 | 401 |
#endif |
368 |
if(avcodec_decode_video2(pCodecCtx, pFrame, &frameFinished, &packet)>0) { |
|
402 |
/* if(packet.dts==AV_NOPTS_VALUE && pFrame->opaque && *(uint64_t*)pFrame->opaque!=AV_NOPTS_VALUE) { |
|
403 |
PTS = *(uint64_t *)pFrame->opaque; |
|
404 |
} |
|
405 |
else if(packet.dts!=AV_NOPTS_VALUE) { |
|
406 |
PTS = packet.dts; |
|
407 |
} |
|
408 |
else |
|
409 |
PTS = 0; |
|
410 |
PTS *= av_q2d(pCodecCtx->time_base);*/ |
|
369 | 411 |
|
370 | 412 |
// Did we get a video frame? |
371 | 413 |
//stime = (int)packet.dts; |
372 |
//fprintf(stderr, "VIDEO: stime:%d %d\n",stime,(int)packet.dts); |
|
414 |
#ifdef DEBUG_VIDEO_FRAMES |
|
415 |
fprintf(stderr, "VIDEO: stime DTS:%d %d\n",stime,(int)packet.dts); |
|
416 |
#endif |
|
373 | 417 |
if(frameFinished) { // it must be true all the time else error |
374 |
out_size = avcodec_encode_video(pCodecCtxEnc, outbuf, outbuf_size, pFrame); |
|
375 |
|
|
376 | 418 |
frame->number = contFrameVideo++;//pCodecCtx->frame_number; |
419 |
#ifdef DEBUG_VIDEO_FRAMES |
|
420 |
fprintf(stderr, "DENTROFRAMEFINISHED %lf\n",PTS); |
|
421 |
#endif |
|
422 |
if(frame->number==0) { |
|
423 |
if(packet.dts==AV_NOPTS_VALUE) |
|
424 |
continue; |
|
425 |
last_pkt_dts = packet.dts; |
|
426 |
newTime = 0; |
|
427 |
} |
|
428 |
else { |
|
429 |
if(packet.dts!=AV_NOPTS_VALUE) { |
|
430 |
delta = packet.dts-last_pkt_dts; |
|
431 |
last_pkt_dts = packet.dts; |
|
432 |
} |
|
433 |
else if(delta==0) |
|
434 |
continue; |
|
435 |
} |
|
436 |
#ifdef DEBUG_VIDEO_FRAMES |
|
437 |
fprintf(stderr, "deltavideo : %lld\n",delta); |
|
438 |
#endif |
|
439 |
out_size = avcodec_encode_video(pCodecCtxEnc, outbuf, outbuf_size, pFrame); |
|
440 |
#ifdef DEBUG_VIDEO_FRAMES |
|
441 |
fprintf(stderr, "video frame size nuovo : %d\n",pCodecCtx->frame_number); |
|
442 |
#endif |
|
377 | 443 |
|
378 | 444 |
Now=(long long)SDL_GetTicks(); |
379 | 445 |
#ifdef DEBUG_VIDEO_FRAMES |
380 |
//fprintf(stderr, "video frame size nuovo pippo : %d\n",pCodecCtxEnc->frame_size); |
|
381 |
//fprintf(stderr, "dtsvideo:%f %d %d %d\n",(double)packet.dts,(int)packet.duration,pCodecCtxEnc->time_base.den,pCodecCtxEnc->time_base.num); |
|
446 |
fprintf(stderr, "ptsvideo:%lld %d %d %d %f %d\n", packet.pts, (int)packet.duration, pCodecCtxEnc->time_base.den, pCodecCtxEnc->time_base.num, (double)packet.dts, pCodecCtx->time_base.den); |
|
382 | 447 |
#endif |
383 |
if(packet.duration>0) |
|
448 |
|
|
449 |
/* if(packet.duration>0) |
|
384 | 450 |
newtimestamp = ((double)((packet.dts)*pCodecCtxEnc->time_base.num))/((double)(packet.duration*(pCodecCtxEnc->time_base.den))); |
385 | 451 |
else |
386 | 452 |
newtimestamp =((double)((packet.dts)*pCodecCtxEnc->time_base.num)/((double)pCodecCtxEnc->time_base.den)); |
387 |
#ifdef DEBUG_VIDEO_FRAMES |
|
388 | 453 |
//frame->timestamp.tv_sec = (long)packet.dts*1.e-5; |
389 | 454 |
//frame->timestamp.tv_usec = ((long)packet.dts%100000/100); |
455 |
#ifdef DEBUG_VIDEO_FRAMES |
|
390 | 456 |
//fprintf(stderr, "ts:%llf\n",newtimestamp); |
391 | 457 |
#endif |
392 | 458 |
if(FirstTimeVideo) { |
393 | 459 |
DeltaTimeVideo=Now-((unsigned int)newtimestamp*(unsigned long long)1000+(newtimestamp-(unsigned int)newtimestamp)*1000); |
394 | 460 |
FirstTimeVideo = 0; |
395 | 461 |
#ifdef DEBUG_VIDEO_FRAMES |
396 |
//fprintf(stderr, "%lld\n",Now); |
|
397 | 462 |
//fprintf(stderr, "DeltaTimeVideo : %lld\n",DeltaTimeVideo); |
463 |
//printf("%lld\n",Now); |
|
398 | 464 |
#endif |
399 | 465 |
} |
400 | 466 |
|
401 | 467 |
newTime = DeltaTimeVideo+((unsigned int)newtimestamp*(unsigned long long)1000+(newtimestamp-(unsigned int)newtimestamp)*1000); |
402 |
#ifdef DEBUG_VIDEO_FRAMES |
|
403 |
fprintf(stderr, "newTime : %d\n",newTime); |
|
404 |
#endif |
|
405 |
if(newTime < 0) { |
|
406 |
#ifdef DEBUG_VIDEO_FRAMES |
|
407 |
fprintf(stderr, "Skipping illegal video frame %d. Bad timestamp!!!\n", contFrameVideo); |
|
408 |
#endif |
|
409 |
goto skipvideoframe; |
|
468 |
*/ |
|
469 |
/* char countzeros[10]; |
|
470 |
int countz=0,ic; |
|
471 |
sprintf(countzeros,"%d",pCodecCtxEnc->time_base.den); |
|
472 |
for(ic=0;ic<strlen(countzeros);ic++) { |
|
473 |
|
|
474 |
if(countzeros[ic]=='0') |
|
475 |
countz++; |
|
476 |
}*/ |
|
477 |
|
|
478 |
//newTime = (((double)((packet.dts)-dtsvideo1))*1000.0*(double)pCodecCtxEnc->time_base.num)/(pCodecCtxEnc->time_base.den*((double)(packet.duration))); |
|
479 |
|
|
480 |
if(FirstTimeVideo && packet.pts>0) { |
|
481 |
ptsvideo1 = packet.pts; |
|
482 |
FirstTimeVideo = 0; |
|
410 | 483 |
} |
411 | 484 |
|
412 |
frame->timestamp.tv_sec = (unsigned int)newTime/1000; |
|
485 |
if(frame->number>0) |
|
486 |
newTime = ((double)packet.pts-ptsvideo1)*1000.0/((double)delta*(double)av_q2d(pFormatCtx->streams[videoStream]->r_frame_rate)); |
|
487 |
|
|
488 |
//newTime = ((double)((packet.dts)))/(1000*av_q2d(pCodecCtxEnc->time_base)); |
|
489 |
|
|
490 |
#ifdef DEBUG_VIDEO_FRAMES |
|
491 |
fprintf(stderr, "newTime : %d\n",newTime); |
|
492 |
#endif |
|
493 |
if(newTime<0) |
|
494 |
continue; //SKIP THIS FRAME (E.G. INTERLACE) |
|
495 |
|
|
496 |
frame->timestamp.tv_sec = (long long)newTime/1000; |
|
413 | 497 |
frame->timestamp.tv_usec = newTime%1000; |
414 | 498 |
|
415 | 499 |
frame->size = out_size; |
416 | 500 |
frame->type = pFrame->pict_type; |
417 | 501 |
#ifdef DEBUG_VIDEO_FRAMES |
418 | 502 |
//fprintf(stderr, "video newt:%lf dts:%ld dur:%d sec:%d usec%d pts:%d\n",newtimestamp,(long)packet.dts,packet.duration,frame->timestamp.tv_sec,frame->timestamp.tv_usec,packet.pts); |
419 |
//fprintf(stderr, "num:%d size:%d type:%d\n",frame->number,frame->size,frame->type);
|
|
420 |
//fprintf(stderr,"VIDEO: tvsec:%d tvusec:%d\n",frame->timestamp.tv_sec,frame->timestamp.tv_usec);
|
|
503 |
fprintf(stderr, "num:%d size:%d type:%d\n",frame->number,frame->size,frame->type); |
|
504 |
fprintf(stderr, "VIDEO: tvsec:%d tvusec:%d\n",frame->timestamp.tv_sec,frame->timestamp.tv_usec);
|
|
421 | 505 |
#endif |
422 | 506 |
// Save the frame to disk |
423 | 507 |
++i; |
... | ... | |
433 | 517 |
return -1; |
434 | 518 |
} |
435 | 519 |
#ifdef DEBUG_VIDEO_FRAMES |
436 |
//fprintf(stderr, "QUI1\n"); |
|
437 | 520 |
//fprintf(stderr, "rialloco data di dim:%d con nuova dim:%d\n",chunk->payload_len,out_size); |
438 | 521 |
#endif |
522 |
|
|
439 | 523 |
tempdata = chunk->data+chunk->payload_len; |
440 | 524 |
*((int *)tempdata) = frame->number; |
441 | 525 |
tempdata+=sizeof(int); |
... | ... | |
473 | 557 |
fwrite(outbuf, 1, out_size, f1); // reconstructing original video |
474 | 558 |
|
475 | 559 |
} |
476 |
skipvideoframe: |
|
477 |
; |
|
478 | 560 |
} |
479 | 561 |
} |
480 | 562 |
else if(packet.stream_index==audioStream) { |
481 | 563 |
#ifdef DEBUG_AUDIO_FRAMES |
482 |
//fprintf(stderr,"readingQUIAUDIO\n"); |
|
483 |
#endif |
|
484 | 564 |
//fprintf(stderr, "packet audio dts:%d dts:%d\n",(int)packet.dts,(int)packet.dts); |
565 |
#endif |
|
485 | 566 |
data_size = AVCODEC_MAX_AUDIO_FRAME_SIZE; |
486 | 567 |
if(avcodec_decode_audio3(aCodecCtx, audio_buf, &data_size, &packet)>0) { |
487 | 568 |
#ifdef DEBUG_AUDIO_FRAMES |
488 |
//fprintf(stderr,"readingQUIAUDIODOPODECODE\n");
|
|
569 |
fprintf(stderr, "newTimeaudioSTART : %lf\n",(double)(packet.pts)*av_q2d(pFormatCtx->streams[audioStream]->time_base));
|
|
489 | 570 |
#endif |
490 | 571 |
if(data_size>0) { |
491 |
/* if a frame has been decoded, output it */ |
|
492 |
//fwrite(audio_buf, 1, data_size, outfileaudio); |
|
493 |
} |
|
494 |
else { |
|
495 | 572 |
#ifdef DEBUG_AUDIO_FRAMES |
496 |
fprintf(stderr, "Skipping illegal audio frame %d. Error data_size!!!!!\n", contFrameAudio);
|
|
573 |
fprintf(stderr, "datasizeaudio:%d\n",data_size);
|
|
497 | 574 |
#endif |
498 |
goto skipaudioframe; |
|
575 |
/* if a frame has been decoded, output it */ |
|
576 |
//fwrite(audio_buf, 1, data_size, outfileaudio); |
|
499 | 577 |
} |
578 |
else |
|
579 |
printf("Error data_size!!\n"); |
|
580 |
|
|
500 | 581 |
audio_size = avcodec_encode_audio(aCodecCtxEnc,outbuf_audio,data_size,audio_buf); |
501 | 582 |
#ifdef DEBUG_AUDIO_FRAMES |
502 |
//fprintf(stderr,"readingQUIAUDIODOPOENCODEE\n"); |
|
503 | 583 |
//fprintf(stderr, "audio frame size nuovo pippo : %d %d datasize:%d %d %d\n",aCodecCtxEnc->frame_size,aCodecCtxEnc->time_base.den,data_size,packet.size, len1); |
504 | 584 |
//fprintf(stderr, "oldaudiosize:%d Audio size : %d\n",len1,audio_size); |
505 | 585 |
//fprintf(stderr, "stream_index:%d flags:%d pos:%d conv_duration:%d dts:%d dts:%d duration:%d\n",packet.stream_index,packet.flags,packet.pos,packet.convergence_duration,packet.dts,packet.dts,packet.duration); |
506 | 586 |
#endif |
507 | 587 |
frame->number = contFrameAudio++;//aCodecCtx->frame_number; |
508 |
|
|
588 |
if(frame->number==0) { |
|
589 |
if(packet.dts==AV_NOPTS_VALUE) |
|
590 |
continue; |
|
591 |
last_pkt_dts_audio = packet.dts; |
|
592 |
newTime = 0; |
|
593 |
} |
|
594 |
else { |
|
595 |
if(packet.dts!=AV_NOPTS_VALUE) { |
|
596 |
delta_audio = packet.dts-last_pkt_dts_audio; |
|
597 |
last_pkt_dts_audio = packet.dts; |
|
598 |
} |
|
599 |
else if(delta_audio==0) |
|
600 |
continue; |
|
601 |
} |
|
602 |
|
|
509 | 603 |
//newtimestamp = (double)((float)packet.dts/(packet.duration*34.100)); |
510 | 604 |
Now=(long long)SDL_GetTicks(); |
605 |
/* |
|
511 | 606 |
if(packet.duration>0) |
512 | 607 |
newtimestamp = ((double)((packet.dts)*aCodecCtxEnc->time_base.num))/((double)(packet.duration*(aCodecCtxEnc->time_base.den))); |
513 | 608 |
else |
514 | 609 |
newtimestamp =((double)((packet.dts)*aCodecCtxEnc->time_base.num)/((double)aCodecCtxEnc->time_base.den)); |
515 |
newtimestamp = (double)((double)packet.dts/(packet.duration*aCodecCtxEnc->time_base.den));
|
|
610 |
newtimestamp = ((double)((packet.dts)*aCodecCtxEnc->time_base.num))/((double)(aCodecCtxEnc->time_base.den));
|
|
516 | 611 |
#ifdef DEBUG_AUDIO_FRAMES |
517 |
//fprintf(stderr, "ts:%llf\n",newtimestamp); |
|
518 |
#endif |
|
612 |
fprintf(stderr, "ts:%llf\n",newtimestamp); |
|
519 | 613 |
//frame->timestamp.tv_sec = (long)packet.dts*1.e-5; |
520 | 614 |
//frame->timestamp.tv_usec = ((long)packet.dts%100000/100); |
521 | 615 |
|
616 |
fprintf(stderr, "dtsaudio:%f %d %d %d\n",(double)packet.dts,(int)packet.duration,aCodecCtxEnc->time_base.den,aCodecCtxEnc->time_base.num); |
|
617 |
#endif |
|
522 | 618 |
if(FirstTimeAudio) { |
523 | 619 |
DeltaTimeAudio=Now-((unsigned int)newtimestamp*(unsigned long long)1000+(newtimestamp-(unsigned int)newtimestamp)*1000); |
524 | 620 |
FirstTimeAudio = 0; |
525 | 621 |
} |
526 | 622 |
|
527 | 623 |
newTime = DeltaTimeAudio+((unsigned int)newtimestamp*(unsigned long long)1000+(newtimestamp-(unsigned int)newtimestamp)*1000); |
528 |
|
|
529 | 624 |
#ifdef DEBUG_AUDIO_FRAMES |
530 | 625 |
//fprintf(stderr, "%lld %lld\n",newTime/1000,newTime%1000); |
531 |
fprintf(stderr, "newTime : %d\n",newTime); |
|
532 |
#endif |
|
533 |
|
|
534 |
if(newTime < 0) { |
|
535 |
#ifdef DEBUG_AUDIO_FRAMES |
|
536 |
fprintf(stderr, "Skipping illegal audio frame %d. Bad timestamp!!!\n", contFrameAudio); |
|
537 | 626 |
#endif |
538 |
goto skipaudioframe; |
|
627 |
*/ |
|
628 |
if(FirstTimeAudio && packet.pts>0) { |
|
629 |
ptsaudio1 = packet.pts; |
|
630 |
FirstTimeAudio = 0; |
|
539 | 631 |
} |
632 |
|
|
633 |
if(frame->number>0) |
|
634 |
newTime = (((double)packet.pts-ptsaudio1)*1000.0*((double)av_q2d(pFormatCtx->streams[audioStream]->time_base)));//*(double)delta_audio; |
|
635 |
|
|
636 |
// newTime = (aCodecCtx->sample_rate/(2*aCodecCtx->channels))*(((double)((packet.pts)-ptsaudio1))/((double)(packet.duration)))*1000.0*(double)pFormatCtx->streams[audioStream]->time_base.num/(double)pFormatCtx->streams[audioStream]->time_base.den; |
|
637 |
// newTime = (double)(packet.pts)*av_q2d(pFormatCtx->streams[audioStream]->time_base); |
|
638 |
|
|
540 | 639 |
|
541 | 640 |
frame->timestamp.tv_sec = (unsigned int)newTime/1000; |
542 | 641 |
frame->timestamp.tv_usec = newTime%1000; |
543 |
#ifdef DEBUG_AUDIO_FRAMES |
|
544 |
//fprintf(stderr,"readingQUIPEPPE\n"); |
|
545 |
//fprintf(stderr, "AUDIO: tvsec:%d tvusec:%d\n",frame->timestamp.tv_sec,frame->timestamp.tv_usec); |
|
642 |
#ifdef DEBUG_AUDIO |
|
643 |
fprintf(stderr, "ptsaudio:%f %d %d %d %f\n",(double)packet.pts,(int)packet.duration,pFormatCtx->streams[audioStream]->time_base.num,pFormatCtx->streams[audioStream]->time_base.den,(double)packet.dts); |
|
644 |
//fprintf(stderr, "newTio : %lf\n",(((double)((packet.pts)-ptsaudio1))/((double)(packet.duration)))*1000.0*(double)pFormatCtx->streams[audioStream]->time_base.num/(double)pFormatCtx->streams[audioStream]->time_base.den); |
|
645 |
|
|
646 |
fprintf(stderr, "AUDIO: tvsec:%d tvusec:%d\n",frame->timestamp.tv_sec,frame->timestamp.tv_usec); |
|
647 |
fprintf(stderr, "newTimeaudio : %d\n",newTime); |
|
648 |
fprintf(stderr, "deltaaudio : %lld\n",delta_audio); |
|
649 |
|
|
546 | 650 |
#endif |
547 | 651 |
//frame->timestamp.tv_sec = (unsigned int)newtimestamp; |
548 | 652 |
//frame->timestamp.tv_usec = (newtimestamp-frame->timestamp.tv_sec)*1000; |
... | ... | |
592 | 696 |
stime=-1; |
593 | 697 |
chunkaudio->priority = 1; |
594 | 698 |
#ifdef DEBUG_AUDIO_FRAMES |
595 |
//fprintf(stderr,"readingQUIPEPPE2\n"); |
|
596 |
#endif |
|
597 | 699 |
//fprintf(stderr, "Frame audio stime:%d frame_num:%d\n",stime,aCodecCtxEnc->frame_number); |
700 |
#endif |
|
598 | 701 |
if(chunkFilled(chunkaudio,cmeta)) { // is chunk filled using current strategy? |
599 | 702 |
//chbAddChunk(chunkbuffer,chunkaudio); // add a chunk to the chunkbuffer |
600 | 703 |
//SAVE ON FILE |
... | ... | |
605 | 708 |
//fprintf(stderr,"readingQUIPEPPE3\n"); |
606 | 709 |
#endif |
607 | 710 |
} |
608 |
skipaudioframe: |
|
609 |
; |
|
610 | 711 |
} |
611 | 712 |
else { |
612 | 713 |
#ifdef DEBUG_AUDIO_FRAMES |
... | ... | |
664 | 765 |
// Close the video file |
665 | 766 |
av_close_input_file(pFormatCtx); |
666 | 767 |
|
667 |
SDL_Quit(); |
|
768 |
SDL_Quit();
|
|
668 | 769 |
return 0; |
669 | 770 |
} |
Also available in: Unified diff