obs-ffmpeg-aac.c 7.6 KB
Newer Older
J
jp9000 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/******************************************************************************
    Copyright (C) 2014 by Hugh Bailey <obs.jim@gmail.com>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/

#include <util/base.h>
#include <util/circlebuf.h>
20
#include <util/darray.h>
J
jp9000 已提交
21 22 23
#include <obs.h>

#include <libavformat/avformat.h>
24

J
jp9000 已提交
25
#include "obs-ffmpeg-formats.h"
26
#include "obs-ffmpeg-compat.h"
J
jp9000 已提交
27 28 29 30 31 32 33 34 35 36 37

struct aac_encoder {
	obs_encoder_t    encoder;

	AVCodec          *aac;
	AVCodecContext   *context;

	uint8_t          *samples[MAX_AV_PLANES];
	AVFrame          *aframe;
	int              total_samples;

38 39
	DARRAY(uint8_t)  packet_buffer;

J
jp9000 已提交
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
	size_t           audio_planes;
	size_t           audio_size;

	int              frame_size; /* pretty much always 1024 for AAC */
	int              frame_size_bytes;
};

static const char *aac_getname(const char *locale)
{
	UNUSED_PARAMETER(locale);
	return "FFmpeg Default AAC Encoder";
}

static void aac_warn(const char *func, const char *format, ...)
{
	va_list args;
	char msg[1024];

	va_start(args, format);
	vsnprintf(msg, sizeof(msg), format, args);
60
	blog(LOG_WARNING, "[%s]: %s", func, msg);
J
jp9000 已提交
61 62 63 64 65 66 67 68 69 70 71 72 73
	va_end(args);
}

static void aac_destroy(void *data)
{
	struct aac_encoder *enc = data;

	if (enc->samples[0])
		av_freep(&enc->samples[0]);
	if (enc->context)
		avcodec_close(enc->context);
	if (enc->aframe)
		av_frame_free(&enc->aframe);
74 75

	da_free(enc->packet_buffer);
J
jp9000 已提交
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
	bfree(enc);
}

static bool initialize_codec(struct aac_encoder *enc)
{
	int ret;

	enc->aframe  = av_frame_alloc();
	if (!enc->aframe) {
		aac_warn("initialize_codec", "Failed to allocate audio frame");
		return false;
	}

	ret = avcodec_open2(enc->context, enc->aac, NULL);
	if (ret < 0) {
		aac_warn("initialize_codec", "Failed to open AAC codec: %s",
				av_err2str(ret));
		return false;
	}

	enc->frame_size = enc->context->frame_size;
	if (!enc->frame_size)
		enc->frame_size = 1024;

	enc->frame_size_bytes = enc->frame_size * (int)enc->audio_size;

	ret = av_samples_alloc(enc->samples, NULL, enc->context->channels,
			enc->frame_size, enc->context->sample_fmt, 0);
	if (ret < 0) {
		aac_warn("initialize_codec", "Failed to create audio buffer: "
		                             "%s", av_err2str(ret));
		return false;
	}

	return true;
}

113 114 115 116 117 118 119 120 121 122 123 124
static void init_sizes(struct aac_encoder *enc, audio_t audio)
{
	const struct audio_output_info *aoi;
	enum audio_format format;

	aoi    = audio_output_getinfo(audio);
	format = convert_ffmpeg_sample_format(enc->context->sample_fmt);

	enc->audio_planes = get_audio_planes(format, aoi->speakers);
	enc->audio_size   = get_audio_size(format, aoi->speakers, 1);
}

J
jp9000 已提交
125 126 127 128 129 130 131 132 133 134 135
static void *aac_create(obs_data_t settings, obs_encoder_t encoder)
{
	struct aac_encoder *enc;
	int                bitrate = (int)obs_data_getint(settings, "bitrate");
	audio_t            audio   = obs_encoder_audio(encoder);

	if (!bitrate) {
		aac_warn("aac_create", "Invalid bitrate specified");
		return NULL;
	}

136 137
	avcodec_register_all();

J
jp9000 已提交
138 139 140 141 142 143 144 145
	enc          = bzalloc(sizeof(struct aac_encoder));
	enc->encoder = encoder;
	enc->aac     = avcodec_find_encoder(AV_CODEC_ID_AAC);
	if (!enc->aac) {
		aac_warn("aac_create", "Couldn't find encoder");
		goto fail;
	}

146 147
	blog(LOG_INFO, "Using ffmpeg \"%s\" aac encoder", enc->aac->name);

J
jp9000 已提交
148 149 150 151 152 153
	enc->context = avcodec_alloc_context3(enc->aac);
	if (!enc->context) {
		aac_warn("aac_create", "Failed to create codec context");
		goto fail;
	}

154
	enc->context->bit_rate    = bitrate * 1000;
J
jp9000 已提交
155 156 157 158 159
	enc->context->channels    = (int)audio_output_channels(audio);
	enc->context->sample_rate = audio_output_samplerate(audio);
	enc->context->sample_fmt  = enc->aac->sample_fmts ?
		enc->aac->sample_fmts[0] : AV_SAMPLE_FMT_FLTP;

160
	init_sizes(enc, audio);
J
jp9000 已提交
161 162 163 164

	/* enable experimental FFmpeg encoder if the only one available */
	enc->context->strict_std_compliance = -2;

165 166
	enc->context->flags = CODEC_FLAG_GLOBAL_HEADER;

J
jp9000 已提交
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
	if (initialize_codec(enc))
		return enc;

fail:
	aac_destroy(enc);
	return NULL;
}

