]> git.sesse.net Git - vlc/blob - modules/codec/x265.c
ff28059ad056f7ae47bedae7293327e2d5cce092
[vlc] / modules / codec / x265.c
1 /*****************************************************************************
2  * x265.c: HEVC/H.265 video encoder
3  *****************************************************************************
4  * Copyright (C) 2013 Rafaël Carré
5  *
6  * Authors: Rafaël Carré <funman@videolanorg>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21  *****************************************************************************/
22
23 /*****************************************************************************
24  * Preamble
25  *****************************************************************************/
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include <vlc_common.h>
31 #include <vlc_plugin.h>
32 #include <vlc_threads.h>
33 #include <vlc_sout.h>
34 #include <vlc_codec.h>
35
36 #include <x265.h>
37
38 /*****************************************************************************
39  * Module descriptor
40  *****************************************************************************/
41 static int  Open (vlc_object_t *);
42 static void Close(vlc_object_t *);
43
44 vlc_module_begin ()
45     set_description(N_("H.265/HEVC encoder (x265)"))
46     set_capability("encoder", 200)
47     set_callbacks(Open, Close)
48     set_category(CAT_INPUT)
49     set_subcategory(SUBCAT_INPUT_VCODEC)
50 vlc_module_end ()
51
52 struct encoder_sys_t
53 {
54     x265_encoder    *h;
55     x265_param      param;
56
57     mtime_t         i_initial_delay;
58
59     mtime_t         dts;
60     mtime_t         initial_date;
61 #ifndef NDEBUG
62     mtime_t         start;
63 #endif
64 };
65
66 static block_t *Encode(encoder_t *p_enc, picture_t *p_pict)
67 {
68     encoder_sys_t *p_sys = p_enc->p_sys;
69     x265_picture pic;
70
71     x265_picture_init(&p_sys->param, &pic);
72
73     if (likely(p_pict)) {
74         pic.pts = p_pict->date;
75         if (unlikely(p_sys->initial_date == 0)) {
76             p_sys->initial_date = p_pict->date;
77 #ifndef NDEBUG
78             p_sys->start = mdate();
79 #endif
80         }
81
82         for (int i = 0; i < p_pict->i_planes; i++) {
83             pic.planes[i] = p_pict->p[i].p_pixels;
84             pic.stride[i] = p_pict->p[i].i_pitch;
85         }
86     }
87
88     x265_nal *nal;
89     uint32_t i_nal = 0;
90     x265_encoder_encode(p_sys->h, &nal, &i_nal,
91             likely(p_pict) ? &pic : NULL, &pic);
92
93     if (!i_nal)
94         return NULL;
95
96     int i_out = 0;
97     for (uint32_t i = 0; i < i_nal; i++)
98         i_out += nal[i].sizeBytes;
99
100     block_t *p_block = block_Alloc(i_out);
101     if (!p_block)
102         return NULL;
103
104     /* all payloads are sequentially laid out in memory */
105     memcpy(p_block->p_buffer, nal[0].payload, i_out);
106
107     /* This isn't really valid for streams with B-frames */
108     p_block->i_length = CLOCK_FREQ *
109         p_enc->fmt_in.video.i_frame_rate_base /
110             p_enc->fmt_in.video.i_frame_rate;
111
112     p_block->i_pts = p_sys->initial_date + pic.poc * p_block->i_length;
113     p_block->i_dts = p_sys->initial_date + p_sys->dts++ * p_block->i_length;
114
115     switch (pic.sliceType)
116     {
117     case X265_TYPE_I:
118     case X265_TYPE_IDR:
119         p_block->i_flags |= BLOCK_FLAG_TYPE_I;
120         break;
121     case X265_TYPE_P:
122         p_block->i_flags |= BLOCK_FLAG_TYPE_P;
123         break;
124     case X265_TYPE_B:
125     case X265_TYPE_BREF:
126         p_block->i_flags |= BLOCK_FLAG_TYPE_B;
127         break;
128     }
129
130 #ifndef NDEBUG
131     msg_Dbg(p_enc, "%zu bytes (frame %"PRId64", %.2ffps)", p_block->i_buffer,
132         p_sys->dts, (float)p_sys->dts * CLOCK_FREQ / (mdate() - p_sys->start));
133 #endif
134
135     return p_block;
136 }
137
138 static int  Open (vlc_object_t *p_this)
139 {
140     encoder_t     *p_enc = (encoder_t *)p_this;
141     encoder_sys_t *p_sys;
142
143     if (p_enc->fmt_out.i_codec != VLC_CODEC_HEVC && !p_enc->b_force)
144         return VLC_EGENERIC;
145
146     p_enc->fmt_out.i_cat = VIDEO_ES;
147     p_enc->fmt_out.i_codec = VLC_CODEC_HEVC;
148     p_enc->p_sys = p_sys = malloc(sizeof(encoder_sys_t));
149     if (!p_sys)
150         return VLC_ENOMEM;
151
152     p_enc->fmt_in.i_codec = VLC_CODEC_I420;
153
154     x265_param *param = &p_sys->param;
155     x265_param_default(param);
156
157     param->frameNumThreads = vlc_GetCPUCount();
158     param->bEnableWavefront = 0; // buggy in x265, use frame threading for now
159     param->maxCUSize = 16; /* use smaller macroblock */
160
161 #if X265_BUILD >= 6
162     param->fpsNum = p_enc->fmt_in.video.i_frame_rate;
163     param->fpsDenom = p_enc->fmt_in.video.i_frame_rate_base;
164     if (!param->fpsNum) {
165         param->fpsNum = 25;
166         param->fpsDenom = 1;
167     }
168 #else
169     if (p_enc->fmt_in.video.i_frame_rate_base) {
170         param->frameRate = p_enc->fmt_in.video.i_frame_rate /
171             p_enc->fmt_in.video.i_frame_rate_base;
172     } else {
173         param->frameRate = 25;
174     }
175 #endif
176     param->sourceWidth = p_enc->fmt_in.video.i_visible_width;
177     param->sourceHeight = p_enc->fmt_in.video.i_visible_height;
178
179     if (param->sourceWidth & (param->maxCUSize - 1)) {
180         msg_Err(p_enc, "Width (%d) must be a multiple of %d",
181             param->sourceWidth, param->maxCUSize);
182         free(p_sys);
183         return VLC_EGENERIC;
184     }
185     if (param->sourceHeight & 7) {
186         msg_Err(p_enc, "Height (%d) must be a multiple of 8", param->sourceHeight);
187         free(p_sys);
188         return VLC_EGENERIC;
189     }
190
191     if (p_enc->fmt_out.i_bitrate > 0) {
192         param->rc.bitrate = p_enc->fmt_out.i_bitrate / 1000;
193         param->rc.rateControlMode = X265_RC_ABR;
194     }
195
196     p_sys->h = x265_encoder_open(param);
197     if (p_sys->h == NULL) {
198         msg_Err(p_enc, "cannot open x265 encoder");
199         free(p_sys);
200         return VLC_EGENERIC;
201     }
202
203     x265_nal *nal;
204     uint32_t i_nal;
205     if (x265_encoder_headers(p_sys->h, &nal, &i_nal) < 0) {
206         msg_Err(p_enc, "cannot get x265 headers");
207         Close(VLC_OBJECT(p_enc));
208         return VLC_EGENERIC;
209     }
210
211     size_t i_extra = 0;
212     for (uint32_t i = 0; i < i_nal; i++)
213         i_extra += nal[i].sizeBytes;
214
215     p_enc->fmt_out.i_extra = i_extra;
216
217     uint8_t *p_extra = p_enc->fmt_out.p_extra = malloc(i_extra);
218     if (!p_extra) {
219         Close(VLC_OBJECT(p_enc));
220         return VLC_ENOMEM;
221     }
222
223     for (uint32_t i = 0; i < i_nal; i++) {
224         memcpy(p_extra, nal[i].payload, nal[i].sizeBytes);
225         p_extra += nal[i].sizeBytes;
226     }
227
228     p_sys->dts = 0;
229     p_sys->initial_date = 0;
230     p_sys->i_initial_delay = 0;
231
232     p_enc->pf_encode_video = Encode;
233     p_enc->pf_encode_audio = NULL;
234
235     return VLC_SUCCESS;
236 }
237
238 static void Close(vlc_object_t *p_this)
239 {
240     encoder_t     *p_enc = (encoder_t *)p_this;
241     encoder_sys_t *p_sys = p_enc->p_sys;
242
243     x265_encoder_close(p_sys->h);
244
245     free(p_sys);
246 }