File: seek.c

package info (click to toggle)
gmerlin-avdecoder 2.0.0%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,432 kB
  • sloc: ansic: 89,363; sh: 667; makefile: 650; awk: 43; sed: 16
file content (468 lines) | stat: -rw-r--r-- 13,131 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
/*****************************************************************
 * gmerlin-avdecoder - a general purpose multimedia decoding library
 *
 * Copyright (c) 2001 - 2024 Members of the Gmerlin project
 * http://github.com/bplaum
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * *****************************************************************/



// #define DUMP_SUPERINDEX    
#include <avdec_private.h>
// #include <parser.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define LOG_DOMAIN "seek"

// #define DUMP_ITERATIVE

static int skip_to(bgav_t * b, bgav_track_t * track, int64_t * time, int scale)
  {
  //  fprintf(stderr, "Skip to: %ld\n", *time);
  if(!bgav_track_skipto(track, time, scale))
    {
    b->flags |= BGAV_FLAG_EOF;
    return 0;
    }
  return 1;
  //  fprintf(stderr, "Skipped to: %ld\n", *time);
  }

/* Seek functions with superindex */

static int get_start(bgav_stream_t ** s, int num)
  {
  int i;
  int ret = -1;
  for(i = 0; i < num; i++)
    {
    if(s[i]->action == BGAV_STREAM_MUTE)
      continue;
    
    if((s[i]->type != GAVL_STREAM_AUDIO) &&
       (s[i]->type != GAVL_STREAM_VIDEO))
      continue;
    
    if((ret < 0) || (ret > s[i]->index_position))
      ret = s[i]->index_position;
    }
  return ret;
  }

static int seek_si(bgav_t * b, bgav_demuxer_context_t * ctx,
                   int64_t time, int scale)
  {
  int64_t orig_time;
  uint32_t j;
  bgav_track_t * track;
  bgav_stream_t * s;
  int64_t time_scaled;
  int sample_scale;
    
  track = ctx->tt->cur;
  bgav_track_clear(track);
  
  /* Seek the start chunks indices of all streams */
  
  orig_time = time;
  
  for(j = 0; j < track->num_video_streams; j++)
    {
    s = bgav_track_get_video_stream(track, j);
    
    if(s->action == BGAV_STREAM_MUTE)
      continue;

    gavl_dictionary_get_int(s->m, GAVL_META_STREAM_SAMPLE_TIMESCALE, &sample_scale);
    
    time_scaled = gavl_time_rescale(scale, sample_scale, time);
    
    s->index_position = gavl_packet_index_seek(b->demuxer->si, s->stream_id, time_scaled);
    s->index_position = gavl_packet_index_get_keyframe_before(b->demuxer->si, s->stream_id, s->index_position);

    if(s->index_position < 0)
      {
      gavl_log(GAVL_LOG_ERROR, LOG_DOMAIN, "Seeking failed");
      return 0;
      }
    
    time = gavl_time_rescale(sample_scale, scale, b->demuxer->si->entries[s->index_position].pts);
    
    }
  for(j = 0; j < track->num_streams; j++)
    {
    s = track->streams[j];
    
    if((s->action == BGAV_STREAM_MUTE) ||
       (s->type == GAVL_STREAM_VIDEO) ||
       (s->flags & STREAM_EXTERN))
      continue;
    
    gavl_dictionary_get_int(s->m, GAVL_META_STREAM_SAMPLE_TIMESCALE, &sample_scale);
    time_scaled = gavl_time_rescale(scale, sample_scale, time);
    
    /* Handle preroll */
    if((s->type == GAVL_STREAM_AUDIO) && s->data.audio.sync_samples)
      time_scaled -= s->data.audio.sync_samples;

    if((s->stats.pts_start != GAVL_TIME_UNDEFINED) &&
       (time_scaled < s->stats.pts_start))
      time_scaled = s->stats.pts_start;
    
    s->index_position = gavl_packet_index_seek(b->demuxer->si, s->stream_id, time_scaled);
    
    if(s->index_position < 0)
      {
      gavl_log(GAVL_LOG_ERROR, LOG_DOMAIN, "Seeking failed");
      return 0;
      }
    
    }
  
  /* Find the start packet */

  if(!(ctx->flags & BGAV_DEMUXER_NONINTERLEAVED))
    {
    ctx->index_position = get_start(track->streams, track->num_streams);
    bgav_input_seek(b->input, ctx->si->entries[ctx->index_position].position, SEEK_SET);
    }

  for(j = 0; j < track->num_streams; j++)
    {
    s = track->streams[j];

    if((s->action == BGAV_STREAM_MUTE) ||
       (s->flags & STREAM_EXTERN))
      continue;

    if(!(ctx->flags & BGAV_DEMUXER_NONINTERLEAVED))
      s->index_position = gavl_packet_index_get_next_keyframe(ctx->si, s->stream_id, ctx->index_position);
    
    STREAM_SET_SYNC(s, b->demuxer->si->entries[s->index_position].pts);
    }
  
  bgav_track_resync(track);
  return skip_to(b, track, &orig_time, scale);
  }