static bool do_aac_encode(struct aac_encoder *enc,
		struct encoder_packet *packet, bool *received_packet)
{
	AVRational time_base = {1, enc->context->sample_rate};
	AVPacket   avpacket  = {0};
	int        got_packet;
	int        ret;

	enc->aframe->nb_samples = enc->frame_size;
	enc->aframe->pts = av_rescale_q(enc->total_samples,
			(AVRational){1, enc->context->sample_rate},
			enc->context->time_base);

	ret = avcodec_fill_audio_frame(enc->aframe, enc->context->channels,
			enc->context->sample_fmt, enc->samples[0],
			enc->frame_size_bytes * enc->context->channels, 1);
	if (ret < 0) {
		aac_warn("do_aac_encode", "avcodec_fill_audio_frame failed: %s",
				av_err2str(ret));
		return false;
	}

	enc->total_samples += enc->frame_size;

	ret = avcodec_encode_audio2(enc->context, &avpacket, enc->aframe,
			&got_packet);
	if (ret < 0) {
		aac_warn("do_aac_encode", "avcodec_encode_audio2 failed: %s",
				av_err2str(ret));
		return false;
	}

	*received_packet = !!got_packet;
	if (!got_packet)
		return true;

211 212 213
	da_resize(enc->packet_buffer, 0);
	da_push_back_array(enc->packet_buffer, avpacket.data, avpacket.size);

J
jp9000 已提交
214 215
	packet->pts  = rescale_ts(avpacket.pts, enc->context, time_base);
	packet->dts  = rescale_ts(avpacket.dts, enc->context, time_base);
216
	packet->data = enc->packet_buffer.array;
217
	packet->size = avpacket.size;
J
jp9000 已提交
218 219 220 221 222 223 224 225 226 227 228 229 230
	packet->type = OBS_ENCODER_AUDIO;
	packet->timebase_num = 1;
	packet->timebase_den = (int32_t)enc->context->sample_rate;
	av_free_packet(&avpacket);
	return true;
}

static bool aac_encode(void *data, struct encoder_frame *frame,
		struct encoder_packet *packet, bool *received_packet)
{
	struct aac_encoder *enc = data;

	for (size_t i = 0; i < enc->audio_planes; i++)
231
		memcpy(enc->samples[i], frame->data[i], enc->frame_size_bytes);
J
jp9000 已提交
232 233 234 235 236 237

	return do_aac_encode(enc, packet, received_packet);
}

static void aac_defaults(obs_data_t settings)
{
238
	obs_data_set_default_int(settings, "bitrate", 128);
J
jp9000 已提交
239 240 241 242
}

static obs_properties_t aac_properties(const char *locale)
{
J
jp9000 已提交
243
	obs_properties_t props = obs_properties_create();
J
jp9000 已提交
244 245

	/* TODO: locale */
246
	obs_properties_add_int(props, "bitrate", "Bitrate", 32, 320, 32);
J
jp9000 已提交
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
	return props;
}

static bool aac_extra_data(void *data, uint8_t **extra_data, size_t *size)
{
	struct aac_encoder *enc = data;

	*extra_data = enc->context->extradata;
	*size       = enc->context->extradata_size;
	return true;
}

static bool aac_audio_info(void *data, struct audio_convert_info *info)
{
	struct aac_encoder *enc = data;

	memset(info, 0, sizeof(struct audio_convert_info));
	info->format = convert_ffmpeg_sample_format(enc->context->sample_fmt);
	return true;
}

268 269 270 271 272 273
static size_t aac_frame_size(void *data)
{
	struct aac_encoder *enc =data;
	return enc->frame_size;
}

J
jp9000 已提交
274 275 276 277 278 279 280 281
struct obs_encoder_info aac_encoder_info = {
	.id         = "ffmpeg_aac",
	.type       = OBS_ENCODER_AUDIO,
	.codec      = "AAC",
	.getname    = aac_getname,
	.create     = aac_create,
	.destroy    = aac_destroy,
	.encode     = aac_encode,
282
	.frame_size = aac_frame_size,
J
jp9000 已提交
283 284 285 286 287
	.defaults   = aac_defaults,
	.properties = aac_properties,
	.extra_data = aac_extra_data,
	.audio_info = aac_audio_info
};