ffmpeg-mux.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770
  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 <stdbool.h>
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include "ffmpeg-mux.h"
  26. #include <libavformat/avformat.h>
  27. #if LIBAVCODEC_VERSION_MAJOR >= 58
  28. #define CODEC_FLAG_GLOBAL_H AV_CODEC_FLAG_GLOBAL_HEADER
  29. #else
  30. #define CODEC_FLAG_GLOBAL_H CODEC_FLAG_GLOBAL_HEADER
  31. #endif
  32. /* ------------------------------------------------------------------------- */
  33. struct resize_buf {
  34. uint8_t *buf;
  35. size_t size;
  36. size_t capacity;
  37. };
  38. static inline void resize_buf_resize(struct resize_buf *rb, size_t size)
  39. {
  40. if (!rb->buf) {
  41. rb->buf = malloc(size);
  42. rb->size = size;
  43. rb->capacity = size;
  44. } else {
  45. if (rb->capacity < size) {
  46. size_t capx2 = rb->capacity * 2;
  47. size_t new_cap = capx2 > size ? capx2 : size;
  48. rb->buf = realloc(rb->buf, new_cap);
  49. rb->capacity = new_cap;
  50. }
  51. rb->size = size;
  52. }
  53. }
  54. static inline void resize_buf_free(struct resize_buf *rb)
  55. {
  56. free(rb->buf);
  57. }
  58. /* ------------------------------------------------------------------------- */
  59. struct main_params {
  60. char *file;
  61. int has_video;
  62. int tracks;
  63. char *vcodec;
  64. int vbitrate;
  65. int gop;
  66. int width;
  67. int height;
  68. int fps_num;
  69. int fps_den;
  70. char *acodec;
  71. char *muxer_settings;
  72. };
  73. struct audio_params {
  74. char *name;
  75. int abitrate;
  76. int sample_rate;
  77. int channels;
  78. };
  79. struct header {
  80. uint8_t *data;
  81. int size;
  82. };
  83. struct audio_info {
  84. AVStream *stream;
  85. AVCodecContext *ctx;
  86. };
  87. struct ffmpeg_mux {
  88. AVFormatContext *output;
  89. AVStream *video_stream;
  90. AVCodecContext *video_ctx;
  91. struct audio_info *audio_infos;
  92. struct main_params params;
  93. struct audio_params *audio;
  94. struct header video_header;
  95. struct header *audio_header;
  96. int num_audio_streams;
  97. bool initialized;
  98. char error[4096];
  99. };
  100. static void header_free(struct header *header)
  101. {
  102. free(header->data);
  103. }
  104. static void free_avformat(struct ffmpeg_mux *ffm)
  105. {
  106. if (ffm->output) {
  107. #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 48, 101)
  108. avcodec_free_context(&ffm->video_ctx);
  109. #endif
  110. if ((ffm->output->oformat->flags & AVFMT_NOFILE) == 0)
  111. avio_close(ffm->output->pb);
  112. avformat_free_context(ffm->output);
  113. ffm->output = NULL;
  114. }
  115. if (ffm->audio_infos) {
  116. #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 48, 101)
  117. for (int i = 0; i < ffm->num_audio_streams; ++i)
  118. avcodec_free_context(&ffm->audio_infos[i].ctx);
  119. #endif
  120. free(ffm->audio_infos);
  121. }
  122. ffm->video_stream = NULL;
  123. ffm->audio_infos = NULL;
  124. ffm->num_audio_streams = 0;
  125. }
  126. static void ffmpeg_mux_free(struct ffmpeg_mux *ffm)
  127. {
  128. if (ffm->initialized) {
  129. av_write_trailer(ffm->output);
  130. }
  131. free_avformat(ffm);
  132. header_free(&ffm->video_header);
  133. if (ffm->audio_header) {
  134. for (int i = 0; i < ffm->params.tracks; i++) {
  135. header_free(&ffm->audio_header[i]);
  136. }
  137. free(ffm->audio_header);
  138. }
  139. if (ffm->audio) {
  140. free(ffm->audio);
  141. }
  142. memset(ffm, 0, sizeof(*ffm));
  143. }
  144. static bool get_opt_str(int *p_argc, char ***p_argv, char **str,
  145. const char *opt)
  146. {
  147. int argc = *p_argc;
  148. char **argv = *p_argv;
  149. if (!argc) {
  150. printf("Missing expected option: '%s'\n", opt);
  151. return false;
  152. }
  153. (*p_argc)--;
  154. (*p_argv)++;
  155. *str = argv[0];
  156. return true;
  157. }
  158. static bool get_opt_int(int *p_argc, char ***p_argv, int *i, const char *opt)
  159. {
  160. char *str;
  161. if (!get_opt_str(p_argc, p_argv, &str, opt)) {
  162. return false;
  163. }
  164. *i = atoi(str);
  165. return true;
  166. }
  167. static bool get_audio_params(struct audio_params *audio, int *argc,
  168. char ***argv)
  169. {
  170. if (!get_opt_str(argc, argv, &audio->name, "audio track name"))
  171. return false;
  172. if (!get_opt_int(argc, argv, &audio->abitrate, "audio bitrate"))
  173. return false;
  174. if (!get_opt_int(argc, argv, &audio->sample_rate, "audio sample rate"))
  175. return false;
  176. if (!get_opt_int(argc, argv, &audio->channels, "audio channels"))
  177. return false;
  178. return true;
  179. }
  180. static bool init_params(int *argc, char ***argv, struct main_params *params,
  181. struct audio_params **p_audio)
  182. {
  183. struct audio_params *audio = NULL;
  184. if (!get_opt_str(argc, argv, &params->file, "file name"))
  185. return false;
  186. if (!get_opt_int(argc, argv, &params->has_video, "video track count"))
  187. return false;
  188. if (!get_opt_int(argc, argv, &params->tracks, "audio track count"))
  189. return false;
  190. if (params->has_video > 1 || params->has_video < 0) {
  191. puts("Invalid number of video tracks\n");
  192. return false;
  193. }
  194. if (params->tracks < 0) {
  195. puts("Invalid number of audio tracks\n");
  196. return false;
  197. }
  198. if (params->has_video == 0 && params->tracks == 0) {
  199. puts("Must have at least 1 audio track or 1 video track\n");
  200. return false;
  201. }
  202. if (params->has_video) {
  203. if (!get_opt_str(argc, argv, &params->vcodec, "video codec"))
  204. return false;
  205. if (!get_opt_int(argc, argv, &params->vbitrate,
  206. "video bitrate"))
  207. return false;
  208. if (!get_opt_int(argc, argv, &params->width, "video width"))
  209. return false;
  210. if (!get_opt_int(argc, argv, &params->height, "video height"))
  211. return false;
  212. if (!get_opt_int(argc, argv, &params->fps_num, "video fps num"))
  213. return false;
  214. if (!get_opt_int(argc, argv, &params->fps_den, "video fps den"))
  215. return false;
  216. }
  217. if (params->tracks) {
  218. if (!get_opt_str(argc, argv, &params->acodec, "audio codec"))
  219. return false;
  220. audio = calloc(params->tracks, sizeof(*audio));
  221. for (int i = 0; i < params->tracks; i++) {
  222. if (!get_audio_params(&audio[i], argc, argv)) {
  223. free(audio);
  224. return false;
  225. }
  226. }
  227. }
  228. *p_audio = audio;
  229. get_opt_str(argc, argv, &params->muxer_settings, "muxer settings");
  230. return true;
  231. }
  232. static bool new_stream(struct ffmpeg_mux *ffm, AVStream **stream,
  233. const char *name, AVCodec **codec)
  234. {
  235. const AVCodecDescriptor *desc = avcodec_descriptor_get_by_name(name);
  236. if (!desc) {
  237. fprintf(stderr, "Couldn't find encoder '%s'\n", name);
  238. return false;
  239. }
  240. *codec = avcodec_find_encoder(desc->id);
  241. if (!*codec) {
  242. fprintf(stderr, "Couldn't create encoder");
  243. return false;
  244. }
  245. *stream = avformat_new_stream(ffm->output, *codec);
  246. if (!*stream) {
  247. fprintf(stderr, "Couldn't create stream for encoder '%s'\n",
  248. name);
  249. return false;
  250. }
  251. (*stream)->id = ffm->output->nb_streams - 1;
  252. return true;
  253. }
  254. static void create_video_stream(struct ffmpeg_mux *ffm)
  255. {
  256. AVCodec *codec;
  257. AVCodecContext *context;
  258. void *extradata = NULL;
  259. if (!new_stream(ffm, &ffm->video_stream, ffm->params.vcodec, &codec))
  260. return;
  261. if (ffm->video_header.size) {
  262. extradata = av_memdup(ffm->video_header.data,
  263. ffm->video_header.size);
  264. }
  265. #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 48, 101)
  266. context = avcodec_alloc_context3(codec);
  267. #else
  268. context = ffm->video_stream->codec;
  269. #endif
  270. context->bit_rate = (int64_t)ffm->params.vbitrate * 1000;
  271. context->width = ffm->params.width;
  272. context->height = ffm->params.height;
  273. context->coded_width = ffm->params.width;
  274. context->coded_height = ffm->params.height;
  275. context->extradata = extradata;
  276. context->extradata_size = ffm->video_header.size;
  277. context->time_base =
  278. (AVRational){ffm->params.fps_den, ffm->params.fps_num};
  279. ffm->video_stream->time_base = context->time_base;
  280. ffm->video_stream->avg_frame_rate = av_inv_q(context->time_base);
  281. if (ffm->output->oformat->flags & AVFMT_GLOBALHEADER)
  282. context->flags |= CODEC_FLAG_GLOBAL_H;
  283. #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 48, 101)
  284. avcodec_parameters_from_context(ffm->video_stream->codecpar, context);
  285. #endif
  286. ffm->video_ctx = context;
  287. }
  288. static void create_audio_stream(struct ffmpeg_mux *ffm, int idx)
  289. {
  290. AVCodec *codec;
  291. AVCodecContext *context;
  292. AVStream *stream;
  293. void *extradata = NULL;
  294. if (!new_stream(ffm, &stream, ffm->params.acodec, &codec))
  295. return;
  296. av_dict_set(&stream->metadata, "title", ffm->audio[idx].name, 0);
  297. stream->time_base = (AVRational){1, ffm->audio[idx].sample_rate};
  298. if (ffm->audio_header[idx].size) {
  299. extradata = av_memdup(ffm->audio_header[idx].data,
  300. ffm->audio_header[idx].size);
  301. }
  302. #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 48, 101)
  303. context = avcodec_alloc_context3(codec);
  304. #else
  305. context = stream->codec;
  306. #endif
  307. context->bit_rate = (int64_t)ffm->audio[idx].abitrate * 1000;
  308. context->channels = ffm->audio[idx].channels;
  309. context->sample_rate = ffm->audio[idx].sample_rate;
  310. context->sample_fmt = AV_SAMPLE_FMT_S16;
  311. context->time_base = stream->time_base;
  312. context->extradata = extradata;
  313. context->extradata_size = ffm->audio_header[idx].size;
  314. context->channel_layout =
  315. av_get_default_channel_layout(context->channels);
  316. //AVlib default channel layout for 4 channels is 4.0 ; fix for quad
  317. if (context->channels == 4)
  318. context->channel_layout = av_get_channel_layout("quad");
  319. //AVlib default channel layout for 5 channels is 5.0 ; fix for 4.1
  320. if (context->channels == 5)
  321. context->channel_layout = av_get_channel_layout("4.1");
  322. if (ffm->output->oformat->flags & AVFMT_GLOBALHEADER)
  323. context->flags |= CODEC_FLAG_GLOBAL_H;
  324. #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 48, 101)
  325. avcodec_parameters_from_context(stream->codecpar, context);
  326. #endif
  327. ffm->audio_infos[ffm->num_audio_streams].stream = stream;
  328. ffm->audio_infos[ffm->num_audio_streams].ctx = context;
  329. ffm->num_audio_streams++;
  330. }
  331. static bool init_streams(struct ffmpeg_mux *ffm)
  332. {
  333. if (ffm->params.has_video)
  334. create_video_stream(ffm);
  335. if (ffm->params.tracks) {
  336. ffm->audio_infos =
  337. calloc(ffm->params.tracks, sizeof(*ffm->audio_infos));
  338. for (int i = 0; i < ffm->params.tracks; i++)
  339. create_audio_stream(ffm, i);
  340. }
  341. if (!ffm->video_stream && !ffm->num_audio_streams)
  342. return false;
  343. return true;
  344. }
  345. static void set_header(struct header *header, uint8_t *data, size_t size)
  346. {
  347. header->size = (int)size;
  348. header->data = malloc(size);
  349. memcpy(header->data, data, size);
  350. }
  351. static void ffmpeg_mux_header(struct ffmpeg_mux *ffm, uint8_t *data,
  352. struct ffm_packet_info *info)
  353. {
  354. if (info->type == FFM_PACKET_VIDEO) {
  355. set_header(&ffm->video_header, data, (size_t)info->size);
  356. } else {
  357. set_header(&ffm->audio_header[info->index], data,
  358. (size_t)info->size);
  359. }
  360. }
  361. static size_t safe_read(void *vdata, size_t size)
  362. {
  363. uint8_t *data = vdata;
  364. size_t total = size;
  365. while (size > 0) {
  366. size_t in_size = fread(data, 1, size, stdin);
  367. if (in_size == 0)
  368. return 0;
  369. size -= in_size;
  370. data += in_size;
  371. }
  372. return total;
  373. }
  374. static bool ffmpeg_mux_get_header(struct ffmpeg_mux *ffm)
  375. {
  376. struct ffm_packet_info info = {0};
  377. bool success = safe_read(&info, sizeof(info)) == sizeof(info);
  378. if (success) {
  379. uint8_t *data = malloc(info.size);
  380. if (safe_read(data, info.size) == info.size) {
  381. ffmpeg_mux_header(ffm, data, &info);
  382. } else {
  383. success = false;
  384. }
  385. free(data);
  386. }
  387. return success;
  388. }
  389. static inline bool ffmpeg_mux_get_extra_data(struct ffmpeg_mux *ffm)
  390. {
  391. if (ffm->params.has_video) {
  392. if (!ffmpeg_mux_get_header(ffm)) {
  393. return false;
  394. }
  395. }
  396. for (int i = 0; i < ffm->params.tracks; i++) {
  397. if (!ffmpeg_mux_get_header(ffm)) {
  398. return false;
  399. }
  400. }
  401. return true;
  402. }
  403. #ifdef _MSC_VER
  404. #pragma warning(disable : 4996)
  405. #endif
  406. static inline int open_output_file(struct ffmpeg_mux *ffm)
  407. {
  408. AVOutputFormat *format = ffm->output->oformat;
  409. int ret;
  410. if ((format->flags & AVFMT_NOFILE) == 0) {
  411. ret = avio_open(&ffm->output->pb, ffm->params.file,
  412. AVIO_FLAG_WRITE);
  413. if (ret < 0) {
  414. fprintf(stderr, "Couldn't open '%s', %s",
  415. ffm->params.file, av_err2str(ret));
  416. return FFM_ERROR;
  417. }
  418. }
  419. AVDictionary *dict = NULL;
  420. if ((ret = av_dict_parse_string(&dict, ffm->params.muxer_settings, "=",
  421. " ", 0))) {
  422. fprintf(stderr, "Failed to parse muxer settings: %s\n%s",
  423. av_err2str(ret), ffm->params.muxer_settings);
  424. av_dict_free(&dict);
  425. }
  426. if (av_dict_count(dict) > 0) {
  427. printf("Using muxer settings:");
  428. AVDictionaryEntry *entry = NULL;
  429. while ((entry = av_dict_get(dict, "", entry,
  430. AV_DICT_IGNORE_SUFFIX)))
  431. printf("\n\t%s=%s", entry->key, entry->value);
  432. printf("\n");
  433. }
  434. ret = avformat_write_header(ffm->output, &dict);
  435. if (ret < 0) {
  436. fprintf(stderr, "Error opening '%s': %s", ffm->params.file,
  437. av_err2str(ret));
  438. av_dict_free(&dict);
  439. return ret == -22 ? FFM_UNSUPPORTED : FFM_ERROR;
  440. }
  441. av_dict_free(&dict);
  442. return FFM_SUCCESS;
  443. }
  444. #define SRT_PROTO "srt"
  445. #define UDP_PROTO "udp"
  446. #define TCP_PROTO "tcp"
  447. static int ffmpeg_mux_init_context(struct ffmpeg_mux *ffm)
  448. {
  449. AVOutputFormat *output_format;
  450. int ret;
  451. bool isNetwork = false;
  452. if (strncmp(ffm->params.file, SRT_PROTO, sizeof(SRT_PROTO) - 1) == 0 ||
  453. strncmp(ffm->params.file, UDP_PROTO, sizeof(UDP_PROTO) - 1) == 0 ||
  454. strncmp(ffm->params.file, TCP_PROTO, sizeof(TCP_PROTO) - 1) == 0)
  455. isNetwork = true;
  456. if (isNetwork) {
  457. avformat_network_init();
  458. output_format = av_guess_format("mpegts", NULL, "video/M2PT");
  459. } else {
  460. output_format = av_guess_format(NULL, ffm->params.file, NULL);
  461. }
  462. if (output_format == NULL) {
  463. fprintf(stderr, "Couldn't find an appropriate muxer for '%s'\n",
  464. ffm->params.file);
  465. return FFM_ERROR;
  466. }
  467. ret = avformat_alloc_output_context2(&ffm->output, output_format, NULL,
  468. ffm->params.file);
  469. if (ret < 0) {
  470. fprintf(stderr, "Couldn't initialize output context: %s\n",
  471. av_err2str(ret));
  472. return FFM_ERROR;
  473. }
  474. ffm->output->oformat->video_codec = AV_CODEC_ID_NONE;
  475. ffm->output->oformat->audio_codec = AV_CODEC_ID_NONE;
  476. if (!init_streams(ffm)) {
  477. free_avformat(ffm);
  478. return FFM_ERROR;
  479. }
  480. ret = open_output_file(ffm);
  481. if (ret != FFM_SUCCESS) {
  482. free_avformat(ffm);
  483. return ret;
  484. }
  485. return FFM_SUCCESS;
  486. }
  487. static int ffmpeg_mux_init_internal(struct ffmpeg_mux *ffm, int argc,
  488. char *argv[])
  489. {
  490. argc--;
  491. argv++;
  492. if (!init_params(&argc, &argv, &ffm->params, &ffm->audio))
  493. return FFM_ERROR;
  494. if (ffm->params.tracks) {
  495. ffm->audio_header =
  496. calloc(ffm->params.tracks, sizeof(*ffm->audio_header));
  497. }
  498. #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(58, 9, 100)
  499. av_register_all();
  500. #endif
  501. if (!ffmpeg_mux_get_extra_data(ffm))
  502. return FFM_ERROR;
  503. /* ffmpeg does not have a way of telling what's supported
  504. * for a given output format, so we try each possibility */
  505. return ffmpeg_mux_init_context(ffm);
  506. }
  507. static int ffmpeg_mux_init(struct ffmpeg_mux *ffm, int argc, char *argv[])
  508. {
  509. int ret = ffmpeg_mux_init_internal(ffm, argc, argv);
  510. if (ret != FFM_SUCCESS) {
  511. ffmpeg_mux_free(ffm);
  512. return ret;
  513. }
  514. ffm->initialized = true;
  515. return ret;
  516. }
  517. static inline int get_index(struct ffmpeg_mux *ffm,
  518. struct ffm_packet_info *info)
  519. {
  520. if (info->type == FFM_PACKET_VIDEO) {
  521. if (ffm->video_stream) {
  522. return ffm->video_stream->id;
  523. }
  524. } else {
  525. if ((int)info->index < ffm->num_audio_streams) {
  526. return ffm->audio_infos[info->index].stream->id;
  527. }
  528. }
  529. return -1;
  530. }
  531. static AVCodecContext *get_codec_context(struct ffmpeg_mux *ffm,
  532. struct ffm_packet_info *info)
  533. {
  534. if (info->type == FFM_PACKET_VIDEO) {
  535. if (ffm->video_stream) {
  536. return ffm->video_ctx;
  537. }
  538. } else {
  539. if ((int)info->index < ffm->num_audio_streams) {
  540. return ffm->audio_infos[info->index].ctx;
  541. }
  542. }
  543. return NULL;
  544. }
  545. static inline AVStream *get_stream(struct ffmpeg_mux *ffm, int idx)
  546. {
  547. return ffm->output->streams[idx];
  548. }
  549. static inline int64_t rescale_ts(struct ffmpeg_mux *ffm,
  550. AVRational codec_time_base, int64_t val,
  551. int idx)
  552. {
  553. AVStream *stream = get_stream(ffm, idx);
  554. return av_rescale_q_rnd(val / codec_time_base.num, codec_time_base,
  555. stream->time_base,
  556. AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX);
  557. }
  558. static inline bool ffmpeg_mux_packet(struct ffmpeg_mux *ffm, uint8_t *buf,
  559. struct ffm_packet_info *info)
  560. {
  561. int idx = get_index(ffm, info);
  562. AVPacket packet = {0};
  563. /* The muxer might not support video/audio, or multiple audio tracks */
  564. if (idx == -1) {
  565. return true;
  566. }
  567. const AVRational codec_time_base =
  568. get_codec_context(ffm, info)->time_base;
  569. av_init_packet(&packet);
  570. packet.data = buf;
  571. packet.size = (int)info->size;
  572. packet.stream_index = idx;
  573. packet.pts = rescale_ts(ffm, codec_time_base, info->pts, idx);
  574. packet.dts = rescale_ts(ffm, codec_time_base, info->dts, idx);
  575. if (info->keyframe)
  576. packet.flags = AV_PKT_FLAG_KEY;
  577. return av_interleaved_write_frame(ffm->output, &packet) >= 0;
  578. }
  579. /* ------------------------------------------------------------------------- */
  580. #ifdef _WIN32
  581. int wmain(int argc, wchar_t *argv_w[])
  582. #else
  583. int main(int argc, char *argv[])
  584. #endif
  585. {
  586. struct ffm_packet_info info = {0};
  587. struct ffmpeg_mux ffm = {0};
  588. struct resize_buf rb = {0};
  589. bool fail = false;
  590. int ret;
  591. #ifdef _WIN32
  592. char **argv;
  593. SetErrorMode(SEM_FAILCRITICALERRORS);
  594. argv = malloc(argc * sizeof(char *));
  595. for (int i = 0; i < argc; i++) {
  596. size_t len = wcslen(argv_w[i]);
  597. int size;
  598. size = WideCharToMultiByte(CP_UTF8, 0, argv_w[i], (int)len,
  599. NULL, 0, NULL, NULL);
  600. argv[i] = malloc(size + 1);
  601. WideCharToMultiByte(CP_UTF8, 0, argv_w[i], (int)len, argv[i],
  602. size + 1, NULL, NULL);
  603. argv[i][size] = 0;
  604. }
  605. _setmode(_fileno(stdin), O_BINARY);
  606. #endif
  607. setvbuf(stderr, NULL, _IONBF, 0);
  608. ret = ffmpeg_mux_init(&ffm, argc, argv);
  609. if (ret != FFM_SUCCESS) {
  610. fprintf(stderr, "Couldn't initialize muxer\n");
  611. return ret;
  612. }
  613. while (!fail && safe_read(&info, sizeof(info)) == sizeof(info)) {
  614. resize_buf_resize(&rb, info.size);
  615. if (safe_read(rb.buf, info.size) == info.size) {
  616. ffmpeg_mux_packet(&ffm, rb.buf, &info);
  617. } else {
  618. fail = true;
  619. }
  620. }
  621. ffmpeg_mux_free(&ffm);
  622. resize_buf_free(&rb);
  623. #ifdef _WIN32
  624. for (int i = 0; i < argc; i++)
  625. free(argv[i]);
  626. free(argv);
  627. #endif
  628. return 0;
  629. }