static int seek_once(bgav_t * b, int64_t * time, int scale)
  {
  bgav_track_t * track = b->tt->cur;
  int64_t sync_time;

  bgav_track_clear(track);
  b->demuxer->demuxer->seek(b->demuxer, *time, scale);
  /* Re-sync decoders */

  bgav_track_resync(track);
  sync_time = bgav_track_sync_time(track, scale);

  if(*time > sync_time)
    return skip_to(b, track, time, scale);
  else
    {
    if(!skip_to(b, track, &sync_time, scale))
      return 0;
    *time = sync_time;
    return 1;
    }
  
  //  return sync_time;
  }

typedef struct
  {
  double percentage;
  int64_t sync_time;
  } seek_tab_t;

static int64_t seek_test(bgav_t * b, int64_t filepos, int scale)
  {
  bgav_track_clear(b->tt->cur);
  bgav_input_seek(b->input, filepos, SEEK_SET);

  if(!b->demuxer->demuxer->post_seek_resync(b->demuxer))
    return GAVL_TIME_UNDEFINED; // EOF

  return bgav_track_sync_time(b->tt->cur, scale);
  }

/* Time can have an arbitrary scale but the zero point must be the same
   as the stream native timestamps */

static int64_t position_from_percentage(bgav_t * b,
                                        int64_t total_bytes,
                                        double percentage)
  {
  int64_t position;
  position = b->tt->cur->data_start + (int64_t)(percentage * (double)total_bytes);
  if(position > b->tt->cur->data_start + total_bytes)
    position = b->tt->cur->data_start + total_bytes;

  if(b->input->block_size)
    {
    int rest = position % b->input->block_size;
    position -= rest;
    }
  return position;
  }

static int seek_generic(bgav_t * b, int64_t * time, int scale)
  {
  int i;
  
  gavl_time_t start_time;
  gavl_time_t end_time;
  
  int64_t position;
  int64_t sync_time;
  
  seek_tab_t tab[2];
  int64_t total_bytes;
  double percentage;
  int num_seeks = 0;
  int64_t offset = gavl_time_scale(scale, GAVL_TIME_SCALE);
  
  if(b->tt->cur->data_end > 0)
    total_bytes = b->tt->cur->data_end - b->tt->cur->data_start;
  else
    total_bytes = b->input->total_bytes - b->tt->cur->data_start;

  start_time = gavl_track_get_start_time(b->tt->cur->info);
  end_time = start_time + gavl_track_get_duration(b->tt->cur->info);
  
  tab[0].sync_time = gavl_time_scale(scale, start_time);
  tab[0].percentage  = 0.0;
  tab[1].sync_time = gavl_time_scale(scale, end_time);
  tab[1].percentage  = 1.0;
  
  for(i = 0; i < 6; i++)
    {
    /* Bisection search */
    percentage = tab[0].percentage +
      (tab[1].percentage - tab[0].percentage) *
      (double)(*time - tab[0].sync_time - offset) / (double)(tab[1].sync_time - tab[0].sync_time);
    
    if(percentage < tab[0].percentage)
      percentage = tab[0].percentage;
    
    position = position_from_percentage(b, total_bytes, percentage);
    sync_time = seek_test(b, position, scale);
    num_seeks++;
    if(sync_time == GAVL_TIME_UNDEFINED)
      {
      gavl_log(GAVL_LOG_ERROR, LOG_DOMAIN, "Failed to re-sync during iterative seek");
      return 0;
      }
    if(sync_time < *time)
      {
      /* Return if we are less than 1 second before the seek point */
      if(gavl_time_unscale(scale, *time - sync_time) <= GAVL_TIME_SCALE)
        break;
      
      tab[0].percentage = percentage;
      tab[0].sync_time = sync_time;
      }
    /* Direct hit (unlikely) */
    else if(sync_time == *time)
      break;
    /* Upper boundary */
    else
      {
      tab[1].percentage = percentage;
      tab[1].sync_time = sync_time;
      }
    }

  if(sync_time == tab[1].sync_time)
    {
    /* Go to lower boundary instead */
    position = position_from_percentage(b, total_bytes, tab[0].percentage);
    sync_time = seek_test(b, position, scale);
    num_seeks++;
    }
  
  gavl_log(GAVL_LOG_INFO, LOG_DOMAIN, "Seek generic: Seeks: %d, goal: %"PRId64", reached: %"PRId64" diff: %"PRId64,
           num_seeks, *time, sync_time, *time - sync_time);
  
  bgav_track_resync(b->tt->cur);

  if(*time > sync_time)
    return skip_to(b, b->tt->cur, time, scale);
  
  *time = sync_time;
  return 1;
  }

static int seek_input(bgav_t * b, int64_t * time, int scale)
  {
  gavl_time_t seek_time;
  //  gavl_time_t time_offset = gavl_track_get_display_time_offset(b->tt->cur->info);
  
  bgav_track_clear(b->tt->cur);

  seek_time = gavl_time_unscale(scale, *time);

  if(!bgav_input_seek_time(b->input, seek_time))
    return 0;
  bgav_track_resync(b->tt->cur);
  //  skip_to(b, b->tt->cur, time, scale);
  return 1;
  }

