Ticket #6981: replex_fix_sync.3.diff
File replex_fix_sync.3.diff, 10.4 KB (added by , 15 years ago) |
---|
-
mythtv/programs/mythtranscode/replex/element.h
124 124 uint32_t mode_extension; 125 125 uint32_t emphasis; 126 126 uint32_t framesize; 127 uint32_t frametime; 127 128 uint32_t off; 128 129 129 130 } audio_frame_t; -
mythtv/programs/mythtranscode/replex/replex.c
41 41 #include "replex.h" 42 42 #include "pes.h" 43 43 44 #include "avcodec.h" 45 #include "avformat.h" 46 44 47 #ifndef O_LARGEFILE 45 48 #define O_LARGEFILE 0 46 49 #endif … … 78 81 return -1; 79 82 } 80 83 84 int encode_mp2_audio(audio_frame_t *aframe, uint8_t *buffer, int bufsize) 85 { 86 AVCodec *codec; 87 AVCodecContext *c= NULL; 88 int frame_size, j, out_size; 89 short *samples; 90 91 fprintf(stderr, "encoding an MP2 audio frame\n"); 81 92 93 /* find the MP2 encoder */ 94 codec = avcodec_find_encoder(CODEC_ID_MP2); 95 if (!codec) { 96 fprintf(stderr, "codec not found\n"); 97 return 1; 98 } 99 100 c = avcodec_alloc_context(); 101 102 /* put sample parameters */ 103 c->bit_rate = aframe->bit_rate; 104 c->sample_rate = aframe->frequency; 105 c->channels = 2; 106 107 /* open it */ 108 if (avcodec_open(c, codec) < 0) { 109 fprintf(stderr, "could not open codec\n"); 110 av_free(c); 111 return 1; 112 } 113 114 /* the codec gives us the frame size, in samples */ 115 frame_size = c->frame_size; 116 samples = malloc(frame_size * 2 * c->channels); 117 118 /* create samples for a single blank frame */ 119 for (j=0;j<frame_size;j++) { 120 samples[2*j] = 0; 121 samples[2*j+1] = 0; 122 } 123 124 /* encode the samples */ 125 out_size = avcodec_encode_audio(c, buffer, bufsize, samples); 126 127 if (out_size != bufsize) { 128 fprintf(stderr, "frame size (%d) does not equal required size (%d)?\n", 129 out_size, bufsize); 130 free(samples); 131 avcodec_close(c); 132 av_free(c); 133 return 1; 134 } 135 136 free(samples); 137 avcodec_close(c); 138 av_free(c); 139 140 return 0; 141 } 142 82 143 static int audio_jump(struct replex *rx) 83 144 { 84 145 int i; … … 255 316 return c; 256 317 } 257 318 258 if ( aframe->framesize > diff){319 if ((int) aframe->framesize > diff){ 259 320 if ( re == -3){ 260 c += pos+1;321 c += pos+1; 261 322 return c; 262 323 } 263 324 … … 268 329 return c; 269 330 } 270 331 } 271 if ( aframe->framesize > diff){332 if ((int) aframe->framesize > diff){ 272 333 c += pos+2; 273 334 //fprintf(stderr,"WRONG HEADER2 %d\n", diff); 274 335 return c; 275 336 } 276 337 } 277 338 278 339 // try to fix audio sync - only works for mpeg audio for now 340 if (aframe->set && rx->fix_sync && first && type == MPEG_AUDIO){ 341 int frame_time = aframe->frametime; 342 int64_t diff; 343 diff = ptsdiff(trans_pts_dts(p->pts), add_pts_audio(0, aframe,*acount + 1) + *fpts); 344 if (abs ((int)diff) >= frame_time){ 345 fprintf(stderr,"fixing audio PTS inconsistency - diff: "); 346 printpts(abs(diff)); 347 348 if (diff < 0){ 349 diff = abs(diff); 350 int framesdiff = diff / frame_time; 351 fprintf(stderr, " - need to remove %d frame(s)\n", framesdiff); 352 353 // FIXME can only remove one frame at a time for now 354 if (framesdiff > 1) 355 framesdiff = 1; 356 iu->pts = add_pts_audio(0, aframe, -framesdiff); 357 c += aframe->framesize; 358 } else { 359 int framesdiff = diff / frame_time; 360 fprintf(stderr, " - need to add %d frame(s)\n", framesdiff); 361 362 // limit inserts to a maximum of 5 frames 363 if (framesdiff > 5) 364 framesdiff = 5; 365 366 // alloc memmory for audio frame 367 uint8_t *framebuf; 368 if ( !(framebuf = (uint8_t *) malloc(sizeof(uint8_t) * aframe->framesize))) { 369 fprintf(stderr,"Not enough memory for audio frame\n"); 370 exit(1); 371 } 372 373 // try to encode a blank frame 374 if (encode_mp2_audio(aframe, framebuf, sizeof(uint8_t) * aframe->framesize) != 0) { 375 // encode failed so just use a copy of the current frame 376 int res; 377 res = ring_peek(rbuf, framebuf, aframe->framesize, 0); 378 if (res != (int) aframe->framesize) { 379 fprintf(stderr,"ring buffer failed to peek frame res: %d\n", res); 380 exit(1); 381 } 382 } 383 384 // add each extra frame required direct to the output file 385 int x; 386 for (x = 0; x < framesdiff; x++){ 387 if (type == AC3) 388 write(rx->dmx_out[pos+1+rx->apidn], framebuf, aframe->framesize); 389 else 390 write(rx->dmx_out[pos+1], framebuf, aframe->framesize); 391 *acount += 1; 392 } 393 394 free(framebuf); 395 } 396 } 397 } 398 279 399 if (aframe->set){ 280 400 if(iu->active){ 281 401 iu->length = ring_posdiff(rbuf, … … 480 600 iu->start = (p->ini_pos+pos+c)%bsize; 481 601 } 482 602 c += pos; 483 if (c + aframe->framesize > len){603 if (c + (int) aframe->framesize > len){ 484 604 // fprintf(stderr,"SHORT %d\n", len -c); 485 605 c = len; 486 606 } else { … … 1330 1450 for (i=0; i<rx->apidn;i++){ 1331 1451 if ((aavail = ring_avail(&rx->index_arbuffer[i]) 1332 1452 /sizeof(index_unit)) < LIMIT) 1333 if (fill < ring_free(&rx->arbuffer[i]))1453 if (fill < (int) ring_free(&rx->arbuffer[i])) 1334 1454 fill = ring_free(&rx->arbuffer[i]); 1335 1455 } 1336 1456 1337 1457 for (i=0; i<rx->ac3n;i++){ 1338 1458 if ((ac3avail = ring_avail(&rx->index_ac3rbuffer[i]) 1339 1459 /sizeof(index_unit)) < LIMIT) 1340 if (fill < ring_free(&rx->ac3rbuffer[i]))1460 if (fill < (int) ring_free(&rx->ac3rbuffer[i])) 1341 1461 fill = ring_free(&rx->ac3rbuffer[i]); 1342 1462 } 1343 1463 … … 1364 1484 uint16_t vpid=0, apid=0, ac3pid=0; 1365 1485 1366 1486 fprintf(stderr,"Trying to find PIDs\n"); 1367 while (!afound && !vfound && count < rx->inflength){1487 while (!afound && !vfound && count < (int) rx->inflength){ 1368 1488 if (rx->vpid) vfound = 1; 1369 1489 if (rx->apidn) afound = 1; 1370 1490 if ((re = save_read(rx,buf,IN_SIZE))<0) … … 1424 1544 memset (ac3pid , 0 , MAXAC3PID*sizeof(uint16_t)); 1425 1545 1426 1546 fprintf(stderr,"Trying to find PIDs\n"); 1427 while (count < rx->inflength-IN_SIZE){1547 while (count < (int) rx->inflength-IN_SIZE){ 1428 1548 if ((re = save_read(rx,buf,IN_SIZE))<0) 1429 1549 perror("reading"); 1430 1550 else … … 2489 2609 printf (" --input_stream, -i <string> : set input stream type (string = TS(default), PS, AVI)\n"); 2490 2610 printf (" --allow_jump, -j : allow jump in the PTS and try repair\n"); 2491 2611 printf (" --keep_PTS, -k : keep and don't correct PTS information of original\n"); 2612 printf (" --fix_sync, -n : try to fix audio sync while demuxing\n"); 2492 2613 printf (" --min_jump, -l <integer> : don't try to fix jumps in PTS larger than <int> but treat them as a cut (default 100ms)\n"); 2493 2614 printf (" --of, -o <filename> : set output file\n"); 2494 2615 printf (" --fillzero -p : fill audio frames with zeros (only MPEG AUDIO)\n"); … … 2534 2655 {"input_stream", required_argument, NULL, 'i'}, 2535 2656 {"allow_jump",required_argument, NULL, 'j'}, 2536 2657 {"keep_PTS",required_argument, NULL, 'k'}, 2658 {"fix_sync",no_argument, NULL, 'n'}, 2537 2659 {"min_jump",required_argument, NULL, 'l'}, 2538 2660 {"of",required_argument, NULL, 'o'}, 2539 2661 {"fillzero",required_argument, NULL, 'p'}, … … 2547 2669 {0, 0, 0, 0} 2548 2670 }; 2549 2671 c = getopt_long (argc, argv, 2550 "a:c:d:e:fg:hi:jkl:o:pq:st:v:xy:z",2672 "t:o:a:v:g:i:hp:q:d:c:n:fkd:e:zy:sx", 2551 2673 long_options, &option_index); 2552 2674 if (c == -1) 2553 2675 break; … … 2624 2746 case 'z': 2625 2747 rx.demux = 1; 2626 2748 break; 2749 case 'n': 2750 rx.fix_sync =1; 2751 break; 2627 2752 case 'h': 2628 2753 case '?': 2629 2754 default: … … 2631 2756 } 2632 2757 } 2633 2758 2759 if (rx.fix_sync) 2760 av_register_all(); 2761 2634 2762 if (rx.allow_jump && min_jump) rx.allow_jump = min_jump; 2635 2763 2636 2764 if (fillzero) rx.fillzero = 1; -
mythtv/programs/mythtranscode/replex/replex.h
49 49 int otype; 50 50 int ignore_pts; 51 51 int keep_pts; 52 int fix_sync; 52 53 uint64_t allow_jump; 53 54 uint64_t inflength; 54 55 uint64_t finread; -
mythtv/programs/mythtranscode/replex/element.c
623 623 } 624 624 af->off = c; 625 625 af->set = 1; 626 af->frametime = ((samples [3-af->layer] * 27000000ULL) / af->frequency); 626 627 af->framesize = calculate_mpg_framesize(af); 627 628 //af->framesize = af->bit_rate *slots [3-af->layer]/ af->frequency; 628 629 if (DEBUG && verb) fprintf(stderr," frame size: %d \n", af->framesize); … … 675 676 676 677 af->off = c; 677 678 af->set = 1; 678 return c; 679 680 //FIXME calculate frametime 681 af->frametime = 0; 682 683 return c; 679 684 } 680 685 681 686 -
mythplugins/mytharchive/mythburn/scripts/mythburn.py
2669 2669 def deMultiplexMPEG2File(folder, mediafile, video, audio1, audio2): 2670 2670 2671 2671 if getFileType(folder) == "mpegts": 2672 command = "mythreplex - -demux --fix_sync -t TS -o %s " % (folder + "/stream")2672 command = "mythreplex -g 12 --demux --fix_sync -t TS -o %s " % (folder + "/stream") 2673 2673 command += "-v %d " % (video[VIDEO_ID]) 2674 2674 2675 2675 if audio1[AUDIO_ID] != -1: … … 2685 2685 command += "-c %d " % (audio2[AUDIO_ID]) 2686 2686 2687 2687 else: 2688 command = "mythreplex - -demux --fix_sync -o %s " % (folder + "/stream")2688 command = "mythreplex -g 12 --demux --fix_sync -o %s " % (folder + "/stream") 2689 2689 command += "-v %d " % (video[VIDEO_ID] & 255) 2690 2690 2691 2691 if audio1[AUDIO_ID] != -1: