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

« back to all changes in this revision

Viewing changes to common/av_common.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 mpv.
 
3
 *
 
4
 * mpv 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
 * mpv 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 mpv.  If not, see <http://www.gnu.org/licenses/>.
 
16
 */
 
17
 
 
18
#include <assert.h>
 
19
 
 
20
#include <libavutil/common.h>
 
21
#include <libavutil/log.h>
 
22
#include <libavcodec/avcodec.h>
 
23
 
 
24
#include "common/common.h"
 
25
#include "common/msg.h"
 
26
#include "demux/packet.h"
 
27
#include "av_common.h"
 
28
#include "codecs.h"
 
29
 
 
30
#include "osdep/numcores.h"
 
31
 
 
32
 
 
33
// Copy the codec-related fields from st into avctx. This does not set the
 
34
// codec itself, only codec related header data provided by libavformat.
 
35
// The goal is to initialize a new decoder with the header data provided by
 
36
// libavformat, and unlike avcodec_copy_context(), allow the user to create
 
37
// a clean AVCodecContext for a manually selected AVCodec.
 
38
// This is strictly for decoding only.
 
39
void mp_copy_lav_codec_headers(AVCodecContext *avctx, AVCodecContext *st)
 
40
{
 
41
    if (st->extradata_size) {
 
42
        av_free(avctx->extradata);
 
43
        avctx->extradata_size = 0;
 
44
        avctx->extradata =
 
45
            av_mallocz(st->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
 
46
        if (avctx->extradata) {
 
47
            avctx->extradata_size = st->extradata_size;
 
48
            memcpy(avctx->extradata, st->extradata, st->extradata_size);
 
49
        }
 
50
    }
 
51
    avctx->codec_tag                = st->codec_tag;
 
52
    avctx->stream_codec_tag         = st->stream_codec_tag;
 
53
    avctx->bit_rate                 = st->bit_rate;
 
54
    avctx->width                    = st->width;
 
55
    avctx->height                   = st->height;
 
56
    avctx->pix_fmt                  = st->pix_fmt;
 
57
    avctx->sample_aspect_ratio      = st->sample_aspect_ratio;
 
58
    avctx->chroma_sample_location   = st->chroma_sample_location;
 
59
    avctx->sample_rate              = st->sample_rate;
 
60
    avctx->channels                 = st->channels;
 
61
    avctx->block_align              = st->block_align;
 
62
    avctx->channel_layout           = st->channel_layout;
 
63
    avctx->audio_service_type       = st->audio_service_type;
 
64
    avctx->bits_per_coded_sample    = st->bits_per_coded_sample;
 
65
}
 
66
 
 
67
// We merely pass-through our PTS/DTS as an int64_t; libavcodec won't use it.
 
68
union pts { int64_t i; double d; };
 
69
 
 
70
// Convert the mpv style timestamp (seconds as double) to a libavcodec style
 
71
// timestamp (integer units in a given timebase).
 
72
//
 
73
// If the given timebase is NULL or invalid, pass through the mpv timestamp by
 
74
// reinterpret casting them to int64_t. In this case, the timestamps will be
 
75
// non-sense for libavcodec, but we expect that it doesn't interpret them,
 
76
// and treats them as opaque.
 
77
int64_t mp_pts_to_av(double mp_pts, AVRational *tb)
 
78
{
 
79
    assert(sizeof(int64_t) >= sizeof(double));
 
80
    if (tb && tb->num > 0 && tb->den > 0)
 
81
        return mp_pts == MP_NOPTS_VALUE ? AV_NOPTS_VALUE : mp_pts / av_q2d(*tb);
 
82
    // The + 0.0 is to squash possible negative zero mp_pts, which would
 
83
    // happen to end up as AV_NOPTS_VALUE.
 
84
    return (union pts){.d = mp_pts + 0.0}.i;
 
85
}
 
86
 
 
87
// Inverse of mp_pts_to_av(). (The timebases must be exactly the same.)
 
88
double mp_pts_from_av(int64_t av_pts, AVRational *tb)
 
89
{
 
90
    assert(sizeof(int64_t) >= sizeof(double));
 
91
    if (tb && tb->num > 0 && tb->den > 0)
 
92
        return av_pts == AV_NOPTS_VALUE ? MP_NOPTS_VALUE : av_pts * av_q2d(*tb);
 
93
    // Should libavcodec set the PTS to AV_NOPTS_VALUE, it would end up as
 
94
    // non-sense (usually negative zero) when unwrapped to double.
 
95
    return av_pts == AV_NOPTS_VALUE ? MP_NOPTS_VALUE : (union pts){.i = av_pts}.d;
 
96
}
 
97
 
 
98
// Set dst from mpkt. Note that dst is not refcountable.
 
99
// mpkt can be NULL to generate empty packets (used to flush delayed data).
 
100
// Sets pts/dts using mp_pts_to_av(ts, tb). (Be aware of the implications.)
 
101
// Set duration field only if tb is set.
 
102
void mp_set_av_packet(AVPacket *dst, struct demux_packet *mpkt, AVRational *tb)
 
103
{
 
104
    av_init_packet(dst);
 
105
    dst->data = mpkt ? mpkt->buffer : NULL;
 
106
    dst->size = mpkt ? mpkt->len : 0;
 
107
    /* Some codecs (ZeroCodec, some cases of PNG) may want keyframe info
 
108
     * from demuxer. */
 
109
    if (mpkt && mpkt->keyframe)
 
110
        dst->flags |= AV_PKT_FLAG_KEY;
 
111
    if (mpkt && mpkt->avpacket) {
 
112
        dst->side_data = mpkt->avpacket->side_data;
 
113
        dst->side_data_elems = mpkt->avpacket->side_data_elems;
 
114
    }
 
115
    if (mpkt && tb && tb->num > 0 && tb->den > 0)
 
116
        dst->duration = mpkt->duration / av_q2d(*tb);
 
117
    dst->pts = mp_pts_to_av(mpkt ? mpkt->pts : MP_NOPTS_VALUE, tb);
 
118
    dst->dts = mp_pts_to_av(mpkt ? mpkt->dts : MP_NOPTS_VALUE, tb);
 
119
}
 
120
 
 
121
void mp_set_avcodec_threads(AVCodecContext *avctx, int threads)
 
122
{
 
123
    if (threads == 0) {
 
124
        threads = default_thread_count();
 
125
        if (threads < 1) {
 
126
            av_log(avctx, AV_LOG_WARNING, "Could not determine "
 
127
                   "thread count to use, defaulting to 1.\n");
 
128
            threads = 1;
 
129
        }
 
130
        // Apparently some libavcodec versions have or had trouble with more
 
131
        // than 16 threads, and/or print a warning when using > 16.
 
132
        threads = MPMIN(threads, 16);
 
133
    }
 
134
    avctx->thread_count = threads;
 
135
}
 
136
 
 
137
void mp_add_lavc_decoders(struct mp_decoder_list *list, enum AVMediaType type)
 
138
{
 
139
    AVCodec *cur = NULL;
 
140
    for (;;) {
 
141
        cur = av_codec_next(cur);
 
142
        if (!cur)
 
143
            break;
 
144
        if (av_codec_is_decoder(cur) && cur->type == type) {
 
145
            mp_add_decoder(list, "lavc", mp_codec_from_av_codec_id(cur->id),
 
146
                           cur->name, cur->long_name);
 
147
        }
 
148
    }
 
149
}
 
150
 
 
151
int mp_codec_to_av_codec_id(const char *codec)
 
152
{
 
153
    int id = AV_CODEC_ID_NONE;
 
154
    if (codec) {
 
155
        const AVCodecDescriptor *desc = avcodec_descriptor_get_by_name(codec);
 
156
        if (desc)
 
157
            id = desc->id;
 
158
        if (id == AV_CODEC_ID_NONE) {
 
159
            AVCodec *avcodec = avcodec_find_decoder_by_name(codec);
 
160
            if (avcodec)
 
161
                id = avcodec->id;
 
162
        }
 
163
    }
 
164
    return id;
 
165
}
 
166
 
 
167
const char *mp_codec_from_av_codec_id(int codec_id)
 
168
{
 
169
    const char *name = NULL;
 
170
    const AVCodecDescriptor *desc = avcodec_descriptor_get(codec_id);
 
171
    if (desc)
 
172
        name = desc->name;
 
173
    if (!name) {
 
174
        AVCodec *avcodec = avcodec_find_decoder(codec_id);
 
175
        if (avcodec)
 
176
            name = avcodec->name;
 
177
    }
 
178
    return name;
 
179
}