Guest User

Untitled

a guest
Jan 24th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.33 KB | None | 0 0
  1. clpi_lookup_spn(CLPI_CL *cl, uint32_t timestamp, int before, uint8_t stc_id)
  2. {
  3.     CLPI_EP_MAP_ENTRY *entry;
  4.     CLPI_CPI *cpi = &cl->cpi;
  5.     int ii, jj;
  6.     uint32_t coarse_pts, pts; // 45khz timestamps
  7.     uint32_t spn, coarse_spn, stc_spn;
  8.     int start, end;
  9.     int ref;
  10.  
  11.     if (cpi->num_stream_pid < 1 || !cpi->entry) {
  12.         if (before) {
  13.             return 0;
  14.         }
  15.         return cl->clip.num_source_packets;
  16.     }
  17.  
  18.     // Assumes that there is only one pid of interest
  19.     entry = &cpi->entry[0];
  20.  
  21.     // Use sequence info to find spn_stc_start before doing
  22.     // PTS search. The spn_stc_start defines the point in
  23.     // the EP map to start searching.
  24.     stc_spn = _find_stc_spn(cl, stc_id);
  25.     for (ii = 0; ii < entry->num_ep_coarse; ii++) {
  26.         ref = entry->coarse[ii].ref_ep_fine_id;
  27.         if (entry->coarse[ii].spn_ep >= stc_spn) {
  28.             // The desired starting point is either after this point
  29.             // or in the middle of the previous coarse entry
  30.             break;
  31.         }
  32.     }
  33.     if (ii >= entry->num_ep_coarse) {
  34.         return cl->clip.num_source_packets;
  35.     }
  36.     pts = ((uint64_t)(entry->coarse[ii].pts_ep & ~0x01) << 18) +
  37.           ((uint64_t)entry->fine[ref].pts_ep << 8);
  38.     if (pts > timestamp && ii) {
  39.         // The starting point and desired PTS is in the previous coarse entry
  40.         ii--;
  41.         coarse_pts = (uint32_t)(entry->coarse[ii].pts_ep & ~0x01) << 18;
  42.         coarse_spn = entry->coarse[ii].spn_ep;
  43.         start = entry->coarse[ii].ref_ep_fine_id;
  44.         end = entry->coarse[ii+1].ref_ep_fine_id;
  45.         // Find a fine entry that has bothe spn > stc_spn and ptc > timestamp
  46.         for (jj = start; jj < end; jj++) {
  47.  
  48.             pts = coarse_pts + ((uint32_t)entry->fine[jj].pts_ep << 8);
  49.             spn = (coarse_spn & ~0x1FFFF) + entry->fine[jj].spn_ep;
  50.             if (stc_spn >= spn && pts > timestamp)
  51.                 break;
  52.         }
  53.         goto done;
  54.     }
  55.  
  56.     // If we've gotten this far, the desired timestamp is somewhere
  57.     // after the coarse entry we found the stc_spn in.
  58.     start = ii;
  59.     for (ii = start; ii < entry->num_ep_coarse; ii++) {
  60.         ref = entry->coarse[ii].ref_ep_fine_id;
  61.         pts = ((uint64_t)(entry->coarse[ii].pts_ep & ~0x01) << 18) +
  62.                 ((uint64_t)entry->fine[ref].pts_ep << 8);
  63.         if (pts > timestamp) {
  64.             break;
  65.         }
  66.     }
  67.     // If the timestamp is before the first entry, then return
  68.     // the beginning of the clip
  69.     if (ii == 0) {
  70.         return 0;
  71.     }
  72.     ii--;
  73.     coarse_pts = (uint32_t)(entry->coarse[ii].pts_ep & ~0x01) << 18;
  74.     start = entry->coarse[ii].ref_ep_fine_id;
  75.     if (ii < entry->num_ep_coarse - 1) {
  76.         end = entry->coarse[ii+1].ref_ep_fine_id;
  77.     } else {
  78.         end = entry->num_ep_fine;
  79.     }
  80.     for (jj = start; jj < end; jj++) {
  81.  
  82.         pts = coarse_pts + ((uint32_t)entry->fine[jj].pts_ep << 8);
  83.         if (pts > timestamp)
  84.             break;
  85.     }
  86.  
  87. done:
  88.     if (before) {
  89.         jj--;
  90.     }
  91.     if (jj == end) {
  92.         ii++;
  93.         if (ii >= entry->num_ep_coarse) {
  94.             // End of file
  95.             return cl->clip.num_source_packets;
  96.         }
  97.         jj = entry->coarse[ii].ref_ep_fine_id;
  98.     }
  99.     spn = (entry->coarse[ii].spn_ep & ~0x1FFFF) + entry->fine[jj].spn_ep;
  100.     return spn;
  101. }
Add Comment
Please, Sign In to add comment