seek.h 4.92 KB
Newer Older
1
/*
2
 * seek utility functions for use within format handlers
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
 *
 * Copyright (c) 2009 Ivan Schreter
 *
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * FFmpeg 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with FFmpeg; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#ifndef AVFORMAT_SEEK_H
#define AVFORMAT_SEEK_H

#include "avformat.h"

28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
/**
 * structure to store parser state of one AVStream
 */
typedef struct AVParserStreamState {
    // saved members of AVStream
    AVCodecParserContext   *parser;
    int64_t                 last_IP_pts;
    int64_t                 cur_dts;
    int64_t                 reference_dts;
    int                     probe_packets;
} AVParserStreamState;

/**
 * structure to store parser state of AVFormat
 */
typedef struct AVParserState {
    int64_t         fpos;                   ///< file position at the time of call

    // saved members of AVFormatContext
    AVPacketList   *packet_buffer;          ///< packet buffer of original state
48
    AVPacketList   *parse_queue;            ///< parse queue of original state
49 50 51 52 53 54 55
    AVPacketList   *raw_packet_buffer;      ///< raw packet buffer of original state
    int raw_packet_buffer_remaining_size;   ///< remaining space in raw_packet_buffer

    // saved info for streams
    int                  nb_streams;        ///< number of streams with stored state
    AVParserStreamState *stream_states;     ///< states of individual streams (array)
} AVParserState;
56 57

/**
58
 * Search for the sync point of all active streams.
59
 *
60
 * This routine is not supposed to be called directly by a user application,
61 62
 * but by demuxers.
 *
63 64 65 66
 * A sync point is defined as a point in stream, such that, when decoding start
 * from this point, the decoded output of all streams synchronizes closest
 * to the given timestamp ts. This routine also takes timestamp limits into account.
 * Thus, the output will synchronize no sooner than ts_min and no later than ts_max.
67
 *
68 69 70 71 72 73 74 75 76 77
 * @param stream_index stream index for time base reference of timestamps
 * @param pos          approximate position where to start searching for key frames
 * @param min_ts       minimum allowed timestamp (position, if AVSEEK_FLAG_BYTE set)
 * @param ts           target timestamp (or position, if AVSEEK_FLAG_BYTE set in flags)
 * @param max_ts       maximum allowed timestamp (position, if AVSEEK_FLAG_BYTE set)
 * @param flags        if AVSEEK_FLAG_ANY is set, seek to any frame, otherwise only
 *                     to a keyframe. If AVSEEK_FLAG_BYTE is set, search by
 *                     position, not by timestamp.
 * @return -1 if no such sync point could be found, otherwise stream position
 *                     (stream is repositioned to this position)
78 79 80 81 82 83 84 85 86 87 88 89
 */
int64_t ff_gen_syncpoint_search(AVFormatContext *s,
                                int stream_index,
                                int64_t pos,
                                int64_t min_ts,
                                int64_t ts,
                                int64_t max_ts,
                                int flags);

/**
 * Store current parser state and file position.
 *
90 91 92
 * This function can be used by demuxers before a destructive seeking algorithm
 * to store the parser state. Depending on the outcome of the seek, either the original
 * state can be restored or the new state kept and the original state freed.
93
 *
94 95 96
 * @note As a side effect, the original parser state is reset, since structures
 *       are relinked to the stored state instead of being deeply-copied (for
 *       performance reasons and to keep the code simple).
97
 *
98 99
 * @param s context from which to save state
 * @return parser state object or NULL if memory could not be allocated
100 101 102 103 104 105 106
 */
AVParserState *ff_store_parser_state(AVFormatContext *s);

/**
 * Restore previously saved parser state and file position.
 *
 * Saved state will be invalidated and freed by this call, since internal
107
 * structures will be relinked back to the stored state instead of being
108 109
 * deeply-copied.
 *
110 111
 * @param s     context to which to restore state (same as used for storing state)
 * @param state state to restore
112 113 114 115 116 117
 */
void ff_restore_parser_state(AVFormatContext *s, AVParserState *state);

/**
 * Free previously saved parser state.
 *
118 119
 * @param s     context to which the state belongs (same as used for storing state)
 * @param state state to free
120 121 122 123
 */
void ff_free_parser_state(AVFormatContext *s, AVParserState *state);

#endif /* AVFORMAT_SEEK_H */