/src/mpv/demux/dovi_split.c
Line | Count | Source |
1 | | /* |
2 | | * This file is part of mpv. |
3 | | * |
4 | | * mpv is free software; you can redistribute it and/or |
5 | | * modify it under the terms of the GNU Lesser General Public |
6 | | * License as published by the Free Software Foundation; either |
7 | | * version 2.1 of the License, or (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 Lesser General Public License for more details. |
13 | | * |
14 | | * You should have received a copy of the GNU Lesser General Public |
15 | | * License along with mpv. If not, see <http://www.gnu.org/licenses/>. |
16 | | */ |
17 | | |
18 | | #include <string.h> |
19 | | |
20 | | #include <libavcodec/avcodec.h> |
21 | | #include <libavcodec/bsf.h> |
22 | | #include <libavutil/dovi_meta.h> |
23 | | #include <libavutil/opt.h> |
24 | | |
25 | | #include "common/av_common.h" |
26 | | #include "common/common.h" |
27 | | #include "common/msg.h" |
28 | | #include "demux.h" |
29 | | #include "demux/packet.h" |
30 | | #include "demux/packet_pool.h" |
31 | | #include "demux/stheader.h" |
32 | | #include "dovi_split.h" |
33 | | #include "mpv_talloc.h" |
34 | | |
35 | | struct mp_dovi_split { |
36 | | struct mp_log *log; |
37 | | struct demuxer *demuxer; |
38 | | struct sh_stream *bl; |
39 | | struct sh_stream *el; |
40 | | AVBSFContext *bsf; |
41 | | AVPacket *staging; |
42 | | }; |
43 | | |
44 | | static void mp_dovi_split_destructor(void *p) |
45 | 0 | { |
46 | 0 | struct mp_dovi_split *s = p; |
47 | 0 | av_packet_free(&s->staging); |
48 | 0 | av_bsf_free(&s->bsf); |
49 | 0 | } |
50 | | |
51 | | struct mp_dovi_split *mp_dovi_split_create(struct demuxer *demuxer, |
52 | | struct sh_stream *bl) |
53 | 0 | { |
54 | 0 | if (!bl || bl->type != STREAM_VIDEO || !bl->codec || |
55 | 0 | !bl->codec->codec || strcmp(bl->codec->codec, "hevc") != 0) |
56 | 0 | return NULL; |
57 | | |
58 | 0 | const AVBitStreamFilter *def = av_bsf_get_by_name("dovi_split"); |
59 | 0 | if (!def) { |
60 | 0 | MP_WARN(demuxer, "Dolby Vision EL: 'dovi_split' BSF not available in " |
61 | 0 | "libavcodec; rendering base layer only.\n"); |
62 | 0 | return NULL; |
63 | 0 | } |
64 | | |
65 | 0 | struct mp_dovi_split *s = talloc_zero(demuxer, struct mp_dovi_split); |
66 | 0 | talloc_set_destructor(s, mp_dovi_split_destructor); |
67 | 0 | s->log = demuxer->log; |
68 | 0 | s->demuxer = demuxer; |
69 | 0 | s->bl = bl; |
70 | |
|
71 | 0 | s->staging = av_packet_alloc(); |
72 | 0 | if (!s->staging) |
73 | 0 | goto fail; |
74 | | |
75 | 0 | int ret = av_bsf_alloc(def, &s->bsf); |
76 | 0 | if (ret < 0) |
77 | 0 | goto fail; |
78 | | |
79 | 0 | AVCodecParameters *par = mp_codec_params_to_av(bl->codec); |
80 | 0 | if (!par) |
81 | 0 | goto fail; |
82 | 0 | ret = avcodec_parameters_copy(s->bsf->par_in, par); |
83 | 0 | avcodec_parameters_free(&par); |
84 | 0 | if (ret < 0) |
85 | 0 | goto fail; |
86 | 0 | s->bsf->time_base_in = mp_get_codec_timebase(bl->codec); |
87 | |
|
88 | 0 | if (av_opt_set(s->bsf, "mode", "el", AV_OPT_SEARCH_CHILDREN) < 0) |
89 | 0 | goto fail; |
90 | 0 | if (av_bsf_init(s->bsf) < 0) |
91 | 0 | goto fail; |
92 | | |
93 | 0 | const AVCodecParameters *par_out = s->bsf->par_out; |
94 | | |
95 | | // Allocate the virtual EL sh_stream. |
96 | 0 | struct sh_stream *el = demux_alloc_sh_stream(STREAM_VIDEO); |
97 | 0 | el->codec->codec = "hevc"; |
98 | 0 | el->codec->native_tb_num = bl->codec->native_tb_num; |
99 | 0 | el->codec->native_tb_den = bl->codec->native_tb_den; |
100 | 0 | el->codec->fps = bl->codec->fps; |
101 | 0 | el->codec->disp_w = par_out->width; |
102 | 0 | el->codec->disp_h = par_out->height; |
103 | 0 | if (par_out->extradata_size > 0) { |
104 | 0 | el->codec->extradata = talloc_memdup(el->codec, par_out->extradata, |
105 | 0 | par_out->extradata_size); |
106 | 0 | el->codec->extradata_size = par_out->extradata_size; |
107 | 0 | } |
108 | 0 | for (int i = 0; i < par_out->nb_coded_side_data; i++) { |
109 | 0 | const AVPacketSideData *sd = &par_out->coded_side_data[i]; |
110 | 0 | if (sd->type != AV_PKT_DATA_DOVI_CONF) |
111 | 0 | continue; |
112 | 0 | const AVDOVIDecoderConfigurationRecord *cfg = (const void *)sd->data; |
113 | 0 | el->codec->dovi = true; |
114 | 0 | el->codec->dv_profile = cfg->dv_profile; |
115 | 0 | el->codec->dv_level = cfg->dv_level; |
116 | 0 | el->codec->dv_el_present = cfg->el_present_flag; |
117 | 0 | break; |
118 | 0 | } |
119 | 0 | el->title = talloc_strdup(el, "Dolby Vision enhancement layer"); |
120 | 0 | el->dependent_track = true; |
121 | |
|
122 | 0 | demux_add_sh_stream(demuxer, el); |
123 | 0 | s->el = el; |
124 | | |
125 | | // Bind BL and EL into a sh_stream_group. |
126 | 0 | struct sh_stream_group *group = talloc_zero(bl, struct sh_stream_group); |
127 | 0 | MP_TARRAY_APPEND(group, group->members, group->num_members, bl); |
128 | 0 | MP_TARRAY_APPEND(group, group->members, group->num_members, el); |
129 | 0 | bl->group = group; |
130 | 0 | el->group = group; |
131 | |
|
132 | 0 | MP_VERBOSE(demuxer, "Dolby Vision Profile 7 splitter: BL stream %d, " |
133 | 0 | "virtual EL stream %d (dependent_track).\n", |
134 | 0 | bl->index, el->index); |
135 | 0 | return s; |
136 | | |
137 | 0 | fail: |
138 | 0 | talloc_free(s); |
139 | 0 | return NULL; |
140 | 0 | } |
141 | | |
142 | | void mp_dovi_split_reset(struct mp_dovi_split *s) |
143 | 0 | { |
144 | 0 | if (!s || !s->bsf) |
145 | 0 | return; |
146 | 0 | av_bsf_flush(s->bsf); |
147 | 0 | } |
148 | | |
149 | | struct sh_stream *mp_dovi_split_el_stream(struct mp_dovi_split *s) |
150 | 0 | { |
151 | 0 | return s ? s->el : NULL; |
152 | 0 | } |
153 | | |
154 | | struct demux_packet *mp_dovi_split_dispatch(struct mp_dovi_split *s, |
155 | | struct demux_packet *bl_dp) |
156 | 0 | { |
157 | 0 | if (!s || !s->bsf || !bl_dp || !bl_dp->buffer || bl_dp->len <= 0) |
158 | 0 | return NULL; |
159 | | |
160 | | // av_bsf_send_packet takes ownership of the packet's buffer, so copy it, |
161 | | // to not steal it from caller. |
162 | 0 | AVPacket *copy = av_packet_alloc(); |
163 | 0 | if (!copy) |
164 | 0 | return NULL; |
165 | 0 | int ret = av_new_packet(copy, bl_dp->len); |
166 | 0 | if (ret < 0) { |
167 | 0 | av_packet_free(©); |
168 | 0 | return NULL; |
169 | 0 | } |
170 | 0 | memcpy(copy->data, bl_dp->buffer, bl_dp->len); |
171 | 0 | copy->flags = bl_dp->keyframe ? AV_PKT_FLAG_KEY : 0; |
172 | |
|
173 | 0 | ret = av_bsf_send_packet(s->bsf, copy); |
174 | 0 | av_packet_free(©); |
175 | 0 | if (ret < 0) { |
176 | 0 | MP_VERBOSE(s->demuxer, "dovi_split: BSF send failed: %s\n", |
177 | 0 | mp_strerror(AVUNERROR(ret))); |
178 | 0 | return NULL; |
179 | 0 | } |
180 | | |
181 | 0 | ret = av_bsf_receive_packet(s->bsf, s->staging); |
182 | 0 | if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) { |
183 | | // No EL NALs in this AU, nothing to emit. |
184 | 0 | return NULL; |
185 | 0 | } |
186 | 0 | if (ret < 0) { |
187 | 0 | MP_VERBOSE(s->demuxer, "dovi_split: BSF receive failed: %s; flushing.\n", |
188 | 0 | mp_strerror(AVUNERROR(ret))); |
189 | 0 | av_bsf_flush(s->bsf); |
190 | 0 | return NULL; |
191 | 0 | } |
192 | | |
193 | 0 | struct demux_packet *dp = |
194 | 0 | new_demux_packet_from_avpacket(s->demuxer->packet_pool, s->staging); |
195 | 0 | if (dp) { |
196 | | // Mirror the BL packet's timing so the pairing filter can match by PTS. |
197 | 0 | dp->pts = bl_dp->pts; |
198 | 0 | dp->dts = bl_dp->dts; |
199 | 0 | dp->duration = bl_dp->duration; |
200 | 0 | dp->keyframe = bl_dp->keyframe; |
201 | 0 | dp->stream = s->el->index; |
202 | 0 | } |
203 | 0 | av_packet_unref(s->staging); |
204 | 0 | return dp; |
205 | 0 | } |