media-remux.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /******************************************************************************
  2. Copyright (C) 2014 by Ruwen Hahn <[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 "media-remux.h"
  15. #include "../util/base.h"
  16. #include "../util/bmem.h"
  17. #include "../util/platform.h"
  18. #include <libavformat/avformat.h>
  19. #include <sys/types.h>
  20. #include <sys/stat.h>
  21. struct media_remux_job {
  22. int64_t in_size;
  23. AVFormatContext *ifmt_ctx, *ofmt_ctx;
  24. };
  25. static inline void init_size(media_remux_job_t job, const char *in_filename)
  26. {
  27. #ifdef _MSC_VER
  28. struct _stat64 st = {0};
  29. _stat64(in_filename, &st);
  30. #else
  31. struct stat st = {0};
  32. stat(in_filename, &st);
  33. #endif
  34. job->in_size = st.st_size;
  35. }
  36. static inline bool init_input(media_remux_job_t job, const char *in_filename)
  37. {
  38. int ret = avformat_open_input(&job->ifmt_ctx, in_filename, NULL, NULL);
  39. if (ret < 0) {
  40. blog(LOG_ERROR, "media_remux: Could not open input file '%s'",
  41. in_filename);
  42. return false;
  43. }
  44. ret = avformat_find_stream_info(job->ifmt_ctx, NULL);
  45. if (ret < 0) {
  46. blog(LOG_ERROR, "media_remux: Failed to retrieve input stream"
  47. " information");
  48. return false;
  49. }
  50. #ifndef _NDEBUG
  51. av_dump_format(job->ifmt_ctx, 0, in_filename, false);
  52. #endif
  53. return true;
  54. }
  55. static inline bool init_output(media_remux_job_t job, const char *out_filename)
  56. {
  57. int ret;
  58. avformat_alloc_output_context2(&job->ofmt_ctx, NULL, NULL,
  59. out_filename);
  60. if (!job->ofmt_ctx) {
  61. blog(LOG_ERROR, "media_remux: Could not create output context");
  62. return false;
  63. }
  64. for (unsigned i = 0; i < job->ifmt_ctx->nb_streams; i++) {
  65. AVStream *in_stream = job->ifmt_ctx->streams[i];
  66. AVStream *out_stream = avformat_new_stream(job->ofmt_ctx,
  67. in_stream->codec->codec);
  68. if (!out_stream) {
  69. blog(LOG_ERROR, "media_remux: Failed to allocate output"
  70. " stream");
  71. return false;
  72. }
  73. ret = avcodec_copy_context(out_stream->codec, in_stream->codec);
  74. if (ret < 0) {
  75. blog(LOG_ERROR, "media_remux: Failed to copy context");
  76. return false;
  77. }
  78. out_stream->time_base = out_stream->codec->time_base;
  79. out_stream->codec->codec_tag = 0;
  80. if (job->ofmt_ctx->oformat->flags & AVFMT_GLOBALHEADER)
  81. out_stream->codec->flags |= CODEC_FLAG_GLOBAL_HEADER;
  82. }
  83. #ifndef _NDEBUG
  84. av_dump_format(job->ofmt_ctx, 0, out_filename, true);
  85. #endif
  86. if (!(job->ofmt_ctx->oformat->flags & AVFMT_NOFILE)) {
  87. ret = avio_open(&job->ofmt_ctx->pb, out_filename,
  88. AVIO_FLAG_WRITE);
  89. if (ret < 0) {
  90. blog(LOG_ERROR, "media_remux: Failed to open output"
  91. " file '%s'", out_filename);
  92. return false;
  93. }
  94. }
  95. return true;
  96. }
  97. bool media_remux_job_create(media_remux_job_t *job, const char *in_filename,
  98. const char *out_filename)
  99. {
  100. if (!job)
  101. return false;
  102. *job = NULL;
  103. if (!os_file_exists(in_filename))
  104. return false;
  105. *job = (media_remux_job_t)bzalloc(sizeof(struct media_remux_job));
  106. if (!*job)
  107. return false;
  108. init_size(*job, in_filename);
  109. av_register_all();
  110. if (!init_input(*job, in_filename))
  111. goto fail;
  112. if (!init_output(*job, out_filename))
  113. goto fail;
  114. return true;
  115. fail:
  116. media_remux_job_destroy(*job);
  117. return false;
  118. }
  119. static inline void process_packet(AVPacket *pkt,
  120. AVStream *in_stream, AVStream *out_stream)
  121. {
  122. pkt->pts = av_rescale_q_rnd(pkt->pts, in_stream->time_base,
  123. out_stream->time_base,
  124. AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX);
  125. pkt->dts = av_rescale_q_rnd(pkt->dts, in_stream->time_base,
  126. out_stream->time_base,
  127. AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX);
  128. pkt->duration = (int)av_rescale_q(pkt->duration,
  129. in_stream->time_base, out_stream->time_base);
  130. pkt->pos = -1;
  131. }
  132. static inline int process_packets(media_remux_job_t job,
  133. media_remux_progress_callback callback, void *data)
  134. {
  135. AVPacket pkt;
  136. int ret, throttle = 0;
  137. for (;;) {
  138. ret = av_read_frame(job->ifmt_ctx, &pkt);
  139. if (ret < 0) {
  140. if (ret != AVERROR_EOF)
  141. blog(LOG_ERROR, "media_remux: Error reading"
  142. " packet: %s",
  143. av_err2str(ret));
  144. break;
  145. }
  146. if (callback != NULL && throttle++ > 10) {
  147. float progress = pkt.pos / (float)job->in_size * 100.f;
  148. if (!callback(data, progress))
  149. break;
  150. throttle = 0;
  151. }
  152. process_packet(&pkt, job->ifmt_ctx->streams[pkt.stream_index],
  153. job->ofmt_ctx->streams[pkt.stream_index]);
  154. ret = av_interleaved_write_frame(job->ofmt_ctx, &pkt);
  155. av_free_packet(&pkt);
  156. if (ret < 0) {
  157. blog(LOG_ERROR, "media_remux: Error muxing packet: %s",
  158. av_err2str(ret));
  159. break;
  160. }
  161. }
  162. return ret;
  163. }
  164. bool media_remux_job_process(media_remux_job_t job,
  165. media_remux_progress_callback callback, void *data)
  166. {
  167. int ret;
  168. bool success = false;
  169. if (!job)
  170. return success;
  171. ret = avformat_write_header(job->ofmt_ctx, NULL);
  172. if (ret < 0) {
  173. blog(LOG_ERROR, "media_remux: Error opening output file: %s",
  174. av_err2str(ret));
  175. return success;
  176. }
  177. if (callback != NULL)
  178. callback(data, 0.f);
  179. ret = process_packets(job, callback, data);
  180. success = ret >= 0 || ret == AVERROR_EOF;
  181. ret = av_write_trailer(job->ofmt_ctx);
  182. if (ret < 0) {
  183. blog(LOG_ERROR, "media_remux: av_write_trailer: %s",
  184. av_err2str(ret));
  185. success = false;
  186. }
  187. if (callback != NULL)
  188. callback(data, 100.f);
  189. return success;
  190. }
  191. void media_remux_job_destroy(media_remux_job_t job)
  192. {
  193. if (!job)
  194. return;
  195. avformat_close_input(&job->ifmt_ctx);
  196. if (job->ofmt_ctx && !(job->ofmt_ctx->oformat->flags & AVFMT_NOFILE))
  197. avio_close(job->ofmt_ctx->pb);
  198. avformat_free_context(job->ofmt_ctx);
  199. bfree(job);
  200. }