media-remux.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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. #if LIBAVCODEC_VERSION_MAJOR >= 58
  22. #define CODEC_FLAG_GLOBAL_H AV_CODEC_FLAG_GLOBAL_HEADER
  23. #else
  24. #define CODEC_FLAG_GLOBAL_H CODEC_FLAG_GLOBAL_HEADER
  25. #endif
  26. struct media_remux_job {
  27. int64_t in_size;
  28. AVFormatContext *ifmt_ctx, *ofmt_ctx;
  29. };
  30. static inline void init_size(media_remux_job_t job, const char *in_filename)
  31. {
  32. #ifdef _MSC_VER
  33. struct _stat64 st = {0};
  34. _stat64(in_filename, &st);
  35. #else
  36. struct stat st = {0};
  37. stat(in_filename, &st);
  38. #endif
  39. job->in_size = st.st_size;
  40. }
  41. static inline bool init_input(media_remux_job_t job, const char *in_filename)
  42. {
  43. int ret = avformat_open_input(&job->ifmt_ctx, in_filename, NULL, NULL);
  44. if (ret < 0) {
  45. blog(LOG_ERROR, "media_remux: Could not open input file '%s'",
  46. in_filename);
  47. return false;
  48. }
  49. ret = avformat_find_stream_info(job->ifmt_ctx, NULL);
  50. if (ret < 0) {
  51. blog(LOG_ERROR, "media_remux: Failed to retrieve input stream"
  52. " information");
  53. return false;
  54. }
  55. #ifndef NDEBUG
  56. av_dump_format(job->ifmt_ctx, 0, in_filename, false);
  57. #endif
  58. return true;
  59. }
  60. static inline bool init_output(media_remux_job_t job, const char *out_filename)
  61. {
  62. int ret;
  63. avformat_alloc_output_context2(&job->ofmt_ctx, NULL, NULL,
  64. out_filename);
  65. if (!job->ofmt_ctx) {
  66. blog(LOG_ERROR, "media_remux: Could not create output context");
  67. return false;
  68. }
  69. for (unsigned i = 0; i < job->ifmt_ctx->nb_streams; i++) {
  70. AVStream *in_stream = job->ifmt_ctx->streams[i];
  71. #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 48, 101)
  72. AVStream *out_stream = avformat_new_stream(job->ofmt_ctx, NULL);
  73. #else
  74. AVStream *out_stream = avformat_new_stream(
  75. job->ofmt_ctx, in_stream->codec->codec);
  76. #endif
  77. if (!out_stream) {
  78. blog(LOG_ERROR, "media_remux: Failed to allocate output"
  79. " stream");
  80. return false;
  81. }
  82. #if FF_API_BUFFER_SIZE_T
  83. int content_size;
  84. #else
  85. size_t content_size;
  86. #endif
  87. const uint8_t *const content_src = av_stream_get_side_data(
  88. in_stream, AV_PKT_DATA_CONTENT_LIGHT_LEVEL,
  89. &content_size);
  90. if (content_src) {
  91. uint8_t *const content_dst = av_stream_new_side_data(
  92. out_stream, AV_PKT_DATA_CONTENT_LIGHT_LEVEL,
  93. content_size);
  94. if (content_dst)
  95. memcpy(content_dst, content_src, content_size);
  96. }
  97. #if FF_API_BUFFER_SIZE_T
  98. int mastering_size;
  99. #else
  100. size_t mastering_size;
  101. #endif
  102. const uint8_t *const mastering_src = av_stream_get_side_data(
  103. in_stream, AV_PKT_DATA_MASTERING_DISPLAY_METADATA,
  104. &mastering_size);
  105. if (mastering_src) {
  106. uint8_t *const mastering_dst = av_stream_new_side_data(
  107. out_stream,
  108. AV_PKT_DATA_MASTERING_DISPLAY_METADATA,
  109. mastering_size);
  110. if (mastering_dst) {
  111. memcpy(mastering_dst, mastering_src,
  112. mastering_size);
  113. }
  114. }
  115. #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 48, 101)
  116. ret = avcodec_parameters_copy(out_stream->codecpar,
  117. in_stream->codecpar);
  118. #else
  119. ret = avcodec_copy_context(out_stream->codec, in_stream->codec);
  120. #endif
  121. if (ret < 0) {
  122. blog(LOG_ERROR,
  123. "media_remux: Failed to copy parameters");
  124. return false;
  125. }
  126. av_dict_copy(&out_stream->metadata, in_stream->metadata, 0);
  127. #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 48, 101)
  128. out_stream->codecpar->codec_tag = 0;
  129. #else
  130. out_stream->codec->codec_tag = 0;
  131. out_stream->time_base = out_stream->codec->time_base;
  132. if (job->ofmt_ctx->oformat->flags & AVFMT_GLOBALHEADER)
  133. out_stream->codec->flags |= CODEC_FLAG_GLOBAL_H;
  134. #endif
  135. }
  136. #ifndef NDEBUG
  137. av_dump_format(job->ofmt_ctx, 0, out_filename, true);
  138. #endif
  139. if (!(job->ofmt_ctx->oformat->flags & AVFMT_NOFILE)) {
  140. ret = avio_open(&job->ofmt_ctx->pb, out_filename,
  141. AVIO_FLAG_WRITE);
  142. if (ret < 0) {
  143. blog(LOG_ERROR,
  144. "media_remux: Failed to open output"
  145. " file '%s'",
  146. out_filename);
  147. return false;
  148. }
  149. }
  150. return true;
  151. }
  152. bool media_remux_job_create(media_remux_job_t *job, const char *in_filename,
  153. const char *out_filename)
  154. {
  155. if (!job)
  156. return false;
  157. *job = NULL;
  158. if (!os_file_exists(in_filename))
  159. return false;
  160. if (strcmp(in_filename, out_filename) == 0)
  161. return false;
  162. *job = (media_remux_job_t)bzalloc(sizeof(struct media_remux_job));
  163. if (!*job)
  164. return false;
  165. init_size(*job, in_filename);
  166. #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(58, 9, 100)
  167. av_register_all();
  168. #endif
  169. if (!init_input(*job, in_filename))
  170. goto fail;
  171. if (!init_output(*job, out_filename))
  172. goto fail;
  173. return true;
  174. fail:
  175. media_remux_job_destroy(*job);
  176. return false;
  177. }
  178. static inline void process_packet(AVPacket *pkt, AVStream *in_stream,
  179. AVStream *out_stream)
  180. {
  181. pkt->pts = av_rescale_q_rnd(pkt->pts, in_stream->time_base,
  182. out_stream->time_base,
  183. AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX);
  184. pkt->dts = av_rescale_q_rnd(pkt->dts, in_stream->time_base,
  185. out_stream->time_base,
  186. AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX);
  187. pkt->duration = (int)av_rescale_q(pkt->duration, in_stream->time_base,
  188. out_stream->time_base);
  189. pkt->pos = -1;
  190. }
  191. static inline int process_packets(media_remux_job_t job,
  192. media_remux_progress_callback callback,
  193. void *data)
  194. {
  195. AVPacket pkt;
  196. int ret, throttle = 0;
  197. for (;;) {
  198. ret = av_read_frame(job->ifmt_ctx, &pkt);
  199. if (ret < 0) {
  200. if (ret != AVERROR_EOF)
  201. blog(LOG_ERROR,
  202. "media_remux: Error reading"
  203. " packet: %s",
  204. av_err2str(ret));
  205. break;
  206. }
  207. if (callback != NULL && throttle++ > 10) {
  208. float progress = pkt.pos / (float)job->in_size * 100.f;
  209. if (!callback(data, progress))
  210. break;
  211. throttle = 0;
  212. }
  213. process_packet(&pkt, job->ifmt_ctx->streams[pkt.stream_index],
  214. job->ofmt_ctx->streams[pkt.stream_index]);
  215. ret = av_interleaved_write_frame(job->ofmt_ctx, &pkt);
  216. av_packet_unref(&pkt);
  217. if (ret < 0) {
  218. blog(LOG_ERROR, "media_remux: Error muxing packet: %s",
  219. av_err2str(ret));
  220. /* Treat "Invalid data found when processing input" and
  221. * "Invalid argument" as non-fatal */
  222. if (ret == AVERROR_INVALIDDATA || ret == -EINVAL)
  223. continue;
  224. break;
  225. }
  226. }
  227. return ret;
  228. }
  229. bool media_remux_job_process(media_remux_job_t job,
  230. media_remux_progress_callback callback, void *data)
  231. {
  232. int ret;
  233. bool success = false;
  234. if (!job)
  235. return success;
  236. ret = avformat_write_header(job->ofmt_ctx, NULL);
  237. if (ret < 0) {
  238. blog(LOG_ERROR, "media_remux: Error opening output file: %s",
  239. av_err2str(ret));
  240. return success;
  241. }
  242. if (callback != NULL)
  243. callback(data, 0.f);
  244. ret = process_packets(job, callback, data);
  245. success = ret >= 0 || ret == AVERROR_EOF;
  246. ret = av_write_trailer(job->ofmt_ctx);
  247. if (ret < 0) {
  248. blog(LOG_ERROR, "media_remux: av_write_trailer: %s",
  249. av_err2str(ret));
  250. success = false;
  251. }
  252. if (callback != NULL)
  253. callback(data, 100.f);
  254. return success;
  255. }
  256. void media_remux_job_destroy(media_remux_job_t job)
  257. {
  258. if (!job)
  259. return;
  260. avformat_close_input(&job->ifmt_ctx);
  261. if (job->ofmt_ctx && !(job->ofmt_ctx->oformat->flags & AVFMT_NOFILE))
  262. avio_close(job->ofmt_ctx->pb);
  263. avformat_free_context(job->ofmt_ctx);
  264. bfree(job);
  265. }