obs-ffmpeg-output.c 35 KB

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