obs-ffmpeg-output.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325
  1. /******************************************************************************
  2. Copyright (C) 2014 by Hugh Bailey <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ******************************************************************************/
  14. #include <obs-module.h>
  15. #include <util/circlebuf.h>
  16. #include <util/threading.h>
  17. #include <util/dstr.h>
  18. #include <util/darray.h>
  19. #include <util/platform.h>
  20. #include "obs-ffmpeg-output.h"
  21. #include "obs-ffmpeg-formats.h"
  22. #include "obs-ffmpeg-compat.h"
  23. #include <libavutil/channel_layout.h>
  24. #include <libavutil/mastering_display_metadata.h>
  25. /* ------------------------------------------------------------------------- */
  26. static void ffmpeg_output_set_last_error(struct ffmpeg_data *data,
  27. const char *error)
  28. {
  29. if (data->last_error)
  30. bfree(data->last_error);
  31. data->last_error = bstrdup(error);
  32. }
  33. void ffmpeg_log_error(int log_level, struct ffmpeg_data *data,
  34. const char *format, ...)
  35. {
  36. va_list args;
  37. char out[4096];
  38. va_start(args, format);
  39. vsnprintf(out, sizeof(out), format, args);
  40. va_end(args);
  41. ffmpeg_output_set_last_error(data, out);
  42. blog(log_level, "%s", out);
  43. }
  44. static bool new_stream(struct ffmpeg_data *data, AVStream **stream,
  45. const AVCodec **codec, enum AVCodecID id,
  46. const char *name)
  47. {
  48. *codec = (!!name && *name) ? avcodec_find_encoder_by_name(name)
  49. : avcodec_find_encoder(id);
  50. if (!*codec) {
  51. ffmpeg_log_error(LOG_WARNING, data,
  52. "Couldn't find encoder '%s'",
  53. avcodec_get_name(id));
  54. return false;
  55. }
  56. *stream = avformat_new_stream(data->output, *codec);
  57. if (!*stream) {
  58. ffmpeg_log_error(LOG_WARNING, data,
  59. "Couldn't create stream for encoder '%s'",
  60. avcodec_get_name(id));
  61. return false;
  62. }
  63. (*stream)->id = data->output->nb_streams - 1;
  64. return true;
  65. }
  66. static bool parse_params(AVCodecContext *context, char **opts)
  67. {
  68. bool ret = true;
  69. if (!context || !context->priv_data)
  70. return true;
  71. while (*opts) {
  72. char *opt = *opts;
  73. char *assign = strchr(opt, '=');
  74. if (assign) {
  75. char *name = opt;
  76. char *value;
  77. *assign = 0;
  78. value = assign + 1;
  79. if (av_opt_set(context, name, value,
  80. AV_OPT_SEARCH_CHILDREN)) {
  81. blog(LOG_WARNING, "Failed to set %s=%s", name,
  82. value);
  83. ret = false;
  84. }
  85. }
  86. opts++;
  87. }
  88. return ret;
  89. }
  90. static bool open_video_codec(struct ffmpeg_data *data)
  91. {
  92. AVCodecContext *const context = data->video_ctx;
  93. char **opts = strlist_split(data->config.video_settings, ' ', false);
  94. int ret;
  95. if (strcmp(data->vcodec->name, "libx264") == 0)
  96. av_opt_set(context->priv_data, "preset", "veryfast", 0);
  97. if (opts) {
  98. // libav requires x264 parameters in a special format which may be non-obvious
  99. if (!parse_params(context, opts) &&
  100. strcmp(data->vcodec->name, "libx264") == 0)
  101. blog(LOG_WARNING,
  102. "If you're trying to set x264 parameters, use x264-params=name=value:name=value");
  103. strlist_free(opts);
  104. }
  105. ret = avcodec_open2(context, data->vcodec, NULL);
  106. if (ret < 0) {
  107. ffmpeg_log_error(LOG_WARNING, data,
  108. "Failed to open video codec: %s",
  109. av_err2str(ret));
  110. return false;
  111. }
  112. data->vframe = av_frame_alloc();
  113. if (!data->vframe) {
  114. ffmpeg_log_error(LOG_WARNING, data,
  115. "Failed to allocate video frame");
  116. return false;
  117. }
  118. data->vframe->format = context->pix_fmt;
  119. data->vframe->width = context->width;
  120. data->vframe->height = context->height;
  121. data->vframe->color_range = data->config.color_range;
  122. data->vframe->color_primaries = data->config.color_primaries;
  123. data->vframe->color_trc = data->config.color_trc;
  124. data->vframe->colorspace = data->config.colorspace;
  125. data->vframe->chroma_location =
  126. (data->config.colorspace == AVCOL_SPC_BT2020_NCL)
  127. ? AVCHROMA_LOC_TOPLEFT
  128. : AVCHROMA_LOC_LEFT;
  129. ret = av_frame_get_buffer(data->vframe, base_get_alignment());
  130. if (ret < 0) {
  131. ffmpeg_log_error(LOG_WARNING, data,
  132. "Failed to allocate vframe: %s",
  133. av_err2str(ret));
  134. return false;
  135. }
  136. #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 48, 101)
  137. avcodec_parameters_from_context(data->video->codecpar, context);
  138. #endif
  139. return true;
  140. }
  141. static bool init_swscale(struct ffmpeg_data *data, AVCodecContext *context)
  142. {
  143. data->swscale = sws_getContext(
  144. data->config.width, data->config.height, data->config.format,
  145. data->config.scale_width, data->config.scale_height,
  146. context->pix_fmt, SWS_BICUBIC, NULL, NULL, NULL);
  147. if (!data->swscale) {
  148. ffmpeg_log_error(LOG_WARNING, data,
  149. "Could not initialize swscale");
  150. return false;
  151. }
  152. return true;
  153. }
  154. static bool create_video_stream(struct ffmpeg_data *data)
  155. {
  156. enum AVPixelFormat closest_format;
  157. AVCodecContext *context;
  158. struct obs_video_info ovi;
  159. if (!obs_get_video_info(&ovi)) {
  160. ffmpeg_log_error(LOG_WARNING, data, "No active video");
  161. return false;
  162. }
  163. if (!new_stream(data, &data->video, &data->vcodec,
  164. data->output->oformat->video_codec,
  165. data->config.video_encoder))
  166. return false;
  167. const enum AVColorTransferCharacteristic trc = data->config.color_trc;
  168. const bool pq = trc == AVCOL_TRC_SMPTE2084;
  169. const bool hlg = trc == AVCOL_TRC_ARIB_STD_B67;
  170. if (pq || hlg) {
  171. const int hdr_nominal_peak_level =
  172. pq ? (int)obs_get_video_hdr_nominal_peak_level()
  173. : (hlg ? 1000 : 0);
  174. size_t content_size;
  175. AVContentLightMetadata *const content =
  176. av_content_light_metadata_alloc(&content_size);
  177. content->MaxCLL = hdr_nominal_peak_level;
  178. content->MaxFALL = hdr_nominal_peak_level;
  179. av_stream_add_side_data(data->video,
  180. AV_PKT_DATA_CONTENT_LIGHT_LEVEL,
  181. (uint8_t *)content, content_size);
  182. AVMasteringDisplayMetadata *const mastering =
  183. av_mastering_display_metadata_alloc();
  184. mastering->display_primaries[0][0] = av_make_q(17, 25);
  185. mastering->display_primaries[0][1] = av_make_q(8, 25);
  186. mastering->display_primaries[1][0] = av_make_q(53, 200);
  187. mastering->display_primaries[1][1] = av_make_q(69, 100);
  188. mastering->display_primaries[2][0] = av_make_q(3, 20);
  189. mastering->display_primaries[2][1] = av_make_q(3, 50);
  190. mastering->white_point[0] = av_make_q(3127, 10000);
  191. mastering->white_point[1] = av_make_q(329, 1000);
  192. mastering->min_luminance = av_make_q(0, 1);
  193. mastering->max_luminance = av_make_q(hdr_nominal_peak_level, 1);
  194. mastering->has_primaries = 1;
  195. mastering->has_luminance = 1;
  196. av_stream_add_side_data(data->video,
  197. AV_PKT_DATA_MASTERING_DISPLAY_METADATA,
  198. (uint8_t *)mastering,
  199. sizeof(*mastering));
  200. }
  201. closest_format = data->config.format;
  202. if (data->vcodec->pix_fmts) {
  203. closest_format = avcodec_find_best_pix_fmt_of_list(
  204. data->vcodec->pix_fmts, data->config.format, 0, NULL);
  205. }
  206. #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 48, 101)
  207. context = avcodec_alloc_context3(data->vcodec);
  208. #else
  209. context = data->video->codec;
  210. #endif
  211. context->bit_rate = (int64_t)data->config.video_bitrate * 1000;
  212. context->width = data->config.scale_width;
  213. context->height = data->config.scale_height;
  214. context->time_base = (AVRational){ovi.fps_den, ovi.fps_num};
  215. context->gop_size = data->config.gop_size;
  216. context->pix_fmt = closest_format;
  217. context->color_range = data->config.color_range;
  218. context->color_primaries = data->config.color_primaries;
  219. context->color_trc = data->config.color_trc;
  220. context->colorspace = data->config.colorspace;
  221. context->chroma_sample_location =
  222. (data->config.colorspace == AVCOL_SPC_BT2020_NCL)
  223. ? AVCHROMA_LOC_TOPLEFT
  224. : AVCHROMA_LOC_LEFT;
  225. context->thread_count = 0;
  226. data->video->time_base = context->time_base;
  227. if (data->output->oformat->flags & AVFMT_GLOBALHEADER)
  228. context->flags |= CODEC_FLAG_GLOBAL_H;
  229. data->video_ctx = context;
  230. if (!open_video_codec(data))
  231. return false;
  232. if (context->pix_fmt != data->config.format ||
  233. data->config.width != data->config.scale_width ||
  234. data->config.height != data->config.scale_height) {
  235. if (!init_swscale(data, context))
  236. return false;
  237. }
  238. return true;
  239. }
  240. static bool open_audio_codec(struct ffmpeg_data *data, int idx)
  241. {
  242. AVCodecContext *const context = data->audio_infos[idx].ctx;
  243. char **opts = strlist_split(data->config.audio_settings, ' ', false);
  244. int ret;
  245. int channels;
  246. if (opts) {
  247. parse_params(context, opts);
  248. strlist_free(opts);
  249. }
  250. data->aframe[idx] = av_frame_alloc();
  251. if (!data->aframe[idx]) {
  252. ffmpeg_log_error(LOG_WARNING, data,
  253. "Failed to allocate audio frame");
  254. return false;
  255. }
  256. data->aframe[idx]->format = context->sample_fmt;
  257. #if LIBAVUTIL_VERSION_INT < AV_VERSION_INT(57, 24, 100)
  258. data->aframe[idx]->channels = context->channels;
  259. data->aframe[idx]->channel_layout = context->channel_layout;
  260. channels = context->channels;
  261. #else
  262. data->aframe[idx]->ch_layout = context->ch_layout;
  263. channels = context->ch_layout.nb_channels;
  264. #endif
  265. data->aframe[idx]->sample_rate = context->sample_rate;
  266. context->strict_std_compliance = -2;
  267. ret = avcodec_open2(context, data->acodec, NULL);
  268. if (ret < 0) {
  269. ffmpeg_log_error(LOG_WARNING, data,
  270. "Failed to open audio codec: %s",
  271. av_err2str(ret));
  272. return false;
  273. }
  274. data->frame_size = context->frame_size ? context->frame_size : 1024;
  275. ret = av_samples_alloc(data->samples[idx], NULL, channels,
  276. data->frame_size, context->sample_fmt, 0);
  277. if (ret < 0) {
  278. ffmpeg_log_error(LOG_WARNING, data,
  279. "Failed to create audio buffer: %s",
  280. av_err2str(ret));
  281. return false;
  282. }
  283. #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 48, 101)
  284. avcodec_parameters_from_context(data->audio_infos[idx].stream->codecpar,
  285. context);
  286. #endif
  287. return true;
  288. }
  289. static bool create_audio_stream(struct ffmpeg_data *data, int idx)
  290. {
  291. AVCodecContext *context;
  292. AVStream *stream;
  293. struct obs_audio_info aoi;
  294. int channels;
  295. if (!obs_get_audio_info(&aoi)) {
  296. ffmpeg_log_error(LOG_WARNING, data, "No active audio");
  297. return false;
  298. }
  299. if (!new_stream(data, &stream, &data->acodec,
  300. data->output->oformat->audio_codec,
  301. data->config.audio_encoder))
  302. return false;
  303. #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 48, 101)
  304. context = avcodec_alloc_context3(data->acodec);
  305. #else
  306. context = stream->codec;
  307. #endif
  308. context->bit_rate = (int64_t)data->config.audio_bitrate * 1000;
  309. context->time_base = (AVRational){1, aoi.samples_per_sec};
  310. #if LIBAVUTIL_VERSION_INT < AV_VERSION_INT(57, 24, 100)
  311. context->channels = get_audio_channels(aoi.speakers);
  312. #endif
  313. channels = get_audio_channels(aoi.speakers);
  314. context->sample_rate = aoi.samples_per_sec;
  315. #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(59, 24, 100)
  316. context->channel_layout = av_get_default_channel_layout(channels);
  317. //avutil default channel layout for 5 channels is 5.0 ; fix for 4.1
  318. if (aoi.speakers == SPEAKERS_4POINT1)
  319. context->channel_layout = av_get_channel_layout("4.1");
  320. #else
  321. av_channel_layout_default(&context->ch_layout, channels);
  322. if (aoi.speakers == SPEAKERS_4POINT1)
  323. context->ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_4POINT1;
  324. #endif
  325. context->sample_fmt = data->acodec->sample_fmts
  326. ? data->acodec->sample_fmts[0]
  327. : AV_SAMPLE_FMT_FLTP;
  328. stream->time_base = context->time_base;
  329. data->audio_samplerate = aoi.samples_per_sec;
  330. data->audio_format = convert_ffmpeg_sample_format(context->sample_fmt);
  331. data->audio_planes = get_audio_planes(data->audio_format, aoi.speakers);
  332. data->audio_size = get_audio_size(data->audio_format, aoi.speakers, 1);
  333. if (data->output->oformat->flags & AVFMT_GLOBALHEADER)
  334. context->flags |= CODEC_FLAG_GLOBAL_H;
  335. data->audio_infos[idx].stream = stream;
  336. data->audio_infos[idx].ctx = context;
  337. return open_audio_codec(data, idx);
  338. }
  339. static inline bool init_streams(struct ffmpeg_data *data)
  340. {
  341. const AVOutputFormat *format = data->output->oformat;
  342. if (format->video_codec != AV_CODEC_ID_NONE)
  343. if (!create_video_stream(data))
  344. return false;
  345. if (format->audio_codec != AV_CODEC_ID_NONE &&
  346. data->num_audio_streams) {
  347. data->audio_infos = calloc(data->num_audio_streams,
  348. sizeof(*data->audio_infos));
  349. for (int i = 0; i < data->num_audio_streams; i++) {
  350. if (!create_audio_stream(data, i))
  351. return false;
  352. }
  353. }
  354. return true;
  355. }
  356. static inline bool open_output_file(struct ffmpeg_data *data)
  357. {
  358. const AVOutputFormat *format = data->output->oformat;
  359. int ret;
  360. AVDictionary *dict = NULL;
  361. if ((ret = av_dict_parse_string(&dict, data->config.muxer_settings, "=",
  362. " ", 0))) {
  363. ffmpeg_log_error(LOG_WARNING, data,
  364. "Failed to parse muxer settings: %s\n%s",
  365. av_err2str(ret), data->config.muxer_settings);
  366. av_dict_free(&dict);
  367. return false;
  368. }
  369. if (av_dict_count(dict) > 0) {
  370. struct dstr str = {0};
  371. AVDictionaryEntry *entry = NULL;
  372. while ((entry = av_dict_get(dict, "", entry,
  373. AV_DICT_IGNORE_SUFFIX)))
  374. dstr_catf(&str, "\n\t%s=%s", entry->key, entry->value);
  375. blog(LOG_INFO, "Using muxer settings: %s", str.array);
  376. dstr_free(&str);
  377. }
  378. if ((format->flags & AVFMT_NOFILE) == 0) {
  379. ret = avio_open2(&data->output->pb, data->config.url,
  380. AVIO_FLAG_WRITE, NULL, &dict);
  381. if (ret < 0) {
  382. ffmpeg_log_error(LOG_WARNING, data,
  383. "Couldn't open '%s', %s",
  384. data->config.url, av_err2str(ret));
  385. av_dict_free(&dict);
  386. return false;
  387. }
  388. }
  389. ret = avformat_write_header(data->output, &dict);
  390. if (ret < 0) {
  391. ffmpeg_log_error(LOG_WARNING, data, "Error opening '%s': %s",
  392. data->config.url, av_err2str(ret));
  393. return false;
  394. }
  395. if (av_dict_count(dict) > 0) {
  396. struct dstr str = {0};
  397. AVDictionaryEntry *entry = NULL;
  398. while ((entry = av_dict_get(dict, "", entry,
  399. AV_DICT_IGNORE_SUFFIX)))
  400. dstr_catf(&str, "\n\t%s=%s", entry->key, entry->value);
  401. blog(LOG_INFO, "Invalid muxer settings: %s", str.array);
  402. dstr_free(&str);
  403. }
  404. av_dict_free(&dict);
  405. return true;
  406. }
  407. static void close_video(struct ffmpeg_data *data)
  408. {
  409. avcodec_free_context(&data->video_ctx);
  410. av_frame_unref(data->vframe);
  411. // This format for some reason derefs video frame
  412. // too many times
  413. if (data->vcodec->id == AV_CODEC_ID_A64_MULTI ||
  414. data->vcodec->id == AV_CODEC_ID_A64_MULTI5)
  415. return;
  416. av_frame_free(&data->vframe);
  417. }
  418. static void close_audio(struct ffmpeg_data *data)
  419. {
  420. for (int idx = 0; idx < data->num_audio_streams; idx++) {
  421. for (size_t i = 0; i < MAX_AV_PLANES; i++)
  422. circlebuf_free(&data->excess_frames[idx][i]);
  423. if (data->samples[idx][0])
  424. av_freep(&data->samples[idx][0]);
  425. if (data->audio_infos[idx].ctx)
  426. avcodec_free_context(&data->audio_infos[idx].ctx);
  427. if (data->aframe[idx])
  428. av_frame_free(&data->aframe[idx]);
  429. }
  430. }
  431. void ffmpeg_data_free(struct ffmpeg_data *data)
  432. {
  433. if (data->initialized)
  434. av_write_trailer(data->output);
  435. if (data->video)
  436. close_video(data);
  437. if (data->audio_infos) {
  438. close_audio(data);
  439. free(data->audio_infos);
  440. data->audio_infos = NULL;
  441. }
  442. if (data->output) {
  443. if ((data->output->oformat->flags & AVFMT_NOFILE) == 0)
  444. avio_close(data->output->pb);
  445. avformat_free_context(data->output);
  446. }
  447. if (data->last_error)
  448. bfree(data->last_error);
  449. memset(data, 0, sizeof(struct ffmpeg_data));
  450. }
  451. static inline const char *safe_str(const char *s)
  452. {
  453. if (s == NULL)
  454. return "(NULL)";
  455. else
  456. return s;
  457. }
  458. static enum AVCodecID get_codec_id(const char *name, int id)
  459. {
  460. const AVCodec *codec;
  461. if (id != 0)
  462. return (enum AVCodecID)id;
  463. if (!name || !*name)
  464. return AV_CODEC_ID_NONE;
  465. codec = avcodec_find_encoder_by_name(name);
  466. if (!codec)
  467. return AV_CODEC_ID_NONE;
  468. return codec->id;
  469. }
  470. #if LIBAVFORMAT_VERSION_INT < AV_VERSION_INT(59, 0, 100)
  471. static void set_encoder_ids(struct ffmpeg_data *data)
  472. {
  473. data->output->oformat->video_codec = get_codec_id(
  474. data->config.video_encoder, data->config.video_encoder_id);
  475. data->output->oformat->audio_codec = get_codec_id(
  476. data->config.audio_encoder, data->config.audio_encoder_id);
  477. }
  478. #endif
  479. bool ffmpeg_data_init(struct ffmpeg_data *data, struct ffmpeg_cfg *config)
  480. {
  481. bool is_rtmp = false;
  482. memset(data, 0, sizeof(struct ffmpeg_data));
  483. data->config = *config;
  484. data->num_audio_streams = config->audio_mix_count;
  485. data->audio_tracks = config->audio_tracks;
  486. if (!config->url || !*config->url)
  487. return false;
  488. #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(58, 9, 100)
  489. av_register_all();
  490. #endif
  491. avformat_network_init();
  492. is_rtmp = (astrcmpi_n(config->url, "rtmp://", 7) == 0);
  493. #if LIBAVFORMAT_VERSION_INT < AV_VERSION_INT(59, 0, 100)
  494. AVOutputFormat *output_format;
  495. #else
  496. const AVOutputFormat *output_format;
  497. #endif
  498. output_format = av_guess_format(
  499. is_rtmp ? "flv" : data->config.format_name, data->config.url,
  500. is_rtmp ? NULL : data->config.format_mime_type);
  501. if (output_format == NULL) {
  502. ffmpeg_log_error(
  503. LOG_WARNING, data,
  504. "Couldn't find matching output format with "
  505. "parameters: name=%s, url=%s, mime=%s",
  506. safe_str(is_rtmp ? "flv" : data->config.format_name),
  507. safe_str(data->config.url),
  508. safe_str(is_rtmp ? NULL
  509. : data->config.format_mime_type));
  510. goto fail;
  511. }
  512. avformat_alloc_output_context2(&data->output, output_format, NULL,
  513. data->config.url);
  514. if (!data->output) {
  515. ffmpeg_log_error(LOG_WARNING, data,
  516. "Couldn't create avformat context");
  517. goto fail;
  518. }
  519. #if LIBAVFORMAT_VERSION_INT < AV_VERSION_INT(59, 0, 100)
  520. if (is_rtmp) {
  521. data->output->oformat->video_codec = AV_CODEC_ID_H264;
  522. data->output->oformat->audio_codec = AV_CODEC_ID_AAC;
  523. } else {
  524. if (data->config.format_name)
  525. set_encoder_ids(data);
  526. }
  527. #else
  528. if (is_rtmp) {
  529. data->config.audio_encoder = "aac";
  530. data->config.audio_encoder_id = AV_CODEC_ID_AAC;
  531. data->config.video_encoder = "libx264";
  532. data->config.video_encoder_id = AV_CODEC_ID_H264;
  533. }
  534. #endif
  535. if (!init_streams(data))
  536. goto fail;
  537. if (!open_output_file(data))
  538. goto fail;
  539. av_dump_format(data->output, 0, NULL, 1);
  540. data->initialized = true;
  541. return true;
  542. fail:
  543. blog(LOG_WARNING, "ffmpeg_data_init failed");
  544. return false;
  545. }
  546. /* ------------------------------------------------------------------------- */
  547. static inline bool stopping(struct ffmpeg_output *output)
  548. {
  549. return os_atomic_load_bool(&output->stopping);
  550. }
  551. static const char *ffmpeg_output_getname(void *unused)
  552. {
  553. UNUSED_PARAMETER(unused);
  554. return obs_module_text("FFmpegOutput");
  555. }
  556. static void ffmpeg_log_callback(void *param, int level, const char *format,
  557. va_list args)
  558. {
  559. if (level <= AV_LOG_INFO)
  560. blogva(LOG_DEBUG, format, args);
  561. UNUSED_PARAMETER(param);
  562. }
  563. static void *ffmpeg_output_create(obs_data_t *settings, obs_output_t *output)
  564. {
  565. struct ffmpeg_output *data = bzalloc(sizeof(struct ffmpeg_output));
  566. pthread_mutex_init_value(&data->write_mutex);
  567. data->output = output;
  568. if (pthread_mutex_init(&data->write_mutex, NULL) != 0)
  569. goto fail;
  570. if (os_event_init(&data->stop_event, OS_EVENT_TYPE_AUTO) != 0)
  571. goto fail;
  572. if (os_sem_init(&data->write_sem, 0) != 0)
  573. goto fail;
  574. av_log_set_callback(ffmpeg_log_callback);
  575. UNUSED_PARAMETER(settings);
  576. return data;
  577. fail:
  578. pthread_mutex_destroy(&data->write_mutex);
  579. os_event_destroy(data->stop_event);
  580. bfree(data);
  581. return NULL;
  582. }
  583. static void ffmpeg_output_full_stop(void *data);
  584. static void ffmpeg_deactivate(struct ffmpeg_output *output);
  585. static void ffmpeg_output_destroy(void *data)
  586. {
  587. struct ffmpeg_output *output = data;
  588. if (output) {
  589. if (output->connecting)
  590. pthread_join(output->start_thread, NULL);
  591. ffmpeg_output_full_stop(output);
  592. pthread_mutex_destroy(&output->write_mutex);
  593. os_sem_destroy(output->write_sem);
  594. os_event_destroy(output->stop_event);
  595. bfree(data);
  596. }
  597. }
  598. static inline void copy_data(AVFrame *pic, const struct video_data *frame,
  599. int height, enum AVPixelFormat format)
  600. {
  601. int h_chroma_shift, v_chroma_shift;
  602. av_pix_fmt_get_chroma_sub_sample(format, &h_chroma_shift,
  603. &v_chroma_shift);
  604. for (int plane = 0; plane < MAX_AV_PLANES; plane++) {
  605. if (!frame->data[plane])
  606. continue;
  607. int frame_rowsize = (int)frame->linesize[plane];
  608. int pic_rowsize = pic->linesize[plane];
  609. int bytes = frame_rowsize < pic_rowsize ? frame_rowsize
  610. : pic_rowsize;
  611. int plane_height = height >> (plane ? v_chroma_shift : 0);
  612. for (int y = 0; y < plane_height; y++) {
  613. int pos_frame = y * frame_rowsize;
  614. int pos_pic = y * pic_rowsize;
  615. memcpy(pic->data[plane] + pos_pic,
  616. frame->data[plane] + pos_frame, bytes);
  617. }
  618. }
  619. }
  620. static void receive_video(void *param, struct video_data *frame)
  621. {
  622. struct ffmpeg_output *output = param;
  623. struct ffmpeg_data *data = &output->ff_data;
  624. // codec doesn't support video or none configured
  625. if (!data->video)
  626. return;
  627. AVCodecContext *context = data->video_ctx;
  628. AVPacket *packet = NULL;
  629. int ret = 0, got_packet;
  630. if (!output->video_start_ts)
  631. output->video_start_ts = frame->timestamp;
  632. if (!data->start_timestamp)
  633. data->start_timestamp = frame->timestamp;
  634. ret = av_frame_make_writable(data->vframe);
  635. if (ret < 0) {
  636. blog(LOG_WARNING,
  637. "receive_video: Error obtaining writable "
  638. "AVFrame: %s",
  639. av_err2str(ret));
  640. //FIXME: stop the encode with an error
  641. return;
  642. }
  643. if (!!data->swscale)
  644. sws_scale(data->swscale, (const uint8_t *const *)frame->data,
  645. (const int *)frame->linesize, 0, data->config.height,
  646. data->vframe->data, data->vframe->linesize);
  647. else
  648. copy_data(data->vframe, frame, context->height,
  649. context->pix_fmt);
  650. packet = av_packet_alloc();
  651. #if LIBAVFORMAT_VERSION_MAJOR < 58
  652. if (data->output->flags & AVFMT_RAWPICTURE) {
  653. packet->flags |= AV_PKT_FLAG_KEY;
  654. packet->stream_index = data->video->index;
  655. packet->data = data->vframe->data[0];
  656. packet->size = sizeof(AVPicture);
  657. pthread_mutex_lock(&output->write_mutex);
  658. da_push_back(output->packets, &packet);
  659. packet = NULL;
  660. pthread_mutex_unlock(&output->write_mutex);
  661. os_sem_post(output->write_sem);
  662. } else {
  663. #endif
  664. data->vframe->pts = data->total_frames;
  665. #if LIBAVFORMAT_VERSION_INT >= AV_VERSION_INT(57, 40, 101)
  666. ret = avcodec_send_frame(context, data->vframe);
  667. if (ret == 0)
  668. ret = avcodec_receive_packet(context, packet);
  669. got_packet = (ret == 0);
  670. if (ret == AVERROR_EOF || ret == AVERROR(EAGAIN))
  671. ret = 0;
  672. #else
  673. ret = avcodec_encode_video2(context, packet, data->vframe, &got_packet);
  674. #endif
  675. if (ret < 0) {
  676. blog(LOG_WARNING,
  677. "receive_video: Error encoding "
  678. "video: %s",
  679. av_err2str(ret));
  680. //FIXME: stop the encode with an error
  681. goto fail;
  682. }
  683. if (!ret && got_packet && packet->size) {
  684. packet->pts = rescale_ts(packet->pts, context,
  685. data->video->time_base);
  686. packet->dts = rescale_ts(packet->dts, context,
  687. data->video->time_base);
  688. packet->duration = (int)av_rescale_q(
  689. packet->duration, context->time_base,
  690. data->video->time_base);
  691. pthread_mutex_lock(&output->write_mutex);
  692. da_push_back(output->packets, &packet);
  693. packet = NULL;
  694. pthread_mutex_unlock(&output->write_mutex);
  695. os_sem_post(output->write_sem);
  696. } else {
  697. ret = 0;
  698. }
  699. #if LIBAVFORMAT_VERSION_MAJOR < 58
  700. }
  701. #endif
  702. if (ret != 0) {
  703. blog(LOG_WARNING, "receive_video: Error writing video: %s",
  704. av_err2str(ret));
  705. //FIXME: stop the encode with an error
  706. }
  707. data->total_frames++;
  708. fail:
  709. av_packet_free(&packet);
  710. }
  711. static void encode_audio(struct ffmpeg_output *output, int idx,
  712. struct AVCodecContext *context, size_t block_size)
  713. {
  714. struct ffmpeg_data *data = &output->ff_data;
  715. AVPacket *packet = NULL;
  716. int ret, got_packet;
  717. int channels;
  718. #if LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(57, 24, 100)
  719. channels = context->ch_layout.nb_channels;
  720. #else
  721. channels = context->channels;
  722. #endif
  723. size_t total_size = data->frame_size * block_size * channels;
  724. data->aframe[idx]->nb_samples = data->frame_size;
  725. data->aframe[idx]->pts = av_rescale_q(
  726. data->total_samples[idx], (AVRational){1, context->sample_rate},
  727. context->time_base);
  728. ret = avcodec_fill_audio_frame(data->aframe[idx], channels,
  729. context->sample_fmt,
  730. data->samples[idx][0], (int)total_size,
  731. 1);
  732. if (ret < 0) {
  733. blog(LOG_WARNING,
  734. "encode_audio: avcodec_fill_audio_frame "
  735. "failed: %s",
  736. av_err2str(ret));
  737. //FIXME: stop the encode with an error
  738. return;
  739. }
  740. data->total_samples[idx] += data->frame_size;
  741. packet = av_packet_alloc();
  742. #if LIBAVFORMAT_VERSION_INT >= AV_VERSION_INT(57, 40, 101)
  743. ret = avcodec_send_frame(context, data->aframe[idx]);
  744. if (ret == 0)
  745. ret = avcodec_receive_packet(context, packet);
  746. got_packet = (ret == 0);
  747. if (ret == AVERROR_EOF || ret == AVERROR(EAGAIN))
  748. ret = 0;
  749. #else
  750. ret = avcodec_encode_audio2(context, packet, data->aframe[idx],
  751. &got_packet);
  752. #endif
  753. if (ret < 0) {
  754. blog(LOG_WARNING, "encode_audio: Error encoding audio: %s",
  755. av_err2str(ret));
  756. //FIXME: stop the encode with an error
  757. goto fail;
  758. }
  759. if (!got_packet)
  760. goto fail;
  761. packet->pts = rescale_ts(packet->pts, context,
  762. data->audio_infos[idx].stream->time_base);
  763. packet->dts = rescale_ts(packet->dts, context,
  764. data->audio_infos[idx].stream->time_base);
  765. packet->duration =
  766. (int)av_rescale_q(packet->duration, context->time_base,
  767. data->audio_infos[idx].stream->time_base);
  768. packet->stream_index = data->audio_infos[idx].stream->index;
  769. pthread_mutex_lock(&output->write_mutex);
  770. da_push_back(output->packets, &packet);
  771. pthread_mutex_unlock(&output->write_mutex);
  772. os_sem_post(output->write_sem);
  773. return;
  774. fail:
  775. av_packet_free(&packet);
  776. }
  777. /* Given a bitmask for the selected tracks and the mix index,
  778. * this returns the stream index which will be passed to the muxer. */
  779. static int get_track_order(int track_config, size_t mix_index)
  780. {
  781. int position = 0;
  782. for (size_t i = 0; i < mix_index; i++) {
  783. if (track_config & 1 << i)
  784. position++;
  785. }
  786. return position;
  787. }
  788. static void receive_audio(void *param, size_t mix_idx, struct audio_data *frame)
  789. {
  790. struct ffmpeg_output *output = param;
  791. struct ffmpeg_data *data = &output->ff_data;
  792. size_t frame_size_bytes;
  793. struct audio_data in = *frame;
  794. int track_order;
  795. // codec doesn't support audio or none configured
  796. if (!data->audio_infos)
  797. return;
  798. /* check that the track was selected */
  799. if ((data->audio_tracks & (1 << mix_idx)) == 0)
  800. return;
  801. /* get track order (first selected, etc ...) */
  802. track_order = get_track_order(data->audio_tracks, mix_idx);
  803. AVCodecContext *context = data->audio_infos[track_order].ctx;
  804. if (!data->start_timestamp && data->video)
  805. return;
  806. if (!output->audio_start_ts)
  807. output->audio_start_ts = in.timestamp;
  808. frame_size_bytes = (size_t)data->frame_size * data->audio_size;
  809. for (size_t i = 0; i < data->audio_planes; i++)
  810. circlebuf_push_back(&data->excess_frames[track_order][i],
  811. in.data[i], in.frames * data->audio_size);
  812. while (data->excess_frames[track_order][0].size >= frame_size_bytes) {
  813. for (size_t i = 0; i < data->audio_planes; i++)
  814. circlebuf_pop_front(
  815. &data->excess_frames[track_order][i],
  816. data->samples[track_order][i],
  817. frame_size_bytes);
  818. encode_audio(output, track_order, context, data->audio_size);
  819. }
  820. }
  821. static uint64_t get_packet_sys_dts(struct ffmpeg_output *output,
  822. AVPacket *packet)
  823. {
  824. struct ffmpeg_data *data = &output->ff_data;
  825. uint64_t pause_offset = obs_output_get_pause_offset(output->output);
  826. uint64_t start_ts;
  827. AVRational time_base;
  828. if (data->video && data->video->index == packet->stream_index) {
  829. time_base = data->video->time_base;
  830. start_ts = output->video_start_ts;
  831. } else {
  832. time_base = data->audio_infos[0].stream->time_base;
  833. start_ts = output->audio_start_ts;
  834. }
  835. return start_ts + pause_offset +
  836. (uint64_t)av_rescale_q(packet->dts, time_base,
  837. (AVRational){1, 1000000000});
  838. }
  839. static int process_packet(struct ffmpeg_output *output)
  840. {
  841. AVPacket *packet = NULL;
  842. int ret = 0;
  843. pthread_mutex_lock(&output->write_mutex);
  844. if (output->packets.num) {
  845. packet = output->packets.array[0];
  846. da_erase(output->packets, 0);
  847. }
  848. pthread_mutex_unlock(&output->write_mutex);
  849. if (!packet)
  850. return 0;
  851. /*blog(LOG_DEBUG, "size = %d, flags = %lX, stream = %d, "
  852. "packets queued: %lu",
  853. packet.size, packet.flags,
  854. packet.stream_index, output->packets.num);*/
  855. if (stopping(output)) {
  856. uint64_t sys_ts = get_packet_sys_dts(output, packet);
  857. if (sys_ts >= output->stop_ts) {
  858. ret = 0;
  859. goto end;
  860. }
  861. }
  862. output->total_bytes += packet->size;
  863. ret = av_interleaved_write_frame(output->ff_data.output, packet);
  864. if (ret < 0) {
  865. ffmpeg_log_error(LOG_WARNING, &output->ff_data,
  866. "process_packet: Error writing packet: %s",
  867. av_err2str(ret));
  868. }
  869. end:
  870. av_packet_free(&packet);
  871. return ret;
  872. }
  873. static void *write_thread(void *data)
  874. {
  875. struct ffmpeg_output *output = data;
  876. while (os_sem_wait(output->write_sem) == 0) {
  877. /* check to see if shutting down */
  878. if (os_event_try(output->stop_event) == 0)
  879. break;
  880. int ret = process_packet(output);
  881. if (ret != 0) {
  882. int code = OBS_OUTPUT_ERROR;
  883. pthread_detach(output->write_thread);
  884. output->write_thread_active = false;
  885. if (ret == -ENOSPC)
  886. code = OBS_OUTPUT_NO_SPACE;
  887. obs_output_signal_stop(output->output, code);
  888. ffmpeg_deactivate(output);
  889. break;
  890. }
  891. }
  892. output->active = false;
  893. return NULL;
  894. }
  895. static inline const char *get_string_or_null(obs_data_t *settings,
  896. const char *name)
  897. {
  898. const char *value = obs_data_get_string(settings, name);
  899. if (!value || !strlen(value))
  900. return NULL;
  901. return value;
  902. }
  903. static int get_audio_mix_count(int audio_mix_mask)
  904. {
  905. int mix_count = 0;
  906. for (int i = 0; i < MAX_AUDIO_MIXES; i++) {
  907. if ((audio_mix_mask & (1 << i)) != 0) {
  908. mix_count++;
  909. }
  910. }
  911. return mix_count;
  912. }
  913. static bool try_connect(struct ffmpeg_output *output)
  914. {
  915. video_t *video = obs_output_video(output->output);
  916. const struct video_output_info *voi = video_output_get_info(video);
  917. struct ffmpeg_cfg config;
  918. obs_data_t *settings;
  919. bool success;
  920. int ret;
  921. settings = obs_output_get_settings(output->output);
  922. obs_data_set_default_int(settings, "gop_size", 120);
  923. config.url = obs_data_get_string(settings, "url");
  924. config.format_name = get_string_or_null(settings, "format_name");
  925. config.format_mime_type =
  926. get_string_or_null(settings, "format_mime_type");
  927. config.muxer_settings = obs_data_get_string(settings, "muxer_settings");
  928. config.video_bitrate = (int)obs_data_get_int(settings, "video_bitrate");
  929. config.audio_bitrate = (int)obs_data_get_int(settings, "audio_bitrate");
  930. config.gop_size = (int)obs_data_get_int(settings, "gop_size");
  931. config.video_encoder = get_string_or_null(settings, "video_encoder");
  932. config.video_encoder_id =
  933. (int)obs_data_get_int(settings, "video_encoder_id");
  934. config.audio_encoder = get_string_or_null(settings, "audio_encoder");
  935. config.audio_encoder_id =
  936. (int)obs_data_get_int(settings, "audio_encoder_id");
  937. config.video_settings = obs_data_get_string(settings, "video_settings");
  938. config.audio_settings = obs_data_get_string(settings, "audio_settings");
  939. config.scale_width = (int)obs_data_get_int(settings, "scale_width");
  940. config.scale_height = (int)obs_data_get_int(settings, "scale_height");
  941. config.width = (int)obs_output_get_width(output->output);
  942. config.height = (int)obs_output_get_height(output->output);
  943. config.format =
  944. obs_to_ffmpeg_video_format(video_output_get_format(video));
  945. config.audio_tracks = (int)obs_output_get_mixers(output->output);
  946. config.audio_mix_count = get_audio_mix_count(config.audio_tracks);
  947. config.color_range = voi->range == VIDEO_RANGE_FULL ? AVCOL_RANGE_JPEG
  948. : AVCOL_RANGE_MPEG;
  949. config.colorspace = format_is_yuv(voi->format) ? AVCOL_SPC_BT709
  950. : AVCOL_SPC_RGB;
  951. switch (voi->colorspace) {
  952. case VIDEO_CS_601:
  953. config.color_primaries = AVCOL_PRI_SMPTE170M;
  954. config.color_trc = AVCOL_TRC_SMPTE170M;
  955. config.colorspace = AVCOL_SPC_SMPTE170M;
  956. break;
  957. case VIDEO_CS_DEFAULT:
  958. case VIDEO_CS_709:
  959. config.color_primaries = AVCOL_PRI_BT709;
  960. config.color_trc = AVCOL_TRC_BT709;
  961. config.colorspace = AVCOL_SPC_BT709;
  962. break;
  963. case VIDEO_CS_SRGB:
  964. config.color_primaries = AVCOL_PRI_BT709;
  965. config.color_trc = AVCOL_TRC_IEC61966_2_1;
  966. config.colorspace = AVCOL_SPC_BT709;
  967. break;
  968. case VIDEO_CS_2100_PQ:
  969. config.color_primaries = AVCOL_PRI_BT2020;
  970. config.color_trc = AVCOL_TRC_SMPTE2084;
  971. config.colorspace = AVCOL_SPC_BT2020_NCL;
  972. break;
  973. case VIDEO_CS_2100_HLG:
  974. config.color_primaries = AVCOL_PRI_BT2020;
  975. config.color_trc = AVCOL_TRC_ARIB_STD_B67;
  976. config.colorspace = AVCOL_SPC_BT2020_NCL;
  977. break;
  978. }
  979. if (config.format == AV_PIX_FMT_NONE) {
  980. blog(LOG_DEBUG, "invalid pixel format used for FFmpeg output");
  981. return false;
  982. }
  983. if (!config.scale_width)
  984. config.scale_width = config.width;
  985. if (!config.scale_height)
  986. config.scale_height = config.height;
  987. success = ffmpeg_data_init(&output->ff_data, &config);
  988. obs_data_release(settings);
  989. if (!success) {
  990. if (output->ff_data.last_error) {
  991. obs_output_set_last_error(output->output,
  992. output->ff_data.last_error);
  993. }
  994. ffmpeg_data_free(&output->ff_data);
  995. return false;
  996. }
  997. struct audio_convert_info aci = {.format =
  998. output->ff_data.audio_format};
  999. output->active = true;
  1000. if (!obs_output_can_begin_data_capture(output->output, 0))
  1001. return false;
  1002. ret = pthread_create(&output->write_thread, NULL, write_thread, output);
  1003. if (ret != 0) {
  1004. ffmpeg_log_error(LOG_WARNING, &output->ff_data,
  1005. "ffmpeg_output_start: failed to create write "
  1006. "thread.");
  1007. ffmpeg_output_full_stop(output);
  1008. return false;
  1009. }
  1010. obs_output_set_video_conversion(output->output, NULL);
  1011. obs_output_set_audio_conversion(output->output, &aci);
  1012. obs_output_begin_data_capture(output->output, 0);
  1013. output->write_thread_active = true;
  1014. return true;
  1015. }
  1016. static void *start_thread(void *data)
  1017. {
  1018. struct ffmpeg_output *output = data;
  1019. if (!try_connect(output))
  1020. obs_output_signal_stop(output->output,
  1021. OBS_OUTPUT_CONNECT_FAILED);
  1022. output->connecting = false;
  1023. return NULL;
  1024. }
  1025. static bool ffmpeg_output_start(void *data)
  1026. {
  1027. struct ffmpeg_output *output = data;
  1028. int ret;
  1029. if (output->connecting)
  1030. return false;
  1031. os_atomic_set_bool(&output->stopping, false);
  1032. output->audio_start_ts = 0;
  1033. output->video_start_ts = 0;
  1034. output->total_bytes = 0;
  1035. ret = pthread_create(&output->start_thread, NULL, start_thread, output);
  1036. return (output->connecting = (ret == 0));
  1037. }
  1038. static void ffmpeg_output_full_stop(void *data)
  1039. {
  1040. struct ffmpeg_output *output = data;
  1041. if (output->active) {
  1042. obs_output_end_data_capture(output->output);
  1043. ffmpeg_deactivate(output);
  1044. }
  1045. }
  1046. static void ffmpeg_output_stop(void *data, uint64_t ts)
  1047. {
  1048. struct ffmpeg_output *output = data;
  1049. if (output->active) {
  1050. if (ts > 0) {
  1051. output->stop_ts = ts;
  1052. os_atomic_set_bool(&output->stopping, true);
  1053. }
  1054. ffmpeg_output_full_stop(output);
  1055. }
  1056. }
  1057. static void ffmpeg_deactivate(struct ffmpeg_output *output)
  1058. {
  1059. if (output->write_thread_active) {
  1060. os_event_signal(output->stop_event);
  1061. os_sem_post(output->write_sem);
  1062. pthread_join(output->write_thread, NULL);
  1063. output->write_thread_active = false;
  1064. }
  1065. pthread_mutex_lock(&output->write_mutex);
  1066. for (size_t i = 0; i < output->packets.num; i++)
  1067. av_packet_free(output->packets.array + i);
  1068. da_free(output->packets);
  1069. pthread_mutex_unlock(&output->write_mutex);
  1070. ffmpeg_data_free(&output->ff_data);
  1071. }
  1072. static uint64_t ffmpeg_output_total_bytes(void *data)
  1073. {
  1074. struct ffmpeg_output *output = data;
  1075. return output->total_bytes;
  1076. }
  1077. struct obs_output_info ffmpeg_output = {
  1078. .id = "ffmpeg_output",
  1079. .flags = OBS_OUTPUT_AUDIO | OBS_OUTPUT_VIDEO | OBS_OUTPUT_MULTI_TRACK |
  1080. OBS_OUTPUT_CAN_PAUSE,
  1081. .get_name = ffmpeg_output_getname,
  1082. .create = ffmpeg_output_create,
  1083. .destroy = ffmpeg_output_destroy,
  1084. .start = ffmpeg_output_start,
  1085. .stop = ffmpeg_output_stop,
  1086. .raw_video = receive_video,
  1087. .raw_audio2 = receive_audio,
  1088. .get_total_bytes = ffmpeg_output_total_bytes,
  1089. };