2
* This file is part of mpv.
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.
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.
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/>.
20
#include <libavutil/common.h>
21
#include <libavutil/log.h>
22
#include <libavcodec/avcodec.h>
24
#include "common/common.h"
25
#include "common/msg.h"
26
#include "demux/packet.h"
27
#include "av_common.h"
30
#include "osdep/numcores.h"
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)
41
if (st->extradata_size) {
42
av_free(avctx->extradata);
43
avctx->extradata_size = 0;
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);
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;
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; };
70
// Convert the mpv style timestamp (seconds as double) to a libavcodec style
71
// timestamp (integer units in a given timebase).
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)
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;
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)
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;
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)
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
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;
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);
121
void mp_set_avcodec_threads(AVCodecContext *avctx, int threads)
124
threads = default_thread_count();
126
av_log(avctx, AV_LOG_WARNING, "Could not determine "
127
"thread count to use, defaulting to 1.\n");
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);
134
avctx->thread_count = threads;
137
void mp_add_lavc_decoders(struct mp_decoder_list *list, enum AVMediaType type)
141
cur = av_codec_next(cur);
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);
151
int mp_codec_to_av_codec_id(const char *codec)
153
int id = AV_CODEC_ID_NONE;
155
const AVCodecDescriptor *desc = avcodec_descriptor_get_by_name(codec);
158
if (id == AV_CODEC_ID_NONE) {
159
AVCodec *avcodec = avcodec_find_decoder_by_name(codec);
167
const char *mp_codec_from_av_codec_id(int codec_id)
169
const char *name = NULL;
170
const AVCodecDescriptor *desc = avcodec_descriptor_get(codec_id);
174
AVCodec *avcodec = avcodec_find_decoder(codec_id);
176
name = avcodec->name;