~ubuntu-branches/ubuntu/vivid/mpv/vivid

« back to all changes in this revision

Viewing changes to player/misc.c

  • Committer: Package Import Robot
  • Author(s): Alessandro Ghedini
  • Date: 2013-12-29 20:04:26 UTC
  • mfrom: (1.1.9)
  • Revision ID: package-import@ubuntu.com-20131229200426-w0qsj8clnui1pxaw
Tags: 0.3.0-1
* New upstream release
  - Fix --vf=expand example in manpage (Closes: #732271)
* Add 03_waf.patch to provide uncompressed waf scripts and modules
* Switch to waf build script
* Drop libmng-dev Build-Depends (not used anymore)
* Bump Standards-Version to 3.9.5 (no changes needed)
* Enable support for dvdnav
* Install more docs

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * This file is part of MPlayer.
 
3
 *
 
4
 * MPlayer is free software; you can redistribute it and/or modify
 
5
 * it under the terms of the GNU General Public License as published by
 
6
 * the Free Software Foundation; either version 2 of the License, or
 
7
 * (at your option) any later version.
 
8
 *
 
9
 * MPlayer is distributed in the hope that it will be useful,
 
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
 * GNU General Public License for more details.
 
13
 *
 
14
 * You should have received a copy of the GNU General Public License along
 
15
 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
 
16
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 
17
 */
 
18
 
 
19
#include <stddef.h>
 
20
#include <stdbool.h>
 
21
#include <assert.h>
 
22
 
 
23
#include "config.h"
 
24
#include "talloc.h"
 
25
 
 
26
#include "osdep/io.h"
 
27
#include "osdep/timer.h"
 
28
 
 
29
#include "common/msg.h"
 
30
#include "options/options.h"
 
31
#include "options/m_property.h"
 
32
#include "common/common.h"
 
33
#include "common/encode.h"
 
34
#include "common/playlist.h"
 
35
#include "input/input.h"
 
36
 
 
37
#include "audio/out/ao.h"
 
38
#include "demux/demux.h"
 
39
#include "stream/stream.h"
 
40
#include "video/out/vo.h"
 
41
 
 
42
#include "core.h"
 
43
#include "command.h"
 
44
 
 
45
double get_relative_time(struct MPContext *mpctx)
 
46
{
 
47
    int64_t new_time = mp_time_us();
 
48
    int64_t delta = new_time - mpctx->last_time;
 
49
    mpctx->last_time = new_time;
 
50
    return delta * 0.000001;
 
51
}
 
52
 
 
53
double rel_time_to_abs(struct MPContext *mpctx, struct m_rel_time t,
 
54
                       double fallback_time)
 
55
{
 
56
    double length = get_time_length(mpctx);
 
57
    switch (t.type) {
 
58
    case REL_TIME_ABSOLUTE:
 
59
        return t.pos;
 
60
    case REL_TIME_NEGATIVE:
 
61
        if (length != 0)
 
62
            return MPMAX(length - t.pos, 0.0);
 
63
        break;
 
64
    case REL_TIME_PERCENT:
 
65
        if (length != 0)
 
66
            return length * (t.pos / 100.0);
 
67
        break;
 
68
    case REL_TIME_CHAPTER:
 
69
        if (chapter_start_time(mpctx, t.pos) >= 0)
 
70
            return chapter_start_time(mpctx, t.pos);
 
71
        break;
 
72
    }
 
73
    return fallback_time;
 
74
}
 
75
 
 
76
double get_play_end_pts(struct MPContext *mpctx)
 
77
{
 
78
    struct MPOpts *opts = mpctx->opts;
 
79
    if (opts->play_end.type) {
 
80
        return rel_time_to_abs(mpctx, opts->play_end, MP_NOPTS_VALUE);
 
81
    } else if (opts->play_length.type) {
 
82
        double startpts = get_start_time(mpctx);
 
83
        double start = rel_time_to_abs(mpctx, opts->play_start, startpts);
 
84
        double length = rel_time_to_abs(mpctx, opts->play_length, -1);
 
85
        if (start != -1 && length != -1)
 
86
            return start + length;
 
87
    }
 
88
    return MP_NOPTS_VALUE;
 
89
}
 
90
 
 
91
// Time used to seek external tracks to.
 
92
double get_main_demux_pts(struct MPContext *mpctx)
 
93
{
 
94
    double main_new_pos = MP_NOPTS_VALUE;
 
95
    if (mpctx->demuxer) {
 
96
        for (int n = 0; n < mpctx->demuxer->num_streams; n++) {
 
97
            if (main_new_pos == MP_NOPTS_VALUE)
 
98
                main_new_pos = demux_get_next_pts(mpctx->demuxer->streams[n]);
 
99
        }
 
100
    }
 
101
    return main_new_pos;
 
102
}
 
103
 
 
104
double get_start_time(struct MPContext *mpctx)
 
105
{
 
106
    struct demuxer *demuxer = mpctx->demuxer;
 
107
    if (!demuxer)
 
108
        return 0;
 
109
    return demuxer_get_start_time(demuxer);
 
110
}
 
111
 
 
112
int mp_get_cache_percent(struct MPContext *mpctx)
 
113
{
 
114
    if (mpctx->stream) {
 
115
        int64_t size = -1;
 
116
        int64_t fill = -1;
 
117
        stream_control(mpctx->stream, STREAM_CTRL_GET_CACHE_SIZE, &size);
 
118
        stream_control(mpctx->stream, STREAM_CTRL_GET_CACHE_FILL, &fill);
 
119
        if (size > 0 && fill >= 0)
 
120
            return fill / (size / 100);
 
121
    }
 
122
    return -1;
 
123
}
 
124
 
 
125
bool mp_get_cache_idle(struct MPContext *mpctx)
 
126
{
 
127
    int idle = 0;
 
128
    if (mpctx->stream)
 
129
        stream_control(mpctx->stream, STREAM_CTRL_GET_CACHE_IDLE, &idle);
 
130
    return idle;
 
131
}
 
132
 
 
133
void update_window_title(struct MPContext *mpctx, bool force)
 
134
{
 
135
    if (!mpctx->video_out && !mpctx->ao) {
 
136
        talloc_free(mpctx->last_window_title);
 
137
        mpctx->last_window_title = false;
 
138
        return;
 
139
    }
 
140
    char *title = mp_property_expand_string(mpctx, mpctx->opts->wintitle);
 
141
    if (!mpctx->last_window_title || force ||
 
142
        strcmp(title, mpctx->last_window_title) != 0)
 
143
    {
 
144
        talloc_free(mpctx->last_window_title);
 
145
        mpctx->last_window_title = talloc_steal(mpctx, title);
 
146
 
 
147
        if (mpctx->video_out) {
 
148
            mpctx->video_out->window_title = talloc_strdup(mpctx->video_out, title);
 
149
            vo_control(mpctx->video_out, VOCTRL_UPDATE_WINDOW_TITLE, title);
 
150
        }
 
151
 
 
152
        if (mpctx->ao) {
 
153
            ao_control(mpctx->ao, AOCONTROL_UPDATE_STREAM_TITLE, title);
 
154
        }
 
155
    } else {
 
156
        talloc_free(title);
 
157
    }
 
158
}
 
159
 
 
160
void stream_dump(struct MPContext *mpctx)
 
161
{
 
162
    struct MPOpts *opts = mpctx->opts;
 
163
    char *filename = opts->stream_dump;
 
164
    stream_t *stream = mpctx->stream;
 
165
    assert(stream && filename);
 
166
 
 
167
    stream_set_capture_file(stream, filename);
 
168
 
 
169
    while (mpctx->stop_play == KEEP_PLAYING && !stream->eof) {
 
170
        if (!opts->quiet && ((stream->pos / (1024 * 1024)) % 2) == 1) {
 
171
            uint64_t pos = stream->pos - stream->start_pos;
 
172
            uint64_t end = stream->end_pos - stream->start_pos;
 
173
            char *line = talloc_asprintf(NULL, "Dumping %lld/%lld...",
 
174
                (long long int)pos, (long long int)end);
 
175
            write_status_line(mpctx, line);
 
176
            talloc_free(line);
 
177
        }
 
178
        stream_fill_buffer(stream);
 
179
        for (;;) {
 
180
            mp_cmd_t *cmd = mp_input_get_cmd(mpctx->input, 0, false);
 
181
            if (!cmd)
 
182
                break;
 
183
            run_command(mpctx, cmd);
 
184
            talloc_free(cmd);
 
185
        }
 
186
    }
 
187
}
 
188
 
 
189
void merge_playlist_files(struct playlist *pl)
 
190
{
 
191
    if (!pl->first)
 
192
        return;
 
193
    char *edl = talloc_strdup(NULL, "edl://");
 
194
    for (struct playlist_entry *e = pl->first; e; e = e->next) {
 
195
        if (e != pl->first)
 
196
            edl = talloc_strdup_append_buffer(edl, ";");
 
197
        // Escape if needed
 
198
        if (e->filename[strcspn(e->filename, "=%,;\n")] ||
 
199
            bstr_strip(bstr0(e->filename)).len != strlen(e->filename))
 
200
        {
 
201
            // %length%
 
202
            edl = talloc_asprintf_append_buffer(edl, "%%%zd%%", strlen(e->filename));
 
203
        }
 
204
        edl = talloc_strdup_append_buffer(edl, e->filename);
 
205
    }
 
206
    playlist_clear(pl);
 
207
    playlist_add_file(pl, edl);
 
208
    talloc_free(edl);
 
209
}