ffmpeg-mux.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310
  1. /*
  2. * Copyright (c) 2015 Hugh Bailey <[email protected]>
  3. *
  4. * Permission to use, copy, modify, and distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #ifdef _WIN32
  17. #include <io.h>
  18. #include <fcntl.h>
  19. #include <windows.h>
  20. #define inline __inline
  21. #endif
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include "ffmpeg-mux.h"
  25. #include <util/threading.h>
  26. #include <util/platform.h>
  27. #include <util/circlebuf.h>
  28. #include <util/dstr.h>
  29. #include <libavcodec/avcodec.h>
  30. #include <libavformat/avformat.h>
  31. #include <libavutil/channel_layout.h>
  32. #include <libavutil/mastering_display_metadata.h>
  33. #define ANSI_COLOR_RED "\x1b[0;91m"
  34. #define ANSI_COLOR_MAGENTA "\x1b[0;95m"
  35. #define ANSI_COLOR_RESET "\x1b[0m"
  36. #if LIBAVCODEC_VERSION_MAJOR >= 58
  37. #define CODEC_FLAG_GLOBAL_H AV_CODEC_FLAG_GLOBAL_HEADER
  38. #else
  39. #define CODEC_FLAG_GLOBAL_H CODEC_FLAG_GLOBAL_HEADER
  40. #endif
  41. #define AVIO_BUFFER_SIZE 65536
  42. /* ------------------------------------------------------------------------- */
  43. static char *global_stream_key = "";
  44. struct resize_buf {
  45. uint8_t *buf;
  46. size_t size;
  47. size_t capacity;
  48. };
  49. static inline void resize_buf_resize(struct resize_buf *rb, size_t size)
  50. {
  51. if (!rb->buf) {
  52. rb->buf = malloc(size);
  53. rb->size = size;
  54. rb->capacity = size;
  55. } else {
  56. if (rb->capacity < size) {
  57. size_t capx2 = rb->capacity * 2;
  58. size_t new_cap = capx2 > size ? capx2 : size;
  59. rb->buf = realloc(rb->buf, new_cap);
  60. rb->capacity = new_cap;
  61. }
  62. rb->size = size;
  63. }
  64. }
  65. static inline void resize_buf_free(struct resize_buf *rb)
  66. {
  67. free(rb->buf);
  68. }
  69. /* ------------------------------------------------------------------------- */
  70. struct main_params {
  71. char *file;
  72. /* printable_file is file with any stream key information removed */
  73. struct dstr printable_file;
  74. int has_video;
  75. int tracks;
  76. char *vcodec;
  77. int vbitrate;
  78. int gop;
  79. int width;
  80. int height;
  81. int fps_num;
  82. int fps_den;
  83. int color_primaries;
  84. int color_trc;
  85. int colorspace;
  86. int color_range;
  87. int chroma_sample_location;
  88. int max_luminance;
  89. char *acodec;
  90. char *muxer_settings;
  91. int codec_tag;
  92. };
  93. struct audio_params {
  94. char *name;
  95. int abitrate;
  96. int sample_rate;
  97. int frame_size;
  98. int channels;
  99. };
  100. struct header {
  101. uint8_t *data;
  102. int size;
  103. };
  104. struct audio_info {
  105. AVStream *stream;
  106. AVCodecContext *ctx;
  107. };
  108. struct io_header {
  109. uint64_t seek_offset;
  110. size_t data_length;
  111. };
  112. struct io_buffer {
  113. bool active;
  114. bool shutdown_requested;
  115. bool output_error;
  116. os_event_t *buffer_space_available_event;
  117. os_event_t *new_data_available_event;
  118. pthread_t io_thread;
  119. pthread_mutex_t data_mutex;
  120. FILE *output_file;
  121. struct circlebuf data;
  122. uint64_t next_pos;
  123. };
  124. struct ffmpeg_mux {
  125. AVFormatContext *output;
  126. AVStream *video_stream;
  127. AVCodecContext *video_ctx;
  128. AVPacket *packet;
  129. struct audio_info *audio_infos;
  130. struct main_params params;
  131. struct audio_params *audio;
  132. struct header video_header;
  133. struct header *audio_header;
  134. int num_audio_streams;
  135. bool initialized;
  136. struct io_buffer io;
  137. };
  138. #define SRT_PROTO "srt"
  139. #define UDP_PROTO "udp"
  140. #define TCP_PROTO "tcp"
  141. #define HTTP_PROTO "http"
  142. #define RIST_PROTO "rist"
  143. static bool ffmpeg_mux_is_network(struct ffmpeg_mux *ffm)
  144. {
  145. return !strncmp(ffm->params.file, SRT_PROTO, sizeof(SRT_PROTO) - 1) ||
  146. !strncmp(ffm->params.file, UDP_PROTO, sizeof(UDP_PROTO) - 1) ||
  147. !strncmp(ffm->params.file, TCP_PROTO, sizeof(TCP_PROTO) - 1) ||
  148. !strncmp(ffm->params.file, HTTP_PROTO, sizeof(HTTP_PROTO) - 1) ||
  149. !strncmp(ffm->params.file, RIST_PROTO, sizeof(RIST_PROTO) - 1);
  150. }
  151. static void header_free(struct header *header)
  152. {
  153. free(header->data);
  154. }
  155. static void free_avformat(struct ffmpeg_mux *ffm)
  156. {
  157. if (ffm->output) {
  158. avcodec_free_context(&ffm->video_ctx);
  159. if ((ffm->output->oformat->flags & AVFMT_NOFILE) == 0) {
  160. if (!ffmpeg_mux_is_network(ffm)) {
  161. av_free(ffm->output->pb->buffer);
  162. avio_context_free(&ffm->output->pb);
  163. } else {
  164. avio_close(ffm->output->pb);
  165. }
  166. }
  167. avformat_free_context(ffm->output);
  168. ffm->output = NULL;
  169. }
  170. if (ffm->audio_infos) {
  171. for (int i = 0; i < ffm->num_audio_streams; ++i)
  172. avcodec_free_context(&ffm->audio_infos[i].ctx);
  173. free(ffm->audio_infos);
  174. }
  175. ffm->video_stream = NULL;
  176. ffm->audio_infos = NULL;
  177. ffm->num_audio_streams = 0;
  178. }
  179. static void ffmpeg_mux_free(struct ffmpeg_mux *ffm)
  180. {
  181. if (ffm->initialized) {
  182. av_write_trailer(ffm->output);
  183. }
  184. // If we're writing to a file with the circlebuf, shut it
  185. // down gracefully
  186. if (ffm->io.active) {
  187. os_atomic_set_bool(&ffm->io.shutdown_requested, true);
  188. // Wakes up the I/O thread and waits for it to finish
  189. pthread_mutex_lock(&ffm->io.data_mutex);
  190. os_event_signal(ffm->io.new_data_available_event);
  191. pthread_mutex_unlock(&ffm->io.data_mutex);
  192. pthread_join(ffm->io.io_thread, NULL);
  193. // Cleanup everything else
  194. os_event_destroy(ffm->io.new_data_available_event);
  195. os_event_destroy(ffm->io.buffer_space_available_event);
  196. pthread_mutex_destroy(&ffm->io.data_mutex);
  197. circlebuf_free(&ffm->io.data);
  198. }
  199. free_avformat(ffm);
  200. header_free(&ffm->video_header);
  201. if (ffm->audio_header) {
  202. for (int i = 0; i < ffm->params.tracks; i++) {
  203. header_free(&ffm->audio_header[i]);
  204. }
  205. free(ffm->audio_header);
  206. }
  207. if (ffm->audio) {
  208. free(ffm->audio);
  209. }
  210. dstr_free(&ffm->params.printable_file);
  211. av_packet_free(&ffm->packet);
  212. memset(ffm, 0, sizeof(*ffm));
  213. }
  214. static bool get_opt_str(int *p_argc, char ***p_argv, char **str,
  215. const char *opt)
  216. {
  217. int argc = *p_argc;
  218. char **argv = *p_argv;
  219. if (!argc) {
  220. printf("Missing expected option: '%s'\n", opt);
  221. return false;
  222. }
  223. (*p_argc)--;
  224. (*p_argv)++;
  225. *str = argv[0];
  226. return true;
  227. }
  228. static bool get_opt_int(int *p_argc, char ***p_argv, int *i, const char *opt)
  229. {
  230. char *str;
  231. if (!get_opt_str(p_argc, p_argv, &str, opt)) {
  232. return false;
  233. }
  234. *i = atoi(str);
  235. return true;
  236. }
  237. static bool get_audio_params(struct audio_params *audio, int *argc,
  238. char ***argv)
  239. {
  240. if (!get_opt_str(argc, argv, &audio->name, "audio track name"))
  241. return false;
  242. if (!get_opt_int(argc, argv, &audio->abitrate, "audio bitrate"))
  243. return false;
  244. if (!get_opt_int(argc, argv, &audio->sample_rate, "audio sample rate"))
  245. return false;
  246. if (!get_opt_int(argc, argv, &audio->frame_size, "audio frame size"))
  247. return false;
  248. if (!get_opt_int(argc, argv, &audio->channels, "audio channels"))
  249. return false;
  250. return true;
  251. }
  252. static void ffmpeg_log_callback(void *param, int level, const char *format,
  253. va_list args)
  254. {
  255. #ifdef ENABLE_FFMPEG_MUX_DEBUG
  256. char out_buffer[4096];
  257. struct dstr out = {0};
  258. vsnprintf(out_buffer, sizeof(out_buffer), format, args);
  259. dstr_copy(&out, out_buffer);
  260. if (global_stream_key && *global_stream_key) {
  261. dstr_replace(&out, global_stream_key, "{stream_key}");
  262. }
  263. switch (level) {
  264. case AV_LOG_INFO:
  265. fprintf(stdout, "info: [ffmpeg_muxer] %s", out.array);
  266. fflush(stdout);
  267. break;
  268. case AV_LOG_WARNING:
  269. fprintf(stdout, "%swarning: [ffmpeg_muxer] %s%s",
  270. ANSI_COLOR_MAGENTA, out.array, ANSI_COLOR_RESET);
  271. fflush(stdout);
  272. break;
  273. case AV_LOG_ERROR:
  274. fprintf(stderr, "%serror: [ffmpeg_muxer] %s%s", ANSI_COLOR_RED,
  275. out.array, ANSI_COLOR_RESET);
  276. fflush(stderr);
  277. }
  278. dstr_free(&out);
  279. #else
  280. UNUSED_PARAMETER(level);
  281. UNUSED_PARAMETER(format);
  282. UNUSED_PARAMETER(args);
  283. #endif
  284. UNUSED_PARAMETER(param);
  285. }
  286. static bool init_params(int *argc, char ***argv, struct main_params *params,
  287. struct audio_params **p_audio)
  288. {
  289. struct audio_params *audio = NULL;
  290. if (!get_opt_str(argc, argv, &params->file, "file name"))
  291. return false;
  292. if (!get_opt_int(argc, argv, &params->has_video, "video track count"))
  293. return false;
  294. if (!get_opt_int(argc, argv, &params->tracks, "audio track count"))
  295. return false;
  296. if (params->has_video > 1 || params->has_video < 0) {
  297. puts("Invalid number of video tracks\n");
  298. return false;
  299. }
  300. if (params->tracks < 0) {
  301. puts("Invalid number of audio tracks\n");
  302. return false;
  303. }
  304. if (params->has_video == 0 && params->tracks == 0) {
  305. puts("Must have at least 1 audio track or 1 video track\n");
  306. return false;
  307. }
  308. if (params->has_video) {
  309. if (!get_opt_str(argc, argv, &params->vcodec, "video codec"))
  310. return false;
  311. if (!get_opt_int(argc, argv, &params->vbitrate,
  312. "video bitrate"))
  313. return false;
  314. if (!get_opt_int(argc, argv, &params->width, "video width"))
  315. return false;
  316. if (!get_opt_int(argc, argv, &params->height, "video height"))
  317. return false;
  318. if (!get_opt_int(argc, argv, &params->color_primaries,
  319. "video color primaries"))
  320. return false;
  321. if (!get_opt_int(argc, argv, &params->color_trc,
  322. "video color trc"))
  323. return false;
  324. if (!get_opt_int(argc, argv, &params->colorspace,
  325. "video colorspace"))
  326. return false;
  327. if (!get_opt_int(argc, argv, &params->color_range,
  328. "video color range"))
  329. return false;
  330. if (!get_opt_int(argc, argv, &params->chroma_sample_location,
  331. "video chroma sample location"))
  332. return false;
  333. if (!get_opt_int(argc, argv, &params->max_luminance,
  334. "video max luminance"))
  335. return false;
  336. if (!get_opt_int(argc, argv, &params->fps_num, "video fps num"))
  337. return false;
  338. if (!get_opt_int(argc, argv, &params->fps_den, "video fps den"))
  339. return false;
  340. if (!get_opt_int(argc, argv, &params->codec_tag,
  341. "video codec tag"))
  342. params->codec_tag = 0;
  343. }
  344. if (params->tracks) {
  345. if (!get_opt_str(argc, argv, &params->acodec, "audio codec"))
  346. return false;
  347. audio = calloc(params->tracks, sizeof(*audio));
  348. for (int i = 0; i < params->tracks; i++) {
  349. if (!get_audio_params(&audio[i], argc, argv)) {
  350. free(audio);
  351. return false;
  352. }
  353. }
  354. }
  355. *p_audio = audio;
  356. dstr_copy(&params->printable_file, params->file);
  357. get_opt_str(argc, argv, &global_stream_key, "stream key");
  358. if (strcmp(global_stream_key, "") != 0) {
  359. dstr_replace(&params->printable_file, global_stream_key,
  360. "{stream_key}");
  361. }
  362. av_log_set_callback(ffmpeg_log_callback);
  363. get_opt_str(argc, argv, &params->muxer_settings, "muxer settings");
  364. return true;
  365. }
  366. static bool new_stream(struct ffmpeg_mux *ffm, AVStream **stream,
  367. const char *name)
  368. {
  369. *stream = avformat_new_stream(ffm->output, NULL);
  370. if (!*stream) {
  371. fprintf(stderr, "Couldn't create stream for encoder '%s'\n",
  372. name);
  373. return false;
  374. }
  375. (*stream)->id = ffm->output->nb_streams - 1;
  376. return true;
  377. }
  378. static void create_video_stream(struct ffmpeg_mux *ffm)
  379. {
  380. AVCodecContext *context;
  381. void *extradata = NULL;
  382. const char *name = ffm->params.vcodec;
  383. const AVCodecDescriptor *codec = avcodec_descriptor_get_by_name(name);
  384. if (!codec) {
  385. fprintf(stderr, "Couldn't find codec '%s'\n", name);
  386. return;
  387. }
  388. if (!new_stream(ffm, &ffm->video_stream, name))
  389. return;
  390. if (ffm->video_header.size) {
  391. extradata = av_memdup(ffm->video_header.data,
  392. ffm->video_header.size);
  393. }
  394. context = avcodec_alloc_context3(NULL);
  395. context->codec_type = codec->type;
  396. context->codec_id = codec->id;
  397. context->codec_tag = ffm->params.codec_tag;
  398. context->bit_rate = (int64_t)ffm->params.vbitrate * 1000;
  399. context->width = ffm->params.width;
  400. context->height = ffm->params.height;
  401. context->coded_width = ffm->params.width;
  402. context->coded_height = ffm->params.height;
  403. context->color_primaries = ffm->params.color_primaries;
  404. context->color_trc = ffm->params.color_trc;
  405. context->colorspace = ffm->params.colorspace;
  406. context->color_range = ffm->params.color_range;
  407. context->chroma_sample_location = ffm->params.chroma_sample_location;
  408. context->extradata = extradata;
  409. context->extradata_size = ffm->video_header.size;
  410. context->time_base =
  411. (AVRational){ffm->params.fps_den, ffm->params.fps_num};
  412. ffm->video_stream->time_base = context->time_base;
  413. #if LIBAVFORMAT_VERSION_MAJOR < 59
  414. // codec->time_base may still be used if LIBAVFORMAT_VERSION_MAJOR < 59
  415. PRAGMA_WARN_PUSH
  416. PRAGMA_WARN_DEPRECATION
  417. ffm->video_stream->codec->time_base = context->time_base;
  418. PRAGMA_WARN_POP
  419. #endif
  420. ffm->video_stream->avg_frame_rate = av_inv_q(context->time_base);
  421. const int max_luminance = ffm->params.max_luminance;
  422. if (max_luminance > 0) {
  423. size_t content_size;
  424. AVContentLightMetadata *const content =
  425. av_content_light_metadata_alloc(&content_size);
  426. content->MaxCLL = max_luminance;
  427. content->MaxFALL = max_luminance;
  428. av_stream_add_side_data(ffm->video_stream,
  429. AV_PKT_DATA_CONTENT_LIGHT_LEVEL,
  430. (uint8_t *)content, content_size);
  431. AVMasteringDisplayMetadata *const mastering =
  432. av_mastering_display_metadata_alloc();
  433. mastering->display_primaries[0][0] = av_make_q(17, 25);
  434. mastering->display_primaries[0][1] = av_make_q(8, 25);
  435. mastering->display_primaries[1][0] = av_make_q(53, 200);
  436. mastering->display_primaries[1][1] = av_make_q(69, 100);
  437. mastering->display_primaries[2][0] = av_make_q(3, 20);
  438. mastering->display_primaries[2][1] = av_make_q(3, 50);
  439. mastering->white_point[0] = av_make_q(3127, 10000);
  440. mastering->white_point[1] = av_make_q(329, 1000);
  441. mastering->min_luminance = av_make_q(0, 1);
  442. mastering->max_luminance = av_make_q(max_luminance, 1);
  443. mastering->has_primaries = 1;
  444. mastering->has_luminance = 1;
  445. av_stream_add_side_data(ffm->video_stream,
  446. AV_PKT_DATA_MASTERING_DISPLAY_METADATA,
  447. (uint8_t *)mastering,
  448. sizeof(*mastering));
  449. }
  450. if (ffm->output->oformat->flags & AVFMT_GLOBALHEADER)
  451. context->flags |= CODEC_FLAG_GLOBAL_H;
  452. avcodec_parameters_from_context(ffm->video_stream->codecpar, context);
  453. ffm->video_ctx = context;
  454. }
  455. static void create_audio_stream(struct ffmpeg_mux *ffm, int idx)
  456. {
  457. AVCodecContext *context;
  458. AVStream *stream;
  459. void *extradata = NULL;
  460. const char *name = ffm->params.acodec;
  461. int channels;
  462. const AVCodecDescriptor *codec_desc =
  463. avcodec_descriptor_get_by_name(name);
  464. if (!codec_desc) {
  465. fprintf(stderr, "Couldn't find codec descriptor '%s'\n", name);
  466. return;
  467. }
  468. const AVCodec *codec = avcodec_find_encoder(codec_desc->id);
  469. if (!codec) {
  470. fprintf(stderr, "Couldn't find codec '%s'\n", name);
  471. return;
  472. }
  473. if (!new_stream(ffm, &stream, name))
  474. return;
  475. av_dict_set(&stream->metadata, "title", ffm->audio[idx].name, 0);
  476. stream->time_base = (AVRational){1, ffm->audio[idx].sample_rate};
  477. if (ffm->audio_header[idx].size) {
  478. extradata = av_memdup(ffm->audio_header[idx].data,
  479. ffm->audio_header[idx].size);
  480. }
  481. context = avcodec_alloc_context3(NULL);
  482. context->codec_type = codec->type;
  483. context->codec_id = codec->id;
  484. if (!(codec_desc->props & AV_CODEC_PROP_LOSSLESS))
  485. context->bit_rate = (int64_t)ffm->audio[idx].abitrate * 1000;
  486. channels = ffm->audio[idx].channels;
  487. #if LIBAVUTIL_VERSION_INT < AV_VERSION_INT(57, 24, 100)
  488. context->channels = channels;
  489. #endif
  490. context->sample_rate = ffm->audio[idx].sample_rate;
  491. if (!(codec->capabilities & AV_CODEC_CAP_VARIABLE_FRAME_SIZE))
  492. context->frame_size = ffm->audio[idx].frame_size;
  493. context->time_base = stream->time_base;
  494. context->extradata = extradata;
  495. context->extradata_size = ffm->audio_header[idx].size;
  496. #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(59, 24, 100)
  497. context->channel_layout = av_get_default_channel_layout(channels);
  498. //avutil default channel layout for 5 channels is 5.0 ; fix for 4.1
  499. if (channels == 5)
  500. context->channel_layout = av_get_channel_layout("4.1");
  501. #else
  502. av_channel_layout_default(&context->ch_layout, channels);
  503. //avutil default channel layout for 5 channels is 5.0 ; fix for 4.1
  504. if (channels == 5)
  505. context->ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_4POINT1;
  506. #endif
  507. if (ffm->output->oformat->flags & AVFMT_GLOBALHEADER)
  508. context->flags |= CODEC_FLAG_GLOBAL_H;
  509. avcodec_parameters_from_context(stream->codecpar, context);
  510. ffm->audio_infos[ffm->num_audio_streams].stream = stream;
  511. ffm->audio_infos[ffm->num_audio_streams].ctx = context;
  512. ffm->num_audio_streams++;
  513. }
  514. static bool init_streams(struct ffmpeg_mux *ffm)
  515. {
  516. if (ffm->params.has_video)
  517. create_video_stream(ffm);
  518. if (ffm->params.tracks) {
  519. ffm->audio_infos =
  520. calloc(ffm->params.tracks, sizeof(*ffm->audio_infos));
  521. for (int i = 0; i < ffm->params.tracks; i++)
  522. create_audio_stream(ffm, i);
  523. }
  524. if (!ffm->video_stream && !ffm->num_audio_streams)
  525. return false;
  526. return true;
  527. }
  528. static void set_header(struct header *header, uint8_t *data, size_t size)
  529. {
  530. header->size = (int)size;
  531. header->data = malloc(size);
  532. memcpy(header->data, data, size);
  533. }
  534. static void ffmpeg_mux_header(struct ffmpeg_mux *ffm, uint8_t *data,
  535. struct ffm_packet_info *info)
  536. {
  537. if (info->type == FFM_PACKET_VIDEO) {
  538. set_header(&ffm->video_header, data, (size_t)info->size);
  539. } else {
  540. set_header(&ffm->audio_header[info->index], data,
  541. (size_t)info->size);
  542. }
  543. }
  544. static size_t safe_read(void *vdata, size_t size)
  545. {
  546. uint8_t *data = vdata;
  547. size_t total = size;
  548. while (size > 0) {
  549. size_t in_size = fread(data, 1, size, stdin);
  550. if (in_size == 0)
  551. return 0;
  552. size -= in_size;
  553. data += in_size;
  554. }
  555. return total;
  556. }
  557. static bool ffmpeg_mux_get_header(struct ffmpeg_mux *ffm)
  558. {
  559. struct ffm_packet_info info = {0};
  560. bool success = safe_read(&info, sizeof(info)) == sizeof(info);
  561. if (success) {
  562. uint8_t *data = malloc(info.size);
  563. if (safe_read(data, info.size) == info.size) {
  564. ffmpeg_mux_header(ffm, data, &info);
  565. } else {
  566. success = false;
  567. }
  568. free(data);
  569. }
  570. return success;
  571. }
  572. static inline bool ffmpeg_mux_get_extra_data(struct ffmpeg_mux *ffm)
  573. {
  574. if (ffm->params.has_video) {
  575. if (!ffmpeg_mux_get_header(ffm)) {
  576. return false;
  577. }
  578. }
  579. for (int i = 0; i < ffm->params.tracks; i++) {
  580. if (!ffmpeg_mux_get_header(ffm)) {
  581. return false;
  582. }
  583. }
  584. return true;
  585. }
  586. #ifdef _MSC_VER
  587. #pragma warning(disable : 4996)
  588. #endif
  589. #define CHUNK_SIZE 1048576
  590. static void *ffmpeg_mux_io_thread(void *data)
  591. {
  592. struct ffmpeg_mux *ffm = data;
  593. // Chunk collects the writes into a larger batch
  594. size_t chunk_used = 0;
  595. unsigned char *chunk = malloc(CHUNK_SIZE);
  596. if (!chunk) {
  597. os_atomic_set_bool(&ffm->io.output_error, true);
  598. fprintf(stderr, "Error allocating memory for output\n");
  599. goto error;
  600. }
  601. bool shutting_down;
  602. bool want_seek = false;
  603. bool force_flush_chunk = false;
  604. // current_seek_position is a virtual position updated as we read from
  605. // the buffer, if it becomes discontinuous due to a seek request from
  606. // ffmpeg, then we flush the chunk. next_seek_position is the actual
  607. // offset we should seek to when we write the chunk.
  608. uint64_t current_seek_position = 0;
  609. uint64_t next_seek_position;
  610. for (;;) {
  611. // Wait for ffmpeg to write data to the buffer
  612. os_event_wait(ffm->io.new_data_available_event);
  613. // Loop to write in chunk_size chunks
  614. for (;;) {
  615. shutting_down = os_atomic_load_bool(
  616. &ffm->io.shutdown_requested);
  617. pthread_mutex_lock(&ffm->io.data_mutex);
  618. // Fetch as many writes as possible from the circlebuf
  619. // and fill up our local chunk. This may involve seeking
  620. // if ffmpeg needs to, so take care of that as well.
  621. for (;;) {
  622. size_t available = ffm->io.data.size;
  623. // Buffer is empty (now) or was already empty (we got
  624. // woken up to exit)
  625. if (!available)
  626. break;
  627. // Get seek offset and data size
  628. struct io_header header;
  629. circlebuf_peek_front(&ffm->io.data, &header,
  630. sizeof(header));
  631. // Do we need to seek?
  632. if (header.seek_offset !=
  633. current_seek_position) {
  634. // If there's already part of a chunk pending,
  635. // flush it at the current offset. Similarly,
  636. // if we already plan to seek, then seek.
  637. if (chunk_used || want_seek) {
  638. force_flush_chunk = true;
  639. break;
  640. }
  641. // Mark that we need to seek and where to
  642. want_seek = true;
  643. next_seek_position = header.seek_offset;
  644. // Update our virtual position
  645. current_seek_position =
  646. header.seek_offset;
  647. }
  648. // Make sure there's enough room for the data, if
  649. // not then force a flush
  650. if (header.data_length + chunk_used >
  651. CHUNK_SIZE) {
  652. force_flush_chunk = true;
  653. break;
  654. }
  655. // Remove header that we already read
  656. circlebuf_pop_front(&ffm->io.data, NULL,
  657. sizeof(header));
  658. // Copy from the buffer to our local chunk
  659. circlebuf_pop_front(&ffm->io.data,
  660. chunk + chunk_used,
  661. header.data_length);
  662. // Update offsets
  663. chunk_used += header.data_length;
  664. current_seek_position += header.data_length;
  665. }
  666. // Signal that there is more room in the buffer
  667. os_event_signal(ffm->io.buffer_space_available_event);
  668. // Try to avoid lots of small writes unless this was the final
  669. // data left in the buffer. The buffer might be entirely empty
  670. // if we were woken up to exit.
  671. if (!force_flush_chunk &&
  672. (!chunk_used ||
  673. (chunk_used < 65536 && !shutting_down))) {
  674. os_event_reset(
  675. ffm->io.new_data_available_event);
  676. pthread_mutex_unlock(&ffm->io.data_mutex);
  677. break;
  678. }
  679. pthread_mutex_unlock(&ffm->io.data_mutex);
  680. // Seek if we need to
  681. if (want_seek) {
  682. os_fseeki64(ffm->io.output_file,
  683. next_seek_position, SEEK_SET);
  684. // Update the next virtual position, making sure to take
  685. // into account the size of the chunk we're about to write.
  686. current_seek_position =
  687. next_seek_position + chunk_used;
  688. want_seek = false;
  689. }
  690. // Write the current chunk to the output file
  691. if (fwrite(chunk, chunk_used, 1, ffm->io.output_file) !=
  692. 1) {
  693. os_atomic_set_bool(&ffm->io.output_error, true);
  694. fprintf(stderr, "Error writing to '%s', %s\n",
  695. ffm->params.printable_file.array,
  696. strerror(errno));
  697. goto error;
  698. }
  699. chunk_used = 0;
  700. force_flush_chunk = false;
  701. }
  702. // If this was the last chunk, time to exit
  703. if (shutting_down)
  704. break;
  705. }
  706. error:
  707. if (chunk)
  708. free(chunk);
  709. fclose(ffm->io.output_file);
  710. return NULL;
  711. }
  712. static int64_t ffmpeg_mux_seek_av_buffer(void *opaque, int64_t offset,
  713. int whence)
  714. {
  715. struct ffmpeg_mux *ffm = opaque;
  716. // If the output thread failed, signal that back up the stack
  717. if (os_atomic_load_bool(&ffm->io.output_error))
  718. return -1;
  719. // Update where the next write should go
  720. pthread_mutex_lock(&ffm->io.data_mutex);
  721. if (whence == SEEK_SET)
  722. ffm->io.next_pos = offset;
  723. else if (whence == SEEK_CUR)
  724. ffm->io.next_pos += offset;
  725. pthread_mutex_unlock(&ffm->io.data_mutex);
  726. return 0;
  727. }
  728. static int ffmpeg_mux_write_av_buffer(void *opaque, uint8_t *buf, int buf_size)
  729. {
  730. struct ffmpeg_mux *ffm = opaque;
  731. // If the output thread failed, signal that back up the stack
  732. if (os_atomic_load_bool(&ffm->io.output_error))
  733. return -1;
  734. for (;;) {
  735. pthread_mutex_lock(&ffm->io.data_mutex);
  736. // Avoid unbounded growth of the circlebuf, cap to 256 MB
  737. if (ffm->io.data.capacity >= 256 * 1048576 &&
  738. ffm->io.data.capacity - ffm->io.data.size <
  739. buf_size + sizeof(struct io_header)) {
  740. // No space, wait for the I/O thread to make space
  741. os_event_reset(ffm->io.buffer_space_available_event);
  742. pthread_mutex_unlock(&ffm->io.data_mutex);
  743. os_event_wait(ffm->io.buffer_space_available_event);
  744. } else {
  745. break;
  746. }
  747. }
  748. struct io_header header;
  749. header.data_length = buf_size;
  750. header.seek_offset = ffm->io.next_pos;
  751. // Copy the data into the buffer
  752. circlebuf_push_back(&ffm->io.data, &header, sizeof(header));
  753. circlebuf_push_back(&ffm->io.data, buf, buf_size);
  754. // Advance the next write position
  755. ffm->io.next_pos += buf_size;
  756. // Tell the I/O thread that there's new data to be written
  757. os_event_signal(ffm->io.new_data_available_event);
  758. pthread_mutex_unlock(&ffm->io.data_mutex);
  759. return buf_size;
  760. }
  761. static inline int open_output_file(struct ffmpeg_mux *ffm)
  762. {
  763. #if LIBAVFORMAT_VERSION_INT < AV_VERSION_INT(59, 0, 100)
  764. AVOutputFormat *format = ffm->output->oformat;
  765. #else
  766. const AVOutputFormat *format = ffm->output->oformat;
  767. #endif
  768. int ret;
  769. if ((format->flags & AVFMT_NOFILE) == 0) {
  770. if (!ffmpeg_mux_is_network(ffm)) {
  771. // If not outputting to a network, write to a circlebuf
  772. // instead of relying on ffmpeg disk output. This hopefully
  773. // works around too small buffers somewhere causing output
  774. // stalls when recording.
  775. // We're in charge of managing the actual file now
  776. ffm->io.output_file = os_fopen(ffm->params.file, "wb");
  777. if (!ffm->io.output_file) {
  778. fprintf(stderr, "Couldn't open '%s', %s\n",
  779. ffm->params.printable_file.array,
  780. strerror(errno));
  781. return FFM_ERROR;
  782. }
  783. // Start at 1MB, this can grow up to 256 MB depending
  784. // how fast data is going in and out (limited in
  785. // ffmpeg_mux_write_av_buffer)
  786. circlebuf_reserve(&ffm->io.data, 1048576);
  787. pthread_mutex_init(&ffm->io.data_mutex, NULL);
  788. os_event_init(&ffm->io.buffer_space_available_event,
  789. OS_EVENT_TYPE_AUTO);
  790. os_event_init(&ffm->io.new_data_available_event,
  791. OS_EVENT_TYPE_AUTO);
  792. pthread_create(&ffm->io.io_thread, NULL,
  793. ffmpeg_mux_io_thread, ffm);
  794. unsigned char *avio_ctx_buffer =
  795. av_malloc(AVIO_BUFFER_SIZE);
  796. ffm->output->pb = avio_alloc_context(
  797. avio_ctx_buffer, AVIO_BUFFER_SIZE, 1, ffm, NULL,
  798. ffmpeg_mux_write_av_buffer,
  799. ffmpeg_mux_seek_av_buffer);
  800. ffm->io.active = true;
  801. } else {
  802. ret = avio_open(&ffm->output->pb, ffm->params.file,
  803. AVIO_FLAG_WRITE);
  804. if (ret < 0) {
  805. fprintf(stderr, "Couldn't open '%s', %s\n",
  806. ffm->params.printable_file.array,
  807. av_err2str(ret));
  808. return FFM_ERROR;
  809. }
  810. }
  811. }
  812. AVDictionary *dict = NULL;
  813. if ((ret = av_dict_parse_string(&dict, ffm->params.muxer_settings, "=",
  814. " ", 0))) {
  815. fprintf(stderr, "Failed to parse muxer settings: %s\n%s\n",
  816. av_err2str(ret), ffm->params.muxer_settings);
  817. av_dict_free(&dict);
  818. }
  819. if (av_dict_count(dict) > 0) {
  820. printf("Using muxer settings:");
  821. AVDictionaryEntry *entry = NULL;
  822. while ((entry = av_dict_get(dict, "", entry,
  823. AV_DICT_IGNORE_SUFFIX)))
  824. printf("\n\t%s=%s", entry->key, entry->value);
  825. printf("\n");
  826. }
  827. ret = avformat_write_header(ffm->output, &dict);
  828. if (ret < 0) {
  829. fprintf(stderr, "Error opening '%s': %s",
  830. ffm->params.printable_file.array, av_err2str(ret));
  831. av_dict_free(&dict);
  832. return ret == -22 ? FFM_UNSUPPORTED : FFM_ERROR;
  833. }
  834. av_dict_free(&dict);
  835. return FFM_SUCCESS;
  836. }
  837. static int ffmpeg_mux_init_context(struct ffmpeg_mux *ffm)
  838. {
  839. #if LIBAVFORMAT_VERSION_INT < AV_VERSION_INT(59, 0, 100)
  840. AVOutputFormat *output_format;
  841. #else
  842. const AVOutputFormat *output_format;
  843. #endif
  844. int ret;
  845. bool is_http = false;
  846. is_http = (strncmp(ffm->params.file, HTTP_PROTO,
  847. sizeof(HTTP_PROTO) - 1) == 0);
  848. bool is_network = ffmpeg_mux_is_network(ffm);
  849. if (is_network) {
  850. avformat_network_init();
  851. }
  852. if (is_network && !is_http)
  853. output_format = av_guess_format("mpegts", NULL, "video/M2PT");
  854. else
  855. output_format = av_guess_format(NULL, ffm->params.file, NULL);
  856. if (output_format == NULL) {
  857. fprintf(stderr, "Couldn't find an appropriate muxer for '%s'\n",
  858. ffm->params.printable_file.array);
  859. return FFM_ERROR;
  860. }
  861. #ifdef ENABLE_FFMPEG_MUX_DEBUG
  862. printf("info: Output format name and long_name: %s, %s\n",
  863. output_format->name ? output_format->name : "unknown",
  864. output_format->long_name ? output_format->long_name : "unknown");
  865. #endif
  866. ret = avformat_alloc_output_context2(&ffm->output, output_format, NULL,
  867. ffm->params.file);
  868. if (ret < 0) {
  869. fprintf(stderr, "Couldn't initialize output context: %s\n",
  870. av_err2str(ret));
  871. return FFM_ERROR;
  872. }
  873. #if LIBAVFORMAT_VERSION_INT < AV_VERSION_INT(59, 0, 100)
  874. ffm->output->oformat->video_codec = AV_CODEC_ID_NONE;
  875. ffm->output->oformat->audio_codec = AV_CODEC_ID_NONE;
  876. #endif
  877. #if LIBAVFORMAT_VERSION_INT < AV_VERSION_INT(60, 0, 100)
  878. /* Allow FLAC/OPUS in MP4 */
  879. ffm->output->strict_std_compliance = FF_COMPLIANCE_EXPERIMENTAL;
  880. #endif
  881. if (!init_streams(ffm)) {
  882. free_avformat(ffm);
  883. return FFM_ERROR;
  884. }
  885. ret = open_output_file(ffm);
  886. if (ret != FFM_SUCCESS) {
  887. free_avformat(ffm);
  888. return ret;
  889. }
  890. return FFM_SUCCESS;
  891. }
  892. static int ffmpeg_mux_init_internal(struct ffmpeg_mux *ffm, int argc,
  893. char *argv[])
  894. {
  895. argc--;
  896. argv++;
  897. if (!init_params(&argc, &argv, &ffm->params, &ffm->audio))
  898. return FFM_ERROR;
  899. if (ffm->params.tracks) {
  900. ffm->audio_header =
  901. calloc(ffm->params.tracks, sizeof(*ffm->audio_header));
  902. }
  903. #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(58, 9, 100)
  904. av_register_all();
  905. #endif
  906. if (!ffmpeg_mux_get_extra_data(ffm))
  907. return FFM_ERROR;
  908. ffm->packet = av_packet_alloc();
  909. /* ffmpeg does not have a way of telling what's supported
  910. * for a given output format, so we try each possibility */
  911. return ffmpeg_mux_init_context(ffm);
  912. }
  913. static int ffmpeg_mux_init(struct ffmpeg_mux *ffm, int argc, char *argv[])
  914. {
  915. int ret = ffmpeg_mux_init_internal(ffm, argc, argv);
  916. if (ret != FFM_SUCCESS) {
  917. ffmpeg_mux_free(ffm);
  918. return ret;
  919. }
  920. ffm->initialized = true;
  921. return ret;
  922. }
  923. static inline int get_index(struct ffmpeg_mux *ffm,
  924. struct ffm_packet_info *info)
  925. {
  926. if (info->type == FFM_PACKET_VIDEO) {
  927. if (ffm->video_stream) {
  928. return ffm->video_stream->id;
  929. }
  930. } else {
  931. if ((int)info->index < ffm->num_audio_streams) {
  932. return ffm->audio_infos[info->index].stream->id;
  933. }
  934. }
  935. return -1;
  936. }
  937. static AVCodecContext *get_codec_context(struct ffmpeg_mux *ffm,
  938. struct ffm_packet_info *info)
  939. {
  940. if (info->type == FFM_PACKET_VIDEO) {
  941. if (ffm->video_stream) {
  942. return ffm->video_ctx;
  943. }
  944. } else {
  945. if ((int)info->index < ffm->num_audio_streams) {
  946. return ffm->audio_infos[info->index].ctx;
  947. }
  948. }
  949. return NULL;
  950. }
  951. static inline AVStream *get_stream(struct ffmpeg_mux *ffm, int idx)
  952. {
  953. return ffm->output->streams[idx];
  954. }
  955. static inline int64_t rescale_ts(struct ffmpeg_mux *ffm,
  956. AVRational codec_time_base, int64_t val,
  957. int idx)
  958. {
  959. AVStream *stream = get_stream(ffm, idx);
  960. return av_rescale_q_rnd(val / codec_time_base.num, codec_time_base,
  961. stream->time_base,
  962. AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX);
  963. }
  964. static inline bool ffmpeg_mux_packet(struct ffmpeg_mux *ffm, uint8_t *buf,
  965. struct ffm_packet_info *info)
  966. {
  967. int idx = get_index(ffm, info);
  968. /* The muxer might not support video/audio, or multiple audio tracks */
  969. if (idx == -1) {
  970. return true;
  971. }
  972. const AVRational codec_time_base =
  973. get_codec_context(ffm, info)->time_base;
  974. ffm->packet->data = buf;
  975. ffm->packet->size = (int)info->size;
  976. ffm->packet->stream_index = idx;
  977. ffm->packet->pts = rescale_ts(ffm, codec_time_base, info->pts, idx);
  978. ffm->packet->dts = rescale_ts(ffm, codec_time_base, info->dts, idx);
  979. if (info->keyframe)
  980. ffm->packet->flags = AV_PKT_FLAG_KEY;
  981. int ret = av_interleaved_write_frame(ffm->output, ffm->packet);
  982. /* Treat "Invalid data found when processing input" and "Invalid argument" as non-fatal */
  983. if (ret == AVERROR_INVALIDDATA || ret == -EINVAL) {
  984. return true;
  985. }
  986. if (ret < 0) {
  987. fprintf(stderr, "av_interleaved_write_frame failed: %d: %s\n",
  988. ret, av_err2str(ret));
  989. }
  990. return ret >= 0;
  991. }
  992. static inline bool read_change_file(struct ffmpeg_mux *ffm, uint32_t size,
  993. struct resize_buf *filename, int argc,
  994. char **argv)
  995. {
  996. resize_buf_resize(filename, size + 1);
  997. if (safe_read(filename->buf, size) != size) {
  998. return false;
  999. }
  1000. filename->buf[size] = 0;
  1001. #ifdef ENABLE_FFMPEG_MUX_DEBUG
  1002. fprintf(stderr, "info: New output file name: %s\n", filename->buf);
  1003. #endif
  1004. int ret;
  1005. char *argv1_backup = argv[1];
  1006. argv[1] = (char *)filename->buf;
  1007. ffmpeg_mux_free(ffm);
  1008. ret = ffmpeg_mux_init(ffm, argc, argv);
  1009. if (ret != FFM_SUCCESS) {
  1010. fprintf(stderr, "Couldn't initialize muxer\n");
  1011. return false;
  1012. }
  1013. argv[1] = argv1_backup;
  1014. return true;
  1015. }
  1016. /* ------------------------------------------------------------------------- */
  1017. #ifdef _WIN32
  1018. int wmain(int argc, wchar_t *argv_w[])
  1019. #else
  1020. int main(int argc, char *argv[])
  1021. #endif
  1022. {
  1023. struct ffm_packet_info info = {0};
  1024. struct ffmpeg_mux ffm = {0};
  1025. struct resize_buf rb = {0};
  1026. struct resize_buf rb_filename = {0};
  1027. bool fail = false;
  1028. int ret;
  1029. #ifdef _WIN32
  1030. char **argv;
  1031. SetErrorMode(SEM_FAILCRITICALERRORS);
  1032. argv = malloc(argc * sizeof(char *));
  1033. for (int i = 0; i < argc; i++) {
  1034. size_t len = wcslen(argv_w[i]);
  1035. int size;
  1036. size = WideCharToMultiByte(CP_UTF8, 0, argv_w[i], (int)len,
  1037. NULL, 0, NULL, NULL);
  1038. argv[i] = malloc(size + 1);
  1039. WideCharToMultiByte(CP_UTF8, 0, argv_w[i], (int)len, argv[i],
  1040. size + 1, NULL, NULL);
  1041. argv[i][size] = 0;
  1042. }
  1043. _setmode(_fileno(stdin), O_BINARY);
  1044. #endif
  1045. setvbuf(stderr, NULL, _IONBF, 0);
  1046. ret = ffmpeg_mux_init(&ffm, argc, argv);
  1047. if (ret != FFM_SUCCESS) {
  1048. fprintf(stderr, "Couldn't initialize muxer\n");
  1049. return ret;
  1050. }
  1051. while (!fail && safe_read(&info, sizeof(info)) == sizeof(info)) {
  1052. if (info.type == FFM_PACKET_CHANGE_FILE) {
  1053. fail = !read_change_file(&ffm, info.size, &rb_filename,
  1054. argc, argv);
  1055. continue;
  1056. }
  1057. resize_buf_resize(&rb, info.size);
  1058. if (safe_read(rb.buf, info.size) == info.size) {
  1059. fail = !ffmpeg_mux_packet(&ffm, rb.buf, &info);
  1060. } else {
  1061. fail = true;
  1062. }
  1063. }
  1064. ffmpeg_mux_free(&ffm);
  1065. resize_buf_free(&rb);
  1066. resize_buf_free(&rb_filename);
  1067. #ifdef _WIN32
  1068. for (int i = 0; i < argc; i++)
  1069. free(argv[i]);
  1070. free(argv);
  1071. #endif
  1072. return 0;
  1073. }