| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527 |
- /******************************************************************************
- Copyright (C) 2013 by Hugh Bailey <[email protected]>
- 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 <obs.h>
- #include "obs-ffmpeg-output.h"
- /* TODO: remove these later */
- #define FILENAME_TODO "D:\\test.mp4"
- #define SPS_TODO 44100
- /* NOTE: much of this stuff is test stuff that was more or less copied from
- * the muxing.c ffmpeg example */
- static inline enum AVPixelFormat obs_to_ffmpeg_video_format(
- enum video_format format)
- {
- switch (format) {
- case VIDEO_FORMAT_NONE: return AV_PIX_FMT_NONE;
- case VIDEO_FORMAT_I420: return AV_PIX_FMT_YUV420P;
- case VIDEO_FORMAT_NV12: return AV_PIX_FMT_NV12;
- case VIDEO_FORMAT_YVYU: return AV_PIX_FMT_NONE;
- case VIDEO_FORMAT_YUY2: return AV_PIX_FMT_YUYV422;
- case VIDEO_FORMAT_UYVY: return AV_PIX_FMT_UYVY422;
- case VIDEO_FORMAT_YUVX: return AV_PIX_FMT_NONE;
- case VIDEO_FORMAT_UYVX: return AV_PIX_FMT_NONE;
- case VIDEO_FORMAT_RGBA: return AV_PIX_FMT_RGBA;
- case VIDEO_FORMAT_BGRA: return AV_PIX_FMT_BGRA;
- case VIDEO_FORMAT_BGRX: return AV_PIX_FMT_BGRA;
- }
- return AV_PIX_FMT_NONE;
- }
- static bool new_stream(struct ffmpeg_data *data, AVStream **stream,
- AVCodec **codec, enum AVCoecID id)
- {
- *codec = avcodec_find_encoder(id);
- if (!*codec) {
- blog(LOG_ERROR, "Couldn't find encoder '%s'",
- avcodec_get_name(id));
- return false;
- }
- *stream = avformat_new_stream(data->output, *codec);
- if (!*stream) {
- blog(LOG_ERROR, "Couldn't create stream for encoder '%s'",
- avcodec_get_name(id));
- return false;
- }
- (*stream)->id = data->output->nb_streams-1;
- return true;
- }
- static bool open_video_codec(struct ffmpeg_data *data,
- struct obs_video_info *ovi)
- {
- AVCodecContext *context = data->video->codec;
- int ret;
- ret = avcodec_open2(context, data->vcodec, NULL);
- if (ret < 0) {
- blog(LOG_ERROR, "Failed to open video codec: %s",
- av_err2str(ret));
- return false;
- }
- data->vframe = av_frame_alloc();
- if (!data->vframe) {
- blog(LOG_ERROR, "Failed to allocate video frame");
- return false;
- }
- data->vframe->format = context->pix_fmt;
- data->vframe->width = context->width;
- data->vframe->height = context->height;
- ret = avpicture_alloc(&data->dst_picture, context->pix_fmt,
- context->width, context->height);
- if (ret < 0) {
- blog(LOG_ERROR, "Failed to allocate dst_picture: %s",
- av_err2str(ret));
- return false;
- }
- *((AVPicture*)data->vframe) = data->dst_picture;
- return true;
- }
- static bool init_swscale(struct ffmpeg_data *data, AVCodecContext *context)
- {
- data->swscale = sws_getContext(
- context->width, context->height, AV_PIX_FMT_YUV420P,
- context->width, context->height, context->pix_fmt,
- SWS_BICUBIC, NULL, NULL, NULL);
- if (!data->swscale) {
- blog(LOG_ERROR, "Could not initialize swscale");
- return false;
- }
- return true;
- }
- static bool create_video_stream(struct ffmpeg_data *data)
- {
- AVCodecContext *context;
- struct obs_video_info ovi;
- if (!obs_get_video_info(&ovi)) {
- blog(LOG_ERROR, "No active video");
- return false;
- }
- if (!new_stream(data, &data->video, &data->vcodec,
- data->output->oformat->video_codec))
- return false;
- context = data->video->codec;
- context->codec_id = data->output->oformat->video_codec;
- context->bit_rate = 6000000;
- context->width = ovi.output_width;
- context->height = ovi.output_height;
- context->time_base.num = ovi.fps_den;
- context->time_base.den = ovi.fps_num;
- context->gop_size = 12;
- context->pix_fmt = AV_PIX_FMT_YUV420P;
- if (data->output->oformat->flags & AVFMT_GLOBALHEADER)
- context->flags |= CODEC_FLAG_GLOBAL_HEADER;
- if (!open_video_codec(data, &ovi))
- return false;
- if (context->pix_fmt != AV_PIX_FMT_YUV420P)
- if (!init_swscale(data, context))
- return false;
- return true;
- }
- static bool open_audio_codec(struct ffmpeg_data *data,
- struct audio_output_info *aoi)
- {
- AVCodecContext *context = data->audio->codec;
- int ret;
- data->aframe = av_frame_alloc();
- if (!data->aframe) {
- blog(LOG_ERROR, "Failed to allocate audio frame");
- return false;
- }
- context->strict_std_compliance = -2;
- ret = avcodec_open2(context, data->acodec, NULL);
- if (ret < 0) {
- blog(LOG_ERROR, "Failed to open audio codec: %s",
- av_err2str(ret));
- return false;
- }
- return true;
- }
- static bool create_audio_stream(struct ffmpeg_data *data)
- {
- AVCodecContext *context;
- struct audio_output_info aoi;
- if (!obs_get_audio_info(&aoi)) {
- blog(LOG_ERROR, "No active audio");
- return false;
- }
- if (!new_stream(data, &data->audio, &data->acodec,
- data->output->oformat->audio_codec))
- return false;
- context = data->audio->codec;
- context->bit_rate = 128000;
- context->channels = get_audio_channels(aoi.speakers);
- context->sample_rate = aoi.samples_per_sec;
- context->sample_fmt = data->acodec->sample_fmts ?
- data->acodec->sample_fmts[0] : AV_SAMPLE_FMT_FLTP;
- if (data->output->oformat->flags & AVFMT_GLOBALHEADER)
- context->flags |= CODEC_FLAG_GLOBAL_HEADER;
- return open_audio_codec(data, &aoi);
- }
- static inline bool init_streams(struct ffmpeg_data *data)
- {
- AVOutputFormat *format = data->output->oformat;
- if (format->video_codec != AV_CODEC_ID_NONE)
- if (!create_video_stream(data))
- return false;
- if (format->audio_codec != AV_CODEC_ID_NONE)
- if (!create_audio_stream(data))
- return false;
- return true;
- }
- static inline bool open_output_file(struct ffmpeg_data *data)
- {
- AVOutputFormat *format = data->output->oformat;
- int ret;
- if ((format->flags & AVFMT_NOFILE) == 0) {
- ret = avio_open(&data->output->pb, FILENAME_TODO,
- AVIO_FLAG_WRITE);
- if (ret < 0) {
- blog(LOG_ERROR, "Couldn't open file '%s', %s",
- FILENAME_TODO, av_err2str(ret));
- return false;
- }
- }
- ret = avformat_write_header(data->output, NULL);
- if (ret < 0) {
- blog(LOG_ERROR, "Error opening file '%s': %s",
- FILENAME_TODO, av_err2str(ret));
- return false;
- }
- return true;
- }
- static void close_video(struct ffmpeg_data *data)
- {
- avcodec_close(data->video->codec);
- avpicture_free(&data->dst_picture);
- av_frame_free(&data->vframe);
- }
- static void close_audio(struct ffmpeg_data *data)
- {
- av_freep(&data->samples[0]);
- avcodec_close(data->audio->codec);
- av_frame_free(&data->aframe);
- }
- static void ffmpeg_data_free(struct ffmpeg_data *data)
- {
- if (data->initialized)
- av_write_trailer(data->output);
- if (data->video)
- close_video(data);
- if (data->audio)
- close_audio(data);
- if ((data->output->oformat->flags & AVFMT_NOFILE) == 0)
- avio_close(data->output->pb);
- avformat_free_context(data->output);
- memset(data, 0, sizeof(struct ffmpeg_data));
- }
- static bool ffmpeg_data_init(struct ffmpeg_data *data)
- {
- memset(data, 0, sizeof(struct ffmpeg_data));
- av_register_all();
- /* TODO: settings */
- avformat_alloc_output_context2(&data->output, NULL, NULL,
- FILENAME_TODO);
- if (!data->output) {
- blog(LOG_ERROR, "Couldn't create avformat context");
- goto fail;
- }
- if (!init_streams(data))
- goto fail;
- if (!open_output_file(data))
- goto fail;
- data->initialized = true;
- return true;
- fail:
- blog(LOG_ERROR, "ffmpeg_data_init failed");
- ffmpeg_data_free(data);
- return false;
- }
- /* ------------------------------------------------------------------------- */
- const char *ffmpeg_output_getname(const char *locale)
- {
- /* TODO: translation */
- return "FFmpeg file output";
- }
- void test_callback(void *param, int bla, const char *format, va_list args)
- {
- blogva(LOG_INFO, format, args);
- }
- struct ffmpeg_output *ffmpeg_output_create(const char *settings,
- obs_output_t output)
- {
- struct ffmpeg_output *data = bmalloc(sizeof(struct ffmpeg_output));
- memset(data, 0, sizeof(struct ffmpeg_output));
- data->output = output;
- av_log_set_callback(test_callback);
- return data;
- }
- void ffmpeg_output_destroy(struct ffmpeg_output *data)
- {
- if (data) {
- if (data->active)
- ffmpeg_data_free(&data->ff_data);
- bfree(data);
- }
- }
- void ffmpeg_output_update(struct ffmpeg_output *data, const char *settings)
- {
- }
- static inline int64_t rescale_ts(int64_t val, AVCodecContext *context,
- AVStream *stream)
- {
- return av_rescale_q_rnd(val, context->time_base,
- stream->time_base,
- AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX);
- }
- #define YUV420_PLANES 3
- static inline void copy_data(AVPicture *pic, const struct video_frame *frame,
- int height)
- {
- for (int plane = 0; plane < YUV420_PLANES; plane++) {
- int frame_rowsize = (int)frame->row_size[plane];
- int pic_rowsize = pic->linesize[plane];
- int bytes = frame_rowsize < pic_rowsize ?
- frame_rowsize : pic_rowsize;
- int plane_height = plane == 0 ? height : height/2;
- for (int y = 0; y < plane_height; y++) {
- int pos_frame = y * frame_rowsize;
- int pos_pic = y * pic_rowsize;
- memcpy(pic->data[plane] + pos_pic,
- frame->data[plane] + pos_frame,
- bytes);
- }
- }
- }
- static void receive_video(void *param, const struct video_frame *frame)
- {
- struct ffmpeg_output *output = param;
- struct ffmpeg_data *data = &output->ff_data;
- AVCodecContext *context = data->video->codec;
- AVPacket packet = {0};
- int ret, got_packet;
- av_init_packet(&packet);
- if (context->pix_fmt != AV_PIX_FMT_YUV420P)
- sws_scale(data->swscale, frame->data, frame->row_size,
- 0, context->height, data->dst_picture.data,
- data->dst_picture.linesize);
- else
- copy_data(&data->dst_picture, frame, context->height);
- if (data->output->flags & AVFMT_RAWPICTURE) {
- packet.flags |= AV_PKT_FLAG_KEY;
- packet.stream_index = data->video->index;
- packet.data = data->dst_picture.data[0];
- packet.size = sizeof(AVPicture);
- ret = av_interleaved_write_frame(data->output, &packet);
- } else {
- data->vframe->pts = data->total_frames;
- ret = avcodec_encode_video2(context, &packet, data->vframe,
- &got_packet);
- if (ret < 0) {
- blog(LOG_ERROR, "receive_video: Error encoding "
- "video: %s", av_err2str(ret));
- return;
- }
- if (!ret && got_packet && packet.size) {
- packet.pts = rescale_ts(packet.pts, context,
- data->video);
- packet.dts = rescale_ts(packet.dts, context,
- data->video);
- packet.duration = (int)av_rescale_q(packet.duration,
- context->time_base,
- data->video->time_base);
- ret = av_interleaved_write_frame(data->output, &packet);
- } else {
- ret = 0;
- }
- }
- if (ret != 0) {
- blog(LOG_ERROR, "receive_video: Error writing video: %s",
- av_err2str(ret));
- }
- data->total_frames++;
- }
- static void receive_audio(void *param, const struct audio_data *frame)
- {
- struct ffmpeg_output *output = param;
- struct ffmpeg_data *data = &output->ff_data;
- AVCodecContext *context = data->audio->codec;
- AVPacket packet = {0};
- int channels = (int)audio_output_channels(obs_audio());
- size_t planes = audio_output_planes(obs_audio());
- int ret, got_packet;
- data->aframe->nb_samples = frame->frames;
- data->aframe->pts = av_rescale_q(data->total_samples,
- (AVRational){1, context->sample_rate},
- context->time_base);
- if (!data->samples[0])
- av_samples_alloc(data->samples, NULL, channels,
- frame->frames, context->sample_fmt, 0);
- for (size_t i = 0; i < planes; i++) {
- /* TODO */
- }
- data->total_samples += frame->frames;
- ret = avcodec_encode_audio2(context, &packet, data->aframe,
- &got_packet);
- if (ret < 0) {
- blog(LOG_ERROR, "receive_audio: Error encoding audio: %s",
- av_err2str(ret));
- return;
- }
- if (!got_packet)
- return;
- packet.pts = rescale_ts(packet.pts, context, data->audio);
- packet.dts = rescale_ts(packet.dts, context, data->audio);
- packet.duration = (int)av_rescale_q(packet.duration, context->time_base,
- data->audio->time_base);
- packet.stream_index = data->audio->index;
- ret = av_interleaved_write_frame(data->output, &packet);
- if (ret != 0)
- blog(LOG_ERROR, "receive_audio: Error writing audio: %s",
- av_err2str(ret));
- }
- bool ffmpeg_output_start(struct ffmpeg_output *data)
- {
- video_t video = obs_video();
- audio_t audio = obs_audio();
- if (!video || !audio) {
- blog(LOG_ERROR, "ffmpeg_output_start: audio and video must "
- "both be active (at least as of this writing)");
- return false;
- }
- if (!ffmpeg_data_init(&data->ff_data))
- return false;
- struct audio_convert_info aci;
- aci.samples_per_sec = SPS_TODO;
- aci.format = AUDIO_FORMAT_FLOAT;
- aci.speakers = SPEAKERS_STEREO;
- struct video_convert_info vci;
- vci.format = VIDEO_FORMAT_I420;
- vci.width = 0;
- vci.height = 0;
- vci.row_align = 1;
- //video_output_connect(video, &vci, receive_video, data);
- //audio_output_connect(audio, &aci, receive_audio, data);
- data->active = true;
- return true;
- }
- void ffmpeg_output_stop(struct ffmpeg_output *data)
- {
- if (data->active) {
- data->active = false;
- video_output_disconnect(obs_video(), receive_video, data);
- audio_output_disconnect(obs_audio(), receive_audio, data);
- ffmpeg_data_free(&data->ff_data);
- }
- }
- bool ffmpeg_output_active(struct ffmpeg_output *data)
- {
- return data->active;
- }
|