int bgav_ensure_index(bgav_t * b)
  {
  if(b->demuxer->si)
    return 1;
  
  if(b->demuxer->index_mode == INDEX_MODE_SIMPLE)
    {
    /* Build packet index */
    const char * location = NULL;

    if(!gavl_metadata_get_src(&b->input->m, GAVL_META_SRC, 0, NULL, &location) |
       !location)
      return 0;
    
    if((b->demuxer->si = bgav_get_packet_index(location)))
      {
      //      gavl_dprintf("Built packet index:\n");
      //      gavl_packet_index_dump(b->demuxer->si);
      return 1;
      }
    else
      {
      gavl_log(GAVL_LOG_ERROR, LOG_DOMAIN, "Building packet index failed");
      return 0;
      }
    }
  gavl_log(GAVL_LOG_ERROR, LOG_DOMAIN, "Cannot build packet index (unsupported file format)");
  return 0;
  }

int
bgav_seek_scaled(bgav_t * b, int64_t * time, int scale)
  {
  bgav_track_t * track = b->tt->cur;

  if(b->flags & BGAV_FLAG_PAUSED)
    {
    gavl_log(GAVL_LOG_ERROR, LOG_DOMAIN, "bgav_seek_scaled failed: Decoder paused");
    return 0;
    }
  
  //  fprintf(stderr, "bgav_seek_scaled: %f\n",
  //          gavl_time_to_seconds(gavl_time_unscale(scale, *time)));
  
  /* Clear EOF */

  bgav_track_clear_eof_d(track);
  b->flags &= ~BGAV_FLAG_EOF;
  
  
  /*
   * Seek with superindex
   *
   * This must be checked *before* we check for seek_sa because
   * AVIs with mp3 audio will also have b->tt->cur->sample_accurate = 1
   */

  if(b->opt.sample_accurate && !(b->demuxer->flags & BGAV_DEMUXER_SAMPLE_ACCURATE))
    {
    if(!bgav_ensure_index(b))
      gavl_log(GAVL_LOG_WARNING, LOG_DOMAIN, "Sample accurate seeking not supported for format");
    }
    
  if(b->demuxer->si)
    return seek_si(b, b->demuxer, *time, scale);
  else if(b->input->flags & BGAV_INPUT_CAN_SEEK_TIME)
    {
    return seek_input(b, time, scale);
    }
  /* Seek once */
  else if(b->demuxer->demuxer->seek)
    return seek_once(b, time, scale);
  /* Seek iterative */
  else if(b->demuxer->demuxer->post_seek_resync)
    return seek_generic(b, time, scale);
  /* Build seek index */
  else if((b->demuxer->index_mode == INDEX_MODE_SIMPLE) &&
          (b->input->flags & BGAV_INPUT_CAN_SEEK_BYTE))
    {
    if(!bgav_ensure_index(b) || !b->demuxer->si)
      {
      gavl_log(GAVL_LOG_ERROR, LOG_DOMAIN, "Building packet index failed");
      return 0;
      }
    return seek_si(b, b->demuxer, *time, scale);
    }
  
  gavl_log(GAVL_LOG_ERROR, LOG_DOMAIN, "Don't know how to seek");
  return 0;
  }

int
bgav_seek_to_video_frame(bgav_t * b, int stream, int frame)
  {
  int64_t pts;

  /* Frame -> Time */
  bgav_stream_t * s = bgav_track_get_video_stream(b->tt->cur, stream);
  if(s->data.video.format->framerate_mode == GAVL_FRAMERATE_CONSTANT)
    pts = s->stats.pts_start + frame * s->data.video.format->frame_duration;
  else if(!(s->ci->flags & GAVL_COMPRESSION_HAS_B_FRAMES))
    {
    pts = gavl_packet_index_packet_number_to_pts(b->demuxer->si,
                                                 s->stream_id,
                                                 frame);
    if(pts == GAVL_TIME_UNDEFINED)
      return 0;
    }
  else /* B-frames and nonconstant framerate */
    {
    if(!s->data.video.frame_table)
      {
      if(!bgav_ensure_index(b))
        return 0;
      
      s->data.video.frame_table = gavl_packet_index_create(0);
      
      gavl_packet_index_extract_stream(b->demuxer->si,
                                       s->data.video.frame_table,
                                       s->stream_id);
      gavl_packet_index_sort_by_pts(s->data.video.frame_table);
      }

    if((frame < 0) || (frame >= s->data.video.frame_table->num_entries))
      {
      gavl_log(GAVL_LOG_ERROR, LOG_DOMAIN, "Frame index %d out of range (must be between zero and %"PRId64")",
               frame, bgav_get_num_video_frames(b, stream));
      return 0;
      }
    pts = s->data.video.frame_table->entries[frame].pts;
    }
  return bgav_seek_scaled(b, &pts, s->data.video.format->timescale);
  }

void
bgav_seek(bgav_t * b, gavl_time_t * time)
  {
  bgav_seek_scaled(b, time, GAVL_TIME_SCALE);
  }